[1]:
import numpy as np
import scipp as sc

How to…#

Create variables#

Scalar variable using operators with units#

[2]:
1.2 * sc.units.kg
[2]:
Show/Hide data repr Show/Hide attributes
scipp.Variable (8 Bytes)
    • ()
      float64
      kg
      1.2
      Values:
      array(1.2)

Scalar variable from Python object#

[3]:
sc.scalar(1.2)
[3]:
Show/Hide data repr Show/Hide attributes
scipp.Variable (8 Bytes)
    • ()
      float64
      𝟙
      1.2
      Values:
      array(1.2)
[4]:
sc.scalar("string")
[4]:
Show/Hide data repr Show/Hide attributes
scipp.Variable (8 Bytes)
    • ()
      string
      string
      Values:
      'string'
[5]:
sc.scalar([1,2,3])
[5]:
Show/Hide data repr Show/Hide attributes
scipp.Variable (8 Bytes)
    • ()
      PyObject
      [1, 2, 3]
      Values:
      [1, 2, 3]

Initialized with default values#

[6]:
sc.zeros(dims=['y', 'x'], shape=(2,3))
[6]:
Show/Hide data repr Show/Hide attributes
scipp.Variable (48 Bytes)
    • (y: 2, x: 3)
      float64
      𝟙
      0.0, 0.0, ..., 0.0, 0.0
      Values:
      array([[0., 0., 0.], [0., 0., 0.]])
[7]:
sc.zeros(dims=['x'], shape=(2,), dtype=sc.DType.bool)
[7]:
Show/Hide data repr Show/Hide attributes
scipp.Variable (2 Bytes)
    • (x: 2)
      bool
      False, False
      Values:
      array([False, False])

From Python list or NumPy array#

[8]:
sc.Variable(dims=['x'], values=[1.2, 2.3])
[8]:
Show/Hide data repr Show/Hide attributes
scipp.Variable (16 Bytes)
    • (x: 2)
      float64
      𝟙
      1.2, 2.3
      Values:
      array([1.2, 2.3])
[9]:
sc.Variable(dims=['y', 'x'], values=np.array([[1, 2], [3, 4]]))
[9]:
Show/Hide data repr Show/Hide attributes
scipp.Variable (32 Bytes)
    • (y: 2, x: 2)
      int64
      𝟙
      1, 2, 3, 4
      Values:
      array([[1, 2], [3, 4]])

Create a data array#

[10]:
var = sc.Variable(dims=['x'], values=[1.0, 2.0])
[11]:
a = sc.DataArray(var)
a
[11]:
Show/Hide data repr Show/Hide attributes
scipp.DataArray (16 Bytes)
    • x: 2
    • (x)
      float64
      𝟙
      1.0, 2.0
      Values:
      array([1., 2.])

Create an empty dataset#

[12]:
d = sc.Dataset()
d
[12]:
Show/Hide data repr Show/Hide attributes
scipp.Dataset (0 Bytes)

    Add / insert a data item, a coord, a mask, or an attr#

    [13]:
    
    d['data'] = var
    d.coords['x'] = var
    d.coords['lab'] = var
    d
    
    [13]:
    
    Show/Hide data repr Show/Hide attributes
    scipp.Dataset (48 Bytes)
      • x: 2
      • lab
        (x)
        float64
        𝟙
        1.0, 2.0
        Values:
        array([1., 2.])
      • x
        (x)
        float64
        𝟙
        1.0, 2.0
        Values:
        array([1., 2.])
      • data
        (x)
        float64
        𝟙
        1.0, 2.0
        Values:
        array([1., 2.])
    [14]:
    
    a.coords['x'] = var
    a.coords['lab'] = var
    a.masks['mask'] = sc.Variable(dims=['x'], values=[False, True])
    a.attrs['info'] = sc.scalar("text")
    a
    
    [14]:
    
    Show/Hide data repr Show/Hide attributes
    scipp.DataArray (58 Bytes)
      • x: 2
      • lab
        (x)
        float64
        𝟙
        1.0, 2.0
        Values:
        array([1., 2.])
      • x
        (x)
        float64
        𝟙
        1.0, 2.0
        Values:
        array([1., 2.])
      • (x)
        float64
        𝟙
        1.0, 2.0
        Values:
        array([1., 2.])
      • mask
        (x)
        bool
        False, True
        Values:
        array([False, True])
      • info
        ()
        string
        text
        Values:
        'text'

    Get a data item, a coord, or a mask#

    [15]:
    
    d['data']
    
    [15]:
    
    Show/Hide data repr Show/Hide attributes
    scipp.DataArray (48 Bytes)
      • x: 2
      • lab
        (x)
        float64
        𝟙
        1.0, 2.0
        Values:
        array([1., 2.])
      • x
        (x)
        float64
        𝟙
        1.0, 2.0
        Values:
        array([1., 2.])
      • (x)
        float64
        𝟙
        1.0, 2.0
        Values:
        array([1., 2.])
    [16]:
    
    d.coords['x']
    
    [16]:
    
    Show/Hide data repr Show/Hide attributes
    scipp.Variable (16 Bytes)
      • (x: 2)
        float64
        𝟙
        1.0, 2.0
        Values:
        array([1., 2.])
    [17]:
    
    d.coords['lab']
    
    [17]:
    
    Show/Hide data repr Show/Hide attributes
    scipp.Variable (16 Bytes)
      • (x: 2)
        float64
        𝟙
        1.0, 2.0
        Values:
        array([1., 2.])
    [18]:
    
    a.masks['mask']
    
    [18]:
    
    Show/Hide data repr Show/Hide attributes
    scipp.Variable (2 Bytes)
      • (x: 2)
        bool
        False, True
        Values:
        array([False, True])
    [19]:
    
    a.attrs['info']
    
    [19]:
    
    Show/Hide data repr Show/Hide attributes
    scipp.Variable (8 Bytes)
      • ()
        string
        text
        Values:
        'text'

    Delete / remove a data item, a coord, a mask, or an attr#

    [20]:
    
    del d['data']
    del d.coords['x']
    del d.coords['lab']
    del a.masks['mask']
    del a.attrs['info']
    d
    
    [20]:
    
    Show/Hide data repr Show/Hide attributes
    scipp.Dataset (0 Bytes)
      • x: 2

      Copy variables, data arrays, or dataset#

      Note that scipp always makes a deep copy. This includes the case of storing native Python objects in a variable.

      [21]:
      
      deep_copy = d.copy()         # Option 1
      import copy
      deep_copy = copy.copy(d)     # Option 2
      deep_copy = copy.deepcopy(d) # Option 3
      

      Again, note that all three options result in a deep copy. The syntax is the same for variables, data arrays, and datasets.

      Create stand-alone objects from (slice) views#

      Views created by slicing, or from selecting an item in a dataset reference data in the original container. Use copy to convert a view into a stand alone object:

      [22]:
      
      deep_copy = var['x', 0:1].copy()
      deep_copy = copy.copy(var['x', 0:1])
      
      [23]:
      
      d['data'] = var
      deep_copy = d['data'].copy()
      deep_copy = copy.copy(d['data'])