-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Naming changes for typing, and package reorganization Chore: Bump version, Major -> Minor
- Loading branch information
Showing
34 changed files
with
1,806 additions
and
1,038 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
name: Python application test with GitHub Actions | ||
|
||
on: | ||
push: | ||
branches: [ trunk ] | ||
pull_request: | ||
branches: [ trunk ] | ||
|
||
jobs: | ||
unit_tests: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
python-version: [ "3.9", "3.10", "3.11" ] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Setup the Python Environment ${{ matrix.python-version }} | ||
uses: Qwerty-133/python-setup@v1 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
skip-pre-commit: true | ||
|
||
- name: Install dependencies | ||
run: | | ||
poetry install --all-extras --with dev | ||
- name: Run tests | ||
run: | | ||
poetry run pytest . | ||
- name: Upload coverage reports to Codecov | ||
uses: codecov/codecov-action@v3 | ||
env: | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||
mypy: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
python-version: [ "3.9", "3.10", "3.11" ] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Setup the Python Environment ${{ matrix.python-version }} | ||
uses: Qwerty-133/python-setup@v1 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
skip-pre-commit: true | ||
|
||
- name: Install dependencies | ||
run: | | ||
poetry install --all-extras --with dev --with ci | ||
- name: Validate type-hints with MyPy | ||
run: | | ||
poetry run mypy --ignore-missing-imports \ | ||
--follow-imports=skip \ | ||
--strict-optional \ | ||
-p pydantic_numpy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,71 @@ | ||
# pydantic-numpy | ||
|
||
Integrate NumPy into Pydantic, and provide tooling! `NumpyModel` make it possible to dump and load `np.ndarray` within model fields! | ||
Package that integrates NumPy Arrays into Pydantic! | ||
|
||
### Install | ||
```shell | ||
pip install pydantic-numpy | ||
``` | ||
- `NumpyModel` make it possible to dump and load `np.ndarray` within model fields alongside other fields that are not instances of `np.ndarray`! | ||
- `pydantic_numpy.typing` provides many typings such as `NpNDArrayFp64`, `Np3DArrayFp64` (float64 that must be 3D)! | ||
|
||
## Usage | ||
|
||
For more examples see [test_ndarray.py](./tests/test_ndarray.py) | ||
For more examples see [test_ndarray.py](./tests/test_typing.py) | ||
|
||
```python | ||
import pydantic_numpy.dtype as pnd | ||
from pydantic_numpy import NDArray, NDArrayFp32, NumpyModel | ||
import numpy as np | ||
|
||
import pydantic_numpy.typing as pnd | ||
from pydantic_numpy import np_array_pydantic_annotated_typing | ||
from pydantic_numpy.model import NumpyModel, MultiArrayNumpyFile | ||
|
||
|
||
class MyNumpyModel(NumpyModel): | ||
any_array_dtype_and_dimension: pnd.NpNDArray | ||
|
||
class MyPydanticNumpyModel(NumpyModel): | ||
K: NDArray[float, pnd.float32] | ||
C: NDArrayFp32 # <- Shorthand for same type as K | ||
# 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 MyDemoModel(NumpyModel): | ||
k: np_array_pydantic_annotated_typing(data_type=np.float32) | ||
|
||
|
||
# Instantiate from array | ||
cfg = MyPydanticNumpyModel(K=[1, 2]) | ||
cfg = MyDemoModel(k=[1, 2]) | ||
# Instantiate from numpy file | ||
cfg = MyPydanticNumpyModel(K={"path": "path_to/array.npy"}) | ||
cfg = MyDemoModel(k="path_to/array.npy") | ||
# Instantiate from npz file with key | ||
cfg = MyPydanticNumpyModel(K={"path": "path_to/array.npz", "key": "K"}) | ||
cfg = MyDemoModel(k=MultiArrayNumpyFile(path="path_to/array.npz", key="k")) | ||
|
||
cfg.K | ||
# np.ndarray[np.float32] | ||
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 mode, use `model_agnostic_load` when you have several models that may be the right model. | ||
|
||
### Data type (dtype) support! | ||
`NumpyModel.load` requires the original mode, use `model_agnostic_load` when you have several models that may be the right model: | ||
```python | ||
from pydantic_numpy.model.np_model import model_agnostic_load | ||
|
||
This package also comes with `pydantic_numpy.dtype`, which adds subtyping support such as `NDArray[float, pnd.float32]`. All subfields must be from this package as numpy dtypes have no Pydantic support, which is implemented in this package through the [generic class workflow](https://pydantic-docs.helpmanual.io/usage/types/#generic-classes-as-types). | ||
cfg.dump("path_to_dump_dir", "object_id") | ||
equals_cfg = model_agnostic_load("path_to_dump_dir", "object_id", models=[MyNumpyModel, MyDemoModel]) | ||
``` | ||
|
||
## Considerations | ||
### Data type (dtype) support! | ||
|
||
The `NDArray` class from `pydantic-numpy` is daughter of `np.ndarray`. IDEs and linters might complain that you are passing an incorrect `type` to a model. The only solution is to merge these change into `numpy`. | ||
This package also comes with `pydantic_numpy.dtype`, which adds subtyping support such as `NpNDArray[float, pnd.float32]`. All subfields must be from this package as numpy dtypes have no Pydantic support, which is implemented in this package through the [generic class workflow](https://pydantic-docs.helpmanual.io/usage/types/#generic-classes-as-types). | ||
|
||
You can also use the `typings` in `pydantic.validate_arguments`. | ||
### Install | ||
```shell | ||
pip install pydantic-numpy | ||
``` | ||
|
||
You can install from [cheind's](https://github.com/cheind/pydantic-numpy) repository if you want Python `3.8` support. | ||
## Considerations | ||
You can install from [cheind's](https://github.com/cheind/pydantic-numpy) repository if you want Python `3.8` support, but this version only support Pydantic V1 and will not work with V2. | ||
|
||
## History | ||
### Licensing notice | ||
As of version `3.0.0` the license has moved over to BSD-4. The versions prior are under the MIT license. | ||
|
||
The original idea originates from [this discussion](https://gist.github.com/danielhfrank/00e6b8556eed73fb4053450e602d2434), and forked from [cheind's](https://github.com/cheind/pydantic-numpy) repository. | ||
### History | ||
The original idea originates from [this discussion](https://gist.github.com/danielhfrank/00e6b8556eed73fb4053450e602d2434), and forked from [cheind's](https://github.com/cheind/pydantic-numpy) repository. |
Oops, something went wrong.