Decoration of data#
For the full reference see decorate.
This module contains functions to decorate data for plotting or printing. It does not fall into the scope of intended functionality of the package, but is included for convenience.
Import#
>>> # Exact import
>>> from radtools.decorate.array import print_2d_array
>>> # Explicit import
>>> from radtools.decorate import print_2d_array
>>> # Recommended import
>>> from radtools import print_2d_array
2D arrays#
This function can take any numerical 2D or 1D array and print it in a nice format. It is useful for debugging and printing of results.
It provides custom formatting, colour highlighting:
Hint
For colour highlighting pass highlight=True
to the function.
For colour highlighting to work, the terminal must support ANSI escape sequences.
It highlights positive values in red, negative values in blue, zero values in green. Complex and real parts of complex numbers are highlighted separately.
Real-valued array#
>>> array = [[1, 2], [3, 4], [5, 6]]
>>> print_2d_array(array)
┌──────┬──────┐
│ 1.00 │ 2.00 │
├──────┼──────┤
│ 3.00 │ 4.00 │
├──────┼──────┤
│ 5.00 │ 6.00 │
└──────┴──────┘
>>> print_2d_array([[0, 1., -0.],[1, 1, 1]])
┌──────┬──────┬──────┐
│ 0.00 │ 1.00 │ 0.00 │
├──────┼──────┼──────┤
│ 1.00 │ 1.00 │ 1.00 │
└──────┴──────┴──────┘
Custom formatting#
>>> print_2d_array(array, fmt="10.2f")
┌────────────┬────────────┐
│ 1.00 │ 2.00 │
├────────────┼────────────┤
│ 3.00 │ 4.00 │
├────────────┼────────────┤
│ 5.00 │ 6.00 │
└────────────┴────────────┘
>>> print_2d_array(array, fmt=".2f")
┌──────┬──────┐
│ 1.00 │ 2.00 │
├──────┼──────┤
│ 3.00 │ 4.00 │
├──────┼──────┤
│ 5.00 │ 6.00 │
└──────┴──────┘
No borders#
>>> print_2d_array(array, borders=False)
1.00 2.00
3.00 4.00
5.00 6.00
Shift#
>>> print_2d_array(array, shift=3)
┌──────┬──────┐
│ 1.00 │ 2.00 │
├──────┼──────┤
│ 3.00 │ 4.00 │
├──────┼──────┤
│ 5.00 │ 6.00 │
└──────┴──────┘
Scientific notation#
>>> array = [[1, 2], [3, 4], [52414345345, 6]]
>>> print_2d_array(array, fmt="10.2E")
┌────────────┬────────────┐
│ 1.00E+00 │ 2.00E+00 │
├────────────┼────────────┤
│ 3.00E+00 │ 4.00E+00 │
├────────────┼────────────┤
│ 5.24E+10 │ 6.00E+00 │
└────────────┴────────────┘
Complex-valued array#
>>> array = [[1, 2 + 1j], [3, 4], [52, 6]]
>>> print_2d_array(array)
┌───────┬──────────────┐
│ 1.00 │ 2.00 + i1.00 │
├───────┼──────────────┤
│ 3.00 │ 4.00 │
├───────┼──────────────┤
│ 52.00 │ 6.00 │
└───────┴──────────────┘
>>> print_2d_array(array, fmt="4.2E")
┌──────────┬──────────────────────┐
│ 1.00E+00 │ 2.00E+00 + i1.00E+00 │
├──────────┼──────────────────────┤
│ 3.00E+00 │ 4.00E+00 │
├──────────┼──────────────────────┤
│ 5.20E+01 │ 6.00E+00 │
└──────────┴──────────────────────┘
>>> array = [[1, 2 - 1j], [3, 4], [52, 6]]
>>> print_2d_array(array)
┌───────┬──────────────┐
│ 1.00 │ 2.00 - i1.00 │
├───────┼──────────────┤
│ 3.00 │ 4.00 │
├───────┼──────────────┤
│ 52.00 │ 6.00 │
└───────┴──────────────┘
Complex-valued array with real part equal to zero#
>>> array = [[1, 1j], [3, 4], [52, 6]]
>>> print_2d_array(array)
┌───────┬──────────────┐
│ 1.00 │ + i1.00 │
├───────┼──────────────┤
│ 3.00 │ 4.00 │
├───────┼──────────────┤
│ 52.00 │ 6.00 │
└───────┴──────────────┘
Empty cells#
>>> array = [[1, 2], [3, 4], [5, 6]]
>>> array[1][1] = None
>>> print_2d_array(array)
┌──────┬──────┐
│ 1.00 │ 2.00 │
├──────┼──────┤
│ 3.00 │ │
├──────┼──────┤
│ 5.00 │ 6.00 │
└──────┴──────┘
>>> array[1][0] = None
>>> print_2d_array(array)
┌──────┬──────┐
│ 1.00 │ 2.00 │
├──────┼──────┤
│ │ │
├──────┼──────┤
│ 5.00 │ 6.00 │
└──────┴──────┘
>>> array[0][1] = None
>>> array[2][1] = None
>>> print_2d_array(array)
┌──────┬──┐
│ 1.00 │ │
├──────┼──┤
│ │ │
├──────┼──┤
│ 5.00 │ │
└──────┴──┘
Empty arrays#
>>> print_2d_array([])
None
>>> print_2d_array([[]])
None
Long numbers#
>>> array = [[1, 2], [3, 4], [52414345345, 6]]
>>> print_2d_array(array)
┌────────────────┬──────┐
│ 1.00 │ 2.00 │
├────────────────┼──────┤
│ 3.00 │ 4.00 │
├────────────────┼──────┤
│ 52414345345.00 │ 6.00 │
└────────────────┴──────┘
Axis lines#
Two shortcuts with the common linestyle are defined for convenience:
>>> from radtools import plot_hlines, plot_vlines
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots()
>>> plot_hlines(ax, [1, 2, 3])
>>> plot_vlines(ax, [1, 2, 3])
>>> # You can pass any keyword arguments to the underlying matplotlib function
>>> plot_hlines(ax, [1, 2, 3], color="red", linewidth=2)
Colormap#
Another shortcut is defined for the custom colormap:
>>> from radtools import custom_cmap
>>> colormap = custom_cmap("#e0218a", "#2e1b25")
Logo and stats#
Three functions are defined for printing the logo and stats of the package:
Logo#
>>> from radtools import logo
>>> print(logo())
██████╗ █████╗ ██████╗ ████████╗ █████╗ █████╗ ██╗ ██████╗
██╔══██╗██╔══██╗██╔══██╗ ╚══██╔══╝██╔══██╗██╔══██╗██║ ██╔════╝
██████╔╝███████║██║ ██║█████╗ ██║ ██║ ██║██║ ██║██║ ╚═█████╗
██╔══██╗██╔══██║██║ ██║╚════╝ ██║ ██║ ██║██║ ██║██║ ╚══██║
██║ ██║██║ ██║██████╔╝ ██║ ╚█████╔╝╚█████╔╝███████╗██████╔╝
╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚═╝ ╚════╝ ╚════╝ ╚══════╝╚═════╝
▄ ▄
Version: 0.8.4 █▀█▀█
Documentation: rad-tools.org █▄█▄█
Release date: 20 September 2023 ███ ▄▄
Git hash: 5b4dc9b04aecb7efebb9cd710c02e0ad7fb68e44 ████ █ █
Licence: GNU GPLv3 ████ █
▀▀▀▀▀▀▀▀
The same behaviour can be achieved within the console (you may need to use python3
):
python -m radtools
One-line summary#
>>> from radtools import stamp_line
>>> print(stamp_line())
on 9 August 2023 at 16:50:17 by rad-tools 0.8.0
>>> print(stamp_line(doclink=True))
on 9 August 2023 at 16:51:0 by rad-tools 0.8.0 Documentation: rad-tools.org
License#
>>> from radtools import license
>>> print(license())
This code prints the full text of the GNU GPLv3 license.
The same behaviour can be achieved within the console (you may need to use python3
):
python -m radtools --license