def clear(self): """Clear the edit annotation from the UI""" with self.reference_viewer.txn() as txn: txn.layers[self.EDIT] = neuroglancer.PointAnnotationLayer( annotation_color=self.EDIT_ANNOTATION_COLOR) with self.moving_viewer.txn() as txn: txn.layers[self.EDIT] = neuroglancer.PointAnnotationLayer( annotation_color=self.EDIT_ANNOTATION_COLOR)
def update_after_edit(self): for viewer, points in ((self.reference_viewer, self.annotation_reference_pts), (self.moving_viewer, self.annotation_moving_pts)): with viewer.txn() as txn: layer = txn.layers[self.CORRESPONDENCE_POINTS] txn.layers[self.CORRESPONDENCE_POINTS] = \ neuroglancer.PointAnnotationLayer(points=points) txn.layers[self.EDIT] = neuroglancer.PointAnnotationLayer( annotation_color=self.EDIT_ANNOTATION_COLOR)
def edit_point(self, idx, add_to_undo=True): reference, moving = self.remove_point(idx, add_to_undo) with self.reference_viewer.txn() as txn: txn.layers[self.EDIT] = neuroglancer.PointAnnotationLayer( points=[reference[::-1]], annotation_color=self.EDIT_ANNOTATION_COLOR) txn.position.voxel_coordinates = reference[::-1] with self.moving_viewer.txn() as txn: txn.layers[self.EDIT] = neuroglancer.PointAnnotationLayer( points=[moving[::-1]], annotation_color=self.EDIT_ANNOTATION_COLOR) txn.position.voxel_coordinates = moving[::-1]
def refresh(self): with self.viewer.txn() as s: s.voxel_size = self.voxel_size * 1000 s.layers["image"] = neuroglancer.ImageLayer( source=self.url, shader=cubehelix_shader % self.brightness) s.layers["points"] = neuroglancer.PointAnnotationLayer( points=self.points)
def on_reference_annotate(self, s): """Handle an edit in the reference viewer :param s: the current state """ point = s.mouse_voxel_coordinates reference_points = np.array(self.reference_pts) if len(reference_points) > 0: distances = np.sqrt( np.sum(np.square(reference_points - point[np.newaxis, ::-1]), 1)) if np.min(distances) < self.min_distance: self.post_message( self.reference_viewer, self.EDIT, "Point at %d %d %d is too close to some other point" % tuple(point.tolist())) return msg = "Edit point: %d %d %d" % tuple(point.tolist()) self.post_message(self.reference_viewer, self.EDIT, msg) with self.reference_viewer.txn() as txn: layer = neuroglancer.PointAnnotationLayer( points=[point.tolist()], annotation_color=self.EDIT_ANNOTATION_COLOR) txn.layers[self.EDIT] = layer
def make_initial_state(self, segment_id, base_state): state = copy.deepcopy(base_state) segments = self.get_state_segment_ids(state) segments.clear() segments.add(segment_id) state.layers[self.point_annotation_layer_name] = neuroglancer.PointAnnotationLayer() return state
def on_translate(self, s): """Translate the editing coordinate in the reference frame to moving""" rp = self.get_reference_edit_point() if self.warper != None and rp: mp = self.warper(np.atleast_2d(rp))[0][::-1] with self.moving_viewer.txn() as txn: txn.layers[self.EDIT] = neuroglancer.PointAnnotationLayer( points=[mp], annotation_color=self.EDIT_ANNOTATION_COLOR) txn.position.voxel_coordinates = mp
def on_moving_annotate(self, s): """Handle an edit in the moving viewer :param s: the current state """ point = s.mouse_voxel_coordinates msg = "Edit point: %d %d %d" % tuple(point.tolist()) self.post_message(self.moving_viewer, self.EDIT, msg) with self.moving_viewer.txn() as txn: layer = neuroglancer.PointAnnotationLayer( points=[point.tolist()], annotation_color=self.EDIT_ANNOTATION_COLOR) txn.layers[self.EDIT] = layer
def plot_df_points(s, df, layer_name='cell_pts', xyzcol=('x_pos', 'y_pos', 'z_pos'), desc_col='ID'): '''function for adding a dataframe of cell positions to the neuroglancer viewer Parameters ---------- s: neuroglancer.ViewerState neuroglancer state from viewer.txn() df: pd.DataFrame pandas DataFrame that contains columns (ID,x_pos,y_pos,z_pos) where ID Is the name of the cell, and (x,y,z)_pos are the position of the cells in the global voxel coordinate system of the viewer (usually microns). layer_name: str name of layer to put these points on (default='cell_pts') xyzcol: tuple x,y,z column names in data frame default=('x_pos','y_pos','z_pos') desc_col: str column name for description in data frame default='ID' Returns ------- None ''' layer_name s.layers.append( name=layer_name, layer=neuroglancer.PointAnnotationLayer(), ) cds = [] for k, row in df.iterrows(): cd = OrderedDict({ 'point': [row[xyzcol[0]], row[xyzcol[1]], row[xyzcol[2]]], 'type': 'point', 'id': row.get(desc_col, ''), 'description': row.get(desc_col, '') }) cds.append(cd) d = {'type': 'annotation', 'annotations': cds} s.layers[layer_name].layer._json_data.update(d)
def display_split_result(graph, agglo_id, cur_eqs, supervoxel_map, split_seeds, image_url, segmentation_url): agglo_members = set(graph.get_agglo_members(agglo_id)) state = neuroglancer.ViewerState() state.layers.append(name='image', layer=neuroglancer.ImageLayer(source=image_url)) state.layers.append( name='original', layer=neuroglancer.SegmentationLayer( source=segmentation_url, segments=agglo_members, ), visible=False, ) state.layers.append( name='isolated-supervoxels', layer=neuroglancer.SegmentationLayer( source=segmentation_url, segments=set(x for x, seeds in six.viewitems(supervoxel_map) if len(seeds) > 1), ), visible=False, ) state.layers.append(name='split', layer=neuroglancer.SegmentationLayer( source=segmentation_url, equivalences=cur_eqs, segments=set(cur_eqs[x] for x in agglo_members), )) for label, component in six.viewitems(split_seeds): state.layers.append( name='seed%d' % label, layer=neuroglancer.PointAnnotationLayer( points=[seed['position'] for seed in component], ), ) state.show_slices = False state.layout = '3d' all_seed_points = [ seed['position'] for component in six.viewvalues(split_seeds) for seed in component ] state.voxel_coordinates = np.mean(all_seed_points, axis=0) state.perspective_zoom = 140 return state
def pointlayer(txn, name, x, y, z, color): """Add a point layer :param txn: the neuroglancer viewer transaction context :param name: the displayable name of the point layer :param x: the x coordinate per point :param y: the y coordinate per point :param z: the z coordinate per point :param color: the color of the points in the layer, e.g. "red", "yellow" """ txn.layers[name] = neuroglancer.PointAnnotationLayer( points=np.column_stack((x, y, z)), annotation_color=color)