miv_simulator.utils.utils#

Functions

NamedTupleWithDocstring(docstring, *ntargs)

A convenience wrapper to add docstrings to named tuples.

add_bins(bins1, bins2, datatype)

apply_filter(data, sos)

autocorr(y, lag)

baks(spktimes, time[, a, b])

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]

butter_bandpass_filter(lowcut, highcut, fs)

compose_iter(f, it)

Given a function and an iterator, apply the function to each element in the iterator and return the element.

config_logging(verbose)

rtype:

None

consecutive(data)

Returns a list of arrays with consecutive values from data.

contiguous_ranges(condition[, return_indices])

Finds contiguous True regions of the boolean array "condition".

finalize_bins(bins, binsize)

flatten(iterables)

from_yaml(filepath)

rtype:

Dict

gauss2d([x, y, mx, my, sx, sy, A])

gaussian(x, mu, sig[, A])

generate_results_file_id(population[, gid, seed])

rtype:

str

generator_ifempty(iterable)

If the iterable is empty, return None, otherwise return the iterable with the first element attached back.

generator_peek(iterable)

If the iterable is empty, return None, otherwise return a tuple with the first element and the iterable with the first element attached back.

get_R2(y_test, y_pred)

Obtain coefficient of determination (R-squared, R2)

get_low_pass_filtered_trace(trace, t[, ...])

get_module_logger(name)

rtype:

Logger

get_root_logger()

rtype:

Logger

get_script_logger(name)

get_trial_time_indices(time_vec, n_trials[, ...])

get_trial_time_ranges(time_vec, n_trials[, ...])

rtype:

List[Tuple[float64, float64]]

ifempty(iterable)

If the iterable is empty, return None, otherwise return the a tuple with first element and iterable.

ifilternone(iterable)

imapreduce(iterable, fmap, freduce[, init])

is_iterable(obj)

list_argsort(f, seq)

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:

list_find(f, lst)

Find the index of the first element in the list that returns "true" value from the function f.

list_find_all(f, lst)

Find the index of all elements in the list that returns "true" value from the function f.

list_index(element, lst)

type element:

make_geometric_graph(x, y, z, edges)

Builds a NetworkX graph with xyz node coordinates and the node indices of the end nodes.

make_random_clusters(centers, ...[, ...])

Generate isotropic Gaussian blobs for clustering.

merge_bins(bins1, bins2, datatype)

mvcorrcoef(X, y)

Multivariate correlation coefficient.

partitionn(items[, predicate, n])

Filter an iterator into N parts lazily http://paddy3118.blogspot.com/2013/06/filtering-iterator-into-n-parts-lazily.html

print_param_dict_like_yaml(param_dict[, digits])

Assumes a flat dict with int or float values.

profile_memory(logger)

random_choice_w_replacement(ranstream, n, p)

type ranstream:

random_clustered_shuffle(centers, ...[, ...])

Generates a Gaussian random clustering given a number of cluster centers, samples per each center, optional integer center ids, and cluster standard deviation.

read_from_yaml(file_path[, include_loader])

type file_path:

str

rejection_sampling(gen, n, clip)

set_union(a, b, datatype)

signal_power_spectrogram(signal, fs, ...)

Computes the power spectrum of the specified signal.

signal_psd(s, Fs[, frequency_range, ...])

update_bins(bins, binsize, *xs)

viewattrs(obj)

viewvalues(obj, **kwargs)

Function for iterating over dictionary values with the same set-like behaviour on Py2.7 as on Py3.

write_to_yaml(file_path, data[, convert_scalars])

type file_path:

yaml_convert_scalars(data)

Traverses dictionary, and converts any scalar objects from numpy types to python types.

zip_longest(*args, **kwds)

rtype:

zip_longest

Classes

AbstractEnv()

Context([namespace_dict])

A container replacement for global variables to be shared and modified by any function in a module.

EnumChoice(enum[, case_sensitive, use_value])

ExplicitDumper(stream[, default_style, ...])

YAML dumper that will never emit aliases.

ExprClosure(parameters, expr[, consts, formals])

Representation of a sympy expression with a mutable local environment.

IncludeLoader(stream)

YAML loader with !include handler.

Promise(clos, args)

An object that represents a closure and unapplied arguments.

RunningStats()

Struct(**items)

class miv_simulator.utils.utils.AbstractEnv[source]#

Bases: ABC

class miv_simulator.utils.utils.ExprClosure(parameters, expr, consts=None, formals=None)[source]#

Bases: object

Representation of a sympy expression with a mutable local environment.

class miv_simulator.utils.utils.Promise(clos, args)[source]#

Bases: object

An object that represents a closure and unapplied arguments.

class miv_simulator.utils.utils.Context(namespace_dict=None, **kwargs)[source]#

Bases: object

A container replacement for global variables to be shared and modified by any function in a module.

update(namespace_dict=None, **kwargs)[source]#

Converts items in a dictionary (such as globals() or locals()) into context object internals. :type namespace_dict: None :param namespace_dict: dict

Return type:

None

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 param and ctx arguments may be None in 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: Loader

YAML loader with !include handler.

include(node)[source]#
Parameters:

node (ScalarNode) –

Return type:

Dict[str, Any]

Returns:

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: SafeDumper

YAML 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_index(element, lst)[source]#
Parameters:
  • element

  • lst

Returns:

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 evaluate

  • lst (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.mvcorrcoef(X, y)[source]#

Multivariate correlation coefficient.

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