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

Class MapLabelValue

monai/transforms/utility/array.py:1277–1330  ·  view source on GitHub ↗

Utility to map label values to another set of values. For example, map [3, 2, 1] to [0, 1, 2], [1, 2, 3] -> [0.5, 1.5, 2.5], ["label3", "label2", "label1"] -> [0, 1, 2], [3.5, 2.5, 1.5] -> ["label0", "label1", "label2"], etc. The label data must be numpy array or array-like data and

Source from the content-addressed store, hash-verified

1275
1276
1277class MapLabelValue:
1278 """
1279 Utility to map label values to another set of values.
1280 For example, map [3, 2, 1] to [0, 1, 2], [1, 2, 3] -> [0.5, 1.5, 2.5], ["label3", "label2", "label1"] -> [0, 1, 2],
1281 [3.5, 2.5, 1.5] -> ["label0", "label1", "label2"], etc.
1282 The label data must be numpy array or array-like data and the output data will be numpy array.
1283
1284 """
1285
1286 backend = [TransformBackends.NUMPY, TransformBackends.TORCH]
1287
1288 def __init__(self, orig_labels: Sequence, target_labels: Sequence, dtype: DtypeLike = np.float32) -> None:
1289 """
1290 Args:
1291 orig_labels: original labels that map to others.
1292 target_labels: expected label values, 1: 1 map to the `orig_labels`.
1293 dtype: convert the output data to dtype, default to float32.
1294 if dtype is from PyTorch, the transform will use the pytorch backend, else with numpy backend.
1295
1296 """
1297 if len(orig_labels) != len(target_labels):
1298 raise ValueError("orig_labels and target_labels must have the same length.")
1299
1300 self.orig_labels = orig_labels
1301 self.target_labels = target_labels
1302 self.pair = tuple((o, t) for o, t in zip(self.orig_labels, self.target_labels) if o != t)
1303 type_dtype = type(dtype)
1304 if getattr(type_dtype, "__module__", "") == "torch":
1305 self.use_numpy = False
1306 self.dtype = get_equivalent_dtype(dtype, data_type=torch.Tensor)
1307 else:
1308 self.use_numpy = True
1309 self.dtype = get_equivalent_dtype(dtype, data_type=np.ndarray)
1310
1311 def __call__(self, img: NdarrayOrTensor):
1312 if self.use_numpy:
1313 img_np, *_ = convert_data_type(img, np.ndarray)
1314 _out_shape = img_np.shape
1315 img_flat = img_np.flatten()
1316 try:
1317 out_flat = img_flat.astype(self.dtype)
1318 except ValueError:
1319 # can't copy unchanged labels as the expected dtype is not supported, must map all the label values
1320 out_flat = np.zeros(shape=img_flat.shape, dtype=self.dtype)
1321 for o, t in self.pair:
1322 out_flat[img_flat == o] = t
1323 out_t = out_flat.reshape(_out_shape)
1324 else:
1325 img_t, *_ = convert_data_type(img, torch.Tensor)
1326 out_t = img_t.detach().clone().to(self.dtype) # type: ignore
1327 for o, t in self.pair:
1328 out_t[img_t == o] = t
1329 out, *_ = convert_to_dst_type(src=out_t, dst=img, dtype=self.dtype)
1330 return out
1331
1332
1333class IntensityStats(Transform):

Callers 4

__init__Method · 0.90
__init__Method · 0.90
test_datasetMethod · 0.90
test_shapeMethod · 0.90

Calls

no outgoing calls

Tested by 2

test_datasetMethod · 0.72
test_shapeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…