Skip to content

Commit 7ae70ed

Browse files
ENH: Default section for Card.add_* methods (#321)
Co-authored-by: Adrin Jalali <adrin.jalali@gmail.com> ## Description The `add_*` methods for `Card` now have default sections (the one used in the skops template) and no longer set descriptions by default. `None` is no longer a valid argument for section. This is useful because it allows us to completely remove all the template checks in the diverse `add_*` methods. Before this change, many `add_*` methods had to check the template and if it was a custom template, would raise an error when the section is not indicated. All methods have been homogenized in that they no longer add a description by default and put a placeholder there if empty. This means that after this PR is merged, the same code will sometimes produce a different looking model card. ## Comment For the `Card` class, this is a nice simplification of the code. The main change affects the unit tests. All tests that used to raise when using a custom template without a section argument no longer raise but use the default template. Furthermore, since some default descriptions have been removed, other tests had to be touched too. Apart from that, some minor cleanups in the tests.
1 parent 7487af7 commit 7ae70ed

File tree

4 files changed

+139
-178
lines changed

4 files changed

+139
-178
lines changed

docs/changes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ v0.6
2222
`Benjamin Bossan`_.
2323
- Fix: skops persistence now also works with many functions from the
2424
:mod:`operator` module. :pr:`287` by `Benjamin Bossan`_.
25+
- ``add_*`` methods on :class:`.Card` now have default section names (but
26+
``None`` is no longer valid) and no longer add descriptions by default.
27+
:pr:`321` by `Benjamin Bossan`_.
2528

2629
v0.5
2730
----

skops/card/_model_card.py

Lines changed: 26 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ def _add_single(self, key: str, val: str | Section) -> Section:
791791

792792
def add_model_plot(
793793
self,
794-
section: str | None = None,
794+
section: str = "Model description/Training Procedure/Model Plot",
795795
description: str | None = None,
796796
) -> Self:
797797
"""Add a model plot
@@ -805,11 +805,10 @@ def add_model_plot(
805805
806806
Parameters
807807
----------
808-
section : str or None, default=None
809-
The section that the model plot should be added to. If you're using
810-
the default skops template, you can leave this parameter as
811-
``None``, otherwise you have to indicate the section. If the section
812-
does not exist, it will be created for you.
808+
section : str (default="Model description/Training Procedure/Model Plot")
809+
The section that the model plot should be added to. By default, the
810+
section is set to fit the skops model card template. If you're using
811+
a different template, you may have to choose a different section name.
813812
814813
description : str or None, default=None
815814
An optional description to be added before the model plot. If you're
@@ -821,18 +820,8 @@ def add_model_plot(
821820
-------
822821
self : object
823822
Card object.
824-
"""
825-
if section is None:
826-
if self.template == Templates.skops.value:
827-
section = "Model description/Training Procedure/Model Plot"
828-
else:
829-
msg = NEED_SECTION_ERR_MSG.format(action="add a model plot")
830-
raise ValueError(msg)
831-
832-
if description is None:
833-
if self.template == Templates.skops.value:
834-
description = "The model plot is below."
835823
824+
"""
836825
self._add_model_plot(
837826
self.get_model(), section_name=section, description=description
838827
)
@@ -864,17 +853,19 @@ def _add_model_plot(
864853
self._add_single(section_name, section)
865854

866855
def add_hyperparams(
867-
self, section: str | None = None, description: str | None = None
856+
self,
857+
section: str = "Model description/Training Procedure/Hyperparameters",
858+
description: str | None = None,
868859
) -> Self:
869860
"""Add the model's hyperparameters as a table
870861
871862
Parameters
872863
----------
873-
section : str or None, default=None
874-
The section that the hyperparamters should be added to. If you're
875-
using the default skops template, you can leave this parameter as
876-
``None``, otherwise you have to indicate the section. If the section
877-
does not exist, it will be created for you.
864+
section : str (default="Model description/Training Procedure/Hyperparameters")
865+
The section that the hyperparameters should be added to. By default,
866+
the section is set to fit the skops model card template. If you're
867+
using a different template, you may have to choose a different section
868+
name.
878869
879870
description : str or None, default=None
880871
An optional description to be added before the hyperparamters. If
@@ -888,17 +879,6 @@ def add_hyperparams(
888879
Card object.
889880
890881
"""
891-
if section is None:
892-
if self.template == Templates.skops.value:
893-
section = "Model description/Training Procedure/Hyperparameters"
894-
else:
895-
msg = NEED_SECTION_ERR_MSG.format(action="add model hyperparameters")
896-
raise ValueError(msg)
897-
898-
if description is None:
899-
if self.template == Templates.skops.value:
900-
description = "The model is trained with below hyperparameters."
901-
902882
self._add_hyperparams(
903883
self.get_model(), section_name=section, description=description
904884
)
@@ -924,7 +904,7 @@ def _add_hyperparams(
924904

925905
def add_get_started_code(
926906
self,
927-
section: str | None = None,
907+
section: str = "How to Get Started with the Model",
928908
description: str | None = None,
929909
file_name: str | None = None,
930910
model_format: Literal["pickle", "skops"] | None = None,
@@ -936,11 +916,11 @@ def add_get_started_code(
936916
937917
Parameters
938918
----------
939-
section : str or None, default=None
940-
The section that the code should be added to. If you're using the
941-
default skops template, you can leave this parameter as ``None``,
942-
otherwise you have to indicate the section. If the section does not
943-
exist, it will be created for you.
919+
section : str (default="How to Get Started with the Model")
920+
The section that the code for loading the model should be added to.
921+
By default, the section is set to fit the skops model card template.
922+
If you're using a different template, you may have to choose a
923+
different section name.
944924
945925
description : str or None, default=None
946926
An optional description to be added before the code. If you're using
@@ -984,17 +964,6 @@ def add_get_started_code(
984964
if (not file_name) or (not model_format):
985965
return self
986966

987-
if section is None:
988-
if self.template == Templates.skops.value:
989-
section = "How to Get Started with the Model"
990-
else:
991-
msg = NEED_SECTION_ERR_MSG.format(action="add get started code")
992-
raise ValueError(msg)
993-
994-
if description is None:
995-
if self.template == Templates.skops.value:
996-
description = "Use the code below to get started with the model."
997-
998967
self._add_get_started_code(
999968
section,
1000969
file_name=file_name,
@@ -1157,7 +1126,7 @@ def add_table(
11571126

11581127
def add_metrics(
11591128
self,
1160-
section: str | None = None,
1129+
section: str = "Model description/Evaluation Results",
11611130
description: str | None = None,
11621131
**kwargs: str | int | float,
11631132
) -> Self:
@@ -1167,11 +1136,10 @@ def add_metrics(
11671136
11681137
Parameters
11691138
----------
1170-
section : str or None, default=None
1171-
The section that the metrics should be added to. If you're using the
1172-
default skops template, you can leave this parameter as ``None``,
1173-
otherwise you have to indicate the section. If the section does not
1174-
exist, it will be created for you.
1139+
section : str (default="Model description/Evaluation Results")
1140+
The section that metrics should be added to. By default, the section
1141+
is set to fit the skops model card template. If you're using a
1142+
different template, you may have to choose a different section name.
11751143
11761144
description : str or None, default=None
11771145
An optional description to be added before the metrics. If you're
@@ -1186,20 +1154,8 @@ def add_metrics(
11861154
-------
11871155
self : object
11881156
Card object.
1189-
"""
1190-
if section is None:
1191-
if self.template == Templates.skops.value:
1192-
section = "Model description/Evaluation Results"
1193-
else:
1194-
msg = NEED_SECTION_ERR_MSG.format(action="add metrics")
1195-
raise ValueError(msg)
11961157
1197-
if description is None:
1198-
if self.template == Templates.skops.value:
1199-
description = (
1200-
"You can find the details about evaluation process and "
1201-
"the evaluation results."
1202-
)
1158+
"""
12031159
self._metrics.update(kwargs)
12041160
self._add_metrics(section, description=description, metrics=self._metrics)
12051161
return self

skops/card/_templates.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class Templates(Enum):
3535
SKOPS_TEMPLATE = {
3636
"Model description": CONTENT_PLACEHOLDER,
3737
"Model description/Intended uses & limitations": CONTENT_PLACEHOLDER,
38-
"Model description/Training Procedure": "",
38+
"Model description/Training Procedure": CONTENT_PLACEHOLDER,
3939
"Model description/Training Procedure/Hyperparameters": CONTENT_PLACEHOLDER,
40-
"Model description/Training Procedure/Model Plot": "The model plot is below.",
40+
"Model description/Training Procedure/Model Plot": CONTENT_PLACEHOLDER,
4141
"Model description/Evaluation Results": CONTENT_PLACEHOLDER,
4242
"How to Get Started with the Model": CONTENT_PLACEHOLDER,
4343
"Model Card Authors": (

0 commit comments

Comments
 (0)