labthings_fastapi.types.numpy
Basic support for numpy arrays in Pydantic models
We define a type alias NDArray which is a numpy array, annotated
to allow pydantic to convert it to and from JSON (as an array-of-arrays).
Usage:
from labthings_fastapi.types.ndarray import NDArray
def double(arr: NDArray) -> NDArray:
return arr * 2 # arr is a numpy.ndarray
The implementation is not super elegant: it isn’t recursive so has only been defined for up to 6d arrays. Specifying the dimensionality might be a nice touch, but is left for the future.
Complex numbers are currently not supported, again this is left for the future.
Module Contents
Classes
Functions
Convert a numpy array to a list of lists |
|
Convert a list of lists to a numpy array (or pass-through ndarrays) |
|
Convert any numpy array in a dict into a list |
|
A Pydantic wrap serializer to denumpify mappings before serialization |
Data
API
- labthings_fastapi.types.numpy.Number: typing_extensions.TypeAlias
None
- labthings_fastapi.types.numpy.NestedListOfNumbers: typing_extensions.TypeAlias
None
- class labthings_fastapi.types.numpy.NestedListOfNumbersModel
Bases:
pydantic.RootModel- root: labthings_fastapi.types.numpy.NestedListOfNumbers
None
- labthings_fastapi.types.numpy.np_to_listoflists(arr: numpy.ndarray) labthings_fastapi.types.numpy.NestedListOfNumbers
Convert a numpy array to a list of lists
NB this will not be quick! Large arrays will be much better serialised by dumping to base64 encoding or similar.
- labthings_fastapi.types.numpy.listoflists_to_np(lol: Union[labthings_fastapi.types.numpy.NestedListOfNumbers, numpy.ndarray]) numpy.ndarray
Convert a list of lists to a numpy array (or pass-through ndarrays)
- labthings_fastapi.types.numpy.NDArray: typing_extensions.TypeAlias
None
- labthings_fastapi.types.numpy.denumpify_serializer(v: Any, nxt: pydantic.SerializerFunctionWrapHandler) Any
A Pydantic wrap serializer to denumpify mappings before serialization
- class labthings_fastapi.types.numpy.DenumpifyingDict
Bases:
pydantic.RootModel- root: Annotated[collections.abc.Mapping, WrapSerializer(denumpify_serializer)]
None
- model_config
‘ConfigDict(…)’