Multi-dimensional data arrays with labeled dimensions for scientific computing
—
Functions for creating arrays, scalars, vectors, and structured data with comprehensive support for physical units, dimensions, and uncertainty propagation. These functions provide the building blocks for all scipp data structures.
Create arrays from data with explicit dimension labels, units, and optional variance information.
def array(dims, values, *, variances=None, unit=None, dtype=None):
"""
Create a Variable from array-like data
Args:
dims (str or Sequence[str]): Dimension labels
values (array-like): Data values
variances (array-like, optional): Variance values for uncertainty
unit (Unit or str, optional): Physical unit
dtype (DType or str, optional): Data type override
Returns:
Variable: New variable with specified data
"""
def scalar(value, *, variance=None, unit=None, dtype=None):
"""
Create a zero-dimensional Variable (scalar)
Args:
value: Scalar value
variance (optional): Scalar variance for uncertainty
unit (Unit or str, optional): Physical unit
dtype (DType or str, optional): Data type override
Returns:
Variable: Zero-dimensional variable
"""
def index(value):
"""
Create an integer index scalar
Args:
value (int): Index value
Returns:
Variable: Integer scalar variable
"""Create arrays filled with specific values (zeros, ones, or custom values) with specified shapes and dimensions.
def zeros(dims, shape, *, unit=None, dtype=None):
"""
Create array filled with zeros
Args:
dims (str or Sequence[str]): Dimension labels
shape (int or Sequence[int]): Shape along each dimension
unit (Unit or str, optional): Physical unit
dtype (DType or str, optional): Data type
Returns:
Variable: Array filled with zeros
"""
def ones(dims, shape, *, unit=None, dtype=None):
"""
Create array filled with ones
Args:
dims (str or Sequence[str]): Dimension labels
shape (int or Sequence[int]): Shape along each dimension
unit (Unit or str, optional): Physical unit
dtype (DType or str, optional): Data type
Returns:
Variable: Array filled with ones
"""
def empty(dims, shape, *, unit=None, dtype=None):
"""
Create uninitialized array
Args:
dims (str or Sequence[str]): Dimension labels
shape (int or Sequence[int]): Shape along each dimension
unit (Unit or str, optional): Physical unit
dtype (DType or str, optional): Data type
Returns:
Variable: Uninitialized array
"""
def full(dims, shape, value, *, unit=None, dtype=None):
"""
Create array filled with specified value
Args:
dims (str or Sequence[str]): Dimension labels
shape (int or Sequence[int]): Shape along each dimension
value: Fill value
unit (Unit or str, optional): Physical unit
dtype (DType or str, optional): Data type
Returns:
Variable: Array filled with value
"""Create arrays with the same shape and structure as existing variables.
def zeros_like(x):
"""
Create zeros array with same shape as input
Args:
x (Variable or DataArray): Template variable
Returns:
Variable: Zeros array with same shape and dimensions
"""
def ones_like(x):
"""
Create ones array with same shape as input
Args:
x (Variable or DataArray): Template variable
Returns:
Variable: Ones array with same shape and dimensions
"""
def empty_like(x):
"""
Create uninitialized array with same shape as input
Args:
x (Variable or DataArray): Template variable
Returns:
Variable: Uninitialized array with same shape and dimensions
"""
def full_like(x, value):
"""
Create filled array with same shape as input
Args:
x (Variable or DataArray): Template variable
value: Fill value
Returns:
Variable: Filled array with same shape and dimensions
"""Generate sequences and ranges with specified spacing and physical units.
def linspace(dim, start, stop, num, *, unit=None, dtype=None, endpoint=True):
"""
Create linearly spaced values
Args:
dim (str): Dimension label
start: Start value
stop: Stop value
num (int): Number of values
unit (Unit or str, optional): Physical unit
dtype (DType or str, optional): Data type
endpoint (bool): Include stop value
Returns:
Variable: Linearly spaced values
"""
def geomspace(dim, start, stop, num, *, unit=None, dtype=None, endpoint=True):
"""
Create geometrically spaced values
Args:
dim (str): Dimension label
start: Start value (must be positive)
stop: Stop value (must be positive)
num (int): Number of values
unit (Unit or str, optional): Physical unit
dtype (DType or str, optional): Data type
endpoint (bool): Include stop value
Returns:
Variable: Geometrically spaced values
"""
def logspace(dim, start, stop, num, *, base=10.0, unit=None, dtype=None, endpoint=True):
"""
Create logarithmically spaced values
Args:
dim (str): Dimension label
start: Start exponent
stop: Stop exponent
num (int): Number of values
base (float): Base of logarithm
unit (Unit or str, optional): Physical unit
dtype (DType or str, optional): Data type
endpoint (bool): Include stop value
Returns:
Variable: Logarithmically spaced values
"""
def arange(dim, start, stop=None, step=None, *, unit=None, dtype=None):
"""
Create range with specified step
Args:
dim (str): Dimension label
start: Start value (or stop if stop is None)
stop (optional): Stop value
step (optional): Step size
unit (Unit or str, optional): Physical unit
dtype (DType or str, optional): Data type
Returns:
Variable: Range of values
"""Create specialized data types for 3D vectors and transformation matrices.
def vector(value, *, unit=None):
"""
Create a scalar 3D vector variable
Args:
value (array-like): 3-element vector [x, y, z]
unit (Unit or str, optional): Physical unit
Returns:
Variable: Scalar variable with vector3 dtype
"""
def vectors(dims, values, *, unit=None):
"""
Create array of 3D vectors
Args:
dims (str or Sequence[str]): Dimension labels
values (array-like): Array of 3-element vectors
unit (Unit or str, optional): Physical unit
Returns:
Variable: Array variable with vector3 dtype
"""Create datetime variables for time-series data with proper temporal units.
def datetime(year=1970, month=1, day=1, hour=0, minute=0, second=0, *, unit='s'):
"""
Create datetime scalar
Args:
year (int): Year
month (int): Month
day (int): Day
hour (int): Hour
minute (int): Minute
second (int): Second
unit (str): Time unit
Returns:
Variable: Datetime scalar
"""
def datetimes(dims, values, *, unit='s'):
"""
Create array of datetimes
Args:
dims (str or Sequence[str]): Dimension labels
values (array-like): Datetime values
unit (str): Time unit
Returns:
Variable: Datetime array
"""
def epoch(*, unit='s'):
"""
Unix epoch time reference
Args:
unit (str): Time unit
Returns:
Variable: Unix epoch reference time
"""import scipp as sc
import numpy as np
# Create 1D array with units
data = sc.array(dims=['x'], values=[1, 2, 3, 4, 5], unit='m')
# Create 2D array with uncertainties
measurements = sc.array(
dims=['y', 'x'],
values=np.random.random((3, 5)),
variances=np.random.random((3, 5)) * 0.01,
unit='counts'
)
# Create scalar with uncertainty
reference = sc.scalar(value=1.23, variance=0.01, unit='kg')# Create zero-filled 2D array
zeros_2d = sc.zeros(dims=['y', 'x'], shape=[10, 20], unit='V')
# Create ones with specific data type
ones_int = sc.ones(dims=['time'], shape=[100], dtype='int32')
# Create array filled with specific value
baseline = sc.full(dims=['detector'], shape=[256], value=1.5, unit='counts')# Time coordinate with regular spacing
time = sc.linspace(dim='time', start=0, stop=10, num=101, unit='s')
# Energy coordinate with logarithmic spacing
energy = sc.geomspace(dim='energy', start=1e-3, stop=1e3, num=50, unit='eV')
# Detector positions as 3D vectors
positions = sc.vectors(
dims=['detector'],
values=np.random.random((100, 3)),
unit='m'
)from datetime import datetime
# Create time stamps
start_time = sc.datetime(2023, 1, 1, 12, 0, 0, unit='s')
# Create time series with regular intervals
time_series = start_time + sc.arange(dim='time', start=0, stop=3600, step=60, unit='s')
# Create measurement data with time coordinate
temperature_data = sc.array(
dims=['time'],
values=20 + np.random.random(60) * 5,
unit='degC'
)
temperature_series = sc.DataArray(
data=temperature_data,
coords={'time': time_series}
)# Create template array
template = sc.zeros(dims=['y', 'x'], shape=[5, 10], unit='m')
# Create similar arrays
field_x = sc.ones_like(template) # Same shape, filled with ones
field_y = sc.full_like(template, -1.5) # Same shape, filled with -1.5
# Create uninitialized array for computation results
result = sc.empty_like(template)Install with Tessl CLI
npx tessl i tessl/pypi-scipp