Quick start
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)float64𝟙0.639, 0.041, ..., 0.347, 0.409
Values:
array([[0.63888866, 0.04108235, 0.96318335, 0.3435437 , 0.49736929], [0.72490326, 0.64915203, 0.58315774, 0.90415743, 0.17200032], [0.68844817, 0.85674765, 0.56840341, 0.45739428, 0.76468145], [0.70321757, 0.73213711, 0.94100991, 0.34663324, 0.40933469]])
[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)float64𝟙0.639, 0.041, ..., 0.347, 0.409
Values:
array([[0.63888866, 0.04108235, 0.96318335, 0.3435437 , 0.49736929], [0.72490326, 0.64915203, 0.58315774, 0.90415743, 0.17200032], [0.68844817, 0.85674765, 0.56840341, 0.45739428, 0.76468145], [0.70321757, 0.73213711, 0.94100991, 0.34663324, 0.40933469]])
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)float64𝟙0.639, 0.041, ..., 0.347, 0.409σ = 0.639, 0.752, ..., 0.638, 0.593
Values:
array([[0.63888866, 0.04108235, 0.96318335, 0.3435437 , 0.49736929], [0.72490326, 0.64915203, 0.58315774, 0.90415743, 0.17200032], [0.68844817, 0.85674765, 0.56840341, 0.45739428, 0.76468145], [0.70321757, 0.73213711, 0.94100991, 0.34663324, 0.40933469]])
Variances (σ²):
array([[4.08394870e-01, 5.65323423e-01, 5.42324610e-01, 4.18795125e-01, 2.75075433e-04], [4.01268076e-01, 9.87938022e-01, 4.51386849e-03, 2.85838272e-02, 6.76577812e-01], [8.43831642e-01, 5.59001134e-01, 2.71609083e-01, 5.54610228e-02, 5.59581215e-01], [3.94223127e-01, 9.64155843e-01, 7.98374672e-03, 4.07080546e-01, 3.51351772e-01]]) - b(y, x)float64𝟙0.639, 0.041, ..., 0.347, 0.409σ = 0.639, 0.752, ..., 0.638, 0.593
Values:
array([[0.63888866, 0.04108235, 0.96318335, 0.3435437 , 0.49736929], [0.72490326, 0.64915203, 0.58315774, 0.90415743, 0.17200032], [0.68844817, 0.85674765, 0.56840341, 0.45739428, 0.76468145], [0.70321757, 0.73213711, 0.94100991, 0.34663324, 0.40933469]])
Variances (σ²):
array([[4.08394870e-01, 5.65323423e-01, 5.42324610e-01, 4.18795125e-01, 2.75075433e-04], [4.01268076e-01, 9.87938022e-01, 4.51386849e-03, 2.85838272e-02, 6.76577812e-01], [8.43831642e-01, 5.59001134e-01, 2.71609083e-01, 5.54610228e-02, 5.59581215e-01], [3.94223127e-01, 9.64155843e-01, 7.98374672e-03, 4.07080546e-01, 3.51351772e-01]]) - c(y)float64𝟙0.963, 0.583, 0.568, 0.941σ = 0.736, 0.067, 0.521, 0.089
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([0.96318335, 0.58315774, 0.56840341, 0.94100991])
Variances (σ²):
array([0.54232461, 0.00451387, 0.27160908, 0.00798375]) - 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])
[9]:
a | b | ||
---|---|---|---|
Coordinates | Data | Data | |
aux [m] | x [m] | [𝟙] | [𝟙] |
0 | 0 | 0.688±0.919 | 0.688±0.919 |
1 | 1 | 0.857±0.748 | 0.857±0.748 |
2 | 2 | 0.568±0.521 | 0.568±0.521 |
3 | 3 | 0.457±0.236 | 0.457±0.236 |
4 | 4 | 0.765±0.748 | 0.765±0.748 |
[10]:
sc.plot(dataset['a'])
[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.638889, 0.0410824, ..., 0.346633, 0.409335] [0.408395, 0.565323, ..., 0.407081, 0.351352]
b float64 [dimensionless] (y, x) [0.638889, 0.0410824, ..., 0.346633, 0.409335] [0.408395, 0.565323, ..., 0.407081, 0.351352]
c float64 [dimensionless] (y) [0.963183, 0.583158, 0.568403, 0.94101] [0.542325, 0.00451387, 0.271609, 0.00798375]
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.735302, ..., 0.426359, 0.503482] [1.23572, 1.47314, ..., 0.615872, 0.53156]
b float64 [m/s] (y, x) [0, -0.735302, ..., 0.426359, 0.503482] [1.23572, 1.47314, ..., 0.615872, 0.53156]
c float64 [m/s] (y) [0.398882, -0.174347, 0.699136, 1.15744] [1.43834, 0.613908, 0.410917, 0.0120786]
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
- (y: 4, x: 5)float64m/s0.0, -0.735, ..., 0.426, 0.503σ = 1.112, 1.214, ..., 0.785, 0.729
Values:
array([[ 0. , -0.73530177, 0.39888247, -0.3632743 , -0.17406883], [ 0. , -0.09317401, -0.17434699, 0.22048262, -0.68007063], [ 0.84679125, 1.05379961, 0.69913619, 0.56259496, 0.94055819], [ 0.86495761, 0.90052865, 1.1574422 , 0.42635889, 0.50348167]])
Variances (σ²):
array([[1.2357212 , 1.47313841, 1.4383435 , 1.25145574, 0.61827676], [1.21415694, 2.10172991, 0.6139075 , 0.65032294, 1.63067304], [1.27663289, 0.84571282, 0.41091738, 0.08390698, 0.84659042], [0.59642017, 1.45867137, 0.01207861, 0.61587216, 0.5315601 ]])
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.735, ..., 0.426, 0.503σ = 1.112, 1.214, ..., 0.785, 0.729
Values:
array([[ 0. , -0.73530177, 0.39888247, -0.3632743 , -0.17406883], [ 0. , -0.09317401, -0.17434699, 0.22048262, -0.68007063], [ 0.84679125, 1.05379961, 0.69913619, 0.56259496, 0.94055819], [ 0.86495761, 0.90052865, 1.1574422 , 0.42635889, 0.50348167]])
Variances (σ²):
array([[1.2357212 , 1.47313841, 1.4383435 , 1.25145574, 0.61827676], [1.21415694, 2.10172991, 0.6139075 , 0.65032294, 1.63067304], [1.27663289, 0.84571282, 0.41091738, 0.08390698, 0.84659042], [0.59642017, 1.45867137, 0.01207861, 0.61587216, 0.5315601 ]])
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.735, ..., 0.426, 0.503σ = 1.112, 1.214, ..., 0.785, 0.729
Values:
array([[ 0. , -0.73530177, 0.39888247, -0.3632743 , -0.17406883], [ 0. , -0.09317401, -0.17434699, 0.22048262, -0.68007063], [ 0.84679125, 1.05379961, 0.69913619, 0.56259496, 0.94055819], [ 0.86495761, 0.90052865, 1.1574422 , 0.42635889, 0.50348167]])
Variances (σ²):
array([[1.2357212 , 1.47313841, 1.4383435 , 1.25145574, 0.61827676], [1.21415694, 2.10172991, 0.6139075 , 0.65032294, 1.63067304], [1.27663289, 0.84571282, 0.41091738, 0.08390698, 0.84659042], [0.59642017, 1.45867137, 0.01207861, 0.61587216, 0.5315601 ]]) - b(y, x)float64m/s0.0, -0.735, ..., 0.426, 0.503σ = 1.112, 1.214, ..., 0.785, 0.729
Values:
array([[ 0. , -0.73530177, 0.39888247, -0.3632743 , -0.17406883], [ 0. , -0.09317401, -0.17434699, 0.22048262, -0.68007063], [ 0.84679125, 1.05379961, 0.69913619, 0.56259496, 0.94055819], [ 0.86495761, 0.90052865, 1.1574422 , 0.42635889, 0.50348167]])
Variances (σ²):
array([[1.2357212 , 1.47313841, 1.4383435 , 1.25145574, 0.61827676], [1.21415694, 2.10172991, 0.6139075 , 0.65032294, 1.63067304], [1.27663289, 0.84571282, 0.41091738, 0.08390698, 0.84659042], [0.59642017, 1.45867137, 0.01207861, 0.61587216, 0.5315601 ]]) - c(y)float64m/s0.399, -0.174, 0.699, 1.157σ = 1.199, 0.784, 0.641, 0.110
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([ 0.39888247, -0.17434699, 0.69913619, 1.1574422 ])
Variances (σ²):
array([1.4383435 , 0.6139075 , 0.41091738, 0.01207861]) - scalar()float64m/s1.23
Values:
array(1.23)
[13]:
<module 'scipp' from '/home/runner/work/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/__init__.py'>