Update test results directory structure and enhance Docker configurations

- Modified `.gitignore` to reflect the new path for test results.
- Updated `docker-compose.test.yml` to mount the correct test results directory.
- Adjusted `Dockerfile.test` to set the `PYTHONPATH` and ensure test results are saved in the updated location.
- Added `boto3` and `netron` to `requirements-test.txt` to support new functionalities.
- Updated `pytest.ini` to include the new `pythonpath` for test discovery.

These changes streamline the testing process and ensure compatibility with the updated directory structure.
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-03-28 00:13:08 +02:00
parent c20018745b
commit 243b69656b
48 changed files with 707 additions and 581 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ _PROJECT_ROOT = Path(__file__).resolve().parent.parent
_DATASET_IMAGES = _PROJECT_ROOT / "_docs/00_problem/input_data/dataset/images"
_DATASET_LABELS = _PROJECT_ROOT / "_docs/00_problem/input_data/dataset/labels"
_ONNX_MODEL = _PROJECT_ROOT / "_docs/00_problem/input_data/azaion.onnx"
_CLASSES_JSON = _PROJECT_ROOT / "classes.json"
_CLASSES_JSON = _PROJECT_ROOT / "src" / "classes.json"
_CONFIG_TEST = _PROJECT_ROOT / "config.test.yaml"
collect_ignore = ["security_test.py", "imagelabel_visualize_test.py"]
+6 -9
View File
@@ -1,9 +1,7 @@
import concurrent.futures
import random
import shutil
import sys
import time
import types
from pathlib import Path
import numpy as np
@@ -11,13 +9,6 @@ import pytest
from tests.conftest import apply_constants_patch
if "matplotlib" not in sys.modules:
_mpl = types.ModuleType("matplotlib")
_plt = types.ModuleType("matplotlib.pyplot")
_mpl.pyplot = _plt
sys.modules["matplotlib"] = _mpl
sys.modules["matplotlib.pyplot"] = _plt
def _patch_augmentation_paths(monkeypatch, base: Path):
apply_constants_patch(monkeypatch, base)
@@ -44,6 +35,7 @@ def _seed():
def test_pt_aug_01_throughput_ten_images_sixty_seconds(
tmp_path, monkeypatch, sample_images_labels
):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_augment_annotation_with_total(monkeypatch)
_seed()
@@ -59,9 +51,11 @@ def test_pt_aug_01_throughput_ten_images_sixty_seconds(
shutil.copy2(p, img_dir / p.name)
for p in src_lbl.glob("*.txt"):
shutil.copy2(p, lbl_dir / p.name)
# Act
t0 = time.perf_counter()
Augmentator().augment_annotations()
elapsed = time.perf_counter() - t0
# Assert
assert elapsed <= 60.0
@@ -69,6 +63,7 @@ def test_pt_aug_01_throughput_ten_images_sixty_seconds(
def test_pt_aug_02_parallel_at_least_one_point_five_x_faster(
tmp_path, monkeypatch, sample_images_labels
):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_augment_annotation_with_total(monkeypatch)
_seed()
@@ -97,6 +92,7 @@ def test_pt_aug_02_parallel_at_least_one_point_five_x_faster(
entries = [_E(n) for n in names]
# Act
aug_seq = Augmentator()
aug_seq.total_images_to_process = len(entries)
t0 = time.perf_counter()
@@ -115,4 +111,5 @@ def test_pt_aug_02_parallel_at_least_one_point_five_x_faster(
list(ex.map(aug_par.augment_annotation, entries))
par_elapsed = time.perf_counter() - t0
# Assert
assert seq_elapsed >= par_elapsed * 1.5
+3 -36
View File
@@ -1,7 +1,5 @@
import shutil
import sys
import time
import types
from os import path as osp
from pathlib import Path
@@ -10,40 +8,6 @@ import pytest
import constants as c_mod
def _stub_train_dependencies():
if getattr(_stub_train_dependencies, "_done", False):
return
def add_mod(name):
if name in sys.modules:
return sys.modules[name]
m = types.ModuleType(name)
sys.modules[name] = m
return m
ultra = add_mod("ultralytics")
class YOLO:
pass
ultra.YOLO = YOLO
def fake_client(*_a, **_k):
return types.SimpleNamespace(
upload_fileobj=lambda *_a, **_k: None,
download_file=lambda *_a, **_k: None,
)
boto = add_mod("boto3")
boto.client = fake_client
add_mod("netron")
add_mod("requests")
_stub_train_dependencies._done = True
_stub_train_dependencies()
def _prepare_form_dataset(
monkeypatch,
tmp_path,
@@ -82,6 +46,7 @@ def test_pt_dsf_01_dataset_formation_under_thirty_seconds(
fixture_images_dir,
fixture_labels_dir,
):
# Arrange
train, today_ds = _prepare_form_dataset(
monkeypatch,
tmp_path,
@@ -91,7 +56,9 @@ def test_pt_dsf_01_dataset_formation_under_thirty_seconds(
100,
set(),
)
# Act
t0 = time.perf_counter()
train.form_dataset()
elapsed = time.perf_counter() - t0
# Assert
assert elapsed <= 30.0
+9 -15
View File
@@ -1,23 +1,10 @@
import re
import sys
import types
import pytest
from dto.annotationClass import AnnotationClass
def _stub_train_imports():
if getattr(_stub_train_imports, "_done", False):
return
for _name in ("ultralytics", "boto3", "netron", "requests"):
if _name not in sys.modules:
sys.modules[_name] = types.ModuleType(_name)
sys.modules["ultralytics"].YOLO = type("YOLO", (), {})
sys.modules["boto3"].client = lambda *a, **k: None
_stub_train_imports._done = True
def _name_lines_under_names(text):
lines = text.splitlines()
out = []
@@ -39,7 +26,6 @@ _PLACEHOLDER_RE = re.compile(r"^-\s+Class-\d+\s*$")
@pytest.fixture
def data_yaml_text(monkeypatch, tmp_path, fixture_classes_json):
_stub_train_imports()
import train
import constants as c
@@ -52,14 +38,18 @@ def data_yaml_text(monkeypatch, tmp_path, fixture_classes_json):
def test_bt_cls_01_base_classes(fixture_classes_json):
# Act
d = AnnotationClass.read_json()
norm = {k: d[k] for k in range(17)}
# Assert
assert len(norm) == 17
assert len({v.id for v in norm.values()}) == 17
def test_bt_cls_02_weather_expansion(fixture_classes_json):
# Act
d = AnnotationClass.read_json()
# Assert
assert d[0].name == "ArmorVehicle"
assert d[20].name == "ArmorVehicle(Wint)"
assert d[40].name == "ArmorVehicle(Night)"
@@ -67,11 +57,13 @@ def test_bt_cls_02_weather_expansion(fixture_classes_json):
@pytest.mark.resource_limit
def test_bt_cls_03_yaml_generation(data_yaml_text):
# Arrange
text = data_yaml_text
assert "nc: 80" in text
# Act
names = _name_lines_under_names(text)
placeholders = [ln for ln in names if _PLACEHOLDER_RE.match(ln)]
named = [ln for ln in names if not _PLACEHOLDER_RE.match(ln)]
# Assert
assert len(names) == 80
assert len(placeholders) == 29
assert len(named) == 51
@@ -79,5 +71,7 @@ def test_bt_cls_03_yaml_generation(data_yaml_text):
@pytest.mark.resource_limit
def test_rl_cls_01_total_class_count(data_yaml_text):
# Act
names = _name_lines_under_names(data_yaml_text)
# Assert
assert len(names) == 80
+1 -1
View File
@@ -7,7 +7,7 @@ from pathlib import Path
import msgpack
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "annotation-queue"))
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "src" / "annotation-queue"))
from annotation_queue_dto import AnnotationBulkMessage, AnnotationMessage, AnnotationStatus, RoleEnum
+24 -9
View File
@@ -1,16 +1,7 @@
import random
import shutil
import sys
import types
from pathlib import Path
if "matplotlib" not in sys.modules:
_mpl = types.ModuleType("matplotlib")
_plt = types.ModuleType("matplotlib.pyplot")
_mpl.pyplot = _plt
sys.modules["matplotlib"] = _mpl
sys.modules["matplotlib.pyplot"] = _plt
import cv2
import numpy as np
@@ -41,6 +32,7 @@ def _augment_annotation_with_total(monkeypatch):
def test_bt_aug_01_augment_inner_returns_eight_image_labels(
tmp_path, monkeypatch, fixture_images_dir, fixture_labels_dir
):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_seed()
from augmentation import Augmentator
@@ -63,11 +55,14 @@ def test_bt_aug_01_augment_inner_returns_eight_image_labels(
labels_path=str(proc_lbl),
labels=labels,
)
# Act
out = aug.augment_inner(img_ann)
# Assert
assert len(out) == 8
def test_bt_aug_02_naming_convention(tmp_path, monkeypatch, fixture_images_dir, fixture_labels_dir):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_seed()
from augmentation import Augmentator
@@ -89,7 +84,9 @@ def test_bt_aug_02_naming_convention(tmp_path, monkeypatch, fixture_images_dir,
labels_path=str(proc_lbl),
labels=labels,
)
# Act
out = aug.augment_inner(img_ann)
# Assert
names = [Path(o.image_path).name for o in out]
expected = [f"{stem}.jpg"] + [f"{stem}_{i}.jpg" for i in range(1, 8)]
assert names == expected
@@ -110,6 +107,7 @@ def _all_coords_in_unit(labels_list):
def test_bt_aug_03_all_bbox_coords_in_zero_one(
tmp_path, monkeypatch, fixture_images_dir, fixture_labels_dir
):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_seed()
from augmentation import Augmentator
@@ -131,7 +129,9 @@ def test_bt_aug_03_all_bbox_coords_in_zero_one(
labels_path=str(proc_lbl),
labels=labels,
)
# Act
out = aug.augment_inner(img_ann)
# Assert
for o in out:
for row in o.labels:
assert len(row) >= 5
@@ -139,13 +139,16 @@ def test_bt_aug_03_all_bbox_coords_in_zero_one(
def test_bt_aug_04_correct_bboxes_clips_edge(tmp_path, monkeypatch):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
from augmentation import Augmentator
aug = Augmentator()
m = aug.correct_margin
inp = [[0.99, 0.5, 0.2, 0.1, 0]]
# Act
res = aug.correct_bboxes(inp)
# Assert
assert len(res) == 1
x, y, w, h, _ = res[0]
hw, hh = 0.5 * w, 0.5 * h
@@ -156,18 +159,22 @@ def test_bt_aug_04_correct_bboxes_clips_edge(tmp_path, monkeypatch):
def test_bt_aug_05_tiny_bbox_removed_after_clipping(tmp_path, monkeypatch):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
from augmentation import Augmentator
aug = Augmentator()
inp = [[0.995, 0.5, 0.01, 0.5, 0]]
# Act
res = aug.correct_bboxes(inp)
# Assert
assert res == []
def test_bt_aug_06_empty_label_eight_outputs_empty_labels(
tmp_path, monkeypatch, fixture_images_dir
):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_seed()
from augmentation import Augmentator
@@ -187,7 +194,9 @@ def test_bt_aug_06_empty_label_eight_outputs_empty_labels(
labels_path=str(proc_lbl),
labels=[],
)
# Act
out = aug.augment_inner(img_ann)
# Assert
assert len(out) == 8
for o in out:
assert o.labels == []
@@ -196,6 +205,7 @@ def test_bt_aug_06_empty_label_eight_outputs_empty_labels(
def test_bt_aug_07_full_pipeline_five_images_forty_outputs(
tmp_path, monkeypatch, sample_images_labels
):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_augment_annotation_with_total(monkeypatch)
_seed()
@@ -211,7 +221,9 @@ def test_bt_aug_07_full_pipeline_five_images_forty_outputs(
shutil.copy2(p, img_dir / p.name)
for p in src_lbl.glob("*.txt"):
shutil.copy2(p, lbl_dir / p.name)
# Act
Augmentator().augment_annotations()
# Assert
proc_img = Path(c.config.processed_images_dir)
proc_lbl = Path(c.config.processed_labels_dir)
assert len(list(proc_img.glob("*.jpg"))) == 40
@@ -219,6 +231,7 @@ def test_bt_aug_07_full_pipeline_five_images_forty_outputs(
def test_bt_aug_08_skips_already_processed(tmp_path, monkeypatch, sample_images_labels):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_augment_annotation_with_total(monkeypatch)
_seed()
@@ -244,7 +257,9 @@ def test_bt_aug_08_skips_already_processed(tmp_path, monkeypatch, sample_images_
dst = proc_img / p.name
shutil.copy2(p, dst)
markers.append(dst.read_bytes())
# Act
Augmentator().augment_annotations()
# Assert
after_jpgs = list(proc_img.glob("*.jpg"))
assert len(after_jpgs) == 19
assert len(list(proc_lbl.glob("*.txt"))) == 16
+12 -9
View File
@@ -1,7 +1,5 @@
import random
import shutil
import sys
import types
from pathlib import Path
from types import SimpleNamespace
@@ -11,13 +9,6 @@ import pytest
from tests.conftest import apply_constants_patch
if "matplotlib" not in sys.modules:
_mpl = types.ModuleType("matplotlib")
_plt = types.ModuleType("matplotlib.pyplot")
_mpl.pyplot = _plt
sys.modules["matplotlib"] = _mpl
sys.modules["matplotlib.pyplot"] = _plt
def _patch_augmentation_paths(monkeypatch, base: Path):
apply_constants_patch(monkeypatch, base)
@@ -44,6 +35,7 @@ def _seed():
def test_rt_aug_01_corrupted_image_skipped(
tmp_path, monkeypatch, fixture_images_dir, fixture_labels_dir
):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_augment_annotation_with_total(monkeypatch)
_seed()
@@ -59,13 +51,16 @@ def test_rt_aug_01_corrupted_image_skipped(
shutil.copy2(fixture_labels_dir / f"{stem}.txt", lbl_dir / f"{stem}.txt")
raw = (fixture_images_dir / f"{stem}.jpg").read_bytes()[:200]
(img_dir / "corrupted_trunc.jpg").write_bytes(raw)
# Act
Augmentator().augment_annotations()
# Assert
proc_img = Path(c.config.processed_images_dir)
assert len(list(proc_img.glob("*.jpg"))) == 8
@pytest.mark.resilience
def test_rt_aug_02_missing_label_no_crash(tmp_path, monkeypatch, fixture_images_dir):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_augment_annotation_with_total(monkeypatch)
import constants as c
@@ -79,7 +74,9 @@ def test_rt_aug_02_missing_label_no_crash(tmp_path, monkeypatch, fixture_images_
shutil.copy2(sorted(fixture_images_dir.glob("*.jpg"))[0], img_dir / f"{stem}.jpg")
aug = Augmentator()
aug.total_images_to_process = 1
# Act
aug.augment_annotation(SimpleNamespace(name=f"{stem}.jpg"))
# Assert
assert len(list(Path(c.config.processed_images_dir).glob("*.jpg"))) == 0
@@ -87,6 +84,7 @@ def test_rt_aug_02_missing_label_no_crash(tmp_path, monkeypatch, fixture_images_
def test_rt_aug_03_narrow_bbox_fewer_or_eight_variants(
tmp_path, monkeypatch, fixture_images_dir
):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_seed()
from augmentation import Augmentator
@@ -107,7 +105,9 @@ def test_rt_aug_03_narrow_bbox_fewer_or_eight_variants(
labels_path=str(proc_lbl),
labels=labels,
)
# Act
out = aug.augment_inner(img_ann)
# Assert
assert 1 <= len(out) <= 8
@@ -115,6 +115,7 @@ def test_rt_aug_03_narrow_bbox_fewer_or_eight_variants(
def test_rl_aug_01_augment_inner_exactly_eight_outputs(
tmp_path, monkeypatch, fixture_images_dir, fixture_labels_dir
):
# Arrange
_patch_augmentation_paths(monkeypatch, tmp_path)
_seed()
from augmentation import Augmentator
@@ -136,5 +137,7 @@ def test_rl_aug_01_augment_inner_exactly_eight_outputs(
labels_path=str(proc_lbl),
labels=labels,
)
# Act
out = aug.augment_inner(img_ann)
# Assert
assert len(out) == 8
+19 -36
View File
@@ -1,6 +1,4 @@
import shutil
import sys
import types
from os import path as osp
from pathlib import Path
@@ -9,40 +7,6 @@ import pytest
import constants as c_mod
def _stub_train_dependencies():
if getattr(_stub_train_dependencies, "_done", False):
return
def add_mod(name):
if name in sys.modules:
return sys.modules[name]
m = types.ModuleType(name)
sys.modules[name] = m
return m
ultra = add_mod("ultralytics")
class YOLO:
pass
ultra.YOLO = YOLO
def fake_client(*_a, **_k):
return types.SimpleNamespace(
upload_fileobj=lambda *_a, **_k: None,
download_file=lambda *_a, **_k: None,
)
boto = add_mod("boto3")
boto.client = fake_client
add_mod("netron")
add_mod("requests")
_stub_train_dependencies._done = True
_stub_train_dependencies()
def _prepare_form_dataset(
monkeypatch,
tmp_path,
@@ -84,6 +48,7 @@ def test_bt_dsf_01_split_ratio_70_20_10(
fixture_images_dir,
fixture_labels_dir,
):
# Arrange
train, today_ds = _prepare_form_dataset(
monkeypatch,
tmp_path,
@@ -93,7 +58,9 @@ def test_bt_dsf_01_split_ratio_70_20_10(
100,
set(),
)
# Act
train.form_dataset()
# Assert
assert _count_jpg(Path(today_ds, "train", "images")) == 70
assert _count_jpg(Path(today_ds, "valid", "images")) == 20
assert _count_jpg(Path(today_ds, "test", "images")) == 10
@@ -106,6 +73,7 @@ def test_bt_dsf_02_six_subdirectories(
fixture_images_dir,
fixture_labels_dir,
):
# Arrange
train, today_ds = _prepare_form_dataset(
monkeypatch,
tmp_path,
@@ -115,7 +83,9 @@ def test_bt_dsf_02_six_subdirectories(
100,
set(),
)
# Act
train.form_dataset()
# Assert
base = Path(today_ds)
assert (base / "train" / "images").is_dir()
assert (base / "train" / "labels").is_dir()
@@ -132,6 +102,7 @@ def test_bt_dsf_03_total_files_one_hundred(
fixture_images_dir,
fixture_labels_dir,
):
# Arrange
train, today_ds = _prepare_form_dataset(
monkeypatch,
tmp_path,
@@ -141,7 +112,9 @@ def test_bt_dsf_03_total_files_one_hundred(
100,
set(),
)
# Act
train.form_dataset()
# Assert
n = (
_count_jpg(Path(today_ds, "train", "images"))
+ _count_jpg(Path(today_ds, "valid", "images"))
@@ -157,6 +130,7 @@ def test_bt_dsf_04_corrupted_labels_quarantined(
fixture_images_dir,
fixture_labels_dir,
):
# Arrange
stems = [p.stem for p in sorted(fixture_images_dir.glob("*.jpg"))[:100]]
corrupt = set(stems[:5])
train, today_ds = _prepare_form_dataset(
@@ -168,7 +142,9 @@ def test_bt_dsf_04_corrupted_labels_quarantined(
100,
corrupt,
)
# Act
train.form_dataset()
# Assert
split_total = (
_count_jpg(Path(today_ds, "train", "images"))
+ _count_jpg(Path(today_ds, "valid", "images"))
@@ -187,6 +163,7 @@ def test_rt_dsf_01_empty_processed_no_crash(
fixture_images_dir,
fixture_labels_dir,
):
# Arrange
train, today_ds = _prepare_form_dataset(
monkeypatch,
tmp_path,
@@ -196,12 +173,15 @@ def test_rt_dsf_01_empty_processed_no_crash(
0,
set(),
)
# Act
train.form_dataset()
# Assert
assert Path(today_ds).is_dir()
@pytest.mark.resource_limit
def test_rl_dsf_01_split_ratios_sum_hundred():
# Assert
import train
assert train.train_set + train.valid_set + train.test_set == 100
@@ -215,6 +195,7 @@ def test_rl_dsf_02_no_filename_duplication_across_splits(
fixture_images_dir,
fixture_labels_dir,
):
# Arrange
train, today_ds = _prepare_form_dataset(
monkeypatch,
tmp_path,
@@ -224,7 +205,9 @@ def test_rl_dsf_02_no_filename_duplication_across_splits(
100,
set(),
)
# Act
train.form_dataset()
# Assert
base = Path(today_ds)
names = []
for split in ("train", "valid", "test"):
+10 -9
View File
@@ -1,39 +1,40 @@
import sys
import types
for _name in ("ultralytics", "boto3", "netron", "requests"):
if _name not in sys.modules:
sys.modules[_name] = types.ModuleType(_name)
sys.modules["ultralytics"].YOLO = type("YOLO", (), {})
sys.modules["boto3"].client = lambda *a, **k: None
from train import check_label
def test_bt_lbl_01_valid_label_returns_true(tmp_path):
# Arrange
p = tmp_path / "a.txt"
p.write_text("0 0.5 0.5 0.1 0.1", encoding="utf-8")
# Assert
assert check_label(str(p)) is True
def test_bt_lbl_02_x_gt_one_returns_false(tmp_path):
# Arrange
p = tmp_path / "a.txt"
p.write_text("0 1.5 0.5 0.1 0.1", encoding="utf-8")
# Assert
assert check_label(str(p)) is False
def test_bt_lbl_03_height_gt_one_returns_false(tmp_path):
# Arrange
p = tmp_path / "a.txt"
p.write_text("0 0.5 0.5 0.1 1.2", encoding="utf-8")
# Assert
assert check_label(str(p)) is False
def test_bt_lbl_04_missing_file_returns_false(tmp_path):
# Arrange
p = tmp_path / "missing.txt"
# Assert
assert check_label(str(p)) is False
def test_bt_lbl_05_multiline_one_corrupted_returns_false(tmp_path):
# Arrange
p = tmp_path / "a.txt"
p.write_text("0 0.5 0.5 0.1 0.1\n3 0.5 0.5 0.1 1.5", encoding="utf-8")
# Assert
assert check_label(str(p)) is False
+2 -16
View File
@@ -1,24 +1,10 @@
import sys
import types
import importlib
import shutil
from os import path as osp
from os import path
from pathlib import Path
import pytest
for _n in ("boto3", "netron", "requests"):
if _n not in sys.modules:
sys.modules[_n] = types.ModuleType(_n)
for _k in [k for k in sys.modules if k == "ultralytics" or k.startswith("ultralytics.")]:
del sys.modules[_k]
from ultralytics import YOLO
for _m in ("exports", "train"):
if _m in sys.modules:
importlib.reload(sys.modules[_m])
import constants as c
import train as train_mod
import exports as exports_mod
@@ -56,7 +42,7 @@ def e2e_result(tmp_path_factory):
exports_mod.export_onnx(c.config.current_pt_model)
exports_mod.export_coreml(c.config.current_pt_model)
today_ds = osp.join(c.config.datasets_dir, train_mod.today_folder)
today_ds = path.join(c.config.datasets_dir, train_mod.today_folder)
yield {
"today_dataset": today_ds,