femto.helpers module#
- grouped(iterable, n)#
Gruoup 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[Any]) – iterable to group
n (int) – size of the sub-groups
- Returns:
grouped iterable
- Return type:
Iterable[Any]
- pairwise(iterable, *, n=2)#
Gruoup 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[Any]) – iterable to group
n (int) – size of the sub-groups
- Returns:
grouped iterable
- Return type:
Iterable[Any]
- swap(array, swap_pos)#
Swaps elements
- Parameters:
array (list[Any]) –
swap_pos (list[tuple[int, int]]) –
- Returns:
- Return type:
list[Any]
- listcast(x)#
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 object
- Returns:
list
- Return type:
list[Any]
- class dotdict(*args, **kwargs)#
Bases:
Dict
[Any
,Any
]dot.notation access to dictionary attributes
- nest_level(lst)#
Compute the neseting level of a list.
- Parameters:
lst (list[Any]) – input object
- Returns:
number of nested lists, a flatten list has nest_level of 1.
- Return type:
int
- flatten(items)#
A recursive function that flattens a list.
- Parameters:
items – input list
- Returns:
list with the same elements of the input list but a single nesting level.
- sign()#
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[int]
- unique_filter(arrays)#
Remove duplicate subsequent points.
It takes a list of numpy arrays and returns a numpy array of unique rows. At least one coordinate have to change between two consecutive lines of the [X,Y,Z,F,S] matrix.
- 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.
- Returns:
Modified coordinate matrix (x, y, z, f, s) without duplicates.
- Return type:
numpy.ndarray
Filtering adjacent identical points from a list of arrays.
Filter adjacent identical point from array. 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])
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[npt.NDArray[np.float32]]) – list of arrays
- Returns:
matrix of arrays, each row has the filtered points on a given coordinate-axis.
- Return type:
npt.NDArray[np.float32]
- split_mask(arr, mask)#
Splits an array into sub-arrays based on a mask.
The function return the list of sub-arrays correspoding to True values.
- Parameters:
arr (npt.NDArray[Any]) – Input array
mask (npt.NDArray[np.generic]) – Boolean array used as mask to split the input array
- Returns:
List of arrays associated to True (1) values of mask.
- Return type:
list[npt.NDArray[Any]]
- pad_infinite(iterable, padding=None)#
- Return type:
Iterator[Any]
- pad(iterable, size, padding=None)#
- Return type:
Iterator[Any]
- almost_equal(polygon, other, tol=1e-06)#
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
- load_parameters(param_file)#
The load_param function loads a YAML configuration file and returns a list of dictionaries containing the parameters. The function first opens the YAML file in binary mode and uses the tomli library to load the configuration into a dictionary. The function then removes the DEFAULT dictionary from the configuration and merges its contents with the other dictionaries in the configuration. Finally, the function returns a list of dictionaries, where each dictionary contains the merged contents of the DEFAULT dictionary and one of the other dictionaries in the configuration.
- Parameters:
param_file (str, pathlib.Path) – Path to the YAML parameter file.
- Return type:
List of the parameters dictionaries.
- 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
Polygon
object.- Returns:
New
Polygon
object 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).