## Description¶

Functions to deal with numpy arrays:

anynan(a)[source]

Returns whether `a` has at least one `nan`

Fastest implementation for arrays with >10^4 elements https://stackoverflow.com/a/45011547/5622825

arange_len(wmin, wmax, wstep) int[source]

Returns len of a `numpy.arange()` `(wmin, max, wstep)` array, accounting for floating point errors

Note: `numpy.arange()` is useful to maintain the input `wstep`. If you don’t have this requirement, you better use `numpy.linspace()` directly.

array_allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=True)[source]

Returns wheter a and b are all close (element wise). If not the same size, returns False (instead of crashing like the numpy version). Cf numpy.allclose docs for more information.

Parameters
• a, b (arrays)

• rtol (float)

• atol (float)

• equal_nan (bool) – whether to consider Nan’s as equal. Contrary to the numpy version this one is set to True by default

autoturn(data, key=-1)[source]

Turns array data. key value:

• `0` don’t transpose

• `1` : transpose

• `-1` : auto : make sure the vectors are along the longest dimension

bining(I, ymin=None, ymax=None, axis=1)[source]

Averages a I multi-dimensional array (typically an image) along the y axis bining(I) corresponds to I.mean(axis=1) Nan are not taken into account.

Parameters
• I (numpy array) – intensity

• ymin (int [0-I.shape]) – If None, 0 is used. Default `None`.

• ymax (int [0-I.shape]) – If None, I.shape is used. Default `None`.

• axis (int) – Default 1

boolean_array_from_ranges(ranges, n)[source]

return a boolean array of length `n` where (`L[i]`, `L[i]`) give the ranges set to `True`

Examples

```L = np.array([[2,4], [5,6], [7,8]])
boolean_array_from_coordinates(*L.T, 8)
>>> np.array([0, 0, 1, 1, 0, 1, 0, 1], dtype=bool)
```
calc_diff(t1, v1, t2, v2)[source]

Substract two vectors that may have slightly offset abscisses interpolating the correct values.

Parameters
• t1, v1 (array_like) – first vector and its abscisses

• t2, v2 (array_like) – second vector and its abscisses

Returns

tdiff, vdiff – substracted vector and its abscisses

Return type

array_like

centered_diff(w)[source]

Return w[i+1]-w[i-1]/2, same size as w.

Similar to `numpy.diff()`, but does not change the array size.

count_nans(a)[source]

Nan are good but only in India.

evenly_distributed(w, atolerance=1e-05)[source]

Make sure array `w` is evenly distributed.

Parameters
• w (numpy array) – array to test

• atolerance (float) – absolute tolerance

Returns

out`True` or `False` if `w` is evenly distributed.

Return type

bool

evenly_distributed_fast(w, rtolerance=1e-05)[source]

Make sure array `w` is evenly distributed, by looking only at the first and last steps. i.e:

```w[-1] - w[-2] == w - w
```
Parameters
• w (numpy array) – array to test

• rtolerance (float) – relative tolerance

Returns

out`True` or `False` if `w` is evenly distributed.

Return type

bool

find_first(arr, treshold)[source]

Return the index of the first element of the array arr whose value is more than the treshold.

find_nearest(array, searched, return_bool=False)[source]

Return the closest elements in array for each element in ‘searched’ array. In case of multiple elements in `array` having equal difference with `searched` element, one with least index is returned. Also returns a boolean array with indices of elements occuring in output list set to true.

Examples

```from numpy import array
find_nearest(array([1,2,3,4]), array([2.1,2]), True)

>>> (array([2, 2]), array([False, True, False, False]))

find_nearest(np.array([1,2,3,4]), np.array([2.6,2]), True)

>>> (array([3, 2]), array([False,  True,  True, False]))

find_nearest(np.array([1, 3]), np.array())

>>> array()

find_nearest(np.array([3, 1]), np.array())

>>> array()
```
first_nonnan_index(a)[source]

Returns index of first non-nan value in `a`

Returns None is all values are `nan`

is_sorted(a)[source]

Returns whether `a` is sorted in ascending order.

From B.M. answer on StackOverflow: https://stackoverflow.com/a/47004533/5622825

is_sorted_backward(a)[source]

Returns whether `a` is sorted in descending order.

last_nonnan_index(a)[source]

Returns index of first non-nan value in `a`

Returns None is all values are `nan`

logspace(xmin, xmax, npoints)[source]

Returns points from xmin to xmax regularly distributed on a logarithm space.

Numpy’s `numpy.logspace()` does the same from 10**xmin to 10**xmax

nantrapz(I, w, dx=1.0, axis=-1)[source]

Returns `trapz()` (I, w) discarding nan.

non_zero_ranges_in_array(b)[source]

return a list of coordinates corresponding to non-zero values in boolean array `b`

Parameters

b (boolean array)

Examples

```b = np.array([0,0,1,1,0,1,0,1], dtype=bool)
non_zero_ranges_in_array(b)
>> ([2, 5, 7],
[4, 6, 8])
```
non_zero_values_around(a, n)[source]

return a boolean array of same size as `a` where each position `i` is `True` if there are non-zero points less than `n` index position away from `a[i]`, and `False` if all points in `a` are 0 `n` index position away from from `a[i]`

norm(a, normby=None, how='max')[source]

Normalize a numpy array with its maximum. Or normalize it with another vector. Works if array contains nans.

Parameters

normby (array, or None) – if array, norm with this other array’s maximum. If None, normalize with its maximum.

norm_on(a, w, wmin=None, wmax=None, how='max')[source]

Normalize `a` on a specific range of `w`

Parameters
• a (array) – array

• w (array) – x-axis array

Other Parameters
• wmin, wmax (float) – crop range

• how (‘mean’, ‘max’) – how to normalize

Returns

a_norm – normalized array

Return type

array

numpy_add_at(LDM, k, l, m, I)[source]

Add the linestrengths on the LDM grid.

Uses the numpy implementation of `at()`, which add arguments element-wise.

Parameters
• LDM (ndarray) – LDM grid

• k, l, m (array) – index

• I (array) – intensity to add

Returns

add – linestrengths distributed over the LDM grid

Return type

ndarray

Notes

`numpy.add.at()`

scale_to(a, b, k=1)[source]

Scale function a to k*b.

sparse_add_at(ki0, Iv0, Iv1, weight, max_range, truncation_pts)[source]

Returns (start, stop) of non-zero ranges, and intensity `I` (length `truncation_pts`) for all line intensities `Iv0` and `Iv1` at position `ki0` and `ki0+1` with weights `weight`

Warning

`ki0` must be sorted.