Skip to content

Commit fde2f46

Browse files
authored
FAI-896: Doc standardization and cleanup (#126)
* default parameter doc standardization * Standardized kwargs labelings, fixed typos * linting + black * Update counterfactuals.py
1 parent bc4ec92 commit fde2f46

File tree

9 files changed

+66
-47
lines changed

9 files changed

+66
-47
lines changed

docs/api.rst

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,37 @@ Model Classes
4646

4747
trustyai.explainers
4848
-------------------
49+
LIME
50+
####
4951
.. currentmodule:: trustyai.explainers
5052
.. explainers_api:
5153
.. autosummary::
5254
:toctree: generated/
5355

54-
CounterfactualExplainer
55-
CounterfactualResult
5656
LimeExplainer
5757
LimeResults
58+
59+
SHAP
60+
####
61+
.. autosummary::
62+
:toctree: generated/
63+
5864
SHAPExplainer
65+
BackgroundGenerator
5966
SHAPResults
6067

68+
Counterfactuals
69+
###############
70+
.. autosummary::
71+
:toctree: generated/
72+
73+
CounterfactualExplainer
74+
CounterfactualResult
75+
6176
trustyai.utils
62-
-------------------
77+
--------------
6378
.. currentmodule:: trustyai.utils.tyrus
64-
.. explainers_api:
79+
.. utils_api:
6580
.. autosummary::
6681
:toctree: generated/
6782

src/trustyai/explainers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# pylint: disable=duplicate-code
33
from .counterfactuals import CounterfactualResult, CounterfactualExplainer
44
from .lime import LimeExplainer, LimeResults
5-
from .shap import SHAPExplainer, SHAPResults
5+
from .shap import SHAPExplainer, SHAPResults, BackgroundGenerator

src/trustyai/explainers/counterfactuals.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,14 @@ def explain(
196196
The desired model outputs to be searched for in the counterfactual explanation.
197197
These can take the form of a: {}
198198
model : :obj:`~trustyai.model.PredictionProvider`
199-
The TrustyAI PredictionProvider, as generated by :class:`~trustyai.model.Model` or
200-
:class:`~trustyai.model.ArrowModel`.
199+
The TrustyAI model as generated by :class:`~trustyai.model.Model` or a Java :class:`PredictionProvider`
201200
data_distribution : Optional[:class:`DataDistribution`]
202201
The :class:`DataDistribution` to use when sampling the inputs.
203202
uuid : Optional[:class:`_uuid.UUID`]
204203
The UUID to use during search.
205204
timeout : Optional[float]
206205
The timeout time in seconds of the counterfactual explanation.
206+
207207
Returns
208208
-------
209209
:class:`~CounterfactualResult`

src/trustyai/explainers/explanation_results.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def plot(self, output_name=None, render_bokeh=False, block=True) -> None:
5151
Parameters
5252
----------
5353
output_name : str
54-
(default=None) The name of the output to be explainer. If `None`, all outputs will
54+
(default= `None`) The name of the output to be explainer. If `None`, all outputs will
5555
be displayed
5656
render_bokeh : bool
57-
(default: false) Whether to render as bokeh (true) or matplotlib (false)
57+
(default= `False`) If true, render plot in bokeh, otherwise use matplotlib.
5858
block: bool
59-
(default: true) Whether displaying the plot blocks subsequent code execution
59+
(default= `True`) Whether displaying the plot blocks subsequent code execution
6060
"""
6161
if output_name is None:
6262
for output_name_iterator in self.saliency_map().keys():

src/trustyai/explainers/lime.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class LimeExplainer:
212212
"""
213213

214214
def __init__(self, samples=10, **kwargs):
215-
"""Initialize the :class:`LimeExplainer`.
215+
r"""Initialize the :class:`LimeExplainer`.
216216
217217
Parameters
218218
----------
@@ -221,23 +221,23 @@ def __init__(self, samples=10, **kwargs):
221221
222222
Keyword Arguments:
223223
* penalise_sparse_balance : bool
224-
(default=True) Whether to penalise features that are likely to produce linearly
224+
(default= ``True``) Whether to penalise features that are likely to produce linearly
225225
inseparable outputs. This can improve the efficacy and interpretability of the
226226
outputted saliencies.
227227
* normalise_weights : bool
228-
(default=False) Whether to normalise the saliencies generated by LIME. If selected,
228+
(default= ``False``) Whether to normalise the saliencies generated by LIME. If selected,
229229
saliencies will be normalized between 0 and 1.
230230
* use_wlr_model : bool
231-
(default=True) Whether to use a weighted linear regression as the LIME explanatory
231+
(default= ``True``) Whether to use a weighted linear regression as the LIME explanatory
232232
model. If `false`, a multilayer perceptron is used, which generally has a slower
233233
runtime,
234234
* seed: int
235-
(default=0) The random seed to be used.
235+
(default= ``0``) The random seed to be used.
236236
* perturbations: int
237-
(default=1) The starting number of feature perturbations within the explanation
237+
(default= ``1``) The starting number of feature perturbations within the explanation
238238
process.
239239
* trackCounterfactuals : bool
240-
(default=False) Keep track of produced byproduct counterfactuals during LIME run.
240+
(default= ``False``) Keep track of produced byproduct counterfactuals during LIME run.
241241
242242
"""
243243
self._jrandom = Random()

src/trustyai/explainers/shap.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@ def _get_bokeh_plot_dict(self):
420420

421421

422422
class BackgroundGenerator:
423-
r"""Generate a background for the SHAP explainer via one of three algorithms:
423+
r"""Generate a background for the SHAP explainer
424+
425+
Generate a background for the SHAP explainer via one of three algorithms:
424426
425427
* `sample`: Randomly sample a set of provided points
426428
* `kmeans`: Summarize a set of provided points into k centroids
@@ -502,33 +504,35 @@ def counterfactual(
502504
The number of background datapoints to generate per goal.
503505
Keyword Arguments:
504506
* k_seeds: int
505-
(default=5) For each goal, a number of starting seeds from `datapoints` are used
507+
(default= ``5``) For each goal, a number of starting seeds from `datapoints` are used
506508
to start the search from. These are the `k_seeds` points within `datapoint`
507509
whose corresponding outputs are closet to the goal output. Choose a larger
508510
number to get a more diverse background dataset, but the search might require
509511
larger `max_attempt_count`, `step_count`, and `timeout_seconds` to get good results.
510512
* goal_threshold: float
511-
(default=.01) The distance (percentage) threshold defining whether
513+
(default= ``.01``) The distance (percentage) threshold defining whether
512514
a particular output satisfies the goal. Set to 0 to require an exact match, but
513-
this will likey require larger `max_attempt_count`, `step_count`,
515+
this will likely require larger `max_attempt_count`, `step_count`,
514516
and `timeout_seconds` to get good results.
515517
* chain: boolean
516-
(default=False) If chaining is set to `true`, found counterfactual datapoints
518+
(default= ``False``) If chaining is set to `true`, found counterfactual datapoints
517519
will be added to the search seeds for subsequent searches. This is useful when a
518520
range of counterfactual outputs is desired; for example, if the desired goals are
519-
[0, 1, 2, 3], whichever goal is closest to the closest point within `datapoints` will
520-
be searched for first. The found counterfactuals from that search are then included
521-
in the search for the second-closest goal, and so on. This is especially helpful
522-
if the extremes of the goal range are far outside the range produced by the
521+
[0, 1, 2, 3], whichever goal is closest to the closest point within `datapoints`
522+
will be searched for first. The found counterfactuals from that search are then
523+
included in the search for the second-closest goal, and so on. This is especially
524+
helpful if the extremes of the goal range are far outside the range produced by the
523525
`datapoints`. If only
524526
* max_attempt_count: int
525-
If no valid counterfactual can be found for a starting seed in the search, the point
526-
is slightly perturbed and search is retried. This parameter sets the maximum
527-
number of perturbation-retry cycles are allowed during generation.
527+
(default= ``5'`) If no valid counterfactual can be found for a starting seed in the
528+
search, the point is slightly perturbed and search is retried. This parameter sets
529+
the maximum number of perturbation-retry cycles are allowed during generation.
528530
* step_count: int
529-
(default=10,000) The number of datapoints to evaluate during the search
531+
(default= ``5_000``) The number of datapoints to evaluate during the search
530532
* timeout_seconds: int
531-
(default=30) The maximum number of seconds allowed for each counterfactual search
533+
(default= ``3``) The maximum number of seconds allowed for each counterfactual search.
534+
This will set the maximum runtime of the search to roughly
535+
`timeout_seconds` * `max_attempt_count` * `k_per_goal` * `len(goals)`
532536
533537
Returns
534538
-------
@@ -610,19 +614,19 @@ def __init__(
610614
Otherwise, choose ``IDENTITY``.
611615
Keyword Arguments:
612616
* samples: int
613-
(default=None) The number of samples to use when computing SHAP values. Higher
617+
(default= ``None``) The number of samples to use when computing SHAP values. Higher
614618
values will increase explanation accuracy, at the cost of runtime. If none,
615619
samples will equal 2048 + 2*n_features
616620
* seed: int
617-
(default=0) The random seed to be used when generating explanations.
621+
(default= ``0``) The random seed to be used when generating explanations.
618622
* batchSize: int
619-
(default=20) The number of batches passed to the PredictionProvider at once.
620-
When uusing :class:`~Model` with `arrow=False` this parameter has no effect.
623+
(default= ``20``) The number of batches passed to the PredictionProvider at once.
624+
When using :class:`~Model` with `disable_arrow=True` this parameter has no effect.
621625
If `arrow=True`, `batch_sizes` of around
622626
:math:`\frac{{2000}}{{\mathtt{{len(background)}}}}` can produce significant
623627
performance gains.
624628
* trackCounterfactuals : bool
625-
(default=False) Keep track of produced byproduct counterfactuals during SHAP run.
629+
(default= ``False``) Keep track of produced byproduct counterfactuals during SHAP run.
626630
627631
Returns
628632
-------

src/trustyai/model/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ def output(name, dtype, value=None, score=1.0) -> _Output:
728728
* ``categorical`` for categorical outputs.
729729
730730
If `dtype` is unspecified or takes a different value than listed above, the
731-
feature type will be set as `UNDEFINED`.
731+
feature type will be set as `UNDEFINED`.
732732
value : Any
733733
The value of this output.
734734
score : float

src/trustyai/model/domain.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ def feature_domain(values: Optional[Union[Tuple, List]]) -> Optional[FeatureDoma
2424
Parameters
2525
----------
2626
values : Optional[Union[Tuple, List]]
27-
The valid values of the feature. If `values` takes the form of:
27+
The valid values of the feature. If ``values`` takes the form of:
2828
29-
* **A tuple of floats or integers:** The feature domain will be a continuous range from
29+
* **A tuple of floats or integers**: The feature domain will be a continuous range from
3030
``values[0]`` to ``values[1]``.
31-
* **A list of floats or integers:**: The feature domain will be a *numeric* categorical,
32-
where `values` contains all possible valid feature values.
33-
* **A list of strings:** The feature domain will be a *string* categorical, where `values`
34-
contains all possible valid feature values.
35-
* **A list of objects:** The feature domain will be an *object* categorical, where `values`
36-
contains all possible valid feature values. These may present an issue if the objects
37-
are not natively Java serializable.
31+
* **A list of floats or integers**: The feature domain will be a *numeric* categorical,
32+
where `values` contains all possible valid feature values.
33+
* **A list of strings**: The feature domain will be a *string* categorical, where ``values``
34+
contains all possible valid feature values.
35+
* **A list of objects**: The feature domain will be an *object* categorical, where
36+
``values`` contains all possible valid feature values. These may present an issue if the
37+
objects are not natively Java serializable.
3838
3939
Otherwise, the feature domain will be taken as `Empty`, which will mean it will be held
4040
fixed during the counterfactual explanation.

src/trustyai/utils/tyrus.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def __init__(
116116
(default=`0.1`) The fraction of found byproduct counterfactuals to display in the
117117
dashboard, as a float between 0 and 1. Choose a larger number to see more,
118118
but this will make plot rendering more expensive.
119-
* notebook : `bool
119+
* notebook : bool
120120
(default=`False`) If true, Tyrus will launch the visualizations inline in a
121121
Jupyter notebook. If false, the visualizations will be saved as HTML and opened
122122
automatically in your default browser.

0 commit comments

Comments
 (0)