From 321b5143f55b4dcb13acf1416f23a8a17c64b8cf Mon Sep 17 00:00:00 2001 From: duzelis Date: Wed, 6 Mar 2024 14:17:20 +0100 Subject: [PATCH 1/3] feat: add a condition on disparity range for interpolation method --- pandora2d/check_configuration.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/pandora2d/check_configuration.py b/pandora2d/check_configuration.py index 24e65b9..519f8a5 100644 --- a/pandora2d/check_configuration.py +++ b/pandora2d/check_configuration.py @@ -23,6 +23,8 @@ This module contains functions allowing to check the configuration given to Pandora pipeline. """ +from __future__ import annotations + from typing import Dict import numpy as np @@ -86,7 +88,7 @@ def check_input_section(user_cfg: Dict[str, dict], estimation_config: dict = Non check_disparities_from_input(cfg["input"]["row_disparity"], None) else: # add wrong disparity for checking only - cfg = update_conf(default_configuration_disp, user_cfg) + cfg = update_conf(default_configuration_disp, cfg) # check schema configuration_schema = {"input": input_configuration_schema} @@ -183,6 +185,14 @@ def check_conf(user_cfg: Dict, pandora2d_machine: Pandora2DMachine) -> dict: if "matching_cost" in cfg_pipeline["pipeline"]: check_right_nodata_condition(cfg_input, cfg_pipeline) + # The refinement step with interpolation method is not allowed with disparity ranges of size lower than 5 + if ( + "refinement" in cfg_pipeline["pipeline"] + and cfg_pipeline["pipeline"]["refinement"]["refinement_method"] == "interpolation" + ): + check_disparity_range_size(cfg_input["input"]["col_disparity"], "Column") + check_disparity_range_size(cfg_input["input"]["row_disparity"], "Row") + cfg = concat_conf([cfg_input, cfg_roi, cfg_pipeline]) return cfg @@ -205,6 +215,26 @@ def check_right_nodata_condition(cfg_input: Dict, cfg_pipeline: Dict) -> None: ) +def check_disparity_range_size(disparity: list[int] | str, title: str) -> None: + """ + Check that disparity ranges with a size < 5 are not allowed for interpolation refinement method. + + :param disparity: disparity range + :type disparity: list[int] | str + :param cfg_pipeline: pipeline section of configuration + :type cfg_pipeline: Dict + """ + + if isinstance(disparity, list): + if (abs(disparity[1] - disparity[0]) + 1) < 5: + raise ValueError( + title + " disparity range with a size < 5 are not allowed with interpolation refinement method" + ) + + if isinstance(disparity, str): + raise TypeError("Grid disparities are not yet handled by Pandora2D") + + def check_roi_coherence(roi_cfg: dict) -> None: """ Check that the first ROI coords are lower than the last. From d571b128aafcaa41a85ec3c612103c4dba1b16e8 Mon Sep 17 00:00:00 2001 From: duzelis Date: Wed, 6 Mar 2024 14:35:51 +0100 Subject: [PATCH 2/3] test: add tests on disparity range condition for interpolation method --- tests/unit_tests/test_check_configuration.py | 55 ++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/unit_tests/test_check_configuration.py b/tests/unit_tests/test_check_configuration.py index 6e5c4e3..0d30875 100644 --- a/tests/unit_tests/test_check_configuration.py +++ b/tests/unit_tests/test_check_configuration.py @@ -24,6 +24,7 @@ Test configuration """ + import pytest import transitions import numpy as np @@ -260,3 +261,57 @@ def test_zncc_fails_with(self, pandora2d_machine, configuration): """Right nodata must can not be float or nan with zncc matching_cost_method.""" with pytest.raises((ValueError, DictCheckerError)): check_configuration.check_conf(configuration, pandora2d_machine) + + +class TestCheckDisparityRangeSize: + """Test check_disparity_range_size method.""" + + @pytest.mark.parametrize( + ["disparity", "title", "string_match"], + [ + pytest.param( + [-1, 1], + "Column", + "Column disparity range with a size < 5 are not allowed with interpolation refinement method", + id="Column disparity range < 5", + ), + pytest.param( + [-3, -1], + "Row", + "Row disparity range with a size < 5 are not allowed with interpolation refinement method", + id="Row disparity range < 5", + ), + ], + ) + def test_fails_with_disparity_ranges_lower_5(self, disparity, title, string_match): + """Disparity range size must be greater than or equal to 5 when interpolation is used as refinement method""" + with pytest.raises(ValueError, match=string_match): + check_configuration.check_disparity_range_size(disparity, title) + + @pytest.mark.parametrize( + ["disparity", "title", "string_match"], + [ + pytest.param( + "disparity_grid_test", + "Column", + "Grid disparities are not yet handled by Pandora2D", + id="Grid disparity", + ), + ], + ) + def test_fails_with_grid_disparity(self, disparity, title, string_match): + """Disparity grid is not handled yet by Pandora2D""" + with pytest.raises(TypeError, match=string_match): + check_configuration.check_disparity_range_size(disparity, title) + + @pytest.mark.parametrize( + ["disparity", "title"], + [ + pytest.param([-2, 2], "Col", id="Column disparity range greater than or equal to 5"), + pytest.param([1, 5], "Row", id="Row disparity range greater than or equal to 5"), + ], + ) + def test_passes_with_disparity_ranges_equal_5(self, disparity, title): + """Disparity range size is correct""" + + check_configuration.check_disparity_range_size(disparity, title) From 80de60191c58b152518c604570ff1e82f4c29110 Mon Sep 17 00:00:00 2001 From: duzelis Date: Wed, 6 Mar 2024 14:37:06 +0100 Subject: [PATCH 3/3] doc: add a warning about disparity range condition for interpolation method --- docs/source/userguide/input.rst | 4 ++++ docs/source/userguide/step_by_step/refinement.rst | 3 +++ 2 files changed, 7 insertions(+) diff --git a/docs/source/userguide/input.rst b/docs/source/userguide/input.rst index 96d1502..cfe6594 100644 --- a/docs/source/userguide/input.rst +++ b/docs/source/userguide/input.rst @@ -40,6 +40,10 @@ Input section is composed of the following keys: - - If the estimation step is not present +.. warning:: + If interpolation is used as refinement method, row_disparity and col_disparity ranges must have a size greater than or equal to 5. + + Left and Right properties are composed of the following keys: .. list-table:: Left and Right properties diff --git a/docs/source/userguide/step_by_step/refinement.rst b/docs/source/userguide/step_by_step/refinement.rst index 6f321e6..53467d1 100644 --- a/docs/source/userguide/step_by_step/refinement.rst +++ b/docs/source/userguide/step_by_step/refinement.rst @@ -13,6 +13,9 @@ It consists on 3 different steps: * The cost map of each pixel is interpolated using scipy to obtain a continuous function. * Then, the interpolated functions are minimized using scipy to obtain the refined disparities. +.. warning:: + When using the interpolation method, row and column disparity ranges must have a size greater than or equal to 5. + Optical_flow method ------------------- .. warning::