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

Refactor custom_preprocess() for more robustness #167

Merged
merged 23 commits into from
Oct 13, 2022
Merged
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3ccd638
implements more robust way of returning in custom_preprocess().
dkrako Oct 11, 2022
b7ea6be
Added running instructions to mypy
annahedstroem Oct 11, 2022
790db7f
Added running instructions to mypy
annahedstroem Oct 11, 2022
14a889e
Added running instructions to mypy
annahedstroem Oct 11, 2022
7418f9b
Update README.md
annahedstroem Oct 11, 2022
c9f437b
Update README.md
annahedstroem Oct 11, 2022
d1c900f
fix error in model_parameter_randomisation
dkrako Oct 11, 2022
529dc41
Merge branch 'main' into feature/robust-custom-preprocessing
dkrako Oct 11, 2022
6b0c9f4
simplify calling of custom_preprocess()
dkrako Oct 12, 2022
c9c75c6
fix return type hint of PixelFlipping.custom_preprocess
dkrako Oct 12, 2022
0a4de84
add documentation and examples to custom_preprocessing()
dkrako Oct 12, 2022
04d7fc6
add more info about custom_preprocessing() to CONTRIBUTING.md
dkrako Oct 12, 2022
9bf05fe
Merge branch 'main' into feature/robust-custom-preprocessing
annahedstroem Oct 12, 2022
ce410f1
Merge branch 'main' into feature/robust-custom-preprocessing
annahedstroem Oct 12, 2022
7211a19
Remove all remaining occurences in docstrings for unused c and p argu…
dkrako Oct 12, 2022
1f4772f
Align evaluate_instance() signatures.
dkrako Oct 12, 2022
b84bbf3
Remove custom_batch from __init__ signatures in metrics which don't u…
dkrako Oct 12, 2022
9e0b704
Add docstring to Metric.get_instance_iterator()
dkrako Oct 12, 2022
c4c0e5e
Add more info on `custom_preprocess()` implementation.
dkrako Oct 12, 2022
08e16d0
Fix return descriptions in custom_preprocess() docstrings.
dkrako Oct 12, 2022
330960c
Remove keyword argument `i` from `evaluate_instance()` as it wasn't u…
dkrako Oct 12, 2022
509ca7e
Remove keyword argument `i` from `evaluate_instance()` as it wasn't u…
dkrako Oct 12, 2022
05ca1c2
Fix type hint in SensitivityN
dkrako Oct 12, 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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -139,7 +139,7 @@ The following methods are expected to be implemented in the metric class:
- `evaluate_instance()`: Gets model and data for a single instance as input, returns evaluation result.

The following methods are optimal for implementation:
- `custom_preprocess()`: In case `general_preprocess()` from base class is not sufficient, additional preprocessing steps can be added here.
- `custom_preprocess()`: In case `general_preprocess()` from base class is not sufficient, additional preprocessing steps can be added here. This method must return a dictionary with string keys or None. If a dictionary is returned, additional keyword arguments can be used in `evaluate_instance()`.
- `custom_postprocess()`: Additional postprocessing steps can be added here that is added on top of the resuling evaluation scores.

### Using Helpers
2 changes: 0 additions & 2 deletions quantus/metrics/axiomatic/completeness.py
Original file line number Diff line number Diff line change
@@ -255,8 +255,6 @@ def evaluate_instance(
y: np.ndarray,
a: np.ndarray,
s: np.ndarray,
c: Any,
p: Any,
) -> bool:
"""
Evaluate instance gets model and data for a single instance as input and returns the evaluation result.
23 changes: 1 addition & 22 deletions quantus/metrics/axiomatic/input_invariance.py
Original file line number Diff line number Diff line change
@@ -237,8 +237,6 @@ def evaluate_instance(
y: np.ndarray,
a: np.ndarray,
s: np.ndarray,
c: Any,
p: Any,
) -> bool:
"""
Evaluate instance gets model and data for a single instance as input and returns the evaluation result.
@@ -257,10 +255,6 @@ def evaluate_instance(
The explanation to be evaluated on an instance-basis.
s: np.ndarray
The segmentation to be evaluated on an instance-basis.
c: any
The custom input to be evaluated on an instance-basis.
p: any
The custom preprocess input to be evaluated on an instance-basis.

Returns
-------
@@ -298,9 +292,7 @@ def custom_preprocess(
a_batch: Optional[np.ndarray],
s_batch: np.ndarray,
custom_batch: Optional[np.ndarray],
) -> Tuple[
ModelInterface, np.ndarray, np.ndarray, np.ndarray, np.ndarray, Any, Any
]:
) -> None:
"""
Implementation of custom_preprocess_batch.

@@ -325,19 +317,6 @@ def custom_preprocess(
In addition to the x_batch, y_batch, a_batch, s_batch and custom_batch,
returning a custom preprocess batch (custom_preprocess_batch).
"""

custom_preprocess_batch = [None for _ in range(len(x_batch))]

# Additional explain_func assert, as the one in prepare() won't be
# executed when a_batch != None.
asserts.assert_explain_func(explain_func=self.explain_func)

return (
model,
x_batch,
y_batch,
a_batch,
s_batch,
custom_batch,
custom_preprocess_batch,
)
25 changes: 2 additions & 23 deletions quantus/metrics/axiomatic/non_sensitivity.py
Original file line number Diff line number Diff line change
@@ -259,8 +259,6 @@ def evaluate_instance(
y: np.ndarray,
a: np.ndarray,
s: np.ndarray,
c: Any,
p: Any,
) -> int:
"""
Evaluate instance gets model and data for a single instance as input and returns the evaluation result.
@@ -279,10 +277,6 @@ def evaluate_instance(
The explanation to be evaluated on an instance-basis.
s: np.ndarray
The segmentation to be evaluated on an instance-basis.
c: any
The custom input to be evaluated on an instance-basis.
p: any
The custom preprocess input to be evaluated on an instance-basis.

Returns
-------
@@ -330,9 +324,7 @@ def custom_preprocess(
a_batch: Optional[np.ndarray],
s_batch: np.ndarray,
custom_batch: Optional[np.ndarray],
) -> Tuple[
ModelInterface, np.ndarray, np.ndarray, np.ndarray, np.ndarray, Any, Any
]:
) -> None:
"""
Implementation of custom_preprocess_batch.

@@ -349,29 +341,16 @@ def custom_preprocess(
s_batch: np.ndarray, optional
A np.ndarray which contains segmentation masks that matches the input.
custom_batch: any
Gives flexibility ot the user to use for evaluation, can hold any variable.
Gives flexibility to the user to use for evaluation, can hold any variable.

Returns
-------
tuple
In addition to the x_batch, y_batch, a_batch, s_batch and custom_batch,
returning a custom preprocess batch (custom_preprocess_batch).
"""

custom_preprocess_batch = [None for _ in range(len(x_batch))]

# Asserts.
asserts.assert_features_in_step(
features_in_step=self.features_in_step,
input_shape=x_batch.shape[2:],
)

return (
model,
x_batch,
y_batch,
a_batch,
s_batch,
custom_batch,
custom_preprocess_batch,
)
Loading