/
metric.py
executable file
·395 lines (355 loc) · 12.5 KB
/
metric.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
"""The ``umami.Metric`` class calculates metrics on a Landlab model grid."""
from collections import OrderedDict
from copy import deepcopy
import numpy as np
import yaml
from landlab import RasterModelGrid, create_grid
import umami.calculations.metric as calcs
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 = calcs.__dict__
class Metric(object):
"""Create a ``Metric`` class based on a Landlab model grid."""
_required_fields = ["topographic__elevation"]
def __init__(
self,
grid,
flow_accumulator_kwds=None,
chi_finder_kwds=None,
metrics=None,
):
"""
Parameters
----------
grid : 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. `
metrics : dict
A dictionary of desired metrics to calculate. See examples for
required format.
Examples
--------
>>> from io import StringIO
>>> from landlab import RasterModelGrid
>>> from umami import Metric
>>> grid = RasterModelGrid((10, 10))
>>> z = grid.add_zeros("node", "topographic__elevation")
>>> z += grid.x_of_node + grid.y_of_node
>>> 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
... ''')
>>> metric = Metric(grid)
>>> metric.add_from_file(file_like)
>>> metric.names
['me', 'ep10', 'oid1_mean', 'sn1']
>>> metric.calculate()
>>> metric.value('me')
9.0
>>> metric.values
[9.0, 5.0, 5.0, 8]
"""
# verify that apppropriate fields are present.
for field in self._required_fields:
if field not in grid.at_node:
msg = "umami: Required field: {field} is missing.".format(
field=field
)
raise ValueError(msg)
# save a reference to the grid.
self._grid = grid
# run FlowAccumulator and ChiFinder
self._fa, self._cf = _create_landlab_components(
self._grid,
chi_finder_kwds=chi_finder_kwds,
flow_accumulator_kwds=flow_accumulator_kwds,
)
# determine which metrics are desired.
self._metrics = OrderedDict(metrics or {})
self._validate_metrics(self._metrics)
@property
def names(self):
"""Names of metrics in metric order."""
self._names = [key for key in self._metrics]
return self._names
def value(self, name):
"""Get a specific metric value.
Parameters
----------
name: str
Name of desired metric.
"""
return self._values[name]
@property
def values(self):
"""Metric values in metric order."""
return [self._values[key] for key in self._metrics.keys()]
def add_from_file(self, file):
"""Add metrics to an ``umami.Metric`` 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 metrics to an ``umami.Metric`` from a dictionary.
Adding metrics through this method does not overwrite already existing
metrics. New metrics are appended to the existing metric list.
Parameters
----------
params : dict or OrderedDict
Keys are metric names and values are a dictionary describing
the creation of the metric. It will be convereted to an OrderedDict
before metrics are added so as to preserve metric order.
"""
new_metrics = OrderedDict(params)
self._validate_metrics(new_metrics)
for key in new_metrics:
self._metrics[key] = new_metrics[key]
def calculate(self):
"""Calculate metric values.
Calculated metric values are stored in the attribute
``Metric.values``.
"""
self._values = OrderedDict()
for key in self._metrics.keys():
info = deepcopy(self._metrics[key])
_func = info.pop("_func")
function = calcs.__dict__[_func]
if _func in ("chi_gradient", "chi_intercept"):
self._values[key] = function(self._cf)
else:
self._values[key] = function(self._grid, **info)
def write_metrics_to_file(self, path, style, decimals=3):
"""Write metrics to a file.
Parameters
----------
path :
style : str
yaml, dakota
decimals: int
Number of decimals to round output to.
Examples
--------
>>> from io import StringIO
>>> from landlab import RasterModelGrid
>>> from umami import Metric
>>> grid = RasterModelGrid((10, 10))
>>> z = grid.add_zeros("node", "topographic__elevation")
>>> z += grid.x_of_node + grid.y_of_node
>>> 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
... ''')
First we ouput in *dakota* style, in which each metric is listed on
its own line with its name as a comment.
>>> metric = Metric(grid)
>>> metric.add_from_file(file_like)
>>> metric.calculate()
>>> out = StringIO()
>>> metric.write_metrics_to_file(out, style="dakota")
>>> file_contents = out.getvalue().splitlines()
>>> for line in file_contents:
... print(line.strip())
9.0 me
5.0 ep10
5.0 oid1_mean
8 sn1
Next we output in *yaml* style, in which each metric is serialized in
YAML format.
>>> out = StringIO()
>>> metric.write_metrics_to_file(out, style="yaml")
>>> file_contents = out.getvalue().splitlines()
>>> for line in file_contents:
... print(line.strip())
me: 9.0
ep10: 5.0
oid1_mean: 5.0
sn1: 8
"""
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 ``Metric`` 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 metrics
are added so as to preserve metric order.
Examples
--------
>>> from io import StringIO
>>> from umami import Metric
>>> params = {
... "grid": {
... "RasterModelGrid": [
... [10, 10],
... {
... "fields": {
... "node": {
... "topographic__elevation": {
... "plane": [
... {"point": [0, 0, 0]},
... {"normal": [-1, -1, 1]},
... ]
... }
... }
... }
... },
... ]
... },
... "metrics": {
... "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,
... },
... },
... }
>>> metric = Metric.from_dict(params)
>>> metric.names
['me', 'ep10', 'oid1_mean', 'sn1']
>>> metric.calculate()
>>> metric.value('me')
9.0
>>> metric.values
[9.0, 5.0, 5.0, 8]
"""
# create grid
grid = create_grid(params.pop("grid"))
return cls(grid, **params)
@classmethod
def from_file(cls, file_like):
"""Create an umami ``Metric`` 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.Metric
Examples
--------
>>> from io import StringIO
>>> from umami import Metric
>>> file_like=StringIO('''
... grid:
... RasterModelGrid:
... - [10, 10]
... - fields:
... node:
... topographic__elevation:
... plane:
... - point: [0, 0, 0]
... - normal: [-1, -1, 1]
... metrics:
... 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
... ''')
>>> metric = Metric.from_file(file_like)
>>> metric.names
['me', 'ep10', 'oid1_mean', 'sn1']
>>> metric.calculate()
>>> metric.value('me')
9.0
>>> metric.values
[9.0, 5.0, 5.0, 8]
"""
params = _read_input(file_like)
return cls.from_dict(params)
def _validate_metrics(self, metrics):
# look at all _funcs, ensure that they are valid
for key in metrics:
info = metrics[key]
_validate_func(key, info, _VALID_FUNCS)
_validate_fields(self._grid, info)