Skip to content

epymorph.data_shape

Utility classes to verify the shape of data as numpy arrays whose dimensions can be relative to the simulation context, and to adapt equivalent shapes.

DataT module-attribute

DataT = TypeVar('DataT', bound=generic)

Dimensions

Bases: Protocol

A class to store the lengths of dimensions which are supported by DataShapes. It is possible to create Dimensions instances which contain a subset of the available dimensions, for cases where the other dimensions aren't known and aren't needed.

T abstractmethod property

T: int

The number of days.

N abstractmethod property

N: int

The number of nodes.

C abstractmethod property

C: int

The number of compartments.

E abstractmethod property

E: int

The number of events.

of staticmethod

of(
    *,
    T: int | None = None,
    N: int | None = None,
    C: int | None = None,
    E: int | None = None,
) -> Dimensions

PartialDimensions

PartialDimensions(
    *,
    T: int | None = None,
    N: int | None = None,
    C: int | None = None,
    E: int | None = None,
)

Bases: Dimensions

A Dimensions instance where some dimensions are unknown. If code accesses an unknown dimension, a DimensionError is raised.

T property

T

The number of days.

N property

N

The number of nodes.

C property

C

The number of compartments.

E property

E

The number of events.

CompleteDimensions

CompleteDimensions(*, T: int, N: int, C: int, E: int)

Bases: Dimensions

A Dimensions instance where all dimensions are known.

T property

T

The number of days.

N property

N

The number of nodes.

C property

C

The number of compartments.

E property

E

The number of events.

DataShape

Bases: ABC

Description of a data attribute's shape relative to a simulation context.

to_tuple abstractmethod

to_tuple(dim: Dimensions) -> tuple[int, ...]

Returns a tuple with the lengths of the dimensions in this shape. If an axis is of indeterminate length, that axis is represented as -1.

matches abstractmethod

matches(dim: Dimensions, value: NDArray) -> bool

Does the given value match this shape expression?

adapt abstractmethod

adapt(
    dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]

Adapt the given value to this shape; raise ValueError if we can't.

Scalar dataclass

Scalar()

Bases: DataShape

A scalar value.

to_tuple

to_tuple(dim: Dimensions) -> tuple[int, ...]

Returns a tuple with the lengths of the dimensions in this shape. If an axis is of indeterminate length, that axis is represented as -1.

matches

matches(dim: Dimensions, value: NDArray) -> bool

Does the given value match this shape expression?

adapt

adapt(
    dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]

Adapt the given value to this shape; raise ValueError if we can't.

Time dataclass

Time()

Bases: DataShape

An array of at least size T (the number of simulation days).

to_tuple

to_tuple(dim: Dimensions) -> tuple[int, ...]

Returns a tuple with the lengths of the dimensions in this shape. If an axis is of indeterminate length, that axis is represented as -1.

matches

matches(dim: Dimensions, value: NDArray) -> bool

Does the given value match this shape expression?

adapt

adapt(
    dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]

Adapt the given value to this shape; raise ValueError if we can't.

Node dataclass

Node()

Bases: DataShape

An array of size N (the number of simulation nodes).

to_tuple

to_tuple(dim: Dimensions) -> tuple[int, ...]

Returns a tuple with the lengths of the dimensions in this shape. If an axis is of indeterminate length, that axis is represented as -1.

matches

matches(dim: Dimensions, value: NDArray) -> bool

Does the given value match this shape expression?

adapt

adapt(
    dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]

Adapt the given value to this shape; raise ValueError if we can't.

NodeAndNode dataclass

NodeAndNode()

Bases: DataShape

An array of size NxN.

to_tuple

to_tuple(dim: Dimensions) -> tuple[int, ...]

Returns a tuple with the lengths of the dimensions in this shape. If an axis is of indeterminate length, that axis is represented as -1.

matches

matches(dim: Dimensions, value: NDArray) -> bool

Does the given value match this shape expression?

adapt

adapt(
    dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]

Adapt the given value to this shape; raise ValueError if we can't.

NodeAndCompartment dataclass

NodeAndCompartment()

Bases: DataShape

An array of size NxC.

to_tuple

to_tuple(dim: Dimensions) -> tuple[int, ...]

Returns a tuple with the lengths of the dimensions in this shape. If an axis is of indeterminate length, that axis is represented as -1.

matches

matches(dim: Dimensions, value: NDArray) -> bool

Does the given value match this shape expression?

adapt

adapt(
    dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]

Adapt the given value to this shape; raise ValueError if we can't.

TimeAndNode dataclass

TimeAndNode()

Bases: DataShape

An array of size at-least-T by exactly-N.

to_tuple

to_tuple(dim: Dimensions) -> tuple[int, ...]

Returns a tuple with the lengths of the dimensions in this shape. If an axis is of indeterminate length, that axis is represented as -1.

matches

matches(dim: Dimensions, value: NDArray) -> bool

Does the given value match this shape expression?

adapt

adapt(
    dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]

Adapt the given value to this shape; raise ValueError if we can't.

NodeAndArbitrary dataclass

NodeAndArbitrary()

Bases: DataShape

An array of size exactly-N by any dimension.

to_tuple

to_tuple(dim: Dimensions) -> tuple[int, ...]

Returns a tuple with the lengths of the dimensions in this shape. If an axis is of indeterminate length, that axis is represented as -1.

matches

matches(dim: Dimensions, value: NDArray) -> bool

Does the given value match this shape expression?

adapt

adapt(
    dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]

Adapt the given value to this shape; raise ValueError if we can't.

ArbitraryAndNode dataclass

ArbitraryAndNode()

Bases: DataShape

An array of size any dimension by exactly-N.

to_tuple

to_tuple(dim: Dimensions) -> tuple[int, ...]

Returns a tuple with the lengths of the dimensions in this shape. If an axis is of indeterminate length, that axis is represented as -1.

matches

matches(dim: Dimensions, value: NDArray) -> bool

Does the given value match this shape expression?

adapt

adapt(
    dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]

Adapt the given value to this shape; raise ValueError if we can't.

Shapes dataclass

Shapes()

Static instances for all available shapes.

Scalar class-attribute instance-attribute

Scalar = Scalar()

T class-attribute instance-attribute

T = Time()

N class-attribute instance-attribute

N = Node()

NxC class-attribute instance-attribute

NxN class-attribute instance-attribute

NxN = NodeAndNode()

TxN class-attribute instance-attribute

TxN = TimeAndNode()

NxA class-attribute instance-attribute

AxN class-attribute instance-attribute

DataShapeMatcher

DataShapeMatcher(
    shape: DataShape,
    dim: Dimensions,
    *,
    exact: bool = False,
)

Bases: Matcher[NDArray]

Matches an array as adaptable to shape under the given Dimensions (dim).

expected

expected() -> str

Describes what the expected value is.

__call__

__call__(value: NDArray) -> bool

parse_shape

parse_shape(shape: str) -> DataShape

Attempt to parse an AttributeShape from a string.