MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / __call__

Method __call__

monai/transforms/utility/array.py:1359–1407  ·  view source on GitHub ↗

Compute statistics for the intensity of input image. Args: img: input image to compute intensity stats. meta_data: metadata dictionary to store the statistics data, if None, will create an empty dictionary. mask: if not None, mask the image to ex

(
        self, img: NdarrayOrTensor, meta_data: dict | None = None, mask: np.ndarray | None = None
    )

Source from the content-addressed store, hash-verified

1357 self.channel_wise = channel_wise
1358
1359 def __call__(
1360 self, img: NdarrayOrTensor, meta_data: dict | None = None, mask: np.ndarray | None = None
1361 ) -> tuple[NdarrayOrTensor, dict]:
1362 """
1363 Compute statistics for the intensity of input image.
1364
1365 Args:
1366 img: input image to compute intensity stats.
1367 meta_data: metadata dictionary to store the statistics data, if None, will create an empty dictionary.
1368 mask: if not None, mask the image to extract only the interested area to compute statistics.
1369 mask must have the same shape as input `img`.
1370
1371 """
1372 img_np, *_ = convert_data_type(img, np.ndarray)
1373 if meta_data is None:
1374 meta_data = {}
1375
1376 if mask is not None:
1377 if mask.shape != img_np.shape:
1378 raise ValueError(f"mask must have the same shape as input `img`, got {mask.shape} and {img_np.shape}.")
1379 if mask.dtype != bool:
1380 raise TypeError(f"mask must be bool array, got type {mask.dtype}.")
1381 img_np = img_np[mask]
1382
1383 supported_ops = {
1384 "mean": np.nanmean,
1385 "median": np.nanmedian,
1386 "max": np.nanmax,
1387 "min": np.nanmin,
1388 "std": np.nanstd,
1389 }
1390
1391 def _compute(op: Callable, data: np.ndarray):
1392 if self.channel_wise:
1393 return [op(c) for c in data]
1394 return op(data)
1395
1396 custom_index = 0
1397 for o in self.ops:
1398 if isinstance(o, str):
1399 o = look_up_option(o, supported_ops.keys())
1400 meta_data[self.key_prefix + "_" + o] = _compute(supported_ops[o], img_np) # type: ignore
1401 elif callable(o):
1402 meta_data[self.key_prefix + "_custom_" + str(custom_index)] = _compute(o, img_np)
1403 custom_index += 1
1404 else:
1405 raise ValueError("ops must be key string for predefined operations or callable function.")
1406
1407 return img, meta_data
1408
1409
1410class ToDevice(Transform):

Callers

nothing calls this directly

Calls 3

convert_data_typeFunction · 0.90
look_up_optionFunction · 0.90
_computeFunction · 0.85

Tested by

no test coverage detected