Пример #1
0
def test_get_borders():
    slc = Slice5D.zero(x=slice(100, 200), y=slice(300, 400), c=slice(0, 4))
    thickness = Shape5D.zero(x=1, y=1)
    expected_borders = {
        slc.with_coord(x=slice(100, 101)),
        slc.with_coord(y=slice(300, 301)),
        slc.with_coord(x=slice(199, 200)),
        slc.with_coord(y=slice(399, 400)),
    }
    assert expected_borders == set(slc.get_borders(thickness))
    assert len(list(slc.get_borders(thickness))) == 4

    thickness = Shape5D.zero(x=10, y=20)
    expected_thick_borders = {
        slc.with_coord(x=slice(100, 110)),
        slc.with_coord(x=slice(190, 200)),
        slc.with_coord(y=slice(300, 320)),
        slc.with_coord(y=slice(380, 400)),
    }
    assert expected_thick_borders == set(slc.get_borders(thickness=thickness))
    assert len(list(slc.get_borders(thickness=thickness))) == 4

    z2_slc = Slice5D.zero(x=slice(100, 200), y=slice(300, 400), z=slice(8, 10))
    thickness = Shape5D.zero(x=10, z=2)
    expected_z2_borders = {
        z2_slc.with_coord(x=slice(100, 110)),
        z2_slc.with_coord(x=slice(190, 200)),
        z2_slc.with_coord(z=slice(8, 10)),
    }
    assert expected_z2_borders == set(z2_slc.get_borders(thickness=thickness))
    assert len(list(z2_slc.get_borders(thickness=thickness))) == 4
Пример #2
0
def test_get_borders():
    slc = Slice5D.zero(x=slice(100, 200), y=slice(300, 400), c=slice(0, 4))
    thickness = Shape5D.zero(x=1, y=1)
    expected_borders = {
        slc.with_coord(x=slice(100, 101)),
        slc.with_coord(y=slice(300, 301)),
        slc.with_coord(x=slice(199, 200)),
        slc.with_coord(y=slice(399, 400)),
    }
    for border_slc in slc.get_borders(thickness):
        expected_borders.remove(border_slc)
    assert len(expected_borders) == 0

    thickness = Shape5D.zero(x=10, y=20)
    expected_thick_borders = {
        slc.with_coord(x=slice(100, 110)),
        slc.with_coord(x=slice(190, 200)),
        slc.with_coord(y=slice(300, 320)),
        slc.with_coord(y=slice(380, 400)),
    }
    for border_slc in slc.get_borders(thickness=thickness):
        expected_thick_borders.remove(border_slc)
    assert len(expected_thick_borders) == 0

    z2_slc = Slice5D.zero(x=slice(100, 200), y=slice(300, 400), z=slice(8, 10))
    thickness = Shape5D.zero(x=10, z=2)
    expected_z2_borders = {
        z2_slc.with_coord(x=slice(100, 110)),
        z2_slc.with_coord(x=slice(190, 200)),
        z2_slc.with_coord(z=slice(8, 10)),
    }
    for border_slc in z2_slc.get_borders(thickness=thickness):
        expected_z2_borders.remove(border_slc)
    assert len(expected_z2_borders) == 0
Пример #3
0
def test_n5_datasink(tmp_path: Path, data: Array5D, datasource: DataSource):
    dataset_path = tmp_path / "test_n5_datasink.n5/data"
    sink = N5DataSink(path=dataset_path, data_slice=DataSourceSlice(datasource), tile_shape=Shape5D(x=10, y=10))
    sink.process(Slice5D.all())

    n5ds = DataSource.create(dataset_path)
    assert n5ds.retrieve(Slice5D.all()) == data
Пример #4
0
def test_cut():
    # fmt: off
    raw = numpy.asarray([
        [1,  2,  3,  4,  5],
        [6,  7,  8,  9,  10],
        [11, 12, 13, 14, 15],
        [16, 17, 18, 19, 20],
    ])
    expected_piece = numpy.asarray([
        [2,  3],
        [7,  8],
        [12, 13],
        [17, 18]
    ])
    expected_global_sub_piece = numpy.asarray([
        [3],
        [8],
        [13],
        [18]
    ])
    # fmt: on
    arr = Array5D(raw, "zy")
    piece = arr.cut(Slice5D(y=slice(1, 3)))
    assert (piece.raw("zy") == expected_piece).all()
    assert piece.location == Point5D.zero(y=1)

    global_sub_piece = piece.cut(Slice5D(y=2))
    assert (global_sub_piece.raw("zy") == expected_global_sub_piece).all()

    local_sub_piece = piece.local_cut(Slice5D(y=1))
    assert (local_sub_piece.raw("zy") == global_sub_piece.raw("zy")).all()
Пример #5
0
def test_slice_enlarge():
    slc = Slice5D(x=slice(10, 100), y=slice(20, 200))
    enlarged = slc.enlarged(radius=Point5D(x=1, y=2, z=3, t=4, c=5))
    assert enlarged == Slice5D(x=slice(9, 101), y=slice(18, 202))

    slc = Slice5D(x=slice(10, 100), y=slice(20, 200), z=0, t=0, c=0)
    enlarged = slc.enlarged(radius=Point5D(x=1, y=2, z=3, t=4, c=5))
    assert enlarged == Slice5D(x=slice(9, 101), y=slice(18, 202), z=slice(-3, 4), t=slice(-4, 5), c=slice(-5, 6))
Пример #6
0
def test_split_when_slice_is_multiple_of_block_shape():
    slc = Slice5D.zero(x=slice(100, 200), y=slice(200, 300))
    pieces = list(slc.split(Shape5D(x=50, y=50)))
    assert Slice5D.zero(x=slice(100, 150), y=slice(200, 250)) in pieces
    assert Slice5D.zero(x=slice(100, 150), y=slice(250, 300)) in pieces
    assert Slice5D.zero(x=slice(150, 200), y=slice(200, 250)) in pieces
    assert Slice5D.zero(x=slice(150, 200), y=slice(250, 300)) in pieces
    assert len(pieces) == 4
Пример #7
0
def test_get_tiles_when_slice_is_multiple_of_tile():
    slc = Slice5D.zero(x=slice(100, 200), y=slice(200, 300))
    tiles = list(slc.get_tiles(Shape5D(x=50, y=50)))
    assert Slice5D.zero(x=slice(100, 150), y=slice(200, 250)) in tiles
    assert Slice5D.zero(x=slice(100, 150), y=slice(250, 300)) in tiles
    assert Slice5D.zero(x=slice(150, 200), y=slice(200, 250)) in tiles
    assert Slice5D.zero(x=slice(150, 200), y=slice(250, 300)) in tiles
    assert len(tiles) == 4
Пример #8
0
def test_allocation():
    arr = Array5D.allocate(Slice5D.zero(x=slice(100, 200), y=slice(200, 300)), numpy.uint8)
    assert arr.shape == Shape5D(x=100, y=100)
    assert arr.location == Point5D.zero(x=100, y=200)

    arr = Array5D.allocate(Slice5D.zero(x=slice(-100, 200), y=slice(200, 300)), numpy.uint8)
    assert arr.shape == Shape5D(x=300, y=100)
    assert arr.location == Point5D.zero(x=-100, y=200)
Пример #9
0
def test_slice_defined_with():
    slc = Slice5D(x=slice(10, 20))

    assert slc.defined_with(Shape5D(x=100, y=15, z=17)) == Slice5D.zero(x=slice(10, 20), y=slice(0, 15), z=slice(0, 17))

    assert slc.defined_with(Slice5D.zero(x=slice(1, 3), y=slice(10, 20))) == Slice5D.zero(
        x=slice(10, 20), y=slice(10, 20)
    )
Пример #10
0
def test_slice_enclosing():
    p1 = Point5D.zero(x=-13, y=40)
    p2 = Point5D.zero(z=-1, c=6)
    p3 = Point5D.zero(t=3, x=4)
    p4 = Point5D.zero(t=100, y=400)

    expected_slice = Slice5D(x=slice(-13, 4 + 1), y=slice(40, 400 + 1), z=slice(-1, -1 + 1), c=slice(6, 6 + 1))
    assert Slice5D.enclosing([p1, p2, p3, p4])
Пример #11
0
 def process(self,
             roi: Slice5D = Slice5D.all(),
             address_mode: AddressMode = AddressMode.BLACK) -> None:
     defined_roi = roi.defined_with(self.data_slice)
     assert self.data_slice.contains(defined_roi)
     for piece in defined_roi.split(self.tile_shape):
         source_data = self.data_slice.datasource.retrieve(
             piece, address_mode=address_mode)
         self._process_tile(source_data)
Пример #12
0
def test_from_start_stop():
    start = Point5D(x=10, y=20, z=30, t=40, c=50)
    stop = start + 10
    slc = Slice5D.create_from_start_stop(start, stop)
    assert slc == Slice5D(x=slice(10, 20),
                          y=slice(20, 30),
                          z=slice(30, 40),
                          t=slice(40, 50),
                          c=slice(50, 60))
Пример #13
0
def test_n5_datasink_saves_roi(tmp_path: Path, data: Array5D, datasource: DataSource):
    roi = DataSourceSlice(datasource, x=slice(5, 8), y=slice(2, 4))

    dataset_path = tmp_path / "test_n5_datasink_saves_roi.n5/data"
    sink = N5DataSink(path=dataset_path, data_slice=roi, tile_shape=Shape5D(x=10, y=10))
    sink.process(Slice5D.all())

    n5ds = DataSource.create(dataset_path)
    assert n5ds.retrieve(Slice5D.all()) == roi.retrieve()
Пример #14
0
 def retrieve(self, roi: Slice5D, address_mode: AddressMode = AddressMode.BLACK) -> Array5D:
     # FIXME: Remove address_mode or implement all variations and make feature extractors use the correct one
     out = self._allocate(roi.defined_with(self.shape).translated(-self.location), fill_value=0)
     local_data_roi = roi.clamped(self.roi).translated(-self.location)
     for tile in local_data_roi.get_tiles(self.tile_shape):
         tile_within_bounds = tile.clamped(self.shape)
         tile_data = self.get_tile(tile_within_bounds)
         out.set(tile_data, autocrop=True)
     out.setflags(write=False)
     return out.translated(self.location)
Пример #15
0
def test_slice_translation():
    slc = Slice5D(x=slice(10, 100), y=slice(20, 200))
    translated_slc = slc.translated(Point5D(x=1, y=2, z=3, t=4, c=5))
    assert translated_slc == Slice5D(x=slice(11, 101), y=slice(22, 202))

    slc = Slice5D(x=slice(10, 100), y=slice(20, 200), z=0, t=0, c=0)
    translated_slc = slc.translated(Point5D(x=-1, y=-2, z=-3, t=-4, c=-5000))
    assert translated_slc == Slice5D(
        x=slice(9, 99), y=slice(18, 198), z=slice(-3, -2), t=slice(-4, -3), c=slice(-5000, -4999)
    )
Пример #16
0
def test_from_stack():
    stack = [
        Array5D(numpy.asarray([[0, 1, 2], [3, 4, 5], [6, 7, 8]]), axiskeys="yx"),
        Array5D(numpy.asarray([[7, 2, 2], [3, 1, 5], [2, 7, 3]]), axiskeys="yx"),
        Array5D(numpy.asarray([[4, 2, 1], [3, 4, 0], [2, 4, 1]]), axiskeys="yx"),
    ]

    z_stacked = Array5D.from_stack(stack, stack_along="z")
    for i in range(len(stack)):
        assert (z_stacked.cut(Slice5D(z=i)).raw("yx") == stack[i].raw("yx")).all()

    y_stacked = Array5D.from_stack(stack, stack_along="y")
    for i in range(len(stack)):
        stack_slc = Slice5D(y=slice(3 * i, 3 * (i + 1)))
        assert (y_stacked.cut(stack_slc).raw("yx") == stack[i].raw("yx")).all()
Пример #17
0
    def run_distributed_export(self, block_roi: Slice5D):
        from lazyflow.distributed.TaskOrchestrator import TaskOrchestrator

        orchestrator = TaskOrchestrator()
        n5_file_path = Path(self.OutputFilenameFormat.value).with_suffix(".n5")
        output_meta = self.ImageToExport.meta
        if orchestrator.rank == 0:
            output_shape = output_meta.getShape5D()
            block_shape = block_roi.clamped(output_shape.to_slice_5d()).shape

            with z5py.File(n5_file_path, "w") as f:
                ds = f.create_dataset(
                    self.OutputInternalPath.value,
                    shape=output_meta.shape,
                    chunks=block_shape.to_tuple(output_meta.getAxisKeys()),
                    dtype=output_meta.dtype.__name__,
                )
                ds.attrs["axes"] = list(reversed(output_meta.getAxisKeys()))
                ds[...] = 1  # FIXME: for some reason setting to 0 does nothing

            cutout = self.get_roi()
            orchestrator.orchestrate(cutout.split(block_shape=block_shape))
        else:

            def process_tile(tile: Slice5D, rank: int):
                self.set_roi(tile)
                slices = tile.to_slices(output_meta.getAxisKeys())
                with z5py.File(n5_file_path, "r+") as n5_file:
                    dataset = n5_file[self.OutputInternalPath.value]
                    dataset[slices] = self.ImageToExport.value

            orchestrator.start_as_worker(process_tile)
Пример #18
0
 def get_roi(self) -> Slice5D:
     input_axiskeys = self.Input.meta.getAxisKeys()
     roi_start, roi_stop = self.get_new_roi()
     roi_slices = tuple(
         slice(start, stop) for start, stop in zip(roi_start, roi_stop))
     return Slice5D.zero(
         **{axis: slc
            for axis, slc in zip(input_axiskeys, roi_slices)})
Пример #19
0
def test_neighboring_tiles():
    # fmt: off
    arr = Array5D(np.asarray(
        [[10, 11, 12, 20, 21, 22, 30], [13, 14, 15, 23, 24, 25, 33],
         [16, 17, 18, 26, 27, 28, 36], [40, 41, 42, 50, 51, 52, 60],
         [43, 44, 45, 53, 54, 55, 63], [46, 47, 48, 56, 57, 58, 66],
         [70, 71, 72, 80, 81, 82, 90], [73, 74, 75, 83, 84, 85, 93],
         [76, 77, 78, 86, 87, 88, 96], [0, 1, 2, 3, 4, 5, 6]],
        dtype=np.uint8),
                  axiskeys="yx")

    ds = DataSource.create(create_png(arr))

    fifties_slice = DataSourceSlice(ds).clamped(
        Slice5D(x=slice(3, 6), y=slice(3, 6)))
    expected_fifties_slice = Array5D(np.asarray([[50, 51, 52], [53, 54, 55],
                                                 [56, 57, 58]]),
                                     axiskeys="yx")
    # fmt: on

    top_slice = DataSourceSlice(ds, x=slice(3, 6), y=slice(0, 3))
    bottom_slice = DataSourceSlice(ds, x=slice(3, 6), y=slice(6, 9))

    right_slice = DataSourceSlice(ds, x=slice(6, 7), y=slice(3, 6))
    left_slice = DataSourceSlice(ds, x=slice(0, 3), y=slice(3, 6))

    # fmt: off
    fifties_neighbor_data = {
        top_slice:
        Array5D(np.asarray([[20, 21, 22], [23, 24, 25], [26, 27, 28]]),
                axiskeys="yx"),
        right_slice:
        Array5D(np.asarray([[60], [63], [66]]), axiskeys="yx"),
        bottom_slice:
        Array5D(np.asarray([[80, 81, 82], [83, 84, 85], [86, 87, 88]]),
                axiskeys="yx"),
        left_slice:
        Array5D(np.asarray([[40, 41, 42], [43, 44, 45], [46, 47, 48]]),
                axiskeys="yx"),
    }

    expected_fifties_neighbors = {}
    # fmt: on

    assert (fifties_slice.retrieve().raw("yx") == expected_fifties_slice.raw(
        "yx")).all()

    for neighbor in fifties_slice.get_neighboring_tiles(
            tile_shape=Shape5D(x=3, y=3)):
        try:
            expected_slice = fifties_neighbor_data.pop(neighbor)
            assert (expected_slice.raw("yx") == neighbor.retrieve().raw("yx")
                    ).all()
        except KeyError:
            print(f"\nWas searching for ", neighbor, "\n")
            for k in fifties_neighbor_data.keys():
                print("--->>> ", k)
    assert len(fifties_neighbor_data) == 0
Пример #20
0
def test_h5_datasource():
    data_2d = Array5D(np.arange(100).reshape(10, 10), axiskeys="yx")
    h5_path = create_h5(data_2d, axiskeys_style="vigra", chunk_shape=Shape5D(x=3, y=3))
    ds = DataSource.create(h5_path)
    assert ds.shape == data_2d.shape
    assert ds.tile_shape == Shape5D(x=3, y=3)

    slc = Slice5D(x=slice(0, 3), y=slice(0, 2))
    assert (ds.retrieve(slc).raw("yx") == data_2d.cut(slc).raw("yx")).all()

    data_3d = Array5D(np.arange(10 * 10 * 10).reshape(10, 10, 10), axiskeys="zyx")
    h5_path = create_h5(data_3d, axiskeys_style="vigra", chunk_shape=Shape5D(x=3, y=3))
    ds = DataSource.create(h5_path)
    assert ds.shape == data_3d.shape
    assert ds.tile_shape == Shape5D(x=3, y=3)

    slc = Slice5D(x=slice(0, 3), y=slice(0, 2), z=3)
    assert (ds.retrieve(slc).raw("yxz") == data_3d.cut(slc).raw("yxz")).all()
Пример #21
0
    def parse_known_cmdline_args(self, cmdline_args):
        # We use the same parser as the DataSelectionApplet
        parser = DataSelectionApplet.get_arg_parser(
            self.dataSelectionApplet.role_names)
        parser.add_argument(
            "--distributed",
            help=
            "Distributed mode. Used for running ilastik on HPCs via SLURM/srun/mpirun",
            action="store_true",
        )

        default_block_roi = Slice5D.all(x=slice(0, 256),
                                        y=slice(0, 256),
                                        z=slice(0, 256),
                                        t=slice(0, 1))

        def parse_distributed_block_roi(value: str) -> Slice5D:
            parsed = ast.literal_eval(value)
            if isinstance(parsed, dict):
                if not set(parsed.keys()).issubset("xyztc"):
                    raise ValueError(
                        f"Bad keys for distributed-block-roi: {value}")
                if not all(
                        isinstance(v, (int, None.__class__))
                        for v in parsed.values()):
                    raise ValueError(
                        f"Bad values for distributed-block-roi: {value}")
                overrides = {k: slice(0, int(v)) for k, v in parsed.items()}
            elif isinstance(parsed, int):
                overrides = {k: slice(0, parsed) for k in "xyz"}
            else:
                raise TypeError(
                    f"Could not convert value {value} into a Slice5D")

            return Slice5D(**{**default_block_roi.to_dict(), **overrides})

        parser.add_argument(
            "--distributed_block_roi",
            "--distributed-block-roi",
            help=textwrap.dedent("""
                Determines the dimensions of the blocks used to split the data in distributed mode.
                Values can be either:"
                    An integer, which will be interpreted as if the following dict was passed in:
                    {'x': value, 'y': value, 'z': value, 't': 1, 'c': None}

                    or a literal python Dict[str, Optional[int]], with keys in 'xyztc'.
                    Missing keys will default like so:
                    {'x': 256, 'y': 256, 'z': 256, 't': 1, 'c': None}
                    Use None anywhere in the dict to mean "the whole dimension".
                """),
            type=parse_distributed_block_roi,
            default=default_block_roi,
        )

        parsed_args, unused_args = parser.parse_known_args(cmdline_args)
        return parsed_args, unused_args
Пример #22
0
def test_get_neighbor_tile_adjacent_to():
    source_tile = Slice5D(x=slice(100, 200),
                          y=slice(300, 400),
                          c=slice(0, 3),
                          z=1,
                          t=1)

    right_border = source_tile.with_coord(x=slice(199, 200))
    right_neighbor = source_tile.get_neighbor_tile_adjacent_to(
        anchor=right_border, tile_shape=source_tile.shape)
    assert right_neighbor == source_tile.with_coord(x=slice(200, 300))

    left_border = source_tile.with_coord(x=slice(100, 101))
    left_neighbor = source_tile.get_neighbor_tile_adjacent_to(
        anchor=left_border, tile_shape=source_tile.shape)
    assert left_neighbor == source_tile.with_coord(x=slice(0, 100))

    top_border = source_tile.with_coord(y=slice(399, 400))
    top_neighbor = source_tile.get_neighbor_tile_adjacent_to(
        anchor=top_border, tile_shape=source_tile.shape)
    assert top_neighbor == source_tile.with_coord(y=slice(400, 500))

    bottom_border = source_tile.with_coord(y=slice(300, 301))
    bottom_neighbor = source_tile.get_neighbor_tile_adjacent_to(
        anchor=bottom_border, tile_shape=source_tile.shape)
    assert bottom_neighbor == source_tile.with_coord(y=slice(200, 300))

    partial_tile = Slice5D(x=slice(100, 200),
                           y=slice(400, 470),
                           c=slice(0, 3),
                           z=1,
                           t=1)

    right_border = partial_tile.with_coord(x=slice(199, 200))
    assert partial_tile.get_neighbor_tile_adjacent_to(
        anchor=right_border, tile_shape=source_tile.shape) == None

    left_border = partial_tile.with_coord(x=slice(100, 101))
    left_neighbor = partial_tile.get_neighbor_tile_adjacent_to(
        anchor=left_border, tile_shape=source_tile.shape)
    assert left_neighbor == partial_tile.with_coord(x=slice(0, 100))
Пример #23
0
 def _get_tile(self, tile: Slice5D) -> Array5D:
     slice_address = "_".join(
         f"{s.start}-{s.stop}"
         for s in tile.to_slices(self.scale.spatial_axiskeys))
     path = self.scale.key + "/" + slice_address
     with self.filesystem.openbin(path) as f:
         raw_tile_bytes = f.read()
     raw_tile_c_shape = tile.shape.to_tuple(self.axiskeys)
     raw_tile = np.frombuffer(raw_tile_bytes,
                              dtype=self.dtype).reshape(raw_tile_c_shape)
     tile_5d = Array5D(raw_tile, axiskeys=self.axiskeys)
     return tile_5d.translated(tile.start)
Пример #24
0
    def _get_tile(self, tile: Slice5D) -> Array5D:
        first_layer_idx = bisect.bisect_left(self.layer_offsets,
                                             tile.start[self.stack_axis])
        out = self._allocate(roi=tile, fill_value=0)
        for layer, layer_offset in zip(self.layers[first_layer_idx:],
                                       self.layer_offsets[first_layer_idx:]):
            if layer_offset > tile.stop[self.stack_axis]:
                break
            layer_tile = tile.clamped(layer.roi)
            layer_data = layer.retrieve(layer_tile)
            out.set(layer_data, autocrop=True)

        return out
Пример #25
0
def test_clamping():
    # fmt: off
    raw = numpy.asarray([
        [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15],
         [16, 17, 18, 19, 20]],
        [[-1, -2, -3, -4, -5], [-6, -7, -8, -9, -10],
         [-11, -12, -13, -14, -15], [-16, -17, -18, -19, -20]],
    ])
    expected_clamped_array = numpy.asarray([[-7, -8, -9], [-12, -13, -14]])
    # fmt: on
    arr = Array5D(raw, "zyx")
    clamped_raw = arr.clamped(Slice5D(z=1, x=slice(1, 4),
                                      y=slice(1, 3))).raw("zyx")
    assert (clamped_raw == expected_clamped_array).all()
Пример #26
0
def test_n5_datasource():
    # fmt: off
    data = Array5D(np.asarray([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],
                               [11, 12, 13, 14, 15], [16, 17, 18, 19,
                                                      20]]).astype(np.uint8),
                   axiskeys="yx")
    # fmt: on

    path = Path(create_n5(data))
    ds = DataSource.create(path)
    assert ds.shape == data.shape

    # fmt: off
    expected_raw_piece = Array5D(np.asarray([[1, 2, 3], [6, 7,
                                                         8]]).astype(np.uint8),
                                 axiskeys="yx")
    # fmt: on
    assert ds.retrieve(Slice5D(x=slice(0, 3),
                               y=slice(0, 2))) == expected_raw_piece

    ds2 = pickle.loads(pickle.dumps(ds))
    assert ds2.retrieve(Slice5D(x=slice(0, 3),
                                y=slice(0, 2))) == expected_raw_piece
    def reshape_datablock_and_slicing_for_input(
            self, block: numpy.ndarray, slicing: List[slice], slot: OutputSlot,
            project: Project) -> Tuple[numpy.ndarray, List[slice]]:
        """Reshapes a block of data and its corresponding slicing into the slot's current shape, so as to be
        compatible with versions of ilastik that saved and loaded block slots in their original shape

        Checks for version 1.3.3 and 1.3.3post1 because those were the versions that saved labels in 5D
        """
        current_axiskeys = self.get_input_image_current_axiskeys(slot)
        saved_data_axiskeys = self.get_saved_data_axiskeys(slot, project)
        fixed_slicing = Slice5D.zero(**dict(zip(
            saved_data_axiskeys, slicing))).to_slices(current_axiskeys)
        fixed_block = Array5D(block, saved_data_axiskeys).raw(current_axiskeys)
        return fixed_block, fixed_slicing
    def reshape_datablock_and_slicing_for_output(
            self, block: numpy.ndarray, slicing: List[slice],
            slot: OutputSlot) -> Tuple[numpy.ndarray, List[slice]]:
        """Reshapes a block of data and its corresponding slicing into the slot's original shape, so as to be
        compatible with versions of ilastik that saved and loaded block slots in their original shape

        Always save using original shape to be backwards compatible with 1.3.2
        """
        original_axiskeys = self.get_input_image_original_axiskeys(slot)
        current_axiskeys = self.get_input_image_current_axiskeys(slot)
        fixed_block = Array5D(block, current_axiskeys).raw(original_axiskeys)
        fixed_slicing = Slice5D.zero(**dict(zip(
            current_axiskeys, slicing))).to_slices(original_axiskeys)
        return fixed_block, fixed_slicing
    def reshape_datablock_and_slicing_for_input(
            self, block: numpy.ndarray, slicing: List[slice], slot: OutputSlot,
            project: Project) -> Tuple[numpy.ndarray, List[slice]]:
        """Reshapes a block of data and its corresponding slicing into the slot's current shape"""
        current_axiskeys = self.get_input_image_current_axiskeys(slot)
        saved_data_axiskeys = self.get_saved_data_axiskeys(slot, project)
        fixed_slicing = Slice5D.zero(**dict(zip(
            saved_data_axiskeys, slicing))).to_slices(current_axiskeys)
        fixed_block = Array5D(block, saved_data_axiskeys).raw(current_axiskeys)

        if current_axiskeys != saved_data_axiskeys:
            self.ignoreDirty = False
            self.dirty = True

        return fixed_block, fixed_slicing
Пример #30
0
def test_with_coord():
    slc = Slice5D(x=0, y=1, z=2, t=3, c=4)
    assert slc.with_coord(z=slice(10, 20)).to_slices("xyztc") == (
        slice(0, 1),
        slice(1, 2),
        slice(10, 20),
        slice(3, 4),
        slice(4, 5),
    )
    assert slc.with_coord(x=123).to_slices("xyztc") == (
        slice(123, 124),
        slice(1, 2),
        slice(2, 3),
        slice(3, 4),
        slice(4, 5),
    )