Пример #1
0
def build_local_fname(nii_fname, user_fol):
    if mu.get_hemi_from_fname(mu.namebase_with_ext(nii_fname)) == '':
        local_fname = mu.get_label_for_full_fname(nii_fname)
        # local_fname = '{}.{}.{}'.format(mu.namebase(nii_fname), hemi, mu.file_type(nii_fname))
    else:
        local_fname = mu.namebase_with_ext(nii_fname)
    return op.join(user_fol, 'fmri', local_fname)
Пример #2
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
Пример #3
0
def init(addon):
    if not bpy.data.objects.get('full_colorbar', None):
        print("No full_colorbar object, Can't load the colorbar panel")
        return
    ColorbarPanel.addon = addon
    colorbar_files_fol = mu.make_dir(
        op.join(mu.get_parent_fol(mu.get_user_fol()), 'color_maps'))
    colorbar_files_code_fol = op.join(mu.get_resources_dir(), 'color_maps')
    colorbar_files_template = op.join(colorbar_files_fol, '*.npy')
    colorbar_files_code_template = op.join(colorbar_files_code_fol, '*.npy')
    # colorbar_files_template = op.join(mu.file_fol(), 'color_maps', '*.npy')
    colorbar_files = glob.glob(colorbar_files_template)
    colorbar_code_files = glob.glob(colorbar_files_code_template)
    extra_files = list(
        set([mu.namebase_with_ext(f) for f in colorbar_code_files]) -
        set([mu.namebase_with_ext(f) for f in colorbar_files]))
    for extra_file in extra_files:
        print('Coping missing cb file: {}'.format(
            mu.namebase_with_ext(extra_file)))
        shutil.copyfile(
            op.join(colorbar_files_code_fol, extra_file),
            op.join(colorbar_files_fol, mu.namebase_with_ext(extra_file)))
    if len(colorbar_files) == 0:
        print("No colorbar files ({}), Can't load the colorbar panel".format(
            colorbar_files_template))
        return None
    colorbar_files = glob.glob(colorbar_files_template)
    files_names = [mu.namebase(fname)
                   for fname in colorbar_files]  # .replace('_', '-')
    ColorbarPanel.maps_names = files_names
    colorbar_items = [(c, c, '', ind) for ind, c in enumerate(files_names)]
    bpy.types.Scene.colorbar_files = bpy.props.EnumProperty(
        items=colorbar_items,
        update=colormap_update,
        description='Selects the colorbar color palette.\n\nCurrent palette')
    if not colorbar_values_are_locked():
        bpy.context.scene.colorbar_files = files_names[0]
    else:
        load_colormap()
    for space in mu.get_3d_spaces():
        if space.lock_object and space.lock_object.name == 'full_colorbar':
            space.show_only_render = True
    register()
    ColorbarPanel.init = True
    bpy.context.scene.show_cb_in_render = False
    mu.select_hierarchy('colorbar_camera', False, False)
    if not ColorbarPanel.colorbar_updated and not colorbar_values_are_locked():
        ColorbarPanel.should_not_lock_values = True
        # bpy.context.scene.colorbar_min = -1
        # bpy.context.scene.colorbar_max = 1
        # bpy.context.scene.colorbar_title = '     MEG'
        bpy.context.scene.colorbar_y = 0.18
        bpy.context.scene.colorbar_text_y = -1.53
        bpy.context.scene.colorbar_prec = 2
        ColorbarPanel.should_not_lock_values = False
Пример #4
0
def run_ela_alg():
    mmvt_code_fol = mu.get_mmvt_code_root()
    ela_code_fol = op.join(mu.get_parent_fol(mmvt_code_fol), 'electrodes_rois')
    if not op.isdir(ela_code_fol) or not op.isfile(op.join(ela_code_fol, 'find_rois', 'find_rois.py')):
        print("Can't find ELA folder!")
        return

    import importlib
    import sys
    if ela_code_fol not in sys.path:
        sys.path.append(ela_code_fol)
    from find_rois import find_rois
    importlib.reload(find_rois)
    args = find_rois.get_args(['-s', mu.get_user(), '-a', bpy.context.scene.ela_atlas,
                               '-b', str(bpy.context.scene.ela_bipolar)])
    find_rois.run_for_all_subjects(args)
    import shutil
    ela_output_fname_template = op.join(
        ela_code_fol, 'electrodes', '{}_{}_electrodes_cigar_r_*_l_*{}.pkl'.format(
            mu.get_user(), bpy.context.scene.ela_atlas, '_bipolar' if bpy.context.scene.ela_bipolar else '' ))
    ela_output_fnames = glob.glob(ela_output_fname_template)
    if len(ela_output_fnames) > 0:
        for ela_output_fname in ela_output_fnames:
            shutil.copyfile(ela_output_fname, op.join(
                mu.get_user_fol(), 'electrodes', mu.namebase_with_ext(ela_output_fname)))
    else:
        print('couldn\'t find any ELA output! ({})'.format(ela_output_fname_template))
    init(_addon(), False)
Пример #5
0
 def execute(self, context):
     stc_fname = self.filepath
     user_fol = mu.get_user_fol()
     stc_fol = mu.get_fname_folder(stc_fname)
     if stc_fol != op.join(user_fol, 'meg'):
         other_hemi_stc_fname = op.join(
             stc_fol, '{}.stc'.format(
                 mu.get_other_hemi_label_name(mu.namebase(stc_fname))))
         shutil.copy(
             stc_fname,
             op.join(user_fol, 'meg', mu.namebase_with_ext(stc_fname)))
         shutil.copy(
             other_hemi_stc_fname,
             op.join(user_fol, 'meg',
                     mu.namebase_with_ext(other_hemi_stc_fname)))
         _addon().init_meg_activity_map()
     _, _, label, hemi = mu.get_hemi_delim_and_pos(
         mu.namebase(stc_fname))
     bpy.context.scene.meg_files = label
     return {'FINISHED'}
Пример #6
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()
Пример #7
0
def load_labels_data(labels_data_fname):
    ret = _load_labels_data(labels_data_fname)
    if isinstance(ret, bool):
        return ret
    d, labels, data, atlas, cb_title, labels_max, labels_min, cmap = ret
    _addon().color_labels_data(labels, data, atlas, cb_title, labels_max, labels_min, cmap)
    new_fname = op.join(mu.get_user_fol(), 'labels', 'labels_data', mu.namebase_with_ext(labels_data_fname))
    if 'atlas' not in d:
        npz_dict = dict(d)
        npz_dict['atlas'] = atlas
        np.savez(new_fname, **npz_dict)
    else:
        if new_fname != labels_data_fname:
            shutil.copy(labels_data_fname, new_fname)
        # init_labels_data_files()
    if atlas in _addon().where_am_i.get_annot_files() and bpy.context.scene.subject_annot_files != atlas:
        bpy.context.scene.subject_annot_files = atlas
    bpy.context.scene.find_closest_label_on_click = True
    _addon().find_closest_label(atlas=atlas, plot_contour=bpy.context.scene.plot_closest_label_contour)
    return True
Пример #8
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