Skip to content

Commit

Permalink
test: update cli test for coregistration
Browse files Browse the repository at this point in the history
  • Loading branch information
vschaffn committed Nov 22, 2024
1 parent c8246ee commit 6602bab
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from setuptools import find_packages, setup


setup(
name="xdem",
use_scm_version=True, # Enable versioning with setuptools_scm
Expand Down
35 changes: 32 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
"""Function to test the CLI"""

import os
import subprocess

import rasterio

import xdem
from xdem import dem_coregistration


class TestCLI:
# Define paths to the DEM files using xDEM examples
ref_dem_path = xdem.examples.get_path("longyearbyen_ref_dem")
tba_dem_path = xdem.examples.get_path("longyearbyen_tba_dem")
aligned_dem_path = "aligned_dem.tiff"
inlier_mask_path = "inlier_mask.tiff"

def test_xdem_cli(self) -> None:
def test_xdem_cli_coreg(self) -> None:
try:
# Run the xDEM CLI command with the reference and secondary DEM files
result = subprocess.run(
["xdem", self.ref_dem_path, self.tba_dem_path],
["xdem", "coregister", self.ref_dem_path, self.tba_dem_path],
capture_output=True,
text=True,
)
assert "hello world" in result.stdout

# Assert ClI ran successfully
assert result.returncode == 0

# Verify the existence of the output files
assert os.path.exists(self.aligned_dem_path), f"Aligned DEM not found: {self.aligned_dem_path}"
assert os.path.exists(self.inlier_mask_path), f"Inlier mask not found: {self.inlier_mask_path}"

# Retrieve ground truth
true_coreg_dem, coreg_method, out_stats, true_inlier_mask = dem_coregistration(
xdem.DEM(self.tba_dem_path), xdem.DEM(self.ref_dem_path), self.aligned_dem_path
)

# Load elements processed by the xDEM CLI command
aligned_dem = xdem.DEM(self.aligned_dem_path)
with rasterio.open(self.inlier_mask_path) as src:
inlier_mask = src.read(1)

# Verify match with ground truth
assert aligned_dem == true_coreg_dem, "Aligned DEM does not match the ground truth."
assert inlier_mask.all() == true_inlier_mask.all(), "Inlier mask does not match the ground truth."

# Erase files
os.remove(self.aligned_dem_path)
os.remove(self.inlier_mask_path)

except FileNotFoundError as e:
# In case 'xdem' is not found
raise AssertionError(f"CLI command 'xdem' not found : {e}")
Expand Down
18 changes: 18 additions & 0 deletions xdem/xdem_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# Copyright (c) 2024 xDEM developers
#
# This file is part of the xDEM project:
# https://github.com/glaciohack/xdem
#
# 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.

# Copyright (c) 2024 xDEM developers
#
# This file is part of xDEM project:
Expand Down

0 comments on commit 6602bab

Please sign in to comment.