models

counter()
atleast_2d(arr)
check_response_dimensionality(train, test[, …]) Make sure matrices are 2D arrays before running models
clean_results_dict(results) Make sure we return arrays, and ndim is at least 2D
crossval_stem_wmvnp((features_train, …[, …]) Cross-validation procedure for spatio-temporal encoding models with MVN priors.
cvridge(Xtrain, Ytrain[, Xtest, Ytest, …]) Cross-validation procedure for tikhonov regularized regression.
dual2primal_weights(kernel_weights, …[, …]) Recover the feature weights from the kernel weights for any one or all feature spaces.
dual2primal_weights_banded(kernel_weights, …) WIP.
estimate_simple_stem_wmvnp(features_train, …) Estimate model with given hyper-parameters
estimate_stem_wmvnp((features_train, …[, …])
featurespace_dual2primal(kernel_weights, …)
find_optimum_mvn(response_cvmean, …)
generalized_tikhonov(X, Y, Li[, ridge]) Implementation fo tikhonov regression using the standard transform (cf.
hyperopt_crossval_stem_wmvnp(features_train, …) Use hyperopt to cross-validate all hyper-parameters parameters.
hyperopt_estimate_stem_wmvnp(features_train, …)
hyperopt_trials2cvperf(Trials)
kernel_banded_temporal_prior(kernel, …)
kernel_cvridge(Ktrain, Ytrain[, Ktest, …])
kernel_spatiotemporal_prior(Xtrain, …[, …]) Compute the kernel matrix of a model with a spatio-temporal prior
loo_ols(xtrain_samples, ytrain_samples[, rcond]) Leave-one out OLS Return the mean weight across head-out folds
nan_to_num(\*args, \*\*kwargs)
ols(X, Y[, rcond]) Perform OLS fit, return weight estimates
olspred(X, Y[, Xtest]) Fit OLS, return predictions Yhat
should_solve_dual(X, kernel) Answer whether we should solve the regression problem in the dual (kernel) space.
simple_generalized_tikhonov(X, Y, L[, ridge]) Direct implementation of generalized tikhonov regression
simple_ridge_dual(X, Y[, ridge]) Return weights for linear kernel ridge regression
simple_ridge_primal(X, Y[, ridge]) Return weights for ridge regression
solve_l2(Xtrain, Ytrain[, Xtest, Ytest, …]) Solve L2 regularized regression problem
solve_l2_dual(Ktrain, Ytrain[, Ktest, …]) Solve the dual (kernel) L2 regression problem for each L2 parameter.
solve_l2_primal(Xtrain, Ytrain[, Xtest, …]) Solve the (primal) L2 regression problem for each L2 parameter.
voxelwise_weights2preds(kernel_weights, …) feature_prior : one per voxel! temporal_prior : one for all voxels
zscore(\*args, \*\*kwargs)

counter

class tikreg.models.counter

Bases: object

__init__(self)

x.__init__(…) initializes x; see help(type(x)) for signature

update(self)

atleast_2d

tikreg.models.atleast_2d(arr)

check_response_dimensionality

tikreg.models.check_response_dimensionality(train, test, allow_test_none=True)

Make sure matrices are 2D arrays before running models

clean_results_dict

tikreg.models.clean_results_dict(results)

Make sure we return arrays, and ndim is at least 2D

crossval_stem_wmvnp

crossval_stem_wmvnp(features_train, responses_train, ridges=array([ 1. , 2.15443469, 4.64158883, 10. ,
21.5443469 , 46.41588834, 100. , 215.443469 ,
464.15888336, 1000. ]), temporal_prior=None, feature_priors=None, population_mean=False, folds=(1, 5), method='SVD', verbosity=1, chunklen=True, kernel_features=False, normalize_kernel=False, normalize_hyparams=False, metric='correlation', zscore_ytrain=False, zscore_yval=False, weights=False, predictions=False)

Cross-validation procedure for spatio-temporal encoding models with MVN priors.

cvridge

tikreg.models.cvridge(Xtrain, Ytrain, Xtest=None, Ytest=None, ridges=[0.0], Li=None, kernel_name='linear', kernel_params=None, folds='cv', nfolds=5, blocklen=5, trainpct=0.8, verbose=True, EPS=1e-10, withinset_test=False, performance=False, predictions=False, weights=False, kernel_weights=False, metric='correlation')

Cross-validation procedure for tikhonov regularized regression.

Parameters:
Xtrain (n, p):

Design matrix

Ytrain (n, v):

Training set responses

Xtest (None, (m, p)):

Design matrix for held-out set

Ytest (None, (m, v)):

Held-out set responses

ridges (r,):

Ridge parameters to evaluate

Li (q,p):

Generalized tikhonov regression. This solves the problem with a prior on $eta$ determined by $L^ op L$. The problem is solved in the standard form and in kernel space if necessary.

kernel_name (str):

Kernel to use

kernel_params (None, (k,)):

Kernel parameters to cross-validate

folds (str, list):
  • (str) Type of cross-validation
    • ‘cv’ - cross-validation with chunks of size blocklen
    • ‘nbb’ - block boostrap with chunks of size blocklen
    • ‘mbb’ - moving/overlapping block bootstrap chunks of size blocklen
  • (list) Can also be a list of (train, test) pairs: [(trn1, test1),…]
nfolds (int):

Number of learning folds

blocklen (int):

Chunk data into blocks of this size, and sample these blocks

trainpct (float 0-1):

Percentage of data to use in training if using a bootstrap sampler.

withinset_test (bool):

If no Xtest or Ytest is given and predictions and/or performance are requested, compute these testues based on training set.

performance (bool):

Held-out prediction performance

predictions (bool):

Held-out timecourse predictions

weights (bool):

Weight estimates on training set (does not depend on (Xtest,``Ytest``)

kernel_weights (bool):

Whether to project kernel weights into feature weights. If True, the kernel weights are returned. This is useful when fitting large models and storing the feature weights is expensive.

verbose (bool):

Verbosity

EPS (float):

Testue used to threshold small eigentestues

Returns:
fit (optional; dict):
cross-validation results per response for each fold, kernel and L2 parameters
  • cvresults (nfolds, len(kernel_params), len(ridges), nresponses)

If a held-out set (Xtest and Ytest) is specified, performs the fit on the full training set with the optimal L2 and kernel parameters. It returns, as requested, any of:

  • predictions (m, v) Ytest prediction for each voxel
  • performance (v,) correlation coefficient of predictions and Ytest
  • weights: (p, v) for linear models, (n by v) for non-linear models

dual2primal_weights

tikreg.models.dual2primal_weights(kernel_weights, features_train, feature_priors, feature_hyparams, temporal_prior, temporal_hhparam=1.0)

Recover the feature weights from the kernel weights for any one or all feature spaces.

Parameters:
kernel_weights : 2D np.ndarray
features_train : list of np.ndarrays

Training set feature spaces each of shape (n, p_i).

feature_priors : list of SpatialPrior objects

One feature prior per feature space.

feature_hyparams : list of scalars

Scalar for each feature space prior

temporal_prior : TemporalPrior object

A temporal prior object to use. The temporal prior may contain a hyper-prior.

temporal_hhparam : scalar

Hyper-prior hyperparameter if required. Defaults to 1.0, no effect.

Returns:
weights : list

Feature space weights [(p_1, v), …, (p_l, v)]

dual2primal_weights_banded

tikreg.models.dual2primal_weights_banded(kernel_weights, feature_space_train, population_feature_prior, temporal_prior, delays_mean=False, verbose=True)

WIP. WILL CHANGE. USE AT OWN RISK.

estimate_simple_stem_wmvnp

tikreg.models.estimate_simple_stem_wmvnp(features_train, responses_train, features_test=None, responses_test=None, feature_priors=None, feature_hyparams=None, temporal_prior=None, temporal_hhparam=1.0, ridge_scale=1.0, weights=False, performance=False, predictions=False, kernel_features=False, method='SVD', verbosity=2, metric='correlation')

Estimate model with given hyper-parameters

Parameters:
features_train : list of np.ndarrays

Training set feature spaces each of shape (n, p_i).

responses_train : 2D np.ndarray

Population responses to the training set (n, v).

features_test : list of np.ndarrays

Test set feature spaces each of shape (m, p_i)

responses_test : 2D np.ndarray

Population responses to the test set (m, v)

temporal_prior : TemporalPrior object

A temporal prior object to use. The temporal prior may contain a hyper-prior.

temporal_hhparam : scalar

Hyper-prior hyperparameter if required. Defaults to 1.0, no effect.

feature_priors : list of SpatialPrior objects

One feature prior per feature space.

feature_hyparams : list of scalars

Scalar for each feature space prior

ridge_scale : scalar

Scalar on the feature prior hyper-parameters. Defaults to 1.0, no effect

weights : bool

Compute weights

performance : bool

Compute accuracy of predictions against test set responses

predictions: boot

Test set prediction time courses

kernel_features : boot

If True, features_train and features_test is a list of kernels, one per feature space. This is only allowed if feature_priors are spherical.

method : str {“SVD”, “Chol”}

Solver to use

Returns:
fit : dictionary

Estimated model dictionary with keys: * weights : [(p_1, v), …, (p_l, v)] * performance : (1, v) * predictions : (m, v)

estimate_stem_wmvnp

estimate_stem_wmvnp(features_train, responses_train, features_test=None, responses_test=None, ridges=array([ 1. , 2.15443469, 4.64158883, 10. ,
21.5443469 , 46.41588834, 100. , 215.443469 ,
464.15888336, 1000. ]), normalize_hyparams=False, normalize_kernel=False, temporal_prior=None, feature_priors=None, weights=False, predictions=False, performance=False, folds=(1, 5), method='SVD', verbosity=1, cvresults=None, population_optimal=False, keep_cvfolds=True, chunklen=True, metric='correlation')

featurespace_dual2primal

tikreg.models.featurespace_dual2primal(kernel_weights, feature_space, feature_prior, feature_hyparam, temporal_prior, temporal_hhparam=1.0)

find_optimum_mvn

tikreg.models.find_optimum_mvn(response_cvmean, temporal_hhparams, spatial_hyparams, ridge_hyparams)

generalized_tikhonov

tikreg.models.generalized_tikhonov(X, Y, Li, ridge=10.0)

Implementation fo tikhonov regression using the standard transform (cf. Hansen, 1998).

hyperopt_crossval_stem_wmvnp

tikreg.models.hyperopt_crossval_stem_wmvnp(features_train, responses_train, features_test=None, responses_test=None, temporal_prior=None, feature_priors=None, spatial_sampler=True, temporal_sampler=False, ridge_sampler=False, population_optimal=False, folds=(1, 5), method='SVD', ntrials=100, verbosity=1, dumpcrossval=False, normalize_hyparams=False, normalize_kernel=False, weights=False, predictions=False, performance=True, metric='correlation', zscore_ytrain=True, zscore_yval=True, search_algorithm='tpe', trials=None, **kwargs)

Use hyperopt to cross-validate all hyper-parameters parameters.

Search the hyper-parameter space to find the population optimum using a cross-validation procedure.

Parameters:
features_train : list of np.ndarrays

The feature spaces of shape (n, p_i).

responses_train : 2D np.ndarray

The population responses to fit (n, v).

temporal_prior : TemporalPrior object

A temporal prior object to use. The temporal prior may contain a hyper-prior.

feature_priors : list of ``SpatialPrior``bjects

One feature prior per feature space.

spatial_sampler : hyperopt.hp, or bool

Specifies how to sample the hyperparameter space. Defaults to hp.loguniform(0,7).

temporal_sampler : hyperopt.hp, or bool

Used iff temporal_prior has a hyper-prior set.

ridge_sampler : hyperopt.hp, or bool

Defaults to False. Use this with caution. Specifies how to sample the scaling on the spatial hyperparameters. However, Specifying a spatial_sampler for all feature spaces and a ridge_sampler is redundant.

population_optimal : bool

If True, individual response cross-validation values are not kept. Only the mean across responses is stored.

folds : tuple (N,K), or list of tuples [(trn1, val1),…, (trnK, valK)]

If tuple, the second element corresponds to the number of cross-validation folds. The first element determines how many times to repeat the cross validation. (1,5) is standard 5-folds cross-validation. (10,5) performs 5-fold cross-validation 10 times.

method : str (“SVD”, “Chol”)

Solver to use

ntrials : int

Number of hyperopt iterations

verbosity : int (1, 2)

Level of print statements

dumpcrossval : function(iteration_num, crossval_dict)

Save the cross-validation results for every iteration. The function takes the iteration number (int) and a dictionary containing the cross-validation results. This is useful for finding the optimum hyper-parameters for each response. The stored data contains all the info. Defaults to False.

kwargs : dict

Additional arguments passed to crossval_stem_wmvnp.

Returns:
crossval_results : hyperopt.Trials object

Contains the cross-validation results from hyperopt.

hyperopt_estimate_stem_wmvnp

tikreg.models.hyperopt_estimate_stem_wmvnp(features_train, responses_train, cvmean, hyparams, features_test=None, responses_test=None, temporal_prior=None, feature_priors=None, spatial_sampler=True, temporal_sampler=False, ridge_sampler=False, population_optimal=False, method='SVD', verbosity=1, normalize_hyparams=False, normalize_kernel=False, weights=False, predictions=False, performance=True, kernel_features=False, metric='correlation', **kwargs)

hyperopt_trials2cvperf

tikreg.models.hyperopt_trials2cvperf(Trials)

kernel_banded_temporal_prior

tikreg.models.kernel_banded_temporal_prior(kernel, temporal_prior, spatial_prior, delays)

kernel_cvridge

tikreg.models.kernel_cvridge(Ktrain, Ytrain, Ktest=None, Ytest=None, ridges=[0.0], folds='cv', nfolds=5, blocklen=5, trainpct=0.8, performance=False, predictions=False, weights=False, metric='correlation', verbose=True, EPS=1e-10)

kernel_spatiotemporal_prior

tikreg.models.kernel_spatiotemporal_prior(Xtrain, temporal_prior, spatial_prior, Xtest=None, delays=None)

Compute the kernel matrix of a model with a spatio-temporal prior

temporal_prior (d, d): d = len(delays)

loo_ols

tikreg.models.loo_ols(xtrain_samples, ytrain_samples, rcond=1e-08)

Leave-one out OLS Return the mean weight across head-out folds

Parameters:
ytrain_samples : np.ndarray (nfolds, time, voxels)
xtrain_samples : np.ndarray (nfolds, time, features)

nan_to_num

tikreg.models.nan_to_num(*args, **kwargs)

ols

tikreg.models.ols(X, Y, rcond=1e-08)

Perform OLS fit, return weight estimates

olspred

tikreg.models.olspred(X, Y, Xtest=False)

Fit OLS, return predictions Yhat

should_solve_dual

tikreg.models.should_solve_dual(X, kernel)

Answer whether we should solve the regression problem in the dual (kernel) space.

simple_generalized_tikhonov

tikreg.models.simple_generalized_tikhonov(X, Y, L, ridge=10.0)

Direct implementation of generalized tikhonov regression

simple_ridge_dual

tikreg.models.simple_ridge_dual(X, Y, ridge=10.0)

Return weights for linear kernel ridge regression

simple_ridge_primal

tikreg.models.simple_ridge_primal(X, Y, ridge=10.0)

Return weights for ridge regression

solve_l2

tikreg.models.solve_l2(Xtrain, Ytrain, Xtest=None, Ytest=None, ridge=0.0, verbose=False, kernel_name='linear', kernel_param=None, kernel_weights=False, **kwargs)

Solve L2 regularized regression problem

solve_l2_dual

tikreg.models.solve_l2_dual(Ktrain, Ytrain, Ktest=None, Ytest=None, ridges=[0.0], method='SVD', EPS=1e-10, verbose=False, performance=False, predictions=False, weights=False, metric='correlation')

Solve the dual (kernel) L2 regression problem for each L2 parameter.

solve_l2_primal

tikreg.models.solve_l2_primal(Xtrain, Ytrain, Xtest=None, Ytest=None, ridges=[0], method='SVD', zscore_ytrain=False, zscore_ytest=False, EPS=1e-10, verbose=False, performance=False, predictions=False, weights=False, metric='correlation')

Solve the (primal) L2 regression problem for each L2 parameter.

voxelwise_weights2preds

tikreg.models.voxelwise_weights2preds(kernel_weights, kernel_test, responses_test, feature_prior, feature_hyparam, temporal_prior, temporal_hyparam=1.0, verbose=True, metric='correlation')

feature_prior : one per voxel! temporal_prior : one for all voxels

zscore

tikreg.models.zscore(*args, **kwargs)