arista.preprocess.drift
Drift-correction fits and chooser.
Replaces _legacy/pytci/tciAnalysis.py with pure functions returning
DriftFit dataclasses. The default chooser is AIC-based
(no GUI prompt). The interactive matplotlib chooser from the legacy
pipeline is intentionally NOT ported into this module — it belongs in
the CLI layer (Phase 3, arista-preprocess drift --method interactive)
where stdout/stdin handling is appropriate.
Three candidate fits are computed, identical in form to pytci:
linear— degree-1 polyfit on pre+post-stimulus tails onlypoly— degree-4 polyfit over the whole traceexp—a·exp(-b·t) + cviascipy.optimize.curve_fit
AIC scoring uses the standard Gaussian-residuals formula and the
params dict on each fit makes the model auditable downstream.
Functions
|
Subtract a fit from the ΔF/F trace and return a new |
|
Convenience: fit all candidates, pick the best, apply it. |
|
Compute linear, poly and exp fits; return them in a dict by method name. |
|
|
|
Degree-1 polyfit on the first and last |
|
Degree- |
|
Select one fit from a |
Classes
|
A fitted drift model plus its evaluation on the full trace. |
- class arista.preprocess.drift.DriftFit(method, fitted, residual_ssq, aic, params=<factory>)[source]
Bases:
objectA fitted drift model plus its evaluation on the full trace.
- Parameters:
- fitted: numpy.ndarray
- arista.preprocess.drift.apply_drift(recording, fit)[source]
Subtract a fit from the ΔF/F trace and return a new
Recording.If
fitisNonethe recording is returned withdrift_method = "none"anddfbf_drift_corrected = None(i.e. drift correction explicitly not applied — the originaldfbfcolumn remains the source of truth).- Parameters:
recording (Recording) – An aligned
Recordingfromarista.preprocess.align.assemble_recording().
- Returns:
A new
Recordingwithdfbf_drift_correctedanddrift_methodfilled in.- Return type:
- arista.preprocess.drift.correct_drift(recording, method='auto')[source]
Convenience: fit all candidates, pick the best, apply it.
For headless / batch / CI use. Matches the default behaviour
arista-preprocess drift --method autowill expose at the CLI level in Phase 3.
- arista.preprocess.drift.fit_all(t, y)[source]
Compute linear, poly and exp fits; return them in a dict by method name.
The exponential fit may fail to converge on flat traces; in that case it is omitted from the returned dict (rather than raising) so the AIC chooser can still pick between linear and poly.
- Parameters:
t (numpy.ndarray)
y (numpy.ndarray)
- Return type:
- arista.preprocess.drift.fit_exponential(t, y)[source]
a·exp(-b·t) + cfit. Mirrors pytci’sfitExpbounds + p0.- Parameters:
t (numpy.ndarray)
y (numpy.ndarray)
- Return type:
- arista.preprocess.drift.fit_linear(t, y)[source]
Degree-1 polyfit on the first and last
_LINEAR_TAIL_FRAMESframes.Matches pytci’s
fitLinear: the fit is trained on pre + post stimulus tails only, then evaluated over the whole trace. This deliberately ignores the stimulus-evoked excursions so the linear component captures photobleach drift rather than the response.- Parameters:
t (numpy.ndarray)
y (numpy.ndarray)
- Return type:
- arista.preprocess.drift.fit_polynomial(t, y, degree=4)[source]
Degree-
degreepolyfit over the whole trace (default 4, pytci default).- Parameters:
t (numpy.ndarray)
y (numpy.ndarray)
degree (int)
- Return type:
- arista.preprocess.drift.pick_best(fits, method='auto')[source]
Select one fit from a
fit_all()result.- Parameters:
- Returns:
The chosen
DriftFit, orNoneifmethod == "none".- Raises:
ValueError – If
methodis not a valid choice, or if a forced method is requested but missing fromfits.- Return type:
DriftFit | None