Cut the lag axis together with the values in cross_correlation_function - #707
Conversation
When 'n_lags' was given, only the correlation values were sliced. The lag vector 'tau' kept the full range, and 't_start' of the returned neo.AnalogSignal was taken from the uncut 'tau', so the whole lag axis was shifted by nt // 2 - n_lags samples. For the example in the function's own docstring (2018 samples, dt = 0.02 s, n_lags = 150) the returned lags ran from -20.18 s to -14.18 s, zero lag was labelled -17.18 s, and the axis did not contain zero at all. The values were correct, only the times they were attached to were wrong, which makes every plot of 'rho.times' against 'rho' mislabelled. Slice 'tau' with the same indices so 't_start' follows the cut. The same slice also silently wrapped when 'n_lags' exceeded the available lag range, because 'tau0 - n_lags' went negative. A signal of 100 samples returned 100 rows for n_lags=50 and 10 rows for n_lags=60, both contradicting the documented shape of 2 * n_lags + 1. That case now raises ValueError with the largest usable value.
|
The docs jobs here are failing on a remote fetch, not on this branch. The sphinx build dies executing a notebook that pulls sample data over HTTPS, ending in The other elephant PR I have open, #706, is red on the same theme from the other direction, with |
|
Following up on my note above, which only covered the docs job. The On the latest run here the only failures are Worth flagging one knock-on effect: Coveralls reports coverage dropping to roughly 45.7 percent from 88.6. That looks alarming but is consistent with the failing jobs never uploading their coverage, leaving only a partial report merged. I would not read it as a real coverage regression from these changes. |
|
Hi again, |
When
n_lagsis given,cross_correlation_functionreturns correct correlation values attached to a wrong lag axis.This is the plotting example from the function's own docstring:
On current master:
The returned lag range is
[-20.18 s, -14.18 s]. It does not contain zero lag at all, and the sample that actually is zero lag is labelled-17.18 s. The expected range forn_lags=150atdt=0.02 sis[-3.0 s, 3.0 s]with zero in the centre.The cause is that only the values are sliced:
taukeeps the full range, sot_startis taken from the uncut vector and the whole axis is shifted bynt // 2 - n_lagssamples, which is 859 samples or 17.18 s here. The magnitudes are right, I verified they match the corresponding slice of the un-cut result exactly, so this shows up as a silently mislabelled plot rather than a crash. The docstring's own example doesplt.plot(rho.times, rho)withn_lags=150. The fix slicestauwith the same indices.The same expression also wrapped silently when
n_lagsexceeded the available lag range, becausetau0 - n_lagswent negative. For a 100 sample signal,n_lags=50returned 100 rows andn_lags=60returned 10 rows, both contradicting the documented output shape[2*n_lags+1, n]. That now raisesValueErrornaming the largest usable value, and theRaisessection gains the matching bullet.Two tests are added.
test_cross_correlation_nlags_time_axischecks that the lag axis runs from-n_lagsto+n_lagswith zero in the centre, and applies the same sine of the lag identity thattest_cross_correlation_freqsalready uses for the un-cut case, which only holds if the axis matches the values.test_cross_correlation_nlags_too_largepins the boundary at 49 for 100 samples and the rejection above it.Reverting
signal_processing.pyto master fails withMismatched elements: 61 / 61 (100%), [0]: -20.18 (ACTUAL), -0.6 (DESIRED). After, 52 passed and 2 subtests passed.signal_processing.pyand its test module arepycodestyleclean before and after.I have not added myself to
doc/authors.rst, since it is a numbered institutional affiliation list. Happy to add an entry if you would like one.Disclosure: this change was prepared with AI assistance. I have reviewed and tested it.