magmap.plot.plot_3d module

3D image stack interaction.

Prepare volumetric image stacks for plotting.

magmap.plot.plot_3d.build_ground_truth(img3d, blobs, ellipsoid=False, labels=None, spacing=None)[source]

Build ground truth volumetric image from blobs.

Variables:
  • img3d – Image as 3D Numpy array in which to store results

  • blobs – Numpy array of segments to display, given as an (n, 4) dimension array, where each segment is in (z, y, x, radius).

  • ellipsoid – True to draw blobs as ellipsoids; defaults to False.

  • labels – Array of labels the same length as blobs to assign as the values for each ground truth; defaults to None to assign a default value of 1 instead.

  • spacing – Spacing by which to multiply blobs` radii; defaults to None, in which case each blob’s radius will be used for all dimensions.

Returns:

img3d with ground drawn as circles or ellipsoids.

magmap.plot.plot_3d.deconvolve(roi)[source]

Deconvolves the image.

Parameters:

roi – ROI given as a (z, y, x) subset of image5d.

Returns:

The ROI deconvolved.

magmap.plot.plot_3d.denoise_roi(roi, channel=None)[source]

Denoise and further preprocess an image.

Applies saturation, denoising, unsharp filtering, and erosion as image preprocessing for blob detection.

Each step can be configured including turned off by magmap.settings.config.roi_profiles.

Parameters:
  • roi (ndarray) – Region of interest as a 3D (z, y, x) array. Note that 4D arrays with channels are not allowed as the Scikit-Image gaussian filter only accepts specifically 3 channels, presumably for RGB.

  • channel (Optional[Sequence[int]], default: None) – Sequence of channel indices in roi to saturate. Defaults to None to use all channels.

Return type:

ndarray

Returns:

Denoised region of interest.

magmap.plot.plot_3d.get_isotropic_vis(settings)[source]

Get the isotropic factor scaled by the profile setting for visualization.

Visualization may require the isotropic factor to be further scaled, such as for images whose z resolution is less precise that x/y resolution.

Parameters:

settings (magmap.settings.profiles.SettingsDict) – Settings dictionary from which to retrieve the isotropic visualization value.

Returns:

Isotropic factor based on the profile setting.

Return type:

numpy.ndarray

magmap.plot.plot_3d.pad_img(img, offset, shape)[source]

Pad image surroundings with zeros.

Parameters:
  • img (numpy.ndarray) – Image array.

  • offset (tuple[int, int, int]) – Offset within padded image at which to place img, given as a sequence of z,y,x.

  • shape (tuple[int, int, int]) – Shape of resulting image, given as a sequence of z,y,x. Values can be None or sequence can stop early to use the corresponding original shape values from img.

Returns:

Padded image.

Return type:

numpy.ndarray

magmap.plot.plot_3d.prepare_roi(image5d, roi_offset, roi_size, *args, **kwargs)[source]

Extracts a region of interest (ROI).

Calls prepare_subimage() but expects size and offset variables to be in x,y,z order following this software’s legacy convention.

Parameters:
  • image5d (ndarray) – 5D image array in the order, t,z,y,x[,c], where the final dimension is optional as with many one channel images.

  • roi_offset (Sequence[int]) – Tuple of offset given as x,y,z for the region of interest. Defaults to (0, 0, 0).

  • roi_size (Sequence[int]) – Size of the region of interest as x,y,z.

  • args – Positional arguments to prepare_subroi().

  • kwargs – Named arguments to prepare_subroi().

Return type:

ndarray

Returns:

The region of interest without separate time dimension as a 3D (or 4-D array if channel dimension exists) array.

magmap.plot.plot_3d.prepare_subimg(image5d, offset, size, ndim_base=5, border=None)[source]

Extracts a subimage from a larger image.

Parameters:
  • image5d (ndarray) – 5D image array in the order, t, z, y, x, [c] unless ndim_base is given.

  • offset (Sequence[int]) – Tuple of offset given as z, y, x for the region of interest.

  • size (Sequence[int]) – Size of the region of interest as z, y, x.

  • ndim_base (int, default: 5) – Number of dimensions in image5d. Defaults to 5 for t, z, y, x, [c]`; if fewer, the t dimension is removed.

  • border (Union[int, Sequence[int], None], default: None) – Border size as either a scalar for all dimensions of values for z, y, x. The resulting subimage will be expanded to include this border outside offset, with the same extra size on opposite sides along each axis.

Return type:

ndarray

Returns:

The sub-imge without separate time dimension as a 3D (or 4-D array if channel dimension exists) array.

magmap.plot.plot_3d.remap_intensity(roi, channel=None, **kwargs)[source]

Remap intensities, currently using adaptive histogram equalization.

May allow plugging in alternative methods in the future.

Parameters:
  • roi (ndarray) – Region of interest as a 3D or 3D+channel array.

  • channel (Optional[Sequence[int]], default: None) – Channel index of roi to saturate. Defaults to None to use all channels. If a specific channel is given, all other channels remain unchanged.

  • kwargs – Additional arguments to skimage.exposure.equalize_adapthist(). Values can be given as sequences, where each element corresponds to channels in channel.

Return type:

ndarray

Returns:

Remapped region of interest as a new array.

magmap.plot.plot_3d.replace_vol(img, vol, center=None, offset=None, vol_as_mask=None)[source]

Replace a volume within an image, centering on the given coordinates and cropping the input volume to fit.

Parameters:
  • img (numpy.ndarray) – Image as a Numpy array into which vol will be placed. Updated in-place.

  • vol (numpy.ndarray) – Volume to place in img.

  • center (tuple[int, int, int]) – Coordinates of the center of volume, given as a sequence of z,y,x. Either center or offset must be given. Takes precedence over offset.

  • offset (tuple[int, int, int]) – Coordinates of offset within img to place vol, given as a sequence of z,y,x.

  • vol_as_mask (numpy.ndarray) – If vol should be taken as a mask, where only its True values will replace the corresponding pixels in img, assign this value to the mask locations. Defaults to None, in which case the entire vol will be assigned.

Returns:

img modified in-place.

Return type:

numpy.ndarray

Raises:

ValueError – if center and offset are both None.

magmap.plot.plot_3d.roi_center_to_offset(offset, shape, reverse=False)[source]

Convert an ROI offset given as the center of the ROI to the coordinates of the upper left hand corner of the ROI.

Parameters:
  • offset (Sequence[int]) – Offset taken as the center of the ROI in any order, typically either x,y,z or z,y,x.

  • shape (Sequence[int]) – ROI shape in the same order as that of offset.

  • reverse (bool, default: False) – True to treat offset as the upper left hand corner of the ROI and to obtain the center coordinates of this ROI; defaults to False.

Return type:

Sequence[int]

Returns:

Coordinates of the upper left corner of the ROI, or the center of the ROI if reverse is True.

magmap.plot.plot_3d.saturate_roi(roi, clip_vmin=-1, clip_vmax=-1, max_thresh_factor=-1, channel=None)[source]

Saturates an image, clipping extreme values and stretching remaining values to fit the full range.

Parameters:
  • roi (ndarray) – Region of interest.

  • clip_vmin (float, default: -1) – Percent for lower clipping. Defaults to -1 to use each channel’s profile setting.

  • clip_vmax (float, default: -1) – Percent for upper clipping. Defaults to -1 to use each channel’s profile setting.

  • max_thresh_factor (float, default: -1) – Multiplier of config.near_max for ROI’s scaled maximum value. If the max data range value adjusted through``clip_vmax``is below this product, this max value will be set to this product. Defaults to -1 to use each channel’s profile setting.

  • channel (Optional[Sequence[int]], default: None) – Sequence of channel indices in roi to saturate. Defaults to None to use all channels.

Return type:

ndarray

Returns:

Saturated region of interest.

magmap.plot.plot_3d.setup_channels(roi, channel, dim_channel)[source]

Setup channels array for the given ROI dimensions.

Parameters:
  • roi (ndarray) – Region of interest, a 3/4D array in the order, z, y, x, [c].

  • channel (Optional[Sequence[int]]) – Channels to select, which can be None to indicate all channels.

  • dim_channel (int) – Index of the channel dimension.

Returns:

  • multichannel: boolean value where True indicates that the ROI is multichannel (ie 4D)

  • channels: an array of the channel indices of roi to include, which is the same as channel for multichannel ROIs or only the first element if roi is single channel

Return type:

A tuple of

magmap.plot.plot_3d.show_surface_labels(segments, vis)[source]

Shows 3D surface segments from labels generated by segmentation methods such as Random-Walker.

Parameters:
  • segments – Labels from segmentation method.

  • vis – Visualization GUI.

magmap.plot.plot_3d.threshold(roi)[source]

Thresholds the ROI, with options for various techniques as well as post-thresholding morphological filtering.

Parameters:

roi – Region of interest, given as [z, y, x].

Returns:

The thresholded region.