Skip to content

Commit 8549e6b

Browse files
fix: use isinstance() instead of type() is tuple checks (#4508)
Closes #4480 Co-authored-by: neha222222 <[email protected]> Co-authored-by: Tobias Pitters <[email protected]>
1 parent 3be7001 commit 8549e6b

4 files changed

Lines changed: 18 additions & 19 deletions

File tree

‎shap/explainers/_deep/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __init__(self, model, data, session=None, learning_phase_flags=None):
6969
7070
"""
7171
# first, we need to find the framework
72-
if type(model) is tuple:
72+
if isinstance(model, tuple):
7373
a, b = model
7474
try:
7575
a.named_parameters()

‎shap/explainers/_deep/deep_pytorch.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self, model, data):
3838
with torch.no_grad():
3939
_ = model(*data)
4040
interim_inputs = self.layer.target_input
41-
if type(interim_inputs) is tuple:
41+
if isinstance(interim_inputs, tuple):
4242
# this should always be true, but just to be safe
4343
self.interim_inputs_shape = [i.shape for i in interim_inputs]
4444
else:
@@ -279,17 +279,17 @@ def add_interim_values(module, input, output):
279279
else:
280280
# check only the 0th input varies
281281
for i in range(len(input)):
282-
if i != 0 and type(output) is tuple:
282+
if i != 0 and isinstance(output, tuple):
283283
assert input[i] == output[i], "Only the 0th input may vary!"
284284
# if a new method is added, it must be added here too. This ensures tensors
285285
# are only saved if necessary
286286
if func_name in ["maxpool", "nonlinear_1d"]:
287287
# only save tensors if necessary
288-
if type(input) is tuple:
288+
if isinstance(input, tuple):
289289
module.x = torch.nn.Parameter(input[0].detach())
290290
else:
291291
module.x = torch.nn.Parameter(input.detach())
292-
if type(output) is tuple:
292+
if isinstance(output, tuple):
293293
module.y = torch.nn.Parameter(output[0].detach())
294294
else:
295295
module.y = torch.nn.Parameter(output.detach())

‎shap/explainers/_gradient.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(
7272
7373
"""
7474
# first, we need to find the framework
75-
if type(model) is tuple:
75+
if isinstance(model, tuple):
7676
a, b = model
7777
try:
7878
a.named_parameters()
@@ -517,7 +517,7 @@ def __init__(
517517
with torch.no_grad():
518518
_ = model(*data)
519519
interim_inputs = self.layer.target_input
520-
if type(interim_inputs) is tuple:
520+
if isinstance(interim_inputs, tuple):
521521
# this should always be true, but just to be safe
522522
self.data = [i.clone().detach() for i in interim_inputs]
523523
else:
@@ -664,13 +664,12 @@ def shap_values(
664664
_ = self.model(*[samples_input[a][k].unsqueeze(0) for a in range(len(X))])
665665
interim_inputs = self.layer.target_input # type: ignore[union-attr]
666666
del self.layer.target_input # type: ignore[union-attr]
667-
if type(interim_inputs) is tuple:
668-
if type(interim_inputs) is tuple:
669-
# this should always be true, but just to be safe
670-
for a in range(len(interim_inputs)):
671-
samples_delta[a][k] = interim_inputs[a].cpu().numpy()
672-
else:
673-
samples_delta[0][k] = interim_inputs.cpu().numpy() # type: ignore[attr-defined]
667+
if isinstance(interim_inputs, tuple):
668+
# this should always be true, but just to be safe
669+
for a in range(len(interim_inputs)):
670+
samples_delta[a][k] = interim_inputs[a].cpu().numpy()
671+
else:
672+
samples_delta[0][k] = interim_inputs.cpu().numpy() # type: ignore[attr-defined]
674673

675674
# compute the gradients at all the sample points
676675
find = model_output_ranks[j, i]

‎shap/plots/_partial_dependence.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def partial_dependence(
6868
feature_names = [f"Feature {i}" for i in range(features.shape[1])]
6969

7070
# this is for a 1D partial dependence plot
71-
if type(ind) is not tuple:
71+
if not isinstance(ind, tuple):
7272
ind = convert_name(ind, None, feature_names)
7373
xv = features[:, ind]
7474
xmin, xmax = compute_bounds(xmin, xmax, xv)
@@ -213,10 +213,10 @@ def partial_dependence(
213213
xv0 = features[:, ind0]
214214
xv1 = features[:, ind1]
215215

216-
xmin0 = xmin[0] if type(xmin) is tuple else xmin
217-
xmin1 = xmin[1] if type(xmin) is tuple else xmin
218-
xmax0 = xmax[0] if type(xmax) is tuple else xmax
219-
xmax1 = xmax[1] if type(xmax) is tuple else xmax
216+
xmin0 = xmin[0] if isinstance(xmin, tuple) else xmin
217+
xmin1 = xmin[1] if isinstance(xmin, tuple) else xmin
218+
xmax0 = xmax[0] if isinstance(xmax, tuple) else xmax
219+
xmax1 = xmax[1] if isinstance(xmax, tuple) else xmax
220220

221221
xmin0, xmax0 = compute_bounds(xmin0, xmax0, xv0)
222222
xmin1, xmax1 = compute_bounds(xmin1, xmax1, xv1)

0 commit comments

Comments
 (0)