scipp.Variable#
- class scipp.Variable#
Array of values with dimension labels and a unit, optionally including an array of variances.
- __init__(self: scipp._scipp.core.Variable, *, dims: object, values: object = None, variances: object = None, unit: Union[str, scipp._scipp.core.Unit, None, scipp._scipp.core.DefaultUnit] = <automatically deduced unit>, dtype: object = None, aligned: bool = True) None #
Initialize a variable with values and/or variances.
At least one argument of
values
andvariances
must be used. if you want to preallocate memory to fill later, usescipp.empty()
.Attention
This constructor is meant primarily for internal use. Use one of the Specialized creation functions instead. See in particular
scipp.array()
andscipp.scalar()
.- Parameters:
dims – Dimension labels.
values – Sequence of values for constructing an array variable.
variances – Sequence of variances for constructing an array variable.
unit – Physical unit, defaults to
scipp.units.dimensionless
.dtype – Type of the variable’s elements. Is deduced from other arguments in most cases. Defaults to
sc.DType.float64
if no deduction is possible.aligned – Initial value for the alignment flag.
Methods
__init__
(self, *, dims, values, variances, ...)Initialize a variable with values and/or variances.
all
([dim])Logical AND over input values.
any
([dim])Logical OR over input values.
astype
(self, type, *[, copy])Converts a Variable or DataArray to a different dtype.
bin
([arg_dict, dim])Create binned data by binning input along all dimensions given by edges.
broadcast
(*[, dims, shape, sizes])Broadcast a Variable or a DataArray.
ceil
(*[, out])Round up to the nearest integer of all values passed in x.
copy
(self[, deep])Return a (by default deep) copy.
cumsum
([dim, mode])Return the cumulative sum along the specified dimension.
flatten
([dims, to])Flatten multiple dimensions into a single dimension.
floor
(*[, out])Round down to the nearest integer of all values passed in x.
fold
(dim, *[, dims, shape, sizes])Fold a single dimension of a variable or data array into multiple dims.
hist
([arg_dict, dim])Compute a histogram.
max
([dim])Maximum of elements in the input.
mean
([dim])Arithmetic mean of elements in the input.
median
([dim])Compute the median of the input values.
min
([dim])Minimum of elements in the input.
nanhist
([arg_dict, dim])Compute a histogram, skipping NaN values.
nanmax
([dim])Maximum of elements in the input ignoring NaN's.
nanmean
([dim])Arithmetic mean of elements in the input ignoring NaN's.
nanmedian
([dim])Compute the median of the input values ignoring NaN's.
nanmin
([dim])Minimum of elements in the input ignoring NaN's.
nanstd
([dim])Compute the standard deviation of the input values ignoring NaN's.
nansum
([dim])Sum of elements in the input ignoring NaN's.
nanvar
([dim])Compute the variance of the input values ignoring NaN's.
plot
(**kwargs)Wrapper function to plot data.
rename
([dims_dict])Rename dimension labels.
rename_dims
([dims_dict])Rename dimensions.
round
(*[, out])Round to the nearest integer of all values passed in x.
save_hdf5
(filename)Write an object out to file in HDF5 format.
squeeze
([dim])Remove dimensions of length 1.
std
([dim])Compute the standard deviation of the input values.
sum
([dim])Sum of elements in the input.
to
(*[, unit, dtype, copy])Converts a Variable or DataArray to a different dtype and/or a different unit.
transpose
([dims])Transpose dimensions of the input.
underlying_size
(self)Return the size of the object in bytes.
var
([dim])Compute the variance of the input values.
Attributes
Alignment flag for coordinates.
Returns helper
scipp.Bins
allowing bin-wise operations to be performed or None if not binned data.The only dimension label for 1-dimensional data, raising an exception if the data is not 1-dimensional.
Dimension labels of the data (read-only).
Data type contained in the variable.
Provides access to fields of structured types such as vectors or matrices.
Number of dimensions of the data (read-only).
Shape of the data (read-only).
Number of elements in the data (read-only).
dict mapping dimension labels to dimension sizes (read-only).
Physical unit of the data.
The only value for 0-dimensional data, raising an exception if the data is not 0-dimensional.
Array of values of the data.
The only variance for 0-dimensional data, raising an exception if the data is not 0-dimensional.
Array of variances of the data.
- __getitem__(*args, **kwargs)#
Overloaded function.
__getitem__(self: scipp._scipp.core.Variable, arg0: int) -> scipp._scipp.core.Variable
__getitem__(self: scipp._scipp.core.Variable, arg0: slice) -> scipp._scipp.core.Variable
__getitem__(self: scipp._scipp.core.Variable, arg0: scipp._scipp.core.Variable) -> scipp._scipp.core.Variable
__getitem__(self: scipp._scipp.core.Variable, arg0: tuple[str, int]) -> scipp._scipp.core.Variable
__getitem__(self: scipp._scipp.core.Variable, arg0: tuple[str, slice]) -> scipp._scipp.core.Variable
__getitem__(self: scipp._scipp.core.Variable, arg0: ellipsis) -> scipp._scipp.core.Variable
__getitem__(self: scipp._scipp.core.Variable, arg0: list[int]) -> scipp._scipp.core.Variable
__getitem__(self: scipp._scipp.core.Variable, arg0: tuple[str, list[int]]) -> scipp._scipp.core.Variable
- property aligned#
Alignment flag for coordinates.
Indicates whether a coordinate is aligned. Aligned coordinates must match between the operands of binary operations while unaligned coordinates are dropped on mismatch.
This flag is only meaningful when the variable is contained in a
coords
dict
.It cannot be set on a variable directly; instead, use
sc.Coords.set_aligned()
.For binned coordinates of a binned data array
da
,da.bins.coords[name].aligned
should always beTrue
. The alignment w.r.t. the events can be queried viada.bins.coords[name].bins.aligned
and set viada.bins.coords.set_aligned(name, aligned)
.
- all(dim=None)#
Logical AND over input values.
- any(dim=None)#
Logical OR over input values.
- astype(self: scipp._scipp.core.Variable, type: object, *, copy: bool = True) scipp._scipp.core.Variable #
Converts a Variable or DataArray to a different dtype.
If the dtype is unchanged and
copy
is False, the object is returned without making a deep copy.- Parameters:
type – Target dtype.
copy – If False, return the input object if possible. If True, the function always returns a new object.
- Raises:
If the data cannot be converted to the requested dtype.
- Returns:
New variable or data array with specified dtype.
- Return type:
Union[scipp.Variable, scipp.DataArray]
- bin(arg_dict=None, /, *, dim=None, **kwargs)#
Create binned data by binning input along all dimensions given by edges.
- Seealso:
Details in
scipp.bin()
- Return type:
- property bins: Bins[_O] | None#
Returns helper
scipp.Bins
allowing bin-wise operations to be performed or None if not binned data.
- broadcast(*, dims=None, shape=None, sizes=None)#
Broadcast a Variable or a DataArray.
- ceil(*, out=None)#
Round up to the nearest integer of all values passed in x.
- Seealso:
Details in
scipp.ceil()
- Return type:
VariableLike
- copy(self: scipp._scipp.core.Variable, deep: bool = True) scipp._scipp.core.Variable #
Return a (by default deep) copy.
If deep=True (the default), a deep copy is made. Otherwise, a shallow copy is made, and the returned data (and meta data) values are new views of the data and meta data values of this object.
- cumsum(dim=None, mode='inclusive')#
Return the cumulative sum along the specified dimension.
- property dim#
The only dimension label for 1-dimensional data, raising an exception if the data is not 1-dimensional.
- property dims#
Dimension labels of the data (read-only).
- property dtype#
Data type contained in the variable.
- flatten(dims=None, to=None)#
Flatten multiple dimensions into a single dimension.
- floor(*, out=None)#
Round down to the nearest integer of all values passed in x.
- Seealso:
Details in
scipp.floor()
- Return type:
VariableLike
- fold(dim, *, dims=None, shape=None, sizes=None)#
Fold a single dimension of a variable or data array into multiple dims.
- hist(arg_dict=None, /, *, dim=None, **kwargs)#
Compute a histogram.
- max(dim=None)#
Maximum of elements in the input.
- mean(dim=None)#
Arithmetic mean of elements in the input.
- median(dim=None)#
Compute the median of the input values.
- min(dim=None)#
Minimum of elements in the input.
- nanhist(arg_dict=None, /, *, dim=None, **kwargs)#
Compute a histogram, skipping NaN values.
- nanmax(dim=None)#
Maximum of elements in the input ignoring NaN’s.
- nanmean(dim=None)#
Arithmetic mean of elements in the input ignoring NaN’s.
- nanmedian(dim=None)#
Compute the median of the input values ignoring NaN’s.
- nanmin(dim=None)#
Minimum of elements in the input ignoring NaN’s.
- nanstd(dim=None, *, ddof)#
Compute the standard deviation of the input values ignoring NaN’s.
- nansum(dim=None)#
Sum of elements in the input ignoring NaN’s.
- nanvar(dim=None, *, ddof)#
Compute the variance of the input values ignoring NaN’s.
- property ndim#
Number of dimensions of the data (read-only).
- plot(**kwargs)#
Wrapper function to plot data. See https://scipp.github.io/plopp/ for details.
- Return type:
- rename(dims_dict=None, /, **names)#
Rename dimension labels.
The renaming can be defined:
using a dict mapping the old to new names, e.g.
rename({'x': 'a', 'y': 'b'})
using keyword arguments, e.g.
rename(x='a', y='b')
In both cases, x is renamed to a and y to b.
Dimensions not specified in either input are unchanged.
- Parameters:
- Returns:
Variable
– A new variable with renamed dimensions which shares a buffer with the input.
See also
scipp.Variable.rename_dims
Equivalent for
Variable
but differs forDataArray
andDataset
.
- rename_dims(dims_dict=None, /, **names)#
Rename dimensions.
The renaming can be defined:
using a dict mapping the old to new names, e.g.
rename_dims({'x': 'a', 'y': 'b'})
using keyword arguments, e.g.
rename_dims(x='a', y='b')
In both cases, x is renamed to a and y to b.
Dimensions not specified in either input are unchanged.
This function only renames dimensions. See the
rename
method to also rename coordinates and attributes.
- round(*, out=None)#
Round to the nearest integer of all values passed in x.
- property shape#
Shape of the data (read-only).
- property size#
Number of elements in the data (read-only).
- property sizes#
dict mapping dimension labels to dimension sizes (read-only).
- squeeze(dim=None)#
Remove dimensions of length 1.
- std(dim=None, *, ddof)#
Compute the standard deviation of the input values.
- sum(dim=None)#
Sum of elements in the input.
- to(*, unit=None, dtype=None, copy=True)#
Converts a Variable or DataArray to a different dtype and/or a different unit.
If the dtype and unit are both unchanged and
copy
is False, the object is returned without making a deep copy.This method will choose whether to do the dtype or units translation first, by using the following rules in order:
If either the input or output dtype is float64, the unit translation will be done on the float64 type
If either the input or output dtype is float32, the unit translation will be done on the float32 type
If both the input and output dtypes are integer types, the unit translation will be done on the larger type
In other cases, the dtype is converted first and then the unit translation is done
- Parameters:
unit (
Union
[Unit
,str
,None
], default:None
) – Target unit. IfNone
, the unit is unchanged.dtype (
Optional
[Any
], default:None
) – Target dtype. IfNone
, the dtype is unchanged.copy (
bool
, default:True
) – IfFalse
, return the input object if possible. IfTrue
, the function always returns a new object.
- Returns:
Same as input
– New object with specified dtype and unit.- Raises:
scipp.DTypeError – If the input cannot be converted to the given dtype.
scipp.UnitError – If the input cannot be converted to the given unit.
- transpose(dims=None)#
Transpose dimensions of the input.
- underlying_size(self: scipp._scipp.core.Variable) int #
Return the size of the object in bytes.
The size includes the object itself and all arrays contained in it. But arrays may be counted multiple times if components share buffers, e.g. multiple coordinates referencing the same memory. Conversely, the size may be underestimated. Especially, but not only, with dtype=PyObject.
This function includes all memory of the underlying buffers. Use
__sizeof__
to get the size of the current slice only.
- property unit#
Physical unit of the data.
- property value#
The only value for 0-dimensional data, raising an exception if the data is not 0-dimensional.
- property values#
Array of values of the data.
- var(dim=None, *, ddof)#
Compute the variance of the input values.
- property variance#
The only variance for 0-dimensional data, raising an exception if the data is not 0-dimensional.
- property variances#
Array of variances of the data.