diff --git a/monai/bundle/config_item.py b/monai/bundle/config_item.py index 0c46665bf5..75a20b5e6a 100644 --- a/monai/bundle/config_item.py +++ b/monai/bundle/config_item.py @@ -64,9 +64,7 @@ def _find_module_names(self) -> List[str]: Find all the modules start with MOD_START and don't contain any of `excludes`. """ - return [ - m for m in sys.modules.keys() if m.startswith(self.MOD_START) and all(s not in m for s in self.excludes) - ] + return [m for m in sys.modules if m.startswith(self.MOD_START) and all(s not in m for s in self.excludes)] def _find_classes_or_functions(self, modnames: Union[Sequence[str], str]) -> Dict[str, List]: """ diff --git a/monai/bundle/scripts.py b/monai/bundle/scripts.py index 21df7c3d05..ace76ac75c 100644 --- a/monai/bundle/scripts.py +++ b/monai/bundle/scripts.py @@ -384,7 +384,7 @@ def get_all_bundles_list( bundles_info = _get_all_bundles_info(repo=repo, tag=tag, auth_token=auth_token) bundles_list = [] - for bundle_name in bundles_info.keys(): + for bundle_name in bundles_info: latest_version = sorted(bundles_info[bundle_name].keys())[-1] bundles_list.append((bundle_name, latest_version)) diff --git a/monai/losses/multi_scale.py b/monai/losses/multi_scale.py index 5e80af30bc..bef1ae1a5d 100644 --- a/monai/losses/multi_scale.py +++ b/monai/losses/multi_scale.py @@ -60,7 +60,7 @@ def __init__( kernel: gaussian or cauchy. """ super().__init__(reduction=LossReduction(reduction).value) - if kernel not in kernel_fn_dict.keys(): + if kernel not in kernel_fn_dict: raise ValueError(f"got unsupported kernel type: {kernel}", "only support gaussian and cauchy") self.kernel_fn = kernel_fn_dict[kernel] self.loss = loss diff --git a/monai/networks/nets/efficientnet.py b/monai/networks/nets/efficientnet.py index 59e66f0713..eef3d68090 100644 --- a/monai/networks/nets/efficientnet.py +++ b/monai/networks/nets/efficientnet.py @@ -529,7 +529,7 @@ def __init__( ] # check if model_name is valid model - if model_name not in efficientnet_params.keys(): + if model_name not in efficientnet_params: model_name_string = ", ".join(efficientnet_params.keys()) raise ValueError(f"invalid model_name {model_name} found, must be one of {model_name_string} ") @@ -586,7 +586,7 @@ def __init__( ] # check if model_name is valid model - if model_name not in efficientnet_params.keys(): + if model_name not in efficientnet_params: model_name_string = ", ".join(efficientnet_params.keys()) raise ValueError(f"invalid model_name {model_name} found, must be one of {model_name_string} ") @@ -720,7 +720,7 @@ def get_efficientnet_image_size(model_name: str) -> int: """ # check if model_name is valid model - if model_name not in efficientnet_params.keys(): + if model_name not in efficientnet_params: model_name_string = ", ".join(efficientnet_params.keys()) raise ValueError(f"invalid model_name {model_name} found, must be one of {model_name_string} ") diff --git a/monai/transforms/compose.py b/monai/transforms/compose.py index 8a1d0d8740..0ce4433218 100644 --- a/monai/transforms/compose.py +++ b/monai/transforms/compose.py @@ -158,7 +158,7 @@ def flatten(self): """ new_transforms = [] for t in self.transforms: - if type(t) is Compose: + if type(t) is Compose: # nopep8 new_transforms += t.flatten().transforms else: new_transforms.append(t) diff --git a/monai/transforms/signal/array.py b/monai/transforms/signal/array.py index 07f29a3039..7b619a4b39 100644 --- a/monai/transforms/signal/array.py +++ b/monai/transforms/signal/array.py @@ -140,7 +140,7 @@ def __call__(self, signal: NdarrayOrTensor) -> NdarrayOrTensor: self.randomize(None) self.magnitude = self.R.uniform(low=self.boundaries[0], high=self.boundaries[1]) - length = signal.shape[len(signal.shape) - 1] + length = signal.shape[-1] mask = torch.zeros(round(self.magnitude * length)) trange = torch.arange(length) loc = trange[torch.randint(0, trange.size(0), (1,))] @@ -265,7 +265,7 @@ def __call__(self, signal: NdarrayOrTensor) -> NdarrayOrTensor: self.fracs = self.R.uniform(low=self.fraction[0], high=self.fraction[1]) self.freqs = self.R.uniform(low=self.frequencies[0], high=self.frequencies[1]) - length = signal.shape[len(signal.shape) - 1] + length = signal.shape[-1] time_partial = np.arange(0, round(self.fracs * length), 1) data = convert_to_tensor(self.freqs * time_partial) @@ -347,7 +347,7 @@ def __call__(self, signal: NdarrayOrTensor) -> NdarrayOrTensor: self.fracs = self.R.uniform(low=self.fraction[0], high=self.fraction[1]) self.freqs = self.R.uniform(low=self.frequencies[0], high=self.frequencies[1]) - length = signal.shape[len(signal.shape) - 1] + length = signal.shape[-1] time_partial = np.arange(0, round(self.fracs * length), 1) squaredpulse_partial = self.magnitude * squarepulse(self.freqs * time_partial) diff --git a/monai/transforms/utils.py b/monai/transforms/utils.py index 26e43126fa..6f12be3c0b 100644 --- a/monai/transforms/utils.py +++ b/monai/transforms/utils.py @@ -1746,7 +1746,7 @@ def paste_slices(tup): given a tuple (pos,w,max_w), return a tuple of slices """ pos, w, max_w = tup - max_w = max_w.shape[len(max_w.shape) - 1] + max_w = max_w.shape[-1] orig_min = max(pos, 0) orig_max = min(pos + w, max_w) block_min = -min(pos, 0) diff --git a/tests/test_flatten_sub_keysd.py b/tests/test_flatten_sub_keysd.py index 24f0e88620..336d0c296e 100644 --- a/tests/test_flatten_sub_keysd.py +++ b/tests/test_flatten_sub_keysd.py @@ -23,7 +23,6 @@ D1 = {"a": A, "b": B} D2 = {"a": A, "b": B, "c": C} - TEST_CASE_0 = [{"keys": "pred"}, {"image": I, "pred": D1}, {"a": A, "b": B, "image": I}] TEST_CASE_1 = [{"keys": "pred"}, {"image": I, "pred": D2}, {"a": A, "b": B, "c": C, "image": I}] TEST_CASE_2 = [{"keys": "pred", "sub_keys": ["a", "b"]}, {"image": I, "pred": D1}, {"a": A, "b": B, "image": I}] diff --git a/tests/test_global_mutual_information_loss.py b/tests/test_global_mutual_information_loss.py index 73395d0572..d53d6c9711 100644 --- a/tests/test_global_mutual_information_loss.py +++ b/tests/test_global_mutual_information_loss.py @@ -94,7 +94,7 @@ def transformation(translate_params=(0.0, 0.0, 0.0), rotate_params=(0.0, 0.0, 0. a1 = transformation() a1 = torch.tensor(a1).unsqueeze(0).unsqueeze(0).to(device) - for mode in transform_params_dict.keys(): + for mode in transform_params_dict: transform_params_list = transform_params_dict[mode] expected_value_list = EXPECTED_VALUE[mode] for transform_params, expected_value in zip(transform_params_list, expected_value_list):