magmap.atlas.transformer module#

Transform large images with multiprocessing, including up/downsampling and image transposition.

class magmap.atlas.transformer.Downsampler[source]#

Bases: object

Downsample (or theoretically upsample) a large image in a way that allows multiprocessing without global variables.

Variables:

img (np.ndarray) – Full image array.

img = None#
classmethod rescale_sub_roi(coord, slices, target_size, multichannel, sub_roi=None)[source]#

Rescale or resize a sub-ROI.

Parameters:
  • coord (Sequence[int]) – Coordinates as a tuple of (z, y, x) of the sub-ROI within the chunked ROI.

  • slices (Sequence[slice]) – Sequence of slices within :attr:img defining the sub-ROI.

  • target_size (Optional[Sequence[int]]) – Target rescaling size for the given sub-ROI in (z, y, x). If rescale is not None, target_size will be ignored.

  • multichannel (bool) – True if the final dimension is for channels.

  • sub_roi (Optional[ndarray], default: None) – Array chunk to rescale/resize; defaults to None to extract from img if available.

Return type:

Tuple[Sequence[int], ndarray]

Returns:

Tuple of coord and the rescaled sub-ROI, where coord is the same as the given parameter to identify where the sub-ROI is located during multiprocessing tasks.

classmethod set_data(img)[source]#

Set the class attributes to be shared during multiprocessing.

Parameters:

img (np.ndarray) – See attributes.

magmap.atlas.transformer.get_transposed_image_path(img_path, scale=None, target_size=None)[source]#

Get path modified for any transposition.

Parameters:
  • img_path (str) – Unmodified image path.

  • scale (Optional[float], default: None) – Scaling factor, which takes precedence over target_size; defaults to None.

  • target_size (Optional[Sequence[int]], default: None) – Target size in x, y, z, typically given by an atlas profile; defaults to None.

Return type:

str

Returns:

Modified path for the given transposition, or img_path unmodified if all transposition factors are None.

magmap.atlas.transformer.make_modifier_plane(plane)[source]#

Make a string designating a plane orthogonal transformation.

Parameters:

plane – Plane to which the image was transposed.

Returns:

String designating the orthogonal plane transformation.

magmap.atlas.transformer.make_modifier_resized(target_size)[source]#

Make a string designating a resize transformation.

Note that the final image size may differ slightly from this size as it only reflects the size targeted.

Parameters:

target_size – Target size of rescaling in x,y,z.

Returns:

String designating the resize transformation.

magmap.atlas.transformer.make_modifier_scale(scale)[source]#

Make a string designating a scaling transformation, typically for filenames of rescaled images.

Parameters:

scale (float) – Scale to which the image was rescaled. Any decimal point will be replaced with “pt” to avoid confusion with path extensions.

Returns:

String designating the scaling transformation.

Return type:

str

magmap.atlas.transformer.preprocess_img(image5d, preprocs, channel, out_path)[source]#

Pre-process an image in 3D.

Parameters:
  • image5d (np.ndarray) – 5D array in t,z,y,x[,c].

  • preprocs (Union[str, list[str]]) – Pre-processing tasks that will be converted to enums in config.PreProcessKeys to perform in the order given.

  • channel (int) – Channel to preprocess, or None for all channels.

  • out_path (str) – Output base path.

Returns:

The pre-processed image array.

Return type:

np.ndarray

magmap.atlas.transformer.rotate_img(roi, rotate=None, order=None)[source]#

Rotate an ROI based on atlas profile settings.

Parameters:
  • roi (np.ndarray) – Region of interst array (z,y,x[,c]).

  • rotate (dict) – Dictionary of rotation settings in magmap.settings.atlas_profile. Defaults to None to take the value from config.register_settings.

  • order (int) – Spline interpolation order; defalts to None to use the value from within rotate. Should be 0 for labels.

Returns:

The rotated image array.

Return type:

np.ndarray

magmap.atlas.transformer.transpose_img(filename, series, plane=None, rescale=None, target_size=None)[source]#

Transpose large NumPy saved arrays, including rescaling or resizing.

Loads a saved array to tranpose its planar orientation. Supports large arrays, with rescaling/resizing performed in multiprocessing and file saving through memmap-based arrays to minimize RAM usage. Output filenames are based on the make_modifer_[task] functions. Currently, transposes all channels, ignoring :attr:magmap.settings.config.channel parameter.

Parameters:
  • filename (str) – Full file path in :attribute:cli:`filename` format.

  • series (Optional[int]) – Series within multi-series file.

  • plane (Optional[str], default: None) – Planar orientation (see magmap.settings.config.PLANES). Defaults to None, in which case no planar transformation will occur.

  • rescale (Optional[float], default: None) – Rescaling factor; defaults to None. Takes precedence over target_size.

  • target_size (Optional[Sequence[int]], default: None) – Target shape in x,y,z; defaults to None, in which case the target size will be extracted from the register profile if available.