Skip to content

Commit

Permalink
added tests for config
Browse files Browse the repository at this point in the history
  • Loading branch information
franioli committed Jan 26, 2024
1 parent 200ff9b commit 513e74b
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 26 deletions.
12 changes: 9 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
import pytest


# Create a temporary directory with the test images for each test
# # Create a temporary directory with the test images for each test
@pytest.fixture
def data_dir():
def data_dir(request):
assets = (Path(__file__).parents[0].parents[0] / "assets/pytest/images").resolve()

tempdir = tempfile.mkdtemp()
shutil.copytree(assets, Path(tempdir) / "images")

def cleanup():
# Cleanup code: remove the temporary directory and its contents
shutil.rmtree(tempdir)

# Add finalizer to the fixture
request.addfinalizer(cleanup)

return tempdir
96 changes: 96 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from pathlib import Path

import pytest
import yaml
from deep_image_matching import GeometricVerification, Quality, TileSelection
from deep_image_matching.config import Config


def create_config_file(config: dict, path: Path) -> Path:
def tuple_representer(dumper, data):
return dumper.represent_sequence("tag:yaml.org,2002:seq", data, flow_style=True)

yaml.add_representer(tuple, tuple_representer)

with open(path, "w") as f:
yaml.dump(config, f)
return Path(path)


# Config object is created successfully with valid input arguments
def test_valid_basic_arguments(data_dir):
cfg = {
"extractor": {
"name": "superpoint",
"max_keypoints": 20000,
}
}
config_file = create_config_file(cfg, Path(data_dir) / "temp.yaml")

args = {
"gui": False,
"dir": data_dir,
"pipeline": "superpoint+lightglue",
"config_file": config_file,
"quality": "high",
"tiling": "preselection",
"strategy": "matching_lowres",
"upright": True,
"skip_reconstruction": False,
"force": True,
"verbose": False,
}
config = Config(args)

assert isinstance(config, Config)

# Check that config dictionary contains at least the specified keys with the correct values
expected_general = {
"quality": Quality.HIGH,
"tile_selection": TileSelection.PRESELECTION,
"tile_size": (2400, 2000),
"tile_overlap": 10,
"tile_preselection_size": 1000,
"min_matches_per_tile": 10,
"geom_verification": GeometricVerification.PYDEGENSAC,
"gv_threshold": 4,
"gv_confidence": 0.99999,
"min_inliers_per_pair": 15,
"min_inlier_ratio_per_pair": 0.25,
"try_match_full_images": False,
}
assert all(
key in config.general and config.general[key] == expected_general[key]
for key in expected_general
)

expected_extractor = {
"name": "superpoint",
"nms_radius": 3,
"keypoint_threshold": 0.0005,
"max_keypoints": 20000,
}
assert all(
key in config.extractor and config.extractor[key] == expected_extractor[key]
for key in expected_extractor
)

expected_matcher = {
"name": "lightglue",
"n_layers": 9,
"mp": False,
"flash": True,
"depth_confidence": 0.95,
"width_confidence": 0.99,
"filter_threshold": 0.1,
}
assert all(
key in config.matcher and config.matcher[key] == expected_matcher[key]
for key in expected_matcher
)


if __name__ == "__main__":
import pytest

pytest.main([__file__])
33 changes: 10 additions & 23 deletions tests/test_pipelines.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import platform
import subprocess
import tempfile
from pathlib import Path

import pytest
Expand All @@ -13,10 +12,11 @@ def script():
return (Path(__file__).parents[1] / "main.py").resolve()


# # Create a temporary directory with the test images for each test
# @pytest.fixture
# def data_dir():
# return (Path(__file__).parents[0].parents[0] / "assets/pytest").resolve()
@pytest.fixture
def config_file_tiling(data_dir):
config = {"general": {"tile_size": (200, 200)}}
config_file = Path(data_dir) / "temp.yaml"
return create_config_file(config, config_file)


def run_pipeline(cmd, verbose: bool = False) -> None:
Expand All @@ -35,20 +35,15 @@ def run_pipeline(cmd, verbose: bool = False) -> None:
), f"Script execution failed with error: {stderr.decode('utf-8')}"


def create_config_file(config: dict, path: Path, temporary: bool = False) -> Path:
def create_config_file(config: dict, path: Path) -> Path:
def tuple_representer(dumper, data):
return dumper.represent_sequence("tag:yaml.org,2002:seq", data, flow_style=True)

yaml.add_representer(tuple, tuple_representer)

if temporary:
with tempfile.NamedTemporaryFile("w", suffix=".yaml", delete=False) as file:
yaml.dump(config, file)
return Path(file.name)
else:
with open(path, "w") as f:
yaml.dump(config, f)
return Path(path)
with open(path, "w") as f:
yaml.dump(config, f)
return Path(path)


# Test matching strategies
Expand Down Expand Up @@ -79,7 +74,7 @@ def test_sp_lg_custom_config(data_dir, script):
}
}
config_file = Path(__file__).parents[1] / "temp.yaml"
config_file = create_config_file(config, config_file, temporary=False)
config_file = create_config_file(config, config_file)
run_pipeline(
f"python {script} --dir {data_dir} --pipeline superpoint+lightglue --config_file {config_file} --strategy sequential --overlap 1 --skip_reconstruction --force"
)
Expand Down Expand Up @@ -136,14 +131,6 @@ def test_sp_lg_quality_medium(data_dir, script):


# Test tiling
@pytest.fixture
def config_file_tiling():
config = {"general": {"tile_size": (200, 200)}}
config_file = Path(__file__).parents[1] / "temp.yaml"
config_file = create_config_file(config, config_file, temporary=False)
return config_file


def test_tiling_preselection(data_dir, script, config_file_tiling):
run_pipeline(
f"python {script} --dir {data_dir} --pipeline superpoint+lightglue --strategy bruteforce --tiling preselection --config {config_file_tiling} --skip_reconstruction --force",
Expand Down

0 comments on commit 513e74b

Please sign in to comment.