Skip to content

Commit bf30646

Browse files
Fix bug in centroids natural earth feature test (#768)
* Fix natural earth data test in centroids * Pass argument as list. * Make sure `allclose` does not throw a broadcasting error. * Update CHANGELOG.md --------- Co-authored-by: Emanuel Schmid <[email protected]>
1 parent 9a30fd4 commit bf30646

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Removed:
6262

6363
- `util.lines_polys_handler` solve polygon disaggregation issue in metre-based projection [#666](https://github.com/CLIMADA-project/climada_python/pull/666)
6464
- Problem with `pyproj.CRS` as `Impact` attribute, [#706](https://github.com/CLIMADA-project/climada_python/issues/706). Now CRS is always stored as `str` in WKT format.
65+
- Correctly handle assertion errors in `Centroids.values_from_vector_files` and fix the associated test [#768](https://github.com/CLIMADA-project/climada_python/pull/768/)
6566

6667
### Deprecated
6768

climada/hazard/centroids/centr.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -575,19 +575,24 @@ def values_from_vector_files(self, file_names, val_names=None, dst_crs=None):
575575
Sparse array of shape (len(val_name), len(geometry)).
576576
"""
577577
if val_names is None:
578-
val_names = ['intensity']
578+
val_names = ["intensity"]
579579

580580
values = []
581581
for file_name in file_names:
582582
tmp_lat, tmp_lon, tmp_geometry, data = u_coord.read_vector(
583-
file_name, val_names, dst_crs=dst_crs)
584-
if not (u_coord.equal_crs(tmp_geometry.crs, self.geometry.crs)
585-
and np.allclose(tmp_lat, self.lat)
586-
and np.allclose(tmp_lon, self.lon)):
587-
raise ValueError('Vector data inconsistent with contained vector.')
583+
file_name, val_names, dst_crs=dst_crs
584+
)
585+
try:
586+
assert u_coord.equal_crs(tmp_geometry.crs, self.geometry.crs)
587+
np.testing.assert_allclose(tmp_lat, self.lat)
588+
np.testing.assert_allclose(tmp_lon, self.lon)
589+
except AssertionError as exc:
590+
raise ValueError(
591+
"Vector data inconsistent with contained vector"
592+
) from exc
588593
values.append(sparse.csr_matrix(data))
589594

590-
return sparse.vstack(values, format='csr')
595+
return sparse.vstack(values, format="csr")
591596

592597
def read_mat(self, *args, **kwargs):
593598
"""This function is deprecated, use Centroids.from_mat instead."""

climada/hazard/centroids/test/test_vec_ras.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,11 @@ def test_from_vector_file(self):
617617
# Test reading values from file with incompatible geometry
618618
shp_file = shapereader.natural_earth(resolution='10m', category='cultural',
619619
name='populated_places_simple')
620-
with self.assertRaises(ValueError) as ve:
620+
with self.assertRaises(ValueError) as cm:
621621
centr.values_from_vector_files([shp_file], val_names=['pop_min', 'pop_max'])
622-
self.assertIn('could not be broadcast together with shapes', str(ve.exception))
622+
self.assertIn(
623+
"Vector data inconsistent with contained vector", str(cm.exception)
624+
)
623625

624626
def test_from_raster_file_wrong_fail(self):
625627
"""Test from_raster_file with wrong centroids"""

0 commit comments

Comments
 (0)