Skip to content

Commit

Permalink
feat: check that disparity ranges are inside images
Browse files Browse the repository at this point in the history
If all disparities in disparity ranges of the input section of the
configuration are outside the image, the check_configuration raises an
error.

Refs: #111
  • Loading branch information
PhML committed Apr 12, 2024
1 parent b5cf4c1 commit 2c2d5bb
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 5 deletions.
39 changes: 34 additions & 5 deletions pandora2d/check_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@

from __future__ import annotations

from typing import Dict
from typing import Dict, List

import numpy as np
import xarray as xr
from json_checker import And, Checker, Or

from pandora.img_tools import get_metadata
from pandora.check_configuration import (
check_dataset,
check_disparities_from_input,
Expand Down Expand Up @@ -83,10 +85,7 @@ def check_input_section(user_cfg: Dict[str, dict], estimation_config: dict = Non
cfg = update_conf(default_short_configuration_input, user_cfg)

# test disparities
if estimation_config is None:
check_disparities_from_input(cfg["input"]["col_disparity"], None)
check_disparities_from_input(cfg["input"]["row_disparity"], None)
else:
if estimation_config is not None:
# add wrong disparity for checking only
cfg = update_conf(default_configuration_disp, cfg)

Expand All @@ -98,9 +97,39 @@ def check_input_section(user_cfg: Dict[str, dict], estimation_config: dict = Non
# test images
check_images(cfg["input"])

if estimation_config is None:
check_disparities_from_input(cfg["input"]["col_disparity"], None)
check_disparities_from_input(cfg["input"]["row_disparity"], None)
left_image_metadata = get_metadata(cfg["input"]["left"]["img"])
check_disparity_ranges_are_inside_image(
left_image_metadata, cfg["input"]["row_disparity"], cfg["input"]["col_disparity"]
)

return cfg


def check_disparity_ranges_are_inside_image(
image_metadata: xr.Dataset, row_disparity_range: List, col_disparity_range: List
) -> None:
"""
Raise an error if disparity ranges are out off image.
:param image_metadata:
:type image_metadata: xr.Dataset
:param row_disparity_range:
:type row_disparity_range: List
:param col_disparity_range:
:type col_disparity_range: List
:return: None
:rtype: None
:raises: ValueError
"""
if np.abs(row_disparity_range).min() > image_metadata.sizes["row"]:
raise ValueError("Row disparity range out of image")
if np.abs(col_disparity_range).min() > image_metadata.sizes["col"]:
raise ValueError("Column disparity range out of image")


def check_roi_section(user_cfg: Dict[str, dict]) -> Dict[str, dict]:
"""
Complete and check if the dictionary is correct
Expand Down
76 changes: 76 additions & 0 deletions tests/unit_tests/test_check_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import numpy as np
import xarray as xr
from json_checker import DictCheckerError, MissKeyCheckerError
from skimage.io import imsave

from pandora2d.img_tools import create_datasets_from_inputs, add_disparity_grid
from pandora2d import check_configuration
Expand Down Expand Up @@ -354,3 +355,78 @@ def test_passes_with_disparity_ranges_equal_5(self, disparity, title):
"""Disparity range size is correct"""

check_configuration.check_disparity_range_size(disparity, title)


class TestDisparityRangeAgainstImageSize:
"""Test that out of image disparity ranges are not allowed."""

@pytest.fixture()
def image_path(self, tmp_path):
path = tmp_path / "tiff_file.tif"
imsave(path, np.empty((450, 450)))
return path

@pytest.fixture()
def row_disparity(self):
return [-4, 1]

@pytest.fixture()
def col_disparity(self):
return [-3, 2]

@pytest.fixture()
def configuration(self, image_path, row_disparity, col_disparity):
return {
"input": {
"left": {
"img": str(image_path),
"nodata": "NaN",
},
"right": {
"img": str(image_path),
"nodata": "NaN",
},
"row_disparity": row_disparity,
"col_disparity": col_disparity,
},
"pipeline": {
"matching_cost": {"matching_cost_method": "zncc", "window_size": 1},
},
}

@pytest.mark.parametrize(
"row_disparity",
[
pytest.param([-460, -451], id="Out on left"),
pytest.param([451, 460], id="Out on right"),
],
)
def test_row_disparity_totally_out(self, pandora2d_machine, configuration):
"""Totally out disparities should raise an error."""
with pytest.raises(ValueError, match="Row disparity range out of image"):
check_configuration.check_conf(configuration, pandora2d_machine)

@pytest.mark.parametrize(
"col_disparity",
[
pytest.param([-460, -451], id="Out on top"),
pytest.param([451, 460], id="Out on bottom"),
],
)
def test_column_disparity_totally_out(self, pandora2d_machine, configuration):
"""Totally out disparities should raise an error."""
with pytest.raises(ValueError, match="Column disparity range out of image"):
check_configuration.check_conf(configuration, pandora2d_machine)

@pytest.mark.parametrize(
["row_disparity", "col_disparity"],
[
pytest.param([-460, -450], [100, 200], id="Partially out on left"),
pytest.param([450, 460], [100, 200], id="Partially out on right"),
pytest.param([100, 200], [-460, -450], id="Partially out on top"),
pytest.param([100, 200], [450, 460], id="Partially out on bottom"),
],
)
def test_disparity_partially_out(self, pandora2d_machine, configuration):
"""Partially out should not raise error."""
check_configuration.check_conf(configuration, pandora2d_machine)

0 comments on commit 2c2d5bb

Please sign in to comment.