Esempio n. 1
0
def test_export_raw_edf(tmp_path, dataset, format):
    """Test saving a Raw instance to EDF format."""
    if dataset == 'test':
        fname = _resource_path('mne.io.tests.data', 'test_raw.fif')
        raw = read_raw_fif(fname)
    elif dataset == 'misc':
        fname = op.join(misc.data_path(), 'ecog', 'sample_ecog_ieeg.fif')
        raw = read_raw_fif(fname)

    # only test with EEG channels
    raw.pick_types(eeg=True, ecog=True, seeg=True)
    raw.load_data()
    orig_ch_names = raw.ch_names
    temp_fname = op.join(str(tmp_path), f'test.{format}')

    # test runtime errors
    with pytest.raises(RuntimeError, match='The maximum'), \
            pytest.warns(RuntimeWarning, match='Data has a non-integer'):
        raw.export(temp_fname, physical_range=(-1e6, 0))
    with pytest.raises(RuntimeError, match='The minimum'), \
            pytest.warns(RuntimeWarning, match='Data has a non-integer'):
        raw.export(temp_fname, physical_range=(0, 1e6))

    if dataset == 'test':
        with pytest.warns(RuntimeWarning, match='Data has a non-integer'):
            raw.export(temp_fname)
    elif dataset == 'misc':
        with pytest.warns(RuntimeWarning, match='EDF format requires'):
            raw.export(temp_fname)

    if 'epoc' in raw.ch_names:
        raw.drop_channels(['epoc'])

    raw_read = read_raw_edf(temp_fname, preload=True)
    assert orig_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=4)

    # 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)
Esempio n. 2
0
import numpy as np
from numpy.testing import (assert_allclose, assert_array_almost_equal,
                           assert_array_equal)

from mne import (read_epochs_eeglab, Epochs, read_evokeds, read_evokeds_mff,
                 Annotations)
from mne.datasets import testing, misc
from mne.export import export_evokeds, export_evokeds_mff
from mne.io import (read_raw_fif, read_raw_eeglab, read_raw_edf,
                    read_raw_brainvision)
from mne.utils import (_check_eeglabio_installed, requires_version,
                       object_diff, _check_edflib_installed, _resource_path,
                       _check_pybv_installed)
from mne.tests.test_epochs import _get_data

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 = op.join(data_path, 'EGI', 'test_egi_evoked.mff')
misc_path = misc.data_path(download=False)


@pytest.mark.skipif(not _check_pybv_installed(strict=False),
                    reason='pybv not installed')
@pytest.mark.parametrize(['meas_date', 'orig_time', 'ext'], [
    [None, None, '.vhdr'],
    [
        datetime(2022, 12, 3, 19, 1, 10, 720100, tzinfo=timezone.utc), None,
        '.eeg'
    ],