Skip to content

Commit c2dd03d

Browse files
authored
Merge pull request #10505 from gem/conseq_with_taxmap
Backported fix to taxonomy mapping with consequences
2 parents 6cd2477 + 4f9d908 commit c2dd03d

5 files changed

Lines changed: 35 additions & 45 deletions

File tree

debian/changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[Michele Simionato]
2+
* Backported fix to taxonomy mapping with consequences
23
* Backported fix to 64 bit poes critical for multifault sources
34
* Backported fix to workerpool critical for zmq clusters
45

openquake/commonlib/readinput.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,8 @@ def get_crmodel(oqparam):
10531053
# build consdict of the form consequence_by_tagname -> tag -> array
10541054
loss_dt = oqparam.loss_dt()
10551055
for by, fnames in oqparam.inputs['consequence'].items():
1056+
if by == 'risk_id':
1057+
by = 'taxonomy'
10561058
if isinstance(fnames, str): # single file
10571059
fnames = [fnames]
10581060
# i.e. files collapsed.csv, fatalities.csv, ... with headers like
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
taxonomy,consequence,peril,slight,moderate,extensive,complete
2-
MUR-CLBRS-LWAL-HBET-1;2/GOV2,losses,groundshaking,35,90,197,287
2+
UNM/C_LR/GOV2,losses,groundshaking,35,90,197,287
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
taxonomy,consequence,peril,slight,moderate,extreme,complete
2-
Concrete1,losses,groundshaking,0.04,0.31,0.6,1
3-
Wood1,losses,groundshaking,0.04,0.31,0.6,1
4-
Concrete1,losses,liquefaction,0,0,0,1
5-
Wood1,losses,liquefaction,0,0,0,1
6-
Concrete1,losses,landslide,0,0,0,1
7-
Wood1,losses,landslide,0,0,0,1
2+
Concrete,losses,groundshaking,0.04,0.31,0.6,1
3+
Wood,losses,groundshaking,0.04,0.31,0.6,1
4+
Concrete,losses,liquefaction,0,0,0,1
5+
Wood,losses,liquefaction,0,0,0,1
6+
Concrete,losses,landslide,0,0,0,1
7+
Wood,losses,landslide,0,0,0,1

openquake/risklib/riskmodels.py

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import re
1919
import json
2020
import copy
21-
import logging
2221
import functools
2322
import collections
2423
import numpy
@@ -140,7 +139,7 @@ def groupby_id(self):
140139
ddic = AccumDict(accum=AccumDict(accum=[]))
141140
dic = AccumDict(accum=[])
142141
for rf in self:
143-
dic[rf.id, rf.peril].append(rf)
142+
dic[rf.id, getattr(rf, 'peril', 'groundshaking')].append(rf)
144143
for (riskid, peril), rfs in dic.items():
145144
ddic[riskid][peril] = group_by_lt(rfs)
146145
num_perils = {riskid: len(ddic[riskid]) for riskid in ddic}
@@ -525,33 +524,6 @@ class ValidationError(Exception):
525524
pass
526525

527526

528-
# TODO: if the consequence tag is different from "taxonomy", pass
529-
# the values of the exposure for that tag
530-
def check_consequences(fname, taxonomies, perils):
531-
"""
532-
Check that the taxonomy field (if any) and the peril field (if any)
533-
in the consequence file are consistent with the expected taxonomies
534-
and perils
535-
"""
536-
df = pandas.read_csv(fname)
537-
if 'taxonomy' in df.columns:
538-
csq_taxonomies = set(df['taxonomy'])
539-
extra = csq_taxonomies - taxonomies
540-
missing = taxonomies - csq_taxonomies
541-
if not csq_taxonomies & taxonomies:
542-
raise InvalidFile(f'{fname}: no matching taxonomies')
543-
elif missing:
544-
raise InvalidFile(f'{fname}: missing taxonomies {missing}')
545-
elif extra:
546-
# tested in event_based_damage/case_15
547-
logging.warning(f'In {fname} there are extra taxonomies missing '
548-
f'in the exposure: {extra}')
549-
if 'peril' in df.columns:
550-
for line, peril in enumerate(df['peril'], 1):
551-
if peril not in perils:
552-
raise InvalidFile(f'{fname}: unknown {peril=} at {line=}')
553-
554-
555527
class CompositeRiskModel(collections.abc.Mapping):
556528
"""
557529
A container (riskid, kind) -> riskmodel
@@ -624,12 +596,27 @@ def set_tmap(self, tmap_df, taxidx):
624596
else:
625597
csq_files.append(fnames)
626598
for fname in csq_files:
627-
check_consequences(fname, set(taxidx), self.perils)
628-
for byname, coeffs in self.consdict.items():
629-
# reduce the consdict to the taxonomies in the exposure
630-
self.consdict[byname] = {taxidx[taxo]: arr
631-
for taxo, arr in coeffs.items()
632-
if taxo in taxidx}
599+
df = pandas.read_csv(fname)
600+
if 'peril' in df.columns:
601+
for line, peril in enumerate(df['peril'], 1):
602+
if peril not in self.perils:
603+
raise InvalidFile(
604+
f'{fname}: unknown {peril=} at {line=}')
605+
606+
cfs = '\n'.join(csq_files)
607+
df = self.tmap_df
608+
for peril in self.perils:
609+
for byname, coeffs in self.consdict.items():
610+
# ex. byname = "losses_by_taxonomy"
611+
if len(coeffs):
612+
for per, risk_id, weight in zip(
613+
df.peril, df.risk_id, df.weight):
614+
if (per == '*' or per == peril) and risk_id != '?':
615+
try:
616+
coeffs[risk_id][peril]
617+
except KeyError:
618+
raise InvalidFile(
619+
f'Missing {risk_id=}, {peril=} in\n{cfs}')
633620

634621
def check_risk_ids(self, inputs):
635622
"""
@@ -639,7 +626,8 @@ def check_risk_ids(self, inputs):
639626
for riskfunc in self.risklist:
640627
ids_by_kind[riskfunc.kind].add(riskfunc.id)
641628
kinds = tuple(ids_by_kind) # vulnerability, fragility, ...
642-
fnames = [fname for kind, fname in inputs.items() if kind.endswith(kinds)]
629+
fnames = [fname for kind, fname in inputs.items()
630+
if kind.endswith(kinds)]
643631
if len(ids_by_kind) > 1:
644632
k = next(iter(ids_by_kind))
645633
base_ids = set(ids_by_kind.pop(k))
@@ -687,7 +675,6 @@ def compute_csq(self, assets, dd5, tmap_df, oq):
687675
:returns: a dict consequence_name, loss_type -> array[P, A, E]
688676
"""
689677
# by construction all assets have the same taxonomy
690-
taxi = assets[0]['taxonomy']
691678
P, A, E, _L, _D = dd5.shape
692679
csq = AccumDict(accum=numpy.zeros((P, A, E)))
693680
for byname, coeffs in self.consdict.items():
@@ -703,7 +690,7 @@ def compute_csq(self, assets, dd5, tmap_df, oq):
703690
else: # assume one weigth per peril
704691
[w] = df[df.peril == peril].weight
705692
coeff = (dd5[pi, :, :, li, 1:] @
706-
coeffs[taxi][peril][lt] * w)
693+
coeffs[risk_id][peril][lt] * w)
707694
cAE = scientific.consequence(
708695
consequence, assets, coeff, lt, oq.time_event)
709696
csq[consequence, li][pi] += cAE

0 commit comments

Comments
 (0)