epymorph.data_shape
Expression of the shape of numpy data whose dimensions can be relative to a simulation context. Provides utilities to declare, check, and adapt data dimensionality.
Dimensions
Bases: Protocol
Stores the lengths of simulation dimensions which are supported by DataShapes
.
See Also
Concrete implementations epymorph.data_shape.CompleteDimensions and epymorph.data_shape.PartialDimensions exist to enable cases where either all or only some of the dimensions are specified.
of
staticmethod
of(
*,
T: int | None = None,
N: int | None = None,
C: int | None = None,
E: int | None = None,
) -> Dimensions
Construct either a CompleteDimensions
or PartialDimensions
instance,
depending on whether or not the dimensions are fully specified.
Parameters:
-
T
(int | None
, default:None
) –The number of simulation days, if known.
-
N
(int | None
, default:None
) –The number of geo nodes, if known.
-
C
(int | None
, default:None
) –The number of compartments in the disease model, if known.
-
E
(int | None
, default:None
) –The number of events in the disease model, if known.
Returns:
-
Dimensions
–A new
Dimensions
instance (either complete or partial).
PartialDimensions
PartialDimensions(
*,
T: int | None = None,
N: int | None = None,
C: int | None = None,
E: int | None = None,
)
Bases: Dimensions
A Dimensions
implementation where some dimensions are unknown.
If code accesses an unspecified dimension, DimensionError
is raised.
CompleteDimensions
Bases: Dimensions
A Dimensions instance where all dimensions are known.
DataShape
Bases: ABC
Description of a data attribute's shape relative to the simulation context.
to_tuple
abstractmethod
to_tuple(dim: Dimensions) -> tuple[int, ...]
Return a tuple with the lengths of the dimensions in this shape.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
Returns:
matches
abstractmethod
matches(dim: Dimensions, value: NDArray) -> bool
Check if the given value matches this shape expression.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray
) –The numpy array to check.
Returns:
-
bool
–True if the array's shape matches this shape description in the given context.
adapt
abstractmethod
adapt(
dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]
Adapt the given value to this shape.
Note that this shape adaptation is more permissive than standard numpy broadcasting.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray[DataT]
) –The numpy array to reshape.
Returns:
Raises:
-
ValueError
–If the array cannot be adapted.
Scalar
dataclass
Bases: DataShape
A scalar value.
to_tuple
to_tuple(dim: Dimensions) -> tuple[int, ...]
Return a tuple with the lengths of the dimensions in this shape.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
Returns:
matches
matches(dim: Dimensions, value: NDArray) -> bool
Check if the given value matches this shape expression.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray
) –The numpy array to check.
Returns:
-
bool
–True if the array's shape matches this shape description in the given context.
adapt
adapt(
dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]
Adapt the given value to this shape.
Note that this shape adaptation is more permissive than standard numpy broadcasting.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray[DataT]
) –The numpy array to reshape.
Returns:
Raises:
-
ValueError
–If the array cannot be adapted.
Time
dataclass
Bases: DataShape
An array of at least size T: the number of simulation days.
to_tuple
to_tuple(dim: Dimensions) -> tuple[int, ...]
Return a tuple with the lengths of the dimensions in this shape.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
Returns:
matches
matches(dim: Dimensions, value: NDArray) -> bool
Check if the given value matches this shape expression.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray
) –The numpy array to check.
Returns:
-
bool
–True if the array's shape matches this shape description in the given context.
adapt
adapt(
dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]
Adapt the given value to this shape.
Note that this shape adaptation is more permissive than standard numpy broadcasting.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray[DataT]
) –The numpy array to reshape.
Returns:
Raises:
-
ValueError
–If the array cannot be adapted.
Node
dataclass
Bases: DataShape
An array of size N: the number of simulation nodes.
to_tuple
to_tuple(dim: Dimensions) -> tuple[int, ...]
Return a tuple with the lengths of the dimensions in this shape.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
Returns:
matches
matches(dim: Dimensions, value: NDArray) -> bool
Check if the given value matches this shape expression.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray
) –The numpy array to check.
Returns:
-
bool
–True if the array's shape matches this shape description in the given context.
adapt
adapt(
dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]
Adapt the given value to this shape.
Note that this shape adaptation is more permissive than standard numpy broadcasting.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray[DataT]
) –The numpy array to reshape.
Returns:
Raises:
-
ValueError
–If the array cannot be adapted.
NodeAndNode
dataclass
Bases: DataShape
An array of size NxN: a square of the number of simulation nodes.
to_tuple
to_tuple(dim: Dimensions) -> tuple[int, ...]
Return a tuple with the lengths of the dimensions in this shape.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
Returns:
matches
matches(dim: Dimensions, value: NDArray) -> bool
Check if the given value matches this shape expression.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray
) –The numpy array to check.
Returns:
-
bool
–True if the array's shape matches this shape description in the given context.
adapt
adapt(
dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]
Adapt the given value to this shape.
Note that this shape adaptation is more permissive than standard numpy broadcasting.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray[DataT]
) –The numpy array to reshape.
Returns:
Raises:
-
ValueError
–If the array cannot be adapted.
NodeAndCompartment
dataclass
Bases: DataShape
An array of size NxC: the number of simulation nodes by the number of disease compartments.
to_tuple
to_tuple(dim: Dimensions) -> tuple[int, ...]
Return a tuple with the lengths of the dimensions in this shape.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
Returns:
matches
matches(dim: Dimensions, value: NDArray) -> bool
Check if the given value matches this shape expression.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray
) –The numpy array to check.
Returns:
-
bool
–True if the array's shape matches this shape description in the given context.
adapt
adapt(
dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]
Adapt the given value to this shape.
Note that this shape adaptation is more permissive than standard numpy broadcasting.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray[DataT]
) –The numpy array to reshape.
Returns:
Raises:
-
ValueError
–If the array cannot be adapted.
TimeAndNode
dataclass
Bases: DataShape
An array of size at-least-T by exactly-N: T is the number of simulation days and N is the number of simulation nodes.
to_tuple
to_tuple(dim: Dimensions) -> tuple[int, ...]
Return a tuple with the lengths of the dimensions in this shape.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
Returns:
matches
matches(dim: Dimensions, value: NDArray) -> bool
Check if the given value matches this shape expression.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray
) –The numpy array to check.
Returns:
-
bool
–True if the array's shape matches this shape description in the given context.
adapt
adapt(
dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]
Adapt the given value to this shape.
Note that this shape adaptation is more permissive than standard numpy broadcasting.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray[DataT]
) –The numpy array to reshape.
Returns:
Raises:
-
ValueError
–If the array cannot be adapted.
NodeAndArbitrary
dataclass
Bases: DataShape
An array of size exactly-N by any dimension: N is the number of geo nodes.
to_tuple
to_tuple(dim: Dimensions) -> tuple[int, ...]
Return a tuple with the lengths of the dimensions in this shape.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
Returns:
matches
matches(dim: Dimensions, value: NDArray) -> bool
Check if the given value matches this shape expression.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray
) –The numpy array to check.
Returns:
-
bool
–True if the array's shape matches this shape description in the given context.
adapt
adapt(
dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]
Adapt the given value to this shape.
Note that this shape adaptation is more permissive than standard numpy broadcasting.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray[DataT]
) –The numpy array to reshape.
Returns:
Raises:
-
ValueError
–If the array cannot be adapted.
ArbitraryAndNode
dataclass
Bases: DataShape
An array of size any dimension by exactly-N: N is the number of geo nodes.
to_tuple
to_tuple(dim: Dimensions) -> tuple[int, ...]
Return a tuple with the lengths of the dimensions in this shape.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
Returns:
matches
matches(dim: Dimensions, value: NDArray) -> bool
Check if the given value matches this shape expression.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray
) –The numpy array to check.
Returns:
-
bool
–True if the array's shape matches this shape description in the given context.
adapt
adapt(
dim: Dimensions, value: NDArray[DataT]
) -> NDArray[DataT]
Adapt the given value to this shape.
Note that this shape adaptation is more permissive than standard numpy broadcasting.
Parameters:
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
value
(NDArray[DataT]
) –The numpy array to reshape.
Returns:
Raises:
-
ValueError
–If the array cannot be adapted.
Shapes
dataclass
Shapes(
Scalar: ClassVar = Scalar(),
T: ClassVar = Time(),
N: ClassVar = Node(),
NxC: ClassVar = NodeAndCompartment(),
NxN: ClassVar = NodeAndNode(),
TxN: ClassVar = TimeAndNode(),
NxA: ClassVar = NodeAndArbitrary(),
AxN: ClassVar = ArbitraryAndNode(),
)
Static instances for all available shapes.
Data can be in any of these shapes, where:
- Scalar is a single scalar value
- T is the number of days
- N is the number of nodes
- C is the number of IPM compartments
- A is any length (arbitrary; this dimension is effectively unchecked)
DataShapeMatcher
DataShapeMatcher(
shape: DataShape,
dim: Dimensions,
*,
exact: bool = False,
)
A Matcher
which checks whether an array is adaptable to shape
under the given
dimensions (dim
).
Parameters:
-
shape
(DataShape
) –The data shape to match.
-
dim
(Dimensions
) –Information about the simulation context's dimensions.
-
exact
(bool
, default:False
) –If True, do not accept array adaptation, require that the array match exactly.
parse_shape
Attempt to parse DataShape
from a shape expression string.
Parameters:
-
shape
(str
) –The shape expression string.
Returns:
-
DataShape
–The
DataShape
instance, if valid.
Raises:
-
ValueError
–If the string is not a supported shape.