[074d3d]: / mne / export / tests / test_export.py

Download this file

672 lines (572 with data), 23.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
"""Test exporting functions."""
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
from contextlib import nullcontext
from datetime import date, datetime, timezone
from pathlib import Path
import numpy as np
import pytest
from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal
from mne import (
Annotations,
Epochs,
create_info,
read_epochs_eeglab,
read_evokeds,
read_evokeds_mff,
)
from mne.datasets import misc, testing
from mne.export import export_evokeds, export_evokeds_mff
from mne.fixes import _compare_version
from mne.io import (
RawArray,
read_raw_brainvision,
read_raw_edf,
read_raw_eeglab,
read_raw_fif,
)
from mne.tests.test_epochs import _get_data
from mne.utils import (
_check_edfio_installed,
_record_warnings,
_resource_path,
object_diff,
)
fname_evoked = _resource_path("mne.io.tests.data", "test-ave.fif")
fname_raw = _resource_path("mne.io.tests.data", "test_raw.fif")
data_path = testing.data_path(download=False)
egi_evoked_fname = data_path / "EGI" / "test_egi_evoked.mff"
misc_path = misc.data_path(download=False)
@pytest.mark.parametrize(
["meas_date", "orig_time", "ext"],
[
[None, None, ".vhdr"],
[datetime(2022, 12, 3, 19, 1, 10, 720100, tzinfo=timezone.utc), None, ".eeg"],
],
)
def test_export_raw_pybv(tmp_path, meas_date, orig_time, ext):
"""Test saving a Raw instance to BrainVision format via pybv."""
pytest.importorskip("pybv")
raw = read_raw_fif(fname_raw, preload=True)
raw.apply_proj()
raw.set_meas_date(meas_date)
# add some annotations
annots = Annotations(
onset=[3, 6, 9, 12, 14], # seconds
duration=[1, 1, 0.5, 0.25, 9], # seconds
description=[
"Stimulus/S 1",
"Stimulus/S2.50",
"Response/R101",
"Look at this",
"Comment/And at this",
],
ch_names=[(), (), (), ("EEG 001",), ("EEG 001", "EEG 002")],
orig_time=orig_time,
)
raw.set_annotations(annots)
temp_fname = tmp_path / ("test" + ext)
with (
_record_warnings(),
pytest.warns(RuntimeWarning, match="'short' format. Converting"),
):
raw.export(temp_fname)
raw_read = read_raw_brainvision(str(temp_fname).replace(".eeg", ".vhdr"))
assert raw.ch_names == raw_read.ch_names
assert_allclose(raw.times, raw_read.times)
assert_allclose(raw.get_data(), raw_read.get_data())
def test_export_raw_eeglab(tmp_path):
"""Test saving a Raw instance to EEGLAB's set format."""
pytest.importorskip("eeglabio")
raw = read_raw_fif(fname_raw, preload=True)
raw.apply_proj()
temp_fname = tmp_path / "test.set"
raw.export(temp_fname)
raw.drop_channels([ch for ch in ["epoc"] if ch in raw.ch_names])
with pytest.warns(RuntimeWarning, match="is above the 99th percentile"):
raw_read = read_raw_eeglab(temp_fname, preload=True, montage_units="m")
assert raw.ch_names == raw_read.ch_names
cart_coords = np.array([d["loc"][:3] for d in raw.info["chs"]]) # just xyz
cart_coords_read = np.array([d["loc"][:3] for d in raw_read.info["chs"]])
assert_allclose(cart_coords, cart_coords_read)
assert_allclose(raw.times, raw_read.times)
assert_allclose(raw.get_data(), raw_read.get_data())
# test overwrite
with pytest.raises(FileExistsError, match="Destination file exists"):
raw.export(temp_fname, overwrite=False)
raw.export(temp_fname, overwrite=True)
# test pathlib.Path files
raw.export(Path(temp_fname), overwrite=True)
# test warning with unapplied projectors
raw = read_raw_fif(fname_raw, preload=True)
with pytest.warns(RuntimeWarning, match="Raw instance has unapplied projectors."):
raw.export(temp_fname, overwrite=True)
@pytest.mark.parametrize("tmin", (0, 1, 5, 10))
def test_export_raw_eeglab_annotations(tmp_path, tmin):
"""Test annotations in the exported EEGLAB file.
All annotations should be preserved and onset corrected.
"""
pytest.importorskip("eeglabio")
raw = read_raw_fif(fname_raw, preload=True)
raw.apply_proj()
annotations = Annotations(
onset=[0.01, 0.05, 0.90, 1.05],
duration=[0, 1, 0, 0],
description=["test1", "test2", "test3", "test4"],
ch_names=[["MEG 0113"], ["MEG 0113", "MEG 0132"], [], ["MEG 0143"]],
)
raw.set_annotations(annotations)
raw.crop(tmin)
# export
temp_fname = tmp_path / "test.set"
raw.export(temp_fname)
# read in the file
with pytest.warns(RuntimeWarning, match="is above the 99th percentile"):
raw_read = read_raw_eeglab(temp_fname, preload=True, montage_units="m")
assert raw_read.first_time == 0 # exportation resets first_time
valid_annot = (
raw.annotations.onset >= tmin
) # only annotations in the cropped range gets exported
# compare annotations before and after export
assert_array_almost_equal(
raw.annotations.onset[valid_annot] - raw.first_time,
raw_read.annotations.onset,
)
assert_array_equal(
raw.annotations.duration[valid_annot], raw_read.annotations.duration
)
assert_array_equal(
raw.annotations.description[valid_annot], raw_read.annotations.description
)
def _create_raw_for_edf_tests(stim_channel_index=None):
rng = np.random.RandomState(12345)
ch_types = [
"eeg",
"eeg",
"ecog",
"ecog",
"seeg",
"eog",
"ecg",
"emg",
"dbs",
"bio",
]
if stim_channel_index is not None:
ch_types.insert(stim_channel_index, "stim")
ch_names = np.arange(len(ch_types)).astype(str).tolist()
info = create_info(ch_names, sfreq=1000, ch_types=ch_types)
data = rng.random(size=(len(ch_names), 2000)) * 1e-5
return RawArray(data, info)
edfio_mark = pytest.mark.skipif(
not _check_edfio_installed(strict=False), reason="requires edfio"
)
@edfio_mark()
def test_double_export_edf(tmp_path):
"""Test exporting an EDF file multiple times."""
raw = _create_raw_for_edf_tests(stim_channel_index=2)
raw.info.set_meas_date("2023-09-04 14:53:09.000")
raw.set_annotations(Annotations(onset=[1], duration=[0], description=["test"]))
# include subject info and measurement date
raw.info["subject_info"] = dict(
his_id="12345",
first_name="mne",
last_name="python",
birthday=date(1992, 1, 20),
sex=1,
weight=78.3,
height=1.75,
hand=3,
)
# export once
temp_fname = tmp_path / "test.edf"
raw.export(temp_fname, add_ch_type=True)
raw_read = read_raw_edf(temp_fname, infer_types=True, preload=True)
# export again
raw_read.export(temp_fname, add_ch_type=True, overwrite=True)
raw_read = read_raw_edf(temp_fname, infer_types=True, preload=True)
assert raw.ch_names == raw_read.ch_names
assert_array_almost_equal(raw.get_data(), raw_read.get_data(), decimal=10)
assert_array_equal(raw.times, raw_read.times)
# check info
for key in set(raw.info) - {"chs"}:
assert raw.info[key] == raw_read.info[key]
orig_ch_types = raw.get_channel_types()
read_ch_types = raw_read.get_channel_types()
assert_array_equal(orig_ch_types, read_ch_types)
@edfio_mark()
def test_edf_physical_range(tmp_path):
"""Test exporting an EDF file with different physical range settings."""
ch_types = ["eeg"] * 4
ch_names = np.arange(len(ch_types)).astype(str).tolist()
fs = 1000
info = create_info(len(ch_types), sfreq=fs, ch_types=ch_types)
data = np.tile(
np.sin(2 * np.pi * 10 * np.arange(0, 2, 1 / fs)) * 1e-5, (len(ch_names), 1)
)
data = (data.T + [0.1, 0, 0, -0.1]).T # add offsets
raw = RawArray(data, info)
# export with physical range per channel type (default)
temp_fname = tmp_path / "test_auto.edf"
raw.export(temp_fname)
raw_read = read_raw_edf(temp_fname, preload=True)
with pytest.raises(AssertionError, match="Arrays are not almost equal"):
assert_array_almost_equal(raw.get_data(), raw_read.get_data(), decimal=10)
# export with physical range per channel
temp_fname = tmp_path / "test_per_channel.edf"
raw.export(temp_fname, physical_range="channelwise")
raw_read = read_raw_edf(temp_fname, preload=True)
assert_array_almost_equal(raw.get_data(), raw_read.get_data(), decimal=10)
@edfio_mark()
@pytest.mark.parametrize("pad_width", (1, 10, 100, 500, 999))
def test_edf_padding(tmp_path, pad_width):
"""Test exporting an EDF file with not-equal-length data blocks."""
ch_types = ["eeg"] * 4
ch_names = np.arange(len(ch_types)).astype(str).tolist()
fs = 1000
info = create_info(len(ch_types), sfreq=fs, ch_types=ch_types)
data = np.tile(
np.sin(2 * np.pi * 10 * np.arange(0, 2, 1 / fs)) * 1e-5, (len(ch_names), 1)
)[:, 0:-pad_width] # remove last pad_width samples
raw = RawArray(data, info)
# export with physical range per channel type (default)
temp_fname = tmp_path / "test.edf"
with pytest.warns(
RuntimeWarning,
match=(
"EDF format requires equal-length data blocks.*"
f"{pad_width / 1000:.3g} seconds of edge values were appended.*"
),
):
raw.export(temp_fname)
# read in the file
raw_read = read_raw_edf(temp_fname, preload=True)
assert raw.n_times == raw_read.n_times - pad_width
edge_data = raw_read.get_data()[:, -pad_width - 1]
pad_data = raw_read.get_data()[:, -pad_width:]
assert_array_almost_equal(
raw.get_data(), raw_read.get_data()[:, :-pad_width], decimal=10
)
assert_array_almost_equal(
pad_data, np.tile(edge_data, (pad_width, 1)).T, decimal=10
)
assert "BAD_ACQ_SKIP" in raw_read.annotations.description
assert_array_almost_equal(raw_read.annotations.onset[0], raw.times[-1] + 1 / fs)
assert_array_almost_equal(raw_read.annotations.duration[0], pad_width / fs)
@edfio_mark()
@pytest.mark.parametrize("tmin", (0, 0.005, 0.03, 1))
def test_export_edf_annotations(tmp_path, tmin):
"""Test annotations in the exported EDF file.
All annotations should be preserved and onset corrected.
"""
raw = _create_raw_for_edf_tests()
annotations = Annotations(
onset=[0.01, 0.05, 0.90, 1.05],
duration=[0, 1, 0, 0],
description=["test1", "test2", "test3", "test4"],
ch_names=[["0"], ["0", "1"], [], ["1"]],
)
raw.set_annotations(annotations)
raw.crop(tmin)
assert raw.first_time == tmin
if raw.n_times % raw.info["sfreq"] == 0:
expectation = nullcontext()
else:
expectation = pytest.warns(
RuntimeWarning, match="EDF format requires equal-length data blocks"
)
# export
temp_fname = tmp_path / "test.edf"
with expectation:
raw.export(temp_fname)
# read in the file
raw_read = read_raw_edf(temp_fname, preload=True)
assert raw_read.first_time == 0 # exportation resets first_time
bad_annot = raw_read.annotations.description == "BAD_ACQ_SKIP"
if bad_annot.any():
raw_read.annotations.delete(bad_annot)
valid_annot = (
raw.annotations.onset >= tmin
) # only annotations in the cropped range gets exported
# compare annotations before and after export
assert_array_almost_equal(
raw.annotations.onset[valid_annot] - raw.first_time, raw_read.annotations.onset
)
assert_array_equal(
raw.annotations.duration[valid_annot], raw_read.annotations.duration
)
assert_array_equal(
raw.annotations.description[valid_annot], raw_read.annotations.description
)
assert_array_equal(
raw.annotations.ch_names[valid_annot], raw_read.annotations.ch_names
)
@edfio_mark()
def test_rawarray_edf(tmp_path):
"""Test saving a Raw array with integer sfreq to EDF."""
raw = _create_raw_for_edf_tests()
# include subject info and measurement date
raw.info["subject_info"] = dict(
first_name="mne",
last_name="python",
birthday=date(1992, 1, 20),
sex=1,
hand=3,
)
time_now = datetime.now()
meas_date = datetime(
year=time_now.year,
month=time_now.month,
day=time_now.day,
hour=time_now.hour,
minute=time_now.minute,
second=time_now.second,
tzinfo=timezone.utc,
)
raw.set_meas_date(meas_date)
temp_fname = tmp_path / "test.edf"
raw.export(temp_fname, add_ch_type=True)
raw_read = read_raw_edf(temp_fname, infer_types=True, preload=True)
assert raw.ch_names == raw_read.ch_names
assert_array_almost_equal(raw.get_data(), raw_read.get_data(), decimal=10)
assert_array_equal(raw.times, raw_read.times)
orig_ch_types = raw.get_channel_types()
read_ch_types = raw_read.get_channel_types()
assert_array_equal(orig_ch_types, read_ch_types)
assert raw.info["meas_date"] == raw_read.info["meas_date"]
@edfio_mark()
def test_edf_export_non_voltage_channels(tmp_path):
"""Test saving a Raw array containing a non-voltage channel."""
temp_fname = tmp_path / "test.edf"
raw = _create_raw_for_edf_tests()
raw.set_channel_types({"9": "hbr"}, on_unit_change="ignore")
raw.export(temp_fname, overwrite=True)
# data should match up to the non-accepted channel
raw_read = read_raw_edf(temp_fname, preload=True)
assert raw.ch_names == raw_read.ch_names
assert_array_almost_equal(raw.get_data()[:-1], raw_read.get_data()[:-1], decimal=10)
assert_array_almost_equal(raw.get_data()[-1], raw_read.get_data()[-1], decimal=5)
assert_array_equal(raw.times, raw_read.times)
@edfio_mark()
def test_channel_label_too_long_for_edf_raises_error(tmp_path):
"""Test trying to save an EDF where a channel label is longer than 16 characters."""
raw = _create_raw_for_edf_tests()
raw.rename_channels({"1": "abcdefghijklmnopqrstuvwxyz"})
with pytest.raises(RuntimeError, match="Signal label"):
raw.export(tmp_path / "test.edf")
@edfio_mark()
def test_measurement_date_outside_range_valid_for_edf(tmp_path):
"""Test trying to save an EDF with a measurement date before 1985-01-01."""
raw = _create_raw_for_edf_tests()
raw.set_meas_date(datetime(year=1984, month=1, day=1, tzinfo=timezone.utc))
with pytest.raises(ValueError, match="EDF only allows dates from 1985 to 2084"):
raw.export(tmp_path / "test.edf", overwrite=True)
@pytest.mark.filterwarnings("ignore:Data has a non-integer:RuntimeWarning")
@pytest.mark.parametrize(
("physical_range", "exceeded_bound"),
[
((-1e6, 0), "maximum"),
((0, 1e6), "minimum"),
],
)
@edfio_mark()
def test_export_edf_signal_clipping(tmp_path, physical_range, exceeded_bound):
"""Test if exporting data exceeding physical min/max clips and emits a warning."""
raw = read_raw_fif(fname_raw)
raw.pick(picks=["eeg", "ecog", "seeg"]).load_data()
temp_fname = tmp_path / "test.edf"
with (
_record_warnings(),
pytest.warns(RuntimeWarning, match=f"The {exceeded_bound}"),
):
raw.export(temp_fname, physical_range=physical_range)
raw_read = read_raw_edf(temp_fname, preload=True)
assert raw_read.get_data().min() >= physical_range[0]
assert raw_read.get_data().max() <= physical_range[1]
@edfio_mark()
def test_export_edf_with_constant_channel(tmp_path):
"""Test if exporting to edf works if a channel contains only constant values."""
temp_fname = tmp_path / "test.edf"
raw = RawArray(np.zeros((1, 10)), info=create_info(1, 1))
raw.export(temp_fname)
raw_read = read_raw_edf(temp_fname, preload=True)
assert_array_equal(raw_read.get_data(), np.zeros((1, 10)))
@edfio_mark()
@pytest.mark.parametrize(
("input_path", "warning_msg"),
[
(fname_raw, "Data has a non-integer"),
pytest.param(
misc_path / "ecog" / "sample_ecog_ieeg.fif",
"EDF format requires",
marks=[pytest.mark.slowtest, misc._pytest_mark()],
),
],
)
def test_export_raw_edf(tmp_path, input_path, warning_msg):
"""Test saving a Raw instance to EDF format."""
raw = read_raw_fif(input_path)
# only test with EEG channels
raw.pick(picks=["eeg", "ecog", "seeg"]).load_data()
temp_fname = tmp_path / "test.edf"
with pytest.warns(RuntimeWarning, match=warning_msg):
raw.export(temp_fname)
if "epoc" in raw.ch_names:
raw.drop_channels(["epoc"])
raw_read = read_raw_edf(temp_fname, preload=True)
assert raw.ch_names == raw_read.ch_names
# only compare the original length, since extra zeros are appended
orig_raw_len = len(raw)
# assert data and times are not different
# Due to the physical range of the data, reading and writing is
# not lossless. For example, a physical min/max of -/+ 3200 uV
# will result in a resolution of 0.09 uV. This resolution
# though is acceptable for most EEG manufacturers.
assert_array_almost_equal(
raw.get_data(), raw_read.get_data()[:, :orig_raw_len], decimal=8
)
# Due to the data record duration limitations of EDF files, one
# cannot store arbitrary float sampling rate exactly. Usually this
# results in two sampling rates that are off by very low number of
# decimal points. This for practical purposes does not matter
# but will result in an error when say the number of time points
# is very very large.
assert_allclose(raw.times, raw_read.times[:orig_raw_len], rtol=0, atol=1e-5)
@edfio_mark()
def test_export_raw_edf_does_not_fail_on_empty_header_fields(tmp_path):
"""Test writing a Raw instance with empty header fields to EDF."""
rng = np.random.RandomState(123456)
ch_types = ["eeg"]
info = create_info(len(ch_types), sfreq=1000, ch_types=ch_types)
info["subject_info"] = {
"his_id": "",
"first_name": "",
"middle_name": "",
"last_name": "",
}
info["device_info"] = {"type": "123"}
data = rng.random(size=(len(ch_types), 1000)) * 1e-5
raw = RawArray(data, info)
raw.export(tmp_path / "test.edf", add_ch_type=True)
@pytest.mark.xfail(reason="eeglabio (usage?) bugs that should be fixed")
@pytest.mark.parametrize("preload", (True, False))
def test_export_epochs_eeglab(tmp_path, preload):
"""Test saving an Epochs instance to EEGLAB's set format."""
eeglabio = pytest.importorskip("eeglabio")
raw, events = _get_data()[:2]
raw.load_data()
epochs = Epochs(raw, events, preload=preload)
temp_fname = tmp_path / "test.set"
# TODO: eeglabio 0.2 warns about invalid events
if _compare_version(eeglabio.__version__, "==", "0.0.2-1"):
ctx = _record_warnings
else:
ctx = nullcontext
with ctx():
epochs.export(temp_fname)
epochs.drop_channels([ch for ch in ["epoc", "STI 014"] if ch in epochs.ch_names])
epochs_read = read_epochs_eeglab(temp_fname, verbose="error") # head radius
assert epochs.ch_names == epochs_read.ch_names
cart_coords = np.array([d["loc"][:3] for d in epochs.info["chs"]]) # just xyz
cart_coords_read = np.array([d["loc"][:3] for d in epochs_read.info["chs"]])
assert_allclose(cart_coords, cart_coords_read)
assert_array_equal(epochs.events[:, 0], epochs_read.events[:, 0]) # latency
assert epochs.event_id.keys() == epochs_read.event_id.keys() # just keys
assert_allclose(epochs.times, epochs_read.times)
assert_allclose(epochs.get_data(), epochs_read.get_data())
# test overwrite
with pytest.raises(FileExistsError, match="Destination file exists"):
epochs.export(temp_fname, overwrite=False)
with ctx():
epochs.export(temp_fname, overwrite=True)
# test pathlib.Path files
with ctx():
epochs.export(Path(temp_fname), overwrite=True)
# test warning with unapplied projectors
epochs = Epochs(raw, events, preload=preload, proj=False)
with pytest.warns(
RuntimeWarning, match="Epochs instance has unapplied projectors."
):
epochs.export(Path(temp_fname), overwrite=True)
@pytest.mark.filterwarnings("ignore::FutureWarning")
@testing.requires_testing_data
@pytest.mark.parametrize("fmt", ("auto", "mff"))
@pytest.mark.parametrize("do_history", (True, False))
def test_export_evokeds_to_mff(tmp_path, fmt, do_history):
"""Test exporting evoked dataset to MFF."""
pytest.importorskip("mffpy", "0.5.7")
pytest.importorskip("defusedxml")
evoked = read_evokeds_mff(egi_evoked_fname)
export_fname = tmp_path / "evoked.mff"
history = [
{
"name": "Test Segmentation",
"method": "Segmentation",
"settings": ["Setting 1", "Setting 2"],
"results": ["Result 1", "Result 2"],
},
{
"name": "Test Averaging",
"method": "Averaging",
"settings": ["Setting 1", "Setting 2"],
"results": ["Result 1", "Result 2"],
},
]
if do_history:
export_evokeds_mff(export_fname, evoked, history=history)
else:
export_evokeds(export_fname, evoked, fmt=fmt)
# Drop non-EEG channels
evoked = [ave.drop_channels(["ECG", "EMG"]) for ave in evoked]
evoked_exported = read_evokeds_mff(export_fname)
assert len(evoked) == len(evoked_exported)
for ave, ave_exported in zip(evoked, evoked_exported):
# Compare infos
assert object_diff(ave_exported.info, ave.info) == ""
# Compare data
assert_allclose(ave_exported.data, ave.data)
# Compare properties
assert ave_exported.nave == ave.nave
assert ave_exported.kind == ave.kind
assert ave_exported.comment == ave.comment
assert_allclose(ave_exported.times, ave.times)
# test overwrite
with pytest.raises(FileExistsError, match="Destination file exists"):
if do_history:
export_evokeds_mff(export_fname, evoked, history=history, overwrite=False)
else:
export_evokeds(export_fname, evoked, overwrite=False)
if do_history:
export_evokeds_mff(export_fname, evoked, history=history, overwrite=True)
else:
export_evokeds(export_fname, evoked, overwrite=True)
# test export from evoked directly
evoked[0].export(export_fname, overwrite=True)
@pytest.mark.filterwarnings("ignore::FutureWarning")
@testing.requires_testing_data
def test_export_to_mff_no_device():
"""Test no device type throws ValueError."""
pytest.importorskip("mffpy", "0.5.7")
pytest.importorskip("defusedxml")
evoked = read_evokeds_mff(egi_evoked_fname, condition="Category 1")
evoked.info["device_info"] = None
with pytest.raises(ValueError, match="No device type."):
export_evokeds("output.mff", evoked)
@pytest.mark.filterwarnings("ignore::FutureWarning")
def test_export_to_mff_incompatible_sfreq():
"""Test non-whole number sampling frequency throws ValueError."""
pytest.importorskip("mffpy", "0.5.7")
evoked = read_evokeds(fname_evoked)
with pytest.raises(ValueError, match=f"sfreq: {evoked[0].info['sfreq']}"):
export_evokeds("output.mff", evoked)
@pytest.mark.parametrize(
"fmt,ext",
[("EEGLAB", "set"), ("EDF", "edf"), ("BrainVision", "vhdr"), ("auto", "vhdr")],
)
def test_export_evokeds_unsupported_format(fmt, ext):
"""Test exporting evoked dataset to non-supported formats."""
evoked = read_evokeds(fname_evoked)
errstr = fmt.lower() if fmt != "auto" else "vhdr"
with pytest.raises(ValueError, match=f"Format '{errstr}' is not .*"):
export_evokeds(f"output.{ext}", evoked, fmt=fmt)