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)
def color_contours(specific_labels=[], specific_hemi='both', labels_contours=None, cumulate=False, change_colorbar=False, specific_colors=None, atlas='', move_cursor=True): if _addon() is None: return if isinstance(specific_labels, str): specific_labels = [specific_labels] if atlas != '' and atlas != bpy.context.scene.contours_coloring and atlas in LabelsPanel.existing_contoures: bpy.context.scene.contours_coloring = atlas if atlas == '' and bpy.context.scene.atlas in LabelsPanel.existing_contoures: bpy.context.scene.contours_coloring = bpy.context.scene.atlas if labels_contours is None: labels_contours = LabelsPanel.labels_contours contour_max = max([labels_contours[hemi]['max'] for hemi in mu.HEMIS]) if not _addon().colorbar_values_are_locked() and change_colorbar: _addon().set_colormap('jet') _addon().set_colorbar_title('{} labels contours'.format(bpy.context.scene.contours_coloring)) _addon().set_colorbar_max_min(contour_max, 1) _addon().set_colorbar_prec(0) _addon().show_activity() specific_label_ind = 0 if specific_colors is not None: specific_colors = np.tile(specific_colors, (len(specific_labels), 1)) for hemi in mu.HEMIS: contours = labels_contours[hemi]['contours'] if specific_hemi != 'both' and hemi != specific_hemi: selected_contours = np.zeros(contours.shape) if specific_colors is None else np.zeros((contours.shape[0], 4)) elif len(specific_labels) > 0: selected_contours = np.zeros(contours.shape) if specific_colors is None else np.zeros((contours.shape[0], 4)) for specific_label in specific_labels: if mu.get_hemi_from_fname(specific_label) != hemi: continue label_ind = np.where(np.array(labels_contours[hemi]['labels']) == specific_label) if len(label_ind) > 0 and len(label_ind[0]) > 0: label_ind = label_ind[0][0] selected_contours[np.where(contours == label_ind + 1)] = \ label_ind + 1 if specific_colors is None else [1, *specific_colors[specific_label_ind]] specific_label_ind += 1 if move_cursor and len(specific_labels) == 1 and 'centers' in labels_contours[hemi]: vert = labels_contours[hemi]['centers'][label_ind] _addon().move_cursor_according_to_vert(vert, 'inflated_{}'.format(hemi)) _addon().set_closest_vertex_and_mesh_to_cursor(vert, 'inflated_{}'.format(hemi)) _addon().create_slices() else: print("Can't find {} in the labels contours!".format(specific_label)) else: selected_contours = labels_contours[hemi]['contours'] mesh = mu.get_hemi_obj(hemi).data mesh.vertex_colors.active_index = mesh.vertex_colors.keys().index('contours') mesh.vertex_colors['contours'].active_render = True _addon().color_hemi_data(hemi, selected_contours, 0.1, 256 / contour_max, override_current_mat=not cumulate, coloring_layer='contours', check_valid_verts=False) _addon().what_is_colored().add(_addon().WIC_CONTOURS)
def color_contours(specific_labels=[], specific_hemi='both', labels_contours=None, cumulate=False, change_colorbar=False, specific_colors=None, atlas='', move_cursor=True, filter=''): if _addon() is None: return if filter != '': bpy.context.scene.labels_contours_filter = filter if isinstance(LabelsPanel.labels['rh'], np.ndarray): specific_labels = LabelsPanel.labels['rh'].tolist() + LabelsPanel.labels['lh'].tolist() else: specific_labels = LabelsPanel.labels['rh'] + LabelsPanel.labels['lh'] elif isinstance(specific_labels, str): specific_labels = [specific_labels] if atlas != '' and atlas != bpy.context.scene.contours_coloring and atlas in LabelsPanel.existing_contoures: bpy.context.scene.contours_coloring = atlas if atlas == '' and bpy.context.scene.atlas in LabelsPanel.existing_contoures: bpy.context.scene.contours_coloring = bpy.context.scene.atlas if labels_contours is None: labels_contours = LabelsPanel.labels_contours contour_max = max([labels_contours[hemi]['max'] for hemi in mu.HEMIS]) if contour_max == 0: print('No contours!') return False if not _addon().colorbar_values_are_locked() and change_colorbar: _addon().set_colormap('jet') _addon().set_colorbar_title('{} labels contours'.format(bpy.context.scene.contours_coloring)) _addon().set_colorbar_max_min(contour_max, 1) _addon().set_colorbar_prec(0) _addon().show_activity() specific_label_ind = 0 if specific_colors is not None: specific_colors = np.tile(specific_colors, (len(specific_labels), 1)) for hemi in mu.HEMIS: contours = labels_contours[hemi]['contours'] if specific_hemi != 'both' and hemi != specific_hemi: selected_contours = np.zeros(contours.shape) if specific_colors is None else np.zeros((contours.shape[0], 4)) elif len(specific_labels) > 0: selected_contours = np.zeros(contours.shape) if specific_colors is None else np.zeros((contours.shape[0], 4)) for specific_label in specific_labels: if mu.get_hemi_from_fname(specific_label) != hemi: continue label_ind = np.where(np.array(labels_contours[hemi]['labels']) == specific_label) if len(label_ind) > 0 and len(label_ind[0]) > 0: label_ind = label_ind[0][0] selected_contours[np.where(contours == label_ind + 1)] = \ label_ind + 1 if specific_colors is None else [1, *specific_colors[specific_label_ind]] specific_label_ind += 1 if move_cursor and len(specific_labels) == 1 and 'centers' in labels_contours[hemi]: vert = labels_contours[hemi]['centers'][label_ind] _addon().move_cursor_according_to_vert(vert, 'inflated_{}'.format(hemi)) _addon().set_closest_vertex_and_mesh_to_cursor(vert, 'inflated_{}'.format(hemi)) _addon().create_slices() _addon().snap_cursor(True) _addon().set_tkreg_ras(bpy.context.scene.cursor_location * 10, False) else: print("Can't find {} in the labels contours!".format(specific_label)) else: selected_contours = labels_contours[hemi]['contours'] mesh = mu.get_hemi_obj(hemi).data mesh.vertex_colors.active_index = mesh.vertex_colors.keys().index('contours') mesh.vertex_colors['contours'].active_render = True # Saving the labels names and RGB values unique_values = np.unique(contours) contours_colors = _addon().coloring.calc_colors(unique_values, 0, 256 / contour_max) contours_labels_info = np.column_stack((labels_contours[hemi]['labels'], contours_colors[1:, :])) output_fname = op.join(mu.get_user_fol(), 'labels', '{}_{}_{}_labels_names_and_colors.csv'.format( bpy.context.scene.contours_coloring, hemi, _addon().colorbar.get_colormap_name())) np.savetxt(output_fname, contours_labels_info, ('%s', '%s', '%s', '%s'), ',') print('Saving labels names and RGB values to {}'.format(output_fname)) # Color the hemi surface _addon().color_hemi_data( hemi, selected_contours, 0.1, 256 / contour_max, override_current_mat=not cumulate, coloring_layer='contours', check_valid_verts=False) _addon().what_is_colored().add(_addon().WIC_CONTOURS)