detectda.hlpr

Functions

alps(arr)

Get the ALPS statistic of an array of values, not all zero.

block_sum(arr, m[, div])

Sums adjacent blocks of m frames.

calc_close(pd, diffs, prox_arr[, val])

Calculates the nearest point in the persistence diagram

calc_reject(arr, val_arr[, alpha, conservative])

Returns dictionary of index array, boolean array, rejection threshold index

degp_totp(arr[, p, inf])

Calculate degree-p total persistence from array of lifetimes.

fitsmoo(im[, polygon, sigma, max_death_pixel_int])

Smooths, then fits 0-dimensional cubical persistence on an image.

get_be(arr)

Gets beginnings and ends of True sequences in Boolean arrays.

get_cc(point, bin_im)

Gets the connected component in the image bin_im containing the element "point".

getxy_col(arr, nrows)

Returns (x,y) image coordinates from column-major representation

pd_thresh_calc(diag, pixels, minv, maxv[, dim, num])

Calculates best persistence preserving threshold as described in Chung and Day (2018).

pers_entr(arr[, neg])

Gets persistence (shannon) entropy of an array of values, not all zero.

persmoo(im[, polygon, sigma])

Smooths, then fits 0 and 1-dimensional cubical persistence on an image. Returns other important information as well.

pg0(arr)

Calculate the proportion of the entire array which is at least as much as the first element.

std_video(video[, flip])

Returns a video where each frame is standardized to have mean 0 pixel intensity with

weight_func(arr)

Weight function for persistence image using defaults from Obayashi, Hiraoka, and Kimura (2018).

Module Contents

detectda.hlpr.alps(arr)[source]

Get the ALPS statistic of an array of values, not all zero.

detectda.hlpr.block_sum(arr, m, div=1)[source]

Sums adjacent blocks of m frames.

detectda.hlpr.calc_close(pd, diffs, prox_arr, val=1)[source]

Calculates the nearest point in the persistence diagram

Parameters:
  • pd (ndarray of shape (n_barcodes, 2)) – A persistence diagram in (birth, death) coordinates. Note that n_barcodes is the number of barcodes

  • diffs (list/tuple of two elements) – Corresponds to the distance between elements of grid discretization, such as in birtt_bd and lifet_bd in the gen_pers_im method of the ImageSeriesPlus class.

  • prox_arr (ndarray of shape (m, 2)) – M observations in (birth, lifetime) coordinates corresponding to elements of the grid discretization used in calculation of the persistence image.

  • val (float, optional) – The default is 1.

Returns:

proxs – List of all generators in the persistence diagram pd that lie in the Voronoi cell of one of the elements of prox_arr. This is indicated with a non-zero entry in proxs.

Return type:

list

detectda.hlpr.calc_reject(arr, val_arr, alpha=0.05, conservative=False)[source]

Returns dictionary of index array, boolean array, rejection threshold index array of indices of hypotheses that are rejected via BH procedure, and boolean array of whether or not hypothesis is rejected.

Parameters:
  • arr (array_like) – Array of probabilities (p-values) between 0 and 1.

  • val_arr (array_like) – Array of values to break ties in p-values.

  • alpha (value between 0 and 1, optional) – Signficance level for BH procedure. The default is 0.05.

  • conservative (default = False) – Dictates whether or not conservative BH procedure is used.

Returns:

out – Dictionary containing information listed above.

Return type:

dict

detectda.hlpr.degp_totp(arr, p=1, inf=False)[source]

Calculate degree-p total persistence from array of lifetimes.

Parameters:
  • arr (array_like) – Should be an array of persistence lifetimes, with nonnegative entries

  • p (double, optional) – Exponent for degree-p total lifetime. The default is 1.

  • inf (bool, optional) – Whether or not to calculate infinity norm. The default is False.

Raises:

ValueError – Raises error if p less than 1

Returns:

The degree-p total persistence.

Return type:

double

Notes

The degree-p total (0-dimensional) persistence is specified as:

\[L_p(A(I)) := \sum_{(b,d) \in PD_0} (d-b)^p.\]

If inf==True, this overwrites the chosen value of p.

detectda.hlpr.fitsmoo(im, polygon=None, sigma=None, max_death_pixel_int=True)[source]

Smooths, then fits 0-dimensional cubical persistence on an image. Returns information on:

  1. Location of positive cells, i.e. local minima (cu_pos, or index-0 and index-1 columns)

  2. Lifetime information (cu_totpers, or index-2 column)

  3. Whether or not a positive cell lies within the polygon (cu_ex_inpoly, or index-3 column)

detectda.hlpr.get_be(arr)[source]

Gets beginnings and ends of True sequences in Boolean arrays.

Parameters:

arr (array_like) – Boolean array

Returns:

out – Tuple consisting of two arrays. Index-0 array consists of beginning of “True” sequences, and Index-1 array consists of ends of “True” sequences.

Return type:

2-tuple

detectda.hlpr.get_cc(point, bin_im)[source]

Gets the connected component in the image bin_im containing the element “point”.

Parameters:
  • point (list/tuple of 2 elements) – (x,y) coordinate point for image matrix

  • bin_im (array_like) – Boolean array.

Returns:

cc – list of elements in connected component containing point.

Return type:

list of lists

detectda.hlpr.getxy_col(arr, nrows)[source]

Returns (x,y) image coordinates from column-major representation

Parameters:
  • arr (ndarray of shape (dim,)) – Should be a 1d numpy array

  • nrows (int) – The number of rows in the 2d array. Should be factor of len(arr)

Returns:

  • x (int) – The x-coordinate from column-major representation. Note that x-coordinate (for an image) corresponds to the column of the matrix

  • y (int) – The y-coordinate from column-major representation. Note that y-coordinate (for an image) corresponds to the row of the matrix

detectda.hlpr.pd_thresh_calc(diag, pixels, minv, maxv, dim='both', num=50)[source]

Calculates best persistence preserving threshold as described in Chung and Day (2018).

Parameters:
  • diag (ndarray of shape (n_samples, 6)) – Persistence diagram object a la the output of the persmoo function.

  • pixels (array-like) – Vector containing the pixels of the image on which to perform PD thresholding.

  • minv (float) – Minimum threshold to consider.

  • maxv (float) – Maximum threshold to consider.

  • dim (str or int, optional) – Integer 0 or 1 corresponds to thresholding only based on dimension 0 and 1 persistence features. The default is “both”, corresponding to both dimensions 0 and 1.

  • num (int, optional) – Positive integer indicating how many evenly spaced pixel distribution quantiles should be considered in PD thresholding (prior to restricting to the range [minv, maxv]).

Returns:

The best topology- and geometry-preserving threshold

Return type:

float

detectda.hlpr.pers_entr(arr, neg=True)[source]

Gets persistence (shannon) entropy of an array of values, not all zero.

detectda.hlpr.persmoo(im, polygon=None, sigma=None)[source]

Smooths, then fits 0 and 1-dimensional cubical persistence on an image. Returns other important information as well.

Parameters:
  • im (individual image, i.e. two-dimensional array) – Greyscale image on which sublevelset homology will be calculated.

  • polygon (Shapely.Polygon object, optional) – Not necessary, only if one wants to restrict region to focus on. The default is None.

  • sigma (smoothing parameter sigma, optional) – Smoothing parameter sigma for Gaussian filter. The default is None.

Returns:

cu_tot – Array with positional and homology information, as follows: cu_pos: #(x,y) coordinates of positive/negative cells (which create components/destroy loops) cu_pers: #(dimension, birth, death)… cu_ex_inpoly: #row indices of positive/negative cells located within specified polygon

Return type:

ndarray

detectda.hlpr.pg0(arr)[source]

Calculate the proportion of the entire array which is at least as much as the first element.

Parameters:

arr (array_like) – Input array consists of numerical values.

Returns:

prop – The aforementioned proportion.

Return type:

double

detectda.hlpr.std_video(video, flip=False)[source]

Returns a video where each frame is standardized to have mean 0 pixel intensity with standard deviation 1.

Parameters:
  • video (ndarray) – An array of shape (frames, rows, columns).

  • flip (bool.) – Whether or not to invert the pixels of the video. The default is False.

Returns:

The frame-standardized video of shape (frames, rows, columns).

Return type:

ndarray

detectda.hlpr.weight_func(arr)[source]

Weight function for persistence image using defaults from Obayashi, Hiraoka, and Kimura (2018).

Parameters:

arr (array_like) – Should be an array of persistence lifetimes, with nonnegative entries

Returns:

Array of weights corresponding to the given persistence lifetimes.

Return type:

ndarray