compas_fab.utilities
¤
Utility functions for compas_fab.
Numerical helpers (allclose, arange, clamp, diffs, map_range,
range_geometric_row, sign, argmax, argmin, argsort) and a
lazy-import helper (LazyLoader).
Classes¤
LazyLoader
¤
Lazily import a module, mainly to avoid pulling in large dependencies.
contrib, and ffmpeg are examples of modules that are large and not always
needed, and this allows them to only be loaded when they are used.
Functions:¤
allclose
¤
Returns True if two lists are element-wise equal within a tolerance.
The function is similar to NumPy's allclose function.
Parameters:
-
l1(list[float]) –The first list.
-
l2(list[float]) –The second list.
-
tol(float, default:1e-05) –The tolerance within which the lists are considered equal. Default is 1e-05.
Returns:
-
bool–True if the lists are equal within the tolerance, otherwise False.
Raises:
-
ValueError–If 2 lists of different length are passed.
arange
¤
Returns evenly spaced values within a given interval.
The function is similar to NumPy's arange function.
Parameters:
-
start(float) –The starting value of the sequence.
-
stop(float) –The end value of the sequence.
-
step(float) –The spacing between the values.
Returns:
-
obj:`list` of :obj:`float`–The list of evenly spaced values.
argmax
¤
Returns the index of the maximum value in numbers.
The function is similar to NumPy's argmax function.
Parameters:
Returns:
-
obj:`int`–The index of the maximum value in the list.
Notes
For a large list of numbers reconsider using NumPy's argmax function, since this function might take too long.
argmin
¤
Returns the index of the minimum value in numbers.
The function is similar to NumPy's argmin function.
Parameters:
Returns:
-
obj:`int`–The index of the minimum value in the list.
Notes
For a large list of numbers reconsider using NumPy's argmin function, since this function might take too long.
argsort
¤
Returns the indices that would sort an array of numbers.
The function is similar to NumPy's argsort function.
Parameters:
Returns:
-
obj:`list` of :obj:`int`–The indices that would sort the array.
Notes
For a large list of numbers reconsider using NumPy's argsort function, since this function might take too long.
clamp
¤
diffs
¤
Returns the element-wise differences between two lists.
Parameters:
Returns:
-
obj:`list` of :obj:`float`–The element-wise differences between the two lists.
Raises:
-
ValueError–If 2 lists of different length are passed.
list_files_in_directory
¤
list_files_in_directory(
directory: str, fullpath: bool = False, extensions: list[str] | None = None
) -> list[str]
This function lists just the files in a directory, not sub-directories.
Parameters:
-
directory(str) –The directory to search for files.
-
fullpath(bool, default:False) –Specifies if the returned list of strings is with the full path.
-
extensions(list[str] | None, default:None) –A list of allowed extensions, e.g. ["jpg", "png"] if you just want to list images.
Returns:
-
obj:`list` of :obj:`str`–A list of files as string if files exist, or empty list.
map_range
¤
Performs a linear interpolation of a value within the range of [from_min, from_max] to another range of [to_min, to_max].
Parameters:
-
value(float) –The value to map.
-
from_min(float) –The minimum value of the input range.
-
from_max(float) –The maximum value of the input range.
-
to_min(float) –The minimum value of the output range.
-
to_max(float) –The maximum value of the output range.
Returns:
-
obj:`float`–The mapped value.
range_geometric_row
¤
Returns a list of numbers with a certain relation to each other.
The function divides one number into a list of d numbers [n0, n1, ...], such that their sum is number and the relation between the numbers is defined with n1 = n0 / r, n2 = n1 / r, n3 = n2 / r, ...
Parameters:
-
number(float) –The initial number to divide. This number is the first element in the returned list.
-
d(int) –The number of elements in the returned list.
-
r(float, default:1.1) –The ratio between the numbers in the list. Default is 1.1.
Returns:
-
obj:`list` of :obj:`float`–The list of numbers with the geometric relation.