iss_preprocess.segment package

Submodules

iss_preprocess.segment.barcodes module

iss_preprocess.segment.barcodes.segment_spot_image(spot_image, binarise_threshold=5.0, distance_threshold=10.0, debug=False)

Segment a spot image using opencv

Parameters:
  • spot_image (numpy.ndarray) – 2D image to segment

  • binarise_threshold (float, optional) – Threshold for initial binarisation. Will cut isolated rolonies. Defaults to 5.

  • distance_threshold (float, optional) – Distance threshold for initial segmentation. Defaults to 10.

  • debug (bool, optional) – Return intermediate results if True. Defaults to False.

Returns:

Segmented image. Background is 0, borders -1, other

numbers label individual cells. If debug is True, return a dictionary with intermediate results

Return type:

numpy.ndarray or dict

iss_preprocess.segment.cells module

iss_preprocess.segment.cells.cellpose_segmentation(img, channels, flow_threshold=0.4, min_pix=0, dilate_pix=0, model_type='cyto3', pretrained_model=None, use_gpu=False, debug=False, **kwargs)

Segment cells using Cellpose.

Parameters:
  • img (np.array) – reference image

  • channels (tuple) – channels to use for segmentation.

  • flow_threshold (float, optional) – flow threshold for cellpose cell detection. Defaults to 0.4.

  • min_pix (int, optional) – minimum number of pixels to keep mask. Defaults to 0.

  • dilate_pix (int, optional) – number of rounds of binary dilation to grow masks. Defaults to 0.

  • rescale (float, optional) – rescale factor for cellpose model. Defaults to 0.55.

  • model_type (str, optional) – Cellpose mode to use. Defaults to “cyto”.

  • use_gpu (bool, optional) – Defaults to False.

  • debug (bool, optional) – If True, return flows and styles. Defaults to False.

  • **kwargs (optional) – Other kwargs are forwarded to CellposeModel.eval

Returns:

numpy.ndarray of masks

iss_preprocess.segment.cells.count_spots(spots, grouping_column, masks=None, mask_id_column='mask_id')

Count number of rolonies within each mask and return a DataFrame of counts.

Parameters:
  • spots (pandas.DataFrame) – table of spot locations for each group

  • grouping_column (str) – name of the column to group counts, usually ‘gene’ or ‘bases’

  • masks (numpy.ndarray, optional) – cell masks. Must be positive integers. Can be None If spots already includes a “mask_id” columns. Defaults to None.

  • mask_id_column (str, optional) – name of the column containing the mask id. Defaults to “mask_id”.

Returns:

A DataFrame of counts by unique values of grouping_column.

iss_preprocess.segment.cells.label_image(binary, mixed_stack, **kwargs)

Label an image and extract properties.

This is intended for mCherry images and expects a mixed stack with two channels.

Parameters:
  • binary (np.ndarray) – Binary image.

  • mixed_stack (np.ndarray) – Mixed stack.

  • **kwargs – Additional keyword arguments, usually roi, tilex, and tiley. They are used to add metadata to the dataframe.

Returns:

Labeled image. props_df (pd.DataFrame): DataFrame of properties.

Return type:

labeled_image (np.ndarray)

iss_preprocess.segment.cells.project_mask(masks, min_pix_size)

Project masks to a single plane.

Parameters:
  • masks (np.array) – 3D array of masks.

  • min_pix_size (int) – Minimum number of pixels in the center plane to keep a mask.

Returns:

2D array of projected masks.

Return type:

np.array

iss_preprocess.segment.cells.remove_overlapping_labels(overlap_ref, overlap_shifted, upper_overlap_thresh=0.3)

Remove overlapping labels in two adjacent tile overlaps.

Dynamically identifies and removes overlapping labels in place, in two adjacent tile overlaps based on upper and lower overlap percentage thresholds. If the overlap is above the upper threshold, the label in the shifted tile is removed. If the overlap is below the lower threshold, the shared region is removed from both masks.

Parameters:
  • overlap_ref (np.array) – The overlap area of the reference tile.

  • overlap_shifted (np.array) – The overlap area of the shifted tile.

  • upper_overlap_thresh (float, optional) – The upper threshold percentage for considering mask overlap significant. Defaults to 0.3.

Returns:

A dataframe containing the labels that

overlapped, their respective percentages and what happened to them.

Return type:

overlapping_pairs (pandas.DataFrame)

iss_preprocess.segment.cells.spot_mask_value(masks, spots)

Find the mask value of each spot

Parameters:
  • masks (numpy.array) – cell masks. Must be positive integers

  • spots (pandas.DataFrame) – table of spot locations. Must have a x and y columns

Returns:

spots, modified inplace to add a “mask_id” column

Return type:

pandas.DataFrame

iss_preprocess.segment.roi module

class iss_preprocess.segment.roi.ROI(xpix=None, ypix=None, shape=None, mask=None, trace=None)

Bases: object

ROI class to manage binary masks of regions of interest.

Stores locations of included pixels rather than full masks for memory efficiency.

property mask

Binary mask of the ROI

property npix

Number of pixels in the ROI

iss_preprocess.segment.spots module

iss_preprocess.segment.spots.convolve_spots(data_path, roi, kernel_um, prefix='barcode_round', dot_threshold=None, tile=None, output_shape=None)

Generate an image of spot density by convolution

Parameters:
  • data_path (str) – Relative path to data

  • roi (int) – Roi ID

  • kernel_um (float) – Width of the kernel for convolution in microns

  • prefix (str, optional) – Prefix of the spots to load. Defaults to ‘barcode_round’

  • dot_threshold (float, optional) – Threshold on the barcode dot_product_score to select spots to use. Defaults to None.

  • tile (tuple, optional) – Tile to use. Defaults to None.

  • output_shape (tuple, optional) – Shape of the output image. If not provided will return the smallest shape that includes (0,0) and all spots. Defaults to None.

Returns:

2D image of roi density

Return type:

numpy.ndarray

iss_preprocess.segment.spots.detect_isolated_spots(im, detection_threshold=40, isolation_threshold=30, annulus_r=(3, 7))

Detect spots that are isolated from their neighbors.

For each spot, we compute the average intensity in a circular annulus around the spot. If the average intensity is below a threshold, we consider the spot to be isolated.

Parameters:
  • im (numpy.ndarray) – X x Y image

  • detection_threshold (float) – threshold for initial spot detection

  • isolation_threshold (float) – threshold for spot isolation. Lower values in fewer spots considered isolated.

  • annulus_r (tuple) – inner and outer radii of the annulus used to compute the average intensity around each spot.

Returns:

pandas.DataFrame of spots

iss_preprocess.segment.spots.detect_spots(im, threshold=100, spot_size=2)

Detect peaks in an image.

TODO: no point assigning size here.

Parameters:
  • stack (numpy.ndarray) – X x Y x C image stack

  • threshold (float) – spot detection threshold

  • spot_size (float) – spot size in pixels. This value is simply assigned to the “size” column of the output DataFrame.

Returns:

pandas.DataFrame of spot location, including x, y, and size.

iss_preprocess.segment.spots.make_spot_image(spots, gaussian_width=30, dtype='single', output_shape=None, x_col='x', y_col='y')

Make an image by convolving spots with a gaussian

A single isolated rolony results in a gaussian with sigma gaussian_width and an amplitude of 1

Parameters:
  • spots (pandas.DataFrame) – Spots DataFrame. Must have x and y columns

  • gaussian_width (int, optional) – Width of the gaussian in pixels. Defaults to 30.

  • dtype (str, optional) – Datatype for computation. Defaults to “single”.

  • output_shape (tuple, optional) – Shape of the output image. If None, the smallest shape fitting all spots + kernel will be used. Defaults to None.

  • x_col (str, optional) – Column name for x coordinates. Defaults to “x”.

  • y_col (str, optional) – Column name for y coordinates. Defaults to “y”.

Returns:

Convolution results

Return type:

numpy.ndarray

Module contents