Skip to content

Commit

Permalink
log_metric: Make timestamp optional. (#352)
Browse files Browse the repository at this point in the history
Default to not include timestamp.
Closes #233
  • Loading branch information
daavoo committed Nov 4, 2022
1 parent 933d5b8 commit 2bb6727
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
6 changes: 4 additions & 2 deletions src/dvclive/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ def set_step(self, step: int) -> None:
def next_step(self):
self.set_step(self.get_step() + 1)

def log_metric(self, name: str, val: Union[int, float]):
def log_metric(
self, name: str, val: Union[int, float], timestamp: bool = False
):
if not Metric.could_log(val):
raise InvalidDataTypeError(name, type(val))

Expand All @@ -164,7 +166,7 @@ def log_metric(self, name: str, val: Union[int, float]):
self._metrics[name] = data

data.step = self.get_step()
data.dump(val)
data.dump(val, timestamp=timestamp)

self.summary = nested_update(self.summary, data.to_summary(val))
self.make_summary()
Expand Down
5 changes: 2 additions & 3 deletions src/dvclive/plots/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import abc
from pathlib import Path
from typing import Optional

from dvclive.error import DataAlreadyLoggedError

Expand All @@ -9,10 +8,10 @@ class Data(abc.ABC):
def __init__(self, name: str, output_folder: str) -> None:
self.name = name
self.output_folder: Path = Path(output_folder) / self.subfolder
self._step: Optional[int] = None
self._step: int = -1

@property
def step(self) -> Optional[int]:
def step(self) -> int:
return self._step

@step.setter
Expand Down
13 changes: 7 additions & 6 deletions src/dvclive/plots/metric.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import csv
import os
import time
from collections import OrderedDict
from pathlib import Path

from dvclive.utils import nested_set
Expand Down Expand Up @@ -30,18 +29,20 @@ def output_path(self) -> Path:
return _path

def dump(self, val, **kwargs) -> None:
ts = int(time.time() * 1000)
d = OrderedDict(
[("timestamp", ts), ("step", self.step), (self.name, val)]
)
row = {}
if kwargs.get("timestamp", False):
row["timestamp"] = int(time.time() * 1000)
row["step"] = self.step
row[self.name] = val

existed = self.output_path.exists()
with open(self.output_path, "a", encoding="utf-8", newline="") as fobj:
writer = csv.DictWriter(
fobj, d.keys(), delimiter="\t", lineterminator="\n"
)
if not existed:
writer.writeheader()
writer.writerow(d)
writer.writerow(row)

def to_summary(self, val):
d = {}
Expand Down
11 changes: 11 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,14 @@ def test_make_summary_without_calling_log(tmp_dir):
}
log_file = tmp_dir / dvclive.plots_dir / Metric.subfolder / "foo.tsv"
assert not log_file.exists()


@pytest.mark.parametrize("timestamp", (True, False))
def test_log_metric_timestamp(timestamp):
live = Live()
live.log_metric("foo", 1.0, timestamp=timestamp)
live.next_step()

history, _ = parse_metrics(live)
logged = next(iter(history.values()))
assert ("timestamp" in logged[0]) == timestamp
2 changes: 0 additions & 2 deletions tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ def test_get_renderers(tmp_dir, mocker):
"foo/bar": "0",
"rev": "workspace",
"step": "0",
"timestamp": mocker.ANY,
},
{
"foo/bar": "1",
"rev": "workspace",
"step": "1",
"timestamp": mocker.ANY,
},
]
assert scalar_renderers[0].properties["y"] == "foo/bar"
Expand Down
16 changes: 4 additions & 12 deletions tests/test_studio.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ def test_post_to_studio(tmp_dir, mocker, monkeypatch):
"rev": mocker.ANY,
"step": 0,
"metrics": {live.metrics_file: {"data": {"step": 0, "foo": 1}}},
"plots": {
scalar_path: {
"data": [{"timestamp": mocker.ANY, "step": 0, "foo": 1.0}]
}
},
"plots": {scalar_path: {"data": [{"step": 0, "foo": 1.0}]}},
"client": "dvclive",
},
headers={
Expand All @@ -73,11 +69,7 @@ def test_post_to_studio(tmp_dir, mocker, monkeypatch):
"rev": mocker.ANY,
"step": 1,
"metrics": {live.metrics_file: {"data": {"step": 1, "foo": 2}}},
"plots": {
scalar_path: {
"data": [{"timestamp": mocker.ANY, "step": 1, "foo": 2.0}]
}
},
"plots": {scalar_path: {"data": [{"step": 1, "foo": 2.0}]}},
"client": "dvclive",
},
headers={
Expand Down Expand Up @@ -139,8 +131,8 @@ def test_post_to_studio_failed_data_request(tmp_dir, mocker, monkeypatch):
"plots": {
scalar_path: {
"data": [
{"timestamp": mocker.ANY, "step": 0, "foo": 1.0},
{"timestamp": mocker.ANY, "step": 1, "foo": 2.0},
{"step": 0, "foo": 1.0},
{"step": 1, "foo": 2.0},
]
}
},
Expand Down

0 comments on commit 2bb6727

Please sign in to comment.