Exemplo n.º 1
0
def test_read_evoked():
    """Test reading evokeds."""
    for data_type in tconf.task_types:
        all_annots = list()
        for run_index in tconf.run_inds[:2]:
            annots = hcp.read_annot(
                data_type=data_type, run_index=run_index, **hcp_params)
            all_annots.append(annots)

        evokeds = hcp.read_evokeds(data_type=data_type,
                                   kind='average', **hcp_params)

        n_average = sum(ee.kind == 'average' for ee in evokeds)
        assert_equal(n_average, len(evokeds))

        n_chans = 248
        if data_type == 'task_motor':
            n_chans += 4
        n_chans -= len(set(sum(
            [an['channels']['all'] for an in all_annots], [])))
        assert_equal(n_chans, len(evokeds[0].ch_names))
        assert_true(
            _check_bounds(evokeds[0].times,
                          tconf.epochs_bounds[data_type])
        )
Exemplo n.º 2
0
def read_raw_data(run_index, hcp_params, lfreq=0.5, hfreq=60):
    raw = hcp.read_raw(run_index=run_index, **hcp_params)
    raw.load_data()
    # apply ref channel correction and drop ref channels
    # preproc.apply_ref_correction(raw)

    annots = hcp.read_annot(run_index=run_index, **hcp_params)
    # construct MNE annotations
    bad_seg = (annots['segments']['all']) / raw.info['sfreq']
    annotations = mne.Annotations(
        bad_seg[:, 0], (bad_seg[:, 1] - bad_seg[:, 0]),
        description='bad')

    raw.annotations = annotations
    raw.info['bads'].extend(annots['channels']['all'])
    raw.pick_types(meg=True, ref_meg=False)

    raw.filter(lfreq, None, method='iir',
               iir_params=dict(order=4, ftype='butter'), n_jobs=1)
    raw.filter(None, hfreq, method='iir',
               iir_params=dict(order=4, ftype='butter'), n_jobs=1)

    # read ICA and remove EOG ECG
    # note that the HCP ICA assumes that bad channels have already been removed
    ica_mat = hcp.read_ica(run_index=run_index, **hcp_params)

    # We will select the brain ICs only
    exclude = annots['ica']['ecg_eog_ic']
    preproc.apply_ica_hcp(raw, ica_mat=ica_mat, exclude=exclude)
    return raw
def test_apply_ica():
    """Test ICA application."""
    raw = hcp.read_raw(data_type='rest', verbose='error', **hcp_params)
    annots = hcp.read_annot(data_type='rest', **hcp_params)
    # construct MNE annotations
    bad_seg = (annots['segments']['all']) / raw.info['sfreq']
    annotations = mne.Annotations(bad_seg[:, 0],
                                  (bad_seg[:, 1] - bad_seg[:, 0]),
                                  description='bad')

    raw.annotations = annotations
    raw.info['bads'].extend(annots['channels']['all'])
    ica_mat = hcp.read_ica(data_type='rest', **hcp_params)
    exclude = [
        ii for ii in range(annots['ica']['total_ic_number'][0])
        if ii not in annots['ica']['brain_ic_vs']
    ]
    assert_raises(RuntimeError,
                  apply_ica_hcp,
                  raw,
                  ica_mat=ica_mat,
                  exclude=exclude)
    # XXX right now this is just a smoke test, should really check some
    # values...
    with warnings.catch_warnings(record=True):
        raw.crop(0, 1).load_data()
    apply_ica_hcp(raw, ica_mat=ica_mat, exclude=exclude)
Exemplo n.º 4
0
def test_read_epochs_rest():
    """Test reading epochs for resting state"""
    for run_index in tconf.run_inds[:tconf.max_runs][:2]:
        annots = hcp.read_annot(
            data_type='rest', run_index=run_index, **hcp_params)

        epochs = hcp.read_epochs(
            data_type='rest', run_index=run_index, **hcp_params)

        _epochs_basic_checks(epochs, annots, data_type='rest')
Exemplo n.º 5
0
def test_read_epochs_task():
    """Test reading epochs for task"""
    for run_index in tconf.run_inds[:tconf.max_runs][:2]:
        for data_type in tconf.task_types:
            annots = hcp.read_annot(
                data_type=data_type, run_index=run_index, **hcp_params)

            epochs = hcp.read_epochs(
                data_type=data_type, run_index=run_index, **hcp_params)

            _epochs_basic_checks(epochs, annots, data_type)
Exemplo n.º 6
0
def test_apply_ica():
    """Test ICA application."""
    raw = hcp.read_raw(data_type='rest', verbose='error', **hcp_params)
    annots = hcp.read_annot(data_type='rest', **hcp_params)
    # construct MNE annotations
    bad_seg = (annots['segments']['all']) / raw.info['sfreq']
    annotations = mne.Annotations(
        bad_seg[:, 0], (bad_seg[:, 1] - bad_seg[:, 0]), description='bad')

    raw.annotations = annotations
    raw.info['bads'].extend(annots['channels']['all'])
    ica_mat = hcp.read_ica(data_type='rest', **hcp_params)
    exclude = [ii for ii in range(annots['ica']['total_ic_number'][0])
               if ii not in annots['ica']['brain_ic_vs']]
    assert_raises(RuntimeError, apply_ica_hcp, raw, ica_mat=ica_mat,
                  exclude=exclude)
    # XXX right now this is just a smoke test, should really check some
    # values...
    with warnings.catch_warnings(record=True):
        raw.crop(0, 1).load_data()
    apply_ica_hcp(raw, ica_mat=ica_mat, exclude=exclude)
Exemplo n.º 7
0
def test_read_annot():
    """Test reading annotations."""
    for run_index in tconf.run_inds:
        annots = hcp.read_annot(data_type='rest', run_index=run_index,
                                **hcp_params)
        # channels
        assert_equal(list(sorted(annots['channels'])),
                     ['all', 'ica', 'manual',  'neigh_corr',
                      'neigh_stdratio'])
        for channels in annots['channels'].values():
            for chan in channels:
                assert_true(chan in tconf.bti_chans)

        # segments
        assert_equal(list(sorted(annots['ica'])),
                     ['bad', 'brain_ic', 'brain_ic_number',
                      'brain_ic_vs', 'brain_ic_vs_number',
                      'ecg_eog_ic', 'flag', 'good',
                      'physio', 'total_ic_number'])
        for components in annots['ica'].values():
            if len(components) > 0:
                assert_true(min(components) >= 0)
                assert_true(max(components) <= 248)
Exemplo n.º 8
0
def preproc_annot_filter(subj_fold, raw, hcp_params):
    """Helper to annotate bad segments and apply Butterworth freq filtering"""

    # apply ref channel correction
    preproc.apply_ref_correction(raw)

    # construct MNE annotations
    annots = hcp.read_annot(**hcp_params)
    bad_seg = (annots['segments']['all']) / raw.info['sfreq']
    annotations = mne.Annotations(bad_seg[:, 0],
                                  (bad_seg[:, 1] - bad_seg[:, 0]),
                                  description='bad')

    raw.annotations = annotations

    # Read all bad channels (concatenated from all runs)
    bad_ch_file = op.join(hcp_path, subj_fold, 'unprocessed', 'MEG',
                          'prebad_chans.txt')
    with open(bad_ch_file, 'rb') as prebad_file:
        prebad_chs = prebad_file.readlines()
    raw.info['bads'].extend([ch.strip() for ch in prebad_chs])  # remove '\n'

    #raw.info['bads'].extend(annots['channels']['all'])
    #print('Bad channels added: %s' % annots['channels']['all'])

    # Band-pass filter (by default, only operates on MEG/EEG channels)
    # Note: MNE complains on Python 2.7
    raw.filter(filt_params['lp'],
               filt_params['hp'],
               method=filt_params['method'],
               filter_length=filt_params['filter_length'],
               l_trans_bandwidth='auto',
               h_trans_bandwidth='auto',
               phase=filt_params['phase'],
               n_jobs=-1)

    return raw, annots
Exemplo n.º 9
0
    # for some reason in the HCP data the time events may not always be unique
    unique_subset = np.nonzero(np.r_[1, np.diff(events[:, 0])])[0]
    events = events[unique_subset]  # use diff to find first unique events

    all_events.append(events)

# now we can go ahead
evokeds = list()
for run_index, events in zip([0, 1], all_events):

    raw = hcp.read_raw(run_index=run_index, **hcp_params)
    raw.load_data()
    # apply ref channel correction and drop ref channels
    # preproc.apply_ref_correction(raw)

    annots = hcp.read_annot(run_index=run_index, **hcp_params)
    # construct MNE annotations
    bad_seg = (annots['segments']['all']) / raw.info['sfreq']
    annotations = mne.Annotations(
        bad_seg[:, 0], (bad_seg[:, 1] - bad_seg[:, 0]),
        description='bad')

    raw.annotations = annotations
    raw.info['bads'].extend(annots['channels']['all'])
    raw.pick_types(meg=True, ref_meg=False)

    #  Note: MNE complains on Python 2.7
    raw.filter(0.50, None, method='iir',
               iir_params=dict(order=4, ftype='butter'), n_jobs=1)
    raw.filter(None, 60, method='iir',
               iir_params=dict(order=4, ftype='butter'), n_jobs=1)
Exemplo n.º 10
0
def reprocess_the_data_from_scratch(all_events, event_id, tmin, tmax, baseline,
                                    decim):
    # now we can go ahead
    evokeds = list()
    all_epochs = list()
    for run_index, events in zip([0, 1], all_events):

        raw = hcp.read_raw(run_index=run_index, **hcp_params)
        raw.load_data()
        # apply ref channel correction and drop ref channels
        # preproc.apply_ref_correction(raw)

        annots = hcp.read_annot(run_index=run_index, **hcp_params)
        # construct MNE annotations
        bad_seg = (annots['segments']['all']) / raw.info['sfreq']
        annotations = mne.Annotations(bad_seg[:, 0],
                                      (bad_seg[:, 1] - bad_seg[:, 0]),
                                      description='bad')

        raw.annotations = annotations
        raw.info['bads'].extend(annots['channels']['all'])
        raw.pick_types(meg=True, ref_meg=False)

        #  Note: MNE complains on Python 2.7
        raw.filter(0.50,
                   None,
                   method='iir',
                   iir_params=dict(order=4, ftype='butter'),
                   n_jobs=1)
        raw.filter(None,
                   60,
                   method='iir',
                   iir_params=dict(order=4, ftype='butter'),
                   n_jobs=1)

        # read ICA and remove EOG ECG
        # note that the HCP ICA assumes that bad channels have already been removed
        ica_mat = hcp.read_ica(run_index=run_index, **hcp_params)

        # We will select the brain ICs only
        exclude = annots['ica']['ecg_eog_ic']
        preproc.apply_ica_hcp(raw, ica_mat=ica_mat, exclude=exclude)

        # now we can epoch
        events = np.sort(events, 0)
        epochs = mne.Epochs(raw,
                            events=events[events[:, 2] == 1],
                            event_id=event_id,
                            tmin=tmin,
                            tmax=tmax,
                            reject=None,
                            baseline=baseline,
                            decim=decim,
                            preload=True)

        all_epochs.append(epochs)
        evoked = epochs.average()
        # now we need to add back out channels for comparison across runs.
        evoked = preproc.interpolate_missing(evoked, **hcp_params)
        evokeds.append(evoked)
    return all_epochs, evokeds
Exemplo n.º 11
0
    # for some reason in the HCP data the time events may not always be unique
    unique_subset = np.nonzero(np.r_[1, np.diff(events[:, 0])])[0]
    events = events[unique_subset]  # use diff to find first unique events

    all_events.append(events)

# now we can go ahead
evokeds = list()
for run_index, events in zip([0, 1], all_events):

    raw = hcp.read_raw(run_index=run_index, **hcp_params)
    raw.load_data()
    # apply ref channel correction and drop ref channels
    # preproc.apply_ref_correction(raw)

    annots = hcp.read_annot(run_index=run_index, **hcp_params)
    # construct MNE annotations
    bad_seg = (annots['segments']['all']) / raw.info['sfreq']
    annotations = mne.Annotations(
        bad_seg[:, 0], (bad_seg[:, 1] - bad_seg[:, 0]),
        description='bad')

    raw.annotations = annotations
    raw.info['bads'].extend(annots['channels']['all'])
    raw.pick_types(meg=True, ref_meg=False)

    #  Note: MNE complains on Python 2.7
    raw.filter(0.50, None, method='iir',
               iir_params=dict(order=4, ftype='butter'), n_jobs=1)
    raw.filter(None, 60, method='iir',
               iir_params=dict(order=4, ftype='butter'), n_jobs=1)
Exemplo n.º 12
0
    for exp_type in exp_types:

        if exp_type is 'task_motor':
            runs = cf.motor_params['runs']
        elif exp_type is 'rest':
            runs = cf.rest_params['runs']
        else:
            raise RuntimeError('Incorrect trial designation: %s' % exp_type)
        hcp_params['data_type'] = exp_type

        for run_i in runs:
            hcp_params['run_index'] = run_i

            # Load annotations and grab all bad channels
            annots = hcp.read_annot(**hcp_params)
            bad_ch_list.extend(annots['channels']['all'])

            print('\tExp: %s, Run: %i, bads: %s' %
                  (exp_type, run_i, annots['channels']['all']))

    ######################################################
    # Convert to set and save all bad channels as txt file
    bad_ch_set = set(bad_ch_list)
    print('\n\tSaving set: %s' % bad_ch_set)

    fname_bad = op.join(hcp_path, subj_fold, 'unprocessed', 'MEG',
                        'prebad_chans.txt')
    with open(fname_bad, 'wb') as out_file:
        for bad_ch in bad_ch_set:
            out_file.write('%s\n' % bad_ch)