Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Simionato committed Feb 8, 2025
1 parent d540b4b commit 1e780bd
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions utils/fix_consequences
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,21 @@ 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')
for conseq in consequences:
cons = pandas.read_csv(conseq).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
df = pandas.concat(dfs)
df.to_csv(conseq)

main.taxonomy_mapping = 'filename'
main.consequences = dict(help='files', nargs='+')
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__':
Expand Down

0 comments on commit 1e780bd

Please sign in to comment.