biapy.data.post_processing.post_processing

Post-processing utilities for image and mask data in BiaPy.

This module provides functions for instance segmentation refinement, watershed segmentation, morphological filtering, synapse and point detection, ensemble predictions, and other post-processing operations for 2D and 3D biomedical images. It supports advanced morphological measurements, filtering, and visualization tools to improve segmentation results and extract quantitative information from model outputs.

biapy.data.post_processing.post_processing.watershed_by_channels(data: ndarray[tuple[int, ...], dtype[_ScalarType_co]], channels: List[str], seed_channels: List[str], seed_channel_ths: List[float | int], topo_surface_channel: str, growth_mask_channels: List[str], growth_mask_channel_ths: List[float | int], remove_before: bool = False, thres_small_before: int = 10, seed_morph_sequence: List[str] = [], seed_morph_radius: List[int] = [], erode_and_dilate_growth_mask: bool = False, fore_erosion_radius: int = 5, fore_dilation_radius: int = 5, resolution: List[float | int] = [1.0, 1.0, 1.0], watershed_by_2d_slices: bool = False, save_dir: str | None = None, verbose: bool = True)[source]

Convert binary foreground probability maps and instance contours to instance masks via watershed segmentation algorithm.

Implementation based on PyTorch Connectomics’ process.py.

Parameters:
  • data (4D Numpy array) – Binary foreground labels and contours data to apply watershed into. E.g. (397, 1450, 2000, 2).

  • channels (str) – Channel type used. Possible options: A, C, BC, BCM, BCD, BCDv2, Dv2 and BDv2.

  • ths (float, optional) – Thresholds to be used on each channel. TH_BINARY_MASK used in the semantic mask to create watershed seeds; TH_CONTOUR used in the contours to create watershed seeds; TH_FOREGROUND used in the semantic mask to create the foreground mask; TH_POINTS used in the point mask to create watershed seeds; TH_DISTANCE used in the distances to create watershed seeds.

  • remove_before (bool, optional) – To remove objects before watershed.

  • thres_small_before (int, optional) – Theshold to remove small objects created by the watershed.

  • seed_morph_sequence (List of str, optional) – List of strings to determine the morphological filters to apply to instance seeds. They will be done in that order. E.g. ['dilate','erode'].

  • seed_morph_radius (List of ints, optional) – List of ints to determine the radius of the erosion or dilation for instance seeds.

  • erode_and_dilate_growth_mask (bool, optional) – To erode and dilate the growth mask before using marker controlled watershed. The idea is to remove the small holes that may be produced so the instances grow without them.

  • fore_erosion_radius (int, optional) – Radius to erode the growth mask.

  • fore_dilation_radius (int, optional) – Radius to dilate the growth mask.

  • resolution (tuple of int/float) – Resolution of the data in (z,y,x) to calibrate coordinates. E.g. [30,8,8].

  • watershed_by_2d_slices (bool, optional) – Whether to apply or not the watershed to create instances slice by slice in a 3D problem. This can solve instances invading others if the objects in Z axis overlap too much.

  • save_dir (str, optional) – Directory to save watershed output into.

class biapy.data.post_processing.post_processing.Embedding_cluster(device: device, patch_size: List[int] = [32, 1024, 1024], anisotropy: List[float | int] = [1, 1, 1], ndims: int = 2)[source]

Bases: object

create_instances(pred: ndarray[tuple[int, ...], dtype[_ScalarType_co]], fg_thresh: float = 0.7, min_mask_sum: int = 0, min_unclustered_sum: int = 0, min_object_size: int = 0) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]

Create instances from predicted offsets, per-axis sigmas, and seediness using the EmbedSeg method.

Parameters:
  • pred (NDArray) – Model predictions with shape: - 2D: (Y, X, C=5) with channels [off_y, off_x, sig_y, sig_x, seed] - 3D: (Z, Y, X, C=7) with channels [off_z, off_y, off_x, sig_z, sig_y, sig_x, seed]

  • fg_thresh (float, optional) – Foreground threshold for seediness map to consider pixels for clustering.

  • min_mask_sum (int, optional) – Minimum number of foreground pixels required to perform clustering.

  • min_unclustered_sum (int, optional) – Minimum number of unclustered foreground pixels to continue clustering.

  • min_object_size (int, optional) – Minimum size of objects to be considered valid. Objects smaller than this will be ignored.

Returns:

Instance labels with shape (*spatial,), dtype uint8 if max(label) ≀ 255 else uint16.

Return type:

NDArray

biapy.data.post_processing.post_processing.connect_pre_post_synapse_points_by_distance(pre_points_df: DataFrame, pre_points: ndarray[tuple[int, ...], dtype[_ScalarType_co]], post_points_df: DataFrame, post_points: ndarray[tuple[int, ...], dtype[_ScalarType_co]], out_dir: str | None = None)[source]

Connect pre and post synaptic points by distance. For each pre point, find the closest post point and assign it as a synapse.

Parameters:
  • pre_points_df (pd.DataFrame) – DataFrame containing the pre synaptic points information. Must have a column β€œpre_id” with the unique identifier of each pre point.

  • pre_points (NDArray) – Array containing the coordinates of the pre synaptic points. Shape (N_pre, 3) for 3D data or (N_pre, 2) for 2D data.

  • post_points_df (pd.DataFrame) – DataFrame containing the post synaptic points information. Must have a column β€œpost_id” with the unique identifier of each post point.

  • post_points (NDArray) – Array containing the coordinates of the post synaptic points. Shape (N_post, 3) for 3D data or (N_post, 2) for 2D data.

  • out_dir (str, optional) – Directory to save the output data into. If None, the output data will not be saved.

biapy.data.post_processing.post_processing.create_synapses_from_point_probs(data: ndarray[tuple[int, ...], dtype[_ScalarType_co]], channels: List[str], point_creation_func: str = 'peak_local_max', min_th_to_be_peak: List[float] = [0.2], min_distance: int = 1, min_sigma: int = 5, max_sigma: int = 10, num_sigma: int = 2, exclude_border: bool = False, relative_th_value: bool = False, out_dir: str | None = None, filenames: List[str] | None = None, verbose: bool = True) Tuple[DataFrame, ndarray[tuple[int, ...], dtype[_ScalarType_co]], DataFrame, ndarray[tuple[int, ...], dtype[_ScalarType_co]]][source]

Create synapses pre/post points from the given data.

Find more info regarding min_distance, min_sigma and exclude_border arguments in peak_local_max and blob_log functions.

Parameters:
  • data (4D Numpy array) – Data to extract synapse points from. E.g. (397, 1450, 2000, 2).

  • channels (List of str) – Channel type used.

  • point_creation_func (str, optional) – Function to be used in the pre/post point creation. Options: [β€œpeak_local_max”, β€œblob_log”]

  • min_th_to_be_peak (List of float, optional) – Minimum values for each channel in data to be used as the minimum when creating the points.

  • min_distance (int, optional) – The minimal allowed distance separating peaks. To find the maximum number of peaks, use min_distance=1.

  • min_sigma (int, optional) – The minimum standard deviation for Gaussian kernel. Keep this low to detect smaller blobs. The standard deviations of the Gaussian filter are given for each axis as a sequence, or as a single number, in which case it is equal for all axes.

  • max_sigma (int, optional) – The maximum standard deviation for Gaussian kernel. Keep this high to detect larger blobs. The standard deviations of the Gaussian filter are given for each axis as a sequence, or as a single number, in which case it is equal for all axes.

  • num_sigma (int, optional) – The number of intermediate values of standard deviations to consider between min_sigma and max_sigma.

  • exclude_border (bool, optional) – To exclude border-pixels of the border of the image.

  • relative_th_value (bool, optional) – To use min_th_to_be_peak as a relative threshold to the maximum value, i.e. threshold_rel is set instead of threshold_abs in the peak_local_max or blob_log call.

  • out_dir (str, optional) – Directory to save the output data into. If None, the output data will not be saved.

  • filenames (List of str, optional) – List of filenames to save the output data into. If None, the output data will not be saved. E.g. ["pred_points.tif"].

  • verbose (bool, optional) – Whether to print the progress of the function or not.

Returns:

pre_points_df: DataFrame with pre points. pre_points: Array with pre points. post_points_df: DataFrame with post points. post_points: Array with post points.

Return type:

Tuple[pd.DataFrame, NDArray, pd.DataFrame, NDArray]

biapy.data.post_processing.post_processing.extract_points_in_predictions(data: ndarray[tuple[int, ...], dtype[_ScalarType_co]], point_type: str, point_creation_func: str = 'peak_local_max', min_th_to_be_peak: float = 0.2, min_distance: int = 1, min_sigma: int = 5, max_sigma: int = 10, num_sigma: int = 2, exclude_border: bool = False, relative_th_value: bool = False, out_dir: str | None = None, filenames: List[str] | None = None, verbose: bool = True) Tuple[DataFrame, ndarray[tuple[int, ...], dtype[_ScalarType_co]]][source]

Extract points from predicted data.

Find more info regarding min_distance, min_sigma and exclude_border arguments in peak_local_max and blob_log functions.

Parameters:
  • data (3D Numpy array) – Data to extract points from. E.g. (397, 1450, 2000).

  • point_type (str) – Type of points to be extracted. Options: [β€œcleft”, β€œpre”, β€œpost”].

  • point_creation_func (str, optional) – Function to be used in the pre/post point creation. Options: [β€œpeak_local_max”, β€œblob_log”]

  • min_th_to_be_peak (float, optional) – Minimum value in data to be used as the minimum when creating the points.

  • min_distance (int, optional) – The minimal allowed distance separating peaks. To find the maximum number of peaks, use min_distance=1.

  • min_sigma (int, optional) – The minimum standard deviation for Gaussian kernel. Keep this low to detect smaller blobs. The standard deviations of the Gaussian filter are given for each axis as a sequence, or as a single number, in which case it is equal for all axes.

  • max_sigma (int, optional) – The maximum standard deviation for Gaussian kernel. Keep this high to detect larger blobs. The standard deviations of the Gaussian filter are given for each axis as a sequence, or as a single number, in which case it is equal for all axes.

  • num_sigma (int, optional) – The number of intermediate values of standard deviations to consider between min_sigma and max_sigma.

  • exclude_border (bool, optional) – To exclude border-pixels of the border of the image.

  • relative_th_value (bool, optional) – To use min_th_to_be_peak as a relative threshold to the maximum value, i.e. threshold_rel is set instead of threshold_abs in the peak_local_max or blob_log call.

  • out_dir (str, optional) – Directory to save the output data into. If None, the output data will not be saved.

  • filenames (List of str, optional) – List of filenames to save the output data into. If None, the output data will not be saved. E.g. ["pred_points.tif"].

  • verbose (bool, optional) – Whether to print the progress of the function or not.

Returns:

pred_points_df: DataFrame with predicted points. pred_points: Array with predicted points.

Return type:

Tuple[pd.DataFrame, NDArray, pd.DataFrame, NDArray]

biapy.data.post_processing.post_processing.collect_point_type_csv_files(csv_dir: str, filename: str, point_type: str, min_th_to_be_peak: float = 0.2, th_type: str = 'manual') Dict[str, float | DataFrame][source]

Collect all CSV files in the given directory that match the given filename and point type.

Parameters:
  • csv_dir (str) – Directory where the CSV files are located.

  • filename (str) – Base filename to look for in the CSV directory (without the extension). E.g. β€œpred_points”.

  • point_type (str) – Type of points to look for. Options: [β€œcleft”, β€œpre”, β€œpost”].

Returns:

Dictionary with the collected CSV files and their corresponding threshold.

Return type:

Dict[str, float | pd.DataFrame]

biapy.data.post_processing.post_processing.extract_synapse_connectivity(filename: str, reuse_predictions: bool, csv_dir: str, min_th_to_be_peak: float = 0.2, th_type: str = 'manual', verbose: bool = True) Tuple[DataFrame, ndarray[tuple[int, ...], dtype[_ScalarType_co]], float, DataFrame, ndarray[tuple[int, ...], dtype[_ScalarType_co]], float][source]

Create global pre/post points CSV files from the local ones. If reuse_predictions is True, it will look for the already created global CSV files and load them instead of creating them again. It creates a mapping between pre and post points by assigning each post point to the closest pre point. The output CSV files will contain the pre/post points and their corresponding probabilities, as well as a mapping CSV file that contains the pre/post point ids.

Parameters:
  • filename (str) – Base filename to look for in the CSV directory (without the extension). E.g. β€œpred_points”.

  • reuse_predictions (bool) – Whether to reuse the already created pre/post points CSV files.

  • csv_dir (str) – Directory where the predicted points CSV files are located and where the output CSV files will be saved.

  • min_th_to_be_peak (float, optional) – Minimum threshold to be considered a peak when creating the pre/post points. If th_type is β€œrelative”, this value will be multiplied by the maximum probability value in the predicted points to get the actual threshold.

  • th_type (str, optional) – Type of threshold to be used when creating the pre/post points. Options: [β€œmanual”, β€œrelative”]. If β€œmanual”, the min_th_to_be_peak value will be used as the actual threshold. If β€œrelative”, the min_th_to_be_peak value will be multiplied by the maximum probability value in the predicted points to get the actual threshold.

  • verbose (bool, optional) – Whether to print the progress of the function or not.

Returns:

pre_points_df: DataFrame with pre points. pre_points: Array with pre points. pre_th: Threshold used for pre points. post_points_df: DataFrame with post points. post_points: Array with post points. post_th: Threshold used for post points.

Return type:

Tuple[pd.DataFrame, NDArray, float, pd.DataFrame, NDArray, float]

biapy.data.post_processing.post_processing.extract_synful_synapses(data: ndarray, channels: List[str] = ['F_post', 'Z', 'V', 'H'], threshold_abs: float = 0.2, min_distance: int = 1, cluster_distance: float = 5.0, out_dir: str | None = None, verbose: bool = True) Tuple[DataFrame, ndarray, DataFrame, ndarray][source]

Create synapses pre/post points from Synful vector predictions.

Parameters:
  • data (4D Numpy array) – Model predictions. E.g. (397, 1450, 2000, 4).

  • channels (List of str) – Channel layout. Default is [β€œF_post”, β€œZ”, β€œV”, β€œH”].

  • threshold_abs (float, optional) – Minimum probability in F_post to be considered a post-synaptic site.

  • min_distance (int, optional) – The minimal allowed distance separating post-synaptic peaks.

  • cluster_distance (float, optional) – Maximum voxel distance to cluster projected pre-sites into the same unique T-bar.

  • out_dir (str, optional) – Directory to save the CSV outputs. If None, nothing is saved to disk.

  • verbose (bool, optional) – Print progress.

Returns:

pre_points_df: DataFrame with pre points. pre_points: Array with pre points. post_points_df: DataFrame with post points. post_points: Array with post points.

Return type:

Tuple[pd.DataFrame, NDArray, pd.DataFrame, NDArray]

biapy.data.post_processing.post_processing.apply_median_filtering(data: ndarray[tuple[int, ...], dtype[_ScalarType_co]], axes: str = 'xy', mf_size: int = 5) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]

Apply a median filtering to the specified axes of the provided data.

Parameters:
  • data (4D/5D Numpy array) – Data to apply the filter to. E.g. (num_of_images, y, x, channels) for 2D and (num_of_images, z, y, x, channels) for 3D.

  • axes (str, optional) – Median filters to apply. There are multiple options: 'xy' or 'yx' to apply the filter in x and y axes together; 'zy' or 'yz' to apply the filter in y and z axes together; 'zx' or 'xz': to apply the filter in x and z axes together; 'z': to apply the filter only in z axis.

  • mf_size (int, optional) – Size of the median filter. If an odd number is not provided, 1 will be added to ensure it remains odd.

Returns:

Array – Filtered data. E.g. (num_of_images, y, x, channels) for 2D and (num_of_images, z, y, x, channels) for 3D.

Return type:

4D/5D Numpy array

biapy.data.post_processing.post_processing.ensemble8_2d_predictions(o_img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], pred_func: Callable, axes_order_back: Tuple[int, ...], axes_order: Tuple[int, ...], device: device, batch_size_value: int = 1, mode='mean') Tensor | Dict[source]

Output the mean prediction of a given image generating its 8 possible rotations and flips.

Parameters:
  • o_img (3D Numpy array) – Input image. E.g. (y, x, channels).

  • pred_func (function) – Function to make predictions.

  • axes_order_back (tuple) – Axis order to convert from tensor to numpy. E.g. (0,3,1,2).

  • axes_order (tuple) – Axis order to convert from numpy to tensor. E.g. (0,3,1,2).

  • device (Torch device) – Device used.

  • batch_size_value (int, optional) – Batch size value.

  • mode (str, optional) – Ensemble mode. Possible options: β€œmean”, β€œmin”, β€œmax”.

Returns:

out – Output of the model with the pred key assembled.

Return type:

dict

biapy.data.post_processing.post_processing.ensemble16_3d_predictions(vol: ndarray[tuple[int, ...], dtype[_ScalarType_co]], pred_func: Callable, axes_order_back: Tuple[int, ...], axes_order: Tuple[int, ...], device: device, batch_size_value: int = 1, mode: str = 'mean') Tensor | Dict[source]

Output the mean prediction of a given image generating its 16 possible rotations and flips.

Parameters:
  • o_img (4D Numpy array) – Input image. E.g. (z, y, x, channels).

  • pred_func (function) – Function to make predictions.

  • axes_order_back (tuple) – Axis order to convert from tensor to numpy. E.g. (0,3,1,2,4).

  • axes_order (tuple) – Axis order to convert from numpy to tensor. E.g. (0,3,1,2).

  • device (Torch device) – Device used.

  • batch_size_value (int, optional) – Batch size value.

  • mode (str, optional) – Ensemble mode. Possible options: β€œmean”, β€œmin”, β€œmax”.

Returns:

out – Output of the model with the pred key assembled.

Return type:

dict

biapy.data.post_processing.post_processing.create_th_plot(ths: List[float], y_list: list[int | float], chart_dir: str, th_name: str = 'TH_BINARY_MASK', per_sample: bool = True, ideal_value: int | float | None = None)[source]

Create plots for threshold value calculation.

Parameters:
  • ths (List of floats) – List of thresholds. It will be the x axis.

  • y_list (List of ints/floats) – Values of y axis.

  • chart_dir (str, optional) – Path where the charts are stored.

  • th_name (str, optional) – Name of the threshold.

  • per_sample (bool, optional) – Create the plot per list in y_list.

  • ideal_value (int/float, optional) – Value that should be the ideal optimum. It is going to be marked with a red line in the chart.

biapy.data.post_processing.post_processing.voronoi_on_mask(data: ndarray[tuple[int, ...], dtype[_ScalarType_co]], mask: ndarray[tuple[int, ...], dtype[_ScalarType_co]], th: float = 0, erode_size: int = 2, verbose: bool = False) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]

Apply Voronoi to the voxels not labeled yet marked by the mask. It is done using distances from the un-labeled voxels to the cell perimeters.

Parameters:
  • data (2D/3D Numpy array) – Data to apply Voronoi. (y, x) for 2D or (z, y, x) for 3D. E.g. (397, 1450, 2000) for 3D.

  • mask (2D/3D Numpy array) – Data mask to determine which points need to be proccessed. (y, x) for 2D or (z, y, x) for 3D.``(z, y, x)`` e.g. (397, 1450, 2000).

  • th (float, optional) – Threshold used to binarize the input. If th=0, otsu threshold is used.

  • erode_size (int, optional) – Size of the erosion to apply to the binary mask before Voronoi.

  • verbose (bool, optional) – To print saving information.

Returns:

data – Image with Voronoi applied. (num_of_images, z, y, x) e.g. (1, 397, 1450, 2000)

Return type:

4D Numpy array

biapy.data.post_processing.post_processing.remove_close_points_by_mask(points: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | List[List[float | int]], radius: float, raw_predictions: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | Type[Group] | Type[Array] | Array | Dataset, bin_th: float, resolution: List[float | int], channel_to_look_into: int = 1, classes: List[int] | None = None, ndim: int = 3, return_drops: bool = False) List[List[float | int]] | Tuple[List[List[float | int]], List[int]] | Tuple[List[List[float | int]], List[int], List[bool]][source]

Remove all points from point_list that are at a radius or less distance from each other but conditioned that the must lay in the same mask label. For that last label creation the given raw_predictions is used, which is expected to be model’s raw prediction. It is binarized using bin_th threshold and then the labels are created using connected-components.

Parameters:
  • points (ndarray of floats) – List of 3D points. E.g. ((0,0,0), (1,1,1).

  • radius (float) – Radius from each point to decide what points to keep. E.g. 10.0.

  • resolution (Tuple of int/float) – Resolution of the data, in (z,y,x) to calibrate coordinates. E.g. [30,8,8].

  • ndim (int, optional) – Number of dimension of the data.

  • return_drops (bool, optional) – Whether to return or not a list containing the positions of the points removed.

  • search_in_mask (bool, optional)

Returns:

new_point_list – New list of points after removing those at a distance of radius or less from each other.

Return type:

List of lists of floats

biapy.data.post_processing.post_processing.remove_close_points(points: ndarray[tuple[int, ...], dtype[_ScalarType_co]] | List[List[float | int]], radius: float, resolution: List[float | int], classes: List[int] | None = None, ndim: int = 3, return_drops: bool = False) List[List[float | int]] | Tuple[List[List[float | int]], List[int]] | Tuple[List[List[float | int]], List[int], List[bool]][source]

Remove all points from point_list that are at a radius or less distance from each other.

Parameters:
  • points (ndarray of floats) – List of 3D points. E.g. ((0,0,0), (1,1,1).

  • radius (float) – Radius from each point to decide what points to keep. E.g. 10.0.

  • resolution (Tuple of int/float) – Resolution of the data, in (z,y,x) to calibrate coordinates. E.g. [30,8,8].

  • ndim (int, optional) – Number of dimension of the data.

  • return_drops (bool, optional) – Whether to return or not a list containing the positions of the points removed.

Returns:

new_point_list – New list of points after removing those at a distance of radius or less from each other.

Return type:

List of lists of floats

biapy.data.post_processing.post_processing.detection_watershed(seeds: ndarray[tuple[int, ...], dtype[_ScalarType_co]], coords: List[List[float | int]], data_filename: str, first_dilation: List[float | int], ndim: int = 3, donuts_classes: List[int] = [-1], donuts_patch: List[int] = [13, 120, 120], donuts_nucleus_diameter: int = 30, save_dir: str | None = None) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]

Grow given detection seeds.

Parameters:
  • seeds (3D/4D Numpy array) – Binary foreground labels and contours data to apply watershed into. E.g. (1450, 2000, 1) for 2D and (397, 1450, 2000, 1) for 3D.

  • coords (List of list of 3 ints) – Coordinates of all detected points.

  • data_filename (str) – Path to load the image paired with seeds.

  • first_dilation (List of float) – Seed dilation before watershed.

  • ndim (int, optional) – Number of dimensions. E.g. for 2D set it to 2 and for 3D to 3.

  • donuts_classes (List of ints, optional) – Classes to check a donuts type cell. Set to -1 to disable it.

  • donuts_patch (List of ints, optional) – Patch to analize donuts cells. Give a shape that covers all sizes of this type of cells.

  • donuts_nucleus_diameter (int, optional) – Aproximate nucleus diameter for donuts type cells.

  • save_dir (str, optional) – Directory to save watershed output into.

Returns:

segm – Image with Voronoi applied. E.g. (y, x) for 2D and (z, y, x) for 3D.

Return type:

2D/3D Numpy array

biapy.data.post_processing.post_processing.measure_morphological_props_and_filter(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], intensity_image: ndarray[tuple[int, ...], dtype[_ScalarType_co]], resolution: List[float | int], extra_props: List[str] = ['label', 'bbox', 'perimeter'], filter_instances: bool = False, properties: List[List[str]] = [[]], prop_values=[[]], comp_signs=[[]])[source]

Measure the properties of input image’s instances.

It calculates each instance id, number of pixels, area/volume (2D/3D respec. and taking into account the resolution), diameter, perimeter/surface_area (2D/3D respec.), circularity/sphericity (2D/3D respec.) and elongation properties. All instances that satisfy the conditions composed by properties, prop_values and comp_signs variables will be removed from img. Apart from returning all properties this function will return also a list identifying those instances that satisfy and not satify the conditions. Those removed will be marked as β€˜Removed’ whereas the rest are β€˜Correct’. Some of the properties follow the formulas used in MorphoLibJ library for Fiji.

Parameters:
  • img (2D/3D Numpy array) – Image with instances. E.g. (1450, 2000) for 2D and (397, 1450, 2000) for 3D.

  • intensity_image (2D/3D Numpy array) – Intensity image to measure intensity-based properties. E.g. (1450, 2000) for 2D and (397, 1450, 2000) for 3D.

  • resolution (tuple of int/float) – Resolution of the data.

  • extra_props (List of str, optional) – List of extra properties to measure. For a more detailed list of the available properties please refer to skimage.measure.regionprops.

  • filter_instances (bool, optional) – Whether to do instance filtering or not.

  • properties (List of lists of str, optional) – List of lists of properties to remove the instances. Options available: ['circularity', 'npixels', 'area', 'diameter', 'elongation', 'sphericity', 'perimeter']. E.g. [['size'], ['circularity', 'npixels']].

  • prop_values (List of lists of floats/ints, optional) – List of lists of values for each property. E.g. [[70], [0.7, 2000]].

  • comp_signs (List of list of str, optional) – List of lists of signs to compose the conditions, together properties prop_values, that the instances must satify to be removed from the input img. E.g. [['le'], ['lt', 'ge']].

Returns:

  • img (2D/3D Numpy array) – Input image without the instances that do not satisfy the circularity constraint. Image with instances. E.g. (1450, 2000) for 2D and (397, 1450, 2000) for 3D.

  • d_result (dict) – Results of the morphological measurements. All the information of the non-filtered instances (if declared to do so) are listed. It contains:

    labelsArray of ints

    Instance label list.

    centersArray of ints

    Coordinates of the centers of each instance.

    npixelsArray of ints

    Number of pixels of each instance.

    areasArray of ints

    Area/volume (2D/3D) of each instance based on the given resolution.

    circularitiesArray of ints

    Circularity/sphericity (2D/3D) of each instance. In 2D, circularity of an instance is defined as the ratio of area over the square of the perimeter, normalized such that the value for a disk equals one: (4 * PI * area) / (perimeter^2). While values of circularity range theoretically within the interval [0;1], the measurements errors of the perimeter may produce circularity values above 1 (Lehmann et al.). In 3D, sphericity is is the ratio of the squared volume over the cube of the surface area, normalized such that the value for a ball equals one: (36 * PI)*((volume^2)/(perimeter^3)).

    diametersArray of ints

    Diameter of each instance obtained from the bounding box.

    elongationsArray of ints

    Elongation of each instance. It is the inverse of the circularity. Only measurable for 2D images.

    perimeterArray of ints

    In 2D, approximates the contour as a line through the centers of border pixels using a 4-connectivity. In 3D, it is the surface area.

    commentList of str

    List containing β€˜Correct’ string when the instance surpass the circularity threshold and β€˜Removed’ otherwise.

    conditionsList of str

    List of conditions that each instance has satisfy or not.

biapy.data.post_processing.post_processing.find_closest_key(requested: str, available_keys)[source]

Find the closest existing key for a canonical regionprops property name. Example: β€œintensity_std” -> β€œintensity_std-0”

biapy.data.post_processing.post_processing.find_neighbors(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], label: int, neighbors: int = 1)[source]

Find neighbors of a label in a given image.

Parameters:
  • img (2D/3D Numpy array) – Image with instances. E.g. (1450, 2000) for 2D and (397, 1450, 2000) for 3D.

  • label (int) – Label to find the neighbors of.

  • neighbors (int, optional) – Number of neighbors in each axis to explore.

Returns:

neighbors – Neighbors instance ids of the given label.

Return type:

list of ints

biapy.data.post_processing.post_processing.repare_large_blobs(img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], size_th: int = 10000)[source]

Try to repare large instances by merging neighbors ones with it and by removing possible central holes.

Parameters:
  • img (2D/3D Numpy array) – Image with instances. E.g. (1450, 2000) for 2D and (397, 1450, 2000) for 3D.

  • size_th (int, optional) – Size that the instances need to be larger than to be analised.

Returns:

img – Input image without the large instances repaired. E.g. (1450, 2000) for 2D and (397, 1450, 2000) for 3D.

Return type:

2D/3D Numpy array

biapy.data.post_processing.post_processing.apply_binary_mask(X: ndarray[tuple[int, ...], dtype[_ScalarType_co]], bin_mask_dir: str)[source]

Apply a binary mask to remove values outside it.

Parameters:
  • X (3D/4D Numpy array) – Data to apply the mask. E.g. (y, x, channels) for 2D or (z, y, x, channels) for 3D.

  • bin_mask_dir (str, optional) – Directory where the binary mask are located.

Returns:

X – Data with the mask applied. E.g. (y, x, channels) for 2D or (z, y, x, channels) for 3D.

Return type:

3D/4D Numpy array

biapy.data.post_processing.post_processing.apply_label_refinement(lbl_img: ndarray[tuple[int, ...], dtype[_ScalarType_co]], is_3d: bool, operations: List[str], values: List[float]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]

Apply label refinement techniques to improve the quality of the label image.

Parameters:
  • lbl_img (NDArray) – Input label image with instances.

  • is_3d (bool) – Whether the label image is 3D or not.

  • operations (List of str) – List of operations to apply. Options available: ['fill_holes', 'clear_border', 'erosion', 'dilation', 'remove_small_objects', 'remove_big_objects'].

  • values (List of float) – List of values associated to each operation. For operations that do not require a value, put any value in that position.

Returns:

Refined label image.

Return type:

NDArray

biapy.data.post_processing.post_processing.fill_label_holes(lbl_img: ndarray[tuple[int, ...], dtype[_ScalarType_co]]) ndarray[tuple[int, ...], dtype[_ScalarType_co]][source]

Fill small holes in label image.

Parameters:

lbl_img (2D/3D Numpy array) – Label image with instances. E.g. (1450, 2000) for 2D and (397, 1450, 2000) for 3D.

Returns:

lbl_img_filled – Label image with instances with holes filled. E.g. (1450, 2000) for 2D and (397, 1450, 2000) for 3D.

Return type:

2D/3D Numpy array