Skip to content

Commit

Permalink
Merge pull request #10324 from gem/fix_consequences
Browse files Browse the repository at this point in the history
Added utils/fix_consequences
  • Loading branch information
micheles authored Feb 8, 2025
2 parents 054d4ce + 1e780bd commit 37122de
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[Michele Simionato]
* Added utils/fix_consequences to fix consequence.csv files
containing risk_ids rather than taxonomies, now invalid
* Implemented quantiles in scenario_risk, visible in aggrisk_tags
* Added checks on the consequence functions across taxonomies and perils
* Fixed the taxonomy field in consequences.csv to be the exposure
Expand Down
42 changes: 42 additions & 0 deletions utils/fix_consequences
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright (C) 2025, GEM Foundation
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.

import pandas
from openquake.baselib import sap


# fix old consequence functions before engine version 3.23
def main(taxonomy_mapping: str, consequences: str):
"""
Use the taxonomy mapping to upgrade risk_id-based consequence functions
to taxonomy-based consequence functions
"""
tmap = pandas.read_csv(taxonomy_mapping).set_index('conversion')
cons = pandas.read_csv(consequences).set_index('taxonomy')
cols = ['taxonomy'] + list(cons.columns)
df = cons.join(tmap, how='inner')[cols]
df.index.names = ['risk_id']
dfs = []
for _, grp in df.groupby(['taxonomy', 'consequence', 'loss_type']):
dfs.append(grp[0:1]) # take the first row
pandas.concat(dfs).to_csv(consequences)


if __name__ == '__main__':
sap.run(main)

0 comments on commit 37122de

Please sign in to comment.