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

Alternative Card implementation #203

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
faf9e37
[skip ci] [WIP] Alternative Card implementation
BenjaminBossan Oct 25, 2022
cc229a2
[WIP] Further align new model card design
BenjaminBossan Oct 31, 2022
166e33d
Make tests pass
BenjaminBossan Nov 1, 2022
a90e3a5
Add tests for new functionalities and docstrings
BenjaminBossan Nov 1, 2022
fcbd0df
Adjust tests to work with older sklearn versions
BenjaminBossan Nov 2, 2022
4f10f80
Adjust examples to new card, more docs
BenjaminBossan Nov 2, 2022
0f5df2e
Continue fixing tests
BenjaminBossan Nov 2, 2022
6212d67
Try fixing Windows error by specifying encoding
BenjaminBossan Nov 2, 2022
80d218d
Adjust doctest: confusion matrix not stored in cwd
BenjaminBossan Nov 2, 2022
c6c8042
Increase test coverage
BenjaminBossan Nov 2, 2022
e7a2ae3
Try fixing test failure on Windows
BenjaminBossan Nov 3, 2022
2a6aab0
Replace old by new Card implementation
BenjaminBossan Nov 10, 2022
ce16019
Merge branch 'main' into alternative-model-card-implementation
BenjaminBossan Nov 10, 2022
00b7665
Address reviewer comments
BenjaminBossan Nov 21, 2022
8558f26
Add TODO notes for when Python 3.7 is dropped
BenjaminBossan Nov 25, 2022
fdb7674
Add Hub model card template, add template arg
BenjaminBossan Nov 25, 2022
772a659
Make _add_single return the Section
BenjaminBossan Nov 30, 2022
7cfddf9
Allow tables without rows to be added
BenjaminBossan Nov 30, 2022
26892d6
Error when calling add_metric w/ invalid template
BenjaminBossan Dec 1, 2022
f0c496d
Merge branch 'main' into alternative-model-card-implementation
BenjaminBossan Dec 6, 2022
b4d821b
Some amendments required after merging
BenjaminBossan Dec 6, 2022
0b66e14
A couple more changes:
BenjaminBossan Dec 6, 2022
d859c00
Add entry to changes.rst
BenjaminBossan Dec 6, 2022
4af4b12
Fix small bug in generated code for loading
BenjaminBossan Dec 6, 2022
c5c21e6
Address reviewer comments
BenjaminBossan Dec 9, 2022
00ec8a2
Adjust getting started code in model card
BenjaminBossan Dec 12, 2022
9ee11fe
Merge branch 'main' into alternative-model-card-implementation
BenjaminBossan Dec 12, 2022
91a5b7a
Adjust docstrings
BenjaminBossan Dec 12, 2022
4564dc3
Merge branch 'main' into alternative-model-card-implementation
BenjaminBossan Dec 14, 2022
fa00e03
Merge branch 'main' into alternative-model-card-implementation
BenjaminBossan Dec 14, 2022
68e4748
Allow adding default sections with custom template
BenjaminBossan Dec 15, 2022
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
68 changes: 48 additions & 20 deletions docs/model_card.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,30 @@ to touch it yourself.

The markdown part does not necessarily need to follow any specification in
terms of information passed, which gives the user a lot of flexibility. The
markdown part of the ``README.md`` file is generated from a Jinja template
with slots that you can inject your content in. ``skops`` has a default
template which includes the following slots for free text sections:

- ``"model_description"``: A description of the model.
- ``"limitations"``: Intended use for the model, limitations and potential
biases. This section should also include risks of using models in certain
domains if relevant.
- ``"get_started_code"``: Code the user can run to load and use the model.
- ``"model_card_authors"``: Authors of the model card. This section includes
authors of the model card, while ``"citation_bibtex"`` includes citations
related to the model if relevant.
- ``"model_card_contact"``: Contact information of people whom can be reached
markdown part of the ``README.md`` file comes with a couple of defaults provided
by ``skops``, which includes the following slots for free text sections:

- ``"Mode description"``: A description of the model.
- ``"Intended uses & limitations"``: Intended use for the model, limitations and
potential biases. This section should also include risks of using models in
certain domains if relevant.
- ``"How to Get Started with the Model"``: Code the user can run to load and use
the model.
- ``"Model Card Authors"``: Authors of the model card. This section includes
authors of the model card
- ``"Model Card Contact"``: Contact information of people whom can be reached
out, in case of questions about the model or the model card.
- ``"citation_bibtex"``: Bibtex style citations for the model or resources used
to train the model.
- ``"eval_methods"``: Details about evaluation process of the model.
- ``"eval_results"``: Evaluation results that are later parsed as a table by
:class:`skops.card.Card`.
- ``"Citation"``: Bibtex style citations for the model or resources used to
train the model.
- ``"Evaluation Results"``: Evaluation results that are later parsed as a table
by :class:`skops.card.Card`.


The template also contains the following sections that are automatically
generated by ``skops``.

- ``"hyperparameter_table"``: Hyperparameters of the model.
- ``"model_plot"``: A diagram of the model, most relevant in case the model is
- ``"Hyperparameters"``: Hyperparameters of the model.
- ``"Model Plot"``: A diagram of the model, most relevant in case the model is
a complex scikit-learn :class:`~sklearn.pipeline.Pipeline`.

Furthermore, it is possible to add plots and tables to the model card. To add
Expand All @@ -77,5 +75,35 @@ dictionaries with the key being the header and the values being list of row
entries, or a pandas ``DataFrame``; use the :meth:`.Card.add_table` method for
this.

To add content to an existing subsection, or create a new subsection, use a
``"/"`` to indicate the subsection. E.g. let's assume you would like to add a
subsection called ``"Figures"`` to the existing section ``"Model description"``,
as well as adding some subsections with plots below that, you can call the
:meth:`Card.add` method like this:

.. code-block:: python

card.add(**{"Model description/Figures": "Here are some nice figures"})
card.add_plot(**{
adrinjalali marked this conversation as resolved.
Show resolved Hide resolved
"Model description/Figures/Confusion Matrix": "path-to-confusion-matrix.png",
"Model description/Figures/ROC": "path-to-roc.png",
})

Furthermore, you can select existing sections (as well as their subsections)
using :meth:`Card.select`, and you can delete sections using
:meth:`Card.delete`:

.. code-block:: python

section = card.select("Model description/Figures")
adrinjalali marked this conversation as resolved.
Show resolved Hide resolved
print(section.content) # 'Here are some nice figures'
print(section.subsections)
card.delete("Model description/Figures/ROC")


To see how you can use the API in ``skops`` to create a model card, please
refer to :ref:`sphx_glr_auto_examples_plot_model_card.py`.

Templates
---------
TODO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove this for the merge

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

37 changes: 21 additions & 16 deletions examples/plot_model_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
)
from sklearn.model_selection import HalvingGridSearchCV, train_test_split

from skops import card, hub_utils
from skops import hub_utils
from skops.card import Card, metadata_from_config

# %%
# Data
Expand Down Expand Up @@ -91,7 +92,7 @@
# :func:`.hub_utils.init` above. We will see below how we can populate the model
# card with useful information.

model_card = card.Card(model, metadata=card.metadata_from_config(Path(local_repo)))
model_card = Card(model, metadata=metadata_from_config(Path(local_repo)))

# %%
# Add more information
Expand All @@ -103,17 +104,19 @@
model_card.metadata.license = "mit"
limitations = "This model is not ready to be used in production."
model_description = (
"This is a HistGradientBoostingClassifier model trained on breast cancer dataset."
" It's trained with Halving Grid Search Cross Validation, with parameter grids on"
" max_leaf_nodes and max_depth."
"This is a `HistGradientBoostingClassifier` model trained on breast cancer "
"dataset. It's trained with `HalvingGridSearchCV`, with parameter grids on "
"`max_leaf_nodes` and `max_depth`."
)
model_card_authors = "skops_user"
citation_bibtex = "bibtex\n@inproceedings{...,year={2020}}"
citation_bibtex = "**BibTeX**\n\n```\n@inproceedings{...,year={2020}}\n```"
model_card.add(
citation_bibtex=citation_bibtex,
model_card_authors=model_card_authors,
limitations=limitations,
model_description=model_description,
**{
"Citation": citation_bibtex,
"Model Card Authors": model_card_authors,
"Model description": model_description,
"Model description/Intended uses & limitations": limitations,
}
)

# %%
Expand All @@ -132,10 +135,10 @@

y_pred = model.predict(X_test)
eval_descr = (
"The model is evaluated on test data using accuracy and F1-score with macro"
" average."
"The model is evaluated on test data using accuracy and F1-score with "
"macro average."
)
model_card.add(eval_method=eval_descr)
model_card.add(**{"Model description/Evaluation Results": eval_descr})

accuracy = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred, average="micro")
Expand All @@ -146,7 +149,9 @@
disp.plot()

disp.figure_.savefig(Path(local_repo) / "confusion_matrix.png")
model_card.add_plot(**{"Confusion matrix": "confusion_matrix.png"})
model_card.add_plot(
**{"Model description/Evaluation Results/Confusion Matrix": "confusion_matrix.png"}
)

cv_results = model.cv_results_
clf_report = classification_report(
Expand All @@ -160,8 +165,8 @@
model_card.add_table(
folded=True,
**{
"Hyperparameter search results": cv_results,
"Classification report": clf_report,
"Model description/Evaluation Results/Hyperparameter search results": cv_results,
"Model description/Evaluation Results/Classification report": clf_report,
adrinjalali marked this conversation as resolved.
Show resolved Hide resolved
},
)

Expand Down
Loading