producer.util module

Miscellaneous package utilities.

producer.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

producer.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

producer.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 polygon is not 2D, if polygon does not have two columns, or if the last axis of point does not have 2 and only 2 elements.

producer.util.powerset(iterable, reverse=False)[source]

” Construct an iterable that steps through all combinations of the provided iterable.

This is pulled from the recipes provided by the itertools documentation.

Examples

Get all unique combinations of the list [1,2,3]: >>> list(powerset([1,2,3])) [() (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)]

Parameters
  • iterable (iterable) – An iterable object

  • reverse (bool, optional) – Reverse the order (only roughly) of the iterable by placing the longer sequences first.

Returns

Iterable object that returns the sequence of combinations.

Return type

itertools.chain

producer.util.string_table(tbl, delimeter='print', has_header=True)[source]

Provided the array of data, format it with equally spaced columns and add a header (first row) and contents delimeter.

Parameters
  • tbl (numpy.ndarray) – Array of string representations of the data to print.

  • delimeter (str, optional) – If the first row in the table containts the column headers (see has_header), this sets the delimeter between first table row and the column data. Use 'print' for a simple line of hyphens, anything else results in an rst style table formatting.

  • has_header (bool, optional) – The first row in tbl contains the column headers.

Returns

Single long string with the data table.

Return type

str