CNN -> Data processing

Image patching - Image trimming

tardis_em.cnn.data_processing.trim.trim_with_stride(image: ndarray, trim_size_xy: int, trim_size_z: int, output: str, image_counter: int, scale: list, clean_empty=True, keep_if=0.01, stride=25, mask: ndarray | None = None, log=True, pixel_size=None, device=device(type='cpu'))

Function to patch image and mask to specified patch size with overlying area

Output images are saved as tiff with naming shame 1_1_1_25. Where number indicate grid position in xyz. Last number indicate stride.

Parameters:
  • image (np.ndarray) – Corresponding image for the labels.

  • trim_size_xy (int) – Size of trimming in xy dimension.

  • trim_size_z (int) – Size of trimming in z dimension.

  • output (str) – Name of the output directory for saving.

  • image_counter (int) – Number id of image.

  • scale (tuple) – Up- DownScale image and mask to the given shape or factor.

  • clean_empty (bool) – Remove empty patches.

  • keep_if (float) – If float, keep only patches that have mask. Evaluated based on % of pixels with mask

  • stride (int) – Trimming step size.

  • mask (np.ndarray, None) – Label mask.

  • log (bool) – If True, output trimming log information.

  • pixel_size (None, float) – If not None, save mask as mrc with pixel size information.

  • device (torch.device) – Optional device.

tardis_em.cnn.data_processing.trim.trim_label_mask(points: ndarray, image: ndarray, label_mask: ndarray) Tuple[ndarray, ndarray, ndarray]

! DEPRECIATED ! Module to trim image and mask to boundary box of point cloud.

Parameters:
  • points (np.ndarray) – 3D coordinates of pitons.

  • image (np.ndarray) – corresponding image for the labels.

  • label_mask (np.ndarray) – empty label mask.

Image patching - Image stitching

class tardis_em.cnn.data_processing.stitch.StitchImages

MAIN MODULE TO STITCH IMAGE FROM IMAGE PATCHES

Class object to stitch cut date into one big image. Object recognize images with naming 0_1_1_1_25_pf where: - 0 indicate id of image - 1 indicate xyz position - 25 indicate stride value for overlap - pf indicate optional prefix in file name

tardis_em.cnn.data_processing.stitch.generate_grid(image_size: tuple, patch_size: list, grid_size: list, stride: int)

Generates grid coordinates for either 2D or 3D images.

Parameters:
  • image_size (list) – The dimensions of the image. For 3D, it should be [z, y, x], and for 2D, [y, x].

  • patch_size (list) – The dimensions of each patch. For 3D, [nz, ny, nx], and for 2D, [ny, nx].

  • grid_size (list) – The grid size. For 3D, [gz, gy, gx], and for 2D, [gy, gx].

  • stride (int) – The stride for grid generation.

Returns:

A tuple of numpy arrays with coordinates of the grid.

Return type:

Tuple[list]

Utils

tardis_em.cnn.data_processing.interpolation.interpolate_generator(points: ndarray) Iterable

Generator for 3D array interpolation

Parameters:

points – Expect array of 2 point in 3D as [X x Y x (Z)] of [2, 3] shape

Returns:

Iterable object to generate 3D list of points between given 2 points as [X x Y x (Z)]

Return type:

Iterable

tardis_em.cnn.data_processing.interpolation.interpolation(points: ndarray) ndarray

3D INTERPOLATION FOR BUILDING SEMANTIC MASK

Parameters:

points (np.ndarray) – numpy array with points belonging to individual segments given by x, y, (z) coordinates.

Returns:

Interpolated 2 or 3D array

Return type:

np.ndarray

tardis_em.cnn.data_processing.draw_mask.draw_instances(mask_size: list | tuple, coordinate: ndarray, pixel_size: float, circle_size=250, label=True, dtype=None) ndarray

Module to build semantic mask from corresponding coordinates

Parameters:
  • mask_size (tuple) – Size of array that will hold created mask.

  • coordinate (np.ndarray) – Segmented coordinates of a shape [Label x X x Y x (Z)].

  • pixel_size (float) – Pixel size in Angstrom.

  • circle_size (int) – Size of a circle the label mask in Angstrom.

  • label (bool) – If True, expect label point cloud.

  • dtype (dtype)

Returns:

Binary mask with drawn all coordinates as lines.

Return type:

np.ndarray

tardis_em.cnn.data_processing.draw_mask.draw_semantic_membrane(mask_size: tuple, coordinate: ndarray, pixel_size: float, spline_size=70) ndarray

Draw semantic membrane

For each Z pick individual instance and draw a fitted spline of given thickness.

Parameters:
  • mask_size (tuple) – Size of array that will hold created mask.

  • coordinate (np.ndarray) – Segmented coordinates of a shape [Label x X x Y x (Z)].

  • pixel_size (float) – Pixel size in Angstrom.

  • spline_size (int) – Size of a circle the label mask in Angstrom.

Returns:

Binary mask with drawn all coordinates as lines.

Return type:

np.ndarray

tardis_em.cnn.data_processing.draw_mask.draw_instances_membrane(mask_size: tuple, coordinate: ndarray, pixel_size: float, spline_size=70) ndarray

Draw instances membrane

For each Z pick individual instance and draw a fitted spline of given thickness.

Parameters:
  • mask_size (tuple) – Size of array that will hold created mask.

  • coordinate (np.ndarray) – Segmented coordinates of a shape [Label x X x Y x (Z)].

  • pixel_size (float) – Pixel size in Angstrom.

  • spline_size (int) – Size of a circle the label mask in Angstrom.

Returns:

Binary mask with drawn all coordinates as lines.

Return type:

np.ndarray

tardis_em.cnn.data_processing.draw_mask.draw_mask(r: int, c: ndarray, label_mask: ndarray, segment_shape: str) Tuple[ndarray, ndarray] | Tuple[ndarray, ndarray, ndarray]

Module draw_mask to construct sphere shape of a label

Parameters:
  • r (int) – radius of a circle in Angstrom.

  • c (np.ndarray) – point in 3D indicating center of a circle [X x Y x Z].

  • label_mask (np.ndarray) – array of a mask on which circle is drawn.

  • segment_shape (str) – Type of shape to draw. Expect [‘s’, ‘c’].

Returns:

Binary mask.

Return type:

np.ndarray

tardis_em.cnn.data_processing.draw_mask.draw_circle(r: int, c: tuple, shape: tuple) Tuple[ndarray, ndarray] | Tuple[ndarray, ndarray, ndarray]

Draw a circle and shift coordinate to c position.

Parameters:
  • r (int) – radius of a circle in Angstrom.

  • c (tuple) – point in 3D indicating center of a circle [(Z), Y, X].

  • shape (tuple) – Shape of mask to eliminated ofe-flowed pixel.

Returns:

Tuple of array’s with zyx coordinates

Return type:

Tuple[np.ndarray, np.ndarray, np.ndarray]

tardis_em.cnn.data_processing.draw_mask.draw_sphere(r: int, c: tuple, shape: tuple) Tuple[ndarray, ndarray, ndarray]

Draw a sphere and shift coordinate to c position.

Parameters:
  • r (int) – radius of a sphere in Angstrom.

  • c (tuple) – point in 3D indicating center of a sphere [Z, Y, X].

  • shape (tuple) – Shape of mask to eliminated ofe-flowed pixel.

Returns:

Tuple of array’s with zyx coordinates

Return type:

Tuple[np.ndarray, np.ndarray, np.ndarray]

tardis_em.cnn.data_processing.scaling.scale_image(scale: tuple, image: ndarray | None = None, mask: ndarray | None = None, device=device(type='cpu')) Tuple[ndarray, ndarray, int] | Tuple[ndarray, int] | Tuple[None, int]

Scale image module using torch GPU interpolation

Expect 2D (XY/YX), 3D (ZYX)

Parameters:
  • image (np.ndarray, Optional) – image data

  • mask (np.ndarray, Optional) – Optional binary mask image data

  • scale (tuple) – scale value for image.

  • device (torch.device)

tardis_em.cnn.data_processing.scaling.nn_scaling(img: ndarray, scale: tuple, device=device(type='cpu'), gauss=False) ndarray
tardis_em.cnn.data_processing.scaling.linear_scaling(img: ndarray, scale: tuple, device=device(type='cpu')) ndarray

Scaling of 2D/3D array using trilinear method from pytorch

Parameters:
  • img – image array.

  • scale – Scale array size.

  • device (torch.device) – Compute device

Returns:

Up or Down scale 3D array.

Return type:

no.ndarray

tardis_em.cnn.data_processing.scaling.area_scaling(img: ndarray, scale: tuple, device=device(type='cpu')) ndarray

Scaling of 3D array using area method from pytorch

Parameters:
  • img – 3D array.

  • scale – Scale array size.

  • device (torch.device) – Compute device

Returns:

Up or Down scale 3D array.

Return type:

no.ndarray

tardis_em.cnn.data_processing.scaling.fourier_scaling(img: ndarray, scale: tuple, device=device(type='cpu')) ndarray

Resize a 2D or 3D image using Fourier cropping.

Parameters: img (np.ndarray): 2D or 3D array representing the image. scale (tuple): Desired shape (for 2D: (height, width), for 3D: (depth, height, width)). dtype (np.dtype): Desired output data type. device (torch.device): Torch device

Returns:

Resized image in the desired data type.

Return type:

np.ndarray