iblutil.numerical
Functions
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]). |
|
Computes a 2D histogram by aggregating values in a 2D array. |
|
Returns a hash of an array or list of UUID strings |
|
Performs intersection on multiple columns arrays a0 and a1 |
|
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] |
|
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, :] |
|
Computes pairwise Pearson correlation coefficients for matrices. |
|
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:
- hash_uuids(uuids, algo='sha256')[source]
Returns a hash of an array or list of UUID strings
- Parameters:
uuids – collection of UUID strings: list | np.ndarray | pd.Series | pd.Index
algo – hashing algorithm to use, default is sha256
- 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]
- Parameters:
a – 1d - array
b – 1d - array
- Returns:
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, :]
- Parameters:
a – 2d array
b – 2d array
- Returns:
isin, locb
- intersect2d(a0, a1, assume_unique=False)[source]
Performs intersection on multiple columns arrays a0 and a1
- Parameters:
a0
a1
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, :]
- bincount2D(x, y, xbin=0, ybin=0, xlim=None, ylim=None, weights=None)[source]
Computes a 2D histogram by aggregating values in a 2D array.
- Parameters:
x – values to bin along the 2nd dimension (c-contiguous)
y – values to bin along the 1st dimension
xbin – scalar: bin size along 2nd dimension 0: aggregate according to unique values array: aggregate according to exact values (count reduce operation)
ybin – scalar: bin size along 1st dimension 0: aggregate according to unique values array: aggregate according to exact values (count reduce operation)
xlim – (optional) 2 values (array or list) that restrict range along 2nd dimension
ylim – (optional) 2 values (array or list) that restrict range along 1st dimension
weights – (optional) defaults to None, weights to apply to each value for aggregation
- Returns:
3 numpy arrays MAP [ny,nx] image, xscale [nx], yscale [ny]
- rcoeff(x, y)[source]
Computes pairwise Pearson correlation coefficients for matrices.
That is for 2 matrices the same size, computes the row to row coefficients and outputs a vector corresponding to the number of rows of the first matrix. If the second array is a vector then computes the correlation coefficient for all rows.
- Parameters:
x – np array [nc, ns]
y – np array [nc, ns] or [ns]
- Returns:
r [nc]
- within_ranges(x: ndarray, ranges: ndarray | Sequence, labels: ndarray | Sequence | None = None, mode: str = 'vector', dtype: Type[D] = 'int8') 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[0] == x.size and out.shape[1] == max(labels) + 1.
Examples
# Assert that points in ranges are mutually exclusive np.all(within_ranges(x, ranges) <= 1)
Tests
>>> 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])