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.260, 0.853, ..., 0.398, 0.899
Values:
array([[0.26033909, 0.85252177, 0.01754777, 0.58943346, 0.82274093], [0.23873476, 0.03352047, 0.46069816, 0.48064053, 0.39939353], [0.54723983, 0.50138113, 0.43964187, 0.45008749, 0.38475728], [0.24076066, 0.1691947 , 0.88384524, 0.39818341, 0.89947434]])
[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.260, 0.853, ..., 0.398, 0.899
Values:
array([[0.26033909, 0.85252177, 0.01754777, 0.58943346, 0.82274093], [0.23873476, 0.03352047, 0.46069816, 0.48064053, 0.39939353], [0.54723983, 0.50138113, 0.43964187, 0.45008749, 0.38475728], [0.24076066, 0.1691947 , 0.88384524, 0.39818341, 0.89947434]])
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.260, 0.853, ..., 0.398, 0.899σ = 0.048, 0.446, ..., 0.496, 0.903
Values:
array([[0.26033909, 0.85252177, 0.01754777, 0.58943346, 0.82274093], [0.23873476, 0.03352047, 0.46069816, 0.48064053, 0.39939353], [0.54723983, 0.50138113, 0.43964187, 0.45008749, 0.38475728], [0.24076066, 0.1691947 , 0.88384524, 0.39818341, 0.89947434]])
Variances (σ²):
array([[0.00234395, 0.19922919, 0.90721292, 0.84677421, 0.33605429], [0.06038284, 0.02246221, 0.06394219, 0.76439184, 0.00176963], [0.32437777, 0.77124412, 0.17160697, 0.00566756, 0.13931449], [0.16567086, 0.05714881, 0.33091188, 0.24559291, 0.81464395]]) - b(y, x)float64𝟙0.260, 0.853, ..., 0.398, 0.899σ = 0.048, 0.446, ..., 0.496, 0.903
Values:
array([[0.26033909, 0.85252177, 0.01754777, 0.58943346, 0.82274093], [0.23873476, 0.03352047, 0.46069816, 0.48064053, 0.39939353], [0.54723983, 0.50138113, 0.43964187, 0.45008749, 0.38475728], [0.24076066, 0.1691947 , 0.88384524, 0.39818341, 0.89947434]])
Variances (σ²):
array([[0.00234395, 0.19922919, 0.90721292, 0.84677421, 0.33605429], [0.06038284, 0.02246221, 0.06394219, 0.76439184, 0.00176963], [0.32437777, 0.77124412, 0.17160697, 0.00566756, 0.13931449], [0.16567086, 0.05714881, 0.33091188, 0.24559291, 0.81464395]]) - c(y)float64𝟙0.018, 0.461, 0.440, 0.884σ = 0.952, 0.253, 0.414, 0.575
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([0.01754777, 0.46069816, 0.43964187, 0.88384524])
Variances (σ²):
array([0.90721292, 0.06394219, 0.17160697, 0.33091188]) - 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.260339, 0.852522, ..., 0.398183, 0.899474] [0.00234395, 0.199229, ..., 0.245593, 0.814644]
b float64 [dimensionless] (y, x) [0.260339, 0.852522, ..., 0.398183, 0.899474] [0.00234395, 0.199229, ..., 0.245593, 0.814644]
c float64 [dimensionless] (y) [0.0175478, 0.460698, 0.439642, 0.883845] [0.907213, 0.0639422, 0.171607, 0.330912]
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.728385, ..., 0.489766, 1.10635] [0.00709231, 0.30496, ..., 0.371558, 1.23247]
b float64 [m/s] (y, x) [0, 0.728385, ..., 0.489766, 1.10635] [0.00709231, 0.30496, ..., 0.371558, 1.23247]
c float64 [m/s] (y) [-0.298633, 0.273015, 0.54076, 1.08713] [1.37607, 0.188091, 0.259624, 0.500637]
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.728, ..., 0.490, 1.106σ = 0.084, 0.552, ..., 0.610, 1.110
Values:
array([[ 0. , 0.72838469, -0.29863333, 0.40478607, 0.69175426], [ 0. , -0.25241358, 0.27301499, 0.2975441 , 0.19761029], [ 0.67310499, 0.61669879, 0.54075951, 0.55360762, 0.47325146], [ 0.29613561, 0.20810948, 1.08712965, 0.48976559, 1.10635343]])
Variances (σ²):
array([[0.00709231, 0.30496 , 1.37606858, 1.28463085, 0.51196269], [0.18270641, 0.12533628, 0.18809134, 1.24780163, 0.09403049], [0.49075113, 1.16681522, 0.25962419, 0.00857445, 0.2107689 ], [0.25064344, 0.08646044, 0.50063658, 0.37155752, 1.23247484]])
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.728, ..., 0.490, 1.106σ = 0.084, 0.552, ..., 0.610, 1.110
Values:
array([[ 0. , 0.72838469, -0.29863333, 0.40478607, 0.69175426], [ 0. , -0.25241358, 0.27301499, 0.2975441 , 0.19761029], [ 0.67310499, 0.61669879, 0.54075951, 0.55360762, 0.47325146], [ 0.29613561, 0.20810948, 1.08712965, 0.48976559, 1.10635343]])
Variances (σ²):
array([[0.00709231, 0.30496 , 1.37606858, 1.28463085, 0.51196269], [0.18270641, 0.12533628, 0.18809134, 1.24780163, 0.09403049], [0.49075113, 1.16681522, 0.25962419, 0.00857445, 0.2107689 ], [0.25064344, 0.08646044, 0.50063658, 0.37155752, 1.23247484]])
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.728, ..., 0.490, 1.106σ = 0.084, 0.552, ..., 0.610, 1.110
Values:
array([[ 0. , 0.72838469, -0.29863333, 0.40478607, 0.69175426], [ 0. , -0.25241358, 0.27301499, 0.2975441 , 0.19761029], [ 0.67310499, 0.61669879, 0.54075951, 0.55360762, 0.47325146], [ 0.29613561, 0.20810948, 1.08712965, 0.48976559, 1.10635343]])
Variances (σ²):
array([[0.00709231, 0.30496 , 1.37606858, 1.28463085, 0.51196269], [0.18270641, 0.12533628, 0.18809134, 1.24780163, 0.09403049], [0.49075113, 1.16681522, 0.25962419, 0.00857445, 0.2107689 ], [0.25064344, 0.08646044, 0.50063658, 0.37155752, 1.23247484]]) - b(y, x)float64m/s0.0, 0.728, ..., 0.490, 1.106σ = 0.084, 0.552, ..., 0.610, 1.110
Values:
array([[ 0. , 0.72838469, -0.29863333, 0.40478607, 0.69175426], [ 0. , -0.25241358, 0.27301499, 0.2975441 , 0.19761029], [ 0.67310499, 0.61669879, 0.54075951, 0.55360762, 0.47325146], [ 0.29613561, 0.20810948, 1.08712965, 0.48976559, 1.10635343]])
Variances (σ²):
array([[0.00709231, 0.30496 , 1.37606858, 1.28463085, 0.51196269], [0.18270641, 0.12533628, 0.18809134, 1.24780163, 0.09403049], [0.49075113, 1.16681522, 0.25962419, 0.00857445, 0.2107689 ], [0.25064344, 0.08646044, 0.50063658, 0.37155752, 1.23247484]]) - c(y)float64m/s-0.299, 0.273, 0.541, 1.087σ = 1.173, 0.434, 0.510, 0.708
- aux()int64m2
Values:
array(2) - x()int64m2
Values:
array(2)
Values:
array([-0.29863333, 0.27301499, 0.54075951, 1.08712965])
Variances (σ²):
array([1.37606858, 0.18809134, 0.25962419, 0.50063658]) - 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'>