def test_anatomy(): """Test anatomy functions (slow!).""" import matplotlib.pyplot as plt # This is where the data are after downloading from HCP temp_dir = mne.utils._TempDir() subjects_dir = op.join(temp_dir, 'hcp-subjects') recordings_path = op.join(temp_dir, 'hcp-meg') make_mne_anatomy(recordings_path=recordings_path, subjects_dir=subjects_dir, **hcp_params) assert_true( op.isfile( op.join(subjects_dir, hcp_params['subject'], 'bem', 'inner_skull.surf'))) # Now we need fsaverage... mne_subjects_dir = mne.get_config('SUBJECTS_DIR') assert_true(mne_subjects_dir is not None) shutil.copytree(op.join(mne_subjects_dir, 'fsaverage'), op.join(subjects_dir, 'fsaverage')) compute_forward_stack(subjects_dir=subjects_dir, recordings_path=recordings_path, src_params=dict(add_dist=False, spacing='oct1'), verbose=True, **hcp_params) # let's do our viz tests, too plot_coregistration(subjects_dir=subjects_dir, recordings_path=recordings_path, **hcp_params) plt.close('all') mne.viz.plot_bem(subject=tconf.test_subject, subjects_dir=subjects_dir) plt.close('all')
def make_mne_anatomy(subject): hcp.make_mne_anatomy(subject, subjects_dir=SUBJECTS_DIR, hcp_path=HCP_DIR, recordings_path=op.join(HCP_DIR, 'hcp-meg')) return op.isfile( op.join( op.join(HCP_DIR, 'hcp-meg', subject, '{}-head_mri-trans.fif'.format(subject))))
# This is to download data from Amazon WS # $ s3cmd get --recursive s3://hcp.aws.amazon.com/hcp/106521/T1w/106521/ ################################ import hcp import os import numpy as np hcp_path = "/media/robbis/DATA/meg/hcp/" subject = '106521' hcp_task = 'task_motor' recordings_path = os.path.join(hcp_path, 'recordings') fs_path = os.path.join(hcp_path, 'subjects') hcp.make_mne_anatomy(subject, subjects_dir=fs_path, hcp_path=hcp_path, recordings_path=recordings_path) epochs = hcp.read_epochs(subject, hcp_task, onset='resp', hcp_path=hcp_path) info = hcp.read_info(subject=subject, hcp_path=hcp_path, data_type=hcp_task, run_index=0) # # cfg=[]; # cfg.method = 'mtmfft'; # cfg.channel = 'MEG'; # cfg.trials = 1; # cfg.output = 'powandcsd'; % gives power and cross-spectral density matrices # cfg.foi = 22;
def compute_src_label_ts(subject, crop_to=[0, 250], resample_to=100., bads=None, mag_reject=5e-12, win_len=2000, n_wins=11, verbose=None, lambda2=1. / 9., inv_method='dSPM', extract_ts_mode='mean_flip'): """ Compute source label time series """ """ Compute anatomy """ hcp.make_mne_anatomy(subject, subjects_dir=subjects_dir, hcp_path=hcp_path, recordings_path=hcp_path) """ Read surface labels """ labels = read_labels_from_annot(subject, parc='aparc', subjects_dir=subjects_dir) labels_fsav = read_labels_from_annot('fsaverage', parc='aparc', subjects_dir=subjects_dir) """ Read raw data """ raw = hcp.read_raw(subject=subject, data_type=data_type, hcp_path=hcp_path, run_index=run_index) raw.load_data() raw.crop(crop_to[0], crop_to[1]) raw.resample(resample_to) raw.info['bads'] = bads hcp.preprocessing.set_eog_ecg_channels(raw) hcp.preprocessing.apply_ref_correction(raw) info = raw.info.copy() raw.info['projs'] = [] ecg_ave = create_ecg_epochs(raw).average() eog_ave = create_eog_epochs(raw).average() ssp_eog, _ = compute_proj_eog(raw, n_grad=1, n_mag=1, average=True, reject=dict(mag=mag_reject)) raw.add_proj(ssp_eog, remove_existing=True) n_fft = next_fast_len(int(round(4 * raw.info['sfreq']))) sfreq = raw.info['sfreq'] """ Compute forward model """ src_outputs = hcp.anatomy.compute_forward_stack( subject=subject, subjects_dir=subjects_dir, hcp_path=hcp_path, recordings_path=hcp_path, src_params=dict(add_dist=False), info_from=dict(data_type=data_type, run_index=run_index)) fwd = src_outputs['fwd'] """ Compute noise covariance """ raw_noise = hcp.read_raw(subject=subject, hcp_path=hcp_path, data_type='noise_empty_room') raw_noise.load_data() hcp.preprocessing.apply_ref_correction(raw_noise) raw_noise.add_proj(ssp_eog) noise_cov = compute_raw_covariance(raw_noise, method='oas') """ Compute inverse operator """ raw.info = info inv_op = make_inverse_operator(raw.info, forward=fwd, noise_cov=noise_cov, verbose=verbose) """ Compute source activity """ wins = [[0, win_len]] for i in range(n_wins): new_wins = [ wins[0][0] + (win_len * (i + 1)), wins[0][1] + (win_len * (i + 1)) ] wins.append(new_wins) raw_srcs = [] for win in wins: res = apply_inverse_raw(raw, inv_op, lambda2=lambda2, method=inv_method, label=None, start=win[0], stop=win[1], nave=1, time_func=None, pick_ori=None, buffer_size=None, prepared=False, method_params=None, verbose=verbose) raw_srcs.append(res) """ Compute source label time series """ src = inv_op['src'] label_ts = extract_label_time_course(raw_srcs, labels, src, mode=extract_ts_mode, return_generator=False) return label_ts, sfreq
new_subjects_dir = op.join(hcp_path, 'anatomy') head_trans_dir = op.join(hcp_path, 'hcp-meg') apply_ref_correction = False n_jobs = 6 # Get all subject ID numbers, exclude missing rest/motor/story&math/working mem dirs = cf.subj_nums_hcp dirs = [str(temp_d) for temp_d in dirs] for subj_fold in dirs: if False: # Construct MNE-compatible anatomy subj_dir = op.join(hcp_path, subj_fold) hcp.make_mne_anatomy(subj_fold, subjects_dir=stored_subjects_dir, hcp_path=hcp_path, recordings_path=hcp_path + '/hcp-meg') if False: # Construct fwd solutions # Leaving most defaults for src_params. Defaults are: # src_params = dict(subject='fsaverage', fname=None, # spacing='oct6', n_jobs=2, surface='white', # subjects_dir=subjects_dir, add_dist=True) src_params = dict(subject='fsaverage', fname=None, spacing='ico5', n_jobs=6, surface='white', overwrite=True, subjects_dir=stored_subjects_dir, add_dist=True) src_outputs = hcp.anatomy.compute_forward_stack( subject=subj_fold, subjects_dir=stored_subjects_dir, hcp_path=hcp_path,
""" Created on Thu Feb 7 19:06:06 2019 @author: Yongjie """ import os import hcp storage_dir = '/run/media/yozhu/Seagate/1-NeuralData/mne-hcp-data' # This is where the data are after downloading from HCP hcp_path = storage_dir + '/HCP' # this is the new directory to be created subjects_dir = storage_dir + '/hcp-subjects' # it will make the subfolders 'bem', 'mir', 'surf' and 'label' # and populate it with symbolic links if possible and write some files # where information has to be presented in a different format to satisfy MNE. # this is where the coregistration matrix is written as # `105923-head_mri-transform.fif` recordings_path = storage_dir + '/hcp-meg' # for subject in os.listdir(hcp_path): hcp.make_mne_anatomy(subject=subject, hcp_path=hcp_path, recordings_path=recordings_path, subjects_dir=subjects_dir)