Exemplo n.º 1
0
def _parse_vec3_int(value: str) -> Vec3Int:
    parts = [int(part.strip()) for part in value.split(",")]
    if len(parts) == 1:
        return Vec3Int.full(parts[0])
    elif len(parts) == 3:
        return Vec3Int(*parts)
    else:
        raise TypeError(f"Cannot convert `{value}` to Vec3Int.")
def downsample_test_helper(WT1_path: Path, tmp_path: Path, use_compress: bool,
                           chunk_size: Vec3Int) -> None:
    source_path = WT1_path
    target_path = tmp_path / "WT1_wkw"

    source_ds = Dataset.open(source_path)
    target_ds = source_ds.copy_dataset(target_path,
                                       chunk_size=chunk_size,
                                       chunks_per_shard=16)

    target_layer = target_ds.get_layer("color")
    mag1 = target_layer.get_mag("1")
    target_layer.delete_mag("2-2-1")  # This is not needed for this test

    # The bounding box has to be set here explicitly because the downsampled data is written to a different dataset.
    target_layer.bounding_box = source_ds.get_layer("color").bounding_box

    mag2 = target_layer._initialize_mag_from_other_mag("2", mag1, use_compress)

    # The actual size of mag1 is (4600, 4600, 512).
    # To keep this test case fast, we are only downsampling a small part
    offset = (4096, 4096, 0)
    size = (504, 504, 512)
    source_buffer = mag1.read(
        absolute_offset=offset,
        size=size,
    )[0]
    assert np.any(source_buffer != 0)

    downsample_cube_job(
        (
            mag1.get_view(absolute_offset=offset, size=size),
            mag2.get_view(
                absolute_offset=offset,
                size=size,
            ),
            0,
        ),
        Vec3Int(2, 2, 2),
        InterpolationModes.MAX,
        Vec3Int.full(128),
    )

    assert np.any(source_buffer != 0)

    target_buffer = mag2.read(absolute_offset=offset, size=size)[0]
    assert np.any(target_buffer != 0)

    assert np.all(target_buffer == downsample_cube(source_buffer, [2, 2, 2],
                                                   InterpolationModes.MAX))
def test_compressed_downsample_cube_job(WT1_path: Path,
                                        tmp_path: Path) -> None:
    with warnings.catch_warnings():
        warnings.filterwarnings(
            "error")  # This escalates the warning to an error
        downsample_test_helper(WT1_path, tmp_path, True, Vec3Int.full(32))
def test_downsample_cube_job(WT1_path: Path, tmp_path: Path) -> None:
    downsample_test_helper(WT1_path, tmp_path, False, Vec3Int.full(16))
import pytest

from webknossos import COLOR_CATEGORY, Dataset, Mag, Vec3Int
from webknossos.dataset._downsampling_utils import (
    InterpolationModes,
    _mode,
    calculate_default_coarsest_mag,
    calculate_mags_to_downsample,
    calculate_mags_to_upsample,
    downsample_cube,
    downsample_cube_job,
    non_linear_filter_3d,
)
from webknossos.dataset.sampling_modes import SamplingModes

BUFFER_SHAPE = Vec3Int.full(256)


def test_downsample_cube() -> None:
    buffer = np.zeros(BUFFER_SHAPE, dtype=np.uint8)
    buffer[:, :, :] = np.arange(0, BUFFER_SHAPE.x)

    output = downsample_cube(buffer, [2, 2, 2], InterpolationModes.MODE)

    assert np.all(output.shape == (BUFFER_SHAPE.to_np() // 2))
    assert buffer[0, 0, 0] == 0
    assert buffer[0, 0, 1] == 1
    assert np.all(output[:, :, :] == np.arange(0, BUFFER_SHAPE.x, 2))


def test_downsample_mode() -> None:
Exemplo n.º 6
0
    setup_logging(args)

    if args.isotropic is not None:
        raise DeprecationWarning(
            "The flag 'isotropic' is deprecated. Consider using '--sampling_mode isotropic' instead."
        )

    if args.anisotropic_target_mag is not None:
        raise DeprecationWarning(
            "The 'anisotropic_target_mag' flag is deprecated. Use '--max' instead (and consider changing the 'sampling_mode')"
        )

    from_mag = Mag(args.from_mag)
    target_mag = Mag(args.target_mag)
    buffer_shape = (
        Vec3Int.full(args.buffer_cube_size)
        if args.buffer_cube_size is not None
        else None
    )

    upsample_mags(
        args.path,
        args.layer_name,
        from_mag,
        target_mag,
        buffer_shape=buffer_shape,
        compress=not args.no_compress,
        sampling_mode=args.sampling_mode,
        args=args,
    )
    args = create_parser().parse_args()
    setup_logging(args)

    if args.isotropic is not None:
        raise DeprecationWarning(
            "The flag 'isotropic' is deprecated. Consider using '--sampling_mode isotropic' instead."
        )

    if args.anisotropic_target_mag is not None:
        raise DeprecationWarning(
            "The 'anisotropic_target_mag' flag is deprecated. Use '--max' instead (and consider changing the 'sampling_mode')"
        )

    from_mag = Mag(args.from_mag)
    max_mag = None if args.max is None else Mag(args.max)
    buffer_shape = (Vec3Int.full(args.buffer_cube_size)
                    if args.buffer_cube_size is not None else None)

    downsample_mags(
        path=args.path,
        layer_name=args.layer_name,
        from_mag=from_mag,
        max_mag=max_mag,
        interpolation_mode=args.interpolation_mode,
        compress=not args.no_compress,
        sampling_mode=args.sampling_mode,
        buffer_shape=buffer_shape,
        force_sampling_scheme=args.force_sampling_scheme,
        args=get_executor_args(args),
    )