Skip to content

Commit

Permalink
live.log_metric: Cast nan and inf to string.
Browse files Browse the repository at this point in the history
- Support string metrics (forgot to support them when DVC added support)
- Don't recast to float when sending to Studio.

Closes iterative/studio-support#93
  • Loading branch information
daavoo committed Aug 17, 2023
1 parent 6b0f5cd commit bc1e94e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/dvclive/live.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import math
import os
import shutil
from pathlib import Path
Expand Down Expand Up @@ -295,13 +296,16 @@ def next_step(self):
def log_metric(
self,
name: str,
val: Union[int, float],
val: Union[int, float, str],
timestamp: bool = False,
plot: bool = True,
):
if not Metric.could_log(val):
raise InvalidDataTypeError(name, type(val))

if not isinstance(val, str) and (math.isnan(val) or math.isinf(val)):
val = str(val)

if name in self._metrics:
metric = self._metrics[name]
else:
Expand Down
2 changes: 1 addition & 1 deletion src/dvclive/plots/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Metric(Data):

@staticmethod
def could_log(val: object) -> bool:
if isinstance(val, (int, float)):
if isinstance(val, (int, float, str)):
return True
if (
val.__class__.__module__ == "numpy"
Expand Down
7 changes: 6 additions & 1 deletion src/dvclive/studio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ruff: noqa: SLF001
import base64
import math
import os
from pathlib import Path

Expand All @@ -21,7 +22,11 @@ def _cast_to_numbers(datapoints):
elif k == "timestamp":
continue
else:
datapoint[k] = float(v)
float_v = float(v)
if math.isnan(float_v) or math.isinf(float_v):
datapoint[k] = str(v)
else:
datapoint[k] = float_v
return datapoints


Expand Down
22 changes: 22 additions & 0 deletions tests/test_log_metric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import math

import numpy as np
import pytest

from dvclive import Live


@pytest.mark.parametrize(
("val"),
[math.inf, math.nan, np.nan, np.inf],
)
def test_log_metric_inf_nan(tmp_dir, val):
with Live() as live:
live.log_metric("metric", val)
assert live.summary["metric"] == str(val)


def test_log_metic_str(tmp_dir):
with Live() as live:
live.log_metric("metric", "foo")
assert live.summary["metric"] == "foo"

0 comments on commit bc1e94e

Please sign in to comment.