diff --git a/monai/metrics/confusion_matrix.py b/monai/metrics/confusion_matrix.py index bc06fe5855..a0c840d45a 100644 --- a/monai/metrics/confusion_matrix.py +++ b/monai/metrics/confusion_matrix.py @@ -87,7 +87,7 @@ def __call__(self, y_pred: torch.Tensor, y: torch.Tensor): dims = y_pred.ndimension() if dims < 2: raise ValueError("y_pred should have at least two dimensions.") - elif dims == 2 or (dims == 3 and y_pred.shape[-1] == 1): + if dims == 2 or (dims == 3 and y_pred.shape[-1] == 1): if self.compute_sample: warnings.warn("As for classification task, compute_sample should be False.") self.compute_sample = False diff --git a/monai/networks/blocks/dynunet_block.py b/monai/networks/blocks/dynunet_block.py index 03413ed816..d43958de0c 100644 --- a/monai/networks/blocks/dynunet_block.py +++ b/monai/networks/blocks/dynunet_block.py @@ -228,13 +228,12 @@ def get_acti_layer(act: Union[Tuple[str, Dict], str]): def get_norm_layer(spatial_dims: int, out_channels: int, norm_name: str, num_groups: int = 16): if norm_name not in ["batch", "instance", "group"]: raise ValueError(f"Unsupported normalization mode: {norm_name}") + if norm_name == "group": + assert out_channels % num_groups == 0, "out_channels should be divisible by num_groups." + norm = Norm[norm_name](num_groups=num_groups, num_channels=out_channels, affine=True) else: - if norm_name == "group": - assert out_channels % num_groups == 0, "out_channels should be divisible by num_groups." - norm = Norm[norm_name](num_groups=num_groups, num_channels=out_channels, affine=True) - else: - norm = Norm[norm_name, spatial_dims](out_channels, affine=True) - return norm + norm = Norm[norm_name, spatial_dims](out_channels, affine=True) + return norm def get_conv_layer( diff --git a/monai/networks/blocks/segresnet_block.py b/monai/networks/blocks/segresnet_block.py index 29a8a8e3aa..6aaa9774b5 100644 --- a/monai/networks/blocks/segresnet_block.py +++ b/monai/networks/blocks/segresnet_block.py @@ -22,16 +22,15 @@ def get_norm_layer(spatial_dims: int, in_channels: int, norm_name: str, num_groups: int = 8): if norm_name not in ["batch", "instance", "group"]: raise ValueError(f"Unsupported normalization mode: {norm_name}") + if norm_name == "group": + norm = Norm[norm_name](num_groups=num_groups, num_channels=in_channels) else: - if norm_name == "group": - norm = Norm[norm_name](num_groups=num_groups, num_channels=in_channels) - else: - norm = Norm[norm_name, spatial_dims](in_channels) - if norm.bias is not None: - nn.init.zeros_(norm.bias) - if norm.weight is not None: - nn.init.ones_(norm.weight) - return norm + norm = Norm[norm_name, spatial_dims](in_channels) + if norm.bias is not None: + nn.init.zeros_(norm.bias) + if norm.weight is not None: + nn.init.ones_(norm.weight) + return norm def get_conv_layer( diff --git a/monai/networks/layers/simplelayers.py b/monai/networks/layers/simplelayers.py index ba60f4eca4..a6524669b1 100644 --- a/monai/networks/layers/simplelayers.py +++ b/monai/networks/layers/simplelayers.py @@ -233,8 +233,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: x = torch.as_tensor(x, device=x.device if torch.is_tensor(x) else None) if torch.is_complex(x): raise ValueError("x must be real.") - else: - x = x.to(dtype=torch.float) + x = x.to(dtype=torch.float) if (self.axis < 0) or (self.axis > len(x.shape) - 1): raise ValueError("Invalid axis for shape of x.") diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index 6f94660ccd..ffbd91690c 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -451,7 +451,7 @@ def create_rotate(spatial_dims: int, radians: Union[Sequence[float], float]) -> return np.array([[cos_, -sin_, 0.0], [sin_, cos_, 0.0], [0.0, 0.0, 1.0]]) raise ValueError("radians must be non empty.") - elif spatial_dims == 3: + if spatial_dims == 3: affine = None if len(radians) >= 1: sin_, cos_ = np.sin(radians[0]), np.cos(radians[0]) diff --git a/monai/utils/aliases.py b/monai/utils/aliases.py index 5a182862d5..ce3c513473 100644 --- a/monai/utils/aliases.py +++ b/monai/utils/aliases.py @@ -91,8 +91,7 @@ def resolve_name(name): modnames = [m.__name__ for m in foundmods] msg = f"Multiple modules ({modnames!r}) with declaration name {name!r} found, resolution is ambiguous." raise ValueError(msg) - else: - mods = list(foundmods) + mods = list(foundmods) obj = getattr(mods[0], name) diff --git a/tests/utils.py b/tests/utils.py index 157eef8aff..efedcdc859 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -407,8 +407,7 @@ def _wrapper(*args, **kwargs): if isinstance(res, Exception): # other errors from obj if hasattr(res, "traceback"): raise RuntimeError(res.traceback) from res - else: - raise res + raise res if timeout_error: # no force_quit finished raise timeout_error return res