def return_bids_test_dir(tmpdir_factory): """Return path to a written test BIDS dir.""" bids_root = str(tmpdir_factory.mktemp('mnebids_utils_test_bids_ds')) data_path = testing.data_path() raw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_trunc_raw.fif') event_id = { 'Auditory/Left': 1, 'Auditory/Right': 2, 'Visual/Left': 3, 'Visual/Right': 4, 'Smiley': 5, 'Button': 32 } events_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_trunc_raw-eve.fif') cal_fname = op.join(data_path, 'SSS', 'sss_cal_mgh.dat') crosstalk_fname = op.join(data_path, 'SSS', 'ct_sparse.fif') raw = mne.io.read_raw_fif(raw_fname) raw.info['line_freq'] = 60 bids_path.update(root=bids_root) # Write multiple runs for test_purposes for run_idx in [run, '02']: name = bids_path.copy().update(run=run_idx) write_raw_bids(raw, name, events_data=events_fname, event_id=event_id, overwrite=True) write_meg_calibration(cal_fname, bids_path=bids_path) write_meg_crosstalk(crosstalk_fname, bids_path=bids_path) return bids_root
def _elektas_extras(crosstalk_file, fine_cal_file, bids_path): """ Save the crosstalk and fine-calibration files :param crosstalk_file: str, path to crosstalk file :param fine_cal_file: str, path to fine_cal file :param bids_path: mne-bids BIDSPath instance :return: """ write_meg_calibration(fine_cal_file, bids_path) write_meg_crosstalk(crosstalk_file, bids_path)
def run(): """Run the calibration_to_bids command.""" from mne.commands.utils import get_optparser parser = get_optparser(__file__, usage="usage: %prog options args", prog_prefix='mne_bids', version=mne_bids.__version__) parser.add_option('--bids_root', dest='bids_root', help='The path of the folder containing the BIDS ' 'dataset') parser.add_option('--subject_id', dest='subject', help=('Subject name')) parser.add_option('--session_id', dest='session', help='Session name') parser.add_option('--file', dest='fname', help='The path of the crosstalk file') parser.add_option('--verbose', dest='verbose', action='store_true', help='Whether do generate additional diagnostic output') opt, args = parser.parse_args() if args: parser.print_help() parser.error(f'Please do not specify arguments without flags. ' f'Got: {args}.\n') if opt.bids_root is None: parser.print_help() parser.error('You must specify bids_root') if opt.subject is None: parser.print_help() parser.error('You must specify a subject') bids_path = BIDSPath(subject=opt.subject, session=opt.session, root=opt.bids_root) logger.info(f'Writing fine-calibration file {bids_path.basename} …') write_meg_calibration(calibration=opt.fname, bids_path=bids_path, verbose=opt.verbose)
def return_bids_test_dir(tmpdir_factory): """Return path to a written test BIDS dir.""" bids_root = str(tmpdir_factory.mktemp('mnebids_utils_test_bids_ds')) data_path = testing.data_path() raw_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_trunc_raw.fif') event_id = { 'Auditory/Left': 1, 'Auditory/Right': 2, 'Visual/Left': 3, 'Visual/Right': 4, 'Smiley': 5, 'Button': 32 } events_fname = op.join(data_path, 'MEG', 'sample', 'sample_audvis_trunc_raw-eve.fif') cal_fname = op.join(data_path, 'SSS', 'sss_cal_mgh.dat') crosstalk_fname = op.join(data_path, 'SSS', 'ct_sparse.fif') raw = mne.io.read_raw_fif(raw_fname) raw.info['line_freq'] = 60 # Drop unknown events. events = mne.read_events(events_fname) events = events[events[:, 2] != 0] bids_path = _bids_path.copy().update(root=bids_root) write_raw_bids(raw, bids_path=bids_path, events_data=events, event_id=event_id, overwrite=True) write_meg_calibration(cal_fname, bids_path=bids_path) write_meg_crosstalk(crosstalk_fname, bids_path=bids_path) return bids_root
"""Add calibration and crosstalk files to BIDS data""" from tqdm import tqdm from mne_bids import write_meg_calibration, write_meg_crosstalk, BIDSPath from metacog.config_parser import cfg from metacog.paths import crosstalk, calibration, dirs sss_data = BIDSPath(root=dirs.bids_root) for subj in tqdm(cfg.all_subjects): sss_data.update(subject=subj) write_meg_calibration(calibration, sss_data) write_meg_crosstalk(crosstalk, sss_data)
root=data_bids_dir) # mri_bids_path = mne_bids.BIDSPath(subject=subj_id, session=date_record, # root=data_bids_dir) # mne_bids.write_raw_bids(raw=raw_meg, bids_path=meg_bids_path, anonymize={'daysback': 40000}, overwrite=True, verbose=True) if subj_id != 'emptyroom': mne_bids.write_meg_crosstalk(ct_file, meg_bids_path) if int(date_record[:4]) < 2020: mne_bids.write_meg_calibration(fc_files['Before_sept2019'], meg_bids_path) else: mne_bids.write_meg_calibration(fc_files['Current'], meg_bids_path) # mri_presence = mri_subjnames.count(subj_fullname) # if mri_presence: # mri_path_i = mri_subjnames.index(subj_fullname) # mne_bids.write_anat(t1w=mri_paths[mri_path_i], bids_path=mri_bids_path, raw=raw_meg, overwrite=True) # print(f'{subj_fullname} has MRI file. Path to MRI file: {mri_paths[mri_path_i]}') # save fullnames and bids_id df_subj_bids_codes = pd.DataFrame.from_dict(sub_id_name, orient='index').reset_index() df_subj_bids_codes.columns = ['Fullname', 'BIDS_id'] df_subj_bids_codes.to_csv(data_raw_dir / 'Fullnames_BIDS-ids_subjects.csv', index=False)
root=output_path) write_raw_bids(raw, bids_path, events_data=events_data, event_id=event_id, overwrite=True) ############################################################################### # The sample MEG dataset comes with fine-calibration and crosstalk files that # are required when processing Elekta/Neuromag/MEGIN data using MaxFilter®. # Let's store these data in appropriate places, too. cal_fname = op.join(data_path, 'SSS', 'sss_cal_mgh.dat') ct_fname = op.join(data_path, 'SSS', 'ct_sparse_mgh.fif') write_meg_calibration(cal_fname, bids_path) write_meg_crosstalk(ct_fname, bids_path) ############################################################################### # Now let's see the structure of the BIDS folder we created. print_dir_tree(output_path) ############################################################################### # Now let's get an overview of the events on the whole dataset counts = count_events(output_path) counts ############################################################################### # A big advantage of having data organized according to BIDS is that software
raw_er = mne.io.read_raw_fif(raw_er_path, verbose=False) # specify power line frequency as required by BIDS raw.info['line_freq'] = 60 raw_er.info['line_freq'] = 60 # Write empty-room data write_raw_bids(raw=raw_er, bids_path=bids_path_er, verbose=False) # Write experimental MEG data, fine-calibration and crosstalk files write_raw_bids(raw=raw, bids_path=bids_path, events_data=events_path, event_id=event_id, empty_room=bids_path_er, verbose=False) write_meg_calibration(cal_path, bids_path=bids_path, verbose=False) write_meg_crosstalk(ct_path, bids_path=bids_path, verbose=False) # Write anatomical scan # We pass the MRI landmark coordinates, which will later be required for # automated defacing mri_landmarks = mne.channels.make_dig_montage( lpa=[66.08580, 51.33362, 46.52982], nasion=[41.87363, 32.24694, 74.55314], rpa=[17.23812, 53.08294, 47.01789], coord_frame='mri_voxel') bids_path.datatype = 'anat' write_anat(image=t1w_path, bids_path=bids_path, landmarks=mri_landmarks, verbose=False)