Skip to content

Refactor unnecessary else / elif when if block has a return statement #2717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion monai/data/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def _detect_batch_size(batch_data: Sequence):
dict_batch[k] = v

return dict_batch
elif isinstance(batch_data, list):
if isinstance(batch_data, list):
batch_size = _detect_batch_size(batch_data)
list_batch = []
for b in batch_data:
Expand Down
2 changes: 1 addition & 1 deletion monai/handlers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def from_engine(keys: KeysCollection, first: bool = False):
def _wrapper(data):
if isinstance(data, dict):
return tuple(data[k] for k in keys)
elif isinstance(data, list) and isinstance(data[0], dict):
if isinstance(data, list) and isinstance(data[0], dict):
# if data is a list of dictionaries, extract expected keys and construct lists,
# if `first=True`, only extract keys from the first item of the list
ret = [data[0][k] if first else [i[k] for i in data] for k in keys]
Expand Down
35 changes: 17 additions & 18 deletions monai/networks/blocks/fcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,24 @@ def forward(self, x: torch.Tensor):
fs3 = self.refine8(self.up_conv(fs2) + gcfm4)
fs4 = self.refine9(self.up_conv(fs3) + gcfm5)
return self.refine10(self.up_conv(fs4))
else:
fs1 = self.refine6(
F.interpolate(gcfm1, fm3.size()[2:], mode=self.upsample_mode, align_corners=True) + gcfm2
)
fs2 = self.refine7(F.interpolate(fs1, fm2.size()[2:], mode=self.upsample_mode, align_corners=True) + gcfm3)
fs3 = self.refine8(
F.interpolate(fs2, pool_x.size()[2:], mode=self.upsample_mode, align_corners=True) + gcfm4
)
fs4 = self.refine9(
F.interpolate(fs3, conv_x.size()[2:], mode=self.upsample_mode, align_corners=True) + gcfm5
)
return self.refine10(
F.interpolate(
fs4,
org_input.size()[2:],
mode=self.upsample_mode,
align_corners=True,
)
fs1 = self.refine6(
F.interpolate(gcfm1, fm3.size()[2:], mode=self.upsample_mode, align_corners=True) + gcfm2
)
fs2 = self.refine7(F.interpolate(fs1, fm2.size()[2:], mode=self.upsample_mode, align_corners=True) + gcfm3)
fs3 = self.refine8(
F.interpolate(fs2, pool_x.size()[2:], mode=self.upsample_mode, align_corners=True) + gcfm4
)
fs4 = self.refine9(
F.interpolate(fs3, conv_x.size()[2:], mode=self.upsample_mode, align_corners=True) + gcfm5
)
return self.refine10(
F.interpolate(
fs4,
org_input.size()[2:],
mode=self.upsample_mode,
align_corners=True,
)
)


class MCFCN(FCN):
Expand Down
5 changes: 2 additions & 3 deletions monai/transforms/intensity/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ def __init__(
raise AssertionError(
"If a sequence is passed to k_intensity, then a sequence of locations must be passed to loc"
)
elif len(k_intensity) != len(loc):
if len(k_intensity) != len(loc):
raise AssertionError("There must be one intensity_factor value for each tuple of indices in loc.")
if isinstance(self.loc[0], Sequence) and k_intensity is not None:
if not isinstance(self.k_intensity, Sequence):
Expand Down Expand Up @@ -1541,8 +1541,7 @@ def _make_sequence(self, x: torch.Tensor) -> Sequence[Sequence[float]]:
if not isinstance(self.intensity_range[0], Sequence):
intensity_range = (ensure_tuple(self.intensity_range),) * x.shape[0]
return intensity_range
else:
return ensure_tuple(self.intensity_range)
return ensure_tuple(self.intensity_range)
else:
# set default range if one not provided
return self._set_default_range(x)
Expand Down
10 changes: 4 additions & 6 deletions monai/transforms/post/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,11 @@ def __call__(self, img: NdarrayTensor) -> NdarrayTensor:
"""
if isinstance(img, np.ndarray):
return np.asarray(np.where(np.isin(img, self.applied_labels), img, 0))
elif isinstance(img, torch.Tensor):
if isinstance(img, torch.Tensor):
img_arr = img.detach().cpu().numpy()
img_arr = self(img_arr)
return torch.as_tensor(img_arr, device=img.device)
else:
raise NotImplementedError(f"{self.__class__} can not handle data of type {type(img)}.")
raise NotImplementedError(f"{self.__class__} can not handle data of type {type(img)}.")


class FillHoles(Transform):
Expand Down Expand Up @@ -415,12 +414,11 @@ def __call__(self, img: NdarrayTensor) -> NdarrayTensor:
"""
if isinstance(img, np.ndarray):
return fill_holes(img, self.applied_labels, self.connectivity)
elif isinstance(img, torch.Tensor):
if isinstance(img, torch.Tensor):
img_arr = img.detach().cpu().numpy()
img_arr = self(img_arr)
return torch.as_tensor(img_arr, device=img.device)
else:
raise NotImplementedError(f"{self.__class__} can not handle data of type {type(img)}.")
raise NotImplementedError(f"{self.__class__} can not handle data of type {type(img)}.")


class LabelToContour(Transform):
Expand Down
3 changes: 1 addition & 2 deletions monai/transforms/utility/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,7 @@ def __call__(
def _compute(op: Callable, data: np.ndarray):
if self.channel_wise:
return [op(c) for c in data]
else:
return op(data)
return op(data)

custom_index = 0
for o in self.ops:
Expand Down
8 changes: 4 additions & 4 deletions monai/transforms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ def convert_to_tensor(data):
"""
if isinstance(data, torch.Tensor):
return data.contiguous()
elif isinstance(data, np.ndarray):
if isinstance(data, np.ndarray):
# skip array of string classes and object, refer to:
# https://github.com/pytorch/pytorch/blob/v1.9.0/torch/utils/data/_utils/collate.py#L13
if re.search(r"[SaUO]", data.dtype.str) is None:
Expand Down Expand Up @@ -1107,11 +1107,11 @@ def tensor_to_numpy(data):
if isinstance(data, torch.Tensor):
# invert Tensor to numpy, if scalar data, convert to number
return data.item() if data.ndim == 0 else np.ascontiguousarray(data.detach().cpu().numpy())
elif isinstance(data, dict):
if isinstance(data, dict):
return {k: tensor_to_numpy(v) for k, v in data.items()}
elif isinstance(data, list):
if isinstance(data, list):
return [tensor_to_numpy(i) for i in data]
elif isinstance(data, tuple):
if isinstance(data, tuple):
return tuple(tensor_to_numpy(i) for i in data)

return data
5 changes: 2 additions & 3 deletions monai/utils/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ def _wrapper(*args, **kwargs):

if is_func:
return _wrapper
else:
obj.__init__ = _wrapper
return obj
obj.__init__ = _wrapper
return obj

return _decorator

Expand Down
2 changes: 1 addition & 1 deletion monai/utils/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_dist_device():
backend = dist.get_backend()
if backend == "nccl" and torch.cuda.is_available():
return torch.device(f"cuda:{torch.cuda.current_device()}")
elif backend == "gloo":
if backend == "gloo":
return torch.device("cpu")
return None

Expand Down
3 changes: 1 addition & 2 deletions monai/utils/jupyter_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ def _get_loss(data):

if isinstance(output, list):
return _get_loss(output[0])
else:
return _get_loss(output)
return _get_loss(output)


class StatusMembers(Enum):
Expand Down