Skip to content

Commit 0c9edcb

Browse files
chahankChahan Kropfclimada-jenkinsemanuel-schmidsarah-hlsn
authored
Feature/centroids as gdf (#787)
* Add geodataframe to centroids: first commit * Remove raster methods, obsolete methods, and fix some. * Remove test for removed methods * Fix from geodataframe method * Remove set meta to lat lon * Update all I/O methods * Remove area pixel * Add method to compute the area per pixel (not attribute) * Make get_area_pixel * Remove not often used dist_coast and elevation * Remove unused meta * Change Centroids call to new signature * Remove vector - raster hazard methods * Restore legacy excell reader * Remove clear method * Remove clear test * Update read/write centroids in hazard * Support empty on land or region id * Check for empty region id * Set region id correctly * Remove scheduler * Add docstring * Add legacy read hdf5 * Replace incorrect dot product * Remove meta in assign centroids * Remove matlab test file * Update set lat/lon * Update test with init * 'Automated update v4.0.0' * Add note on method * changelog: for 4.0.1 * 'Automated update v4.0.1' * Add from_meta class method * Update centroids init * Update read raster intensity / fraction data * undo changelog duplications * Update test for points outside of raster within threshold * Docstring and cosmetics * Add to default crs method * Fix legacy from hdf5 for empty extra values * Update legacy from_hdf5 to exclude 'latitude'/'longitude' from extra * Update centroids.select to work properly with mask and indices * Fix forecast translate bug * Remove _set_centroids * Remove reproject raster * Remove test hazard raster * Update naming for plot fraction centroids * Replace HAZ_DEMO_MAT with HAZ_TEST_TC * Replace mat file with hdf5 test file * Rewrite write raster method for hazard * Add a default for sel_cen in mask * Correct indent typos * Change duplicate test function name * Replace mat file with hdf5 * Remove unused mat file import * Add legacy code to read old centroid hdf5 files. * Update access to centroids dist coast in gdf * test_trop_cyclone: get test centroids from data api * Add method to estimate meta raster from centroids * Remove unecessary conversion to ne_geom * Add good lat/lon values for unit test * centroids.centr.write_hdf5: eliminate side effect * centroids.centr.write_hdf5: elimination of side effect futile for pandas>=2.1 * centroids.test.TestCentroidsFuns.test_select_pass: adapt to downsized LAT/LON arrays * test_vec_ras: fix TestCentroids.test_centroids_check_pass * test_vec_ras: remove TestReader.test_write_read_points_h5 as it is redundant to and covered by test_centr.TestCentroidsWriter.test_read_write_hdf5 * fix typo in from_exposures method * centroids.centr.from_csv: write class method * centroids.centr.from_csv: correct order of xy * centroids.centr.from_excel: update method for gdf * centroids.centr. update csv and excel methods * Update tests in test_vec_ras * Update more tests and remove some with meta * centroids.centr.from_excel fix columnn indexing * Add possibility to NOT recenter crs for countires. This lead to a bug in the assign region id method because longitude of the countries geometry was shifted, but not the entry lat/lon points. * Add to_csre convenience method * Prepare the stage for setting other than admin0 region id * Remove uneeded import * cenctroids.centr: fix the obvious * gencenctroids.centr: change crs of the geodataframe instead of the geometry * white space cosmetics * PEP8 * Add solid region id and on land tests * Auto stash before merge of "feature/centroids_as_gdf" and "origin/feature/centroids_as_gdf" * Update distance and area tests * Update set crs in load vector shape file * centr.Centroids.from_excel: deal with region_id column * Fix write hazard raster with centroids.get_meta * Area pixel now correctly uses CEA * Add actual NE CRS test. * Make on_land and region_id always part of centroids * Update docstring * Update import path * Add overwrite argument to set region id and on land * Remove from base grid method * Fix some linting * Add some docstrings * Avoid costly computation in from geodataframe * Add comment * Fix typo * Fix set crs in from geodataframe * Make properties return arrays and not series * Remove not needed matlab vars * Revert return numpy arrays. * update from excel * update from_excel and from_csv * centroids.centr: from_excel fix column renaming and adapt to "new argument names" * Centroids.from_excel fix: switch key,val in test_storm_europe rather than in centr! * Return numpy arrays instead of series * Add some basic tests * Add tests for meta * Add tentative changelog. * Make keywords argument only : name lat/lon : remove set_* * Auto stash before revert of "Make keywords argument only : name lat/lon : remove set_*" * remove file * Fix exposures method test * Fix setting region id and on land at init. * Fix typo * Fix all close values for big numbers * Improve cosmetics. * update docstrings, doc cosmetics * update from_csv method, add test * remove DEF_VAR_CSV * Update from_excel method and test * remove unnecessary import * fix storm_europe tests * improve csv and excel tests * Apply suggestions from code review Co-authored-by: Lukas Riedel <[email protected]> * add excel, csv write methods and tests * Replace gdf with _gdf * Revert "Replace gdf with _gdf" This reverts commit c7e6ffd. * Update to_crs methods * Raise error if wrong exposures * Make from geodataframe more restrictive. * Use consistently to_crs method * Improve read centroids * Add literal to import * Update to crs with inplace argument * improve excel, csv method, test based on review * Update typing * Fix equal centroids for different gdf columns ordering * Make consistent gdf column ordering * Simplify from excel/csv * Add support for legacy hazard excell * Avoid single column loading error for df * fix path in write_excel, write_csv * fixing docstrings in centr.py * Update code cosmetics * Fix typo * Allo for kwargs in init and update from exposures * code linting * Centroids: test from_meta * Centroids: refined tests * Centroids: fix from_exposures * Centroids: implement get_pixel_shapes * Centroids: code linting and docstrings * hazard.test.test_base: remove undefined classes from main * hazard.base: remove unused imports * hazard.base: clean up imports * cosmetics * hazard.base.write_htdf5: add inline comment. * Centroids: fix _gdf_from_legacy_hdf signature * Centroids._gdf_from_legacy_hdf5: static, not classmethod * hazard.centroids.centr: pylint * centroids.centr : fix pydoc typos and pylint * climada.hazard.base: pylint * hazard.centroids.centr: pylint * climada.test.test_calicbration: use hzard test file with hdf5 format * Update climada/hazard/centroids/test/test_vec_ras.py Co-authored-by: Emanuel Schmid <[email protected]> * Hazard: fix write_raster, and some docstrings * test.test_calibration: fix test file name * test_api_client: added test for basic centroids plotting (just because it fails in PR #787) * centr.Centroids.plot: all changes reverted (with the exception of the obsolete `self.set_meta_to_lat_lon()`) * Hazard tutorial: use centroids.get_meta() instead of the attribute meta * Hazard tutorial: use Centroids() instead of from_lat_lon() * hazard tutorial: update Hazard from raster section * hazard tutorial: fix dist_coast access * climada.hazard.Hazard: remove broken vector file support * climada.hazard.centr: add deprecated methods section * fix from_tracks method, remove side effect * climada.hazard.trop_cyclone.TropCytlone.from_tracks: option for predefined dist_coast in centroids * TropicalCyclone.from_tracks pydoc * white space cosmetics * CHANGELOG: list deprecated and removed methods * Refactor and add to centroids testing * trop_cyclone.TropCyclone.from_tracks: fix dist_coast type * climada.hazard.Centroids: raise exception in deprecated methods that really _are_ failing now * changelog: list added Centroids methods * hazard.centr: depreecated methods: pydoc string instaed of pass * fixing geodataframe issues * Revert "fixing geodataframe issues" This reverts commit d6d2859. * centroids: deprecation warnings improved --------- Co-authored-by: Chahan Kropf <[email protected]> Co-authored-by: climada <[email protected]> Co-authored-by: emanuel-schmid <[email protected]> Co-authored-by: Sarah Hülsen <[email protected]> Co-authored-by: Igor Detring <[email protected]> Co-authored-by: Lukas Riedel <[email protected]> Co-authored-by: Thomas Vogt <[email protected]> Co-authored-by: Thomas Vogt <[email protected]> Co-authored-by: Emanuel Schmid <[email protected]>
1 parent 7f74ea4 commit 0c9edcb

26 files changed

+2357
-2691
lines changed

CHANGELOG.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,62 @@ Code freeze date: YYYY-MM-DD
1010

1111
### Dependency Changes
1212

13-
### Added
14-
1513
### Changed
1614

15+
- Centroids complete overhaul. Most function should be backward compatible. Internal data is stored in a geodataframe attribute. Raster are now stored as points, and the meta attribute is removed. Several methds were deprecated or removed. [#787](https://github.com/CLIMADA-project/climada_python/pull/787)
1716
- Improved error messages produced by `ImpactCalc.impact()` in case impact function in the exposures is not found in impf_set [#863](https://github.com/CLIMADA-project/climada_python/pull/863)
1817

1918
### Fixed
2019

20+
### Added
21+
22+
- climada.hazard.centroids.centr.Centroids.get_area_pixel
23+
- climada.hazard.centroids.centr.Centroids.get_dist_coast
24+
- climada.hazard.centroids.centr.Centroids.get_elevation
25+
- climada.hazard.centroids.centr.Centroids.get_meta
26+
- climada.hazard.centroids.centr.Centroids.get_pixel_shapes
27+
- climada.hazard.centroids.centr.Centroids.to_crs
28+
- climada.hazard.centroids.centr.Centroids.to_default_crs
29+
- climada.hazard.centroids.centr.Centroids.write_csv
30+
- climada.hazard.centroids.centr.Centroids.write_excel
31+
2132
### Deprecated
2233

34+
- climada.hazard.centroids.centr.Centroids.from_lat_lon
35+
- climada.hazard.centroids.centr.Centroids.def set_area_pixel
36+
- climada.hazard.centroids.centr.Centroids.def set_area_approx
37+
- climada.hazard.centroids.centr.Centroids.set_dist_coast
38+
- climada.hazard.centroids.centr.Centroids.empty_geometry_points
39+
- climada.hazard.centroids.centr.Centroids.set_meta_to_lat_lon
40+
- climada.hazard.centroids.centr.Centroids.set_lat_lon_to_meta
41+
2342
### Removed
2443

44+
- climada.hazard.base.Hazard.clear
45+
- climada.hazard.base.Hazard.raster_to_vector
46+
- climada.hazard.base.Hazard.read_mat
47+
- climada.hazard.base.Hazard.reproject_raster
48+
- climada.hazard.base.Hazard.set_vector
49+
- climada.hazard.base.Hazard.vector_to_raster
50+
- climada.hazard.centroids.centr.Centroids.calc_pixels_polygons
51+
- climada.hazard.centroids.centr.Centroids.check
52+
- climada.hazard.centroids.centr.Centroids.clear
53+
- climada.hazard.centroids.centr.Centroids.equal
54+
- climada.hazard.centroids.centr.Centroids.from_mat
55+
- climada.hazard.centroids.centr.Centroids.from_base_grid
56+
- climada.hazard.centroids.centr.Centroids.read_excel
57+
- climada.hazard.centroids.centr.Centroids.read_hdf5
58+
- climada.hazard.centroids.centr.Centroids.read_mat
59+
- climada.hazard.centroids.centr.Centroids.set_elevation
60+
- climada.hazard.centroids.centr.Centroids.set_geometry_points
61+
- climada.hazard.centroids.centr.Centroids.set_lat_lon
62+
- climada.hazard.centroids.centr.Centroids.set_raster_file
63+
- climada.hazard.centroids.centr.Centroids.set_raster_from_pnt_bounds
64+
- climada.hazard.centroids.centr.Centroids.set_vector_file
65+
- climada.hazard.centroids.centr.Centroids.values_from_raster_files
66+
- climada.hazard.centroids.centr.Centroids.values_from_vector_files
67+
- climada.hazard.centroids.centr.generate_nat_earth_centroids
68+
2569
## 4.1.1
2670

2771
Release date: 2024-02-21

climada/engine/test/test_cost_benefit.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@
3535
from climada.test import get_test_file
3636

3737

38-
HAZ_TEST_MAT = get_test_file('atl_prob_no_name', file_format='matlab')
3938
ENT_TEST_MAT = get_test_file('demo_today', file_format='MAT-file')
40-
39+
HAZ_TEST_TC :Path = get_test_file('test_tc_florida')
4140

4241
class TestSteps(unittest.TestCase):
4342
"""Test intermediate steps"""
4443
def test_calc_impact_measures_pass(self):
4544
"""Test _calc_impact_measures against reference value"""
46-
self.assertTrue(HAZ_TEST_MAT.is_file(), "{} is not a file".format(HAZ_TEST_MAT))
47-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
45+
self.assertTrue(HAZ_TEST_TC.is_file(), "{} is not a file".format(HAZ_TEST_TC))
46+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
4847

4948
self.assertTrue(ENT_TEST_MAT.is_file(), "{} is not a file".format(ENT_TEST_MAT))
5049
entity = Entity.from_mat(ENT_TEST_MAT)
@@ -230,7 +229,7 @@ def test_cb_one_meas_fut_pass(self):
230229

231230
def test_calc_cb_no_change_pass(self):
232231
"""Test _calc_cost_benefit without present value against reference value"""
233-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
232+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
234233
entity = Entity.from_mat(ENT_TEST_MAT)
235234
entity.measures._data['TC'] = entity.measures._data.pop('XX')
236235
for meas in entity.measures.get_measure('TC'):
@@ -267,7 +266,7 @@ def test_calc_cb_no_change_pass(self):
267266

268267
def test_calc_cb_change_pass(self):
269268
"""Test _calc_cost_benefit with present value against reference value"""
270-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
269+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
271270
entity = Entity.from_mat(ENT_TEST_MAT)
272271
entity.measures._data['TC'] = entity.measures._data.pop('XX')
273272
for meas in entity.measures.get_measure('TC'):
@@ -438,7 +437,7 @@ def test_norm_value(self):
438437

439438
def test_combine_fut_pass(self):
440439
"""Test combine_measures with present and future"""
441-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
440+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
442441
entity = Entity.from_excel(ENT_DEMO_TODAY)
443442
entity.check()
444443
entity.exposures.ref_year = 2018
@@ -498,7 +497,7 @@ def test_combine_fut_pass(self):
498497

499498
def test_combine_current_pass(self):
500499
"""Test combine_measures with only future"""
501-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
500+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
502501
entity = Entity.from_excel(ENT_DEMO_TODAY)
503502
entity.check()
504503
entity.exposures.ref_year = 2018
@@ -538,7 +537,7 @@ def test_combine_current_pass(self):
538537

539538
def test_apply_transf_current_pass(self):
540539
"""Test apply_risk_transfer with only future"""
541-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
540+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
542541
entity = Entity.from_excel(ENT_DEMO_TODAY)
543542
entity.check()
544543
entity.exposures.ref_year = 2018
@@ -588,7 +587,7 @@ def test_apply_transf_current_pass(self):
588587

589588
def test_apply_transf_cost_fact_pass(self):
590589
"""Test apply_risk_transfer with only future annd cost factor"""
591-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
590+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
592591
entity = Entity.from_excel(ENT_DEMO_TODAY)
593592
entity.check()
594593
entity.exposures.ref_year = 2018
@@ -636,7 +635,7 @@ def test_apply_transf_cost_fact_pass(self):
636635

637636
def test_apply_transf_future_pass(self):
638637
"""Test apply_risk_transfer with present and future"""
639-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
638+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
640639
entity = Entity.from_excel(ENT_DEMO_TODAY)
641640
entity.check()
642641
entity.exposures.ref_year = 2018
@@ -692,7 +691,7 @@ def test_apply_transf_future_pass(self):
692691

693692
def test_remove_measure(self):
694693
"""Test remove_measure method"""
695-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
694+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
696695
entity = Entity.from_excel(ENT_DEMO_TODAY)
697696
entity.check()
698697
entity.exposures.ref_year = 2018
@@ -720,7 +719,7 @@ class TestCalc(unittest.TestCase):
720719
def test_calc_change_pass(self):
721720
"""Test calc with future change"""
722721
# present
723-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
722+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
724723
entity = Entity.from_excel(ENT_DEMO_TODAY)
725724
entity.exposures.gdf.rename(columns={'impf_': 'impf_TC'}, inplace=True)
726725
entity.check()
@@ -777,7 +776,7 @@ def test_calc_change_pass(self):
777776

778777
def test_calc_no_change_pass(self):
779778
"""Test calc without future change"""
780-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
779+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
781780
entity = Entity.from_excel(ENT_DEMO_TODAY)
782781
entity.check()
783782
entity.exposures.ref_year = 2018
@@ -808,7 +807,7 @@ class TestRiskFuncs(unittest.TestCase):
808807
def test_impact(self):
809808
ent = Entity.from_excel(ENT_DEMO_TODAY)
810809
ent.check()
811-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
810+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
812811
impact = ImpactCalc(ent.exposures, ent.impact_funcs, hazard).impact()
813812
return impact
814813

climada/engine/test/test_forecast.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,12 @@ def test_Forecast_plot(self):
110110
HAZ_DIR.joinpath('storm_europe_cosmoe_forecast_vmax_testfile.nc'),
111111
run_datetime=dt.datetime(2018,1,1),
112112
event_date=dt.datetime(2018,1,3))
113-
haz1.centroids.lat += 0.6
114-
haz1.centroids.lon -= 1.2
113+
haz1.centroids.gdf.geometry = haz1.centroids.gdf.geometry.translate(-1.2, 0.6)
115114
haz2 = StormEurope.from_cosmoe_file(
116115
HAZ_DIR.joinpath('storm_europe_cosmoe_forecast_vmax_testfile.nc'),
117116
run_datetime=dt.datetime(2018,1,1),
118117
event_date=dt.datetime(2018,1,3))
119-
haz2.centroids.lat += 0.6
120-
haz2.centroids.lon -= 1.2
118+
haz2.centroids.gdf.geometry = haz2.centroids.gdf.geometry.translate(-1.2, 0.6)
121119
#exposure
122120
data = {}
123121
data['latitude'] = haz1.centroids.lat

climada/entity/exposures/base.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,6 @@ def from_raster(cls, file_name, band=1, src_crs=None, window=None,
498498
--------
499499
Exposures
500500
"""
501-
exp = cls()
502501
meta, value = u_coord.read_raster(file_name, [band], src_crs, window,
503502
geometry, dst_crs, transform, width,
504503
height, resampling)
@@ -507,14 +506,16 @@ def from_raster(cls, file_name, band=1, src_crs=None, window=None,
507506
lry = uly + meta['height'] * yres
508507
x_grid, y_grid = np.meshgrid(np.arange(ulx + xres / 2, lrx, xres),
509508
np.arange(uly + yres / 2, lry, yres))
509+
return cls(
510+
{
511+
'longitude': x_grid.flatten(),
512+
'latitude': y_grid.flatten(),
513+
'value': value.reshape(-1),
514+
},
515+
meta=meta,
516+
crs=meta['crs'],
517+
)
510518

511-
if exp.crs is None:
512-
exp.set_crs()
513-
exp.gdf['longitude'] = x_grid.flatten()
514-
exp.gdf['latitude'] = y_grid.flatten()
515-
exp.gdf['value'] = value.reshape(-1)
516-
exp.meta = meta
517-
return exp
518519

519520
def plot_scatter(self, mask=None, ignore_zero=False, pop_name=True,
520521
buffer=0.0, extend='neither', axis=None, figsize=(9, 13),

climada/entity/exposures/test/test_base.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def test_assign_pass(self):
6060
np_rand = np.random.RandomState(123456789)
6161

6262
haz = Hazard.from_raster([HAZ_DEMO_FL], haz_type='FL', window=Window(10, 20, 50, 60))
63-
haz.raster_to_vector()
6463
ncentroids = haz.centroids.size
6564

6665
exp = Exposures(crs=haz.centroids.crs)
@@ -74,8 +73,8 @@ def test_assign_pass(self):
7473

7574
# make sure that it works for both float32 and float64
7675
for test_dtype in [np.float64, np.float32]:
77-
haz.centroids.lat = haz.centroids.lat.astype(test_dtype)
78-
haz.centroids.lon = haz.centroids.lon.astype(test_dtype)
76+
haz.centroids.gdf["lat"] = haz.centroids.lat.astype(test_dtype)
77+
haz.centroids.gdf["lon"] = haz.centroids.lon.astype(test_dtype)
7978
exp.assign_centroids(haz)
8079
self.assertEqual(exp.gdf.shape[0], len(exp.gdf[INDICATOR_CENTR + 'FL']))
8180
np.testing.assert_array_equal(exp.gdf[INDICATOR_CENTR + 'FL'].values, expected_result)
@@ -131,7 +130,7 @@ def test_assign_raster_pass(self):
131130
'width': 20, 'height': 10,
132131
'transform': rasterio.Affine(1.5, 0.0, -20, 0.0, -1.4, 8)
133132
}
134-
haz = Hazard('FL', centroids=Centroids(meta=meta))
133+
haz = Hazard('FL', centroids=Centroids.from_meta(meta))
135134

136135
# explicit points with known results (see `expected_result` for details)
137136
exp = Exposures(crs=DEF_CRS)
@@ -151,9 +150,9 @@ def test_assign_raster_pass(self):
151150

152151
expected_result = [
153152
# constant y-value, varying x-value
154-
-1, 0, 0, 0, 0, 1,
153+
0, 0, 0, 0, 0, 1,
155154
# constant x-value, varying y-value
156-
-1, 0, 0, 20,
155+
0, 0, 0, 20,
157156
# out of bounds: topleft, top, topright, right, bottomright, bottom, bottomleft, left
158157
-1, -1, -1, -1, -1, -1, -1, -1,
159158
# some explicit points within the raster
@@ -171,18 +170,20 @@ def test_assign_raster_same_pass(self):
171170
np.testing.assert_array_equal(exp.gdf[INDICATOR_CENTR + 'FL'].values,
172171
np.arange(haz.centroids.size, dtype=int))
173172

173+
# Test fails because exposures stores the crs in the meta attribute as rasterio object,
174+
# while the centroids stores the crs in the geodataframe, which is not a rasterio object.
175+
# The comparison in assign_centroids then fails.
174176
def test_assign_large_hazard_subset_pass(self):
175177
"""Test assign_centroids with raster hazard"""
176178
exp = Exposures.from_raster(HAZ_DEMO_FL, window=Window(10, 20, 50, 60))
177179
exp.gdf.latitude[[0, 1]] = exp.gdf.latitude[[1, 0]]
178180
exp.gdf.longitude[[0, 1]] = exp.gdf.longitude[[1, 0]]
179181
exp.check()
180182
haz = Hazard.from_raster([HAZ_DEMO_FL], haz_type='FL')
181-
haz.raster_to_vector()
182183
exp.assign_centroids(haz)
183184
assigned_centroids = haz.centroids.select(sel_cen=exp.gdf[INDICATOR_CENTR + 'FL'].values)
184-
np.testing.assert_array_equal(assigned_centroids.lat, exp.gdf.latitude)
185-
np.testing.assert_array_equal(assigned_centroids.lon, exp.gdf.longitude)
185+
np.testing.assert_array_equal(np.unique(assigned_centroids.lat), np.unique(exp.gdf.latitude))
186+
np.testing.assert_array_equal(np.unique(assigned_centroids.lon), np.unique(exp.gdf.longitude))
186187

187188
def test_affected_total_value(self):
188189
haz_type = "RF"

climada/entity/measures/test/test_base.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939

4040
DATA_DIR = CONFIG.measures.test_data.dir()
4141

42-
HAZ_TEST_MAT = get_test_file('atl_prob_no_name', file_format='matlab')
42+
HAZ_TEST_TC :Path = get_test_file('test_tc_florida', file_format='hdf5')
43+
"""
44+
Hazard test file from Data API: Hurricanes from 1851 to 2011 over Florida with 100 centroids.
45+
Fraction is empty. Format: HDF5.
46+
"""
4347
ENT_TEST_MAT = Path(exposures_test.__file__).parent / 'data' / 'demo_today.mat'
4448

4549
class TestApply(unittest.TestCase):
@@ -78,7 +82,7 @@ def test_cutoff_hazard_pass(self):
7882
meas = MeasureSet.from_mat(ENT_TEST_MAT)
7983
act_1 = meas.get_measure(name='Seawall')[0]
8084

81-
haz = Hazard.from_mat(HAZ_TEST_MAT)
85+
haz = Hazard.from_hdf5(HAZ_TEST_TC)
8286
exp = Exposures.from_mat(ENT_TEST_MAT)
8387
exp.gdf.rename(columns={'impf': 'impf_TC'}, inplace=True)
8488
exp.check()
@@ -112,7 +116,7 @@ def test_cutoff_hazard_region_pass(self):
112116
act_1 = meas.get_measure(name='Seawall')[0]
113117
act_1.exp_region_id = [1]
114118

115-
haz = Hazard.from_mat(HAZ_TEST_MAT)
119+
haz = Hazard.from_hdf5(HAZ_TEST_TC)
116120
exp = Exposures.from_mat(ENT_TEST_MAT)
117121
exp.gdf['region_id'] = np.zeros(exp.gdf.shape[0])
118122
exp.gdf.region_id.values[10:] = 1
@@ -246,7 +250,7 @@ def test_filter_exposures_pass(self):
246250

247251
imp_set = ImpactFuncSet.from_mat(ENT_TEST_MAT)
248252

249-
haz = Hazard.from_mat(HAZ_TEST_MAT)
253+
haz = Hazard.from_hdf5(HAZ_TEST_TC)
250254
exp.assign_centroids(haz)
251255

252256
new_exp = copy.deepcopy(exp)
@@ -329,7 +333,7 @@ def test_filter_exposures_pass(self):
329333

330334
def test_apply_ref_pass(self):
331335
"""Test apply method: apply all measures but insurance"""
332-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
336+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
333337

334338
entity = Entity.from_mat(ENT_TEST_MAT)
335339
entity.measures._data['TC'] = entity.measures._data.pop('XX')
@@ -365,7 +369,7 @@ def test_apply_ref_pass(self):
365369
def test_calc_impact_pass(self):
366370
"""Test calc_impact method: apply all measures but insurance"""
367371

368-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
372+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
369373

370374
entity = Entity.from_mat(ENT_TEST_MAT)
371375
entity.exposures.gdf.rename(columns={'impf': 'impf_TC'}, inplace=True)
@@ -399,7 +403,7 @@ def test_calc_impact_pass(self):
399403
def test_calc_impact_transf_pass(self):
400404
"""Test calc_impact method: apply all measures and insurance"""
401405

402-
hazard = Hazard.from_mat(HAZ_TEST_MAT)
406+
hazard = Hazard.from_hdf5(HAZ_TEST_TC)
403407

404408
entity = Entity.from_mat(ENT_TEST_MAT)
405409
entity.exposures.gdf.rename(columns={'impf': 'impf_TC'}, inplace=True)

0 commit comments

Comments
 (0)