Skip to content

Commit

Permalink
CI: Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-braun committed Oct 18, 2024
1 parent 56578d2 commit d52ae8d
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- FIX: Fix band retrieving when the env variable `CI_EOREADER_BAND_FOLDER` is set, in case of multiple files of the same band from different satellite data are present in the directory
- FIX: Don't force set `remove_tmp` to `True` for `eoreader.Product` in `Mosaic`.
- CI: Add more tests

## 0.2.3 (2024-10-16)

Expand Down
17 changes: 17 additions & 0 deletions ci/scripts_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ def pair_folder() -> AnyPathType:
return get_ci_db_dir() / "PAIR"


def get_ci_data_dir() -> AnyPathType:
"""
Get CI DATA directory (S3 bucket)
Returns:
AnyPathType: CI DATA directory
"""
if len(os.getenv(ci.AWS_ACCESS_KEY_ID, "")) > 0:
return get_ci_db_dir().joinpath("CI_DATA")
else:
return get_ci_dir().joinpath("CI_DATA")


def compare_geom(geom_type: str, obj: Any, obj_folder: AnyPathType, on_disk: bool):
# Check extent
geom_out = obj.output / f"{geom_type}.geojson"
Expand All @@ -125,3 +137,8 @@ def compare_geom(geom_type: str, obj: Any, obj_folder: AnyPathType, on_disk: boo

def s3_env(*args, **kwargs):
return unistra.s3_env(use_s3_env_var=CI_EOSETS_S3, *args, **kwargs)


def get_copdem_30():
dem_sub_dir_path = ["GLOBAL", "COPDEM_30m", "COPDEM_30m.vrt"]
return str(get_db_dir().joinpath(*dem_sub_dir_path))
114 changes: 58 additions & 56 deletions ci/test_mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ci.scripts_utils import (
compare_geom,
data_folder,
get_db_dir,
get_copdem_30,
mosaic_folder,
s3_env,
)
Expand All @@ -27,67 +27,69 @@
@s3_env
def test_s2_mosaic():
"""Test mosaic object with Sentinel-2 products"""
dem_sub_dir_path = ["GLOBAL", "COPDEM_30m", "COPDEM_30m.vrt"]
os.environ[DEM_PATH] = str(get_db_dir().joinpath(*dem_sub_dir_path))

# Get some Sentinel-2 paths
s2_32umu = (
data_folder()
/ "S2B_MSIL2A_20220330T102619_N0400_R108_T32UMU_20220330T141833.SAFE"
)
s2_32ulu = (
data_folder()
/ "S2B_MSIL2A_20220228T102849_N0400_R108_T32ULU_20220228T134712.SAFE"
)
s2_32ulv = (
data_folder()
/ "S2B_MSIL2A_20220228T102849_N0400_R108_T32ULV_20220228T134712.SAFE"
)

with tempfile.TemporaryDirectory() as output:
if ON_DISK:
output = r"/mnt/ds2_db3/CI/eosets/MOSAIC"
with tempenv.TemporaryEnvironment(
{
DEM_PATH: get_copdem_30(),
}
):
# Get some Sentinel-2 paths
s2_32umu = (
data_folder()
/ "S2B_MSIL2A_20220330T102619_N0400_R108_T32UMU_20220330T141833.SAFE"
)
s2_32ulu = (
data_folder()
/ "S2B_MSIL2A_20220228T102849_N0400_R108_T32ULU_20220228T134712.SAFE"
)
s2_32ulv = (
data_folder()
/ "S2B_MSIL2A_20220228T102849_N0400_R108_T32ULV_20220228T134712.SAFE"
)

# First try with incompatible products
with pytest.raises(IncompatibleProducts):
mosaic = Mosaic(
[s2_32umu, s2_32ulu], output_path=output, mosaic_method="VRT"
with tempfile.TemporaryDirectory() as output:
if ON_DISK:
output = r"/mnt/ds2_db3/CI/eosets/MOSAIC"

# First try with incompatible products
with pytest.raises(IncompatibleProducts):
mosaic = Mosaic(
[s2_32umu, s2_32ulu], output_path=output, mosaic_method="VRT"
)

# Create object
mosaic = Mosaic([s2_32ulv, s2_32ulu], mosaic_method="VRT")
mosaic.output = os.path.join(output, mosaic.condensed_name)

# Check extent
compare_geom("extent", mosaic, mosaic_folder(), ON_DISK)

# Check footprint
compare_geom("footprint", mosaic, mosaic_folder(), ON_DISK)

# Stack with a pixel_size of 600m
mosaic_out = mosaic.output / "red_stack.tif"
assert mosaic.has_bands(RED)
mosaic.stack(
[RED],
stack_path=mosaic_out,
pixel_size=600,
)

# Create object
mosaic = Mosaic([s2_32ulv, s2_32ulu], mosaic_method="VRT")
mosaic.output = os.path.join(output, mosaic.condensed_name)

# Check extent
compare_geom("extent", mosaic, mosaic_folder(), ON_DISK)

# Check footprint
compare_geom("footprint", mosaic, mosaic_folder(), ON_DISK)

# Stack with a pixel_size of 600m
mosaic_out = mosaic.output / "red_stack.tif"
assert mosaic.has_bands(RED)
mosaic.stack(
[RED],
stack_path=mosaic_out,
pixel_size=600,
)

# Test it
if ON_DISK:
ci_path = mosaic_out
else:
ci_path = mosaic_folder() / mosaic.condensed_name / mosaic_out.name
# Test it
if ON_DISK:
ci_path = mosaic_out
else:
ci_path = mosaic_folder() / mosaic.condensed_name / mosaic_out.name

ci.assert_raster_equal(mosaic_out, ci_path)
ci.assert_raster_equal(mosaic_out, ci_path)

# Not implemented
with pytest.raises(NotImplementedError):
mosaic.read_mtd()
# Not implemented
with pytest.raises(NotImplementedError):
mosaic.read_mtd()

# Clean everything
mosaic.clear()
mosaic.clean_tmp()
# Clean everything
mosaic.clear()
mosaic.clean_tmp()


@s3_env
Expand Down
78 changes: 74 additions & 4 deletions ci/test_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,63 @@
import tempfile

import pytest
from eoreader.bands import RED
from eoreader.bands import BLUE, GREEN, NIR, PAN, RED
from eoreader.env_vars import CI_EOREADER_BAND_FOLDER, DEM_PATH
from sertit import ci

from ci.scripts_utils import compare_geom, data_folder, pair_folder, s3_env
from tempenv import tempenv

from ci.scripts_utils import (
compare_geom,
data_folder,
get_ci_data_dir,
get_copdem_30,
pair_folder,
s3_env,
)
from eosets.exceptions import IncompatibleProducts
from eosets.pair import Pair

ci.reduce_verbosity()

ON_DISK = False
ON_DISK = True


@s3_env
def test_pair_non_ortho_with_window():
"""Test pair non-ortho native with a window"""
# DO NOT REPROJECT BANDS --> WAY TOO SLOW
with tempenv.TemporaryEnvironment(
{DEM_PATH: get_copdem_30(), CI_EOREADER_BAND_FOLDER: str(get_ci_data_dir())}
):
aoi = data_folder() / "psh_pld_aoi.shp"
pld_paths = {
"reference_paths": [data_folder() / "IMG_PHR1A_P_001"],
"secondary_paths": [data_folder() / "IMG_PHR1A_MS_004"],
}

with tempfile.TemporaryDirectory() as output:
if ON_DISK:
output = r"/mnt/ds2_db3/CI/eosets/PAIR"

pld_pair = Pair(**pld_paths, window=aoi, remove_tmp=not ON_DISK)
pld_pair.output = os.path.join(output, pld_pair.condensed_name)

ms_path = pld_pair.output / "rgbn_stack.tif"
pan_path = pld_pair.output / "pan_stack.tif"

# RGBN
rgbn_stck = pld_pair.secondary_mosaic.stack(
[RED, GREEN, BLUE, NIR], stack_path=ms_path, window=aoi
)
ci.assert_val(rgbn_stck.rio.resolution()[0], 2.0, "RGBN resolution")
ci.assert_val(rgbn_stck.rio.count, 4, "RGBN number of bands")

# PAN
pan_stck = pld_pair.reference_mosaic.stack(
[PAN], stack_path=pan_path, window=aoi
)
ci.assert_val(pan_stck.rio.resolution()[0], 0.5, "PAN resolution")
ci.assert_val(pan_stck.rio.count, 1, "PAN number of bands")


def _test_pair_core(paths: dict) -> None:
Expand Down Expand Up @@ -154,3 +201,26 @@ def test_pair_fail():
# Fails with not overlapping products
with pytest.raises(IncompatibleProducts):
Pair(**paths)


# @s3_env
# def test_pair_multi_res():
# """ Test mosaic multi res with a window """
# # DO NOT REPROJECT BANDS --> WAY TOO SLOW
# with tempenv.TemporaryEnvironment(
# {
# CI_EOREADER_BAND_FOLDER: str(get_ci_data_dir())
# }
# ):
# l9_paths = {
# "reference_paths": [
# data_folder() / "LC09_L1TP_200030_20220201_20220201_02_T1.tar",
# ],
# }
#
# with tempfile.TemporaryDirectory() as output:
# if ON_DISK:
# output = r"/mnt/ds2_db3/CI/eosets/PAIR"
#
# l9_pair = Pair(**l9_paths, remove_tmp=not ON_DISK)
# l9_pair.output = os.path.join(output, l9_pair.condensed_name)

0 comments on commit d52ae8d

Please sign in to comment.