File size: 20,937 Bytes
d086025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
diff --git a/hdp/tests/__init__.py b/hdp/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/hdp/tests/test_heatwave_average.py b/hdp/tests/test_heatwave_average.py
new file mode 100644
--- /dev/null
+++ b/hdp/tests/test_heatwave_average.py
@@ -0,0 +1,54 @@
+from hdp.metric import heatwave_average
+import numpy as np
+
+
+class TestHeatwaveAverage:
+    def test_heatwave_average_null_case(self):
+        hot_day_null = np.zeros(100, dtype=int)
+        season_range = np.array([[0, 100]], dtype=int)
+        assert np.array_equal(heatwave_average(hot_day_null, season_range), np.array([0]))
+
+    def test_heatwave_average_full_case(self):
+        hot_day_full = np.ones(100, dtype=bool)
+        season_range = np.array([[0, 100]], dtype=int)
+        assert np.array_equal(heatwave_average(hot_day_full, season_range), np.array([100]))
+
+    def test_heatwave_average_case1(self):
+        hot_day_case1 = np.array(
+            [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case1.size]], dtype=int)
+        assert np.array_equal(heatwave_average(hot_day_case1, season_range), np.array([5.5]))
+
+    def test_heatwave_average_case2(self):
+        hot_day_case2 = np.array(
+            [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 3],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case2.size]], dtype=int)
+        assert np.allclose(heatwave_average(hot_day_case2, season_range), np.array([3.333333]))
+
+    def test_heatwave_average_case3(self):
+        hot_day_case3 = np.array(
+            [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_average(hot_day_case3, season_range), np.array([1.75]))
+
+    def test_heatwave_average_season_full_case(self):
+        hot_day_full = np.ones(100, dtype=bool)
+        season_range = np.array([[0, 5], [0, 10], [20, 30], [42, 50]], dtype=int)
+        assert np.array_equal(heatwave_average(hot_day_full, season_range), np.array([5, 10, 10, 8]))
+
+    def test_heatwave_average_season_case1(self):
+        hot_day_case3 = np.array(
+            [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_average(hot_day_case3, season_range), np.array([6, 2.5]))
+
+    def test_heatwave_average_season_case2(self):
+        hot_day_case3 = np.array(
+            [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_average(hot_day_case3, season_range), np.array([1.5, 2]))
diff --git a/hdp/tests/test_heatwave_duration.py b/hdp/tests/test_heatwave_duration.py
new file mode 100644
--- /dev/null
+++ b/hdp/tests/test_heatwave_duration.py
@@ -0,0 +1,54 @@
+from hdp.metric import heatwave_duration
+import numpy as np
+
+
+class TestHeatwaveDuration:
+    def test_heatwave_duration_null_case(self):
+        hot_day_null = np.zeros(100, dtype=int)
+        season_range = np.array([[0, 100]], dtype=int)
+        assert np.array_equal(heatwave_duration(hot_day_null, season_range), np.array([0]))
+
+    def test_heatwave_duration_full_case(self):
+        hot_day_full = np.ones(100, dtype=bool)
+        season_range = np.array([[0, 100]], dtype=int)
+        assert np.array_equal(heatwave_duration(hot_day_full, season_range), np.array([100]))
+
+    def test_heatwave_duration_case1(self):
+        hot_day_case1 = np.array(
+            [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case1.size]], dtype=int)
+        assert np.array_equal(heatwave_duration(hot_day_case1, season_range), np.array([7]))
+
+    def test_heatwave_duration_case2(self):
+        hot_day_case2 = np.array(
+            [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 3],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case2.size]], dtype=int)
+        assert np.array_equal(heatwave_duration(hot_day_case2, season_range), np.array([5]))
+
+    def test_heatwave_duration_case3(self):
+        hot_day_case3 = np.array(
+            [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_duration(hot_day_case3, season_range), np.array([3]))
+
+    def test_heatwave_duration_season_full_case(self):
+        hot_day_full = np.ones(100, dtype=bool)
+        season_range = np.array([[0, 5], [0, 10], [20, 30], [42, 50]], dtype=int)
+        assert np.array_equal(heatwave_duration(hot_day_full, season_range), np.array([5, 10, 10, 8]))
+
+    def test_heatwave_duration_season_case1(self):
+        hot_day_case3 = np.array(
+            [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_duration(hot_day_case3, season_range), np.array([6, 4]))
+
+    def test_heatwave_duration_season_case2(self):
+        hot_day_case3 = np.array(
+            [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_duration(hot_day_case3, season_range), np.array([2, 3]))
diff --git a/hdp/tests/test_heatwave_frequency.py b/hdp/tests/test_heatwave_frequency.py
new file mode 100644
--- /dev/null
+++ b/hdp/tests/test_heatwave_frequency.py
@@ -0,0 +1,54 @@
+from hdp.metric import heatwave_frequency
+import numpy as np
+
+
+class TestHeatwaveFrequency:
+    def test_heatwave_frequency_null_case(self):
+        hot_day_null = np.zeros(100, dtype=int)
+        season_range = np.array([[0, 100]], dtype=int)
+        assert np.array_equal(heatwave_frequency(hot_day_null, season_range), np.array([0]))
+
+    def test_heatwave_frequency_full_case(self):
+        hot_day_full = np.ones(100, dtype=bool)
+        season_range = np.array([[0, 100]], dtype=int)
+        assert np.array_equal(heatwave_frequency(hot_day_full, season_range), np.array([100]))
+
+    def test_heatwave_frequency_case1(self):
+        hot_day_case1 = np.array(
+            [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case1.size]], dtype=int)
+        assert np.array_equal(heatwave_frequency(hot_day_case1, season_range), np.array([11]))
+
+    def test_heatwave_frequency_case2(self):
+        hot_day_case2 = np.array(
+            [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 3],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case2.size]], dtype=int)
+        assert np.array_equal(heatwave_frequency(hot_day_case2, season_range), np.array([10]))
+
+    def test_heatwave_frequency_case3(self):
+        hot_day_case3 = np.array(
+            [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_frequency(hot_day_case3, season_range), np.array([7]))
+
+    def test_heatwave_frequency_season_full_case(self):
+        hot_day_full = np.ones(100, dtype=bool)
+        season_range = np.array([[0, 5], [0, 10], [20, 30], [42, 50]], dtype=int)
+        assert np.array_equal(heatwave_frequency(hot_day_full, season_range), np.array([5, 10, 10, 8]))
+
+    def test_heatwave_frequency_season_case1(self):
+        hot_day_case3 = np.array(
+            [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_frequency(hot_day_case3, season_range), np.array([6, 5]))
+
+    def test_heatwave_frequency_season_case2(self):
+        hot_day_case3 = np.array(
+            [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_frequency(hot_day_case3, season_range), np.array([3, 4]))
diff --git a/hdp/tests/test_heatwave_number.py b/hdp/tests/test_heatwave_number.py
new file mode 100644
--- /dev/null
+++ b/hdp/tests/test_heatwave_number.py
@@ -0,0 +1,54 @@
+from hdp.metric import heatwave_number
+import numpy as np
+
+
+class TestHeatwaveNumber:
+    def test_heatwave_number_null_case(self):
+        hot_day_null = np.zeros(100, dtype=int)
+        season_range = np.array([[0, 100]], dtype=int)
+        assert np.array_equal(heatwave_number(hot_day_null, season_range), np.array([0]))
+
+    def test_heatwave_number_full_case(self):
+        hot_day_full = np.ones(100, dtype=bool)
+        season_range = np.array([[0, 100]], dtype=int)
+        assert np.array_equal(heatwave_number(hot_day_full, season_range), np.array([1]))
+
+    def test_heatwave_number_case1(self):
+        hot_day_case1 = np.array(
+            [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case1.size]], dtype=int)
+        assert np.array_equal(heatwave_number(hot_day_case1, season_range), np.array([2]))
+
+    def test_heatwave_number_case2(self):
+        hot_day_case2 = np.array(
+            [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 3],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case2.size]], dtype=int)
+        assert np.array_equal(heatwave_number(hot_day_case2, season_range), np.array([3]))
+
+    def test_heatwave_number_case3(self):
+        hot_day_case3 = np.array(
+            [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_number(hot_day_case3, season_range), np.array([4]))
+
+    def test_heatwave_number_season_full_case(self):
+        hot_day_full = np.ones(100, dtype=bool)
+        season_range = np.array([[0, 5], [0, 10], [20, 30], [42, 50]], dtype=int)
+        assert np.array_equal(heatwave_number(hot_day_full, season_range), np.array([1, 1, 1, 1]))
+
+    def test_heatwave_number_season_case1(self):
+        hot_day_case3 = np.array(
+            [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_number(hot_day_case3, season_range), np.array([1, 2]))
+
+    def test_heatwave_number_season_case2(self):
+        hot_day_case3 = np.array(
+            [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0],
+            dtype=int)
+        season_range = np.array([[0, 8], [8, hot_day_case3.size]], dtype=int)
+        assert np.array_equal(heatwave_number(hot_day_case3, season_range), np.array([2, 2]))
diff --git a/hdp/tests/test_index_heatwaves.py b/hdp/tests/test_index_heatwaves.py
new file mode 100644
--- /dev/null
+++ b/hdp/tests/test_index_heatwaves.py
@@ -0,0 +1,41 @@
+from hdp.metric import index_heatwaves
+import numpy as np
+import pytest
+
+
+class TestIndexHeatwave:
+    def test_index_heatwaves_null_case(self):
+        hot_day_null = np.zeros(100, dtype=bool)
+        assert np.array_equal(index_heatwaves(hot_day_null, 1, 1, 1), np.zeros(hot_day_null.size))
+        assert np.array_equal(index_heatwaves(hot_day_null, 1, 0, 1), np.zeros(hot_day_null.size))
+        assert np.array_equal(index_heatwaves(hot_day_null, 0, 0, 1), np.zeros(hot_day_null.size))
+
+    def test_index_heatwaves_full_case(self):
+        hot_day_full = np.ones(100, dtype=bool)
+        assert np.array_equal(index_heatwaves(hot_day_full, 1, 1, 1), np.ones(hot_day_full.size))
+        assert np.array_equal(index_heatwaves(hot_day_full, 1, 0, 1), np.ones(hot_day_full.size))
+        assert np.array_equal(index_heatwaves(hot_day_full, 0, 0, 1), np.ones(hot_day_full.size))
+
+    def test_index_heatwaves_case1(self):
+        hot_day_case1 = np.array(
+            [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0],
+            dtype=bool)
+        assert np.array_equal(index_heatwaves(hot_day_case1, 1, 1, 1), [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 0, 0])
+        assert np.array_equal(index_heatwaves(hot_day_case1, 1, 0, 1), [0, 1, 1, 1, 1, 0, 2, 2, 2, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0])
+        assert np.array_equal(index_heatwaves(hot_day_case1, 0, 0, 1), [0, 1, 1, 1, 1, 0, 2, 2, 2, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0])
+
+    def test_index_heatwaves_case2(self):
+        hot_day_case2 = np.array(
+            [0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1],
+            dtype=bool)
+        assert np.array_equal(index_heatwaves(hot_day_case2, 1, 1, 1), [0, 1, 0, 1, 1, 0, 2, 2, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 4])
+        assert np.array_equal(index_heatwaves(hot_day_case2, 1, 0, 1), [0, 1, 0, 2, 2, 0, 3, 3, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 5])
+        assert np.array_equal(index_heatwaves(hot_day_case2, 0, 0, 1), [0, 1, 0, 2, 2, 0, 3, 3, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 0, 5])
+
+    def test_index_heatwaves_case3(self):
+        hot_day_case3 = np.array(
+            [0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
+            dtype=bool)
+        assert np.array_equal(index_heatwaves(hot_day_case3, 1, 1, 1), [0, 0, 0, 1, 1, 0, 1, 0, 2, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0])
+        assert np.array_equal(index_heatwaves(hot_day_case3, 1, 0, 1), [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0])
+        assert np.array_equal(index_heatwaves(hot_day_case3, 0, 0, 1), [0, 0, 0, 1, 1, 0, 2, 0, 3, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0])
diff --git a/hdp/tests/test_utils.py b/hdp/tests/test_utils.py
new file mode 100644
--- /dev/null
+++ b/hdp/tests/test_utils.py
@@ -0,0 +1,57 @@
+from hdp.utils import *
+import numpy as np
+from math import isclose
+from xarray import DataArray
+
+def test_get_time_stamp():
+    assert type(get_time_stamp()) is str
+
+
+def test_get_version():
+    assert type(get_version()) is str
+
+
+def test_control_dataarray():
+    control_da = generate_test_control_dataarray(grid_shape=(1,1), start_date="2000", end_date="2100")
+    assert type(control_da) is DataArray
+    var = control_da.name
+    assert control_da.attrs["units"] == "degC"
+    assert control_da.dims == ("lon", "lat", "time")
+    assert control_da.dtype == float
+    assert control_da.time.values[0].calendar == "noleap"
+    assert control_da.time.values.size >= 365
+    assert np.sum(np.isnan(control_da)) == 0
+
+    control_slope = np.polyfit(np.arange(control_da["time"].size), control_da.mean(dim=["lat", "lon"]).values, 1)[0]
+    assert np.abs(control_slope) < 0.01
+
+
+def test_warming_dataarray():
+    warming_da = generate_test_warming_dataarray(grid_shape=(1,1), start_date="2000", end_date="2100")
+    avg_da = warming_da.mean(dim=["lat", "lon"]).values
+    warm_slope = np.polyfit(np.arange(avg_da.size), avg_da, 1)[0]
+
+    assert warm_slope > 0
+    assert not isclose(warm_slope, 0)
+
+
+def test_rh_dataarray():
+    rh_da = generate_test_rh_dataarray()
+
+    assert rh_da.max() <= 1
+    assert rh_da.min() >= 0
+
+
+def test_defaults_compatibility():
+    control_da = generate_test_control_dataarray()
+    warming_da = generate_test_warming_dataarray()
+    rh_da = generate_test_rh_dataarray()
+
+    assert control_da.shape == warming_da.shape
+    assert warming_da.shape == rh_da.shape
+
+    assert control_da.dims == warming_da.dims
+    assert warming_da.dims == rh_da.dims
+
+    assert control_da.name == warming_da.name
+    assert control_da.units == warming_da.units
diff --git a/hdp/tests/test_workflow.py b/hdp/tests/test_workflow.py
new file mode 100644
--- /dev/null
+++ b/hdp/tests/test_workflow.py
@@ -0,0 +1,99 @@
+from hdp.graphics.notebook import create_notebook
+import pytest
+import hdp.utils
+import hdp.measure
+import hdp.threshold
+import hdp.metric
+import numpy as np
+
+
+@pytest.fixture(scope="function")
+def temp_output_dir(tmp_path_factory):
+    return tmp_path_factory.mktemp("output_dir")
+
+
+def test_full_data_workflow(temp_output_dir):
+    grid_shape = (2, 3)
+
+    baseline_temp = hdp.utils.generate_test_control_dataarray(grid_shape=grid_shape).rename("temp")
+    baseline_rh = hdp.utils.generate_test_rh_dataarray(grid_shape=grid_shape).rename("rh")
+    baseline_measures = hdp.measure.format_standard_measures([baseline_temp], rh=baseline_rh)
+    
+    percentiles = np.arange(0.9, 1, 0.01)
+    
+    thresholds = hdp.threshold.compute_thresholds(baseline_measures, percentiles=percentiles)
+        
+    test_temp = hdp.utils.generate_test_warming_dataarray(grid_shape=grid_shape).rename("temp")
+    test_rh = baseline_rh
+    
+    hw_definitions = [[3,0,0], [3,1,1], [4,2,0], [4,1,3], [5,0,1], [5,1,4]]
+    
+    test_measures = hdp.measure.format_standard_measures([test_temp], rh=test_rh)
+    metrics = hdp.metric.compute_group_metrics(test_measures, thresholds, hw_definitions)
+    
+    metrics = metrics.compute()
+    thresholds = thresholds.compute()
+    
+    assert (thresholds.percentile.values == percentiles).all()
+    assert len(thresholds.data_vars) == 2
+    
+    assert metrics.definition.values[0] == "3-0-0"
+    assert metrics.definition.values[1] == "3-1-1"
+    assert metrics.definition.values[2] == "4-2-0"
+    assert metrics.definition.values[3] == "4-1-3"
+    assert metrics.definition.values[4] == "5-0-1"
+    assert metrics.definition.values[5] == "5-1-4"
+    assert (metrics.percentile.values == percentiles).all()
+
+    metric_means = metrics.mean()
+
+    assert metric_means["temp.temp_threshold.HWF"] >= metric_means["temp.temp_threshold.HWD"]
+    assert metric_means["temp.temp_threshold.HWD"] >= metric_means["temp.temp_threshold.HWA"]
+    
+    for var in metrics:
+        assert metrics[var].shape == (metrics.percentile.size, metrics.definition.size, metrics.lon.size, metrics.lat.size, metrics.time.size) 
+        assert metrics[var].dtype == int
+        if "HWF" in var or "HWD" in var:
+            assert metrics[var].attrs["units"] == 'heatwave days', f"Variable '{var}' has incorrect units '{metrics[var].attrs["units"]}'"
+        elif "HWN" in var or "HWA" in var:
+            assert metrics[var].attrs["units"] == 'heatwave events', f"Variable '{var}' has incorrect units '{metrics[var].attrs["units"]}'"
+        else:
+            assert False, f"Cannot determine primary heatwave metric from variable '{var}'."
+
+    figure_notebook = create_notebook(metrics)
+    figure_notebook.save_notebook(f"{temp_output_dir}/sample_hw_summary_figures.ipynb")
+
+
+def test_include_threshold(temp_output_dir):
+    grid_shape = (2, 3)
+
+    baseline_temp = hdp.utils.generate_test_control_dataarray(grid_shape=grid_shape).rename("temp")
+    test_temp = hdp.utils.generate_test_warming_dataarray(grid_shape=grid_shape).rename("temp")
+
+    baseline_measures = hdp.measure.format_standard_measures([baseline_temp])
+    test_measures = hdp.measure.format_standard_measures([test_temp])
+
+    percentiles = np.arange(0.9, 1, 0.02)
+    thresholds = hdp.threshold.compute_thresholds(baseline_measures, percentiles=percentiles)
+
+    hw_definitions = [[3, 0, 0], [3, 1, 1]]
+
+    metrics_with = hdp.metric.compute_group_metrics(test_measures, thresholds, hw_definitions, include_threshold=True).compute()
+    metrics_without = hdp.metric.compute_group_metrics(test_measures, thresholds, hw_definitions, include_threshold=False).compute()
+    thresholds = thresholds.compute()
+
+    # The threshold variable is embedded under its own name only when include_threshold=True.
+    assert "temp_threshold" in metrics_with.data_vars
+    assert "temp_threshold" not in metrics_without.data_vars
+
+    # The metric variables are unaffected by include_threshold.
+    metric_vars = [var for var in metrics_with.data_vars if var != "temp_threshold"]
+    assert set(metric_vars) == set(metrics_without.data_vars)
+
+    # The embedded threshold matches the source threshold exactly.
+    embedded = metrics_with["temp_threshold"]
+    source = thresholds["temp_threshold"]
+    assert embedded.broadcast_like(source).equals(source.broadcast_like(embedded))
+
+    figure_notebook = create_notebook(metrics_with)
+    figure_notebook.save_notebook(f"{temp_output_dir}/sample_hw_summary_figures.ipynb")