Pydantic Model integration of the NumPy array
Project description
pydantic-numpy
Usage
Package that integrates NumPy Arrays into Pydantic!
pydantic_numpy.typing
provides many typings such asNpNDArrayFp64
,Np3DArrayFp64
(float64 that must be 3D)! Works with bothpydantic.BaseModel
andpydantic.dataclass
NumpyModel
(derived frompydantic.BaseModel
) make it possible to dump and loadnp.ndarray
within model fields alongside other fields that are not instances ofnp.ndarray
!
See the test.helper.groups
to see types that are defined explicitly. Define your own NumPy types with pydantic_numpy.np_array_pydantic_annotated_typing
.
Examples
For more examples see test_ndarray.py
import numpy as np
from pydantic import BaseModel
import pydantic_numpy.typing as pnd
from pydantic_numpy import np_array_pydantic_annotated_typing
from pydantic_numpy.model import NumpyModel, MultiArrayNumpyFile
class MyBaseModelDerivedModel(BaseModel):
any_array_dtype_and_dimension: pnd.NpNDArray
# Must be numpy float32 as dtype
k: np_array_pydantic_annotated_typing(data_type=np.float32)
shorthand_for_k: pnd.NpNDArrayFp32
must_be_1d_np_array: np_array_pydantic_annotated_typing(dimensions=1)
class MyDemoNumpyModel(NumpyModel):
k: np_array_pydantic_annotated_typing(data_type=np.float32)
# Instantiate from array
cfg = MyDemoModel(k=[1, 2])
# Instantiate from numpy file
cfg = MyDemoModel(k="path_to/array.npy")
# Instantiate from npz file with key
cfg = MyDemoModel(k=MultiArrayNumpyFile(path="path_to/array.npz", key="k"))
cfg.k # np.ndarray[np.float32]
cfg.dump("path_to_dump_dir", "object_id")
cfg.load("path_to_dump_dir", "object_id")
NumpyModel.load
requires the original model:
MyNumpyModel.load(<path>)
Use model_agnostic_load
when you have several models that may be the correct model:
from pydantic_numpy.model import model_agnostic_load
cfg.dump("path_to_dump_dir", "object_id")
equals_cfg = model_agnostic_load("path_to_dump_dir", "object_id", models=[MyNumpyModel, MyDemoModel])
Install
pip install pydantic-numpy
Considerations
The package is designed to work with Pydantic V2 and not V1. You can install from cheind's repository if you want Python <=3.8
and Pydantic V1 support.
Licensing notice
As of version 3.0.0
the license has moved over to BSD-4. The versions prior are under the MIT license.
History
The original idea originates from this discussion, and forked from cheind's repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pydantic_numpy-5.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd3daa5d7c3c803675803b60a9221fc5c3a54c75761d0277cb85b6caf2449fa5 |
|
MD5 | 1214ed75b539321d8a16a1f9fda18d7e |
|
BLAKE2b-256 | e5ddd3ee6de45b09a811deec19afd5f64368beecc0a7844cc950438f2455f90d |