Patch Filter
Patch filtering strategies.
MaskPatchFilter
Bases: PatchFilter
Filter patches based on a boolean image mask.
Evaluates patches by computing the fraction of True pixels in a boolean mask and filters out patches that fall below a coverage threshold.
Parameters:
-
coverage(float) –Minimum percentage of masked pixels (True values) required to keep a patch. Must be between 0 and 1.
Attributes:
-
coverage(float) –Minimum percentage of masked pixels required to keep a patch.
__init__(coverage)
Create a MaskFilter.
This filter removes patches that fall below a threshold of masked pixels (True values) in a boolean mask. The mask is expected to be a boolean array where True represents regions of interest and False represents background.
Parameters:
-
coverage(float) –Minimum percentage of masked pixels (True values) required to keep a patch. Must be between 0 and 1.
Raises:
-
ValueError–If coverage is not between 0 and 1.
apply_filter(filter_map, threshold)
staticmethod
Apply the max filter to a filter map.
The filter map is the output of the filter_map method.
Parameters:
-
filter_map(ndarray) –The max filter map of the image.
-
threshold(float) –The threshold to apply to the filter map.
Returns:
-
NDArray[bool_]–A binary map where True indicates patches that pass the filter, i.e. they should be kept for training.
filter_map(image, patch_size)
staticmethod
Compute a filter map for the entire image based on the patch filtering criteria.
The filter map will show the percentage coverage of the mask in a patch.
Parameters:
-
image(NDArray[bool_]) –The boolean mask image to evaluate.
-
patch_size(Sequence[int]) –The size of the patches to consider (unused for mask filter).
Returns:
-
NDArray[bool_]–The mask image itself.
filter_out(patch)
Determine whether to filter out a patch based on mask coverage.
Parameters:
-
patch(NDArray[bool_]) –A boolean mask patch to evaluate. Expected to have dtype bool where True indicates regions of interest and False indicates background.
Returns:
-
bool–True if the patch should be filtered out, False otherwise.
plot_filter_map(image, filter_map, z_idx=None)
staticmethod
Plot the filter map over an image.
Parameters:
-
image(ndarray) –The image that has been evaluated.
-
filter_map(ndarray) –The filter map that has been evaluated using the method
filter_map. -
z_idx(int | None, default:None) –If the image is 3D,
z_idxselects the slice to display. IfNonethe central slice will be selected.
Returns:
-
Figure–The figure object displaying the filter map.
MaxPatchFilter
Bases: PatchFilter
Patch filter based on thresholding the maximum filter (CSBDeep-inspired).
Parameters:
-
threshold(float) –Maximum-filter threshold; patches below are filtered out.
-
coverage(float, default:0.25) –Ratio of pixels below threshold to filter out (0-1).
Attributes:
-
threshold(float) –Threshold for the maximum filter of the patch.
__init__(threshold, coverage=0.25)
apply_filter(filter_map, threshold)
staticmethod
Apply the max filter to a filter map.
The filter map is the output of the filter_map method.
Parameters:
-
filter_map(ndarray) –The max filter map of the image.
-
threshold(float) –The threshold to apply to the filter map.
Returns:
-
NDArray[bool_]–A binary map where True indicates patches that pass the filter, i.e. they should be kept for training.
filter_map(image, patch_size, coverage=None)
staticmethod
Compute a filter map for the entire image based on the patch filtering criteria.
The filter map will show the threshold that, above which, will result in regions of the image being excluded from training.
Parameters:
-
image(ndarray) –A 2D or 3D image.
-
patch_size(sequence of int) –The patch size intended to be used for training.
-
coverage(float | None, default:None) –If the ratio of pixels above the threshold is below this value, the patch will be filtered out. If
None, for 2D it will be 0.25 and for 3D it will be 0.125.
Returns:
-
ndarray–The filter map, which has the same shape as the input image.
filter_out(patch)
Return True if patch should be filtered out by max-filter criteria.
Parameters:
-
patch(ndarray) –Image patch to evaluate.
Returns:
-
bool–True if patch should be filtered out, False otherwise.
plot_filter_map(image, filter_map, z_idx=None)
staticmethod
Plot the filter map over an image.
Parameters:
-
image(ndarray) –The image that has been evaluated.
-
filter_map(ndarray) –The filter map that has been evaluated using the method
filter_map. -
z_idx(int | None, default:None) –If the image is 3D,
z_idxselects the slice to display. IfNonethe central slice will be selected.
Returns:
-
Figure–The figure object displaying the filter map.
MeanStdPatchFilter
Bases: PatchFilter
Filter patches based on mean and standard deviation thresholds.
Parameters:
-
mean_threshold(float) –Threshold for the mean of the patch.
-
std_threshold(float, default:None) –Threshold for the standard deviation of the patch. If None, then no standard deviation filtering is applied.
Attributes:
-
mean_threshold(float) –Threshold for the mean of the patch.
-
std_threshold(float) –Threshold for the standard deviation of the patch.
__init__(mean_threshold, std_threshold=None)
Create a MeanStdPatchFilter.
This filter removes patches whose mean and standard deviation are both below
specified thresholds. The filtering is applied with a probability p, allowing
for stochastic filtering.
Parameters:
-
mean_threshold(float) –Threshold for the mean of the patch.
-
std_threshold(float | None, default:None) –Threshold for the standard deviation of the patch. If None, then no standard deviation filtering is applied.
Raises:
-
ValueError–If mean_threshold or std_threshold is negative.
-
ValueError–If std_threshold is negative.
apply_filter(filter_map, mean_threshold, std_threshold=None)
staticmethod
Apply mean and std thresholds to a filter map.
The filter map is the output of the filter_map method.
Parameters:
-
filter_map(ndarray) –Stacked mean and std maps of the image.
-
mean_threshold(float) –Threshold for the mean of the patch.
-
std_threshold(float | None, default:None) –Threshold for the standard deviation of the patch. If None, then no standard deviation filtering is applied.
Returns:
-
NDArray[bool_]–A binary map where True indicates patches that pass the filter, i.e. they should be kept for training.
filter_map(image, patch_size)
staticmethod
Compute a filter map for the entire image based on the patch filtering criteria.
The filter map will show the threshold that, above which, will result in regions of the image being excluded from training.
Parameters:
-
image(ndarray) –A 2D or 3D image.
-
patch_size(sequence of int) –The patch size intended to be used for training.
Returns:
-
ndarray–Stacked mean and std filter maps of the image.
filter_out(patch)
Determine whether to filter out a patch based on mean and std thresholds.
Parameters:
-
patch(NDArray) –The image patch to evaluate.
Returns:
-
bool–True if the patch should be filtered out, False otherwise.
plot_filter_map(image, filter_map, z_idx=None)
staticmethod
Plot both the mean and standard-deviation filter over an image.
Parameters:
-
image(ndarray) –The image that has been evaluated.
-
filter_map(ndarray) –The filter map that has been evaluated using the method
filter_map. -
z_idx(int | None, default:None) –If the image is 3D,
z_idxselects the slice to display. IfNonethe central slice will be selected.
Returns:
-
Figure–The figure object displaying the filter maps.
PatchFilter
Bases: Protocol
Interface for implementing patch filtering strategies.
filter_map(image, patch_size, *args, **kwargs)
staticmethod
Compute a filter map for the entire image based on the patch filtering criteria.
Parameters:
-
image(ndarray) –The full image to evaluate.
-
patch_size(Sequence[int]) –The size of the patches to consider.
-
*args(Any, default:()) –Concrete implementations may have additional positional arguments.
-
**kwargs(Any, default:{}) –Concrete implementations may have additional key-word arguments.
Returns:
-
NDArray–A map where each element is the .
filter_out(patch)
Determine whether to filter out a given patch.
Parameters:
-
patch(NDArray) –The image patch to evaluate.
Returns:
-
bool–True if the patch should be filtered out (excluded), False otherwise.
plot_filter_map(image, filter_map, z_idx=None)
staticmethod
Plot the filter map over an image.
Parameters:
-
image(ndarray) –The image that has been evaluated.
-
filter_map(ndarray) –The filter map that has been evaluated using the method
filter_map. -
z_idx(int | None, default:None) –If the image is 3D,
z_idxselects the slice to display. IfNonethe central slice will be selected.
Returns:
-
Figure–The figure object displaying the filter map.
ShannonPatchFilter
Bases: PatchFilter
Filter patches based on Shannon entropy threshold.
Parameters:
-
threshold(float) –Shannon entropy threshold; patches below are filtered out.
Attributes:
-
threshold(float) –Threshold for the Shannon entropy of the patch.
__init__(threshold)
Create a ShannonPatchFilter.
This filter removes patches whose Shannon entropy is below a specified threshold.
Parameters:
-
threshold(float) –Threshold for the Shannon entropy of the patch.
Raises:
-
ValueError–If threshold is negative.
apply_filter(filter_map, threshold)
staticmethod
Apply the Shannon entropy filter to a precomputed filter map.
The filter map is the output of the filter_map method.
Parameters:
-
filter_map(NDArray) –The precomputed Shannon entropy map of the image.
-
threshold(float) –The Shannon entropy threshold for filtering.
Returns:
-
NDArray[bool_]–A binary map where True indicates patches that pass the filter, i.e. they should be kept for training.
filter_map(image, patch_size)
staticmethod
Compute a filter map for the entire image based on the patch filtering criteria.
The filter map will show the threshold that, above which, will result in regions of the image being excluded from training.
Parameters:
-
image(ndarray) –A 2D or 3D image.
-
patch_size(sequence of int) –The patch size intended to be used for training.
Returns:
-
ndarray–The filter map, which has the same shape as the input image.
filter_out(patch)
Determine whether to filter out a patch based on its Shannon entropy.
Parameters:
-
patch(NDArray) –The patch to evaluate.
Returns:
-
bool–True if the patch should be filtered out, False otherwise.
plot_filter_map(image, filter_map, z_idx=None)
staticmethod
Plot the filter map over an image.
Parameters:
-
image(ndarray) –The image that has been evaluated.
-
filter_map(ndarray) –The filter map that has been evaluated using the method
filter_map. -
z_idx(int | None, default:None) –If the image is 3D,
z_idxselects the slice to display. IfNonethe central slice will be selected.
Returns:
-
Figure–The figure object displaying the filter map.
create_patch_filter(filter_config)
Factory function to create patch filter instances based on the filter name.
Parameters:
-
filter_config(FilterConfig) –Pydantic config of the filter to be created.
Returns:
-
PatchFilter–Instance of the requested patch filter.