Utils
Utility functions for Tolvera.
CONSTS
Dict of CONSTS that can be used in Taichi scope
Source code in src/tolvera/utils.py
dotdict
create_and_validate_slice(arg, target_array)
Creates and validates a slice object based on the target array.
Source code in src/tolvera/utils.py
create_ndslices(dims)
Create a multi-dimensional slice from a list of tuples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dims |
list[tuple]
|
A list of tuples containing the slice parameters for each dimension. |
required |
Returns:
Type | Description |
---|---|
s_
|
np.s_: A multi-dimensional slice object. |
Source code in src/tolvera/utils.py
create_safe_slice(arg)
Creates a slice object based on the input argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arg |
(int, tuple, slice)
|
The argument for creating the slice. It can be an integer, a tuple with slice parameters, or a slice object itself. |
required |
Returns:
Name | Type | Description |
---|---|---|
slice |
slice
|
A slice object created based on the provided argument. |
Source code in src/tolvera/utils.py
flatten(lst)
Flatten a nested list or return a non-nested list as is.
generic_slice(array, slice_params)
Slices a NumPy array based on a tuple of slice parameters for each dimension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
ndarray
|
The array to be sliced. |
required |
slice_params |
tuple
|
A tuple where each item is either an integer, a tuple with slice parameters, or a slice object. |
required |
Returns:
Name | Type | Description |
---|---|---|
ndarray |
ndarray
|
The sliced array. |
Source code in src/tolvera/utils.py
time_function(func, *args, **kwargs)
Time how long it takes to run a function and print the result
Source code in src/tolvera/utils.py
validate_json_path(path)
Validate a JSON file path. It uses validate_path for initial validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The JSON file path to be validated. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the path is a valid JSON file path, raises an exception otherwise. |
Raises:
Type | Description |
---|---|
ValueError
|
If the path does not end with '.json'. |
Source code in src/tolvera/utils.py
validate_path(path)
Validate a path using os.path and pathlib.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path to be validated. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the path is valid, raises an exception otherwise. |
Raises:
Type | Description |
---|---|
TypeError
|
If the input is not a string. |
FileNotFoundError
|
If the path does not exist. |
PermissionError
|
If the path is not accessible. |
Source code in src/tolvera/utils.py
validate_slice(slice_obj, target_array)
Validates if the given slice object is applicable to the target ndarray.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
slice_obj |
tuple[slice]
|
A tuple containing slice objects for each dimension. |
required |
target_array |
ndarray
|
The array to be sliced. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the slice is valid for the given array, False otherwise. |