nstat.Trial
- class nstat.Trial(spike_collection=None, covariate_collection=None, events=None, hist=None, ensCovHist=None, ensCovMask=None, *, spikeColl=None, covarColl=None, event=None)[source]
Bases:
objectSingle-trial data container binding spikes, covariates, and events (Matlab
Trial).A
Trialenforces consistent time bounds and sample rate across its spike collection, covariate collection, and optional event stream. It provides the design-matrix construction used byAnalysisto fit point-process GLMs.- Parameters:
spike_collection (SpikeTrainCollection) – Neural spike data.
covariate_collection (CovariateCollection) – Stimulus or task covariates.
events (Events, optional) – Discrete event markers.
hist (History or array_like, optional) – Self-history specification.
ensCovHist (History or array_like, optional) – Ensemble-history specification.
ensCovMask (array_like, optional) – Binary mask for ensemble neighbours.
spikeColl (SpikeTrainCollection | None)
covarColl (CovariateCollection | None)
event (Events | None)
See also
- __init__(spike_collection=None, covariate_collection=None, events=None, hist=None, ensCovHist=None, ensCovMask=None, *, spikeColl=None, covarColl=None, event=None)[source]
Construct a Trial bundling spikes, covariates, and events (Matlab
Trial).- Parameters:
spike_collection (SpikeTrainCollection) – Neural spike data. Required. Also accepted as the MATLAB-style keyword
spikeColl=.covariate_collection (CovariateCollection) – Stimulus or task covariates. Required. Also accepted as the MATLAB-style keyword
covarColl=.events (Events, optional) – Discrete event markers (e.g. trial onsets). Also accepted as the MATLAB-style keyword
event=.hist (History or array_like, optional) – Self-history specification. May be a
Historyobject or a vector of window boundary times (seconds). An empty list /Noneis the “unset” sentinel for MATLAB parity.ensCovHist (History or array_like, optional) – Ensemble-history specification (history terms aggregated across neighbour neurons).
ensCovMask (array_like, optional) – Binary mask selecting which neighbours contribute ensemble history.
spikeColl (SpikeTrainCollection | None)
covarColl (CovariateCollection | None)
event (Events | None)
- Return type:
None
Notes
The constructor enforces consistent time bounds (in seconds) and sample rate (in Hz) across the spike collection and covariate collection. Resampling is performed automatically when the two disagree.
- Raises:
ValueError – If spike_collection or covariate_collection is missing or of the wrong type.
- Parameters:
spike_collection (SpikeTrainCollection | None)
covariate_collection (CovariateCollection | None)
events (Events | None)
hist (object | None)
ensCovHist (object | None)
ensCovMask (object | None)
spikeColl (SpikeTrainCollection | None)
covarColl (CovariateCollection | None)
event (Events | None)
- Return type:
None
See also
- property spike_collection: SpikeTrainCollection
The trial’s spike-train collection.
- property covariate_collection: CovariateCollection
The trial’s covariate collection.
- property spikeColl: SpikeTrainCollection
Alias for
spike_collection(Matlab compat).
- property numCov: int
Number of covariates in the trial’s covariate collection (delegates to
CovariateCollection.numCov).
- property numSpikeTrains: int
Number of spike trains in the trial’s spike collection (delegates to
SpikeTrainCollection.numSpikeTrains).
- setTrialEvents(event)[source]
Attach an
Eventsobject (orNoneto clear).- Parameters:
event (Events | None)
- Return type:
None
- property covarColl: CovariateCollection
- setTrialPartition(partitionTimes)[source]
Set training and validation time windows from a 3- or 4-element array.
- Return type:
None
- getTrialPartition()[source]
Return
[trainMin, trainMax, valMin, valMax]partition times.- Return type:
- setTrialTimesFor(partitionName='training')[source]
Set trial time bounds to either the
'training'or'validation'window.- Parameters:
partitionName (str)
- Return type:
None
- setMinTime(minTime=None)[source]
Set minimum time across spikes, covariates, and ensemble covariates.
- Parameters:
minTime (float | None)
- Return type:
None
- setMaxTime(maxTime=None)[source]
Set maximum time across spikes, covariates, and ensemble covariates.
- Parameters:
maxTime (float | None)
- Return type:
None
- updateTimePartitions()[source]
Clamp training/validation windows to current min/max time.
- Return type:
None
- plotRaster(handle=None)[source]
Plot only the spike raster for this trial.
- Parameters:
handle (matplotlib Figure or Axes, optional) – If an
Axesis provided the raster is drawn there. If aFigureis provided a new axes is added. If None a new figure is created.- Return type:
- plotCovariates(handle=None)[source]
Plot covariates (and events, if set) for this trial.
Layout adapts to the number of active covariates, following the Matlab
Trial.plotCovariatesbehaviour.- Parameters:
handle (matplotlib Figure, optional) – Figure to draw on. If None a new figure is created.
- Return type:
- plot(*_, handle=None, **__)[source]
Plot spike raster, covariates, and events in a multi-panel figure.
- Return type:
- setSampleRate(sampleRate)[source]
Resample spikes, covariates, and ensemble covariates to sampleRate.
- Parameters:
sampleRate (float)
- Return type:
None
- resample(sampleRate)[source]
Alias for
setSampleRate().- Parameters:
sampleRate (float)
- Return type:
None
- setEnsCovMask(mask=None)[source]
Set the ensemble-covariate neighbour mask (default: all-to-all minus self).
- Return type:
None
- setCovMask(mask)[source]
Set the covariate mask;
'all'resets to full visibility.- Return type:
None
- setNeuronMask(mask)[source]
Set the neuron (spike-train) mask and sync to
self.neuronMask.- Return type:
None
- setNeighbors(*args)[source]
Set the neighbour structure for ensemble-history covariates.
- Return type:
None
- setEnsCovHist(hist=None)[source]
Set the ensemble-covariate history and rebuild the ensemble collection.
- Parameters:
hist (History or array-like, optional) – A
Historyobject or window-edge array. PassingNoneclears the ensemble history and removes theensCovColl.- Return type:
None
- isCovMaskSet()[source]
Return
Trueif any covariate dimension is currently masked out.- Return type:
- getNumHist()[source]
Return the number of history coefficients.
If a single
Historyobject is set, returns the number of history window coefficients (len(windowTimes) - 1). If a list ofHistoryobjects is set, returns a list with the count for each. Returns0when no history is set.Matches Matlab
Trial.getNumHist().
- addCov(cov)[source]
Add a covariate and enforce consistent sample rate / time bounds.
- Parameters:
cov (Covariate)
- Return type:
None
- getSpikeVector(*args, neuron_index=1)[source]
Return the spike data as a column matrix.
- Parameters:
*args – When empty, returns all neurons via
dataToMatrix(). An int selects a single neuron (0-based). A sequence of bin edges returns binned counts for the neuron given by neuron_index.neuron_index (int, default 1) – Neuron to bin when args provides bin edges (0-based).
- Return type:
- get_covariate_matrix(selected_covariates=None)[source]
Return
(time, data, names)for the covariate collection.
- getDesignMatrix(neuronNum, dataSelector=None)[source]
Build the full design matrix for neuron neuronNum (0-based).
Horizontally concatenates covariates, spike-history columns, and ensemble-history columns — the complete regressor matrix used by the GLM fitter.
- getHistForNeurons(neuronIndex)[source]
Compute the spike-history covariates for one neuron.
- Parameters:
neuronIndex (int) – 0-based neuron index whose spike train supplies the history.
- Returns:
Collection of history-basis covariates aligned to the trial time grid.
- Return type:
- getHistMatrices(neuronIndex)[source]
Return the spike-history columns as a 2-D array for neuronIndex (0-based).
- getEnsembleNeuronCovariates(*args)[source]
Delegate to
SpikeTrainCollection.getEnsembleNeuronCovariates.
- getEnsCovMatrix(neuronNum, includedNeurons=None)[source]
Return the ensemble-covariate design-matrix columns for neuronNum.
Uses
ensCovMaskto exclude self-history and applies neighbour filtering when includedNeurons is not specified.
- getNumUniqueNeurons()[source]
Return the number of distinct neuron names in the collection.
- Return type:
- getNeuronIndFromName(neuronName)[source]
Return 0-based indices matching neuronName, filtered by the neuron mask.
- Parameters:
neuronName (str)
- neuronMask_indices()[source]
Return 0-based indices of unmasked neurons (alias for
getNeuronIndFromMask).
- getNeuronNeighbors(neuronNum=None)[source]
Return the neighbour list for neuronNum (defaults to all unmasked neurons).
- getCovSelectorFromMask()[source]
Return the per-covariate selector list derived from the current mask.
- getEnsCovLabelsFromMask(neuronNum)[source]
Return ensemble-covariate labels for neuronNum, filtered by
ensCovMask.
- getAllLabels()[source]
Return all covariate + history + ensemble labels (no mask filtering).
Matlab equivalent:
Trial.getAllLabels.
- getLabelsFromMask(neuronNum)[source]
Return all design-matrix labels for neuronNum, respecting masks.
- flattenCovMask()[source]
Flatten the per-covariate mask list into a single 1-D int array.
- Return type:
- flattenMask()[source]
Flatten the full mask (covariates + history + ensemble) into 1-D.
- Return type:
- shiftCovariates(*args)[source]
Apply a time shift to covariates and re-synchronize time bounds.
- Return type:
None
- resetEnsCovMask()[source]
Reset the ensemble-covariate mask to the default (all-to-all minus self).
- Return type:
None
- resampleEnsColl()[source]
Rebuild the ensemble-covariate collection at the current sample rate.
- Return type:
None
- restoreToOriginal()[source]
Reset all collections to their original state and re-synchronize.
- Return type:
None
- static fromStructure(structure)[source]
Reconstruct a Trial from a dict produced by
toStructure()(MatlabTrial.fromStructure).
- makeConsistentSampleRate()[source]
Resample all collections to the maximum sample rate found.
- Return type:
None
- makeConsistentTime()[source]
Set all collections to the union of min/max time across sub-collections.
- Return type:
None
- isSampleRateConsistent()[source]
Return
Trueif spike and covariate collections share the same sample rate.- Return type:
- findMaxSampleRate()[source]
Return the maximum sample rate across spike and covariate collections.
- Return type: