Design matrix constructor that will take in information about the temporal structure of a trial and allow the generation of a design matrix with specified regressors

class DesignMatrix(trialsdf, vartypes=None, binwidth=0.02)[source]

Bases: object

Design matrix constructor that will take in information about the temporal structure of a trial and allow the generation of a design matrix with specified regressors


Function to bin time t into binwidth of DM


t (float) – time, in seconds


number of time bins

Return type


add_covariate_timing(covlabel, eventname, bases, offset=0, deltaval=None, cond=None, desc='')[source]

Convenience wrapper for adding timing event regressors to the design matrix.

Timing events are regressed against using basis functions, such as those generated by the functions in this module, which are convolved with a kronecker delta at the time where the event occurred. This operation is effectively a copy/paste of the basis functions, each of which have their own column in the design matrix. This means that when we fit weights, a single weight can govern the prediction of the model over a longer time period.

Can be offset, such that the bases functions are applied before or after the timing.

The height of the bases can be modified by a column in the design matrix, which will then multiply the delta function which is convolved with the bases.

  • covlabel (str) – Label which the covariate will use. Can be accessed via dot syntax of the instance usually.

  • eventname (str) – Label of the column in trialsdf which has the event timing for each trial.

  • bases (numpy.array) – nTB x nB array, i.e. number of time bins for the bases functions by number of bases. Each column in the array is used together to describe the response of a unit to that timing event.

  • offset (float, seconds) – Offset of bases functions relative to timing event. Negative values mean bases will be applied before the timing event by that amount.

  • deltaval (None, str, or pandas series, optional) – Values of the kronecker delta function peak used to encode the event. If a string, the column in trialsdf with that label will be used. If a pandas series with indexes matching trialsdf, corresponding elements of the series will be the delta funtion val. If None (default) height is 1.

  • cond (None, list, or fun, optional) – Condition which to apply this covariate. Can either be a list of trial indices, or a function which takes in rows of the trialsdf and returns booleans.

  • desc (str, optional) – Additional information about the covariate, if desired. by default ‘’

add_covariate_boxcar(covlabel, boxstart, boxend, cond=None, height=None, desc='')[source]
add_covariate_raw(covlabel, raw, cond=None, desc='')[source]
add_covariate(covlabel, regressor, bases, offset=0, cond=None, desc='')[source]

Parent function to add covariates to model object. Takes a regressor in the form of a pandas Series object, a T x M array of M bases, and stores them for use in the design matrix generation.

  • covlabel (str) – Label for the covariate being added. Will be exposed, if possible, through (instance).(covlabel) attribute.

  • regressor (pandas.Series) – Series in which each element is the value(s) of a regressor for a trial at that index. These will be convolved with the bases functions (if provided) to produce the components of the design matrix. Regressor must be (T / dt) x 1 array for each trial

  • bases (numpy.array or None) – T x M array of M basis functions over T timesteps. Columns will be convolved with the elements of regressor to produce elements of the design matrix. If None, it is assumed a raw regressor is being used.

  • offset (int, optional) – Offset of the regressor from the bases during convolution. Negative values indicate that the firing of the unit will be , by default 0

  • cond (list or func, optional) – Condition for which to apply covariate. Either a list of trials which the covariate applies to, or a function of the form f(dataframerow) which returns a boolean, by default None

  • desc (str, optional) – Description of the covariate for reference purposes, by default ‘’ (empty)


Compiles design matrix for the current experiment based on the covariates which were added with the various NeuralGLM.add_covariate methods available. Can optionally compile a sparse design matrix using the scipy.sparse package, however that method may take longer depending on the degree of sparseness.


dense (bool, optional) – Whether or not to compute a dense design matrix or a sparse one, by default True

denseconv(X, bases)[source]
convbasis(stim, bases, offset=0)[source]