magmap.cv.stack_detect module

Stack blob detector.

Detect blobs within a stack that has been chunked to allow parallel processing.

class magmap.cv.stack_detect.Blocks(sub_roi_slices: ndarray, sub_rois_offsets: ndarray, denoise_max_shape: ndarray, exclude_border: Sequence[int], tol: ndarray, overlap_base: ndarray, overlap: ndarray, overlap_padding: ndarray, max_pixels: ndarray)[source]

Bases: NamedTuple

Block processing chunk tuples.

denoise_max_shape: ndarray

Int array of max shape for each sub-block used for denoising.

exclude_border: Sequence[int]

List of ints for border pixels to exclude in z, y, x.

max_pixels: ndarray

Max pixels for each side in z, y, x order.

overlap: ndarray

Similar overlap array but modified by the border exclusion.

overlap_base: ndarray

Float array of overlapping pixels in z, y, x.

overlap_padding: ndarray

Similar overlap array but for padding beyond the overlap.

sub_roi_slices: ndarray

Object array of tuples containing slices of each block.

sub_rois_offsets: ndarray

Alias for field number 1

tol: ndarray

match tolerance as a Numpy float array in z, y, x.

class magmap.cv.stack_detect.StackDetector[source]

Bases: object

Detect blobs within a stack in a way that allows multiprocessing without global variables.

Variables:
  • img (np.ndarray) – Full image array.

  • last_coord (np.ndarray) – Indices of last sub-ROI given as coordinates in z,y,x.

  • denoise_max_shape (Tuple[int]) – Maximum shape of each unit within each sub-ROI for denoising.

  • exclude_border (bool) – Sequence of border pixels in x,y,z to exclude; defaults to None.

  • coloc (bool) – True to perform blob co-localizations; defaults to False.

  • channel (Sequence[int]) – Sequence of channels; defaults to None to detect in all channels.

channel = None
coloc = False
denoise_max_shape = None
classmethod detect_blobs_sub_rois(img, sub_roi_slices, sub_rois_offsets, denoise_max_shape, exclude_border, coloc, channel)[source]

Process blobs in chunked sub-ROIs via multiprocessing.

Parameters:
  • img (np.ndarray) – Array in which to detect blobs.

  • sub_roi_slices (np.ndarray) – Numpy object array containing chunked sub-ROIs within a stack.

  • sub_rois_offsets (np.ndarray) – Numpy object array of the same shape as sub_rois with offsets in z,y,x corresponding to each sub-ROI. Offets are used to transpose blobs into absolute coordinates.

  • denoise_max_shape (Tuple[int]) – Maximum shape of each unit within each sub-ROI for denoising.

  • exclude_border (Tuple[int]) – Sequence of border pixels in x,y,z to exclude; defaults to None.

  • coloc (bool) – True to perform blob co-localizations; defaults to False.

  • channel (Sequence[int]) – Sequence of channels, where None detects in all channels.

Returns:

Numpy object array of blobs corresponding to sub_rois, with each set of blobs given as a Numpy array in the format, [n, [z, row, column, radius, ...]], including additional elements as given in :meth:StackDetect.detect_sub_roi.

Return type:

np.ndarray

classmethod detect_sub_roi(coord, offset, last_coord, denoise_max_shape, exclude_border, sub_roi, channel, img_path=None, coloc=False)[source]

Perform 3D blob detection within a sub-ROI without accessing class attributes, such as for spawned multiprocessing.

Parameters:
  • coord (Sequence[int]) – Coordinate of the sub-ROI in the order z,y,x.

  • offset (Sequence[int]) – Offset of the sub-ROI within the full ROI, in z,y,x.

  • last_coord (Sequence[int]) – See attributes.

  • denoise_max_shape (Sequence[int]) – See attributes.

  • exclude_border (bool) – See attributes.

  • sub_roi (ndarray) – Array in which to perform detections.

  • img_path (Optional[str], default: None) – Path from which to load metadatat; defaults to None. If given, the command line arguments will be reloaded to set up the image and processing parameters.

  • coloc (bool, default: False) – True to perform blob co-localizations; defaults to False.

  • channel (Sequence[int]) – Sequence of channels, where None detects in all channels.

Returns:

  • coord: the coordinate given back again to identify the sub-ROI position

  • blobs: array of detected blobs

Return type:

Tuple of

classmethod detect_sub_roi_from_data(coord, sub_roi_slices, offset)[source]

Perform 3D blob detection within a sub-ROI using data stored as class attributes for forked multiprocessing.

Parameters:
  • coord (Tuple[int]) – Coordinate of the sub-ROI in the order z,y,x.

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

  • offset (Tuple[int]) – Offset of the sub-ROI within the full ROI, in z,y,x.

Returns:

The coordinate given back again to identify the sub-ROI position and an array of detected blobs.

Return type:

Tuple[int], np.ndarray

exclude_border = None
img = None
last_coord = None
class magmap.cv.stack_detect.StackPruner[source]

Bases: object

Prune blobs within a stack in a way that allows multiprocessing without global variables.

Variables:

blobs_to_prune – List of tuples to be passed to :meth:detector.remove_close_blobs. The final colums should have the coordinates of the sub-ROI in z, y, x order as given by chunking.merge_blobs().

blobs_to_prune = None
classmethod prune_blobs_mp(img, seg_rois, overlap, tol, sub_roi_slices, sub_rois_offsets, channels, overlap_padding=None)[source]

Prune close blobs within overlapping regions by checking within entire planes across the ROI in parallel with multiprocessing.

Parameters:
  • img (np.ndarray) – Array in which to detect blobs.

  • seg_rois (np.ndarray) – Blobs from each sub-region.

  • overlap – 1D array of size 3 with the number of overlapping pixels for each image axis.

  • tol – Tolerance as (z, y, x), within which a segment will be considered a duplicate of a segment in the master array and removed.

  • sub_roi_slices (np.ndarray) – Object array of ub-regions, used to check size.

  • sub_rois_offsets – Offsets of each sub-region.

  • channels (Sequence[int]) – Sequence of channels; defaults to None to detect in all channels.

  • overlap_padding (default: None) – Sequence of z,y,x for additional padding beyond overlap. Defaults to None to use tol as padding.

Returns:

All blobs as a Numpy array and a data frame of pruning stats, or None for both if no blobs are in the seg_rois.

Return type:

np.ndarray, pd.DataFrame

classmethod prune_overlap(i, pruner)[source]

Prune overlapping blobs.

Parameters:
  • i (int) – Index of :attr:blobs_to_prune.

  • pruner – Corresponding value from :attr:blobs_to_prune.

Returns:

Blobs after pruning and pruning ratio metrics.

Return type:

np.ndarray, tuple

classmethod prune_overlap_by_index(i)[source]

Prune an overlapping region.

Parameters:

i (int) – Index in :attr:blobs_to_prune.

Returns:

The results from prune_overlap().

class magmap.cv.stack_detect.StackTimes(value)[source]

Bases: Enum

Stack processing durations.

DETECTION = 'Detection'
PRUNING = 'Pruning'
TOTAL = 'Total_stack'
magmap.cv.stack_detect.detect_blobs_blocks(filename_base, image5d, offset=None, size=None, channels=None, verify=False, save_dfs=True, full_roi=False, coloc=False)[source]

Detect blobs by block processing of a large image.

All channels are processed in the same blocks.

Parameters:
  • filename_base (str) – Base path to use file output.

  • image5d (ndarray) – Large image to process as a Numpy array of t,z,y,x,[c]

  • offset (Optional[Sequence[int]], default: None) – Sub-image offset given as coordinates in z,y,x.

  • size (Optional[Sequence[int]], default: None) – Sub-image shape given in z,y,x.

  • channels (Optional[Sequence[int]], default: None) – Sequence of channels, where None detects in all channels.

  • verify (bool, default: False) – True to verify detections against truth database; defaults to False.

  • save_dfs (bool, default: True) – True to save data frames to file; defaults to True.

  • full_roi (bool, default: False) – True to treat image5d as the full ROI; defaults to False.

  • coloc (bool, default: False) – True to perform blob co-localizations; defaults to False.

Returns:

  • stats_detection: accuracy metrics from magmap.cv.detector.verify_rois

  • fdbk: feedback message from this same function

  • blobs: detected blobs

Return type:

Tuple of

magmap.cv.stack_detect.detect_blobs_stack(filename_base, subimg_offset=None, subimg_size=None, coloc=False)[source]

Detect blobs in a full stack, such as a whole large image.

Process channels in separate sets of blocks if their profiles specify different block sizes.

Parameters:
  • filename_base (str) – Base image filename, for saving files.

  • subimg_offset (Optional[Sequence[int]], default: None) – Sub-image offset as z,y,x to load from config.image5d; defaults to None.

  • subimg_size (Optional[Sequence[int]], default: None) – Sub-image size as z,y,x to load from config.image5d; defaults to None.

  • coloc (bool, default: False) – True to also detect blob-colocalizations based on image intensity; defaults to False. For match-based colocalizations, use the coloc_match task (magmap.colocalizer.StackColocalizer.colocalize_stack()) instead.

Returns:

  • stats_detection: combined accuracy metrics from magmap.cv.detector.verify_rois

  • fdbk: feedback message from this same function

  • blobs: detected blobs across all channels in magmap.settings.config.channel

Return type:

Tuple of

magmap.cv.stack_detect.setup_blocks(settings, shape)[source]

Set up blocks for block processing

Each block is a chunk of a larger image processed sequentially or in parallel to optimize resource usage.

Parameters:
  • settings (SettingsDict) – Settings dictionary that defines that the blocks.

  • shape (Sequence[int]) – Shape of full image in z, y, x.

Return type:

Blocks

Returns:

A named tuple of the block parameters.