python-neo icon indicating copy to clipboard operation
python-neo copied to clipboard

time_slice() behaves differently for AnalogSignal and IrregularlySampledSignal

Open apdavison opened this issue 5 years ago • 1 comments

from neo import AnalogSignal, IrregularlySampledSignal
from quantities import ms, mV
import numpy as np

sig = AnalogSignal(np.arange(50) * mV, sampling_period=1 * ms, t_start=0.1 * ms)
irr = IrregularlySampledSignal(sig.times, np.arange(50) * mV)

assert np.all(sig.times == irr.times)

print(sig.time_slice(5.5 * ms, 9.9 * ms).times)
print(irr.time_slice(5.5 * ms, 9.9 * ms).times)

print(sig.time_slice(5.3 * ms, 9.9 * ms).times)
print(irr.time_slice(5.3 * ms, 9.9 * ms).times)

gives

[5.1 6.1 7.1 8.1] ms
[6.1 7.1 8.1 9.1] ms
[5.1 6.1 7.1 8.1 9.1] ms
[6.1 7.1 8.1 9.1] ms

In the docstring for AnalogSignal.time_slice it says "Note, that for numerical stability reasons if t_start does not fall exactly on the time bins defined by the sampling_period it will be rounded to the nearest sampling bin. The time bin for t_stop will be chosen to make the duration of the resultant signal as close as possible to t_stop - t_start. This means that for a given duration, the size of the slice will always be the same."

This is why t_start for the sliced analog signal is before the requested slice start. I guess IrregularlySampledSignal should match the behaviour of AnalogSignal in this case.

apdavison avatar Jul 07 '20 15:07 apdavison

Perhaps we should add a flag to time_slice() to allow choosing either consistent size, or ensuring that all times fall strictly within t_start, t_stop.

apdavison avatar Jul 07 '20 15:07 apdavison