Quick start#
This section provides a quick introduction to Scipp. For in depth explanations refer to the sections in the user guide. We recommend importing Scipp with the alias sc
, i.e., import scipp as sc
:
[1]:
import scipp as sc
We start by creating some variables:
[2]:
x = sc.arange('x', 5, unit='m')
y = sc.linspace('y', 0.0, 1.0, num=4, unit='m')
Type the name of a variable at the end of a cell to generate an HTML representation:
[3]:
y
[3]:
- (y: 4)float64m0.0, 0.333, 0.667, 1.0
Values:
array([0. , 0.33333333, 0.66666667, 1. ])
We can initialize the values of a variable from a NumPy array:
[4]:
import numpy as np
rng = np.random.default_rng(12345)
var = sc.array(dims=['y', 'x'], values=rng.random((4, 5)))
sc.show(var)
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)float64m0.0, 0.333, 0.667, 1.0
Values:
array([0. , 0.33333333, 0.66666667, 1. ])
- (y, x)float64𝟙0.227, 0.317, ..., 0.734, 0.220
Values:
array([[0.22733602, 0.31675834, 0.79736546, 0.67625467, 0.39110955], [0.33281393, 0.59830875, 0.18673419, 0.67275604, 0.94180287], [0.24824571, 0.94888115, 0.66723745, 0.09589794, 0.44183967], [0.88647992, 0.6974535 , 0.32647286, 0.73392816, 0.22013496]])
Variables can have uncertainties. Scipp stores these as variances (the square of the standard deviation):
[6]:
array = array.copy() # For more explanations why we `copy` see https://scipp.github.io/reference/ownership-mechanism-and-readonly-flags.html
array.variances = np.square(rng.random((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
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]:
sliced = dataset['b']['x', 2]
sc.show(sliced)
sliced
[8]:
- y: 4
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2) - y(y)float64m0.0, 0.333, 0.667, 1.0
Values:
array([0. , 0.33333333, 0.66666667, 1. ])
- (y)float64𝟙0.797, 0.187, 0.667, 0.326σ = 0.340, 0.129, 0.932, 0.938
Values:
array([0.79736546, 0.18673419, 0.66723745, 0.32647286])
Variances (σ²):
array([0.11566814, 0.01676224, 0.86860231, 0.87923058])
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.248 | 0.248±0.855 |
1 | 1 | 0.949 | 0.949±0.602 |
2 | 2 | 0.667 | 0.667±0.932 |
3 | 3 | 0.096 | 0.096±0.725 |
4 | 4 | 0.442 | 0.442±0.861 |
[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 float64 [m] (y) [0, 0.333333, 0.666667, 1]
Data:
a float64 [dimensionless] (y, x) [0.227336, 0.316758, ..., 0.733928, 0.220135]
b float64 [dimensionless] (y, x) [0.227336, 0.316758, ..., 0.733928, 0.220135] [0.00665767, 0.0255666, ..., 0.245013, 0.0749518]
[12]:
dataset['a']['y', 0:2] -= dataset['y', 0:2]['a']['x', 0]
dataset['b'] *= 1.23 * sc.Unit('m/s')
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 float64 [m] (y) [0, 0.333333, 0.666667, 1]
Data:
a float64 [dimensionless] (y, x) [0, 0.0894223, ..., 0.733928, 0.220135]
b float64 [m/s] (y, x) [0.279623, 0.389613, ..., 0.902732, 0.270766] [0.0100724, 0.0386797, ..., 0.37068, 0.113395]
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)float64𝟙0.0, 0.089, ..., 0.734, 0.220
Values:
array([[ 0. , 0.08942232, 0.57002943, 0.44891865, 0.16377353], [ 0. , 0.26549483, -0.14607974, 0.33994212, 0.60898894], [ 0.24824571, 0.94888115, 0.66723745, 0.09589794, 0.44183967], [ 0.88647992, 0.6974535 , 0.32647286, 0.73392816, 0.22013496]])
x
- (x: 5)int64m0, 1, 2, 3, 4
Values:
array([0, 1, 2, 3, 4])
y
- (y: 4)float64m0.0, 0.333, 0.667, 1.0
Values:
array([0. , 0.33333333, 0.66666667, 1. ])
DataArrays:(2)
array
- y: 4
- x: 5
- x(x)int64m0, 1, 2, 3, 4
Values:
array([0, 1, 2, 3, 4]) - y(y)float64m0.0, 0.333, 0.667, 1.0
Values:
array([0. , 0.33333333, 0.66666667, 1. ])
- (y, x)float64m/s0.280, 0.390, ..., 0.903, 0.271σ = 0.100, 0.197, ..., 0.609, 0.337
Values:
array([[0.27962331, 0.38961276, 0.98075951, 0.83179325, 0.48106475], [0.40936113, 0.73591977, 0.22968305, 0.82748993, 1.15841752], [0.30534223, 1.16712382, 0.82070207, 0.11795446, 0.54346279], [1.0903703 , 0.8578678 , 0.40156162, 0.90273164, 0.270766 ]])
Variances (σ²):
array([[0.01007239, 0.03867971, 0.17499432, 0.32739863, 0.10738589], [1.00682155, 0.05652606, 0.0253596 , 0.01271203, 0.54204736], [1.10530011, 0.54759131, 1.31410843, 0.79473851, 1.12037593], [1.30664445, 0.45132705, 1.33018794, 0.37068026, 0.11339451]])
sliced
- y: 4
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2) - y(y)float64m0.0, 0.333, 0.667, 1.0
Values:
array([0. , 0.33333333, 0.66666667, 1. ])
- (y)float64m/s0.981, 0.230, 0.821, 0.402σ = 0.418, 0.159, 1.146, 1.153
Values:
array([0.98075951, 0.22968305, 0.82070207, 0.40156162])
Variances (σ²):
array([0.17499432, 0.0253596 , 1.31410843, 1.33018794])
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)float64m0.0, 0.333, 0.667, 1.0
Values:
array([0. , 0.33333333, 0.66666667, 1. ])
- a(y, x)float64𝟙0.0, 0.089, ..., 0.734, 0.220
Values:
array([[ 0. , 0.08942232, 0.57002943, 0.44891865, 0.16377353], [ 0. , 0.26549483, -0.14607974, 0.33994212, 0.60898894], [ 0.24824571, 0.94888115, 0.66723745, 0.09589794, 0.44183967], [ 0.88647992, 0.6974535 , 0.32647286, 0.73392816, 0.22013496]]) - b(y, x)float64m/s0.280, 0.390, ..., 0.903, 0.271σ = 0.100, 0.197, ..., 0.609, 0.337
Values:
array([[0.27962331, 0.38961276, 0.98075951, 0.83179325, 0.48106475], [0.40936113, 0.73591977, 0.22968305, 0.82748993, 1.15841752], [0.30534223, 1.16712382, 0.82070207, 0.11795446, 0.54346279], [1.0903703 , 0.8578678 , 0.40156162, 0.90273164, 0.270766 ]])
Variances (σ²):
array([[0.01007239, 0.03867971, 0.17499432, 0.32739863, 0.10738589], [1.00682155, 0.05652606, 0.0253596 , 0.01271203, 0.54204736], [1.10530011, 0.54759131, 1.31410843, 0.79473851, 1.12037593], [1.30664445, 0.45132705, 1.33018794, 0.37068026, 0.11339451]])
DataGroups:(0)
[13]:
<module 'scipp' from '/home/runner/work/scipp/scipp/.tox/docs/lib/python3.10/site-packages/scipp/__init__.py'>