nstat.nstColl
- class nstat.nstColl(trains=None)[source]
Bases:
SpikeTrainCollectionMATLAB-facing spike-train collection class.
- Parameters:
trains (Sequence[nspikeTrain] | nspikeTrain | None)
- __init__(trains=None)
Construct an ordered spike-train collection (Matlab
nstColl).- Parameters:
trains (nspikeTrain, sequence of nspikeTrain, or None) – Initial spike train(s) to add.
Nonecreates an empty collection.- Return type:
None
Notes
On construction the collection captures the maximal sample rate across all input trains (in Hz) and the union of their observation windows (in seconds), and initialises the neuron mask to all ones (no neurons masked out).
Indexing is 0-based to match MATLAB (
coll.getNST(0)is the first spike train). Trains added after construction are deep-copied (vianspikeTrain.nstCopy()) to prevent shared mutable state.See also
nspikeTrainSingle-neuron point-process representation.
TrialCombines a
SpikeTrainCollectionwith covariates.
- addNeuronNamesToEnsCovColl(ensembleCovariates)
Prefix ensemble-covariate labels with their neuron name.
- Parameters:
ensembleCovariates (CovariateCollection)
- Return type:
None
- addSingleSpikeToColl(nst)
Append a single spike train (deep-copied) to the collection.
- Parameters:
nst (nspikeTrain)
- Return type:
None
- addToColl(nst)
Add one or more spike trains (or another collection) to this collection.
- Parameters:
nst (Sequence[nspikeTrain] | nspikeTrain | 'SpikeTrainCollection')
- Return type:
None
- dataToMatrix(selectorArray=None, binwidth=None, minTime=None, maxTime=None)
Return an
(nTimeBins, nNeurons)binary spike-count matrix.
- enforceSampleRate()
Resample any train whose rate differs from the collection rate.
Accesses
self.nstraindirectly (not viagetNST) because the intent is in-place mutation of the stored trains.- Return type:
None
- ensureConsistancy()
Enforce consistent sample rate and time bounds across all trains.
- Return type:
None
- estimateVarianceAcrossTrials(numBasis=None, windowTimes=None, numIter=None, fitType=None)
Estimate the state-noise covariance
Qfrom bootstrap GLM fits.Used internally by
ssglm()/ssglmFB()to initialise the EM algorithm’s state-noise prior.
- static fromStructure(structure)
Reconstruct from a dict produced by
toStructure()(MatlabnstColl.fromStructure).
- static generateUnitImpulseBasis(basisWidth, minTime, maxTime, sampleRate=1000.0)
Create a piecewise-constant (unit impulse) basis
Covariate.Each column is a rectangular pulse spanning one basisWidth interval, used as the design matrix for GLM-PSTH estimation.
- get(name=None)
Return spike train(s) by neuron name (Matlab
nstColl.get).Mirrors MATLAB’s
get.uniqueNeuronNamesproperty accessor, which delegates togetUniqueNSTnames(). When name isNonethe list of unique neuron names is returned; when a name is supplied the corresponding spike train(s) are returned viagetNSTFromName().- Parameters:
name (str | None)
- getEnsembleNeuronCovariates(neuronNum=1, neighborIndex=None, windowTimes=None)
Build ensemble-history covariates for neuronNum from its neighbours.
- Parameters:
neuronNum (int)
- getFieldVal(fieldName)
Collect a named field from every spike train (Matlab
nstColl.getFieldVal).- Parameters:
fieldName (str)
- getISIs(selectorArray=None, minTime=None, maxTime=None)
Return a list of ISI arrays, one per selected neuron.
- getIndFromMask()
Return 0-based indices of neurons currently enabled by the mask.
- getIndFromMaskMinusOne(neuron)
Return active indices excluding neuron (0-based).
- getMaxBinSizeBinary()
Return the largest bin-width that keeps all active trains binary.
- Return type:
- getMinISIs(selectorArray=None, minTime=None, maxTime=None)
Return the minimum ISI for each selected neuron.
- getNST(idx)
Return spike train(s) by 0-based index (Matlab
nstColl.getNST).Always returns a deep copy of the stored train (never the stored reference itself). This is intentional — the PR #80
non-destructivecontract forgetNSTwas previously implemented ascopy-only-when-rates-differ, which meant callers in the common path got a reference and could silently mutate the stored train via attribute assignment. All in-place collection-wide operations (e.g.enforceSampleRate) accessself.nstraindirectly and bypass this method.- Return type:
- getNSTFromName(neuronName=None)
Return spike train(s) matching the given neuron name(s).
- getNSTIndicesFromName(name)
Return 0-based index(es) for a neuron name (or list of names).
- getNSTnameFromInd(ind)
Return the neuron name for 0-based index ind.
- getNSTnames(selectorArray=None)
Return neuron names, optionally filtered by selectorArray (0-based indices).
- getNeighbors(neuronNum)
Return the 0-based neighbour indices for one or more neurons.
- getSpikeTimes(minTime=None, maxTime=None)
Return a list of spike-time arrays, one per active neuron.
- getUniqueNSTnames(selectorArray=None)
Return unique, insertion-ordered neuron names.
- get_nst(idx)
Return a spike train by 0-based index (Pythonic API).
- Parameters:
idx (int)
- Return type:
- isSigRepBinary()
Alias for
BinarySigRep().- Return type:
- merge(nstColl2)
Merge another collection into this one (in-place).
- Parameters:
nstColl2 (SpikeTrainCollection)
- Return type:
SpikeTrainCollection
- property neuronNames: list[str]
Neuron name for each spike train in the collection.
Mirrors the MATLAB
neuronNamesstored property. In Python this is derived dynamically from each train’s.nameattribute so it is always consistent with the underlying data.
- plot(selectorArray=None, minTime=None, maxTime=None, handle=None, reverseOrder=False, **__)
Plot a spike-train raster.
- Parameters:
selectorArray (sequence of int, optional) – 0-based indices of neurons to plot. Defaults to the neuron mask (or all neurons if no mask is set). Matches Matlab positional arg.
minTime (float, optional) – Time window to display. Defaults to the collection’s time span.
maxTime (float, optional) – Time window to display. Defaults to the collection’s time span.
handle (matplotlib Axes, optional) – Axes to plot into.
reverseOrder (bool) – If
True, reverse the display order so the last neuron is at the top. Matches MatlabreverseOrderPlotparameter.
- plotExponentialFit(selectorArray=None, minTime=None, maxTime=None, numBins=None, handle=None)
Plot exponential-distribution fits of ISIs for selected neurons.
- plotISIHistogram(selectorArray=None, minTime=None, maxTime=None, handle=None)
Plot ISI histograms for each selected neuron in stacked subplots.
- psth(binwidth=0.1, selectorArray=None, minTime=None, maxTime=None)
Compute the peri-stimulus time histogram (standard binned PSTH).
Returns a
Covariatewith firing rate in Hz.
- psthBars(binwidth=0.1, selectorArray=None, minTime=None, maxTime=None)
Deterministic pure-Python fallback for MATLAB nstColl.psthBars.
MATLAB delegates this method to an external BARS package that is not bundled with the source tree. The Python port preserves the public surface and return structure with a smoothed PSTH approximation.
- psthGLM(basisWidth=None, history=None, fitType='poisson', selectorArray=None, minTime=None, maxTime=None, sampleRate=None, *, binwidth=None, windowTimes=None, alphaVal=0.05, Mc=1000)
GLM-based PSTH estimation (Matlab
nstColl.psthGLM).Parameters match MATLAB:
psthGLM(basisWidth, history, fitType, selectorArray, minTime, maxTime, sampleRate)The Python keyword-only
binwidthandwindowTimesare accepted as aliases forbasisWidthandhistoryrespectively.Returns
(psth_covariate, histSignal, psthFitResult)matching the Matlab signature.
- resample(sampleRate)
Resample all trains to sampleRate and align time bounds.
- Parameters:
sampleRate (float)
- Return type:
None
- resetMask()
Enable all neurons (ones-mask).
- Return type:
None
- restoreToOriginal(rMask=0)
Restore all trains to their original state; optionally reset the mask.
- Parameters:
rMask (int)
- Return type:
None
- setMask(mask)
Set the neuron mask from a binary array or 0-based indices.
- setMaxTime(value=None)
Set the maximum time for every train in the collection.
- Parameters:
value (float | None)
- Return type:
None
- setMinTime(value=None)
Set the minimum time for every train in the collection.
- Parameters:
value (float | None)
- Return type:
None
- setNeighbors(neighborArray=None)
Set or auto-generate the neuron neighbour matrix.
If neighborArray is
None, every neuron is a neighbour of every other neuron (all-to-all minus self).
- setNeuronMask(mask)
Set the binary neuron mask directly (length must match
numSpikeTrains).
- setNeuronMaskFromInd(mask)
Set the neuron mask from 0-based neuron indices.
- shiftTime(timeShift=None)
Return a new collection with spike times shifted by timeShift.
- Parameters:
timeShift (float | None)
- Return type:
SpikeTrainCollection
- ssglm(windowTimes=None, numBasis=None, numVarEstIter=None, fitType=None, rng=None)
State-space GLM via EM algorithm (forward only).
Matches Matlab nstColl.ssglm(). Estimates time-varying firing rate using a state-space model with EM parameter estimation.
- Parameters:
rng (np.random.Generator, optional) – Random number generator used to jitter zero entries in the initial variance estimate. Defaults to
np.random.default_rng()(non-reproducible across runs). Pass an explicit seeded generator for reproducible fits.windowTimes (array-like or None) – History window boundaries. None for no history.
numBasis (int or None) – Number of basis functions. Defaults to duration/0.02.
numVarEstIter (int or None) – Iterations for variance estimation. Default 10.
fitType ('poisson' or 'binomial')
- Returns:
xK ((R, K) estimated state trajectories)
WK ((R, R, K) estimated state covariances)
Qhat ((R,) estimated state noise variance)
gammahat ((J,) estimated history coefficients)
logll (float, log-likelihood)
fitResults (FitResult)
- ssglmFB(windowTimes=None, numBasis=None, numVarEstIter=None, fitType=None, rng=None)
State-space GLM via EM Forward-Backward algorithm.
Enhanced version of ssglm() that uses forward-backward-forward iterations for improved convergence. Calls PPSS_EMFB.
- Parameters:
rng (np.random.Generator, optional) – Random number generator for Q0 jitter; see
ssglm().windowTimes (array-like or None) – History window boundaries.
numBasis (int or None) – Number of basis functions.
numVarEstIter (int or None) – Iterations for variance estimation.
fitType ('poisson' or 'binomial')
- Returns:
xK, WK, Wku, Qhat, gammahat, fitResults, stimulus, stimCIs, logll,
QhatAll, gammahatAll, nIter
- toSpikeTrain(selectorArray=None, minTime=None, maxTime=None, windowTimes=None)
Collapse selected spike trains into a single
nspikeTrain.Concatenates spike times end-to-end, optionally rescaling each trial into windows defined by windowTimes.
- updateTimes(nst)
Expand collection time bounds to include nst, or clamp nst.
- Parameters:
nst (nspikeTrain)
- Return type:
None