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

MNT Model card refactor to move plotting & table #163

Merged
merged 5 commits into from
Oct 5, 2022
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
21 changes: 11 additions & 10 deletions skops/card/_model_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,8 @@ def __init__(
metadata: Optional[CardData] = None,
) -> None:
self.model = model
self._hyperparameter_table = self._extract_estimator_config()
# the spaces in the pipeline breaks markdown, so we replace them
self.model_diagram = model_diagram
self._eval_results = {} # type: ignore
if model_diagram is True:
self._model_plot: str | None = re.sub(
r"\n\s+", "", str(estimator_html_repr(model))
)
else:
self._model_plot = None
self._template_sections: dict[str, str] = {}
self._extra_sections: list[tuple[str, Any]] = []
self.metadata = metadata or CardData()
Expand Down Expand Up @@ -393,6 +386,7 @@ def _generate_card(self) -> ModelCard:
# add evaluation results

template_sections = copy.deepcopy(self._template_sections)

if self.metadata:
if self.metadata.to_dict().get("model_file"):
model_file = self.metadata.to_dict().get("model_file")
Expand All @@ -415,11 +409,18 @@ def _generate_card(self) -> ModelCard:
" json.load(f)\n"
'clf.predict(pd.DataFrame.from_dict(config["sklearn"]["example_input"]))'
)
if self.model_diagram is True:
model_plot: str | None = re.sub(
r"\n\s+", "", str(estimator_html_repr(self.model))
)
else:
model_plot = None
template_sections["eval_results"] = tabulate(
list(self._eval_results.items()),
headers=["Metric", "Value"],
tablefmt="github",
)

# if template path is not given, use default
if template_sections.get("template_path") is None:
template_sections["template_path"] = str(
Expand All @@ -446,8 +447,8 @@ def _generate_card(self) -> ModelCard:

card = ModelCard.from_template(
card_data=self.metadata,
hyperparameter_table=self._hyperparameter_table,
model_plot=self._model_plot,
hyperparameter_table=self._extract_estimator_config(),
model_plot=model_plot,
**template_sections,
)
return card
Expand Down
6 changes: 6 additions & 0 deletions skops/card/tests/test_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ def test_metadata_keys(destination_path, model_card):
assert "tags: dummy" in model_card


def test_default_sections_save(model_card):
# test if the plot and hyperparameters are only added during save
assert "<style>" not in str(model_card)
assert "fit_intercept" not in str(model_card)


def test_add_metrics(destination_path, model_card):
model_card.add_metrics(**{"acc": 0.1})
model_card.add_metrics(f1=0.1)
Expand Down