Measurement Module

flyqma.measurement provides tools for quantifying reporter fluorescence intensity within individual cell nuclei.

Image Segmentation

Tools for detecting individual cell nuclei.

class flyqma.measurement.segmentation.Segmentation(image, seed_kws={}, seg_kws={}, exclude_edges=True)[source]

Object for finding nuclear contours within an image.

Seed detection is performed by finding local maxima in a euclidean distance transform of the image foreground mask. Segmentation is achieved via the watershed method.


seeds (np.ndarray[float]) - seeds for segmentation, 2 x N

labels (2D np.ndarray[int]) - segment label mask, number denotes segment ID

segment_ids (1D np.ndarray[int]) - unique segment IDs, length N

cmap (matplotlib.colors.ColorMap) - segment ID colormap, length N+1

static array_to_dict(arr)[source]

Convert array to dictionary.


Removes segments overlaying the edge_mask.


Exclude small segments.


min_area (float) - minimum contour area

static find_maxima(im, min_distance=1, num_peaks=inf)[source]

Find local maxima of euclidean distance transform.


im (np.ndarray[bool]) - 2D boolean foreground mask

min_distance (int) - minimum distance separating maxima, px

num_peaks (int) - maximum number of peaks


seeds (np.ndarray[float]) - local maxima, shape (N, 2)

static get_borders(im)[source]

Returns boolean array with borders masked as True.

classmethod get_seeds_from_distance(mask, sigma=2, min_distance=1, num_peaks=inf)[source]

Seed detection via euclidean distance transform of binary map.


mask (nd.ndarray[bool]) - foreground mask

sigma (float) - smoothing applied to euclidean distance mask

min_distance (int) - minimum pixel distance between local maxima

num_peaks (int) - maximum number of local maxima


seeds (dict) - {segment_id: (xpos, ypos)} pairs

classmethod get_segment_mask(im, seeds)[source]

Get mask for markers.


im (np.ndarray[float]) - image to be segmented

seeds (dict) - {segment_id: [x, y]} pairs

property num_objects

Number of unique objects in image.

show(figsize=(15, 15))[source]

Visualize segment label mask.


figsize (tuple) - figure size


Use current seeds to build colormap.

watershed(mask, sigma=0.5, watershed_line=True)[source]

Run watershed segmentation to generate segment label mask.


mask (np.ndarray[bool]) - binary foreground mask

sigma (float) - parameter for smoothing distance mask

watershed_line (bool) - if True, include 1px line between contours

Expression Measurement

Tools for measuring the properties of detected cell nuclei.

class flyqma.measurement.measure.Measurements(im, labels)[source]

Object measures properties of labeled segments within an image.


colordepth (int) - number of color channels

segment_ids (np.ndarray[float]) - ordered segment labels

levels (dict) - {channel: np.ndarray[float]} - expression levels

std (dict) - {channel: np.ndarray[float]} - expression std. deviation

xpos (np.ndarray[float]) - segment centroid x-positions

ypos (np.ndarray[float]) - segment centroid y-positions

voxel_size (np.ndarray[float]) - segment voxel size


Build and return dataframe containing all measurements.


measurements (pd.DataFrame) - measurement data

static evaluate_centroids(labels)[source]

Evaluate center of mass of each label.

  • Note: scipy returns centroids as (y, x) which are flipped to (x, y)


labels (np.ndarray[int]) - segment label mask


center_of_mass (dict) - {segment_id: [xpos, ypos]} pairs

measure_centroids(labels, segment_ids)[source]

Measure the centroid of each segment.


labels (np.ndarray[int]) - cell segment labels

segment_ids (np.ndarray[int]) - ordered segment IDs

measure_expression(im, labels, segment_ids)[source]

Measure expression levels.


im (np.ndarray[float]) - 3D array of pixel values

labels (np.ndarray[int]) - cell segment labels

segment_ids (np.ndarray[int]) - ordered segment IDs

measure_segment_size(labels, segment_ids)[source]

Measure the voxel size of each segment.

Args: labels (np.ndarray[int]) - cell segment labels segment_ids (np.ndarray[int]) - ordered segment IDs