def visualise_edges(rag, edge_values, edge_direction=2, ignore_edges=None, n_threads=None): """ Visualize values mapped to the edges of a rag as volume. Arguments: rag [nifty.rag] - region adjacency graph edge_values [np.ndarray] - values mapped to rag edges edge_direction [int] - direction into which the edges will be drawn: 0 - drawn in both directions 1 - drawn in negative direction 2 - drawn in positive direction ignore_edges [np.ndarray]: mask or indices of edges that should not be drawn n_threads [int] - number of threads (default: None) Returns: np.ndarray - edge volume """ assert rag.numberOfEdges == len( edge_values), "%i, %i" % (rag.numberOfEdges, len(edge_values)) n_threads = multiprocessing.cpu_count() if n_threads is None else n_threads edge_builder = nrag.ragCoordinates(rag, numberOfThreads=n_threads) if ignore_edges is None: edge_values_ = edge_values else: edge_values_ = edge_values.copy() edge_values_[ignore_edges] = 0 edge_vol = edge_builder.edgesToVolume(edge_values_, edgeDirection=edge_direction) return edge_vol
def visualize_probabilities_for_subvolume(sub_ws, probs, uv_ids, edge_direction=2): rag = nrag.gridRag(sub_ws, numberOfLabels=int(sub_ws.max()) + 1) edge_builder = nrag.ragCoordinates(rag) edge_map_att = np.zeros(rag.numberOfEdges, dtype='uint32') edge_map_rep = np.zeros(rag.numberOfEdges, dtype='uint32') rag_uvs = rag.uvIds() indices = find_matching_row_indices(rag_uvs, uv_ids)[:, 0] sub_probs = probs[indices] # build attractive edges edge_map_att[sub_probs <= .1] = 3 edge_map_att[np.logical_and(sub_probs > .1, sub_probs <= .3)] = 2 edge_map_att[np.logical_and(sub_probs > .3, sub_probs <= .5)] = 1 edge_vol_att = edge_builder.edgesToVolume(edge_map_att, edgeDirection=edge_direction) # build repulsive edges edge_map_rep[np.logical_and(sub_probs > .5, sub_probs <= .7)] = 1 edge_map_rep[np.logical_and(sub_probs > .7, sub_probs <= .9)] = 2 edge_map_rep[sub_probs > .9] = 3 edge_vol_rep = edge_builder.edgesToVolume(edge_map_rep, edgeDirection=edge_direction) # build edge ids edge_ids = np.arange(rag.numberOfEdges).astype('uint32') edge_id_vol = edge_builder.edgesToVolume(edge_ids, edgeDirection=edge_direction) return edge_id_vol, edge_vol_att, edge_vol_rep
def visualize_probabilities(rag, probs, edge_direction=2, ignore_edges=None): assert rag.numberOfEdges == len(probs), "%i, %i" % (rag.numberOfEdges, len(probs)) edge_builder = nrag.ragCoordinates(rag) edge_map_att = np.zeros_like(probs, dtype='uint32') edge_map_rep = np.zeros_like(probs, dtype='uint32') # build attractive edges edge_map_att[probs <= .1] = 3 edge_map_att[np.logical_and(probs > .1, probs <= .3)] = 2 edge_map_att[np.logical_and(probs > .3, probs <= .5)] = 1 if ignore_edges is not None: edge_map_att[ignore_edges] = 0 edge_vol_att = edge_builder.edgesToVolume(edge_map_att, edgeDirection=edge_direction) # build repulsive edges edge_map_rep[np.logical_and(probs > .5, probs <= .7)] = 1 edge_map_rep[np.logical_and(probs > .7, probs <= .9)] = 2 edge_map_rep[probs > .9] = 3 if ignore_edges is not None: edge_map_rep[ignore_edges] = 0 edge_vol_rep = edge_builder.edgesToVolume(edge_map_rep, edgeDirection=edge_direction) # build edge ids edge_ids = np.arange(rag.numberOfEdges).astype('uint32') edge_id_vol = edge_builder.edgesToVolume(edge_ids, edgeDirection=edge_direction) return edge_id_vol, edge_vol_att, edge_vol_rep
def __init__(self, base_segmentation, n_threads=8): assert isinstance(base_segmentation, np.ndarray) self.base_segmentation = base_segmentation self.n_threads = n_threads self.rag = nrag.gridRag(self.base_segmentation, numberOfLabels=int(base_segmentation.max()) + 1, numberOfThreads=self.n_threads) self.uv_ids = self.rag.uvIds() self.volume_builder = nrag.ragCoordinates(self.rag, self.n_threads)
def visualize_costs(rag, costs, edge_direction=2): assert rag.numberOfEdges == len(costs), "%i, %i" % (rag.numberOfEdges, len(costs)) edge_builder = nrag.ragCoordinates(rag) edge_map_att = np.zeros_like(costs, dtype='uint32') edge_map_rep = np.zeros_like(costs, dtype='uint32') # build attractive edges edge_map_att[costs > 0] = 1 edge_vol_att = edge_builder.edgesToVolume(edge_map_att, edgeDirection=edge_direction) # build repulsive edges edge_map_rep[costs < 0] = 1 edge_vol_rep = edge_builder.edgesToVolume(edge_map_rep, edgeDirection=edge_direction) # build edge ids edge_ids = np.arange(rag.numberOfEdges).astype('uint32') edge_id_vol = edge_builder.edgesToVolume(edge_ids, edgeDirection=edge_direction) return edge_id_vol, edge_vol_att, edge_vol_rep
def get_contact_sites(seg, n_labels, labels_of_interest, n_threads): # compute the region adjacency graph and filter for the edges # that connect neurites of interest rag = nrag.gridRag(seg, numberOfLabels=n_labels, numberOfThreads=n_threads) uv_ids = rag.uvIds() # find the edges that connect neurites of interest edge_mask = np.in1d(uv_ids, labels_of_interest).reshape(uv_ids.shape).all(axis=1) pairs = uv_ids[edge_mask] n_pairs = len(pairs) edge_labels = np.zeros_like(edge_mask, dtype='uint32') edge_labels[edge_mask] = np.arange(1, n_pairs + 1).astype('uint32') # build the edge volume with the edges of interest edge_builder = nrag.ragCoordinates(rag, numberOfThreads=n_threads) edge_vol = edge_builder.edgesToVolume(edge_labels) # run connected components and for each connected component # get the association to the connected neurites edge_vol_cc = vigra.analysis.labelVolumeWithBackground(edge_vol) return edge_vol, edge_vol_cc, pairs
def get_edge_costs(rag, probs): assert rag.numberOfEdges == len(probs), "%i, %i" % (rag.numberOfEdges, len(probs)) edge_builder = nrag.ragCoordinates(rag) edge_map_att = np.zeros_like(probs, dtype='uint32') edge_map_rep = np.zeros_like(probs, dtype='uint32') # build attractive edges edge_map_att[probs <= .1] = 3 edge_map_att[np.logical_and(probs > .1, probs <= .3)] = 2 edge_map_att[np.logical_and(probs > .3, probs <= .5)] = 1 edge_vol_att = edge_builder.edgesToVolume(edge_map_att, edgeDirection=2) # build repulsive edges edge_map_rep[np.logical_and(probs > .5, probs <= .7)] = 1 edge_map_rep[np.logical_and(probs > .7, probs <= .9)] = 2 edge_map_rep[probs > .9] = 3 edge_vol_rep = edge_builder.edgesToVolume(edge_map_rep, edgeDirection=2) # build edge ids edge_ids = np.arange(rag.numberOfEdges).astype('uint32') edge_id_vol = edge_builder.edgesToVolume(edge_ids, edgeDirection=2) return edge_id_vol, edge_vol_att, edge_vol_rep
def visualise_attractive_and_repulsive_edges(rag, edge_values, threshold, large_values_are_attractive=True, edge_direction=2, ignore_edges=None, n_threads=None): """ Visualize values mapped to the edges of a rag that are attractive and repulsive. Arguments: rag [nifty.rag] - region adjacency graph edge_values [np.ndarray] - values mapped to rag edges threshold [float] - values below this threhold are repulsive, above repulsive large_values_are_attractive [bool] - are large values or small values attractive? (default: True) edge_direction [int] - direction into which the edges will be drawn: (default: 2) 0 - drawn in both directions 1 - drawn in negative direction 2 - drawn in positive direction ignore_edges [np.ndarray]: mask or indices of edges that should not be drawn n_threads [int] - number of threads (default: None) Returns: np.ndarray - volume of attractive edges np.ndarray - volume of repulsive edges """ assert rag.numberOfEdges == len( edge_values), "%i, %i" % (rag.numberOfEdges, len(edge_values)) n_threads = multiprocessing.cpu_count() if n_threads is None else n_threads edge_builder = nrag.ragCoordinates(rag, numberOfThreads=n_threads) if ignore_edges is None: edge_values_ = edge_values else: edge_values_ = edge_values.copy() edge_values_[ignore_edges] = threshold # find and normalize the attractive edge values attractive_edge_values = np.zeros_like(edge_values) if large_values_are_attractive: attractive_edges = edge_values_ > threshold attractive_edge_values[attractive_edges] = _scale_values( edge_values_[attractive_edges], threshold, False) else: attractive_edges = edge_values_ < threshold attractive_edge_values[attractive_edges] = _scale_values( edge_values_[attractive_edges], threshold, True) edge_vol_attractive = edge_builder.edgesToVolume( attractive_edge_values, edgeDirection=edge_direction) # find and normalize the repulsive edge values repulsive_edge_values = np.zeros_like(edge_values) if large_values_are_attractive: repulsive_edges = edge_values_ < threshold repulsive_edge_values[repulsive_edges] = _scale_values( edge_values_[repulsive_edges], threshold, True) else: repulsive_edges = edge_values_ > threshold repulsive_edge_values[repulsive_edges] = _scale_values( edge_values_[repulsive_edges], threshold, False) edge_vol_repulsive = edge_builder.edgesToVolume( repulsive_edge_values, edgeDirection=edge_direction) return edge_vol_attractive, edge_vol_repulsive