Skip to content

Commit

Permalink
Update tests (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
alekfal committed Jan 31, 2024
1 parent cbc1d39 commit e022592
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 146 deletions.
13 changes: 6 additions & 7 deletions tests/test_LandCover.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@
from cropmaps.sts import sentimeseries
import rasterio

for directory, _, _ in os.walk("./data"):
for directory, _, _ in os.walk(os.path.join(os.path.dirname(__file__), "data")):
for file in os.listdir(directory):
if file.endswith(".tif"):
os.remove(os.path.join(directory, file))
if file.endswith(".tif.aux.xml"):
os.remove(os.path.join(directory, file))

if not os.path.exists(os.path.join(os.path.dirname(__file__), "data/eodata_local")):
os.makedirs(os.path.join(os.path.dirname(__file__), "data/eodata_local"))

if not os.path.exists("./data/eodata_local"):
os.makedirs("./data/eodata_local")

landcover_data = "./data/LandCover"
landcover_data = os.path.join(os.path.dirname(__file__), "data/LandCover")

search_params = [(landcover_data, None, "LC_mosaic.tif", do_not_raise()),
(None, None, "LC_mosaic.tif", pytest.raises(TypeError)),
(landcover_data, "./data/AOI/AOI.geojson", "LC_mosaic.tif", do_not_raise()),
(landcover_data, os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), "LC_mosaic.tif", do_not_raise()),
]

@pytest.mark.parametrize("store, aoi, outname, exception", search_params)
def test_LandCover(store, aoi, outname, exception):
with exception:
eodata = sentimeseries("S2-timeseries")
eodata.find("./data/eodata")
eodata.find(os.path.join(os.path.dirname(__file__), "data/eodata"))
landcover_image = eodata.LandCover(store, aoi, outname)
src = rasterio.open(landcover_image)
assert src.meta["dtype"] == "uint8"
Expand Down
27 changes: 13 additions & 14 deletions tests/test_apply_SCL.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,34 @@
from cropmaps.exceptions import BandNotFound
import rasterio

for directory, _, _ in os.walk("./data"):
for directory, _, _ in os.walk(os.path.join(os.path.dirname(__file__), "data")):
for file in os.listdir(directory):
if file.endswith(".tif"):
os.remove(os.path.join(directory, file))
if file.endswith(".tif.aux.xml"):
os.remove(os.path.join(directory, file))

if not os.path.exists(os.path.join(os.path.dirname(__file__), "data/eodata_local")):
os.makedirs(os.path.join(os.path.dirname(__file__), "data/eodata_local"))

if not os.path.exists("./data/eodata_local"):
os.makedirs("./data/eodata_local")

search_params = [(None, "NDWI", "./data/eodata_local", "AOI", None, do_not_raise()),
(None, "NDBI", "./data/eodata_local", "AOI", None, do_not_raise()),
(None, "NDVI", "./data/eodata_local", "AOI", None, do_not_raise()),
(None, None, "./data/eodata_local", "AOI", None, do_not_raise()),
search_params = [(None, "NDWI", os.path.join(os.path.dirname(__file__), "data/eodata_local"), "AOI", None, do_not_raise()),
(None, "NDBI", os.path.join(os.path.dirname(__file__), "data/eodata_local"), "AOI", None, do_not_raise()),
(None, "NDVI", os.path.join(os.path.dirname(__file__), "data/eodata_local"), "AOI", None, do_not_raise()),
(None, None, os.path.join(os.path.dirname(__file__), "data/eodata_local"), "AOI", None, do_not_raise()),
(None, None, None, None, None, do_not_raise()),
("./data/eodata/Sentinel-2/L2A/2018/06/27/S2A_MSIL2A_20180627T104021_N0208_R008_T31TEJ_20180627T143337.SAFE/GRANULE/L2A_T31TEJ_A015735_20180627T104837/IMG_DATA/T31TEJ_20180627T104021_B04_10m.jp2", None, None, None, None, pytest.raises(TypeError)),
(None, "NDBI", "./data/eodata_local", "AOI", "highest", pytest.raises(BandNotFound)),
(None, "NDBI", os.path.join(os.path.dirname(__file__), "/data/eodata_local"), "AOI", "highest", pytest.raises(BandNotFound)),
]

@pytest.mark.parametrize("image, band, store, subregion, resolution, exception", search_params)
def test_apply_SCL(image, band, store, subregion, resolution, exception):
with exception:
eodata = sentimeseries("S2-timeseries")
eodata.find("./data/eodata")
eodata.clipbyMask("./data/AOI/AOI.geojson", store = "./data/eodata_local")
eodata.getVI("NDVI", store = "./data/eodata_local", subregion = "AOI") # Subregion is the name of the mask shapefile
eodata.getVI("NDWI", store = "./data/eodata_local", subregion = "AOI") # This is in 10m
eodata.getVI("NDBI", store = "./data/eodata_local", subregion = "AOI") # This is in 20m
eodata.find(os.path.join(os.path.dirname(__file__), "data/eodata"))
eodata.clipbyMask(os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), store = os.path.join(os.path.dirname(__file__), "data/eodata_local"))
eodata.getVI("NDVI", store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), subregion = "AOI") # Subregion is the name of the mask shapefile
eodata.getVI("NDWI", store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), subregion = "AOI") # This is in 10m
eodata.getVI("NDBI", store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), subregion = "AOI") # This is in 20m
eodata.apply_SCL(image, band, store, subregion, resolution)

if subregion is None:
Expand Down
13 changes: 6 additions & 7 deletions tests/test_burn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@
from cropmaps.sts import sentimeseries
from cropmaps.prepare_vector import burn

for directory, _, _ in os.walk("./data"):
for directory, _, _ in os.walk(os.path.join(os.path.dirname(__file__), "data")):
for file in os.listdir(directory):
if file.endswith(".tif"):
os.remove(os.path.join(directory, file))
if file.endswith(".tif.aux.xml"):
os.remove(os.path.join(directory, file))

if not os.path.exists(os.path.join(os.path.dirname(__file__), "data/eodata_local")):
os.makedirs(os.path.join(os.path.dirname(__file__), "data/eodata_local"))

if not os.path.exists("./data/eodata_local"):
os.makedirs("./data/eodata_local")

search_params = [("./data/reference_data/france_data_2018.shp", "EC_hcat_n", None, True, None, "gt.tif", do_not_raise()),
search_params = [(os.path.join(os.path.dirname(__file__), "data/reference_data/france_data_2018.shp"), "EC_hcat_n", None, True, None, "gt.tif", do_not_raise()),
]

@pytest.mark.parametrize("shapefile, classes, classes_id, save_nomenclature, save_to, outfname, exception", search_params)
def test_burn(shapefile, classes, classes_id, save_nomenclature, save_to, outfname, exception):
with exception:
eodata = sentimeseries("S2-timeseries")
eodata.find("./data/eodata")
eodata.find(os.path.join(os.path.dirname(__file__), "data/eodata"))
# To AOI
eodata.clipbyMask("./data/AOI/AOI.geojson", store = "./data/eodata_local")
eodata.clipbyMask(os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), store = os.path.join(os.path.dirname(__file__), "data/eodata_local"))
base = eodata.data[0].B04["10"]["AOI"]
metadata = rasterio.open(base).meta.copy()

Expand Down
25 changes: 12 additions & 13 deletions tests/test_clipper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,34 @@
from contextlib import suppress as do_not_raise
from cropmaps.sts import sentimeseries

for directory, _, _ in os.walk("./data"):
for directory, _, _ in os.walk(os.path.join(os.path.dirname(__file__), "data")):
for file in os.listdir(directory):
if file.endswith(".tif"):
os.remove(os.path.join(directory, file))
if file.endswith(".tif.aux.xml"):
os.remove(os.path.join(directory, file))


if not os.path.exists("./data/eodata_local"):
os.makedirs("./data/eodata_local")
if not os.path.exists(os.path.join(os.path.dirname(__file__), "data/eodata_local")):
os.makedirs(os.path.join(os.path.dirname(__file__), "data/eodata_local"))

eodata = sentimeseries("S2-timeseries")
eodata.find("./data/eodata")
eodata.find(os.path.join(os.path.dirname(__file__), "data/eodata"))
image = eodata.data[0]

search_params = [("./data/AOI/AOI.geojson", False, "B08", False, None, None, "./data/eodata_local", True, do_not_raise()),
("./data/AOI/AOI.geojson", False, "B08", False, None, None, None, True, do_not_raise()),
("./data/AOI/AOI.geojson", False, "B8A", True, None, None, None, True, do_not_raise()),
("./data/AOI/AOI.geojson", True, "B8A", True, None, None, None, True, do_not_raise()),
("./data/AOI/AOI.geojson", False, None, True, None, None, None, True, do_not_raise()),
("./data/AOI/AOI.geojson", False, "B8A", False, None, None, None, True, do_not_raise()),
("./data/AOI/AOI.geojson", False, None, True, None, None, "./data/eodata_local", True, do_not_raise()),
search_params = [(os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), False, "B08", False, None, None, os.path.join(os.path.dirname(__file__), "data/eodata_local"), True, do_not_raise()),
(os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), False, "B08", False, None, None, None, True, do_not_raise()),
(os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), False, "B8A", True, None, None, None, True, do_not_raise()),
(os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), True, "B8A", True, None, None, None, True, do_not_raise()),
(os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), False, None, True, None, None, None, True, do_not_raise()),
(os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), False, "B8A", False, None, None, None, True, do_not_raise()),
(os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), False, None, True, None, None, os.path.join(os.path.dirname(__file__), "data/eodata_local"), True, do_not_raise()),
]

@pytest.mark.parametrize("shapefile, series, band, resize, method, new, store, force_update, exception", search_params)
def test_clip_by_mask(shapefile, series, band, resize, method, new, store, force_update, exception):
with exception:
eodata = sentimeseries("S2-timeseries")
eodata.find("./data/eodata")
eodata.find(os.path.join(os.path.dirname(__file__), "data/eodata"))
if series:
eodata.clipbyMask(shapefile, None, band, resize, method, new, store, force_update)
for im in eodata.data:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_find.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from cropmaps.sts import sentimeseries
from cropmaps.exceptions import NoDataError

if not os.path.exists("./data/eodata_local"):
os.makedirs("./data/eodata_local")
if not os.path.exists(os.path.join(os.path.dirname(__file__), "data/eodata_local")):
os.makedirs(os.path.join(os.path.dirname(__file__), "data/eodata_local"))

# Test cropmaps.sts.sentimeseries.find()

search_params = [("./data/eodata", "L2A", do_not_raise()),
("./data/eodata", "L1C", pytest.raises(NoDataError))
search_params = [(os.path.join(os.path.dirname(__file__), "data/eodata"), "L2A", do_not_raise()),
(os.path.join(os.path.dirname(__file__), "data/eodata"), "L1C", pytest.raises(NoDataError))
]

@pytest.mark.parametrize("search, level, exception", search_params)
Expand Down Expand Up @@ -45,14 +45,14 @@ def local_DIAS_path_creator(image):

# Get data
eodata = sentimeseries("S2-timeseries")
eodata.find("./data/eodata")
eodata.find(os.path.join(os.path.dirname(__file__), "data/eodata"))
eodata.sort_images(date=True)

creodias_paths = []
for image in eodata.data:
src = os.path.join(image.path, image.name)
DIAS_path = local_DIAS_path_creator(image)
creodias_paths.append(os.path.join("./data/eodata", DIAS_path, image.name))
creodias_paths.append(os.path.join(os.path.dirname(__file__), "data/eodata", DIAS_path, image.name))

search_params = [(creodias_paths, do_not_raise()),
([], pytest.raises(NoDataError))
Expand Down
46 changes: 23 additions & 23 deletions tests/test_generate_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from cropmaps.sts import sentimeseries
from cropmaps.cube import generate_cube_paths

for directory, _, _ in os.walk("./data"):
for directory, _, _ in os.walk(os.path.join(os.path.dirname(__file__), "data")):
for file in os.listdir(directory):
if file.endswith(".tif"):
os.remove(os.path.join(directory, file))
if file.endswith(".tif.aux.xml"):
os.remove(os.path.join(directory, file))

if not os.path.exists("./data/eodata_local"):
os.makedirs("./data/eodata_local")
if not os.path.exists(os.path.join(os.path.dirname(__file__), "data/eodata_local")):
os.makedirs(os.path.join(os.path.dirname(__file__), "data/eodata_local"))

search_params = [("AOI", do_not_raise()),
(None, do_not_raise()),
Expand All @@ -24,29 +24,29 @@ def test_generate_cube_paths(mask, exception):
bands = ["B02", "B03", "B04", "B05", "B06", "B07", "B08", "B8A", "B11", "B12", "NDVI", "NDBI", "NDWI"]

eodata = sentimeseries("S2-timeseries")
eodata.find("./data/eodata")
eodata.find(os.path.join(os.path.dirname(__file__), "data/eodata"))
# To AOI
eodata.clipbyMask("./data/AOI/AOI.geojson", store = "./data/eodata_local")
eodata.clipbyMask("./data/AOI/AOI.geojson", store = "./data/eodata_local", resize = True)
eodata.getVI("NDVI", store = "./data/eodata_local", subregion = "AOI") # Subregion is the name of the mask shapefile
eodata.getVI("NDWI", store = "./data/eodata_local", subregion = "AOI")
eodata.getVI("NDBI", store = "./data/eodata_local", subregion = "AOI") # This is in 20m
eodata.upsample(store = "./data/eodata_local", band = "NDBI", subregion = "AOI", new = "AOI_10_Upsampled") # Transforming to 10m
eodata.apply_SCL(store = "./data/eodata_local", resolution="highest", subregion="AOI") # This performs only the default bands
eodata.apply_SCL(store = "./data/eodata_local", band = "NDVI", resolution="highest", subregion="AOI")
eodata.apply_SCL(store = "./data/eodata_local", band = "NDWI", resolution="highest", subregion="AOI")
eodata.apply_SCL(store = "./data/eodata_local", band = "NDBI", resolution="highest", subregion="AOI")
eodata.clipbyMask(os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), store = os.path.join(os.path.dirname(__file__), "data/eodata_local"))
eodata.clipbyMask(os.path.join(os.path.dirname(__file__), "data/AOI/AOI.geojson"), store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), resize = True)
eodata.getVI("NDVI", store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), subregion = "AOI") # Subregion is the name of the mask shapefile
eodata.getVI("NDWI", store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), subregion = "AOI")
eodata.getVI("NDBI", store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), subregion = "AOI") # This is in 20m
eodata.upsample(store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), band = "NDBI", subregion = "AOI", new = "AOI_10_Upsampled") # Transforming to 10m
eodata.apply_SCL(store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), resolution="highest", subregion="AOI") # This performs only the default bands
eodata.apply_SCL(store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), band = "NDVI", resolution="highest", subregion="AOI")
eodata.apply_SCL(store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), band = "NDWI", resolution="highest", subregion="AOI")
eodata.apply_SCL(store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), band = "NDBI", resolution="highest", subregion="AOI")

# To raw
eodata.upsample(store = "./data/eodata_local") # Transforming all 20m bands to 10m
eodata.getVI("NDVI", store = "./data/eodata_local") # Subregion is the name of the mask shapefile
eodata.getVI("NDWI", store = "./data/eodata_local")
eodata.getVI("NDBI", store = "./data/eodata_local") # This is in 20m
eodata.upsample(store = "./data/eodata_local", band = "NDBI") # Transforming to 10m
eodata.apply_SCL(store = "./data/eodata_local", resolution="highest") # This performs only the default bands
eodata.apply_SCL(store = "./data/eodata_local", band = "NDVI", resolution="highest")
eodata.apply_SCL(store = "./data/eodata_local", band = "NDWI", resolution="highest")
eodata.apply_SCL(store = "./data/eodata_local", band = "NDBI", resolution="highest")
eodata.upsample(store = os.path.join(os.path.dirname(__file__), "data/eodata_local")) # Transforming all 20m bands to 10m
eodata.getVI("NDVI", store = os.path.join(os.path.dirname(__file__), "data/eodata_local")) # Subregion is the name of the mask shapefile
eodata.getVI("NDWI", store = os.path.join(os.path.dirname(__file__), "data/eodata_local"))
eodata.getVI("NDBI", store = os.path.join(os.path.dirname(__file__), "data/eodata_local")) # This is in 20m
eodata.upsample(store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), band = "NDBI") # Transforming to 10m
eodata.apply_SCL(store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), resolution="highest") # This performs only the default bands
eodata.apply_SCL(store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), band = "NDVI", resolution="highest")
eodata.apply_SCL(store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), band = "NDWI", resolution="highest")
eodata.apply_SCL(store = os.path.join(os.path.dirname(__file__), "data/eodata_local"), band = "NDBI", resolution="highest")
paths = generate_cube_paths(eodata, bands, mask)
assert isinstance(paths, list)
assert len(paths) == len(bands)*eodata.total
Loading

0 comments on commit e022592

Please sign in to comment.