Machine Learning

NeuroPop

class spykes.ml.neuropop.NeuroPop(tunemodel='glm', n_neurons=100, random_state=1, eta=0.4, learning_rate=0.2, convergence_threshold=1e-05, maxiter=1000, n_repeats=1, verbose=False)[source]

Implements conveniences for plotting, fitting and decoding.

Implements convenience methods for plotting, fitting and decoding population tuning curves. We allow the fitting of two classes of parametric tuning curves.

Two types of models are available. The Generalized von Mises model by Amirikan & Georgopulos (2000) is defined by

\[ \begin{align}\begin{aligned}f(x) = b + g * exp(k * cos(x - mu))\\f(x) = b + g * exp(k1 * cos(x) + k2 * sin(x))\end{aligned}\end{align} \]

The Poisson generalized linear model is defined by

\[ \begin{align}\begin{aligned}f(x) = exp(k0 + k * cos(x - mu))\\f(x) = exp(k0 + k1 * cos(x) + k2 * sin(x))\end{aligned}\end{align} \]
Parameters:
  • tunemodel (str) – Can be either gvm, the Generalized von Mises model, or glm, the Poisson generalized linear model.
  • n_neurons (float) – Number of neurons in the population.
  • random_state (int) – Seed for numpy.random.
  • eta (float) – Linearizes the exponent above eta.
  • learning_rate (float) – The learning rate for fitting.
  • convergence_threshold (float) – The convergence threshold.
  • maxiter (float) – Max number of iterations.
  • n_repeats (float) – Number of repetitions.
  • verbose (bool) – Whether to print convergence and loss at each iteration.
decode(Y)[source]

Estimates the features that generated a given population activity.

Parameters:Y (float) – n_samples x n_neurons, population activity.
Returns:n_samples x 1, feature of interest.
Return type:float array
display(x, Y, neuron, colors=['k', 'c'], alpha=0.5, ylim=[0, 25], xlabel='direction [radians]', ylabel='firing rate [spk/s]', style='../mpl_styles/spykes.mplstyle', xjitter=False, yjitter=False)[source]

Visualize data and estimated tuning curves

Parameters:
  • x (float) – n_samples x 1, feature of interest.
  • Y (float) – n_samples x 1, firing rates.
  • neuron (int) – Which neuron’s fit to plot from the population?
  • colors (list of str) – Plot strings that specify color for raw data and fit.
  • alpha (float) – Transparency for raw data.
  • ylim (list of float) – Y axis limits.
  • xlabel (str) – X label (typically name of the feature).
  • ylabel (str) – Y label (typically firing rate).
  • style (str) – Name of the mpl style file to use with path.
  • xjitter (bool) – Whether to add jitter to x variable while plotting.
  • ylitter (bool) – Whether to add jitter to y variable while plotting.
fit(x, Y)[source]

Fits the parameters of the model.

Estimate the parameters of the tuning curve under the model specified by tunemodel(), given features and population activity.

Parameters:
  • x (float) – n_samples x 1, feature of interest.
  • Y (float) – n_samples x n_neurons, population activity.
predict(x)[source]

Predicts the firing rates for the population.

Computes the firing rates for the population based on the fit or specified tuning models.

Parameters:x (float) – n_samples x 1, feature of interest.
Returns:n_samples x n_neurons, population activity.
Return type:float array
score(Y, Yhat, Ynull=None, method='circ_corr')[source]

Scores the model.

Parameters:
  • Y (array) – The true firing rates, an array with shape (n_samples, n_neurons).
  • Yhat (array) – The estimated firing rates, an array with shape (n_samples, [n_neurons]).
  • Ynull (array or None) – The labels of the null model. Must be None if method is not pseudo_R2. The array has shape (n_samples, [n_classes]).
  • method (str) – One of pseudo_R2, circ_corr, or cosine_dist.
Returns:

The computed score.

Return type:

scalar float

set_params(tunemodel=None, neurons=None, mu=None, k0=None, k=None, g=None, b=None)[source]

A function that sets tuning curve parameters as specified.

If any of the parameters is None, it is randomly initialized for all neurons.

Parameters:
  • tunemodel (str) – Either ‘gvm’ or ‘glm’.
  • neurons (list) – A list of integers which specifies the subset of neurons to set.
  • mu (float) – len(neurons) x 1, feature of interest.
  • k0 (float) – len(neurons) x 1, baseline.
  • k (float) – len(neurons) x 1, gain.
  • g (float) – len(neurons) x 1, gain.
  • b (float) – len(neurons) x 1, baseline.
simulate(tunemodel, n_samples=500, winsize=200)[source]

Simulates firing rates from a neural population.

Simulates firing rates from a neural population by randomly sampling circular variables (feature of interest), as well as parameters (mu, k0, k, g, b).

Parameters:
  • tunemodel (str) – Can be either gvm, the Generalized von Mises model, or glm, the Poisson generalized linear model.
  • n_samples (int) – Number of samples required.
  • winsize (float) – Time interval in which to simulate spike counts, milliseconds.
Returns:

The simulation parameters.

  • x, n_samples x 1 array, features of interest
  • Y, n_samples x n_neurons array, population activity
  • mu, n_neurons x 1 array, preferred feature, [-pi, pi]; k0, n_neurons x 1, baseline
  • k, n_neurons x 1 array, shape (width)
  • g, n_neurons x 1 array, gain
  • b, n_neurons x 1, baseline

Return type:

tuple

STRF

class spykes.ml.strf.STRF(patch_size=100, sigma=0.5, n_spatial_basis=25, n_temporal_basis=3)[source]

Allows the estimation of spatiotemporal receptive fields

Parameters:
  • patch_size (int) – Dimension of the square patch spanned by the spatial basis.
  • sigma (float) – Standard deviation of the Gaussian distribution.
  • n_spatial_basis (int) – Number of spatial basis functions for the Gaussian basis (has to be a perfect square).
  • n_temporal_basis (int) – Number of temporal basis functions.
convolve_with_temporal_basis(design_matrix, temporal_basis)[source]

Convolves a design matrix with a temporal basis function.

Convolves each column of the design matrix with a series of temporal basis functions.

Parameters:
  • design_matrix (numpy array) – 2D array of size (n_samples, n_features).
  • temporal_basis (numpy array) – 2D array of size (n_basis, n_timepoints).
Returns:

2D array of size (n_samples, n_features * n_basis).

Return type:

numpy array

design_prior_covariance(sigma_temporal=2.0, sigma_spatial=5.0)[source]

Design a prior covariance matrix for STRF estimation.

Parameters:
  • sigma_temporal (float) – Standard deviation of temporal prior covariance.
  • sigma_spatial (float) – Standard deviation of spatial prior covariance.
Returns:

2-d array of size (n_spatial_basis * n_temporal_basis, n_spatial_basis * n_temporal_basis), the ordering of rows and columns is so that all temporal basis are consecutive for each spatial basis.

Return type:

numpy array

make_2d_gaussian(center=(0, 0))[source]

Makes a 2D Gaussian filter with arbitary mean and variance.

Parameters:center (tuple) – The coordinates of the center of the Gaussian, specified as (row, col). The center of the image is (0, 0).
Returns:The Gaussian mask.
Return type:numpy array
make_cosine_basis()[source]

Makes a spatial cosine and sine basis.

Returns:A list where each entry is a 2D array of size (patch_size, patch_size) specifing the spatial basis.
Return type:list
make_gaussian_basis()[source]

Makes a list of Gaussian filters.

Returns:A list where each entry is a 2D array of size (patch_size, patch_size) specifing the spatial basis.
Return type:list
make_image_from_spatial_basis(basis, weights)[source]

Recovers an image from a basis given a set of weights.

Parameters:
  • spatial_basis (list) – A list where each entry is a 2D array of size (patch_size, patch_size) specifing the spatial basis.
  • weights (numpy array) – A 1D array of coefficients.
Returns:

2D array of size (patch_size, patch_size), the resulting image.

Return type:

numpy array

make_raised_cosine_temporal_basis(time_points, centers, widths)[source]

Makes a series of raised cosine temporal basis.

Parameters:
  • time_points (numpy array) – List of time points at which the basis function is computed.
  • centers (numpy array or list) – List of coordinates at which each basis function is centered 1D array of size (n_temporal_basis).
  • widths (numpy array or list) – List of widths, one per basis function; each is a 1D array of size (n_temporal_basis).
Returns:

2D array of size (n_basis, n_timepoints).

Return type:

numpy array

project_to_spatial_basis(image, spatial_basis)[source]

Projects a given image into a spatial basis.

Parameters:
  • image (numpy array) – Image that must be projected into the spatial basis 2D array of size (patch_size, patch_size).
  • spatial_basis (list) – A list where each entry is a 2D array of size (patch_size, patch_size) specifing the spatial basis.
Returns:

A 1D array of coefficients for the projection.

Return type:

numpy array

visualize_gaussian_basis(spatial_basis, color='Greys', show=True)[source]

Plots spatial basis functions in a tile of images.

Parameters:
  • spatial_basis (list) – A list where each entry is a 2D array of size (patch_size, patch_size) specifing the spatial basis.
  • color (str) – The color for the figure.
  • show (bool) – Whether or not to show the image when it is plotted.

Sparse Filtering

This module makes use of Tensorflow. Make sure your system is configured correctly before using it.

class spykes.ml.tensorflow.sparse_filtering.SparseFiltering(model, layers=None)[source]

Defines a class for performing sparse filtering on a dataset.

The MATLAB code on which this is based is available here, from the paper Sparse Filtering by Ngiam et. al..

Parameters:
  • model (Keras model) – The trainable model, which takes as inputs the data you are training on and outputs a feature vector, which is minimized according to the loss function described above.
  • layers (str or list of str) – An optional name or list of names of layers in the provided model whose outputs to apply the sparse filtering loss to. If none are provided, the sparse filtering loss is applied to each layer in the model.
compile(optimizer, freeze=False, **kwargs)[source]

Compiles the model to create all the submodels.

Parameters:
  • optimizer (str or list of str) – The optimizer to use. If a list is provided, it must specify one optimizer for each layer passed to the constructor.
  • freeze (bool) – If set, for each submodel, all the previous layers are frozen, so that only the last layer is “learned”.
  • kwargs (dict) – Extra arguments to be passed to the compile function of each submodel.
fit(x, epochs=1, **kwargs)[source]

Fits the model to the provided data.

Parameters:
  • x (Numpy array) – An array where the first dimension is the batch size, and the remaining dimensions match the input dimensions of the provided model.
  • epochs (int or list of ints) – The number of epochs to train. If a list is provided, there must be one value for each named layer, specifying the number of epochs to train that layer for.
Returns:

A list of histories, the training history for each submodel.

Return type:

list

spykes.ml.tensorflow.sparse_filtering.sparse_filtering_loss(_, y_pred)[source]

Defines the sparse filtering loss function.

Parameters:
  • y_true (tensor) – The ground truth tensor (not used, since this is an unsupervised learning algorithm).
  • y_pred (tensor) – Tensor representing the feature vector at a particular layer.
Returns:

The sparse filtering loss.

Return type:

scalar tensor

Poisson Layers

This module provides a TensorFlow implementation of the Poisson estimators used in the NeuroPop modules.

class spykes.ml.tensorflow.poisson_models.PoissonLayer(model_type, num_neurons, num_features=None, mu_initializer=<tensorflow.python.ops.init_ops.RandomUniform object>, k_initializer=<tensorflow.python.ops.init_ops.RandomNormal object>, g_initializer=<tensorflow.python.ops.init_ops.RandomNormal object>, b_initializer=<tensorflow.python.ops.init_ops.RandomNormal object>, k0_initializer=<tensorflow.python.ops.init_ops.RandomNormal object>, **kwargs)[source]

Defines a TensorFlow implementation of the NeuroPop layers.

Two types of models are available. The Generalized von Mises model by Amirikan & Georgopulos (2000) is defined by

\[ \begin{align}\begin{aligned}f(x) = b + g * exp(k * cos(x - mu))\\f(x) = b + g * exp(k1 * cos(x) + k2 * sin(x))\end{aligned}\end{align} \]

The Poisson generalized linear model is defined by

\[ \begin{align}\begin{aligned}f(x) = exp(k0 + k * cos(x - mu))\\f(x) = exp(k0 + k1 * cos(x) + k2 * sin(x))\end{aligned}\end{align} \]
Parameters:
  • model_type (str) – Can be either gvm, the Generalized von Mises model, or glm, the Poisson generalized linear model.
  • num_neurons (int) – Number of neurons in the population (being inferred from the input features).
  • num_features (int) – Number of input features. Convenience parameter for for setting the input shape.
  • mu_initializer (Keras initializer) – The initializer for the mu.
  • k_initializer (Keras initializer) – The initializer for the k.
  • g_initializer (Keras initializer) – The initializer for the g. If model_type is glm, this is ignored.
  • b_initializer (Keras initializer) – The initializer for the b. If model_type is glm, this is ignored.
  • k0_initializer (Keras initializer) – The initializer for the k0. If model_type is gvm, this is ignored.