nstat.FitResult

class nstat.FitResult(neuralSpikeTrain, *args, **kwargs)[source]

Bases: object

GLM fit results for one neuron across one or more model configs (Matlab FitResult).

Stores coefficients, deviance, AIC/BIC, log-likelihood, fitted λ signal, and KS-test diagnostics for each configuration in a ConfigCollection. Provides coefficient accessors, residual analysis, and Matlab-compatible plot methods.

Parameters:
  • neuralSpikeTrain (nspikeTrain or sequence of nspikeTrain) – The observed spike train(s) that were fitted.

  • *args – Positional / keyword construction matching the Matlab FitResult(nst, covLabels, numHist, …) signature, or the simplified FitResult(nst, lambdaCov, fits) form.

  • **kwargs – Positional / keyword construction matching the Matlab FitResult(nst, covLabels, numHist, …) signature, or the simplified FitResult(nst, lambdaCov, fits) form.

See also

FitSummary

Aggregate summary across multiple neurons.

Analysis.RunAnalysisForAllNeurons

Main entry point that produces FitResult objects.

__init__(neuralSpikeTrain, *args, **kwargs)[source]

Construct a per-neuron GLM fit result (Matlab FitResult).

Supports two construction patterns:

  1. Simplified Python form FitResult(nst, lambda_signal, fits) where lambda_signal is a Covariate and fits is a sequence of internal _SingleFit records.

  2. MATLAB-faithful form matching the FitResult(nst, covLabels, numHist, histObjects, ensHistObj, lambda_signal, b, dev, stats, AIC, BIC, logLL, configColl, XvalData, XvalTime, distribution) signature.

Parameters:
  • neuralSpikeTrain (nspikeTrain or sequence of nspikeTrain) – The observed spike train(s) that were fitted. When a sequence is provided, the result represents a pooled / multi-trial fit (Matlab parity).

  • *args –

    Positional / keyword arguments dispatched to either _init_simplified() or _init_matlab_style() based on whether args[0] is a Covariate.

    Notable keyword fields:

    • covLabels (list of list of str) — per-config covariate labels in the design matrix.

    • b (list of array_like) — per-config coefficient vectors.

    • dev, AIC, BIC, logLL (array_like) — goodness-of-fit scalars (one entry per config).

    • stats (list of dict) — per-config diagnostic dicts.

    • lambda_signal (Covariate) — fitted intensity in spikes/s.

    • configColl (ConfigCollection) — the configuration collection used to drive the fits.

    • distribution (str) — "poisson" (default) or "binomial".

  • **kwargs –

    Positional / keyword arguments dispatched to either _init_simplified() or _init_matlab_style() based on whether args[0] is a Covariate.

    Notable keyword fields:

    • covLabels (list of list of str) — per-config covariate labels in the design matrix.

    • b (list of array_like) — per-config coefficient vectors.

    • dev, AIC, BIC, logLL (array_like) — goodness-of-fit scalars (one entry per config).

    • stats (list of dict) — per-config diagnostic dicts.

    • lambda_signal (Covariate) — fitted intensity in spikes/s.

    • configColl (ConfigCollection) — the configuration collection used to drive the fits.

    • distribution (str) — "poisson" (default) or "binomial".

Return type:

None

See also

FitSummary

Aggregate summary across multiple neurons.

Analysis.RunAnalysisForAllNeurons

Main entry point that produces FitResult objects.

property lambdaSignal: Covariate

MATLAB-style alias for lambda_signal.

property lambda_obj: Covariate
property lambda_model: Covariate
property lambda_result: Covariate
property lambdaObj: Covariate
property lambdaCov: Covariate
property lambda_sig: Covariate
property lambda_data: ndarray
property lambda_values: ndarray
property lambda_time: ndarray
property lambda_rate: ndarray
setNeuronName(name)[source]

Rename the neuron on the underlying spike train(s).

Parameters:

name (str)

mergeResults(other)[source]

Concatenate another FitResult’s configs into this one.

Parameters:

other (FitResult)

Return type:

FitResult

getSubsetFitResult(subfits)[source]

Return a new FitResult with only the selected fit indices (0-based).

Return type:

FitResult

addParamsToFit(neuronNum, lambda_signal, b, dev, stats, AIC, BIC, logLL, configColl)[source]

Append a new fit configuration’s results (Matlab addParamsToFit).

computeValLambda()[source]

Compute the conditional intensity on validation data (Matlab computeValLambda).

Returns:

  • lambda_val (Covariate) – The validation-set conditional intensity function.

  • logLL (np.ndarray) – Log-likelihood for each fit configuration on the validation data.

Return type:

tuple[Covariate, ndarray]

mapCovLabelsToUniqueLabels()[source]

Rebuild the unique-label map and flatMask from covLabels.

getPlotParams()[source]

Alias for computePlotParams().

plotValidation()[source]

Plot validation fit results (if present).

isValDataPresent()[source]

Return True if cross-validation data was stored.

Return type:

bool

getCoeffsWithLabels(fit_num=0)[source]

Return (coefficients, labels, standardErrors) for fit_num.

Parameters:

fit_num (int)

Return type:

tuple[ndarray, list[str], ndarray]

getHistCoeffsWithLabels(fit_num=0)[source]

Return (histMat, labels, SEMat) — Matlab multi-output form.

Matlab: [histMat, labels, SEMat] = getHistCoeffs(fitObj, fitNum)

Parameters:

fit_num (int)

Return type:

tuple[ndarray, list[str], ndarray]

computeKSStats(fit_num=0, *, dt_correction=1)[source]

Return KS statistic, p-value, and within-CI flag for fit_num.

Parameters:
  • fit_num (int)

  • dt_correction (int)

Return type:

dict[str, float]

computeInvGausTrans(fit_num=0)[source]

Return Gaussianized (inverse-normal-transformed) rescaled ISIs.

Parameters:

fit_num (int)

Return type:

ndarray

computeFitResidual(fit_num=0, *, windowSize=None)[source]

Compute the martingale residual M(t) = N(t) − Λ(t) (Matlab computeFitResidual).

Parameters:
  • fit_num (int)

  • windowSize (float | None)

Return type:

Covariate

evalLambda(fit_num=0, newData=None)[source]

Evaluate λ(t) = exp(X·β) · sampleRate on newData (Matlab evalLambda).

Parameters:

fit_num (int)

Return type:

ndarray

computePlotParams(fit_num=None)[source]

Compute the aligned coefficient / SE / significance arrays for plotting.

Parameters:

fit_num (int | None)

getCoeffIndex(fit_num=0, sortByEpoch=0)[source]

Return (indices, epochIds, numEpochs) for non-history coefficients.

Parameters:
  • fit_num (int)

  • sortByEpoch (int)

plotCoeffsWithoutHistory(fit_num=0, sortByEpoch=0, plotSignificance=1, handle=None)[source]

Plot non-history (stimulus/baseline) coefficients only.

Parameters:
  • fit_num (int)

  • sortByEpoch (int)

  • plotSignificance (int)

getHistIndex(fit_num=0, sortByEpoch=0)[source]

Return (indices, epochIds, numEpochs) for history coefficients.

Parameters:
  • fit_num (int)

  • sortByEpoch (int)

getCoeffs(fit_num=0)[source]

Return (coeffMat, labels, SEMat) for fit_num (0-based).

Matches MATLAB: [coeffMat, labels, SEMat] = getCoeffs(fitObj, fitNum).

Parameters:

fit_num (int)

Return type:

tuple[ndarray, list[str], ndarray]

getHistCoeffs(fit_num=0)[source]

Return (histMat, labels, SEMat) for fit_num (0-based).

Matches MATLAB: [histMat, labels, SEMat] = getHistCoeffs(fitObj, fitNum).

Parameters:

fit_num (int)

Return type:

tuple[ndarray, list[str], ndarray]

plotHistCoeffs(fit_num=0, sortByEpoch=0, plotSignificance=1, handle=None)[source]

Plot history-filter coefficients (Matlab plotHistCoeffs).

Parameters:
  • fit_num (int)

  • sortByEpoch (int)

  • plotSignificance (int)

plotCoeffs(fit_num=None, handle=None, plotSignificance=1)[source]

Plot GLM coefficients with error bars and significance markers.

Matches Matlab FitResult.plotCoeffs: when fit_num is None (default) all fits are overlaid with per-fit colours, errorbar plots with ±1 SE, and asterisks (*) above significant coefficients (p < 0.05). Includes a legend matching Matlab’s lambda dataLabels.

Parameters:
plotResults(fit_num=0, handle=None)[source]

Matlab-matching 2x4 subplot layout with 5 diagnostic panels.

Layout (matching Matlab subplot(2,4,...)):

[1,2] KSPlot (double-wide) [3] InvGausTrans [4] SeqCorr [5,6] plotCoeffs (double-wide) [7,8] plotResidual (double-wide)

Parameters:

fit_num (int)

KSPlot(fit_num=None, handle=None)[source]

KS goodness-of-fit plot with 95 % confidence bands (Matlab KSPlot).

Parameters:
  • fit_num (int, list of int, or None) – Which model(s) to plot. None (default) plots all models (1:numResults), matching the MATLAB default behaviour. A single int plots one model; a list plots the specified subset.

  • handle (matplotlib Axes, optional) – Axes to draw on. A new figure is created when None.

toStructure()[source]

Serialize to a JSON-compatible dict (Matlab toStructure).

Return type:

dict[str, Any]

plotSeqCorr(fit_num=None, handle=None)[source]

Plot U_j vs U_{j+1} scatter with correlation coefficient and p-value.

Matlab: plotSeqCorr plots the sequential correlation scatter of U_j (uniform-transformed rescaled ISIs) to detect serial dependence. When multiple models are present, each is plotted with a different colour and a legend entry showing label, ρ=X.XX (p=Y.YY).

Parameters:
  • fit_num (int, list of int, or None) – Which model(s) to plot. None (default) plots all models, matching the MATLAB default behaviour.

  • handle (matplotlib Axes, optional) – Axes to draw on. A new figure is created when None.

plotInvGausTrans(fit_num=None, handle=None)[source]

Plot ACF of gaussianized rescaled ISIs with 95% CIs.

Matlab: plotInvGausTrans computes X_j = Φ⁻¹(U_j) and plots the autocorrelation function of X_j with 95% confidence bounds. Supports multi-fit overlay with per-fit colours matching KS/SeqCorr.

Parameters:

fit_num (int | list[int] | None)

plotResidual(fit_num=None, handle=None)[source]

Plot the martingale residual M(t) for one or more fits.

Matches Matlab plotResidual: plots all residuals with per-fit colours and a legend using lambda.dataLabels.

Parameters:

fit_num (int | list[int] | None)

setKSStats(Z, U, xAxis, KSSorted, ks_stat)[source]

Store pre-computed KS-test arrays (Matlab setKSStats).

setInvGausStats(X, rhoSig, confBoundSig)[source]

Store pre-computed inverse-Gaussian transform statistics.

setFitResidual(M)[source]

Store the pre-computed fit residual Covariate.

getParam(paramNames, fit_num=0)[source]

Return (coeffs, SE, significance) for named parameters.

Parameters:

fit_num (int)

static fromStructure(structure)[source]

Reconstruct a FitResult from a dict.

Parameters:

structure (dict[str, Any])

Return type:

FitResult

static CellArrayToStructure(fitResObjCell)[source]

Serialize a list of FitResult objects to a list of dicts.