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.319, 0.043, ..., 0.644, 0.708
Values:
array([[0.3190775 , 0.04266251, 0.58435288, 0.19796241, 0.73769976], [0.00802536, 0.22622086, 0.85263886, 0.52117018, 0.23658822], [0.22859141, 0.28637725, 0.75970833, 0.95115741, 0.31560759], [0.7067321 , 0.88800128, 0.20141595, 0.64350078, 0.70829665]])
[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.319, 0.043, ..., 0.644, 0.708
Values:
array([[0.3190775 , 0.04266251, 0.58435288, 0.19796241, 0.73769976], [0.00802536, 0.22622086, 0.85263886, 0.52117018, 0.23658822], [0.22859141, 0.28637725, 0.75970833, 0.95115741, 0.31560759], [0.7067321 , 0.88800128, 0.20141595, 0.64350078, 0.70829665]])
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.319, 0.043, ..., 0.644, 0.708σ = 0.780, 0.896, ..., 0.768, 0.006
Values:
array([[0.3190775 , 0.04266251, 0.58435288, 0.19796241, 0.73769976], [0.00802536, 0.22622086, 0.85263886, 0.52117018, 0.23658822], [0.22859141, 0.28637725, 0.75970833, 0.95115741, 0.31560759], [0.7067321 , 0.88800128, 0.20141595, 0.64350078, 0.70829665]])
Variances (σ²):
array([[6.09149305e-01, 8.02919767e-01, 2.54898352e-01, 3.04294168e-01, 7.26285145e-03], [8.35132324e-01, 1.45966768e-01, 2.33159223e-01, 9.15620961e-01, 8.33350991e-01], [8.16832506e-01, 2.91349767e-01, 1.75377229e-02, 8.56537731e-02, 6.38848348e-01], [3.00856392e-04, 2.02239956e-01, 6.54426902e-01, 5.89832101e-01, 3.59998041e-05]]) - b(y, x)float64𝟙0.319, 0.043, ..., 0.644, 0.708σ = 0.780, 0.896, ..., 0.768, 0.006
Values:
array([[0.3190775 , 0.04266251, 0.58435288, 0.19796241, 0.73769976], [0.00802536, 0.22622086, 0.85263886, 0.52117018, 0.23658822], [0.22859141, 0.28637725, 0.75970833, 0.95115741, 0.31560759], [0.7067321 , 0.88800128, 0.20141595, 0.64350078, 0.70829665]])
Variances (σ²):
array([[6.09149305e-01, 8.02919767e-01, 2.54898352e-01, 3.04294168e-01, 7.26285145e-03], [8.35132324e-01, 1.45966768e-01, 2.33159223e-01, 9.15620961e-01, 8.33350991e-01], [8.16832506e-01, 2.91349767e-01, 1.75377229e-02, 8.56537731e-02, 6.38848348e-01], [3.00856392e-04, 2.02239956e-01, 6.54426902e-01, 5.89832101e-01, 3.59998041e-05]]) - c(y)float64𝟙0.584, 0.853, 0.760, 0.201σ = 0.505, 0.483, 0.132, 0.809
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([0.58435288, 0.85263886, 0.75970833, 0.20141595])
Variances (σ²):
array([0.25489835, 0.23315922, 0.01753772, 0.6544269 ]) - 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.319078, 0.0426625, ..., 0.643501, 0.708297] [0.609149, 0.80292, ..., 0.589832, 3.59998e-05]
b float64 [dimensionless] (y, x) [0.319078, 0.0426625, ..., 0.643501, 0.708297] [0.609149, 0.80292, ..., 0.589832, 3.59998e-05]
c float64 [dimensionless] (y) [0.584353, 0.852639, 0.759708, 0.201416] [0.254898, 0.233159, 0.0175377, 0.654427]
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.33999, ..., 0.791506, 0.871205] [1.84316, 2.13632, ..., 0.892357, 5.44641e-05]
b float64 [m/s] (y, x) [0, -0.33999, ..., 0.791506, 0.871205] [1.84316, 2.13632, ..., 0.892357, 5.44641e-05]
c float64 [m/s] (y) [0.326289, 1.03887, 0.934441, 0.247742] [1.30722, 1.61622, 0.0265328, 0.990082]
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.340, ..., 0.792, 0.871σ = 1.358, 1.462, ..., 0.945, 0.007
Values:
array([[ 0. , -0.33999044, 0.32628871, -0.14897156, 0.51490538], [ 0. , 0.26838046, 1.0388746 , 0.63116813, 0.28113231], [ 0.28116744, 0.35224402, 0.93444125, 1.16992361, 0.38819733], [ 0.86928048, 1.09224157, 0.24774161, 0.79150596, 0.87120488]])
Variances (σ²):
array([[1.84316397e+00, 2.13631930e+00, 1.30721770e+00, 1.38194863e+00, 9.32569951e-01], [2.52694339e+00, 1.48430482e+00, 1.61621828e+00, 2.64871464e+00, 2.52424841e+00], [1.23578590e+00, 4.40783063e-01, 2.65328210e-02, 1.29585593e-01, 9.66513665e-01], [4.55165635e-04, 3.05968830e-01, 9.90082460e-01, 8.92356986e-01, 5.44641036e-05]])
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.340, ..., 0.792, 0.871σ = 1.358, 1.462, ..., 0.945, 0.007
Values:
array([[ 0. , -0.33999044, 0.32628871, -0.14897156, 0.51490538], [ 0. , 0.26838046, 1.0388746 , 0.63116813, 0.28113231], [ 0.28116744, 0.35224402, 0.93444125, 1.16992361, 0.38819733], [ 0.86928048, 1.09224157, 0.24774161, 0.79150596, 0.87120488]])
Variances (σ²):
array([[1.84316397e+00, 2.13631930e+00, 1.30721770e+00, 1.38194863e+00, 9.32569951e-01], [2.52694339e+00, 1.48430482e+00, 1.61621828e+00, 2.64871464e+00, 2.52424841e+00], [1.23578590e+00, 4.40783063e-01, 2.65328210e-02, 1.29585593e-01, 9.66513665e-01], [4.55165635e-04, 3.05968830e-01, 9.90082460e-01, 8.92356986e-01, 5.44641036e-05]])
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.340, ..., 0.792, 0.871σ = 1.358, 1.462, ..., 0.945, 0.007
Values:
array([[ 0. , -0.33999044, 0.32628871, -0.14897156, 0.51490538], [ 0. , 0.26838046, 1.0388746 , 0.63116813, 0.28113231], [ 0.28116744, 0.35224402, 0.93444125, 1.16992361, 0.38819733], [ 0.86928048, 1.09224157, 0.24774161, 0.79150596, 0.87120488]])
Variances (σ²):
array([[1.84316397e+00, 2.13631930e+00, 1.30721770e+00, 1.38194863e+00, 9.32569951e-01], [2.52694339e+00, 1.48430482e+00, 1.61621828e+00, 2.64871464e+00, 2.52424841e+00], [1.23578590e+00, 4.40783063e-01, 2.65328210e-02, 1.29585593e-01, 9.66513665e-01], [4.55165635e-04, 3.05968830e-01, 9.90082460e-01, 8.92356986e-01, 5.44641036e-05]]) - b(y, x)float64m/s0.0, -0.340, ..., 0.792, 0.871σ = 1.358, 1.462, ..., 0.945, 0.007
Values:
array([[ 0. , -0.33999044, 0.32628871, -0.14897156, 0.51490538], [ 0. , 0.26838046, 1.0388746 , 0.63116813, 0.28113231], [ 0.28116744, 0.35224402, 0.93444125, 1.16992361, 0.38819733], [ 0.86928048, 1.09224157, 0.24774161, 0.79150596, 0.87120488]])
Variances (σ²):
array([[1.84316397e+00, 2.13631930e+00, 1.30721770e+00, 1.38194863e+00, 9.32569951e-01], [2.52694339e+00, 1.48430482e+00, 1.61621828e+00, 2.64871464e+00, 2.52424841e+00], [1.23578590e+00, 4.40783063e-01, 2.65328210e-02, 1.29585593e-01, 9.66513665e-01], [4.55165635e-04, 3.05968830e-01, 9.90082460e-01, 8.92356986e-01, 5.44641036e-05]]) - c(y)float64m/s0.326, 1.039, 0.934, 0.248σ = 1.143, 1.271, 0.163, 0.995
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([0.32628871, 1.0388746 , 0.93444125, 0.24774161])
Variances (σ²):
array([1.3072177 , 1.61621828, 0.02653282, 0.99008246]) - scalar()float64m/s1.23
Values:
array(1.23)
[13]:
<module 'scipp' from '/home/simon/code/scipp/scipp/.tox/docs/lib/python3.8/site-packages/scipp/__init__.py'>