Esempio n. 1
0
    def apply_maxfilter(self, st_duration=None, st_correlation=0.98):

        if not self.raw.info['bads']:
            warnings.warn("Detecting bad Channel, Please check manually")
            self.detect_bad_channel()
        if self.head_pos is None:
            self.get_headposition()
        print('Applying maxfilter; This will take time')
        if self.head_pos is not None:
            self.raw_sss = maxwell_filter(self.raw,
                                          cross_talk=self.ctc_fname,
                                          calibration=self.fine_cal_fname,
                                          head_pos=self.head_pos,
                                          st_duration=st_duration,
                                          st_correlation=st_correlation)
        else:
            self.raw_sss = maxwell_filter(self.raw,
                                          cross_talk=ctc_fname,
                                          calibration=fine_cal_fname,
                                          st_duration=st_duration,
                                          st_correlation=st_correlation)

        self.maxfilter_applied = True
        self.raw_sss.save(self.fname_sss, overwrite=True)
        print('maxfilter done, file saved as {}'.format(self.fname_sss))
Esempio n. 2
0
def test_sss_proj():
    """Test `meg` proj option."""
    raw = read_raw_fif(raw_fname)
    raw.crop(0, 1.0).load_data().pick_types(exclude=())
    raw.pick_channels(raw.ch_names[:51]).del_proj()
    with pytest.raises(ValueError, match='can only be used with Maxfiltered'):
        compute_proj_raw(raw, meg='combined')
    raw_sss = maxwell_filter(raw, int_order=5, ext_order=2)
    sss_rank = 21  # really low due to channel picking
    assert len(raw_sss.info['projs']) == 0
    for meg, n_proj, want_rank in (('separate', 6, sss_rank),
                                   ('combined', 3, sss_rank - 3)):
        proj = compute_proj_raw(raw_sss, n_grad=3, n_mag=3, meg=meg,
                                verbose='error')
        this_raw = raw_sss.copy().add_proj(proj).apply_proj()
        assert len(this_raw.info['projs']) == n_proj
        sss_proj_rank = _compute_rank_int(this_raw)
        cov = compute_raw_covariance(this_raw, verbose='error')
        W, ch_names, rank = compute_whitener(cov, this_raw.info,
                                             return_rank=True)
        assert ch_names == this_raw.ch_names
        assert want_rank == sss_proj_rank == rank  # proper reduction
        if meg == 'combined':
            assert this_raw.info['projs'][0]['data']['col_names'] == ch_names
        else:
            mag_names = ch_names[2::3]
            assert this_raw.info['projs'][3]['data']['col_names'] == mag_names
Esempio n. 3
0
def test_sss_proj():
    """Test `meg` proj option."""
    raw = read_raw_fif(raw_fname)
    raw.crop(0, 1.0).load_data().pick_types(meg=True, exclude=())
    raw.pick_channels(raw.ch_names[:51]).del_proj()
    raw_sss = maxwell_filter(raw, int_order=5, ext_order=2)
    sss_rank = 21  # really low due to channel picking
    assert len(raw_sss.info['projs']) == 0
    for meg, n_proj, want_rank in (('separate', 6, sss_rank), ('combined', 3,
                                                               sss_rank - 3)):
        proj = compute_proj_raw(raw_sss,
                                n_grad=3,
                                n_mag=3,
                                meg=meg,
                                verbose='error')
        this_raw = raw_sss.copy().add_proj(proj).apply_proj()
        assert len(this_raw.info['projs']) == n_proj
        sss_proj_rank = _compute_rank_int(this_raw)
        cov = compute_raw_covariance(this_raw, verbose='error')
        W, ch_names, rank = compute_whitener(cov,
                                             this_raw.info,
                                             return_rank=True)
        assert ch_names == this_raw.ch_names
        assert want_rank == sss_proj_rank == rank  # proper reduction
        if meg == 'combined':
            assert this_raw.info['projs'][0]['data']['col_names'] == ch_names
        else:
            mag_names = ch_names[2::3]
            assert this_raw.info['projs'][3]['data']['col_names'] == mag_names
Esempio n. 4
0
def _maxwell_fun(f_name):
    '''Aux function to run SSS and store shielding calculations'''

    # Load data, crop, and update magnetometer coil type info
    raw = mne.io.Raw(op.join(data_dir, f_name), verbose=params['verbose'])
    erm_error_checks(raw)
    print ('Time of recording: ' +
           dt.fromtimestamp(raw.info['meas_date'][0]).strftime('%Y-%m-%d %H:%M:%S')
           + '\n')

    raw.crop(tmax=params['erm_len'], copy=False)
    raw.fix_mag_coil_types()

    shielding_dict = dict(f_name=f_name)

    # Store power to get relative change
    shielding_dict['raw_norm'] = get_power(raw)

    # Do SSS processing using different calibration files
    for cal_key, cal_fname in zip(params['cal_keys'], params['cal_fnames']):
        raw_sss = maxwell_filter(raw, calibration=cal_fname,
                                 cross_talk=params['ctc_fname'],
                                 st_duration=params['st_duration'],
                                 coord_frame=params['coord_frame'],
                                 regularize=params['regularize'],
                                 verbose=params['verbose'])
        shielding_dict[cal_key] = comp_shielding(raw, raw_sss)

    print 'Spatial filtering mean/max:'
    for cal_key in params['cal_keys']:
        # Print mean and max shielding factor
        print (cal_key + '\tMean: {mean} \tMax: {max}'.format(
            mean=np.mean(shielding_dict[cal_key]),
            max=np.max(shielding_dict[cal_key])))
    return shielding_dict
Esempio n. 5
0
def process_raw(raw_fname):
    raw = read_raw_fif(raw_fname, preload=True, allow_maxshield='yes')
    head_pos = _calculate_chpi_positions(raw=raw)
    raw = mne.chpi.filter_chpi(raw)
    raw.fix_mag_coil_types()
    raw_sss = maxwell_filter(raw, head_pos=head_pos, st_duration=300)
    raw_sss.save(raw_fname[:-4] + '_sss.fif')
    return raw, head_pos
Esempio n. 6
0
def raw_epochs_events():
    """Create raw, epochs, and events for tests."""
    raw = read_raw_fif(raw_fname).set_eeg_reference(projection=True).crop(0, 3)
    raw = maxwell_filter(raw, regularize=None)  # heavily reduce the rank
    assert raw.info['bads'] == []  # no bads
    events = make_fixed_length_events(raw)
    epochs = Epochs(raw, events, tmin=-0.2, tmax=0, preload=True)
    return (raw, epochs, events)
Esempio n. 7
0
def raw_epochs_events():
    """Create raw, epochs, and events for tests."""
    raw = read_raw_fif(raw_fname).set_eeg_reference(projection=True).crop(0, 3)
    raw = maxwell_filter(raw, regularize=None)  # heavily reduce the rank
    assert raw.info['bads'] == []  # no bads
    events = make_fixed_length_events(raw)
    epochs = Epochs(raw, events, tmin=-0.2, tmax=0, preload=True)
    return (raw, epochs, events)
Esempio n. 8
0
 def apply_maxfilter_emptyroom(self, st_duration=None):
     if not self.raw.info['bads']:
         warnings.warn("Detecting bad Channel, Please check manually")
         self.detect_bad_channel()
     self.raw_sss = maxwell_filter(self.raw,
                                   coord_frame='meg',
                                   regularize=None,
                                   cross_talk=self.ctc_fname,
                                   calibration=self.fine_cal_fname,
                                   origin=(0., 0.013, -0.006))
     self.raw_sss.save(self.fname_sss)
Esempio n. 9
0
def apply_maxfilter(raw_path, bads_path, annot_path, maxfilt_path, is_er):
    raw = prepare_raw(raw_path, bads_path, annot_path, is_er)

    coord_frame = "head" if is_er else "meg"

    raw_sss = maxwell_filter(
        raw,
        cross_talk=crosstalk_file,
        calibration=cal_file,
        skip_by_annotation=[],
        coord_frame=coord_frame,
    )

    raw_sss.save(maxfilt_path, overwrite=True)
Esempio n. 10
0
def test_xdawn_regularization():
    """Test Xdawn with regularization."""
    # Get data, this time MEG so we can test proper reg/ch type support
    raw = read_raw_fif(raw_fname, verbose=False, preload=True)
    events = read_events(event_name)
    picks = pick_types(raw.info, meg=True, eeg=False, stim=False,
                       ecg=False, eog=False,
                       exclude='bads')[::8]
    raw.pick_channels([raw.ch_names[pick] for pick in picks])
    del picks
    raw.info.normalize_proj()
    epochs = Epochs(raw, events, event_id, tmin, tmax,
                    preload=True, baseline=None, verbose=False)

    # Test with overlapping events.
    # modify events to simulate one overlap
    events = epochs.events
    sel = np.where(events[:, 2] == 2)[0][:2]
    modified_event = events[sel[0]]
    modified_event[0] += 1
    epochs.events[sel[1]] = modified_event
    # Fit and check that overlap was found and applied
    xd = Xdawn(n_components=2, correct_overlap='auto', reg='oas')
    xd.fit(epochs)
    assert xd.correct_overlap_
    evoked = epochs['cond2'].average()
    assert np.sum(np.abs(evoked.data - xd.evokeds_['cond2'].data))

    # With covariance regularization
    for reg in [.1, 0.1, 'ledoit_wolf', 'oas']:
        xd = Xdawn(n_components=2, correct_overlap=False,
                   signal_cov=np.eye(len(epochs.ch_names)), reg=reg)
        xd.fit(epochs)
    # With bad shrinkage
    xd = Xdawn(n_components=2, correct_overlap=False,
               signal_cov=np.eye(len(epochs.ch_names)), reg=2)
    with pytest.raises(ValueError, match='shrinkage must be'):
        xd.fit(epochs)
    # With rank-deficient input
    raw = maxwell_filter(raw, int_order=4, ext_order=2)
    xd = Xdawn(correct_overlap=False, reg=None)
    # this is a bit wacky because `epochs` has projectors on from the old raw
    # but it works as a rank-deficient test case
    with pytest.raises(ValueError, match='Could not compute eigenvalues'):
        xd.fit(epochs)
    xd = Xdawn(correct_overlap=False, reg=0.5)
    xd.fit(epochs)
    xd = Xdawn(correct_overlap=False, reg='diagonal_fixed')
    xd.fit(epochs)
Esempio n. 11
0
def test_xdawn_regularization():
    """Test Xdawn with regularization."""
    # Get data, this time MEG so we can test proper reg/ch type support
    raw = read_raw_fif(raw_fname, verbose=False, preload=True)
    events = read_events(event_name)
    picks = pick_types(raw.info, meg=True, eeg=False, stim=False,
                       ecg=False, eog=False,
                       exclude='bads')[::8]
    raw.pick_channels([raw.ch_names[pick] for pick in picks])
    del picks
    raw.info.normalize_proj()
    epochs = Epochs(raw, events, event_id, tmin, tmax,
                    preload=True, baseline=None, verbose=False)

    # Test with overlapping events.
    # modify events to simulate one overlap
    events = epochs.events
    sel = np.where(events[:, 2] == 2)[0][:2]
    modified_event = events[sel[0]]
    modified_event[0] += 1
    epochs.events[sel[1]] = modified_event
    # Fit and check that overlap was found and applied
    xd = Xdawn(n_components=2, correct_overlap='auto', reg='oas')
    xd.fit(epochs)
    assert_equal(xd.correct_overlap_, True)
    evoked = epochs['cond2'].average()
    assert_true(np.sum(np.abs(evoked.data - xd.evokeds_['cond2'].data)))

    # With covariance regularization
    for reg in [.1, 0.1, 'ledoit_wolf', 'oas']:
        xd = Xdawn(n_components=2, correct_overlap=False,
                   signal_cov=np.eye(len(epochs.ch_names)), reg=reg)
        xd.fit(epochs)
    # With bad shrinkage
    xd = Xdawn(n_components=2, correct_overlap=False,
               signal_cov=np.eye(len(epochs.ch_names)), reg=2)
    with pytest.raises(ValueError, match='shrinkage must be'):
        xd.fit(epochs)
    # With rank-deficient input
    raw = maxwell_filter(raw, int_order=4, ext_order=2)
    xd = Xdawn(correct_overlap=False, reg=None)
    # this is a bit wacky because `epochs` has projectors on from the old raw
    # but it works as a rank-deficient test case
    with pytest.raises(ValueError, match='Could not compute eigenvalues'):
        xd.fit(epochs)
    xd = Xdawn(correct_overlap=False, reg=0.5)
    xd.fit(epochs)
    xd = Xdawn(correct_overlap=False, reg='diagonal_fixed')
    xd.fit(epochs)
def run_maxfilter(sub_id,
                  raw,
                  cal_fname,
                  ctc_fname,
                  show_figs=False,
                  results_dir=None):
    """Run maxfilter on one subject"""
    raw = maxwell_filter(raw,
                         origin='auto',
                         calibration=cal_fname,
                         cross_talk=ctc_fname,
                         st_duration=10)

    if show_figs or results_dir is not None:
        fig = raw.plot_psd(show=show_figs)
    if results_dir is not None:
        sub_dir = os.path.join(results_dir,
                               sub_id)  # Create a dir with the ID name
        if not os.path.exists(sub_dir):
            os.mkdir(sub_dir)
        fig.savefig(os.path.join(sub_dir, f'sss_{sub_id}_psd.png'))

    return sub_id, raw
raw_stat = mne.io.read_raw_fif(op.join(data_path,
                                       'simulated_stationary_raw.fif'))

###############################################################################
# Visualize the "subject" head movements (traces)

mne.viz.plot_head_positions(pos, mode='traces')

###############################################################################
# Process our simulated raw data (taking into account head movements)

# extract our resulting events
events = mne.find_events(raw, stim_channel='STI 014')
events[:, 2] = 1
raw.plot(events=events)

topo_kwargs = dict(times=[0, 0.1, 0.2], ch_type='mag', vmin=-500, vmax=500)

# 0. Take average of stationary data (bilateral auditory patterns)
evoked_stat = mne.Epochs(raw_stat, events, 1, -0.2, 0.8).average()
evoked_stat.plot_topomap(title='Stationary', **topo_kwargs)

# 1. Take a naive average (smears activity)
evoked = mne.Epochs(raw, events, 1, -0.2, 0.8).average()
evoked.plot_topomap(title='Moving: naive average', **topo_kwargs)

# 2. Use raw movement compensation (restores pattern)
raw_sss = maxwell_filter(raw, head_pos=pos)
evoked_raw_mc = mne.Epochs(raw_sss, events, 1, -0.2, 0.8).average()
evoked_raw_mc.plot_topomap(title='Moving: movement compensated', **topo_kwargs)
Esempio n. 14
0
raw.info['bads'] = ['MEG 2443', 'EEG 053', 'MEG 1032', 'MEG 2313']  # set bads

##..........................................................................##
##                               Ploting Data                               ##
raw.plot_psd(fmax=100)
raw.plot(duration=5, n_channels=30)

#****************************************************************************#
#                                Preprocessing                               #
#****************************************************************************#

##..........................................................................##
##                              Maxwell Filter                              ##

# Here we don't use tSSS (set st_duration) because MGH data is very clean
raw_sss = maxwell_filter(raw, cross_talk=ctc_fname, calibration=fine_cal_fname)

raw.plot_psd(fmax=100)
raw_sss.plot_psd(fmax=100)

##..........................................................................##
##                              Band-pass Filter                            ##

tmin, tmax = 0, 20  # use the first 20s of data

# Setup for reading the raw data (save memory by cropping the raw data
# before loading it)
raw_sss.crop(tmin, tmax).load_data()
raw_sss.info['bads'] = ['MEG 2443', 'EEG 053']  # bads + 2 more

n_fft = 2048  # the FFT size (n_fft). Ideally a power of 2
Esempio n. 15
0
def test_low_rank():
    """Test low-rank covariance matrix estimation."""
    raw = read_raw_fif(raw_fname).set_eeg_reference(projection=True).crop(0, 3)
    raw = maxwell_filter(raw, regularize=None)  # heavily reduce the rank
    sss_proj_rank = 139  # 80 MEG + 60 EEG - 1 proj
    n_ch = 366
    proj_rank = 365  # one EEG proj
    events = make_fixed_length_events(raw)
    methods = ('empirical', 'diagonal_fixed', 'oas')
    epochs = Epochs(raw, events, tmin=-0.2, tmax=0, preload=True)
    bounds = {
        'None': dict(empirical=(-6000, -5000),
                     diagonal_fixed=(-1500, -500),
                     oas=(-700, -600)),
        'full': dict(empirical=(-9000, -8000),
                     diagonal_fixed=(-2000, -1600),
                     oas=(-1600, -1000)),
    }
    for rank in ('full', None):
        covs = compute_covariance(
            epochs, method=methods, return_estimators=True,
            verbose='error', rank=rank)
        for cov in covs:
            method = cov['method']
            these_bounds = bounds[str(rank)][method]
            this_rank = _cov_rank(cov, epochs.info)
            if rank is None or method == 'empirical':
                assert this_rank == sss_proj_rank
            else:
                assert this_rank == proj_rank
            assert these_bounds[0] < cov['loglik'] < these_bounds[1], \
                (rank, method)
            if method == 'empirical':
                emp_cov = cov  # save for later, rank param does not matter

    # Test equivalence with mne.cov.regularize subspace
    with pytest.raises(ValueError, match='are dependent.*must equal'):
        regularize(emp_cov, epochs.info, rank=None, mag=0.1, grad=0.2)
    assert _cov_rank(emp_cov, epochs.info) == sss_proj_rank
    reg_cov = regularize(emp_cov, epochs.info, proj=True, rank='full')
    assert _cov_rank(reg_cov, epochs.info) == proj_rank
    del reg_cov
    with catch_logging() as log:
        reg_r_cov = regularize(emp_cov, epochs.info, proj=True, rank=None,
                               verbose=True)
    log = log.getvalue()
    assert 'jointly' in log
    assert _cov_rank(reg_r_cov, epochs.info) == sss_proj_rank
    reg_r_only_cov = regularize(emp_cov, epochs.info, proj=False, rank=None)
    assert _cov_rank(reg_r_only_cov, epochs.info) == sss_proj_rank
    assert_allclose(reg_r_only_cov['data'], reg_r_cov['data'])
    del reg_r_only_cov, reg_r_cov

    # test that rank=306 is same as rank='full'
    epochs_meg = epochs.copy().pick_types()
    assert len(epochs_meg.ch_names) == 306
    epochs_meg.info.update(bads=[], projs=[])
    cov_full = compute_covariance(epochs_meg, method='oas',
                                  rank='full', verbose='error')
    assert _cov_rank(cov_full, epochs_meg.info) == 306
    cov_dict = compute_covariance(epochs_meg, method='oas',
                                  rank=306, verbose='error')
    assert _cov_rank(cov_dict, epochs_meg.info) == 306
    assert_allclose(cov_full['data'], cov_dict['data'])

    # Work with just EEG data to simplify projection / rank reduction
    raw.pick_types(meg=False, eeg=True)
    n_proj = 2
    raw.add_proj(compute_proj_raw(raw, n_eeg=n_proj))
    n_ch = len(raw.ch_names)
    rank = n_ch - n_proj - 1  # plus avg proj
    assert len(raw.info['projs']) == 3
    epochs = Epochs(raw, events, tmin=-0.2, tmax=0, preload=True)
    assert len(raw.ch_names) == n_ch
    emp_cov = compute_covariance(epochs, rank='full', verbose='error')
    assert _cov_rank(emp_cov, epochs.info) == rank
    reg_cov = regularize(emp_cov, epochs.info, proj=True, rank='full')
    assert _cov_rank(reg_cov, epochs.info) == rank
    reg_r_cov = regularize(emp_cov, epochs.info, proj=False, rank=None)
    assert _cov_rank(reg_r_cov, epochs.info) == rank
    dia_cov = compute_covariance(epochs, rank=None, method='diagonal_fixed',
                                 verbose='error')
    assert _cov_rank(dia_cov, epochs.info) == rank
    assert_allclose(dia_cov['data'], reg_cov['data'])
    # test our deprecation: can simply remove later
    epochs.pick_channels(epochs.ch_names[:103])
    # degenerate
    with pytest.raises(ValueError, match='can.*only be used with rank="full"'):
        compute_covariance(epochs, rank=None, method='pca')
    with pytest.raises(ValueError, match='can.*only be used with rank="full"'):
        compute_covariance(epochs, rank=None, method='factor_analysis')
picks = mne.pick_types(raw.info, meg=True, exclude='bads')
raw.filter(1, 40, **filter_params)

events = mne.find_events(raw, stim_channel='STI101', consecutive='increasing',
                         mask=4352, mask_type='not_and', min_duration=0.003,
                         verbose=True)
evoked_before = Epochs(raw, events, event_id=event_ids, picks=picks).average()

###############################################################################
# Then Maxfiltered and SSS'd data.
raw = mne.io.read_raw_fif(raw_fname_in, preload=True, add_eeg_ref=False)
raw_sss = mne.io.read_raw_fif(sss_fname_in, preload=True, add_eeg_ref=False)
raw.info['bads'] = bads
raw_sss.info['bads'] = bads

raw = maxwell_filter(raw, calibration=cal, cross_talk=ctc)

raw.filter(1, 40, **filter_params)
raw_sss.filter(1, 40, **filter_params)

evoked_after = Epochs(raw, events, event_id=event_ids, picks=picks).average()
evoked_sss = Epochs(raw_sss, events, event_id=event_ids, picks=picks).average()

###############################################################################
# Plotting
ylim = dict(grad=(-100, 100), mag=(-400, 400))
evoked_before.plot(spatial_colors=True, ylim=ylim,
                   titles={'grad': 'Gradiometers before SSS',
                           'mag': 'Magnetometers before SSS'})
evoked_after.plot(spatial_colors=True, ylim=ylim,
                  titles={'grad': 'SSS gradiometers',
Esempio n. 17
0
def test_low_rank():
    """Test low-rank covariance matrix estimation."""
    raw = read_raw_fif(raw_fname).set_eeg_reference(projection=True).crop(0, 3)
    raw = maxwell_filter(raw, regularize=None)  # heavily reduce the rank
    sss_proj_rank = 139  # 80 MEG + 60 EEG - 1 proj
    n_ch = 366
    proj_rank = 365  # one EEG proj
    events = make_fixed_length_events(raw)
    methods = ('empirical', 'diagonal_fixed', 'oas')
    epochs = Epochs(raw, events, tmin=-0.2, tmax=0, preload=True)
    bounds = {
        'None':
        dict(empirical=(-6000, -5000),
             diagonal_fixed=(-1500, -500),
             oas=(-700, -600)),
        'full':
        dict(empirical=(-9000, -8000),
             diagonal_fixed=(-2000, -1600),
             oas=(-1600, -1000)),
    }
    for rank in ('full', None):
        covs = compute_covariance(epochs,
                                  method=methods,
                                  return_estimators=True,
                                  verbose='error',
                                  rank=rank)
        for cov in covs:
            method = cov['method']
            these_bounds = bounds[str(rank)][method]
            this_rank = _cov_rank(cov, epochs.info)
            if rank is None or method == 'empirical':
                assert this_rank == sss_proj_rank
            else:
                assert this_rank == proj_rank
            assert these_bounds[0] < cov['loglik'] < these_bounds[1], \
                (rank, method)
            if method == 'empirical':
                emp_cov = cov  # save for later, rank param does not matter

    # Test equivalence with mne.cov.regularize subspace
    with pytest.raises(ValueError, match='are dependent.*must equal'):
        regularize(emp_cov, epochs.info, rank=None, mag=0.1, grad=0.2)
    assert _cov_rank(emp_cov, epochs.info) == sss_proj_rank
    reg_cov = regularize(emp_cov, epochs.info, proj=True, rank='full')
    assert _cov_rank(reg_cov, epochs.info) == proj_rank
    del reg_cov
    with catch_logging() as log:
        reg_r_cov = regularize(emp_cov,
                               epochs.info,
                               proj=True,
                               rank=None,
                               verbose=True)
    log = log.getvalue()
    assert 'jointly' in log
    assert _cov_rank(reg_r_cov, epochs.info) == sss_proj_rank
    reg_r_only_cov = regularize(emp_cov, epochs.info, proj=False, rank=None)
    assert _cov_rank(reg_r_only_cov, epochs.info) == sss_proj_rank
    assert_allclose(reg_r_only_cov['data'], reg_r_cov['data'])
    del reg_r_only_cov, reg_r_cov

    # test that rank=306 is same as rank='full'
    epochs_meg = epochs.copy().pick_types()
    assert len(epochs_meg.ch_names) == 306
    epochs_meg.info.update(bads=[], projs=[])
    cov_full = compute_covariance(epochs_meg,
                                  method='oas',
                                  rank='full',
                                  verbose='error')
    assert _cov_rank(cov_full, epochs_meg.info) == 306
    cov_dict = compute_covariance(epochs_meg,
                                  method='oas',
                                  rank=306,
                                  verbose='error')
    assert _cov_rank(cov_dict, epochs_meg.info) == 306
    assert_allclose(cov_full['data'], cov_dict['data'])

    # Work with just EEG data to simplify projection / rank reduction
    raw.pick_types(meg=False, eeg=True)
    n_proj = 2
    raw.add_proj(compute_proj_raw(raw, n_eeg=n_proj))
    n_ch = len(raw.ch_names)
    rank = n_ch - n_proj - 1  # plus avg proj
    assert len(raw.info['projs']) == 3
    epochs = Epochs(raw, events, tmin=-0.2, tmax=0, preload=True)
    assert len(raw.ch_names) == n_ch
    emp_cov = compute_covariance(epochs, rank='full', verbose='error')
    assert _cov_rank(emp_cov, epochs.info) == rank
    reg_cov = regularize(emp_cov, epochs.info, proj=True, rank='full')
    assert _cov_rank(reg_cov, epochs.info) == rank
    reg_r_cov = regularize(emp_cov, epochs.info, proj=False, rank=None)
    assert _cov_rank(reg_r_cov, epochs.info) == rank
    dia_cov = compute_covariance(epochs,
                                 rank=None,
                                 method='diagonal_fixed',
                                 verbose='error')
    assert _cov_rank(dia_cov, epochs.info) == rank
    assert_allclose(dia_cov['data'], reg_cov['data'])
    # test our deprecation: can simply remove later
    epochs.pick_channels(epochs.ch_names[:103])
    # degenerate
    with pytest.raises(ValueError, match='can.*only be used with rank="full"'):
        compute_covariance(epochs, rank=None, method='pca')
    with pytest.raises(ValueError, match='can.*only be used with rank="full"'):
        compute_covariance(epochs, rank=None, method='factor_analysis')
Esempio n. 18
0
print('Converting %s to .fif file and applying maxwell filtering' % (result))

result = result.replace('.con', '')
result = result.replace('.', '_')
result = result.replace(' ', '_')
result = result + '_raw_tsss.fif'

print('Importing MNE')
import mne
from mne.preprocessing import maxwell_filter

print('Loading data...')
# Load Raw
raw = mne.io.read_raw_kit(confile,
                          mrk=mrkfile,
                          elp=elpfile,
                          hsp=hspfile,
                          verbose=True)

## Check if any bad channels have been specified
if args.bad_chan is not None:
    raw.info['bads'] = args.bad_chan

print('Perorming maxwell_filter (tSSS)')
## Perform tSSS
raw_tsss = maxwell_filter(raw, st_duration=60, st_correlation=0.9)

print('Saving data')
## Save as .fif file
raw_tsss.save(result, buffer_size_sec=1)
Esempio n. 19
0
def process_one(dipole_no, amp, n_components, crop_idx, crop_len=None):
    raw_fname = "%sAmp%d_Dip%d_IASoff.fif" % (get_root_dir(), amp, dipole_no)
    raw = read_raw_fif(raw_fname)
    raw.load_data()
    raw.crop(5, None)
    if crop_len is not None:
        raw.crop(crop_idx * crop_len, (crop_idx + 1) * crop_len)
    raw.fix_mag_coil_types()
    events = find_events(raw, "SYS201")
    picks = mne.pick_types(raw.info,
                           meg="mag",
                           eeg=False,
                           eog=False,
                           stim=False,
                           exclude="bads")
    n_bins = 40

    freqs = np.linspace(1, 70, n_bins + 1)
    smica = ICA(n_components, freqs, rng=0).fit(
        raw,
        picks=picks,
        em_it=50000,
        n_it_lbfgs=200,
        tol=1e-8,
        verbose=100,
        n_it_min=10000,
    )

    ica = ICA_mne(n_components, method="picard").fit(raw.copy().filter(1, 70),
                                                     picks=picks)
    jdiag = JDIAG_mne(n_components, freqs, rng=0).fit(raw, picks=picks)

    tmin, tmax = -0.1, 0.1
    event_id = [dipole_no]
    # pows = np.linalg.norm(smica.A, axis=0) *
    #                       np.linalg.norm(smica.powers, axis=0) ** .5
    raw_max = raw.copy().filter(1, 40)
    raw_max = maxwell_filter(raw_max, origin=(0.0, 0.0, 0.0))
    epochs = mne.Epochs(
        raw_max,
        events,
        event_id,
        tmin,
        tmax,
        baseline=(None, -0.01),
        preload=True,
        picks=picks,
    )
    cov = mne.compute_covariance(epochs, tmax=0)
    e1 = get_error(epochs, cov)
    e_2 = get_error_A(smica.A, cov, epochs.info)
    good = np.argmin(e_2[0])
    e2 = [e_2[0][good], e_2[1][good]]

    e_3 = get_error_A(jdiag.A, cov, epochs.info)
    good = np.argmin(e_3[0])
    e3 = [e_3[0][good], e_3[1][good]]

    e_4 = get_error_A(ica.get_components(), cov, epochs.info)
    good = np.argmin(e_4[0])
    e4 = [e_4[0][good], e_4[1][good]]
    crop_str = crop_len if crop_len is not None else 0
    np.save(
        "results/phantom_%d_%d_%d_%d_%d.npy" %
        (dipole_no, amp, n_components, crop_idx, crop_str),
        np.array([e1, e2, e3, e4]),
    )
    print(e1, e2, e3, e4)
    return e1, e2, e3, e4
Esempio n. 20
0
            '/meg16_0097/160512/', #16 
            '/meg16_0122/160707/', #17
            '/meg16_0125/160712/', #18
            ]

for i in np.arange(0, len(subjects)):
    meg = subjects[i]

    # complete path to raw data 
    raw_fname_fruit = main_path + meg + 'block4_fruit.fif'  
    raw_fname_odour = main_path + meg + 'block3_odour.fif'
    raw_fname_milk  = main_path + meg + 'block2_milk.fif'
    
    # loading raw data
    raw_fruit = mne.io.Raw(raw_fname_fruit, preload=True)
    raw_odour = mne.io.Raw(raw_fname_odour, preload=True)
    raw_milk  = mne.io.Raw(raw_fname_milk , preload=True)
    

    # It is critical to mark MEG bad channels in raw.info['bads'] prior to 
    # processing in order to prevent artifact spreading
    raw_fruit.info['bads'] = []
    raw_odour.info['bads'] = []
    raw_milk.info['bads']  = []
    

    raw_sss = maxwell_filter(raw_fruit, calibration = calibration, 
              cross_talk=cross_talk , destination = raw_fname_fruit)
    

# The head positions in the FIF file are all zero for invalid positions
# so let's remove them, and then concatenate our times.
mask = (head_pos != 0).any(axis=0)
head_pos = np.concatenate((t[np.newaxis], head_pos)).T[mask]
# In this dataset due to old MaxFilter (2.2.10), data are uniformly
# sampled at 1 Hz, so we save some processing time in
# maxwell_filter by downsampling.
skip = int(round(raw_sss_mf.info['sfreq']))
head_pos = head_pos[::skip]

###############################################################################
# Run :func:`mne.preprocessing.maxwell_filter`, and band-pass filter the data.

raw_sss_py = maxwell_filter(raw,
                            calibration=cal,
                            cross_talk=ctc,
                            origin=origin,
                            head_pos=head_pos)
del raw

raw_sss_py.filter(l_freq, 40, **filter_params)
raw_sss_mf.filter(l_freq, 40, **filter_params)

evoked_sss_py = Epochs(raw_sss_py, events, event_id=event_ids,
                       tmax=tmax).average()
evoked_sss_mf = Epochs(raw_sss_mf, events, event_id=event_ids,
                       tmax=tmax).average()

###############################################################################
# Plotting
Esempio n. 22
0
 def apply_max_move(self, dest_filename):
     self.raw_sss = maxwell_filter(self.raw,
                                   cross_talk=self.ctc_fname,
                                   calibration=self.fine_cal_fname,
                                   destination=dest_filename)
Esempio n. 23
0
print('\n'.join(sys.path))
subjects_dir = '/Users/ktavabi/Data/freesufer'
n_jobs = 2
raw_fname = '/Users/ktavabi/Data/genzds/genz_bo_resting_01_raw.fif'
raw = mne.io.read_raw_fif(raw_fname, preload=True, allow_maxshield='yes')
head_pos = mne.chpi._calculate_chpi_positions(raw=raw)
mne.viz.plot_head_positions(head_pos,
                            mode='traces',
                            destination=raw.info['dev_head_t'],
                            info=raw.info,
                            show=True)
# Denoise
if not op.isfile(raw_fname[:-4] + '_sss.fif'):
    raw = mne.chpi.filter_chpi(raw)
    raw.fix_mag_coil_types()
    raw_sss = maxwell_filter(raw, head_pos=head_pos, st_duration=300)
    raw_sss.save(raw_fname[:-4] + '_sss.fif')
else:
    raw_sss = mne.io.read_raw_fif(raw_fname[:-4] + '_sss.fif', preload=True)

# artifact correction
ecg_projs, events = compute_proj_ecg(raw_sss,
                                     n_grad=3,
                                     n_mag=3,
                                     n_eeg=0,
                                     average=True)
print(ecg_projs)
mne.viz.plot_projs_topomap(ecg_projs)
eog_projs, events = compute_proj_eog(raw_sss,
                                     n_grad=3,
                                     n_mag=3,
Esempio n. 24
0
import mne
from mne.preprocessing import maxwell_filter

print(__doc__)

data_path = mne.datasets.sample.data_path()

###############################################################################
# Set parameters
raw_fname = data_path + "/MEG/sample/sample_audvis_raw.fif"
ctc_fname = data_path + "/SSS/ct_sparse_mgh.fif"
fine_cal_fname = data_path + "/SSS/sss_cal_mgh.dat"

# Preprocess with Maxwell filtering
raw = mne.io.Raw(raw_fname)
raw.info["bads"] = ["MEG 2443", "EEG 053", "MEG 1032", "MEG 2313"]  # set bads
# Here we don't use tSSS (set st_duration) because MGH data is very clean
raw_sss = maxwell_filter(raw, cross_talk=ctc_fname, calibration=fine_cal_fname)

# Select events to extract epochs from, pick M/EEG channels, and plot evoked
tmin, tmax = -0.2, 0.5
event_id = {"Auditory/Left": 1}
events = mne.find_events(raw, "STI 014")
picks = mne.pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, include=[], exclude="bads")
for r, kind in zip((raw, raw_sss), ("Raw data", "Maxwell filtered data")):
    epochs = mne.Epochs(
        r, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(eog=150e-6), preload=False
    )
    evoked = epochs.average()
    evoked.plot(window_title=kind)
raw_stat = mne.io.read_raw_fif(
    op.join(data_path, 'simulated_stationary_raw.fif'))

###############################################################################
# Visualize the "subject" head movements (traces)

mne.viz.plot_head_positions(pos, mode='traces')

###############################################################################
# Process our simulated raw data (taking into account head movements)

# extract our resulting events
events = mne.find_events(raw, stim_channel='STI 014')
events[:, 2] = 1
raw.plot(events=events)

topo_kwargs = dict(times=[0, 0.1, 0.2], ch_type='mag', vmin=-500, vmax=500)

# 0. Take average of stationary data (bilateral auditory patterns)
evoked_stat = mne.Epochs(raw_stat, events, 1, -0.2, 0.8).average()
evoked_stat.plot_topomap(title='Stationary', **topo_kwargs)

# 1. Take a naive average (smears activity)
evoked = mne.Epochs(raw, events, 1, -0.2, 0.8).average()
evoked.plot_topomap(title='Moving: naive average', **topo_kwargs)

# 2. Use raw movement compensation (restores pattern)
raw_sss = maxwell_filter(raw, head_pos=pos)
evoked_raw_mc = mne.Epochs(raw_sss, events, 1, -0.2, 0.8).average()
evoked_raw_mc.plot_topomap(title='Moving: movement compensated', **topo_kwargs)
Esempio n. 26
0
        # Interpolate bad EEG channels
        raw.info['bads'] = bad_EEG[nip]
        raw.interpolate_bads()
        raw.info['bads'] = bad_MEG[nip]

        #######################################################################
        ############################ Maxfilter data ###########################
        #######################################################################
        sss_file_path = data_sss_directory + '/' + nip + '/' + raw_file_path.split(
            '/')[-1]
        sss_file_path = '_sss_raw'.join(sss_file_path.split('_raw'))

        sss_raw = maxwell_filter(raw,
                                 calibration=cal_path,
                                 cross_talk=ct_path,
                                 verbose=False,
                                 destination=refrun_path,
                                 bad_condition='warning')
        sss_raw.save(sss_file_path, overwrite=True, verbose=False)
        report.add_figs_to_section(sss_raw.plot(),
                                   captions=raw_file_path,
                                   section=nip)

        #######################################################################
        ######################## Close and save report ########################
        #######################################################################
        # Delete variables
        del raw, sss_raw

    # Save report
    report.save(os.getcwd() + '/1_maxFilter.html',
Esempio n. 27
0
##..........................................................................##
##                               Ploting Data                               ##
raw.plot_psd(fmax=100)
raw.plot(duration=5, n_channels=30)

#****************************************************************************#
#                                Preprocessing                               #
#****************************************************************************#

##..........................................................................##
##                              Maxwell Filter                              ##

# Here we don't use tSSS (set st_duration) because MGH data is very clean
#raw_sss = maxwell_filter(raw, cross_talk=ctc_fname, calibration=fine_cal_fname)
raw_sss = maxwell_filter(raw, calibration=None, cross_talk=None)

raw_sss.plot_psd(fmax=100)

##..........................................................................##
##                              Band-pass Filter                            ##

raw_sss.filter(None, 40., fir_design='firwin')
raw_sss.plot_psd(fmax=100)

raw_sss.filter(1., None, fir_design='firwin')
raw_sss.plot_psd(fmax=100)

##..........................................................................##
##                                Down Sampling                             ##
Esempio n. 28
0
    # Load data, crop, and update magnetometer coil type info
    raw = mne.io.Raw(op.join(data_dir, f_name), verbose=params['verbose'],
                     allow_maxshield=False)

    # Error checks, crop data, and fix coil types
    coilNoise_error_checks(raw)
    raw.crop(tmax=params['n_secs'], copy=False)
    raw.fix_mag_coil_types()

    shielding_dict = dict(f_name=f_name, freq=freq)

    # Do SSS processing using different calibration files
    for cal_key, cal_fname in zip(params['cal_keys'], params['cal_fnames']):
        raw_sss = maxwell_filter(raw, calibration=cal_fname,
                                 cross_talk=params['ctc_fname'],
                                 st_duration=params['st_duration'],
                                 coord_frame=params['coord_frame'],
                                 regularize=params['regularize'],
                                 verbose=params['verbose'])

        shielding_dict[cal_key] = comp_shielding(raw, raw_sss)

    print 'Spatial filtering mean/max:'
    for cal_key in params['cal_keys']:
        # Print mean and max shielding factor
        print (cal_key + '\tMean: {mean} \tMax: {max}'.format(
            mean=np.mean(shielding_dict[cal_key]),
            max=np.max(shielding_dict[cal_key])))

    sf_list.append(shielding_dict)

####################################################
Esempio n. 29
0
    #     f.write('{},{}\n'.format(meg[1:11],auto_noisy_chs_f + auto_flat_chs_f))
    #     f.close()
    # with open('MEG_bad_channels_odour.csv', 'a') as f:
    #     f.write('{},{}\n'.format(meg[1:11],auto_noisy_chs_o + auto_flat_chs_o))
    #     f.close()
    # with open('MEG_bad_channels_milk.csv', 'a') as f:
    #     f.write('{},{}\n'.format(meg[1:11],auto_noisy_chs_m + auto_flat_chs_m))
    #     f.close()
    # with open('MEG_bad_channels_LD.csv', 'a') as f:
    #     f.write('{},{}\n'.format(meg[1:11],auto_noisy_chs_l + auto_flat_chs_l))
    #     f.close()

    # Maxwell filtering data
    raw_sss_fruit = maxwell_filter(raw_fruit,
                                   calibration=calibration,
                                   cross_talk=cross_talk,
                                   destination=CBU_path + meg +
                                   destination_files[i])
    raw_sss_odour = maxwell_filter(raw_odour,
                                   calibration=calibration,
                                   cross_talk=cross_talk,
                                   destination=CBU_path + meg +
                                   destination_files[i])
    raw_sss_milk = maxwell_filter(raw_milk,
                                  calibration=calibration,
                                  cross_talk=cross_talk,
                                  destination=CBU_path + meg +
                                  destination_files[i])
    raw_sss_LD = maxwell_filter(raw_LD,
                                calibration=calibration,
                                cross_talk=cross_talk,
Esempio n. 30
0
def test_compute_fine_cal():
    """Test computing fine calibration coefficients."""
    raw = read_raw_fif(erm_fname)
    want_cal = read_fine_calibration(cal_mf_fname)
    got_cal, counts = compute_fine_calibration(raw,
                                               cross_talk=ctc,
                                               n_imbalance=1,
                                               verbose='debug')
    assert counts == 1
    assert set(got_cal.keys()) == set(want_cal.keys())
    assert got_cal['ch_names'] == want_cal['ch_names']
    # in practice these should never be exactly 1.
    assert sum([(ic == 1.).any() for ic in want_cal['imb_cals']]) == 0
    assert sum([(ic == 1.).any() for ic in got_cal['imb_cals']]) == 0

    got_imb = np.array(got_cal['imb_cals'], float)
    want_imb = np.array(want_cal['imb_cals'], float)
    assert got_imb.shape == want_imb.shape == (306, 1)
    got_imb, want_imb = got_imb[:, 0], want_imb[:, 0]

    orig_locs = np.array([ch['loc'] for ch in raw.info['chs'][:306]])
    want_locs = want_cal['locs']
    got_locs = got_cal['locs']
    assert want_locs.shape == got_locs.shape

    orig_trans = _loc_to_coil_trans(orig_locs)
    want_trans = _loc_to_coil_trans(want_locs)
    got_trans = _loc_to_coil_trans(got_locs)
    dist = np.linalg.norm(got_trans[:, :3, 3] - want_trans[:, :3, 3], axis=1)
    assert_allclose(dist, 0., atol=1e-6)
    dist = np.linalg.norm(got_trans[:, :3, 3] - orig_trans[:, :3, 3], axis=1)
    assert_allclose(dist, 0., atol=1e-6)
    orig_quat = rot_to_quat(orig_trans[:, :3, :3])
    want_quat = rot_to_quat(want_trans[:, :3, :3])
    got_quat = rot_to_quat(got_trans[:, :3, :3])
    want_orig_angles = np.rad2deg(_angle_between_quats(want_quat, orig_quat))
    got_want_angles = np.rad2deg(_angle_between_quats(got_quat, want_quat))
    got_orig_angles = np.rad2deg(_angle_between_quats(got_quat, orig_quat))
    for key in ('mag', 'grad'):
        # imb_cals value
        p = pick_types(raw.info, meg=key, exclude=())
        r2 = np.dot(got_imb[p], want_imb[p]) / (np.linalg.norm(want_imb[p]) *
                                                np.linalg.norm(got_imb[p]))
        assert 0.99 < r2 <= 1.00001, f'{key}: {r2:0.3f}'
        # rotation angles
        want_orig_max_angle = want_orig_angles[p].max()
        got_orig_max_angle = got_orig_angles[p].max()
        got_want_max_angle = got_want_angles[p].max()
        if key == 'mag':
            assert 8 < want_orig_max_angle < 11, want_orig_max_angle
            assert 1 < got_orig_max_angle < 2, got_orig_max_angle
            assert 9 < got_want_max_angle < 11, got_want_max_angle
        else:
            # Some of these angles are large, but mostly this has to do with
            # processing a very short (one 10-sec segment), downsampled (90 Hz)
            # file
            assert 66 < want_orig_max_angle < 68, want_orig_max_angle
            assert 67 < got_orig_max_angle < 107, got_orig_max_angle
            assert 53 < got_want_max_angle < 60, got_want_max_angle

    kwargs = dict(bad_condition='warning', cross_talk=ctc, coord_frame='meg')
    raw_sss = maxwell_filter(raw, **kwargs)
    raw_sss_mf = maxwell_filter(raw, calibration=cal_mf_fname, **kwargs)
    raw_sss_py = maxwell_filter(raw, calibration=got_cal, **kwargs)
    _assert_shielding(raw_sss, raw, 26, 27)
    _assert_shielding(raw_sss_mf, raw, 61, 63)
    _assert_shielding(raw_sss_py, raw, 61, 63)

    # redoing with given mag data should yield same result
    got_cal_redo, _ = compute_fine_calibration(raw,
                                               cross_talk=ctc,
                                               n_imbalance=1,
                                               calibration=got_cal,
                                               verbose='debug')
    assert got_cal['ch_names'] == got_cal_redo['ch_names']
    assert_allclose(got_cal['imb_cals'], got_cal_redo['imb_cals'], atol=5e-5)
    assert_allclose(got_cal['locs'], got_cal_redo['locs'], atol=1e-6)
    assert sum([(ic == 1.).any() for ic in got_cal['imb_cals']]) == 0

    # redoing with 3 imlabance parameters should improve the shielding factor
    grad_picks = pick_types(raw.info, meg='grad')
    assert len(grad_picks) == 204 and grad_picks[0] == 0
    got_grad_imbs = np.array(
        [got_cal['imb_cals'][pick] for pick in grad_picks])
    assert got_grad_imbs.shape == (204, 1)
    got_cal_3, _ = compute_fine_calibration(raw,
                                            cross_talk=ctc,
                                            n_imbalance=3,
                                            calibration=got_cal,
                                            verbose='debug')
    got_grad_3_imbs = np.array(
        [got_cal_3['imb_cals'][pick] for pick in grad_picks])
    assert got_grad_3_imbs.shape == (204, 3)
    corr = np.corrcoef(got_grad_3_imbs[:, 0], got_grad_imbs[:, 0])[0, 1]
    assert 0.6 < corr < 0.7
    raw_sss_py = maxwell_filter(raw, calibration=got_cal_3, **kwargs)
    _assert_shielding(raw_sss_py, raw, 68, 70)