Skip to content

Commit

Permalink
Finish test tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dvalters committed Sep 15, 2023
1 parent 296f2df commit a034117
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
Binary file added test/baseline/test_hazard_map_regression.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/baseline/test_succeeds.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 35 additions & 16 deletions test/test.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,61 @@
import os
import pytest
import logging

import numpy as np
import matplotlib.pyplot as plt
from hazardmaps import hazardmap as hazmap

from hazardmaps import hazardmap as hazmap
from hazardmaps.config import volcfile, volcnames, exposure_file, exposure_breakdown_file, floodratio, floodtypes, eearthquake_file

logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)


@pytest.mark.mpl_image_compare
def test_succeeds():
"""
Very simple test to check that the matplotlib
image compare hook is set up correctly.
"""
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot([1,2,3], linestyle='-')
return fig

# This is a long-running test as it essential runs the whole script against
# the full Tanzania dataset.
# Should rewrite or skip if nepal data because the logic is slightly different

@pytest.mark.skipif(os.getenv("GITLAB_CI") is not None, reason="cannot run this test on CI server due to data requirement")
@pytest.mark.mpl_image_compare
def test_hazard_map_regression():
# Call the main function here to get the image gpd
print("Generating regression test data...this may take a while")
"""Regression test to check that running a full hazard map analysis to completion
produces the same map by the way of an image comparison check.
You must run this test locally. If the test case data is changed, you will need
to regenerate the test images, the "baseline" images. and replace
the ones in the baseline folder in the test directory.
pytest --mpl-generate-path="test/baseline" test/test.py
Then run the tests with the --mpl flag
pytest --mpl test/test.py
"""
logging.info("Generating regression test data...this may take a while")
# The steps below are similar to the ones found in the main() function in the hazardmaps.py file
volcano_lahar, volcano_pyro = hazmap.read_volcano_data(volcfile, volcnames)
tz, tz_withgeometry = hazmap.buildings(exposure_file, exposure_breakdown_file) # tz needs a rename...
tz_withgeometry_withflood = hazmap.flood_data(floodratio, floodtypes, tz, tz_withgeometry) # returns tz_withgeometry again!
tz_withgeometry_withflood_withvolcano = hazmap.combine_volcano_buildings(tz_withgeometry_withflood, volcano_lahar, volcano_pyro) # as above - flood_data is the tz_geometry
tz_earthquakes = hazmap.earthquake_data(eearthquake_file) # as above - but this one only generates earthquake data
tz, tz_withgeometry = hazmap.buildings(exposure_file, exposure_breakdown_file)
tz_withgeometry_withflood = hazmap.flood_data(floodratio, floodtypes, tz, tz_withgeometry)
tz_withgeometry_withflood_withvolcano = hazmap.combine_volcano_buildings(tz_withgeometry_withflood, volcano_lahar, volcano_pyro)
tz_earthquakes = hazmap.earthquake_data(eearthquake_file)
combined_data = hazmap.hazards_combined(tz_earthquakes, tz_withgeometry_withflood_withvolcano)

np.sum(np.isnan(combined_data.volc))

print("Printing single hmap plot:")
logging.info("Printing single hmap plot:")

# Now plot the test figures
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax = combined_data.plot(ax=ax, column="hmap", markersize=0.01, legend=True) # Problemm calling from pandas?
plt.savefig("regression_test_hmap_step1.png")
ax = combined_data.plot(ax=ax, column="hmap", markersize=0.01, legend=True)

lims = plt.axis('equal')
plt.savefig("regression_test_hmap_step2.png")
return fig # Test function must return the figure to work wth decorator
# Test function must return the figure to work with the mpl image compare decorator
return fig

0 comments on commit a034117

Please sign in to comment.