numpy¶
numpy
¶
Pydantic adapters for numpy types
The current supported types are:
dtype- DTypeAdapterndarray- NDArrayAdapter
DTypeAdapter
¶
Pydantic adapter for numpy.dtype
JSON Serialization
dtype's are serialized to JSON via the .str property.
Examples:
>>> import pydantic
>>> import numpy as np
>>> from scientific_pydantic.numpy import (
... DTypeAdapter,
... )
>>> class Model(pydantic.BaseModel):
... dt: ty.Annotated[np.dtype, DTypeAdapter()]
>>> Model(dt="|i4")
Model(dt=dtype('int32'))
>>> Model(dt=float)
Model(dt=dtype('float64'))
>>> Model(dt=np.float64)
Model(dt=dtype('float64'))
Source code in src/scientific_pydantic/numpy/dtype_adapter.py
__get_pydantic_core_schema__(_source_type: type[ty.Any], _handler: pydantic.GetCoreSchemaHandler) -> core_schema.CoreSchema
classmethod
¶
Get the pydantic schema for this type
Source code in src/scientific_pydantic/numpy/dtype_adapter.py
__get_pydantic_json_schema__(_core_schema: core_schema.CoreSchema, _handler: pydantic.GetJsonSchemaHandler) -> JsonSchemaValue
¶
Generate JSON schema for the ndarray field
Source code in src/scientific_pydantic/numpy/dtype_adapter.py
NDArrayAdapter
¶
Pydantic type adapter for numpy ndarrays with validation constraints.
Shape Specifiers
Shape specifiers for arrays are a sequence of entries, which support the options:
Ellipsis/...- A wildcard match that matches any number of dimensions with any size. Multiple can be used in a shape specifier.int- The corresponding dimension of the array must have exactly this size.range- The corresponding dimension of the array must have a size that is in thisrange.slice- The corresponding dimension of the array must have a size that is in thisslice. ANonein the start or stop of thesliceindicates that there is no lower or upper bound, respectively, for the dimension size.None- The corresponding dimension must exist, but no constraint is applied to the size.
For instance, a shape specifier of:
would indicate the array must have at least 4 dimensions, where the last 4 dimensions must be of size 4, anything, 1 or 2, and at least 3.JSON Serialization
ndarrays are encoded in JSON via nested list, which are obtained by
calling .tolist() on the ndarray.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype
|
type | dtype | str | None
|
If given, the array will be coerced into this data type via |
None
|
ndim
|
int | None
|
If given, the array must have this dimensionality. |
None
|
shape
|
Sequence[EllipsisType | int | range | slice | None] | None
|
If given a shape specifier for the array. |
None
|
gt
|
float | None
|
If given, all elements in the array must be |
None
|
ge
|
float | None
|
If given, all elements in the array must be |
None
|
lt
|
float | None
|
If given, all elements in the array must be |
None
|
le
|
float | None
|
If given, all elements in the array must be |
None
|
clip
|
tuple[float | None, float | None]
|
If not |
(None, None)
|
Examples:
>>> import typing as ty
>>> import pydantic
>>> import numpy as np
>>> from scientific_pydantic.numpy import (
... NDArrayAdapter,
... )
>>> class Model(pydantic.BaseModel):
... a: ty.Annotated[
... np.ndarray, NDArrayAdapter()
... ]
>>> Model(a=[[1, 2], [3, 4]])
Model(a=array([[1, 2],
[3, 4]]))
Source code in src/scientific_pydantic/numpy/ndarray_adapter.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
__get_pydantic_core_schema__(_source_type: ty.Any, _handler: pydantic.GetCoreSchemaHandler) -> core_schema.CoreSchema
¶
Get the pydantic schema for an NDArray
Source code in src/scientific_pydantic/numpy/ndarray_adapter.py
__get_pydantic_json_schema__(_core_schema: core_schema.CoreSchema, _handler: pydantic.GetJsonSchemaHandler) -> JsonSchemaValue
¶
Generate JSON schema for the ndarray field