Skip to content

Commit

Permalink
Additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kcartier-wri committed Sep 10, 2024
1 parent 10b2205 commit 7977d11
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion city_metrix/layers/ndvi_sentinel2_gee.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NdviSentinel2(Layer):
Notebook: https://github.com/wri/cities-cities4forests-indicators/blob/dev-eric/scripts/extract-VegetationCover.ipynb
Reference: https://en.wikipedia.org/wiki/Normalized_difference_vegetation_index
"""
def __init__(self, year=None, spatial_resolution=10, **kwargs):
def __init__(self, year=2021, spatial_resolution=10, **kwargs):
super().__init__(**kwargs)
self.year = year
self.spatial_resolution = spatial_resolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# RUN_DUMPS is the master control for whether the writes and tests are executed
# Setting RUN_DUMPS to True turns on code execution.
# Values should normally be set to False in order to avoid unnecessary execution.
RUN_DUMPS = True
RUN_DUMPS = False

# Multiplier applied to the default spatial_resolution of the layer
# Use value of 1 for default resolution.
Expand Down
Binary file not shown.
51 changes: 44 additions & 7 deletions tests/test_layer_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
TreeCanopyHeight,
TreeCover,
UrbanLandUse,
WorldPop
WorldPop, OpenBuildings
)
from tests.resources.bbox_constants import BBOX_BRA_LAURO_DE_FREITAS_1
from tests.tools.general_tools import get_class_from_instance, get_class_default_spatial_resolution
Expand Down Expand Up @@ -191,8 +191,7 @@ def test_null_spatial_resolution(self):
with pytest.raises(Exception) as e_info:
_get_modified_resolution_data(class_instance, spatial_resolution, BBOX)

class TestThreshold:

class TestOtherParameters:
def test_albedo_threshold(self):
threshold=0.1
data = Albedo(threshold=threshold).get_data(BBOX)
Expand All @@ -201,6 +200,44 @@ def test_albedo_threshold(self):
assert threshold > max_albedo,\
f"Maximum value ({max_albedo}) in Albedo dataset is not < threshold of {threshold}."

def test_albedo_dates(self):
with pytest.raises(Exception) as e_info:
Albedo(start_date="2021-01-01", end_date="2021-01-02").get_data(BBOX)

with pytest.raises(Exception) as e_info:
Albedo(start_date="2021-01-01", end_date=None).get_data(BBOX)

def test_high_land_surface_temperature_dates(self):
with pytest.raises(Exception) as e_info:
HighLandSurfaceTemperature(start_date="2021-01-01", end_date="2021-01-02").get_data(BBOX)

def test_land_surface_temperature_dates(self):
with pytest.raises(Exception) as e_info:
LandSurfaceTemperature(start_date="2021-01-01", end_date="2021-01-02").get_data(BBOX)

with pytest.raises(Exception) as e_info:
LandSurfaceTemperature(start_date="2021-01-01", end_date=None).get_data(BBOX)

def test_ndvi_sentinel2_dates(self):
with pytest.raises(Exception) as e_info:
data = NdviSentinel2(Year=None).get_data(BBOX)

with pytest.raises(Exception) as e_info:
NdviSentinel2(Year="1970").get_data(BBOX)

def test_open_buildings_country(self):
with pytest.raises(Exception) as e_info:
OpenBuildings(country="ZZZ").get_data(BBOX)

def test_tree_cover_min_max_cover(self):
data = TreeCover(min_tree_cover = 150).get_data(BBOX)
non_null_cells = data.values[~np.isnan(data)].size
assert non_null_cells == 0

data = TreeCover(max_tree_cover = -1).get_data(BBOX)
non_null_cells = data.values[~np.isnan(data)].size
assert non_null_cells == 0


def test_function_validate_layer_instance():
is_valid, except_str = _validate_layer_instance('t')
Expand Down Expand Up @@ -288,12 +325,12 @@ def _evaluate_raster_value(raw_data, downsized_data):
# Below values where determined through trial and error evaluation of results in QGIS
ratio_tolerance = 0.2
normalized_rmse_tolerance = 0.3
ssim_index_tolerance = 0.6


populated_raw_data_ratio = _get_populate_ratio(raw_data)
populated_downsized_data_ratio = _get_populate_ratio(raw_data)
diff = abs(populated_raw_data_ratio - populated_downsized_data_ratio)
ratio_eval = True if diff <= ratio_tolerance else False
ratio_diff = abs(populated_raw_data_ratio - populated_downsized_data_ratio)
ratio_eval = True if ratio_diff <= ratio_tolerance else False

filled_raw_data = raw_data.fillna(0)
filled_downsized_data = downsized_data.fillna(0)
Expand All @@ -320,9 +357,9 @@ def _evaluate_raster_value(raw_data, downsized_data):
matching_rmse = True if normalized_rmse < normalized_rmse_tolerance else False

# Calculate and evaluate Structural Similarity Index (SSIM)
ssim_index_tolerance = 0.6 if (processed_downsized_data_np.size > 100 and ratio_diff <= 0.1) else 0.4
ssim_index, _ = ssim(processed_downsized_data_np, processed_raw_data_np, full=True, data_range=max_val)
matching_ssim = True if ssim_index > ssim_index_tolerance else False

results_match = True if (ratio_eval & matching_rmse & matching_ssim) else False

return results_match

0 comments on commit 7977d11

Please sign in to comment.