Exemple #1
0
    def test_output(self):

        cropped_roi_raw = Roi((400, 40, 40), (1000, 100, 100))
        cropped_roi_presyn = Roi((800, 80, 80), (800, 80, 80))

        GraphKey("PRESYN")

        pipeline = (
            ExampleSourceCrop()
            + Crop(ArrayKeys.RAW, cropped_roi_raw)
            + Crop(GraphKeys.PRESYN, cropped_roi_presyn)
        )

        with build(pipeline):

            self.assertTrue(pipeline.spec[ArrayKeys.RAW].roi == cropped_roi_raw)
            self.assertTrue(pipeline.spec[GraphKeys.PRESYN].roi == cropped_roi_presyn)

        pipeline = ExampleSourceCrop() + Crop(
            ArrayKeys.RAW,
            fraction_negative=(0.25, 0, 0),
            fraction_positive=(0.25, 0, 0),
        )
        expected_roi_raw = Roi((650, 20, 20), (900, 180, 180))

        with build(pipeline):

            logger.info(pipeline.spec[ArrayKeys.RAW].roi)
            logger.info(expected_roi_raw)
            self.assertTrue(pipeline.spec[ArrayKeys.RAW].roi == expected_roi_raw)
Exemple #2
0
    def __init__(self):

        self.voxel_size = Coordinate((40, 4, 4))

        self.nodes = [
            # corners
            Node(id=1, location=np.array((-200, -200, -200))),
            Node(id=2, location=np.array((-200, -200, 199))),
            Node(id=3, location=np.array((-200, 199, -200))),
            Node(id=4, location=np.array((-200, 199, 199))),
            Node(id=5, location=np.array((199, -200, -200))),
            Node(id=6, location=np.array((199, -200, 199))),
            Node(id=7, location=np.array((199, 199, -200))),
            Node(id=8, location=np.array((199, 199, 199))),
            # center
            Node(id=9, location=np.array((0, 0, 0))),
            Node(id=10, location=np.array((-1, -1, -1))),
        ]

        self.graph_spec = GraphSpec(roi=Roi((-100, -100, -100), (300, 300, 300)))
        self.array_spec = ArraySpec(
                roi=Roi((-200, -200, -200), (400, 400, 400)), voxel_size=self.voxel_size
            )

        self.graph = Graph(self.nodes, [], self.graph_spec)
Exemple #3
0
    def test_output(self):
        a = ArrayKey("A")
        b = ArrayKey("B")
        source_a = TestSourceRandomLocation(a)
        source_b = TestSourceRandomLocation(b)

        pipeline = (source_a, source_b) + \
            MergeProvider() + CustomRandomLocation()

        with build(pipeline):

            for i in range(10):
                batch = pipeline.request_batch(
                    BatchRequest({
                        a: ArraySpec(roi=Roi((0, 0, 0), (20, 20, 20))),
                        b: ArraySpec(roi=Roi((0, 0, 0), (20, 20, 20)))
                    }))

                self.assertTrue(np.sum(batch.arrays[a].data) > 0)
                self.assertTrue(np.sum(batch.arrays[b].data) > 0)

                # Request a ROI with the same shape as the entire ROI
                full_roi_a = Roi((0, 0, 0), source_a.roi.get_shape())
                full_roi_b = Roi((0, 0, 0), source_b.roi.get_shape())
                batch = pipeline.request_batch(
                    BatchRequest({
                        a: ArraySpec(roi=full_roi_a),
                        b: ArraySpec(roi=full_roi_b)
                    }))
Exemple #4
0
    def test_precache(self):

        logging.getLogger("gunpowder.torch.nodes.predict").setLevel(
            logging.INFO)

        a = ArrayKey("A")
        pred = ArrayKey("PRED")

        model = ExampleModel()

        reference_request = BatchRequest()
        reference_request[a] = ArraySpec(roi=Roi((0, 0), (7, 7)))
        reference_request[pred] = ArraySpec(roi=Roi((1, 1), (5, 5)))

        source = ExampleTorchTrain2DSource()
        predict = Predict(
            model=model,
            inputs={"a": a},
            outputs={0: pred},
            array_specs={pred: ArraySpec()},
        )
        pipeline = source + predict + PreCache(cache_size=3, num_workers=2)

        request = BatchRequest({
            a: ArraySpec(roi=Roi((0, 0), (17, 17))),
            pred: ArraySpec(roi=Roi((0, 0), (15, 15))),
        })

        # train for a couple of iterations
        with build(pipeline):

            batch = pipeline.request_batch(request)
            assert pred in batch
Exemple #5
0
    def test_shift_points5(self):
        data = [
            Node(id=0, location=np.array([3, 0])),
            Node(id=1, location=np.array([3, 2])),
            Node(id=2, location=np.array([3, 4])),
            Node(id=3, location=np.array([3, 6])),
            Node(id=4, location=np.array([3, 8])),
        ]
        spec = GraphSpec(Roi(offset=(0, 0), shape=(15, 10)))
        points = Graph(data, [], spec)
        request_roi = Roi(offset=(3, 0), shape=(9, 10))
        shift_array = np.array([[3, 0], [-3, 0], [0, 0], [-3, 0], [3, 0]],
                               dtype=int)

        lcm_voxel_size = Coordinate((3, 2))
        shifted_data = [
            Node(id=0, location=np.array([6, 0])),
            Node(id=2, location=np.array([3, 4])),
            Node(id=4, location=np.array([6, 8])),
        ]
        result = ShiftAugment.shift_points(
            points,
            request_roi,
            shift_array,
            shift_axis=1,
            lcm_voxel_size=lcm_voxel_size,
        )
        # print("test 4", result.data, shifted_data)
        self.assertTrue(self.points_equal(result.nodes, shifted_data))
        self.assertTrue(result.spec == GraphSpec(request_roi))
Exemple #6
0
def test_6_neighborhood():
    # array keys
    graph = GraphKey("GRAPH")
    neighborhood = ArrayKey("NEIGHBORHOOD")
    neighborhood_mask = ArrayKey("NEIGHBORHOOD_MASK")

    distance = 1

    pipeline = TestSource(graph) + Neighborhood(
        graph,
        neighborhood,
        neighborhood_mask,
        distance,
        array_specs={
            neighborhood: ArraySpec(voxel_size=Coordinate((1, 1, 1))),
            neighborhood_mask: ArraySpec(voxel_size=Coordinate((1, 1, 1))),
        },
        k=6,
    )

    request = BatchRequest()
    request[neighborhood] = ArraySpec(roi=Roi((0, 0, 0), (10, 10, 10)))
    request[neighborhood_mask] = ArraySpec(roi=Roi((0, 0, 0), (10, 10, 10)))

    with build(pipeline):
        batch = pipeline.request_batch(request)
        n_data = batch[neighborhood].data
        n_mask = batch[neighborhood_mask].data
        masked_ind = list(
            set([(0, i, 0) for i in range(10) if i not in [0, 4]] +
                [(i, 5, 0)
                 for i in range(10)] + [(i, 4, 0)
                                        for i in range(10) if i not in [0]]))
        assert all(n_mask[tuple(zip(*masked_ind))]
                   ), f"expected {masked_ind} but saw {np.where(n_mask==1)}"
    def test_voxel_size(self):

        locations = [[0, 0, 0], [91, 20, 20], [42, 24, 57]]

        pipeline = (
            ExampleSourceSpecifiedLocation(roi=Roi((0, 0, 0), (100, 100, 100)),
                                           voxel_size=(5, 2, 2)) +
            SpecifiedLocation(
                locations, choose_randomly=False, extra_data=None,
                jitter=None))

        with build(pipeline):

            batch = pipeline.request_batch(
                BatchRequest({
                    ArrayKeys.RAW:
                    ArraySpec(roi=Roi((0, 0, 0), (20, 20, 20)))
                }))
            # first locations is skipped
            # second should start at [80/5, 10/2, 10/2] = [16, 5, 5]
            self.assertEqual(batch.arrays[ArrayKeys.RAW].data[0, 0, 0], 40255)

            batch = pipeline.request_batch(
                BatchRequest({
                    ArrayKeys.RAW:
                    ArraySpec(roi=Roi((0, 0, 0), (20, 20, 20)))
                }))
            # third should start at [30/5, 14/2, 48/2] = [6, 7, 23]
            self.assertEqual(batch.arrays[ArrayKeys.RAW].data[0, 0, 0], 15374)
    def test_mirror(self):
        test_graph = GraphKey("TEST_GRAPH")

        pipeline = TestSource() + SimpleAugment(
            mirror_only=[0, 1, 2], transpose_only=[]
        )

        request = BatchRequest()
        request[GraphKeys.TEST_GRAPH] = GraphSpec(roi=Roi((0, 20, 33), (100, 100, 120)))
        possible_loc = [[50, 49], [70, 29], [100, 86]]
        with build(pipeline):
            seen_mirrored = False
            for i in range(100):
                batch = pipeline.request_batch(request)

                assert len(list(batch[GraphKeys.TEST_GRAPH].nodes)) == 1
                node = list(batch[GraphKeys.TEST_GRAPH].nodes)[0]
                logging.debug(node.location)
                assert all(
                    [
                        node.location[dim] in possible_loc[dim] 
                        for dim in range(3)
                    ]
                )
                seen_mirrored = seen_mirrored or any(
                    [node.location[dim] == possible_loc[dim][1] for dim in range(3)]
                )
                assert Roi((0, 20, 33), (100, 100, 120)).contains(batch[GraphKeys.TEST_GRAPH].spec.roi)
                assert batch[GraphKeys.TEST_GRAPH].spec.roi.contains(node.location)
            assert seen_mirrored
    def test_square(self):
        

        test_graph = GraphKey("TEST_GRAPH")
        test_array1 = ArrayKey("TEST_ARRAY1")
        test_array2 = ArrayKey("TEST_ARRAY2")

        pipeline = ((ArrayTestSource(), TestSource()) + MergeProvider() + 
                    Pad(test_array1, None) + Pad(test_array2, None) + Pad(test_graph, None)
                    + SimpleAugment(
            mirror_only=[1,2], transpose_only=[1,2]
        ))

        request = BatchRequest()
        request[GraphKeys.TEST_GRAPH] = GraphSpec(roi=Roi((0, 50, 65), (100, 100, 100)))
        request[ArrayKeys.TEST_ARRAY1] = ArraySpec(roi=Roi((0, 0, 15), (100, 200, 200)))
        request[ArrayKeys.TEST_ARRAY2] = ArraySpec(roi=Roi((0, 50, 65), (100, 100, 100)))

        
        with build(pipeline):
            for i in range(100):
                batch = pipeline.request_batch(request)
                assert len(list(batch[GraphKeys.TEST_GRAPH].nodes)) == 1

                for (array_key, array) in batch.arrays.items():
                    assert batch.arrays[array_key].data.shape == batch.arrays[array_key].spec.roi.get_shape()
Exemple #10
0
    def test_compute_upstream_roi2(self):
        request_roi = Roi(offset=(0, 0), shape=(5, 10))
        sub_shift_array = np.array([[2, 0], [-1, 0], [5, 0], [-2, 0], [0, 0]],
                                   dtype=int)

        upstream_roi = Roi(offset=(-5, 0), shape=(12, 10))
        result = ShiftAugment.compute_upstream_roi(request_roi,
                                                   sub_shift_array)
        self.assertTrue(upstream_roi == result)
Exemple #11
0
    def setup(self):

        self.provides(
            ArrayKeys.RAW,
            ArraySpec(roi=Roi((200, 20, 20), (1800, 180, 180)), voxel_size=(20, 2, 2)),
        )

        self.provides(
            GraphKeys.PRESYN, GraphSpec(roi=Roi((200, 20, 20), (1800, 180, 180)))
        )
Exemple #12
0
    def setup(self):

        self.provides(
            ArrayKeys.TEST_LABELS,
            ArraySpec(roi=Roi((200, 20, 20), (1800, 180, 180)),
                      voxel_size=(20, 2, 2)),
        )

        self.provides(GraphKeys.TEST_GRAPH,
                      GraphSpec(roi=Roi((200, 20, 20), (1800, 180, 180))))
    def test_output(self):
        meta_base = self.path_to('tf_graph')

        ArrayKey('A')
        ArrayKey('B')
        ArrayKey('C')
        ArrayKey('GRADIENT_A')

        # create model meta graph file and get input/output names
        (a, b, c, optimizer, loss) = self.create_meta_graph(meta_base)

        source = ExampleTensorflowTrainSource()
        train = Train(
            meta_base,
            optimizer=optimizer,
            loss=loss,
            inputs={a: ArrayKeys.A, b: ArrayKeys.B},
            outputs={c: ArrayKeys.C},
            gradients={a: ArrayKeys.GRADIENT_A},
            save_every=100)
        pipeline = source + train

        request = BatchRequest({
            ArrayKeys.A: ArraySpec(roi=Roi((0, 0), (2, 2))),
            ArrayKeys.B: ArraySpec(roi=Roi((0, 0), (2, 2))),
            ArrayKeys.C: ArraySpec(roi=Roi((0, 0), (2, 2))),
            ArrayKeys.GRADIENT_A: ArraySpec(roi=Roi((0, 0), (2, 2))),
        })

        # train for a couple of iterations
        with build(pipeline):

            batch = pipeline.request_batch(request)

            self.assertAlmostEqual(batch.loss, 9.8994951)

            gradient_a = batch.arrays[ArrayKeys.GRADIENT_A].data
            self.assertTrue(gradient_a[0, 0] < gradient_a[0, 1])
            self.assertTrue(gradient_a[0, 1] < gradient_a[1, 0])
            self.assertTrue(gradient_a[1, 0] < gradient_a[1, 1])

            for i in range(200-1):
                loss1 = batch.loss
                batch = pipeline.request_batch(request)
                loss2 = batch.loss
                self.assertLess(loss2, loss1)

        # resume training
        with build(pipeline):

            for i in range(100):
                loss1 = batch.loss
                batch = pipeline.request_batch(request)
                loss2 = batch.loss
                self.assertLess(loss2, loss1)
Exemple #14
0
def test_transpose():
    voxel_size = Coordinate((20, 20))
    graph_key = GraphKey("GRAPH")
    array_key = ArrayKey("ARRAY")
    graph = Graph(
        [Node(id=1, location=np.array([450, 550]))],
        [],
        GraphSpec(roi=Roi((100, 200), (800, 600))),
    )
    data = np.zeros([40, 30])
    data[17, 17] = 1
    array = Array(
        data, ArraySpec(roi=Roi((100, 200), (800, 600)),
                        voxel_size=voxel_size))

    default_pipeline = (
        (GraphSource(graph_key, graph), ArraySource(array_key, array)) +
        MergeProvider() + SimpleAugment(
            mirror_only=[], transpose_only=[0, 1], transpose_probs=[0, 0]))

    transpose_pipeline = (
        (GraphSource(graph_key, graph), ArraySource(array_key, array)) +
        MergeProvider() + SimpleAugment(
            mirror_only=[], transpose_only=[0, 1], transpose_probs=[1, 1]))

    request = BatchRequest()
    request[graph_key] = GraphSpec(roi=Roi((400, 500), (200, 300)))
    request[array_key] = ArraySpec(roi=Roi((400, 500), (200, 300)))
    with build(default_pipeline):
        expected_location = [450, 550]
        batch = default_pipeline.request_batch(request)

        assert len(list(batch[graph_key].nodes)) == 1
        node = list(batch[graph_key].nodes)[0]
        assert all(np.isclose(node.location, expected_location))
        node_voxel_index = Coordinate(
            (node.location - batch[array_key].spec.roi.get_offset()) /
            voxel_size)
        assert (
            batch[array_key].data[node_voxel_index] == 1
        ), f"Node at {np.where(batch[array_key].data == 1)} not {node_voxel_index}"

    with build(transpose_pipeline):
        expected_location = [410, 590]
        batch = transpose_pipeline.request_batch(request)

        assert len(list(batch[graph_key].nodes)) == 1
        node = list(batch[graph_key].nodes)[0]
        assert all(np.isclose(node.location, expected_location))
        node_voxel_index = Coordinate(
            (node.location - batch[array_key].spec.roi.get_offset()) /
            voxel_size)
        assert (
            batch[array_key].data[node_voxel_index] == 1
        ), f"Node at {np.where(batch[array_key].data == 1)} not {node_voxel_index}"
Exemple #15
0
    def test_crop(self):
        g = Graph(self.nodes, self.edges, self.spec)

        sub_g = g.crop(Roi(Coordinate([1, 1, 1]), Coordinate([3, 3, 3])))
        self.assertEqual(g.spec.roi, self.spec.roi)
        self.assertEqual(sub_g.spec.roi,
                         Roi(Coordinate([1, 1, 1]), Coordinate([3, 3, 3])))

        sub_g.spec.directed = False
        self.assertTrue(g.spec.directed)
        self.assertFalse(sub_g.spec.directed)
Exemple #16
0
    def test_get_sub_shift_array2(self):
        total_roi = Roi(offset=(0, 0), shape=(6, 6))
        item_roi = Roi(offset=(1, 2), shape=(3, 3))
        shift_array = np.arange(12).reshape(6, 2).astype(int)
        shift_axis = 0
        lcm_voxel_size = Coordinate((1, 1))

        sub_shift_array = np.array([[2, 3], [4, 5], [6, 7]], dtype=int)
        result = ShiftAugment.get_sub_shift_array(total_roi, item_roi,
                                                  shift_array, shift_axis,
                                                  lcm_voxel_size)
        self.assertTrue(np.array_equal(result, sub_shift_array))
Exemple #17
0
    def test_multi_transpose(self):
        test_graph = GraphKey("TEST_GRAPH")
        test_array1 = ArrayKey("TEST_ARRAY1")
        test_array2 = ArrayKey("TEST_ARRAY2")
        point = np.array([50, 70, 100])

        transpose_dims = [0, 1, 2]
        pipeline = (ArrayTestSource(),
                    ExampleSource()) + MergeProvider() + SimpleAugment(
                        mirror_only=[], transpose_only=transpose_dims)

        request = BatchRequest()
        offset = (0, 20, 33)
        request[GraphKeys.TEST_GRAPH] = GraphSpec(
            roi=Roi(offset, (100, 100, 120)))
        request[ArrayKeys.TEST_ARRAY1] = ArraySpec(
            roi=Roi((0, 0, 0), (100, 200, 300)))
        request[ArrayKeys.TEST_ARRAY2] = ArraySpec(
            roi=Roi((0, 100, 250), (100, 100, 50)))

        # Create all possible permurations of our transpose dims
        transpose_combinations = list(permutations(transpose_dims, 3))
        possible_loc = np.zeros((len(transpose_combinations), 3))

        # Transpose points in all possible ways
        for i, comb in enumerate(transpose_combinations):
            possible_loc[i] = point[np.array(comb)]

        with build(pipeline):
            seen_transposed = False
            seen_node = True
            for i in range(100):
                batch = pipeline.request_batch(request)

                if len(list(batch[GraphKeys.TEST_GRAPH].nodes)) == 1:
                    seen_node = True
                    node = list(batch[GraphKeys.TEST_GRAPH].nodes)[0]

                    assert node.location in possible_loc

                    seen_transposed = seen_transposed or any(
                        [node.location[dim] != point[dim] for dim in range(3)])
                    assert Roi((0, 20, 33), (100, 100, 120)).contains(
                        batch[GraphKeys.TEST_GRAPH].spec.roi)
                    assert batch[GraphKeys.TEST_GRAPH].spec.roi.contains(
                        node.location)

                for (array_key, array) in batch.arrays.items():
                    assert batch.arrays[array_key].data.shape == batch.arrays[
                        array_key].spec.roi.get_shape()
            assert seen_transposed
            assert seen_node
Exemple #18
0
    def test_3d(self):

        test_graph = GraphKey("TEST_GRAPH")
        graph_spec = GraphSpec(roi=Roi((0, 0, 0), (5, 5, 5)))
        test_array = ArrayKey("TEST_ARRAY")
        array_spec = ArraySpec(
            roi=Roi((0, 0, 0), (5, 5, 5)), voxel_size=Coordinate((1, 1, 1))
        )
        test_array2 = ArrayKey("TEST_ARRAY2")
        array2_spec = ArraySpec(
            roi=Roi((0, 0, 0), (5, 5, 5)), voxel_size=Coordinate((1, 1, 1))
        )

        snapshot_request = BatchRequest()
        snapshot_request.add(test_graph, Coordinate((5, 5, 5)))

        pipeline = ExampleSource(
            [test_graph, test_array, test_array2], [graph_spec, array_spec, array2_spec]
        ) + Snapshot(
            {
                test_graph: "graphs/graph",
                test_array: "volumes/array",
                test_array2: "volumes/array2",
            },
            output_dir=str(self.test_dir),
            every=2,
            additional_request=snapshot_request,
            output_filename="snapshot.hdf",
        )

        snapshot_file_path = Path(self.test_dir, "snapshot.hdf")

        with build(pipeline):

            request = BatchRequest()
            roi = Roi((0, 0, 0), (5, 5, 5))

            request[test_array] = ArraySpec(roi=roi)
            request[test_array2] = ArraySpec(roi=roi)

            pipeline.request_batch(request)

            assert snapshot_file_path.exists()
            f = h5py.File(snapshot_file_path)
            assert f["volumes/array"] is not None
            assert f["graphs/graph-ids"] is not None

            snapshot_file_path.unlink()

            pipeline.request_batch(request)

            assert not snapshot_file_path.exists()
Exemple #19
0
    def setup(self):

        self.provides(
            ArrayKeys.RAW,
            ArraySpec(roi=Roi((20000, 2000, 2000), (2000, 200, 200)),
                      voxel_size=(20, 2, 2)))
        self.provides(
            ArrayKeys.GT_LABELS,
            ArraySpec(roi=Roi((20100, 2010, 2010), (1800, 180, 180)),
                      voxel_size=(20, 2, 2)))
        self.provides(
            GraphKeys.GT_GRAPH,
            GraphSpec(roi=Roi((None, None, None), (None, None, None)), ))
Exemple #20
0
    def setup(self):

        self.provides(
            ArrayKeys.A,
            ArraySpec(roi=Roi((0, 0, 0), (1000, 1000, 1000)),
                      voxel_size=(4, 4, 4)),
        )

        self.provides(
            ArrayKeys.B,
            ArraySpec(roi=Roi((0, 0, 0), (1000, 1000, 1000)),
                      voxel_size=(4, 4, 4)),
        )
Exemple #21
0
def test_mismatched_voxel_multiples():
    """
    Ensure we don't shift by half a voxel when transposing 2 axes.

    If voxel_size = [2, 2], and we transpose array of shape [4, 6]:

        center = total_roi.get_center() -> [2, 3]

        # Get distance from center, then transpose
        dist_to_center = center - roi.get_offset() -> [2, 3]
        dist_to_center = transpose(dist_to_center)  -> [3, 2]

        # Using the transposed distance to center, get the offset.
        new_offset = center - dist_to_center -> [-1, 1]

        shape = transpose(shape) -> [6, 4]

        original = ((0, 0), (4, 6))
        transposed = ((-1, 1), (6, 4))

    This result is what we would expect from tranposing, but no longer fits the voxel grid.
    dist_to_center should be limited to multiples of the lcm_voxel_size.

        instead we should get:
        original = ((0, 0), (4, 6))
        transposed = ((0, 0), (6, 4))
    """

    test_array = ArrayKey("TEST_ARRAY")
    data = np.zeros([3, 3])
    data[
        2,
        1] = 1  # voxel has Roi((4, 2) (2, 2)). Contained in Roi((0, 0), (6, 4)). at 2, 1
    source = ArraySource(
        test_array,
        Array(
            data,
            ArraySpec(roi=Roi((0, 0), (6, 6)), voxel_size=(2, 2)),
        ),
    )
    pipeline = source + SimpleAugment(
        mirror_only=[], transpose_only=[0, 1], transpose_probs={(1, 0): 1})

    with build(pipeline):
        request = BatchRequest()
        request[test_array] = ArraySpec(roi=Roi((0, 0), (4, 6)))

        batch = pipeline.request_batch(request)
        data = batch[test_array].data

        assert data[1, 2] == 1, f"{data}"
    def setup(self):

        for identifier in [ArrayKeys.RAW, ArrayKeys.GT_LABELS]:

            self.provides(
                identifier,
                ArraySpec(roi=Roi((1000, 1000, 1000), (400, 400, 400)),
                          voxel_size=(20, 2, 2)))

        for identifier in [PointsKeys.PRESYN, PointsKeys.POSTSYN]:

            self.provides(
                identifier,
                PointsSpec(roi=Roi((1000, 1000, 1000), (400, 400, 400))))
    def __init__(self):

        self.graph = Graph(
            [Node(id=1, location=np.array([50, 70, 100]))],
            [],
            GraphSpec(roi=Roi((-200, -200, -200), (400, 400, 478))),
        )
Exemple #24
0
    def test_relabel_components(self):
        path = Path(self.path_to("test_swc_source.swc"))

        # write test swc
        self._write_swc(path, self._toy_swc_points())

        # read arrays
        swc = PointsKey("SWC")
        source = SwcFileSource(path, swc)

        with build(source):
            batch = source.request_batch(
                BatchRequest(
                    {swc: PointsSpec(roi=Roi((0, 1, 0), (11, 10, 1)))}))

        temp_g = nx.DiGraph()
        for point_id, point in batch.points[swc].data.items():
            temp_g.add_node(point.point_id, label_id=point.label_id)
            if (point.parent_id != -1 and point.parent_id != point.point_id
                    and point.parent_id in batch.points[swc].data):
                temp_g.add_edge(point.point_id, point.parent_id)

        previous_label = None
        ccs = list(nx.weakly_connected_components(temp_g))
        self.assertEqual(len(ccs), 3)
        for cc in ccs:
            self.assertEqual(len(cc), 10)
            label = None
            for point_id in cc:
                if label is None:
                    label = temp_g.nodes[point_id]["label_id"]
                    self.assertNotEqual(label, previous_label)
                self.assertEqual(temp_g.nodes[point_id]["label_id"], label)
            previous_label = label
    def __init__(self):

        self.graph = Graph(
            [Node(id=1, location=np.array([500, 500, 500]))],
            [],
            GraphSpec(roi=Roi((0, 0, 0), (1000, 1000, 1000))),
        )
Exemple #26
0
    def test_ensure_center_non_zero(self):
        path = Path(self.path_to("test_swc_source.swc"))

        # write test swc
        self._write_swc(path, self._toy_swc_points().to_nx_graph())

        # read arrays
        swc = PointsKey("SWC")
        img = ArrayKey("IMG")
        pipeline = (SwcFileSource(
            path, [swc], [PointsSpec(roi=Roi((0, 0, 0), (11, 11, 11)))]) +
                    RandomLocation(ensure_nonempty=swc, ensure_centered=True) +
                    RasterizeSkeleton(
                        points=swc,
                        array=img,
                        array_spec=ArraySpec(
                            interpolatable=False,
                            dtype=np.uint32,
                            voxel_size=Coordinate((1, 1, 1)),
                        ),
                    ))

        request = BatchRequest()
        request.add(img, Coordinate((5, 5, 5)))
        request.add(swc, Coordinate((5, 5, 5)))

        with build(pipeline):
            batch = pipeline.request_batch(request)

            data = batch[img].data
            g = batch[swc]
            assert g.num_vertices() > 0

            self.assertNotEqual(data[tuple(np.array(data.shape) // 2)], 0)
Exemple #27
0
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]))
Exemple #28
0
 def setup(self):
     self.enable_autoskip()
     spatial_dims = len(self.spec[self.embeddings].voxel_size)
     mst_spec = GraphSpec(roi=Roi((None, ) * spatial_dims,
                                  (None, ) * spatial_dims),
                          directed=False)
     self.provides(self.mst, mst_spec)
Exemple #29
0
    def test_with_edge(self):
        graph_with_edge = GraphKey("TEST_GRAPH_WITH_EDGE")
        array_with_edge = ArrayKey("RASTERIZED_EDGE")

        pipeline = GraphTestSourceWithEdge() + RasterizeGraph(
            GraphKeys.TEST_GRAPH_WITH_EDGE,
            ArrayKeys.RASTERIZED_EDGE,
            ArraySpec(voxel_size=(1, 1, 1)),
            settings=RasterizationSettings(0.5),
        )

        with build(pipeline):
            request = BatchRequest()
            roi = Roi((0, 0, 0), (10, 10, 10))

            request[GraphKeys.TEST_GRAPH_WITH_EDGE] = GraphSpec(roi=roi)
            request[ArrayKeys.RASTERIZED_EDGE] = ArraySpec(roi=roi)

            batch = pipeline.request_batch(request)

            rasterized = batch.arrays[ArrayKeys.RASTERIZED_EDGE].data

            assert (
                rasterized.sum() == 10
            ), f"rasterized has ones at: {np.where(rasterized==1)}"
    def __init__(self):

        self.graph = Graph([
            Node(1, np.array([1, 1, 1])),
            Node(2, np.array([500, 500, 500])),
            Node(3, np.array([550, 550, 550])),
        ], [], GraphSpec(roi=Roi((-500, -500, -500), (1500, 1500, 1500))))