MCPcopy
hub / github.com/mne-tools/mne-python / test_crop

Function test_crop

mne/tests/test_annotations.py:192–298  ·  view source on GitHub ↗

Test cropping with annotations.

(tmp_path)

Source from the content-addressed store, hash-verified

190
191
192def 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]

Callers

nothing calls this directly

Calls 13

saveMethod · 0.95
cropMethod · 0.95
read_raw_fifFunction · 0.90
AnnotationsClass · 0.90
concatenate_rawsFunction · 0.90
read_annotationsFunction · 0.90
create_infoFunction · 0.90
RawArrayClass · 0.90
set_annotationsMethod · 0.45
cropMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected