magmap.cv.colocalizer module

Colocalize objects in an image, typically in separate channels.

class magmap.cv.colocalizer.BlobMatch(matches=None, match_id=None, roi_id=None, blob1_id=None, blob2_id=None, df=None)[source]

Bases: object

Blob match storage class as a wrapper for a data frame of matches.

Variables:
  • df – Data frame of matches with column names given by BlobMatch.Cols; defaults to None.

  • coords – Blob match coordinates, typically the mean coordinates of each blob pair; defaults to None.

  • cmap – Colormap for each match; defaults to None.

class Cols(value)[source]

Bases: Enum

Blob match column names.

BLOB1 = 'Blob1'
BLOB1_ID = 'Blob1ID'
BLOB2 = 'Blob2'
BLOB2_ID = 'Blob2ID'
DIST = 'Distance'
MATCH_ID = 'MatchID'
ROI_ID = 'RoiID'
__repr__()[source]

Format the underlying data frame.

get_blobs(n)[source]

Get blobs as a numpy array.

Parameters:

n (int) – 1 for blob1, otherwise blob 2.

Return type:

Optional[ndarray]

Returns:

Numpy array of the given blob type, or None if the df is None or the blob column does not exist.

get_blobs_all()[source]

Get all blobs in the blob matches.

Return type:

Optional[List[ndarray]]

Returns:

Tuple of (blobs1, blobs2), or None if either are None.

get_mean_coords()[source]

Get mean value of each pair of matched blobs.

Returns:

[n, 3] array of n blob pairs, also set to coords.

update_blobs(fn, *args)[source]

Update all blobs with the given function.

Parameters:
  • fn (func) – Function that accepts the output of get_blobs() separately for each set of blobs.

  • *args (Any) – Additional arguments to fn.

class magmap.cv.colocalizer.StackColocalizer[source]

Bases: object

Colocalize blobs in blocks based on matching blobs across channels.

Support shared memory for spawned multiprocessing, with fallback to pickling in forked multiprocessing.

blobs: Optional[Blobs] = None
channels: Optional[Sequence[int]] = None

Channels to match; defaults to None.

classmethod colocalize_block(coord, offset, shape, blobs=None, tol=None, setup_cli=False, channels=None)[source]

Colocalize blobs from different channels within a block.

Parameters:
  • coord (Sequence[int]) – Block coordinate.

  • offset (Sequence[int]) – Block offset within the full image in z,y,x.

  • shape (Sequence[int]) – Block shape in z,y,x.

  • blobs (Optional[Blobs], default: None) – Blobs; defaults to None, which will use blobs.

  • tol (Optional[Sequence[float]], default: None) – Tolerance for colocalizing blobs; defaults to None to use match_tol.

  • setup_cli (bool, default: False) – True to set up CLI arguments, typically for a spawned (rather than forked) environment; defaults to False.

  • channels (Optional[Sequence[int]], default: None) – Channels to match; defaults to None, where channels will be used.

Returns:

  • coord: coord, for tracking multiprocessing

  • matches: dictionary of matches

Return type:

Tuple of

classmethod colocalize_stack(shape, blobs, channels=None)[source]

Entry point to colocalizing blobs within a stack.

Parameters:
Return type:

Dict[Tuple[int, int], BlobMatch]

Returns:

Dictionary of matches, where keys are tuples of the channel pairs, and values are blob match objects.

match_tol: Optional[Sequence[float]] = None
magmap.cv.colocalizer.colocalize_blobs(roi, blobs, thresh=None)[source]

Co-localize blobs from different channels based on surrounding intensities.

Thresholds for detection are first identified in each channel by taking the blobs in the given channel, finding the surrounding intensities, and taking a low (5th) percentile. Then for each channel, the surrounding intensities of blobs in that channel are compared with the thresholds in the other channels. Blobs exceeding any given threshold are considered to co-localize in that channel.

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

  • blobs (np.ndarray) – Blobs as a 2D array in the format [[z, y, x, radius, confirmation, truth, channel...], ...].

  • thresh (int, float, str) – Threshold percentile of intensities from pixels surrounding each blob in the given channel. Use “min” to instead take the mininimum average intensity of all blobs in the channel. Defaults to None to use “min”.

Returns:

2D Numpy array of same length as blobs with a column for each channel where 1 indicates that the corresponding blob has signal is present in the given channels at the blob’s location, and 0 indicates insufficient signal.

Return type:

np.ndarray

magmap.cv.colocalizer.colocalize_blobs_match(blobs, offset, size, tol, inner_padding=None, channels=None)[source]

Co-localize blobs in separate channels but the same ROI by finding optimal blob matches.

Parameters:
  • blobs (Blobs) – Blobs.

  • offset (Sequence[int]) – ROI offset given as x,y,z.

  • size (Sequence[int]) – ROI shape given as x,y,z.

  • tol (Sequence[float]) – Tolerances for matching given as x,y,z

  • inner_padding (Optional[Sequence[int]], default: None) – ROI padding given as x,y,z; defaults to None to use the padding based on tol.

  • channels (Optional[Sequence[int]], default: None) – Indices of channels to colocalize. Defaults to None, which will use all channels in blobs.

Return type:

Optional[Dict[Tuple[int, int], BlobMatch]]

Returns:

Dictionary where keys are tuples of the two channels compared and values are blob matches objects, or None if blobs is None.

magmap.cv.colocalizer.insert_matches(db, matches)[source]

Insert matches into database for a whole image.

Parameters:
  • db (sqlite.ClrDB) – Database object.

  • matches (dict[tuple[int, int], BlobMatch) – Dictionary of channel combo tuples to blob match objects.

magmap.cv.colocalizer.select_matches(db, channels, offset=None, shape=None, exp_name=None)[source]

Select blob matches for the given region from a database.

Blob matches are assumed to have been processed from the whole image. To retrieve matches from a selected ROI, use magmap.io.sqlite.ClrDB.select_blob_matches() instead.

Parameters:
  • db (sqlite.ClrDB) – Database object.

  • channels (list[int]) – List of channels.

  • offset (list[int]) – ROI offset in z,y,x; defaults to None to use (0, 0, 0).

  • shape (list[int]) – ROI shape in z,y,x; defaults to None to use (0, 0, 0).

  • exp_name (str) – Name of experiment in db.

Returns:

Dictionary where keys are tuples of the two channels compared and values are a list of blob matches. None if no blob matches are found.

Return type:

dict[tuple[int, int], list[BlobMatch]