prysm.geometry

Functions used to generate various geometrical constructs.

prysm.geometry.mask_cleaner(mask_or_str_or_tuple, samples)

Return an array if given one, otherwise generate it from the parameters.

Parameters

mask_or_string_or_tuple (numpy.ndarray, string, or iterable) – if an array, returned untouched. If a string, return a radius=1 mask of that geometry. If an interable, (string, float) of name and radius, generated as given

Returns

square array; value of one inside the mask, zero otuside

Return type

numpy.ndarray

class prysm.geometry.MaskCache

Bases: object

Cache for geometric masks.

get_mask(shape, samples, radius=1)

Get a mask with the given number of samples and shape.

Parameters
  • shape (str) – string of a regular n-sided polygon, e.g. ‘square’, ‘hexagon’.

  • samples (int) – number of samples, mask is (samples,samples) in shape

  • radius (float, optional) – normalized radius of the mask. radius=1 will fill the x, y extent

Returns

ndarray; ones inside the shape, zeros outside

Return type

numpy.ndarray

clear(*args)

Empty the cache.

prysm.geometry.gaussian(sigma=0.5, samples=128)

Generate a gaussian mask with a given sigma.

Parameters
  • sigma (float) – width parameter of the gaussian, expressed in samples of the output array

  • samples (int) – number of samples in square array

Returns

mask with gaussian shape

Return type

numpy.ndarray

prysm.geometry.rectangle(width, height=None, angle=0, samples=128)

Generate a rectangular, with the “width” axis aligned to ‘x’.

Parameters
  • width (float) – diameter of the rectangle, relative to the width of the array. width=1 fills the horizontal extent when angle=0

  • height (float) – diameter of the rectangle, relative to the height of the array. height=1 fills the vertical extent when angle=0. If None, inherited from width to make a square

  • angle (float) – angle

Returns

array with the rectangle painted at 1 and the background at 0

Return type

numpy.ndarray

prysm.geometry.rotated_ellipse(width_major, width_minor, major_axis_angle=0, samples=128)

Generate a binary mask for an ellipse, centered at the origin.

The major axis will notionally extend to the limits of the array, but this will not be the case for rotated cases.

Parameters
  • width_major (float) – width of the ellipse in its major axis

  • width_minor (float) – width of the ellipse in its minor axis

  • major_axis_angle (float) – angle of the major axis w.r.t. the x axis, degrees

  • samples (int) – number of samples

Returns

An ndarray of shape (samples,samples) of value 0 outside the ellipse and value 1 inside the ellipse

Return type

numpy.ndarray

Notes

The formula applied is:

((x-h)cos(A)+(y-k)sin(A))^2 ((x-h)sin(A)+(y-k)cos(A))^2

______________________________ + ______________________________ 1

a^2 b^2

where x and y are the x and y dimensions, A is the rotation angle of the major axis, h and k are the centers of the the ellipse, and a and b are the major and minor axis widths. In this implementation, h=k=0 and the formula simplifies to:

(x*cos(A)+y*sin(A))^2 (x*sin(A)+y*cos(A))^2

______________________________ + ______________________________ 1

a^2 b^2

see SO: https://math.stackexchange.com/questions/426150/what-is-the-general-equation-of-the-ellipse-that-is-not-in-the-origin-and-rotate

Raises

ValueError – if minor axis width is larger than major axis width

prysm.geometry.square(samples=128, radius=1)

Create a square mask.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.pentagon(samples=128, radius=1)

Create a pentagon mask.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.hexagon(samples=128, radius=1)

Create a hexagon mask.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.heptagon(samples=128, radius=1)

Create a heptagon mask.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.octagon(samples=128, radius=1)

Create a octagon mask.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.nonagon(samples=128, radius=1)

Create a nonagon mask.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.decagon(samples=128, radius=1)

Create a decagon mask.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.hendecagon(samples=128, radius=1)

Create a hendecagon mask.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.dodecagon(samples=128, radius=1)

Create a dodecagon mask.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.trisdecagon(samples=128, radius=1)

Create a trisdecagonal mask.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.truecircle(samples=128, radius=1)

Create a “true” circular mask with anti-aliasing.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

nonbinary ndarray representation of the mask

Return type

numpy.ndarray

Notes

Based on a more general algorithm by Jim Fienup

prysm.geometry.circle(samples=128, radius=1)

Create a circular mask.

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.inverted_circle(samples=128, radius=1)

Create an inverted circular mask (obscuration).

Parameters
  • samples (int, optional) – number of samples in the square output array

  • radius (float, optional) – radius of the shape in the square output array. radius=1 will fill the x

Returns

binary ndarray representation of the mask

Return type

numpy.ndarray

prysm.geometry.regular_polygon(sides, samples, radius=1)

Generate a regular polygon mask with the given number of sides and samples in the mask array.

Parameters
  • sides (int) – number of sides to the polygon

  • samples (int) – number of samples in the output polygon

  • radius (float, optional) – radius of the regular polygon. For R=1, will fill the x and y extent

Returns

mask for regular polygon with radius equal to the array radius

Return type

numpy.ndarray

prysm.geometry.generate_mask(vertices, num_samples=128)

Create a filled convex polygon mask based on the given vertices.

Parameters
  • vertices (iterable) – ensemble of vertice (x,y) coordinates, in array units

  • num_samples (int) – number of points in the output array along each dimension

Returns

polygon mask

Return type

numpy.ndarray

prysm.geometry.generate_vertices(sides, radius=1)

Generate a list of vertices for a convex regular polygon with the given number of sides and radius.

Parameters
  • sides (int) – number of sides to the polygon

  • radius (float) – radius of the polygon

Returns

array with first column X points, second column Y points

Return type

numpy.ndarray

prysm.geometry.generate_spider(vanes, width, rotation=0, arydiam=1, samples=128)

Generate the mask for a spider.

Parameters
  • vanes (int) – number of spider vanes

  • width (float) – width of the vanes in array units, i.e. a width=1/128 spider with arydiam=1 and samples=128 will be 1 pixel wide

  • rotation (float, optional) – rotational offset of the vanes, clockwise

  • arydiam (float, optional) – array diameter

  • samples (int, optional) – number of samples in the square output array

Returns

array, 0 inside the spider and 1 outside

Return type

numpy.ndarray