Esempio n. 1
0
def find_closest_label(atlas=None, plot_contour=True):
    subjects_dir = mu.get_link_dir(mu.get_links_dir(), 'subjects')
    if bpy.context.scene.cursor_is_snapped:
        vertex_ind, hemi = _addon().get_closest_vertex_and_mesh_to_cursor()
    else:
        closest_mesh_name, vertex_ind, _ = \
            _addon().find_closest_vertex_index_and_mesh(use_shape_keys=True)
        hemi = closest_mesh_name[len('infalted_'):] if _addon().is_inflated() else closest_mesh_name
    if vertex_ind == -1:
        print("find_closest_label: Can't find the closest vertex")
        return
    hemi = 'rh' if 'rh' in hemi else 'lh'
    if atlas is None:
        atlas = bpy.context.scene.subject_annot_files
    annot_fname = op.join(subjects_dir, mu.get_user(), 'label', '{}.{}.annot'.format(hemi, atlas))
    if not op.isfile(annot_fname):
        annot_fname = op.join(mu.get_user_fol(), 'labels', '{}.{}.annot'.format(hemi, atlas))
    if op.isfile(annot_fname):
        labels = mu.read_labels_from_annot(annot_fname)
        vert_labels = [l for l in labels if vertex_ind in l.vertices]
        if len(vert_labels) > 0:
            label = vert_labels[0]
            bpy.context.scene.closest_label_output = label.name
            if plot_contour:
                plot_closest_label_contour(label, hemi)
        return label.name
    else:
        print("Can't find the annotation file for atlas {}!".format(atlas))
Esempio n. 2
0
def init(addon):
    HNNPanel.addon = addon
    hnn_folder = mu.get_link_dir(mu.get_links_dir(), 'hnn')
    if op.isdir(hnn_folder):
        bpy.context.scene.hnn_folder = hnn_folder
        init_hnn_files()
    register()
    HNNPanel.init = True
Esempio n. 3
0
def set_hnn_folder(self, context):
    hnn_folder = op.abspath(bpy.path.abspath(bpy.context.scene.hnn_folder))
    if not op.isdir(hnn_folder):
        return
    hnn_link = op.join(mu.get_links_dir(), 'hnn')
    if op.islink(hnn_link):
        os.remove(hnn_link)
    try:
        os.symlink(hnn_folder, hnn_link)
    except:
        print('set_hnn_folder: Error in creating hnn link!')
    init_hnn_files()
Esempio n. 4
0
def init(addon):
    try:
        trans_fname = op.join(mu.get_user_fol(), 'orig_trans.npz')
        volumes = glob.glob(
            op.join(mu.get_user_fol(), 'freeview', '*+aseg.npy'))
        luts = glob.glob(
            op.join(mu.get_user_fol(), 'freeview', '*ColorLUT.npz'))
        if op.isfile(trans_fname):
            WhereAmIPanel.subject_orig_trans = mu.Bag(np.load(trans_fname))
        for atlas_vol_fname, atlas_vol_lut_fname in zip(volumes, luts):
            atlas = mu.namebase(atlas_vol_fname)[:-len('+aseg')]
            WhereAmIPanel.vol_atlas[atlas] = np.load(atlas_vol_fname)
            WhereAmIPanel.vol_atlas_lut[atlas] = np.load(atlas_vol_lut_fname)
        subjects_dir = mu.get_link_dir(mu.get_links_dir(), 'subjects')
        annot_files = glob.glob(
            op.join(subjects_dir, mu.get_user(), 'label', 'rh.*.annot'))
        if len(annot_files) > 0:
            files_names = [mu.namebase(fname)[3:] for fname in annot_files]
            items = [(c, c, '', ind) for ind, c in enumerate(files_names)]
            bpy.types.Scene.subject_annot_files = bpy.props.EnumProperty(
                items=items)
            bpy.context.scene.subject_annot_files = files_names[0]
        else:
            bpy.types.Scene.subject_annot_files = bpy.props.EnumProperty(
                items=[])
            # bpy.context.scene.subject_annot_files = ''

        bpy.context.scene.closest_label_output = ''
        bpy.context.scene.new_label_r = 5

        mri_data_fname = op.join(mu.get_user_fol(), 'freeview',
                                 '{}_data.npz'.format('mri'))
        if op.isfile(mri_data_fname):
            WhereAmIPanel.mri_data = mu.Bag(np.load(mri_data_fname))
        gray_colormap_fname = op.join(mu.file_fol(), 'color_maps', 'gray.npy')
        if op.isfile(gray_colormap_fname):
            WhereAmIPanel.gray_colormap = np.load(gray_colormap_fname)

        WhereAmIPanel.addon = addon
        WhereAmIPanel.init = True
        WhereAmIPanel.run_slices_listener = False
        init_slices()
        if WhereAmIPanel.run_slices_listener:
            start_slicer_server()
        else:
            WhereAmIPanel.slicer_state = slicer.init('mri')
            create_slices('mri', bpy.context.scene.cursor_location)
        save_slices_cursor_pos()
        register()
    except:
        print("Can't init where-am-I panel!")
        print(traceback.format_exc())
Esempio n. 5
0
def find_closest_label():
    subjects_dir = mu.get_link_dir(mu.get_links_dir(), 'subjects')
    closest_mesh_name, vertex_ind, vertex_co, _ = \
        _addon().find_vertex_index_and_mesh_closest_to_cursor(use_shape_keys=True)
    hemi = closest_mesh_name[len('infalted_'):] if _addon().is_inflated(
    ) else closest_mesh_name
    annot_fname = op.join(
        subjects_dir, mu.get_user(), 'label',
        '{}.{}.annot'.format(hemi, bpy.context.scene.subject_annot_files))
    labels = mu.read_labels_from_annot(annot_fname)
    vert_labels = [l for l in labels if vertex_ind in l.vertices]
    if len(vert_labels) > 0:
        label = vert_labels[0]
        bpy.context.scene.closest_label_output = label.name
        return label.name, hemi
    else:
        return 'unknown', hemi
Esempio n. 6
0
def init(addon):
    try:
        t1_trans_fname = op.join(mu.get_user_fol(), 't1_trans.npz')
        if not op.isfile(t1_trans_fname):
            # backward compatibility
            t1_trans_fname = op.join(mu.get_user_fol(), 'orig_trans.npz')
        t2_trans_fname = op.join(mu.get_user_fol(), 't2_trans.npz')
        ct_trans_fname = op.join(mu.get_user_fol(), 'ct', 'ct_trans.npz')
        volumes = glob.glob(op.join(mu.get_user_fol(), 'freeview', '*+aseg.npy'))
        luts = glob.glob(op.join(mu.get_user_fol(), 'freeview', '*ColorLUT.npz'))
        if op.isfile(t1_trans_fname):
            WhereAmIPanel.subject_t1_trans = mu.Bag(np.load(t1_trans_fname))
        if op.isfile(t2_trans_fname):
            WhereAmIPanel.subject_t2_trans = mu.Bag(np.load(t2_trans_fname))
        if op.isfile(ct_trans_fname):
            WhereAmIPanel.subject_ct_trans = mu.Bag(np.load(ct_trans_fname))
        for atlas_vol_fname, atlas_vol_lut_fname in zip(volumes, luts):
            atlas = mu.namebase(atlas_vol_fname)[:-len('+aseg')]
            WhereAmIPanel.vol_atlas[atlas] = np.load(atlas_vol_fname)
            WhereAmIPanel.vol_atlas_lut[atlas] = np.load(atlas_vol_lut_fname)
        try:
            subjects_dir = mu.get_link_dir(mu.get_links_dir(), 'subjects')
            annot_files = [mu.namebase(fname)[3:] for fname in glob.glob(
                op.join(subjects_dir, mu.get_user(), 'label', 'rh.*.annot'))]
            annot_files += [mu.namebase(fname)[3:] for fname in glob.glob(
                op.join(mu.get_user_fol(), 'labels', 'rh.*.annot'))]
            WhereAmIPanel.annot_files = annot_files = list(set(annot_files))
            if len(annot_files) > 0:
                items = [(c, c, '', ind) for ind, c in enumerate(annot_files)]
                bpy.types.Scene.subject_annot_files = bpy.props.EnumProperty(
                    items=items, update=subject_annot_files_update, description='List of different atlases.\n\nCurrent atlas')
                ind = mu.index_in_list(bpy.context.scene.atlas, annot_files, 0)
                bpy.context.scene.subject_annot_files = annot_files[ind]
            else:
                bpy.types.Scene.subject_annot_files = bpy.props.EnumProperty(items=[])
                # bpy.context.scene.subject_annot_files = ''
        except:
            bpy.types.Scene.subject_annot_files = bpy.props.EnumProperty(items=[])
        bpy.context.scene.closest_label_output = ''
        bpy.context.scene.new_label_r = 5
        bpy.context.scene.find_closest_label_on_click = False
        bpy.context.scene.plot_closest_label_contour = False

        mri_data_fname = op.join(mu.get_user_fol(), 'freeview', 'mri_data.npz')
        if op.isfile(mri_data_fname):
            WhereAmIPanel.mri_data = mu.Bag(np.load(mri_data_fname))
        gray_colormap_fname = op.join(mu.file_fol(), 'color_maps', 'gray.npy')
        if op.isfile(gray_colormap_fname):
            WhereAmIPanel.gray_colormap = np.load(gray_colormap_fname)

        WhereAmIPanel.addon = addon
        WhereAmIPanel.init = True
        WhereAmIPanel.run_slices_listener = False
        init_slices()
        if WhereAmIPanel.run_slices_listener:
            start_slicer_server()
        else:
            WhereAmIPanel.slicer_state = mu.Bag({})
            WhereAmIPanel.slicer_state['mri'] = slicer.init(_addon(), 'mri')
            create_slices(None, bpy.context.scene.cursor_location)
        save_slices_cursor_pos()
        register()
    except:
        print("Can't init where-am-I panel!")
        print(traceback.format_exc())