8
8
9
9
10
10
class SchemaError (Exception ):
11
+ '''Custom Schema Error'''
12
+
11
13
pass
12
14
13
15
14
16
class DataArraySchema :
17
+ '''A light-weight xarray.DataArray validator
18
+
19
+ Parameters
20
+ ----------
21
+ dtype : Any, optional
22
+ Datatype of the the variable. If a string is specified it must be a valid NumPy data type value, by default None
23
+ shape : Tuple[Union[int, None]], optional
24
+ Shape of the DataArray. `None` may be used as a wildcard value. By default None
25
+ dims : Tuple[Union[Hashable, None]], optional
26
+ Dimensions of the DataArray. `None` may be used as a wildcard value. By default None
27
+ name : str, optional
28
+ Name of the DataArray, by default None
29
+ array_type : Any, optional
30
+ Type of the underlying data in a DataArray (e.g. `numpy.ndarray`), by default None
31
+ checks : Iterable[Callable], optional
32
+ List of callables that take and return a DataArray, by default None'''
33
+
15
34
def __init__ (
16
35
self ,
17
36
dtype : Any = None ,
@@ -20,9 +39,9 @@ def __init__(
20
39
coords : Dict [Hashable , Any ] = None ,
21
40
chunks : Dict [Hashable , Union [int , None ]] = None ,
22
41
name : str = None ,
23
- checks : Iterable [Callable ] = None ,
24
42
array_type : Any = None ,
25
43
attrs : Dict [Hashable , Any ] = None ,
44
+ checks : Iterable [Callable ] = None ,
26
45
) -> None :
27
46
28
47
self .dtype = dtype
@@ -37,6 +56,22 @@ def __init__(
37
56
self .checks = checks if checks is not None else []
38
57
39
58
def validate (self , da : xr .DataArray ) -> xr .DataArray :
59
+ '''Check if the DataArray complies with the Schema.
60
+
61
+ Parameters
62
+ ----------
63
+ da : xr.DataArray
64
+ DataArray to be validated
65
+
66
+ Returns
67
+ -------
68
+ xr.DataArray
69
+ Validated DataArray
70
+
71
+ Raises
72
+ ------
73
+ SchemaError
74
+ '''
40
75
41
76
if self .dtype is not None and not np .issubdtype (da .dtype , self .dtype ):
42
77
raise SchemaError (f'dtype { da .dtype } != { self .dtype } ' )
@@ -84,6 +119,16 @@ def validate(self, da: xr.DataArray) -> xr.DataArray:
84
119
85
120
86
121
class DatasetSchema :
122
+ '''A light-weight xarray.Dataset validator
123
+
124
+ Parameters
125
+ ----------
126
+ data_vars : mapping of variable names and DataArraySchemas, optional
127
+ Per-variable DataArraySchema's, by default None
128
+ checks : Iterable[Callable], optional
129
+ Dataset wide checks, by default None
130
+ '''
131
+
87
132
def __init__ (
88
133
self ,
89
134
data_vars : Dict [Hashable , Union [DataArraySchema , None ]] = None ,
@@ -98,6 +143,22 @@ def __init__(
98
143
self .checks = checks
99
144
100
145
def validate (self , ds : xr .Dataset ) -> xr .Dataset :
146
+ '''Check if the Dataset complies with the Schema.
147
+
148
+ Parameters
149
+ ----------
150
+ ds : xr.Dataset
151
+ Dataset to be validated
152
+
153
+ Returns
154
+ -------
155
+ xr.Dataset
156
+ Validated Dataset
157
+
158
+ Raises
159
+ ------
160
+ SchemaError
161
+ '''
101
162
102
163
if self .data_vars is not None :
103
164
for key , da_schema in self .data_vars .items ():
0 commit comments