Example #1
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)}"
Example #2
0
    def test_3d_basics(self):

        test_labels = ArrayKey("TEST_LABELS")
        test_points = GraphKey("TEST_POINTS")
        test_raster = ArrayKey("TEST_RASTER")

        pipeline = (
            PointTestSource3D() + ElasticAugment(
                [10, 10, 10],
                [0.1, 0.1, 0.1],
                # [0, 0, 0], # no jitter
                [0, 2.0 * math.pi],
            ) + RasterizeGraph(
                test_points,
                test_raster,
                settings=RasterizationSettings(radius=2, mode="peak"),
            ) + Snapshot(
                {
                    test_labels: "volumes/labels",
                    test_raster: "volumes/raster"
                },
                dataset_dtypes={test_raster: np.float32},
                output_dir=self.path_to(),
                output_filename="elastic_augment_test{id}-{iteration}.hdf",
            ))

        for _ in range(5):

            with build(pipeline):

                request_roi = Roi((-20, -20, -20), (40, 40, 40))

                request = BatchRequest()
                request[test_labels] = ArraySpec(roi=request_roi)
                request[test_points] = GraphSpec(roi=request_roi)
                request[test_raster] = ArraySpec(roi=request_roi)

                batch = pipeline.request_batch(request)
                labels = batch[test_labels]
                points = batch[test_points]

                # the point at (0, 0, 0) should not have moved
                self.assertTrue(points.contains(0))

                labels_data_roi = (
                    labels.spec.roi -
                    labels.spec.roi.get_begin()) / labels.spec.voxel_size

                # points should have moved together with the voxels
                for point in points.nodes:
                    loc = point.location - labels.spec.roi.get_begin()
                    loc = loc / labels.spec.voxel_size
                    loc = Coordinate(int(round(x)) for x in loc)
                    if labels_data_roi.contains(loc):
                        self.assertEqual(labels.data[loc], point.id)
Example #3
0
    def test_fast_transform_no_recompute(self):
        test_labels = ArrayKey("TEST_LABELS")
        test_points = GraphKey("TEST_POINTS")
        test_raster = ArrayKey("TEST_RASTER")
        fast_pipeline = (DensePointTestSource3D() + ElasticAugment(
            [10, 10, 10],
            [0.1, 0.1, 0.1],
            [0, 2.0 * math.pi],
            use_fast_points_transform=True,
            recompute_missing_points=False,
        ) + RasterizeGraph(
            test_points,
            test_raster,
            settings=RasterizationSettings(radius=2, mode="peak"),
        ))

        reference_pipeline = (
            DensePointTestSource3D() +
            ElasticAugment([10, 10, 10], [0.1, 0.1, 0.1], [0, 2.0 * math.pi]) +
            RasterizeGraph(
                test_points,
                test_raster,
                settings=RasterizationSettings(radius=2, mode="peak"),
            ))

        timings = []
        for i in range(5):
            points_fast = {}
            points_reference = {}
            # seed chosen specifically to make this test fail
            seed = i + 15
            with build(fast_pipeline):

                request_roi = Roi((0, 0, 0), (40, 40, 40))

                request = BatchRequest(random_seed=seed)
                request[test_labels] = ArraySpec(roi=request_roi)
                request[test_points] = GraphSpec(roi=request_roi)
                request[test_raster] = ArraySpec(roi=request_roi)

                t1_fast = time.time()
                batch = fast_pipeline.request_batch(request)
                t2_fast = time.time()
                points_fast = {
                    node.id: node
                    for node in batch[test_points].nodes
                }

            with build(reference_pipeline):

                request_roi = Roi((0, 0, 0), (40, 40, 40))

                request = BatchRequest(random_seed=seed)
                request[test_labels] = ArraySpec(roi=request_roi)
                request[test_points] = GraphSpec(roi=request_roi)
                request[test_raster] = ArraySpec(roi=request_roi)

                t1_ref = time.time()
                batch = reference_pipeline.request_batch(request)
                t2_ref = time.time()
                points_reference = {
                    node.id: node
                    for node in batch[test_points].nodes
                }

            timings.append((t2_fast - t1_fast, t2_ref - t1_ref))
            diffs = []
            missing = 0
            for point_id, point in points_reference.items():
                if point_id not in points_fast:
                    missing += 1
                    continue
                diff = point.location - points_fast[point_id].location
                diffs.append(tuple(diff))
                self.assertAlmostEqual(
                    np.linalg.norm(diff),
                    0,
                    delta=1,
                    msg=
                    "fast transform returned location {} but expected {} for point {}"
                    .format(point.location, points_fast[point_id].location,
                            point_id),
                )

            t_fast, t_ref = [np.mean(x) for x in zip(*timings)]
            self.assertLess(t_fast, t_ref)
            self.assertGreater(missing, 0)
Example #4
0
    def test_random_seed(self):

        test_labels = ArrayKey('TEST_LABELS')
        test_points = GraphKey('TEST_POINTS')
        test_raster = ArrayKey('TEST_RASTER')

        pipeline = (
            PointTestSource3D() + ElasticAugment(
                [10, 10, 10],
                [0.1, 0.1, 0.1],
                # [0, 0, 0], # no jitter
                [0, 2.0 * math.pi]) +  # rotate randomly
            # [math.pi/4, math.pi/4]) + # rotate by 45 deg
            # [0, 0]) + # no rotation
            RasterizeGraph(test_points,
                           test_raster,
                           settings=RasterizationSettings(radius=2,
                                                          mode='peak')) +
            Snapshot(
                {
                    test_labels: 'volumes/labels',
                    test_raster: 'volumes/raster'
                },
                dataset_dtypes={test_raster: np.float32},
                output_dir=self.path_to(),
                output_filename='elastic_augment_test{id}-{iteration}.hdf'))

        batch_points = []
        for _ in range(5):

            with build(pipeline):

                request_roi = Roi((-20, -20, -20), (40, 40, 40))

                request = BatchRequest(random_seed=10)
                request[test_labels] = ArraySpec(roi=request_roi)
                request[test_points] = GraphSpec(roi=request_roi)
                request[test_raster] = ArraySpec(roi=request_roi)
                batch = pipeline.request_batch(request)
                labels = batch[test_labels]
                points = batch[test_points]
                batch_points.append(
                    tuple((node.id, tuple(node.location))
                          for node in points.nodes))

                # the point at (0, 0, 0) should not have moved
                data = {node.id: node for node in points.nodes}
                self.assertTrue(0 in data)

                labels_data_roi = (
                    labels.spec.roi -
                    labels.spec.roi.get_begin()) / labels.spec.voxel_size

                # points should have moved together with the voxels
                for node in points.nodes:
                    loc = node.location - labels.spec.roi.get_begin()
                    loc = loc / labels.spec.voxel_size
                    loc = Coordinate(int(round(x)) for x in loc)
                    if labels_data_roi.contains(loc):
                        self.assertEqual(labels.data[loc], node.id)

        for point_data in zip(*batch_points):
            self.assertEqual(len(set(point_data)), 1)
Example #5
0
    def test_3d(self):

        GraphKey("TEST_GRAPH")
        ArrayKey("RASTERIZED")

        pipeline = GraphTestSource3D() + RasterizeGraph(
            GraphKeys.TEST_GRAPH,
            ArrayKeys.RASTERIZED,
            ArraySpec(voxel_size=(40, 4, 4)),
        )

        with build(pipeline):

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

            request[GraphKeys.TEST_GRAPH] = GraphSpec(roi=roi)
            request[ArrayKeys.GT_LABELS] = ArraySpec(roi=roi)
            request[ArrayKeys.RASTERIZED] = ArraySpec(roi=roi)

            batch = pipeline.request_batch(request)

            rasterized = batch.arrays[ArrayKeys.RASTERIZED].data
            self.assertEqual(rasterized[0, 0, 0], 1)
            self.assertEqual(rasterized[2, 20, 20], 0)
            self.assertEqual(rasterized[4, 49, 49], 1)

        # same with different foreground/background labels

        pipeline = GraphTestSource3D() + RasterizeGraph(
            GraphKeys.TEST_GRAPH,
            ArrayKeys.RASTERIZED,
            ArraySpec(voxel_size=(40, 4, 4)),
            RasterizationSettings(radius=1, fg_value=0, bg_value=1),
        )

        with build(pipeline):

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

            request[GraphKeys.TEST_GRAPH] = GraphSpec(roi=roi)
            request[ArrayKeys.GT_LABELS] = ArraySpec(roi=roi)
            request[ArrayKeys.RASTERIZED] = ArraySpec(roi=roi)

            batch = pipeline.request_batch(request)

            rasterized = batch.arrays[ArrayKeys.RASTERIZED].data
            self.assertEqual(rasterized[0, 0, 0], 0)
            self.assertEqual(rasterized[2, 20, 20], 1)
            self.assertEqual(rasterized[4, 49, 49], 0)

        # same with different radius and inner radius

        pipeline = GraphTestSource3D() + RasterizeGraph(
            GraphKeys.TEST_GRAPH,
            ArrayKeys.RASTERIZED,
            ArraySpec(voxel_size=(40, 4, 4)),
            RasterizationSettings(
                radius=40, inner_radius_fraction=0.25, fg_value=1, bg_value=0
            ),
        )

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

            request[GraphKeys.TEST_GRAPH] = GraphSpec(roi=roi)
            request[ArrayKeys.GT_LABELS] = ArraySpec(roi=roi)
            request[ArrayKeys.RASTERIZED] = ArraySpec(roi=roi)

            batch = pipeline.request_batch(request)

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

            # in the middle of the ball, there should be 0 (since inner radius is set)
            self.assertEqual(rasterized[0, 0, 0], 0)
            # check larger radius: rasterized point (0, 0, 0) should extend in
            # x,y by 10; z, by 1
            self.assertEqual(rasterized[0, 10, 0], 1)
            self.assertEqual(rasterized[0, 0, 10], 1)
            self.assertEqual(rasterized[1, 0, 0], 1)

            self.assertEqual(rasterized[2, 20, 20], 0)
            self.assertEqual(rasterized[4, 49, 49], 0)

        # same with anisotropic radius

        pipeline = GraphTestSource3D() + RasterizeGraph(
            GraphKeys.TEST_GRAPH,
            ArrayKeys.RASTERIZED,
            ArraySpec(voxel_size=(40, 4, 4)),
            RasterizationSettings(radius=(40, 40, 20), fg_value=1, bg_value=0),
        )

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

            request[GraphKeys.TEST_GRAPH] = GraphSpec(roi=roi)
            request[ArrayKeys.GT_LABELS] = ArraySpec(roi=roi)
            request[ArrayKeys.RASTERIZED] = ArraySpec(roi=roi)

            batch = pipeline.request_batch(request)

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

            # check larger radius: rasterized point (0, 0, 0) should extend in
            # x,y by 10; z, by 1
            self.assertEqual(rasterized[0, 10, 0], 1)
            self.assertEqual(rasterized[0, 11, 0], 0)
            self.assertEqual(rasterized[0, 0, 5], 1)
            self.assertEqual(rasterized[0, 0, 6], 0)
            self.assertEqual(rasterized[1, 0, 0], 1)
            self.assertEqual(rasterized[2, 0, 0], 0)

        # same with anisotropic radius and inner radius

        pipeline = GraphTestSource3D() + RasterizeGraph(
            GraphKeys.TEST_GRAPH,
            ArrayKeys.RASTERIZED,
            ArraySpec(voxel_size=(40, 4, 4)),
            RasterizationSettings(
                radius=(40, 40, 20), inner_radius_fraction=0.75, fg_value=1, bg_value=0
            ),
        )

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

            request[GraphKeys.TEST_GRAPH] = GraphSpec(roi=roi)
            request[ArrayKeys.GT_LABELS] = ArraySpec(roi=roi)
            request[ArrayKeys.RASTERIZED] = ArraySpec(roi=roi)

            batch = pipeline.request_batch(request)

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

            # in the middle of the ball, there should be 0 (since inner radius is set)
            self.assertEqual(rasterized[0, 0, 0], 0)
            # check larger radius: rasterized point (0, 0, 0) should extend in
            # x,y by 10; z, by 1
            self.assertEqual(rasterized[0, 10, 0], 1)
            self.assertEqual(rasterized[0, 11, 0], 0)
            self.assertEqual(rasterized[0, 0, 5], 1)
            self.assertEqual(rasterized[0, 0, 6], 0)
            self.assertEqual(rasterized[1, 0, 0], 1)
            self.assertEqual(rasterized[2, 0, 0], 0)
Example #6
0
    def test_3d_basics(self):

        test_labels = ArrayKey("TEST_LABELS")
        test_graph = GraphKey("TEST_GRAPH")
        test_raster = ArrayKey("TEST_RASTER")

        pipeline = (
            GraphTestSource3D()
            + ElasticAugment(
                [10, 10, 10],
                [0.1, 0.1, 0.1],
                # [0, 0, 0], # no jitter
                [0, 2.0 * math.pi],
            )
            +  # rotate randomly
            # [math.pi/4, math.pi/4]) + # rotate by 45 deg
            # [0, 0]) + # no rotation
            RasterizeGraph(
                test_graph,
                test_raster,
                settings=RasterizationSettings(radius=2, mode="peak"),
            )
            + Snapshot(
                {test_labels: "volumes/labels", test_raster: "volumes/raster"},
                dataset_dtypes={test_raster: np.float32},
                output_dir=self.path_to(),
                output_filename="elastic_augment_test{id}-{iteration}.hdf",
            )
        )

        for _ in range(5):

            with build(pipeline):

                request_roi = Roi((-20, -20, -20), (40, 40, 40))

                request = BatchRequest()
                request[test_labels] = ArraySpec(roi=request_roi)
                request[test_graph] = GraphSpec(roi=request_roi)
                request[test_raster] = ArraySpec(roi=request_roi)

                batch = pipeline.request_batch(request)
                labels = batch[test_labels]
                graph = batch[test_graph]

                # the node at (0, 0, 0) should not have moved
                # The node at (0,0,0) seems to have moved
                # self.assertIn(
                #     Node(id=0, location=np.array([0, 0, 0])), list(graph.nodes)
                # )
                self.assertIn(
                    0, [v.id for v in graph.nodes]
                )

                labels_data_roi = (
                    labels.spec.roi - labels.spec.roi.get_begin()
                ) / labels.spec.voxel_size

                # graph should have moved together with the voxels
                for node in graph.nodes:
                    loc = node.location - labels.spec.roi.get_begin()
                    loc = loc / labels.spec.voxel_size
                    loc = Coordinate(int(round(x)) for x in loc)
                    if labels_data_roi.contains(loc):
                        self.assertEqual(labels.data[loc], node.id)