# brainbox.numerical¶

Functions

 `between_sorted` Given a vector of sorted values, returns a boolean vector indices True when the value is between bounds. If multiple bounds are given, returns the equivalent OR of individual bounds tuple Especially useful for spike times indices = between_sorted(spike_times, [tstart, tstop]) :param sorted_v: vector containing sorted values (won’t check) :param bounds: minimum included value and maximum included value can be a list[tstart, tstop] or an array of dimension (n, 2) :return:. `find_first_2d` Returns first index where The purpose of this function is performance: uses low level numba and avoids looping through the full array :param mat: np.array :param val: values to search for :return: index or empty array `intersect2d` Performs intersection on multiple columns arrays a0 and a1 :param a0: :param a1: :param assume_unique: If True, the input arrays are both assumed to be unique, which can speed up the calculation. `ismember` equivalent of np.isin but returns indices as in the matlab ismember function returns an array containing logical 1 (true) where the data in A is B also returns the location of members in b such as a[lia] == b[locb] :param a: 1d - array :param b: 1d - array :return: isin, locb `ismember2d` Equivalent of np.isin but returns indices as in the matlab ismember function returns an array containing logical 1 (true) where the data in A is B also returns the location of members in b such as a[lia, :] == b[locb, :] :param a: 2d array :param b: 2d array :return: isin, locb `within_ranges` Detects which points of the input vector lie within one of the ranges specified in the ranges.
`between_sorted`(sorted_v, bounds=None)[source]

Given a vector of sorted values, returns a boolean vector indices True when the value is between bounds. If multiple bounds are given, returns the equivalent OR of individual bounds tuple Especially useful for spike times

indices = between_sorted(spike_times, [tstart, tstop])

Parameters
• sorted_v – vector containing sorted values (won’t check)

• bounds – minimum included value and maximum included value can be a list[tstart, tstop] or an array of dimension (n, 2)

Returns

`ismember`(a, b)[source]

equivalent of np.isin but returns indices as in the matlab ismember function returns an array containing logical 1 (true) where the data in A is B also returns the location of members in b such as a[lia] == b[locb] :param a: 1d - array :param b: 1d - array :return: isin, locb

`ismember2d`(a, b)[source]

Equivalent of np.isin but returns indices as in the matlab ismember function returns an array containing logical 1 (true) where the data in A is B also returns the location of members in b such as a[lia, :] == b[locb, :] :param a: 2d array :param b: 2d array :return: isin, locb

`intersect2d`(a0, a1, assume_unique=False)[source]

Performs intersection on multiple columns arrays a0 and a1 :param a0: :param a1: :param assume_unique: If True, the input arrays are both assumed to be unique, which can speed up the calculation. :return: intersection :return: index of a0 such as intersection = a0[ia, :] :return: index of b0 such as intersection = b0[ib, :]

`find_first_2d`(mat, val)[source]

Returns first index where The purpose of this function is performance: uses low level numba and avoids looping through the full array :param mat: np.array :param val: values to search for :return: index or empty array

`within_ranges`(x: numpy.ndarray, ranges: Union[numpy.ndarray, Sequence], labels: Optional[Union[numpy.ndarray, Sequence]] = None, mode: str = 'vector', dtype: Type[D] = 'int8') → numpy.ndarray[source]

Detects which points of the input vector lie within one of the ranges specified in the ranges. Returns an array the size of x with a 1 if the corresponding point is within a range.

The function uses a stable sort algorithm (timsort) to find the edges within the input array. Edge behaviour is inclusive.

Ranges are [(start0, stop0), (start1, stop1), etc.] or n-by-2 numpy array. The ranges may be optionally assigned a row in ‘matrix’ mode or a numerical label in ‘vector’ mode. Labels must have a length of n. Overlapping ranges have a value that is the sum of the relevant range labels (ones in ‘matrix’ mode).

If mode is ‘vector’ (default) it will give a vector, specifying the range of each point. If mode is ‘matrix’ it will give a matrix output where each range is assigned a particular row index with 1 if the point belongs to that range label. Multiple ranges can be assigned to a particular row, e.g. [0, 0,1] would give a 2-by-N matrix with the first two ranges in the first row. Points within more than one range are given a value > 1

Parameters
• x (array_like) – An array whose points are tested against the ranges. multi-dimensional arrays are flattened to 1D

• ranges (array_like) – A list of tuples or N-by-2 array of ranges to test, where N is the number of ranges, i.e. [[start0, stop0], [start1, stop1]]

• labels (vector, list) – If mode is ‘vector’; a list of integer labels to demarcate which points lie within each range. In ‘matrix’ mode; a list of column indices (ranges can share indices). The number of labels should match the number of ranges. If None, ones are used for all ranges.

• mode ({'matrix', 'vector'}) – The type of output to return. If ‘matrix’ (default), an N-by-M matrix is returned where N is the size of x and M corresponds to the max index in labels, e.g. with labels=[0,1,2], the output matrix would have 3 columns. If ‘vector’ a vector the size of x is returned.

• dtype (str, numeric or boolean type) – The data type of the returned array. If type is bool, the labels in vector mode will be ignored. Default is int8.

Returns

• A vector of size like x where zeros indicate that the points do not lie within ranges (

• ‘vector’ mode) or a matrix where out.shape == x.size and out.shape == max(labels) + 1.

Examples

# Assert that points in ranges are mutually exclusive np.all(within_ranges(x, ranges) <= 1)

```>>> import numpy as np
>>> within_ranges(np.arange(11), [(1, 2), (5, 8)])
array([0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0], dtype=int8)
>>> ranges = np.array([[1, 2], [5, 8]])
>>> within_ranges(np.arange(10) + 1, ranges, labels=np.array([0,1]), mode='matrix')
array([[1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0]], dtype=int8)
>>> within_ranges(np.arange(11), [(1,2), (5,8), (4,6)], labels=[0,1,1], mode='matrix')
array([[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 2, 2, 1, 1, 0, 0]], dtype=int8)
>>> within_ranges(np.arange(10) + 1, ranges, np.array([3,1]), mode='vector')
array([3, 3, 0, 0, 1, 1, 1, 1, 0, 0], dtype=int8)
>>> within_ranges(np.arange(11), [(1,2), (5,8), (4,6)], dtype=bool)
array([False,  True,  True, False,  True,  True,  True,  True,  True,
False, False])
```