def test_process_file(tmpdir): '''Generate a csv file and use this as input for the config file loop Loop over all entries in a tab separated csv file''' test_process_file = op.join(loop_test_data.__path__[0], 'test_process_file.csv') test_dframe = pd.read_csv(test_process_file, delimiter='\t') output_dir = tmpdir #op.expanduser('~/Desktop/TEMP') # Process Elekta info elekta_dat = datasets().elekta test_dframe.loc[2, 'subject'] = elekta_dat['subject'] test_dframe.loc[2, 'fs_subjects_dir'] = elekta_dat['SUBJECTS_DIR'] test_dframe.loc[2, 'meg_top_dir'] = loop_test_data.__path__[0] test_dframe.loc[2, 'meg_file_path'] = 'short_elekta_rest.fif' test_dframe.loc[2, 'eroom_file_path'] = 'short_elekta_eroom.fif' test_dframe.loc[2, 'output_dir'] = output_dir test_dframe.loc[2, 'line_freq'] = 50 test_dframe.loc[2, 'trans_file'] = elekta_dat['trans'] enigma_subj_dir = op.join(output_dir, elekta_dat['subject']) if not op.exists(enigma_subj_dir): os.mkdir(enigma_subj_dir) shutil.copy(elekta_dat['src'], enigma_subj_dir) shutil.copy(elekta_dat['bem'], enigma_subj_dir) # Process CTF info ctf_dat = datasets().ctf test_dframe.loc[3, 'subject'] = ctf_dat['subject'] test_dframe.loc[3, 'fs_subjects_dir'] = ctf_dat['SUBJECTS_DIR'] test_dframe.loc[3, 'meg_top_dir'] = loop_test_data.__path__[0] test_dframe.loc[3, 'meg_file_path'] = 'short_ctf_rest.ds' test_dframe.loc[3, 'eroom_file_path'] = 'short_ctf_eroom.ds' test_dframe.loc[3, 'output_dir'] = output_dir test_dframe.loc[3, 'line_freq'] = 60 test_dframe.loc[3, 'trans_file'] = ctf_dat['trans'] enigma_subj_dir = op.join(output_dir, ctf_dat['subject']) if not op.exists(enigma_subj_dir): os.mkdir(enigma_subj_dir) shutil.copy(ctf_dat['src'], enigma_subj_dir) shutil.copy(ctf_dat['bem'], enigma_subj_dir) output_csv = op.join(output_dir, 'process.csv') test_dframe.to_csv(output_csv, sep='\t', index=False) # parse_inputs(output_csv) parse_proc_inputs(output_csv) #Verify that the outputs have been created for the multiple inputs assert op.exists( op.join(output_dir, elekta_dat['subject'], 'Band_rel_power.csv')) assert op.exists( op.join(output_dir, ctf_dat['subject'], 'Band_rel_power.csv')) print(test_dframe)
def test_main_elekta(tmpdir): ''' MEGIN/Elekta - Specific Test - Using fif data from CAMCAN Perform the full process on the MEG data and compare the output spectra and binned power estimates''' #Get elekta data paths from git annex repo test_dat = datasets().elekta inputs = SimpleNamespace(**test_dat) info = SimpleNamespace() info.bem_sol_filename = inputs.bem info.src_filename = inputs.src info.outfolder = tmpdir #Override the typical enigma_outputs folder os.environ['SUBJECTS_DIR'] = inputs.SUBJECTS_DIR main( filename=inputs.meg_rest, subjid=inputs.subject, trans=inputs.trans, emptyroom_filename=inputs.meg_eroom, info=info, line_freq=50 #CAMCAN mains freq ) standard_csv_path = op.join(test_dat['enigma_outputs'], 'elekta_fs', 'Band_rel_power.csv') standard_dframe = pd.read_csv(standard_csv_path, delimiter='\t') test_dframe = pd.read_csv(tmpdir.join('Band_rel_power.csv'), delimiter='\t') allclose(standard_dframe.iloc[:, 1:], test_dframe.iloc[:, 1:])
def test_generate_subjects_psuedomeg(tmpdir): from enigmeg.test_data.get_test_data import datasets #For elekta data elekta_dat = datasets().elekta subject = elekta_dat['subject'] subjects_dir = elekta_dat['SUBJECTS_DIR'] raw_fname = elekta_dat['meg_rest'] eroom_fname = elekta_dat['meg_eroom'] trans_fname = elekta_dat['trans'] src_fname = elekta_dat['src'] bem_fname = elekta_dat['bem'] input_dir = elekta_dat['enigma_outputs'] sfreq = 100 duration = 10 import os os.chdir(tmpdir) np.random.seed(31) generate_subjects_psuedomeg(subject, subjects_dir, raw_fname, trans_fname, bem_fname, src_fname, sfreq, duration, input_dir)
def test_iterate_elekta_simulations(): from enigmeg.test_data.get_test_data import datasets topdir = '/home/stoutjd/data/NEWTEST' #For elekta data elekta_dat = datasets().elekta subject = 'sub-CC320342' #elekta_dat['subject'] subjects_dir = '/data/CAMCAN/SUBJECTS_DIR' #elekta_dat['SUBJECTS_DIR'] raw_fname = elekta_dat['meg_rest'] eroom_fname = elekta_dat['meg_eroom'] trans_fname = elekta_dat['trans'] src_fname = elekta_dat['src'] bem_fname = elekta_dat['bem'] input_dir = elekta_dat['enigma_outputs'] raw = mne.io.read_raw_fif(raw_fname) fwd = mne.forward.make_forward_solution(raw.info, trans_fname, src_fname, bem_fname) import itertools amp_vals = np.arange(10, 100, 10) freq_vals = np.arange(5, 20, 5) label_index = np.arange(0, 68) #For DK atlas amp_freq_label = itertools.product(amp_vals, freq_vals, label_index) output_dir = op.join(topdir, subject) if not op.exists(output_dir): os.mkdir(output_dir) #Load raw and save to outfolder raw.load_data() raw.resample(300) raw_out_fname = op.join(output_dir, '{}_raw_meg.fif'.format(subject)) raw.save(raw_out_fname, overwrite=True) for amp, freq, label_idx in amp_freq_label: generate_combined_simulation(raw_out_fname, fwd, subject=subject, subjects_dir=subjects_dir, topdir=output_dir, label_index=label_idx, sine_amplitude=amp, sine_frequency=freq)
def generate_short_test_data(): '''This is unecessary to run - Data is stored in the git annex repo''' elekta_dat = datasets().elekta raw = mne.io.read_raw_fif(elekta_dat['meg_rest']) raw.crop(tmax=20) raw.resample(300) outfname = op.join(loop_test_data.__path__[0], 'short_elekta_rest.fif') raw.save(outfname) eroom_raw = mne.io.read_raw_fif(elekta_dat['meg_eroom']) eroom_raw.crop(tmax=20) eroom_raw.resample(300) outfname_eroom = op.join(loop_test_data.__path__[0], 'short_elekta_eroom.fif') eroom_raw.save(outfname_eroom) # For CTF data it is necessary to use the CTF commandline tools tmp1 = 'newDs -time 1 21 -resample 4 ctf_rest.ds short_ctf_rest.ds' tmp2 = 'newDs -resample 4 ctf_eroom.ds short_ctf_eroom.ds' print('Must process CTF data manually\n{}\n{}'.format(tmp1, tmp2))
def test_load_data(): filename = datasets().ctf['meg_rest'] assert check_datatype(filename) == 'ctf' load_data(filename)
def test_beamformer(): #Load filenames from test datasets from enigmeg.test_data.get_test_data import datasets test_dat = datasets().ctf meg_filename = test_dat['meg_rest'] subjid = test_dat['subject'] subjects_dir = test_dat['SUBJECTS_DIR'] trans_fname = test_dat['trans'] src_fname = test_dat['src'] bem = test_dat['bem'] outfolder = './tmp' #<<< Change this ############################ raw = mne.io.read_raw_ctf(meg_filename, preload=True) trans = mne.read_trans(trans_fname) # info.subjid, info.subjects_dir = subjid, subjects_dir raw.apply_gradient_compensation(3) raw.resample(300) raw.filter(1.0, None) raw.notch_filter([60,120]) eraw.notch_filter([60,120]) epochs = mne.make_fixed_length_epochs(raw, duration=4.0, preload=True) data_cov = mne.compute_covariance(epochs, method='empirical') eroom_filename = test_dat['meg_eroom'] eroom_raw = mne.io.read_raw_ctf(eroom_filename, preload=True) eroom_raw.resample(300) eroom_raw.notch_filter([60,120]) eroom_raw.filter(1.0, None) eroom_epochs = mne.make_fixed_length_epochs(eroom_raw, duration=4.0) noise_cov = mne.compute_covariance(eroom_epochs) fwd = mne.make_forward_solution(epochs.info, trans, src_fname, bem) from mne.beamformer import make_lcmv, apply_lcmv_epochs filters = make_lcmv(epochs.info, fwd, data_cov, reg=0.01, noise_cov=noise_cov, pick_ori='max-power', weight_norm='unit-noise-gain', rank=None) labels_lh=mne.read_labels_from_annot(subjid, parc='aparc', subjects_dir=subjects_dir, hemi='lh') labels_rh=mne.read_labels_from_annot(subjid, parc='aparc', subjects_dir=subjects_dir, hemi='rh') labels=labels_lh + labels_rh # labels[1].center_of_mass() results_stcs = apply_lcmv_epochs(epochs, filters, return_generator=True)#, max_ori_out='max_power') #Monkey patch of mne.source_estimate to perform 15 component SVD label_ts = mod_source_estimate.extract_label_time_course(results_stcs, labels, fwd['src'], mode='pca15_multitaper') #Convert list of numpy arrays to ndarray (Epoch/Label/Sample) label_stack = np.stack(label_ts) # label_stack = np.mean(label_stack, axis=0) # freq_bins, _ = label_psd(label_stack[:,0, :], raw.info['sfreq']) freq_bins = np.linspace(1,45,177) ######################################3######### FIX #Initialize label_power = np.zeros([len(labels), len(freq_bins)]) alpha_peak = np.zeros(len(labels)) #Create PSD for each label for label_idx in range(len(labels)): print(str(label_idx)) current_psd = label_stack[:,label_idx, :].mean(axis=0) label_power[label_idx,:] = current_psd spectral_image_path = os.path.join(outfolder, 'Spectra_'+ labels[label_idx].name + '.png') try: tmp_fmodel = calc_spec_peak(freq_bins, current_psd, out_image_path=spectral_image_path) #FIX FOR MULTIPLE ALPHA PEAKS potential_alpha_idx = np.where((8.0 <= tmp_fmodel.peak_params[:,0] ) & \ (tmp_fmodel.peak_params[:,0] <= 12.0 ) )[0] if len(potential_alpha_idx) != 1: alpha_peak[label_idx] = np.nan #############FIX ###########################3 FIX else: alpha_peak[label_idx] = tmp_fmodel.peak_params[potential_alpha_idx[0]][0] except: alpha_peak[label_idx] = np.nan #Fix <<<<<<<<<<<<<<