Skip to content

Commit

Permalink
log upon errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wd60622 committed Aug 13, 2024
1 parent 2901248 commit 39b9f9e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
13 changes: 11 additions & 2 deletions pymc_marketing/mlflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"""

import json
import logging
import os
from functools import wraps
from pathlib import Path
Expand Down Expand Up @@ -214,12 +215,20 @@ def log_model_graph(model: Model, path: str | Path) -> None:
"""
try:
graph = pm.model_to_graphviz(model)
except ImportError:
except ImportError as e:
msg = (

Check warning on line 219 in pymc_marketing/mlflow.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/mlflow.py#L216-L219

Added lines #L216 - L219 were not covered by tests
"Unable to render the model graph. Please install the graphviz package. "
f"{e}"
)
logging.info(msg)

Check warning on line 223 in pymc_marketing/mlflow.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/mlflow.py#L223

Added line #L223 was not covered by tests

return None

Check warning on line 225 in pymc_marketing/mlflow.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/mlflow.py#L225

Added line #L225 was not covered by tests

try:
saved_path = graph.render(path)
except Exception:
except Exception as e:
msg = f"Unable to render the model graph. {e}"
logging.info(msg)
return None

Check warning on line 232 in pymc_marketing/mlflow.py

View check run for this annotation

Codecov / codecov/patch

pymc_marketing/mlflow.py#L227-L232

Added lines #L227 - L232 were not covered by tests
else:
mlflow.log_artifact(saved_path)
Expand Down
27 changes: 22 additions & 5 deletions tests/test_mlflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging

import arviz as az
import mlflow
Expand Down Expand Up @@ -137,19 +138,35 @@ def test_multi_likelihood_type(multi_likelihood_model) -> None:


@pytest.mark.parametrize(
"to_patch, side_effect",
"to_patch, side_effect, expected_info_message",
[
("pymc.model_to_graphviz", ImportError("No module named 'graphviz'")),
("graphviz.graphs.Digraph.render", Exception("Unknown error occurred")),
(
"pymc.model_to_graphviz",
ImportError("No module named 'graphviz'"),
"Unable to render the model graph. Please install the graphviz package. No module named 'graphviz'",
),
(
"graphviz.graphs.Digraph.render",
Exception("Unknown error occurred"),
"Unable to render the model graph. Unknown error occurred",
),
],
ids=["no_graphviz", "render_error"],
)
def test_log_model_graph_no_graphviz(mocker, model, to_patch, side_effect) -> None:
def test_log_model_graph_no_graphviz(
caplog, mocker, model, to_patch, side_effect, expected_info_message
) -> None:
mocker.patch(
to_patch,
side_effect=side_effect,
)
with mlflow.start_run() as run:
log_model_graph(model, "model_graph")
with caplog.at_level(logging.INFO):
log_model_graph(model, "model_graph")

assert caplog.messages == [
expected_info_message,
]

run_id = run.info.run_id
artifacts = get_run_data(run_id)[-1]
Expand Down

0 comments on commit 39b9f9e

Please sign in to comment.