Ejemplo n.º 1
0
def read_xls(xls_fname,
             subject_to='colin27',
             atlas='aparc.DKTatlas',
             overwrite=False,
             check_morph_file=False):
    bipolar = True
    template_header = nib.load(
        op.join(SUBJECTS_DIR, subject_to, 'mri', 'T1.mgz')).header
    subjects_electrodes = defaultdict(list)
    electrodes_colors = defaultdict(list)
    for line in utils.xlsx_reader(xls_fname, skip_rows=1):
        subject, _, elec_name, _, anat_group = line
        subject = subject.replace('\'', '')
        if subject == '':
            break
        if check_morph_file:
            electrodes_fname = op.join(
                MMVT_DIR, subject, 'electrodes',
                'electrodes_morph_to_{}.txt'.format(subject_to))
            if not op.isfile(electrodes_fname):
                continue
        elec_group, num1, num2 = utils.elec_group_number(elec_name, bipolar)
        if '{}{}-{}'.format(elec_group, num2, num1) != elec_name:
            num1, num2 = str(num1).zfill(2), str(num2).zfill(2)
        if '{}{}-{}'.format(elec_group, num2, num1) != elec_name:
            raise Exception('Wrong group or numbers!')
        for num in [num1, num2]:
            subjects_electrodes[subject].append('{}{}'.format(elec_group, num))
        electrodes_colors[subject].append((elec_name, int(anat_group)))
    subjects = list(subjects_electrodes.keys())
    bad_subjects = []
    for subject in subjects:
        atlas = utils.fix_atlas_name(subject, atlas, SUBJECTS_DIR)
        if not utils.both_hemi_files_exist(
                op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format(
                    '{hemi}', atlas))):
            anat.create_annotation(subject, atlas)
            if not utils.both_hemi_files_exist(
                    op.join(SUBJECTS_DIR, subject, 'label',
                            '{}.{}.annot'.format('{hemi}', atlas))):
                print('No atlas for {}!'.format(atlas))
                bad_subjects.append((subject, 'No atlas'))
                continue
        try:
            ela_morph_electrodes.calc_elas(subject,
                                           subject_to,
                                           subjects_electrodes[subject],
                                           bipolar=False,
                                           atlas=atlas,
                                           overwrite=overwrite)
        except:
            err = utils.print_last_error_line()
            bad_subjects.append((subject, err))
            continue

    print(bad_subjects)
Ejemplo n.º 2
0
def _morph_electrodes_parallel(p):
    subjects, atlas, subject_to, subjects_electrodes, annotation_template, overwrite = p
    bad_subjects = []
    for subject in subjects:
        get_subject_files_from_mad([subject], atlas)
        atlas = utils.fix_atlas_name(subject, atlas, SUBJECTS_DIR)
        if not utils.both_hemi_files_exist(
                op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format(
                    '{hemi}', atlas))):
            err = ''
            try:
                anat.create_annotation(subject,
                                       atlas,
                                       annotation_template,
                                       n_jobs=1,
                                       overwrite_vertices_labels_lookup=True)
            except:
                print(traceback.format_exc())
                err = utils.print_last_error_line()
            if not utils.both_hemi_files_exist(
                    op.join(SUBJECTS_DIR, subject, 'label',
                            '{}.{}.annot'.format('{hemi}', atlas))):
                bad_subjects.append(
                    (subject, 'No atlas' if err == '' else err))
                continue
        try:
            electrodes = list(
                set(utils.flat_list_of_lists(subjects_electrodes[subject])))
            if not overwrite:
                electrodes = [
                    elc_name for elc_name in electrodes if not op.isfile(
                        op.join(MMVT_DIR, subject, 'electrodes',
                                '{}_ela_morphed.npz'.format(elc_name)))
                ]
            ela_morph_electrodes.calc_elas(subject,
                                           subject_to,
                                           electrodes,
                                           bipolar=False,
                                           atlas=atlas,
                                           overwrite=overwrite,
                                           n_jobs=1)
        except:
            print(traceback.format_exc())
            err = utils.print_last_error_line()
            bad_subjects.append((subject, err))
    return bad_subjects
Ejemplo n.º 3
0
def run_on_subjects(args, main_func, subjects_itr=None, subject_func=None):
    if subjects_itr is None:
        subjects_itr = args.subject
    subjects_flags, subjects_errors = {}, {}
    args = init_args(args)
    subject = ''
    for tup in subjects_itr:
        subject = get_subject(tup, subject_func)
        # utils.make_dir(op.join(MMVT_DIR, subject, 'mmvt'))
        remote_subject_dir = utils.build_remote_subject_dir(
            args.remote_subject_dir, subject)
        if remote_subject_dir == '':
            remote_subject_dir = op.join(SUBJECTS_DIR, subject)
        logging.info(args)
        print(
            '****************************************************************')
        print('subject: {}, atlas: {}'.format(subject, args.atlas))
        print('remote dir: {}'.format(remote_subject_dir))
        print(
            '****************************************************************')
        os.environ['SUBJECT'] = subject
        flags = dict()
        try:
            args.atlas = utils.fix_atlas_name(subject, args.atlas,
                                              SUBJECTS_DIR)
            print('Setting the atlas to: {}'.format(args.atlas))
            # if utils.should_run(args, 'prepare_subject_folder'):
            # I think we always want to run this
            # *) Prepare the local subject's folder
            if not 'recon_all' in args.function:
                flags[
                    'prepare_subject_folder'], password = prepare_subject_folder(
                        subject, remote_subject_dir, args)
                if not flags[
                        'prepare_subject_folder'] and not args.ignore_missing:
                    ans = input('Do you wish to continue (y/n)? ')
                    if not au.is_true(ans):
                        continue
            flags['prepare_subject_folder'] = True
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                flags = main_func(tup, remote_subject_dir, args, flags)
                subjects_flags[subject] = flags
        except:
            subjects_errors[subject] = traceback.format_exc()
            print('Error in subject {}'.format(subject))
            print(traceback.format_exc())

    if subject == '':
        print('No subjects were found!')
        return False
    errors = defaultdict(list)
    ret = True
    good_subjects, bad_subjects = [], []
    # logs_fol = utils.make_dir(op.join(MMVT_DIR, subject, 'logs'))
    # logging.basicConfig(filename=op.join(logs_fol, 'preproc.log'), level=logging.DEBUG)
    for subject, flags in subjects_flags.items():
        print('subject {}:'.format(subject))
        logging.info('subject {}:'.format(subject))
        for flag_type, val in flags.items():
            print('{}: {}'.format(flag_type, val))
            logging.info('{}: {}'.format(flag_type, val))
            if not val:
                errors[subject].append(flag_type)
    if len(errors) > 0:
        ret = False
        print('Errors:')
        logging.info('Errors:')
        for subject, error in errors.items():
            print('{}: {}'.format(subject, error))
            logging.info('{}: {}'.format(subject, error))
    for subject in subjects_flags.keys():
        if len(errors[subject]) == 0:
            good_subjects.append(subject)
        else:
            bad_subjects.append(subject)
    print('Good subjects:\n {}'.format(good_subjects))
    logging.info('Good subjects:\n {}'.format(good_subjects))
    print('Bad subjects:\n {}'.format(bad_subjects))
    logging.info('Good subjects:\n {}'.format(good_subjects))
    utils.write_list_to_file(
        good_subjects, op.join(utils.get_logs_fol(), 'good_subjects.txt'))
    utils.write_list_to_file(bad_subjects,
                             op.join(utils.get_logs_fol(), 'bad_subjects.txt'))
    return subjects_flags
Ejemplo n.º 4
0
def calc_elas(subject,
              template,
              specific_elecs_names=[],
              bipolar=False,
              atlas='aparc.DKTatlas',
              error_radius=3,
              elc_length=4,
              print_warnings=False,
              overwrite=False,
              n_jobs=1):
    fol = utils.make_dir(
        op.join(MMVT_DIR, subject, 'electrodes', 'ela_morphed'))
    elecs_names, elecs_pos, elecs_dists, elecs_types, elecs_oris, excludes = get_electrodes_info(
        subject, atlas, bipolar, n_jobs)
    specific_elecs_names = specific_elecs_names if len(
        specific_elecs_names) > 0 else elecs_names
    elecs_info = [(elec_name, elec_pos, elec_dist, elec_type, elec_ori) for
                  elec_name, elec_pos, elec_dist, elec_type, elec_ori in \
                  zip(elecs_names, elecs_pos, elecs_dists, elecs_types, elecs_oris)
                  if elec_name in specific_elecs_names]
    (labels_vertices, regions_center_of_mass, regions_names, aseg_data, lut,
     pia_verts) = init(subject, atlas, n_jobs)
    atlas = utils.fix_atlas_name(subject, atlas, SUBJECTS_DIR)
    if not utils.both_hemi_files_exist(
            op.join(SUBJECTS_DIR, template, 'label', '{}.{}.annot'.format(
                '{hemi}', atlas))):
        raise Exception('No {} atlas for {}!'.format(atlas, template))
    else:
        template_atlas = atlas

    (template_labels_vertices, template_regions_center_of_mass,
     template_regions_names, template_aseg_data, lut,
     template_pia_verts) = init(template, template_atlas, n_jobs)
    len_lh_pia = len(pia_verts['lh'])
    template_len_lh_pia = len(template_pia_verts['lh'])
    template_header = nib.load(op.join(SUBJECTS_DIR, template, 'mri',
                                       'T1.mgz')).header
    epsilon = 0
    max_run_num = 1000
    parallel = True

    for elec_name, elec_pos, elec_dist, elec_type, elec_ori in elecs_info:
        elec_output_fname = op.join(fol,
                                    '{}_ela_morphed.npz'.format(elec_name))
        if op.isfile(elec_output_fname) and not overwrite:
            d = np.load(elec_output_fname)
            print('{}: err: {}, new_pos={}'.format(elec_name, d['err'],
                                                   d['pos']))
            continue
        elec_labeling = calc_ela(subject, bipolar, elec_name, elec_pos,
                                 elec_type, elec_ori, elec_dist,
                                 labels_vertices, aseg_data, lut, pia_verts,
                                 len_lh_pia, excludes, error_radius,
                                 elc_length, print_warnings, overwrite, n_jobs)
        print('subject_ela:')
        print_ela(elec_labeling)

        elec_labeling_no_whites = calc_elec_labeling_no_white(elec_labeling)
        template_elec_pos = calc_prob_pos(elec_labeling_no_whites,
                                          template_regions_center_of_mass,
                                          template_regions_names)
        subject_prob_pos_in_template_space = template_elec_pos.copy()
        template_elec_vox = np.rint(
            utils.apply_trans(np.linalg.inv(template_header.get_vox2ras_tkr()),
                              template_elec_pos).astype(int))

        elec_labeling_template = calc_ela(
            template, bipolar, elec_name, template_elec_pos, elec_type,
            elec_ori, elec_dist, template_labels_vertices, template_aseg_data,
            lut, template_pia_verts, template_len_lh_pia, excludes,
            error_radius, elc_length, print_warnings, overwrite, n_jobs)
        err = comp_elecs_labeling(elec_labeling_template,
                                  template_regions_center_of_mass,
                                  template_regions_names,
                                  subject_prob_pos_in_template_space)
        run_num = 0
        stop_gradient = False
        print(err)
        while not stop_gradient and err > epsilon and run_num < max_run_num:
            dxyzs = [(1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1),
                     (0, 0, -1)]
            if parallel:
                new_template_elec_pos_arr = [
                    utils.apply_trans(template_header.get_vox2ras_tkr(),
                                      template_elec_vox + dxyz)
                    for dxyz in dxyzs
                ]
                params = [
                    (template, bipolar, elec_name, new_template_elec_pos,
                     elec_type, elec_ori, elec_dist, template_labels_vertices,
                     template_aseg_data, lut, template_pia_verts,
                     template_len_lh_pia, elec_labeling_no_whites,
                     regions_center_of_mass, regions_names,
                     template_regions_center_of_mass, template_regions_names,
                     subject_prob_pos_in_template_space, excludes,
                     error_radius, elc_length, overwrite)
                    for new_template_elec_pos in new_template_elec_pos_arr
                ]
                results = utils.run_parallel(_parallel_calc_ela_err, params,
                                             len(dxyzs) if n_jobs > 1 else 1)
                errs = [res[1] for res in results]
                ind = np.argmin(errs)
                new_template_pos = new_template_elec_pos_arr[ind]
                best_ela = results[ind][0]
                regions, new_probs = norm_probs(
                    calc_elec_labeling_no_white(best_ela))
                print([
                    '{} ({}) '.format(r, p)
                    for r, p in zip(regions, new_probs)
                ])
                min_err = errs[ind]
                if min_err >= err:
                    stop_gradient = True
                else:
                    err = min_err
            else:
                for dxyz in dxyzs:
                    new_template_elec_pos = utils.apply_trans(
                        template_header.get_vox2ras_tkr(),
                        template_elec_vox + dxyz)
                    elec_labeling_template = calc_ela(
                        template, bipolar, elec_name, new_template_elec_pos,
                        elec_type, elec_ori, elec_dist,
                        template_labels_vertices, template_aseg_data, lut,
                        template_pia_verts, template_len_lh_pia, excludes,
                        error_radius, elc_length, print_warnings, overwrite,
                        n_jobs)
                    new_err = comp_elecs_labeling(
                        elec_labeling_template,
                        template_regions_center_of_mass,
                        template_regions_names,
                        subject_prob_pos_in_template_space)
                    if new_err < err:
                        new_template_pos = new_template_elec_pos
                        err = new_err
                        break
                else:
                    stop_gradient = True

            print('*** {}){} ***'.format(run_num + 1, err))
            print_ela(elec_labeling_template)
            run_num += 1
            template_elec_vox = np.rint(
                utils.apply_trans(
                    np.linalg.inv(template_header.get_vox2ras_tkr()),
                    new_template_pos).astype(int))
            if stop_gradient:
                print('Stop gradient!!!')
                print('subject_ela:')
                print_ela(elec_labeling)
                print('template ela:')
                print_ela(elec_labeling_template)
        print('Save output to {}'.format(elec_output_fname))
        np.savez(elec_output_fname,
                 pos=new_template_pos,
                 name=elec_name,
                 err=err)