neurotools.util.array module
Helper functions related to Numpy arrays and other indexing tasks.
- neurotools.util.array.lmap(function, *args)[source]
Map, collecting results immediately in a list. Also aliased as lap
- neurotools.util.array.amap(function, *args)[source]
Map, collecting results immediately in a Numpy array. This will try to create a numeric or boolean array first, and fall-back to an object array if a ValueError is encountered. Also aliased as aap.
- neurotools.util.array.lap(function, *args)
Map, collecting results immediately in a list. Also aliased as lap
- neurotools.util.array.aap(function, *args)
Map, collecting results immediately in a Numpy array. This will try to create a numeric or boolean array first, and fall-back to an object array if a ValueError is encountered. Also aliased as aap.
- neurotools.util.array.sls(x)
Convert iterable to sorted list. Also aliased as sls.
- neurotools.util.array.arraymap(f, *iterables, **kwargs)[source]
Map functionality over numpy arrays replaces depricated arraymap from pylab.
- neurotools.util.array.find(x)[source]
Replacement to Pylab’s lost find() function. Synonym for np.where(np.array(x).ravel())[0]
- Parameters:
x (np.array)
- neurotools.util.array.asiterable(x)[source]
Attempt to convert an iterable object to a list. This mat eventually be replaced with something fancier, but for now just calls list(iter(x)).
- Parameters:
x (iterable)
- Return type:
list
- neurotools.util.array.invert_permutation(p)[source]
Invert a a permutation
- Parameters:
x (list of ints) – Permutation to invert
- neurotools.util.array.ndargmax(x)[source]
Get coordinates of largest value in a multidimensional array
- Parameters:
x (np.array)
- neurotools.util.array.complex_to_nan(x, value=nan)[source]
Replce complex entries with NaN or other value
- Parameters:
x (np.array)
value (float; default np.NaN) – Value to replace complex entries with
- neurotools.util.array.axes_complement(x, axes)[source]
Set of all axes indeces for
x
not contained inaxes
.
- neurotools.util.array.reslice(naxes, expand_into)[source]
Generate a slice object to expand an array along
expand_into
to broadcast with an array withnaxes
dimenstions.
- neurotools.util.array.make_rebroadcast_slice(x, axis=0, verbose=False)[source]
Generate correct slice object for broadcasting stastistics averaged over the given axis(es) back to the original shape.
- Parameters:
x (np.array)
axis (int or tuple; default 0)
verbose (boolean; default False)
- neurotools.util.array.deep_tuple(x)[source]
Convert x to tuple, deeply. Defaults to the identity function if x is not iterable
- Parameters:
x (nested iterable)
- Returns:
x
- Return type:
nested iterable
- neurotools.util.array.deep_map(f, tree)[source]
Maps over a tree like structure
- Parameters:
f (function)
tree (nested iterable)
- Return type:
nested iterable
- neurotools.util.array.to_indices(x)[source]
There are two ways to extract a subset from numpy arrays: 1. providing a boolean array of the same shape 2. providing a list of indecies
This function is designed to accept either, and return a list of indecies.
- Parameters:
x (np.array)
- neurotools.util.array.onehot(ids, n=None, dense=False)[source]
Generate “one-hot” representation from integer class labels.
- Parameters:
ids (np.array)
n (int or None; default None) – Total number of labels. If None, will default to the largest value in ids.
dense (boolean; default False) – Whether to create missing labels. Ignored if the parameter n is specified.
- Returns:
labels – labels corresponding to each index
r – One-hot label format
- neurotools.util.array.zeroslike(x)[source]
Create numpy array of zeros the same shape and type as x
- Parameters:
x (np.array)
- neurotools.util.array.oneslike(x)[source]
Create numpy array of ones the same shape and type as x
- Parameters:
x (np.array)
- neurotools.util.array.split_into_groups(x, group_sizes)[source]
Split np.array x into len(group_sizes) groups, with the size of the groups specified by group_sizes.
This operates along the last axis of x
- Parameters:
x (np.array) – Numpy array to split; Last axis should have the same length as sum(group_sizes)
group_sizes (iterable of positive ints) – Group sizes
- Returns:
List of sub-arrays for each group
- Return type:
list
- neurotools.util.array.maybe_integer(x)[source]
Cast a float numpy array to int32 or int64, if it would not result in loss of precision.
- Parameters:
x (np.array)
- Returns:
x – np.int32 or np.int64 if possible, otherwise the original value of x.
- Return type:
np.array
- neurotools.util.array.widths_to_edges(widths, startat=0)[source]
Convert a list of widths into a list of edges delimiting consecutive bands of the given width
- Parameters:
widths (list of numbers) – Width of each band
startat (number, default 0) – Starting position of bands
- Returns:
edges
- Return type:
1D np.array
- neurotools.util.array.widths_to_limits(widths, startat=0)[source]
Convert a list of integer widths into a list of [a,b) indecies delimiting the concatenated width
- Parameters:
widths (list of integers) – Width of each band
startat (int; default 0) – Starting index
- Returns:
limits – List of [start,stop) indecies
- Return type:
N×2 np.int32
- neurotools.util.array.centers(edges)[source]
Get center of histogram bins given as a list of edges.
- Parameters:
edges (list of numbers) – Edges of histogram bins
- Returns:
centers – Center of histogram bins
- Return type:
1D np.array
- neurotools.util.array.widths_to_centers(widths, startat=0)[source]
Get centers of a consecutive collection of histogram widths.
- Parameters:
widths (list of numbers) – Width of each band
startat (number, default 0) – Starting position of bands