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.indices_to_slices(indices)[source]¶
From a series of integer indices, return corresponding slice() objects.
- Parameters:
indices (collection of sorted unique integers) – Arrays indices
Examples
slices_from_indices([0, 1, 2, 3]) returns [slice(0, 4)] slices_from_indices([8, 9, 10, 14, 15, 16]) returns [slice(8, 11), slice(15, 17)]
- nabu.utils.merge_slices(slice1, slice2)[source]¶
Merge two slicing operations in one.
Examples
array = numpy.arange(200) array[slice(133, 412, 2)][slice(31, 35, 2)] # gives [195 199] array[merge_slices(slice(133, 412, 2), slice(31, 35, 2))] # gives [195 199]
- nabu.utils.compacted_views(slices_)[source]¶
From a list of slice objects, returns the slice objects corresponding to a compact view.
- If “array” is obtained with
array = np.hstack([big_array[slice1], big_array[slice2]])
- Then, compacted_views([slice1, slice2]) returns [slice3, slice4] where
slice3 are the indices, in ‘array’, corresponding to indices of slice1 in ‘big_array’
slice4 are the indices, in ‘array’, corresponding to indices of slice2 in ‘big_array’
Example
compacted_views([slice(1, 26), slice(526, 551)]) gives [slice(0, 25), slice(25, 50)]
- nabu.utils.get_size_from_sliced_dimension(length, slice_)[source]¶
From a given array size, returns the size of the array once it is accessed using a slice.
Examples
If data.shape = (3500, 2160, 2560) get_size_from_sliced_dimension(data.shape[0], None) returns 3500 get_size_from_sliced_dimension(data.shape[0], slice(100, 200)) returns 100
- nabu.utils.get_shape_from_sliced_dims(shape, slices_)[source]¶
Same as get_size_from_sliced_dimension() but in 3D
- nabu.utils.list_match_queries(available, queries)[source]¶
Given a list of strings, return all items matching any of one elements of “queries”
- nabu.utils.safe_format(str_, **kwargs)[source]¶
Alternative to str.format(), but does not throw a KeyError when fields are missing.
- 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)[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
- 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