backpack.arraymanip

Useful functions for everyday use —> array and iterables

brixs.backpack.arraymanip.sort(ref, *args)

Returns sorted arrays based on a reference array.

Parameters:
  • ref (list or array) – 1D array.

  • *args – array to sort.

Returns:

sorted arrays.

brixs.backpack.arraymanip.moving_average(x, n)

Returns the moving average of an array.

Parameters:
  • x (list or array) – 1D array.

  • n (int) – number of points to average.

Returns:

array of length given by (len(x)-n+1).

Example

>>> x = [0,1,2,3,4,5,6,7,8,9]
>>> print(am.moving_average(x, 1))
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
>>> print(am.moving_average(x, 2))
[0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5]
>>> print(am.moving_average(x, 3))
[1. 2. 3. 4. 5. 6. 7. 8.]
>>> print(am.moving_average(x, 4))
[1.5 2.5 3.5 4.5 5.5 6.5 7.5]
brixs.backpack.arraymanip.derivative(x, y, order=1)

Returns the derivative of y-coordinates as a function of x-coordinates.

Parameters:
  • x (list or array) – 1D array x-coordinates.

  • y (list or array) – 1D array y-coordinates.

  • order (number, optional) – derivative order.

Returns:

x and y arrays.

brixs.backpack.arraymanip.shifted(x, y, value, mode='hard')

Shift (x, y) data.

Parameters:
  • x (list or array) – 1D array.

  • y (list or array) – 1D array.

  • value (float or int) – shift value.

  • mode (string, optional) –

    1. mode='x' or mode='hard'

      y is fully preserved while x is shifted.

    2. mode='y', 'interp', or 'soft'

      x is preserved while y is interpolated with a shift

    3. mode='roll'` or ``r,

      x and y are preserved and y elements are just rolled along the array (in this case shift value must be an integer).

Returns:

Shifted x and y.

Warning

It is always better to use mode='hard' or 'roll' since the form of y is fully preserved (no interpolation). After applying a shift using the mode='interp', one can apply a ‘inverse’ shift to retrieve the original data. The diference between the retrieved y data and the original data will give an ideia of the information loss caused by the interpolation.

brixs.backpack.arraymanip.transpose(array)

Returns transposed lists/arrays.

Differently from numpy.transpose(), this function also transposes 1D arrays/lists.

brixs.backpack.arraymanip.mask(x, mask)

Returns a reduced array based on mask.

Usage:

mask(‘ABCDEF’, [1, 0, 1, 0, 1, 1]) –> A C E F

Parameters:
  • x (Iterable) – array

  • mask (list) – list with bools (True, False, …)

Returns:

reduced array

brixs.backpack.arraymanip.index(x, value, closest=True)

Returns the first index of the element in array.

Parameters:
  • x (list or array) – 1D array.

  • value (float or int) – value.

  • closest (book, optional) – if True, returns the index of the element in array which is closest to value.

Returns:

index (int)

brixs.backpack.arraymanip.all_equal(array)

Returns True if all elements of an array are equal.

brixs.backpack.arraymanip.has_duplicates(array)

Returns True if given array contains any duplicates.

brixs.backpack.arraymanip.remove_duplicates(array)

Returns the sorted unique elements of an array. Wrapper for np.unique()

brixs.backpack.arraymanip.check_monotonicity(array)

return 1 (-1) if increas. (decre.) monotonic or 0 if not monotonic.

brixs.backpack.arraymanip.fix_monotonicity(x, y, mode='increasing')

return x, y where the x array is monotonically increasing or decreasing.

brixs.backpack.arraymanip.choose(x, limits)

Return a mask of x values inside range pairs.

Parameters:
  • x (list or array) – 1d array.

  • limits (list) – a pair of values or a list of pairs. Each pair represents the start and stop of a data range from x. [[xi, xf], [xi_2, xf_2], …]

Raises:

RuntimeError – if limits have overlapping intervals

Returns:

1d list.

brixs.backpack.arraymanip.extract(x, y, limits, invert=False)

Returns specific data limits from x and y.

Parameters:
  • x (list or array) – 1D reference vector.

  • y (list or array) – 1D y-coordinates or list of several data sets.

  • limits (list) – a pair of values or a list of pairs. Each pair represents the start and stop of a data range from x.

  • invert (bool, optional) – if inverted is True, data outside of the data will be returned. Default is False.

Returns:

x and y arrays. If y is 1d, the returned y is 1d. If y is a multicolumn array then the returned y is also multicolumn

Examples

if y is 1d, the returned y is 1d:

>>> x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> y = np.array(x)**2
>>> ranges = ((0, 3), (7.5, 9))
>>> x_sliced, y_sliced = am.extract(x, y, ranges)
>>> print(x_sliced)
[0 1 2 3 8 9]
>>> print(y_sliced)
[0 1 4 9 64 81]

if y is multicolumn, the returned y is also multicolumn:

>>> x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> y = np.zeros((10, 2))
>>> y[:, 0] = x**2
>>> y[:, 0] = x**3
>>> ranges = ((0, 3), (7.5, 9))
>>> x_sliced, y_sliced = am.extract(x, y, ranges)
>>> print(x_sliced)
[0. 1. 2. 3. 8. 9.]
>>> print(y_sliced)
[[  0.   0.]
 [  1.   0.]
 [  8.   0.]
 [ 27.   0.]
 [512.   0.]
 [729.   0.]]
brixs.backpack.arraymanip.flatten(x)

[EXPERIMENTAL] Return a copy of the array collapsed into one dimension.