Ejemplo n.º 1
0
def test_sleep_physionet_age(physionet_tmpdir, monkeypatch, download_is_error):
    """Test Sleep Physionet URL handling."""
    # check download_is_error patching
    with pytest.raises(AssertionError, match='Test should not download'):
        age.fetch_data(subjects=[0], recording=[1], path=physionet_tmpdir)
    # then patch
    my_func = _FakeFetch()
    monkeypatch.setattr(pooch, 'retrieve', my_func)

    paths = age.fetch_data(subjects=[0], recording=[1], path=physionet_tmpdir)
    assert_array_equal(_keep_basename_only(paths),
                       [['SC4001E0-PSG.edf', 'SC4001EC-Hypnogram.edf']])

    paths = age.fetch_data(subjects=[0, 1],
                           recording=[1],
                           path=physionet_tmpdir)
    assert_array_equal(_keep_basename_only(paths),
                       [['SC4001E0-PSG.edf', 'SC4001EC-Hypnogram.edf'],
                        ['SC4011E0-PSG.edf', 'SC4011EH-Hypnogram.edf']])

    paths = age.fetch_data(subjects=[0],
                           recording=[1, 2],
                           path=physionet_tmpdir)
    assert_array_equal(_keep_basename_only(paths),
                       [['SC4001E0-PSG.edf', 'SC4001EC-Hypnogram.edf'],
                        ['SC4002E0-PSG.edf', 'SC4002EC-Hypnogram.edf']])

    EXPECTED_CALLS = ({
        'name': 'SC4001E0-PSG.edf',
        'hash': 'adabd3b01fc7bb75c523a974f38ee3ae4e57b40f'
    }, {
        'name': 'SC4001EC-Hypnogram.edf',
        'hash': '21c998eadc8b1e3ea6727d3585186b8f76e7e70b'
    }, {
        'name': 'SC4001E0-PSG.edf',
        'hash': 'adabd3b01fc7bb75c523a974f38ee3ae4e57b40f'
    }, {
        'name': 'SC4001EC-Hypnogram.edf',
        'hash': '21c998eadc8b1e3ea6727d3585186b8f76e7e70b'
    }, {
        'name': 'SC4011E0-PSG.edf',
        'hash': '4d17451f7847355bcab17584de05e7e1df58c660'
    }, {
        'name': 'SC4011EH-Hypnogram.edf',
        'hash': 'd582a3cbe2db481a362af890bc5a2f5ca7c878dc'
    }, {
        'name': 'SC4001E0-PSG.edf',
        'hash': 'adabd3b01fc7bb75c523a974f38ee3ae4e57b40f'
    }, {
        'name': 'SC4001EC-Hypnogram.edf',
        'hash': '21c998eadc8b1e3ea6727d3585186b8f76e7e70b'
    }, {
        'name': 'SC4002E0-PSG.edf',
        'hash': 'c6b6d7a8605cc7e7602b6028ee77f6fbf5f7581d'
    }, {
        'name': 'SC4002EC-Hypnogram.edf',
        'hash': '386230188a3552b1fc90bba0fb7476ceaca174b6'
    })
    base_path = age.data_path(path=physionet_tmpdir)
    _check_mocked_function_calls(my_func, EXPECTED_CALLS, base_path)
Ejemplo n.º 2
0
def test_sleep_physionet_age(physionet_tmpdir, mocker):
    """Test Sleep Physionet URL handling."""
    my_func = mocker.patch('mne.datasets.sleep_physionet._utils._fetch_file',
                           side_effect=_fake_fetch_file)

    params = {'path': physionet_tmpdir, 'update_path': False}

    with pytest.raises(ValueError, match='Only subjects 0 to 19 are'):
        paths = age.fetch_data(subjects=[20], recording=[1], **params)

    paths = age.fetch_data(subjects=[0], recording=[1], **params)
    assert_array_equal(_keep_basename_only(paths),
                       [['SC4001E0-PSG.edf', 'SC4001EC-Hypnogram.edf']])

    paths = age.fetch_data(subjects=[0, 1], recording=[1], **params)
    assert_array_equal(_keep_basename_only(paths),
                       [['SC4001E0-PSG.edf', 'SC4001EC-Hypnogram.edf'],
                        ['SC4011E0-PSG.edf', 'SC4011EH-Hypnogram.edf']])

    paths = age.fetch_data(subjects=[0], recording=[1, 2], **params)
    assert_array_equal(_keep_basename_only(paths),
                       [['SC4001E0-PSG.edf', 'SC4001EC-Hypnogram.edf'],
                        ['SC4002E0-PSG.edf', 'SC4002EC-Hypnogram.edf']])

    EXPECTED_CALLS = ({
        'name': 'SC4001E0-PSG.edf',
        'hash': 'adabd3b01fc7bb75c523a974f38ee3ae4e57b40f'
    }, {
        'name': 'SC4001EC-Hypnogram.edf',
        'hash': '21c998eadc8b1e3ea6727d3585186b8f76e7e70b'
    }, {
        'name': 'SC4001E0-PSG.edf',
        'hash': 'adabd3b01fc7bb75c523a974f38ee3ae4e57b40f'
    }, {
        'name': 'SC4001EC-Hypnogram.edf',
        'hash': '21c998eadc8b1e3ea6727d3585186b8f76e7e70b'
    }, {
        'name': 'SC4011E0-PSG.edf',
        'hash': '4d17451f7847355bcab17584de05e7e1df58c660'
    }, {
        'name': 'SC4011EH-Hypnogram.edf',
        'hash': 'd582a3cbe2db481a362af890bc5a2f5ca7c878dc'
    }, {
        'name': 'SC4001E0-PSG.edf',
        'hash': 'adabd3b01fc7bb75c523a974f38ee3ae4e57b40f'
    }, {
        'name': 'SC4001EC-Hypnogram.edf',
        'hash': '21c998eadc8b1e3ea6727d3585186b8f76e7e70b'
    }, {
        'name': 'SC4002E0-PSG.edf',
        'hash': 'c6b6d7a8605cc7e7602b6028ee77f6fbf5f7581d'
    }, {
        'name': 'SC4002EC-Hypnogram.edf',
        'hash': '386230188a3552b1fc90bba0fb7476ceaca174b6'
    })
    base_path = age.data_path(path=physionet_tmpdir)
    _check_mocked_function_calls(mocked_func=my_func,
                                 call_fname_hash_pairs=EXPECTED_CALLS,
                                 base_path=base_path)
Ejemplo n.º 3
0
def test_sleep_physionet_age(physionet_tmpdir, mocker):
    """Test Sleep Physionet URL handling."""
    my_func = mocker.patch('mne.datasets.sleep_physionet._utils._fetch_file',
                           side_effect=_fake_fetch_file)

    params = {'path': physionet_tmpdir, 'update_path': False}

    with pytest.raises(ValueError, match='Only subjects 0 to 19 are'):
        paths = age.fetch_data(subjects=[20], recording=[1], **params)

    paths = age.fetch_data(subjects=[0], recording=[1], **params)
    assert_array_equal(_keep_basename_only(paths),
                       [['SC4001E0-PSG.edf', 'SC4001EC-Hypnogram.edf']])

    paths = age.fetch_data(subjects=[0, 1], recording=[1], **params)
    assert_array_equal(_keep_basename_only(paths),
                       [['SC4001E0-PSG.edf', 'SC4001EC-Hypnogram.edf'],
                        ['SC4011E0-PSG.edf', 'SC4011EH-Hypnogram.edf']])

    paths = age.fetch_data(subjects=[0], recording=[1, 2], **params)
    assert_array_equal(_keep_basename_only(paths),
                       [['SC4001E0-PSG.edf', 'SC4001EC-Hypnogram.edf'],
                        ['SC4002E0-PSG.edf', 'SC4002EC-Hypnogram.edf']])

    EXPECTED_CALLS = (
        {'name': 'SC4001E0-PSG.edf',
         'hash': 'adabd3b01fc7bb75c523a974f38ee3ae4e57b40f'},
        {'name': 'SC4001EC-Hypnogram.edf',
         'hash': '21c998eadc8b1e3ea6727d3585186b8f76e7e70b'},
        {'name': 'SC4001E0-PSG.edf',
         'hash': 'adabd3b01fc7bb75c523a974f38ee3ae4e57b40f'},
        {'name': 'SC4001EC-Hypnogram.edf',
         'hash': '21c998eadc8b1e3ea6727d3585186b8f76e7e70b'},
        {'name': 'SC4011E0-PSG.edf',
         'hash': '4d17451f7847355bcab17584de05e7e1df58c660'},
        {'name': 'SC4011EH-Hypnogram.edf',
         'hash': 'd582a3cbe2db481a362af890bc5a2f5ca7c878dc'},
        {'name': 'SC4001E0-PSG.edf',
         'hash': 'adabd3b01fc7bb75c523a974f38ee3ae4e57b40f'},
        {'name': 'SC4001EC-Hypnogram.edf',
         'hash': '21c998eadc8b1e3ea6727d3585186b8f76e7e70b'},
        {'name': 'SC4002E0-PSG.edf',
         'hash': 'c6b6d7a8605cc7e7602b6028ee77f6fbf5f7581d'},
        {'name': 'SC4002EC-Hypnogram.edf',
         'hash': '386230188a3552b1fc90bba0fb7476ceaca174b6'})
    base_path = age.data_path(path=physionet_tmpdir)
    _check_mocked_function_calls(mocked_func=my_func,
                                 call_fname_hash_pairs=EXPECTED_CALLS,
                                 base_path=base_path)
Ejemplo n.º 4
0
    def __init__(self,
                 subject_ids=None,
                 recording_ids=None,
                 preload=False,
                 load_eeg_only=True,
                 crop_wake_mins=30,
                 crop=None):
        if subject_ids is None:
            subject_ids = range(83)
        if recording_ids is None:
            recording_ids = [1, 2]

        paths = fetch_data(subject_ids,
                           recording=recording_ids,
                           on_missing='warn')

        all_base_ds = list()
        for p in paths:
            raw, desc = self._load_raw(p[0],
                                       p[1],
                                       preload=preload,
                                       load_eeg_only=load_eeg_only,
                                       crop_wake_mins=crop_wake_mins,
                                       crop=crop)
            base_ds = BaseDataset(raw, desc)
            all_base_ds.append(base_ds)
        super().__init__(all_base_ds)
Ejemplo n.º 5
0
def test_sleep_physionet_age_missing_subjects(physionet_tmpdir, subject,
                                              download_is_error):
    """Test handling of missing subjects in Sleep Physionet age fetcher."""
    with pytest.raises(ValueError,
                       match='This dataset contains subjects 0 to 82'):
        age.fetch_data(subjects=[subject],
                       recording=[1],
                       on_missing='raise',
                       path=physionet_tmpdir)
    with pytest.warns(RuntimeWarning,
                      match='This dataset contains subjects 0 to 82'):
        age.fetch_data(subjects=[subject],
                       recording=[1],
                       on_missing='warn',
                       path=physionet_tmpdir)
    paths = age.fetch_data(subjects=[subject],
                           recording=[1],
                           on_missing='ignore',
                           path=physionet_tmpdir)
    assert paths == []
Ejemplo n.º 6
0
def test_sleep_physionet_age_missing_recordings(physionet_tmpdir, subject,
                                                recording, download_is_error):
    """Test handling of missing recordings in Sleep Physionet age fetcher."""
    with pytest.raises(ValueError,
                       match=f'Requested recording {recording} for subject'):
        age.fetch_data(subjects=[subject],
                       recording=[recording],
                       on_missing='raise',
                       path=physionet_tmpdir)
    with pytest.warns(RuntimeWarning,
                      match=f'Requested recording {recording} for subject'):
        age.fetch_data(subjects=[subject],
                       recording=[recording],
                       on_missing='warn',
                       path=physionet_tmpdir)
    paths = age.fetch_data(subjects=[subject],
                           recording=[recording],
                           on_missing='ignore',
                           path=physionet_tmpdir)
    assert paths == []
Ejemplo n.º 7
0
    def fetch_and_extract(self, data_folder, subject_ids):
        prepared_filenames = []
        orig_filenames = fetch_data(subject_ids)
        for psg_file, _hypnogram_file in orig_filenames:
            target_filename = Path(psg_file).name.replace(".edf", ".npy")
            target_filename = data_folder / target_filename
            prepared_filenames.append(target_filename)
            if target_filename.exists():
                continue

            print("Loading PSG file with MNE...")
            raw_edf = mne.io.read_raw_edf(psg_file)
            raw_edf.set_channel_types(self.mapping)

            eeg_array = raw_edf.get_data(picks="eeg")
            eeg_array = np.ascontiguousarray(eeg_array.T, dtype=np.float32)
            scale = eeg_array.std(axis=0).mean()
            eeg_array /= scale

            print(f"Saving scale EEG channel data to {target_filename}...")
            np.save(target_filename, eeg_array, allow_pickle=False)
        return prepared_filenames
Ejemplo n.º 8
0
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import FunctionTransformer


TIME_OF_W_TO_KEEP_SEC = 1800


nb_subjects =  20
# CAN BE UP TO 20
nights = [1] 
# [1,2]

subjects = range(0,nb_subjects) 

# Get path to local copies of data from Physionet files are PSG.edf and Hypnogram.edf
files = fetch_data(subjects=subjects, recording=nights)

mapping = {'EOG horizontal': 'eog',
           'Resp oro-nasal': 'misc',
           'EMG submental': 'misc',
           'Temp rectal': 'misc',
           'Event marker': 'misc'}

sleep_stages = {
    'Sleep stage W' : 0,
    'Sleep stage 1' : 1,
    'Sleep stage 2' : 2,
    'Sleep stage 3' : 3,
    'Sleep stage 4' : 3,
    'Sleep stage R' : 4,
    'Sleep stage ?' : 5
Ejemplo n.º 9
0
#
# * ``-PSG.edf`` containing the polysomnography. The :term:`raw` data from the
#   EEG helmet,
# * ``-Hypnogram.edf`` containing the :term:`annotations` recorded by an
#   expert.
#
# Combining these two in a :class:`mne.io.Raw` object then we can extract
# :term:`events` based on the descriptions of the annotations to obtain the
# :term:`epochs`.
#
# Read the PSG data and Hypnograms to create a raw object
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ALICE, BOB = 0, 1

[alice_files, bob_files] = fetch_data(subjects=[ALICE, BOB], recording=[1])

mapping = {
    'EOG horizontal': 'eog',
    'Resp oro-nasal': 'resp',
    'EMG submental': 'emg',
    'Temp rectal': 'misc',
    'Event marker': 'misc'
}

raw_train = mne.io.read_raw_edf(alice_files[0])
annot_train = mne.read_annotations(alice_files[1])

raw_train.set_annotations(annot_train, emit_warning=False)
raw_train.set_channel_types(mapping)
Ejemplo n.º 10
0
#
# * ``-PSG.edf`` containing the polysomnography. The :term:`raw` data from the
#   EEG helmet,
# * ``-Hypnogram.edf`` containing the :term:`annotations` recorded by an
#   expert.
#
# Combining these two in a :class:`mne.io.Raw` object then we can extract
# :term:`events` based on the descriptions of the annotations to obtain the
# :term:`epochs`.
#
# Read the PSG data and Hypnograms to create a raw object
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ALICE, BOB = 0, 1

[alice_files, bob_files] = fetch_data(subjects=[ALICE, BOB], recording=[1])

mapping = {'EOG horizontal': 'eog',
           'Resp oro-nasal': 'misc',
           'EMG submental': 'misc',
           'Temp rectal': 'misc',
           'Event marker': 'misc'}

raw_train = mne.io.read_raw_edf(alice_files[0])
annot_train = mne.read_annotations(alice_files[1])

raw_train.set_annotations(annot_train, emit_warning=False)
raw_train.set_channel_types(mapping)

# plot some data
raw_train.plot(duration=60, scalings='auto')
Ejemplo n.º 11
0

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Automatic Sleep Scoring with NPA.')
    args = parser.parse_args()

    os.makedirs(data_dir + '/epochs/', exist_ok=True)
    os.makedirs(data_dir + '/FOOOFs/', exist_ok=True)

    print('Arguments for this experiment:', args)

    start_all = time.time()
    all_epochs = []

    datafiles = fetch_data(subjects=list(range(20)))

    # fg = FOOOFGroup(peak_width_limits=[1, 12.0])

    for eeg_filename, label_filename in datafiles:
        eeg = mne.io.read_raw_edf(eeg_filename, preload=True, verbose=0)
        labels = mne.read_annotations(label_filename)

        subject_id = int(eeg_filename.split('\\')[-1][3:5])
        session_id = int(eeg_filename.split('\\')[-1][5])

        eeg.set_annotations(labels, emit_warning=False)
        eeg.set_channel_types(mapping)

        chunk_length = 2.
        tmax = chunk_length - 1. / eeg.info['sfreq']
Ejemplo n.º 12
0
import mne
from mne.datasets.sleep_physionet.age import fetch_data
from mne.time_frequency import psd_welch

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import FunctionTransformer



GROUPA, GROUPB = 0, 1
#加载两个组的实验数据
[groupa_files, groupb_files] = fetch_data(subjects=[GROUPA, GROUPB], recording=[1])
#通道名称映射
mapping = {'EOG horizontal': 'eog',
           'Resp oro-nasal': 'misc',
           'EMG submental': 'misc',
           'Temp rectal': 'misc',
           'Event marker': 'misc'}
#读取GROUPA组的edf文件和其对应的注释文件
raw_train = mne.io.read_raw_edf(groupa_files[0])
annot_train = mne.read_annotations(groupa_files[1])

raw_train.set_annotations(annot_train, emit_warning=False)
raw_train.set_channel_types(mapping)

# 绘制0s开始,时间窗口长度为60s的连续通道数据波形图
raw_train.plot(duration=60, scalings='auto')
#
# License: BSD Style.

import os
import numpy as np
import mne
from mne.datasets.sleep_physionet.age import fetch_data
from joblib import Memory

##############################################################################
# Define code to get epochs for all subjects

# subjects = [0, 1]
subjects = range(20)
n_groups = 5  # keep 5 subjects out
files = fetch_data(subjects=subjects, recording=[1])

mapping = {
    'EOG horizontal': 'eog',
    'Resp oro-nasal': 'misc',
    'EMG submental': 'misc',
    'Temp rectal': 'misc',
    'Event marker': 'misc'
}

mem = Memory(mne.get_config('MNE_DATA'))


@mem.cache
def get_epochs_data(raw_fname, annot_fname):
    print("Extracting Epochs from: %s" % os.path.basename(raw_fname))