Representations and Tables
Contents
Representations and Tables#
Scipp provides a number of options for visualizing the structure and contents of variables, data arrays, and datasets in Jupyter notebooks:
scipp.to_html
produces a HTML representation. This is also bound to_repr_html_
, i.e., Jupyter will display this when the name of a scipp object is typed at the end of a cell.scipp.show
draws a SVG representation of the contained items and their shapes.scipp.table
outputs a table representation of 0-D and 1-D data.
In all cases, these can be called with variable, data array, and dataset, as well as slices or items of any of the former. While the outputs are mostly self-explanatory we discuss some details below.
HTML represenation#
scipp.to_html
is used to define _repr_html_
. This special property is used by Jupyter in place of __repr__
.
[1]:
import numpy as np
import scipp as sc
[2]:
x = sc.zeros(dims=['x'], shape=(2,))
y = sc.zeros(dims=['y'], unit=sc.units.m, shape=(4,))
labels = sc.zeros(dims=['y'], shape=(4,))
d = sc.Dataset(
data={'a':sc.zeros(dims=['y', 'x'], shape=(3, 2), with_variances=True, unit=sc.units.angstrom)},
coords={'x':x, 'y':y, 'y_label':labels})
d['b'] = d['a']
d['c'] = 1.0 * sc.units.kg
d['a'].attrs['x_attr'] = sc.zeros(dims=['x'], shape=(2,))
d['b'].attrs['x_attr'] = sc.zeros(dims=['x'], shape=(2,))
d['b'].attrs['b_attr'] = 1.2 * sc.units.m
Simply typing the name of a variable, data array, or dataset will show the HTML representation:
[3]:
d
[3]:
- y: 3
- x: 2
- x(x)float64𝟙0.0, 0.0
Values:
array([0., 0.]) - y(y [bin-edge])float64m0.0, 0.0, 0.0, 0.0
Values:
array([0., 0., 0., 0.]) - y_label(y [bin-edge])float64𝟙0.0, 0.0, 0.0, 0.0
Values:
array([0., 0., 0., 0.])
- a(y, x)float64Å0.0, 0.0, ..., 0.0, 0.0σ = 0.0, 0.0, ..., 0.0, 0.0
- x_attr(x)float64𝟙0.0, 0.0
Values:
array([0., 0.])
Values:
array([[0., 0.], [0., 0.], [0., 0.]])
Variances (σ²):
array([[0., 0.], [0., 0.], [0., 0.]]) - b(y, x)float64Å0.0, 0.0, ..., 0.0, 0.0σ = 0.0, 0.0, ..., 0.0, 0.0
- b_attr()float64m1.2
Values:
array(1.2) - x_attr(x)float64𝟙0.0, 0.0
Values:
array([0., 0.])
Values:
array([[0., 0.], [0., 0.], [0., 0.]])
Variances (σ²):
array([[0., 0.], [0., 0.], [0., 0.]]) - c()float64kg1.0
Values:
array(1.)
Note that (as usual) Jupyter only shows the last variable mentioned in a cell:
[4]:
a = 1
d
a
[4]:
1
In this case, to_html
can be used to retain the HTML view, e.g., to show multiple objects in a single cell:
[5]:
sc.to_html(d['a'])
sc.to_html(d['c'])
- y: 3
- x: 2
- x(x)float64𝟙0.0, 0.0
Values:
array([0., 0.]) - y(y [bin-edge])float64m0.0, 0.0, 0.0, 0.0
Values:
array([0., 0., 0., 0.]) - y_label(y [bin-edge])float64𝟙0.0, 0.0, 0.0, 0.0
Values:
array([0., 0., 0., 0.])
- (y, x)float64Å0.0, 0.0, ..., 0.0, 0.0σ = 0.0, 0.0, ..., 0.0, 0.0
Values:
array([[0., 0.], [0., 0.], [0., 0.]])
Variances (σ²):
array([[0., 0.], [0., 0.], [0., 0.]])
- x_attr(x)float64𝟙0.0, 0.0
Values:
array([0., 0.])
- ()float64kg1.0
Values:
array(1.)
Typing the scipp module name at the end of a cell yields an HTML view of all scipp objects (variables, data arrays, and datasets):
[6]:
sc
Variables:(3)
labels
- (y: 4)float64𝟙0.0, 0.0, 0.0, 0.0
Values:
array([0., 0., 0., 0.])
x
- (x: 2)float64𝟙0.0, 0.0
Values:
array([0., 0.])
y
- (y: 4)float64m0.0, 0.0, 0.0, 0.0
Values:
array([0., 0., 0., 0.])
DataArrays:(0)
Datasets:(1)
d
- y: 3
- x: 2
- x(x)float64𝟙0.0, 0.0
Values:
array([0., 0.]) - y(y [bin-edge])float64m0.0, 0.0, 0.0, 0.0
Values:
array([0., 0., 0., 0.]) - y_label(y [bin-edge])float64𝟙0.0, 0.0, 0.0, 0.0
Values:
array([0., 0., 0., 0.])
- a(y, x)float64Å0.0, 0.0, ..., 0.0, 0.0σ = 0.0, 0.0, ..., 0.0, 0.0
- x_attr(x)float64𝟙0.0, 0.0
Values:
array([0., 0.])
Values:
array([[0., 0.], [0., 0.], [0., 0.]])
Variances (σ²):
array([[0., 0.], [0., 0.], [0., 0.]]) - b(y, x)float64Å0.0, 0.0, ..., 0.0, 0.0σ = 0.0, 0.0, ..., 0.0, 0.0
- b_attr()float64m1.2
Values:
array(1.2) - x_attr(x)float64𝟙0.0, 0.0
Values:
array([0., 0.])
Values:
array([[0., 0.], [0., 0.], [0., 0.]])
Variances (σ²):
array([[0., 0.], [0., 0.], [0., 0.]]) - c()float64kg1.0
Values:
array(1.)
[6]:
<module 'scipp' from '/home/simon/code/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/__init__.py'>
SVG representation#
scipp.show
works similar to scipp.to_html
. It should be noted that if a dimension extent is large, show
will truncate it to avoid generation of massive and unreadable SVGs. Objects with more than three dimensions are not supported and will result in an error message.
[7]:
sc.show(d)
Table representation#
scipp.table
works similar to scipp.to_html
and scipp.show
. Objects with more than one dimension per variable are not supported and are omitted in the output:
[8]:
sc.table(d)
In such cases slicing can be used to produce tables from higher-dimensional entries:
[9]:
sc.table(d['a']['x', 0])