Reference#

merge_stardist_masks.naive_fusion#

Naively merge all masks that have sufficient overlap and probability.

merge_stardist_masks.naive_fusion.get_poly_list_to_label(shape: Tuple[int, ...], rays: Rays_Base | None) merge_stardist_masks.naive_fusion.PolyToLabelSignature#

Depending on len(shape) return different functions to calculate labels.

Parameters:
  • shape (Tuple[int, ...]) –

  • rays (Rays_Base | None) –

Return type:

merge_stardist_masks.naive_fusion.PolyToLabelSignature

merge_stardist_masks.naive_fusion.get_poly_to_label(shape: Tuple[int, ...], rays: Rays_Base | None) merge_stardist_masks.naive_fusion.PolyToLabelSignature#

Depending on len(shape) return different functions to calculate labels.

Parameters:
  • shape (Tuple[int, ...]) –

  • rays (Rays_Base | None) –

Return type:

merge_stardist_masks.naive_fusion.PolyToLabelSignature

merge_stardist_masks.naive_fusion.inflate_array(x: ndarray[Any, dtype[T]], grid: Tuple[int, ...], default_value: int | float = 0) ndarray[Any, dtype[T]]#

Create new array with increased shape but old values of x.

Parameters:
  • x (ndarray[Any, dtype[T]]) –

  • grid (Tuple[int, ...]) –

  • default_value (int | float) –

Return type:

ndarray[Any, dtype[T]]

merge_stardist_masks.naive_fusion.mesh_from_shape(shape: Tuple[int, ...]) ndarray[Any, dtype[int64]]#

Convenience function to generate a mesh.

Parameters:

shape (Tuple[int, ...]) –

Return type:

ndarray[Any, dtype[int64]]

merge_stardist_masks.naive_fusion.my_polygons_list_to_label(dists: numpy.typing.ArrayLike, points: numpy.typing.ArrayLike, shape: Tuple[int, ...]) ndarray[Any, dtype[int64]]#

Convenience funtion to pass 1-d arrays to polygons_to_label.

Parameters:
  • dists (numpy.typing.ArrayLike) –

  • points (numpy.typing.ArrayLike) –

  • shape (Tuple[int, ...]) –

Return type:

ndarray[Any, dtype[int64]]

merge_stardist_masks.naive_fusion.my_polygons_to_label(dists: numpy.typing.ArrayLike, points: numpy.typing.ArrayLike, shape: Tuple[int, ...]) ndarray[Any, dtype[int64]]#

Convenience funtion to pass 1-d arrays to polygons_to_label.

Parameters:
  • dists (numpy.typing.ArrayLike) –

  • points (numpy.typing.ArrayLike) –

  • shape (Tuple[int, ...]) –

Return type:

ndarray[Any, dtype[int64]]

merge_stardist_masks.naive_fusion.my_polyhedron_list_to_label(rays: Rays_Base, dists: numpy.typing.ArrayLike, points: numpy.typing.ArrayLike, shape: Tuple[int, ...]) ndarray[Any, dtype[int64]]#

Convenience funtion to pass 1-d arrays to polyhedron_to_label.

Parameters:
  • rays (Rays_Base) –

  • dists (numpy.typing.ArrayLike) –

  • points (numpy.typing.ArrayLike) –

  • shape (Tuple[int, ...]) –

Return type:

ndarray[Any, dtype[int64]]

merge_stardist_masks.naive_fusion.my_polyhedron_to_label(rays: Rays_Base, dists: numpy.typing.ArrayLike, points: numpy.typing.ArrayLike, shape: Tuple[int, ...]) ndarray[Any, dtype[int64]]#

Convenience funtion to pass 1-d arrays to polyhedron_to_label.

Parameters:
  • rays (Rays_Base) –

  • dists (numpy.typing.ArrayLike) –

  • points (numpy.typing.ArrayLike) –

  • shape (Tuple[int, ...]) –

Return type:

ndarray[Any, dtype[int64]]

merge_stardist_masks.naive_fusion.naive_fusion(dists: ndarray[Any, dtype[float64]], probs: ndarray[Any, dtype[float64]], rays: Rays_Base | None = None, prob_thresh: float = 0.5, grid: Tuple[int, ...] = (2, 2, 2), no_slicing: bool = False, max_full_overlaps: int = 2, erase_probs_at_full_overlap: bool = False, show_overlaps: bool = False, respect_probs: bool = False) ndarray[Any, dtype[uint16]] | ndarray[Any, dtype[int32]]#

Merge overlapping masks given by dists, probs, rays.

Performs a naive iterative scheme to merge the masks that a StarDist network has calculated to generate a label image. This function can perform 2D and 3D segmentation. For 3D segmentation rays has to be passed from the StarDist model.

Parameters:
  • dists (ndarray[Any, dtype[float64]]) – 3- or 4-dimensional array representing distances of each mask as outputed by a StarDist model. For 2D predictions the shape is (len_y, len_x, n_rays), for 3D predictions it is (len_z, len_y, len_x, n_rays).

  • probs (ndarray[Any, dtype[float64]]) – 2- or 3-dimensional array representing the probabilities for each mask as outputed by a StarDist model. For 2D predictions the shape is (len_y, len_x), for 3D predictions it is (len_z, len_y, len_x).

  • rays (Rays_Base | None) – For 3D predictions rays must be set otherwise a ValueError is raised. It should be the Rays_Base instance used by the StarDist model.

  • prob_thresh (float) – Only masks with probability above this threshold are considered.

  • grid (Tuple[int, ...]) – Should be of length 2 for 2D and of length 3 for 3D segmentation. This is the grid information about the subsampling occuring in the StarDist model.

  • no_slicing (bool) – For very big and winded objects this should be set to True. However, this might result in longer calculation times.

  • max_full_overlaps (int) – Maximum no. of full overlaps before current object is treated as finished.

  • erase_probs_at_full_overlap (bool) – If set to True probs are set to -1 whenever a full overlap is detected.

  • show_overlaps (bool) – If set to true, overlaps are set to -1.

  • respect_probs (bool) – If set to true, overlapping elements are overwritten by considering their probabilities. Only works when show_overlaps is ‘false’.

Returns:

The label image with uint16 labels. For 2D, the shape is (len_y * grid[0], len_x * grid[1]) and for 3D it is (len_z * grid[0], len_y * grid[1], len_z * grid[2]).

Raises:
  • ValueError – If rays is None and 3D inputs are given or when probs.ndim != len(grid). # noqa: DAR402 ValueError

  • NotImplementedError – If grid is anisotropic and respect_probs is set to true.

Return type:

ndarray[Any, dtype[uint16]] | ndarray[Any, dtype[int32]]

Example

>>> from merge_stardist_masks.naive_fusion import naive_fusion
>>> from stardist.rays3d import rays_from_json
>>> probs, dists = model.predict(img)  # model is a 3D StarDist model
>>> rays = rays_from_json(model.config.rays_json)
>>> lbl = naive_fusion(dists, probs, rays, grid=model.config.grid)
merge_stardist_masks.naive_fusion.naive_fusion_anisotropic_grid(dists: ndarray[Any, dtype[float64]], probs: ndarray[Any, dtype[float64]], rays: Rays_Base | None = None, prob_thresh: float = 0.5, grid: Tuple[int, ...] = (2, 2, 2), no_slicing: bool = False, max_full_overlaps: int = 2, erase_probs_at_full_overlap: bool = False, show_overlaps: bool = False, respect_probs: bool = False) ndarray[Any, dtype[uint16]] | ndarray[Any, dtype[int32]]#

Merge overlapping masks given by dists, probs, rays for anisotropic grid.

Performs a naive iterative scheme to merge the masks that a StarDist network has calculated to generate a label image. This function can perform 2D and 3D segmentation. For 3D segmentation rays has to be passed from the StarDist model.

Parameters:
  • dists (ndarray[Any, dtype[float64]]) – 3- or 4-dimensional array representing distances of each mask as outputed by a StarDist model. For 2D predictions the shape is (len_y, len_x, n_rays), for 3D predictions it is (len_z, len_y, len_x, n_rays).

  • probs (ndarray[Any, dtype[float64]]) – 2- or 3-dimensional array representing the probabilities for each mask as outputed by a StarDist model. For 2D predictions the shape is (len_y, len_x), for 3D predictions it is (len_z, len_y, len_x).

  • rays (Rays_Base | None) – For 3D predictions rays must be set otherwise a ValueError is raised. It should be the Rays_Base instance used by the StarDist model.

  • prob_thresh (float) – Only masks with probability above this threshold are considered.

  • grid (Tuple[int, ...]) – Should be of length 2 for 2D and of length 3 for 3D segmentation. This is the grid information about the subsampling occuring in the StarDist model.

  • no_slicing (bool) – For very big and winded objects this should be set to True. However, this might result in longer calculation times.

  • max_full_overlaps (int) – Maximum no. of full overlaps before current object is treated as finished.

  • erase_probs_at_full_overlap (bool) – If set to True probs are set to -1 whenever a full overlap is detected.

  • show_overlaps (bool) – If set to true, overlaps are set to -1.

  • respect_probs (bool) – If set to true, overlapping elements are overwritten by considering their probabilities. Only works when show_overlaps is ‘false’.

Returns:

The label image with uint16 labels. For 2D, the shape is (len_y * grid[0], len_x * grid[1]) and for 3D it is (len_z * grid[0], len_y * grid[1], len_z * grid[2]).

Raises:

ValueError – If rays is None and 3D inputs are given or when probs.ndim != len(grid). # noqa: DAR402 ValueError

Return type:

ndarray[Any, dtype[uint16]] | ndarray[Any, dtype[int32]]

Example

>>> from merge_stardist_masks.naive_fusion import naive_fusion_anisotropic_grid
>>> from stardist.rays3d import rays_from_json
>>> probs, dists = model.predict(img)  # model is a 3D StarDist model
>>> rays = rays_from_json(model.config.rays_json)
>>> grid = model.config.grid
>>> lbl = naive_fusion_anisotropic_grid(dists, probs, rays, grid=grid)
merge_stardist_masks.naive_fusion.naive_fusion_isotropic_grid(dists: ndarray[Any, dtype[float64]], probs: ndarray[Any, dtype[float64]], rays: Rays_Base | None = None, prob_thresh: float = 0.5, grid: int = 2, no_slicing: bool = False, max_full_overlaps: int = 2, erase_probs_at_full_overlap: bool = False, show_overlaps: bool = False, respect_probs: bool = False) ndarray[Any, dtype[uint16]] | ndarray[Any, dtype[int32]]#

Merge overlapping masks given by dists, probs, rays.

Performs a naive iterative scheme to merge the masks that a StarDist network has calculated to generate a label image. This function can perform 2D and 3D segmentation. For 3D segmentation rays has to be passed from the StarDist model.

Parameters:
  • dists (ndarray[Any, dtype[float64]]) – 3- or 4-dimensional array representing distances of each mask as outputed by a StarDist model. For 2D predictions the shape is (len_y, len_x, n_rays), for 3D predictions it is (len_z, len_y, len_x, n_rays).

  • probs (ndarray[Any, dtype[float64]]) – 2- or 3-dimensional array representing the probabilities for each mask as outputed by a StarDist model. For 2D predictions the shape is (len_y, len_x), for 3D predictions it is (len_z, len_y, len_x).

  • rays (Rays_Base | None) – For 3D predictions rays must be set otherwise a ValueError is raised. It should be the Rays_Base instance used by the StarDist model.

  • prob_thresh (float) – Only masks with probability above this threshold are considered.

  • grid (int) – This is the grid information about the subsampling occuring in the StarDist model.

  • no_slicing (bool) – For very big and winded objects this should be set to True. However, this might result in longer calculation times.

  • max_full_overlaps (int) – Maximum no. of full overlaps before current object is treated as finished.

  • erase_probs_at_full_overlap (bool) – If set to True probs are set to -1 whenever a full overlap is detected.

  • show_overlaps (bool) – If set to true, overlaps are set to -1.

  • respect_probs (bool) – If set to true, overlapping elements are overwritten by considering their probabilities. Only works when show_overlaps is ‘false’.

Returns:

The label image with uint16 labels. For 2D, the shape is (len_y * grid[0], len_x * grid[1]) and for 3D it is (len_z * grid[0], len_y * grid[1], len_z * grid[2]).

Raises:

ValueError – If rays is None and 3D inputs are given or when probs.ndim != len(grid). # noqa: DAR402 ValueError

Return type:

ndarray[Any, dtype[uint16]] | ndarray[Any, dtype[int32]]

Example

>>> from merge_stardist_masks.naive_fusion import naive_fusion_isotropic_grid
>>> from stardist.rays3d import rays_from_json
>>> probs, dists = model.predict(img)  # model is a 3D StarDist model
>>> rays = rays_from_json(model.config.rays_json)
>>> grid = model.config.grid[0]
>>> lbl = naive_fusion_isotropic_grid(dists, probs, rays, grid=grid)
merge_stardist_masks.naive_fusion.no_slicing_slice_point(point: numpy.typing.ArrayLike, max_dist: int) Tuple[Tuple[slice, ...], ndarray[Any, dtype[int64]]]#

Convenience function that returns the same point and tuple of slice(None).

Parameters:
  • point (numpy.typing.ArrayLike) –

  • max_dist (int) –

Return type:

Tuple[Tuple[slice, …], ndarray[Any, dtype[int64]]]

merge_stardist_masks.naive_fusion.paint_in_with_overlaps(paint_in: ndarray[Any, dtype[T]], shape: ndarray[Any, dtype[bool_]], paint_id: int) ndarray[Any, dtype[T]]#

Set entries of array paint_in to paint_id or -1 if not free anymore.

Parameters:
  • paint_in (ndarray[Any, dtype[T]]) –

  • shape (ndarray[Any, dtype[bool_]]) –

  • paint_id (int) –

Return type:

ndarray[Any, dtype[T]]

merge_stardist_masks.naive_fusion.paint_in_without_overlaps(paint_in: ndarray[Any, dtype[T]], shape: ndarray[Any, dtype[bool_]], paint_id: int) ndarray[Any, dtype[T]]#

Set entries of array to paint_id according to boolean values in shape.

Parameters:
  • paint_in (ndarray[Any, dtype[T]]) –

  • shape (ndarray[Any, dtype[bool_]]) –

  • paint_id (int) –

Return type:

ndarray[Any, dtype[T]]

merge_stardist_masks.naive_fusion.paint_in_without_overlaps_check_probs(paint_in: ndarray[Any, dtype[T]], shape: ndarray[Any, dtype[bool_]], old_probs: ndarray[Any, dtype[float32]], new_probs: ndarray[Any, dtype[float32]], paint_id: int) Tuple[ndarray[Any, dtype[T]], ndarray[Any, dtype[float32]]]#

Set and overwrite entries of array to paint_id respecting their probabilities.

Parameters:
  • paint_in (ndarray[Any, dtype[T]]) –

  • shape (ndarray[Any, dtype[bool_]]) –

  • old_probs (ndarray[Any, dtype[float32]]) –

  • new_probs (ndarray[Any, dtype[float32]]) –

  • paint_id (int) –

Return type:

Tuple[ndarray[Any, dtype[T]], ndarray[Any, dtype[float32]]]

merge_stardist_masks.naive_fusion.points_from_grid(shape: Tuple[int, ...], grid: Tuple[int, ...]) ndarray[Any, dtype[int64]]#

Generate array giving out points for indices.

Parameters:
  • shape (Tuple[int, ...]) –

  • grid (Tuple[int, ...]) –

Return type:

ndarray[Any, dtype[int64]]

merge_stardist_masks.naive_fusion.poly_list_with_probs(dists_: numpy.typing.ArrayLike, points_: numpy.typing.ArrayLike, probs_: numpy.typing.ArrayLike, shape: Tuple[int, ...], poly_list_func: merge_stardist_masks.naive_fusion.PolyToLabelSignature) Tuple[ndarray[Any, dtype[int64]], ndarray[Any, dtype[float32]]]#

Return labels and according probabilities.

Parameters:
  • dists_ (numpy.typing.ArrayLike) –

  • points_ (numpy.typing.ArrayLike) –

  • probs_ (numpy.typing.ArrayLike) –

  • shape (Tuple[int, ...]) –

  • poly_list_func (merge_stardist_masks.naive_fusion.PolyToLabelSignature) –

Return type:

Tuple[ndarray[Any, dtype[int64]], ndarray[Any, dtype[float32]]]

merge_stardist_masks.naive_fusion.slice_point(point: numpy.typing.ArrayLike, max_dist: int) Tuple[Tuple[slice, ...], ndarray[Any, dtype[int64]]]#

Calculate the extents of a slice for a given point and its coordinates within.

Parameters:
  • point (numpy.typing.ArrayLike) –

  • max_dist (int) –

Return type:

Tuple[Tuple[slice, …], ndarray[Any, dtype[int64]]]