File size: 1,733 Bytes
2857cf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from __future__ import annotations

from pathlib import Path
import random

from PIL import Image

import kneiff.datasets.augmentations.transforms as augmentation_transforms
import kneiff.datasets.augmentations.workflow as augmentation_workflow


def _write_image(path: Path, *, size: tuple[int, int] = (24, 18)) -> None:
    image = Image.new("RGB", size, color=(80, 100, 120))
    image.save(path)


def test_apply_transforms_is_repeatable_with_local_rng() -> None:
    image = Image.new("RGB", (24, 18), color=(80, 100, 120))

    first_image, first_loss = augmentation_transforms.apply_transforms(
        image,
        flip_lr=True,
        rng=random.Random(42),
    )
    second_image, second_loss = augmentation_transforms.apply_transforms(
        image,
        flip_lr=True,
        rng=random.Random(42),
    )

    assert first_loss == second_loss
    assert first_image.size == image.size
    assert first_image.tobytes() == second_image.tobytes()


def test_dataset_augmentation_writes_numbered_outputs_and_preserves_extension_filter(
    tmp_path: Path,
) -> None:
    input_dir = tmp_path / "input"
    output_dir = tmp_path / "output"
    input_dir.mkdir()
    _write_image(input_dir / "source.png")
    _write_image(input_dir / "ignored.tiff")

    augmentation_workflow.augment_dataset(
        input_dir=input_dir,
        output_dir=output_dir,
        num_aug=1,
        workers=1,
    )

    assert (output_dir / "source_aug_1.png").exists()
    assert not (output_dir / "ignored_aug_1.tiff").exists()


def test_augmentation_modules_expose_transform_and_workflow_entrypoints() -> None:
    assert callable(augmentation_transforms.apply_transforms)
    assert callable(augmentation_workflow.augment_dataset)