-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_dataset.py
More file actions
86 lines (60 loc) · 1.73 KB
/
test_dataset.py
File metadata and controls
86 lines (60 loc) · 1.73 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
80
81
82
83
84
85
86
# 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.dataset import AsDataset
from xarray_dataclasses.dataoptions import DataOptions
from xarray_dataclasses.typing import Attr, Coord, Data
# constants
DIMS = "x", "y"
SHAPE = 10, 10
# type hints
X = Literal["x"]
Y = Literal["y"]
Custom: TypeAlias = xr.Dataset
@dataclass
class Image(AsDataArray):
"""2D image as DataArray."""
data: Data[Tuple[X, Y], float]
@dataclass
class ColorImage(AsDataset):
"""2D color image as Dataset."""
__dataoptions__ = DataOptions(Custom)
red: Data[Tuple[X, Y], float]
green: Data[Tuple[X, Y], float]
blue: Data[Tuple[X, Y], float]
x: Coord[X, int] = 0
y: Coord[Y, int] = 0
units: Attr[str] = "cd / m^2"
# test datasets
created = ColorImage.new(
Image.ones(SHAPE),
Image.ones(SHAPE),
Image.ones(SHAPE),
)
expected = Custom(
data_vars={
"red": xr.DataArray(np.ones(SHAPE), dims=DIMS),
"green": xr.DataArray(np.ones(SHAPE), dims=DIMS),
"blue": xr.DataArray(np.ones(SHAPE), 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"},
)
# test functions
def test_type() -> None:
assert type(created) is type(expected)
def test_data_vars() -> None:
assert (created == expected).all() # type: ignore
def test_dims() -> None:
assert created.dims == expected.dims
def test_attrs() -> None:
assert created.attrs == expected.attrs