Beispiel #1
0
def plot_blob(cluster_labels, faces_verts, is_inflated=None, use_abs=None):
    if use_abs is None:
        use_abs = bpy.context.scene.coloring_use_abs
    is_inflated = _addon().is_inflated() if is_inflated is None else is_inflated
    fMRIPanel.dont_show_clusters_info = False
    _addon().init_activity_map_coloring('FMRI')#, subcorticals=False)
    blob_vertices = cluster_labels['vertices']
    hemi = cluster_labels['hemi']
    real_hemi = hemi
    if is_inflated:
        hemi = 'inflated_{}'.format(hemi)
    # fMRIPanel.blobs_plotted = True
    fMRIPanel.colors_in_hemis[hemi] = True
    activity = fMRIPanel.constrast[real_hemi]
    blob_activity = np.ones(activity.shape)
    blob_activity[blob_vertices] = activity[blob_vertices]
    if fMRIPanel.blobs_activity is None:
        fMRIPanel.blobs_activity, _ = calc_blobs_activity(
            fMRIPanel.constrast, fMRIPanel.clusters_labels_filtered, fMRIPanel.colors_in_hemis)
    data_min, colors_ratio = calc_colors_ratio(fMRIPanel.blobs_activity)
    threshold = bpy.context.scene.fmri_clustering_threshold
    cur_obj = bpy.data.objects[hemi]
    _addon().activity_map_obj_coloring(
        cur_obj, blob_activity, faces_verts[real_hemi], threshold, True,
        data_min=data_min, colors_ratio=colors_ratio, use_abs=use_abs)
    other_real_hemi = mu.other_hemi(real_hemi)
    other_hemi = mu.other_hemi(hemi)
    if other_hemi in fMRIPanel.colors_in_hemis and fMRIPanel.colors_in_hemis[other_hemi]:
        _addon().clear_cortex([other_real_hemi])
        fMRIPanel.colors_in_hemis[other_hemi] = False
Beispiel #2
0
def load_camera(camera_fname=''):
    if camera_fname == '':
        camera_fname = op.join(mu.get_user_fol(), 'camera',
                               '{}.pkl'.format(bpy.context.scene.camera_files))
    if op.isfile(camera_fname):
        camera_name = mu.namebase(camera_fname)
        for hemi in mu.HEMIS:
            if hemi in camera_name:
                _addon().show_hide_hemi(False, hemi)
                _addon().show_hide_hemi(True, mu.other_hemi(hemi))
        X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location = mu.load(
            camera_fname)
        RenderFigure.update_camera = False
        bpy.context.scene.X_rotation = X_rotation
        bpy.context.scene.Y_rotation = Y_rotation
        bpy.context.scene.Z_rotation = Z_rotation
        bpy.context.scene.X_location = X_location
        bpy.context.scene.Y_location = Y_location
        bpy.context.scene.Z_location = Z_location
        # print('Camera loaded: rotation: {},{},{} locatioin: {},{},{}'.format(
        #     X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location))
        # print('Camera loaded: {}'.format(camera_fname))
        RenderFigure.update_camera = True
        update_camera()
    else:
        pass
Beispiel #3
0
def load_surf_files(nii_fname, run_fmri_preproc=True, user_fol='', debug=True):
    fmri_file_template = ''
    if user_fol == '':
        user_fol = mu.get_user_fol()
    nii_fol = mu.get_fname_folder(nii_fname)
    if debug:
        print('load_surf_files: nii_fol: {}'.format(nii_fol))
    hemi, fmri_hemis = mu.get_hemi_from_full_fname(nii_fname)
    if debug:
        print('load_surf_files: hemi, fmri_hemis: {}, {}'.format(
            hemi, fmri_hemis))
    if hemi == '':
        hemi = mu.find_hemi_using_vertices_num(nii_fname)
        if hemi == '':
            return ''
    # fmri_hemis = mu.get_both_hemis_files(nii_fname)
    local_fname = build_local_fname(nii_fname, user_fol)
    if debug:
        print('load_surf_files: local_fname: {}'.format(local_fname))
    mu.make_dir(op.join(user_fol, 'fmri'))
    if nii_fol != op.join(user_fol, 'fmri'):
        mu.make_link(nii_fname, local_fname, True)
    other_hemi = mu.other_hemi(hemi)
    if debug:
        print('load_surf_files: other_hemi: {}'.format(other_hemi))
    other_hemi_fname = fmri_hemis[other_hemi]
    if other_hemi_fname == '':
        other_hemi_fname = local_fname.replace(hemi, other_hemi)
    if debug:
        print('load_surf_files: other_hemi_fname: {}'.format(other_hemi_fname))
    # todo: if the other hemi file doens't exist, just create an empty one
    output_fname_template = ''
    if op.isfile(other_hemi_fname):
        local_other_hemi_fname = build_local_fname(other_hemi_fname, user_fol)
        if nii_fol != op.join(user_fol, 'fmri'):
            mu.make_link(other_hemi_fname, local_other_hemi_fname, True)
        fmri_file_template = mu.get_template_hemi_label_name(
            mu.namebase_with_ext(local_fname))
        if run_fmri_preproc:
            mu.add_mmvt_code_root_to_path()
            from src.preproc import fMRI
            importlib.reload(fMRI)
            vertices_num = mu.get_vertices_num()
            ret, npy_output_fname_template = fMRI.load_surf_files(
                mu.get_user(), fmri_hemis, vertices_num=vertices_num)
            output_fname_template = op.join(
                mu.get_parent_fol(npy_output_fname_template),
                mu.namebase_with_ext(npy_output_fname_template)[len('fmri_'):])
        else:
            mu.run_mmvt_func(
                'src.preproc.fMRI',
                'load_surf_files',
                flags='--fmri_file_template "{}"'.format(fmri_file_template))
            # todo: find what should be the output_fname_template
    else:
        print(
            "Couldn't find the other hemi file! ({})".format(other_hemi_fname))
    return output_fname_template  #, hemi, other_hemi
Beispiel #4
0
def plot_blob(cluster_labels, faces_verts):
    _addon().init_activity_map_coloring('FMRI', subcorticals=False)
    blob_vertices = cluster_labels['vertices']
    hemi = cluster_labels['hemi']
    real_hemi = hemi
    if _addon().is_inflated():
        hemi = 'inflated_{}'.format(hemi)
    fMRIPanel.colors_in_hemis[hemi] = True
    activity = _addon().get_fMRI_activity(real_hemi)
    blob_activity = np.ones((len(activity), 4))
    blob_activity[blob_vertices] = activity[blob_vertices]
    cur_obj = bpy.data.objects[hemi]
    _addon().activity_map_obj_coloring(cur_obj, blob_activity, faces_verts[real_hemi],
                                       bpy.context.scene.fmri_cluster_val_threshold, True)
    other_real_hemi = mu.other_hemi(real_hemi)
    other_hemi = mu.other_hemi(hemi)
    if fMRIPanel.colors_in_hemis[other_hemi]:
        _addon().clear_cortex([other_real_hemi])
        fMRIPanel.colors_in_hemis[other_hemi] = False
Beispiel #5
0
def render_all_images():
    camera_files = glob.glob(op.join(bpy.path.abspath(bpy.context.scene.output_path), 'camera_*.pkl'))
    for camera_file in camera_files:
        load_camera(camera_file)
        camera_name = mu.namebase(camera_file)
        for hemi in mu.HEMIS:
            if hemi in camera_name:
                RenderingMakerPanel.addon.show_hide_hemi(False, hemi)
                RenderingMakerPanel.addon.show_hide_hemi(True, mu.other_hemi(hemi))
        render_image('{}_fig'.format(camera_name[len('camera') + 1:]))
Beispiel #6
0
def render_all_images():
    camera_files = glob.glob(
        op.join(bpy.path.abspath(bpy.context.scene.output_path),
                'camera_*.pkl'))
    for camera_file in camera_files:
        load_camera(camera_file)
        camera_name = mu.namebase(camera_file)
        for hemi in mu.HEMIS:
            if hemi in camera_name:
                RenderingMakerPanel.addon.show_hide_hemi(False, hemi)
                RenderingMakerPanel.addon.show_hide_hemi(
                    True, mu.other_hemi(hemi))
        render_image('{}_fig'.format(camera_name[len('camera') + 1:]))
Beispiel #7
0
def plot_blob(cluster_labels, faces_verts):
    fMRIPanel.addon.init_activity_map_coloring('FMRI', subcorticals=False)
    blob_vertices = cluster_labels['vertices']
    hemi = cluster_labels['hemi']
    fMRIPanel.colors_in_hemis[hemi] = True
    activity = fMRIPanel.addon.get_fMRI_activity(hemi)
    blob_activity = np.zeros((len(activity), 4))
    blob_activity[blob_vertices] = activity[blob_vertices]
    cur_obj = bpy.data.objects[hemi]
    fMRIPanel.addon.activity_map_obj_coloring(cur_obj, blob_activity, faces_verts[hemi], 0, True)
    other_hemi = mu.other_hemi(hemi)
    if fMRIPanel.colors_in_hemis[other_hemi]:
        fMRIPanel.addon.clear_cortex([other_hemi])
        fMRIPanel.colors_in_hemis[other_hemi] = False
Beispiel #8
0
def _save_image():
    if not bpy.context.scene.save_split_views:
        save_image(view_selected=bpy.context.scene.save_selected_view,
                   add_colorbar=bpy.context.scene.save_views_with_cb)
    else:
        images = {}
        org_hide = {hemi: mu.get_hemi_obj(hemi).hide for hemi in mu.HEMIS}
        for hemi in mu.HEMIS:
            mu.get_hemi_obj(hemi).hide = False
            mu.get_hemi_obj(mu.other_hemi(hemi)).hide = True
            images[hemi] = save_image(view_selected=bpy.context.scene.save_selected_view, add_colorbar=False)
        for hemi in mu.HEMIS:
            mu.get_hemi_obj(hemi).hide = org_hide[hemi]
        combine_two_images_and_add_colorbar(images['lh'], images['rh'], images['lh'])
Beispiel #9
0
def save_all_views(views=None, inflated_ratio_in_file_name=False, rot_lh_axial=None, render_images=False, quality=0,
                   img_name_prefix='', add_colorbar=None, cb_ticks_num=None, cb_ticks_font_size=None, overwrite=True):
    if not render_images:
        _addon().change_to_solid_brain()
    if add_colorbar is None:
        add_colorbar = bpy.context.scene.save_views_with_cb
    if cb_ticks_num is None:
        cb_ticks_num = bpy.context.scene.cb_ticks_num
    if cb_ticks_font_size is None:
        cb_ticks_font_size = bpy.context.scene.cb_ticks_font_size

    _addon().show_hemis()
    if not bpy.context.scene.save_split_views:
        rot_lh_axial = False if rot_lh_axial is None else rot_lh_axial
        _save_all_views(views, inflated_ratio_in_file_name, rot_lh_axial, render_images, quality,  img_name_prefix,
                        add_colorbar, cb_ticks_num, cb_ticks_font_size, overwrite)
    else:
        if views is None:
            views = list(set(_addon().ANGLES_DICT.keys()) - set([_addon().ROT_MEDIAL_LEFT, _addon().ROT_MEDIAL_RIGHT]))
        images_names = []
        rot_lh_axial = True if rot_lh_axial is None else rot_lh_axial
        org_hide = {hemi: mu.get_hemi_obj(hemi).hide for hemi in mu.HEMIS}
        for hemi in mu.HEMIS:
            mu.get_hemi_obj(hemi).hide = False
            mu.get_hemi_obj(mu.other_hemi(hemi)).hide = True
            images_names.extend(_save_all_views(
                views, inflated_ratio_in_file_name, rot_lh_axial, render_images, quality,  img_name_prefix, False,
                overwrite=overwrite))
        for hemi in mu.HEMIS:
            mu.get_hemi_obj(hemi).hide = org_hide[hemi]
        images_hemi_inv_list = set(
            [mu.namebase(fname)[3:] for fname in images_names if mu.namebase(fname)[:2] in ['rh', 'lh']])
        files = [[fname for fname in images_names if mu.namebase(fname)[3:] == img_hemi_inv] for img_hemi_inv in
                 images_hemi_inv_list]
        fol = mu.get_fname_folder(files[0][0])
        for files_coup in files:
            hemi = 'rh' if mu.namebase(files_coup[0]).startswith('rh') else 'lh'
            coup_template = files_coup[0].replace(hemi, '{hemi}')
            coup = {hemi: coup_template.format(hemi=hemi) for hemi in mu.HEMIS}
            new_image_fname = op.join(fol, mu.namebase_with_ext(files_coup[0])[3:])
            combine_two_images_and_add_colorbar(
                coup['lh'], coup['rh'], new_image_fname, cb_ticks_num, cb_ticks_font_size)
    _addon().show_hemis()
Beispiel #10
0
def load_camera(camera_fname=''):
    if camera_fname == '':
        camera_fname = op.join(mu.get_user_fol(), 'camera', '{}.pkl'.format(bpy.context.scene.camera_files))
    if op.isfile(camera_fname):
        camera_name = mu.namebase(camera_fname)
        for hemi in mu.HEMIS:
            if hemi in camera_name:
                _addon().show_hide_hemi(False, hemi)
                _addon().show_hide_hemi(True, mu.other_hemi(hemi))
        X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location = mu.load(camera_fname)
        RenderFigure.update_camera = False
        bpy.context.scene.X_rotation = X_rotation
        bpy.context.scene.Y_rotation = Y_rotation
        bpy.context.scene.Z_rotation = Z_rotation
        bpy.context.scene.X_location = X_location
        bpy.context.scene.Y_location = Y_location
        bpy.context.scene.Z_location = Z_location
        RenderFigure.update_camera = True
        update_camera()
    else:
        print('No camera file was found in {}!'.format(camera_fname))
Beispiel #11
0
def load_surf_files(nii_fname):
    fmri_file_template = ''
    user_fol = mu.get_user_fol()
    nii_fol = mu.get_fname_folder(nii_fname)
    fmri_hemis = mu.get_both_hemis_files(nii_fname)
    hemi = mu.get_hemi_from_full_fname(nii_fname)
    local_fname = build_local_fname(nii_fname, user_fol)
    mu.make_dir(op.join(user_fol, 'fmri'))
    if nii_fol != op.join(user_fol, 'fmri'):
        mu.make_link(nii_fname, local_fname, True)
    other_hemi = mu.other_hemi(hemi)
    other_hemi_fname = fmri_hemis[other_hemi]
    if op.isfile(other_hemi_fname):
        local_other_hemi_fname = build_local_fname(other_hemi_fname, user_fol)
        if nii_fol != op.join(user_fol, 'fmri'):
            mu.make_link(other_hemi_fname, local_other_hemi_fname, True)
        fmri_file_template = mu.get_template_hemi_label_name(mu.namesbase_with_ext(local_fname))
        cmd = '{} -m src.preproc.fMRI -s {} -f load_surf_files --fmri_file_template "{}"'.format(
            bpy.context.scene.python_cmd, mu.get_user(), fmri_file_template)
        mu.run_command_in_new_thread(cmd, False)
    return fmri_file_template, hemi
Beispiel #12
0
def load_surf_files(nii_fname, run_fmri_preproc=True, user_fol=''):
    fmri_file_template = ''
    if user_fol == '':
        user_fol = mu.get_user_fol()
    nii_fol = mu.get_fname_folder(nii_fname)
    # hemi = mu.get_hemi_from_fname(mu.namebase(nii_fname))
    # if hemi == '':
    hemi, fmri_hemis = mu.get_hemi_from_full_fname(nii_fname)
    if hemi == '':
        hemi = mu.find_hemi_using_vertices_num(nii_fname)
        if hemi == '':
            return '', ''
    # fmri_hemis = mu.get_both_hemis_files(nii_fname)
    local_fname = build_local_fname(nii_fname, user_fol)
    mu.make_dir(op.join(user_fol, 'fmri'))
    if nii_fol != op.join(user_fol, 'fmri'):
        mu.make_link(nii_fname, local_fname, True)
    other_hemi = mu.other_hemi(hemi)
    other_hemi_fname = fmri_hemis[other_hemi]
    # todo: if the other hemi file doens't exist, just create an empty one
    if op.isfile(other_hemi_fname):
        local_other_hemi_fname = build_local_fname(other_hemi_fname, user_fol)
        if nii_fol != op.join(user_fol, 'fmri'):
            mu.make_link(other_hemi_fname, local_other_hemi_fname, True)
        fmri_file_template = mu.get_template_hemi_label_name(
            mu.namebase_with_ext(local_fname))
        if run_fmri_preproc:
            mu.run_mmvt_func(
                'src.preproc.fMRI',
                'load_surf_files',
                flags='--fmri_file_template "{}"'.format(fmri_file_template))
            # cmd = '{} -m  -s {} -f  --fmri_file_template "{}" --ignore_missing 1'.format(
            #     bpy.context.scene.python_cmd, mu.get_user(), fmri_file_template)
            # mu.run_command_in_new_thread(cmd, False, cwd=mu.get_mmvt_code_root())
    else:
        print(
            "Couldn't find the other hemi file! ({})".format(other_hemi_fname))
    return fmri_file_template, hemi, other_hemi