Skip to content

Commit 342ec1b

Browse files
merge isinstance calls (#6689)
You can pass a tuple of types you want to check as the second argument to `isinstance`. If the object matches with any of the types, it will return `True` else `False`. --------- Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
1 parent 1530d89 commit 342ec1b

File tree

7 files changed

+6
-7
lines changed

7 files changed

+6
-7
lines changed

monai/apps/nnunet/nnunetv2_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ def train_single_model(self, config: Any, fold: int, gpu_id: tuple | list | int
521521
kwargs.pop("export_validation_probabilities")
522522
logger.warning("please specify the `export_validation_probabilities` in the __init__ of `nnUNetV2Runner`.")
523523

524-
if isinstance(gpu_id, tuple) or isinstance(gpu_id, list):
524+
if isinstance(gpu_id, (tuple, list)):
525525
if len(gpu_id) > 1:
526526
gpu_ids_str = ""
527527
for _i in range(len(gpu_id)):

monai/handlers/garbage_collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class GarbageCollector:
4949

5050
def __init__(self, trigger_event: str | Events | CallableEventWithFilter = "epoch", log_level: int = 10):
5151
self.trigger_event: Events | CallableEventWithFilter
52-
if isinstance(trigger_event, Events) or isinstance(trigger_event, CallableEventWithFilter):
52+
if isinstance(trigger_event, (Events, CallableEventWithFilter)):
5353
self.trigger_event = trigger_event
5454
elif trigger_event.lower() == "epoch":
5555
self.trigger_event = Events.EPOCH_COMPLETED

tests/test_focal_loss.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ def test_consistency_with_cross_entropy_2d(self):
110110

111111
def test_consistency_with_cross_entropy_2d_no_reduction(self):
112112
"""For gamma=0 the focal loss reduces to the cross entropy loss"""
113-
import numpy as np
114113

115114
focal_loss = FocalLoss(to_onehot_y=False, gamma=0.0, reduction="none", weight=1.0)
116115
ce = nn.BCEWithLogitsLoss(reduction="none")

tests/test_grid_patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def test_grid_patch(self, in_type, input_parameters, image, expected):
109109
output_patch,
110110
in_type(expected_patch),
111111
type_test=False,
112-
device_test=True if isinstance(in_type(expected_patch), torch.Tensor) else False,
112+
device_test=bool(isinstance(in_type(expected_patch), torch.Tensor)),
113113
)
114114

115115
@parameterized.expand([TEST_CASE_META_0, TEST_CASE_META_1])

tests/test_grid_patchd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_grid_patchd(self, in_type, input_parameters, image_dict, expected):
9494
output_patch,
9595
in_type(expected_patch),
9696
type_test=False,
97-
device_test=True if isinstance(in_type(expected_patch), torch.Tensor) else False,
97+
device_test=bool(isinstance(in_type(expected_patch), torch.Tensor)),
9898
)
9999

100100

tests/test_rand_grid_patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def test_rand_grid_patch(self, in_type, input_parameters, image, expected):
114114
output_patch,
115115
in_type(expected_patch),
116116
type_test=False,
117-
device_test=True if isinstance(in_type(expected_patch), torch.Tensor) else False,
117+
device_test=bool(isinstance(in_type(expected_patch), torch.Tensor)),
118118
)
119119

120120
@parameterized.expand([TEST_CASE_META_0, TEST_CASE_META_1])

tests/test_rand_grid_patchd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_rand_grid_patchd(self, in_type, input_parameters, image_dict, expected)
9999
output_patch,
100100
in_type(expected_patch),
101101
type_test=False,
102-
device_test=True if isinstance(in_type(expected_patch), torch.Tensor) else False,
102+
device_test=bool(isinstance(in_type(expected_patch), torch.Tensor)),
103103
)
104104

105105

0 commit comments

Comments
 (0)