Skip to main content

Pydantic adapters for common scientific types

Project description

scientific_pydantic

CI Pre-commit Docs Coverage Status PyPI - Version

scientific_pydantic is an extension module to pydantic that adds support for a number of common data types in scientific computing.

Installation

This project can be installed from either PyPI or conda-forge via the scientific-pydantic package.

Motivation

Let's say with only pydantic, you wanted to put a numpy.ndarray object into one of your models:

import numpy as np
import pydantic

class MyModel(pydantic.BaseModel):
    arr: np.ndarray

this will produce the following error:

pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'numpy.ndarray'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.

Specifying arbitrary_types_allowed=True can work, but disables JSON serialization and validation and does not support lax parsing and input conversions, which are a powerful feature of pydantic. This library takes the approach of implementing __get_pydantic_core_schema__ in adapter objects and using Annotated, as described here.

With scientific_pydantic, this example looks like:

import typing as ty

import numpy as np
import pydantic
from scientific_pydantic.numpy import NDArrayAdapter

class MyModel(pydantic.BaseModel):
    arr: ty.Annotated[np.ndarray, NDArrayAdapter()]

Using this pattern, you can embed scientific data types into your models and get the full pydantic experience with serialization and input conversions.

Usage

In general, it is recommended to use from-style imports with this library. The import path for an adapter is normally akin to the import path of the type it is adapting. For instance, the adapter for scipy.spatial.transform.Rotation would be:

from scientific_pydantic.scipy.spatial.transform import RotationAdapter

Adapters are used via the Annotated pattern, as was shown in the Motivation section. This allows for typecheckers that support pydantic, such as pyrefly to understand the type of the fields.

A number of adapters provided with this library also take parameters that define common validation operations. This takes inspiration from pydantic.Field. For instance,

import pydantic

class MyModel(pydantic.BaseModel):
    a: int = pydantic.Field(ge=0)

defines a non-negative integer. In scientific_pydantic, this style of validation logic can be accomplished via:

import typing as ty

import numpy as np
import pydantic
from scientific_pydantic.numpy import NDArrayAdapter

class MyModel(pydantic.BaseModel):
    a: ty.Annotated[np.ndarray, NDArrayAdapter(shape=(None, 3), ge=0)]

which constrains a to be an N x 3 ndarray where all elements are non-negative. See the individual adapters in the API documentation for a description of the parameters each one takes.

Design Philosophy

This library has an interesting conundrum from a dependency standpoint. Since the goal is to provide adapters for common types that come from many different libraries, there are a few options for how dependency management can work:

  1. Depend on all packages being supported and enforce version constraints. This would violate the "pay for what you use" principle and is thus not a good option.
  2. Split the package into N different, but related, packages (e.g. scientific_pydantic_shapely) that enforce version constraints individually. This is tractable, but leads to a large number of packages to maintain and version together.
  3. Have 1 package and only depend on pydantic (and pydantic_core). Users will bring their own versions of the packages they want to use.

This library takes approach #3. This puts the burden of version compatibility onto this library. For instance, scipy.spatial.transform.Rotation objects gained the ability to support N-D arrays of rotation transforms in version 1.17.0. Thus, validation features related to this must be disabled if the user brings their own scipy version that is < 1.17.0. This adds complexity to this library, but prevents either the dependency bloat from option 1 or the package bloat from option 2.

By only depending on pydantic, the library must not import anything from a third-party library at global scope. This is accomplished via liberal use of delayed and nested import statments and enforced via a unit test.

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

scientific_pydantic-0.1.1.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scientific_pydantic-0.1.1-py3-none-any.whl (39.5 kB view details)

Uploaded Python 3

File details

Details for the file scientific_pydantic-0.1.1.tar.gz.

File metadata

  • Download URL: scientific_pydantic-0.1.1.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for scientific_pydantic-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e228837962f93a9f12628fa59b11906587a43aef44b16f27cb90051d83baa7c9
MD5 b8d886fdd721ddba9e009981d5036df0
BLAKE2b-256 98ec590d5605b04e6f5ce65112351c5e05490631efa55a03d6ea2a9fd8114091

See more details on using hashes here.

Provenance

The following attestation bundles were made for scientific_pydantic-0.1.1.tar.gz:

Publisher: ci.yml on psalvaggio/scientific_pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file scientific_pydantic-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for scientific_pydantic-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0823cf2012f087960fdabddc9021cf13a6198678285426cfc24b77b3ee62e786
MD5 92f951fac9f3d8ad0e9803616df6d263
BLAKE2b-256 8d3fc0fb653319459d15da2276864211120dfaf7fc0ab649fde5350528f269e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for scientific_pydantic-0.1.1-py3-none-any.whl:

Publisher: ci.yml on psalvaggio/scientific_pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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