nstat.CovariateCollection
- class nstat.CovariateCollection(covariates=None, *more_covariates)[source]
Bases:
objectOrdered collection of
Covariateobjects (MatlabCovColl).Provides collection-level masking, time alignment, sample-rate enforcement, and covariate shifting. Individual covariates are accessed via 0-based indexing (
getCov(1)) to match Matlab.- Parameters:
See also
- __init__(covariates=None, *more_covariates)[source]
Construct an ordered covariate collection (Matlab
CovColl).- Parameters:
covariates (Covariate, sequence of Covariate, CovariateCollection, or None) – Initial covariate(s) to add. May be a single
Covariate, an iterable of them, anotherCovariateCollection(whose contents are merged in), orNone(empty collection).*more_covariates (Covariate) – Additional covariates appended after covariates — supports the MATLAB-style
CovColl(c1, c2, c3, ...)call signature.
- Return type:
None
Notes
On construction the collection captures the maximal sample rate across all input covariates (in Hz), the union of their time windows (in seconds), and a per-dimension mask of ones for each covariate (no dimensions masked out). Time bounds and sample rate are recomputed lazily via
_refresh_summary.Indexing is 0-based to match MATLAB (
coll.getCov(0)is the first covariate).
- property covariates: list[Covariate]
List of all covariates (copies with collection state applied).
- setMinTime(minTime=None)[source]
Set the collection-level minimum time (applies shift if set).
- Parameters:
minTime (float | None)
- Return type:
None
- setMaxTime(maxTime=None)[source]
Set the collection-level maximum time (applies shift if set).
- Parameters:
maxTime (float | None)
- Return type:
None
- setSampleRate(sampleRate)[source]
Set the collection sample rate and enforce it on all covariates.
- Parameters:
sampleRate (float)
- Return type:
None
- setMask(cellInput)[source]
Set the covariate mask from a selector or
'all'to reset.Accepts the same formats as
generateSelectorCell().- Return type:
None
- isCovMaskSet()[source]
Return
Trueif any covariate dimension is currently masked out.- Return type:
- nActCovar()[source]
Return the number of covariates with at least one active dimension.
- Return type:
- maskAwayOnlyCov(identifier)[source]
Reset all masks then mask away only the specified covariate(s).
- maskAwayAllExcept(identifier)[source]
Mask away every covariate except the ones specified.
Matches MATLAB
CovColl.m:202-208(maskAwayOnlyCov→resetMaskfirst, thenmaskAwayCov). Before the reset the kept covariates’ masks were preserved as-is, which silently dropped any covariate whose stored mask was already all-zero — e.g. the per-neighbor masks insideTrial.ensCovColl, which are initialised for neuron 1’s view and contain zeros for the other neuron’s row. Without resetting,getEnsCovMatrix(n=2)returned an empty matrix and the NetworkTutorial fit silently dropped the inter-neuron coupling term.
- getCov(identifier)[source]
Return a covariate copy with collection state (shift, mask, rate) applied.
- get(name)[source]
Retrieve a covariate by name (convenience alias for
getCov()).- Parameters:
name (str)
- Return type:
Covariate
- getCovDimension(identifier=None)[source]
Return the dimension of each covariate selected by identifier.
Matlab signature:
dim = getCovDimension(ccObj, identifier)Returns a 1-D int array whose i-th element is
covs{i}.dimension.- Return type:
- findMaxTime()[source]
Return the latest
maxTimeacross all stored covariates.Note: MATLAB
CovColl.m:371-380appliescovShifttwice here (once inside the per-covariatemax(...)and once after the loop) — see AUDIT_REPORT M7 / nSTAT issue #18. Python adds the shift exactly once, in_refresh_summaryandsetMaxTime. Do not “fix” this to match MATLAB’s behavior.- Return type:
- findMaxSampleRate()[source]
Return the highest sample rate across all stored covariates.
- Return type:
- add(covariate)[source]
Alias for
addToColl().- Parameters:
covariate (Covariate)
- Return type:
None
- addCovariate(covariate)[source]
Alias for
addToColl().- Parameters:
covariate (Covariate)
- Return type:
None
- addCovCollection(covariates)[source]
Merge all covariates from another collection into this one.
- Parameters:
covariates (CovariateCollection)
- Return type:
None
- resample(sampleRate)[source]
Alias for
setSampleRate().- Parameters:
sampleRate (float)
- Return type:
None
- restoreToOriginal()[source]
Restore original sample rate, time bounds, shift, and masks.
- Return type:
None
- enforceSampleRate()[source]
Resample every stored covariate to match
self.sampleRate.Matches MATLAB
CovColl.m:491-502which iterates each covariate and callsresampleMewhenever its rate differs from the collection’s rate. The previous Python implementation only validated thatself.sampleRatewas finite; it did NOT actually resample stored covariates. Net effect: a collection built by sequentially adding covariates at increasing rates would reportself.sampleRateat the highest rate but leave earlier covariates at their original (lower) rates. Closes audit finding H3.- Return type:
None
- setCovShift(deltaT, identifier=None)[source]
Apply a temporal shift deltaT to the collection’s time axis.
Matches MATLAB
CovColl.m:504-520which callsresetCovShiftfirst (zeroing any previously-applied shift and recomputing base bounds) and then sets the new shift. The previous Python implementation accumulated shifts: callingsetCovShift(0.2)twice producedbase + 0.4instead of the intendedbase + 0.2. Closes audit finding L1.- Parameters:
deltaT (float)
- Return type:
- flattenCovMask()[source]
Concatenate all per-covariate masks into a single 1-D binary array.
- Return type:
- getSelectorFromMasks(covMask=None)[source]
Convert per-covariate binary masks to lists of active 0-based indices.
- generateSelectorCell(dataSelector)[source]
Parse a heterogeneous dataSelector into per-covariate index lists.
Accepts name-based (
[['covName', 'label1', ...], ...]) or numeric ([[1,2], [3], ...]) selectors.
- dataToMatrix(repType='standard', dataSelector=None, *_)[source]
Return the covariate data matrix (no time column) for active dimensions.
- matrixWithTime(repType='standard', dataSelector=None)[source]
Return
(time, data_matrix, labels)for active covariate dimensions.
- dataToMatrixFromNames(repType='standard', dataSelector=None, *args)[source]
Return the data matrix for a name-based
dataSelector.Thin wrapper that resolves the name-based selector through
generateSelectorCell()and dispatches todataToMatrixFromSel(). Mirrors MATLABCovColl.dataToMatrixFromNames(CovColl.m:563).
- dataToMatrixFromSel(repType='standard', selectorCell=None, *_args)[source]
Return the data matrix for an explicit per-covariate
selectorCell.Each entry of
selectorCellis a list of 0-based dimension indices for the corresponding covariate. Mirrors MATLABCovColl.dataToMatrixFromSel(CovColl.m:567).
- dataToStructure(selectorCell=None, binwidth=None, minTime=None, maxTime=None)[source]
Serialize active covariate data to a
{'time': ..., 'signals': ...}dict.
- plot(*_, handle=None, **__)[source]
Plot each covariate in a vertically stacked panel layout.
- Parameters:
handle (matplotlib Figure, Axes, list of Axes, or None) – If a Figure, new subplots are created. If a single Axes or a list of Axes, plot into those directly. If None, a new figure is created.
- static fromStructure(structure)[source]
Reconstruct from a dict produced by
toStructure().- Return type:
‘CovariateCollection’ | list[‘CovariateCollection’]