Skip to content

Commit 7331ad6

Browse files
authored
[Fix] Concatenate raw objects with annotations (#5851)
Fix #5839, #5555 This PR fixes the fact that `raw_A.append(raw_B)` ignores `meas_date` and supposes that the buffers of `raw_A` and `raw_B` where captured one right after the other.
1 parent e96d706 commit 7331ad6

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

doc/whats_new.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Changelog
5454
Bug
5555
~~~
5656

57+
- Fix :meth:`mne.io.Raw.append` annotations miss-alignment by `Joan Massich`_
58+
5759
- Fix :func:`mne.io.read_raw_edf` reading duplicate channel names by `Larry Eisenman`_
5860

5961
- Fix :func:`set_bipolar_reference` in the case of generating all bipolar combinations and also in the case of repeated channels in both lists (anode and cathode) by `Cristóbal Moënne-Loccoz`_
@@ -3142,4 +3144,4 @@ of commits):
31423144

31433145
.. _Larry Eisenman: https://github.com/lneisenman
31443146

3145-
.. _Stanislas Chambon: https://github.com/Slasnista
3147+
.. _Stanislas Chambon: https://github.com/Slasnista

mne/io/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2127,8 +2127,12 @@ def append(self, raws, preload=None):
21272127
edge_samps = list()
21282128
for ri, r in enumerate(raws):
21292129
n_samples = self.last_samp - self.first_samp + 1
2130+
r_annot = Annotations(onset=r.annotations.onset - r._first_time,
2131+
duration=r.annotations.duration,
2132+
description=r.annotations.description,
2133+
orig_time=None)
21302134
annotations = _combine_annotations(
2131-
annotations, r.annotations, n_samples,
2135+
annotations, r_annot, n_samples,
21322136
self.first_samp, r.first_samp,
21332137
self.info['sfreq'], self.info['meas_date'])
21342138
edge_samps.append(sum(self._last_samps) -

mne/io/fiff/raw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def __init__(self, fname, allow_maxshield=False, preload=False,
112112
self.annotations, r.annotations,
113113
n_samples, self.first_samp, r.first_samp,
114114
r.info['sfreq'], self.info['meas_date'])
115-
self.set_annotations(annotations, False)
115+
self.set_annotations(annotations, emit_warning=False)
116116
n_samples += r.last_samp - r.first_samp + 1
117117

118118
# Add annotations for in-data skips

mne/io/tests/test_raw.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,46 @@ def test_get_data_reject():
281281
assert log.getvalue().strip() == msg
282282
assert data.shape == (len(ch_names), 2560) # shape doesn't change
283283
assert np.isnan(data).sum() == 3072 # but NaNs are introduced instead
284+
285+
286+
def test_5839():
287+
"""Test concatenating raw objects with annotations."""
288+
# Global Time 0 1 2 3 4
289+
# .
290+
# raw_A |---------XXXXXXXXXX
291+
# annot |--------------AA
292+
# latency . 0 0 1 1 2 2 3
293+
# . 5 0 5 0 5 0
294+
#
295+
# raw_B . |---------YYYYYYYYYY
296+
# annot . |--------------AA
297+
# latency . 0 1
298+
# . 5 0
299+
# .
300+
# output |---------XXXXXXXXXXYYYYYYYYYY
301+
# annot |--------------AA---|----AA
302+
# latency . 0 0 1 1 2 2 3
303+
# . 5 0 5 0 5 0
304+
#
305+
EXPECTED_ONSET = [1.5, 2.5, 2., 2.]
306+
EXPECTED_DURATION = [0.2, 0.2, 0., 0.]
307+
EXPECTED_DESCRIPTION = ['dummy', 'dummy', 'BAD boundary', 'EDGE boundary']
308+
309+
def raw_factory(meas_date):
310+
raw = RawArray(data=np.empty((10, 10)),
311+
info=create_info(ch_names=10, sfreq=10., ),
312+
first_samp=10)
313+
raw.info['meas_date'] = meas_date
314+
raw.set_annotations(annotations=Annotations(onset=[.5],
315+
duration=[.2],
316+
description='dummy',
317+
orig_time=None))
318+
return raw
319+
320+
raw_A, raw_B = [raw_factory((x, 0)) for x in [0, 2]]
321+
raw_A.append(raw_B)
322+
323+
assert_array_equal(raw_A.annotations.onset, EXPECTED_ONSET)
324+
assert_array_equal(raw_A.annotations.duration, EXPECTED_DURATION)
325+
assert_array_equal(raw_A.annotations.description, EXPECTED_DESCRIPTION)
326+
assert raw_A.annotations.orig_time == 0.0

0 commit comments

Comments
 (0)