Skip to content

nabu.utils

source module nabu.utils

Classes

  • DictToObj utility class to transform a dictionary into an object with dictionary items as members. Example:

  • BaseClassError

Functions

source nextpow2(N, dtype=np.int32)

source previouspow2(N, dtype=np.int32)

source updiv(a, b)

source convert_index(idx, idx_max, default_val)

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.

source get_folder_path(foldername='')

source get_cuda_srcfile(filename)

source get_opencl_srcfile(filename)

source get_resource_file(filename, subfolder=None)

source indices_to_slices(indices)

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)]

source merge_slices(slice1, slice2)

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]

source compacted_views(slices_)

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)]

source get_size_from_sliced_dimension(length, slice_)

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

source get_shape_from_sliced_dims(shape, slices_)

Same as get_size_from_sliced_dimension() but in 3D

source get_available_threads()

source list_match_queries(available, queries)

Given a list of strings, return all items matching any of one elements of "queries"

source is_writeable(location)

Return True if a file/location is writeable.

source is_int(num, eps=1e-07)

source is_scalar(stuff)

source safe_format(str_, **kwargs)

Alternative to str.format(), but does not throw a KeyError when fields are missing.

source get_ftype(url)

return supposed filetype of an url

source get_2D_3D_shape(shape)

source get_subregion(sub_region)

Raises

  • ValueError

source get_3D_subregion(sub_region)

Raises

  • ValueError

source to_3D_array(arr)

Turn an array to a (C-Contiguous) 3D array with the layout (n_arrays, n_y, n_x).

source view_as_images_stack(img)

View an image (2D array) as a stack of one image (3D array). No data is duplicated.

source rescale_integers(items, new_tot)

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

source merged_shape(shapes, axis=0)

Raises

  • ValueError

source is_device_backend(backend)

source get_decay(curve, cutoff=1000.0, vmax=None)

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

source generate_powers()

Generate a list of powers of [2, 3, 5, 7], up to (215)*(39)(56)(7**5).

source calc_padding_lengths1D(length, length_padded)

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 : tuple A tuple under the form (padding_left, padding_right). These are the lengths needed to pad the original array.

source calc_padding_lengths(shape, shape_padded)

Multi-dimensional version of calc_padding_lengths1D. Please refer to the documentation of calc_padding_lengths1D.

source partition_dict(dict_, n_partitions)

source first_dict_item(dict_)

source first_generator_item(gen)

source subsample_dict(dic, subsampling_factor)

Subsample a dict where keys are integers.

source compare_dicts(dic1, dic2)

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: result which can be the following:

    • 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

Raises

  • ValueError

source remove_items_from_list(list_, items_to_remove)

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

source restore_items_in_list(list_, removed_items)

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

source search_sorted(arr, val)

Binary search that returns the "nearest" index given a query, i.e find "i" that minimizes abs(arr[i] - val) It does not return the "insersion point" contrarily to numpy.searchsorted() or bisect_left

source check_supported(param_value, available, param_desc)

Raises

  • ValueError

source check_supported_enum(param_value, enum_cls, param_desc)

source check_shape(shape, expected_shape, name)

Raises

  • ValueError

source copy_dict_items(dict_, keys)

Perform a shallow copy of a subset of a dictionary. The subset if done by a list of keys.

source recursive_copy_dict(dict_)

Perform a shallow copy of a dictionary of dictionaries. This is NOT a deep copy ! Only reference to objects are kept.

source subdivide_into_overlapping_segment(N, window_width, half_overlap)

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 : list A list where each item is in the form (left_margin_start, inner_segment_start, inner_segment_end, right_margin_end)

Raises

  • ValueError

source get_num_threads(n=None)

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)

source class DictToObj(dictio)

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

source remove_parenthesis_or_brackets(input_str)

clear string from left and or roght parenthesis / braquets

source filter_str_def(elmt)

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

source convert_str_to_tuple(input_str: str, none_if_empty: bool = False)

:param str input_str: string to convert :param bool none_if_empty: if true and the conversion is an empty tuple return None instead of an empty tuple

Raises

  • TypeError

source concatenate_dict(dict_1, dict_2)dict

update dict which has dict as values. And we want concatenate those values to

source class BaseClassError(*args, **kwargs)

Bases : BaseException

source MissingComponentError(msg)

Raises

  • RuntimeError

source generate_coords(img_shp, center=None)

source clip_circle(img, center=None, radius=None, out_value=0)

source extend_image_onepixel(img)

source median2(img)

3x3 median filter for 2D arrays, with "reflect" boundary mode. Roughly same speed as scipy median filter, but more memory demanding.

source no_decorator(func)

source measure_time(func)

source wip(func)

source warning(msg)

source deprecated(msg, do_print=False)

source deprecated_class(msg, do_print=False)

source deprecation_warning(message, do_print=True, func_name=None)

source docstring(origin)

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.

:param origin: The method, function or class from which to get the docstring :raises ValueError: If the origin class has not method n case the

source catch_warnings(*args, **kwargs)

source dummy(*args, **kwargs)