miv_simulator.utils.utils#
Functions
|
A convenience wrapper to add docstrings to named tuples. |
|
|
|
|
|
|
|
Bayesian Adaptive Kernel Smoother (BAKS) BAKS is a method for estimating firing rate from spike train data that uses kernel smoothing technique with adaptive bandwidth determined using a Bayesian approach ---------------INPUT--------------- - spktimes : spike event times [s] - time : time points at which the firing rate is estimated [s] - a : shape parameter (alpha) - b : scale parameter (beta) ---------------OUTPUT--------------- - rate : estimated firing rate [nTime x 1] (Hz) - h : adaptive bandwidth [nTime x 1] |
|
|
|
Given a function and an iterator, apply the function to each element in the iterator and return the element. |
|
|
|
Returns a list of arrays with consecutive values from data. |
|
Finds contiguous True regions of the boolean array "condition". |
|
|
|
|
|
|
|
|
|
|
|
|
|
If the iterable is empty, return None, otherwise return the iterable with the first element attached back. |
|
If the iterable is empty, return None, otherwise return a tuple with the first element and the iterable with the first element attached back. |
|
Obtain coefficient of determination (R-squared, R2) |
|
|
|
|
|
|
|
|
|
|
|
|
|
If the iterable is empty, return None, otherwise return the a tuple with first element and iterable. |
|
|
|
|
|
|
|
http://stackoverflow.com/questions/3382352/equivalent-of-numpy-argsort-in-basic-python/3383106#3383106 lambda version by Tony Veijalainen :type f: :param f: :type seq: :param seq: :return: |
|
Find the index of the first element in the list that returns "true" value from the function f. |
|
Find the index of all elements in the list that returns "true" value from the function f. |
|
|
|
Builds a NetworkX graph with xyz node coordinates and the node indices of the end nodes. |
|
Generate isotropic Gaussian blobs for clustering. |
|
|
|
Multivariate correlation coefficient. |
|
Filter an iterator into N parts lazily http://paddy3118.blogspot.com/2013/06/filtering-iterator-into-n-parts-lazily.html |
|
Assumes a flat dict with int or float values. |
|
|
|
|
|
Generates a Gaussian random clustering given a number of cluster centers, samples per each center, optional integer center ids, and cluster standard deviation. |
|
|
|
|
|
|
|
Computes the power spectrum of the specified signal. |
|
|
|
|
|
|
|
Function for iterating over dictionary values with the same set-like behaviour on Py2.7 as on Py3. |
|
|
|
Traverses dictionary, and converts any scalar objects from numpy types to python types. |
|
|
Classes
|
A container replacement for global variables to be shared and modified by any function in a module. |
|
|
|
YAML dumper that will never emit aliases. |
|
Representation of a sympy expression with a mutable local environment. |
|
YAML loader with !include handler. |
|
An object that represents a closure and unapplied arguments. |
|
|
|
- class miv_simulator.utils.utils.ExprClosure(parameters, expr, consts=None, formals=None)[source]#
Bases:
objectRepresentation of a sympy expression with a mutable local environment.
- class miv_simulator.utils.utils.Promise(clos, args)[source]#
Bases:
objectAn object that represents a closure and unapplied arguments.
- class miv_simulator.utils.utils.Context(namespace_dict=None, **kwargs)[source]#
Bases:
objectA container replacement for global variables to be shared and modified by any function in a module.
- class miv_simulator.utils.utils.EnumChoice(enum, case_sensitive=False, use_value=False)[source]#
Bases:
Choice- convert(value, param, ctx)[source]#
Convert the value to the correct type. This is not called if the value is
None(the missing value).This must accept string values from the command line, as well as values that are already the correct type. It may also convert other compatible types.
The
paramandctxarguments may beNonein certain situations, such as when converting prompt input.If the value cannot be converted, call
fail()with a descriptive message.- Parameters:
value – The value to convert.
param – The parameter that is using this type to convert its value. May be
None.ctx – The current context that arrived at this value. May be
None.
- class miv_simulator.utils.utils.IncludeLoader(stream)[source]#
Bases:
LoaderYAML loader with !include handler.
- class miv_simulator.utils.utils.ExplicitDumper(stream, default_style=None, default_flow_style=False, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, version=None, tags=None, sort_keys=True)[source]#
Bases:
SafeDumperYAML dumper that will never emit aliases.
- miv_simulator.utils.utils.write_to_yaml(file_path, data, convert_scalars=False)[source]#
- Parameters:
file_path – str (should end in ‘.yaml’)
data – dict
convert_scalars – bool
- Returns:
- miv_simulator.utils.utils.read_from_yaml(file_path, include_loader=None)[source]#
- Parameters:
file_path (
str) – str (should end in ‘.yaml’)- Return type:
Dict[str,Dict[str,Dict[str,Union[Dict[str,float],Dict[str,int]]]]]- Returns:
- miv_simulator.utils.utils.print_param_dict_like_yaml(param_dict, digits=6)[source]#
Assumes a flat dict with int or float values. :type param_dict: :param param_dict: dict :type digits: :param digits: int
- miv_simulator.utils.utils.yaml_convert_scalars(data)[source]#
Traverses dictionary, and converts any scalar objects from numpy types to python types. :type data: :param data: dict :return: dict
- miv_simulator.utils.utils.list_find(f, lst)[source]#
Find the index of the first element in the list that returns “true” value from the function f.
- Parameters:
f (
Callable) – Callable Function to evaluatelst (
List[str]) – List[str] List of elements
- Return type:
int- Returns:
int Index that satisfies f(x) where x is elements in lst.
- miv_simulator.utils.utils.list_find_all(f, lst)[source]#
Find the index of all elements in the list that returns “true” value from the function f.
- Parameters:
f – Callable Function to evaluate
lst – List[str] List of elements
- Returns:
List[int] List of indices that satisfies f(x) where x is elements in lst.
- miv_simulator.utils.utils.list_argsort(f, seq)[source]#
http://stackoverflow.com/questions/3382352/equivalent-of-numpy-argsort-in-basic-python/3383106#3383106 lambda version by Tony Veijalainen :type f: :param f: :type seq: :param seq: :return:
- miv_simulator.utils.utils.viewvalues(obj, **kwargs)[source]#
Function for iterating over dictionary values with the same set-like behaviour on Py2.7 as on Py3.
Passes kwargs to method.
- miv_simulator.utils.utils.consecutive(data)[source]#
Returns a list of arrays with consecutive values from data.
- miv_simulator.utils.utils.make_geometric_graph(x, y, z, edges)[source]#
Builds a NetworkX graph with xyz node coordinates and the node indices of the end nodes.
- Parameters:
- x: ndarray
x coordinates of the points
- y: ndarray
y coordinates of the points
- z: ndarray
z coordinates of the points
- edges: the (2, N) array returned by compute_delaunay_edges()
containing node indices of the end nodes. Weights are applied to the edges based on their euclidean length for use by the MST algorithm.
- Returns:
- g: A NetworkX undirected graph
Notes
We don’t bother putting the coordinates into the NX graph. Instead the graph node is an index to the column.
- miv_simulator.utils.utils.random_choice_w_replacement(ranstream, n, p)[source]#
- Parameters:
ranstream –
n –
p –
- Returns:
- miv_simulator.utils.utils.make_random_clusters(centers, n_samples_per_center, n_features=2, cluster_std=1.0, center_ids=None, center_box=(-10.0, 10.0), random_seed=None)[source]#
Generate isotropic Gaussian blobs for clustering.
- Parameters:
- centersint or array of shape [n_centers, n_features]
The number of centers to generate, or the fixed center locations.
- n_samples_per_centerint array
Number of points for each cluster.
- n_featuresint, optional (default=2)
The number of features for each sample.
- cluster_stdfloat or sequence of floats, optional (default=1.0)
The standard deviation of the clusters.
- center_idsarray of integer center ids, if None then centers will be numbered 0 .. n_centers-1
- center_boxpair of floats (min, max), optional (default=(-10.0, 10.0))
The bounding box for each cluster center when centers are generated at random.
- random_seedint or None, optional (default=None)
If int, random_seed is the seed used by the random number generator; If None, the random number generator is the RandomState instance used by np.random.
- Returns
- ——-
- Xarray of shape [n_samples, n_features]
The generated samples.
- yarray of shape [n_samples]
The integer labels for cluster membership of each sample.
- Examples
- ——–
- >>> X, y = make_random_clusters (centers=6, n_samples_per_center=np.array([1,3,10,15,7,9]), n_features=1, center_ids=np.array([10,13,21,25,27,29]).reshape(-1,1), cluster_std=1.0, center_box=(-10.0, 10.0))
- >>> print(X.shape)
- (45, 1)
- >>> y
- array([10, 13, 13, 13, …, 29, 29, 29])
- miv_simulator.utils.utils.random_clustered_shuffle(centers, n_samples_per_center, center_ids=None, cluster_std=1.0, center_box=(-1.0, 1.0), random_seed=None)[source]#
Generates a Gaussian random clustering given a number of cluster centers, samples per each center, optional integer center ids, and cluster standard deviation.
- Parameters:
- centersint or array of shape [n_centers]
The number of centers to generate, or the fixed center locations.
- n_samples_per_centerint array
Number of points for each cluster.
- cluster_stdfloat or sequence of floats, optional (default=1.0)
The standard deviation of the clusters.
- center_idsarray of integer center ids, if None then centers will be numbered 0 .. n_centers-1
- random_seedint or None, optional (default=None)
If int, random_seed is the seed used by the random number generator; If None, the random number generator is the RandomState instance used by np.random.
- >>> x = random_clustered_shuffle(centers=6,center_ids=np.array([10,13,21,25,27,29]).reshape(-1,1), n_samples_per_center=np.array([1,3,10,15,7,9]))
- >>> array([10, 13, 13, 25, 13, 29, 21, 25, 27, 21, 27, 29, 25, 25, 25, 21, 29,
27, 25, 21, 29, 25, 25, 25, 25, 29, 21, 25, 21, 29, 29, 29, 21, 25, 29, 21, 27, 27, 21, 27, 25, 21, 25, 27, 25])
- miv_simulator.utils.utils.NamedTupleWithDocstring(docstring, *ntargs)[source]#
A convenience wrapper to add docstrings to named tuples. This is only needed in python 2, where __doc__ is not writeable. https://stackoverflow.com/questions/1606436/adding-docstrings-to-namedtuples
- miv_simulator.utils.utils.partitionn(items, predicate=<class 'int'>, n=2)[source]#
Filter an iterator into N parts lazily http://paddy3118.blogspot.com/2013/06/filtering-iterator-into-n-parts-lazily.html
- Return type:
Iterator[Any]
- miv_simulator.utils.utils.generator_peek(iterable)[source]#
If the iterable is empty, return None, otherwise return a tuple with the first element and the iterable with the first element attached back.
- miv_simulator.utils.utils.generator_ifempty(iterable)[source]#
If the iterable is empty, return None, otherwise return the iterable with the first element attached back.
- Return type:
Optional[chain]
- miv_simulator.utils.utils.ifempty(iterable)[source]#
If the iterable is empty, return None, otherwise return the a tuple with first element and iterable.
- miv_simulator.utils.utils.compose_iter(f, it)[source]#
Given a function and an iterator, apply the function to each element in the iterator and return the element.
- miv_simulator.utils.utils.get_R2(y_test, y_pred)[source]#
Obtain coefficient of determination (R-squared, R2)
- Parameters:
- y_test - the true outputs (a matrix of size number of examples x number of features)
- y_pred - the predicted outputs (a matrix of size number of examples x number of features)
- Returns:
- An array of R2s for each feature
- miv_simulator.utils.utils.contiguous_ranges(condition, return_indices=False)[source]#
Finds contiguous True regions of the boolean array “condition”. Returns a list of ranges with the start and end index of each region. Code based on: https://stackoverflow.com/questions/4494404/find-large-number-of-consecutive-values-fulfilling-condition-in-a-numpy-array/4495197
- miv_simulator.utils.utils.signal_power_spectrogram(signal, fs, window_size, window_overlap)[source]#
Computes the power spectrum of the specified signal.
A Hanning window with the specified size and overlap is used.
- Parameters:
- signal: numpy.ndarray
The input signal
- fs: int
Sampling frequency of the input signal
- window_size: int
Size of the Hann windows in samples
- window_overlap: float
Overlap between Hann windows as fraction of window_size
- Returns:
- f: numpy.ndarray
Array of frequency values for the first axis of the returned spectrogram
- t: numpy.ndarray
Array of time values for the second axis of the returned spectrogram
- sxx: numpy.ndarray
Power spectrogram of the input signal with axes [frequency, time]
- miv_simulator.utils.utils.baks(spktimes, time, a=1.5, b=None)[source]#
Bayesian Adaptive Kernel Smoother (BAKS) BAKS is a method for estimating firing rate from spike train data that uses kernel smoothing technique with adaptive bandwidth determined using a Bayesian approach —————INPUT————— - spktimes : spike event times [s] - time : time points at which the firing rate is estimated [s] - a : shape parameter (alpha) - b : scale parameter (beta) —————OUTPUT————— - rate : estimated firing rate [nTime x 1] (Hz) - h : adaptive bandwidth [nTime x 1]
Based on “Estimation of neuronal firing rate using Bayesian adaptive kernel smoother (BAKS)” nurahmadi/BAKS