RAD-tools splits into WULFRIC Read more here

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 │
└────────────────┴──────┘

Headers and footers#

>>> array = [[1, 2], [3, 4], [5, 6]]
>>> print_2d_array(array, header_row=["a", "b"])
┌──────┬──────┐
│    a │    b │
├──────┼──────┤
│ 1.00 │ 2.00 │
├──────┼──────┤
│ 3.00 │ 4.00 │
├──────┼──────┤
│ 5.00 │ 6.00 │
└──────┴──────┘
>>> print_2d_array(array, footer_row=["c", "d"])
┌──────┬──────┐
│ 1.00 │ 2.00 │
├──────┼──────┤
│ 3.00 │ 4.00 │
├──────┼──────┤
│ 5.00 │ 6.00 │
├──────┼──────┤
│    c │    d │
└──────┴──────┘
>>> print_2d_array(array, header_column=["a", "b", "c"])
┌───┬──────┬──────┐
│ a │ 1.00 │ 2.00 │
├───┼──────┼──────┤
│ b │ 3.00 │ 4.00 │
├───┼──────┼──────┤
│ c │ 5.00 │ 6.00 │
└───┴──────┴──────┘
>>> print_2d_array(array, footer_column=["a", "b", "c"])
┌──────┬──────┬───┐
│ 1.00 │ 2.00 │ a │
├──────┼──────┼───┤
│ 3.00 │ 4.00 │ b │
├──────┼──────┼───┤
│ 5.00 │ 6.00 │ c │
└──────┴──────┴───┘
>>> print_2d_array(array, header_column=["a", "B", "c"], header_row = ["", "A", "B"])
┌───┬──────┬──────┐
│   │    A │    B │
├───┼──────┼──────┤
│ a │ 1.00 │ 2.00 │
├───┼──────┼──────┤
│ B │ 3.00 │ 4.00 │
├───┼──────┼──────┤
│ c │ 5.00 │ 6.00 │
└───┴──────┴──────┘
>>> print_2d_array(array, header_column=["a", "B", "c"], header_row = ["corner", "A", "B"])
┌────────┬──────┬──────┐
│ corner │    A │    B │
├────────┼──────┼──────┤
│      a │ 1.00 │ 2.00 │
├────────┼──────┼──────┤
│      B │ 3.00 │ 4.00 │
├────────┼──────┼──────┤
│      c │ 5.00 │ 6.00 │
└────────┴──────┴──────┘
>>> print_2d_array(array, header_column=["a", "B", "c"], header_row = ["", "A", "B",""], footer_row = ["","c","d",""], footer_column=["c","d","e"])
┌───┬──────┬──────┬───┐
│   │    A │    B │   │
├───┼──────┼──────┼───┤
│ a │ 1.00 │ 2.00 │ c │
├───┼──────┼──────┼───┤
│ B │ 3.00 │ 4.00 │ d │
├───┼──────┼──────┼───┤
│ c │ 5.00 │ 6.00 │ e │
├───┼──────┼──────┼───┤
│   │    c │    d │   │
└───┴──────┴──────┴───┘
>>> array = [[1, 2], [3, 4], [5, 6+1j]]
>>> print_2d_array(array, header_row=["a", "b"])
┌──────┬──────────────┐
│    a │            b │
├──────┼──────────────┤
│ 1.00 │ 2.00         │
├──────┼──────────────┤
│ 3.00 │ 4.00         │
├──────┼──────────────┤
│ 5.00 │ 6.00 + i1.00 │
└──────┴──────────────┘
>>> print_2d_array(array, header_row=["a", "b"], fmt="^.2f")
┌──────┬──────────────┐
│  a   │      b       │
├──────┼──────────────┤
│ 1.00 │ 2.00         │
├──────┼──────────────┤
│ 3.00 │ 4.00         │
├──────┼──────────────┤
│ 5.00 │ 6.00 + i1.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:

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