Exemplo n.º 1
0
    def provide(self, request):

        batch = Batch()

        # have the pixels encode their position
        if ArrayKeys.RAW in request:

            # the z,y,x coordinates of the ROI
            roi = request[ArrayKeys.RAW].roi
            roi_voxel = roi // self.spec[ArrayKeys.RAW].voxel_size
            meshgrids = np.meshgrid(
                range(roi_voxel.get_begin()[0], roi_voxel.get_end()[0]),
                range(roi_voxel.get_begin()[1], roi_voxel.get_end()[1]),
                range(roi_voxel.get_begin()[2], roi_voxel.get_end()[2]),
                indexing="ij",
            )
            data = meshgrids[0] + meshgrids[1] + meshgrids[2]

            spec = self.spec[ArrayKeys.RAW].copy()
            spec.roi = roi
            batch.arrays[ArrayKeys.RAW] = Array(data, spec)

        if ArrayKeys.GT_LABELS in request:
            roi = request[ArrayKeys.GT_LABELS].roi
            roi_voxel_shape = (
                roi // self.spec[ArrayKeys.GT_LABELS].voxel_size
            ).get_shape()
            data = np.ones(roi_voxel_shape)
            data[roi_voxel_shape[0] // 2 :, roi_voxel_shape[1] // 2 :, :] = 2
            data[roi_voxel_shape[0] // 2 :, -(roi_voxel_shape[1] // 2) :, :] = 3
            spec = self.spec[ArrayKeys.GT_LABELS].copy()
            spec.roi = roi
            batch.arrays[ArrayKeys.GT_LABELS] = Array(data, spec)

        if GraphKeys.PRESYN in request:
            data_presyn, data_postsyn = self.__get_pre_and_postsyn_locations(
                roi=request[GraphKeys.PRESYN].roi
            )
        elif GraphKeys.POSTSYN in request:
            data_presyn, data_postsyn = self.__get_pre_and_postsyn_locations(
                roi=request[GraphKeys.POSTSYN].roi
            )

        voxel_size_points = self.spec[ArrayKeys.RAW].voxel_size
        for (graph_key, spec) in request.graph_specs.items():
            if graph_key == GraphKeys.PRESYN:
                data = data_presyn
            if graph_key == GraphKeys.POSTSYN:
                data = data_postsyn
            batch.graphs[graph_key] = Graph(
                list(data.values()), [], GraphSpec(spec.roi)
            )

        return batch
Exemplo n.º 2
0
    def process(self, batch, request):
        gt = batch[self.gt]
        voxel_size = self.spec[self.neighborhood].voxel_size
        request_roi = request[self.neighborhood].roi
        if voxel_size is None:
            voxel_size = Coordinate((1, ) * len(gt.spec.roi.get_shape()))

        neighborhood = np.zeros(
            (self.k, ) + (request_roi.get_shape() / voxel_size),
            dtype=np.float32)
        neighborhood_mask = np.zeros((request_roi.get_shape() / voxel_size),
                                     dtype=int)

        k_neighborhood_offsets = self.get_neighborhood_offsets()
        for i, connected_component in enumerate(gt.connected_components):
            component_id = i + 1
            node_locations = [
                gt.node(node_id).location for node_id in connected_component
            ]
            component_kdtree = cKDTree(node_locations)
            for node_id in connected_component:
                node = gt.node(node_id)
                location = node.location
                if request_roi.contains(location):
                    query_points = k_neighborhood_offsets + location
                    query_neighbors = component_kdtree.query(query_points,
                                                             1)[0]
                    voxel_index = Coordinate(
                        (location - request_roi.get_offset()) // voxel_size)
                    neighborhood[(slice(None), ) +
                                 voxel_index] = (query_neighbors /
                                                 self.distance)
                    if neighborhood_mask[voxel_index] == 0:
                        neighborhood_mask[voxel_index] = component_id
                    elif neighborhood_mask[voxel_index] != component_id:
                        neighborhood_mask[voxel_index] = -1

        neighborhood_mask = neighborhood_mask > 0

        outputs = Batch()
        neighborhood_spec = self.array_specs[self.neighborhood].copy()
        neighborhood_spec.roi = request_roi
        outputs[self.neighborhood] = Array(neighborhood, neighborhood_spec)
        neighborhood_mask_spec = self.array_specs[
            self.neighborhood_mask].copy()
        neighborhood_mask_spec.roi = request_roi
        outputs[self.neighborhood_mask] = Array(neighborhood_mask > 0,
                                                neighborhood_mask_spec)
        return outputs
Exemplo n.º 3
0
    def provide(self, request):

        batch = Batch()

        roi_points = request[GraphKeys.TEST_POINTS].roi
        roi_array = request[ArrayKeys.TEST_LABELS].roi
        roi_voxel = roi_array // self.spec[ArrayKeys.TEST_LABELS].voxel_size

        data = np.zeros(roi_voxel.get_shape(), dtype=np.uint32)
        data[:, ::2] = 100

        for node in self.points:
            loc = self.point_to_voxel(roi_array, node.location)
            data[loc] = node.id

        spec = self.spec[ArrayKeys.TEST_LABELS].copy()
        spec.roi = roi_array
        batch.arrays[ArrayKeys.TEST_LABELS] = Array(data, spec=spec)

        points = []
        for node in self.points:
            if roi_points.contains(node.location):
                points.append(node)
        batch.graphs[GraphKeys.TEST_POINTS] = Graph(points, [],
                                                    GraphSpec(roi=roi_points))

        return batch
Exemplo n.º 4
0
    def provide(self, request):

        batch = Batch()

        # have the pixels encode their position
        for (array_key, spec) in request.array_specs.items():

            roi = spec.roi

            for d in range(3):
                assert roi.get_begin(
                )[d] % 4 == 0, "roi %s does not align with voxels"

            data_roi = roi / 4

            # the z,y,x coordinates of the ROI
            meshgrids = np.meshgrid(
                range(data_roi.get_begin()[0],
                      data_roi.get_end()[0]),
                range(data_roi.get_begin()[1],
                      data_roi.get_end()[1]),
                range(data_roi.get_begin()[2],
                      data_roi.get_end()[2]),
                indexing="ij",
            )
            data = meshgrids[0] + meshgrids[1] + meshgrids[2]

            spec = self.spec[array_key].copy()
            spec.roi = roi
            batch.arrays[array_key] = Array(data, spec)
        return batch
Exemplo n.º 5
0
def test_get_total_roi_nonspatial_array():

    raw = ArrayKey('RAW')
    nonspatial = ArrayKey('NONSPATIAL')

    voxel_size = Coordinate((1, 2))
    roi = Roi((100, 200), (20, 20))

    raw_spec = ArraySpec(roi=roi, voxel_size=voxel_size)
    nonspatial_spec = ArraySpec(nonspatial=True)

    batch = Batch()
    batch[raw] = Array(data=np.zeros((20, 10)), spec=raw_spec)
    batch[nonspatial] = Array(data=np.zeros((2, 3)), spec=nonspatial_spec)

    assert batch.get_total_roi() == roi
Exemplo n.º 6
0
    def provide(self, request):

        batch = Batch()

        if PointsKeys.TEST_POINTS in request:
            roi_points = request[PointsKeys.TEST_POINTS].roi

            points = {}
            for i, point in self.points.items():
                if roi_points.contains(point.location):
                    points[i] = copy.deepcopy(point)
            batch.points[PointsKeys.TEST_POINTS] = Points(
                points, PointsSpec(roi=roi_points)
            )

        if ArrayKeys.TEST_LABELS in request:
            roi_array = request[ArrayKeys.TEST_LABELS].roi
            roi_voxel = roi_array // self.spec[ArrayKeys.TEST_LABELS].voxel_size

            data = np.zeros(roi_voxel.get_shape(), dtype=np.uint32)
            data[:, ::2] = 100

            for i, point in self.points.items():
                loc = self.point_to_voxel(roi_array, point.location)
                data[loc] = i

            spec = self.spec[ArrayKeys.TEST_LABELS].copy()
            spec.roi = roi_array
            batch.arrays[ArrayKeys.TEST_LABELS] = Array(data, spec=spec)

        return batch
Exemplo n.º 7
0
class ArrayTestSource(BatchProvider):
    def __init__(self):
        spec = ArraySpec(roi=Roi((-200, -200, -200), (600, 600, 600)),
                         dtype=np.float64,
                         voxel_size=(1, 1, 1))
        self.array = Array(np.zeros(spec.roi.get_shape()), spec=spec)

    def setup(self):

        self.provides(ArrayKeys.TEST_ARRAY1, self.array.spec)
        self.provides(ArrayKeys.TEST_ARRAY2, self.array.spec)

    def prepare(self, request):
        return request

    def provide(self, request):

        batch = Batch()

        roi1 = request[ArrayKeys.TEST_ARRAY1].roi
        roi2 = request[ArrayKeys.TEST_ARRAY2].roi

        batch[ArrayKeys.TEST_ARRAY1] = self.array.crop(roi1)
        batch[ArrayKeys.TEST_ARRAY2] = self.array.crop(roi2)

        return batch
Exemplo n.º 8
0
    def provide(self, request):

        batch = Batch()

        if GraphKeys.TEST_POINTS in request:
            roi_points = request[GraphKeys.TEST_POINTS].roi

            contained_points = []
            for point in self.points:
                if roi_points.contains(point.location):
                    contained_points.append(copy.deepcopy(point))
            batch[GraphKeys.TEST_POINTS] = Graph(contained_points, [],
                                                 GraphSpec(roi=roi_points))

        if ArrayKeys.TEST_LABELS in request:
            roi_array = request[ArrayKeys.TEST_LABELS].roi
            roi_voxel = roi_array // self.spec[
                ArrayKeys.TEST_LABELS].voxel_size

            data = np.zeros(roi_voxel.get_shape(), dtype=np.uint32)
            data[:, ::2] = 100

            for point in self.points:
                loc = self.point_to_voxel(roi_array, point.location)
                data[loc] = point.id

            spec = self.spec[ArrayKeys.TEST_LABELS].copy()
            spec.roi = roi_array
            batch.arrays[ArrayKeys.TEST_LABELS] = Array(data, spec=spec)

        return batch
Exemplo n.º 9
0
    def provide(self, request: BatchRequest) -> Batch:
        if self.array in request:
            spec = request[self.array].copy()
            spec.dtype = self.array_specs[self.array].dtype
            spec.voxel_size = self.voxel_size
            roi = spec.roi
            assert (a % b == 0
                    for a, b in zip(roi.get_shape(), spec.voxel_size))
            assert (a // b < 500
                    for a, b in zip(roi.get_shape(), spec.voxel_size))
            array_start = roi.get_begin() / spec.voxel_size
            array_size = roi.get_shape() / spec.voxel_size
            array = np.zeros((2, ) + tuple(array_size), dtype=spec.dtype)
            for x, y, z in itertools.product(
                    *
                [range(a, a + b) for a, b in zip(array_start, array_size)]):
                i = x - array_start[0]
                j = y - array_start[1]
                k = z - array_start[2]
                if (abs(x * spec.voxel_size[0] - y * spec.voxel_size[1]) <=
                        max(spec.voxel_size)
                        and abs(x * spec.voxel_size[0] -
                                z * spec.voxel_size[2]) <= max(spec.voxel_size)
                        and
                        abs(y * spec.voxel_size[1] - z * spec.voxel_size[2]) <=
                        max(spec.voxel_size)):
                    array[:, int(i), int(j), int(k)] = 255

            batch = Batch()
            batch[self.array] = Array(array, spec)

            return batch
        else:
            return Batch()
Exemplo n.º 10
0
 def _empty_copy(self, base: Batch):
     add = Batch()
     for key, array in base.arrays.items():
         add[key] = Array(np.zeros_like(array.data),
                          spec=copy.deepcopy(array.spec))
     for key, points in base.points.items():
         add[key] = Graph([], [], spec=copy.deepcopy(points.spec))
     return add
Exemplo n.º 11
0
    def provide(self, request):

        batch = Batch()

        spec = self.spec[ArrayKeys.A]
        spec.roi = request[ArrayKeys.A].roi

        batch.arrays[ArrayKeys.A] = Array(
            np.array([[0, 1], [2, 3]], dtype=np.float32), spec)

        spec = self.spec[ArrayKeys.B]
        spec.roi = request[ArrayKeys.B].roi

        batch.arrays[ArrayKeys.B] = Array(
            np.array([[0, 1], [2, 3]], dtype=np.float32), spec)

        return batch
Exemplo n.º 12
0
    def process(self, batch, request):
        if self.skip:
            return

        dims = len(self.voxel_size)

        segmentation_array = batch[self.segmentation]

        # get voxel roi of requested descriptors
        # this is the only region in
        # which we have to compute the descriptors
        seg_roi = segmentation_array.spec.roi
        descriptor_roi = request[self.descriptor].roi
        voxel_roi_in_seg = (seg_roi.intersect(descriptor_roi) -
                            seg_roi.get_offset()) / self.voxel_size

        descriptor = self.extractor.get_descriptors(
            segmentation=segmentation_array.data,
            voxel_size=self.voxel_size,
            roi=voxel_roi_in_seg)

        # create descriptor array
        descriptor_spec = self.spec[self.descriptor].copy()
        descriptor_spec.roi = request[self.descriptor].roi.copy()
        descriptor_array = Array(descriptor, descriptor_spec)

        # Create new batch for descriptor:
        batch = Batch()

        # create mask array
        if self.mask and self.mask in request:
            channel_mask = (segmentation_array.crop(descriptor_roi).data !=
                            0).astype(np.float32)

            mask_shape = len(channel_mask.shape)

            assert channel_mask.shape[-mask_shape:] == \
                    descriptor.shape[-mask_shape:]

            mask = np.array([channel_mask] * descriptor.shape[0])
            batch[self.mask] = Array(mask, descriptor_spec.copy())

        batch[self.descriptor] = descriptor_array

        return batch
Exemplo n.º 13
0
    def process(self, batch, request: BatchRequest):
        data = batch[self.array].data

        if self.threshold is None:
            data_min = np.min(data)
            data_med = np.median(data)
            threshold = (data_min + data_med) / 2
        else:
            threshold = self.threshold

        thresholded = data > threshold
        skeleton = np.squeeze(thresholded)
        t1 = time.time()
        skeleton = skeletonize(skeleton)
        t2 = time.time()
        logger.debug(f"SKELETONIZING TOOK {t2-t1} SECONDS!")
        skeleton = skeleton > 0

        spec = batch[self.array].spec.copy()
        spec.dtype = bool

        skeleton_array = Array(skeleton, spec)

        """
        t1 = time.time()
        skeleton_graph = self.array_to_graph(skeleton_array)
        t2 = time.time()
        logger.debug(f"GRID TO GRAPH TOOK {t2-t1} SECONDS!")

        t1 = time.time()
        skeleton_graph = self.downsample(skeleton_graph, self.sample_distance)
        t2 = time.time()
        logger.debug(f"DOWNSAMPLING TOOK {t2-t1} SECONDS!")

        t1 = time.time()
        candidates = self.graph_to_array(skeleton_graph, spec)
        t2 = time.time()
        logger.debug(f"GRAPH TO GRID TOOK {t2-t1} SECONDS!")
        """

        skeleton_array.data = self.candidates_via_sins(skeleton_array)
        candidates = skeleton_array
        candidates.data = np.expand_dims(candidates.data, 0)

        batch.arrays[self.maxima] = candidates
Exemplo n.º 14
0
    def _shift_and_crop(self, points: Graph, array: Array,
                        direction: Coordinate, output_roi: Roi):
        # Shift and crop the array
        center = array.spec.roi.get_offset() + array.spec.roi.get_shape() // 2
        new_center = center + direction
        new_offset = new_center - output_roi.get_shape() // 2
        new_roi = Roi(new_offset, output_roi.get_shape())
        array = array.crop(new_roi)
        array.spec.roi = output_roi

        new_points_data = {}
        new_points_spec = points.spec
        new_points_spec.roi = new_roi
        new_points_graph = nx.DiGraph()

        # shift points and add them to a graph
        for point_id, point in points.data.items():
            if new_roi.contains(point.location):
                new_point = point.copy()
                new_point.location = (point.location - new_offset +
                                      output_roi.get_begin())
                new_points_graph.add_node(
                    new_point.point_id,
                    point_id=new_point.point_id,
                    parent_id=new_point.parent_id,
                    location=new_point.location,
                    label_id=new_point.label_id,
                    radius=new_point.radius,
                    point_type=new_point.point_type,
                )
                if points.data.get(
                        new_point.parent_id, False) and new_roi.contains(
                            points.data[new_point.parent_id].location):
                    new_points_graph.add_edge(new_point.parent_id,
                                              new_point.point_id)

        # relabel connected components
        for i, connected_component in enumerate(
                nx.weakly_connected_components(new_points_graph)):
            for node in connected_component:
                new_points_graph.nodes[node]["label_id"] = i

        # store new graph data in points
        new_points_data = {
            point_id: Node(
                point["location"],
                point_id=point["point_id"],
                point_type=point["point_type"],
                radius=point["radius"],
                parent_id=point["parent_id"],
                label_id=point["label_id"],
            )
            for point_id, point in new_points_graph.nodes.items()
        }
        points = Graph(new_points_data, new_points_spec)
        points.spec.roi = output_roi
        return points, array
Exemplo n.º 15
0
 def provide(self, request):
     roi_array = request[ArrayKeys.GT_LABELS].roi
     data = np.zeros(roi_array.get_shape() /
                     self.spec[ArrayKeys.GT_LABELS].voxel_size)
     batch = Batch()
     spec = self.spec[ArrayKeys.GT_LABELS].copy()
     spec.roi = roi_array
     batch.arrays[ArrayKeys.GT_LABELS] = Array(data, spec)
     return batch
Exemplo n.º 16
0
    def process(self, batch, request):
        source_request = request[self.source]
        logger.debug("Shuffling channels for %s and roi %s" %
                     (self.source, source_request.roi))
        data = batch[self.source].crop(source_request.roi).data
        np.random.shuffle(data)

        spec = self.spec[self.source]
        spec.roi = source_request.roi
        batch[self.source] = Array(data, spec)
Exemplo n.º 17
0
 def graph_to_array(self, graph, array_spec):
     data = np.zeros(
         array_spec.roi.get_shape() / array_spec.voxel_size, dtype=array_spec.dtype
     )
     for node in graph.nodes:
         voxel_location = (
             node.location - array_spec.roi.get_begin()
         ) / array_spec.voxel_size
         data[tuple(int(x) for x in voxel_location)] = 1
     return Array(data, array_spec)
Exemplo n.º 18
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}"
    def process(self, batch, request):

        if self.skip:
            return

        dims = len(self.voxel_size)

        assert dims == 3, "AddLocalShapeDescriptor only works on 3D arrays."

        segmentation_array = batch.arrays[self.segmentation]

        # get voxel roi of requested descriptors -- this is the only region in
        # which we have to compute the descriptors
        seg_roi = segmentation_array.spec.roi
        descriptor_roi = request[self.descriptor].roi
        voxel_roi_in_seg = (
            seg_roi.intersect(descriptor_roi) -
            seg_roi.get_offset())/self.voxel_size

        descriptor = self.__get_descriptors(
            segmentation_array.data,
            voxel_roi_in_seg)

        # create descriptor array
        descriptor_spec = self.spec[self.descriptor].copy()
        descriptor_spec.roi = request[self.descriptor].roi.copy()
        descriptor_array = Array(descriptor, descriptor_spec)

        # create mask array
        if self.mask and self.mask in request:
            channel_mask = (segmentation_array.crop(descriptor_roi).data!=0).astype(np.float32)
            assert channel_mask.shape[-3:] == descriptor.shape[-3:]
            mask = np.array([channel_mask]*descriptor.shape[0])
            batch.arrays[self.mask] = Array(mask, descriptor_spec.copy())

        # crop segmentation back to original request
        seg_request_roi = request[self.segmentation].roi
        cropped_segmentation_array = segmentation_array.crop(seg_request_roi)

        batch.arrays[self.segmentation] = cropped_segmentation_array
        batch.arrays[self.descriptor] = descriptor_array
Exemplo n.º 20
0
    def provide(self, request):
        batch = Batch()

        spec = request[ArrayKeys.RAW].copy()
        spec.voxel_size = self.voxel_size
        size = spec.roi.get_shape() / spec.voxel_size
        offset = spec.roi.get_offset() / spec.voxel_size
        slce = tuple(slice(o, o + s) for o, s in zip(offset, size))

        batch.arrays[ArrayKeys.RAW] = Array(data=self.data[slce], spec=spec)

        return batch
Exemplo n.º 21
0
    def process(self, batch, request):

        spec = batch[self.labels].spec.copy()
        spec.dtype = np.uint8

        binarized = Array(data=(batch[self.labels].data > 0).astype(np.uint8),
                          spec=spec)

        if self.labels_binary is not None:
            batch[self.labels_binary] = binarized
        else:
            batch[self.labels] = binarized
Exemplo n.º 22
0
    def provide(self, request):

        batch = Batch()

        spec = self.spec[ArrayKeys.A]

        x = np.array(list(range(17)), dtype=np.float32).reshape([17, 1])
        x = x + x.T

        batch.arrays[ArrayKeys.A] = Array(x,
                                          spec).crop(request[ArrayKeys.A].roi)

        return batch
Exemplo n.º 23
0
    def provide(self, request):

        data = np.zeros(
            request[ArrayKeys.RAW].roi.get_shape() /
            self.spec[ArrayKeys.RAW].voxel_size,
            dtype=np.uint8,
        )
        spec = copy.deepcopy(self.spec[ArrayKeys.RAW])
        spec.roi = request[ArrayKeys.RAW].roi

        batch = Batch()
        batch.arrays[ArrayKeys.RAW] = Array(data, spec)
        return batch
Exemplo n.º 24
0
    def process(self, batch, request: BatchRequest):
        labels = batch[self.label_array_key].data
        spec = batch[self.label_array_key].spec.copy()
        spec.dtype = self.dtype

        binarized = labels != 0
        dt = -distance_transform_edt(np.logical_not(binarized),
                                     sampling=spec.voxel_size).astype(
                                         self.dtype)

        expanded = Array(data=dt, spec=spec)

        batch.arrays[self.distance_array_key] = expanded
Exemplo n.º 25
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}"
Exemplo n.º 26
0
    def provide(self, request):

        batch = Batch()

        spec = request[self.array].copy()
        spec.voxel_size = self.voxel_size

        data = np.zeros(request[self.array].roi.get_shape() / self.voxel_size)
        if request.array_specs[self.array].roi.contains((0, 0, 0)):
            data[:] = 1

        batch.arrays[self.array] = Array(data=data, spec=spec)

        return batch
Exemplo n.º 27
0
    def process(self, batch, request):
        out_request = request[self.output]
        data_1 = batch.arrays[self.channel_1].crop(out_request.roi).data
        data_2 = batch.arrays[self.channel_2].crop(out_request.roi).data

        data = np.stack([data_1, data_2], axis=0)
        if self.transpose:
            np.random.shuffle(data)

        spec = self.spec[self.output]
        spec.roi = out_request.roi
        logger.debug("Adding key %s with spec %s to batch" %
                     (self.output, spec))
        batch[self.output] = Array(data, spec)
Exemplo n.º 28
0
    def provide(self, request):

        batch = Batch()

        roi_array = request[ArrayKeys.TEST_LABELS].roi
        roi_voxel = roi_array // self.spec[ArrayKeys.TEST_LABELS].voxel_size

        data = np.zeros(roi_voxel.get_shape(), dtype=np.uint32)
        data[:, ::2] = 100

        spec = self.spec[ArrayKeys.TEST_LABELS].copy()
        spec.roi = roi_array
        batch.arrays[ArrayKeys.TEST_LABELS] = Array(data, spec=spec)

        return batch
Exemplo n.º 29
0
 def provide(self, request):
     outputs = Batch()
     if self.n % self.every == 0:
         assert GraphKeys.TEST_GRAPH in request
     else:
         assert GraphKeys.TEST_GRAPH not in request
     for key, spec in request.items():
         if isinstance(key, GraphKey):
             outputs[key] = Graph([], [], spec)
         if isinstance(key, ArrayKey):
             spec.voxel_size = self.spec[key].voxel_size
             outputs[key] = Array(
                 np.zeros(spec.roi.get_shape(), dtype=spec.dtype), spec)
     self.n += 1
     return outputs
Exemplo n.º 30
0
    def provide(self, request):

        # print("ScanTestSource: Got request " + str(request))

        batch = Batch()

        # have the pixels encode their position
        for (array_key, spec) in request.array_specs.items():

            roi = spec.roi
            roi_voxel = roi // self.spec[array_key].voxel_size
            # print("ScanTestSource: Adding " + str(array_key))

            # the z,y,x coordinates of the ROI
            meshgrids = np.meshgrid(range(roi_voxel.get_begin()[0],
                                          roi_voxel.get_end()[0]),
                                    range(roi_voxel.get_begin()[1],
                                          roi_voxel.get_end()[1]),
                                    range(roi_voxel.get_begin()[2],
                                          roi_voxel.get_end()[2]),
                                    indexing='ij')
            data = meshgrids[0] + meshgrids[1] + meshgrids[2]

            # print("Roi is: " + str(roi))

            spec = self.spec[array_key].copy()
            spec.roi = roi
            batch.arrays[array_key] = Array(data, spec)

        for graph_key, spec in request.graph_specs.items():
            # node at x, y, z if x%100==0, y%10==0, z%10==0
            nodes = []
            start = spec.roi.get_begin() - tuple(
                x % s for x, s in zip(spec.roi.get_begin(), [100, 10, 10]))
            for i, j, k in itertools.product(*[
                    range(a, b, s) for a, b, s in zip(
                        start, spec.roi.get_end(), [100, 10, 10])
            ]):
                location = np.array([i, j, k])
                if spec.roi.contains(location):
                    nodes.append(
                        Node(id=coordinate_to_id(i, j, k), location=location))
            batch.graphs[graph_key] = Graph(nodes, [], spec)

        return batch