Skip to content

Commit

Permalink
exposures: don't do anything in check, update Tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuel-schmid committed Oct 4, 2024
1 parent ed9d6ce commit e1800f5
Show file tree
Hide file tree
Showing 5 changed files with 585 additions and 690 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ Code freeze date: YYYY-MM-DD

- In `climada.util.plot.geo_im_from_array`, NaNs are plotted in gray while cells with no centroid are not plotted [#929](https://github.com/CLIMADA-project/climada_python/pull/929)
- Renamed `climada.util.plot.subplots_from_gdf` to `climada.util.plot.plot_from_gdf` [#929](https://github.com/CLIMADA-project/climada_python/pull/929)
- Exposures complete overhaul. Notably the _geometry_ column of the inherent `GeoDataFrame` is set up at initialization, while latitude and longitude column are no longer present there (the according arrays can be retrieved as properties of the Exposures object: `exp.latitude` instead of `exp.gdf.latitude.values`).
- Exposures complete overhaul. Notably
- the _geometry_ column of the inherent `GeoDataFrame` is set up at initialization
- latitude and longitude column are no longer present there (the according arrays can be retrieved as properties of the Exposures object: `exp.latitude` instead of `exp.gdf.latitude.values`).
- `Exposures.gdf` has been renamed to `Exposures.data` (it still works though, as it is a property now pointing to the latter)
- the `check` method does not add a default "IMPF_" column to the GeoDataFrame anymore

### Fixed

Expand Down
1 change: 1 addition & 0 deletions climada/engine/impact_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def impact(self, save_mat=True, assign_centroids=True,
apply_deductible_to_mat : apply deductible to impact matrix
apply_cover_to_mat : apply cover to impact matrix
"""
# TODO: consider refactoring, making use of Exposures.hazard_impf
# check for compatibility of exposures and hazard type
if all(name not in self.exposures.gdf.columns for
name in ['if_', f'if_{self.hazard.haz_type}',
Expand Down
14 changes: 6 additions & 8 deletions climada/entity/exposures/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,8 @@ def hazard_centroids(self, haz_type=""):
return self.data[INDICATOR_CENTR].values
raise ValueError("Missing hazard centroids.")

Check warning on line 227 in climada/entity/exposures/base.py

View check run for this annotation

Jenkins - WCR / Code Coverage

Not covered line

Line 227 is not covered by tests

@property
def _meta(self):
"""Metadata dictionary, containing raster information derived from geometry"""
def derive_raster(self):
"""Metadata dictionary, containing raster information, derived from the geometry"""
if not self.data.size:
return None
_r, meta = u_coord.points_to_raster(self.data)
Expand Down Expand Up @@ -388,7 +387,9 @@ def __init__(self,
def __str__(self):
return '\n'.join(
[f"{md}: {self.__dict__[md]}" for md in type(self)._metadata] +
[f"crs: {self.crs}", "data:", str(self.gdf)]
[f"crs: {self.crs}", f"data: ({self.data.shape[0]} entries)",
str(self.data) if self.data.shape[0] < 10 else
str(pd.concat([self.data[:4], self.data[-4:]]))]
)

def _access_item(self, *args):
Expand All @@ -403,8 +404,6 @@ def check(self):
"""Check Exposures consistency.
Reports missing columns in log messages.
If no ``impf_*`` column is present in the dataframe, a default column ``impf_`` is added
with default impact function id 1.
"""
# mandatory columns
for var in self.vars_oblig:
Expand All @@ -427,8 +426,7 @@ def check(self):
col for col in self.gdf.columns
if col.startswith(INDICATOR_IMPF) or col.startswith(INDICATOR_IMPF_OLD)
]:
LOGGER.info("Setting %s to default impact functions ids 1.", INDICATOR_IMPF)
self.gdf[INDICATOR_IMPF] = 1
LOGGER.warning("There are no impact functions assigned to the exposures")

# optional columns except centr_*
for var in sorted(set(self.vars_opt).difference([INDICATOR_CENTR])):
Expand Down
12 changes: 5 additions & 7 deletions climada/entity/exposures/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,30 +532,28 @@ def test_get_impf_column(self):
self.assertRaises(ValueError, expo.get_impf_column, 'HAZ')

# removed impf column
expo.gdf.drop(columns='impf_NA', inplace=True)
expo.data.drop(columns='impf_NA', inplace=True)
self.assertRaises(ValueError, expo.get_impf_column, 'NA')
self.assertRaises(ValueError, expo.get_impf_column)

# default (anonymous) impf column
expo.check()
expo.data['impf_'] = 1
self.assertEqual('impf_', expo.get_impf_column())
self.assertEqual('impf_', expo.get_impf_column('HAZ'))

# rename impf column to old style column name
expo.gdf.rename(columns={'impf_': 'if_'}, inplace=True)
expo.check()
expo.data.rename(columns={'impf_': 'if_'}, inplace=True)
self.assertEqual('if_', expo.get_impf_column())
self.assertEqual('if_', expo.get_impf_column('HAZ'))

# rename impf column to old style column name
expo.gdf.rename(columns={'if_': 'if_NA'}, inplace=True)
expo.check()
expo.data.rename(columns={'if_': 'if_NA'}, inplace=True)
self.assertEqual('if_NA', expo.get_impf_column('NA'))
self.assertRaises(ValueError, expo.get_impf_column)
self.assertRaises(ValueError, expo.get_impf_column, 'HAZ')

# add anonymous impf column
expo.gdf['impf_'] = expo.region_id
expo.data['impf_'] = expo.region_id
self.assertEqual('if_NA', expo.get_impf_column('NA'))
self.assertEqual('impf_', expo.get_impf_column())
self.assertEqual('impf_', expo.get_impf_column('HAZ'))
Expand Down
Loading

0 comments on commit e1800f5

Please sign in to comment.