-
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.
Merge branch 'main' into 667-reorganize
- Loading branch information
Showing
18 changed files
with
927 additions
and
226 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
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 |
---|---|---|
|
@@ -22,17 +22,17 @@ | |
""" | ||
import importlib | ||
import logging | ||
import warnings | ||
from . import utils | ||
|
||
logging.getLogger(__name__).addHandler(logging.NullHandler()) | ||
|
||
__author__ = 'DIANNA Team' | ||
__email__ = '[email protected]' | ||
__version__ = '1.3.0' | ||
__version__ = '1.4.0' | ||
|
||
|
||
def explain_timeseries(model_or_function, input_timeseries, method, labels, **kwargs): | ||
def explain_timeseries(model_or_function, input_timeseries, method, labels, | ||
**kwargs): | ||
"""Explain timeseries data given a model and a chosen method. | ||
Args: | ||
|
@@ -49,15 +49,13 @@ def explain_timeseries(model_or_function, input_timeseries, method, labels, **kw | |
""" | ||
explainer = _get_explainer(method, kwargs, modality='Timeseries') | ||
explain_timeseries_kwargs = utils.get_kwargs_applicable_to_function( | ||
explainer.explain, kwargs | ||
) | ||
explainer.explain, kwargs) | ||
for key in explain_timeseries_kwargs.keys(): | ||
kwargs.pop(key) | ||
if kwargs: | ||
warnings.warn(message = f'Please note the following kwargs are not being used: {kwargs}') | ||
return explainer.explain( | ||
model_or_function, input_timeseries, labels, **explain_timeseries_kwargs | ||
) | ||
raise TypeError(f'Error due to following unused kwargs: {kwargs}') | ||
return explainer.explain(model_or_function, input_timeseries, labels, | ||
**explain_timeseries_kwargs) | ||
|
||
|
||
def explain_image(model_or_function, input_image, method, labels, **kwargs): | ||
|
@@ -80,18 +78,17 @@ def explain_image(model_or_function, input_image, method, labels, **kwargs): | |
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 | ||
) | ||
explainer.explain, kwargs) | ||
for key in explain_image_kwargs.keys(): | ||
kwargs.pop(key) | ||
if kwargs: | ||
warnings.warn(message = f'Please note the following kwargs are not being used: {kwargs}') | ||
return explainer.explain( | ||
model_or_function, input_image, labels, **explain_image_kwargs | ||
) | ||
raise TypeError(f'Error due to following unused kwargs: {kwargs}') | ||
return explainer.explain(model_or_function, input_image, 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: | ||
|
@@ -109,12 +106,11 @@ def explain_text(model_or_function, input_text, tokenizer, method, labels, **kwa | |
""" | ||
explainer = _get_explainer(method, kwargs, modality='Text') | ||
explain_text_kwargs = utils.get_kwargs_applicable_to_function( | ||
explainer.explain, kwargs | ||
) | ||
explainer.explain, kwargs) | ||
for key in explain_text_kwargs.keys(): | ||
kwargs.pop(key) | ||
if kwargs: | ||
warnings.warn(message = f'Please note the following kwargs are not being used: {kwargs}') | ||
raise TypeError(f'Error due to following unused kwargs: {kwargs}') | ||
return explainer.explain( | ||
model_or_function=model_or_function, | ||
input_text=input_text, | ||
|
@@ -124,7 +120,11 @@ def explain_text(model_or_function, input_text, tokenizer, method, labels, **kwa | |
) | ||
|
||
|
||
def explain_tabular(model_or_function, input_tabular, method, labels=(1, ), **kwargs): | ||
def explain_tabular(model_or_function, | ||
input_tabular, | ||
method, | ||
labels=(1, ), | ||
**kwargs): | ||
"""Explain tabular (input_text) given a model and a chosen method. | ||
Args: | ||
|
@@ -140,24 +140,23 @@ def explain_tabular(model_or_function, input_tabular, method, labels=(1, ), **kw | |
""" | ||
explainer = _get_explainer(method, kwargs, modality='Tabular') | ||
explain_tabular_kwargs = utils.get_kwargs_applicable_to_function( | ||
explainer.explain, kwargs | ||
) | ||
explainer.explain, kwargs) | ||
for key in explain_tabular_kwargs.keys(): | ||
kwargs.pop(key) | ||
if kwargs: | ||
warnings.warn(message = f'Please note the following kwargs are not being used: {kwargs}') | ||
raise TypeError(f'Error due to following unused kwargs: {kwargs}') | ||
return explainer.explain( | ||
model_or_function=model_or_function, | ||
input_tabular=input_tabular, | ||
labels=labels, | ||
**explain_tabular_kwargs, | ||
) | ||
|
||
|
||
def _get_explainer(method, kwargs, modality): | ||
try: | ||
method_submodule = importlib.import_module( | ||
f'dianna.methods.{method.lower()}_{modality.lower()}' | ||
) | ||
f'dianna.methods.{method.lower()}_{modality.lower()}') | ||
except ImportError as err: | ||
raise ValueError( | ||
f'Method {method.lower()}_{modality.lower()} does not exist' | ||
|
@@ -169,8 +168,7 @@ def _get_explainer(method, kwargs, modality): | |
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 | ||
) | ||
method_class.__init__, kwargs) | ||
# Remove used kwargs from list of kwargs passed to the function. | ||
for key in method_kwargs.keys(): | ||
kwargs.pop(key) | ||
|
Binary file not shown.
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
Oops, something went wrong.