Skip to content

Commit 2e9a4e6

Browse files
Fixup pycodestyle issues (#3050)
Co-authored-by: Jeremy Goh <[email protected]>
1 parent d0db73f commit 2e9a4e6

23 files changed

+83
-54
lines changed

pyproject.toml

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ select = [
9595
"F", # pyflakes
9696
"I", # isort
9797
"UP", # pyupgrade
98+
"E", # pycodestyle
99+
"W", # warning
100+
]
101+
ignore = [
102+
# Aim to progressively fix address these codes over time
103+
"E501", # Line too long
104+
"E741", # Ambiguous variable name: `l`
105+
"E402", # Module level import not at top of file
98106
]
99107
target-version = "py37"
100108

shap/_explanation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def _numpy_func(self, fname, **kwargs):
532532
if new_self.data is not None:
533533
try:
534534
new_self.data = getattr(np, fname)(np.array(self.data), **kwargs)
535-
except:
535+
except Exception:
536536
new_self.data = None
537537
if new_self.base_values is not None and issubclass(type(axis), int) and len(self.base_values.shape) > axis:
538538
new_self.base_values = getattr(np, fname)(self.base_values, **kwargs)

shap/benchmark/metrics.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,10 @@ def __score_method(X, y, fcounts, model_generator, score_function, method_name,
432432
""" Test an explanation method.
433433
"""
434434

435-
try: pickle
436-
except NameError: assert False, "The 'dill' package could not be loaded and is needed for the benchmark!"
435+
try:
436+
pickle
437+
except NameError:
438+
raise ImportError("The 'dill' package could not be loaded and is needed for the benchmark!")
437439

438440
old_seed = np.random.seed()
439441
np.random.seed(3293)

shap/datasets.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def adult(display=False, n_points=None):
176176
raw_data = shap.utils.sample(raw_data, n_points, random_state=0)
177177

178178
data = raw_data.drop(["Education"], axis=1) # redundant with Education-Num
179-
filt_dtypes = list(filter(lambda x: not (x[0] in ["Target", "Education"]), dtypes))
179+
filt_dtypes = list(filter(lambda x: x[0] not in ["Target", "Education"], dtypes))
180180
data["Target"] = data["Target"] == " >50K"
181181
rcode = {
182182
"Not-in-family": 0,
@@ -238,7 +238,8 @@ def corrgroups60(display=False, n_points=1_000): # pylint: disable=unused-argume
238238
C[i,i+1] = C[i+1,i] = 0.99
239239
C[i,i+2] = C[i+2,i] = 0.99
240240
C[i+1,i+2] = C[i+2,i+1] = 0.99
241-
f = lambda X: np.matmul(X, beta)
241+
def f(X):
242+
return np.matmul(X, beta)
242243

243244
# Make sure the sample correlation is a perfect match
244245
X_start = np.random.randn(N, M)
@@ -273,7 +274,8 @@ def independentlinear60(display=False, n_points=1_000): # pylint: disable=unused
273274
# set one coefficent from each group of 3 to 1
274275
beta = np.zeros(M)
275276
beta[0:30:3] = 1
276-
f = lambda X: np.matmul(X, beta)
277+
def f(X):
278+
return np.matmul(X, beta)
277279

278280
# Make sure the sample correlation is a perfect match
279281
X_start = np.random.randn(N, M)

shap/explainers/_deep/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ def __init__(self, model, data, session=None, learning_phase_flags=None):
7171
try:
7272
a.named_parameters()
7373
framework = 'pytorch'
74-
except:
74+
except Exception:
7575
framework = 'tensorflow'
7676
else:
7777
try:
7878
model.named_parameters()
7979
framework = 'pytorch'
80-
except:
80+
except Exception:
8181
framework = 'tensorflow'
8282

8383
if framework == 'tensorflow':

shap/explainers/_deep/deep_tf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def __init__(self, model, data, session=None, learning_phase_flags=None):
100100
try:
101101
import keras
102102
warnings.warn("keras is no longer supported, please use tf.keras instead.")
103-
except:
103+
except Exception:
104104
pass
105105

106106
if version.parse(tf.__version__) >= version.parse("2.4.0"):
@@ -129,7 +129,7 @@ def __init__(self, model, data, session=None, learning_phase_flags=None):
129129
self.multi_input = False
130130
if type(self.model_inputs) != list:
131131
self.model_inputs = [self.model_inputs]
132-
if type(data) != list and (hasattr(data, '__call__')==False):
132+
if type(data) != list and (hasattr(data, "__call__") is False):
133133
data = [data]
134134
self.data = data
135135

shap/explainers/_gradient.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ def __init__(self, model, data, session=None, batch_size=50, local_smoothing=0):
6565
try:
6666
a.named_parameters()
6767
framework = 'pytorch'
68-
except:
68+
except Exception:
6969
framework = 'tensorflow'
7070
else:
7171
try:
7272
model.named_parameters()
7373
framework = 'pytorch'
74-
except:
74+
except Exception:
7575
framework = 'tensorflow'
7676

7777
if isinstance(data, pd.DataFrame):
@@ -159,7 +159,7 @@ def __init__(self, model, data, session=None, batch_size=50, local_smoothing=0):
159159
from tensorflow import keras
160160
if version.parse(keras.__version__) < version.parse("2.1.0"):
161161
warnings.warn("Your Keras version is older than 2.1.0 and not supported.")
162-
except:
162+
except Exception:
163163
pass
164164

165165
# determine the model inputs and outputs

shap/explainers/_kernel.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ def explain(self, incoming_instance, **kwargs):
347347

348348
# determine how many subsets (and their complements) are of the current size
349349
nsubsets = binom(self.M, subset_size)
350-
if subset_size <= num_paired_subset_sizes: nsubsets *= 2
350+
if subset_size <= num_paired_subset_sizes:
351+
nsubsets *= 2
351352
log.debug(f"subset_size = {subset_size}")
352353
log.debug(f"nsubsets = {nsubsets}")
353354
log.debug("self.nsamples*weight_vector[subset_size-1] = {}".format(
@@ -366,7 +367,8 @@ def explain(self, incoming_instance, **kwargs):
366367

367368
# add all the samples of the current subset size
368369
w = weight_vector[subset_size - 1] / binom(self.M, subset_size)
369-
if subset_size <= num_paired_subset_sizes: w /= 2.0
370+
if subset_size <= num_paired_subset_sizes:
371+
w /= 2.0
370372
for inds in itertools.combinations(group_inds, subset_size):
371373
mask[:] = 0.0
372374
mask[np.array(inds, dtype='int64')] = 1.0

shap/explainers/_linear.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def supports_model_with_masker(model, masker):
281281

282282
try:
283283
Linear._parse_model(model)
284-
except:
284+
except Exception:
285285
return False
286286
return True
287287

shap/explainers/_sampling.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def explain(self, incoming_instance, **kwargs):
132132
phi_var /= phi_var.sum(0)[np.newaxis, :]
133133
nsamples_each2 = (phi_var[self.varyingInds,:].mean(1) * round2_samples).astype(int)
134134
for i in range(len(nsamples_each2)):
135-
if nsamples_each2[i] % 2 == 1: nsamples_each2[i] += 1
135+
if nsamples_each2[i] % 2 == 1:
136+
nsamples_each2[i] += 1
136137
for i in range(len(nsamples_each2)):
137138
if nsamples_each2.sum() > round2_samples:
138139
nsamples_each2[i] -= 2

shap/explainers/_tree.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def supports_model_with_masker(model, masker):
586586

587587
try:
588588
TreeEnsemble(model)
589-
except:
589+
except Exception:
590590
return False
591591
return True
592592

@@ -1016,7 +1016,7 @@ def __init__(self, model, data=None, data_missing=None, model_output=None):
10161016
tree_info = self.original_model.dump_model()["tree_info"]
10171017
try:
10181018
self.trees = [SingleTree(e, data=data, data_missing=data_missing) for e in tree_info]
1019-
except:
1019+
except Exception:
10201020
self.trees = None # we get here because the cext can't handle categorical splits yet
10211021

10221022
self.objective = objective_name_map.get(model.params.get("objective", "regression"), None)
@@ -1029,7 +1029,7 @@ def __init__(self, model, data=None, data_missing=None, model_output=None):
10291029
tree_info = self.original_model.dump_model()["tree_info"]
10301030
try:
10311031
self.trees = [SingleTree(e, data=data, data_missing=data_missing) for e in tree_info]
1032-
except:
1032+
except Exception:
10331033
self.trees = None # we get here because the cext can't handle categorical splits yet
10341034

10351035
self.objective = objective_name_map.get(model.params.get("objective", "regression"), None)
@@ -1042,7 +1042,7 @@ def __init__(self, model, data=None, data_missing=None, model_output=None):
10421042
tree_info = self.original_model.dump_model()["tree_info"]
10431043
try:
10441044
self.trees = [SingleTree(e, data=data, data_missing=data_missing) for e in tree_info]
1045-
except:
1045+
except Exception:
10461046
self.trees = None # we get here because the cext can't handle categorical splits yet
10471047
self.objective = objective_name_map.get(model.objective, None)
10481048
self.tree_output = tree_output_name_map.get(model.objective, None)
@@ -1056,7 +1056,7 @@ def __init__(self, model, data=None, data_missing=None, model_output=None):
10561056
tree_info = self.original_model.dump_model()["tree_info"]
10571057
try:
10581058
self.trees = [SingleTree(e, data=data, data_missing=data_missing) for e in tree_info]
1059-
except:
1059+
except Exception:
10601060
self.trees = None # we get here because the cext can't handle categorical splits yet
10611061
# Note: for ranker, leaving tree_output and objective as None as they
10621062
# are not implemented in native code yet
@@ -1069,7 +1069,7 @@ def __init__(self, model, data=None, data_missing=None, model_output=None):
10691069
tree_info = self.original_model.dump_model()["tree_info"]
10701070
try:
10711071
self.trees = [SingleTree(e, data=data, data_missing=data_missing) for e in tree_info]
1072-
except:
1072+
except Exception:
10731073
self.trees = None # we get here because the cext can't handle categorical splits yet
10741074
self.objective = objective_name_map.get(model.objective, None)
10751075
self.tree_output = tree_output_name_map.get(model.objective, None)
@@ -1089,7 +1089,7 @@ def __init__(self, model, data=None, data_missing=None, model_output=None):
10891089
try:
10901090
cb_loader = CatBoostTreeModelLoader(model)
10911091
self.trees = cb_loader.get_trees(data=data, data_missing=data_missing)
1092-
except:
1092+
except Exception:
10931093
self.trees = None # we get here because the cext can't handle categorical splits yet
10941094
self.tree_output = "log_odds"
10951095
self.objective = "binary_crossentropy"

shap/explainers/tf_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _get_session(session):
2727
if session is None:
2828
try:
2929
session = tf.compat.v1.keras.backend.get_session()
30-
except:
30+
except Exception:
3131
session = tf.keras.backend.get_session()
3232
return tf.get_default_session() if session is None else session
3333

shap/maskers/_tabular.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def __init__(self, data, max_samples=100, clustering=None):
3636
The distance metric to use for creating the clustering of the features. The
3737
distance function can be any valid scipy.spatial.distance.pdist's metric argument.
3838
However we suggest using 'correlation' in most cases. The full list of options is
39-
braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’, ‘correlation’, ‘cosine’, ‘dice,
40-
euclidean’, ‘hamming’, ‘jaccard’, ‘jensenshannon’, ‘kulsinski’, ‘mahalanobis,
41-
matching’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean,
42-
sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule. These are all
39+
`braycurtis`, `canberra`, `chebyshev`, `cityblock`, `correlation`, `cosine`, `dice`,
40+
`euclidean`, `hamming`, `jaccard`, `jensenshannon`, `kulsinski`, `mahalanobis`,
41+
`matching`, `minkowski`, `rogerstanimoto`, `russellrao`, `seuclidean`,
42+
`sokalmichener`, `sokalsneath`, `sqeuclidean`, `yule`. These are all
4343
the options from scipy.spatial.distance.pdist's metric argument.
4444
"""
4545

@@ -289,10 +289,10 @@ def __init__(self, data, max_samples=100, clustering="correlation"):
289289
If a string, then this is the distance metric to use for creating the clustering of
290290
the features. The distance function can be any valid scipy.spatial.distance.pdist's metric
291291
argument. However we suggest using 'correlation' in most cases. The full list of options is
292-
braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’, ‘correlation’, ‘cosine’, ‘dice,
293-
euclidean’, ‘hamming’, ‘jaccard’, ‘jensenshannon’, ‘kulsinski’, ‘mahalanobis,
294-
matching’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean,
295-
sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule. These are all
292+
`braycurtis`, `canberra`, `chebyshev`, `cityblock`, `correlation`, `cosine`, `dice`,
293+
`euclidean`, `hamming`, `jaccard`, `jensenshannon`, `kulsinski`, `mahalanobis`,
294+
`matching`, `minkowski`, `rogerstanimoto`, `russellrao`, `seuclidean`,
295+
`sokalmichener`, `sokalsneath`, `sqeuclidean`, `yule`. These are all
296296
the options from scipy.spatial.distance.pdist's metric argument.
297297
If an array, then this is assumed to be the clustering of the features.
298298
"""

shap/maskers/_text.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(self, tokenizer=None, mask_token=None, collapse_mask_token="auto",
4949
else:
5050
try:
5151
self.tokenizer = SimpleTokenizer(tokenizer)
52-
except:
52+
except Exception:
5353
raise Exception( # pylint: disable=raise-missing-from
5454
"The passed tokenizer cannot be wrapped as a masker because it does not have a __call__ " + \
5555
"method, not can it be interpreted as a splitting regexp!"
@@ -457,7 +457,7 @@ def merge_score(group1, group2, special_tokens):
457457
score -= 100
458458

459459
# attach surrounding an openers and closers a bit later
460-
if group1[0].s in openers and not group2[-1] in closers:
460+
if group1[0].s in openers and group2[-1] not in closers:
461461
score -= 2
462462

463463
# reach across connectors later

shap/plots/_bar.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def bar(shap_values, max_display=10, order=Explanation.abs, clustering=None, clu
269269
try:
270270
if round(features[i]) == features[i]:
271271
features[i] = int(features[i])
272-
except:
272+
except Exception:
273273
pass # features[i] must not be a number
274274

275275
pl.gca().xaxis.set_ticks_position('bottom')

shap/plots/_beeswarm.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def beeswarm(shap_values, max_display=10, order=Explanation.abs.mean(0),
336336
colored_feature = False
337337
else:
338338
fvalues = np.array(fvalues, dtype=np.float64) # make sure this can be numeric
339-
except:
339+
except Exception:
340340
colored_feature = False
341341
N = len(shaps)
342342
# hspacing = (np.max(shaps) - np.min(shaps)) / 200
@@ -505,7 +505,8 @@ def summary_legacy(shap_values, features=None, feature_names=None, max_display=N
505505
if plot_type == 'layered_violin':
506506
color = "coolwarm"
507507
elif multi_class:
508-
color = lambda i: colors.red_blue_circle(i/len(shap_values))
508+
def color(i):
509+
return colors.red_blue_circle(i / len(shap_values))
509510
else:
510511
color = colors.blue_rgb
511512

@@ -659,7 +660,7 @@ def summary_legacy(shap_values, features=None, feature_names=None, max_display=N
659660
colored_feature = False
660661
else:
661662
values = np.array(values, dtype=np.float64) # make sure this can be numeric
662-
except:
663+
except Exception:
663664
colored_feature = False
664665
N = len(shaps)
665666
# hspacing = (np.max(shaps) - np.min(shaps)) / 200

shap/plots/_force_matplotlib.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,11 @@ def format_data(data):
217217

218218
# Define link function
219219
if data['link'] == 'identity':
220-
convert_func = lambda x: x
220+
def convert_func(x):
221+
return x
221222
elif data['link'] == 'logit':
222-
convert_func = lambda x: 1 / (1 + np.exp(-x))
223+
def convert_func(x):
224+
return 1 / (1 + np.exp(-x))
223225
else:
224226
assert False, "ERROR: Unrecognized link function: " + str(data['link'])
225227

shap/plots/_scatter.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ def scatter(shap_values, color="#1E88E5", hist=True, axis_color="#333333", cmap=
305305
# optionally add jitter to feature values
306306
xv_no_jitter = xv.copy()
307307
if x_jitter > 0:
308-
if x_jitter > 1: x_jitter = 1
308+
if x_jitter > 1:
309+
x_jitter = 1
309310
xvals = xv.copy()
310311
if isinstance(xvals[0], float):
311312
xvals = xvals.astype(float)
@@ -663,7 +664,8 @@ def dependence_legacy(ind, shap_values=None, features=None, feature_names=None,
663664

664665
# optionally add jitter to feature values
665666
if x_jitter > 0:
666-
if x_jitter > 1: x_jitter = 1
667+
if x_jitter > 1:
668+
x_jitter = 1
667669
xvals = xv.copy()
668670
if isinstance(xvals[0], float):
669671
xvals = xvals.astype(float)

shap/utils/_keras.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def clone_keras_layers(model, start_layer, stop_layer):
3939
# behind the next one in line
4040
layers_to_process.append(layer)
4141
continue
42-
if not layer.output.name in new_layers:
42+
if layer.output.name not in new_layers:
4343
new_layers[layer.output.name] = layer(layer_inputs)
4444
if layer.output.name == stop_layer.output.name:
4545
break

shap/utils/_legacy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def match_model_to_data(model, data):
121121
out_val = model.f(data.convert_to_df())
122122
else:
123123
out_val = model.f(data.data)
124-
except:
124+
except Exception:
125125
print("Provided model function fails when applied to the provided data set.")
126126
raise
127127

tests/benchmark/framework.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import shap
44

5-
model = lambda x: np.array([np.linalg.norm(x)])
5+
6+
def model(x):
7+
return np.array([np.linalg.norm(x)])
8+
69
X = np.array([[3, 4], [5, 12], [7, 24]])
710
y = np.array([5, 13, 25])
811
explainer = np.array([[-1, 2], [-4, 2], [1, 2]])
@@ -12,7 +15,8 @@ def test_update():
1215
""" This is to test the update function within benchmark/framework
1316
"""
1417
sort_order = 'positive'
15-
score_function = lambda true, pred: np.mean(pred)
18+
def score_function(true, pred):
19+
return np.mean(pred)
1620
perturbation = 'keep'
1721
scores = {'name': 'test', 'metrics': list(), 'values': dict()}
1822

0 commit comments

Comments
 (0)