-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrids.py
More file actions
385 lines (329 loc) · 11.4 KB
/
grids.py
File metadata and controls
385 lines (329 loc) · 11.4 KB
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
"""
Simvue Server Grid
==================
Contains a class for remotely connecting to a Simvue grid, or defining
a new grid given relevant arguments.
"""
import http
import numpy
import typing
import pydantic
from simvue.api.url import URL
from simvue.models import GridMetricSet
from .base import SimvueObject, write_only
from simvue.api.request import (
get as sv_get,
put as sv_put,
post as sv_post,
get_json_from_response,
)
try:
from typing import Self
except ImportError:
from typing_extensions import Self
__all__ = ["Grid"]
def check_ordered_array(
axis_ticks: list[list[float]] | numpy.ndarray,
) -> list[list[float]]:
"""Returns if array is ordered or reverse ordered."""
if isinstance(axis_ticks, numpy.ndarray):
axis_ticks = axis_ticks.tolist()
for i, _array in enumerate(axis_ticks):
_array = numpy.array(_array)
if not numpy.all(numpy.sort(_array) == _array) or numpy.all(
reversed(numpy.sort(_array)) == _array
):
raise ValueError(f"Axis {i} has unordered values.")
return axis_ticks
class Grid(SimvueObject):
"""Class for retrieving grids stored on the server."""
@pydantic.validate_call
@write_only
def attach_metric_for_run(self, run_id: str, metric_name: str) -> None:
"""Associates a metric for a given run to this grid."""
if self._offline:
self._staging.setdefault("runs", [])
self._staging["runs"].append((run_id, metric_name))
super().commit()
return
_response = sv_put(
url=f"{self.run_data_url(run_id)}",
headers=self._headers,
json={"metric": metric_name},
)
return get_json_from_response(
expected_status=[http.HTTPStatus.OK],
scenario=(
f"Adding '{metric_name}' to grid "
f"'{self._identifier}' to run '{run_id}'",
),
response=_response,
)
def on_reconnect(self, id_mapping: dict[str, str]) -> None:
"""Operations performed when this grid is switched from offline to online mode.
Parameters
----------
id_mapping : dict[str, str]
mapping from offline identifier to new online identifier.
"""
_online_runs = (
(id_mapping[run_id], metric_name)
for run_id, metric_name in self._staging.pop("runs", [])
)
super().commit()
for run_id, metric_name in _online_runs:
try:
self.attach_metric_for_run(run_id=run_id, metric_name=metric_name)
except KeyError:
raise RuntimeError("Failed to retrieve online run identifier.")
@property
def grid(self) -> list[list[float]]:
return self._get_attribute("grid")
@property
def name(self) -> str:
return self._get_attribute("name")
@classmethod
@pydantic.validate_call(config={"arbitrary_types_allowed": True})
def new(
cls,
*,
name: str,
grid: typing.Annotated[
list[list[float]],
pydantic.conlist(
pydantic.conlist(float, min_length=1), min_length=1, max_length=2
),
pydantic.AfterValidator(check_ordered_array),
],
labels: list[str],
offline: bool = False,
**kwargs,
) -> Self:
"""Create a new Grid on the Simvue server.
Parameters
----------
name : str
name for this grid.
grid : list[list[float]]
define a grid as a list of axes containing tick values
number of axes must be 1 or 2
labels : list[str]
label each of the axes defined.
offline: bool, optional
whether to create in offline mode, default is False.
Returns
-------
Metrics
metrics object
"""
if len(labels) != len(grid):
raise AssertionError(
"Length of argument 'labels' must match first "
f"grid dimension {len(grid)}."
)
return Grid(
grid=grid,
labels=labels,
name=name,
_read_only=False,
_offline=offline,
**kwargs,
)
@property
def dimensions(self) -> tuple[int, int]:
"""Returns the grid dimensions."""
return len(self.grid)
def run_data_url(self, run_id: str) -> URL:
"""Returns the URL for grid data for a specific run."""
return URL(
f"{self._user_config.server.url}/runs/{run_id}/grids/{self._identifier}"
)
def run_metric_url(self, run_id: str, metric_name: str) -> URL:
"""Returns the URL for the values for a given run metric."""
return URL(
f"{self._user_config.server.url}/runs/{run_id}/metrics/{metric_name}/"
)
@pydantic.validate_call
def get_run_metric_values(
self, *, run_id: str, metric_name: str, step: int
) -> dict:
"""Retrieve values for this grid from the server for a given run at a given step.
Parameters
----------
run_id : str
run to return grid metrics for.
metric_name : str
name of metric to return values for
step : int
time step to retrieve values for.
Returns
------
dict[str, list[dict[str, float]]
dictionary containing values from this for the run at specified step.
"""
_response = sv_get(
url=f"{self.run_metric_values_url(run_id, metric_name) / 'values'}",
headers=self._headers,
params={"step": step},
)
return get_json_from_response(
response=_response,
expected_status=[http.HTTPStatus.OK],
expected_type=dict,
scenario=(
f"Retrieving '{metric_name}' grid values "
f"for run '{self._run_id}' at step {step}",
),
)
@pydantic.validate_call
def get_run_metric_span(self, *, run_id: str, metric_name: str) -> dict:
"""Retrieve span for this grid from the server for a given run.
Parameters
----------
run_id : str
run to return grid metrics for.
metric_name : str
metric to retrieve span information for.
Returns
------
dict[str, list[dict[str, float]]
dictionary containing span from this for the run at specified step.
"""
_response = sv_get(
url=f"{self.run_metric_values_url(run_id, metric_name) / 'span'}",
headers=self._headers,
)
return get_json_from_response(
response=_response,
expected_status=[http.HTTPStatus.OK],
expected_type=dict,
scenario=f"Retrieving grid span for run '{run_id}'",
)
@classmethod
def get(
cls,
*_,
**__,
) -> typing.Generator[tuple[str, Self | None], None, None]:
raise NotImplementedError
class GridMetrics(SimvueObject):
def __init__(
self,
_read_only: bool = True,
_local: bool = False,
**kwargs,
) -> None:
"""Initialise a GridMetrics object instance."""
self._label = "grid_metric"
super().__init__(_read_only=_read_only, _local=_local, **kwargs)
self._run_id = self._staging.get("run")
self._is_set = True
@staticmethod
def run_grids_endpoint(run: str | None = None) -> URL:
"""Returns the URL for grids for a specific run."""
return URL(f"runs/{run}/metrics/")
def _get_attribute(self, attribute: str, *default) -> typing.Any:
return super()._get_attribute(
attribute,
*default,
url=f"{self._user_config.server.url}/{self.run_grids_endpoint(self._run_id)}",
)
@classmethod
@pydantic.validate_call
def new(
cls, *, run: str, data: list[GridMetricSet], offline: bool = False, **kwargs
) -> Self:
"""Create a new GridMetrics object for n-dimensional metric submission.
Parameters
----------
run: str
identifier for the run to attach metrics to.
data: list[GridMetricSet]
set of tensor-based metrics to attach to run.
offline: bool, optional
whether to create in offline mode, default is False.
Returns
-------
Metrics
metrics object
"""
return GridMetrics(
run=run,
data=[metric.model_dump() for metric in data],
_read_only=False,
_offline=offline,
)
@classmethod
@pydantic.validate_call
def get(
cls,
*,
runs: list[str],
metrics: list[str],
step: pydantic.NonNegativeInt,
spans: bool = False,
**kwargs,
) -> typing.Generator[dict[str, dict[str, list[dict[str, float]]]], None, None]:
"""Retrieve tensor-metrics from the server for a given set of runs.
Parameters
----------
runs : list[str]
list of runs to return metric values for.
metrics : list[str]
list of metrics to retrieve.
step : int
the timestep to retrieve grid metrics for
spans : bool, optional
return spans informations
Yields
------
dict[str, dict[str, list[dict[str, float]]]
metric set object containing metrics for run.
"""
for metric in metrics:
for run in runs:
yield from cls._get_all_objects(
endpoint=f"{cls.run_grids_endpoint(run)}/{metric}/values",
step=step,
offset=None,
count=None,
)
def commit(self) -> dict | None:
if not (_run_staging := self._staging.pop("data", None)):
return
return self._log_values(_run_staging)
def on_reconnect(self, id_mapping: dict[str, str]) -> None:
"""Operations performed when this grid metrics object is switched from offline to online mode.
Parameters
----------
id_mapping : dict[str, str]
mapping from offline identifier to new online identifier.
"""
metrics = self._staging.pop("data", [])
if not (run_id := id_mapping.get(self._run_id)):
raise RuntimeError("Failed to retrieve online run identifier.")
self._run_id = run_id
for metric in metrics:
if not (new_id := id_mapping.get(metric["grid"])):
raise RuntimeError("Failed to retrieve new online identifier for grid")
metric["grid"] = new_id
self._log_values(metrics)
@pydantic.validate_call
@write_only
def _log_values(self, metrics: list[GridMetricSet]) -> None:
if self._offline:
self._staging.setdefault("data", [])
self._staging["data"] += metrics
super().commit()
return
_response = sv_post(
url=f"{self._user_config.server.url}/{self.run_grids_endpoint(self._run_id)}",
headers=self._headers,
data=metrics,
params={},
)
return get_json_from_response(
expected_status=[http.HTTPStatus.OK],
scenario=f"adding tensor values to run '{self._run_id}'",
response=_response,
)