Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 9c41d04

Browse files
shuklak13ml-fairness-infra-github
authored andcommitted
ModelCard class docstrings
PiperOrigin-RevId: 353777939
1 parent a3e667b commit 9c41d04

1 file changed

Lines changed: 188 additions & 62 deletions

File tree

model_card_toolkit/model_card.py

Lines changed: 188 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -28,175 +28,301 @@
2828

2929
@dataclasses.dataclass
3030
class Version:
31-
"""The information about verions of a model."""
32-
# The name of the version.
31+
"""The information about verions of a model.
32+
33+
If there are multiple versions of the model, or there may be in the future,
34+
it’s useful for your audience to know which version of the model is discussed
35+
in the Model Card. If there are previous versions of this model, briefly
36+
describe how this version is different. If no more than one version of the
37+
model will be released, this field may be omitted.
38+
39+
## Attributes
40+
41+
* `name`: The name of the version.
42+
* `date`: The date this version was released.
43+
* `diff`: The changes from the previous version.
44+
"""
3345
name: Optional[Text] = None
34-
# The date the version was released.
3546
date: Optional[Text] = None
36-
# The changes from the previous version.
3747
diff: Optional[Text] = None
3848

3949

4050
@dataclasses.dataclass
4151
class Owner:
42-
"""The information about owners of a model."""
43-
# The name of the owner.
52+
"""The information about owners of a model.
53+
54+
## Attributes
55+
56+
* `name`: The name of the model owner.
57+
* `contact`: The contact information for the model owner or owners. These
58+
could be individual email addresses, a team mailing list expressly, or a
59+
monitored feedback form.
60+
"""
4461
name: Optional[Text] = None
45-
# The contact information of the owner.
4662
contact: Optional[Text] = None
4763

4864

4965
@dataclasses.dataclass
5066
class ModelDetails:
51-
"""Metadata about the model."""
52-
# The name of the model.
67+
"""This section provides a general, high-level description of the model.
68+
69+
## Attributes
70+
71+
* `name`: The name of the model.
72+
* `overview`: A description of the model card.
73+
* `owners`: The individuals or teams who own the model.
74+
* `version`: The version of the model. If there are previous versions of this
75+
model, briefly describe how this version is different.
76+
* `license`: The license information for the model. If the model is licensed
77+
for use by others, include the license type. If the model is not licensed for
78+
future use, you may state that here as well.
79+
* `references`: Provide any additional links the reader may need. You can
80+
link to foundational research, technical documentation, or other materials
81+
that may be useful to your audience.
82+
* `citation`: How should the model be cited? If the model is based on
83+
published academic research, cite the research.
84+
"""
5385
name: Optional[Text] = None
54-
# A description of the model card.
5586
overview: Optional[Text] = None
56-
# The individuals or teams who own the model.
5787
owners: List[Owner] = dataclasses.field(default_factory=list)
58-
# The version of the model.
59-
version: Version = dataclasses.field(default_factory=Version)
60-
# The model's license for use.
88+
version: Optional[Version] = dataclasses.field(default_factory=Version)
6189
license: Optional[Text] = None
62-
# Links providing more information about the model.
6390
references: List[Text] = dataclasses.field(default_factory=list)
64-
# How to reference this model card.
6591
citation: Optional[Text] = None
6692

6793

6894
@dataclasses.dataclass
6995
class Graphic:
70-
"""A named inline plot."""
71-
# The name of the graphic.
96+
"""A named inline plot.
97+
98+
## Attributes
99+
100+
* `name`: The name of the graphic.
101+
* `image`: The image string encoded as a base64 string.
102+
"""
72103
name: Text
73-
# The image string encoded as a base64 string.
74104
image: Text
75105

76106

77107
@dataclasses.dataclass
78108
class Graphics:
79-
"""A collection of graphics."""
80-
# A description of this collection of graphics.
109+
"""A collection of graphics.
110+
111+
Each ```graphic``` in the ```collection``` field has both a ```name``` and
112+
an ```image```. For instance, you might want to display a graph showing the
113+
number of examples belonging to each class in your training dataset:
114+
115+
```python
116+
117+
model_card.model_parameters.data.train.graphics.collection = [
118+
{'name': 'Training Set Size', 'image': training_set_size_barchart},
119+
]
120+
```
121+
122+
Then, provide a description of the graph:
123+
124+
```python
125+
126+
model_card.model_parameters.data.train.graphics.description = (
127+
'This graph displays the number of examples belonging to each class ',
128+
'in the training dataset. ')
129+
```
130+
131+
## Attributes
132+
133+
* `description`: The name of the dataset.
134+
* `collection`: A collection of graphics.
135+
"""
81136
description: Optional[Text] = None
82-
# A collection of graphics.
83137
collection: List[Graphic] = dataclasses.field(default_factory=list)
84138

85139

86140
@dataclasses.dataclass
87141
class Dataset:
88-
"""The information about a dataset used to generate a model."""
89-
# The name of the dataset.
142+
"""Provide some information about a dataset used to generate a model.
143+
144+
## Attributes
145+
146+
* `name`: The name of the dataset.
147+
* `link`: A link to the dataset.
148+
* `sensitive`: Does this dataset contain human or other sensitive data?
149+
* `graphics`: Visualizations of the dataset.
150+
"""
90151
name: Optional[Text] = None
91-
# The contact information of the owner
92152
link: Optional[Text] = None
93-
# Does this dataset contain human or other sensitive data?
94153
sensitive: Optional[bool] = None
95-
# Visualizations of the dataset.
96154
graphics: Graphics = dataclasses.field(default_factory=Graphics)
97155

98156

99157
@dataclasses.dataclass
100158
class Data:
101-
"""The related datasets used to train and evaluate the model."""
102-
# The training dataset
159+
"""The related datasets used to train and evaluate the model.
160+
161+
## Attributes
162+
163+
* `train`: The training dataset
164+
* `eval`: The evaluation dataset
165+
"""
103166
train: Dataset = dataclasses.field(default_factory=Dataset)
104-
# The evaluation dataset
105167
eval: Dataset = dataclasses.field(default_factory=Dataset)
106168

107169

108170
@dataclasses.dataclass
109171
class ModelParameters:
110-
"""Parameters for construction of the model."""
111-
# The architecture of the model.
172+
"""Parameters for construction of the model.
173+
174+
## Attributes
175+
176+
* `model_architecture`: specifies the architecture of your model.
177+
* `data`: specifies the datasets used to train and evaluate your model.
178+
* `input_format`: describes the data format for inputs to your model.
179+
* `output_format`: describes the data format for outputs from your model.
180+
"""
112181
model_architecture: Optional[Text] = None
113-
# The datasets used to train and evaluate the model.
114182
data: Data = dataclasses.field(default_factory=Data)
115-
# The data format for inputs to the model.
116183
input_format: Optional[Text] = None
117-
# The data format for outputs from the model.
118184
output_format: Optional[Text] = None
119185

120186

121187
@dataclasses.dataclass
122188
class ConfidenceInterval:
123-
"""The confidence interval of the metric."""
124-
# The lower bound of the confidence interval.
189+
"""The confidence interval of the metric.
190+
191+
## Attributes
192+
193+
* `lower_bound`: The lower bound of the confidence interval.
194+
* `upper_bound`: The upper bound of the confidence interval.
195+
"""
125196
lower_bound: float
126-
# The upper bound of the confidence interval.
127197
upper_bound: float
128198

129199

130200
@dataclasses.dataclass
131201
class PerformanceMetric:
132-
"""The details of the performance metric."""
133-
# The type of performance metric.
202+
"""The details of the performance metric.
203+
204+
## Attributes
205+
206+
* `type`: What performance metric are you reporting on?
207+
* `value`: What is the value of this performance metric?
208+
* `confidence_interval`: What is the confidence interval for this
209+
performance metric's value?
210+
* `threshold`: At what threshold was this metric computed?
211+
* `slice`: What slice of your data was this metric computed on?
212+
"""
134213
type: Text
135-
# The value of the performance metric.
136214
value: Union[int, float, Text]
137-
# The confidence interval of the metric.
138215
confidence_interval: Optional[ConfidenceInterval] = None
139-
# The decision threshold the metric was computed on.
140216
threshold: Optional[float] = None
141-
# The name of the slice this metric was computed on.
142217
slice: Optional[Text] = None
143218

144219

145220
@dataclasses.dataclass
146221
class QuantitativeAnalysis:
147-
"""The quantitative analysis of a model."""
148-
# The model performance metrics being reported.
222+
"""The quantitative analysis of a model.
223+
224+
Identify relevant performance metrics and display values. Let’s say you’re
225+
interested in displaying the accuracy and false positive rate (FPR) of a
226+
cat vs. dog classification model. Assuming you have already computed both
227+
metrics, both overall and per-class, you can specify metrics like so:
228+
229+
```python
230+
model_card.quantitative_analysis.performance_metrics = [
231+
{'type': 'accuracy', 'value': computed_accuracy},
232+
{'type': 'accuracy', 'value': cat_accuracy, 'slice': 'cat'},
233+
{'type': 'accuracy', 'value': dog_accuracy, 'slice': 'dog'},
234+
{'type': 'fpr', 'value': computed_fpr},
235+
{'type': 'fpr', 'value': cat_fpr, 'slice': 'cat'},
236+
{'type': 'fpr', 'value': dog_fpr, 'slice': 'dog'},
237+
]
238+
```
239+
240+
## Attributes
241+
242+
* `performance_metrics`: The performance metrics being reported.
243+
* `graphics`: A collection of visualizations of model performance.
244+
"""
149245
performance_metrics: List[PerformanceMetric] = dataclasses.field(
150246
default_factory=list)
151-
# Visualizations of model performance.
152247
graphics: Graphics = dataclasses.field(default_factory=Graphics)
153248

154249

155250
@dataclasses.dataclass
156251
class Risk:
157-
"""The information about risks when using the model."""
158-
# The name of the risk.
252+
"""Information about risks involved when using the model.
253+
254+
## Attributes
255+
256+
* `name`: The name of the risk.
257+
* `mitigation_strategy`: A mitigation strategy that you've implemented, or
258+
one that you suggest to users.
259+
"""
159260
name: Text
160-
# Strategy used to address this risk.
161261
mitigation_strategy: Text
162262

163263

164264
@dataclasses.dataclass
165265
class Considerations:
166-
"""Considerations related to model construction, training, and application."""
167-
# Who are the intended users of the model?
266+
"""Considerations related to model construction, training, and application.
267+
268+
The considerations section includes qualitative information about your model,
269+
including some analysis of its risks and limitations. As such, this section
270+
usually requires careful consideration, and conversations with many relevant
271+
stakeholders, including other model developers, dataset producers, and
272+
downstream users likely to interact with your model, or be affected by its
273+
outputs.
274+
275+
## Attributes
276+
277+
* `users`: Who are the intended users of the model? This may include
278+
researchers, developers, and/or clients. You might also include information
279+
about the downstream users you expect to interact with your model.
280+
* `user_cases`: What are the intended use cases of the model? What use cases
281+
are out-of-scope?
282+
* `limitations`: What are the known limitations of the model? This may
283+
include technical limitations, or conditions that may degrade model
284+
performance.
285+
* `tradeoffs`: What are the known accuracy/performance tradeoffs for the
286+
model?
287+
* `ethical_considerations`: What are the ethical risks involved in
288+
application of this model? For each risk, you may also provide a mitigation
289+
strategy that you've implemented, or one that you suggest to users.
290+
"""
168291
users: List[Text] = dataclasses.field(default_factory=list)
169-
# What are the intended use cases of the model.
170292
use_cases: List[Text] = dataclasses.field(default_factory=list)
171-
# What are the known technical limitations of the model.
172293
limitations: List[Text] = dataclasses.field(default_factory=list)
173-
# What are the known tradeoffs in accuracy/performance of the model
174294
tradeoffs: List[Text] = dataclasses.field(default_factory=list)
175-
# What are the ethical risks involved in the application of this model.
176295
ethical_considerations: List[Risk] = dataclasses.field(default_factory=list)
177296

178297

179298
@dataclasses.dataclass
180299
class ModelCard:
181-
"""Fields used to generate the Model Card."""
182-
# The json schema version of the ModelCard
300+
"""Fields used to generate the Model Card.
301+
302+
## Attributes
303+
304+
* `schema_version`: The Model Card JSON schema version.
305+
* `model_details`: Descriptive metadata for the model.
306+
* `model_parameters`: Technical metadata for the model.
307+
* `quantitative_analysis`: Quantitative analysis of model performance.
308+
* `considerations`: Any considerations related to model construction,
309+
training, and application.
310+
"""
183311
schema_version: Optional[Text] = None
184-
# Descriptive metadata for the model.
185312
model_details: ModelDetails = dataclasses.field(default=ModelDetails())
186-
# Parameters used when generating the model.
187313
model_parameters: ModelParameters = dataclasses.field(
188314
default_factory=ModelParameters)
189-
# The quantitative analysis of the ModelCard
190315
quantitative_analysis: QuantitativeAnalysis = dataclasses.field(
191316
default_factory=QuantitativeAnalysis)
192-
# The considerations related to model construction, training, and application.
193317
considerations: Considerations = dataclasses.field(
194318
default_factory=Considerations)
195319

196320
def to_dict(self) -> Dict[Text, Any]:
321+
"""Convert your model card to a python dictionary."""
197322
# ignore None properties recusively to allow missing values.
198323
ignore_none = lambda properties: {k: v for k, v in properties if v}
199324
return dataclasses.asdict(self, dict_factory=ignore_none)
200325

201326
def to_json(self) -> Text:
327+
"""Convert your model card to json."""
202328
return json.dumps(self.to_dict(), indent=2)

0 commit comments

Comments
 (0)