From 5da0de8ec06520a23b1a04f7fa8e8194f1f30b67 Mon Sep 17 00:00:00 2001 From: Thomas Lazarus Date: Mon, 27 Feb 2023 06:23:49 -0600 Subject: [PATCH 01/10] Adds create_toc() to Card --- skops/card/_model_card.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/skops/card/_model_card.py b/skops/card/_model_card.py index d5e0e55c..3a33fd23 100644 --- a/skops/card/_model_card.py +++ b/skops/card/_model_card.py @@ -1308,3 +1308,36 @@ def render(self) -> str: sections inserted. """ return "\n".join(self._generate_card()) + + def _iterate_key_section_content( + self, + data: dict[str, Section], + level: int = 0, + ): + for key, val in data.items(): + if not getattr(val, "visible", True): + continue + + title = val.title + yield title, level + + if val.subsections: + yield from self._iterate_key_section_content( + val.subsections, + level=level + 1, + ) + + def create_toc(self) -> str: + """Create a table of contents for the model card. + + + Returns + ------- + toc : str + The table of contents for the model card formatted as a markdown string. + """ + sections = [] + for title, level in self._iterate_key_section_content(self._data): + sections.append(f"{' ' * level}- [{title}](#{title})") + + return "\n".join(sections) From 96f84af51123efbc6403e7164cd014cade1b8a8f Mon Sep 17 00:00:00 2001 From: Thomas Lazarus Date: Mon, 27 Feb 2023 06:35:00 -0600 Subject: [PATCH 02/10] Updates changelog --- docs/changes.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/changes.rst b/docs/changes.rst index 28fa9975..b151deed 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -14,6 +14,8 @@ v0.6 - Added tabular regression example. :pr: `254` by `Thomas Lazarus` - All public ``scipy.special`` ufuncs (Universal Functions) are trusted by default by :func:`.io.load`. :pr:`295` by :user:`Omar Arab Oghli `. +- Add :func:`.Card.create_toc` to create a table of contents for the model card in markdown format. + :pr:`305` by :user:`Thomas Lazarus `. v0.5 ---- From bffba13d23265a28b53cbea9b3b6321b600b8954 Mon Sep 17 00:00:00 2001 From: Thomas Lazarus Date: Tue, 28 Feb 2023 19:17:37 -0600 Subject: [PATCH 03/10] Adds Test --- skops/card/_model_card.py | 2 +- skops/card/tests/test_card.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/skops/card/_model_card.py b/skops/card/_model_card.py index 3a33fd23..148610f9 100644 --- a/skops/card/_model_card.py +++ b/skops/card/_model_card.py @@ -1338,6 +1338,6 @@ def create_toc(self) -> str: """ sections = [] for title, level in self._iterate_key_section_content(self._data): - sections.append(f"{' ' * level}- [{title}](#{title})") + sections.append(f"{' ' * level}- {title}") return "\n".join(sections) diff --git a/skops/card/tests/test_card.py b/skops/card/tests/test_card.py index 9ad94277..45c498e7 100644 --- a/skops/card/tests/test_card.py +++ b/skops/card/tests/test_card.py @@ -1593,3 +1593,26 @@ def test_visibility_with_card_save(self, card): "Jane Doe" ) assert loaded.strip() == expected + + +class TestCardTableOfContents: + @pytest.fixture + def card(self): + model = LinearRegression() + card = Card(model=model) + return card + + def test_toc(self, card): + card.add_model_plot() + card.add_hyperparams() + card.add_metrics(accuracy=0.1) + card.add_get_started_code() + toc = card.create_toc() + exptected_toc = ( + "- Model description\n - Intended uses & limitations\n - Training" + " Procedure\n - Hyperparameters\n - Model Plot\n - Evaluation" + " Results\n- How to Get Started with the Model\n- Model Card Authors\n-" + " Model Card Contact\n- Citation" + ) + + assert toc == exptected_toc From b8ffe4ef7e44cd7281c75c6fb18b7e61f5c7c973 Mon Sep 17 00:00:00 2001 From: Thomas Lazarus Date: Tue, 28 Feb 2023 19:19:37 -0600 Subject: [PATCH 04/10] Fixes line length --- docs/changes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/changes.rst b/docs/changes.rst index 680fb870..ce058549 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -14,8 +14,8 @@ v0.6 - Added tabular regression example. :pr: `254` by `Thomas Lazarus` - All public ``scipy.special`` ufuncs (Universal Functions) are trusted by default by :func:`.io.load`. :pr:`295` by :user:`Omar Arab Oghli `. -- Add :func:`.Card.create_toc` to create a table of contents for the model card in markdown format. - :pr:`305` by :user:`Thomas Lazarus `. +- Add :func:`.Card.create_toc` to create a table of contents for the model card in + markdown format. :pr:`305` by :user:`Thomas Lazarus `. - Add example of using model card without the skops template. :pr:`291` by `Benjamin Bossan`_. From c2602f0a440293cb2fd71c0a1c2679e367c71c05 Mon Sep 17 00:00:00 2001 From: Thomas Lazarus Date: Wed, 1 Mar 2023 21:02:17 -0600 Subject: [PATCH 05/10] Fixes from feedback --- skops/card/_model_card.py | 2 +- skops/card/tests/test_card.py | 218 ++++++++++++++++++---------------- 2 files changed, 115 insertions(+), 105 deletions(-) diff --git a/skops/card/_model_card.py b/skops/card/_model_card.py index e9356681..a68eb9d2 100644 --- a/skops/card/_model_card.py +++ b/skops/card/_model_card.py @@ -1321,6 +1321,7 @@ def _iterate_key_section_content( data: dict[str, Section], level: int = 0, ): + """Iterate through the key sections and yield the title and level.""" for key, val in data.items(): if not getattr(val, "visible", True): continue @@ -1337,7 +1338,6 @@ def _iterate_key_section_content( def create_toc(self) -> str: """Create a table of contents for the model card. - Returns ------- toc : str diff --git a/skops/card/tests/test_card.py b/skops/card/tests/test_card.py index 45c498e7..fee207ba 100644 --- a/skops/card/tests/test_card.py +++ b/skops/card/tests/test_card.py @@ -153,7 +153,7 @@ def test_default(self, model_card): "Model description/Training Procedure/Model Plot" ).content # don't compare whole text, as it's quite long and non-deterministic - assert result.startswith("The model plot is below.\n\n