Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
elbeejay committed Apr 14, 2024
1 parent 017af46 commit d775326
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 13 deletions.
23 changes: 13 additions & 10 deletions src/GOSTurban/UrbanRaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ def __init__(self, inRaster):
)
)

def _burnValue(self, mask, val, final_raster, allFeatures, idx, pop, cShape):
"""Private function to burn value into final mask."""
mask = (mask ^ 1) * val
yy = np.dstack([final_raster, mask])
final_raster = np.amax(yy, axis=2)
allFeatures.append([idx, pop, val, shape(geojson.loads(json.dumps(cShape)))])
return final_raster, allFeatures

def calculateDegurba(
self,
urbDens=300,
Expand Down Expand Up @@ -218,11 +226,8 @@ def modal(P):
val = 30

# Burn value into the final raster
mask = (mask ^ 1) * val
yy = np.dstack([final_raster, mask])
final_raster = np.amax(yy, axis=2)
allFeatures.append(
[idx, pop, val, shape(geojson.loads(json.dumps(cShape)))]
final_raster, allFeatures = self._burnValue(
mask, val, final_raster, allFeatures, idx, pop, cShape
)

HD_raster = final_raster
Expand Down Expand Up @@ -257,12 +262,10 @@ def modal(P):
if pop > urbThresh:
val = 21
# Burn value into the final raster
mask = (mask ^ 1) * val
yy = np.dstack([final_raster, mask])
final_raster = np.amax(yy, axis=2)
allFeatures.append(
[idx, pop, val, shape(geojson.loads(json.dumps(cShape)))]
final_raster, allFeatures = self._burnValue(
mask, val, final_raster, allFeatures, idx, pop, cShape
)

URB_raster = final_raster

# Combine the urban layers
Expand Down
63 changes: 61 additions & 2 deletions tests/test_UrbanRaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,77 @@
import pytest # noqa: F401
import geopandas as gpd
from GOSTurban import UrbanRaster
from unittest.mock import MagicMock
from unittest import mock
import numpy as np


def test_tprint(capfd):
"""Test tprint function."""
# call function
UrbanRaster.tPrint("mymsg")
# captured output
captured = capfd.readouterr()
assert captured.out.split("\t")[1][:5] == "mymsg"


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")

def test_geocode_cities(self):
# read some of the tutorial data to test
gdf = gpd.read_file("data/tutorial_data/AOI.geojson")
gdf = self.gdf
# 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


class TestUrbanGriddedPop:
"""Testing the urban gridded population data class."""

def mocked_rasterio_open(self, t="w"):
"""Mocked function for rasterio.open()"""

class tmpOutput:
def __init__(self):
self.crs = "EPSG:4326"
self.meta = MagicMock()

def read(self):
raster = np.zeros((10, 10))
raster[:5, :5] = 4
raster[5:, 5:] = 3
raster = np.reshape(raster, [1, 10, 10])
return raster

return_val = tmpOutput()
return return_val

@mock.patch("rasterio.open", mocked_rasterio_open)
def test_init_class(self):
"""Init with string that uses rasterio.open"""
ugp = UrbanRaster.urbanGriddedPop("str")
# assert known properties of the mocked object
assert ugp.inR.crs == "EPSG:4326"

def test_init_value_error(self):
"""Init with wrong type, raising a value error."""
with pytest.raises(ValueError):
UrbanRaster.urbanGriddedPop(5)

# @mock.patch("rasterio.open", mocked_rasterio_open)
# def test_burn_value(self):
# """Testing the private burn value function."""
# # make the object
# ugp = UrbanRaster.urbanGriddedPop('str')
# with patch("shapely.geometry") as mock_shape:
# mock_shape = MagicMock()
# final_raster, allFeatures = ugp._burnValue(
# np.ones((5, 5), dtype=bool), 1, np.zeros((5, 5)), [], 'a', 'pop', mock_shape
# )
37 changes: 36 additions & 1 deletion tests/test_country_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import GOSTrocks.rasterMisc as rMisc
from unittest.mock import MagicMock
from unittest.mock import patch
from unittest import mock
import numpy as np


class TestUrbanHelper:
Expand Down Expand Up @@ -78,7 +80,7 @@ def test_summarize_ghsl(self, tmp_path):
rMisc.zonalStats.assert_called()

def test_summarize_ghsl02(self, tmp_path):
"""Test the summarize_ghsl method."""
"""Test the summarize_ghsl method with clip_raster=True."""
# make a tmp location for output
out_folder = tmp_path / "output"
# make the class
Expand All @@ -98,6 +100,39 @@ def test_summarize_ghsl02(self, tmp_path):
rMisc.zonalStats.assert_called()
rMisc.clipRaster.assert_called()

def mocked_rasterio_open(self, t="w"):
"""Mocked function for rasterio.open()"""

class tmpOutput:
def __init__(self):
self.crs = "EPSG:4326"
self.meta = MagicMock()

def read(self):
raster = np.zeros((10, 10))
raster[:5, :5] = 1500
raster[5:, 5:] = 3
return raster

return_val = tmpOutput()
return return_val

@mock.patch("rasterio.open", mocked_rasterio_open)
def test_summarize_ghsl03(self, tmp_path):
"""Test the summarize_ghsl method with binary_calc=True.
Have not clipped local ghsl data so will throw error."""
# make a tmp location for output
out_folder = tmp_path / "output"
# make the class
ch = country_helper.urban_country(
iso3="USA", sel_country="placeholder", cur_folder=out_folder, inP=[1, 2, 3]
)
# mock zonalStats
rMisc.zonalStats = MagicMock()
# try calling the method expecting the value error
with pytest.raises(ValueError):
ch.summarize_ghsl(ghsl_files=["a_a_a_e", "b_b_b_f"], binary_calc=True)

def test_delete_urban_data(self, tmp_path):
"""Test the delete_urban_data method."""
# make a tmp location for output
Expand Down

0 comments on commit d775326

Please sign in to comment.