Skip to content

Commit

Permalink
Merge branch '231-performance-tests-mi' into 'release'
Browse files Browse the repository at this point in the history
Resolve "IM: Test de performance"

See merge request 3d/PandoraBox/pandora2d!195
  • Loading branch information
PhML committed Jan 29, 2025
2 parents b59c084 + 74b4a23 commit 9ad4eb4
Show file tree
Hide file tree
Showing 83 changed files with 273 additions and 99 deletions.
18 changes: 18 additions & 0 deletions tests/functional_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,21 @@ def correct_pipeline_with_dichotomy_cpp():
"refinement": {"refinement_method": "dichotomy", "iterations": 2, "filter": {"method": "bicubic"}},
}
}


@pytest.fixture()
def correct_input_for_functional_tests(left_img_path, right_img_path, col_disparity, row_disparity):
return {
"input": {
"left": {
"img": str(left_img_path),
"nodata": "NaN",
},
"right": {
"img": str(right_img_path),
"nodata": "NaN",
},
"col_disparity": col_disparity,
"row_disparity": row_disparity,
}
}
20 changes: 3 additions & 17 deletions tests/functional_tests/matching_cost/test_mutual_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ class TestMutualInformation:
"""

@pytest.fixture()
def make_cfg_for_mutual_information( # pylint: disable=too-many-arguments
def make_cfg_for_mutual_information(
self,
left_img_path,
right_img_path,
col_disparity,
row_disparity,
correct_input_for_functional_tests,
window_size,
subpix,
step,
Expand All @@ -55,18 +52,7 @@ def make_cfg_for_mutual_information( # pylint: disable=too-many-arguments
"""

user_cfg = {
"input": {
"left": {
"img": str(left_img_path),
"nodata": "NaN",
},
"right": {
"img": str(right_img_path),
"nodata": "NaN",
},
"col_disparity": col_disparity,
"row_disparity": row_disparity,
},
**correct_input_for_functional_tests,
"ROI": roi,
"pipeline": {
"matching_cost": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
Test the refinement.dichotomy pipeline.
"""

# Make pylint happy with fixtures:
# pylint: disable=too-many-positional-arguments

import copy
import pytest

Expand All @@ -38,35 +35,21 @@


@pytest.fixture()
def make_cfg_for_dichotomy( # pylint: disable=too-many-arguments
left_img_path,
right_img_path,
def make_cfg_for_dichotomy(
correct_input_for_functional_tests,
dicho_method,
filter_method,
subpix,
step,
iterations,
roi,
col_disparity,
row_disparity,
):
"""
Creates user configuration to test dichotomy loop
"""

user_cfg = {
"input": {
"left": {
"img": str(left_img_path),
"nodata": "NaN",
},
"right": {
"img": str(right_img_path),
"nodata": "NaN",
},
"col_disparity": col_disparity,
"row_disparity": row_disparity,
},
**correct_input_for_functional_tests,
"ROI": roi,
"pipeline": {
"matching_cost": {
Expand Down
1 change: 0 additions & 1 deletion tests/functional_tests/test_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"""

# pylint: disable=redefined-outer-name
# pylint: disable=too-many-positional-arguments

import json
from copy import deepcopy
Expand Down
18 changes: 18 additions & 0 deletions tests/performance_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2025 Centre National d'Etudes Spatiales (CNES).
#
# This file is part of PANDORA2D
#
# https://github.com/CNES/Pandora2D
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
88 changes: 88 additions & 0 deletions tests/performance_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Copyright (c) 2025 Centre National d'Etudes Spatiales (CNES).
#
# This file is part of PANDORA2D
#
# https://github.com/CNES/Pandora2D
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Module with global performance test fixtures and methods."""

# Make pylint happy with fixtures:
# pylint: disable=redefined-outer-name

from typing import Tuple
from pathlib import Path

import pytest

import numpy as np

from numpy.typing import NDArray


@pytest.fixture()
def remove_edges():
"""
Remove medicis disparity maps edges
"""

def inner(
medicis_map: NDArray[np.floating], pandora2d_map: NDArray[np.floating]
) -> Tuple[NDArray[np.floating], NDArray[np.floating]]:
"""
Get reduced disparity maps after removing medicis edges full of nans (greater than pandora2d edges)
on both pandora2d and medicis disparity maps.
"""

# Gets coordinates for which medicis col_map is different from nan
# i.e. points that are not within the edges
non_nan_row_indexes, non_nan_col_indexes = np.where(~np.isnan(medicis_map))

# Remove medicis edges
medicis_map = medicis_map[
non_nan_row_indexes[0] : non_nan_row_indexes[-1] + 1, non_nan_col_indexes[0] : non_nan_col_indexes[-1] + 1
]

# Remove pandora2d edges to get the same points as the ones in medicis disparity maps
pandora2d_map = pandora2d_map[
non_nan_row_indexes[0] : non_nan_row_indexes[-1] + 1, non_nan_col_indexes[0] : non_nan_col_indexes[-1] + 1
]

return medicis_map, pandora2d_map

return inner


@pytest.fixture()
def data_path():
"""
Return path to get left and right images and medicis data
"""
return Path("tests/performance_tests/data_medicis/")


@pytest.fixture()
def shift_path(data_path, img_path):
"""
Return path to get left and right images and medicis data
"""
return data_path / img_path


@pytest.fixture()
def medicis_maps_path(shift_path, medicis_method_path):
"""
Return path to get medicis data
"""
return shift_path / medicis_method_path
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Copyright (c) 2025 Centre National d'Etudes Spatiales (CNES).
#
# This file is part of PANDORA2D
#
# https://github.com/CNES/Pandora2D
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Test the refinement.dichotomy pipeline.
"""

import pytest
import numpy as np
import rasterio


class TestComparisonMedicis:
"""
Test that pandora2d disparity maps are equal or close to the medicis ones
when matching cost is used with mutual information method.
"""

@pytest.fixture()
def cfg_mutual_information(self, shift_path, subpix):
"""
Make user configuration for mutual information computation
"""

return {
"input": {
"left": {"nodata": -9999, "img": str(shift_path / "left.tif")},
"right": {"nodata": -9999, "img": str(shift_path / "right.tif")},
"col_disparity": {"init": 0, "range": 3},
"row_disparity": {"init": 0, "range": 3},
},
"pipeline": {
"matching_cost": {
"matching_cost_method": "mutual_information",
"window_size": 65,
"step": [1, 1],
"subpix": subpix,
},
"disparity": {"disparity_method": "wta", "invalid_disparity": np.nan},
},
}

@pytest.mark.parametrize(
[
"img_path",
"subpix",
"medicis_method_path",
],
[
pytest.param(
"T19KER/r+0.00c+0.50/",
2,
"mi/gri_resultat_",
id="T19KER (Calama, Chile) shifted of 0.5 in columns with subpix=2",
),
pytest.param(
"T50JML/r+0.00c+0.50/",
2,
"mi/gri_resultat_",
id="T50JML (Perth, Australia) shifted of 0.5 in columns with subpix=2",
),
pytest.param(
"T19KER/r+0.00c-0.25/",
4,
"mi/gri_resultat_",
id="T19KER (Calama, Chile) shifted of -0.25 in columns with subpix=4",
),
pytest.param(
"T50JML/r+0.00c-0.25/",
4,
"mi/gri_resultat_",
id="T50JML (Perth, Australia) shifted of -0.25 in columns with subpix=4",
),
],
)
def test_pandora2d_medicis_mutual_information(
self, run_pipeline, remove_edges, cfg_mutual_information, medicis_maps_path
):
"""
Compute mean errors of medicis and pandora2d disparity maps
"""

# Run pandora2D pipeline
run_dir = run_pipeline(cfg_mutual_information)

# Get pandora2d disparity maps
with rasterio.open(run_dir / "output" / "row_map.tif") as src:
row_map_pandora2d = src.read(1)
with rasterio.open(run_dir / "output" / "col_map.tif") as src:
col_map_pandora2d = src.read(1)

# Get medicis disparity maps
with rasterio.open(str(medicis_maps_path) + "row_disp.tif") as src:
row_map_medicis = src.read(1)
with rasterio.open(str(medicis_maps_path) + "col_disp.tif") as src:
col_map_medicis = src.read(1)

# Remove medicis edges on both pandora2d and medicis disparity maps
# in order to compare the same sample of points.
row_map_medicis, row_map_pandora2d = remove_edges(row_map_medicis, row_map_pandora2d)
col_map_medicis, col_map_pandora2d = remove_edges(col_map_medicis, col_map_pandora2d)

np.testing.assert_array_equal(row_map_medicis, row_map_pandora2d)
np.testing.assert_array_equal(col_map_medicis, col_map_pandora2d)
Loading

0 comments on commit 9ad4eb4

Please sign in to comment.