Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log_sklearn_plot: Support custom name. #339

Merged
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
3 changes: 2 additions & 1 deletion src/dvclive/data/sklearn_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class SKLearnPlot(Data):

@property
def output_path(self) -> Path:
_path = Path(f"{self.output_folder / self.name}.json")
_name = self.name.replace(".json", "")
_path = Path(f"{self.output_folder / _name}.json")
_path.parent.mkdir(exist_ok=True, parents=True)
return _path

Expand Down
7 changes: 4 additions & 3 deletions src/dvclive/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,14 @@ def log_image(self, name: str, val):
data.dump(val, self._step)
logger.debug(f"Logged {name}: {val}")

def log_sklearn_plot(self, name, labels, predictions, **kwargs):
def log_sklearn_plot(self, kind, labels, predictions, name=None, **kwargs):
val = (labels, predictions)

name = name or kind
if name in self._plots:
data = self._plots[name]
elif name in PLOTS and PLOTS[name].could_log(val):
data = PLOTS[name](name, self.plots_path)
elif kind in PLOTS and PLOTS[kind].could_log(val):
data = PLOTS[kind](name, self.plots_path)
self._plots[name] = data
else:
raise InvalidPlotTypeError(name)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_data/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,19 @@ def test_cleanup(tmp_dir, y_true_y_pred_y_score):
Live()

assert not (tmp_dir / live.plots_path / SKLearnPlot.subfolder).exists()


def test_custom_name(tmp_dir, y_true_y_pred_y_score):
live = Live()
out = tmp_dir / live.plots_path / SKLearnPlot.subfolder

y_true, y_pred, _ = y_true_y_pred_y_score

live.log_sklearn_plot("confusion_matrix", y_true, y_pred, name="train/cm")
live.log_sklearn_plot("confusion_matrix", y_true, y_pred, name="val/cm")
# ".json" should be stripped from the name
live.log_sklearn_plot("confusion_matrix", y_true, y_pred, name="cm.json")

assert (out / "train" / "cm.json").exists()
assert (out / "val" / "cm.json").exists()
assert (out / "cm.json").exists()