Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies = [
"pyyaml (>=6.0.2,<7.0.0)",
"flatdict (==4.0.0)",
"pytest (>=9.0.3,<10.0.0)",
"unyt (>=3.1.0,<4.0.0)",
]

[project.urls]
Expand Down
47 changes: 47 additions & 0 deletions simvue/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import humanfriendly
import datetime
import os
from unyt import unyt_quantity
from unyt.exceptions import UnitParseError

import pydantic
import re
Expand Down Expand Up @@ -183,6 +185,8 @@ def __init__(
self._executor = Executor(self)
self._dispatcher: DispatcherBaseClass | None = None

self._meta_cache: dict[str, typing.Any] = {}

self._folder: Folder | None = None
self._term_color: bool = True
self._grids: dict[str, str] = {}
Expand Down Expand Up @@ -1680,6 +1684,12 @@ def log_metrics(
)
```
"""

# If there are any metric units to be uploaded do so now
if _units := self._meta_cache.get("metrics"):
self.update_metadata({"simvue": {"metrics": _units}})
del self._meta_cache["metrics"]

# TODO: When metrics and grids are combined into a single entity
# this can be removed. For now need to separate tensor based metrics
# from regular
Expand Down Expand Up @@ -2586,3 +2596,40 @@ def log_alert(
_alert.commit()

return True

@prettify_pydantic
@pydantic.validate_call
def set_metric_units(
self,
metric_name: MetricKeyString,
*,
units: str,
mks_unit: str | None = None,
mks_conversion: float | None = None,
) -> None:
"""Define units for metrics.

Parameters
----------
metric_name : str
name of metric to assign units to
units : str
unit symbol
label : str | None, optional
alternative longer name for units
"""
self._meta_cache.setdefault("metrics", {})

try:
_unit_obj = unyt_quantity.from_string(units)
self._meta_cache["metrics"][metric_name] = {
"units": units,
"mks_conversion": mks_conversion or float(_unit_obj.in_mks().value),
"mks_units": mks_unit or f"{_unit_obj.in_mks().units}",
}
except UnitParseError:
self._meta_cache["metrics"][metric_name] = {
"units": units,
"mks_conversion": mks_conversion,
"mks_units": mks_unit,
}
15 changes: 15 additions & 0 deletions tests/functional/test_run_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -1724,3 +1724,18 @@ def test_no_alert_dupes_different_run(alert_type: typing.Literal["user", "events
else:
MetricsRangeAlert(identifier=created_id).delete()


@pytest.mark.run
@pytest.mark.online
def test_set_metric_units(create_plain_run: tuple[sv_run.Run, dict]) -> None:
run, _ = create_plain_run
run.set_metric_units("x", units="ft")
run.set_metric_units("y", units="m")
run.set_metric_units("z", units="Foobars", mks_conversion=3.542, mks_unit="m")
run.log_metrics({"x": 10, "y": 3, "z": 22})

_metadata = RunObject(run.id).metadata
assert (_metric_data := _metadata.get("simvue", {}).get("metrics"))
assert _metric_data["x"] == {"units": "ft", "mks_units": "m", "mks_conversion": 0.3048}
assert _metric_data["y"] == {"units": "m", "mks_units": "m", "mks_conversion": 1}
assert _metric_data["z"] == {"units": "Foobars", "mks_units": "m", "mks_conversion": 3.542}
Loading