erlab.analysis.mask

Functions related to masking.

Polygon masking is adapted from the CGAL library. More information on point-in-polygon strategies can be found in Ref. [5].

Modules

polygon

Point-in-polygon algorithm.

Functions

hex_bz_mask_points(x, y[, a, rotate, ...])

Return a mask for given points.

mask_with_hex_bz(kxymap[, a, rotate, invert])

Return map masked with a hexagonal BZ.

mask_with_polygon(arr, vertices[, dims, invert])

polygon_mask(vertices, x, y[, invert])

Create a mask based on a polygon defined by its vertices.

polygon_mask_points(vertices, x, y[, invert])

Compute a mask indicating whether points are inside or outside a polygon.

erlab.analysis.mask.hex_bz_mask_points(x, y, a=3.54, rotate=0, offset=(0.0, 0.0), reciprocal=False, invert=False)[source]

Return a mask for given points.

erlab.analysis.mask.mask_with_hex_bz(kxymap, a=3.54, rotate=0.0, invert=False)[source]

Return map masked with a hexagonal BZ.

erlab.analysis.mask.mask_with_polygon(arr, vertices, dims=('kx', 'ky'), invert=False)[source]
erlab.analysis.mask.polygon_mask(vertices, x, y, invert=False)[source]

Create a mask based on a polygon defined by its vertices.

Parameters:
  • vertices (ndarray[Any, dtype[float64]]) – The vertices of the polygon. The shape should be (N, 2), where N is the number of vertices.

  • x (ndarray[Any, dtype[float64]]) – The x-coordinates of the grid points.

  • y (ndarray[Any, dtype[float64]]) – The y-coordinates of the grid points.

  • invert (bool) – If True, invert the mask (i.e., set True where the polygon is outside and False where it is inside). Default is False.

Returns:

mask – The mask array with shape (len(x), len(y)). The mask contains True where the polygon is inside and False where it is outside (or vice versa if invert is True).

Return type:

ndarray

Note

This function uses the erlab.analysis.mask.polygon.bounded_side_bool to determine whether a point is inside or outside the polygon.

Example

>>> vertices = np.array([[0.2, 0.2], [0.2, 0.8], [0.8, 0.8], [0.8, 0.2]])
>>> x = np.linspace(0, 1, 5)
>>> y = np.linspace(0, 1, 5)
>>> polygon_mask(vertices, x, y)
array([[False, False, False, False, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False,  True,  True,  True, False],
       [False, False, False, False, False]])
erlab.analysis.mask.polygon_mask_points(vertices, x, y, invert=False)[source]

Compute a mask indicating whether points are inside or outside a polygon.

Parameters:
  • vertices (ndarray[Any, dtype[float64]]) – The vertices of the polygon. The shape should be (N, 2), where N is the number of vertices.

  • x (ndarray[Any, dtype[float64]]) – The x-coordinates of the points.

  • y (ndarray[Any, dtype[float64]]) – The y-coordinates of the points.

  • invert (bool) – If True, invert the mask (i.e., set True where the polygon is outside and False where it is inside). Default is False.

Returns:

mask – A boolean array of shape (len(x),) indicating whether each point is inside or outside the polygon.

Return type:

ndarray

Raises:

ValueError – If the lengths of x and y are not equal.

Notes

This function uses the erlab.analysis.mask.polygon.bounded_side_bool to determine whether a point is inside or outside the polygon.