stnmf.spatial.contour

stnmf.spatial.contour(image, sigma=1.22, smooth=1.2)

Create contour outline of an image and return coordinate data points

Parameters:
  • image ((x,y) or (r,x,y) array_like) – Two-dimensional image or stack of r independent two-dimensional images, where x and y are the spatial dimensions

  • sigma (float, optional) – Standard deviation (s.d.) equivalent to a linearly interpolated contour level. A contour level at 1.22 s.d. offers very nice visual tiling (for mosaics). Default is 1.22

  • smooth ({0.5, 1.2} or float, optional) – Sigma for Gaussian smoothing. This value determines the detail of the contour line. The larger the number of pixels, the more smoothing is required. As a rule of thumb, 0.5 is suitable for subunits and 1.2 for receptive fields. See notes. Default is 1.2

Returns:

points ((n,2) or (r,) numpy.ndarray) – Contour outline of n data points as xy-coordinate pairs in image-pixel coordinates or stack of contours in an array of object-type (due to varying number of data points)

Raises:

Notes

The image is first nearest-neighbor upsampled and smoothed using a smooth-sigma Gaussian filter to obtain smooth contour lines. The returned contour line is the largest-area island among the positive closed contours. The contour is then scaled back down to image-pixel resolution.

When obtaining the contour of a receptive field, a value of 1.2 for smooth is generally suitable, whereas a value of 0.5 works well for the smaller-sized subunits.

Note

However, suitable values for smooth depend on the size of the receptive field/subunit in terms of stixels (stimulus/image pixels) and may need to be increased for coarser stimuli.

Examples

>>> cntr = contour(spatial_filter)

Visualize the contour on top of the spatial filter

>>> from matplotlib import pyplot as plt
>>> vm = np.nanmax(np.abs(spatial_filter), initial=0) or 1
>>> pretty = dict(cmap='binary_r', origin='lower', aspect='equal',
>>>               interpolation='nearest', vmin=-vm, vmax=vm)
>>> plt.imshow(spatial_filter.T, **pretty)  # Draw image
>>> plt.plot(*cntr.T, 'r', lw=3)            # Draw contour on top

Use shapely to perform geometrical analyses

>>> import shapely
>>> poly = shapely.Polygon(cntr)
>>> rf_area = poly.area * pixel_size