Test cropping with annotations.
(tmp_path)
| 190 | |
| 191 | |
| 192 | def test_crop(tmp_path): |
| 193 | """Test cropping with annotations.""" |
| 194 | raw = read_raw_fif(fif_fname) |
| 195 | events = mne.find_events(raw) |
| 196 | onset = events[events[:, 2] == 1, 0] / raw.info["sfreq"] |
| 197 | duration = np.full_like(onset, 0.5) |
| 198 | description = [f"bad {k}" for k in range(len(onset))] |
| 199 | annot = mne.Annotations( |
| 200 | onset, duration, description, orig_time=raw.info["meas_date"] |
| 201 | ) |
| 202 | raw.set_annotations(annot) |
| 203 | |
| 204 | split_time = raw.times[-1] / 2.0 + 2.0 |
| 205 | split_idx = len(onset) // 2 + 1 |
| 206 | raw_cropped_left = raw.copy().crop(0.0, split_time - 1.0 / raw.info["sfreq"]) |
| 207 | assert_array_equal( |
| 208 | raw_cropped_left.annotations.description, |
| 209 | raw.annotations.description[:split_idx], |
| 210 | ) |
| 211 | assert_allclose( |
| 212 | raw_cropped_left.annotations.duration, raw.annotations.duration[:split_idx] |
| 213 | ) |
| 214 | assert_allclose( |
| 215 | raw_cropped_left.annotations.onset, raw.annotations.onset[:split_idx] |
| 216 | ) |
| 217 | raw_cropped_right = raw.copy().crop(split_time, None) |
| 218 | assert_array_equal( |
| 219 | raw_cropped_right.annotations.description, |
| 220 | raw.annotations.description[split_idx:], |
| 221 | ) |
| 222 | assert_allclose( |
| 223 | raw_cropped_right.annotations.duration, raw.annotations.duration[split_idx:] |
| 224 | ) |
| 225 | assert_allclose( |
| 226 | raw_cropped_right.annotations.onset, raw.annotations.onset[split_idx:] |
| 227 | ) |
| 228 | raw_concat = mne.concatenate_raws( |
| 229 | [raw_cropped_left, raw_cropped_right], verbose="debug" |
| 230 | ) |
| 231 | assert_allclose(raw_concat.times, raw.times) |
| 232 | assert_allclose(raw_concat[:][0], raw[:][0], atol=1e-20) |
| 233 | assert_and_remove_boundary_annot(raw_concat) |
| 234 | # Ensure we annotations survive round-trip crop->concat |
| 235 | assert_array_equal(raw_concat.annotations.description, raw.annotations.description) |
| 236 | for attr in ("onset", "duration"): |
| 237 | assert_allclose( |
| 238 | getattr(raw_concat.annotations, attr), |
| 239 | getattr(raw.annotations, attr), |
| 240 | err_msg=f"Failed for {attr}:", |
| 241 | ) |
| 242 | |
| 243 | raw.set_annotations(None) # undo |
| 244 | |
| 245 | # Test concatenating annotations with and without orig_time. |
| 246 | raw2 = raw.copy() |
| 247 | raw.set_annotations(Annotations([45.0], [3], "test", raw.info["meas_date"])) |
| 248 | raw2.set_annotations(Annotations([2.0], [3], "BAD", None)) |
| 249 | expected_onset = [45.0, 2.0 + raw._last_time] |
nothing calls this directly
no test coverage detected