Skip to content

Commit

Permalink
adding tests and an integrative test
Browse files Browse the repository at this point in the history
  • Loading branch information
elbeejay committed May 10, 2024
1 parent a6bb36f commit 61fe477
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/GOSTurban/UrbanRaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ def calculateDegurba(
tPrint("%s: Creating Shape %s" % (print_message, idx))
idx = idx + 1
if value > 0:
# RRemove holes from urban shape
# origShape = cShape
# Remove holes from urban shape
xx = shape(cShape)
xx = Polygon(xx.exterior)
cShape = xx.__geo_interface__
Expand Down Expand Up @@ -252,7 +251,7 @@ def calculateDegurba(
urban_raster = _create_urban_raster(data, urbDens, 22)
final_raster = data[0, :, :] * 0 + 11

# Analyze the high density shapes
# Analyze the urban density shapes
if verbose:
tPrint(f"{print_message}: extracting URBAN clusters")

Expand Down
15 changes: 13 additions & 2 deletions tests/test_UrbanRaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,21 @@ class TestGeocodeCities:
"""Tests for the geocode_cities() function."""

# read some of the tutorial data to test
gdf = gpd.read_file("data/tutorial_data/AOI.geojson")
geopath = "data/tutorial_data/AOI.geojson"

def test_geocode_cities(self):
gdf = self.gdf
gdf = gpd.read_file(self.geopath)
# run the function - adding city/state/country info
result = UrbanRaster.geocode_cities(gdf)
# assert things about the result
# e.g., should have city/state/country columns
assert "City" in result.columns
assert "State" in result.columns
assert "Country" in result.columns

def test_geocode_cities_reversed(self):
gdf = gpd.read_file(self.geopath)
gdf["geometry"][0] = gdf["geometry"][0].reverse()
# run the function - adding city/state/country info
result = UrbanRaster.geocode_cities(gdf)
# assert things about the result
Expand Down
41 changes: 41 additions & 0 deletions tests/test_UrbanRaster_integrated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Integrative tests for UrbanRaster.py based on the tutorial notebook."""
import os
import rasterio
import geopandas as gpd
import GOSTurban.UrbanRaster as urban


def test_urbanraster_integrated():
# Define input population raster
tutorial_folder = "./data/tutorial_data"
pop_file = os.path.join(tutorial_folder, "wp_2020_1k_AOI.tif")

inR = rasterio.open(pop_file)
# Initiate the urban calculator
urban_calculator = urban.urbanGriddedPop(inR)

# Extract the urban extents
# (minimum density 300/km2, minimum total population 5000)
urban_extents = urban_calculator.calculateUrban(
densVal=300, totalPopThresh=5000, smooth=False, queen=False, verbose=True
)
urban_extents["Type"] = 1
urban_extents.head()

# assert that urban_extents is a geopandas.GeoDataFrame
assert isinstance(urban_extents, gpd.GeoDataFrame)

# Extract the high density urban extents
# (minimum density 1500/km2, minimum total population 50000)
hd_urban_extents = urban_calculator.calculateUrban(
densVal=1500,
totalPopThresh=50000,
smooth=True,
queen=True, # high density extents use queen's case contiguity, and
verbose=True,
) # High density extents have hole smoothing applied.
hd_urban_extents["Type"] = 2
hd_urban_extents.head()

# assert that hd_urban_extents is a geopandas.GeoDataFrame
assert isinstance(hd_urban_extents, gpd.GeoDataFrame)
19 changes: 19 additions & 0 deletions tests/test_urban_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Unit tests for the urban_helper.py module"""
import pytest # noqa: F401
from GOSTurban import urban_helper
import os
import numpy as np
from unittest import mock
import geopandas as gpd
Expand Down Expand Up @@ -96,3 +97,21 @@ def test_init(self, tmp_path):
# process the dem
uc.process_dem()
# doesn't return anything or change any attributes so nothing to assert

def test_init_02(self, tmp_path):
outf = os.path.join(tmp_path, "fin_folder")
os.makedirs(outf)
os.makedirs(os.path.join(outf, "FINAL_STANDARD"))
admin_pth = os.path.join(outf, "FINAL_STANDARD", "usa_adm.shp")
with open(admin_pth, "w") as f:
f.write("fake shapefile")

uc = urban_helper.urban_country(
iso3="USA",
output_folder=outf,
country_bounds=[],
pop_files=[],
)
# minor assertions about class attributes
assert uc.admin_shp == admin_pth
assert isinstance(uc.pop_files, list)

0 comments on commit 61fe477

Please sign in to comment.