Skip to content
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
13 changes: 11 additions & 2 deletions monai/data/image_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]):
Expand Down
31 changes: 14 additions & 17 deletions monai/metrics/active_learning_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import torch

from monai.metrics.utils import ignore_background
from monai.utils import MetricReduction

from .metric import Metric

Expand Down Expand Up @@ -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

Expand All @@ -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(
Expand Down Expand Up @@ -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.")
3 changes: 2 additions & 1 deletion monai/visualize/occlusion_sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/test_pil_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down