Bases: Transform
Keep only the largest connected component of each label.
For each specified label value, connected-component analysis is
performed and all but the largest component are removed (set to
the background value). This is useful for cleaning up noisy
segmentation predictions.
Only single-channel LabelMap images are
affected.
Parameters:
| Name |
Type |
Description |
Default |
labels
|
Sequence[int] | None
|
Label values to filter. None means all non-zero
labels found in the data.
|
None
|
background_label
|
int
|
Value used for removed components.
|
0
|
fully_connected
|
bool
|
If True, use 26-connectivity (voxels
sharing a corner are connected). If False, use
6-connectivity (face-connected only).
|
True
|
**kwargs
|
Any
|
|
{}
|
Raises:
| Type |
Description |
RuntimeError
|
If a label map has more than one channel.
|
Examples:
>>> import torchio as tio
>>> transform = tio.KeepLargestComponent()
>>> transform = tio.KeepLargestComponent(labels=[1, 2])
Source code in src/torchio/transforms/label/keep_largest.py
| class KeepLargestComponent(Transform):
r"""Keep only the largest connected component of each label.
For each specified label value, connected-component analysis is
performed and all but the largest component are removed (set to
the background value). This is useful for cleaning up noisy
segmentation predictions.
Only single-channel [`LabelMap`][torchio.LabelMap] images are
affected.
Args:
labels: Label values to filter. `None` means all non-zero
labels found in the data.
background_label: Value used for removed components.
fully_connected: If `True`, use 26-connectivity (voxels
sharing a corner are connected). If `False`, use
6-connectivity (face-connected only).
**kwargs: See [`Transform`][torchio.Transform].
Raises:
RuntimeError: If a label map has more than one channel.
Examples:
>>> import torchio as tio
>>> transform = tio.KeepLargestComponent()
>>> transform = tio.KeepLargestComponent(labels=[1, 2])
"""
def __init__(
self,
labels: Sequence[int] | None = None,
*,
background_label: int = 0,
fully_connected: bool = True,
**kwargs: Any,
) -> None:
super().__init__(**kwargs)
self.labels = list(labels) if labels is not None else None
self.background_label = background_label
self.fully_connected = fully_connected
def make_params(self, batch: SubjectsBatch) -> dict[str, Any]:
"""No random parameters."""
return {}
def apply_transform(
self,
batch: SubjectsBatch,
params: dict[str, Any],
) -> SubjectsBatch:
"""Keep only the largest connected component per label."""
for _name, img_batch in batch.images.items():
if not issubclass(img_batch._image_class, LabelMap):
continue
b, c = img_batch.data.shape[:2]
if c != 1:
msg = (
"KeepLargestComponent requires single-channel"
f" label maps, got {c} channels"
)
raise RuntimeError(msg)
for i in range(b):
img_batch.data[i, 0] = _keep_largest_per_label(
img_batch.data[i, 0],
labels=self.labels,
background_label=self.background_label,
fully_connected=self.fully_connected,
)
return batch
|
invertible
property
Whether this transform can be inverted.
forward(data)
forward(data: Subject) -> Subject
forward(data: Image) -> Image
forward(data: Tensor) -> Tensor
forward(data: np.ndarray) -> np.ndarray
forward(data: sitk.Image) -> sitk.Image
forward(data: nib.Nifti1Image) -> nib.Nifti1Image
forward(data: dict) -> dict
forward(data: ImagesBatch) -> ImagesBatch
forward(data: SubjectsBatch) -> SubjectsBatch
Apply the transform.
The output type always matches the input type.
Parameters:
| Name |
Type |
Description |
Default |
data
|
Any
|
|
required
|
Source code in src/torchio/transforms/transform.py
| def forward(self, data: Any) -> Any:
"""Apply the transform.
The output type always matches the input type.
Args:
data: Input data to transform.
"""
if self.copy:
data = _copy.deepcopy(data)
batch, unwrap = self._wrap(data)
if torch.rand(1).item() > self.p:
return unwrap(batch)
params = self.make_params(batch)
batch = self.apply_transform(batch, params)
# Record history on the batch
trace = AppliedTransform(name=type(self).__name__, params=params)
if not hasattr(batch, "applied_transforms"):
batch.applied_transforms = []
batch.applied_transforms.append(trace)
result = unwrap(batch)
# Propagate history to outputs that can carry it
if (
hasattr(batch, "applied_transforms")
and not isinstance(result, (SubjectsBatch, Tensor, np.ndarray))
and not isinstance(result, dict)
):
with contextlib.suppress(AttributeError):
result.applied_transforms = list(batch.applied_transforms)
return result
|
inverse(params)
Return a transform that undoes this one.
Override in invertible subclasses. The returned transform,
when applied, reverses the effect of the forward pass with
the given parameters.
Parameters:
| Name |
Type |
Description |
Default |
params
|
dict[str, Any]
|
The parameters recorded in the forward pass.
|
required
|
Returns:
| Type |
Description |
Transform
|
A new Transform instance that inverts this one.
|
Source code in src/torchio/transforms/transform.py
| def inverse(self, params: dict[str, Any]) -> Transform:
"""Return a transform that undoes this one.
Override in invertible subclasses. The returned transform,
when applied, reverses the effect of the forward pass with
the given parameters.
Args:
params: The parameters recorded in the forward pass.
Returns:
A new `Transform` instance that inverts this one.
"""
msg = f"{type(self).__name__} is not invertible"
raise NotImplementedError(msg)
|
to_hydra()
Export as a Hydra-compatible config dict.
Returns a dict with _target_ set to the fully qualified
class name and only non-default field values included.
Returns:
| Type |
Description |
dict[str, Any]
|
Dict suitable for hydra.utils.instantiate().
|
Source code in src/torchio/transforms/transform.py
| def to_hydra(self) -> dict[str, Any]:
"""Export as a Hydra-compatible config dict.
Returns a dict with `_target_` set to the fully qualified
class name and only non-default field values included.
Returns:
Dict suitable for `hydra.utils.instantiate()`.
"""
from .parameter_range import _ParameterRange
cls = type(self)
target = f"torchio.{cls.__qualname__}"
cfg: dict[str, Any] = {"_target_": target}
for name, default in _collect_init_params(cls).items():
value = getattr(self, name, default)
if isinstance(value, _ParameterRange):
if value._original == default:
continue
value = _hydra_value(value._original)
elif value == default:
continue
else:
value = _hydra_value(value)
cfg[name] = value
return cfg
|
make_params(batch)
No random parameters.
Source code in src/torchio/transforms/label/keep_largest.py
| def make_params(self, batch: SubjectsBatch) -> dict[str, Any]:
"""No random parameters."""
return {}
|
Keep only the largest connected component per label.
Source code in src/torchio/transforms/label/keep_largest.py
| def apply_transform(
self,
batch: SubjectsBatch,
params: dict[str, Any],
) -> SubjectsBatch:
"""Keep only the largest connected component per label."""
for _name, img_batch in batch.images.items():
if not issubclass(img_batch._image_class, LabelMap):
continue
b, c = img_batch.data.shape[:2]
if c != 1:
msg = (
"KeepLargestComponent requires single-channel"
f" label maps, got {c} channels"
)
raise RuntimeError(msg)
for i in range(b):
img_batch.data[i, 0] = _keep_largest_per_label(
img_batch.data[i, 0],
labels=self.labels,
background_label=self.background_label,
fully_connected=self.fully_connected,
)
return batch
|