Skip to content

Commit cfc1992

Browse files
committed
start model card
1 parent 22405bd commit cfc1992

4 files changed

Lines changed: 153 additions & 1 deletion

File tree

vetiver/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .handlers.statsmodels import StatsmodelsHandler # noqa
1818
from .rsconnect import deploy_rsconnect # noqa
1919
from .monitor import compute_metrics, pin_metrics, plot_metrics, _rolling_df # noqa
20+
from .model_card import model_card # noqa
2021

2122
__author__ = "Isabel Zimmerman <isabel.zimmerman@rstudio.com>"
2223
__all__ = []

vetiver/model_card.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import logging
2+
from importlib_resources import files as _files
3+
import shutil
4+
5+
_log = logging.getLogger(__name__)
6+
7+
8+
def model_card(path="."):
9+
"""Create a model card for documentation
10+
11+
Parameters
12+
----------
13+
path : str
14+
Path to save model card
15+
"""
16+
src_path = _files("vetiver") / "templates/model_card.qmd"
17+
18+
if _log is not None:
19+
_log.info("Writing model card template...")
20+
21+
return shutil.copy(src=src_path, dst=path)

vetiver/pin_read_write.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def vetiver_pin_write(board, model: VetiverModel, versioned: bool = True):
3737
# message = """
3838
# Create a Model Card for your published model.
3939
# Model Cards provide a framework for transparent, responsible reporting.
40-
# Use the vetiver `.Rmd` template as a place to start."""
40+
# Use the vetiver `.qmd` Quarto template as a place to start,
41+
# with vetiver.model_card()"""
4142

4243
# warnings.warn(message=message)
4344

vetiver/templates/model_card.qmd

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
title: "Model Card: your model title"
3+
date: '`python date.today()`'
4+
output:
5+
html_document
6+
params:
7+
board: !python pins.board_rsconnect()
8+
name: isabel.zimmerman/sacramento_rf
9+
version: NULL
10+
---
11+
12+
```{python setup, include=FALSE}
13+
from datetime import date
14+
from sklearn import metrics
15+
import vetiver
16+
import pins
17+
18+
# library(yardstick)
19+
# knitr::opts_chunk$set(echo = FALSE)
20+
v = vetiver.VetiverModel.from_pin(pins.board_rsconnect(server_url = "https://colorado.rstudio.com/rsc/"), "isabel.zimmerman/cars_mpg")
21+
```
22+
23+
A [model card](https://doi.org/10.1145/3287560.3287596) provides brief, transparent, responsible reporting for a trained machine learning model.
24+
25+
## Model details
26+
27+
- Developed by PERSON AND/OR TEAM
28+
- `python print(f"{v.description} using {v.ptype} features")`
29+
- More details about how model was developed and what it is predicting
30+
- More details on feature engineering and/or data preprocessing for model
31+
- Version `python v.metadata.version` of this model was published at `r v_meta.created`
32+
- Citation and/or license details for the model
33+
- If you have questions about this model, please contact PERSON@ORG.ORG
34+
35+
## Intended use
36+
37+
- The primary intended uses of this model are ...
38+
- The primary intended users of this model are ...
39+
- Some use cases are out of scope for this model, such as ...
40+
41+
## Important aspects/factors
42+
43+
- Aspects or factors (demographic, environmental, technical) that are relevant to the context of this model are ...
44+
- In evaluating this model, we examined aspects such as ...
45+
46+
## Metrics
47+
48+
- The metrics used to evaluate this model are ...
49+
- These metrics are computed via ...
50+
- We chose these metrics because ...
51+
52+
## Training data & evaluation data
53+
54+
- The training dataset for this model was ...
55+
- The training dataset for this model has the "prototype" or signature:
56+
57+
```{python}
58+
v.ptype
59+
```
60+
61+
- The evaluation dataset used in this model card is ...
62+
- We chose this evaluation data because ...
63+
64+
```{python}
65+
## EVALUATION DATA:
66+
from vetiver.data import mtcars
67+
## consider using a package like skimr or DataExplorer for automated
68+
## presentation of evaluation data characteristics
69+
```
70+
71+
72+
## Quantitative analyses {.tabset}
73+
74+
```{python}
75+
## compute predictions for your evaluation data
76+
## `handler_startup` is designed to get the R process ready to make predictions
77+
#suppressPackageStartupMessages(handler_startup(v))
78+
new_data["preds"] = v.model.predict(mtcars.drop(columns="mpg"))
79+
```
80+
81+
82+
### Overall model performance
83+
84+
```{python}
85+
86+
metric_set = [metrics.mean_absolute_error, metrics.mean_squared_error]
87+
88+
```
89+
90+
### Disaggregated model performance
91+
92+
```{python}
93+
94+
preds %>%
95+
group_by(type) %>%
96+
metrics(price, .pred)
97+
```
98+
99+
### Visualize model performance
100+
101+
```{python, fig.height=3}
102+
preds %>%
103+
ggplot(aes(price, .pred, color = type)) +
104+
geom_abline(slope = 1, lty = 2, color = "gray60", size = 1.2) +
105+
geom_point(alpha = 0.5, show.legend = FALSE) +
106+
facet_wrap(vars(type))
107+
```
108+
109+
### Make a custom plot
110+
111+
```{r}
112+
preds %>%
113+
mutate(.resid = price - .pred) %>%
114+
ggplot(aes(longitude, latitude, color = .resid)) +
115+
geom_point(alpha = 0.8) +
116+
scale_color_gradient2() +
117+
coord_fixed()
118+
```
119+
120+
121+
## Ethical considerations
122+
123+
- We considered ...
124+
125+
## Caveats & recommendations
126+
127+
- This model does ...
128+
- This model does not ...
129+
- We recommend ...

0 commit comments

Comments
 (0)