scipp.Coords#
- class scipp.Coords#
dict-like collection of coordinates.
Returned by
DataArray.coords()andDataset.coords().- __init__(*args, **kwargs)#
Methods
__init__(*args, **kwargs)clear(self)copy(self[, deep])Return a (by default deep) copy.
Get the value associated with the provided key or the default value.
is_edges(self, key[, dim])Return True if the given key contains bin-edges in the given dim.
items(self)view on self's items
keys(self)view on self's keys
Remove and return an element.
popitem(self)set_aligned(self, key, aligned)Set the alignment flag for a coordinate.
update(self[, other])Update items from dict-like or iterable.
values(self)view on self's values
- __getitem__(self: scipp._scipp.core.Coords, arg0: str) scipp._scipp.core.Variable#
- clear(self: scipp._scipp.core.Coords) None#
- copy(self: scipp._scipp.core.Coords, deep: bool = True) scipp._scipp.core.Coords#
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.
- get()#
Get the value associated with the provided key or the default value.
Examples
Access a coordinate with a default value:
>>> import scipp as sc >>> da = sc.DataArray(sc.array(dims=['x'], values=[1, 2, 3])) >>> da.coords.get('x') # returns None if 'x' does not exist >>> da.coords['x'] = sc.arange('x', 3) >>> da.coords.get('x') <scipp.Variable> (x: 3) int64 [dimensionless] [0, 1, 2]
Access a Dataset item with a default value:
>>> ds = sc.Dataset({'a': sc.array(dims=['x'], values=[1, 2, 3])}) >>> ds.get('b', sc.DataArray(sc.zeros(dims=['x'], shape=[3]))) <scipp.DataArray> Dimensions: Sizes[x:3, ] Data: float64 [dimensionless] (x) [0, 0, 0]
- is_edges(self: scipp._scipp.core.Coords, key: str, dim: str | None = None) bool#
Return True if the given key contains bin-edges in the given dim.
Bin-edge coordinates have one more element than the corresponding dimension size. They define the boundaries of histogram bins.
- Parameters:
key – Name of the coordinate to check.
dim – Dimension to check against. If not provided, checks the coordinate’s single dimension.
- Returns:
True if the coordinate is a bin-edge coordinate.
Examples
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0]), ... coords={'x': sc.array(dims=['x'], values=[0.0, 1.0, 2.0, 3.0])} ... ) >>> da.coords.is_edges('x') True
Point coordinates have the same size as the dimension:
>>> da2 = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0]), ... coords={'x': sc.array(dims=['x'], values=[0.5, 1.5, 2.5])} ... ) >>> da2.coords.is_edges('x') False
- items(self: scipp._scipp.core.Coords) scipp._scipp.core.Coords_items_view#
view on self’s items
- keys(self: scipp._scipp.core.Coords) scipp._scipp.core.Coords_keys_view#
view on self’s keys
- pop()#
Remove and return an element.
If key is not found, default is returned if given, otherwise KeyError is raised.
Examples
Remove a coordinate from a DataArray:
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0]), ... coords={'x': sc.arange('x', 3), 'y': sc.arange('x', 3) * 10} ... ) >>> da.coords.pop('y') <scipp.Variable> (x: 3) int64 [dimensionless] [0, 10, 20] >>> 'y' in da.coords False
Pop with default value for missing key:
>>> da.coords.pop('z', sc.scalar(0)) <scipp.Variable> () int64 [dimensionless] 0
Remove an item from a Dataset:
>>> ds = sc.Dataset({'a': sc.array(dims=['x'], values=[1, 2, 3]), ... 'b': sc.array(dims=['x'], values=[4, 5, 6])}) >>> ds.pop('b') <scipp.DataArray> ... >>> list(ds.keys()) ['a']
- popitem(self: scipp._scipp.core.Coords) tuple[str, scipp._scipp.core.Variable]#
- set_aligned(self: scipp._scipp.core.Coords, key: str, aligned: bool) None#
Set the alignment flag for a coordinate.
Aligned coordinates (the default) are compared in binary operations and must match. Unaligned coordinates are not compared and are dropped if they do not match.
- Parameters:
key – Name of the coordinate.
aligned – True to mark as aligned, False to mark as unaligned.
Examples
>>> import scipp as sc >>> da = sc.DataArray( ... sc.array(dims=['x'], values=[1.0, 2.0, 3.0]), ... coords={'x': sc.arange('x', 3, unit='m')} ... )
Mark a coordinate as unaligned:
>>> da.coords.set_aligned('x', False)
Unaligned coordinates are shown without the ‘*’ prefix in the repr.
- update(self: scipp._scipp.core.Coords, other: object = None, /, **kwargs) None#
Update items from dict-like or iterable.
If
otherhas a .keys() method, then update does:for k in other.keys(): self[k] = other[k].If
otheris given but does not have a .keys() method, then update does:for k, v in other: self[k] = v.In either case, this is followed by:
for k in kwargs: self[k] = kwargs[k].See also
- values(self: scipp._scipp.core.Coords) scipp._scipp.core.Coords_values_view#
view on self’s values