Repository navigation
Fix rr2ann with times and with a symbol column - #612
Conversation
tompollard
left a comment
There was a problem hiding this comment.
Thanks, this looks good! Rounding the times fixes the truncation, and the symbol column now works.
One small thing: converting to integers before cumsum changes the result for non-integer intervals, because each interval is truncated and the error builds up:
rr2ann(np.array([4.5, 4.5, 4.5]), "t", "atr")
# main: [4, 9, 13] this PR: [4, 8, 12]Intervals like "4.5" with a symbol column also raise ValueError: invalid literal for int(). Summing as floats and then rounding, as the as_time branch does, handles both:
ann_sample = np.round(np.cumsum(ann_sample.astype(np.float64))).astype(np.int64)That gives [4, 9, 14] for the example above, and your three tests still pass. ann2rr returns integers, so the usual case isn't affected either way.
c137670 to
835e6a6
Compare
|
Thanks, good point! I've switched to summing as floats and rounding once, as you suggested. That also fixes |
835e6a6 to
baac2b3
Compare
Times were converted to samples by truncation, so floating-point error moved some annotations one sample early (0.29 s at 100 Hz gave 28). Round instead. With a symbol column the array holds strings, and np.cumsum failed on them, so convert the intervals to integers first.
baac2b3 to
6404001
Compare
Converting each interval to an integer first truncated fractional intervals, and the error built up over the record.
6404001 to
641976c
Compare
rr2annhas two problems:as_time=True, times are converted to samples with.astype(np.int64), which truncates.0.29 * 100is28.999999999999996, so the annotation lands one sample early. This now rounds to the nearest sample.as_timethe intervals go straight intonp.cumsum, which fails on strings. They are now converted to integers first (theas_timepath already converted to float).Tests: times that truncate wrongly, intervals with a symbol column (both fail on
main), and the docstring example with times and symbols.pytest -m "not network"passes (99 tests), and the docs build passes.