neurotools.spatial.geometry module

Collected functions from 2018–2023 concerning analyses of 2D data.

These routines work on 2D (x,y) points encoded as complex z=x+iy numbers.

neurotools.spatial.geometry.p2z(px, py=None)[source]

Ensure that a numpy array contains (x,y) points encoded as z = x+iy, or convert two arrays (x,y) int z = x+iy format.

Parameters:
  • px (np.array) –

    x coordinate or points, or, if py is None
    • complex z=x + iy array (in which case this function is a noop)

    • array with on dimension length 2 containing (px,py)

  • py (np.array) – y coordinate of points

Return type:

np.complex64

neurotools.spatial.geometry.z2p(pz)[source]

Convert complex points to 2D (x,y) points

Parameters:

ps (np.complex64)

Return type:

np.float32

neurotools.spatial.geometry.polar_smooth_contour(z, sigma=2)[source]

Smooth the radial and angular components of a closed, circular, non-self-intersecting contour z in the complex plane.

To avoid coodinate singularity, z should not intersect its own centroid.

Smoothing is accomplished in terms of adjacent samples, and the kernel standard deviation has units of samples. See resample_convex_hull to convert a convex shape with irregular angular sampling to one with regular angular sampling for better results.

Parameters:
  • px (1D complex64 z=x+iy points)

  • sigma (positive float)

neurotools.spatial.geometry.convex_hull(px, py=None)[source]

A wrapper for scipy.spatial.ConvexHull that returns points as z=x+iy.

Parameters:
  • px

  • py

neurotools.spatial.geometry.convex_hull_from_mask(x, Ntheta=None, sigma=None, close=True)[source]

Extract convex hull containing all pixels in a 2D boolean array that are True. The array x is interpreted as a (rows,cols) matrix where row number is the y coordinate and col number is the x coordinate.

Parameters:
  • x (2D np.bool)

  • Ntheta (positive int) – If not None, the resulting hull will be resampled at Ntheta uniform angular intervals around the centroid.

  • sigma (positive float) – If not None, resulting hull will be smoothed in polar coordinates by a circular Gaussian kernel with standard deviation sigma (in DEGRESS).

  • close (boolean; default True) – Whenter to repeat the first point in the convext hull at the end so that it can be plotted directly as a closed contour.

Returns:

z

Return type:

np.complex64

neurotools.spatial.geometry.resample_convex_hull(z, Ntheta=60)[source]

Resample a convex shape at uniform angular intervals around its centroid

Parameters:
  • z

  • Ntheta (positive int; default 60)

neurotools.spatial.geometry.in_hull(z, hull)[source]

Determine if the list of points P lies inside a convex hull

credit: https://stackoverflow.com/a/52405173/900749

Parameters:
  • z (z=x+iy points to test)

  • hull (ConvexHull, or points to form one with)

Returns:

in_hull

Return type:

np.boolean