Skip to content

Commit a61ac08

Browse files
authored
switch to rounded duration to get i_stop
Change comment Add more to strict assertion Add comment to explain duration calculation
1 parent 9a95a75 commit a61ac08

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

neo/io/proxyobjects.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,25 @@ def _time_slice_indices(self, time_slice, strict_slicing=True):
187187
assert self.t_start <= t_start <= self.t_stop, 't_start is outside'
188188
else:
189189
t_start = max(t_start, self.t_start)
190-
# the i_start is necessary ceil
190+
# the i_start is rounded to the nearest sample
191191
i_start = np.rint((t_start - self.t_start).magnitude * sr.magnitude).astype(np.int64)
192-
# this needed to get the real t_start of the first sample
193-
# because do not necessary match what is demanded
192+
# this is needed to get the real t_start of the first sample
193+
# because it does not necessary match what is demanded
194194
sig_t_start = self.t_start + i_start / sr
195195

196196
if t_stop is None:
197197
i_stop = None
198198
else:
199199
t_stop = ensure_second(t_stop)
200200
if strict_slicing:
201-
assert self.t_start <= t_stop <= self.t_stop, 't_stop is outside'
201+
assert self.t_start <= t_stop <= self.t_stop, 't_stop is outside possible time range'
202202
else:
203203
t_stop = min(t_stop, self.t_stop)
204-
i_stop = np.rint((t_stop - self.t_start).magnitude * sr.magnitude).astype(np.int64)
204+
# calculate duration demanded then round it to nearest sample number
205+
# add this to i_start to get i_stop
206+
delta = (t_stop - t_start) * sr
207+
i_stop = i_start + int(np.rint(delta.simplified.magnitude))
208+
205209
return i_start, i_stop, sig_t_start
206210

207211
def load(self, time_slice=None, strict_slicing=True,

0 commit comments

Comments
 (0)