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)
Type the name of a variable at the end of a cell to generate an HTML respresentation:
[3]:
var
[3]:
- (y: 4, x: 5)float640.66, 0.07, ..., 0.26, 0.6
Values:
array([[0.65780539, 0.06505393, 0.34145527, 0.74368694, 0.05012935], [0.40935821, 0.47425872, 0.66428449, 0.4985452 , 0.43700121], [0.49794469, 0.28874962, 0.43060269, 0.6405841 , 0.35433544], [0.31362005, 0.3457533 , 0.13291178, 0.26009168, 0.59534877]])
[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
[5]:
- y: 4
- x: 5
- x(x)int64m0, 1, 2, 3, 4
Values:
array([0, 1, 2, 3, 4]) - y(y)int64m0, 1, 2, 3
Values:
array([0, 1, 2, 3])
- (y, x)float640.66, 0.07, ..., 0.26, 0.6
Values:
array([[0.65780539, 0.06505393, 0.34145527, 0.74368694, 0.05012935], [0.40935821, 0.47425872, 0.66428449, 0.4985452 , 0.43700121], [0.49794469, 0.28874962, 0.43060269, 0.6405841 , 0.35433544], [0.31362005, 0.3457533 , 0.13291178, 0.26009168, 0.59534877]])
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)
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)
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
[8]:
- y: 4
- x: 5
- aux(x)int64m0, 1, 2, 3, 4
Values:
array([0, 1, 2, 3, 4]) - x(x)int64m0, 1, 2, 3, 4
Values:
array([0, 1, 2, 3, 4]) - y(y)int64m0, 1, 2, 3
Values:
array([0, 1, 2, 3])
- a(y, x)float640.66, 0.07, ..., 0.26, 0.6σ = 0.62, 0.38, ..., 0.13, 0.47
Values:
array([[0.65780539, 0.06505393, 0.34145527, 0.74368694, 0.05012935], [0.40935821, 0.47425872, 0.66428449, 0.4985452 , 0.43700121], [0.49794469, 0.28874962, 0.43060269, 0.6405841 , 0.35433544], [0.31362005, 0.3457533 , 0.13291178, 0.26009168, 0.59534877]])
Variances (σ²):
array([[0.39057516, 0.1439567 , 0.08855967, 0.8733477 , 0.17032368], [0.02730934, 0.20264379, 0.21115033, 0.9964424 , 0.24788929], [0.99048862, 0.19774823, 0.01145165, 0.86301724, 0.10338583], [0.00122493, 0.2213885 , 0.04630171, 0.01656389, 0.21871896]]) - b(y, x)float640.66, 0.07, ..., 0.26, 0.6σ = 0.62, 0.38, ..., 0.13, 0.47
Values:
array([[0.65780539, 0.06505393, 0.34145527, 0.74368694, 0.05012935], [0.40935821, 0.47425872, 0.66428449, 0.4985452 , 0.43700121], [0.49794469, 0.28874962, 0.43060269, 0.6405841 , 0.35433544], [0.31362005, 0.3457533 , 0.13291178, 0.26009168, 0.59534877]])
Variances (σ²):
array([[0.39057516, 0.1439567 , 0.08855967, 0.8733477 , 0.17032368], [0.02730934, 0.20264379, 0.21115033, 0.9964424 , 0.24788929], [0.99048862, 0.19774823, 0.01145165, 0.86301724, 0.10338583], [0.00122493, 0.2213885 , 0.04630171, 0.01656389, 0.21871896]]) - c(y)float640.34, 0.66, 0.43, 0.13σ = 0.3, 0.46, 0.11, 0.22
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([0.34145527, 0.66428449, 0.43060269, 0.13291178])
Variances (σ²):
array([0.08855967, 0.21115033, 0.01145165, 0.04630171]) - scalar()float64m/s1.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, 2, 3, 4]
x int64 [m] (x) [0, 1, 2, 3, 4]
y int64 [m] (y) [0, 1, 2, 3]
Data:
a float64 [dimensionless] (y, x) [0.657805, 0.065054, 0.341455, 0.743687, ..., 0.345753, 0.132912, 0.260092, 0.595349] [0.390575, 0.143957, 0.088560, 0.873348, ..., 0.221389, 0.046302, 0.016564, 0.218719]
b float64 [dimensionless] (y, x) [0.657805, 0.065054, 0.341455, 0.743687, ..., 0.345753, 0.132912, 0.260092, 0.595349] [0.390575, 0.143957, 0.088560, 0.873348, ..., 0.221389, 0.046302, 0.016564, 0.218719]
c float64 [dimensionless] (y) [0.341455, 0.664284, 0.430603, 0.132912] [0.088560, 0.211150, 0.011452, 0.046302]
Attributes:
aux int64 [m] () [2]
x int64 [m] () [2]
scalar float64 [m/s] () [1.230000]
[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, 2, 3, 4]
x int64 [m] (x) [0, 1, 2, 3, 4]
y int64 [m] (y) [0, 1, 2, 3]
Data:
a float64 [m/s] (y, x) [0.000000, -0.729084, -0.389111, 0.105634, ..., 0.425277, 0.163481, 0.319913, 0.732279] [1.181802, 0.808693, 0.724883, 1.912189, ..., 0.334939, 0.070050, 0.025060, 0.330900]
b float64 [m/s] (y, x) [0.000000, -0.729084, -0.389111, 0.105634, ..., 0.425277, 0.163481, 0.319913, 0.732279] [1.181802, 0.808693, 0.724883, 1.912189, ..., 0.334939, 0.070050, 0.025060, 0.330900]
c float64 [m/s] (y) [-0.389111, 0.313559, 0.529641, 0.163481] [0.724883, 0.360766, 0.017325, 0.070050]
Attributes:
aux int64 [m] () [2]
x int64 [m] () [2]
scalar float64 [m/s] () [1.230000]
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
- (y: 4, x: 5)float64m/s0.0, -0.73, ..., 0.32, 0.73σ = 1.09, 0.9, ..., 0.16, 0.58
Values:
array([[ 0. , -0.72908429, -0.38911064, 0.10563431, -0.74744152], [ 0. , 0.07982762, 0.31355932, 0.1097 , 0.03400089], [ 0.61247197, 0.35516204, 0.52964131, 0.78791845, 0.43583259], [ 0.38575266, 0.42527655, 0.16348149, 0.31991276, 0.73227899]])
Variances (σ²):
array([[1.18180233e+00, 8.08693260e-01, 7.24883090e-01, 1.91218890e+00, 8.48583866e-01], [8.26325957e-02, 3.47896091e-01, 3.60765632e-01, 1.54883401e+00, 4.16347998e-01], [1.49851023e+00, 2.99173302e-01, 1.73251975e-02, 1.30565878e+00, 1.56412420e-01], [1.85318951e-03, 3.34938667e-01, 7.00498545e-02, 2.50595151e-02, 3.30899910e-01]])
x
- (x: 5)int64m0, 1, 2, 3, 4
Values:
array([0, 1, 2, 3, 4])
y
- (y: 4)int64m0, 1, 2, 3
Values:
array([0, 1, 2, 3])
DataArrays:(1)
array
- y: 4
- x: 5
- x(x)int64m0, 1, 2, 3, 4
Values:
array([0, 1, 2, 3, 4]) - y(y)int64m0, 1, 2, 3
Values:
array([0, 1, 2, 3])
- (y, x)float64m/s0.0, -0.73, ..., 0.32, 0.73σ = 1.09, 0.9, ..., 0.16, 0.58
Values:
array([[ 0. , -0.72908429, -0.38911064, 0.10563431, -0.74744152], [ 0. , 0.07982762, 0.31355932, 0.1097 , 0.03400089], [ 0.61247197, 0.35516204, 0.52964131, 0.78791845, 0.43583259], [ 0.38575266, 0.42527655, 0.16348149, 0.31991276, 0.73227899]])
Variances (σ²):
array([[1.18180233e+00, 8.08693260e-01, 7.24883090e-01, 1.91218890e+00, 8.48583866e-01], [8.26325957e-02, 3.47896091e-01, 3.60765632e-01, 1.54883401e+00, 4.16347998e-01], [1.49851023e+00, 2.99173302e-01, 1.73251975e-02, 1.30565878e+00, 1.56412420e-01], [1.85318951e-03, 3.34938667e-01, 7.00498545e-02, 2.50595151e-02, 3.30899910e-01]])
Datasets:(1)
dataset
- y: 4
- x: 5
- aux(x)int64m0, 1, 2, 3, 4
Values:
array([0, 1, 2, 3, 4]) - x(x)int64m0, 1, 2, 3, 4
Values:
array([0, 1, 2, 3, 4]) - y(y)int64m0, 1, 2, 3
Values:
array([0, 1, 2, 3])
- a(y, x)float64m/s0.0, -0.73, ..., 0.32, 0.73σ = 1.09, 0.9, ..., 0.16, 0.58
Values:
array([[ 0. , -0.72908429, -0.38911064, 0.10563431, -0.74744152], [ 0. , 0.07982762, 0.31355932, 0.1097 , 0.03400089], [ 0.61247197, 0.35516204, 0.52964131, 0.78791845, 0.43583259], [ 0.38575266, 0.42527655, 0.16348149, 0.31991276, 0.73227899]])
Variances (σ²):
array([[1.18180233e+00, 8.08693260e-01, 7.24883090e-01, 1.91218890e+00, 8.48583866e-01], [8.26325957e-02, 3.47896091e-01, 3.60765632e-01, 1.54883401e+00, 4.16347998e-01], [1.49851023e+00, 2.99173302e-01, 1.73251975e-02, 1.30565878e+00, 1.56412420e-01], [1.85318951e-03, 3.34938667e-01, 7.00498545e-02, 2.50595151e-02, 3.30899910e-01]]) - b(y, x)float64m/s0.0, -0.73, ..., 0.32, 0.73σ = 1.09, 0.9, ..., 0.16, 0.58
Values:
array([[ 0. , -0.72908429, -0.38911064, 0.10563431, -0.74744152], [ 0. , 0.07982762, 0.31355932, 0.1097 , 0.03400089], [ 0.61247197, 0.35516204, 0.52964131, 0.78791845, 0.43583259], [ 0.38575266, 0.42527655, 0.16348149, 0.31991276, 0.73227899]])
Variances (σ²):
array([[1.18180233e+00, 8.08693260e-01, 7.24883090e-01, 1.91218890e+00, 8.48583866e-01], [8.26325957e-02, 3.47896091e-01, 3.60765632e-01, 1.54883401e+00, 4.16347998e-01], [1.49851023e+00, 2.99173302e-01, 1.73251975e-02, 1.30565878e+00, 1.56412420e-01], [1.85318951e-03, 3.34938667e-01, 7.00498545e-02, 2.50595151e-02, 3.30899910e-01]]) - c(y)float64m/s-0.39, 0.31, 0.53, 0.16σ = 0.85, 0.6, 0.13, 0.26
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([-0.38911064, 0.31355932, 0.52964131, 0.16348149])
Variances (σ²):
array([0.72488309, 0.36076563, 0.0173252 , 0.07004985]) - scalar()float64m/s1.23
Values:
array(1.23)
[13]:
<module 'scipp' from '/usr/share/miniconda/envs/test/conda-bld/scipp_1638972693602/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeho/lib/python3.7/site-packages/scipp/__init__.py'>