pykrait.preprocessing package#
Submodules#
pykrait.preprocessing.segmentation module#
- pykrait.preprocessing.segmentation.create_cellpose_segmentation(image: ndarray, cellpose_model_path: str) ndarray[source]#
Wrapper function to create a label image from an input image using a prespecified cellpose model.
- Parameters:
image (ndarray) – image to be processed with cellpose.
cellpose_model_path (str) – path to the cellpose model file. Can also be cpsam if using cellpose-4 and the standard builtin model.
- Return label_img:
The generated label image.
- Return type:
np.ndarray
- Raises:
FileNotFoundError – If the Cellpose model path does not exist.
ValueError – If the input std image is not a 2D array.
RuntimeError – If the Cellpose model fails to load with both GPU and CPU backends.
- pykrait.preprocessing.segmentation.timelapse_projection(lazy_timelapse: array, normalize: bool = True, method: Literal['std', 'sum'] = 'std', verbose: bool = True) ndarray[source]#
Computes a T-projection of a timelapse to be used for cell segmentation.
- Parameters:
lazy_timelapse (da.array) – 4D dask array representing the timelapse, with shape order “TCYX”.
normalize (bool, optional) – If True, applies CLAHE normalization.
method (Literal["std", "sum"], optional, optional) – method to be used for the projection, either “std” or “sum” for sum, defaults to “std”
verbose (bool, optional) – whether to output progress bar, defaults to True
- Raises:
TypeError – if input is not a dask array
ValueError – if input does not have 4 dimensions
ValueError – if unknown T-proj method
- Returns:
3D “CYX” numpy array containing the standard deviation for each pixel across all frames in 16-bit.
- Return type:
np.ndarray
pykrait.preprocessing.timeseries_extraction module#
- pykrait.preprocessing.timeseries_extraction.extract_cell_properties(masks: ndarray) tuple[source]#
Returns cell properties (area, axis lengths, perimeter) and positions (centroid coordinates) of each cell in the label image.
- Parameters:
masks (np.ndarray) – input label image
- Return cell_properties:
pandas dataframe of cell properties
- Rtype cell_properties:
pd.DataFrame
- Return cell_positions:
(nd.array of size len(roi) x 2, np.uint16): pixel positions of every roi
- Rtype cell_positions:
np.ndarray
- pykrait.preprocessing.timeseries_extraction.extract_mean_intensities(dask_timelapse: Array, masks: ndarray, verbose: bool = True, channel_index: int = 0) ndarray[source]#
This function computes the mean intensity of each cell across all frames in a timelapse, given a label image that identifies the regions.
Note: If the input timelapse has multiple channels, only the first channel (C=0) will be used for intensity calculations. It uses dask to lazily compute the mean intensities for each ROI and timepoint and does not load the entire timelapse into memory.
- Parameters:
dask_timelapse (da.Array) – A 4D dask array with order “TCYX”, fully loaded into memory.
masks (np.ndarray) – A 2D numpy array containing labels for each region as label image.
verbose (bool) – if True, shows the progress bar for the computation.
- Raises:
TypeError – If the input is not a dask array.
ValueError – If the input timelapse is not a 4D array or if the masks do not match the spatial dimensions of the timelapse.
- Returns:
a T x n_rois numpy array containing the mean intensity of each cell per frame
- Return type:
np.ndarray
- pykrait.preprocessing.timeseries_extraction.get_adjacency_matrix(masks: ndarray, neighbour_tolerance: int, verbose: bool = True) tuple[ndarray, ndarray][source]#
Computes the adjacency matrix and shortest path matrix for ROIs within the neighbour_tolerance in the given mask.
- Parameters:
masks (np.ndarray) – 2D label image where the value of every ROI is equal to its index. A pixel value of 0 indicates the image background.
neighbour_tolerance (int) – maximum distance between two ROIs to still be considered neighbours, in pixels.
verbose (bool, optional) – if true, prints a progress bar for adjacency matrix construction, defaults to True
- Returns:
returns a ROI x ROI adjacency matrix, where the element at (i, j) is 1 if ROI i and ROI j are neighbours, and 0 otherwise. Also returns a shortest path matrix, where the element at (i, j) is the length of the shortest path between ROI i and ROI j.
- Return type:
tuple[np.ndarray, np.ndarray]
In benchmarking, this function is about 25x faster than the legacy version _legacy_get_adjacency_matrix, which uses cdist to compute the distances between all pairs of ROIs. However, they do not produce exactly the same adjacency matrix. _legacy_get_adjacency_matrix identifies ca. 1.7% more pairs of adjacent ROIs than this function, which is likely due to edge cases around the kernel radius and the way boundaries are defined here.
- pykrait.preprocessing.timeseries_extraction.shrink_masks(masks: ndarray, shrink_factor: float = 0.7) ndarray[source]#
returns a label image with labels shrunk concentrically by a factor
- Parameters:
masks – label image of size m x n
shrink_factor (float, optional) – factor to shrink label by, defaults to 0.7
- Returns:
returns the shrunk label image
- Return type:
np.ndarray