tilejoining

pysmFISH.stitching_package.tilejoining.apply_IJ_corners(tiles, corners, micData, nr_pixels)[source]

Use corners from ImageJ for each tile to detemine corners.

tiles: list
List of hdf5-references, references to the images in tile_file, with a reference for each tile. Here only used to check the length. If a tile does not have an associated image, its reference is None.
corners: list
List of list, each list is a pair of an image number (int) and it’s coordinates (numpy array containing floats).
micData: object
MicData object. Used to make an image list: the numbers of the images to be stitches sorted according to the tile indexes.
nr_pixels: int
Denoting size of the tile.
: dict
Same as calc_corners_coord() returns. Dictionary containing keys corner_list and final_image_shape. Corner_list is a list of list, each list is a pair of an image number (int) and it’s coordinates (numpy array containing floats). Final_image_shape is a tuple of size 2 or 3 depending on the numer of dimensions and contains ints.
pysmFISH.stitching_package.tilejoining.assess_overlap(joining, tiles, tile_file, contig_tuples)[source]

Calculate the covariance of the overlap

This function is usefull to see the quality of the final overlap. It always works with flattened (max projected) tiles.

joining: dict
Dictionary containing the corner list (with key: ‘corner_list’) with the tile indexes and their corresponding corners
tiles: list
List of hdf5-references, references to the images in tile_file, with a reference for each tile. If a tile does not have an associated image, its name is None.
tile_file: pointer
hdf5 file object. The opened file containing the tiles to stitch.
contig_tuples: list
List of tuples. Each tuple is a tile pair. Tuples contain two tile indexes denoting these tiles are contingent to each other.
xcov: float
The cross covariance between the overlapping parts of the two images.
pysmFISH.stitching_package.tilejoining.calc_corners_coord(tiles, transforms, md, nr_pixels, z_count)[source]

Calculate global corner for each tile using coordinates.

Calculates global corner for each tile using transforms and the microscope coordinates.

tiles: list
List of hdf5-references, references to the images in tile_file, with a reference for each tile. Here only used to check the length. If a tile does not have an associated image, its reference is None.
transforms: np.array
2d np array of size “number of tiles” by 2 representing the y and x transform for each tile.
md: object
MicroscopeData object containing the tile set (to know the positioning of the tiles) and the y and x coordinates for each tile as documented by the microscope.
nr_pixels: int
Height and length of the tile in pixels, tile is assumed to be square.
z_count: int
The number of layers in one tile (size of the z axis). Should be 1 or None if the tile is 2D.
: dict
Dictionary containing keys corner_list and final_image_shape. Corner_list is a list of list, each list is a pair of an image number (int) and it’s coordinates (numpy array containing floats). Final_image_shape is a tuple of size 2 or 3 depending on the number of dimensions and contains ints.
pysmFISH.stitching_package.tilejoining.check_corner_blending(weights, border_dist_list, max_dist_pixels, overlapping_values, border_direct, border_indirect, center, linear_blending, max_value=4)[source]

Function to improve blending of differently overlapping corners.

In a corner 2, 3 or 4 tile can overlap. Depending on how many tiles overlap and in wich direction they overlap the weights for a tile are adjusted by this function.

weights: np.array
1D numpy array. A weight for each overlapping pixel, as determined by the function perform_blending_par_proof (Weights depend on direct borders, or 1 if there is only a lonely corner)
border_dist_list: np.array
2D numpy array of shape: (nr of overlapping pixels, 4). Array containing for each pixel the distance to each of the four borders in this order [top, left, bottom, right], for borders that do not overlap to another tile the distance has been set to 999 or greater.
max_dist_pixels: np.array
Numpy array of shape: (1, 4). The maximum distance to each of the four borders in this order [top, left, bottom, right], for borders that do not overlap to another tile the max distance will be to 9999 or greater.
overlapping_values: np.array
1D numpy array of floats. An float value indicating how many tiles are overlapping for each overlapping pixel. This array has the same size as weights.
border_direct: np.array
1 by 4 numpy array. Array indicating for each border if there is a direct overlap. If there is overlap the value is 0, otherwise the value is 9999.
border_indirect: np.array
1 by 4 numpy array. Array indicating for each border if there is a indirect overlap. If there is overlap the value is 0, otherwise the value is 9999.
center: int
The center of th tile, this is the same number in the x and y direction.
linear_blending: bool
If true perform linear blending, otherwise non-linear blending.
max_value: int
The maximum value expected in the overlap. Default is 4, this is the overall maximum in the blending mask. When max_value is 2, we assume that the tile only overlaps in one or multiple corners. Because when only a corner of a tile is overlapping and there is no other overlap the maximum is 2. (Default: 4)
weights: np.array
1D numpy array. The array ‘weights’ that was passed to as an argument, but now adjusted for the corners.
pysmFISH.stitching_package.tilejoining.generate_blended_tile(temp_file, im_file, tiles, tile_file, corner_ind_coord, nr_pixels, tile_set, blend, linear_blending, ubyte, nr_dim=2)[source]

Blend the tile if necessary and then save it temp_file.

temp_file: pointer
Pointer to hdf5 file withv following groups: tiles, temp_masks, ubytes. Each group contains as many datasets as there are tiles, the datasets are named after the the tile index found in the first element of corner. This function places a blended tile and a corner in data set that matches the tile ind argument.
im_file: pointer
Pointer to hdf5 file with dataset “blending_mask” which contains a numpy array. blending_mask should be 1 where ther is no overlap and 2, 3 or 4 where the respective number of tiles overlap. Other datasets in this file are: final_image and temp_mask
tiles: list
List of hdf5-references, references to the images in tile_file, with a reference for each tile. If a tile does not have an associated image, its reference is None.
tile_file: pointer
hdf5 file object. The opened file containing the tiles to stitch.
corner_ind_coord: list
Contains two elements, the first one is an int representing the tile index, the second one is a numpy array containing the corner’s coordinates.
nr_pixels: int
Denoting size of the tile.
tile_set: np.array
Masked numpy array. The shape of the array indicates the shape of the tile set.
blend: bool
When True blending will be applied, when false no blending at all will be applied.
linear_blending: bool
When True blending will be linear and when False, blending will be non-linear.
ubyte: bool
Ubyte image will be saved when True. Only full resolution image will be saved when False.
nr_dim: int
If 3, the code will assume three dimensional data for the tile, where z is the first dimension and y and x the second and third. For any other value 2-dimensional data is assumed. (default: 2)
pysmFISH.stitching_package.tilejoining.generate_blended_tile_npy(corner_ind_coord, stitching_files_dir, blended_tiles_directory, masked_tiles_directory, analysis_name, processing_hyb, reference_gene, micData, tiles, nr_pixels, linear_blending)[source]

Blend the tile if necessary and then save it temp blended folder. Modification of the generate_blended_tile that run using .npy files and doesn’t save the data in a hdf5 file

corner_ind_coord: list
Contains two elements, the first one is an int representing the tile index, the second one is a numpy array containing the corner’s coordinates.
stitching_files_dir: str
Path to the files to stitch
analysis_name: str
Name of the current analysis
blended_tiles_directory: str
Path to the directory where to save the blended tiles
masked_tiles_directory: str
Path to the directory with the masks
processing_hyb: str
Name of the hybridization processed
reference_gene: str
Name of the gene to be stitched
blending_mask: np.array
Array containing the blending mask used for blending the images
micData: object
MicroscopeData object. Contains coordinates of the tile corners as taken from the microscope.
tiles: np.array
Array with tile number. -1 correspond to missing tile
nr_pixels: int
Denoting size of the tile.
linear_blending: bool
When True blending will be linear and when False, blending will be non-linear.
pysmFISH.stitching_package.tilejoining.make_final_image(joining, temp_file, im_file, nr_pixels)[source]

Puts blended tiles at the correct position in the final image.

Takes blended tiles as found in temp_file and “pastes” them at the correct position as indicated by joining in the final image. The final image is kept in im_file.

joining: dict
Containing corners for tiles
temp_file: pointer
Pointer to hdf5 object with the following groups: tiles, temp_masks, ubytes. Each group contains as many datasets as there are tiles, the datasets are named after the the tile index found in the first element of corner. This function places a blended tile and a corner in data set that matches the tile ind argument.
im_file: pointer
Pointer to hdf5 object with dataset “blending_mask” which contains a numpy array. blending_mask should be 1 where ther is no overlap and 2, 3 or 4 where the respective number of tiles overlap. Other datasets in this file are: final_image and temp_mask
nr_pixels: int
Size of the tile, tile is assumed to be a square.
pysmFISH.stitching_package.tilejoining.make_final_image_npy(joining, stitching_file, blended_tiles_directory, tiles, gene, nr_pixels)[source]

Puts blended tiles at the correct position in the final image. Modified version of the make_final_image that works on .npy stored images. Works only in 2D.

Takes blended tiles as found in the blended_tiles_directory and “pastes” them at the correct position as indicated by joining in the final image in the hdf5 file.

joining: dict
Containing corners for tiles
stitching_file: pointer
Pointer to hdf5 object with the following groups:
blended_tiles_directory: str
Path to the directory where to save the blended tiles
tiles: np.array
Array with tile number. -1 correspond to missing tile
gene: str
Name of the gene to be stitched
nr_pixels: int
Size of the tile, tile is assumed to be a square.
pysmFISH.stitching_package.tilejoining.make_mask(joining, nr_pixels, blending_mask)[source]

Calculate the mask that indicates where tiles overlap.

This mask will have the same size a the final image. This function assigns a float (1.0, 2.0, 3.0 or 4.0) to each pixel, indicating if 1, 2, 3 or 4 tiles are going to overlap in this pixel.

joining: dict
Dictionary containing the corner list (with key: ‘corner_list’) with the tile indexes and their corresponding corners
nr_pixels: int
Indicates the size of the tiles
blending_mask: pointer
Dataset in an hdf5 file containing a 2D numpy array. Array has the size of final image.
pysmFISH.stitching_package.tilejoining.non_linear_blending(x)[source]

Define sigmoid for non-linear blending

The steepness and half value of the curve are hardcoded here and good for a 10% overlap. For other overlaps flexibity in the steepness may be good to implement. (Halfpoint should be the same)

x: np.array
1d np-array, used as x values in the sigmoid curve
y : np.array
1d np-array, y values corresponding to x after applying sigmoid function on them
pysmFISH.stitching_package.tilejoining.non_linear_blending_corner(x, y)[source]

Calculate blending weights for pixels where four tiles overlap

x: np.array
1d numpy array, x and y should be of the same length. The distance from the corner in the x direction for each pixel.
y: np.array
1d numpy array, x and y should be of the same length. The distance from the corner in the x direction for each pixel.
: np.array
1d numpy array, same size as x and y. Weight for each pixel.
pysmFISH.stitching_package.tilejoining.paste_in_final_image_MPI(joining, temp_file, im_file, tile_ind, nr_pixels)[source]

Puts one blended tile at the correct position in the final image.

Takes the blended tile as found at tile_ind in temp_file and “pastes” them at the correct position as indicated by joining in the final image. The final image is kept in im_file.

joining: dict
Containing corners for tiles
temp_file: pointer
Pointer to hdf5 object with the following groups: tiles, temp_masks, ubytes. Each group contains as many datasets as there are tiles, the datasets are named after the the tile index found in the first element of corner. This function places a blended tile and a corner in data set that matches the tile ind argument.
im_file: pointer
Pointer to hdf5 object with dataset “blending_mask” which contains a numpy array. blending_mask should be 1 where ther is no overlap and 2, 3 or 4 where the respective number of tiles overlap. Other datasets in this file are: final_image and temp_mask
tile_ind: int
Index of the tile that should be placed
nr_pixels: int
Size of the tile, tile is assumed to be a square.
pysmFISH.stitching_package.tilejoining.perform_blending_par_proof(tile_ind, cur_mask, cur_tile, linear_blending, tiles, tile_set, nr_pixels)[source]

Blend a tile with the background around it.

Each pixel in the overlap gets a weight depending on its distance from the border.

tile_ind: int
Index of the current tile
cur_mask: np.array
The mask denoting were in the picture tile have already been placed. A value of 0 means no picture, a value of 1 means that a picture has been placed, a value of 2 means that there is overlap with the current tile.
cur_tile: np.array
The warped image to be placed..
linear_blending: bool
If true perform linear blending, otherwise non-linear blending.
tiles: list
List of hdf5-references, references to the images in tile_file, with a reference for each tile. Here only used to check if a tile exists, if a tile does not have an associated image, its reference is None.
tile_set: np.array
np-array representing the shape of the tile set
nr_pixels: int
Denoting size of the tile.
cur_tile: np.array
The blended cur_tile, same type and size as the input argument cur_type.
cur_temp_mask: np.array
Array like the cur_mask as it was passed to the function, but with the pixels that overlap replaced by their respective weights. This mask can be plotted for debugging.