Segmentation
- class segmentation.Segmentation(img, spot_center: ndarray, out_dir: str = '', prob_thresh: float = 0.2, nms_thresh: float = 0.5, spot_radius: float = 36.5, n_tiles=(8, 8, 1))[source]
Bases:
object
- static n_cell_in_spot(nucleus_center: ndarray, spot_center: ndarray, spot_radius: float, nucleus_df: DataFrame | None = None) DataFrame [source]
Find the number of cells in each spot.
If the center of a nucleus is inside the spot, we assume that the cell is in the spot.
- Parameters:
nucleus_center – np.ndarray(n_nucleus*2). Coordinates of the nucleus centers.
spot_center – np.ndarray(n_spot*2). Coordinates of the spot centers.
spot_radius – Radius of the spots.
nucleus_df – Optional, dataframe of the nucleus.
- Returns:
Pandas data frame with two columns. Column ‘cell_count’ represents the number of cells in a spot. Column ‘centers’ represents the coordinates of the nucleus centers.
- plot(fig_size=(10, 4.5), dpi=300, crop=None, cmap_segmented='hot', save=False, path=None)[source]
Plot the segmentation results.
It is recommended to adjust the stardist parameters nms_thresh and prob_thresh based on this plot.
- Parameters:
fig_size – Size of the figure.
dpi – Dots per inch (DPI) of the figure.
crop – If None, show the full image. Otherwise, crop should be
cmap_segmented – Color map of the segmented image.
save – If true, save the figure.
path – Path to the save figure.
- segment_nucleus(save=True)[source]
Conduct the segmentation using StarDist pretrained model.
- Parameters:
save – Whether to save the segmentation results.
- static stardist_2D_versatile_he(img, prob_thresh: float = 0.2, nms_thresh: float = 0.5, n_tiles=(8, 8, 1), verbose: bool = True)[source]
Segmentation function provided by Stardist.
- Parameters:
img – Three channel image.
nms_thresh – Parameter of non-maximum suppression.
prob_thresh – The probability threshold that determines the retention of a nucleus.
n_tiles – Out of memory (OOM) errors can occur if the input image is too large. To avoid this problem, the input image is broken up into (overlapping) tiles that are processed independently and re-assembled. (Copied from stardist document). In default, we break the image into 8*8 tiles.
verbose – Whether to print segmentation progress.
- Returns:
There are two returns.
np.ndarray: The segmented image. Background pixels has value 0 and nucleus pixels has the positive integer value as the index of the nucleus.
list: Nucleus details. Details[0]: np.ndarray(n_nucleus*2*32). Boundaries of each nucleus. Details[1]: np.ndarray(n_nucleus*2). Center of each nucleus. Details[2]: np.ndarray(n_nucleus). Probability that a segmented nucleus is indeed a nucleus.
- segmentation.cell_boundary(nucleus_location, img_size, max_dist, max_area, search_direction, verbose=0, delta=1)[source]
Infer the cell boundary.
- Parameters:
nucleus_location – Coordinates of the nucleus centers.
img_size – The size of the image.
max_dist – The largest distance from the cell boundary to the nucleus center.
max_area – Largest area of a cell. Segmented nuclei with size larger than this value are discarded.
search_direction – The search direction when determine the cell boundary.
verbose – Whether to print progress.
delta – Increment of the radius in each round.
- Returns:
Dictionary of the cell boundary information.
- segmentation.change_predict_defaults(predict_function)[source]
Wrap prediction function for tensorflow>=2.9.0 to reduce outputs.
Start from tensorflow 2.9.0, the default verbose value in function tensorflow.keras.Model.predict is set to ‘auto’, which takes value 1 for the most of the time. Thus, this function is a decorator to wrap the predict function and set the default verbose to 0.
- Parameters:
predict_function – tensorflow.keras.Model.predict