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 (416 Bytes)
    • (y: 4, x: 5)
      float64
      𝟙
      0.651, 0.860, ..., 0.736, 0.327
      Values:
      array([[0.65098239, 0.86044099, 0.43594134, 0.11731904, 0.80448348], [0.76675464, 0.04289888, 0.04183491, 0.93360158, 0.53752035], [0.28852665, 0.66897157, 0.00459487, 0.96690272, 0.71799225], [0.20426842, 0.87737024, 0.82512391, 0.73618549, 0.32716409]])
[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 (1.48 KB)
    • 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.651, 0.860, ..., 0.736, 0.327
      Values:
      array([[0.65098239, 0.86044099, 0.43594134, 0.11731904, 0.80448348], [0.76675464, 0.04289888, 0.04183491, 0.93360158, 0.53752035], [0.28852665, 0.66897157, 0.00459487, 0.96690272, 0.71799225], [0.20426842, 0.87737024, 0.82512391, 0.73618549, 0.32716409]])

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)
aa(dims=('y', 'x'), shape=(4, 5), unit=dimensionless, variances=True)variances yxvalues yx bb(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 auxaux(dims=('x',), shape=(5,), unit=m, variances=False)values x xx(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
aa(dims=('y', 'x'), shape=(4, 5), unit=dimensionless, variances=True)variances yxvalues yx bb(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 auxaux(dims=('x',), shape=(5,), unit=m, variances=False)values x xx(dims=('x',), shape=(5,), unit=m, variances=False)values x
[8]:
Show/Hide data repr Show/Hide attributes
scipp.Dataset (5.28 KB out of 5.59 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.651, 0.860, ..., 0.736, 0.327
      σ = 0.266, 0.701, ..., 0.238, 0.613
      Values:
      array([[0.65098239, 0.86044099, 0.43594134, 0.11731904, 0.80448348], [0.76675464, 0.04289888, 0.04183491, 0.93360158, 0.53752035], [0.28852665, 0.66897157, 0.00459487, 0.96690272, 0.71799225], [0.20426842, 0.87737024, 0.82512391, 0.73618549, 0.32716409]])

      Variances (σ²):
      array([[7.08913566e-02, 4.91558630e-01, 1.36393885e-01, 9.73335179e-01, 2.19480374e-04], [1.01892712e-01, 1.84912574e-01, 4.23904054e-02, 2.31883466e-01, 8.80681581e-02], [3.15671080e-02, 3.72781980e-03, 8.01926496e-01, 1.20596261e-01, 2.89413432e-01], [1.47826795e-01, 4.26541180e-01, 6.58204624e-01, 5.64865778e-02, 3.75219716e-01]])
    • b
      (y, x)
      float64
      𝟙
      0.651, 0.860, ..., 0.736, 0.327
      σ = 0.266, 0.701, ..., 0.238, 0.613
      Values:
      array([[0.65098239, 0.86044099, 0.43594134, 0.11731904, 0.80448348], [0.76675464, 0.04289888, 0.04183491, 0.93360158, 0.53752035], [0.28852665, 0.66897157, 0.00459487, 0.96690272, 0.71799225], [0.20426842, 0.87737024, 0.82512391, 0.73618549, 0.32716409]])

      Variances (σ²):
      array([[7.08913566e-02, 4.91558630e-01, 1.36393885e-01, 9.73335179e-01, 2.19480374e-04], [1.01892712e-01, 1.84912574e-01, 4.23904054e-02, 2.31883466e-01, 8.80681581e-02], [3.15671080e-02, 3.72781980e-03, 8.01926496e-01, 1.20596261e-01, 2.89413432e-01], [1.47826795e-01, 4.26541180e-01, 6.58204624e-01, 5.64865778e-02, 3.75219716e-01]])
    • c
      (y)
      float64
      𝟙
      0.436, 0.042, 0.005, 0.825
      σ = 0.369, 0.206, 0.896, 0.811
        • aux
          ()
          int64
          m
          2
          Values:
          array(2)
        • x
          ()
          int64
          m
          2
          Values:
          array(2)
      Values:
      array([0.43594134, 0.04183491, 0.00459487, 0.82512391])

      Variances (σ²):
      array([0.13639388, 0.04239041, 0.8019265 , 0.65820462])
    • 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])
[9]:
ab
CoordinatesDataData
aux [m]x [m] [𝟙] [𝟙]
000.289±0.1780.289±0.178
110.669±0.0610.669±0.061
220.005±0.8960.005±0.896
330.967±0.3470.967±0.347
440.718±0.5380.718±0.538
[10]:
sc.plot(dataset)
[10]:

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.650982, 0.860441, ..., 0.736185, 0.327164]  [0.0708914, 0.491559, ..., 0.0564866, 0.37522]
  b                         float64  [dimensionless]  (y, x)  [0.650982, 0.860441, ..., 0.736185, 0.327164]  [0.0708914, 0.491559, ..., 0.0564866, 0.37522]
  c                         float64  [dimensionless]  (y)  [0.435941, 0.0418349, 0.00459487, 0.825124]  [0.136394, 0.0423904, 0.801926, 0.658205]
    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.257634, ..., 0.905508, 0.402412]  [0.214503, 0.850931, ..., 0.0854585, 0.56767]
  b                         float64            [m/s]  (y, x)  [0, 0.257634, ..., 0.905508, 0.402412]  [0.214503, 0.850931, ..., 0.0854585, 0.56767]
  c                         float64            [m/s]  (y)  [-0.2645, -0.891651, 0.00565169, 1.0149]  [0.313602, 0.218286, 1.21323, 0.995798]
    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 (576 Bytes)
    • (y: 4, x: 5)
      float64
      m/s
      0.0, 0.258, ..., 0.906, 0.402
      σ = 0.463, 0.922, ..., 0.292, 0.753
      Values:
      array([[ 0. , 0.25763407, -0.2645005 , -0.65640592, 0.18880634], [ 0. , -0.89034258, -0.89165126, 0.20522174, -0.28195817], [ 0.35488778, 0.82283503, 0.00565169, 1.18929034, 0.88313047], [ 0.25125015, 1.0791654 , 1.01490241, 0.90550815, 0.40241182]])

      Variances (σ²):
      array([[0.21450307, 0.85093058, 0.31360184, 1.57981033, 0.10758359], [0.30830697, 0.43390772, 0.21828593, 0.50496998, 0.2873918 ], [0.04775788, 0.00563982, 1.2132346 , 0.18245008, 0.43785358], [0.22364716, 0.64531415, 0.99579778, 0.08545854, 0.56766991]])
x
Show/Hide data repr Show/Hide attributes
scipp.Variable (296 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 (288 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 (1.63 KB)
    • 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.258, ..., 0.906, 0.402
      σ = 0.463, 0.922, ..., 0.292, 0.753
      Values:
      array([[ 0. , 0.25763407, -0.2645005 , -0.65640592, 0.18880634], [ 0. , -0.89034258, -0.89165126, 0.20522174, -0.28195817], [ 0.35488778, 0.82283503, 0.00565169, 1.18929034, 0.88313047], [ 0.25125015, 1.0791654 , 1.01490241, 0.90550815, 0.40241182]])

      Variances (σ²):
      array([[0.21450307, 0.85093058, 0.31360184, 1.57981033, 0.10758359], [0.30830697, 0.43390772, 0.21828593, 0.50496998, 0.2873918 ], [0.04775788, 0.00563982, 1.2132346 , 0.18245008, 0.43785358], [0.22364716, 0.64531415, 0.99579778, 0.08545854, 0.56766991]])
Datasets:(1)
dataset
Show/Hide data repr Show/Hide attributes
scipp.Dataset (5.28 KB out of 5.59 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.258, ..., 0.906, 0.402
      σ = 0.463, 0.922, ..., 0.292, 0.753
      Values:
      array([[ 0. , 0.25763407, -0.2645005 , -0.65640592, 0.18880634], [ 0. , -0.89034258, -0.89165126, 0.20522174, -0.28195817], [ 0.35488778, 0.82283503, 0.00565169, 1.18929034, 0.88313047], [ 0.25125015, 1.0791654 , 1.01490241, 0.90550815, 0.40241182]])

      Variances (σ²):
      array([[0.21450307, 0.85093058, 0.31360184, 1.57981033, 0.10758359], [0.30830697, 0.43390772, 0.21828593, 0.50496998, 0.2873918 ], [0.04775788, 0.00563982, 1.2132346 , 0.18245008, 0.43785358], [0.22364716, 0.64531415, 0.99579778, 0.08545854, 0.56766991]])
    • b
      (y, x)
      float64
      m/s
      0.0, 0.258, ..., 0.906, 0.402
      σ = 0.463, 0.922, ..., 0.292, 0.753
      Values:
      array([[ 0. , 0.25763407, -0.2645005 , -0.65640592, 0.18880634], [ 0. , -0.89034258, -0.89165126, 0.20522174, -0.28195817], [ 0.35488778, 0.82283503, 0.00565169, 1.18929034, 0.88313047], [ 0.25125015, 1.0791654 , 1.01490241, 0.90550815, 0.40241182]])

      Variances (σ²):
      array([[0.21450307, 0.85093058, 0.31360184, 1.57981033, 0.10758359], [0.30830697, 0.43390772, 0.21828593, 0.50496998, 0.2873918 ], [0.04775788, 0.00563982, 1.2132346 , 0.18245008, 0.43785358], [0.22364716, 0.64531415, 0.99579778, 0.08545854, 0.56766991]])
    • c
      (y)
      float64
      m/s
      -0.265, -0.892, 0.006, 1.015
      σ = 0.560, 0.467, 1.101, 0.998
        • aux
          ()
          int64
          m
          2
          Values:
          array(2)
        • x
          ()
          int64
          m
          2
          Values:
          array(2)
      Values:
      array([-0.2645005 , -0.89165126, 0.00565169, 1.01490241])

      Variances (σ²):
      array([0.31360184, 0.21828593, 1.2132346 , 0.99579778])
    • scalar
      ()
      float64
      m/s
      1.23
      Values:
      array(1.23)
[13]:
<module 'scipp' from '/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/__init__.py'>