diff --git a/.circleci/config.yml b/.circleci/config.yml index e27e175c14..054228a38c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -38,6 +38,13 @@ commands: name: "Lint with black" command: black --check --diff . + isort: + description: "Check import order with isort" + steps: + - run: + name: "Check import order with isort" + command: isort --check-only + mypy_check: description: "Static type checking with mypy" steps: @@ -132,6 +139,7 @@ jobs: args: "-n -f" - lint_flake8 - lint_black + - isort - mypy_check - unit_tests - sphinx @@ -145,6 +153,7 @@ jobs: args: "-f" - lint_flake8 - lint_black + - isort - mypy_check - unit_tests - sphinx @@ -176,6 +185,7 @@ jobs: args: "-n" - lint_flake8 - lint_black + - isort - mypy_check - unit_tests - sphinx @@ -200,6 +210,7 @@ jobs: args: "-n -f -d" - lint_flake8 - lint_black + - isort - unit_tests - sphinx - configure_github_bot diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 0000000000..8bcfae5787 --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,7 @@ +[settings] +multi_line_output=3 +include_trailing_comma=True +force_grid_wrap=0 +use_parentheses=True +line_length=88 +known_third_party=pytext,torchvision,bs4 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 51d9190fec..f7c0cfcc52 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -35,7 +35,7 @@ pip using `pip install isort`, and run locally by calling ```bash isort ``` -from the repository root. No additional configuration should be needed. +from the repository root. Configuration for isort is located in .isort.cfg. We feel strongly that having a consistent code style is extremely important, so CircleCI will fail on your PR if it does not adhere to the black or flake8 formatting style or isort import ordering. diff --git a/captum/attr/__init__.py b/captum/attr/__init__.py index 72593cd06b..9b52914441 100644 --- a/captum/attr/__init__.py +++ b/captum/attr/__init__.py @@ -1,51 +1,52 @@ #!/usr/bin/env python3 -from ._core.integrated_gradients import IntegratedGradients # noqa from ._core.deep_lift import DeepLift, DeepLiftShap # noqa -from ._core.input_x_gradient import InputXGradient # noqa -from ._core.saliency import Saliency # noqa -from ._core.noise_tunnel import NoiseTunnel # noqa -from ._core.gradient_shap import GradientShap # noqa -from ._core.guided_backprop_deconvnet import GuidedBackprop, Deconvolution # noqa -from ._core.guided_grad_cam import GuidedGradCam # noqa from ._core.feature_ablation import FeatureAblation # noqa from ._core.feature_permutation import FeaturePermutation # noqa -from ._core.occlusion import Occlusion # noqa -from ._core.shapley_value import ShapleyValueSampling, ShapleyValues # noqa -from ._core.layer.layer_conductance import LayerConductance # noqa -from ._core.layer.layer_gradient_x_activation import LayerGradientXActivation # noqa -from ._core.layer.layer_activation import LayerActivation # noqa -from ._core.layer.internal_influence import InternalInfluence # noqa +from ._core.gradient_shap import GradientShap # noqa +from ._core.guided_backprop_deconvnet import Deconvolution # noqa +from ._core.guided_backprop_deconvnet import GuidedBackprop +from ._core.guided_grad_cam import GuidedGradCam # noqa +from ._core.input_x_gradient import InputXGradient # noqa +from ._core.integrated_gradients import IntegratedGradients # noqa from ._core.layer.grad_cam import LayerGradCam # noqa -from ._core.layer.layer_deep_lift import LayerDeepLift, LayerDeepLiftShap # noqa +from ._core.layer.internal_influence import InternalInfluence # noqa +from ._core.layer.layer_activation import LayerActivation # noqa +from ._core.layer.layer_conductance import LayerConductance # noqa +from ._core.layer.layer_deep_lift import LayerDeepLift # noqa +from ._core.layer.layer_deep_lift import LayerDeepLiftShap from ._core.layer.layer_gradient_shap import LayerGradientShap # noqa +from ._core.layer.layer_gradient_x_activation import LayerGradientXActivation # noqa from ._core.layer.layer_integrated_gradients import LayerIntegratedGradients # noqa -from ._core.neuron.neuron_feature_ablation import NeuronFeatureAblation # noqa from ._core.neuron.neuron_conductance import NeuronConductance # noqa +from ._core.neuron.neuron_deep_lift import NeuronDeepLift # noqa +from ._core.neuron.neuron_deep_lift import NeuronDeepLiftShap +from ._core.neuron.neuron_feature_ablation import NeuronFeatureAblation # noqa from ._core.neuron.neuron_gradient import NeuronGradient # noqa -from ._core.neuron.neuron_integrated_gradients import NeuronIntegratedGradients # noqa -from ._core.neuron.neuron_deep_lift import NeuronDeepLift, NeuronDeepLiftShap # noqa from ._core.neuron.neuron_gradient_shap import NeuronGradientShap # noqa -from ._core.neuron.neuron_guided_backprop_deconvnet import ( +from ._core.neuron.neuron_guided_backprop_deconvnet import ( # noqa NeuronDeconvolution, NeuronGuidedBackprop, -) # noqa - +) +from ._core.neuron.neuron_integrated_gradients import NeuronIntegratedGradients # noqa +from ._core.noise_tunnel import NoiseTunnel # noqa +from ._core.occlusion import Occlusion # noqa +from ._core.saliency import Saliency # noqa +from ._core.shapley_value import ShapleyValues, ShapleyValueSampling # noqa +from ._models.base import InterpretableEmbeddingBase # noqa from ._models.base import ( - InterpretableEmbeddingBase, TokenReferenceBase, configure_interpretable_embedding_layer, remove_interpretable_embedding_layer, -) # noqa +) +from ._utils import visualization # noqa from ._utils.attribution import Attribution # noqa from ._utils.attribution import GradientAttribution # noqa -from ._utils.attribution import PerturbationAttribution # noqa from ._utils.attribution import LayerAttribution # noqa from ._utils.attribution import NeuronAttribution # noqa -from ._utils import visualization # noqa -from ._utils.summarizer import Summarizer, CommonSummarizer -from ._utils.stat import Mean, StdDev, MSE, Var, Min, Max, Sum, Count - +from ._utils.attribution import PerturbationAttribution # noqa +from ._utils.stat import MSE, Count, Max, Mean, Min, StdDev, Sum, Var +from ._utils.summarizer import CommonSummarizer, Summarizer __all__ = [ "Attribution", diff --git a/captum/attr/_core/deep_lift.py b/captum/attr/_core/deep_lift.py index da51b91d9c..6b8def255f 100644 --- a/captum/attr/_core/deep_lift.py +++ b/captum/attr/_core/deep_lift.py @@ -1,8 +1,9 @@ #!/usr/bin/env python3 import typing -from typing import Tuple, Union, Any, List, Callable, cast - import warnings +from typing import Any, Callable, List, Tuple, Union, cast + +import numpy as np import torch import torch.nn as nn import torch.nn.functional as F @@ -10,32 +11,30 @@ from torch.nn import Module from torch.utils.hooks import RemovableHandle -import numpy as np - +from .._utils.attribution import GradientAttribution from .._utils.common import ( - _is_tuple, - _format_input, + ExpansionTypes, + _call_custom_attribution_func, + _compute_conv_delta_and_format_attrs, + _expand_additional_forward_args, + _expand_target, + _format_additional_forward_args, + _format_attributions, _format_baseline, _format_callable_baseline, - _format_attributions, + _format_input, _format_tensor_into_tuples, - _format_additional_forward_args, + _is_tuple, _run_forward, - _validate_input, - _expand_target, - _expand_additional_forward_args, _tensorize_baseline, - _call_custom_attribution_func, - _compute_conv_delta_and_format_attrs, - ExpansionTypes, + _validate_input, ) -from .._utils.attribution import GradientAttribution from .._utils.gradient import apply_gradient_requirements, undo_gradient_requirements from .._utils.typing import ( - TensorOrTupleOfTensorsGeneric, + BaselineType, Literal, TargetType, - BaselineType, + TensorOrTupleOfTensorsGeneric, ) diff --git a/captum/attr/_core/feature_ablation.py b/captum/attr/_core/feature_ablation.py index bda61daf0d..e886034542 100644 --- a/captum/attr/_core/feature_ablation.py +++ b/captum/attr/_core/feature_ablation.py @@ -1,24 +1,23 @@ #!/usr/bin/env python3 -import torch +from typing import Any, Callable, Tuple, Union, cast +import torch from torch import Tensor, dtype -from typing import Any, Callable, Tuple, Union, cast - +from .._utils.attribution import PerturbationAttribution from .._utils.common import ( + _expand_additional_forward_args, + _expand_target, _find_output_mode_and_verify, + _format_additional_forward_args, _format_attributions, _format_input, _format_input_baseline, _is_tuple, _run_forward, - _expand_additional_forward_args, - _expand_target, - _format_additional_forward_args, ) -from .._utils.attribution import PerturbationAttribution -from .._utils.typing import TensorOrTupleOfTensorsGeneric, TargetType, BaselineType +from .._utils.typing import BaselineType, TargetType, TensorOrTupleOfTensorsGeneric class FeatureAblation(PerturbationAttribution): diff --git a/captum/attr/_core/feature_permutation.py b/captum/attr/_core/feature_permutation.py index 7cfe2428bf..923a9ea9ba 100644 --- a/captum/attr/_core/feature_permutation.py +++ b/captum/attr/_core/feature_permutation.py @@ -4,8 +4,8 @@ import torch from torch import Tensor -from .feature_ablation import FeatureAblation from .._utils.typing import TargetType, TensorOrTupleOfTensorsGeneric +from .feature_ablation import FeatureAblation def _permute_feature(x: Tensor, feature_mask: Tensor) -> Tensor: diff --git a/captum/attr/_core/gradient_shap.py b/captum/attr/_core/gradient_shap.py index b591308c70..99f1b32e40 100644 --- a/captum/attr/_core/gradient_shap.py +++ b/captum/attr/_core/gradient_shap.py @@ -1,26 +1,25 @@ #!/usr/bin/env python3 -import torch +import typing +from typing import Any, Callable, Tuple, Union import numpy as np +import torch from .._utils.attribution import GradientAttribution from .._utils.common import ( - _is_tuple, - _format_input_baseline, - _format_callable_baseline, _compute_conv_delta_and_format_attrs, + _format_callable_baseline, + _format_input_baseline, + _is_tuple, ) - -from .noise_tunnel import NoiseTunnel -from typing import Any, Callable, Tuple, Union from .._utils.typing import ( - Tensor, - TensorOrTupleOfTensorsGeneric, + BaselineType, Literal, TargetType, - BaselineType, + Tensor, + TensorOrTupleOfTensorsGeneric, ) -import typing +from .noise_tunnel import NoiseTunnel class GradientShap(GradientAttribution): diff --git a/captum/attr/_core/guided_backprop_deconvnet.py b/captum/attr/_core/guided_backprop_deconvnet.py index 3755830235..623b5829f3 100644 --- a/captum/attr/_core/guided_backprop_deconvnet.py +++ b/captum/attr/_core/guided_backprop_deconvnet.py @@ -1,15 +1,15 @@ #!/usr/bin/env python3 import warnings +from typing import Any, List, Tuple, Union + import torch import torch.nn.functional as F -from typing import Any, List, Union, Tuple - from torch import Tensor from torch.nn import Module from torch.utils.hooks import RemovableHandle from .._utils.attribution import GradientAttribution -from .._utils.common import _format_input, _format_attributions, _is_tuple +from .._utils.common import _format_attributions, _format_input, _is_tuple from .._utils.gradient import apply_gradient_requirements, undo_gradient_requirements from .._utils.typing import TargetType, TensorOrTupleOfTensorsGeneric diff --git a/captum/attr/_core/guided_grad_cam.py b/captum/attr/_core/guided_grad_cam.py index d4aeaa8647..d7e105969a 100644 --- a/captum/attr/_core/guided_grad_cam.py +++ b/captum/attr/_core/guided_grad_cam.py @@ -1,17 +1,16 @@ #!/usr/bin/env python3 -import torch import warnings +from typing import Any, List, Union -from .._utils.attribution import GradientAttribution, LayerAttribution -from .._utils.common import _format_input, _format_attributions, _is_tuple - -from .layer.grad_cam import LayerGradCam -from .guided_backprop_deconvnet import GuidedBackprop - -from torch.nn import Module +import torch from torch import Tensor -from typing import Any, List, Union +from torch.nn import Module + +from .._utils.attribution import GradientAttribution, LayerAttribution +from .._utils.common import _format_attributions, _format_input, _is_tuple from .._utils.typing import TargetType, TensorOrTupleOfTensorsGeneric +from .guided_backprop_deconvnet import GuidedBackprop +from .layer.grad_cam import LayerGradCam class GuidedGradCam(GradientAttribution): diff --git a/captum/attr/_core/input_x_gradient.py b/captum/attr/_core/input_x_gradient.py index 801740dafc..6246f84162 100644 --- a/captum/attr/_core/input_x_gradient.py +++ b/captum/attr/_core/input_x_gradient.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 from typing import Any, Callable -from .._utils.common import _format_input, _format_attributions, _is_tuple from .._utils.attribution import GradientAttribution +from .._utils.common import _format_attributions, _format_input, _is_tuple from .._utils.gradient import apply_gradient_requirements, undo_gradient_requirements from .._utils.typing import TargetType, TensorOrTupleOfTensorsGeneric diff --git a/captum/attr/_core/integrated_gradients.py b/captum/attr/_core/integrated_gradients.py index b11138397a..5a26183adb 100644 --- a/captum/attr/_core/integrated_gradients.py +++ b/captum/attr/_core/integrated_gradients.py @@ -6,23 +6,23 @@ from torch import Tensor from .._utils.approximation_methods import approximation_parameters +from .._utils.attribution import GradientAttribution from .._utils.batching import _batched_operator from .._utils.common import ( - _validate_input, + _expand_additional_forward_args, + _expand_target, _format_additional_forward_args, _format_attributions, _format_input_baseline, - _reshape_and_sum, - _expand_additional_forward_args, - _expand_target, _is_tuple, + _reshape_and_sum, + _validate_input, ) -from .._utils.attribution import GradientAttribution from .._utils.typing import ( - TensorOrTupleOfTensorsGeneric, + BaselineType, Literal, TargetType, - BaselineType, + TensorOrTupleOfTensorsGeneric, ) diff --git a/captum/attr/_core/layer/grad_cam.py b/captum/attr/_core/layer/grad_cam.py index 3bdb93fdcc..f62473020e 100644 --- a/captum/attr/_core/layer/grad_cam.py +++ b/captum/attr/_core/layer/grad_cam.py @@ -1,19 +1,20 @@ #!/usr/bin/env python3 -from typing import Callable, List, Tuple, Union, Any +from typing import Any, Callable, List, Tuple, Union + import torch +import torch.nn.functional as F from torch import Tensor from torch.nn import Module -import torch.nn.functional as F -from ..._utils.attribution import LayerAttribution, GradientAttribution +from ..._utils.attribution import GradientAttribution, LayerAttribution from ..._utils.common import ( - _format_input, _format_additional_forward_args, _format_attributions, + _format_input, ) from ..._utils.gradient import ( - compute_layer_gradients_and_eval, apply_gradient_requirements, + compute_layer_gradients_and_eval, undo_gradient_requirements, ) from ..._utils.typing import TargetType diff --git a/captum/attr/_core/layer/internal_influence.py b/captum/attr/_core/layer/internal_influence.py index b5727b1e51..d294551e4c 100644 --- a/captum/attr/_core/layer/internal_influence.py +++ b/captum/attr/_core/layer/internal_influence.py @@ -1,24 +1,24 @@ #!/usr/bin/env python3 -from typing import Callable, List, Tuple, Union, Any +from typing import Any, Callable, List, Tuple, Union import torch from torch import Tensor from torch.nn import Module from ..._utils.approximation_methods import approximation_parameters -from ..._utils.attribution import LayerAttribution, GradientAttribution +from ..._utils.attribution import GradientAttribution, LayerAttribution from ..._utils.batching import _batched_operator from ..._utils.common import ( - _reshape_and_sum, - _format_input_baseline, - _validate_input, - _format_additional_forward_args, - _format_attributions, _expand_additional_forward_args, _expand_target, + _format_additional_forward_args, + _format_attributions, + _format_input_baseline, + _reshape_and_sum, + _validate_input, ) from ..._utils.gradient import compute_layer_gradients_and_eval -from ..._utils.typing import TargetType, BaselineType +from ..._utils.typing import BaselineType, TargetType class InternalInfluence(LayerAttribution, GradientAttribution): diff --git a/captum/attr/_core/layer/layer_activation.py b/captum/attr/_core/layer/layer_activation.py index 28ffc4de49..b95349cab0 100644 --- a/captum/attr/_core/layer/layer_activation.py +++ b/captum/attr/_core/layer/layer_activation.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from typing import Callable, List, Tuple, Union, Any +from typing import Any, Callable, List, Tuple, Union import torch from torch import Tensor diff --git a/captum/attr/_core/layer/layer_conductance.py b/captum/attr/_core/layer/layer_conductance.py index baa7aced74..ad3284877e 100644 --- a/captum/attr/_core/layer/layer_conductance.py +++ b/captum/attr/_core/layer/layer_conductance.py @@ -1,23 +1,25 @@ #!/usr/bin/env python3 +import typing +from typing import Any, Callable, List, Tuple, Union + import torch from torch import Tensor from torch.nn import Module -import typing -from typing import Any, Callable, List, Tuple, Union + from ..._utils.approximation_methods import approximation_parameters -from ..._utils.attribution import LayerAttribution, GradientAttribution +from ..._utils.attribution import GradientAttribution, LayerAttribution from ..._utils.batching import _batched_operator from ..._utils.common import ( - _reshape_and_sum, - _format_input_baseline, + _expand_additional_forward_args, + _expand_target, _format_additional_forward_args, _format_attributions, - _expand_additional_forward_args, + _format_input_baseline, + _reshape_and_sum, _validate_input, - _expand_target, ) from ..._utils.gradient import compute_layer_gradients_and_eval -from ..._utils.typing import Literal, TargetType, BaselineType +from ..._utils.typing import BaselineType, Literal, TargetType class LayerConductance(LayerAttribution, GradientAttribution): diff --git a/captum/attr/_core/layer/layer_deep_lift.py b/captum/attr/_core/layer/layer_deep_lift.py index c617ceb106..2267800319 100644 --- a/captum/attr/_core/layer/layer_deep_lift.py +++ b/captum/attr/_core/layer/layer_deep_lift.py @@ -1,44 +1,36 @@ #!/usr/bin/env python3 import typing -from typing import ( - Tuple, - Union, - Any, - Callable, - Sequence, - cast, -) +from typing import Any, Callable, Sequence, Tuple, Union, cast import torch from torch import Tensor from torch.nn import Module -from ..._utils.attribution import LayerAttribution from ..._core.deep_lift import DeepLift, DeepLiftShap -from ..._utils.gradient import ( - apply_gradient_requirements, - undo_gradient_requirements, - compute_layer_gradients_and_eval, -) - +from ..._utils.attribution import LayerAttribution from ..._utils.common import ( - _format_input, - _format_baseline, - _format_callable_baseline, - _format_additional_forward_args, - _validate_input, - _tensorize_baseline, + ExpansionTypes, _call_custom_attribution_func, _compute_conv_delta_and_format_attrs, _expand_additional_forward_args, _expand_target, - ExpansionTypes, + _format_additional_forward_args, + _format_baseline, + _format_callable_baseline, + _format_input, + _tensorize_baseline, + _validate_input, +) +from ..._utils.gradient import ( + apply_gradient_requirements, + compute_layer_gradients_and_eval, + undo_gradient_requirements, ) from ..._utils.typing import ( - TensorOrTupleOfTensorsGeneric, + BaselineType, Literal, TargetType, - BaselineType, + TensorOrTupleOfTensorsGeneric, ) diff --git a/captum/attr/_core/layer/layer_feature_ablation.py b/captum/attr/_core/layer/layer_feature_ablation.py index 96618aa785..7c5ad388a7 100644 --- a/captum/attr/_core/layer/layer_feature_ablation.py +++ b/captum/attr/_core/layer/layer_feature_ablation.py @@ -1,21 +1,21 @@ #!/usr/bin/env python3 +from typing import Any, Callable, List, Tuple, Union + import torch -from torch.nn.parallel.scatter_gather import scatter from torch import Tensor from torch.nn import Module - -from typing import Callable, List, Tuple, Union, Any +from torch.nn.parallel.scatter_gather import scatter from ..._utils.attribution import LayerAttribution, PerturbationAttribution from ..._utils.common import ( - _format_input, + _extract_device, _format_additional_forward_args, _format_attributions, + _format_input, _run_forward, - _extract_device, ) from ..._utils.gradient import _forward_layer_eval -from ..._utils.typing import TargetType, BaselineType +from ..._utils.typing import BaselineType, TargetType from ..feature_ablation import FeatureAblation diff --git a/captum/attr/_core/layer/layer_gradient_shap.py b/captum/attr/_core/layer/layer_gradient_shap.py index 4b5daa8ef1..1a84c4a5f5 100644 --- a/captum/attr/_core/layer/layer_gradient_shap.py +++ b/captum/attr/_core/layer/layer_gradient_shap.py @@ -1,29 +1,22 @@ #!/usr/bin/env python3 -import torch -from torch import Tensor -from torch.nn import Module - import typing -from typing import Callable, List, Tuple, Union, Any +from typing import Any, Callable, List, Tuple, Union import numpy as np +import torch +from torch import Tensor +from torch.nn import Module -from ..._utils.attribution import LayerAttribution, GradientAttribution -from ..._utils.gradient import compute_layer_gradients_and_eval, _forward_layer_eval - -from ..gradient_shap import _scale_input +from ..._utils.attribution import GradientAttribution, LayerAttribution from ..._utils.common import ( - _format_input_baseline, - _format_callable_baseline, _compute_conv_delta_and_format_attrs, + _format_callable_baseline, + _format_input_baseline, ) -from ..._utils.typing import ( - TensorOrTupleOfTensorsGeneric, - Literal, - TargetType, -) - +from ..._utils.gradient import _forward_layer_eval, compute_layer_gradients_and_eval +from ..._utils.typing import Literal, TargetType, TensorOrTupleOfTensorsGeneric +from ..gradient_shap import _scale_input from ..noise_tunnel import NoiseTunnel diff --git a/captum/attr/_core/layer/layer_integrated_gradients.py b/captum/attr/_core/layer/layer_integrated_gradients.py index ccc722de72..56e0327b08 100644 --- a/captum/attr/_core/layer/layer_integrated_gradients.py +++ b/captum/attr/_core/layer/layer_integrated_gradients.py @@ -1,27 +1,24 @@ #!/usr/bin/env python3 import typing -from typing import Callable, List, Tuple, Union, Any +from typing import Any, Callable, List, Tuple, Union + import torch from torch import Tensor - from torch.nn import Module from torch.nn.parallel.scatter_gather import scatter +from captum.attr._core.integrated_gradients import IntegratedGradients +from captum.attr._utils.attribution import GradientAttribution, LayerAttribution from captum.attr._utils.common import ( - _tensorize_baseline, - _validate_input, + _extract_device, _format_additional_forward_args, _format_attributions, _format_input_baseline, - _extract_device, + _tensorize_baseline, + _validate_input, ) - -from captum.attr._utils.gradient import _forward_layer_eval - -from captum.attr._utils.attribution import LayerAttribution, GradientAttribution -from captum.attr._core.integrated_gradients import IntegratedGradients -from captum.attr._utils.gradient import _run_forward -from captum.attr._utils.typing import Literal, TargetType, BaselineType +from captum.attr._utils.gradient import _forward_layer_eval, _run_forward +from captum.attr._utils.typing import BaselineType, Literal, TargetType class LayerIntegratedGradients(LayerAttribution, GradientAttribution): diff --git a/captum/attr/_core/neuron/neuron_conductance.py b/captum/attr/_core/neuron/neuron_conductance.py index e7aec753da..5cecbbfe49 100644 --- a/captum/attr/_core/neuron/neuron_conductance.py +++ b/captum/attr/_core/neuron/neuron_conductance.py @@ -1,23 +1,25 @@ #!/usr/bin/env python3 +from typing import Any, Callable, List, Tuple, Union + import torch -from typing import Callable, List, Tuple, Union, Any from torch.nn import Module + from ..._utils.approximation_methods import approximation_parameters -from ..._utils.attribution import NeuronAttribution, GradientAttribution +from ..._utils.attribution import GradientAttribution, NeuronAttribution from ..._utils.batching import _batched_operator from ..._utils.common import ( + _expand_additional_forward_args, + _expand_target, + _format_additional_forward_args, + _format_attributions, + _format_input_baseline, _is_tuple, _reshape_and_sum, - _format_input_baseline, - _format_additional_forward_args, _validate_input, - _format_attributions, - _expand_additional_forward_args, - _expand_target, _verify_select_column, ) from ..._utils.gradient import compute_layer_gradients_and_eval -from ..._utils.typing import TensorOrTupleOfTensorsGeneric, TargetType, BaselineType +from ..._utils.typing import BaselineType, TargetType, TensorOrTupleOfTensorsGeneric class NeuronConductance(NeuronAttribution, GradientAttribution): diff --git a/captum/attr/_core/neuron/neuron_deep_lift.py b/captum/attr/_core/neuron/neuron_deep_lift.py index 9df695bc72..0d7bd1b0ca 100644 --- a/captum/attr/_core/neuron/neuron_deep_lift.py +++ b/captum/attr/_core/neuron/neuron_deep_lift.py @@ -1,13 +1,13 @@ #!/usr/bin/env python3 -from ..._utils.attribution import NeuronAttribution, GradientAttribution -from ..._utils.gradient import construct_neuron_grad_fn - -from ..deep_lift import DeepLift, DeepLiftShap +from typing import Any, Callable, Tuple, Union, cast -from typing import Callable, Tuple, Union, Any, cast from torch import Tensor from torch.nn import Module -from ..._utils.typing import TensorOrTupleOfTensorsGeneric, BaselineType + +from ..._utils.attribution import GradientAttribution, NeuronAttribution +from ..._utils.gradient import construct_neuron_grad_fn +from ..._utils.typing import BaselineType, TensorOrTupleOfTensorsGeneric +from ..deep_lift import DeepLift, DeepLiftShap class NeuronDeepLift(NeuronAttribution, GradientAttribution): diff --git a/captum/attr/_core/neuron/neuron_feature_ablation.py b/captum/attr/_core/neuron/neuron_feature_ablation.py index a51718c2aa..481e5b7ade 100644 --- a/captum/attr/_core/neuron/neuron_feature_ablation.py +++ b/captum/attr/_core/neuron/neuron_feature_ablation.py @@ -1,14 +1,13 @@ #!/usr/bin/env python3 +from typing import Any, Callable, List, Tuple, Union + import torch from torch.nn import Module -from typing import Callable, List, Tuple, Union, Any - from ..._utils.attribution import NeuronAttribution, PerturbationAttribution from ..._utils.common import _verify_select_column from ..._utils.gradient import _forward_layer_eval -from ..._utils.typing import TensorOrTupleOfTensorsGeneric, BaselineType - +from ..._utils.typing import BaselineType, TensorOrTupleOfTensorsGeneric from ..feature_ablation import FeatureAblation diff --git a/captum/attr/_core/neuron/neuron_gradient.py b/captum/attr/_core/neuron/neuron_gradient.py index 11bea817b3..f148c95a8c 100644 --- a/captum/attr/_core/neuron/neuron_gradient.py +++ b/captum/attr/_core/neuron/neuron_gradient.py @@ -1,18 +1,19 @@ #!/usr/bin/env python3 +from typing import Any, Callable, List, Tuple, Union + from torch.nn import Module -from typing import Callable, List, Tuple, Union, Any -from ..._utils.attribution import NeuronAttribution, GradientAttribution +from ..._utils.attribution import GradientAttribution, NeuronAttribution from ..._utils.common import ( - _is_tuple, - _format_input, _format_additional_forward_args, _format_attributions, + _format_input, + _is_tuple, ) from ..._utils.gradient import ( + _forward_layer_eval_with_neuron_grads, apply_gradient_requirements, undo_gradient_requirements, - _forward_layer_eval_with_neuron_grads, ) from ..._utils.typing import TensorOrTupleOfTensorsGeneric diff --git a/captum/attr/_core/neuron/neuron_gradient_shap.py b/captum/attr/_core/neuron/neuron_gradient_shap.py index b3e6405c99..fdeef0a620 100644 --- a/captum/attr/_core/neuron/neuron_gradient_shap.py +++ b/captum/attr/_core/neuron/neuron_gradient_shap.py @@ -1,11 +1,12 @@ #!/usr/bin/env python3 -from typing import Callable, List, Tuple, Union, Any +from typing import Any, Callable, List, Tuple, Union + from torch.nn import Module -from ..gradient_shap import GradientShap -from ..._utils.attribution import NeuronAttribution, GradientAttribution +from ..._utils.attribution import GradientAttribution, NeuronAttribution from ..._utils.gradient import construct_neuron_grad_fn from ..._utils.typing import TensorOrTupleOfTensorsGeneric +from ..gradient_shap import GradientShap class NeuronGradientShap(NeuronAttribution, GradientAttribution): diff --git a/captum/attr/_core/neuron/neuron_guided_backprop_deconvnet.py b/captum/attr/_core/neuron/neuron_guided_backprop_deconvnet.py index bfc554910b..6c71c2ea0e 100644 --- a/captum/attr/_core/neuron/neuron_guided_backprop_deconvnet.py +++ b/captum/attr/_core/neuron/neuron_guided_backprop_deconvnet.py @@ -3,10 +3,10 @@ from torch.nn import Module -from ..._utils.attribution import NeuronAttribution, GradientAttribution +from ..._utils.attribution import GradientAttribution, NeuronAttribution from ..._utils.gradient import construct_neuron_grad_fn from ..._utils.typing import TensorOrTupleOfTensorsGeneric -from ..guided_backprop_deconvnet import GuidedBackprop, Deconvolution +from ..guided_backprop_deconvnet import Deconvolution, GuidedBackprop class NeuronDeconvolution(NeuronAttribution, GradientAttribution): diff --git a/captum/attr/_core/neuron/neuron_integrated_gradients.py b/captum/attr/_core/neuron/neuron_integrated_gradients.py index 8a04fc13d2..5989ee7a14 100644 --- a/captum/attr/_core/neuron/neuron_integrated_gradients.py +++ b/captum/attr/_core/neuron/neuron_integrated_gradients.py @@ -1,12 +1,12 @@ #!/usr/bin/env python3 -from typing import Callable, List, Tuple, Union, Any +from typing import Any, Callable, List, Tuple, Union + from torch import Tensor from torch.nn import Module -from ..._utils.attribution import NeuronAttribution, GradientAttribution +from ..._utils.attribution import GradientAttribution, NeuronAttribution from ..._utils.gradient import construct_neuron_grad_fn from ..._utils.typing import TensorOrTupleOfTensorsGeneric - from ..integrated_gradients import IntegratedGradients diff --git a/captum/attr/_core/noise_tunnel.py b/captum/attr/_core/noise_tunnel.py index 47162227d4..777049f16a 100644 --- a/captum/attr/_core/noise_tunnel.py +++ b/captum/attr/_core/noise_tunnel.py @@ -1,25 +1,24 @@ #!/usr/bin/env python3 -from typing import Tuple, Union, Any +from enum import Enum +from typing import Any, Tuple, Union +import numpy as np import torch from torch import Tensor -import numpy as np -from enum import Enum - from .._utils.attribution import Attribution from .._utils.common import ( - _is_tuple, - _validate_noise_tunnel_type, - _validate_input, + ExpansionTypes, + _expand_additional_forward_args, + _expand_target, + _format_additional_forward_args, + _format_attributions, _format_baseline, _format_input, - _format_attributions, - _format_additional_forward_args, _format_tensor_into_tuples, - _expand_additional_forward_args, - _expand_target, - ExpansionTypes, + _is_tuple, + _validate_input, + _validate_noise_tunnel_type, ) diff --git a/captum/attr/_core/occlusion.py b/captum/attr/_core/occlusion.py index 28cb32248a..3b8eb7bc6e 100644 --- a/captum/attr/_core/occlusion.py +++ b/captum/attr/_core/occlusion.py @@ -1,17 +1,16 @@ #!/usr/bin/env python3 from typing import Any, Callable, Tuple, Union +import numpy as np import torch from torch import Tensor -import numpy as np from .._utils.common import ( - _format_input, - _format_and_verify_strides, _format_and_verify_sliding_window_shapes, + _format_and_verify_strides, + _format_input, ) -from .._utils.typing import TensorOrTupleOfTensorsGeneric, TargetType, BaselineType - +from .._utils.typing import BaselineType, TargetType, TensorOrTupleOfTensorsGeneric from .feature_ablation import FeatureAblation diff --git a/captum/attr/_core/saliency.py b/captum/attr/_core/saliency.py index 0b3a5d7845..b679358b1a 100644 --- a/captum/attr/_core/saliency.py +++ b/captum/attr/_core/saliency.py @@ -3,6 +3,7 @@ from typing import Any, Callable import torch + from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric from .._utils.attribution import GradientAttribution diff --git a/captum/attr/_core/shapley_value.py b/captum/attr/_core/shapley_value.py index 0d38cb2f37..630ebc56d3 100644 --- a/captum/attr/_core/shapley_value.py +++ b/captum/attr/_core/shapley_value.py @@ -1,26 +1,26 @@ #!/usr/bin/env python3 +import itertools import warnings -from typing import Any, Callable, Tuple, Union, Iterable, Sequence +from typing import Any, Callable, Iterable, Sequence, Tuple, Union -import itertools import torch from torch import Tensor +from .._utils.attribution import PerturbationAttribution from .._utils.common import ( + _expand_additional_forward_args, + _expand_target, _find_output_mode_and_verify, + _format_additional_forward_args, _format_attributions, _format_input, _format_input_baseline, _is_tuple, _run_forward, - _expand_additional_forward_args, - _expand_target, - _format_additional_forward_args, _tensorize_baseline, ) -from .._utils.attribution import PerturbationAttribution -from .._utils.typing import TensorOrTupleOfTensorsGeneric, TargetType, BaselineType +from .._utils.typing import BaselineType, TargetType, TensorOrTupleOfTensorsGeneric def _all_perm_generator(num_features: int, num_samples: int) -> Iterable[Sequence[int]]: diff --git a/captum/attr/_models/base.py b/captum/attr/_models/base.py index a1199b5c58..e3852a3356 100644 --- a/captum/attr/_models/base.py +++ b/captum/attr/_models/base.py @@ -1,10 +1,9 @@ #!/usr/bin/env python3 -import torch - import warnings - from functools import reduce + +import torch from torch.nn import Module diff --git a/captum/attr/_models/pytext.py b/captum/attr/_models/pytext.py index 21747501a3..61cba7e64f 100644 --- a/captum/attr/_models/pytext.py +++ b/captum/attr/_models/pytext.py @@ -2,10 +2,9 @@ from collections import defaultdict import torch - -from pytext.models.model import EmbeddingList, EmbeddingBase -from pytext.models.embeddings.word_embedding import WordEmbedding from pytext.models.embeddings.dict_embedding import DictEmbedding +from pytext.models.embeddings.word_embedding import WordEmbedding +from pytext.models.model import EmbeddingBase, EmbeddingList class PyTextInterpretableEmbedding(EmbeddingBase): diff --git a/captum/attr/_utils/approximation_methods.py b/captum/attr/_utils/approximation_methods.py index 4977afc054..3d865f87fb 100644 --- a/captum/attr/_utils/approximation_methods.py +++ b/captum/attr/_utils/approximation_methods.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 -from typing import List, Callable, Tuple -import numpy as np from enum import Enum +from typing import Callable, List, Tuple + +import numpy as np class Riemann(Enum): diff --git a/captum/attr/_utils/attribution.py b/captum/attr/_utils/attribution.py index e77c5f275f..2f8a6806e8 100644 --- a/captum/attr/_utils/attribution.py +++ b/captum/attr/_utils/attribution.py @@ -1,8 +1,10 @@ #!/usr/bin/env python3 -from typing import Callable, Union, Tuple, Any, Type, List, cast +from typing import Any, Callable, List, Tuple, Type, Union, cast import torch import torch.nn.functional as F +from torch import Tensor +from torch.nn import Module from .common import ( _format_additional_forward_args, @@ -16,9 +18,6 @@ from .gradient import compute_gradients from .typing import TargetType -from torch import Tensor -from torch.nn import Module - class Attribution: r""" diff --git a/captum/attr/_utils/batching.py b/captum/attr/_utils/batching.py index 828778cbfa..95f73d0057 100644 --- a/captum/attr/_utils/batching.py +++ b/captum/attr/_utils/batching.py @@ -1,15 +1,15 @@ #!/usr/bin/env python3 import typing -from typing import Any, Callable, List, Tuple, Union, Iterator, Dict +from typing import Any, Callable, Dict, Iterator, List, Tuple, Union import torch from torch import Tensor, device -from .common import _format_input, _format_additional_forward_args +from .common import _format_additional_forward_args, _format_input from .typing import ( + TargetType, TensorOrTupleOfTensorsGeneric, TupleOrTensorOrBoolGeneric, - TargetType, ) diff --git a/captum/attr/_utils/common.py b/captum/attr/_utils/common.py index c9c0d75022..b422a9a6b3 100644 --- a/captum/attr/_utils/common.py +++ b/captum/attr/_utils/common.py @@ -1,25 +1,15 @@ #!/usr/bin/env python3 import typing -from typing import ( - Tuple, - Union, - overload, - List, - Any, - TYPE_CHECKING, - Callable, - cast, -) +from enum import Enum +from inspect import signature +from typing import TYPE_CHECKING, Any, Callable, List, Tuple, Union, cast, overload import torch from torch import Tensor, device from torch.nn import Module -from enum import Enum -from inspect import signature - from .approximation_methods import SUPPORTED_METHODS -from .typing import TensorOrTupleOfTensorsGeneric, Literal, TargetType, BaselineType +from .typing import BaselineType, Literal, TargetType, TensorOrTupleOfTensorsGeneric if TYPE_CHECKING: from .attribution import GradientAttribution diff --git a/captum/attr/_utils/gradient.py b/captum/attr/_utils/gradient.py index ea96103ff9..e89bf54263 100644 --- a/captum/attr/_utils/gradient.py +++ b/captum/attr/_utils/gradient.py @@ -1,23 +1,16 @@ #!/usr/bin/env python3 import threading -import warnings import typing -from typing import ( - Any, - List, - Tuple, - Union, - Callable, - Dict, - cast, -) +import warnings +from typing import Any, Callable, Dict, List, Tuple, Union, cast import torch from torch import Tensor, device from torch.nn import Module -from .typing import Literal, TargetType, TensorOrTupleOfTensorsGeneric + from .batching import _reduce_list, _sort_key_list from .common import _run_forward, _verify_select_column +from .typing import Literal, TargetType, TensorOrTupleOfTensorsGeneric def apply_gradient_requirements(inputs: Tuple[Tensor, ...]) -> List[bool]: diff --git a/captum/attr/_utils/typing.py b/captum/attr/_utils/typing.py index ce0021896b..99d0c82c18 100644 --- a/captum/attr/_utils/typing.py +++ b/captum/attr/_utils/typing.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 -from typing import Tuple, TypeVar, TYPE_CHECKING, Union, List +from typing import TYPE_CHECKING, List, Tuple, TypeVar, Union + from torch import Tensor if TYPE_CHECKING: diff --git a/captum/attr/_utils/visualization.py b/captum/attr/_utils/visualization.py index fa21476d3c..8f0b52965c 100644 --- a/captum/attr/_utils/visualization.py +++ b/captum/attr/_utils/visualization.py @@ -1,16 +1,15 @@ #!/usr/bin/env python3 -from typing import Iterable, Union, Tuple, Any, List - -from enum import Enum -import numpy as np -from numpy import ndarray import warnings +from enum import Enum +from typing import Any, Iterable, List, Tuple, Union +import numpy as np from matplotlib import pyplot as plt -from matplotlib.pyplot import figure, axis -from matplotlib.figure import Figure from matplotlib.colors import LinearSegmentedColormap +from matplotlib.figure import Figure +from matplotlib.pyplot import axis, figure from mpl_toolkits.axes_grid1 import make_axes_locatable +from numpy import ndarray try: from IPython.core.display import display, HTML diff --git a/captum/insights/api.py b/captum/insights/api.py index 77931b1397..dd11be2c2d 100644 --- a/captum/insights/api.py +++ b/captum/insights/api.py @@ -502,7 +502,13 @@ def _get_outputs(self) -> List[Tuple[VisualizationOutput, SampleCache]]: batch_data = next(self._dataset_iter) vis_outputs = [] - for inputs, additional_forward_args, label in _batched_generator( + # Type ignore for issue with passing union to function taking generic + # https://github.com/python/mypy/issues/1533 + for ( + inputs, + additional_forward_args, + label, + ) in _batched_generator( # type: ignore inputs=batch_data.inputs, additional_forward_args=batch_data.additional_args, target_ind=batch_data.labels, diff --git a/captum/insights/widget/widget.py b/captum/insights/widget/widget.py index a64150f052..c7a92cd7c8 100644 --- a/captum/insights/widget/widget.py +++ b/captum/insights/widget/widget.py @@ -1,8 +1,9 @@ #!/usr/bin/env python3 import ipywidgets as widgets +from traitlets import Dict, Instance, List, Unicode, observe + from captum.insights import AttributionVisualizer from captum.insights.server import namedtuple_to_dict -from traitlets import Dict, Instance, List, Unicode, observe @widgets.register diff --git a/scripts/install_via_conda.sh b/scripts/install_via_conda.sh index beb7741b15..57708ebb51 100755 --- a/scripts/install_via_conda.sh +++ b/scripts/install_via_conda.sh @@ -38,7 +38,7 @@ fi # install other deps conda install -y numpy sphinx pytest flake8 ipywidgets ipython -conda install -y -c conda-forge black matplotlib pytest-cov sphinx-autodoc-typehints mypy flask +conda install -y -c conda-forge black matplotlib pytest-cov sphinx-autodoc-typehints mypy flask isort # build insights and install captum # TODO: remove CI=false when we want React warnings treated as errors diff --git a/scripts/parse_sphinx.py b/scripts/parse_sphinx.py index 37511d241f..2256a89403 100644 --- a/scripts/parse_sphinx.py +++ b/scripts/parse_sphinx.py @@ -5,7 +5,6 @@ from bs4 import BeautifulSoup - js_scripts = """ diff --git a/scripts/parse_tutorials.py b/scripts/parse_tutorials.py index 085585400a..a93925e7fa 100644 --- a/scripts/parse_tutorials.py +++ b/scripts/parse_tutorials.py @@ -8,7 +8,6 @@ from bs4 import BeautifulSoup from nbconvert import HTMLExporter, ScriptExporter - TEMPLATE = """const CWD = process.cwd(); const React = require('react'); diff --git a/scripts/update_versions_html.py b/scripts/update_versions_html.py index 1d64e0c0bb..80785fe2a2 100644 --- a/scripts/update_versions_html.py +++ b/scripts/update_versions_html.py @@ -5,7 +5,6 @@ from bs4 import BeautifulSoup - BASE_URL = "/" diff --git a/setup.py b/setup.py index 09b741e7ca..64bdeee19a 100755 --- a/setup.py +++ b/setup.py @@ -15,7 +15,6 @@ from setuptools import find_packages, setup - REQUIRED_MAJOR = 3 REQUIRED_MINOR = 6 diff --git a/tests/attr/helpers/conductance_reference.py b/tests/attr/helpers/conductance_reference.py index 73cc91e858..96ce5a6187 100644 --- a/tests/attr/helpers/conductance_reference.py +++ b/tests/attr/helpers/conductance_reference.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import numpy as np import torch + from captum.attr._utils.approximation_methods import approximation_parameters from captum.attr._utils.attribution import LayerAttribution from captum.attr._utils.common import _reshape_and_sum diff --git a/tests/attr/layer/test_grad_cam.py b/tests/attr/layer/test_grad_cam.py index f7d1214dc5..a8b07d405f 100644 --- a/tests/attr/layer/test_grad_cam.py +++ b/tests/attr/layer/test_grad_cam.py @@ -1,15 +1,16 @@ #!/usr/bin/env python3 import unittest -from typing import List, Tuple, Union, Any +from typing import Any, List, Tuple, Union import torch from torch import Tensor from torch.nn import Module + from captum.attr._core.layer.grad_cam import LayerGradCam -from ..helpers.basic_models import BasicModel_MultiLayer, BasicModel_ConvNet_One_Conv -from ..helpers.utils import assertTensorTuplesAlmostEqual, BaseTest +from ..helpers.basic_models import BasicModel_ConvNet_One_Conv, BasicModel_MultiLayer +from ..helpers.utils import BaseTest, assertTensorTuplesAlmostEqual class Test(BaseTest): diff --git a/tests/attr/layer/test_internal_influence.py b/tests/attr/layer/test_internal_influence.py index c201a503ef..04dd86aa30 100644 --- a/tests/attr/layer/test_internal_influence.py +++ b/tests/attr/layer/test_internal_influence.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 -from typing import Union, Tuple, List, Any - import unittest +from typing import Any, List, Tuple, Union import torch from torch import Tensor @@ -14,7 +13,7 @@ BasicModel_MultiLayer, BasicModel_MultiLayer_MultiInput, ) -from ..helpers.utils import assertTensorTuplesAlmostEqual, BaseTest +from ..helpers.utils import BaseTest, assertTensorTuplesAlmostEqual class Test(BaseTest): diff --git a/tests/attr/layer/test_layer_ablation.py b/tests/attr/layer/test_layer_ablation.py index 476df97362..670b89d319 100644 --- a/tests/attr/layer/test_layer_ablation.py +++ b/tests/attr/layer/test_layer_ablation.py @@ -1,11 +1,12 @@ #!/usr/bin/env python3 import unittest -from typing import List, Tuple, Union, Any +from typing import Any, List, Tuple, Union import torch from torch import Tensor from torch.nn import Module + from captum.attr._core.layer.layer_feature_ablation import LayerFeatureAblation from captum.attr._utils.typing import BaselineType @@ -14,7 +15,7 @@ BasicModel_MultiLayer, BasicModel_MultiLayer_MultiInput, ) -from ..helpers.utils import assertTensorTuplesAlmostEqual, BaseTest +from ..helpers.utils import BaseTest, assertTensorTuplesAlmostEqual class Test(BaseTest): diff --git a/tests/attr/layer/test_layer_activation.py b/tests/attr/layer/test_layer_activation.py index 56f1e02db6..5d9626b20f 100644 --- a/tests/attr/layer/test_layer_activation.py +++ b/tests/attr/layer/test_layer_activation.py @@ -1,12 +1,12 @@ #!/usr/bin/env python3 import unittest -from typing import List, Tuple, Union, Any +from typing import Any, List, Tuple, Union import torch +import torch.nn as nn from torch import Tensor from torch.nn import Module -import torch.nn as nn from captum.attr._core.layer.layer_activation import LayerActivation @@ -16,8 +16,8 @@ ) from ..helpers.utils import ( BaseTest, - assertTensorTuplesAlmostEqual, assertTensorAlmostEqual, + assertTensorTuplesAlmostEqual, ) diff --git a/tests/attr/layer/test_layer_conductance.py b/tests/attr/layer/test_layer_conductance.py index 1cc0fb5cbe..fc4eea9bc9 100644 --- a/tests/attr/layer/test_layer_conductance.py +++ b/tests/attr/layer/test_layer_conductance.py @@ -1,13 +1,15 @@ #!/usr/bin/env python3 import unittest +from typing import Any, List, Tuple, Union, cast import torch from torch import Tensor from torch.nn import Module -from typing import cast, Any, List, Tuple, Union + from captum.attr._core.layer.layer_conductance import LayerConductance from captum.attr._utils.typing import BaselineType + from ..helpers.basic_models import ( BasicModel_ConvNet, BasicModel_MultiLayer, @@ -15,9 +17,9 @@ ) from ..helpers.conductance_reference import ConductanceReference from ..helpers.utils import ( + BaseTest, assertArraysAlmostEqual, assertTensorTuplesAlmostEqual, - BaseTest, ) diff --git a/tests/attr/layer/test_layer_deeplift_basic.py b/tests/attr/layer/test_layer_deeplift_basic.py index f6e9440001..487b841dfe 100644 --- a/tests/attr/layer/test_layer_deeplift_basic.py +++ b/tests/attr/layer/test_layer_deeplift_basic.py @@ -2,24 +2,25 @@ from __future__ import print_function +from typing import List, Tuple, Union, cast + import torch from torch import Tensor -from typing import Tuple, Union, List, cast +from captum.attr._core.layer.layer_deep_lift import LayerDeepLift, LayerDeepLiftShap + +from ..helpers.basic_models import ( + BasicModel_MultiLayer, + LinearMaxPoolLinearModel, + ReLULinearDeepLiftModel, +) from ..helpers.utils import ( BaseTest, + assert_delta, + assertArraysAlmostEqual, assertTensorAlmostEqual, assertTensorTuplesAlmostEqual, - assertArraysAlmostEqual, - assert_delta, ) -from ..helpers.basic_models import ( - ReLULinearDeepLiftModel, - BasicModel_MultiLayer, - LinearMaxPoolLinearModel, -) - -from captum.attr._core.layer.layer_deep_lift import LayerDeepLift, LayerDeepLiftShap class TestDeepLift(BaseTest): diff --git a/tests/attr/layer/test_layer_gradient_shap.py b/tests/attr/layer/test_layer_gradient_shap.py index b02e8c883f..c147770785 100644 --- a/tests/attr/layer/test_layer_gradient_shap.py +++ b/tests/attr/layer/test_layer_gradient_shap.py @@ -1,23 +1,24 @@ #!/usr/bin/env python3 +from typing import Any, Callable, List, Tuple, Union + import torch from torch import Tensor from torch.nn import Module -from typing import Union, Tuple, List, Any, Callable - -from ..helpers.utils import BaseTest +from captum.attr._core.gradient_shap import GradientShap +from captum.attr._core.layer.layer_gradient_shap import LayerGradientShap +from captum.attr._utils.typing import TargetType, TensorOrTupleOfTensorsGeneric -from ..helpers.utils import assertTensorTuplesAlmostEqual, assertTensorAlmostEqual -from ..helpers.classification_models import SoftmaxModel from ..helpers.basic_models import ( BasicModel_MultiLayer, BasicModel_MultiLayer_MultiInput, ) - -from captum.attr._core.gradient_shap import GradientShap -from captum.attr._core.layer.layer_gradient_shap import LayerGradientShap -from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric, TargetType - +from ..helpers.classification_models import SoftmaxModel +from ..helpers.utils import ( + BaseTest, + assertTensorAlmostEqual, + assertTensorTuplesAlmostEqual, +) from ..test_gradient_shap import _assert_attribution_delta diff --git a/tests/attr/layer/test_layer_integrated_gradients.py b/tests/attr/layer/test_layer_integrated_gradients.py index f93bbcb886..5e086b666e 100644 --- a/tests/attr/layer/test_layer_integrated_gradients.py +++ b/tests/attr/layer/test_layer_integrated_gradients.py @@ -1,26 +1,24 @@ #!/usr/bin/env python3 -from typing import List, Tuple, Union, Any, cast +from typing import Any, List, Tuple, Union, cast import torch from torch import Tensor from torch.nn import Module -from ..helpers.utils import assertArraysAlmostEqual, assertTensorTuplesAlmostEqual - +from captum.attr._core.integrated_gradients import IntegratedGradients +from captum.attr._core.layer.layer_conductance import LayerConductance +from captum.attr._core.layer.layer_integrated_gradients import LayerIntegratedGradients from captum.attr._models.base import ( configure_interpretable_embedding_layer, remove_interpretable_embedding_layer, ) -from captum.attr._core.integrated_gradients import IntegratedGradients -from captum.attr._core.layer.layer_integrated_gradients import LayerIntegratedGradients -from captum.attr._core.layer.layer_conductance import LayerConductance - -from ..helpers.utils import BaseTest -from ..helpers.basic_models import ( - BasicEmbeddingModel, - BasicModel_MultiLayer, +from ..helpers.basic_models import BasicEmbeddingModel, BasicModel_MultiLayer +from ..helpers.utils import ( + BaseTest, + assertArraysAlmostEqual, + assertTensorTuplesAlmostEqual, ) diff --git a/tests/attr/models/test_base.py b/tests/attr/models/test_base.py index 08c6b792ca..6b0d65687c 100644 --- a/tests/attr/models/test_base.py +++ b/tests/attr/models/test_base.py @@ -2,20 +2,19 @@ from __future__ import print_function -import torch import unittest +import torch from torch.nn import Embedding -from ..helpers.utils import assertArraysAlmostEqual - from captum.attr._models.base import ( + InterpretableEmbeddingBase, configure_interpretable_embedding_layer, remove_interpretable_embedding_layer, - InterpretableEmbeddingBase, ) from ..helpers.basic_models import BasicEmbeddingModel, TextModule +from ..helpers.utils import assertArraysAlmostEqual class Test(unittest.TestCase): diff --git a/tests/attr/models/test_pytext.py b/tests/attr/models/test_pytext.py index 2ae8bc8bad..a798ca9d44 100644 --- a/tests/attr/models/test_pytext.py +++ b/tests/attr/models/test_pytext.py @@ -3,8 +3,8 @@ from __future__ import print_function import os -import unittest import tempfile +import unittest import torch diff --git a/tests/attr/neuron/test_neuron_ablation.py b/tests/attr/neuron/test_neuron_ablation.py index 4e86d07690..f0a0b5307f 100644 --- a/tests/attr/neuron/test_neuron_ablation.py +++ b/tests/attr/neuron/test_neuron_ablation.py @@ -1,21 +1,21 @@ #!/usr/bin/env python3 import unittest +from typing import Any, List, Tuple, Union import torch from torch import Tensor from torch.nn import Module -from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric, BaselineType from captum.attr._core.neuron.neuron_feature_ablation import NeuronFeatureAblation -from typing import Tuple, Union, Any, List +from captum.attr._utils.typing import BaselineType, TensorOrTupleOfTensorsGeneric from ..helpers.basic_models import ( BasicModel_ConvNet_One_Conv, BasicModel_MultiLayer, BasicModel_MultiLayer_MultiInput, ) -from ..helpers.utils import assertTensorAlmostEqual, BaseTest +from ..helpers.utils import BaseTest, assertTensorAlmostEqual class Test(BaseTest): diff --git a/tests/attr/neuron/test_neuron_conductance.py b/tests/attr/neuron/test_neuron_conductance.py index 8f3d3db220..0eae7fade6 100644 --- a/tests/attr/neuron/test_neuron_conductance.py +++ b/tests/attr/neuron/test_neuron_conductance.py @@ -1,20 +1,22 @@ #!/usr/bin/env python3 import unittest -from typing import List, Tuple, Union, Any, cast +from typing import Any, List, Tuple, Union, cast + import torch from torch import Tensor from torch.nn import Module + from captum.attr._core.layer.layer_conductance import LayerConductance from captum.attr._core.neuron.neuron_conductance import NeuronConductance +from captum.attr._utils.typing import BaselineType, TensorOrTupleOfTensorsGeneric from ..helpers.basic_models import ( BasicModel_ConvNet, BasicModel_MultiLayer, BasicModel_MultiLayer_MultiInput, ) -from ..helpers.utils import assertArraysAlmostEqual, BaseTest -from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric, BaselineType +from ..helpers.utils import BaseTest, assertArraysAlmostEqual class Test(BaseTest): diff --git a/tests/attr/neuron/test_neuron_deeplift_basic.py b/tests/attr/neuron/test_neuron_deeplift_basic.py index 3d50e04702..c421ffc62a 100644 --- a/tests/attr/neuron/test_neuron_deeplift_basic.py +++ b/tests/attr/neuron/test_neuron_deeplift_basic.py @@ -2,21 +2,21 @@ from __future__ import print_function -import torch +from typing import Tuple, Union -from ..helpers.utils import BaseTest, assertTensorAlmostEqual -from ..helpers.basic_models import ReLULinearDeepLiftModel +import torch +from torch import Tensor from captum.attr._core.neuron.neuron_deep_lift import NeuronDeepLift, NeuronDeepLiftShap +from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric + +from ..helpers.basic_models import ReLULinearDeepLiftModel +from ..helpers.utils import BaseTest, assertTensorAlmostEqual from ..layer.test_layer_deeplift_basic import ( _create_inps_and_base_for_deeplift_neuron_layer_testing, _create_inps_and_base_for_deepliftshap_neuron_layer_testing, ) -from typing import Tuple, Union -from torch import Tensor -from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric - class Test(BaseTest): def test_relu_neuron_deeplift(self) -> None: diff --git a/tests/attr/neuron/test_neuron_gradient.py b/tests/attr/neuron/test_neuron_gradient.py index 582663e219..fc8caec228 100644 --- a/tests/attr/neuron/test_neuron_gradient.py +++ b/tests/attr/neuron/test_neuron_gradient.py @@ -1,13 +1,14 @@ #!/usr/bin/env python3 -from torch.nn import Module -from torch import Tensor -from typing import Any, List, Tuple, Union, cast import unittest +from typing import Any, List, Tuple, Union, cast import torch -from captum.attr._core.saliency import Saliency +from torch import Tensor +from torch.nn import Module + from captum.attr._core.neuron.neuron_gradient import NeuronGradient +from captum.attr._core.saliency import Saliency from captum.attr._utils.gradient import _forward_layer_eval from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric @@ -17,9 +18,9 @@ BasicModel_MultiLayer_MultiInput, ) from ..helpers.utils import ( + BaseTest, assertArraysAlmostEqual, assertTensorTuplesAlmostEqual, - BaseTest, ) diff --git a/tests/attr/neuron/test_neuron_gradient_shap.py b/tests/attr/neuron/test_neuron_gradient_shap.py index 2fa5deb6d8..b0fe57e884 100644 --- a/tests/attr/neuron/test_neuron_gradient_shap.py +++ b/tests/attr/neuron/test_neuron_gradient_shap.py @@ -5,17 +5,15 @@ from torch import Tensor from torch.nn import Module -from ..helpers.utils import BaseTest - -from ..helpers.utils import assertTensorAlmostEqual -from ..helpers.classification_models import SoftmaxModel -from ..helpers.basic_models import BasicModel_MultiLayer - from captum.attr._core.neuron.neuron_gradient_shap import NeuronGradientShap from captum.attr._core.neuron.neuron_integrated_gradients import ( NeuronIntegratedGradients, ) +from ..helpers.basic_models import BasicModel_MultiLayer +from ..helpers.classification_models import SoftmaxModel +from ..helpers.utils import BaseTest, assertTensorAlmostEqual + class Test(BaseTest): def test_basic_multilayer(self) -> None: diff --git a/tests/attr/neuron/test_neuron_integrated_gradients.py b/tests/attr/neuron/test_neuron_integrated_gradients.py index 32d88ae377..554f103c03 100644 --- a/tests/attr/neuron/test_neuron_integrated_gradients.py +++ b/tests/attr/neuron/test_neuron_integrated_gradients.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import unittest -from typing import List, Tuple, Union, Any +from typing import Any, List, Tuple, Union import torch from torch import Tensor @@ -11,7 +11,6 @@ from captum.attr._core.neuron.neuron_integrated_gradients import ( NeuronIntegratedGradients, ) - from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric from ..helpers.basic_models import ( @@ -20,9 +19,9 @@ BasicModel_MultiLayer_MultiInput, ) from ..helpers.utils import ( + BaseTest, assertArraysAlmostEqual, assertTensorTuplesAlmostEqual, - BaseTest, ) diff --git a/tests/attr/test_approximation_methods.py b/tests/attr/test_approximation_methods.py index b1aa549fd3..a5d9e90319 100644 --- a/tests/attr/test_approximation_methods.py +++ b/tests/attr/test_approximation_methods.py @@ -2,7 +2,7 @@ import unittest -from captum.attr._utils.approximation_methods import riemann_builders, Riemann +from captum.attr._utils.approximation_methods import Riemann, riemann_builders from .helpers.utils import assertArraysAlmostEqual diff --git a/tests/attr/test_common.py b/tests/attr/test_common.py index e30f8374ee..08d702151a 100644 --- a/tests/attr/test_common.py +++ b/tests/attr/test_common.py @@ -1,16 +1,17 @@ #!/usr/bin/env python3 +from typing import List, Tuple, cast + import torch -from typing import cast, List, Tuple +from captum.attr._core.noise_tunnel import SUPPORTED_NOISE_TUNNEL_TYPES from captum.attr._utils.common import ( + _select_targets, _validate_input, _validate_noise_tunnel_type, - _select_targets, ) -from captum.attr._core.noise_tunnel import SUPPORTED_NOISE_TUNNEL_TYPES -from .helpers.utils import assertTensorAlmostEqual, BaseTest +from .helpers.utils import BaseTest, assertTensorAlmostEqual class Test(BaseTest): diff --git a/tests/attr/test_data_parallel.py b/tests/attr/test_data_parallel.py index 8d9488d4d6..be88e209df 100644 --- a/tests/attr/test_data_parallel.py +++ b/tests/attr/test_data_parallel.py @@ -4,39 +4,37 @@ import torch -from captum.attr._core.feature_ablation import FeatureAblation from captum.attr._core.deep_lift import DeepLift +from captum.attr._core.feature_ablation import FeatureAblation from captum.attr._core.gradient_shap import GradientShap -from captum.attr._core.occlusion import Occlusion -from captum.attr._core.shapley_value import ShapleyValueSampling, ShapleyValues - from captum.attr._core.layer.grad_cam import LayerGradCam from captum.attr._core.layer.internal_influence import InternalInfluence from captum.attr._core.layer.layer_activation import LayerActivation from captum.attr._core.layer.layer_conductance import LayerConductance -from captum.attr._core.layer.layer_gradient_x_activation import LayerGradientXActivation from captum.attr._core.layer.layer_deep_lift import LayerDeepLift, LayerDeepLiftShap +from captum.attr._core.layer.layer_feature_ablation import LayerFeatureAblation from captum.attr._core.layer.layer_gradient_shap import LayerGradientShap +from captum.attr._core.layer.layer_gradient_x_activation import LayerGradientXActivation from captum.attr._core.layer.layer_integrated_gradients import LayerIntegratedGradients -from captum.attr._core.layer.layer_feature_ablation import LayerFeatureAblation - from captum.attr._core.neuron.neuron_conductance import NeuronConductance +from captum.attr._core.neuron.neuron_deep_lift import NeuronDeepLift, NeuronDeepLiftShap +from captum.attr._core.neuron.neuron_feature_ablation import NeuronFeatureAblation from captum.attr._core.neuron.neuron_gradient import NeuronGradient -from captum.attr._core.neuron.neuron_integrated_gradients import ( - NeuronIntegratedGradients, -) +from captum.attr._core.neuron.neuron_gradient_shap import NeuronGradientShap from captum.attr._core.neuron.neuron_guided_backprop_deconvnet import ( NeuronDeconvolution, NeuronGuidedBackprop, ) -from captum.attr._core.neuron.neuron_deep_lift import NeuronDeepLift, NeuronDeepLiftShap -from captum.attr._core.neuron.neuron_gradient_shap import NeuronGradientShap -from captum.attr._core.neuron.neuron_feature_ablation import NeuronFeatureAblation +from captum.attr._core.neuron.neuron_integrated_gradients import ( + NeuronIntegratedGradients, +) +from captum.attr._core.occlusion import Occlusion +from captum.attr._core.shapley_value import ShapleyValues, ShapleyValueSampling from .helpers.basic_models import ( + BasicModel_ConvNet, BasicModel_MultiLayer, BasicModel_MultiLayer_MultiInput, - BasicModel_ConvNet, ReLULinearDeepLiftModel, ) from .helpers.utils import BaseGPUTest diff --git a/tests/attr/test_deconvolution.py b/tests/attr/test_deconvolution.py index acb57f6ee4..a6972b2c0b 100644 --- a/tests/attr/test_deconvolution.py +++ b/tests/attr/test_deconvolution.py @@ -3,18 +3,19 @@ from __future__ import print_function import unittest +from typing import Any, List, Tuple, Union import torch +from torch.nn import Module + from captum.attr._core.guided_backprop_deconvnet import Deconvolution from captum.attr._core.neuron.neuron_guided_backprop_deconvnet import ( NeuronDeconvolution, ) +from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric -from typing import Any, Tuple, Union, List -from torch.nn import Module from .helpers.basic_models import BasicModel_ConvNet_One_Conv -from .helpers.utils import assertTensorAlmostEqual, BaseTest -from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric +from .helpers.utils import BaseTest, assertTensorAlmostEqual class Test(BaseTest): diff --git a/tests/attr/test_deeplift_basic.py b/tests/attr/test_deeplift_basic.py index 63445bc6de..5596ba7461 100644 --- a/tests/attr/test_deeplift_basic.py +++ b/tests/attr/test_deeplift_basic.py @@ -1,29 +1,29 @@ #!/usr/bin/env python3 -from typing import Tuple, List, Union, Callable +from inspect import signature +from typing import Callable, List, Tuple, Union import torch from torch import Tensor from torch.nn import Module -from inspect import signature - from captum.attr._core.deep_lift import DeepLift, DeepLiftShap from captum.attr._core.integrated_gradients import IntegratedGradients -from .helpers.utils import ( - assertAttributionComparision, - assertArraysAlmostEqual, - assertTensorAlmostEqual, - BaseTest, -) from .helpers.basic_models import ( - ReLUDeepLiftModel, - TanhDeepLiftModel, BasicModelWithReusableModules, + Conv1dDeepLiftModel, LinearMaxPoolLinearModel, + ReLUDeepLiftModel, + ReLULinearDeepLiftModel, + TanhDeepLiftModel, +) +from .helpers.utils import ( + BaseTest, + assertArraysAlmostEqual, + assertAttributionComparision, + assertTensorAlmostEqual, ) -from .helpers.basic_models import ReLULinearDeepLiftModel, Conv1dDeepLiftModel class Test(BaseTest): diff --git a/tests/attr/test_deeplift_classification.py b/tests/attr/test_deeplift_classification.py index 3534c7d698..62f4fa6f65 100644 --- a/tests/attr/test_deeplift_classification.py +++ b/tests/attr/test_deeplift_classification.py @@ -10,12 +10,13 @@ from captum.attr._core.integrated_gradients import IntegratedGradients from captum.attr._utils.typing import TargetType -from .helpers.utils import assertAttributionComparision, BaseTest -from .helpers.classification_models import SigmoidDeepLiftModel -from .helpers.classification_models import SoftmaxDeepLiftModel -from .helpers.basic_models import BasicModel_ConvNet -from .helpers.basic_models import BasicModel_ConvNet_MaxPool1d -from .helpers.basic_models import BasicModel_ConvNet_MaxPool3d +from .helpers.basic_models import ( + BasicModel_ConvNet, + BasicModel_ConvNet_MaxPool1d, + BasicModel_ConvNet_MaxPool3d, +) +from .helpers.classification_models import SigmoidDeepLiftModel, SoftmaxDeepLiftModel +from .helpers.utils import BaseTest, assertAttributionComparision class Test(BaseTest): diff --git a/tests/attr/test_feature_ablation.py b/tests/attr/test_feature_ablation.py index d470508699..62dd71d025 100644 --- a/tests/attr/test_feature_ablation.py +++ b/tests/attr/test_feature_ablation.py @@ -1,25 +1,26 @@ #!/usr/bin/env python3 import unittest +from typing import Any, Callable, List, Tuple, Union import torch from torch import Tensor + from captum.attr._core.feature_ablation import FeatureAblation -from typing import List, Tuple, Union, Any, Callable +from captum.attr._utils.typing import ( + BaselineType, + TargetType, + TensorOrTupleOfTensorsGeneric, +) from .helpers.basic_models import ( BasicModel, - BasicModelWithSparseInputs, BasicModel_ConvNet_One_Conv, BasicModel_MultiLayer, BasicModel_MultiLayer_MultiInput, + BasicModelWithSparseInputs, ) -from .helpers.utils import assertTensorAlmostEqual, BaseTest -from captum.attr._utils.typing import ( - TensorOrTupleOfTensorsGeneric, - TargetType, - BaselineType, -) +from .helpers.utils import BaseTest, assertTensorAlmostEqual class Test(BaseTest): diff --git a/tests/attr/test_feature_permutation.py b/tests/attr/test_feature_permutation.py index 2ca8f041cd..2dcbcface9 100644 --- a/tests/attr/test_feature_permutation.py +++ b/tests/attr/test_feature_permutation.py @@ -3,10 +3,8 @@ import torch from torch import Tensor -from captum.attr._core.feature_permutation import ( - FeaturePermutation, - _permute_feature, -) + +from captum.attr._core.feature_permutation import FeaturePermutation, _permute_feature from .helpers.basic_models import BasicModelWithSparseInputs from .helpers.utils import BaseTest, assertArraysAlmostEqual, assertTensorAlmostEqual diff --git a/tests/attr/test_gradient.py b/tests/attr/test_gradient.py index d8b7651d0f..31edb82f44 100644 --- a/tests/attr/test_gradient.py +++ b/tests/attr/test_gradient.py @@ -6,18 +6,18 @@ from torch import Tensor from captum.attr._utils.gradient import ( + apply_gradient_requirements, compute_gradients, compute_layer_gradients_and_eval, - apply_gradient_requirements, undo_gradient_requirements, ) -from .helpers.utils import assertArraysAlmostEqual, BaseTest from .helpers.basic_models import ( BasicModel, BasicModel6_MultiTensor, BasicModel_MultiLayer, ) +from .helpers.utils import BaseTest, assertArraysAlmostEqual class Test(BaseTest): diff --git a/tests/attr/test_gradient_shap.py b/tests/attr/test_gradient_shap.py index 6687a6f1c6..6c8f8a9107 100644 --- a/tests/attr/test_gradient_shap.py +++ b/tests/attr/test_gradient_shap.py @@ -1,16 +1,18 @@ #!/usr/bin/env python3 -import torch +from typing import Tuple, Union, cast + import numpy as np +import torch +from numpy import ndarray -from .helpers.utils import assertArraysAlmostEqual, assertTensorAlmostEqual, BaseTest -from .helpers.classification_models import SoftmaxModel -from .helpers.basic_models import BasicModel2, BasicLinearModel from captum.attr._core.gradient_shap import GradientShap from captum.attr._core.integrated_gradients import IntegratedGradients -from typing import Union, Tuple, cast from captum.attr._utils.typing import Tensor -from numpy import ndarray + +from .helpers.basic_models import BasicLinearModel, BasicModel2 +from .helpers.classification_models import SoftmaxModel +from .helpers.utils import BaseTest, assertArraysAlmostEqual, assertTensorAlmostEqual class Test(BaseTest): diff --git a/tests/attr/test_guided_backprop.py b/tests/attr/test_guided_backprop.py index 8ddb2f6e2a..f6c51fe7d9 100644 --- a/tests/attr/test_guided_backprop.py +++ b/tests/attr/test_guided_backprop.py @@ -1,18 +1,19 @@ #!/usr/bin/env python3 import unittest +from typing import Any, List, Tuple, Union import torch +from torch.nn import Module + from captum.attr._core.guided_backprop_deconvnet import GuidedBackprop from captum.attr._core.neuron.neuron_guided_backprop_deconvnet import ( NeuronGuidedBackprop, ) +from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric from .helpers.basic_models import BasicModel_ConvNet_One_Conv -from .helpers.utils import assertTensorAlmostEqual, BaseTest -from typing import Any, Tuple, Union, List -from torch.nn import Module -from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric +from .helpers.utils import BaseTest, assertTensorAlmostEqual class Test(BaseTest): diff --git a/tests/attr/test_guided_grad_cam.py b/tests/attr/test_guided_grad_cam.py index 6604c0cb37..30fb251164 100644 --- a/tests/attr/test_guided_grad_cam.py +++ b/tests/attr/test_guided_grad_cam.py @@ -1,16 +1,16 @@ #!/usr/bin/env python3 import unittest +from typing import Any import torch +from torch.nn import Module + from captum.attr._core.guided_grad_cam import GuidedGradCam +from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric from .helpers.basic_models import BasicModel_ConvNet_One_Conv -from .helpers.utils import assertTensorAlmostEqual, BaseTest - -from torch.nn import Module -from typing import Any -from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric +from .helpers.utils import BaseTest, assertTensorAlmostEqual class Test(BaseTest): diff --git a/tests/attr/test_input_x_gradient.py b/tests/attr/test_input_x_gradient.py index 6977f79a53..9a146f7089 100644 --- a/tests/attr/test_input_x_gradient.py +++ b/tests/attr/test_input_x_gradient.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 from typing import Any + import torch from torch import Tensor from torch.nn import Module @@ -9,7 +10,7 @@ from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric from .helpers.classification_models import SoftmaxModel -from .helpers.utils import assertArraysAlmostEqual, BaseTest +from .helpers.utils import BaseTest, assertArraysAlmostEqual from .test_saliency import _get_basic_config, _get_multiargs_basic_config diff --git a/tests/attr/test_integrated_gradients_basic.py b/tests/attr/test_integrated_gradients_basic.py index 7455bd46ca..0158120549 100644 --- a/tests/attr/test_integrated_gradients_basic.py +++ b/tests/attr/test_integrated_gradients_basic.py @@ -1,12 +1,18 @@ #!/usr/bin/env python3 +import unittest +from typing import Any, Tuple, Union, cast + +import torch +from torch.nn import Module + from captum.attr._core.integrated_gradients import IntegratedGradients from captum.attr._core.noise_tunnel import NoiseTunnel -from captum.attr._utils.common import _zeros, _tensorize_baseline +from captum.attr._utils.common import _tensorize_baseline, _zeros from captum.attr._utils.typing import ( + BaselineType, Tensor, TensorOrTupleOfTensorsGeneric, - BaselineType, ) from .helpers.basic_models import ( @@ -18,12 +24,7 @@ BasicModel6_MultiTensor, BasicModel_MultiLayer, ) -from .helpers.utils import assertArraysAlmostEqual, assertTensorAlmostEqual, BaseTest - -import unittest -import torch -from torch.nn import Module -from typing import Any, cast, Tuple, Union +from .helpers.utils import BaseTest, assertArraysAlmostEqual, assertTensorAlmostEqual class Test(BaseTest): diff --git a/tests/attr/test_integrated_gradients_classification.py b/tests/attr/test_integrated_gradients_classification.py index 94b006c921..e834fd574f 100644 --- a/tests/attr/test_integrated_gradients_classification.py +++ b/tests/attr/test_integrated_gradients_classification.py @@ -1,17 +1,16 @@ #!/usr/bin/env python3 -import torch import unittest +import torch from torch.nn import Module from captum.attr._core.integrated_gradients import IntegratedGradients from captum.attr._core.noise_tunnel import NoiseTunnel -from captum.attr._utils.typing import Tensor, BaselineType +from captum.attr._utils.typing import BaselineType, Tensor -from .helpers.utils import BaseTest from .helpers.classification_models import SigmoidModel, SoftmaxModel -from .helpers.utils import assertTensorAlmostEqual +from .helpers.utils import BaseTest, assertTensorAlmostEqual class Test(BaseTest): diff --git a/tests/attr/test_occlusion.py b/tests/attr/test_occlusion.py index 8afbc73926..8b16b86f06 100644 --- a/tests/attr/test_occlusion.py +++ b/tests/attr/test_occlusion.py @@ -1,15 +1,15 @@ #!/usr/bin/env python3 -from typing import Callable, Union, Tuple, List, Any import unittest +from typing import Any, Callable, List, Tuple, Union import torch from torch import Tensor from captum.attr._core.occlusion import Occlusion from captum.attr._utils.typing import ( - TensorOrTupleOfTensorsGeneric, - TargetType, BaselineType, + TargetType, + TensorOrTupleOfTensorsGeneric, ) from .helpers.basic_models import ( @@ -18,7 +18,7 @@ BasicModel_MultiLayer, BasicModel_MultiLayer_MultiInput, ) -from .helpers.utils import assertTensorAlmostEqual, BaseTest +from .helpers.utils import BaseTest, assertTensorAlmostEqual class Test(BaseTest): diff --git a/tests/attr/test_saliency.py b/tests/attr/test_saliency.py index bc131fb70a..be0afb22e4 100644 --- a/tests/attr/test_saliency.py +++ b/tests/attr/test_saliency.py @@ -4,6 +4,7 @@ import torch from torch import Tensor from torch.nn import Module + from captum.attr._core.noise_tunnel import NoiseTunnel from captum.attr._core.saliency import Saliency from captum.attr._utils.gradient import compute_gradients diff --git a/tests/attr/test_shapley.py b/tests/attr/test_shapley.py index d61f4f3bae..0ec64eda9c 100644 --- a/tests/attr/test_shapley.py +++ b/tests/attr/test_shapley.py @@ -1,19 +1,18 @@ #!/usr/bin/env python3 -from typing import Any, Callable, Tuple, Union - import unittest +from typing import Any, Callable, Tuple, Union import torch -from captum.attr._core.shapley_value import ShapleyValueSampling, ShapleyValues -from captum.attr._utils.typing import TensorOrTupleOfTensorsGeneric, BaselineType +from captum.attr._core.shapley_value import ShapleyValues, ShapleyValueSampling +from captum.attr._utils.typing import BaselineType, TensorOrTupleOfTensorsGeneric from .helpers.basic_models import ( BasicModel_MultiLayer, BasicModel_MultiLayer_MultiInput, ) -from .helpers.utils import assertTensorTuplesAlmostEqual, BaseTest +from .helpers.utils import BaseTest, assertTensorTuplesAlmostEqual class Test(BaseTest): diff --git a/tests/attr/test_stat.py b/tests/attr/test_stat.py index 2a755b20ea..d50c83abeb 100644 --- a/tests/attr/test_stat.py +++ b/tests/attr/test_stat.py @@ -1,10 +1,12 @@ #!/usr/bin/env python3 +import random + import numpy as np import torch -import random -from captum.attr import Mean, Var, StdDev, Min, Max, Sum, MSE, Summarizer -from .helpers.utils import BaseTest, assertTensorAlmostEqual, assertArraysAlmostEqual +from captum.attr import MSE, Max, Mean, Min, StdDev, Sum, Summarizer, Var + +from .helpers.utils import BaseTest, assertArraysAlmostEqual, assertTensorAlmostEqual def get_values(n=100, lo=None, hi=None, integers=False): diff --git a/tests/attr/test_summarizer.py b/tests/attr/test_summarizer.py index ef7d5b2d40..bbfb10a324 100644 --- a/tests/attr/test_summarizer.py +++ b/tests/attr/test_summarizer.py @@ -2,6 +2,7 @@ import torch from captum.attr import CommonSummarizer + from .helpers.utils import BaseTest diff --git a/tests/attr/test_targets.py b/tests/attr/test_targets.py index f900600399..1a7d4f71ef 100644 --- a/tests/attr/test_targets.py +++ b/tests/attr/test_targets.py @@ -3,24 +3,23 @@ import unittest import torch -from captum.attr._core.integrated_gradients import IntegratedGradients -from captum.attr._core.saliency import Saliency -from captum.attr._core.input_x_gradient import InputXGradient + from captum.attr._core.deep_lift import DeepLift, DeepLiftShap -from captum.attr._core.gradient_shap import GradientShap -from captum.attr._core.noise_tunnel import NoiseTunnel from captum.attr._core.feature_ablation import FeatureAblation -from captum.attr._core.occlusion import Occlusion -from captum.attr._core.shapley_value import ShapleyValueSampling - +from captum.attr._core.gradient_shap import GradientShap +from captum.attr._core.input_x_gradient import InputXGradient +from captum.attr._core.integrated_gradients import IntegratedGradients from captum.attr._core.layer.internal_influence import InternalInfluence from captum.attr._core.layer.layer_conductance import LayerConductance -from captum.attr._core.layer.layer_integrated_gradients import LayerIntegratedGradients -from captum.attr._core.layer.layer_gradient_x_activation import LayerGradientXActivation -from captum.attr._core.layer.layer_feature_ablation import LayerFeatureAblation from captum.attr._core.layer.layer_deep_lift import LayerDeepLift, LayerDeepLiftShap - +from captum.attr._core.layer.layer_feature_ablation import LayerFeatureAblation +from captum.attr._core.layer.layer_gradient_x_activation import LayerGradientXActivation +from captum.attr._core.layer.layer_integrated_gradients import LayerIntegratedGradients from captum.attr._core.neuron.neuron_conductance import NeuronConductance +from captum.attr._core.noise_tunnel import NoiseTunnel +from captum.attr._core.occlusion import Occlusion +from captum.attr._core.saliency import Saliency +from captum.attr._core.shapley_value import ShapleyValueSampling from .helpers.basic_models import BasicModel_MultiLayer from .helpers.utils import BaseTest, assertTensorAlmostEqual diff --git a/tests/attr/test_utils_batching.py b/tests/attr/test_utils_batching.py index 85c9e77ce5..737df86bee 100644 --- a/tests/attr/test_utils_batching.py +++ b/tests/attr/test_utils_batching.py @@ -3,11 +3,11 @@ import torch from captum.attr._utils.batching import ( - _tuple_splice_range, + _batched_generator, + _batched_operator, _reduce_list, _sort_key_list, - _batched_operator, - _batched_generator, + _tuple_splice_range, ) from .helpers.utils import BaseTest, assertTensorAlmostEqual diff --git a/tests/insights/test_contribution.py b/tests/insights/test_contribution.py index 7c65bbbcc1..ecf9801cfd 100644 --- a/tests/insights/test_contribution.py +++ b/tests/insights/test_contribution.py @@ -5,6 +5,7 @@ import torch import torch.nn as nn + from captum.insights import AttributionVisualizer, Batch, FilterConfig from captum.insights.features import BaseFeature, FeatureOutput, ImageFeature from tests.attr.helpers.utils import BaseTest