/
residual.py
551 lines (495 loc) · 18.6 KB
/
residual.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
""""""
from collections import OrderedDict
from copy import deepcopy
import numpy as np
import yaml
from landlab import RasterModelGrid, create_grid
from numpy.testing import assert_array_equal
import umami.calculations.metric as metric_calcs
import umami.calculations.residual as residual_calcs
from umami.metric import Metric
from umami.utils.create_landlab_components import _create_landlab_components
from umami.utils.io import _read_input, _write_output
from umami.utils.validate import _validate_fields, _validate_func
_VALID_FUNCS = {}
_VALID_FUNCS.update(residual_calcs.__dict__)
_VALID_FUNCS.update(metric_calcs.__dict__)
class Residual(object):
"""Create a ``Residual`` class based on a model and data Landlab grid."""
_required_fields = ["topographic__elevation"]
def __init__(
self,
model,
data,
flow_accumulator_kwds=None,
chi_finder_kwds=None,
residuals=None,
):
"""
Parameters
----------
model : Landlab model grid
data : Landlab model grid
flow_accumulator_kwds : dict
Parameters to pass to the Landlab ``FlowAccumulator`` to specify
flow direction and accumulation.
chi_finder_kwds : dict
Parameters to pass to the Landlab ``ChiFinder`` to specify optional
arguments. `
residuals : dict
A dictionary of desired residuals to calculate. See examples for
required format.
Examples
--------
>>> import numpy as np
>>> from io import StringIO
>>> from landlab import RasterModelGrid
>>> from umami import Residual
>>> np.random.seed(42)
>>> model = RasterModelGrid((10, 10))
>>> z_model = model.add_zeros("node", "topographic__elevation")
>>> z_model += model.x_of_node + model.y_of_node
>>> data = RasterModelGrid((10, 10))
>>> z_data = data.add_zeros("node", "topographic__elevation")
>>> z_data += data.x_of_node + data.y_of_node
>>> z_data[data.core_nodes] += np.random.random(data.core_nodes.shape)
>>> file_like=StringIO('''
... me:
... _func: aggregate
... method: mean
... field: topographic__elevation
... ep10:
... _func: aggregate
... method: percentile
... field: topographic__elevation
... q: 10
... oid1_mean:
... _func: watershed_aggregation
... field: topographic__elevation
... method: mean
... outlet_id: 1
... sn1:
... _func: count_equal
... field: drainage_area
... value: 1
... ''')
>>> residual = Residual(model, data)
>>> residual.add_from_file(file_like)
>>> residual.names
['me', 'ep10', 'oid1_mean', 'sn1']
>>> residual.calculate()
>>> np.testing.assert_array_almost_equal(
... residual.value("me"),
... -0.467,
... decimal=3)
>>> np.testing.assert_array_almost_equal(
... residual.values,
... np.array([ -0.467, -0.151, 3.313, -18. ]),
... decimal=3)
"""
# assert that the model grids have the same x_of_node and y_of_node.
assert_array_equal(data.x_of_node, model.x_of_node)
assert_array_equal(data.y_of_node, model.y_of_node)
assert_array_equal(data.core_nodes, model.core_nodes)
self._data_grid = data
self._model_grid = model
# verify that apppropriate fields are present.
for field in self._required_fields:
if (field not in model.at_node) or (field not in data.at_node):
msg = "umami: Required field: {field} is missing.".format(
field=field
)
raise ValueError(msg)
# run FlowAccumulator and ChiFinder
self._data_fa, self._data_cf = _create_landlab_components(
self._data_grid,
chi_finder_kwds=chi_finder_kwds,
flow_accumulator_kwds=flow_accumulator_kwds,
)
self._model_fa, self._model_cf = _create_landlab_components(
self._model_grid,
chi_finder_kwds=chi_finder_kwds,
flow_accumulator_kwds=flow_accumulator_kwds,
)
# determine which residuals are desired.
self._residuals = OrderedDict(residuals or {})
self._validate_residuals(self._residuals)
self._distinguish_metric_from_resid()
self._category = None
# set up metric and residual objects
self._data_metric = Metric(
data,
flow_accumulator_kwds=flow_accumulator_kwds,
chi_finder_kwds=chi_finder_kwds,
metrics=self._metrics,
)
self._model_metric = Metric(
model,
flow_accumulator_kwds=flow_accumulator_kwds,
chi_finder_kwds=chi_finder_kwds,
metrics=self._metrics,
)
@property
def names(self):
"""Names of residuals in residual order."""
self._names = []
for key, info in self._residuals.items():
if info["_func"] != "discretized_misfit":
self._names.append(key)
else:
n_f1_levels = np.size(info["field_1_percentile_edges"]) - 1
n_f2_levels = np.size(info["field_2_percentile_edges"]) - 1
label = info["name"]
for f1l in range(n_f1_levels):
for f2l in range(n_f2_levels):
n = label.format(field_1_level=f1l, field_2_level=f2l)
self._names.append(n)
return self._names
@property
def category(self):
"""Category labels from discretized_misfit.
Returns
-------
category: ndarray of size (number of nodes,)
The category labels used with ``discretized_misfit`` or ``None`` if
this calculation is not used.
"""
return self._category
def value(self, name):
"""Get a specific residual value.
Parameters
----------
name: str
Name of desired residual.
"""
return self._values[name]
@property
def values(self):
"""Residual values in residual order."""
return [self._values[key] for key in self.names]
def add_from_file(self, file):
"""Add residuals to an ``umami.Residual`` from a file.
Parameters
----------
file_like : file path or StringIO
File will be parsed by ``yaml.safe_load`` and converted to an
``OrderedDict``.
"""
params = _read_input(file)
self.add_from_dict(params)
def add_from_dict(self, params):
"""Add residuals to an ``umami.Residual`` from a dictionary.
Adding residuals through this method does not overwrite already existing
residuals. New residuals are appended to the existing residual list.
Parameters
----------
params : dict or OrderedDict
Keys are residual names and values are a dictionary describing
the creation of the residual. It will be convereted to an OrderedDict
before residuals are added so as to preserve residual order.
"""
new_residuals = OrderedDict(params)
self._validate_residuals(new_residuals)
for key in new_residuals:
self._residuals[key] = new_residuals[key]
self._distinguish_metric_from_resid()
new_metrics = {}
for name, info in self._metrics.items():
if name not in self._data_metric._metrics:
new_metrics[name] = info
if len(new_metrics) > 0:
self._data_metric.add_from_dict(new_metrics)
self._model_metric.add_from_dict(new_metrics)
def calculate(self):
"""Calculate residual values.
Calculated residual values are stored in the attribute
``Residual.values``.
"""
self._values = OrderedDict()
self._model_metric.calculate()
self._data_metric.calculate()
for key in self._residuals.keys():
info = deepcopy(self._residuals[key])
_func = info.pop("_func")
if key in self._metrics:
resid = (
self._model_metric._values[key]
- self._data_metric._values[key]
)
else:
function = residual_calcs.__dict__[_func]
resid = function(self._model_grid, self._data_grid, **info)
if _func != "discretized_misfit":
self._values[key] = resid
else:
# if
self._category = resid[0]
for label, value in resid[1].items():
self._values[label] = value
def write_residuals_to_file(self, path, style, decimals=3):
"""Write residuals to a file.
Parameters
----------
path :
style : str
yaml, dakota
decimals: int
Number of decimals to round output to.
Examples
--------
>>> import numpy as np
>>> from io import StringIO
>>> from landlab import RasterModelGrid
>>> from umami import Residual
>>> np.random.seed(42)
>>> model = RasterModelGrid((10, 10))
>>> z_model = model.add_zeros("node", "topographic__elevation")
>>> z_model += model.x_of_node + model.y_of_node
>>> data = RasterModelGrid((10, 10))
>>> z_data = data.add_zeros("node", "topographic__elevation")
>>> z_model += data.x_of_node + data.y_of_node
>>> z_data[data.core_nodes] += np.random.random(data.core_nodes.shape)
>>> file_like=StringIO('''
... me:
... _func: aggregate
... method: mean
... field: topographic__elevation
... ep10:
... _func: aggregate
... method: percentile
... field: topographic__elevation
... q: 10
... oid1_mean:
... _func: watershed_aggregation
... field: topographic__elevation
... method: mean
... outlet_id: 1
... sn1:
... _func: count_equal
... field: drainage_area
... value: 1
... ''')
>>> residual = Residual(model, data)
>>> residual.add_from_file(file_like)
>>> residual.calculate()
First we ouput in *dakota* style, in which each metric is listed on
its own line with its name as a comment.
>>> out = StringIO()
>>> residual.write_residuals_to_file(out, style="dakota", decimals=3)
>>> file_contents = out.getvalue().splitlines()
>>> for line in file_contents:
... print(line.strip())
17.533 me
9.909 ep10
9.813 oid1_mean
-41 sn1
Next we output in *yaml* style, in which each metric is serialized in
YAML format.
>>> out = StringIO()
>>> residual.write_residuals_to_file(out, style="yaml", decimals=3)
>>> file_contents = out.getvalue().splitlines()
>>> for line in file_contents:
... print(line.strip())
me: 17.533
ep10: 9.909
oid1_mean: 9.813
sn1: -41
"""
if style == "dakota":
stream = "\n".join(
[
str(np.round(val, decimals=decimals)) + " " + str(key)
for key, val in self._values.items()
]
)
if style == "yaml":
stream = "\n".join(
[
str(key) + ": " + str(np.round(val, decimals=decimals))
for key, val in self._values.items()
]
)
_write_output(path, stream)
@classmethod
def from_dict(cls, params):
"""Create an umami ``Residual`` from a dictionary.
Parameters
----------
params : dict or OrderedDict
This dict must contain a key *grid*, the values of which will be
passed to the `Landlab` function ``create_grid`` to create the
model grid. It will be convereted to an OrderedDict before residuals
are added so as to preserve residual order.
Examples
--------
>>> import numpy as np
>>> from io import StringIO
>>> from umami import Residual
>>> np.random.seed(42)
>>> params = {
... "model": {
... "RasterModelGrid": [
... [10, 10],
... {
... "fields": {
... "node": {
... "topographic__elevation": {
... "plane": [
... {"point": [0, 0, 0]},
... {"normal": [-1, -1, 1]},
... ]
... }
... }
... }
... },
... ]
... },
... "data": {
... "RasterModelGrid": [
... [10, 10],
... {
... "fields": {
... "node": {
... "topographic__elevation": {
... "plane": [
... {"point": [0, 0, 0]},
... {"normal": [-1, -1, 1]},
... ],
... "random" : [
... {"where": "CORE_NODE"},
... {"distribution": "standard_normal"},
... ]
... }
... }
... }
... },
... ]
... },
... "residuals": {
... "me": {
... "_func": "aggregate",
... "method": "mean",
... "field": "topographic__elevation",
... },
... "ep10": {
... "_func": "aggregate",
... "method": "percentile",
... "field": "topographic__elevation",
... "q": 10,
... },
... "oid1_mean": {
... "_func": "watershed_aggregation",
... "field": "topographic__elevation",
... "method": "mean",
... "outlet_id": 1,
... },
... "sn1": {
... "_func": "count_equal",
... "field": "drainage_area",
... "value": 1,
... },
... },
... }
>>> residual = Residual.from_dict(params)
>>> residual.names
['me', 'ep10', 'oid1_mean', 'sn1']
>>> residual.calculate()
>>> np.testing.assert_array_almost_equal(
... residual.value("me"),
... 0.158,
... decimal=3)
>>> np.testing.assert_array_almost_equal(
... residual.values,
... np.array([ 0.158, 0.67 , 4.138, -20. ]),
... decimal=3)
"""
model = create_grid(params.pop("model"))
data = create_grid(params.pop("data"))
return cls(model, data, **params)
@classmethod
def from_file(cls, file_like):
"""Create an umami ``Residual`` from a file-like object.
Parameters
----------
file_like : file path or StringIO
File will be parsed by ``yaml.safe_load`` and converted to an
``OrderedDict``.
Returns
-------
umami.Residual
Examples
--------
>>> import numpy as np
>>> from io import StringIO
>>> from umami import Residual
>>> np.random.seed(42)
>>> file_like=StringIO('''
... model:
... RasterModelGrid:
... - [10, 10]
... - fields:
... node:
... topographic__elevation:
... plane:
... - point: [0, 0, 0]
... - normal: [-1, -1, 1]
... data:
... RasterModelGrid:
... - [10, 10]
... - fields:
... node:
... topographic__elevation:
... plane:
... - point: [0, 0, 0]
... - normal: [-1, -1, 1]
... random:
... distribution: standard_normal
... where: CORE_NODE
... residuals:
... me:
... _func: aggregate
... method: mean
... field: topographic__elevation
... ep10:
... _func: aggregate
... method: percentile
... field: topographic__elevation
... q: 10
... oid1_mean:
... _func: watershed_aggregation
... field: topographic__elevation
... method: mean
... outlet_id: 1
... sn1:
... _func: count_equal
... field: drainage_area
... value: 1
... ''')
>>> residual = Residual.from_file(file_like)
>>> residual.names
['me', 'ep10', 'oid1_mean', 'sn1']
>>> residual.calculate()
>>> np.testing.assert_array_almost_equal(
... residual.value("me"),
... 0.191,
... decimal=3)
>>> np.testing.assert_array_almost_equal(
... residual.values,
... np.array([ 0.191, 0.426, 3.252, -20. ]),
... decimal=3)
"""
params = _read_input(file_like)
return cls.from_dict(params)
def _distinguish_metric_from_resid(self):
self._metrics = {}
for key, info in self._residuals.items():
func = info["_func"]
if func in metric_calcs.__dict__:
self._metrics[key] = info
def _validate_residuals(self, residuals):
""""""
for key in residuals:
info = residuals[key]
_validate_func(key, info, _VALID_FUNCS)
_validate_fields(self._data_grid, info)
_validate_fields(self._model_grid, info)