From 75e5ffaeab818b02e8815a4222fe43d795cce953 Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Thu, 6 Jun 2024 14:29:29 +0200 Subject: [PATCH 01/18] Revert "MNT space creator only after merge" This reverts commit 3803876655d76d84f7287358de3eb272e380ea9b. --- .github/workflows/deploy-model-card-creator.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/deploy-model-card-creator.yml b/.github/workflows/deploy-model-card-creator.yml index f4c94f21..258269e8 100644 --- a/.github/workflows/deploy-model-card-creator.yml +++ b/.github/workflows/deploy-model-card-creator.yml @@ -2,6 +2,7 @@ name: Deploy-Space-Creator on: - push + - pull_request concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} From b73d1fd52480e1914e9ebcff8cd78d3239f02267 Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Fri, 21 Jun 2024 18:06:48 +0200 Subject: [PATCH 02/18] MNT fix card tests --- skops/card/_model_card.py | 31 +++++++++++++++++++------------ skops/card/tests/test_card.py | 32 ++++++++++++++++---------------- skops/io/_numpy.py | 6 +++++- skops/io/_quantile_forest.py | 3 +++ skops/io/_trusted_types.py | 16 ++++++++++++---- 5 files changed, 55 insertions(+), 33 deletions(-) diff --git a/skops/card/_model_card.py b/skops/card/_model_card.py index 31633985..61e09f0f 100644 --- a/skops/card/_model_card.py +++ b/skops/card/_model_card.py @@ -29,11 +29,6 @@ else: from typing_extensions import Self -# Repr attributes can be used to control the behavior of repr -aRepr = Repr() -aRepr.maxother = 79 -aRepr.maxstring = 79 - VALID_TEMPLATES = {item.value for item in Templates} NEED_SECTION_ERR_MSG = ( "You are trying to {action} but you're using a custom template, please pass the " @@ -1316,6 +1311,11 @@ def _add_metrics( def _generate_metadata(self, metadata: ModelCardData) -> Iterator[str]: """Yield metadata in yaml format""" + # Repr attributes can be used to control the behavior of repr + aRepr = Repr() + aRepr.maxother = 79 + aRepr.maxstring = 79 + for key, val in metadata.to_dict().items() if metadata else {}: yield aRepr.repr(f"metadata.{key}={val},").strip('"').strip("'") @@ -1367,11 +1367,18 @@ def _iterate_content( yield from self._iterate_content(val.subsections, parent_section=title) @staticmethod - def _format_repr(text: str) -> str: + def _format_repr(title: str, content: str) -> str: # Remove new lines, multiple spaces, quotation marks, and cap line length - text = text.replace("\n", " ") - text = re.sub(r"\s+", r" ", text) - return aRepr.repr(text).strip('"').strip("'") + content = content.replace("\n", " ") + content = re.sub(r"\s+", r" ", content) + + # Repr attributes can be used to control the behavior of repr + aRepr = Repr() + aRepr.maxother = max(3, 79 - len(title)) + aRepr.maxstring = max(3, 79 - len(title)) + + content = aRepr.repr(content).strip('"').strip("'") + return f"{title}={content}," def __str__(self) -> str: return self.__repr__() @@ -1380,7 +1387,7 @@ def __repr__(self) -> str: # repr for the model model = getattr(self, "model", None) if model: - model_repr = self._format_repr(f"model={repr(self.get_model())},") + model_repr = self._format_repr("model", repr(self.get_model())) else: model_repr = None @@ -1391,7 +1398,7 @@ def __repr__(self) -> str: metadata_reprs.append("metadata.widget=[{...}],") continue - metadata_reprs.append(self._format_repr(f"metadata.{key}={val},")) + metadata_reprs.append(self._format_repr(f"metadata.{key}", repr(val))) metadata_repr = "\n".join(metadata_reprs) # repr for contents @@ -1403,7 +1410,7 @@ def __repr__(self) -> str: if content.rstrip("`").rstrip().endswith(CONTENT_PLACEHOLDER): # if content is just some default text, no need to show it continue - content_reprs.append(self._format_repr(f"{title}={section},")) + content_reprs.append(self._format_repr(title, repr(section))) content_repr = "\n".join(content_reprs) # combine all parts diff --git a/skops/card/tests/test_card.py b/skops/card/tests/test_card.py index 9af02530..750bebb4 100644 --- a/skops/card/tests/test_card.py +++ b/skops/card/tests/test_card.py @@ -47,6 +47,14 @@ def save_model_to_file(model_instance, suffix): return save_file_handle, save_file +def reprs_equal(repr1, repr2): + """Check that repr1 and repr2 are basically equal. + + This ignores line order of what comes after the first and before the last line. + """ + return sorted(repr1.split("\n")[1:-1]) == sorted(repr2.split("\n")[1:-1]) + + @pytest.mark.parametrize("suffix", [".pkl", ".pickle", ".skops"]) def test_load_model(suffix): model0 = LinearRegression(n_jobs=123) @@ -1294,14 +1302,14 @@ def expected_lines(self): Card( model=LinearRegression(fit_intercept=False), Model description/Training Procedure/Hyperparameters=TableSection(4x2), - Model description/Training Procedure/..., + Model description/Training Procedure/Model Plot=