Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the max(coeffs) strategy in the consequences #10122

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions openquake/calculators/event_based_damage.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ def _gen_dd3(asset_df, gmf_df, crmodel, dparam, mon):
else:
dd4[p, a, :, d] *= dprobs

df = crmodel.tmap_df[crmodel.tmap_df.taxi == assets[0]['taxonomy']]
# compose damage distributions
if P > 1:
# compose damage distributions
dd3 = numpy.empty(dd4.shape[1:])
for a in range(A):
for e in range(E):
dd3[a, e, :D] = scientific.compose_dds(dd4[:, a, e, :D])
else:
dd3 = dd4[0]
df = crmodel.tmap_df[crmodel.tmap_df.taxi == assets[0]['taxonomy']]
csq = crmodel.compute_csq(assets, dd4[:, :, :, :D], df, oq)
for name, values in csq.items():
dd3[:, :, dparam.csqidx[name]] = values
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-git30e245f39e', start_date='2024-11-05T09:13:45', checksum=525960216, investigation_time=None, risk_investigation_time=None"
#,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-git57ee5a27d0', start_date='2024-11-06T08:27:37', checksum=525960216, investigation_time=None, risk_investigation_time=None"
loss_type,parent_id,rlz_id,no_damage,slight,moderate,extensive,complete,non_operational_value,non_operational_ratio
structural,A,0,1.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00
structural,A,1,1.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00
structural,B,0,9.00000E-01,0.00000E+00,0.00000E+00,0.00000E+00,1.00000E-01,3.00000E-01,3.00000E-01
structural,B,1,8.00000E-01,0.00000E+00,0.00000E+00,0.00000E+00,2.00000E-01,4.00000E-01,4.00000E-01
structural,E1,0,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,1.00000E+00,3.00000E+00,3.00000E+00
structural,E1,1,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,1.00000E+00,3.00000E+00,3.00000E+00
structural,B,0,9.00000E-01,0.00000E+00,0.00000E+00,0.00000E+00,1.00000E-01,1.00000E-01,1.00000E-01
structural,B,1,8.00000E-01,0.00000E+00,0.00000E+00,0.00000E+00,2.00000E-01,2.00000E-01,2.00000E-01
structural,E1,0,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,1.00000E+00,1.00000E+00,1.00000E+00
structural,E1,1,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,1.00000E+00,1.00000E+00,1.00000E+00
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#,,,,,,,,"generated_by='OpenQuake engine 3.22.0-git30e245f39e', start_date='2024-11-05T09:13:45', checksum=525960216, investigation_time=None, risk_investigation_time=None"
#,,,,,,,,"generated_by='OpenQuake engine 3.22.0-git57ee5a27d0', start_date='2024-11-06T08:27:37', checksum=525960216, investigation_time=None, risk_investigation_time=None"
loss_type,rlz_id,no_damage,slight,moderate,extensive,complete,non_operational_value,non_operational_ratio
structural,0,1.90000E+00,0.00000E+00,0.00000E+00,0.00000E+00,1.10000E+00,3.30000E+00,1.10000E+00
structural,1,1.80000E+00,0.00000E+00,0.00000E+00,0.00000E+00,1.20000E+00,3.40000E+00,1.13333E+00
structural,0,1.90000E+00,0.00000E+00,0.00000E+00,0.00000E+00,1.10000E+00,1.10000E+00,3.66667E-01
structural,1,1.80000E+00,0.00000E+00,0.00000E+00,0.00000E+00,1.20000E+00,1.20000E+00,4.00000E-01
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#,,,,,,,"generated_by='OpenQuake engine 3.22.0-git795c396951', start_date='2024-11-05T10:07:11', checksum=1872246383, investigation_time=None, risk_investigation_time=None"
#,,,,,,,"generated_by='OpenQuake engine 3.22.0-git57ee5a27d0', start_date='2024-11-06T08:27:37', checksum=1872246383, investigation_time=None, risk_investigation_time=None"
loss_type,no_damage,slight,moderate,extreme,complete,losses_value,losses_ratio
structural,9.10790E+00,1.04866E+00,8.59773E-01,4.06873E-01,3.57679E+00,1.84217E+04,1.08299E-01
structural,9.10790E+00,1.04866E+00,8.59773E-01,4.06873E-01,3.57679E+00,3.17694E+04,1.86769E-01
14 changes: 7 additions & 7 deletions openquake/qa_tests_data/scenario_damage/case_22/expected/dmg.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#,,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-git795c396951', start_date='2024-11-05T10:07:11', checksum=1872246383"
#,,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-git57ee5a27d0', start_date='2024-11-06T08:27:37', checksum=1872246383"
asset_id,NAME_1,taxonomy,lon,lat,no_damage,slight,moderate,extreme,complete,losses
a1,a,Wood1,83.31382,29.46117,7.801411E-01,1.301565E-01,7.167403E-02,1.495041E-02,3.078026E-03,4.476292E+02
a2,a,Wood1,83.31382,29.23617,7.971102E-01,1.201108E-01,6.614210E-02,1.379650E-02,2.840465E-03,4.130804E+02
Expand All @@ -7,11 +7,11 @@ a4,a,Wood1,80.68882,28.93617,1.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00
a5,a,Wood1,83.53882,29.01117,5.816740E-01,2.215957E-01,1.431113E-01,3.980681E-02,1.381211E-02,1.031084E+03
a6,a,Wood1,81.13882,28.78617,1.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00
a7,a,Wood1,83.98882,28.48617,2.732201E-01,2.542852E-01,2.626550E-01,1.192409E-01,9.059871E-02,2.763948E+03
a8,a,Concrete1,83.23882,29.38617,4.304808E-01,2.356633E-02,3.248136E-02,2.912861E-02,4.843429E-01,1.620410E+03
a9,a,Concrete1,83.01382,29.08617,3.883127E-01,3.027743E-02,4.171199E-02,3.736362E-02,5.023342E-01,1.518432E+03
a10,a,Concrete1,83.31382,28.71117,1.177844E-01,2.779621E-02,3.839571E-02,3.461971E-02,7.814040E-01,3.197053E+03
a11,a,Concrete1,86.91382,27.73617,5.506500E-01,2.571168E-02,3.542192E-02,3.172928E-02,3.564871E-01,8.564750E+02
a12,a,Concrete1,83.16382,29.31117,4.493183E-01,3.425396E-02,4.719029E-02,4.227083E-02,4.269666E-01,1.279804E+03
a8,a,Concrete1,83.23882,29.38617,4.304808E-01,2.356633E-02,3.248136E-02,2.912861E-02,4.843429E-01,3.547600E+03
a9,a,Concrete1,83.01382,29.08617,3.883127E-01,3.027743E-02,4.171199E-02,3.736362E-02,5.023342E-01,3.339751E+03
a10,a,Concrete1,83.31382,28.71117,1.177844E-01,2.779621E-02,3.839571E-02,3.461971E-02,7.814040E-01,6.332979E+03
a11,a,Concrete1,86.91382,27.73617,5.506500E-01,2.571168E-02,3.542192E-02,3.172928E-02,3.564871E-01,2.155821E+03
a12,a,Concrete1,83.16382,29.31117,4.493183E-01,3.425396E-02,4.719029E-02,4.227083E-02,4.269666E-01,2.653371E+03
a13,a,Concrete1,80.61382,28.93617,1.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00
a14,a,Concrete1,83.91382,29.01117,3.962755E-02,1.441644E-02,2.011603E-02,1.858578E-02,9.072542E-01,4.603935E+03
a14,a,Concrete1,83.91382,29.01117,3.962755E-02,1.441644E-02,2.011603E-02,1.858578E-02,9.072542E-01,8.394333E+03
a15,a,Concrete1,82.03882,30.28617,1.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00
10 changes: 6 additions & 4 deletions openquake/risklib/riskmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ class ValidationError(Exception):
pass


def get_cdict(fractions, coeffs, df, perils):
def get_cdict(fractions, coeffs, df, loss_type, perils):
"""
:returns: a dict peril -> array of shape (A, E)
"""
Expand All @@ -510,7 +510,7 @@ def get_cdict(fractions, coeffs, df, perils):
for per, risk_id, weight in zip(df.peril, df.risk_id, df.weight):
cs = coeffs[risk_id]
if per == peril or per == '*':
cdict[peril] = fractions[pi] @ cs[peril]['structural'] * weight
cdict[peril] = fractions[pi] @ cs[peril][loss_type] * weight
return cdict


Expand Down Expand Up @@ -656,15 +656,17 @@ def compute_csq(self, assets, fractions, tmap_df, oq):
"""
_L, A, E, _D = fractions.shape
csq = AccumDict(accum=numpy.zeros((A, E)))
[lt] = oq.loss_types
for byname, coeffs in self.consdict.items():
# ex. byname = "losses_by_taxonomy"
if len(coeffs):
consequence, _tagname = byname.split('_by_')
# by construction all assets have the same taxonomy
for risk_id, df in tmap_df.groupby('risk_id'):
cdict = get_cdict(fractions[:, :, :, 1:], coeffs, df, self.perils)
cdict = get_cdict(fractions[:, :, :, 1:], coeffs, df, lt, self.perils)
# array peril -> coeffs for the given loss type
csq[consequence] += scientific.consequence(
consequence, assets, cdict, oq.total_loss_types, oq.time_event)
consequence, assets, cdict, lt, oq.time_event)
return csq

def init(self):
Expand Down
32 changes: 13 additions & 19 deletions openquake/risklib/scientific.py
Original file line number Diff line number Diff line change
Expand Up @@ -1738,45 +1738,39 @@ def pprint(self):

# ####################### Consequences ##################################### #

def _max(lossdic):
if len(lossdic) == 1:
res = lossdic[next(iter(lossdic))]
def _max(coeffs):
perils = list(coeffs)
if len(perils) == 1:
res = coeffs[perils[0]]
else:
res = numpy.array([lossdic[peril] for peril in lossdic]).max(axis=0)
res = numpy.array([coeffs[peril] for peril in perils]).max(axis=0)
return res


def _sum(lossdic):
if len(lossdic) == 1:
res = lossdic[next(iter(lossdic))]
else:
res = sum(lossdic[peril] for peril in lossdic)
return res


def consequence(consequence, assets, coeffs, total_loss_types, time_event):
def consequence(consequence, assets, cdict, loss_type, time_event):
"""
:param consequence: kind of consequence
:param assets: asset array (shape A)
:param coeffs: a composite array of multiplicative coefficients of shape (A, E)
:param cdict: peril -> composite array of coefficients of shape (A, E)
:param loss_type: loss type string
:param time_event: time event string
:returns: array of shape (A, E)
"""
if consequence not in KNOWN_CONSEQUENCES:
raise NotImplementedError(consequence)
if consequence.startswith('losses'):
res = _max({lt: assets['value-' + lt].reshape(-1, 1) * coeffs['earthquake']
for lt in total_loss_types}) / assets['value-number'].reshape(-1, 1)
res = (assets['value-' + loss_type].reshape(-1, 1) *
_max(cdict)) / assets['value-number'].reshape(-1, 1)
return res
elif consequence in ['collapsed', 'non_operational']:
return _sum(coeffs)
return _max(cdict)
elif consequence in ['injured', 'fatalities']:
# NOTE: time_event default is 'avg'
values = assets[f'occupants_{time_event}'] / assets['value-number']
return values.reshape(-1, 1) * _sum(coeffs)
return values.reshape(-1, 1) * _max(cdict)
elif consequence == 'homeless':
values = assets['value-residents'] / assets['value-number']
return values.reshape(-1, 1) * _sum(coeffs)
return values.reshape(-1, 1) * _max(cdict)
else:
raise NotImplementedError(consequence)

Expand Down