-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_component.py
284 lines (251 loc) · 9.82 KB
/
test_component.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import unittest
import doctest
from importlib import import_module
from datetime import timedelta
import numpy
import cf
import unifhy
from .test_time import get_dummy_timedomain
from .test_space import (
get_dummy_spacedomain, get_dummy_land_sea_mask_field,
get_dummy_flow_direction_field
)
from .test_data import (
get_dummy_dataset, get_dummy_component_substitute_dataset
)
time_resolutions = {
'surfacelayer': {
'same_t': 'daily',
'diff_t': 'daily'
},
'subsurface': {
'same_t': 'daily',
'diff_t': '4daily'
},
'openwater': {
'same_t': 'daily',
'diff_t': '2daily'
},
}
space_resolutions = {
'surfacelayer': {
'same_s': '1deg',
'diff_s': '1deg'
},
'subsurface': {
'same_s': '1deg',
'diff_s': 'pt5deg'
},
'openwater': {
'same_s': '1deg',
'diff_s': 'pt25deg'
},
}
parameters = {
'surfacelayer': {
'same_s': {},
'diff_s': {}
},
'subsurface': {
'same_s': {
'parameter_a':
cf.read(
'data/dummy_subsurface_parameter_a_{}'
'.nc'.format(space_resolutions['subsurface']['same_s'])
).select_field('long_name=parameter_a')
},
'diff_s': {
'parameter_a':
cf.read(
'data/dummy_subsurface_parameter_a_{}'
'.nc'.format(space_resolutions['subsurface']['diff_s'])
).select_field('long_name=parameter_a')
}
},
'openwater': {
'same_s': {
'parameter_c': [3, '1']
},
'diff_s': {
'parameter_c': [3, '1']
}
},
}
constants = {
'surfacelayer': {
'same_s': {},
'diff_s': {}
},
'subsurface': {
'same_s': {},
'diff_s': {}
},
'openwater': {
'same_s': {
'constant_c': cf.Data(3, '1')
},
'diff_s': {
'constant_c': cf.Data(3, '1')
}
},
}
records = {
'surfacelayer': {
'same_t': {'output_x': {timedelta(days=1): ['instantaneous'],
timedelta(days=8): ['cumulative', 'average',
'min', 'max']},
# using aliases for methods in 'output_x'
'transfer_i': {timedelta(days=1): ['point']},
'transfer_j': {timedelta(days=1): ['point']},
'state_a': {timedelta(days=1): ['point']},
'state_b': {timedelta(days=1): ['point']}},
'diff_t': {'output_x': {timedelta(days=1): ['point'],
timedelta(days=8): ['sum', 'mean',
'minimum', 'maximum']},
# using defaults for methods in 'output_x'
'transfer_i': {timedelta(days=1): ['point']},
'transfer_j': {timedelta(days=1): ['point']},
'state_a': {timedelta(days=1): ['point']},
'state_b': {timedelta(days=1): ['point']}}
},
'subsurface': {
'same_t': {'output_x': {timedelta(days=1): ['instantaneous'],
timedelta(days=8): ['cumulative', 'average',
'min', 'max']},
# using aliases for methods in 'output_x'
'transfer_k': {timedelta(days=1): ['point']},
'transfer_m': {timedelta(days=1): ['point']},
'state_a': {timedelta(days=1): ['point']},
'state_b': {timedelta(days=1): ['point']}},
'diff_t': {'output_x': {timedelta(days=4): ['point'],
timedelta(days=8): ['sum', 'mean',
'minimum', 'maximum']},
# using defaults for methods in 'output_x'
'transfer_k': {timedelta(days=4): ['point']},
'transfer_m': {timedelta(days=4): ['point']},
'state_a': {timedelta(days=4): ['point']},
'state_b': {timedelta(days=4): ['point']}},
},
'openwater': {
'same_t': {'output_x': {timedelta(days=1): ['instantaneous'],
timedelta(days=8): ['cumulative', 'average',
'min', 'max']},
# using aliases for methods in 'output_x'
'output_y': {timedelta(days=1): ['point']},
'transfer_l': {timedelta(days=1): ['point']},
'transfer_n': {timedelta(days=1): ['point']},
'transfer_o': {timedelta(days=1): ['point']},
'state_a': {timedelta(days=1): ['point']}},
'diff_t': {'output_x': {timedelta(days=2): ['point'],
timedelta(days=8): ['sum', 'mean',
'minimum', 'maximum']},
# using defaults for methods in 'output_x'
'output_y': {timedelta(days=2): ['point']},
'transfer_l': {timedelta(days=2): ['point']},
'transfer_n': {timedelta(days=2): ['point']},
'transfer_o': {timedelta(days=2): ['point']},
'state_a': {timedelta(days=2): ['point']}}
}
}
def get_dummy_component(category, kind, time_, space_, source):
# get component class
component_class = getattr(
import_module('tests.components.{}'.format(category)),
'Dummy{}'.format('' if source == 'Python' else source)
)
# get timedomain, spacedomain, dataset
time_resolution = time_resolutions[category][time_]
timedomain = get_dummy_timedomain(time_resolution)
space_resolution = space_resolutions[category][space_]
spacedomain = get_dummy_spacedomain(space_resolution)
if category == 'surfacelayer':
spacedomain.land_sea_mask = get_dummy_land_sea_mask_field(
space_resolution
)
spacedomain.flow_direction = get_dummy_flow_direction_field(
space_resolution
)
dataset = get_dummy_dataset(category, time_resolution, space_resolution)
if kind == 'c':
return component_class(
saving_directory='outputs',
timedomain=timedomain,
spacedomain=spacedomain,
dataset=dataset,
parameters=parameters[category][space_],
constants=constants[category],
records=records[category][time_],
io_slice=10
)
elif kind == 'd':
return unifhy.DataComponent(
timedomain=timedomain,
spacedomain=spacedomain,
dataset=get_dummy_component_substitute_dataset(
'{}_{}'.format(category, time_),
time_resolution, space_resolution
),
substituting_class=component_class,
io_slice=10
)
else: # kind == 'n'
return unifhy.NullComponent(
timedomain=timedomain,
spacedomain=spacedomain,
substituting_class=component_class
)
class TestSubstituteComponent(unittest.TestCase):
td = get_dummy_timedomain("daily")
sd = get_dummy_spacedomain("1deg")
def test_instantiation_datacomponent(self):
td = self.td
sd = self.sd
for substituting_class in [unifhy.component.SurfaceLayerComponent,
unifhy.component.SubSurfaceComponent,
unifhy.component.OpenWaterComponent]:
with self.subTest(subtituting_class=substituting_class.category):
# prepare dummy dataset for substituting component
ds = unifhy.data.DataSet()
for outward, info in substituting_class.outwards_info.items():
# create dummy field from dummy timedomain and spacedomain
fld = sd.to_field()
fld.set_construct(td.to_field().domain_axis('T'))
fld.set_construct(td.to_field().dimension_coordinate('T'))
# populate with zero data
fld.set_data(
numpy.zeros((td.time.size, sd.Y.size, sd.X.size)),
axes=('T', 'Y', 'X')
)
# define some basic metadata
fld.standard_name = outward
fld.units = info["units"]
# turn field into unifhy Variable
var = unifhy.data.Variable(fld, ())
# append Variable to DataSet
ds[outward] = var
# test instantiation of DataComponent
dc = unifhy.DataComponent(td, sd, ds, substituting_class)
self.assertEqual(
dc.outwards_info, substituting_class._outwards_info
)
def test_instantiation_nullcomponent(self):
td = self.td
sd = self.sd
for substituting_class in [unifhy.component.SurfaceLayerComponent,
unifhy.component.SubSurfaceComponent,
unifhy.component.OpenWaterComponent]:
with self.subTest(subtituting_class=substituting_class.category):
# test instantiation of NullComponent
nc = unifhy.NullComponent(td, sd, substituting_class)
self.assertEqual(
nc.outwards_info, substituting_class._outwards_info
)
if __name__ == '__main__':
test_loader = unittest.TestLoader()
test_suite = unittest.TestSuite()
test_suite.addTests(
test_loader.loadTestsFromTestCase(TestSubstituteComponent)
)
test_suite.addTests(doctest.DocTestSuite(unifhy.component))
runner = unittest.TextTestRunner(verbosity=2)
runner.run(test_suite)