dantro.containers.xrdatactr module

This module implements specialisations of the BaseDataContainer class.

class dantro.containers.xrdatactr.XrDataContainer(*, name: str, data: Union[numpy.ndarray, xarray.core.dataarray.DataArray], dims: Sequence[str] = None, coords: dict = None, extract_metadata: bool = True, apply_metadata: bool = True, **dc_kwargs)[source]

Bases: dantro.mixins.general.ForwardAttrsToDataMixin, dantro.mixins.numeric.NumbersMixin, dantro.mixins.numeric.ComparisonMixin, dantro.mixins.base.CheckDataMixin, dantro.mixins.base.ItemAccessMixin, dantro.base.BaseDataContainer

The XrDataContainer stores numerical xarray.DataArray data associated with dimensions, coordinates, and attributes.

DATA_EXPECTED_TYPES = (<class 'xarray.core.dataarray.DataArray'>, <class 'numpy.ndarray'>)
DATA_ALLOW_PROXY = False
DATA_UNEXPECTED_ACTION = 'raise'
_XRC_DIMS_ATTR = 'dims'
_XRC_DIM_NAME_PREFIX = 'dim_name__'
_XRC_COORDS_ATTR_PREFIX = 'coords__'
_XRC_COORDS_MODE_DEFAULT = 'values'
_XRC_COORDS_MODE_ATTR_PREFIX = 'coords_mode__'
_XRC_INHERIT_CONTAINER_ATTRIBUTES = True
_XRC_STRICT_ATTR_CHECKING = True
__init__(*, name: str, data: Union[numpy.ndarray, xarray.core.dataarray.DataArray], dims: Sequence[str] = None, coords: dict = None, extract_metadata: bool = True, apply_metadata: bool = True, **dc_kwargs)[source]

Initialize a XrDataContainer and extract dimension and coordinate labels.

Parameters
  • name (str) – which name to give to the XrDataContainer

  • data (Union[np.ndarray, xr.DataArray]) – The data to store; anything that an xr.DataArray can take

  • dims (Sequence[str], optional) – The dimension names.

  • coords (dict, optional) – The coordinates. The keys of this dict have to correspond to the dimension names.

  • extract_metadata (bool, optional) – If True, missing dims or coords arguments are tried to be populated from the container attributes.

  • apply_metadata (bool, optional) – Whether to apply the extracted or passed dims and coords to the underlying data. This might not be desired in cases where the given data already is a labelled xr.DataArray or where the data is a proxy and the labelling should be postponed.

  • **dc_kwargs – passed to parent

_format_info() → str[source]

A __format__ helper function: returns info about the item.

In this case, the dtype and sizes of the stored data is returned. Depending on whether metadata is available, the shape information is shown or the dimension names and the length of the dimensions are used.

_format_shape() → str[source]

A __format__ helper for parsing shape information

__len__() → int[source]

Length of the underlying data, i.e. first entry in shape

copy(*, deep: bool = True)[source]

Return a new object with a copy of the data. The copy is deep if not specified otherwise.

Parameters

deep (bool, optional) – Whether the copy is deep

Returns

A (deep) copy of this object.

Return type

XrDataContainer

save(path: str, **save_kwargs)[source]

Saves the XrDataContainer to a file by invoking the .to_netcdf method of the underlying data.

The recommended file extension is .xrdc or .nc_da, which are compatible with the xarray-based data loader.

Warning

This does NOT store container attributes!

Parameters
  • path (str) – The path to save the file at

  • **save_kwargs – Passed to the .to_netcdf method

_extract_metadata()[source]

Extracts metadata from the container attributes and stores them in the _dim_names and _dim_to_coords_map cache attributes.

_inherit_attrs()[source]

Carry over container attributes to the xr.DataArray attributes

This does not include container attributes that are used for extracting metadata; it makes no sense to have them in the attributes of the already labelled xr.DataArray

_apply_metadata()[source]

Applies the cached metadata to the underlying xr.DataArray

_postprocess_proxy_resolution()[source]

Only invoked from ProxySupportMixin, which have to be added to the class specifically. This function takes care to apply the potentially existing metadata after the proxy was resolved.

_parse_sizes_from_metadata() → Sequence[Tuple[str, int]][source]

Invoked from _format_shape when no metadata was applied but the dimension names are available. Should return data in the same form as xr.DataArray.sizes.items() does.

FORWARD_ATTR_EXCLUDE = ()
FORWARD_ATTR_ONLY = None
FORWARD_ATTR_TO = 'data'
_ATTRS_CLS

alias of dantro.base.BaseDataAttrs

__abs__()

Absolute value

Returns

A new object with the absolute value of the elements

__add__(other)

Add two objects

Returns

A new object containing the summed data

__bool__()

Truth value

__ceil__()

Smallest integer

Returns

A new object containing the smallest integer

__delitem__(key)

Deletes an item

__divmod__(other)

Calculate the floor division and modulo of two objects

Returns

A new object containing the floor divided data and its modulo

__eq__(other)

Equality

__floor__()

Largest integer

Returns

A new object containing the largest element

__floordiv__(other)

Floor divide two objects

Returns

A new object containing the floor divided data

__format__(spec_str: str) → str

Creates a formatted string from the given specification.

Invokes further methods which are prefixed by _format_.

__ge__(other)

Greater than or equal

__getattr__(attr_name: str)

Forward attributes that were not available in this class to some other attribute of the group or container.

Parameters

attr_name (str) – The name of the attribute that was tried to be accessed but was not available in self.

Returns

The attribute attr_name of getattr(self, self.FORWARD_ATTR_TO)

__getitem__(key)

Returns an item.

__getstate__() → dict

Returns the object’s __dict__

__gt__(other)

Greater than

__iadd__(other)

Add two objects

Returns

Self with modified data

__ifloordiv__(other)

Floor divide two objects

Returns

Self with modified data

__imod__(other)

Calculate the modulo of two objects

Returns

Self with modified data

__imul__(other)

Multiply two objects

Returns

Self with modified data

__invert__()

Inverse value

Returns

A new object with the inverted values of the elements

__ipow__(other)

Calculate the self data to the power of other data

Returns

Self with modified data

__isub__(other)

Subtract two objects

Returns

Self with modified data

__itruediv__(other)

Divide two objects

Returns

Self with modified data

__le__(other)

Less than or equal

__lt__(other)

Less than

__mod__(other)

Calculate the modulo of two objects

Returns

A new object containing the summed data

__mul__(other)

Multiply two objects

Returns

A object containing the multiplied data

__ne__(other)

Inequality

__neg__()

Make negative

Returns

A new object with negative elements

__pos__()

Make positive

Returns

A new object with negative elements

__pow__(other)

Calculate the self data to the power of other data

Returns

A new object containing the result

__repr__() → str

Same as __str__

__round__()

Rounds number to nearest integer

Returns

A new object as rounded number to nearest integer

__setitem__(key, val)

Sets an item.

__setstate__(d: dict)

Sets the object’s __dict__ to the given one

__sizeof__() → int

Returns the size of the data (in bytes) stored in this container’s data and its attributes.

Note that this value is approximate. It is computed by calling the sys.getsizeof function on the data, the attributes, the name and some caching attributes that each dantro data tree class contains. Importantly, this is not a recursive algorithm.

Also, derived classes might implement further attributes that are not taken into account either. To be more precise in a subclass, create a specific __sizeof__ method and invoke this parent method additionally.

For more information, see the documentation of sys.getsizeof:

__str__() → str

An info string, that describes the object. This invokes the formatting helpers to show the log string (type and name) as well as the info string of this object.

__sub__(other)

Subtract two objects

Returns

A new object containing the subtracted data

__truediv__(other)

Divide two objects

Returns

A new object containing the divided data

__trunc__()

Truncated to the nearest integer toward 0

Returns

A new object containing the truncated element

_abc_impl = <_abc_data object>
_attrs = None
_check_data(data) → None

A general method to check the received data for its type

Parameters

data – The data to check

Raises
  • TypeError – If the type was unexpected and the action was ‘raise’

  • ValueError – Illegal value for DATA_UNEXPECTED_ACTION class variable

Returns

None

_check_name(new_name: str) → None

Called from name.setter and can be used to check the name that the container is supposed to have. On invalid name, this should raise.

This method can be subclassed to implement more specific behaviour. To propagate the parent classes’ behaviour the subclassed method should always call its parent method using super().

Parameters

new_name (str) – The new name, which is to be checked.

_format_cls_name() → str

A __format__ helper function: returns the class name

_format_logstr() → str

A __format__ helper function: returns the log string, a combination of class name and name

_format_name() → str

A __format__ helper function: returns the name

_format_path() → str

A __format__ helper function: returns the path to this container

_forward_attr_get_forwarding_target()

Get the object that the attribute call is to be forwarded to

_forward_attr_post_hook(attr)

Invoked before attribute forwarding occurs

_forward_attr_pre_hook(attr_name: str = None)

Invoked before attribute forwarding occurs

_item_access_convert_list_key(key)

If given something that is not a list, just return that key

property attrs

The container attributes.

property classname

Returns the name of this DataContainer-derived class

property data

The stored data.

property logstr

Returns the classname and name of this object

property name

The name of this DataContainer-derived object.

property parent

The associated parent of this container or group

property path

The path to get to this container or group from some root path