dantro.containers.numeric module

This module implements specializations of the BaseDataContainer class that focus on holding numerical, array-like data

class dantro.containers.numeric.NumpyDataContainer(*, name: str, data: numpy.ndarray, **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 NumpyDataContainer stores numerical array-shaped data.

Specifically: it is made for use with the np.ndarray class.

DATA_EXPECTED_TYPES = (<class 'numpy.ndarray'>,)
DATA_ALLOW_PROXY = False
DATA_UNEXPECTED_ACTION = 'raise'
__init__(*, name: str, data: numpy.ndarray, **dc_kwargs)[source]

Initialize a NumpyDataContainer, storing data that is ndarray-like.

Parameters
  • name (str) – The name of this container

  • data (np.ndarray) – The numpy data to store

  • **dc_kwargs – Additional arguments for container initialization, passed on to parent method

_format_info() → str[source]

A __format__ helper function: returns info about the item

In this case, the dtype and shape of the stored data is returned. Note that this relies on the ForwardAttrsToDataMixin.

__len__() → int[source]

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

copy()[source]

Return a copy of this NumpyDataContainer.

NOTE that this will create copies of the stored data.

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

Saves the NumpyDataContainer to a file by invoking the np.save function on the underlying data.

The file extension should be .npy, which is compatible with the numpy-based data loader. If another file extension is given, the numpy method will _append_ .npy!

Warning

This does NOT store container attributes!

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

  • **save_kwargs – Passed to the np.save method

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