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]:
Show/Hide data repr Show/Hide attributes
scipp.Dataset (320 Bytes)
    • y: 3
    • x: 2
    • x
      (x)
      float64
      𝟙
      0.0, 0.0
      Values:
      array([0., 0.])
    • y
      (y [bin-edge])
      float64
      m
      0.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
          ()
          float64
          m
          1.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
      ()
      float64
      kg
      1.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'])
Show/Hide data repr Show/Hide attributes
scipp.DataArray (192 Bytes)
    • y: 3
    • x: 2
    • x
      (x)
      float64
      𝟙
      0.0, 0.0
      Values:
      array([0., 0.])
    • y
      (y [bin-edge])
      float64
      m
      0.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.])
Show/Hide data repr Show/Hide attributes
scipp.DataArray (8 Bytes)
    • ()
      float64
      kg
      1.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
Show/Hide data repr Show/Hide attributes
scipp.Variable (32 Bytes)
    • (y: 4)
      float64
      𝟙
      0.0, 0.0, 0.0, 0.0
      Values:
      array([0., 0., 0., 0.])
x
Show/Hide data repr Show/Hide attributes
scipp.Variable (16 Bytes)
    • (x: 2)
      float64
      𝟙
      0.0, 0.0
      Values:
      array([0., 0.])
y
Show/Hide data repr Show/Hide attributes
scipp.Variable (32 Bytes)
    • (y: 4)
      float64
      m
      0.0, 0.0, 0.0, 0.0
      Values:
      array([0., 0., 0., 0.])
DataArrays:(0)
Datasets:(1)
d
Show/Hide data repr Show/Hide attributes
scipp.Dataset (320 Bytes)
    • y: 3
    • x: 2
    • x
      (x)
      float64
      𝟙
      0.0, 0.0
      Values:
      array([0., 0.])
    • y
      (y [bin-edge])
      float64
      m
      0.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
          ()
          float64
          m
          1.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
      ()
      float64
      kg
      1.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)
bb(dims=['y', 'x'], shape=[3, 2], unit=Å, variances=True)variances yxvalues yx aa(dims=['y', 'x'], shape=[3, 2], unit=Å, variances=True)variances yxvalues yx y_lab..y_label(dims=['y'], shape=[4], unit=dimensionless, variances=False)values y yy(dims=['y'], shape=[4], unit=m, variances=False)values y cc(dims=[], shape=[], unit=kg, variances=False)values xx(dims=['x'], shape=[2], unit=dimensionless, variances=False)values x

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])