Skip to content

FAI-926: Implement custom counterfactual goal criteria #140

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

Merged
merged 7 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 10 additions & 3 deletions src/trustyai/explainers/counterfactuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
counterfactual_prediction,
PredictionInput,
Model,
GoalCriteria,
)


from trustyai.utils.data_conversions import (
prediction_object_to_numpy,
prediction_object_to_pandas,
Expand Down Expand Up @@ -184,12 +184,13 @@ def __init__(self, steps=10_000):
def explain(
self,
inputs: OneInputUnionType,
goal: OneOutputUnionType,
model: Union[PredictionProvider, Model],
goal: Optional[OneOutputUnionType] = None,
feature_domains: List[FeatureDomain] = None,
data_distribution: Optional[DataDistribution] = None,
uuid: Optional[_uuid.UUID] = None,
timeout: Optional[float] = None,
criteria: Optional[GoalCriteria] = None,
) -> CounterfactualResult:
r"""Request for a counterfactual explanation given a list of features, goals and a
:class:`~PredictionProvider`
Expand Down Expand Up @@ -217,7 +218,9 @@ def explain(
uuid : Optional[:class:`_uuid.UUID`]
The UUID to use during search.
timeout : Optional[float]
The timeout time in seconds of the counterfactual explanation.
The timeout time in seconds of the counterfactual explanation.
criteria : Optional[:class:`GoalCriteria`]
An optional custom scoring function, wrapped as a :class:`GoalCriteria`.

Returns
-------
Expand All @@ -226,6 +229,9 @@ def explain(
"""
feature_names = model.feature_names if isinstance(model, Model) else None
output_names = model.output_names if isinstance(model, Model) else None
if goal is None and criteria is None:
raise ValueError("Either a goal or criteria must be provided.")

_prediction = counterfactual_prediction(
input_features=one_input_convert(
inputs, feature_names=feature_names, feature_domains=feature_domains
Expand All @@ -236,6 +242,7 @@ def explain(
data_distribution=data_distribution,
uuid=uuid,
timeout=timeout,
criteria=criteria,
)

with Model.NonArrowTransmission(model):
Expand Down
Loading