Skip to content

Commit

Permalink
[CIF-183] Fixed the pytest setup
Browse files Browse the repository at this point in the history
  • Loading branch information
arshdoda committed Jun 26, 2024
1 parent 90de050 commit 71bc126
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,4 @@ cython_debug/
/keys
keys/
wri-gee-358d958ce7c6.json
citiesindicators-fe8fd6514c70.json
5 changes: 2 additions & 3 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ dependencies:
- xarray-spatial=0.3.7
- xee=0.0.3
- utm=0.7.0
- osmnx=1.8.1
- osmnx=1.9.3
- dask[complete]=2023.11.0
- matplotlib=3.8.2
- jupyterlab=4.0.10
- s3fs=2024.5.0
- geemap=0.32.0
- pip=23.3.1
- boto3=1.34.124
- pip:
- cartoframes==1.2.5
- cartoframes==1.2.5
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
testpaths = tests
71 changes: 58 additions & 13 deletions tests/layers.py → tests/test_layers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import ee
import numpy as np
import pytest

from city_metrix.layers import LandsatCollection2, Albedo, LandSurfaceTemperature, EsaWorldCover, EsaWorldCoverClass, TreeCover, AverageNetBuildingHeight, OpenStreetMap, OpenStreetMapClass, UrbanLandUse, OpenBuildings, TreeCanopyHeight, AlosDSM
from city_metrix.layers import (
Albedo,
AlosDSM,
AverageNetBuildingHeight,
EsaWorldCover,
EsaWorldCoverClass,
LandSurfaceTemperature,
OpenBuildings,
OpenStreetMap,
OpenStreetMapClass,
TreeCanopyHeight,
TreeCover,
UrbanLandUse,
)
from city_metrix.layers.layer import get_image_collection
from .conftest import MockLayer, MockMaskLayer, ZONES, LARGE_ZONES, MockLargeLayer, MockGroupByLayer, \
MockLargeGroupByLayer

import pytest
import numpy as np
from .conftest import (
LARGE_ZONES,
ZONES,
MockGroupByLayer,
MockLargeGroupByLayer,
MockLargeLayer,
MockLayer,
MockMaskLayer,
)


def test_count():
Expand Down Expand Up @@ -49,29 +69,39 @@ def test_group_by_layer():


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


SAMPLE_BBOX = (-38.35530428121955, -12.821710300686393, -38.33813814352424, -12.80363249765361)
SAMPLE_BBOX = (
-38.35530428121955,
-12.821710300686393,
-38.33813814352424,
-12.80363249765361,
)


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

assert data.rio.crs == 32724
assert data.dims == {'x': 187, 'y': 200}
assert data.dims == {"x": 187, "y": 200}


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


def test_tree_cover():
assert pytest.approx(53.84184165912419, rel=0.001) == TreeCover().get_data(SAMPLE_BBOX).mean()
assert (
pytest.approx(53.84184165912419, rel=0.001)
== TreeCover().get_data(SAMPLE_BBOX).mean()
)


def test_albedo():
Expand All @@ -84,27 +114,42 @@ def test_lst():


def test_esa():
count = EsaWorldCover(land_cover_class=EsaWorldCoverClass.BUILT_UP).get_data(SAMPLE_BBOX).count()
count = (
EsaWorldCover(land_cover_class=EsaWorldCoverClass.BUILT_UP)
.get_data(SAMPLE_BBOX)
.count()
)
assert count


def test_average_net_building_height():
assert AverageNetBuildingHeight().get_data(SAMPLE_BBOX).mean()


def test_open_street_map():
count = OpenStreetMap(osm_class=OpenStreetMapClass.ROAD).get_data(SAMPLE_BBOX).count().sum()
count = (
OpenStreetMap(osm_class=OpenStreetMapClass.ROAD)
.get_data(SAMPLE_BBOX)
.count()
.sum()
)
assert count


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


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


def test_tree_canopy_hight():
count = TreeCanopyHeight().get_data(SAMPLE_BBOX).count()
assert count



def test_AlosDSM():
mean = AlosDSM().get_data(SAMPLE_BBOX).mean()
assert mean
File renamed without changes.

0 comments on commit 71bc126

Please sign in to comment.