Skip to content
Open
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
2 changes: 1 addition & 1 deletion deeptab/models/_mixins/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ def _log_run_metadata_to_mlflow(
except Exception: # noqa: S110
pass
ckpt_dir = os.path.join(_run_dir, "checkpoints")
if os.path.isdir(ckpt_dir):
if obs.mlflow_log_model and os.path.isdir(ckpt_dir):
for ckpt in os.listdir(ckpt_dir):
try:
client.log_artifact(run_id, os.path.join(ckpt_dir, ckpt), artifact_path="checkpoints")
Expand Down
40 changes: 40 additions & 0 deletions tests/integration/test_mlflow_artifacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""The MLflow checkpoint upload follows ``ObservabilityConfig.mlflow_log_model``."""

import numpy as np
import pytest

from deeptab.core.observability import ObservabilityConfig
from deeptab.models import MLPRegressor


def _artifact_paths(client, run_id, path=None):
paths = []
for artifact in client.list_artifacts(run_id, path):
paths.extend(_artifact_paths(client, run_id, artifact.path) if artifact.is_dir else [artifact.path])
return paths


@pytest.mark.parametrize("log_model", [True, False])
def test_checkpoint_upload_follows_mlflow_log_model(log_model, tmp_path):
pytest.importorskip("mlflow")
from mlflow.tracking import MlflowClient

observability = ObservabilityConfig(
root_dir=str(tmp_path), experiment_trackers=["mlflow"], mlflow_log_model=log_model
)
features = np.random.default_rng(0).normal(size=(60, 3))
model = MLPRegressor(observability_config=observability, random_state=0)
model.fit(features, features[:, 0], max_epochs=1, accelerator="cpu", enable_progress_bar=False)

client = MlflowClient(observability.mlflow_tracking_uri)
experiment = client.get_experiment_by_name(observability.mlflow_experiment_name)
assert experiment is not None
(run,) = client.search_runs([experiment.experiment_id])
artifacts = _artifact_paths(client, run.info.run_id)
assert "summary.json" in artifacts

checkpoints = [path for path in artifacts if path.endswith(".ckpt")]
if log_model:
assert "checkpoints/best_model.ckpt" in checkpoints
else:
assert checkpoints == []
Loading