Skip to content

Commit

Permalink
Fix ruff rule F401: remove unused imports (#31)
Browse files Browse the repository at this point in the history
* Remove unused imports

* Move fixture to conftest.py

* Enable rule F401

* Remove one more import
  • Loading branch information
connortann authored Jun 1, 2023
1 parent df6ca19 commit c266372
Show file tree
Hide file tree
Showing 40 changed files with 34 additions and 106 deletions.
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import os
import sys
import shutil
import sphinx_rtd_theme
print(os.path.abspath('./shap'))
sys.path.insert(0, os.path.abspath('..'))

Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ testpaths = ["tests"]
select = ["F"]
# For now, deselect rules that do not pass
ignore = [
"F401", # unused imports
"F811", # Redefinition of unused variable
"F841", # Local variables assigned but unused
]

[tool.ruff.per-file-ignores]
# Unused imports often intentional in top-level __init__.py
"__init__.py" = ["F401"]
2 changes: 0 additions & 2 deletions shap/_explanation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import pandas as pd
import numpy as np
import scipy as sp
import sys
import warnings
import copy
import operator
import sklearn
Expand Down
1 change: 0 additions & 1 deletion shap/actions/_optimizer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import queue
import numpy as np
import warnings
import copy
from ._action import Action
Expand Down
2 changes: 1 addition & 1 deletion shap/benchmark/_sequential.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from shap.utils import safe_isinstance, MaskedModel
from shap.maskers import Independent, Partition, Impute, Text, Image, FixedComposite
from shap.maskers import Text, Image, FixedComposite
from shap import Explanation, links
import matplotlib.pyplot as pl
import sklearn
Expand Down
3 changes: 0 additions & 3 deletions shap/benchmark/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
from .. import datasets
from . import metrics
from . import models
from . import methods
from .. import __version__
import numpy as np
import sklearn
import os
import pickle
import sys
Expand Down
6 changes: 1 addition & 5 deletions shap/benchmark/framework.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import itertools as it
import sklearn
import shap
from sklearn.model_selection import train_test_split
from shap.utils import safe_isinstance, MaskedModel
from shap.utils import safe_isinstance
from . import perturbation

def update(model, attributions, X, y, masker, sort_order, perturbation_method, scores):
Expand Down
1 change: 0 additions & 1 deletion shap/benchmark/measures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
from tqdm.autonotebook import tqdm
import gc
import warnings
import sklearn.utils

Expand Down
6 changes: 0 additions & 6 deletions shap/benchmark/metrics.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
from .. import LinearExplainer
from .. import KernelExplainer
from .. import SamplingExplainer
from ..explainers import other
from .. import __version__
from . import measures
from . import methods
import sklearn
import numpy as np
import copy
import functools
import time
import hashlib
import os
Expand Down
1 change: 0 additions & 1 deletion shap/benchmark/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sklearn
import sklearn.ensemble
import gc
from sklearn.preprocessing import StandardScaler
import numpy as np

Expand Down
3 changes: 0 additions & 3 deletions shap/explainers/_additive.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import numpy as np
import scipy as sp
import warnings
from ._explainer import Explainer
from ..utils import safe_isinstance, MaskedModel
from .. import maskers


class Additive(Explainer):
Expand Down
1 change: 0 additions & 1 deletion shap/explainers/_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from ..utils._legacy import convert_to_instance_with_index, convert_to_link, IdentityLink, convert_to_data, DenseData, SparseData
from ..utils import safe_isinstance
from scipy.special import binom
from scipy.sparse import issparse
import numpy as np
import pandas as pd
import scipy as sp
Expand Down
10 changes: 1 addition & 9 deletions shap/explainers/_partition.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import types
import copy
import inspect
from ..utils import MaskedModel
import numpy as np
import warnings
import time
from tqdm.auto import tqdm
import queue
from ..utils import assert_import, record_import_error, safe_isinstance, make_masks, OpChain
from ..utils import safe_isinstance, make_masks, OpChain
from .. import Explanation
from .. import maskers
from ._explainer import Explainer
from .. import links
import cloudpickle
import pickle
from ..maskers import Masker
from ..models import Model
from numba import njit

Expand Down
9 changes: 0 additions & 9 deletions shap/explainers/_permutation.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import functools
import types
from ..utils import partition_tree_shuffle, MaskedModel
from .._explanation import Explanation
from ._explainer import Explainer
import numpy as np
import pandas as pd
import scipy as sp
import pickle
import cloudpickle
import warnings
from .. import links
from .. import maskers
from ..maskers import Masker
from ..models import Model

class Permutation(Explainer):
Expand Down
3 changes: 1 addition & 2 deletions shap/explainers/_sampling.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from ..utils._legacy import convert_to_instance, convert_to_model, match_instance_to_data, match_model_to_data
from ..utils._legacy import convert_to_instance_with_index, convert_to_link, IdentityLink, convert_to_data, DenseData
from ..utils._legacy import convert_to_instance, match_instance_to_data
from ..utils import safe_isinstance
from .._explanation import Explanation
from ._kernel import Kernel
Expand Down
10 changes: 1 addition & 9 deletions shap/explainers/_tree.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import time
import numpy as np
import scipy.special
import multiprocessing
import sys
import json
import os
import struct
import itertools
from packaging import version
from ._explainer import Explainer
from ..utils import assert_import, record_import_error, safe_isinstance
Expand All @@ -27,7 +23,7 @@
record_import_error("cext", "C extension was not built during install!", e)

try:
import pyspark
import pyspark # noqa
except ImportError as e:
record_import_error("pyspark", "PySpark could not be imported!", e)

Expand Down Expand Up @@ -829,7 +825,6 @@ def __init__(self, model, data=None, data_missing=None, model_output=None):
else:
assert False, "Unsupported Spark model type: " + str(type(model))
elif safe_isinstance(model, "xgboost.core.Booster"):
import xgboost
self.original_model = model
self.model_type = "xgboost"
xgb_loader = XGBTreeModelLoader(self.original_model)
Expand All @@ -840,7 +835,6 @@ def __init__(self, model, data=None, data_missing=None, model_output=None):
if xgb_loader.num_class > 0:
self.num_stacked_models = xgb_loader.num_class
elif safe_isinstance(model, "xgboost.sklearn.XGBClassifier"):
import xgboost
self.input_dtype = np.float32
self.model_type = "xgboost"
self.original_model = model.get_booster()
Expand All @@ -864,7 +858,6 @@ def __init__(self, model, data=None, data_missing=None, model_output=None):
else:
self.model_output = "probability"
elif safe_isinstance(model, "xgboost.sklearn.XGBRegressor"):
import xgboost
self.original_model = model.get_booster()
self.model_type = "xgboost"
xgb_loader = XGBTreeModelLoader(self.original_model)
Expand All @@ -876,7 +869,6 @@ def __init__(self, model, data=None, data_missing=None, model_output=None):
if xgb_loader.num_class > 0:
self.num_stacked_models = xgb_loader.num_class
elif safe_isinstance(model, "xgboost.sklearn.XGBRanker"):
import xgboost
self.original_model = model.get_booster()
self.model_type = "xgboost"
xgb_loader = XGBTreeModelLoader(self.original_model)
Expand Down
1 change: 0 additions & 1 deletion shap/explainers/pytree.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""
import numpy as np
#import numba
from .explainer import Explainer
from ..utils._exceptions import ExplainerError

# class TreeExplainer(Explainer):
Expand Down
2 changes: 1 addition & 1 deletion shap/maskers/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import heapq
from numba import njit
try:
import torch
import torch # noqa: F401
except ImportError as e:
record_import_error("torch", "torch could not be imported!", e)

Expand Down
2 changes: 1 addition & 1 deletion shap/models/_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ..utils import record_import_error, safe_isinstance
from .._serializable import Serializable, Serializer, Deserializer
try:
import torch
import torch # noqa: F401
except ImportError as e:
record_import_error("torch", "torch could not be imported!", e)

Expand Down
3 changes: 1 addition & 2 deletions shap/plots/_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
pass
from ._labels import labels
from ..utils import format_value, ordinal_str
from ._utils import convert_ordering, convert_color, merge_nodes, get_sort_order, sort_inds, dendrogram_coords
from ._utils import convert_ordering, merge_nodes, get_sort_order, sort_inds, dendrogram_coords
from . import colors
import numpy as np
import scipy
import copy
from .. import Explanation, Cohorts


Expand Down
2 changes: 1 addition & 1 deletion shap/plots/_beeswarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
pass
from ._labels import labels
from . import colors
from ..utils import safe_isinstance, OpChain, format_value
from ..utils import safe_isinstance
from ._utils import convert_ordering, convert_color, merge_nodes, get_sort_order, sort_inds
from .. import Explanation

Expand Down
1 change: 0 additions & 1 deletion shap/plots/_decision.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
try:
import matplotlib.cm as cm
import matplotlib.pyplot as pl
import matplotlib
except ImportError:
warnings.warn("matplotlib could not be loaded!")
pass
Expand Down
2 changes: 0 additions & 2 deletions shap/plots/_embedding.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import numpy as np
import sklearn
import warnings
try:
import matplotlib.pyplot as pl
import matplotlib
except ImportError:
warnings.warn("matplotlib could not be loaded!")
pass
Expand Down
1 change: 0 additions & 1 deletion shap/plots/_group_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import warnings
try:
import matplotlib.pyplot as pl
import matplotlib
except ImportError:
warnings.warn("matplotlib could not be loaded!")
pass
Expand Down
3 changes: 1 addition & 2 deletions shap/plots/_heatmap.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import numpy as np
try:
import matplotlib.pyplot as pl
import matplotlib
except ImportError:
pass
from . import colors
from .. import Explanation
from ..utils import OpChain
from ._utils import convert_ordering, convert_color
from ._utils import convert_ordering

def heatmap(shap_values, instance_order=Explanation.hclust(), feature_values=Explanation.abs.mean(0),
feature_order=None, max_display=10, cmap=colors.red_white_blue, show=True,
Expand Down
1 change: 0 additions & 1 deletion shap/plots/_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import warnings
try:
import matplotlib.pyplot as pl
import matplotlib
except ImportError:
warnings.warn("matplotlib could not be loaded!")
pass
Expand Down
1 change: 0 additions & 1 deletion shap/plots/_partial_dependence.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
except ImportError:
warnings.warn("matplotlib could not be loaded!")
pass
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import pandas as pd

Expand Down
2 changes: 0 additions & 2 deletions shap/plots/_text.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import numpy as np
import warnings
from . import colors
from ..utils import ordinal_str
import random
import string
import json
Expand Down
2 changes: 1 addition & 1 deletion shap/plots/colors/_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ._colorconv import lab2rgb, lch2lab

try:
import matplotlib
import matplotlib # noqa: F401
from matplotlib.colors import LinearSegmentedColormap

def lch2rgb(x):
Expand Down
1 change: 0 additions & 1 deletion shap/utils/_clustering.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
import scipy as sp
from scipy.spatial.distance import pdist
from numba import njit
import sklearn
import warnings
Expand Down
3 changes: 0 additions & 3 deletions shap/utils/_general.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import re
import pandas as pd
import numpy as np
import scipy as sp
from scipy.spatial.distance import pdist
import sys
import warnings
import sklearn
import importlib
import copy
from contextlib import contextmanager
import sys, os
Expand Down
2 changes: 0 additions & 2 deletions shap/utils/image.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import cv2
import json
import matplotlib.pyplot as plt
import numpy as np
import os
import requests
import shap
from textwrap import wrap

Expand Down
1 change: 0 additions & 1 deletion tests/benchmark/perturbation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import shap
import shap.benchmark as benchmark
from transformers import AutoTokenizer
from shap.maskers import Independent, Partition, Impute, Text, Image, FixedComposite
Expand Down
Loading

0 comments on commit c266372

Please sign in to comment.