Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial work on expanding coverage. #59

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 72 additions & 91 deletions tests/test_layers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ee
import numpy as np
import pytest

from city_metrix.layers import (
Expand All @@ -8,124 +7,101 @@
AverageNetBuildingHeight,
EsaWorldCover,
EsaWorldCoverClass,
HighLandSurfaceTemperature,
LandsatCollection2,
LandSurfaceTemperature,
NasaDEM,
NaturalAreas,
OpenBuildings,
OpenStreetMap,
OpenStreetMapClass,
OvertureBuildings,
Sentinel2Level2,
SmartSurfaceLULC,
TreeCanopyHeight,
TreeCover,
UrbanLandUse,
WorldPop
)
from city_metrix.layers.layer import get_image_collection
from tests.fixtures.bbox_constants import BBOX_BRAZIL_LAURO_DE_FREITAS_1

from .conftest import (
LARGE_ZONES,
ZONES,
MockGroupByLayer,
MockLargeGroupByLayer,
MockLargeLayer,
MockLayer,
MockMaskLayer,
)
from .fixtures.bbox_constants import *


def test_count():
counts = MockLayer().groupby(ZONES).count()
assert counts.size == 100
assert all([count == 100 for count in counts])


def test_mean():
means = MockLayer().groupby(ZONES).mean()
assert means.size == 100
assert all([mean == i for i, mean in enumerate(means)])
EE_IMAGE_DIMENSION_TOLERANCE = 1 # Tolerance compensates for variable results from GEE service


def test_fishnetted_count():
counts = MockLargeLayer().groupby(LARGE_ZONES).count()
assert counts.size == 100
assert all([count == 100 for count in counts])


def test_fishnetted_mean():
means = MockLargeLayer().groupby(LARGE_ZONES).mean()
assert means.size == 100
assert all([mean == i for i, mean in enumerate(means)])
def test_albedo():
assert Albedo().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()


def test_masks():
counts = MockLayer().mask(MockMaskLayer()).groupby(ZONES).count()
assert counts.size == 100
for i, count in enumerate(counts):
if i % 2 == 0:
assert np.isnan(count)
else:
assert count == 100
def test_alos_dsm():
mean = AlosDSM().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean


def test_group_by_layer():
counts = MockLayer().groupby(ZONES, layer=MockGroupByLayer()).count()
assert all([count == {1: 50.0, 2: 50.0} for count in counts])
def test_average_net_building_height():
assert AverageNetBuildingHeight().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()


def test_group_by_large_layer():
counts = (
MockLargeLayer().groupby(LARGE_ZONES, layer=MockLargeGroupByLayer()).count()
def test_esa_world_cover():
count = (
EsaWorldCover(land_cover_class=EsaWorldCoverClass.BUILT_UP)
.get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
.count()
)
assert all([count == {1: 50.0, 2: 50.0} for count in counts])
assert count


def test_read_image_collection():
ic = ee.ImageCollection("ESA/WorldCover/v100")
data = get_image_collection(ic, BBOX_BRAZIL_LAURO_DE_FREITAS_1, 10, "test")

expected_crs = 32724
expected_crs = 32724
expected_x_dimension = 187
expected_y_dimension = 199

assert data.rio.crs == expected_crs
assert data.dims == {"x": expected_x_dimension, "y": expected_y_dimension}
assert (
pytest.approx(expected_x_dimension, rel=EE_IMAGE_DIMENSION_TOLERANCE) == "x",
pytest.approx(expected_y_dimension, rel=EE_IMAGE_DIMENSION_TOLERANCE) == "y"
)


def test_read_image_collection_scale():
ic = ee.ImageCollection("ESA/WorldCover/v100")
data = get_image_collection(ic, BBOX_BRAZIL_LAURO_DE_FREITAS_1, 100, "test")
assert data.dims == {"x": 19, "y": 20}
expected_x_dimension = 19
expected_y_dimension = 20
assert data.dims == {"x": expected_x_dimension, "y": expected_y_dimension}


def test_tree_cover():
actual = TreeCover().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
expected = 54.0
tolerance = 0.1
assert (
pytest.approx(expected, rel=tolerance) == actual
)
def test_high_land_surface_temperature():
data = HighLandSurfaceTemperature().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
assert data.any()


def test_albedo():
assert Albedo().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
def test_land_surface_temperature():
mean_lst = LandSurfaceTemperature().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean_lst


def test_landsat_collection_2():
bands = ['green']
data = LandsatCollection2(bands).get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
assert data.any()

def test_lst():
mean = LandSurfaceTemperature().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()

def test_nasa_dem():
mean = NasaDEM().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean


def test_esa():
count = (
EsaWorldCover(land_cover_class=EsaWorldCoverClass.BUILT_UP)
.get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
.count()
)
assert count
def test_natural_areas():
data = NaturalAreas().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
assert data.any()


def test_average_net_building_height():
assert AverageNetBuildingHeight().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
def test_openbuildings():
count = OpenBuildings().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count().sum()
assert count


def test_open_street_map():
Expand All @@ -138,35 +114,40 @@ def test_open_street_map():
assert count


def test_urban_land_use():
assert UrbanLandUse().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count()


def test_openbuildings():
count = OpenBuildings().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count().sum()
assert count


def test_tree_canopy_hight():
count = TreeCanopyHeight().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count()
def test_overture_buildings():
count = OvertureBuildings().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count().sum()
assert count


def test_alos_dsm():
mean = AlosDSM().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean
def test_sentinel_2_level2():
sentinel_2_bands = ["green"]
data = Sentinel2Level2(sentinel_2_bands).get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
assert data.any()


def test_smart_surface_lulc():
count = SmartSurfaceLULC().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count()
assert count


def test_overture_buildings():
count = OvertureBuildings().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count().sum()
def test_tree_canopy_height():
count = TreeCanopyHeight().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count()
assert count


def test_nasa_dem():
mean = NasaDEM().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
assert mean
def test_tree_cover():
actual = TreeCover().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).mean()
expected = 54.0
tolerance = 0.1
assert (
pytest.approx(expected, rel=tolerance) == actual
)


def test_urban_land_use():
assert UrbanLandUse().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1).count()


def test_world_pop():
data = WorldPop().get_data(BBOX_BRAZIL_LAURO_DE_FREITAS_1)
assert data.any()
58 changes: 58 additions & 0 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np

from .conftest import (
LARGE_ZONES,
ZONES,
MockGroupByLayer,
MockLargeGroupByLayer,
MockLargeLayer,
MockLayer,
MockMaskLayer,
)
from .fixtures.bbox_constants import *


def test_count():
counts = MockLayer().groupby(ZONES).count()
assert counts.size == 100
assert all([count == 100 for count in counts])


def test_mean():
means = MockLayer().groupby(ZONES).mean()
assert means.size == 100
assert all([mean == i for i, mean in enumerate(means)])


def test_fishnetted_count():
counts = MockLargeLayer().groupby(LARGE_ZONES).count()
assert counts.size == 100
assert all([count == 100 for count in counts])


def test_fishnetted_mean():
means = MockLargeLayer().groupby(LARGE_ZONES).mean()
assert means.size == 100
assert all([mean == i for i, mean in enumerate(means)])


def test_masks():
counts = MockLayer().mask(MockMaskLayer()).groupby(ZONES).count()
assert counts.size == 100
for i, count in enumerate(counts):
if i % 2 == 0:
assert np.isnan(count)
else:
assert count == 100


def test_group_by_layer():
counts = MockLayer().groupby(ZONES, layer=MockGroupByLayer()).count()
assert all([count == {1: 50.0, 2: 50.0} for count in counts])


def test_group_by_large_layer():
counts = (
MockLargeLayer().groupby(LARGE_ZONES, layer=MockLargeGroupByLayer()).count()
)
assert all([count == {1: 50.0, 2: 50.0} for count in counts])
Loading