multiScaleAnalysis.SegmentationHighres package

Subpackages

Submodules

multiScaleAnalysis.SegmentationHighres.Cellpose_based_MacrophageSegmentation module

multiScaleAnalysis.SegmentationHighres.Cellpose_based_MacrophageSegmentation.generate2Dsegmentations(im, im_cancer, savedeconvfilepath)[source]

Generate and save preprocessed/deconvolved data and cell pose segmentation masks

Parameters:
  • im – image of macrophages

  • im_cancer – image of cancer cells

  • im_cancer – folder to save the deconvolved file

multiScaleAnalysis.SegmentationHighres.Cellpose_based_MacrophageSegmentation.generate_binaryimage(im_deconvolved)[source]

Generate binary image from deconvolved image using otsu tresholding.

Parameters:

im_deconvolved – a deconvolved image

Returns:

binary image

multiScaleAnalysis.SegmentationHighres.Cellpose_based_MacrophageSegmentation.merge_masks(mask_xy, mask_xz, mask_yz, im_binary)[source]

Return the gradients fused from 3 orthogonal segmentations

Parameters:
  • mask_xy – Cellpose segmentation in xy direction

  • mask_xz – Cellpose segmentation in xz direction

  • mask_yz – Cellpose segmentation in yz direction

  • im_binary – Binary image to constrain the Cellpose-based segmentation

Returns:

labels gradients for 3D watershed

multiScaleAnalysis.SegmentationHighres.Cellpose_based_MacrophageSegmentation.register_stack(imagestack, applymedian=0, reference_flag='mean')[source]

Register the stack to account for drift that occurs.

Parameters:
  • imagestack – stack to register

  • applymedian – apply value to zero elements if non zero

  • reference_flag – how should stackreg register stack

Returns:

registered stack

multiScaleAnalysis.SegmentationHighres.ConnectedComponent_MacrophageSegmentation module

multiScaleAnalysis.SegmentationHighres.ConnectedComponent_MacrophageSegmentation.generate_binaryimage(im_deconvolved)[source]

Generate binary image from deconvolved image using multi-otsu tresholding

Parameters:

im_deconvolved

Returns:

binary image

multiScaleAnalysis.SegmentationHighres.ConnectedComponent_MacrophageSegmentation.prepare_segmentations(im, savedeconvfilepath, lateral_axialratio, psffilepath)[source]

Generate and save preprocessed/deconvolved data

Parameters:
  • im – image of macrophages

  • savedeconvfilepath – folder to save the deconvolved file

  • lateral_axialratio – ratio of lateral to axial spacing

  • psffilepath – filepath to PSF file for deconvolution

Returns:

saved images, ready to segment

multiScaleAnalysis.SegmentationHighres.ConnectedComponent_MacrophageSegmentation.register_stack(imagestack, applymedian=0, reference_flag='mean')[source]

Register the stack to account for drift that occurs

Parameters:
  • imagestack – stack to register

  • applymedian – apply value to zero elements if non zero

  • reference_flag – how should stackreg register stack

Returns:

registered stack

multiScaleAnalysis.SegmentationHighres.Manual_curation module

class multiScaleAnalysis.SegmentationHighres.Manual_curation.manual_curate_segmentation[source]

Bases: object

This class provides functions to manually curate images

give_label_new_identiyinarea(stack, label, xrange=[], yrange=[], zrange=[])[source]

Find a label in (xrange, yrange, zrange) and give it a new unique label

Parameters:
  • stack – numpy array to process

  • label – label to modify

  • xrange – narrow down where to modify the label (range in x/shape[1])

  • yrange – narrow down where to modify the label (range in y/shape[2])

  • zrange – narrow down where to modify the label (range in z/shape[0])

Returns:

modified np.array, new unique label

merge_label(stack, label1, label2)[source]

Merge labels (replaces label2 with label1)

Parameters:
  • stack – numpy array to process

  • label1 – integer label

  • label2 – integer label

Returns:

processed numpy array

remove_label(stack, label)[source]

Removes a specific label from a 3D image stack.

Parameters:
  • stack – numpy array

  • label – label

Returns:

processed numpy array

replace_one_label(stack, anotherstack, labeltoreplace)[source]

Replace all positions in a stack with a defined label (labeltoreplace) with unique labels of anotherstack at these positions.

Parameters:
  • stack – np.array to replace a label

  • anotherstack – take the label id from this stack for all position with labeltoreplace in stack

  • labeltoreplace – label to replace

Returns:

modified stack

saveimage(new_image, path_save, path_save_RGB)[source]

Save the image to disk

Parameters:
  • new_image – image to save

  • path_save – path to save label image

  • path_save_RGB – path to save label image with RGB color labels.

takeLabel_fromanotherstack(stack, anotherstack, label)[source]

Take a label from another stack (anotherstack) and insert it into the current stack (stack).

Parameters:
  • stack – numpy array to process

  • anotherstack – numpy array to take label from

  • label – label to take

Returns:

processed numpy array

takeLabel_fromanotherstack_range(stack, anotherstack_orig, label, xrange=[], yrange=[], zrange=[])[source]

Take a label from another stack (anotherstack) and insert it into the current stack (stack).

Parameters:
  • stack – numpy array to process

  • anotherstack – numpy array to take label from

  • label – label to take

  • xrange – narrow down where to take the label from (range in x/shape[1])

  • yrange – narrow down where to take the label from (range in y/shape[2])

  • zrange – narrow down where to take the label from (range in z/shape[0])

Returns:

processed numpy array

multiScaleAnalysis.SegmentationHighres.Merge_ConnectedComponents_Cellpose module

class multiScaleAnalysis.SegmentationHighres.Merge_ConnectedComponents_Cellpose.merge_segmentations[source]

Bases: object

This class merges the segmentation of the binary mask which is ideal for macrophages that do not touch and the cellpose based segmentation that is required to distinguish touching macrophages

__init__()[source]

Init function and parameters

iterate_throughfolder(segmenteddata_connected_folder, segmenteddata_cellpose_folder, parentsavefolder)[source]

Iterate through parentfolder to process individual timepoints.

Parameters:
  • segmenteddata_connected_folder – folder with the data of the connected-components segmentation

  • segmenteddata_cellpose_folder – folder with the data of the cellpose-based segmentation

  • parentsavefolder – parent folder to save the image output

process_timepoint(segmenteddata_connected, segmenteddata_cellpose, savefolder)[source]

Merge segmentations of one timepoint and save resulting image files to disk.

Parameters:
  • segmenteddata_connected – imagefile with segmented data of connected component

  • segmenteddata_cellpose – imagefile with segmented data of cellpose segmentation

  • savefolder – folder where to save merged, segmented data

multiScaleAnalysis.SegmentationHighres.postprocessing_removeCancersignal module

# In this file, the segmented macrophages are postprocessed by # 1. removing the cancer signal from the label / segmented image by thresholding the cancer cell image and masking the label image # 2. by removing small labeled volumes (below volume size, determined by minimum_volumesize

multiScaleAnalysis.SegmentationHighres.preprocessing_class module

class multiScaleAnalysis.SegmentationHighres.preprocessing_class.ImagePreprocessing[source]

Bases: object

This class contains all functions to preprocess the light-sheet images for successful segmentation

__init__()[source]

Initialize the class.

anisodiff(img, niter=1, kappa=50, gamma=0.1, step=(1.0, 1.0), sigma=0, option=1, ploton=False)[source]

Anisotropic diffusion. Use as: imgout = anisodiff(im, niter, kappa, gamma, option)

Kappa controls conduction as a function of gradient. If kappa is low small intensity gradients are able to block conduction and hence diffusion across step edges. A large value reduces the influence of intensity gradients on conduction. Gamma controls speed of diffusion (you usually want it at a maximum of 0.25). Step is used to scale the gradients in case the spacing between adjacent pixels differs in the x and y axes. Diffusion equation 1 favours high contrast edges over low contrast ones. Diffusion equation 2 favours wide regions over smaller ones.

Reference: P. Perona and J. Malik. Scale-space and edge detection using ansotropic diffusion. IEEE Transactions on Pattern Analysis and Machine Intelligence, 12(7):629-639, July 1990.

Original MATLAB code by Peter Kovesi School of Computer Science & Software Engineering The University of Western Australia pk @ csse uwa edu au <http://www.csse.uwa.edu.au>

Translated to Python and optimised by Alistair Muldal Department of Pharmacology University of Oxford <alistair.muldal@pharm.ox.ac.uk>

Parameters:
  • img – Input image

  • niter – Number of iterations

  • kappa – Conduction coefficient 20-100 ?

  • gamma – Max value of .25 for stability

  • step – Tuple, the distance between adjacent pixels in (y,x) option 1: Perona Malik diffusion equation No 1 option 2: Perona Malik diffusion equation No 2

  • ploton

    • if True, the image will be plotted on every iteration

Returns:

imgout - diffused image.

apply_unmix_model(img, model)[source]

Apply unmixing model on an image

Parameters:
  • img – image to apply the unmixing to

  • model – Unmixing model

Returns:

unmixed image.

demix_videos(vid1, vid2, l1_ratio=0.5)[source]

Spectrally unmix two arrays.

Parameters:
  • vid1 – np.array Stack 1 to unmix

  • vid2 – np.array Stack 2 to unmix

  • l1_ratio – The regularization mixing parameter, with 0 <= l1_ratio <= 1. For l1_ratio = 0 the penalty is an elementwise L2 penalty (aka Frobenius Norm). For l1_ratio = 1 it is an elementwise L1 penalty. For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2.

Returns:

unmixed image in one stack

mkdir(folder)[source]

Make folders if they do not exist.

Parameters:

folder – path to folder

Returns:

empty list

normalize(x, pmin=2, pmax=99.8, axis=None, clip=False, eps=1e-20, dtype=<class 'numpy.float32'>)[source]

Percentile-based image normalization.

Parameters:
  • x – np.array to normalize

  • pmin – lower percentile boundary

  • pmax – upper percentile boundary

  • axis – {int, tuple of int, None} (optional) Axis or axes along which the percentiles are computed. The default is to compute the percentile(s) along a flattened version of the array.

  • clip – clip values of normalized array to [0,1]

  • eps – (optional) make sure that division is not zero with small value

  • dtype – (optional) type used for calculations

normalize99(Y, lower=0.01, upper=99.99)[source]

Normalize image so 0.0 is 0.01st percentile and 1.0 is 99.99th percentile Upper and lower percentile ranges configurable.

Parameters:
  • Y – ndarray, float. Component array of length N by L1 by L2 by … by LN.

  • upper – float. Upper percentile above which pixels are sent to 1.0

  • lower – float. Lower percentile below which pixels are sent to 0.0

Returns:

Normalized array with a minimum of 0 and maximum of 1

normalize_mi_ma(x, mi, ma, clip=False, eps=1e-20, dtype=<class 'numpy.float32'>)[source]

Minimal (mi) and maximal (ma) value based image normalization.

Parameters:
  • x – np.array to normalize

  • mi – lower boundary for normalization

  • ma – upper boundary for normalization

  • clip – clip values of normalized array to [0,1]

  • eps – (optional) make sure that division is not zero with small value

  • dtype – (optional) type used for calculations

smooth_vol(vol_binary, ds=4, smooth=5)[source]

Smooth an array with Gaussian filtering.

Parameters:
  • vol_binary – np.array to smooth.

  • ds – scale on which to apply the smoothing.

  • smooth – Gaussian sigma to smooth data.

Returns:

smoothed array.

spectral_unmix_RGB(img, n_components=3, l1_ratio=0.5)[source]

Spectral unmixing function using Non-Negative Matrix Factorization.

Parameters:
  • img – Stack of all combined images to unmix (e.g. combine two images: vid = np.dstack([np.max(vid1, axis=0), np.max(vid2, axis=0)])

  • n_components – how many images/components there are

  • l1_ratio – The regularization mixing parameter, with 0 <= l1_ratio <= 1. For l1_ratio = 0 the penalty is an elementwise L2 penalty (aka Frobenius Norm). For l1_ratio = 1 it is an elementwise L1 penalty.

Returns:

unmixed image stack, unmix color model

Module contents