-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_ndarray.py
142 lines (97 loc) · 3.93 KB
/
test_ndarray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
from pathlib import Path
from typing import Optional
from typing import Dict
import numpy as np
import pytest
from numpy.testing import assert_allclose
from pydantic import BaseModel, ValidationError
import pydantic_numpy.dtype as pnd
from pydantic_numpy import NDArray, NPFileDesc, PotentialNDArray
JSON_ENCODERS = {np.ndarray: lambda arr: arr.tolist()}
class NDArrayTestingModel(BaseModel):
K: pnd.NDArrayFp32
class Config:
json_encoders = JSON_ENCODERS
def test_init_from_values():
# Directly specify values
cfg = NDArrayTestingModel(K=[1, 2])
assert_allclose(cfg.K, [1.0, 2.0])
assert cfg.K.dtype == np.float32
assert cfg.json()
cfg = NDArrayTestingModel(K=np.eye(2))
assert_allclose(cfg.K, [[1.0, 0], [0.0, 1.0]])
assert cfg.K.dtype == np.float32
def test_load_from_npy_path(tmpdir):
# Load from npy
np.save(Path(tmpdir) / "data.npy", np.arange(5))
cfg = NDArrayTestingModel(K={"path": Path(tmpdir) / "data.npy"})
assert_allclose(cfg.K, [0.0, 1.0, 2.0, 3.0, 4.0])
assert cfg.K.dtype == np.float32
def test_load_from_NPFileDesc(tmpdir):
np.save(Path(tmpdir) / "data.npy", np.arange(5))
cfg = NDArrayTestingModel(K=NPFileDesc(path=Path(tmpdir) / "data.npy"))
assert_allclose(cfg.K, [0.0, 1.0, 2.0, 3.0, 4.0])
assert cfg.K.dtype == np.float32
def test_load_field_from_npz(tmpdir):
np.savez(Path(tmpdir) / "data.npz", values=np.arange(5))
cfg = NDArrayTestingModel(K={"path": Path(tmpdir) / "data.npz", "key": "values"})
assert_allclose(cfg.K, [0.0, 1.0, 2.0, 3.0, 4.0])
assert cfg.K.dtype == np.float32
def test_exceptional(tmpdir):
with pytest.raises(ValidationError):
NDArrayTestingModel(K={"path": Path(tmpdir) / "nosuchfile.npz", "key": "values"})
with pytest.raises(ValidationError):
NDArrayTestingModel(K={"path": Path(tmpdir) / "nosuchfile.npy", "key": "nosuchkey"})
with pytest.raises(ValidationError):
NDArrayTestingModel(K={"path": Path(tmpdir) / "nosuchfile.npy"})
with pytest.raises(ValidationError):
NDArrayTestingModel(K="absc")
def test_unspecified_npdtype():
# Not specifying a dtype will use numpy default dtype resolver
class NDArrayNoGeneric(BaseModel):
K: NDArray
cfg = NDArrayNoGeneric(K=[1, 2])
assert_allclose(cfg.K, [1, 2])
assert cfg.K.dtype == int
def test_json_encoders():
import json
class NDArrayNoGeneric(BaseModel):
K: NDArray
class Config:
json_encoders = JSON_ENCODERS
cfg = NDArrayNoGeneric(K=[1, 2])
jdata = json.loads(cfg.json())
assert "K" in jdata
assert type(jdata["K"]) == list
assert jdata["K"] == list([1, 2])
def test_optional_construction():
class NDArrayOptional(BaseModel):
K: Optional[pnd.NDArrayFp32]
cfg = NDArrayOptional()
assert cfg.K is None
cfg = NDArrayOptional(K=[1, 2])
assert type(cfg.K) == np.ndarray
assert cfg.K.dtype == np.float32
def test_potential_array(tmpdir):
class NDArrayPotential(BaseModel):
K: PotentialNDArray[pnd.float32]
np.savez(Path(tmpdir) / "data.npz", values=np.arange(5))
cfg = NDArrayPotential(K={"path": Path(tmpdir) / "data.npz", "key": "values"})
assert cfg.K is not None
assert_allclose(cfg.K, [0.0, 1.0, 2.0, 3.0, 4.0])
# Path not found
cfg = NDArrayPotential(K={"path": Path(tmpdir) / "nothere.npz", "key": "values"})
assert cfg.K is None
# Key not there
cfg = NDArrayPotential(K={"path": Path(tmpdir) / "data.npz", "key": "nothere"})
assert cfg.K is None
def test_subclass_basemodel():
model_field = NDArrayTestingModel(K=[1.0, 2.0])
assert model_field.json()
class MappingTestingModel(BaseModel):
L: Dict[str, NDArrayTestingModel]
class Config:
json_encoders = JSON_ENCODERS
model = MappingTestingModel(L={"a": NDArrayTestingModel(K=[1.0, 2.0])})
assert model.L["a"].K.dtype == np.dtype("float32")
assert model.json()