def test_nodes(): initial_locations = { 1: np.array([1, 1, 1], dtype=np.float32), 2: np.array([500, 500, 500], dtype=np.float32), 3: np.array([550, 550, 550], dtype=np.float32), } replacement_locations = { 1: np.array([0, 0, 0], dtype=np.float32), 2: np.array([50, 50, 50], dtype=np.float32), 3: np.array([55, 55, 55], dtype=np.float32), } nodes = [ Node(id=id, location=location) for id, location in initial_locations.items() ] edges = [Edge(1, 2), Edge(2, 3)] spec = GraphSpec(roi=Roi(Coordinate([-500, -500, -500]), Coordinate([1500, 1500, 1500]))) graph = Graph(nodes, edges, spec) for node in graph.nodes: node.location = replacement_locations[node.id] for node in graph.nodes: assert all(np.isclose(node.location, replacement_locations[node.id]))
def __init__(self): self.dtype = float self.__vertices = [ Node(id=1, location=np.array([1, 1, 1], dtype=self.dtype)), Node(id=2, location=np.array([500, 500, 500], dtype=self.dtype)), Node(id=3, location=np.array([550, 550, 550], dtype=self.dtype)), ] self.__edges = [Edge(1, 2), Edge(2, 3)] self.__spec = GraphSpec(roi=Roi(Coordinate([-500, -500, -500]), Coordinate([1500, 1500, 1500]))) self.graph = Graph(self.__vertices, self.__edges, self.__spec)
def _get_points(self, inside: np.ndarray, slope: np.ndarray, bb: Roi) -> Tuple[Dict[int, Node], List[Tuple[int, int]]]: slope = slope / max(slope) shape = np.array(bb.get_shape()) outside_down = inside - shape * slope outside_up = inside + shape * slope down_intercept = self._resample_relative(inside, outside_down, bb) up_intercept = self._resample_relative(inside, outside_up, bb) points = { # line Node(id=0, location=down_intercept, attrs={ "node_type": 0, "radius": 0 }), Node(id=1, location=up_intercept, attrs={ "node_type": 0, "radius": 0 }), } edges = [Edge(0, 1)] return self._graph_points(points, edges)
def provide(self, request): outputs = Batch() nodes = [ Node(id=0, location=np.array((1, 1, 1))), Node(id=1, location=np.array((10, 10, 10))), Node(id=2, location=np.array((19, 19, 19))), Node(id=3, location=np.array((21, 21, 21))), Node(id=104, location=np.array((30, 30, 30))), Node(id=5, location=np.array((39, 39, 39))), ] edges = [Edge(0, 1), Edge(1, 2), Edge(3, 104), Edge(104, 5)] spec = self.spec[GraphKeys.RAW].copy() spec.roi = request[GraphKeys.RAW].roi graph = Graph(nodes, edges, spec) outputs[GraphKeys.RAW] = graph.crop(spec.roi) return outputs
def setup(self): roi = Roi(Coordinate([0] * len(self.size)), self.size) for points_key in self.points: self.provides(points_key, GraphSpec(roi=roi, directed=self.directed)) k = min(self.size) nodes = [ Node(id=i, location=np.array([i * k / self.num_points] * 3)) for i in range(self.num_points) ] edges = [Edge(i, i + 1) for i in range(self.num_points - 1)] self.graph = Graph(nodes, edges, GraphSpec(roi=roi, directed=self.directed))
def edges(self): return [Edge(0, 1), Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 0)]
def process(self, batch, request): outputs = Batch() raw_base_spec = batch[self.raw_base].spec.copy() # Get base arrays raw_base_array = batch[self.raw_base].data labels_base_array = batch[self.labels_base].data # Get add arrays raw_add_array = batch[self.raw_add].data labels_add_array = batch[self.labels_add].data if self.scale_add_volume: raw_base_median = np.median(raw_base_array) raw_add_median = np.median(raw_add_array) diff = raw_base_median - raw_add_median raw_add_array = raw_add_array + diff # fuse labels fused_labels_array = self._relabel(labels_base_array) next_label_id = np.max(fused_labels_array) + 1 add_mask = np.zeros_like(fused_labels_array, dtype=bool) for label in np.unique(labels_add_array): if label == 0: continue label_mask = labels_add_array == label # handle overlap overlap = np.logical_and(fused_labels_array, label_mask) fused_labels_array[overlap] = -1 # assign new label add_mask[label_mask] = True fused_labels_array[label_mask] = next_label_id next_label_id += 1 # fuse raw if self.blend_mode == "intensity": add_mask = raw_add_array.astype(np.float32) / np.max(raw_add_array) raw_fused_array = add_mask * raw_add_array + (1 - add_mask) * raw_base_array elif self.blend_mode == "add": raw_fused_array = 0.5*raw_add_array / np.max( raw_add_array ) + 0.5*raw_base_array / np.max(raw_base_array) raw_fused_array = np.clip(raw_fused_array, 0, 1) elif self.blend_mode == "labels_mask": soft_mask = np.zeros_like(add_mask, dtype="float32") ndimage.gaussian_filter( add_mask.astype("float32"), sigma=self.blend_smoothness / np.array(raw_base_spec.voxel_size), output=soft_mask, mode=self.gaussian_smooth_mode, ) soft_mask /= np.clip(np.max(soft_mask), 1e-5, float("inf")) soft_mask = np.clip((soft_mask * 2), 0, 1) if self.soft_mask is not None: outputs.arrays[self.soft_mask] = Array( soft_mask, spec=ArraySpec( roi=raw_base_spec.roi, voxel_size=raw_base_spec.voxel_size ), ) if self.masked_base is not None: outputs.arrays[self.masked_base] = Array( raw_base_array * (soft_mask > 0.25), spec=raw_base_spec.copy() ) if self.masked_add is not None: outputs.arrays[self.masked_add] = Array( raw_add_array * soft_mask, spec=ArraySpec( roi=raw_base_spec.roi, voxel_size=raw_base_spec.voxel_size ), ) if self.mask_maxed is not None: outputs.arrays[self.mask_maxed] = Array( np.maximum( raw_base_array * (soft_mask > 0.25), raw_add_array * soft_mask ), spec=ArraySpec( roi=raw_base_spec.roi, voxel_size=raw_base_spec.voxel_size ), ) raw_fused_array = np.maximum(soft_mask * raw_add_array, raw_base_array) raw_fused_array = np.clip(raw_fused_array, 0, 1) else: raise NotImplementedError("Unknown blend mode %s." % self.blend_mode) # load specs labels_add_spec = batch[self.labels_add].spec.copy() raw_base_spec = batch[self.raw_base].spec.copy() raw_dtype = batch[self.raw_base].data.dtype raw_base_spec.dtype = raw_dtype # return raw and labels for "fused" volume # raw_fused_array.astype(raw_base_spec.dtype) outputs.arrays[self.raw_fused] = Array( data=raw_fused_array.astype(raw_base_spec.dtype), spec=raw_base_spec ) outputs.arrays[self.labels_fused] = Array( data=fused_labels_array, spec=labels_add_spec ) # fuse points: if self.points_fused in request: node_ids = [node.id for node in batch.graphs[self.points_base].nodes] num_nodes = len(node_ids) offset = 0 if num_nodes == 0 else max(node_ids) + 1 fused_graph = batch.graphs[self.points_base].copy() for node in batch.graphs[self.points_add].nodes: attrs = deepcopy(node.all) attrs["id"] += offset fused_graph.add_node(Node.from_attrs(attrs)) for edge in batch.graphs[self.points_add].edges: edge = Edge(edge.u + offset, edge.v + offset) fused_graph.add_edge(edge) outputs.graphs[self.points_fused] = fused_graph return outputs
def _toy_swc_points(self): """ shape: ----------- | | |---------- | | ----------- """ arr = np.array points = [ # backbone Node(id=0, location=arr([0, 0, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=1, location=arr([1, 0, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=2, location=arr([2, 0, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=3, location=arr([3, 0, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=4, location=arr([4, 0, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=5, location=arr([5, 0, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=6, location=arr([6, 0, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=7, location=arr([7, 0, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=8, location=arr([8, 0, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=9, location=arr([9, 0, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=10, location=arr([10, 0, 5]), attrs={ "radius": 0, "node_type": 0 }), # bottom line Node(id=11, location=arr([0, 1, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=12, location=arr([0, 2, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=13, location=arr([0, 3, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=14, location=arr([0, 4, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=15, location=arr([0, 5, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=16, location=arr([0, 6, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=17, location=arr([0, 7, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=18, location=arr([0, 8, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=19, location=arr([0, 9, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=20, location=arr([0, 10, 5]), attrs={ "radius": 0, "node_type": 0 }), # mid line Node(id=21, location=arr([5, 1, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=22, location=arr([5, 2, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=23, location=arr([5, 3, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=24, location=arr([5, 4, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=25, location=arr([5, 5, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=26, location=arr([5, 6, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=27, location=arr([5, 7, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=28, location=arr([5, 8, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=29, location=arr([5, 9, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=30, location=arr([5, 10, 5]), attrs={ "radius": 0, "node_type": 0 }), # top line Node(id=31, location=arr([10, 1, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=32, location=arr([10, 2, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=33, location=arr([10, 3, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=34, location=arr([10, 4, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=35, location=arr([10, 5, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=36, location=arr([10, 6, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=37, location=arr([10, 7, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=38, location=arr([10, 8, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=39, location=arr([10, 9, 5]), attrs={ "radius": 0, "node_type": 0 }), Node(id=40, location=arr([10, 10, 5]), attrs={ "radius": 0, "node_type": 0 }), ] edges = [ Edge(0, 0), Edge(0, 1), Edge(1, 2), Edge(2, 3), Edge(3, 4), Edge(4, 5), Edge(5, 6), Edge(6, 7), Edge(7, 8), Edge(8, 9), Edge(9, 10), Edge(0, 11), Edge(11, 12), Edge(12, 13), Edge(13, 14), Edge(14, 15), Edge(15, 16), Edge(16, 17), Edge(17, 18), Edge(18, 19), Edge(19, 20), Edge(5, 21), Edge(21, 22), Edge(22, 23), Edge(23, 24), Edge(24, 25), Edge(25, 26), Edge(26, 27), Edge(27, 28), Edge(28, 29), Edge(29, 30), Edge(10, 31), Edge(31, 32), Edge(32, 33), Edge(33, 34), Edge(34, 35), Edge(35, 36), Edge(36, 37), Edge(37, 38), Edge(38, 39), Edge(39, 40), ] return Graph( points, edges, GraphSpec( roi=Roi(Coordinate((-100, -100, -100)), Coordinate((300, 300, 300))), directed=True, ), )