magmap.io.export_stack module

Import and export image stacks in various formats.

class magmap.io.export_stack.StackPlaneIO[source]

Bases: SharedArrsContainer

Worker class to export planes from a stack with support for multiprocessing.

Variables:
  • imgs – A list of images, with exact specification determined by the calling function.

  • images – Sequence of images. For import, each “image” is a path to and image file. For export, each “image” is a sequence of planes, with the first sequence assumed to an atlas, followed by labels-based images, each consisting of corresponding planes.

  • fn_process – Function to process each image through multiprocessing, where the function should take an index and image and return the index and processed plane.

  • rescale (float) – Rescale factor; defaults to 1.

  • cmaps_labels – Sequence of colormaps for labels-based images; defaults to None. Length should be equal to that of images - 1.

  • start_planei (int) – Index of start plane, used for labeling the plane; defaults to 0. The plane is only annotated when config.plot_labels[config.PlotLabels.TEXT_POS] is given to specify the position of the text in x,y relative to the axes.

build_stack(axs, scale_bar=True, fit=False)[source]

Builds a stack of Matploblit 2D images.

Uses multiprocessing to load or resize each image.

Parameters:
  • axs (List) – Sub-plot axes.

  • scale_bar (bool, default: True) – True to include scale bar; defaults to True.

  • fit (bool, default: False) – True to fit the figure frame to the resulting image.

Return type:

Optional[List]

Returns:

List[List[matplotlib.image.AxesImage]]: Nested list of axes image objects. The first list level contains planes, and the second level are channels within each plane.

classmethod convert_imgs()[source]

Restore all shared arrays to a list of arrays.

imgs = None
classmethod import_img(i, rescale, multichannel)[source]

Import and rescale an image.

Assumes that :attr:imgs is a list of paths to 2D images.

Parameters:
  • i – Index within :attr:imgs to plot.

  • rescale – Rescaling multiplier.

  • multichannel – True if the images are multichannel.

Returns:

A tuple of i and a list of the processed images. The processed image list has the same length as :attr:imgs, or the number of image paths.

interp_order: Optional[int] = None

Interpolation order, used in skimage.transform.resize().

classmethod process_plane(i, target_size, multichannel, rotate=None)[source]

Process corresponding planes from related images.

Assumes that :attr:imgs is a list of nested 2D image lists, where the first nested list is assumed to be a sequence of histology image planes, while subsequent images are labels-based images.

Parameters:
  • i – Index within nested lists of :attr:imgs to plot.

  • target_size – Resize to this shape.

  • multichannel – True for multichannel images.

  • rotate (default: None) – Degrees by which to rotate; defaults to None.

Returns:

A tuple of i and a list of the processed images. The processed image list has the same length as :attr:imgs, or the number of nested lists.

classmethod set_data(imgs)[source]

Set data to be accessed by worker functions.

magmap.io.export_stack.animate_imgs(base_path, plotted_imgs, delay, ext=None, suffix=None)[source]

Export to an animated image.

Defaults to an animated GIF unless ext specifies otherwise. Requires FFMpeg for MP4 file format exports and ImageMagick for all other types of exports.

Parameters:
  • base_path (str) – String from which an output path will be constructed.

  • (List[ (plotted_imgs) – obj:`matplotlib.image.AxesImage]): Sequence of images to include in the animation.

  • delay (int) – Delay between image display in ms. If None, the delay will defaul to 100ms.

  • ext (str) – Extension to use when saving, without the period. Defaults to None, in which case “gif” will be used.

  • suffix (str) – String to append to output path before extension; defaults to None to ignore.

magmap.io.export_stack.export_planes(image5d, ext, channel=None, separate_chls=False)[source]

Export all planes of a 3D+ image into separate 2D image files.

Unlike stack_to_img(), this method exports raw planes and each channels into separate files, without processing through Matplotlib. Supports image rotation set in magmap.settings.config.transform.

By default, all z-planes are exported, with plane indices specified through config.slice_vals. Alternatively, regions of interest can be specified by config.roi_offset and config.roi_size. The planar orientation can be configured through config.plane.

Parameters:
  • image5d (ndarray) – Image in t,z,y,x[,c] format.

  • ext (str) – Save format given as an extension without period.

  • channel (Optional[int], default: None) – Channel to save; defaults to None for all channels.

  • separate_chls (bool) – True to export all channels from each plane to a separate image; defaults to False.

magmap.io.export_stack.reg_planes_to_img(imgs, path=None, ax=None)[source]

Export registered image single planes to a single figure.

Simplified export tool taking a single plane from each registered image type, overlaying in a single figure, and exporting to file.

Parameters:
  • imgs (List[np.ndarray]) – Sequence of image planes to display. The first image is assumed to be greyscale, the second is labels, and any subsequent images are borders.

  • path (str) – Output base path, which will be combined with config.savefig; defaults to None to not save.

  • ax (matplotlib.image.Axes) – Axes on which to plot; defaults to False, in which case a new figure and axes will be generated.

magmap.io.export_stack.setup_stack(image5d, path=None, offset=None, roi_size=None, slice_vals=None, rescale=None, labels_imgs=None)[source]

Set up a stack of images for export to file.

Supports a stack of image files in a directory or a single volumetric image and associated labels images.

Parameters:
  • image5d (ndarray) – Images as a 4/5D Numpy array (t,z,y,x[c]). Can be None if path is set.

  • path (Optional[str], default: None) – Path to an image directory from which all files will be imported in Python sorted order, taking precedence over imaged5d; defaults to None.

  • offset (Optional[Sequence[int]], default: None) – Tuple of offset given in user order (x, y, z); defaults to None. Requires roi_size to not be None.

  • roi_size (Optional[Sequence[int]], default: None) – Size of the region of interest in user order (x, y, z); defaults to None. Requires offset to not be None.

  • slice_vals (Optional[Sequence[int]], default: None) – List from which to construct a slice object to extract only a portion of the image. Defaults to None, which will give the whole image. If offset and roi_size are also given, slice_vals will only be used for its interval term.

  • rescale (Optional[float], default: None) – Rescaling factor for each image, performed on a plane-by-plane basis; defaults to None, in which case 1.0 will be used.

  • labels_imgs (Optional[Sequence[ndarray]], default: None) – Sequence of labels-based images as a Numpy z,y,x arrays, typically including labels and borders images; defaults to None.

Return type:

StackPlaneIO

Returns:

Stack builder instance.

magmap.io.export_stack.stack_to_img(paths, roi_offset, roi_size, series=None, subimg_offset=None, subimg_size=None, animated=False, suffix=None)[source]

Build an image file from a stack of images in a directory or an array, exporting as an animated GIF or movie for multiple planes or extracting a single plane to a standard image file format.

Writes the file to the parent directory of path.

Parameters:
  • paths (List[str]) – Image paths, which can each be either an image directory or a base path to a single image, including volumetric images. If more than one path is given, a collage of images will be generated with the layout set by magmap.settings.config.plot_labels[config.PlotLabels.LAYOUT].

  • roi_offset (Sequence[int]) – Tuple of offset given in user order x,y,z; defaults to None. Requires roi_size to not be None.

  • roi_size (Sequence[int]) – Size of the region of interest in user order x,y,z; defaults to None. Requires roi_offset to not be None.

  • series (int) – Image series number; defaults to None.

  • subimg_offset (List[int]) – Sub-image offset as (z,y,x) to load; defaults to None.

  • subimg_size (List[int]) – Sub-image size as (z,y,x) to load; defaults to None.

  • animated (bool) – True to export as an animated image; defaults to False.

  • suffix (str) – String to append to output path before extension; defaults to None to ignore.