def read_args(argv=None):
    parser = su.add_default_args()
    parser.add_argument('-t', '--type', help='connection type', required=False, default='')
    parser.add_argument('--threshold', help='connection threshold', required=False, default=0, type=int)
    args = su.parse_args(parser, argv)
    rois_file_exist = op.isfile(op.join(su.get_mmvt_dir(), args.subject, 'rois_con.npz'))
    electrodes_file_exist = op.isfile(op.join(su.get_mmvt_dir(), args.subject, 'electrodes_con.npz'))
    if args.type == '':
        if rois_file_exist and electrodes_file_exist:
            raise Exception('More than one connection file exist, please select one using the -t flag')
        elif rois_file_exist:
            args.type = 'rois'
        elif electrodes_file_exist:
            args.type = 'electrodes'
        else:
            raise Exception('No connection file was found!\n'+
                            'ROIs connection file: rois_con.npz\n' +
                            'Electrodes connection file: electrodes_con.npz')
    elif args.type == 'rois':
        if not rois_file_exist:
            raise Exception('You chose ROIs connection, but the file rois_con.npz does not exist!')
    elif args.type == 'electrodes':
        if not electrodes_file_exist:
            raise Exception('You chose electrodes connection, but the file electrodes_con.npz does not exist!')
    return args
Example #2
0
def read_args(argv=None):
    parser = su.add_default_args()
    parser.add_argument('-r', '--radius', help='radius', required=False, default=0.15, type=float)
    parser.add_argument('-p', '--pos_file', help='position file', required=False, default='', type=su.str_arr_type)
    args = su.parse_args(parser, argv)
    if len(args.subjects) == 0:
        args.subjects = [args.subject]
    if len(args.pos_file) == 0:
        args.pos_file = [''] * len(args.subjects)
    pos_files = []
    for subject, pos_file in zip(args.subjects, args.pos_file):
        args.subject = subject
        pos_files_fol = op.join(su.get_mmvt_dir(), args.subject, 'electrodes')
        if pos_file == '':
            pos_file_options = glob.glob(op.join(pos_files_fol, 'electrodes*positions*.npz'))
            if args.bipolar:
                pos_file_options = [fname for fname in pos_file_options if 'bipolar' in fname]
            else:
                pos_file_options = [fname for fname in pos_file_options if 'bipolar' not in fname]
            if len(pos_file_options) == 0:
                raise Exception('No electrodes position files in {}!'.format(pos_files_fol))
            elif len(pos_file_options) == 1:
                pos_file = pos_file_options[0]
                print('electrodes file: {}'.format(pos_file))
            else:
                raise Exception('More than one electrodes positions files in {}'.format(pos_files_fol) +
                                'please indicate which one using the -p flag')
        pos_files.append(pos_file)
    args.pos_file = pos_files
    if len(args.pos_file) == 1:
        args.pos_file = args.pos_file[0]
    return args
Example #3
0
def create_new_subject_file(args):
    # Create a file for the new subject
    if len(args.subjects) == 0:
        args.subjects = [args.subject]
    for subject in args.subjects:
        args.subject = subject
        new_fname = su.get_subject_fname(args)
        empty_subject_fname = op.join(su.get_mmvt_dir(), "empty_subject.blend")
        if not op.isfile(empty_subject_fname):
            shutil.copy(op.join(su.get_resources_dir(), "empty_subject.blend"), empty_subject_fname)
        if op.isfile(new_fname):
            overwrite = input("The file {} already exist, do you want to overwrite? ".format(new_fname))
            if su.is_true(overwrite):
                os.remove(new_fname)
                shutil.copy(op.join(su.get_mmvt_dir(), "empty_subject.blend"), new_fname)
        else:
            shutil.copy(op.join(su.get_mmvt_dir(), "empty_subject.blend"), new_fname)
def render_electrodes_probs(subject_fname):
    args = read_args(su.get_python_argv())
    figures_dir = su.get_figures_dir(args)
    if args.rel_output_path:
        args.output_path = op.join(figures_dir, args.output_path)
    su.make_dir(args.output_path)

    # Set the labeling file
    labeling_fname = '{}_{}_electrodes_cigar_r_*_l_*{}*.pkl'.format(args.subject, args.real_atlas,
        '_bipolar' if args.bipolar else '')
    labels_fol = op.join(su.get_mmvt_dir(), args.subject, 'electrodes')
    labeling_files = glob.glob(op.join(labels_fol, labeling_fname))
    if len(labeling_files) == 0:
        print('No labeling files in {}!'.format(labels_fol))
        return
    if len(labeling_files) > 1:
        print('More than one labeling files in {}, please choose one using the --labeling flag'.format(labels_fol))
        return
    else:
        labeling_file = labeling_files[0]

    mmvt = su.init_mmvt_addon()
    mmvt.show_hide_hemi(args.hide_lh, 'lh')
    mmvt.show_hide_hemi(args.hide_rh, 'rh')
    mmvt.show_hide_sub_corticals(args.hide_subs)
    mmvt.set_render_quality(args.quality)
    mmvt.set_render_output_path(args.output_path)
    mmvt.set_render_smooth_figure(args.smooth_figure)
    if op.isfile(op.join(args.output_path, 'camera.pkl')):
        mmvt.load_camera()
    else:
        # Try to find the camera in the figures folder
        if op.isfile(op.join(figures_dir, 'camera.pkl')):
            mmvt.set_render_output_path(figures_dir)
            mmvt.load_camera()
            mmvt.set_render_output_path(args.output_path)
        else:
            cont = input('No camera file was detected in the output folder, continue?')
            if not su.is_true(cont):
                return

    mmvt.set_electrodes_labeling_file(labeling_file)
    mmvt.show_electrodes()
    mmvt.color_the_relevant_lables(True)
    leads = mmvt.get_leads()
    for lead in leads:
        electrodes = mmvt.get_lead_electrodes(lead)
        for electrode in electrodes:
            print(electrode)
            mmvt.clear_cortex()
            mmvt.set_current_electrode(electrode, lead)
            mmvt.render_image(electrode)

    su.save_blend_file(subject_fname)
    su.exit_blender()
Example #5
0
def check_if_all_figures_were_rendered(args):
    from src.utils import figures_utils as fu
    from src.mmvt_addon import fMRI_panel as fmri

    subject_fol = op.join(su.get_mmvt_dir(), args.subject)
    figures_fol = op.join(subject_fol, 'figures')
    clusters_file_names, _ = fmri.get_clusters_files(subject_fol)
    clusters_names = [f for f in clusters_file_names if args.clusters_type in f]
    render_figures = False
    all_figures = []
    for clusters_name, inflated, background_color in product(clusters_names, args.inflated, args.background_color):
        figures_fnames = fu.get_brain_perspectives_figures(
            figures_fol, inflated, background_color, clusters_name, args.inflated_ratio)
        all_figures.extend(figures_fnames)
        if any(not op.isfile(f) for f in figures_fnames):
            render_figures = True
            break
    return render_figures
Example #6
0
def post_script(args):
    from src.utils import figures_utils as fu
    from src.mmvt_addon import fMRI_panel as fmri
    from src.utils import utils

    subject_fol = op.join(su.get_mmvt_dir(), args.subject)
    figures_fol = op.join(subject_fol, 'figures')
    clusters_file_names, _ = fmri.get_clusters_files(subject_fol)
    clusters_names = [f for f in clusters_file_names if args.clusters_type in f]
    print('clusters_names: {}'.format(clusters_names))
    fmri_files_minmax_fname = op.join(subject_fol, 'fmri', 'fmri_files_minmax_cm.pkl')
    data_min, data_max, colors_map_name = utils.load(fmri_files_minmax_fname)
    for clusters_name, inflated, background_color in product(clusters_names, args.inflated, args.background_color):
        print('Combing figures for {}, inflated: {}, background color: {}'.format(
            clusters_name, inflated, background_color))
        perspectives_image_fname = fu.combine_four_brain_perspectives(
            figures_fol, inflated, args.dpi, background_color,
            clusters_name, args.inflated_ratio, True, args.overwrite)
        fu.combine_brain_with_color_bar(
            data_max, data_min, perspectives_image_fname, colors_map_name, args.overwrite, args.dpi,
            args.x_left_crop, args.x_right_crop, args.y_top_crop, args.y_buttom_crop,
            args.w_fac, args.h_fac, background_color)