Esempio n. 1
0
def test_max_projection_preserves_coordinates():
    e = data.ISS(use_test_data=True)
    nuclei = e.fov().get_image('nuclei')

    red = Reduce(dims=[Axes.ROUND, Axes.CH, Axes.ZPLANE], func='max')
    nuclei_proj = red.run(nuclei)

    # Since this data already has only 1 round, 1 ch, 1 zplane
    # let's just assert that the max_proj operation didn't change anything
    assert nuclei.xarray.equals(nuclei_proj.xarray)

    stack_shape = OrderedDict([(Axes.ROUND, 3), (Axes.CH, 2),
                               (Axes.ZPLANE, 3), (Axes.Y, 10), (Axes.X, 10)])

    # Create stack with coordinates, verify coords unaffected by max_poj
    physical_coords = OrderedDict([(PhysicalCoordinateTypes.X_MIN, X_COORDS[0]),
                                   (PhysicalCoordinateTypes.X_MAX, X_COORDS[1]),
                                   (PhysicalCoordinateTypes.Y_MIN, Y_COORDS[0]),
                                   (PhysicalCoordinateTypes.Y_MAX, Y_COORDS[1]),
                                   (PhysicalCoordinateTypes.Z_MIN, Z_COORDS[0]),
                                   (PhysicalCoordinateTypes.Z_MAX, Z_COORDS[1])])

    stack = imagestack_with_coords_factory(stack_shape, physical_coords)

    stack_proj = red.run(stack)
    expected_z = np.average(Z_COORDS)
    verify_physical_coordinates(stack_proj, X_COORDS, Y_COORDS, expected_z)
Esempio n. 2
0
def tilepath_test(
    tmpdir_path: Path,
    tilepath_generator: Callable[[Path, str, ImageFormat, TileIdentifier],
                                 Path],
    rounds: Sequence[int],
    chs: Sequence[int],
    zplanes: Sequence[int],
    tile_height: int,
    tile_width: int,
    x_coords: Tuple[int, int],
    y_coords: Tuple[int, int],
    zplane_to_coords: Mapping[int, Number],
):
    """Write the tiles for a single aligned (physical coordinates) regular (the dimensions have the
    same cardinality) FOV, with a provided tile_path_generator.  Then build an experiment from the
    tiles.  Finally, load the resulting experiment as an ImageStack and verify that the data
    matches."""
    tile_identifiers: Sequence[TileIdentifier] = [
        TileIdentifier(0, round_label, ch_label, zplane_label)
        for round_label in rounds for ch_label in chs
        for zplane_label in zplanes
    ]
    tile_fetcher: TileFetcher = tile_fetcher_factory(
        UniqueTiles,
        pass_tile_indices=True,
        fovs=[0],
        rounds=rounds,
        chs=chs,
        zplanes=zplanes,
        tile_height=tile_height,
        tile_width=tile_width,
    )
    tile_coordinates: Mapping[TileIdentifier, Mapping[
        Coordinates, CoordinateValue]] = {
            tile_identifier: {
                Coordinates.X: x_coords,
                Coordinates.Y: y_coords,
                Coordinates.Z: zplane_to_coords[tile_identifier.zplane_label],
            }
            for tile_identifier in tile_identifiers
        }

    write_tile_data(
        tmpdir_path,
        FieldOfView.PRIMARY_IMAGES,
        ImageFormat.TIFF,
        tile_identifiers,
        tile_fetcher,
        tilepath_generator,
    )

    coordinates_csv_path = tmpdir_path / "coordinates.csv"
    rows = render_coordinates_to_rows(tile_coordinates)
    write_coordinates_csv(coordinates_csv_path, rows, True)

    format_structured_dataset(
        os.fspath(tmpdir_path),
        os.fspath(coordinates_csv_path),
        os.fspath(tmpdir_path),
        ImageFormat.TIFF,
        False,
    )

    # load the data and verify it.
    exp = Experiment.from_json(os.fspath(tmpdir_path / "experiment.json"))
    fov = exp.fov()
    stack = fov.get_image(FieldOfView.PRIMARY_IMAGES)

    for round_label in rounds:
        for ch_label in chs:
            for zplane_label in zplanes:
                data, _ = stack.get_slice({
                    Axes.ROUND: round_label,
                    Axes.CH: ch_label,
                    Axes.ZPLANE: zplane_label
                })
                expected_data = unique_data(
                    0,
                    rounds.index(round_label),
                    chs.index(ch_label),
                    zplanes.index(zplane_label),
                    1,
                    len(rounds),
                    len(chs),
                    len(zplanes),
                    tile_height,
                    tile_width,
                )
                assert np.allclose(data, expected_data)

    for selectors in stack._iter_axes({Axes.ZPLANE}):
        zplane_label = selectors[Axes.ZPLANE]
        verify_physical_coordinates(stack, x_coords, y_coords,
                                    zplane_to_coords[zplane_label],
                                    selectors[Axes.ZPLANE])
Esempio n. 3
0
def test_multiple_aligned_regular_fov(
    tmpdir,
    fovs=(0, 1, 2, 3),
    rounds=(1, 2, 4),
    chs=(2, 3, 4),
    zplanes=(0, 1, 2),
    tile_height=100,
    tile_width=60,
    fov_to_x_coords={
        0: (0.0, 0.1),
        1: (0.0, 0.1),
        2: (0.1, 0.2),
        3: (0.1, 0.2),
    },
    fov_to_y_coords={
        0: (0.0, 0.1),
        1: (0.1, 0.2),
        2: (0.0, 0.1),
        3: (0.1, 0.2),
    },
    zplane_to_coords={
        0: 0.20,
        1: 0.25,
        2: 0.3
    },
    # default value is mutable, but it's for readability reasons!
):
    """Write the tiles for a multi-fov aligned (physical coordinates) regular (the dimensions have
    the same cardinality) image.  Then build an experiment from the tiles.  Finally, load the
    resulting experiment as an ImageStack and verify that the data matches."""
    tmpdir_path: Path = Path(tmpdir)
    tile_identifiers: Sequence[TileIdentifier] = [
        TileIdentifier(fov_id, round_label, ch_label, zplane_label)
        for fov_id in fovs for round_label in rounds for ch_label in chs
        for zplane_label in zplanes
    ]
    tile_fetcher: TileFetcher = tile_fetcher_factory(
        UniqueTiles,
        pass_tile_indices=True,
        fovs=fovs,
        rounds=rounds,
        chs=chs,
        zplanes=zplanes,
        tile_height=tile_height,
        tile_width=tile_width,
    )
    tile_coordinates: Mapping[TileIdentifier, Mapping[
        Coordinates, CoordinateValue]] = {
            tile_identifier: {
                Coordinates.X: fov_to_x_coords[tile_identifier.fov_id],
                Coordinates.Y: fov_to_y_coords[tile_identifier.fov_id],
                Coordinates.Z: zplane_to_coords[tile_identifier.zplane_label],
            }
            for tile_identifier in tile_identifiers
        }

    write_tile_data(tmpdir_path, FieldOfView.PRIMARY_IMAGES, ImageFormat.TIFF,
                    tile_identifiers, tile_fetcher)

    coordinates_csv_path = tmpdir_path / "coordinates.csv"
    rows = render_coordinates_to_rows(tile_coordinates)
    write_coordinates_csv(coordinates_csv_path, rows, True)

    format_data(
        os.fspath(tmpdir_path),
        os.fspath(coordinates_csv_path),
        os.fspath(tmpdir_path),
        ImageFormat.TIFF,
        False,
    )

    # load the data and verify it.
    exp = Experiment.from_json(os.fspath(tmpdir_path / "experiment.json"))

    for fov_id in fovs:
        fov = exp.fov(
            lambda fieldofview: fieldofview.name == f"fov_{fov_id:03}")
        stack = fov.get_image(FieldOfView.PRIMARY_IMAGES)
        for round_label in rounds:
            for ch_label in chs:
                for zplane_label in zplanes:
                    data, _ = stack.get_slice({
                        Axes.ROUND: round_label,
                        Axes.CH: ch_label,
                        Axes.ZPLANE: zplane_label
                    })
                    expected_data = unique_data(
                        fovs.index(fov_id),
                        rounds.index(round_label),
                        chs.index(ch_label),
                        zplanes.index(zplane_label),
                        len(fovs),
                        len(rounds),
                        len(chs),
                        len(zplanes),
                        tile_height,
                        tile_width,
                    )
                    assert np.allclose(data, expected_data)

        for selectors in stack._iter_axes({Axes.ZPLANE}):
            zplane_label = selectors[Axes.ZPLANE]
            verify_physical_coordinates(stack, fov_to_x_coords[fov_id],
                                        fov_to_y_coords[fov_id],
                                        zplane_to_coords[zplane_label],
                                        selectors[Axes.ZPLANE])
Esempio n. 4
0
def test_single_aligned_regular_fov(
    tmpdir,
    tile_format: ImageFormat,
    in_place: bool,
    rounds=(1, 2, 4),
    chs=(2, 3, 4),
    zplanes=(0, 1, 2),
    tile_height=100,
    tile_width=60,
    x_coords=(0.0, 0.1),
    y_coords=(0.1, 0.2),
    zplane_to_coords={
        0: 0.20,
        1: 0.25,
        2: 0.3
    },
    # default value is mutable, but it's for readability reasons!
):
    """Write the tiles for a single aligned (physical coordinates) regular (the dimensions have the
    same cardinality) FOV.  Then build an experiment from the tiles.  Finally, load the resulting
    experiment as an ImageStack and verify that the data matches."""
    tmpdir_path: Path = Path(tmpdir)
    tile_identifiers: Sequence[TileIdentifier] = [
        TileIdentifier(0, round_label, ch_label, zplane_label)
        for round_label in rounds for ch_label in chs
        for zplane_label in zplanes
    ]
    tile_fetcher: TileFetcher = tile_fetcher_factory(
        UniqueTiles,
        pass_tile_indices=True,
        fovs=[0],
        rounds=rounds,
        chs=chs,
        zplanes=zplanes,
        tile_height=tile_height,
        tile_width=tile_width,
    )
    tile_coordinates: Mapping[TileIdentifier, Mapping[
        Coordinates, CoordinateValue]] = {
            tile_identifier: {
                Coordinates.X: x_coords,
                Coordinates.Y: y_coords,
                Coordinates.Z: zplane_to_coords[tile_identifier.zplane_label],
            }
            for tile_identifier in tile_identifiers
        }

    write_tile_data(tmpdir_path, FieldOfView.PRIMARY_IMAGES, tile_format,
                    tile_identifiers, tile_fetcher)

    coordinates_csv_path = tmpdir_path / "coordinates.csv"
    rows = render_coordinates_to_rows(tile_coordinates)
    write_coordinates_csv(coordinates_csv_path, rows, True)

    # Sleeping by 1 second will result in different timestamps written to TIFF files (the timestamp
    # in the TIFF header has 1 second resolution).  This exposes potential bugs, depending on the
    # nature of the bug and whether in_place is True.
    if tile_format == ImageFormat.TIFF:
        time.sleep(1)

    format_structured_dataset(
        os.fspath(tmpdir_path),
        os.fspath(coordinates_csv_path),
        os.fspath(tmpdir_path),
        tile_format,
        in_place,
    )

    # load the data and verify it.
    exp = Experiment.from_json(os.fspath(tmpdir_path / "experiment.json"))
    fov = exp.fov()
    stack = fov.get_image(FieldOfView.PRIMARY_IMAGES)

    for round_label in rounds:
        for ch_label in chs:
            for zplane_label in zplanes:
                data, _ = stack.get_slice({
                    Axes.ROUND: round_label,
                    Axes.CH: ch_label,
                    Axes.ZPLANE: zplane_label
                })
                expected_data = unique_data(
                    0,
                    rounds.index(round_label),
                    chs.index(ch_label),
                    zplanes.index(zplane_label),
                    1,
                    len(rounds),
                    len(chs),
                    len(zplanes),
                    tile_height,
                    tile_width,
                )
                assert np.allclose(data, expected_data)

    for selectors in stack._iter_axes({Axes.ZPLANE}):
        zplane_label = selectors[Axes.ZPLANE]
        verify_physical_coordinates(stack, x_coords, y_coords,
                                    zplane_to_coords[zplane_label],
                                    selectors[Axes.ZPLANE])
Esempio n. 5
0
def test_single_ragged_fov(
    tmpdir,
    raw_tile_identifiers=(
        (0, 0, 0),
        (0, 0, 1),
        (0, 0, 2),
        (0, 1, 0),
        (0, 1, 1),
        (0, 1, 2),
        (1, 0, 0),
    ),
    tile_height=100,
    tile_width=60,
    x_coords=(0.0, 0.1),
    y_coords=(0.1, 0.2),
    zplane_to_coords={
        0: 0.20,
        1: 0.25,
        2: 0.3
    },
    # default value is mutable, but it's for readability reasons!
):
    """Write the tiles for a single aligned (physical coordinates) ragged (the dimensions do not
    always have the same cardinality) FOV.  Then build an experiment from the tiles.  Finally, load
    the resulting experiment as an ImageStack and verify that the data matches."""
    tmpdir_path: Path = Path(tmpdir)
    tile_identifiers: Sequence[TileIdentifier] = [
        TileIdentifier(0, round_label, ch_label, zplane_label)
        for round_label, ch_label, zplane_label in raw_tile_identifiers
    ]
    rounds = sorted(
        set([round_label for round_label, _, _ in raw_tile_identifiers]))
    chs = sorted(set([ch_label for _, ch_label, _ in raw_tile_identifiers]))
    zplanes = sorted(
        set([zplane_label for _, _, zplane_label in raw_tile_identifiers]))
    tile_fetcher: TileFetcher = tile_fetcher_factory(
        UniqueTiles,
        pass_tile_indices=True,
        fovs=[0],
        rounds=rounds,
        chs=chs,
        zplanes=zplanes,
        tile_height=tile_height,
        tile_width=tile_width,
    )
    tile_coordinates: Mapping[TileIdentifier, Mapping[
        Coordinates, CoordinateValue]] = {
            tile_identifier: {
                Coordinates.X: x_coords,
                Coordinates.Y: y_coords,
                Coordinates.Z: zplane_to_coords[tile_identifier.zplane_label],
            }
            for tile_identifier in tile_identifiers
        }

    write_tile_data(tmpdir_path, FieldOfView.PRIMARY_IMAGES, ImageFormat.TIFF,
                    tile_identifiers, tile_fetcher)

    coordinates_csv_path = tmpdir_path / "coordinates.csv"
    rows = render_coordinates_to_rows(tile_coordinates)
    write_coordinates_csv(coordinates_csv_path, rows, True)

    format_structured_dataset(
        os.fspath(tmpdir_path),
        os.fspath(coordinates_csv_path),
        os.fspath(tmpdir_path),
        ImageFormat.TIFF,
        False,
    )

    # load the data and verify it.
    exp = Experiment.from_json(os.fspath(tmpdir_path / "experiment.json"))
    fov = exp.fov()

    for loaded_rounds, expected_chs, expected_zplanes in (
        ({0}, {0, 1}, {0, 1, 2}),
        ({1}, {0}, {0}),
    ):
        stacks = list(
            fov.get_images(FieldOfView.PRIMARY_IMAGES, rounds=loaded_rounds))

        assert len(stacks) == 1
        stack = stacks[0]
        assert set(stack.axis_labels(Axes.CH)) == set(expected_chs)
        assert set(stack.axis_labels(Axes.ZPLANE)) == set(expected_zplanes)

        for round_label in loaded_rounds:
            for ch_label in expected_chs:
                for zplane_label in expected_zplanes:
                    data, _ = stack.get_slice({
                        Axes.ROUND: round_label,
                        Axes.CH: ch_label,
                        Axes.ZPLANE: zplane_label
                    })
                    expected_data = unique_data(
                        0,
                        rounds.index(round_label),
                        chs.index(ch_label),
                        zplanes.index(zplane_label),
                        1,
                        len(rounds),
                        len(chs),
                        len(zplanes),
                        tile_height,
                        tile_width,
                    )
                    assert np.allclose(data, expected_data)

        for selectors in stack._iter_axes({Axes.ZPLANE}):
            zplane_label = selectors[Axes.ZPLANE]
            verify_physical_coordinates(stack, x_coords, y_coords,
                                        zplane_to_coords[zplane_label],
                                        selectors[Axes.ZPLANE])