nanshe.imp.segment module

The segment module has components to make an image processing workflow.

Overview

The segment (or segmentation) module contains algorithms useful in implementing ADINA ( doi:10.1109/ISBI.2013.6556660 ), but could be reused in other workflows. These primary sections can be grouped as follows

  1. Preprocessing steps
    1. registration correction ( remove_zeroed_lines() )
    2. background estimation and subtraction ( estimate_f0(), extract_f0() )
    3. wavelet transform ( transform() )
    4. normalization ( normalize_data() )
  2. Dictionary learning step ( generate_dictionary() )
  3. Postprocessing
    1. segmentation and refinement ( wavelet_denoising() )
    2. merging ( merge_neuron_sets() )

API

class nanshe.imp.segment.ExtendedRegionProps(**kwargs)[source]

Bases: object

Using the results of extended_region_local_maxima_properties with given intensity and label images, the ExtendedRegionProps attempts to make it easy to get information about local maxima in the image. Further, it provides methods to allow removal of local maxima. When removing local maxima, it will updated the given label image to reflect labels that have been removed completely and will automatically renumber the label image (as some functions expect a label image with sequential numbering, no missing label number). This provides a simple way to maintain a label image, whose local maxima one is more interested in directly manipulating.

Note

Adding local maxima is not allowed for and wouldn’t make sense given its dependency on the intensity image.

get_local_max_index_array(**kwargs)[source]

Gets a numpy style index array from the local maxima present.

Returns:
a numpy styled index array, which can be used on
the intensity image, or label image.
Return type:tuple
get_local_max_label_image(**kwargs)[source]

Gets an int numpy array that contains the labels of the local maxima.

Returns:
an int numpy array that contains the labels
of the local maxima and is zero in the background.
Return type:numpy.ndarray
get_local_max_mask(**kwargs)[source]

Gets a bool numpy array that is True at the locations of the local maxima.

Returns:
a bool numpy array that is True where a
local max is and False everywhere else.
Return type:numpy.ndarray
recorders = <nanshe.io.hdf5.record.ClassSubgroupingRecorders object>
remove_prop_indices(**kwargs)[source]

Removes the local maxima of the indices listed (like remove_prop_mask except with indices).

Note

If a label is removed completely, the label image, intensity image, and image mask will be updated. Also, the count (of local maxima per label) will be updated, regardless.

Parameters:*i (int) – A mask of the indices to drop (True for the ones to remove, and False for the ones to keep).
remove_prop_mask(**kwargs)[source]

Removes the local maxima by the mask over their indices (only False elements will be kept).

Note

If a label is removed completely, the label image, intensity image, and image mask will be updated. Also, the count (of local maxima per label) will be updated, regardless.

Parameters:remove_prop_indices_mask (numpy.ndarray) – A mask of the indices to drop (True for the ones to remove, and False for the ones to keep).
renumber_labels(**kwargs)[source]

An internal method to be called after a label is completely removed from the local maxima.

Ensures all labels are in sequential order in the label image, count, and that all local maxima have their labels updated.

Note

Calling this externally will not make a difference as long as removal has occurred through the usual mechanisms.

nanshe.imp.segment.estimate_f0(*args, **kwargs)[source]

Estimates F_0 using a rank order filter with some smoothing.

Parameters:
  • new_data (numpy.ndarray) – array of data for finding baseline ( first axis is time).
  • half_window_size (int) – the rank filter window size is 2*half_window_size+1.
  • which_quantile (float) – which quantile to return from the rank filter.
  • temporal_smoothing_gaussian_filter_stdev (float) – stdev for gaussian filter to convolve over time.
  • temporal_smoothing_gaussian_filter_window_size (float) – window for gaussian filter to convolve over time. (Measured in standard deviations)
  • spatial_smoothing_gaussian_filter_stdev (float) – stdev for gaussian filter to convolve over space.
  • spatial_smoothing_gaussian_filter_window_size (float) – window for gaussian filter to convolve over space. (Measured in standard deviations)
  • out (numpy.ndarray) – where the final result will be stored.
  • **parameters (dict) – essentially unused ( catches unneeded arguments).
Returns:

the F_0

estimate.

Return type:

numpy.ndarray

nanshe.imp.segment.expand_rois(*args, **kwargs)[source]
nanshe.imp.segment.extended_region_local_maxima_properties(*args, **kwargs)[source]

Generates local maxima along with other properties for each labeled region (therefore at least one entry per label). Gets a label image if not provided by using the threshold (if not provided is zero).

Note

This is similar to region_properties. It differs in that it keeps track of all local maxima. So, it is no longer one-to-one in labels, but is one-to-one in local maxima.

Parameters:
  • new_intensity_image (numpy.ndarray) – The intensity image to find local maxima for (ideally, all axes are spatial; however, it will treat all axes the same).
  • new_label_image (numpy.ndarray) – The label image to find local maxima for (ideally, all axes are spatial; however, it will treat all axes the same).
Returns:

A mask of the local maxima.

Return type:

numpy.ndarray

nanshe.imp.segment.extract_f0(*args, **kwargs)[source]

Attempts to find an estimate for dF/F.

Parameters:
  • new_data (numpy.ndarray) – array of data for finding baseline ( first axis is time).
  • half_window_size (int) – the rank filter window size is 2*half_window_size+1.
  • which_quantile (float) – which quantile to return from the rank filter.
  • temporal_smoothing_gaussian_filter_stdev (float) – stdev for gaussian filter to convolve over time.
  • temporal_smoothing_gaussian_filter_window_size (float) – window for gaussian filter to convolve over time. (Measured in standard deviations)
  • spatial_smoothing_gaussian_filter_stdev (float) – stdev for gaussian filter to convolve over space.
  • spatial_smoothing_gaussian_filter_window_size (float) – window for gaussian filter to convolve over space. (Measured in standard deviations)
  • bias (float) – value to be added to dataset to avoid nan.
  • out (numpy.ndarray) – where the final result will be stored.
  • return_f0 (bool) – whether to return F_0 also, F_0 will be returned first.
  • **parameters (dict) – essentially unused ( catches unneeded arguments).
Returns:

dF/F or if

return_f0 is True a tuple (F_0, dF/F).

Return type:

(tuple of numpy.ndarray)

nanshe.imp.segment.extract_neurons(*args, **kwargs)[source]

Extracts neurons from an image using a stack of masks.

Parameters:
  • new_image (numpy.ndarray) – spatial coordinates only (no time).
  • neuron_masks (numpy.ndarray) – first index of denotes which mask and all others are spatial indices.
Returns:

a stack of neurons in the same

order as the masks.

Return type:

numpy.ndarray

nanshe.imp.segment.fuse_neurons(*args, **kwargs)[source]

Merges the two neurons into one neuron, which is returned.

Note

The first neuron (neuron_1) is preferred in tie-breaking situations.

Parameters:
  • neuron_1 (numpy.ndarray) – numpy structured array (dtype get_neuron_dtype) containing the first neuron.
  • neuron_2 (numpy.ndarray) – numpy structured array (dtype get_neuron_dtype) containing the second neuron.
  • **parameters (dict) – dictionary of parameters
Returns:

a new neuron that is the result

of fusing the two.

Return type:

numpy.ndarray

nanshe.imp.segment.generate_dictionary(*args, **kwargs)[source]

Generates a dictionary using the data and parameters given for trainDL.

Parameters:
  • new_data (numpy.ndarray) – array of data for generating a dictionary (first axis is time).
  • initial_dictionary (numpy.ndarray) – dictionary to start the algorithm with.
  • n_components (int) – number of components for the dictionary to use.
  • **parameters (dict) – passed directly to spams.trainDL.
Returns:

the dictionary found.

Return type:

dict

nanshe.imp.segment.generate_local_maxima(*args, **kwargs)[source]

Creates a mask the same size as the intensity image with local maxima as True and background False.

Parameters:new_intensity_image (numpy.ndarray) – The image to find local maxima for (ideally, all axes are spatial; however, it will treat all axes the same).
Returns:A mask of the local maxima.
Return type:numpy.ndarray
nanshe.imp.segment.generate_local_maxima_scikit_image(*args, **kwargs)[source]

Creates a mask the same size as the intensity image with local maxima as True and background False. Uses scikit image’s skimage.feature.peak_local_max.

Parameters:
  • new_intensity_image (numpy.ndarray) – The image to find local maxima for (ideally, all axes are spatial; however, it will treat all axes the same).
  • local_max_neighborhood_size (int) – Size of the neighborhood to check for a local maxima.
Returns:

A mask of the local maxima.

Return type:

numpy.ndarray

nanshe.imp.segment.generate_local_maxima_vigra(*args, **kwargs)[source]

Creates a mask the same size as the intensity image with local maxima as True and background False. Uses vigra’s vigra.analysis.extendedLocalMaxima for 2D and vigra’s vigra.analysis.extendedLocalMaxima3D for 3D.

Parameters:new_intensity_image (numpy.ndarray) – The image to find local maxima for (ideally, all axes are spatial; however, it will treat all axes the same).
Returns:A mask of the local maxima.
Return type:numpy.ndarray
nanshe.imp.segment.get_empty_neuron(*args, **kwargs)[source]

Gets a numpy structured array using the type from get_neuron_dtype that has no contents.

Parameters:new_image (numpy.ndarray) – an empty numpy structured array with all types needed for a neuron.
Returns:
a numpy structured array with no
contents and type from get_neuron_dtype.
Return type:numpy.ndarray

Examples

>>> get_empty_neuron(
...     (3,), numpy.float64
... ) #doctest: +NORMALIZE_WHITESPACE
array([], dtype=[('mask', '?', (3,)),
                 ('contour', '?', (3,)),
                 ('image', '<f8', (3,)),
                 ('area', '<f8'),
                 ('max_F', '<f8'),
                 ('gaussian_mean', '<f8', (1,)),
                 ('gaussian_cov', '<f8', (1, 1)),
                 ('centroid', '<f8', (1,))])
>>> get_empty_neuron(
...     (2, 3), numpy.float64
... ) #doctest: +NORMALIZE_WHITESPACE
array([], dtype=[('mask', '?', (2, 3)),
                 ('contour', '?', (2, 3)),
                 ('image', '<f8', (2, 3)),
                 ('area', '<f8'),
                 ('max_F', '<f8'),
                 ('gaussian_mean', '<f8', (2,)),
                 ('gaussian_cov', '<f8', (2, 2)),
                 ('centroid', '<f8', (2,))])
nanshe.imp.segment.get_neuron_dtype(*args, **kwargs)[source]

Gets the type based on properties of an image.

Parameters:new_image (numpy.ndarray) – a single sample image (only spatial dimension).
Returns:
a list that can be converted to a
numpy.dtype using numpy.ndtype’s constructor.
Return type:list

Examples

>>> get_neuron_dtype(
...     (3,), numpy.float64
... ) #doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
[('mask', <... 'numpy.bool_'>, (3,)),
 ('contour', <... 'numpy.bool_'>, (3,)),
 ('image', <... 'numpy.float64'>, (3,)),
 ('area', <... 'numpy.float64'>),
 ('max_F', <... 'numpy.float64'>),
 ('gaussian_mean', <... 'numpy.float64'>, (1,)),
 ('gaussian_cov', <... 'numpy.float64'>, (1, 1)),
 ('centroid', <... 'numpy.float64'>, (1,))]
>>> get_neuron_dtype(
...     (2, 3), numpy.float64
... ) #doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
[('mask', <... 'numpy.bool_'>, (2, 3)),
 ('contour', <... 'numpy.bool_'>, (2, 3)),
 ('image', <... 'numpy.float64'>, (2, 3)),
 ('area', <... 'numpy.float64'>),
 ('max_F', <... 'numpy.float64'>),
 ('gaussian_mean', <... 'numpy.float64'>, (2,)),
 ('gaussian_cov', <... 'numpy.float64'>, (2, 2)),
 ('centroid', <... 'numpy.float64'>, (2,))]
nanshe.imp.segment.get_one_neuron(*args, **kwargs)[source]

Gets a numpy structured array using the type from get_neuron_dtype that has one neuron with all zeros.

Parameters:new_image (numpy.ndarray) – an empty numpy structured array with all types needed for a neuron.
Returns:
a numpy structured array with one
neuron using type from get_neuron_dtype.
Return type:numpy.ndarray

Examples

>>> numpy.set_printoptions(
...     formatter={"float": lambda x: str(float(x))}
... )
>>> get_one_neuron(
...     (3,), numpy.float64
... ) #doctest: +NORMALIZE_WHITESPACE
array([ ([False, False, False],
         [False, False, False],
         [0.0, 0.0, 0.0],
         0.0,
         0.0,
         [0.0],
         [[0.0]],
         [0.0])],
       dtype=[('mask', '?', (3,)),
              ('contour', '?', (3,)),
              ('image', '<f8', (3,)),
              ('area', '<f8'),
              ('max_F', '<f8'),
              ('gaussian_mean', '<f8', (1,)),
              ('gaussian_cov', '<f8', (1, 1)),
              ('centroid', '<f8', (1,))])
>>> get_one_neuron(
...     (2, 3), numpy.float64
... ) #doctest: +NORMALIZE_WHITESPACE
array([ ([[False, False, False],
          [False, False, False]],
         [[False, False, False],
          [False, False, False]],
         [[0.0, 0.0, 0.0],
          [0.0, 0.0, 0.0]],
         0.0,
         0.0,
         [0.0, 0.0],
        [[0.0, 0.0],
         [0.0, 0.0]],
        [0.0, 0.0])],
       dtype=[('mask', '?', (2, 3)),
              ('contour', '?', (2, 3)),
              ('image', '<f8', (2, 3)),
              ('area', '<f8'),
              ('max_F', '<f8'),
              ('gaussian_mean', '<f8', (2,)),
              ('gaussian_cov', '<f8', (2, 2)),
              ('centroid', '<f8', (2,))])
>>> numpy.set_printoptions()
nanshe.imp.segment.match_regions_properties(*args, **kwargs)[source]

Returns a mask selecting the regions with matching properties.

Parameters:
  • props (numpy.ndarray) – properties of each labeled region.
  • constraints (dict) – dictionary containing each property with optional min and max for each value.
Returns:

mask with each region property that

matches marked as True and the rest marked as False.

Return type:

numpy.ndarray

nanshe.imp.segment.merge_neuron_sets(*args, **kwargs)[source]
nanshe.imp.segment.merge_neuron_sets_once(*args, **kwargs)[source]

Merges the two sets of neurons into one. Appends neurons that cannot be merged with the existing set.

In the functions that follow, it helps to think of each neuron as a vector (instead of as an image). So, dot products for vectors are important.

Note

The first neuron set (new_neuron_set_1) is preferred and treated as the set to merge into. However, this function will not change the original argument.

Parameters:
  • new_neuron_set_1 (numpy.ndarray) – numpy structured array (dtype get_neuron_dtype) containing the first neuron set (preferred for tie breaking).
  • new_neuron_set_2 (numpy.ndarray) – numpy structured array (dtype get_neuron_dtype) containing the second neuron set.
  • alignment_min_threshold (float) – The minimum required cosine of the angle between two neurons for them to be treated as candidates for merging (uses the function expanded_numpy.dot_product_normalized with ord = 2).
  • overlap_min_threshold (numpy.ndarray) – The minimum required dot product (divided by the L1 norm of one of the neurons) for them to be treated as candidates for merging (uses the function expanded_numpy.dot_product_partially_normalized).
  • **parameters (dict) – dictionary of parameters
Returns:

a numpy structured

array that contains the result of merging the two sets (or appending for neurons that could not be merged).

Return type:

numpy.ndarray

nanshe.imp.segment.merge_neuron_sets_repeatedly(*args, **kwargs)[source]

Merges the two sets of neurons into one. Appends neurons that cannot be merged with the existing set.

In the functions that follow, it helps to think of each neuron as a vector (instead of as an image). So, dot products for vectors are important.

Note

The first neuron set (new_neuron_set_1) is preferred and treated as the set to merge into. However, this function will not change the original argument.

Parameters:
  • new_neuron_set_1 (numpy.ndarray) – numpy structured array (dtype get_neuron_dtype) containing the first neuron set (preferred for tie breaking).
  • new_neuron_set_2 (numpy.ndarray) – numpy structured array (dtype get_neuron_dtype) containing the second neuron set.
  • alignment_min_threshold (float) – The minimum required cosine of the angle between two neurons for them to be treated as candidates for merging (uses the function expanded_numpy.dot_product_normalized with ord = 2).
  • overlap_min_threshold (numpy.ndarray) – The minimum required dot product (divided by the L1 norm of one of the neurons) for them to be treated as candidates for merging (uses the function expanded_numpy.dot_product_partially_normalized).
  • **parameters (dict) – dictionary of parameters
Returns:

a numpy structured

array that contains the result of merging the two sets (or appending for neurons that could not be merged).

Return type:

numpy.ndarray

nanshe.imp.segment.normalize_data(*args, **kwargs)[source]

Removes the mean from each image and normalizes each image as if they were vectors.

Parameters:
  • new_data (numpy.ndarray) – array of data for normalization (first axis is time).
  • **parameters (dict) – contains arguments for renormalized_images.
Returns:

data that has been

normalized.

Return type:

numpy.ndarray

Examples

>>> a = numpy.zeros((2,2,2,))
>>> a[1,1,1] = 1
>>> a[0,0,0] = 1
>>> normalize_data(a)
array([[[ 0.8660254 , -0.28867513],
        [-0.28867513, -0.28867513]],
<BLANKLINE>
       [[-0.28867513, -0.28867513],
        [-0.28867513,  0.8660254 ]]])
>>> normalize_data(a, **{"renormalized_images" : { "ord" : 2 }})
array([[[ 0.8660254 , -0.28867513],
        [-0.28867513, -0.28867513]],
<BLANKLINE>
       [[-0.28867513, -0.28867513],
        [-0.28867513,  0.8660254 ]]])
>>> b = numpy.zeros_like(a)
>>> normalize_data(
...     a, out=b, **{"renormalized_images" : { "ord" : 2 }}
... )
array([[[ 0.8660254 , -0.28867513],
        [-0.28867513, -0.28867513]],
<BLANKLINE>
       [[-0.28867513, -0.28867513],
        [-0.28867513,  0.8660254 ]]])
>>> b
array([[[ 0.8660254 , -0.28867513],
        [-0.28867513, -0.28867513]],
<BLANKLINE>
       [[-0.28867513, -0.28867513],
        [-0.28867513,  0.8660254 ]]])
>>> normalize_data(
...     a, out=a, **{"renormalized_images" : { "ord" : 2 }}
... )
array([[[ 0.8660254 , -0.28867513],
        [-0.28867513, -0.28867513]],
<BLANKLINE>
       [[-0.28867513, -0.28867513],
        [-0.28867513,  0.8660254 ]]])
>>> a
array([[[ 0.8660254 , -0.28867513],
        [-0.28867513, -0.28867513]],
<BLANKLINE>
       [[-0.28867513, -0.28867513],
        [-0.28867513,  0.8660254 ]]])
nanshe.imp.segment.postprocess_data(*args, **kwargs)[source]

Generates neurons from the dictionary.

Parameters:
  • new_dictionary (numpy.ndarray) – dictionary of basis images to analyze for neurons.
  • **parameters (dict) – dictionary of parameters
Returns:

structured array with

relevant information for each neuron found.

Return type:

numpy.ndarray

nanshe.imp.segment.preprocess_data(*args, **kwargs)[source]

Performs all preprocessing steps that are specified.

(e.g. remove_zeroed_lines, bias, extract_f0, and wavelet.transform).

Parameters:
  • new_data (numpy.ndarray) – array of data for generating a dictionary (first axis is time).
  • out (numpy.ndarray) – where the final result will be stored.
  • **parameters (dict) – additional parameters for each step of preprocessing.
Returns:

the dictionary found.

Return type:

dict

nanshe.imp.segment.region_properties(*args, **kwargs)[source]

Grabs region properties from a label image.

Parameters:
  • new_label_image (numpy.ndarray) – label image used for generating properties.
  • args (list) – additional position arguments to pass skimage.measure.regionprops.
  • **parameters (dict) – additional keyword arguments to pass skimage.measure.regionprops.

Note

Uses all the same options in skimage.measure.regionprops. If a property is not specified, then it won’t be returned.

Returns:
a structured array of all the
properties found for each label.
Return type:numpy.ndarray

Examples

>>> numpy.set_printoptions(
...     formatter={"float": lambda x: str(float(x))}
... )
>>> region_properties(
...     numpy.zeros((2,2), dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties(
...     numpy.ones((2,2), dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 4.0, [0.5, 0.5])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties(
...     numpy.ones((3,3), dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 9.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties(
...     numpy.eye(3, dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 3.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties(
...     numpy.eye(3, dtype=int), ["area", "centroid"]
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 3.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties(
...     numpy.eye(3, dtype=int), properties=["area", "centroid"]
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 3.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> numpy.set_printoptions()
nanshe.imp.segment.region_properties_scikit_image(*args, **kwargs)[source]

Grabs region properties from a label image.

Parameters:
  • new_label_image (numpy.ndarray) – label image used for generating properties.
  • args (list) – additional position arguments to pass skimage.measure.regionprops.
  • **parameters (dict) – additional keyword arguments to pass skimage.measure.regionprops.

Note

Uses all the same options in skimage.measure.regionprops. If a property is not specified, then it won’t be returned.

Returns:
a structured array of all the
properties found for each label.
Return type:numpy.ndarray

Examples

>>> numpy.set_printoptions(
...     formatter={"float": lambda x: str(float(x))}
... )
>>> region_properties_scikit_image(
...     numpy.zeros((2,2), dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties_scikit_image(
...     numpy.ones((2,2), dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 4.0, [0.5, 0.5])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties_scikit_image(
...     numpy.ones((3,3), dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 9.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties_scikit_image(
...     numpy.eye(3, dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 3.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties_scikit_image(
...     numpy.eye(3, dtype=int), ["area", "centroid"]
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 3.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties_scikit_image(
...     numpy.eye(3, dtype=int), properties=["area", "centroid"]
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 3.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> numpy.set_printoptions()
nanshe.imp.segment.region_properties_vigra(*args, **kwargs)[source]

Grabs region properties from a label image.

Parameters:
  • new_label_image (numpy.ndarray) – label image used for generating properties.
  • args (list) – additional position arguments to pass skimage.measure.regionprops.
  • **parameters (dict) – additional keyword arguments to pass skimage.measure.regionprops.

Note

Uses all the same options in skimage.measure.regionprops. If a property is not specified, then it won’t be returned.

Returns:
a structured array of all the
properties found for each label.
Return type:numpy.ndarray

Examples

>>> numpy.set_printoptions(
...     formatter={"float": lambda x: str(float(x))}
... )
>>> region_properties_vigra(
...     numpy.zeros((2,2), dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties_vigra(
...     numpy.ones((2,2), dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 4.0, [0.5, 0.5])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties_vigra(
...     numpy.ones((3,3), dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 9.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties_vigra(
...     numpy.eye(3, dtype=int)
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 3.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties_vigra(
...     numpy.eye(3, dtype=int), ["area", "centroid"]
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 3.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> region_properties_vigra(
...     numpy.eye(3, dtype=int), properties=["area", "centroid"]
... ) # doctest: +NORMALIZE_WHITESPACE
array([(1, 3.0, [1.0, 1.0])],
      dtype=[('label', '<i8'), ('area', '<f8'), ('centroid', '<f8', (2,))])
>>> numpy.set_printoptions()
nanshe.imp.segment.remove_low_intensity_local_maxima(*args, **kwargs)[source]

Removes the local maxima for regions, which have too many pixels that are below the intensity of their local maxima.

Parameters:
  • local_maxima (ExtendedRegionProps) – The local maxima to refine
  • percentage_pixels_below_max (float) – The percentage of pixels out of a region that are below the local maxima of the region
  • **parameters (dict) – essentially unused (catches unneeded arguments).
Returns:

a new instance with the

local maxima removed

Return type:

ExtendedRegionProps

nanshe.imp.segment.remove_too_close_local_maxima(*args, **kwargs)[source]

Removes local maxima that are too close to each other.

Parameters:
  • local_maxima (ExtendedRegionProps) – The local maxima to refine
  • min_local_max_distance (float) – The minimum allowed distance between local maxima
  • **parameters (dict) – essentially unused (catches unneeded arguments).
Returns:

a new instance with the

local maxima removed

Return type:

ExtendedRegionProps

nanshe.imp.segment.remove_zeroed_lines(*args, **kwargs)[source]

Due to registration errors, there will sometimes be lines that are zero. To correct this, we find an interpolated value to replace the zeros with. If that fails, we copy nearby values over.

Parameters:
  • new_data (numpy.ndarray) – data to remove lines from ( first axis is time).
  • erosion_shape (numpy.ndarray) – shape of the erosion element (will be filled with 1).
  • dilation_shape (numpy.ndarray) – shape of the dilation element (will be filled with 1).
  • out (numpy.ndarray) – where the final results will be stored.
  • **parameters (dict) – essentially unused (catches unneeded arguments).
Returns:

a new array with the zeroed

lines interpolated away.

Return type:

numpy.ndarray

nanshe.imp.segment.wavelet_denoising(*args, **kwargs)[source]

Performs wavelet denoising on the given dictionary.

Parameters:
  • new_image (numpy.ndarray) – array of data for generating a dictionary (first axis is time).
  • accepted_region_shape_constraints (dict) – a dictionary containing properties (that could be passed to region_properties) under this should be a dictionary that contains the keys min and/or max with a value for each.
  • accepted_neuron_shape_constraints (dict) – a dictionary containing properties (that could be passed to region_properties) under this should be a dictionary that contains the keys min and/or max with a value for each.
  • **parameters (dict) – additional parameters for various other function calls.
Returns:

a structured array of

candidate neurons.

Return type:

numpy.ndarray

nanshe.imp.segment.wavelet_thresholding(*args, **kwargs)[source]

Finds a thresholding using a noise estimate and the wavelet transform.

Parameters:
  • new_image (numpy.ndarray) – image to threshold.
  • significance_threshold (float) – threshold above which everything is considered significant for the purpose of noise estimation.
  • wavelet_scale (int) – the scale of wavelet transform to use.
  • noise_threshold (float) – a ratio to which the noise estimate scales (e.g. 1.0 means the noise computed is the noise used).
Returns:

a wavelet transformed

array and a mask.

Return type:

tuple of numpy.ndarray