fiberlab.util module
Miscellaneous package utilities.
- fiberlab.util.all_subclasses(cls)[source]
Collect all the subclasses of the provided class.
The search follows the inheritance to the highest-level class. Intermediate base classes are included in the returned set, but not the base class itself.
Thanks to: https://stackoverflow.com/questions/3862310/how-to-find-all-the-subclasses-of-a-class-given-its-name
- Parameters:
cls (object) – The base class
- Returns:
The unique set of derived classes, including any intermediate base classes in the inheritance thread.
- Return type:
set
- fiberlab.util.boxcar_average(arr, boxcar)[source]
Boxcar average an array.
- Parameters:
arr (numpy.ndarray) – Array to average. Currently cannot be masked.
boxcar (
int,tuple) – Integer number of pixels to average. If a single integer, all axes are averaged with the same size box. If atuple, the integer is defined separately for each array axis; length of tuple must match the number of array dimensions.
- Returns:
The averaged array. If boxcar is a single integer, the returned array shape is:
tuple([s//boxcar for s in arr.shape])
A similar operation gives the shape when boxcar has elements defined for each array dimension. If the input array is not an integer number of boxcar pixels along a given dimension, the remainder of the array elements along that dimension are ignored (i.e., pixels within the modulus of the array shape and boxcar of the end of the array dimension are ignored).
- Return type:
- fiberlab.util.boxcar_replicate(arr, boxcar)[source]
Boxcar replicate an array.
- Parameters:
arr (numpy.ndarray) – Array to replicate.
boxcar (
int,tuple) – Integer number of times to replicate each pixel. If a single integer, all axes are replicated the same number of times. If atuple, the integer is defined separately for each array axis; length of tuple must match the number of array dimensions.
- Returns:
The block-replicated array.
- Return type:
- fiberlab.util.point_inside_polygon(polygon, point)[source]
Determine if one or more points is inside the provided polygon.
Primarily a wrapper for
polygon_winding_number(), that returns True for each point that is inside the polygon.- Parameters:
polygon (numpy.ndarray) – An Nx2 array containing the x,y coordinates of a polygon. The points should be ordered either counter-clockwise or clockwise.
point (numpy.ndarray) – One or more points for the winding number calculation. Must be either a 2-element array for a single (x,y) pair, or an Nx2 array with N (x,y) points.
- Returns:
Boolean indicating whether or not each point is within the polygon.
- Return type:
bool, numpy.ndarray
- fiberlab.util.polygon_area(x, y)[source]
Return the area of an arbitrary polygon.
Thanks to: https://stackoverflow.com/questions/24467972/calculate-area-of-polygon-given-x-y-coordinates
- fiberlab.util.polygon_winding_number(polygon, point)[source]
Determine the winding number of a 2D polygon about a point.
The code does not check if the polygon is simple (no interesecting line segments). Algorithm taken from Numerical Recipes Section 21.4.
- Parameters:
polygon (numpy.ndarray) – An Nx2 array containing the x,y coordinates of a polygon. The points should be ordered either counter-clockwise or clockwise.
point (numpy.ndarray) – One or more points for the winding number calculation. Must be either a 2-element array for a single (x,y) pair, or an Nx2 array with N (x,y) points.
- Returns:
The winding number of each point with respect to the provided polygon. Points inside the polygon have winding numbers of 1 or -1; see
point_inside_polygon().- Return type:
int, numpy.ndarray- Raises:
ValueError – Raised if
polygonis not 2D, ifpolygondoes not have two columns, or if the last axis ofpointdoes not have 2 and only 2 elements.