-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_dataarray.py
More file actions
79 lines (52 loc) · 1.49 KB
/
test_dataarray.py
File metadata and controls
79 lines (52 loc) · 1.49 KB
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
# standard library
from dataclasses import dataclass
from typing import Literal, Tuple
# dependencies
import numpy as np
import xarray as xr
from typing_extensions import TypeAlias
# submodules
from xarray_dataclasses.dataarray import AsDataArray
from xarray_dataclasses.dataoptions import DataOptions
from xarray_dataclasses.typing import Attr, Coord, Data, Name
# constants
DIMS = "x", "y"
SHAPE = 10, 10
# type hints
X = Literal["x"]
Y = Literal["y"]
Custom: TypeAlias = xr.DataArray
@dataclass
class Image(AsDataArray):
"""2D image as DataArray."""
__dataoptions__ = DataOptions(Custom)
data: Data[Tuple[X, Y], float]
x: Coord[X, int] = 0
y: Coord[Y, int] = 0
units: Attr[str] = "cd / m^2"
name: Name[str] = "luminance"
# test datasets
created = Image.ones(SHAPE)
expected = Custom(
np.ones(SHAPE, float),
dims=DIMS,
coords={
"x": xr.DataArray(np.zeros(SHAPE[0]), dims="x"),
"y": xr.DataArray(np.zeros(SHAPE[1]), dims="y"),
},
attrs={"units": "cd / m^2"},
name="luminance",
)
# test functions
def test_type() -> None:
assert type(created) is type(expected)
def test_data() -> None:
assert (created == expected).all() # type: ignore
def test_dtype() -> None:
assert created.dtype == expected.dtype # type: ignore
def test_dims() -> None:
assert created.dims == expected.dims
def test_attrs() -> None:
assert created.attrs == expected.attrs
def test_name() -> None:
assert created.name == expected.name