Ejemplo n.º 1
0
def rotate_brain_via_lookup(elc_name, loc):
    cortical_probs_max, subcortical_probs_max = 0, 0
    cortical_probs_rois = \
        [(p, r) for p, r in zip(loc['cortical_probs'], loc['cortical_rois']) if 'white' not in r.lower()]
    subcortical_probs_rois = \
        [(p, r) for p, r in zip(loc['subcortical_probs'], loc['subcortical_rois']) if 'white' not in r.lower()]
    if len(cortical_probs_rois) > 0:
        cortical_probs_ind = np.argmax(cortical_probs_rois, 0)[0]
        cortical_probs_max = cortical_probs_rois[cortical_probs_ind][0]
    if len(subcortical_probs_rois) > 0:
        subcortical_probs_ind = np.argmax(subcortical_probs_rois, 0)[0]
        subcortical_probs_max = subcortical_probs_rois[subcortical_probs_ind][0]
    if cortical_probs_max > subcortical_probs_max:
        max_roi = cortical_probs_rois[cortical_probs_ind][1]
        max_roi_is_cortical = True
    else:
        max_roi = subcortical_probs_rois[subcortical_probs_ind][1]
        max_roi_is_cortical = False
    if max_roi_is_cortical:
        atlas = bpy.context.scene.electrodes_labeling_files.split('_')[1]
        hemi = mu.get_obj_hemi(max_roi)
        annot_fname = mu.get_annot_fname(hemi, atlas)
        max_roi_vertices_indices = mu.read_label_vertices_from_annot(annot_fname, max_roi)
        if max_roi_vertices_indices is None:
            print('Can\'t find {} vertices in {}!'.format(max_roi, annot_fname))
            return
        elc_pos = bpy.data.objects[elc_name].location
        mesh = 'inflated_{}'.format(hemi)
        vert_ind, vert_pos = _addon().vertex_data.find_closest_vertices(
            elc_pos, max_roi_vertices_indices, mesh, False)
        vert_pos *= 0.1
        mu.rotate_view_to_vertice(vert_ind, mesh)
Ejemplo n.º 2
0
def _clusters_update():
    if fMRIPanel.addon is None or not fMRIPanel.init:
        return
    clusters_labels_file = bpy.context.scene.fmri_clusters_labels_files
    # key = '{}_{}'.format(clusters_labels_file, bpy.context.scene.fmri_clusters_labels_parcs)
    key = clusters_labels_file
    fMRIPanel.cluster_labels = cluster = fMRIPanel.lookup[key][bpy.context.scene.fmri_clusters]
    cluster_centroid = np.mean(cluster['coordinates'], 0) / 10.0
    _addon().clear_closet_vertex_and_mesh_to_cursor()
    _addon().set_vertex_data(cluster['max'])
    if 'max_vert' in cluster:
        bpy.context.scene.cursor_location = mu.get_vert_co(cluster.max_vert, cluster.hemi)
        _addon().set_closest_vertex_and_mesh_to_cursor(cluster.max_vert, 'inflated_{}'.format(cluster.hemi))
    else:
        if _addon().is_pial():
            bpy.context.scene.cursor_location = cluster_centroid
            closest_mesh_name, vertex_ind, vertex_co = _addon().find_vertex_index_and_mesh_closest_to_cursor(
                cluster_centroid, mu.HEMIS, False)
            _addon().set_closest_vertex_and_mesh_to_cursor(vertex_ind, closest_mesh_name)
        else:
            closest_mesh_name, vertex_ind, vertex_co = _addon().find_vertex_index_and_mesh_closest_to_cursor(
                cluster_centroid, mu.HEMIS, False)
            inflated_mesh = 'inflated_{}'.format(closest_mesh_name)
            me = bpy.data.objects[inflated_mesh].to_mesh(bpy.context.scene, True, 'PREVIEW')
            bpy.context.scene.cursor_location = me.vertices[vertex_ind].co / 10.0
            bpy.data.meshes.remove(me)
            _addon().set_closest_vertex_and_mesh_to_cursor(vertex_ind, closest_mesh_name)

    tkreg_ras = _addon().calc_tkreg_ras_from_cursor()
    if tkreg_ras is not None:
        _addon().set_tkreg_ras(tkreg_ras, move_cursor=False)

    if bpy.context.scene.plot_current_cluster and not fMRIPanel.blobs_plotted:
        faces_verts = fMRIPanel.addon.get_faces_verts()
        if bpy.context.scene.fmri_what_to_plot == 'blob':
            plot_blob(cluster, faces_verts, True)
    if bpy.context.scene.plot_fmri_cluster_contours:
        inter_labels = [inter_label['name'] for inter_label in cluster['intersects']]
        atlas = fMRIPanel.clusters_labels[bpy.context.scene.fmri_clusters_labels_files].atlas
        _addon().color_contours(
            inter_labels, cluster.hemi, None, False, False,
            specific_colors=bpy.context.scene.fmri_cluster_contours_color, atlas=atlas)

    _addon().save_cursor_position()
    _addon().create_slices(pos=tkreg_ras)
    find_electrodes_in_cluster()
    if bpy.context.scene.fmri_rotate_view_to_vertice:
        mu.rotate_view_to_vertice()
Ejemplo n.º 3
0
 def invoke(self, context, event=None):
     label_name = context.scene.labels_regex
     SearchMark.marked_objects_select = {}
     objects = mu.get_non_functional_objects()
     SearchPanel.marked_objects = []
     for obj in objects:
         SearchFilter.marked_objects_select[obj.name] = obj.select
         obj.select = label_name in obj.name
         try:
             import fnmatch
             if fnmatch.fnmatch(obj.name, '*{}*'.format(label_name)):
                 SearchPanel.marked_objects.append(obj.name)
         except:
             if label_name in obj.name:
                 SearchPanel.marked_objects.append(obj.name)
     if len(SearchPanel.marked_objects) == 0:
         print('No objects found for "{}"'.format(
             context.scene.labels_regex))
         return
     # todo: show rois only if the object is an ROI. Also, move the cursor
     selected_obj = bpy.data.objects[SearchPanel.marked_objects[0]]
     verts = np.array([vert.co for vert in selected_obj.data.vertices])
     center = np.mean(verts, axis=0)
     if any([mu.obj_is_cortex(o) for o in SearchPanel.marked_objects]):
         selected_roi = [
             o for o in SearchPanel.marked_objects if mu.obj_is_cortex(o)
         ][0]
         hemi = mu.get_obj_hemi(selected_roi)
         center = _addon().where_am_i.pos_to_current_inflation(center,
                                                               hemis=[hemi])
     else:
         center = mathutils.Vector(center) * mu.get_matrix_world()
         # if bpy.context.scene.search_plot_contour:
         #     _addon().where_am_i.plot_closest_label_contour(selected_roi, hemi)
     # bpy.context.scene.cursor_location = tuple(center)
     _addon().where_am_i.set_cursor_location(center)
     # _addon().set_cursor_pos()
     # _addon().set_tkreg_ras(bpy.context.scene.cursor_location * 10, False)
     if bpy.context.scene.slices_rotate_view_on_click:
         mu.rotate_view_to_vertice()
     # if any([mu.obj_is_cortex(o.name) for o in SearchPanel.marked_objects]):
     # SearchPanel.addon.show_rois()
     return {"FINISHED"}
Ejemplo n.º 4
0
    def modal(self, context, event):
        if self.left_clicked:
            self.left_clicked = False
            active_image, pos = click_inside_images_view(event)
            if active_image is not None:
                xyz = _addon().slices_were_clicked(active_image, pos)
                bpy.context.scene.cursor_location = tuple(xyz)
                set_cursor_pos()
                if bpy.context.scene.cursor_is_snapped:  # and is_view_3d():
                    _addon().set_tkreg_ras(
                        bpy.context.scene.cursor_location * 10, False)
                    snap_cursor(True)
                    if bpy.context.scene.slices_rotate_view_on_click:
                        mu.rotate_view_to_vertice()
                # if bpy.context.scene.slices_zoom > 1:
                #     ohad(pos/bpy.context.scene.slices_zoom)
                return {'PASS_THROUGH'}
            if not click_inside_3d_view(event):
                return {'PASS_THROUGH'}
            if _addon().meg.change_cursor_on_selection():
                cluster = _addon().select_meg_cluster(
                    event, context, bpy.context.scene.cursor_location)
                if cluster is not None:
                    return {'PASS_THROUGH'}
            cursor_moved = np.linalg.norm(
                SelectionListener.cursor_pos -
                bpy.context.scene.cursor_location) > 1e-3
            if cursor_moved and bpy.data.objects.get('inner_skull',
                                                     None) is not None:
                _addon().find_point_thickness()
                return {'PASS_THROUGH'}
            if bpy.context.scene.cursor_is_snapped:  # and is_view_3d():
                snap_cursor(True)
            if _addon().fMRI_clusters_files_exist(
            ) and bpy.context.scene.plot_fmri_cluster_per_click:
                _addon().find_closest_cluster(only_within=True)

            tkreg_ras = _addon().calc_tkreg_ras_from_cursor()
            if tkreg_ras is not None:
                _addon().set_tkreg_ras(tkreg_ras, move_cursor=False)
            # if _addon().is_pial():
            #     tkreg_ras = bpy.context.scene.cursor_location * 10
            #     _addon().set_tkreg_ras(tkreg_ras)
            # elif bpy.context.scene.cursor_is_snapped:
            #     tkreg_ras = _addon().calc_tkreg_ras_from_snapped_cursor()
            #     _addon().set_tkreg_ras(tkreg_ras)

            if cursor_moved:
                set_cursor_pos()
                # print('cursor position was changed by the user!')
                _addon().create_slices(pos=tkreg_ras)
                _addon().freeview.save_cursor_position()
                clear_slice()
            if bpy.context.scene.find_closest_label_on_click:  # coloring_panel.WIC_CONTOURS in _addon().what_is_colored():
                _addon().find_closest_label(
                    plot_contour=bpy.context.scene.plot_closest_label_contour)

        if self.right_clicked:
            self.right_clicked = False
            if not click_inside_3d_view(event):
                return {'PASS_THROUGH'}
            # print(bpy.context.selected_objects)
            # cluster = _addon().select_meg_cluster(event, context)
            # if cluster is not None:
            #     return {'PASS_THROUGH'}
            if len(bpy.context.selected_objects):
                mu.unfilter_graph_editor()
                if bpy.context.scene.fit_graph_on_selection:
                    mu.view_all_in_graph_editor()
                selected_obj = bpy.context.active_object  # bpy.context.selected_objects[-1]
                selected_obj_name = selected_obj.name
                selected_obj_type = mu.check_obj_type(selected_obj_name)
                if selected_obj_type in [
                        mu.OBJ_TYPE_CORTEX_LH, mu.OBJ_TYPE_CORTEX_RH
                ]:
                    _addon().select_roi(selected_obj_name)
                elif selected_obj_type in [
                        mu.OBJ_TYPE_CORTEX_INFLATED_LH,
                        mu.OBJ_TYPE_CORTEX_INFLATED_RH
                ]:
                    pial_obj_name = selected_obj_name[len('inflated_'):]
                    pial_obj = bpy.data.objects.get(pial_obj_name)
                    if not pial_obj is None:
                        # pial_obj.select = True
                        _addon().select_roi(pial_obj_name)
                        # mu.change_selected_fcurves_colors(pial_obj)
                        # mu.change_selected_fcurves_colors()
                elif selected_obj_type == mu.OBJ_TYPE_CON:
                    _addon().select_connection(selected_obj_name)
                elif selected_obj_type == mu.OBJ_TYPE_CON_VERTICE:
                    _addon().vertices_selected(selected_obj_name)
                elif selected_obj_type == mu.OBJ_TYPE_ELECTRODE:
                    bpy.context.scene.cursor_is_snapped = False
                    _addon().electode_was_manually_selected(selected_obj_name)
                    try:
                        _addon().dell.dell_ct_electrode_was_selected(
                            selected_obj_name)
                    except:
                        pass
                if bpy.context.scene.find_curves_sep_auto:
                    _addon().calc_best_curves_sep()
                elif bpy.context.scene.curves_sep > 0:
                    _addon().curves_sep_update()
            else:
                _addon().clear_electrodes_selection()
                #todo: should call to _addon().clear_rois_selection()
                # if is_activity():
                #     bpy.context.scene.cursor_location = mouse_coo_to_3d_loc(event, context)
                #     snap_cursor(True)
                #     _addon().find_closest_label()

        if time.time() - self.press_time > 1 and event.value == 'PRESS':
            if event.type == 'RIGHTMOUSE':
                self.press_time = time.time()
                self.right_clicked = True
            if event.type == 'LEFTMOUSE':
                self.press_time = time.time()
                self.left_clicked = True

        if time.time() - self.press_time > 0.1:
            if event.type == 'TIMER':
                if bpy.context.scene.rotate_brain:
                    if _addon().render.is_camera_view():
                        _addon().render.camera_mode('ORTHO')
                        _addon().show_hide.rotate_brain()
                        _addon().render.camera_mode('CAMERA')
                    else:
                        _addon().show_hide.rotate_brain()

        if _addon() and _addon().render_in_queue():
            rendering_data = mu.queue_get(_addon().render_in_queue())
            if not rendering_data is None:
                try:
                    rendering_data = rendering_data.decode(
                        sys.getfilesystemencoding(), 'ignore')
                    if '*** finish rendering! ***' in rendering_data.lower():
                        print('Finish rendering!')
                        _addon().finish_rendering()
                except:
                    print("Can't read the stdout from the rendering")

        return {'PASS_THROUGH'}