femto.helpers module¶
- grouped(iterable, n)¶
Group an iterable in sub-groups of n elements.
The returned iterable have len(iterable)//n tuples containing n elements. If the number of elements of the input iterable are not a multiple of n, the remaining elements will not be returned.
s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ...- Parameters:
iterable (iterable) – Iterable to group in subset.
n (int) – Size of the sub-groups.
- Returns:
Grouped iterable in n-sized subsets.
- Return type:
iterable
- pairwise()¶
Group an iterable in sub-groups of n elements.
The returned iterable have len(iterable)//n tuples containing n elements. If the number of elements of the input iterable are not a multiple of n, the remaining elements will not be returned.
s -> (s0,s1,s2,...sn-1), (sn,sn+1,sn+2,...s2n-1), (s2n,s2n+1,s2n+2,...s3n-1), ...- Parameters:
iterable (iterable) – Iterable to group in subset.
n (int) – Size of the sub-groups.
- Returns:
Grouped iterable in n-sized subsets.
- Return type:
iterable
- swap(array, swap_pos)¶
Swaps elements.
Swap elements of an array given as input.
- Parameters:
array (list, numpy.ndarray) – Input array.
swap_pos (list[tuple[int, int]]) – List of tuple containing indexes of the element of the array to swap.
- Returns:
Swapped array.
- Return type:
list, numpy.ndarray
- listcast(x)¶
Cast to list.
Cast any input object to a list. If x is a Python dictionary, the output will be the list of all the dict-keys.
Code example: >>> d = {‘a’: 1, ‘b’: 2, ‘c’: 3} >>> e = listcast(d) >>> e >>> [‘a’, ‘b’, ‘c’]
- Parameters:
x (Any) – Input element to be casted.
- Returns:
x input casted to list.
- Return type:
list
- nest_level(lst)¶
Nest level.
Compute the neseting level of a list.
- Parameters:
lst (list[Any] | Any) – Input list with a certain nested level.
- Returns:
Number of nested lists, a flattened list has
nest_levelof 1.- Return type:
int
- collapse(iterable)¶
Collapse Generator.
Flatten an iterable with multiple levels of nesting (e.g., a list of lists of tuples) into non-iterable types.
>>> iterable = [(1, 2), ([3, 4], [[5], [6]])] >>> list(collapse(iterable)) [1, 2, 3, 4, 5, 6]
Binary and text strings are not considered iterable and will not be collapsed.
- Parameters:
iterable (Iterable[Any]) – Input list with an arbitrary nesting level.
- Yields:
Iterable[Any] – Flattened version of input iterable.
- Return type:
Iterable[Any]
- flatten(iterable)¶
Flatten list.
Flatten an arbitrarily nested list. Returns a new list, the original list is unchanged.
>>> flatten([1, 2, 3, [4], [], [[[[[[[[[5]]]]]]]]]]) [1, 2, 3, 4, 5] >>> flatten([[1, 2], 3] [1, 2, 3]
- Parameters:
iterable (list) – Input list with an arbitrary nesting level.
- Returns:
List with the same elements of the input list but a single nesting level.
- Return type:
list
- sign()¶
Sign iterator.
A generator that cycles through +1 and -1.
Code example: >>> s = sign() >>> next(s) 1 >>> next(s) -1 >>> next(s) 1 >>> next(s) -1 …
- Returns:
Iterator cycling through +1 and -1.
- Return type:
Iterator
- remove_repeated_coordinates(array)¶
Remove repeated coordinates.
- Parameters:
array (numpy.ndarray) – Coordinate array.
- Returns:
Returns an array of the same dimensions as the input one in which repeated points (redundant movements) are substituted with NaN values.
- Return type:
numpy.ndarray
- unique_filter(arrays)¶
Remove duplicate subsequent points.
Filtering adjacent identical points from a list of arrays. The function is different from other unique functions such as numpy’s unique function. Indeed, unique return (a sorted list of) the unique elements of the whole array. For example:
>>> x = np.array([1, 2, 3, 3, 3, 4, 3, 3]) >>> np.unique(x) np.array([1, 2, 3, 4]) >>> unique_filter([x]) np.array([1, 2, 3, 4, 3])
- Duplicates can be selected by creating a boolean index mask as follows:
make a row-wise diff (numpy.diff)
compute absolute value of all elements in order to work only with positive numbers
make a column-wise sum (numpy.diff)
mask is converted to boolean values
In this way consecutive duplicates correspond to a 0 value in the latter array. Converting this array to boolean (all non-zero values are True) the index mask can be retrieved. The first element is set to True by default since it is lost by the diff operation.
unique_filter works also with multiple arrays. If the input list contains several elements, the arrays are stacked together to form a [length_array, length_list] matrix. Each row of this matrix represents the coordinates of a point in a space with length_list dimensions. Finally, filtering operation is applied to the newly-constructed matrix to filter all the identical adjacent points. For example:
>>> x = np.array([1, 2, 3, 3, 3, 4, 3, 3]) >>> y = np.array([0, 1, 0, 0, 1, 1, 0, 1]) >>> unique_filter([x, y]) np.array([1, 2, 3, 3, 4, 3, 3])
- Parameters:
arrays (list[numpy.ndarray]) – List of arrays to filter.
- Returns:
Modified numpy matrix without adjacent duplicates.
- Return type:
numpy.ndarray
- split_mask(arr, mask)¶
Split maks.
Splits an array into sub-arrays based on a mask. The function return the list of sub-arrays correspoding to True values.
- Parameters:
arr (numpy.ndarray) – Input array.
maks (numpy.ndarray[bool]) – Boolean array used as mask to split the input array.
- Returns:
List of arrays associated to True (1) values of mask.
- Return type:
list[numpy.ndarray]
- pad_infinite(iterable, padding=None)¶
- Return type:
Iterator[Any]
- pad(iterable, size, padding=None)¶
- Return type:
Iterator[Any]
- almost_equal(polygon, other, tol=1e-6)¶
Compute equality between polygons using the shapely builtin function symmetric_difference to build a new polygon. The more similar the two input polygons, the smaller the are of the new computed polygon.
- Parameters:
polygon (Polygon) – shaperly Polygon object
other (Polygon) – second shapely Polygon object
tol (float) – tolerance controlling the similarity
- Returns:
boolean value True if the polygon are almost equal, False otherwise
- Return type:
bool
- normalize_polygon(poly)¶
Normalize polygon.
The function standardize the input polygon. It set a given orientation and set a definite starting point for the inner and outer rings of the polygon.
- Parameters:
poly (geometry.Polygon) – Input
Polygonobject.- Returns:
New
Polygonobject constructed with the new ordered sequence of points.- Return type:
geometry.Polygon
See also
This stackoverflow answer.
- lookahead(iterable)¶
Lookahead.
Pass through all values from the given iterable, augmented by the information if there are more values to come after the current one (False), or if it is the last value (True).
- Parameters:
iterable (iterable[Any]) – Any iterable.
- Returns:
Returns a tuple of values, the i-th element of the iterable and a boolean value, if True the i-th element is the last element of the iterable.
- Return type:
Generator[tuple[Any, bool]]
- walklevel(path, depth=1)¶
Walklevel.
It works just like os.walk, but you can pass it a level parameter that indicates how deep the recursion will go. If depth is 1, the current directory is listed. If depth is 0, nothing is returned. If depth is -1 (or less than 0), the full depth is walked.
- Parameters:
path (str | pathlib.Path) – Path of the directory to explore.
depth (int) – Number of directory-tree levels to traverse.
- Returns:
root, subdirectories, files.
- Return type:
Generator[tuple]
- delete_folder(path)¶
Delete folder.
Empty and remove the folder given as input.
- Parameters:
path (str | pathlib.Path) – Directory to remove.
- Return type:
None
- normalize_phase(phase, zero_to_two_pi=False)¶
Normalize a phase to be within +/- pi.
- Parameters:
phase (float) – Phase to normalize.
zero_to_two_pi (bool) – True -> 0 to 2*pi, False -> +/- pi.
- Returns:
Normalized phase within +/- pi or 0 to 2*pi
- Return type:
float