-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10324 from gem/fix_consequences
Added utils/fix_consequences
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |