imageProcessing package#

Submodules#

imageProcessing.alignImages module#

Created on Sun Apr 5 09:22:18 2020

@author: marcnol

Sets of functions that do alignment of 2D fiducial images. It also contains code to apply these alignments to other channels (masks/ barcodes)

For the time being alignment is purely based on optimized sub-pixed accuracy image cross correlation

class imageProcessing.alignImages.ApplyRegisterGlobal(params: core.parameters.RegistrationParams)#

Bases: imageProcessing.makeProjections.Feature

class imageProcessing.alignImages.RegisterGlobal(params: core.parameters.RegistrationParams)#

Bases: imageProcessing.makeProjections.Feature

merge_results(results: list[dict])#
run(raw_2d_img, reference_2d_img)#
imageProcessing.alignImages.align_2_images_cross_correlation(image1_uncorrected, image2_uncorrected, lower_threshold=0.999, higher_threshold=0.9999999, upsample_factor=100)#

Aligns 2 images by contrast adjust and cross correlation :param img_reference: DESCRIPTION. :type img_reference: TYPE :param img_2: DESCRIPTION. :type img_2: TYPE

Returns

  • shift (TYPE) – DESCRIPTION.

  • error (TYPE) – DESCRIPTION.

  • diffphase (TYPE) – DESCRIPTION.

  • lower_threshold (TYPE) – DESCRIPTION.

  • i_histogram (TYPE) – DESCRIPTION.

  • image2_corrected (TYPE) – DESCRIPTION.

  • image1_adjusted (TYPE) – DESCRIPTION.

  • image2_adjusted (TYPE) – DESCRIPTION.

  • image2_corrected_raw (TYPE) – DESCRIPTION.

imageProcessing.alignImages.align_images_by_blocks(img_1, img_2, block_size, upsample_factor=100, min_number_pollsters=4, tolerance=0.1, shift_error_tolerance=5)#
imageProcessing.alignImages.apply_registrations_to_current_folder(data_path, current_param, params: core.parameters.RegistrationParams, roi_name, projection_params: core.parameters.ProjectionParams)#

This function will - load masks, RNA and barcode 2D projected images, - apply registrations - save registered images as npy arrays

Parameters
  • data_path (TYPE) – DESCRIPTION.

  • current_param (Parameters class) –

Return type

None.

imageProcessing.alignImages.apply_registrations_to_filename(filename_to_process, dict_shifts, data_path, params: core.parameters.RegistrationParams, roi_name, projection_params: core.parameters.ProjectionParams)#

Applies registration of filename_to_process

Parameters
  • filename_to_process (string) – file to apply registration to

  • dict_shifts (Dictionnary) – contains the shifts to be applied to all rois

Return type

None.

imageProcessing.alignImages.apply_xy_shift_3d_images(image, shift, parallel_execution=True)#

Applies XY shift to a 3D stack

Parameters

images (3D numpy array) – image to process.

Return type

shifted 3D image.

imageProcessing.alignImages.calcul_error(shifted_img, ref_img)#
imageProcessing.alignImages.combine_blocks_image_by_reprojection(block_ref, block_target, shift_matrices=None, axis1=0)#

This routine will overlap block_ref and block_target images block by block. block_ref will be used as a template. - block_target will be first translated in ZXY using the corresponding values in shift_matrices to realign each block - then an rgb image will be created with block_ref in the red channel, and the reinterpolated block_target block in the green channel. - the Blue channel is used for the grid to improve visualization of blocks.

Parameters
  • block_ref (npy array) – return of view_as_blocks()

  • block_target (npy array) – return of view_as_blocks()

  • shift_matrices (list of npy arrays) – index 0 contains Z, index 1 X and index 2 Y

  • axis1 (int) – axis used for the reprojection: The default is 0. - 0 means an XY projection - 1 an ZX projection - 2 an ZY projection

Returns

  • output (NPY array of size im_size x im_size x 3) – rgb image.

  • ssim_as_blocks = NPY array of size number_blocks x number_blocks – Structural similarity index between ref and target blocks

imageProcessing.alignImages.compute_global_shift(image1_uncorrected, image2_uncorrected, lower_threshold, higher_threshold)#
imageProcessing.alignImages.compute_shift_by_block(image1_uncorrected, image2_uncorrected, dict_block_size, tolerance)#
imageProcessing.alignImages.image_block_alignment_3d(images, block_size_xy=256, upsample_factor=100)#
imageProcessing.alignImages.preprocess_2d_img(img, background_sigma)#
imageProcessing.alignImages.remove_inhomogeneous_background(im, background_sigma)#

imageProcessing.alignImages3D module#

Spyder Editor

Purpose: Corrects drift in 3D

The drift correction routines in alignImages.py take care of the corrections in XY but not in Z.

Drift in the z-position from cycle to cycle is small, typically 100-200 nm, but needs to be checked and corrected. In addition, sample deformation can lead to inhomogeneous drift that cannot be corrected by a rigid body translation.

This routine solves both issues by correcting drift in 3D by block decomposition.

steps:
  • iterate over rois

  • load 3D fiducial file for reference fiducial

  • iterate over cycles <i>

  • load 3D fiducial file for fiducial barcode <i>

  • re-align 3D fiducial image using XY alignment

  • perform block alignment in 3D by cross=correlating blocks in 3D.

  • store in database.

  • display results:
    • drift correction maps in X-Y-Z

    • corrected blocks in XY, ZX, ZY

During buildMatrix this database is loaded, if available.
  • check database exist and load it

  • correct z-coordinate of the barcode provided the correction given in the dict

class imageProcessing.alignImages3D.Drift3D(param, registration_params, parallel=False)#

Bases: object

align_fiducials_3d(data_path, params: core.parameters.RegistrationParams, dict_shifts_path, roi_name, z_binning)#

runs refitting routine in root_folder

Return type

None.

align_fiducials_3d_file(filename_to_process, data_path, params: core.parameters.RegistrationParams, roi_name, cycle_name, z_binning)#

Aligns <filename_to_process> fiducial against reference

Return type

None.

align_fiducials_3d_in_folder(data_path, dict_shifts_path, params: core.parameters.RegistrationParams, roi_name, z_binning)#

Refits all the barcode files found in root_folder

Return type

None.

load_dict_shifts(dict_shifts_path)#

Lods dictionary of XY shifts

Return type

None.

load_reference_fiducial(filename_reference, z_binning, lower_threshold_3d, higher_threshold_3d)#

Loads Reference fiducial image

Return type

None.

imageProcessing.alignImages3D.create_output_table()#
imageProcessing.alignImages3D.find_cycle(param, filename)#
imageProcessing.alignImages3D.load_n_preprocess_image(filename_to_process, z_binning, lower_threshold, higher_threshold, parallel_execution=True)#

imageProcessing.imageProcessing module#

Classes for common image processing

class imageProcessing.imageProcessing.Image(current_log=None)#

Bases: object

get_image_filename(root_folder, tag)#
load_image(file_name)#
load_image_2d(file_name, master_folder, tag='_2d')#
save_image_2d(root_folder, tag='_2d')#
show_image(show=False, size=(10, 10), output_name='tmp.png', save=True, normalization='stretch')#
imageProcessing.imageProcessing.image_adjust(image, lower_threshold=0.3, higher_threshold=0.9999)#
Adjust intensity levels:
  • rescales exposures

  • gets histogram of pixel intensities to define cutoffs

  • applies thresholds

Parameters
  • image (numpy array) – input 3D image.

  • lower_threshold (float, optional) – lower threshold for adjusting image levels. The default is 0.3.

  • higher_threshold (float, optional) – higher threshold for adjusting image levels.. The default is 0.9999.

Returns

  • image1 (numpy array) – adjusted 3D image.

  • hist1_before (numpy array) – histogram of pixel intensities before adjusting levels.

  • hist1 (numpy array) – histogram of pixel intensities after adjusting levels.

  • lower_cutoff (float) – lower cutoff used for thresholding.

  • higher_cutoff (float) – higher cutoff used for thresholding.

imageProcessing.imageProcessing.preprocess_3d_image(x, lower_threshold, higher_threshold, parallel_execution=True)#
3D stack pre-procesing:
  • rescales intensities to 0->1

  • removes inhomogeneous background plane by plane

  • adjusts image levels by thresholding

Parameters
  • x (numpy array) – 3D image.

  • lower_threshold (float) – lower threshold for adjusting image levels.

  • higher_threshold (float) – higher threshold for adjusting image levels..

Returns

image – pre-processed 3D image.

Return type

numpy array

imageProcessing.imageProcessing.reassemble_3d_image(client, futures, output_shape)#

waits for futures to arrive collects them into a results list reassembles 3D image plane by plane

Parameters
  • client (dask CLient()) – result of get_client()

  • futures ([]) – list of futures

  • output_shape (tuple) – result of image.shape

Returns

output – contains reassembled 3D image.

Return type

numpy array

imageProcessing.imageProcessing.scatter_3d_image(image)#

splits 3D image plane by plane and scatteres them to a cluster

Parameters

image (numpy array) – 3D image.

Returns

image_list_scattered – scattered image.

Return type

List, dict, iterator, or queue of futures matching the type of input.

imageProcessing.localization_table module#

Created on Mon Feb 7 17:06:14 2022

@author: marcnol

This class will contain methods to load, save, plot barcode localizations and statistics

class imageProcessing.localization_table.LocalizationTable#

Bases: object

compares_localizations(barcode_map_1, barcode_map_2, filename_list, fontsize=20)#

Compares the localizations of two barcode tables

Parameters
  • barcode_map_1 (astropy Table) – localization table 1.

  • barcode_map_2 (astropy Table) – localization table 2.

Return type

None.

load(file)#

Loads barcode_map

Parameters

filename_barcode_coordinates (string) – filename with barcode_map

Returns

  • barcode_map (Table())

  • unique_barcodes (list) – lis of unique barcodes read from barcode_map

plot_distribution_fluxes(barcode_map, filename_list)#

This function plots the distribution of fluxes, sharpness, roundness, magnitude and peak intensity from a Table

Parameters
  • barcode_map (TYPE) – DESCRIPTION.

  • filename_list (list) – filename

Return type

None.

plots_localizations(barcode_map_full, filename_list)#

This function plots 3 subplots (xy, xz, yz) with the localizations. One figure is produced per ROI.

Parameters
  • image (List of numpy ndarray (N-dimensional array)) – 3D raw image of format .tif

  • label (List of numpy ndarray (N-dimensional array)) – 3D labeled image of format .tif

  • filename_list (list) – filename

save(file_name, barcode_map, comments='')#

Saves output table

Parameters
  • filename_barcode_coordinates (string) – filename of table.

  • barcode_map (astropy Table) – Table to be written to file.

  • tag (string, optional) – tag to be added to filename. The default is “_”.

  • ext (string, optional) – file extension. The default is ‘ecsv’.

  • comments (list of strings, optional) – Will output as comments to the header. The default is [].

Return type

None.

imageProcessing.localization_table.build_color_dict(data, key='Barcode #')#
imageProcessing.localization_table.decode_rois(data)#
imageProcessing.localization_table.plots_localization_projection(coord1, coord2, axis, colors, title='')#

This function will produce the scatter plot and add title

Parameters
  • coord1 (1D Numpy array, float) – first coordinate (x).

  • coord2 (1D Numpy array, float) – first coordinate (y).

  • axis (matplotlib axis) – figure axis handle.

  • colors (1D Numpy array, float) – colorcode used in scatter plot.

  • title (string, optional) – title of subpanel. The default is ‘’*3.

Return type

None.

imageProcessing.makeProjections module#

Created on Fri Apr 3 23:17:58 2020

@author: marcnol

This file contains functions to project 3D images to 2D

Operation will be defined in the parameters file. Options are:
  • user-defined range

  • all z range

  • optimal range based on detection of focal plane and use of user defined window around it

class imageProcessing.makeProjections.Feature(params)#

Bases: object

get_required_inputs()#
merge_results(results)#
run()#
class imageProcessing.makeProjections.Project(params: core.parameters.ProjectionParams)#

Bases: imageProcessing.makeProjections.Feature

check_zmax(img_size)#
precise_z_planes(img, mode)#
projection_2d(img)#
run(img, reference)#
imageProcessing.makeProjections.reinterpolate_z(image_3d, z_range, mode='average')#

wrapper function for any kind of z-interpolation to reduce the number of planes in an image

Parameters
  • image_3d (numpy array) – input 3D image.

  • z_range (range) – range of planes for the output image.

  • mode (str, optional) – ‘remove’ will remove planes ‘interpolate’ will perform an interpolation The default is ‘remove’.

Returns

output

Return type

numpy array

imageProcessing.segmentMasks module#

Created on Sat Apr 11 14:59:43 2020

@author: marcnol

File containing all functions responsible for segmentation of masks for Hi-M, including DNA masks, barcodes, and fiducials

At the moment, fittings of the 2D positions of barcodes is also performed just after image segmentation.

imageProcessing.segmentMasks.get_tessellation(xy, img_shape)#
  • runs the actual tesselation based on the xy position of the markers in an image of given shape

# follow this tutorial # https://hpaulkeeler.com/voronoi-dirichlet-tessellations/ # hpaulkeeler/posts

# changes: # added dummy points outside of the image corners (in quite some distance) # they are supposed “catch” all the vertices that end up at infinity # follows an answer given here # https://stackoverflow.com/questions/20515554/colorize-voronoi-diagram/20678647#20678647

# Attributes # points ndarray of double, shape (npoints, ndim) # Coordinates of input points. # # vertices ndarray of double, shape (nvertices, ndim) # Coordinates of the Voronoi vertices. # # ridge_points ndarray of ints, shape (nridges, 2) # Indices of the points between which each Voronoi ridge lies. # # ridge_vertices list of list of ints, shape (nridges, *) # Indices of the Voronoi vertices forming each Voronoi ridge. # # regions list of list of ints, shape (nregions, *) # Indices of the Voronoi vertices forming each Voronoi region. -1 indicates vertex outside the Voronoi diagram. # # point_region list of ints, shape (npoints) # Index of the Voronoi region for each input point. If qhull option “Qc” was not specified, the list will contain -1 for points that are not associated with a Voronoi region. # # furthest_site # True if this was a furthest site triangulation and False if not. # New in version 1.4.0.

Parameters
  • xy (TYPE) – DESCRIPTION.

  • img_shape (TYPE) – DESCRIPTION.

Returns

voronoi_data – DESCRIPTION.

Return type

TYPE

imageProcessing.segmentMasks.make_segmentations(file_name, current_param, data_path, seg_params: core.parameters.SegmentationParams, align_folder, label)#
imageProcessing.segmentMasks.segment_mask_inhomog_background(im, seg_params: core.parameters.SegmentationParams)#

Function used for segmenting masks with the ASTROPY library that uses image processing

Parameters

im (2D np array) – image to be segmented.

Returns

segm_deblend

Return type

2D np array where each pixel contains the label of the mask segmented. Background: 0

imageProcessing.segmentMasks.segment_mask_stardist(im, seg_params: core.parameters.SegmentationParams)#

Function used for segmenting masks with the STARDIST package that uses Deep Convolutional Networks

Parameters

im (2D np array) – image to be segmented.

Returns

segm_deblend

Return type

2D np array where each pixel contains the label of the mask segmented. Background: 0

imageProcessing.segmentMasks.segment_masks(current_param, data_path, params: core.parameters.SegmentationParams, align_folder, label, file_name=None)#
imageProcessing.segmentMasks.segment_source_flat_background(im, seg_params: core.parameters.SegmentationParams)#

Segments barcodes using flat background :param im: image to be segmented :type im: NPY 2D

Returns

  • table (~astropy.table.Table or None)

  • A table of found stars with the following parameters

  • * ``id`` (unique object identification number.)

  • * ``xcentroid, ycentroid`` (object centroid.)

  • * ``sharpness`` (object sharpness.)

  • * ``roundness1`` (object roundness based on symmetry.)

  • * ``roundness2`` (object roundness based on marginal Gaussian) – fits.

  • * ``npix`` (the total number of pixels in the Gaussian kernel) – array.

  • * ``sky`` (the input sky parameter.)

  • * ``peak`` (the peak, sky-subtracted, pixel value of the object.)

  • * ``flux`` (the object flux calculated as the peak density in) – the convolved image divided by the detection threshold. This derivation matches that of DAOFIND if sky is 0.0.

  • * ``mag`` (the object instrumental magnitude calculated as) – -2.5 * log10(flux). The derivation matches that of DAOFIND if sky is 0.0.

  • None is returned if no stars are found.

  • img_bkc_substracted (2D NPY array with background substracted image)

imageProcessing.segmentMasks.segment_source_inhomog_background(im, seg_params: core.parameters.SegmentationParams)#

Wrapper for function that segments barcodes by estimating inhomogeneous background :param im: image to be segmented :type im: NPY 2D

Returns

  • table (astropy.table.Table or None)

  • A table of found stars with the following parameters

  • * ``id`` (unique object identification number.)

  • * ``xcentroid, ycentroid`` (object centroid.)

  • * ``sharpness`` (object sharpness.)

  • * ``roundness1`` (object roundness based on symmetry.)

  • * ``roundness2`` (object roundness based on marginal Gaussian) – fits.

  • * ``npix`` (the total number of pixels in the Gaussian kernel) – array.

  • * ``sky`` (the input sky parameter.)

  • * ``peak`` (the peak, sky-subtracted, pixel value of the object.)

  • * ``flux`` (the object flux calculated as the peak density in) – the convolved image divided by the detection threshold. This derivation matches that of DAOFIND if sky is 0.0.

  • * ``mag`` (the object instrumental magnitude calculated as) – -2.5 * log10(flux). The derivation matches that of DAOFIND if sky is 0.0.

  • None is returned if no stars are found.

  • img_bkc_substracted (2D NPY array with background substracted image)

imageProcessing.segmentMasks.show_image_masks(im, segm_deblend, markdown_filename, output_filename)#
imageProcessing.segmentMasks.show_image_sources(im, im1_bkg_substracted, sources, markdown_filename, output_filename)#
imageProcessing.segmentMasks.tessellate_masks(segm_deblend)#
  • takes a labeled mask (background 0, nuclei labeled 1, 2, …)

  • calls get_tessellation(xy, img_shape)

  • returns the tesselated mask and the voronoi data structure

Parameters

segm_deblend (TYPE) – Labeled mask.

Returns

  • voronoi_data (TYPE) – DESCRIPTION.

  • mask_voronoi (TYPE) – DESCRIPTION.

imageProcessing.segmentMasks3D module#

Purpose: Segments Masks in 3D using AI (stardist)

steps:
  • iterate over rois

  • load 3D file for cycle <i>

  • load global alignment for this cycle

  • re-align 3D image using XY alignment

  • segment objects to get labeled masks

  • display results:

  • output results

class imageProcessing.segmentMasks3D.Mask3D(param, parallel=False)#

Bases: object

segment_masks_3d(roi_name: str, data_path, dict_shifts_path, seg_params, acq_params, reference_fiducial)#

segments 3D masks in root_folder

Return type

None.

segment_masks_3d_file(filename_to_process, data_path, seg_params, roi_name, acq_params: core.parameters.AcquisitionParams, reference_fiducial: str)#
segment_masks_3d_in_folder(roi_name: str, data_path, dict_shifts_path, seg_params, acq_params: core.parameters.AcquisitionParams, reference_fiducial)#

Segments 3D Masks in all files in root_folder

Return type

None.

imageProcessing.segmentMasks3D.plot_image_3d(image_3d, masks)#

makes list with XY, XZ and ZY projections and sends for plotting

Parameters
  • image_3d (numpy array) – image in 3D.

  • localizations (list) – list of localizations to overlay onto 3D image. The default is None.

Return type

figure handle

imageProcessing.segmentSources3D module#

Created on Mon Mar 15 15:41:58 2021

@author: marcnol

Purpose: Corrects drift in 3D

Often there is drift in the z-position from cycle to cycle.

The drift correction routines take care of the corrections in XY but not in Z.

steps:
  • iterate over rois

  • load 3D file for cycle <i>

  • substract background

  • load global alignment for this cycle

  • re-align 3D image using XY alignment

  • segment objects to get labeled masks

  • Get weighted moments and gaussian fits

  • display results:
    • overlap XY, XZ, YZ image projections with moment and gaussian fits

  • output results in a Table() using same formatting as that used in segmentMasks.py

class imageProcessing.segmentSources3D.Localize3D(param, roi_name: str, acq_params: core.parameters.AcquisitionParams, proj_params: core.parameters.ProjectionParams, reg_params: core.parameters.RegistrationParams, seg_params: core.parameters.SegmentationParams, parallel=False)#

Bases: object

plot_image_3d(image_3d, localizations=None, masks=None, normalize_b=None)#

makes list with XY, XZ and ZY projections and sends for plotting

Parameters
  • image_3d (numpy array) – image in 3D.

  • localizations (list) – list of localizations to overlay onto 3D image. The default is None.

Return type

figure handle

segment_sources_3d(data_path, dict_shifts_path, params: core.parameters.SegmentationParams)#

runs 3D fitting routine in root_folder

Return type

None.

segment_sources_3d_file(filename_to_process, data_path, seg_params)#
segment_sources_3d_in_folder(data_path, dict_shifts_path, seg_params)#

Fits sources in all files in root_folder

Return type

None.

imageProcessing.segmentSources3D.create_output_table()#
imageProcessing.segmentSources3D.get_mask_properties(segmented_image_3d, image_3d_aligned, threshold=10, n_tolerance=1000)#

get object properties from labeled image and formats centroids in NPY array

Parameters
  • segmented_image_3d (NPY 3D array) – labeled 3D image.

  • image_3d_aligned (NPY 3D array) – pre-processed 3D image.

Returns

spots – list of spots with the format: zyx

Return type

NPY int64 array

Module contents#

Created on Wed Aug 12 15:59:09 2020

@author: marcnol

The pyHi-M.projections package includes functions to produce 2D projections from 3d images