diff --git a/monai/data/image_reader.py b/monai/data/image_reader.py index 904a5bb2d2..087d4d1950 100644 --- a/monai/data/image_reader.py +++ b/monai/data/image_reader.py @@ -28,7 +28,16 @@ orientation_ras_lps, ) from monai.transforms.utility.array import EnsureChannelFirst -from monai.utils import MetaKeys, SpaceKeys, deprecated, ensure_tuple, ensure_tuple_rep, optional_import, require_pkg +from monai.utils import ( + MetaKeys, + SpaceKeys, + TraceKeys, + deprecated, + ensure_tuple, + ensure_tuple_rep, + optional_import, + require_pkg, +) if TYPE_CHECKING: import itk @@ -131,7 +140,7 @@ def _copy_compatible_dict(from_dict: Dict, to_dict: Dict): datum = from_dict[key] if isinstance(datum, np.ndarray) and np_str_obj_array_pattern.search(datum.dtype.str) is not None: continue - to_dict[key] = datum + to_dict[key] = str(TraceKeys.NONE) if datum is None else datum # NoneType to string for default_collate else: affine_key, shape_key = MetaKeys.AFFINE, MetaKeys.SPATIAL_SHAPE if affine_key in from_dict and not np.allclose(from_dict[affine_key], to_dict[affine_key]): diff --git a/monai/metrics/active_learning_metrics.py b/monai/metrics/active_learning_metrics.py index ad62935f44..9d2de8b1a2 100644 --- a/monai/metrics/active_learning_metrics.py +++ b/monai/metrics/active_learning_metrics.py @@ -15,6 +15,7 @@ import torch from monai.metrics.utils import ignore_background +from monai.utils import MetricReduction from .metric import Metric @@ -135,7 +136,7 @@ def compute_variance( n_len = len(y_pred.shape) - if n_len < 4 and spatial_map is True: + if n_len < 4 and spatial_map: warnings.warn("Spatial map requires a 2D/3D image with N-repeats and C-channels") return None @@ -149,16 +150,14 @@ def compute_variance( y_reshaped = torch.reshape(y_pred, new_shape) variance = torch.var(y_reshaped, dim=0, unbiased=False) - if spatial_map is True: + if spatial_map: return variance - elif spatial_map is False: - if scalar_reduction == "mean": - var_mean = torch.mean(variance) - return var_mean - elif scalar_reduction == "sum": - var_sum = torch.sum(variance) - return var_sum + if scalar_reduction == MetricReduction.MEAN: + return torch.mean(variance) + if scalar_reduction == MetricReduction.SUM: + return torch.sum(variance) + raise ValueError(f"scalar_reduction={scalar_reduction} not supported.") def label_quality_score( @@ -196,13 +195,11 @@ def label_quality_score( abs_diff_map = torch.abs(y_pred - y) - if scalar_reduction == "none": + if scalar_reduction == MetricReduction.NONE: return abs_diff_map - elif scalar_reduction != "none": - if scalar_reduction == "mean": - lbl_score_mean = torch.mean(abs_diff_map, dim=list(range(1, n_len))) - return lbl_score_mean - elif scalar_reduction == "sum": - lbl_score_sum = torch.sum(abs_diff_map, dim=list(range(1, n_len))) - return lbl_score_sum + if scalar_reduction == MetricReduction.MEAN: + return torch.mean(abs_diff_map, dim=list(range(1, n_len))) + if scalar_reduction == MetricReduction.SUM: + return torch.sum(abs_diff_map, dim=list(range(1, n_len))) + raise ValueError(f"scalar_reduction={scalar_reduction} not supported.") diff --git a/monai/visualize/occlusion_sensitivity.py b/monai/visualize/occlusion_sensitivity.py index b4845b7e27..cfc2d2b675 100644 --- a/monai/visualize/occlusion_sensitivity.py +++ b/monai/visualize/occlusion_sensitivity.py @@ -155,7 +155,8 @@ def gaussian_occlusion(x: torch.Tensor, mask_size, sigma=0.25) -> Tuple[torch.Te [GaussianSmooth(sigma=[b * sigma for b in spatial_shape]), Lambda(lambda x: -x), ScaleIntensity()] ) # transform and add batch - mul: torch.Tensor = gaussian(kernel)[None] # type: ignore + mul: torch.Tensor = gaussian(kernel)[None] + return mul, 0 @staticmethod diff --git a/tests/test_pil_reader.py b/tests/test_pil_reader.py index 0f7792a56c..4f0b891b72 100644 --- a/tests/test_pil_reader.py +++ b/tests/test_pil_reader.py @@ -64,6 +64,7 @@ def test_converter(self, data_shape, filenames, expected_shape, meta_shape): Image.fromarray(test_image.astype("uint8")).save(filenames[i]) reader = PILReader(converter=lambda image: image.convert("LA")) result = reader.get_data(reader.read(filenames, mode="r")) + self.assertEqual(result[1]["format"], "none") # project-monai/monai issue#5251 # load image by PIL and compare the result test_image = np.asarray(Image.open(filenames[0]).convert("LA"))