def get_empty_fnames(subject, tasks, args): utils.make_dir(op.join(MEG_DIR, subject)) utils.make_link(op.join(args.remote_subject_dir.format(subject=subject), 'bem'), op.join(MEG_DIR, subject, 'bem')) for task in tasks: utils.make_dir(op.join(MEG_DIR, task, subject)) utils.make_link(op.join(MEG_DIR, subject, 'bem'), op.join(MEG_DIR, task, subject, 'bem')) utils.make_link(op.join(MEG_DIR, subject, 'bem'), op.join(SUBJECTS_DIR, subject, 'bem')) remote_meg_fol = '/autofs/space/lilli_003/users/DARPA-TRANSFER/meg/{}'.format(subject) csv_fname = op.join(remote_meg_fol, 'cfg.txt') if not op.isfile(csv_fname): print('No cfg file!') return {task:'' for task in tasks} days, empty_fnames = {}, {} for line in utils.csv_file_reader(csv_fname, ' '): for task in tasks: if line[4].lower() == task.lower(): days[task] = line[2] print(days) for line in utils.csv_file_reader(csv_fname, ' '): if line[4] == 'empty': for task in tasks: empty_fnames[task] = op.join(MEG_DIR, task, subject, '{}_empty_raw.fif'.format(subject)) if op.isfile(empty_fnames[task]): continue task_day = days[task] if line[2] == task_day: empty_fname = op.join(remote_meg_fol, line[0].zfill(3), line[-1]) if not op.isfile(empty_fname): raise Exception('empty file does not exist! {}'.format(empty_fname[task])) utils.make_link(empty_fname, empty_fnames[task]) return empty_fnames
def get_meg_empty_fnames(subject, remote_fol, args, ask_for_different_day_empty=False): csv_fname = op.join(remote_fol, 'cfg.txt') if not op.isfile(csv_fname): print('No cfg file ({})!'.format(csv_fname)) return '', '', '' day, empty_fname, cor_fname, local_rest_raw_fname = '', '', '', '' for line in utils.csv_file_reader(csv_fname, ' '): if line[4].lower() == 'resting': day = line[2] remote_rest_raw_fname = op.join(remote_fol, line[0].zfill(3), line[-1]) if not op.isfile(remote_rest_raw_fname): raise Exception('rest file does not exist! {}'.format( remote_rest_raw_fname)) local_rest_raw_fname = op.join( MEG_DIR, subject, '{}_resting_raw.fif'.format(subject)) if not op.isfile(local_rest_raw_fname): utils.make_link(remote_rest_raw_fname, local_rest_raw_fname) break if day == '': print('Couldn\'t find the resting day in the cfg!') return '', '', '' for line in utils.csv_file_reader(csv_fname, ' '): if line[4] == 'empty': empty_fname = op.join(MEG_DIR, subject, '{}_empty_raw.fif'.format(subject)) if op.isfile(empty_fname): continue if line[2] == day: remote_empty_fname = op.join(remote_fol, line[0].zfill(3), line[-1]) if not op.isfile(remote_empty_fname): raise Exception('empty file does not exist! {}'.format( remote_empty_fname)) utils.make_link(remote_empty_fname, empty_fname) if not op.isfile(empty_fname): for line in utils.csv_file_reader(csv_fname, ' '): if line[4] == 'empty': ret = input('empty recordings from a different day were found, continue (y/n)? ') \ if ask_for_different_day_empty else True if au.is_true(ret): remote_empty_fname = op.join(remote_fol, line[0].zfill(3), line[-1]) if not op.isfile(remote_empty_fname): raise Exception('empty file does not exist! {}'.format( remote_empty_fname)) utils.make_link(remote_empty_fname, empty_fname) cor_dir = op.join(args.remote_subject_dir.format(subject=subject), 'mri', 'T1-neuromag', 'sets') if op.isfile(op.join(cor_dir, 'COR-{}-resting.fif'.format(subject))): cor_fname = op.join(cor_dir, 'COR-{}-resting.fif'.format(subject)) elif op.isfile(op.join(cor_dir, 'COR-{}-day{}.fif'.format(subject, day))): cor_fname = op.join(cor_dir, 'COR-{}-day{}.fif'.format(subject, day)) return local_rest_raw_fname, empty_fname, cor_fname
def get_empty_fnames(subject, tasks, args, overwrite=False): utils.make_dir(op.join(MEG_DIR, subject)) utils.make_link(op.join(args.remote_subject_dir.format(subject=subject), 'bem'), op.join(MEG_DIR, subject, 'bem'), overwrite=overwrite) for task in tasks: utils.make_dir(op.join(MEG_DIR, task, subject)) utils.make_link(op.join(MEG_DIR, subject, 'bem'), op.join(MEG_DIR, task, subject, 'bem'), overwrite=overwrite) utils.make_link(op.join(MEG_DIR, subject, 'bem'), op.join(SUBJECTS_DIR, subject, 'bem'), overwrite=overwrite) remote_meg_fol = op.join(args.remote_meg_dir, subject) csv_fname = op.join(remote_meg_fol, 'cfg.txt') empty_fnames, cors, days = '', '', '' if not op.isfile(csv_fname): print('No cfg file!') return '', '', '' days, empty_fnames, cors = {}, {}, {} for line in utils.csv_file_reader(csv_fname, ' '): for task in tasks: if line[4].lower() == task.lower(): days[task] = line[2] # print(days) for line in utils.csv_file_reader(csv_fname, ' '): if line[4] == 'empty': for task in tasks: empty_fnames[task] = op.join( MEG_DIR, task, subject, '{}_empty_raw.fif'.format(subject)) if op.isfile(empty_fnames[task]): continue task_day = days[task] if line[2] == task_day: empty_fname = op.join(remote_meg_fol, line[0].zfill(3), line[-1]) if not op.isfile(empty_fname): raise Exception('empty file does not exist! {}'.format( empty_fname[task])) utils.make_link(empty_fname, empty_fnames[task]) cor_dir = op.join(args.remote_subject_dir.format(subject=subject), 'mri', 'T1-neuromag', 'sets') for task in tasks: if op.isfile( op.join(cor_dir, 'COR-{}-{}.fif'.format(subject, task.lower()))): cors[task] = op.join( cor_dir, 'COR-{}-{}.fif'.format('{subject}', task.lower())) elif op.isfile( op.join(cor_dir, 'COR-{}-day{}.fif'.format(subject, days[task]))): cors[task] = op.join( cor_dir, 'COR-{}-day{}.fif'.format('{subject}', days[task])) return empty_fnames, cors, days
def get_fMRI_rest_fol(subject, remote_root): remote_fol = op.join(remote_root, '{}_01'.format(subject.upper())) csv_fname = op.join(remote_fol, 'cfg.txt') if not op.isfile(csv_fname): print('No cfg file!') return '', '', '' num = None for line in utils.csv_file_reader(csv_fname, '\t'): if line[1].lower() == 'resting': num = line[0] break if num is None: raise Exception( 'Can\'t find rest in the cfg file for {}!'.format(subject)) subject_folders = glob.glob( op.join(remote_root, '{}_*'.format(subject.upper()))) rest_fols = [] for subject_fol in subject_folders: rest_fols = glob.glob(op.join(subject_fol, '**', num.zfill(3)), recursive=True) if len(rest_fols) == 1: break if len(rest_fols) == 0: raise Exception( 'Can\'t find rest in the cfg file for {}!'.format(subject)) return rest_fols[0]
def get_tal_coordaintes(files): rois, errors = {}, {} driver = None for fname in tqdm(files): # subject = utils.namebase(utils.get_parent_fol(fname, 3)) roi = utils.namebase(fname).split('.')[0] if roi not in rois: rois[roi] = {} rois[roi]['tal'] = [] rois[roi]['mni'] = [] lines = list(utils.csv_file_reader(fname, delimiter=' ')) if len(lines) == 0: errors[fname] = '{} is empty!'.format(fname) continue elif len(lines) > 1: errors[fname] = 'More than one line in {}!'.format(fname) continue tal = [int(float(v)) for v in lines[0] if utils.is_float(v)] rois[roi]['tal'].append(tal) if driver is None: driver = tu.yale_get_driver() rois[roi]['mni'].append(tu.yale_tal2mni(tal, driver)) if len(errors) > 0: print(errors) del driver return rois
def import_subs(movie_fol, subs_name='subs', delim=' '): if op.isfile(op.join(movie_fol, subs_name)): subs_fname = op.join(movie_fol, subs_name) else: subs_fnames = glob.glob(op.join(movie_fol, '{}.*'.format(subs_name))) subs_fname = utils.select_one_file(subs_fnames) if subs_fname is None: return subs = [] for line in utils.csv_file_reader(subs_fname, delim): from_t, to_t = [utils.time_to_seconds(t, '%M:%S') for t in line[0].split('-')] subs.append(((from_t, to_t), ' '.join(line[1:]))) return subs
def calc_distances_from_rois(subject, dist_threshold=0.05): from scipy.spatial.distance import cdist import nibabel as nib dipoles_dict = utils.load(op.join(MMVT_DIR, subject, 'meg', 'dipoles.pkl')) labels_times_fol = op.join(MMVT_DIR, subject, 'meg', 'time_accumulate') labels = lu.read_labels(subject, SUBJECTS_DIR, 'laus125') labels_center_of_mass = lu.calc_center_of_mass(labels) labels_pos = np.array([labels_center_of_mass[l.name] for l in labels]) labels_dict = {l.name: labels_center_of_mass[l.name] for l in labels} outer_skin_surf_fname = op.join(SUBJECTS_DIR, subject, 'surf', 'lh.seghead') outer_skin_surf_verts, _ = nib.freesurfer.read_geometry(outer_skin_surf_fname) for dipole_name, dipoles in dipoles_dict.items(): dipole_pos = np.array([dipoles[0][2], dipoles[0][3], dipoles[0][4]]) lables_times_fname = op.join(labels_times_fol, '{}_labels_times.txt'.format(dipole_name)) if not op.isfile(lables_times_fname): print('Can\'t find {}!'.format(lables_times_fname)) continue dists_from_outer_skin = np.min(cdist(outer_skin_surf_verts * 0.001, [dipole_pos]), 0)[0] output_fname = op.join(labels_times_fol, '{}_labels_times_dists.txt'.format(dipole_name)) lines = utils.csv_file_reader(lables_times_fname, delimiter=':', skip_header=1) output, dists = [], [] labels_dists = cdist(labels_pos, [dipole_pos]) dists_argmin = np.argmin(labels_dists, 0)[0] dists_min = np.min(labels_dists, 0)[0] closest_label = labels[dists_argmin].name print('Parsing {} ({})'.format(dipole_name, closest_label)) for line in lines: if len(line) == 0: continue elif len(line) != 2: print('{}: Problem parsing "{}"'.format(lables_times_fname, line)) continue label_name, label_time = line label_pos = labels_dict.get(label_name, None) if label_pos is not None: dist_from_dipole = np.linalg.norm(dipole_pos - label_pos) dists.append(dist_from_dipole) else: dist_from_dipole = -1 dists.append(np.nan) output.append('{}: {} ({:.4f})'.format(label_name, label_time, dist_from_dipole)) for ind, dist in enumerate(dists): if dist < dist_threshold: output[ind] = '{} ***'.format(output[ind]) title = '{}: {} {:.4f} dist from outer skin: {:.4f} '.format( dipole_name, closest_label, dists_min, dists_from_outer_skin) utils.save_arr_to_file(output, output_fname, title)
def trans_tal_coords(roi, file_name, subjects_dir, template='colin27', overwrite=False): subjects = {} output_fol = utils.make_dir(op.join(MMVT_DIR, template, 'rois_peaks')) csv_fname = op.join(output_fol, '{}.csv'.format(roi)) pkl_fname = op.join(output_fol, '{}.pkl'.format(roi)) if op.isfile(pkl_fname) and op.isfile(csv_fname) and not overwrite: print('Data already exist for {}'.format(roi)) return driver = tu.yale_get_driver() files = list(utils.find_recursive(subjects_dir, file_name)) for fname in tqdm(files): lines = list(utils.csv_file_reader(fname, delimiter=' ')) subject = utils.namebase(utils.get_parent_fol(fname, 3)) subjects[subject] = {} if len(lines) == 0: print() subjects[subject]['error'] = '{} is empty!'.format(fname) continue elif len(lines) > 1: print('More than one line in {}!'.format(fname)) subjects[subject] = '>1' continue tal = [int(float(v)) for v in lines[0] if utils.is_float(v)] subjects[subject]['tal'] = tal subjects[subject]['mni'] = tu.yale_tal2mni(tal, driver) del driver print(subjects) with open(csv_fname, 'w') as csv_file: csv_writer = csv.writer(csv_file, delimiter=',') for subject, subject_data in subjects.items(): if 'mni' in subject_data: csv_writer.writerow(subjects[subject]['mni']) utils.save(subjects, pkl_fname)