nabu.utils module¶
- nabu.utils.convert_index(idx, idx_max, default_val)[source]¶
Convert an index (possibly negative or None) to a non-negative integer.
- Parameters:
idx (int or None) – Index
idx_max (int) – Maximum value (upper bound) for the index.
default_val (int) – Default value if idx is None
Examples
Given an integer M, J = convert_index(i, M, XX) returns an integer in the mathematical range [0, M] (or Python range(0, M)). J can then be used to define an upper bound of a range.
- nabu.utils.to_3D_array(arr)[source]¶
Turn an array to a (C-Contiguous) 3D array with the layout (n_arrays, n_y, n_x).
- nabu.utils.view_as_images_stack(img)[source]¶
View an image (2D array) as a stack of one image (3D array). No data is duplicated.
- nabu.utils.rescale_integers(items, new_tot)[source]¶
” From a given sequence of integers, create a new sequence where the sum of all items must be equal to “new_tot”. The relative contribution of each item to the new total is approximately kept.
- Parameters:
items (array-like) – Sequence of integers
new_tot (int) – Integer indicating that the sum of the new array must be equal to this value
- nabu.utils.get_decay(curve, cutoff=1000.0, vmax=None)[source]¶
Assuming a decreasing curve, get the first point below a certain threshold.
- Parameters:
curve (numpy.ndarray) – A 1D array
cutoff (float, optional) – Threshold. Default is 1000.
vmax (float, optional) – Curve maximum value
- nabu.utils.generate_powers()[source]¶
Generate a list of powers of [2, 3, 5, 7], up to (2**15)*(3**9)*(5**6)*(7**5).
- nabu.utils.calc_padding_lengths1D(length, length_padded)[source]¶
Compute the padding lengths at both side along one dimension.
- Parameters:
length (int) – Number of elements along one dimension of the original array
length_padded (tuple) – Number of elements along one dimension of the padded array
- Returns:
pad_lengths – A tuple under the form (padding_left, padding_right). These are the lengths needed to pad the original array.
- Return type:
tuple
- nabu.utils.calc_padding_lengths(shape, shape_padded)[source]¶
Multi-dimensional version of calc_padding_lengths1D. Please refer to the documentation of calc_padding_lengths1D.
- nabu.utils.subsample_dict(dic, subsampling_factor)[source]¶
Subsample a dict where keys are integers.
- nabu.utils.compare_dicts(dic1, dic2)[source]¶
Compare two dictionaries. Return None if and only iff the dictionaries are the same.
- Parameters:
dic1 (dict) – First dictionary
dic2 (dict) – Second dictionary
- Returns:
res –
None: it means that dictionaries are the same
empty string (“”): the dictionaries do not have the same keys
nonempty string: path to the first differing items
- Return type:
result which can be the following:
- nabu.utils.remove_items_from_list(list_, items_to_remove)[source]¶
Remove items from a list and return the removed elements.
- Parameters:
list (list) – List containing items to remove
items_to_remove (list) – List of items to remove
- Returns:
reduced_list (list) – List with removed items
removed_items (dict) – Dictionary where the keys are the indices of removed items, and values are the items
- nabu.utils.restore_items_in_list(list_, removed_items)[source]¶
Undo the effect of the function remove_items_from_list
- Parameters:
list (list) – List where items were removed
removed_items (dict) – Dictionary where the keys are the indices of removed items, and values are the items
- nabu.utils.copy_dict_items(dict_, keys)[source]¶
Perform a shallow copy of a subset of a dictionary. The subset if done by a list of keys.
- nabu.utils.recursive_copy_dict(dict_)[source]¶
Perform a shallow copy of a dictionary of dictionaries. This is NOT a deep copy ! Only reference to objects are kept.
- nabu.utils.subdivide_into_overlapping_segment(N, window_width, half_overlap)[source]¶
Divide a segment into a number of possibly-overlapping sub-segments.
- Parameters:
N (int) – Total segment length
window_width (int) – Length of each segment
half_overlap (int) – Half-length of the overlap between each sub-segment.
- Returns:
segments – A list where each item is in the form (left_margin_start, inner_segment_start, inner_segment_end, right_margin_end)
- Return type:
list
- nabu.utils.get_num_threads(n=None)[source]¶
Get a number of threads (ex. to be used by fftw). If the argument is None, returns the total number of CPU threads. If the argument is negative, the total number of available threads plus this number is returned.
- Parameters:
n (int, optional) –
If an positive integer n is provided, then n threads are used
If a negative integer n is provided, then n_avail + n threads are used (so -1 to use all available threads minus one)
- class nabu.utils.DictToObj(dictio)[source]¶
Bases:
object
utility class to transform a dictionary into an object with dictionary items as members. Example:
>>> a=DictToObj( dict(i=1,j=1)) ... a.j+a.i
- nabu.utils.remove_parenthesis_or_brackets(input_str)[source]¶
clear string from left and or roght parenthesis / braquets
- nabu.utils.filter_str_def(elmt)[source]¶
clean elemt if is a string defined from a text file. Remove some character that could have be put on left or right and some empty spaces
- nabu.utils.convert_str_to_tuple(input_str: str, none_if_empty: bool = False) None | tuple [source]¶
- Parameters:
input_str (str) – string to convert
none_if_empty (bool) – if true and the conversion is an empty tuple return None instead of an empty tuple
- class nabu.utils.Progress(name: str)[source]¶
Bases:
object
Simple interface for defining advancement on a 100 percentage base
- reset(max_: None | int = None) None [source]¶
reset the advancement to n and max advancement to max_ :param int max_:
- nabu.utils.concatenate_dict(dict_1, dict_2) dict [source]¶
update dict which has dict as values. And we want concatenate those values to
- nabu.utils.median2(img)[source]¶
3x3 median filter for 2D arrays, with “reflect” boundary mode. Roughly same speed as scipy median filter, but more memory demanding.
- nabu.utils.docstring(origin)[source]¶
Decorator to initialize the docstring from another source.
This is useful to duplicate a docstring for inheritance and composition.
If origin is a method or a function, it copies its docstring. If origin is a class, the docstring is copied from the method of that class which has the same name as the method/function being decorated.
- Parameters:
origin – The method, function or class from which to get the docstring
- Raises:
ValueError – If the origin class has not method n case the