Skip to main content

Pydantic Model integration of the NumPy array

Project description

pydantic-numpy

Python 3.10-3.13 Packaged with Poetry Code style: black Imports: isort Ruff

Usage

Package that integrates NumPy Arrays into Pydantic!

  • pydantic_numpy.typing provides many typings such as NpNDArrayFp64, Np3DArrayFp64 (float64 that must be 3D)! Works with both pydantic.BaseModel and pydantic.dataclass
  • NumpyModel (derived from pydantic.BaseModel) make it possible to dump and load np.ndarray within model fields alongside other fields that are not instances of np.ndarray!

See the test.helper.testing_groups to see types that are defined explicitly.

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])

Custom type

There are two ways to define. Function derived types with pydantic_numpy.helper.annotation.np_array_pydantic_annotated_typing.

Function derived types don't work with static type checkers like Pyright and MyPy. In case you need the support, just create the types yourself:

NpStrict1DArrayInt64 = Annotated[
    np.ndarray[tuple[int], np.dtype[np.int64]],
    NpArrayPydanticAnnotation.factory(data_type=np.int64, dimensions=1, strict_data_typing=True),
]

Custom serialization

If the default serialization of NumpyDataDict, as outlined in typing.py, doesn't meet your requirements, you have the option to define a custom type with its own serializer. This can be achieved using the NpArrayPydanticAnnotation.factory method, which accepts a custom serialization function through its serialize_numpy_array_to_json parameter. This parameter expects a function of the form Callable[[npt.ArrayLike], Iterable], allowing you to tailor the serialization process to your specific needs.

Example below illustrates definition of 1d-array of float32 type that serializes to flat Python list (without nested dict as in default NumpyDataDict case):

def _serialize_numpy_array_to_float_list(array_like: npt.ArrayLike) -> Iterable:
    return np.array(array_like).astype(float).tolist()


Np1DArrayFp32 = Annotated[
    np.ndarray[tuple[int], np.dtype[np.float32]],
    NpArrayPydanticAnnotation.factory(
        data_type=np.float32,
        dimensions=1,
        strict_data_typing=False,
        serialize_numpy_array_to_json=_serialize_numpy_array_to_float_list,
    ),
]

Install

pip install pydantic-numpy

History

The original idea originates from this discussion, and forked from cheind's repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_numpy-7.0.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

pydantic_numpy-7.0.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_numpy-7.0.0.tar.gz.

File metadata

  • Download URL: pydantic_numpy-7.0.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for pydantic_numpy-7.0.0.tar.gz
Algorithm Hash digest
SHA256 c70138afd8443dc593a0ef5c8f88f140f51f3ad38f4ebdb1e05f3a0b784ca6b3
MD5 0688b264076f66addaef53292282aa90
BLAKE2b-256 d6698b528273fabaebcfcb3f864e237b9a187863ecb3ad4cacfc446cfd7c879a

See more details on using hashes here.

File details

Details for the file pydantic_numpy-7.0.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_numpy-7.0.0-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for pydantic_numpy-7.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 51602b946fc0f4aab4940faef7046a93676e0d94bb9f59ea4bd18a712a58a434
MD5 86f4139dc1f371a344a390cbbdfb80e0
BLAKE2b-256 50e7d2876afc8d2a3ef839c1428919063c4a0a3608e4aac821b9c57d1181233d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page