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.098, 0.924, ..., 0.330, 0.328
Values:
array([[0.09833837, 0.92407188, 0.4903907 , 0.2810445 , 0.1546288 ], [0.2066901 , 0.93029172, 0.46673729, 0.43659741, 0.94705058], [0.22550587, 0.84341108, 0.24288544, 0.4037169 , 0.19883732], [0.98451595, 0.35153845, 0.50836422, 0.33013965, 0.32765522]])
[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.098, 0.924, ..., 0.330, 0.328
Values:
array([[0.09833837, 0.92407188, 0.4903907 , 0.2810445 , 0.1546288 ], [0.2066901 , 0.93029172, 0.46673729, 0.43659741, 0.94705058], [0.22550587, 0.84341108, 0.24288544, 0.4037169 , 0.19883732], [0.98451595, 0.35153845, 0.50836422, 0.33013965, 0.32765522]])
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.098, 0.924, ..., 0.330, 0.328σ = 0.937, 0.419, ..., 0.005, 0.747
Values:
array([[0.09833837, 0.92407188, 0.4903907 , 0.2810445 , 0.1546288 ], [0.2066901 , 0.93029172, 0.46673729, 0.43659741, 0.94705058], [0.22550587, 0.84341108, 0.24288544, 0.4037169 , 0.19883732], [0.98451595, 0.35153845, 0.50836422, 0.33013965, 0.32765522]])
Variances (σ²):
array([[8.78392079e-01, 1.75915875e-01, 4.75703524e-01, 5.14279461e-02, 6.72308928e-01], [6.12471541e-02, 9.61468611e-01, 5.75805926e-01, 1.99061685e-01, 1.03396748e-02], [8.11318841e-01, 1.52997946e-02, 1.10770470e-02, 7.21371355e-01, 1.15686885e-01], [7.69980819e-02, 2.36799147e-01, 4.67681817e-01, 2.42837776e-05, 5.58411543e-01]]) - b(y, x)float64𝟙0.098, 0.924, ..., 0.330, 0.328σ = 0.937, 0.419, ..., 0.005, 0.747
Values:
array([[0.09833837, 0.92407188, 0.4903907 , 0.2810445 , 0.1546288 ], [0.2066901 , 0.93029172, 0.46673729, 0.43659741, 0.94705058], [0.22550587, 0.84341108, 0.24288544, 0.4037169 , 0.19883732], [0.98451595, 0.35153845, 0.50836422, 0.33013965, 0.32765522]])
Variances (σ²):
array([[8.78392079e-01, 1.75915875e-01, 4.75703524e-01, 5.14279461e-02, 6.72308928e-01], [6.12471541e-02, 9.61468611e-01, 5.75805926e-01, 1.99061685e-01, 1.03396748e-02], [8.11318841e-01, 1.52997946e-02, 1.10770470e-02, 7.21371355e-01, 1.15686885e-01], [7.69980819e-02, 2.36799147e-01, 4.67681817e-01, 2.42837776e-05, 5.58411543e-01]]) - c(y)float64𝟙0.490, 0.467, 0.243, 0.508σ = 0.690, 0.759, 0.105, 0.684
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([0.4903907 , 0.46673729, 0.24288544, 0.50836422])
Variances (σ²):
array([0.47570352, 0.57580593, 0.01107705, 0.46768182]) - 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, ..., 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.0983384, 0.924072, ..., 0.33014, 0.327655] [0.878392, 0.175916, ..., 2.42838e-05, 0.558412]
b float64 [dimensionless] (y, x) [0.0983384, 0.924072, ..., 0.33014, 0.327655] [0.878392, 0.175916, ..., 2.42838e-05, 0.558412]
c float64 [dimensionless] (y) [0.490391, 0.466737, 0.242885, 0.508364] [0.475704, 0.575806, 0.011077, 0.467682]
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, 1.01565, ..., 0.406072, 0.403016] [2.65784, 1.59506, ..., 3.67389e-05, 0.844821]
b float64 [m/s] (y, x) [0, 1.01565, ..., 0.406072, 0.403016] [2.65784, 1.59506, ..., 3.67389e-05, 0.844821]
c float64 [m/s] (y) [0.482224, 0.319858, 0.298749, 0.625288] [2.04861, 0.963798, 0.0167585, 0.707556]
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, 1.016, ..., 0.406, 0.403σ = 1.630, 1.263, ..., 0.006, 0.919
Values:
array([[0. , 1.01565222, 0.48222436, 0.22472853, 0.06923723], [0. , 0.89003 , 0.31985805, 0.28278599, 0.91064339], [0.27737222, 1.03739563, 0.29874909, 0.49657179, 0.2445699 ], [1.21095461, 0.43239229, 0.62528799, 0.40607177, 0.40301592]])
Variances (σ²):
array([[2.65783875e+00, 1.59506250e+00, 2.04861124e+00, 1.40672472e+00, 2.34605555e+00], [1.85321639e-01, 1.54726668e+00, 9.63797605e-01, 3.93821243e-01, 1.08303714e-01], [1.22744427e+00, 2.31470592e-02, 1.67584644e-02, 1.09136272e+00, 1.75022689e-01], [1.16490398e-01, 3.58253430e-01, 7.07555820e-01, 3.67389272e-05, 8.44820823e-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, 1.016, ..., 0.406, 0.403σ = 1.630, 1.263, ..., 0.006, 0.919
Values:
array([[0. , 1.01565222, 0.48222436, 0.22472853, 0.06923723], [0. , 0.89003 , 0.31985805, 0.28278599, 0.91064339], [0.27737222, 1.03739563, 0.29874909, 0.49657179, 0.2445699 ], [1.21095461, 0.43239229, 0.62528799, 0.40607177, 0.40301592]])
Variances (σ²):
array([[2.65783875e+00, 1.59506250e+00, 2.04861124e+00, 1.40672472e+00, 2.34605555e+00], [1.85321639e-01, 1.54726668e+00, 9.63797605e-01, 3.93821243e-01, 1.08303714e-01], [1.22744427e+00, 2.31470592e-02, 1.67584644e-02, 1.09136272e+00, 1.75022689e-01], [1.16490398e-01, 3.58253430e-01, 7.07555820e-01, 3.67389272e-05, 8.44820823e-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, 1.016, ..., 0.406, 0.403σ = 1.630, 1.263, ..., 0.006, 0.919
Values:
array([[0. , 1.01565222, 0.48222436, 0.22472853, 0.06923723], [0. , 0.89003 , 0.31985805, 0.28278599, 0.91064339], [0.27737222, 1.03739563, 0.29874909, 0.49657179, 0.2445699 ], [1.21095461, 0.43239229, 0.62528799, 0.40607177, 0.40301592]])
Variances (σ²):
array([[2.65783875e+00, 1.59506250e+00, 2.04861124e+00, 1.40672472e+00, 2.34605555e+00], [1.85321639e-01, 1.54726668e+00, 9.63797605e-01, 3.93821243e-01, 1.08303714e-01], [1.22744427e+00, 2.31470592e-02, 1.67584644e-02, 1.09136272e+00, 1.75022689e-01], [1.16490398e-01, 3.58253430e-01, 7.07555820e-01, 3.67389272e-05, 8.44820823e-01]]) - b(y, x)float64m/s0.0, 1.016, ..., 0.406, 0.403σ = 1.630, 1.263, ..., 0.006, 0.919
Values:
array([[0. , 1.01565222, 0.48222436, 0.22472853, 0.06923723], [0. , 0.89003 , 0.31985805, 0.28278599, 0.91064339], [0.27737222, 1.03739563, 0.29874909, 0.49657179, 0.2445699 ], [1.21095461, 0.43239229, 0.62528799, 0.40607177, 0.40301592]])
Variances (σ²):
array([[2.65783875e+00, 1.59506250e+00, 2.04861124e+00, 1.40672472e+00, 2.34605555e+00], [1.85321639e-01, 1.54726668e+00, 9.63797605e-01, 3.93821243e-01, 1.08303714e-01], [1.22744427e+00, 2.31470592e-02, 1.67584644e-02, 1.09136272e+00, 1.75022689e-01], [1.16490398e-01, 3.58253430e-01, 7.07555820e-01, 3.67389272e-05, 8.44820823e-01]]) - c(y)float64m/s0.482, 0.320, 0.299, 0.625σ = 1.431, 0.982, 0.129, 0.841
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([0.48222436, 0.31985805, 0.29874909, 0.62528799])
Variances (σ²):
array([2.04861124, 0.96379761, 0.01675846, 0.70755582]) - 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'>