Skip to content

Commit

Permalink
Restructured tests to have individual test per layer
Browse files Browse the repository at this point in the history
  • Loading branch information
kcartier-wri committed Aug 30, 2024
1 parent ea8cf88 commit 08e3d8b
Showing 1 changed file with 87 additions and 37 deletions.
124 changes: 87 additions & 37 deletions tests/test_layer_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,99 @@
from tests.tools.spatial_tools import get_distance_between_geocoordinates

"""
Note: To add a test for another scalable layer that has the spatial_resolution property:
Evaluation of spatial_resolution property
To add a test for a scalable layer that has the spatial_resolution property:
1. Add the class name to the city_metrix.layers import statement above
2. Specify a minimal class instance in the set below. Do no specify the spatial_resolution
property in the instance definition.
2. Copy an existing test_*_spatial_resolution() test
a. rename for the new layer
b. specify a minimal class instance for the layer, not specifying the spatial_resolution attribute.
"""
CLASSES_WITH_SPATIAL_RESOLUTION_PROPERTY = \
{
Albedo(),
AlosDSM(),
AverageNetBuildingHeight(),
BuiltUpHeight(),
EsaWorldCover(land_cover_class=EsaWorldCoverClass.BUILT_UP),
LandSurfaceTemperature(),
NasaDEM(),
NaturalAreas(),
NdviSentinel2(year=2023),
TreeCanopyHeight(),
TreeCover(),
UrbanLandUse(),
WorldPop()
}

COUNTRY_CODE_FOR_BBOX = 'BRA'
BBOX = BBOX_BRA_LAURO_DE_FREITAS_1

def test_spatial_resolution_for_all_scalable_layers():
for obj in CLASSES_WITH_SPATIAL_RESOLUTION_PROPERTY:
is_valid, except_str = validate_layer_instance(obj)
if is_valid is False:
raise Exception(except_str)

cls = get_class_from_instance(obj)

# Double the spatial_resolution for the specified Class
doubled_default_resolution = 2 * cls.spatial_resolution
obj.spatial_resolution=doubled_default_resolution
def test_albedo_spatial_resolution():
class_instance = Albedo()
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_alos_dsm_spatial_resolution():
class_instance = AlosDSM()
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_average_net_building_height_spatial_resolution():
class_instance = AverageNetBuildingHeight()
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_built_up_height_spatial_resolution():
class_instance = BuiltUpHeight()
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_esa_world_cover_spatial_resolution():
class_instance = EsaWorldCover(land_cover_class=EsaWorldCoverClass.BUILT_UP)
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_land_surface_temperature_spatial_resolution():
class_instance = LandSurfaceTemperature()
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_nasa_dem_spatial_resolution():
class_instance = NasaDEM()
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_natural_areas_spatial_resolution():
class_instance = NaturalAreas()
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_ndvi_sentinel2_spatial_resolution():
class_instance = NdviSentinel2(year=2023)
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_tree_canopy_height_spatial_resolution():
class_instance = TreeCanopyHeight()
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_tree_cover_spatial_resolution():
class_instance = TreeCover()
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_urban_land_use_spatial_resolution():
class_instance = UrbanLandUse()
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def test_world_pop_spatial_resolution():
class_instance = WorldPop()
doubled_default_resolution, actual_estimated_resolution = evaluate_resolution__property(class_instance)
assert doubled_default_resolution == actual_estimated_resolution

def evaluate_resolution__property(obj):
is_valid, except_str = validate_layer_instance(obj)
if is_valid is False:
raise Exception(except_str)

cls = get_class_from_instance(obj)

# Double the spatial_resolution for the specified Class
doubled_default_resolution = 2 * cls.spatial_resolution
obj.spatial_resolution=doubled_default_resolution

data = obj.get_data(BBOX)

expected_resolution = doubled_default_resolution
actual_estimated_resolution = estimate_spatial_resolution(data)

evaluate_layer(obj, doubled_default_resolution)
return expected_resolution, actual_estimated_resolution


def test_function_validate_layer_instance():
Expand Down Expand Up @@ -95,11 +150,6 @@ def get_class_from_instance(obj):
cls = obj.__class__()
return cls

def evaluate_layer(layer, expected_resolution):
data = layer.get_data(BBOX)
actual_estimated_resolution = estimate_spatial_resolution(data)
assert expected_resolution == actual_estimated_resolution

def estimate_spatial_resolution(data):
y_cells = float(data['y'].size - 1)

Expand All @@ -113,7 +163,7 @@ def estimate_spatial_resolution(data):
# if xarray doesn't have crs property, assume meters
crs_units = 'metre'

if crs_units == 'metre':
if crs_units == 'metre' or crs_units == 'm':
y_diff = y_max - y_min
elif crs_units == 'foot':
feet_to_meter = 0.3048
Expand Down

0 comments on commit 08e3d8b

Please sign in to comment.