-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
57 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,12 +26,13 @@ | |
|
||
logging.getLogger(__name__).addHandler(logging.NullHandler()) | ||
|
||
__author__ = "DIANNA Team" | ||
__email__ = "[email protected]" | ||
__version__ = "0.9.0" | ||
__author__ = 'DIANNA Team' | ||
__email__ = '[email protected]' | ||
__version__ = '1.0.0' | ||
|
||
|
||
def explain_timeseries(model_or_function, timeseries_data, method, labels, **kwargs): | ||
def explain_timeseries(model_or_function, timeseries_data, method, labels, | ||
**kwargs): | ||
"""Explain timeseries data given a model and a chosen method. | ||
Args: | ||
|
@@ -46,11 +47,10 @@ def explain_timeseries(model_or_function, timeseries_data, method, labels, **kwa | |
One heatmap per class. | ||
""" | ||
explainer = _get_explainer(method, kwargs, modality="Timeseries") | ||
explain_timeseries_kwargs = utils.get_kwargs_applicable_to_function(explainer.explain, kwargs) | ||
return explainer.explain(model_or_function, | ||
timeseries_data, | ||
labels, | ||
explainer = _get_explainer(method, kwargs, modality='Timeseries') | ||
explain_timeseries_kwargs = utils.get_kwargs_applicable_to_function( | ||
explainer.explain, kwargs) | ||
return explainer.explain(model_or_function, timeseries_data, labels, | ||
**explain_timeseries_kwargs) | ||
|
||
|
||
|
@@ -69,15 +69,18 @@ def explain_image(model_or_function, input_data, method, labels, **kwargs): | |
One heatmap (2D array) per class. | ||
""" | ||
if method.upper() == "KERNELSHAP": | ||
if method.upper() == 'KERNELSHAP': | ||
# To avoid Access Violation on Windows with SHAP: | ||
from onnx_tf.backend import prepare # noqa: F401 | ||
explainer = _get_explainer(method, kwargs, modality="Image") | ||
explain_image_kwargs = utils.get_kwargs_applicable_to_function(explainer.explain, kwargs) | ||
return explainer.explain(model_or_function, input_data, labels, **explain_image_kwargs) | ||
explainer = _get_explainer(method, kwargs, modality='Image') | ||
explain_image_kwargs = utils.get_kwargs_applicable_to_function( | ||
explainer.explain, kwargs) | ||
return explainer.explain(model_or_function, input_data, labels, | ||
**explain_image_kwargs) | ||
|
||
|
||
def explain_text(model_or_function, input_text, tokenizer, method, labels, **kwargs): | ||
def explain_text(model_or_function, input_text, tokenizer, method, labels, | ||
**kwargs): | ||
"""Explain text (input_text) given a model and a chosen method. | ||
Args: | ||
|
@@ -93,24 +96,28 @@ def explain_text(model_or_function, input_text, tokenizer, method, labels, **kwa | |
List of (word, index of word in raw text, importance for target class) tuples. | ||
""" | ||
explainer = _get_explainer(method, kwargs, modality="Text") | ||
explain_text_kwargs = utils.get_kwargs_applicable_to_function(explainer.explain, kwargs) | ||
return explainer.explain( | ||
model_or_function=model_or_function, | ||
input_text=input_text, | ||
labels=labels, | ||
tokenizer=tokenizer, | ||
**explain_text_kwargs) | ||
explainer = _get_explainer(method, kwargs, modality='Text') | ||
explain_text_kwargs = utils.get_kwargs_applicable_to_function( | ||
explainer.explain, kwargs) | ||
return explainer.explain(model_or_function=model_or_function, | ||
input_text=input_text, | ||
labels=labels, | ||
tokenizer=tokenizer, | ||
**explain_text_kwargs) | ||
|
||
|
||
def _get_explainer(method, kwargs, modality): | ||
try: | ||
method_submodule = importlib.import_module(f'dianna.methods.{method.lower()}') | ||
method_submodule = importlib.import_module( | ||
f'dianna.methods.{method.lower()}') | ||
except ImportError as err: | ||
raise ValueError(f"Method {method} does not exist") from err | ||
raise ValueError(f'Method {method} does not exist') from err | ||
try: | ||
method_class = getattr(method_submodule, f"{method.upper()}{modality}") | ||
method_class = getattr(method_submodule, f'{method.upper()}{modality}') | ||
except AttributeError as err: | ||
raise ValueError(f"Data modality {modality} is not available for method {method.upper()}") from err | ||
method_kwargs = utils.get_kwargs_applicable_to_function(method_class.__init__, kwargs) | ||
raise ValueError( | ||
f'Data modality {modality} is not available for method {method.upper()}' | ||
) from err | ||
method_kwargs = utils.get_kwargs_applicable_to_function( | ||
method_class.__init__, kwargs) | ||
return method_class(**method_kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters