AnalysisFunctions module

This class contains functions pertaining to analysis of images based on their radiality, relating to the RASP concept. jsb92, 2024/01/02

class AnalysisFunctions.Analysis_Functions

Bases: object

Gauss2DFitting(image, pixel_index_list, expanded_area=5)

Gets HWHM of PSFs from fitting Gaussians to spots (from pixel_index_list) Pixels expanded borders by n pixels (default 5)

Parameters:
  • image (array) – image as numpy array

  • pixel_index_list (list) – list of pixel arrays

  • area (expended) – range to expand the pixel mask to

Returns:

HWHMarray (array) – array of half-width-half maxima from the fits

bincalculator(data)

bincalculator function reads in data and generates bins according to Freedman-Diaconis rule

Parameters:

data (np.1darray) – data to calculate bins

Returns:

bins (np.1darray) – bins for histogram according to Freedman-Diaconis rule

calculate_gradient_field(image, kernel)

Calculate the gradient field of an image and compute focus-related measures.

Parameters:
  • image (numpy.ndarray) – The input image.

  • kernel (numpy.ndarray) – The kernel for low-pass filtering.

Returns:
  • filtered_image (numpy.ndarray) – Image after low-pass filtering.

  • gradient_x (numpy.ndarray) – X-gradient of the filtered image.

  • gradient_y (numpy.ndarray) – Y-gradient of the filtered image.

  • focus_score (numpy.ndarray) – Focus score of the image.

  • concentration_factor (numpy.ndarray) – Concentration factor of the image.

calculate_mask_fill(mask_indices, image_size)

calculate amount of image filled by mask.

Parameters:
  • mask_indices (1D array) – indices of pixels in mask

  • image_size (tuple) – Image dimensions (height, width).

Returns:

mask_fill (float) – proportion of image filled by mask.

calculate_radiality(pil_small, img, gradient_x, gradient_y, d=2)

Calculate radiality measures based on pixel neighborhoods and gradients.

Parameters:
  • pil_small (list) – List of pixel indices.

  • img (numpy.ndarray) – The input image.

  • gradient_x (numpy.ndarray) – X-gradient of the image.

  • gradient_y (numpy.ndarray) – Y-gradient of the image.

  • d (integer) – pixel ring size

Returns:

radiality (numpy.ndarray) – Radiality measures.

calculate_region_properties(binary_mask)

Calculate properties for labeled regions in a binary mask.

Parameters:

binary_mask (numpy.ndarray) – Binary mask of connected components.

Returns:
  • pixel_index_list (list) – List containing pixel indices for each labeled object.

  • areas (numpy.ndarray) – Array containing areas of each labeled object.

  • centroids (numpy.ndarray) – Array containing centroids (x, y) of each labeled object.

calculate_spot_colocalisation_likelihood_ratio(spot_indices, mask_indices, image_size, tol=0.01, n_iter=100, blur_degree=1, max_iter=10000)

gets spot colocalisation likelihood ratio, as well as reporting error bounds on the likelihood ratio for one image

Parameters:
  • spot_indices (1D array) – indices of spots

  • mask_indices (1D array) – indices of pixels in mask

  • image_size (tuple) – Image dimensions (height, width).

  • tol (float) – default 0.01; tolerance for convergence

  • n_iter (int) – default 100; number of iterations to start with

  • blur_degree (int) – number of pixels to blur spot indices with (i.e. number of pixels surrounding centroid to consider part of spot). Default 1

Returns:
  • colocalisation_likelihood_ratio (float) – likelihood ratio of spots for mask

  • perc_std (float) – standard deviation on this CLR based on bootstrapping

  • meanCSR (float) – mean of randomised spot data

  • expected_spots (float) – number of spots we expect based on mask % of image

  • n_iter (int) – how many iterations it took to converge

compute_image_props(image, k1, k2, thres=0.05, large_thres=450.0, areathres=30.0, rdl=[50.0, 0.0, 0.0], d=2, z_planes=0, calib=False)

Gets basic image properties (dl_mask, centroids, radiality) from a single image

Parameters:
  • image (array) – image as numpy array

  • k1 (array) – gaussian blur kernel

  • k2 (array) – ricker wavelet kernel

  • thres (float) – percentage threshold

  • areathres (float) – area threshold

  • rdl (array) – radiality thresholds

  • d (int) – radiality ring

  • z_planes (array) – If multiple z planes, give z planes

  • calib (bool) – If True, for radiality calibration

compute_spot_and_cell_props(image, image_cell, k1, k2, prot_thres=0.05, large_prot_thres=450.0, areathres=30.0, rdl=[50.0, 0.0, 0.0], z=0, cell_threshold1=200.0, cell_threshold2=200, cell_sigma1=2.0, cell_sigma2=40.0, d=2)

Gets basic image properties (centroids, radiality) from a single image and compare to a cell mask from another image channel

Parameters:
  • image (array) – image of protein stain as numpy array

  • image – image of cell stain as numpy array

  • k1 (array) – gaussian blur kernel

  • k2 (array) – ricker wavelet kernel

  • prot_thres (float) – percentage threshold for protein

  • large_prot_thres (float) – Protein threshold intensity

  • areathres (float) – area threshold

  • rdl (array) – radiality thresholds

  • z (array) – z planes to image, default 0

  • cell_threshold1 (float) – 1st cell intensity threshold

  • cell_threshold2 (float) – 2nd cell intensity threshold

  • cell_sigma1 (float) – cell blur value 1

  • cell_sigma2 (float) – cell blur value 2

  • d (integer) – pixel radius value

compute_spot_props(image, k1, k2, thres=0.05, large_thres=450.0, areathres=30.0, rdl=[50.0, 0.0, 0.0], z=0, d=2)

Gets basic image properties (centroids, radiality) from a single image

Parameters:
  • image (array) – image as numpy array

  • k1 (array) – gaussian blur kernel

  • k2 (array) – ricker wavelet kernel

  • thres (float) – percentage threshold

  • areathres (float) – area threshold

  • rdl (array) – radiality thresholds

  • z (array) – z planes to image, default 0

  • d (int) – Pixel radius value

create_filled_region(image_size, indices_to_keep)

Fill a region in a boolean matrix based on specified indices.

Parameters:
  • image_size (tuple) – Size of the boolean matrix.

  • indices_to_keep (list) – List of indices to set as True.

Returns:

boolean_matrix (numpy.ndarray) – Boolean matrix with specified indices set to True.

create_gaussian_kernel(sigmas, size)

Create a 2D Gaussian kernel.

Parameters:
  • sigmas (tuple) – Standard deviations in X and Y directions.

  • size (tuple) – Size of the kernel.

Returns:

kernel (numpy.ndarray) – 2D Gaussian kernel.

create_kernel(background_sigma, wavelet_sigma)

Create Gaussian and Ricker wavelet kernels.

Parameters:
  • background_sigma (float) – Standard deviation for Gaussian kernel.

  • wavelet_sigma (float) – Standard deviation for Ricker wavelet.

Returns:
  • gaussian_kernel (numpy.ndarray) – Gaussian kernel for background suppression.

  • ricker_kernel (numpy.ndarray) – Ricker wavelet for feature enhancement.

default_spotanalysis_routine(image, k1, k2, thres=0.05, large_thres=450.0, areathres=30.0, rdl=[50.0, 0.0, 0.0], d=2)

Daisy-chains analyses to get basic image properties (centroids, radiality) from a single image

Parameters:
  • image (array) – image as numpy array

  • k1 (array) – gaussian blur kernel

  • k2 (array) – ricker wavelet kernel

  • thres (float) – percentage threshold

  • areathres (float) – area threshold

  • rdl (array) – radiality thresholds

Returns:
  • centroids (2D array) – centroid positions per oligomer

  • estimated_intensity (numpy.ndarray) – Estimated sum intensity per oligomer.

  • estimated_background (numpy.ndarray) – Estimated mean background per oligomer.

  • estimated_background_perpixel (numpy.ndarray) – Estimated mean background per pixel.

detect_large_features(image, threshold1, threshold2=0, sigma1=2.0, sigma2=60.0)

Detects large features in an image based on a given threshold.

Parameters:
  • image (numpy.ndarray) – Original image.

  • threshold1 (float) – Threshold for determining features. Only this is used for the determination of large protein aggregates.

  • threshold2 (float) – Threshold for determining cell features. If above 0, gets used and cellular features are detected.

  • sigma1 (float) – first gaussian blur width

  • sigma2 (float) – second gaussian blur width

Returns:

large_mask (numpy.ndarray) – Binary mask for the large features.

dilate_pixel(index, image_size, width=5, edge=1)

Dilate a pixel index to form a neighborhood.

Parameters:
  • index (int) – Pixel index.

  • image_size (tuple) – Image dimensions (height, width).

  • width – width of dilation (default 5)

  • edge – edge of dilation (default 1)

Returns:

dilated_indices (numpy.ndarray) – Dilated pixel indices forming a neighborhood.

estimate_intensity(image, centroids)

Estimate intensity values for each centroid in the image.

Parameters:
  • image (numpy.ndarray) – Input image.

  • centroids (numpy.ndarray) – Centroid locations.

Returns:
  • estimated_intensity (numpy.ndarray) – Estimated sum intensity per oligomer.

  • estimated_background (numpy.ndarray) – Estimated mean background per oligomer.

  • estimated_background_perpixel (numpy.ndarray) – Estimated mean background per pixel.

gen_CSRmats(image_z_shape)

Generates empty matrices for the CSR

Parameters:

image_z_shape (int) – shape of new array

Returns:
  • clr (ndarray) – empty array

  • norm_std (ndarray) – empty array

  • norm_CSR (ndarray) – empty array

  • expected_spots (ndarray) – empty array

  • n_iter (ndarray) – empty array

generate_mask_and_spot_indices(mask, centroids, image_size)

makes mask and spot indices from xy coordinates

Parameters:
  • mask (2D array) – boolean matrix

  • centroids (2D array) – xy centroid coordinates

  • image_size (tuple) – Image dimensions (height, width).

Returns:
  • mask_indices (1D array) – indices of mask

  • spot_indices (1D array) – indices of spots

infocus_indices(focus_scores, threshold_differential)

Identify in-focus indices based on focus scores and a threshold differential.

Parameters:
  • focus_scores (numpy.ndarray) – Focus scores for different slices.

  • threshold_differential (float) – Threshold for differential focus scores.

Returns:

in_focus_indices (list) – List containing the first and last in-focus indices.

intensity_pixel_indices(centroid_loc, image_size)

Calculate pixel indices for inner and outer regions around the given index.

Parameters:
  • centroid_loc (2D array) – xy location of the pixel.

  • image_size (tuple) – Size of the image.

Returns:
  • inner_indices (numpy.ndarray) – Pixel indices for the inner region.

  • outer_indices (numpy.ndarray) – Pixel indices for the outer region.

make_datarray_cell(clr, norm_std, norm_CSR, expected_spots, n_iter, columns, z_planes='none')

makes a datarray in pandas for cell information

Parameters:
  • clr (ndarray) – colocalisation likelihood ratios

  • columns (list of strings) – column labels

  • zp (string or int) – if int, gives out z-plane version of datarray

  • z_planes – z_planes to put in array (if needed)

Returns:

to_save (pandas DataArray) – pandas array to save

make_datarray_spot(centroids, estimated_intensity, estimated_background, estimated_background_perpixel, columns, z_planes=0)

makes a datarray in pandas for spot information

Parameters:
  • centroids (ndarray) – centroid positions

  • estimated_intensity (ndarray) – estimated intensities

  • estimated_background (ndarray) – estimated backgrounds per punctum

  • estimated_background_perpixel (np.ndarray) – estimated background per pixel

  • columns (list of strings) – column labels

  • z_planes – z_planes to put in array (if needed); if int, assumes only one z-plane

Returns:

to_save (pandas DataArray)

rejectoutliers(data)

rejectoutliers function # rejects outliers from data, does iqr method (i.e. anything below lower quartile (25 percent) or above upper quartile (75 percent) is rejected)

Parameters:

data (np.1darray) – data matrix

Returns:

newdata (np.1darray) – data matrix

rejectoutliers_ind(data)

rejectoutliers function # gets indices to reject outliers from data, does iqr method (i.e. anything below lower quartile (25 percent) or above upper quartile (75 percent) is rejected)

Parameters:

data (np.1darray) – data matrix

Returns:

ind_arr (np.1darray) – index array

ricker_wavelet(sigma)

Create a 2D Ricker wavelet.

Parameters:

sigma (float) – Standard deviation for the wavelet.

Returns:

wavelet (numpy.ndarray) – 2D Ricker wavelet.

small_feature_kernel(img, large_mask, img2, Gx, Gy, k2, thres, area_thres, rdl, d=2)

Find small features in an image and determine diffraction-limited (dl) and non-diffraction-limited (ndl) features.

Parameters:
  • img (numpy.ndarray) – Original image.

  • large_mask (numpy.ndarray) – Binary mask for large features.

  • img2 (numpy.ndarray) – Smoothed image for background suppression.

  • Gx (numpy.ndarray) – Gradient image in x-direction.

  • Gy (numpy.ndarray) – Gradient image in y-direction.

  • k2 (numpy.ndarray) – The kernel for blob feature enhancement.

  • thres (float) – Converting real-valued image into a binary mask.

  • area_thres (float) – The maximum area in pixels a diffraction-limited object can be.

  • rdl (list) – Radiality threshold [min_radiality, max_radiality, area].

  • d (integer) – pixel radius

Returns:
  • dl_mask (numpy.ndarray) – Binary mask for diffraction-limited (dl) features.

  • centroids (numpy.ndarray) – Centroids for dl features.

  • radiality (numpy.ndarray) – Radiality value for all features (before the filtering based on the radiality).

  • idxs (numpy.ndarray) – Indices for objects that satisfy the decision boundary.

test_spot_mask_overlap(spot_indices, mask_indices)

Tests which spots overlap with a given mask.

Parameters:
  • spot_indices (1D array) – indices of spots

  • mask_indices (1D array) – indices of pixels in mask

Returns:

n_spots_in_mask (float) – number of spots that overlap with the mask.