PixelMaskCollection
eitprocessing.roi.pixelmaskcollection
¶
PixelMaskCollection
dataclass
¶
PixelMaskCollection(
masks: (
dict[Hashable, PixelMask]
| frozendict[Hashable, PixelMask]
| list[PixelMask]
| None
) = None,
*,
label: str | None = None
)
A collection of pixel masks, each representing a specific region of interest (ROI) in the EIT data.
This class allows for the application of multiple masks to numpy arrays, EIT data or pixel maps, enabling the extraction of specific regions based on the defined masks.
PixelMasks can be labelled with strings, or anonymous (label=None
). Either all or none of the masks should have a
label; it is not possible to mix labelled and unlabelled masks.
At initialization, masks can be provided as a list or a dictionary. If provided as a list, the masks are converted to a dictionary with either their labels as keys (if all masks have labels) or their indices as keys (if none have labels). If provided as a dictionary, the keys must match the labels of the masks.
When the apply
method is called, it applies each mask to the provided data and returns a dictionary
mapping each mask's key (label or index) to the resulting masked data.
Example:
>>> mask_collection = PixelMaskCollection(
[
PixelMask(right_lung_data, label="right lung"),
PixelMask(left_lung_data, label="left lung"),
],
label="lung masks",
)
PARAMETER | DESCRIPTION |
---|---|
masks
|
A dictionary mapping mask names to their corresponding pixel masks or a list of masks. |
label
|
An optional label for the collection of masks.
TYPE:
|
ATTRIBUTE | DESCRIPTION |
---|---|
is_anonymous |
A boolean indicating whether all masks in the collection are anonymous (i.e., have no label).
TYPE:
|
is_anonymous
property
¶
is_anonymous: bool
Check if all masks in the collection are anonymous (i.e., have no label).
add
¶
Return a new collection with one or more masks added.
Masks can be added as positional arguments or keyword arguments (non-anonymous masks only).
Example:
>>> left_lung_mask = PixelMask(data, label="left_lung")
>>> mask_collection.add(left_lung_mask) # equals mask_collection.add(left_lung=left_lung_mask)
PARAMETER | DESCRIPTION |
---|---|
*mask
|
One or more
TYPE:
|
overwrite
|
If True, allows overwriting existing masks with the same label or index.
TYPE:
|
**kwargs
|
Keyword arguments where the key is the label of the mask and the value is the mask itself.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
A new |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If no masks are provided, if trying to mix labelled and unlabelled masks, or if provided keys don't match the masks label. |
KeyError
|
If trying to overwrite an existing mask without setting |
combine
¶
Combine all masks in the collection into a single mask using the specified method.
The method can be either "sum" or "product". If "sum" is used, the masks are summed, generally resulting in the union of the represented ROIs. If "product" is used, the masks are multiplied, generally resulting in the intersection of the ROIs.
Example:
>>> pm1 = PixelMask([[True, False], [False, True]])
>>> pm2 = PixelMask([[True, True], [False, False]])
>>> collection = PixelMaskCollection([pm1, pm2])
>>> summed_mask = collection.combine(method="sum")
PixelMask(mask=array([[ 1., 1.], [nan, 1.]]))
PARAMETER | DESCRIPTION |
---|---|
method
|
The method to combine the masks. Defaults to "sum".
TYPE:
|
label
|
The label for the combined mask.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
PixelMask
|
A new
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the PixelMaskCollection is empty. |
ValueError
|
If an unsupported method is provided. |
apply
¶
apply(data, *, label_format: str | None = None, **kwargs)
Apply the masks to the provided data.
The input data can be a numpy array, EITData, or PixelMap. The method applies each mask in the collection to the data and returns a dictionary mapping each mask's key (label or index) to the resulting masked data.
If label_format
is provided, it should be a format string that includes {mask_label}
. This label will be
passed to the resulting objects, with the appropriate mask label applied. If label_format
is not provided, no
label will be provided.
Additional keyword arguments are passed to the update
of the EITData or PixelMap, if applicable. If the input
data is a numpy array, label_format
and additional keyword arguments are not applicable and will raise a
ValueError
.
Example:
>>> mask_collection = PixelMaskCollection([
PixelMask(data, label="right lung"),
PixelMask(data, label="left lung")
])
>>> mask_collection.apply(eit_data, label_format="masked {mask_label}")
{"right lung": EITData(label="masked right lung"), "left lung": EITData(label="masked left lung")}
PARAMETER | DESCRIPTION |
---|---|
data
|
The data to which the masks will be applied. |
label_format
|
A format string to create the label of the returned EITData or PixelMap objects.
TYPE:
|
**kwargs
|
Additional keyword arguments to pass to the
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
A dictionary mapping each mask's key (label or index) to the resulting masked data. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the PixelMaskCollection is empty. |
ValueError
|
If a label is passed as a keyword argument. |
ValueError
|
If label_format or additional keyword arguments are provided when the input data is a numpy array. |
ValueError
|
If provided label format does not contain '{mask_label}'. |
TypeError
|
If provided data is not an array, EITData, or PixelMap. |