neurotools.spatial.masking module

Routines related to 2D boolean arrays used as image masks depends on neurotools.spatial.geometry These routines expect 2D (x,y) points to be encoded as complex z=x+iy numbers.

neurotools.spatial.masking.as_mask(x)[source]

Verify that x is a 2D np.ndarray(dtype=bool), or attempt to convert it to one if not.

Parameters:

x (2D np.array) – This routine understands np.ndarray(dtype=bool), as well as numeric arrays that contain only two distinct values, and use a positive value for True and any other value for False.

Returns:

x – Image mask as a 2D Numpy array with datatype bool.

Return type:

np.ndarray(dtype=bool)

neurotools.spatial.masking.mask_to_points(x)[source]

Get locations of all True pixels in a 2D boolean array encoded as z = column + i*row.

Parameters:

x (2D np.ndarray(dtype=bool))

Returns:

z

Return type:

np.complex64

neurotools.spatial.masking.extend_mask(mask, sigma=2, thr=0.5)[source]

Extend 2D image mask by blurring and thresholding. Note: this uses circular convolution; Pad accordingly.

Parameters:
  • mask (2D np.ndarray(dtype=bool)) – Image mask to extend.

  • sigma (float, default 3) – Gaussian blur radius.

  • thr – (float, default .5): Threshold for new mask.

Returns:

smoothed mask

Return type:

2D np.ndarray(dtype=bool)

neurotools.spatial.masking.pgrid(W, H=None)[source]

Create a (W,H) = (Nrows,Ncols) coordinate grid where each cell is z = irow + 1j * icol

Parameters:
  • W (int or 2D np.array) – If int: the number of columns in the grid. if np.array: Take (H,W) from the array’s shape

  • H (int) – Number of rows in grid; Defaults to H=W if H=None.

neurotools.spatial.masking.nan_mask(mask, nanvalue=False, value=None)[source]

Create a (W,H) = (Nrows,Ncols) coordinate grid where each cell is z = irow + 1j * icol

Parameters:

mask (2D np.ndarray(dtype=bool))

neurotools.spatial.masking.maskout(x, mask, **kwargs)[source]

Set pixels in x where mask is False to NaN

Parameters:
  • x (2D np.float32)

  • mask (2D np.ndarray(dtype=bool))

neurotools.spatial.masking.trim_mask(mask)[source]

Remove empty edges of boolean mask. See mask_crop(array,mask) to use a mask to trim another array.

Parameters:

mask (2D np.ndarray(dtype=bool))

neurotools.spatial.masking.mask_crop(x, mask, fill_nan=True)[source]

Set pixels in x where mask is False to NaN, and then remove empty rows and columns.

See trim_mask(mask) to crop out empty rows, columns from a mask.

Parameters:
  • x (2D np.float32)

  • mask (2D np.ndarray(dtype=bool))

  • fill_nan (bool; default True) – Whether to fill “false” values with NaN

neurotools.spatial.masking.to_image(x, mask, fill=nan, crop=False)[source]

Assign list of values x to locations in mask that are True, in row-major order.

Parameters:
  • x (1D np.array)

  • mask (2D np.ndarray(dtype=bool))

  • full (float; default np.NaN) – Fill value for regions outside the mask

  • crop (bool; default False) – Whether to remove empty rows/cols of the resulting image.