Synapses and ROIs

Classes for describing ROIs, synapses and detection algorithms

class neurotorchmz.utils.synapse_detection.CircularSynapseROI

Implements a circular ROI of a given radius

CLASS_DESC = 'Circular ROI'

Every subclass defines a description string

get_coordinates(shape: tuple | None) tuple[array, array]

Return coordinates of points inside the ROI and inside the given shape. They are returned as a tuple with the first parameter beeing the y coordinates and the second the x coordinates.

Example output: ([Y0, Y1, Y2, …], [X0, X1, X2, …])

Returns tuple[np.array, np.array]:

The coordinates inside the ROI in the format [yy, xx]

radius: int | float | None

Radius of the ROI

set_radius(radius: int | float) Self

Set the radius of the ROI

exception neurotorchmz.utils.synapse_detection.DetectionError

Error thrown by calling detect() on a IDetectionAlgorithm object indicating something went wrong in the detection process.

class neurotorchmz.utils.synapse_detection.DetectionResult

Class to store the result of synapse detections

property synapses: list[ISynapse]

The current list of synapses in this tab. If you modify values inside this list, make sure to call tvSynapses.SyncSynapses() to reflect the changes in the TreeView.

property synapses_dict: dict[str, ISynapse]

The current list of synapses in this tab presented as dict with the UUID as key. If you modify values inside this list, make sure to call tvSynapses.SyncSynapses() to reflect the changes in the TreeView.

class neurotorchmz.utils.synapse_detection.HysteresisTh
detect(img: ndarray, lowerThreshold: int | float, upperThreshold: int | float, minArea: int | float) list[ISynapseROI]

Find ROIs in a given image. For details see the documentation

Parameters:
  • img (np.ndarray) – The image as 2D numpy array

  • lowerThreshold (int|float) – lower threshold for detection

  • upperThreshold (int|float) – upper threshold for detection

  • minArea (int|float) – Consider only ROIs with a pixel area greater than the given value

reset()

Abstract funtion which must be overwritten by a subclass to reset internal variables and states

class neurotorchmz.utils.synapse_detection.IDetectionAlgorithm

Abstract base class for a detection algorithm implementation

detect(img: ndarray, **kwargs) list[ISynapseROI]

Given an input image as 2D np.ndarray and algorithm dependend arbitary arguments, return a list of ISynapseROI.

Parameters:

img (np.ndarray) – The input image as 2D array

reset()

Abstract funtion which must be overwritten by a subclass to reset internal variables and states

class neurotorchmz.utils.synapse_detection.ISynapse

This abstract class defines the concept of a synapse. Currently there are two types of synapses: Singleframe and Multiframe.

get_roi_description() str

Abstract function for displaying information about the rois. Needs to be implemented by each subclass

property location: tuple | None

Get the location of the synapse

property location_string: str

Returns the location of the synapse in the format ‘X, Y’ or ‘’ if the location is not set

name: str | None

Name of the synapse

property rois: list[ISynapseROI]

Returns the list of ROIS in this synapse

property rois_dict: dict[str, ISynapseROI]

Returns the rois as dictionary with their UUID as key

set_name(name: str | None) Self

Set the name of the synapse

staged

A synapse can be staged meaning it will not be replaced when rerunning the detection

uuid

Unique id of the synapse

class neurotorchmz.utils.synapse_detection.ISynapseROI

This abstract class defines a synapse ROI describing a specific shape in an image or image frame

Convention: The order of coordiantes is Y, X to be compatible with the shape of the image (t, row, col). But for any kind of displaying convert them to X, Y to not cofuse the user

CLASS_DESC = 'ISynapseROI'

Every subclass defines a description string

frame: int | None

The associate frame for this ROI or None if the object just defines a shape

get_coordinates(shape: tuple | None) tuple[array, array]

Return coordinates of points inside the ROI and inside the given shape. They are returned as a tuple with the first parameter beeing the y coordinates and the second the x coordinates.

Example output: ([Y0, Y1, Y2, …], [X0, X1, X2, …])

Returns tuple[np.array, np.array]:

The coordinates inside the ROI in the format [yy, xx]

get_distance(roi2: Self) float

Returns the distance between the locations of the ROIs or np.inf if at least one has no location

get_signal_from_image(img: ndarray) ndarray

Given an 3D ImageObject (t, y, x), flatten x and y to the pixels given by get_coordinates providing a shape (t, num_image_mask_pixel)

location: tuple | None

(Estimated or actual) center of the synapse; used for selecting it in the plot and ordering

property location_string: str

Returns the location of the synapse in the format ‘X, Y’ or ‘’ if the location is not set

region_props: RegionProperties | None

Stores skimage RegionProperties for the ROI

serialize() dict

Serialize the object

set_frame(frame: int | None) Self

Set the frame of the synapse or removes it by providing None

set_location(location: tuple[int | float, int | float] = None, y: int | float = None, x: int | float = None) Self

Set the location of the synapse by either providing a tuple or Y and X explicitly

Parameters:
  • location (tuple[int|float, int|float]) – Location tuple (Y, X)

  • y (int|float)

  • x (int|float)

set_region_props(region_props: RegionProperties | None) Self

Set skimage.RegionProperties for this synapse

uuid

A unique UUID to identify the synapse

class neurotorchmz.utils.synapse_detection.LocalMax

Implementation of the LocalMax algorithm for ROI detection. For details see the documentation

detect(img: ndarray, lowerThreshold: int | float, upperThreshold: int | float, expandSize: int, minArea: int, minDistance: int, radius: int | float | None) list[ISynapseROI]

Detect ROIs in the given 2D image. For details see the documentation

Find ROIs in a given image. For details see the documentation

Parameters:
  • img (np.ndarray) – The image as 2D numpy array

  • lowerThreshold (int|float) – The lower threshold

  • upperThreshold (int|float) – The upper threshold

  • expandSize (int) – Pixel to expand the peak search into

  • minArea (int) – Minimum area of a ROI

  • minDistance (int) – Mimum distance between two ROIs

  • radius (int|float|None) – Returns circular ROIs if radius >= 0 and polygonal ROIs if radius is None. Raises exception otherwise

reset()

Abstract funtion which must be overwritten by a subclass to reset internal variables and states

class neurotorchmz.utils.synapse_detection.MultiframeSynapse

Implements a synapse which can hold multiple rois (one for each frame)

add_roi(roi: ISynapseROI) Self

Add a ROI to the synapse

add_rois(rois: list[ISynapseROI]) Self

Add a range of rois to the synapse

clear_rois() Self

Remove all rois

get_roi_description() str

In the future return information about the rois. Currently, only the text ‘Multiframe Synapse’ is returned

property location: tuple | None

Get the location of the synapse

remove_roi(roi: ISynapseROI) Self

Remove a roi

set_location(location: tuple[int | float, int | float] = None, y: int | float = None, x: int | float = None) Self

Set the location of the synapse by either providing a tuple or Y and X explicitly which is for example used for sorting them. There is no need to provide an exact center

Parameters:
  • location (tuple[int|float, int|float]) – Location tuple (Y, X)

  • y (int|float)

  • x (int|float)

set_rois(rois: list[ISynapseROI]) Self

Remove all rois and append all rois in the given list

class neurotorchmz.utils.synapse_detection.PolygonalSynapseROI

Implements a polygonal synapse ROI

CLASS_DESC = 'Polyonal ROI'

Every subclass defines a description string

coords

List of points inside the polygon in the format [(Y, X), (Y, X), ..]

get_coordinates(shape: tuple | None) tuple[array, array]

Return coordinates of points inside the ROI and inside the given shape. They are returned as a tuple with the first parameter beeing the y coordinates and the second the x coordinates.

Example output: ([Y0, Y1, Y2, …], [X0, X1, X2, …])

Returns tuple[np.array, np.array]:

The coordinates inside the ROI in the format [yy, xx]

polygon: list[tuple[int, int]]

List of polygon points in the format [(Y, X), (Y, X), ..]

set_polygon(polygon: list[tuple[int, int]], coords: list[tuple[int, int]], region_props: RegionProperties)

Set the polygon by providing the coordinate tuples and either a) the pixel coords or b) a RegionProperties object (from which the coords are derived)

Parameters:
  • polygon (list[tuple[int, int]]) – The contour of the polygon in the format [(Y, X), (Y, X), ..]

  • coords (list[tuple[int, int]]) – The pixel coordinates of the polygon in the format [(Y, X), (Y, X), ..]. Either it or a RegionProperties object must be given

  • region_props (RegionPropertiers) – A region_props object. Either it or the coords must be given

class neurotorchmz.utils.synapse_detection.SimpleCustering
class neurotorchmz.utils.synapse_detection.SingleframeSynapse(roi: ISynapseROI = None)

Implements a synapse class which can hold exactly one ROI

get_roi_description() str

Displays information about the roi by calling str(roi)

property location: tuple | None

Get the location of the synapse accessed from the ROI

set_roi(roi: ISynapseROI | None = None) Self

Set the ROI or remove it by passing None or no argument

class neurotorchmz.utils.synapse_detection.SynapseClusteringAlgorithm

A synapse clustering algorithm merges a list of ROIs detected from a defined list of frames to a new list of synapses.

class neurotorchmz.utils.synapse_detection.Thresholding

Implementation of the thresholding detection algorithm. For details see the documentation

detect(img: ndarray, threshold: int | float, radius: int | float | None, minArea: int | float) list[ISynapseROI]

Find ROIs in a given image. For details see the documentation

Parameters:
  • img (np.ndarray) – The image as 2D numpy array

  • threshold (int|float) – Detection is performed on a thresholded image

  • radius (int|float|None) – Returns circular ROIs if radius >= 0 and polygonal ROIs if radius is None. Raises exception otherwise

  • minArea (int|float) – Consider only ROIs with a pixel area greater than the provided value

reset()

Abstract funtion which must be overwritten by a subclass to reset internal variables and states