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.651, 0.860, ..., 0.736, 0.327
Values:
array([[0.65098239, 0.86044099, 0.43594134, 0.11731904, 0.80448348], [0.76675464, 0.04289888, 0.04183491, 0.93360158, 0.53752035], [0.28852665, 0.66897157, 0.00459487, 0.96690272, 0.71799225], [0.20426842, 0.87737024, 0.82512391, 0.73618549, 0.32716409]])
[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.651, 0.860, ..., 0.736, 0.327
Values:
array([[0.65098239, 0.86044099, 0.43594134, 0.11731904, 0.80448348], [0.76675464, 0.04289888, 0.04183491, 0.93360158, 0.53752035], [0.28852665, 0.66897157, 0.00459487, 0.96690272, 0.71799225], [0.20426842, 0.87737024, 0.82512391, 0.73618549, 0.32716409]])
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.651, 0.860, ..., 0.736, 0.327σ = 0.266, 0.701, ..., 0.238, 0.613
Values:
array([[0.65098239, 0.86044099, 0.43594134, 0.11731904, 0.80448348], [0.76675464, 0.04289888, 0.04183491, 0.93360158, 0.53752035], [0.28852665, 0.66897157, 0.00459487, 0.96690272, 0.71799225], [0.20426842, 0.87737024, 0.82512391, 0.73618549, 0.32716409]])
Variances (σ²):
array([[7.08913566e-02, 4.91558630e-01, 1.36393885e-01, 9.73335179e-01, 2.19480374e-04], [1.01892712e-01, 1.84912574e-01, 4.23904054e-02, 2.31883466e-01, 8.80681581e-02], [3.15671080e-02, 3.72781980e-03, 8.01926496e-01, 1.20596261e-01, 2.89413432e-01], [1.47826795e-01, 4.26541180e-01, 6.58204624e-01, 5.64865778e-02, 3.75219716e-01]]) - b(y, x)float64𝟙0.651, 0.860, ..., 0.736, 0.327σ = 0.266, 0.701, ..., 0.238, 0.613
Values:
array([[0.65098239, 0.86044099, 0.43594134, 0.11731904, 0.80448348], [0.76675464, 0.04289888, 0.04183491, 0.93360158, 0.53752035], [0.28852665, 0.66897157, 0.00459487, 0.96690272, 0.71799225], [0.20426842, 0.87737024, 0.82512391, 0.73618549, 0.32716409]])
Variances (σ²):
array([[7.08913566e-02, 4.91558630e-01, 1.36393885e-01, 9.73335179e-01, 2.19480374e-04], [1.01892712e-01, 1.84912574e-01, 4.23904054e-02, 2.31883466e-01, 8.80681581e-02], [3.15671080e-02, 3.72781980e-03, 8.01926496e-01, 1.20596261e-01, 2.89413432e-01], [1.47826795e-01, 4.26541180e-01, 6.58204624e-01, 5.64865778e-02, 3.75219716e-01]]) - c(y)float64𝟙0.436, 0.042, 0.005, 0.825σ = 0.369, 0.206, 0.896, 0.811
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([0.43594134, 0.04183491, 0.00459487, 0.82512391])
Variances (σ²):
array([0.13639388, 0.04239041, 0.8019265 , 0.65820462]) - 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.289±0.178 | 0.289±0.178 |
1 | 1 | 0.669±0.061 | 0.669±0.061 |
2 | 2 | 0.005±0.896 | 0.005±0.896 |
3 | 3 | 0.967±0.347 | 0.967±0.347 |
4 | 4 | 0.718±0.538 | 0.718±0.538 |
[10]:
sc.plot(dataset)
[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.650982, 0.860441, ..., 0.736185, 0.327164] [0.0708914, 0.491559, ..., 0.0564866, 0.37522]
b float64 [dimensionless] (y, x) [0.650982, 0.860441, ..., 0.736185, 0.327164] [0.0708914, 0.491559, ..., 0.0564866, 0.37522]
c float64 [dimensionless] (y) [0.435941, 0.0418349, 0.00459487, 0.825124] [0.136394, 0.0423904, 0.801926, 0.658205]
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.257634, ..., 0.905508, 0.402412] [0.214503, 0.850931, ..., 0.0854585, 0.56767]
b float64 [m/s] (y, x) [0, 0.257634, ..., 0.905508, 0.402412] [0.214503, 0.850931, ..., 0.0854585, 0.56767]
c float64 [m/s] (y) [-0.2645, -0.891651, 0.00565169, 1.0149] [0.313602, 0.218286, 1.21323, 0.995798]
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.258, ..., 0.906, 0.402σ = 0.463, 0.922, ..., 0.292, 0.753
Values:
array([[ 0. , 0.25763407, -0.2645005 , -0.65640592, 0.18880634], [ 0. , -0.89034258, -0.89165126, 0.20522174, -0.28195817], [ 0.35488778, 0.82283503, 0.00565169, 1.18929034, 0.88313047], [ 0.25125015, 1.0791654 , 1.01490241, 0.90550815, 0.40241182]])
Variances (σ²):
array([[0.21450307, 0.85093058, 0.31360184, 1.57981033, 0.10758359], [0.30830697, 0.43390772, 0.21828593, 0.50496998, 0.2873918 ], [0.04775788, 0.00563982, 1.2132346 , 0.18245008, 0.43785358], [0.22364716, 0.64531415, 0.99579778, 0.08545854, 0.56766991]])
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.258, ..., 0.906, 0.402σ = 0.463, 0.922, ..., 0.292, 0.753
Values:
array([[ 0. , 0.25763407, -0.2645005 , -0.65640592, 0.18880634], [ 0. , -0.89034258, -0.89165126, 0.20522174, -0.28195817], [ 0.35488778, 0.82283503, 0.00565169, 1.18929034, 0.88313047], [ 0.25125015, 1.0791654 , 1.01490241, 0.90550815, 0.40241182]])
Variances (σ²):
array([[0.21450307, 0.85093058, 0.31360184, 1.57981033, 0.10758359], [0.30830697, 0.43390772, 0.21828593, 0.50496998, 0.2873918 ], [0.04775788, 0.00563982, 1.2132346 , 0.18245008, 0.43785358], [0.22364716, 0.64531415, 0.99579778, 0.08545854, 0.56766991]])
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.258, ..., 0.906, 0.402σ = 0.463, 0.922, ..., 0.292, 0.753
Values:
array([[ 0. , 0.25763407, -0.2645005 , -0.65640592, 0.18880634], [ 0. , -0.89034258, -0.89165126, 0.20522174, -0.28195817], [ 0.35488778, 0.82283503, 0.00565169, 1.18929034, 0.88313047], [ 0.25125015, 1.0791654 , 1.01490241, 0.90550815, 0.40241182]])
Variances (σ²):
array([[0.21450307, 0.85093058, 0.31360184, 1.57981033, 0.10758359], [0.30830697, 0.43390772, 0.21828593, 0.50496998, 0.2873918 ], [0.04775788, 0.00563982, 1.2132346 , 0.18245008, 0.43785358], [0.22364716, 0.64531415, 0.99579778, 0.08545854, 0.56766991]]) - b(y, x)float64m/s0.0, 0.258, ..., 0.906, 0.402σ = 0.463, 0.922, ..., 0.292, 0.753
Values:
array([[ 0. , 0.25763407, -0.2645005 , -0.65640592, 0.18880634], [ 0. , -0.89034258, -0.89165126, 0.20522174, -0.28195817], [ 0.35488778, 0.82283503, 0.00565169, 1.18929034, 0.88313047], [ 0.25125015, 1.0791654 , 1.01490241, 0.90550815, 0.40241182]])
Variances (σ²):
array([[0.21450307, 0.85093058, 0.31360184, 1.57981033, 0.10758359], [0.30830697, 0.43390772, 0.21828593, 0.50496998, 0.2873918 ], [0.04775788, 0.00563982, 1.2132346 , 0.18245008, 0.43785358], [0.22364716, 0.64531415, 0.99579778, 0.08545854, 0.56766991]]) - c(y)float64m/s-0.265, -0.892, 0.006, 1.015σ = 0.560, 0.467, 1.101, 0.998
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([-0.2645005 , -0.89165126, 0.00565169, 1.01490241])
Variances (σ²):
array([0.31360184, 0.21828593, 1.2132346 , 0.99579778]) - 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'>