Quick start#

This section provides a quick introduction to scipp. For in depth explanations refer to the sections in the user guide.

[1]:
import numpy as np
import scipp as sc

We start by creating some variables:

[2]:
var = sc.Variable(dims=['y', 'x'], values=np.random.rand(4,5))
sc.show(var)
dims=['y', 'x'], shape=[4, 5], unit=dimensionless, variances=Falsevalues yx

Type the name of a variable at the end of a cell to generate an HTML respresentation:

[3]:
var
[3]:
Show/Hide data repr Show/Hide attributes
scipp.Variable (160 Bytes)
    • (y: 4, x: 5)
      float64
      𝟙
      0.260, 0.853, ..., 0.398, 0.899
      Values:
      array([[0.26033909, 0.85252177, 0.01754777, 0.58943346, 0.82274093], [0.23873476, 0.03352047, 0.46069816, 0.48064053, 0.39939353], [0.54723983, 0.50138113, 0.43964187, 0.45008749, 0.38475728], [0.24076066, 0.1691947 , 0.88384524, 0.39818341, 0.89947434]])
[4]:
x = sc.Variable(dims=['x'], values=np.arange(5), unit=sc.units.m)
y = sc.Variable(dims=['y'], values=np.arange(4), unit=sc.units.m)

We combine the variables into a data array:

[5]:
array = sc.DataArray(
    data=var,
    coords={'x': x, 'y': y})
sc.show(array)
array
(dims=['y', 'x'], shape=[4, 5], unit=dimensionless, variances=False)values yx yy(dims=['y'], shape=[4], unit=m, variances=False)values y xx(dims=['x'], shape=[5], unit=m, variances=False)values x
[5]:
Show/Hide data repr Show/Hide attributes
scipp.DataArray (232 Bytes)
    • y: 4
    • x: 5
    • x
      (x)
      int64
      m
      0, 1, 2, 3, 4
      Values:
      array([0, 1, 2, 3, 4])
    • y
      (y)
      int64
      m
      0, 1, 2, 3
      Values:
      array([0, 1, 2, 3])
    • (y, x)
      float64
      𝟙
      0.260, 0.853, ..., 0.398, 0.899
      Values:
      array([[0.26033909, 0.85252177, 0.01754777, 0.58943346, 0.82274093], [0.23873476, 0.03352047, 0.46069816, 0.48064053, 0.39939353], [0.54723983, 0.50138113, 0.43964187, 0.45008749, 0.38475728], [0.24076066, 0.1691947 , 0.88384524, 0.39818341, 0.89947434]])

Variables can have uncertainties. Scipp stores these as variances (the square of the standard deviation):

[6]:
array.variances = np.square(np.random.rand(4,5))
sc.show(array)
(dims=['y', 'x'], shape=[4, 5], unit=dimensionless, variances=True)variances yxvalues yx yy(dims=['y'], shape=[4], unit=m, variances=False)values y xx(dims=['x'], shape=[5], unit=m, variances=False)values x

We create a dataset:

[7]:
dataset = sc.Dataset(
    data={'a': var},
    coords={'x': x, 'y': y, 'aux': x})
dataset['b'] = array
dataset['scalar'] = 1.23 * (sc.units.m / sc.units.s)
sc.show(dataset)
bb(dims=['y', 'x'], shape=[4, 5], unit=dimensionless, variances=True)variances yxvalues yx aa(dims=['y', 'x'], shape=[4, 5], unit=dimensionless, variances=True)variances yxvalues yx yy(dims=['y'], shape=[4], unit=m, variances=False)values y scala..scalar(dims=[], shape=[], unit=m/s, variances=False)values xx(dims=['x'], shape=[5], unit=m, variances=False)values x auxaux(dims=['x'], shape=[5], unit=m, variances=False)values x

We can slice variables, data arrays, and datasets using a dimension label and an index or a slice object like i:j:

[8]:
dataset['c'] = dataset['b']['x', 2]
sc.show(dataset)
dataset
bb(dims=['y', 'x'], shape=[4, 5], unit=dimensionless, variances=True)variances yxvalues yx aa(dims=['y', 'x'], shape=[4, 5], unit=dimensionless, variances=True)variances yxvalues yx yy(dims=['y'], shape=[4], unit=m, variances=False)values y cc(dims=['y'], shape=[4], unit=dimensionless, variances=True)variances yvalues y scala..scalar(dims=[], shape=[], unit=m/s, variances=False)values xx(dims=['x'], shape=[5], unit=m, variances=False)values x auxaux(dims=['x'], shape=[5], unit=m, variances=False)values x
[8]:
Show/Hide data repr Show/Hide attributes
scipp.Dataset (840 Bytes out of 1.13 KB)
    • y: 4
    • x: 5
    • aux
      (x)
      int64
      m
      0, 1, 2, 3, 4
      Values:
      array([0, 1, 2, 3, 4])
    • x
      (x)
      int64
      m
      0, 1, 2, 3, 4
      Values:
      array([0, 1, 2, 3, 4])
    • y
      (y)
      int64
      m
      0, 1, 2, 3
      Values:
      array([0, 1, 2, 3])
    • a
      (y, x)
      float64
      𝟙
      0.260, 0.853, ..., 0.398, 0.899
      σ = 0.048, 0.446, ..., 0.496, 0.903
      Values:
      array([[0.26033909, 0.85252177, 0.01754777, 0.58943346, 0.82274093], [0.23873476, 0.03352047, 0.46069816, 0.48064053, 0.39939353], [0.54723983, 0.50138113, 0.43964187, 0.45008749, 0.38475728], [0.24076066, 0.1691947 , 0.88384524, 0.39818341, 0.89947434]])

      Variances (σ²):
      array([[0.00234395, 0.19922919, 0.90721292, 0.84677421, 0.33605429], [0.06038284, 0.02246221, 0.06394219, 0.76439184, 0.00176963], [0.32437777, 0.77124412, 0.17160697, 0.00566756, 0.13931449], [0.16567086, 0.05714881, 0.33091188, 0.24559291, 0.81464395]])
    • b
      (y, x)
      float64
      𝟙
      0.260, 0.853, ..., 0.398, 0.899
      σ = 0.048, 0.446, ..., 0.496, 0.903
      Values:
      array([[0.26033909, 0.85252177, 0.01754777, 0.58943346, 0.82274093], [0.23873476, 0.03352047, 0.46069816, 0.48064053, 0.39939353], [0.54723983, 0.50138113, 0.43964187, 0.45008749, 0.38475728], [0.24076066, 0.1691947 , 0.88384524, 0.39818341, 0.89947434]])

      Variances (σ²):
      array([[0.00234395, 0.19922919, 0.90721292, 0.84677421, 0.33605429], [0.06038284, 0.02246221, 0.06394219, 0.76439184, 0.00176963], [0.32437777, 0.77124412, 0.17160697, 0.00566756, 0.13931449], [0.16567086, 0.05714881, 0.33091188, 0.24559291, 0.81464395]])
    • c
      (y)
      float64
      𝟙
      0.018, 0.461, 0.440, 0.884
      σ = 0.952, 0.253, 0.414, 0.575
        • aux
          ()
          int64
          m
          2
          Values:
          array(2)
        • x
          ()
          int64
          m
          2
          Values:
          array(2)
      Values:
      array([0.01754777, 0.46069816, 0.43964187, 0.88384524])

      Variances (σ²):
      array([0.90721292, 0.06394219, 0.17160697, 0.33091188])
    • scalar
      ()
      float64
      m/s
      1.23
      Values:
      array(1.23)

We can also generate table representations (only 0-D and 1-D) and plots:

[9]:
sc.table(dataset['y', 2])
[10]:
sc.plot(dataset)

Arithmetic operations can be combined with slicing and handle propagation of uncertainties and units:

[11]:
print(dataset)
<scipp.Dataset>
Dimensions: Sizes[y:4, x:5, ]
Coordinates:
  aux                         int64              [m]  (x)  [0, 1, ..., 3, 4]
  x                           int64              [m]  (x)  [0, 1, ..., 3, 4]
  y                           int64              [m]  (y)  [0, 1, 2, 3]
Data:
  a                         float64  [dimensionless]  (y, x)  [0.260339, 0.852522, ..., 0.398183, 0.899474]  [0.00234395, 0.199229, ..., 0.245593, 0.814644]
  b                         float64  [dimensionless]  (y, x)  [0.260339, 0.852522, ..., 0.398183, 0.899474]  [0.00234395, 0.199229, ..., 0.245593, 0.814644]
  c                         float64  [dimensionless]  (y)  [0.0175478, 0.460698, 0.439642, 0.883845]  [0.907213, 0.0639422, 0.171607, 0.330912]
    Attributes:
        aux                         int64              [m]  ()  [2]
        x                           int64              [m]  ()  [2]
  scalar                    float64            [m/s]  ()  [1.23]


[12]:
dataset['b']['y', 0:2] -= dataset['y', 0:2]['a']['x', 0]
dataset['b'] *= dataset['scalar']
print(dataset)
<scipp.Dataset>
Dimensions: Sizes[y:4, x:5, ]
Coordinates:
  aux                         int64              [m]  (x)  [0, 1, ..., 3, 4]
  x                           int64              [m]  (x)  [0, 1, ..., 3, 4]
  y                           int64              [m]  (y)  [0, 1, 2, 3]
Data:
  a                         float64            [m/s]  (y, x)  [0, 0.728385, ..., 0.489766, 1.10635]  [0.00709231, 0.30496, ..., 0.371558, 1.23247]
  b                         float64            [m/s]  (y, x)  [0, 0.728385, ..., 0.489766, 1.10635]  [0.00709231, 0.30496, ..., 0.371558, 1.23247]
  c                         float64            [m/s]  (y)  [-0.298633, 0.273015, 0.54076, 1.08713]  [1.37607, 0.188091, 0.259624, 0.500637]
    Attributes:
        aux                         int64              [m]  ()  [2]
        x                           int64              [m]  ()  [2]
  scalar                    float64            [m/s]  ()  [1.23]


Finally, type the imported name of the scipp module at the end of a cell for a list of all current scipp objects (variables, data arrays, datasets). Click on entries to expand nested sections:

[13]:
sc
Variables:(3)
var
Show/Hide data repr Show/Hide attributes
scipp.Variable (320 Bytes)
    • (y: 4, x: 5)
      float64
      m/s
      0.0, 0.728, ..., 0.490, 1.106
      σ = 0.084, 0.552, ..., 0.610, 1.110
      Values:
      array([[ 0. , 0.72838469, -0.29863333, 0.40478607, 0.69175426], [ 0. , -0.25241358, 0.27301499, 0.2975441 , 0.19761029], [ 0.67310499, 0.61669879, 0.54075951, 0.55360762, 0.47325146], [ 0.29613561, 0.20810948, 1.08712965, 0.48976559, 1.10635343]])

      Variances (σ²):
      array([[0.00709231, 0.30496 , 1.37606858, 1.28463085, 0.51196269], [0.18270641, 0.12533628, 0.18809134, 1.24780163, 0.09403049], [0.49075113, 1.16681522, 0.25962419, 0.00857445, 0.2107689 ], [0.25064344, 0.08646044, 0.50063658, 0.37155752, 1.23247484]])
x
Show/Hide data repr Show/Hide attributes
scipp.Variable (40 Bytes)
    • (x: 5)
      int64
      m
      0, 1, 2, 3, 4
      Values:
      array([0, 1, 2, 3, 4])
y
Show/Hide data repr Show/Hide attributes
scipp.Variable (32 Bytes)
    • (y: 4)
      int64
      m
      0, 1, 2, 3
      Values:
      array([0, 1, 2, 3])
DataArrays:(1)
array
Show/Hide data repr Show/Hide attributes
scipp.DataArray (392 Bytes)
    • y: 4
    • x: 5
    • x
      (x)
      int64
      m
      0, 1, 2, 3, 4
      Values:
      array([0, 1, 2, 3, 4])
    • y
      (y)
      int64
      m
      0, 1, 2, 3
      Values:
      array([0, 1, 2, 3])
    • (y, x)
      float64
      m/s
      0.0, 0.728, ..., 0.490, 1.106
      σ = 0.084, 0.552, ..., 0.610, 1.110
      Values:
      array([[ 0. , 0.72838469, -0.29863333, 0.40478607, 0.69175426], [ 0. , -0.25241358, 0.27301499, 0.2975441 , 0.19761029], [ 0.67310499, 0.61669879, 0.54075951, 0.55360762, 0.47325146], [ 0.29613561, 0.20810948, 1.08712965, 0.48976559, 1.10635343]])

      Variances (σ²):
      array([[0.00709231, 0.30496 , 1.37606858, 1.28463085, 0.51196269], [0.18270641, 0.12533628, 0.18809134, 1.24780163, 0.09403049], [0.49075113, 1.16681522, 0.25962419, 0.00857445, 0.2107689 ], [0.25064344, 0.08646044, 0.50063658, 0.37155752, 1.23247484]])
Datasets:(1)
dataset
Show/Hide data repr Show/Hide attributes
scipp.Dataset (840 Bytes out of 1.13 KB)
    • y: 4
    • x: 5
    • aux
      (x)
      int64
      m
      0, 1, 2, 3, 4
      Values:
      array([0, 1, 2, 3, 4])
    • x
      (x)
      int64
      m
      0, 1, 2, 3, 4
      Values:
      array([0, 1, 2, 3, 4])
    • y
      (y)
      int64
      m
      0, 1, 2, 3
      Values:
      array([0, 1, 2, 3])
    • a
      (y, x)
      float64
      m/s
      0.0, 0.728, ..., 0.490, 1.106
      σ = 0.084, 0.552, ..., 0.610, 1.110
      Values:
      array([[ 0. , 0.72838469, -0.29863333, 0.40478607, 0.69175426], [ 0. , -0.25241358, 0.27301499, 0.2975441 , 0.19761029], [ 0.67310499, 0.61669879, 0.54075951, 0.55360762, 0.47325146], [ 0.29613561, 0.20810948, 1.08712965, 0.48976559, 1.10635343]])

      Variances (σ²):
      array([[0.00709231, 0.30496 , 1.37606858, 1.28463085, 0.51196269], [0.18270641, 0.12533628, 0.18809134, 1.24780163, 0.09403049], [0.49075113, 1.16681522, 0.25962419, 0.00857445, 0.2107689 ], [0.25064344, 0.08646044, 0.50063658, 0.37155752, 1.23247484]])
    • b
      (y, x)
      float64
      m/s
      0.0, 0.728, ..., 0.490, 1.106
      σ = 0.084, 0.552, ..., 0.610, 1.110
      Values:
      array([[ 0. , 0.72838469, -0.29863333, 0.40478607, 0.69175426], [ 0. , -0.25241358, 0.27301499, 0.2975441 , 0.19761029], [ 0.67310499, 0.61669879, 0.54075951, 0.55360762, 0.47325146], [ 0.29613561, 0.20810948, 1.08712965, 0.48976559, 1.10635343]])

      Variances (σ²):
      array([[0.00709231, 0.30496 , 1.37606858, 1.28463085, 0.51196269], [0.18270641, 0.12533628, 0.18809134, 1.24780163, 0.09403049], [0.49075113, 1.16681522, 0.25962419, 0.00857445, 0.2107689 ], [0.25064344, 0.08646044, 0.50063658, 0.37155752, 1.23247484]])
    • c
      (y)
      float64
      m/s
      -0.299, 0.273, 0.541, 1.087
      σ = 1.173, 0.434, 0.510, 0.708
        • aux
          ()
          int64
          m
          2
          Values:
          array(2)
        • x
          ()
          int64
          m
          2
          Values:
          array(2)
      Values:
      array([-0.29863333, 0.27301499, 0.54075951, 1.08712965])

      Variances (σ²):
      array([1.37606858, 0.18809134, 0.25962419, 0.50063658])
    • scalar
      ()
      float64
      m/s
      1.23
      Values:
      array(1.23)
[13]:
<module 'scipp' from '/home/simon/code/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/__init__.py'>