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

Composing probabilities in event_based_damage #10095

Merged
merged 4 commits into from
Oct 26, 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
56 changes: 25 additions & 31 deletions openquake/calculators/event_based_damage.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def damage_from_gmfs(gmfslices, oqparam, dstore, monitor):
return event_based_damage(df, oqparam, dstore, monitor)


def _gen_d3(asset_df, gmf_df, crmodel, dparam):
# yields (aids, d3) triples
def _gen_dd3(asset_df, gmf_df, crmodel, dparam):
# yields (aids, dd3) triples
oq = crmodel.oqparam
sec_sims = oq.secondary_simulations.items()
for prob_field, num_sims in sec_sims:
Expand All @@ -97,19 +97,19 @@ def _gen_d3(asset_df, gmf_df, crmodel, dparam):
number = assets['value-number']
else:
number = assets['value-number'] = U32(assets['value-number'])
d4 = numpy.zeros((L, A, E, dparam.Dc), F32)
dd4 = numpy.zeros((L, A, E, dparam.Dc), F32)
D = dparam.D
for lti, lt in enumerate(oq.loss_types):
fractions = out[lt]
if oq.float_dmg_dist:
d4[lti, :, :, :D] = fractions
dd4[lti, :, :, :D] = fractions
for a in range(A):
d4[lti, a] *= number[a]
dd4[lti, a] *= number[a]
else:
# this is a performance distaster; for instance
# the Messina test in oq-risk-tests becomes 12x
# slower even if it has only 25_736 assets
d4[lti, :, :, :D] = dparam.rng.discrete_dmg_dist(
dd4[lti, :, :, :D] = dparam.rng.discrete_dmg_dist(
dparam.eids, fractions, number)

# secondary perils and consequences
Expand All @@ -118,23 +118,27 @@ def _gen_d3(asset_df, gmf_df, crmodel, dparam):
for d in range(1, D):
# doing the mean on the secondary simulations
if oq.float_dmg_dist:
d4[lti, a, :, d] *= probs
dd4[lti, a, :, d] *= probs
else:
d4[lti, a, :, d] *= dprobs
dd4[lti, a, :, d] *= dprobs

df = crmodel.tmap_df[crmodel.tmap_df.taxi == assets[0]['taxonomy']]
if 'losses' in crmodel.get_consequences():
loss_types = oq.total_loss_types
else:
loss_types = {lt: i for i, lt in enumerate(oq.loss_types)}
if L > 1:
# compose probabilities
dd3 = numpy.zeros((A, E, dparam.Dc), F32)
for a in range(A):
dd3[a] = general.pprod(dd4[:, a] / number[a], axis=0) * number[a]
else:
dd3 = dd4[0]
csq = crmodel.compute_csq(
assets, d4[:, :, :, :D], df, loss_types, oq.time_event)
d3 = numpy.zeros((A, E, dparam.Dc), F32)
for li, lt in enumerate(oq.loss_types):
d3[:] += d4[li]
assets, dd4[:, :, :, :D], df, loss_types, oq.time_event)
for name, values in csq.items():
d3[:, :, dparam.csqidx[name]] = values
yield aids, d3 # d3 has shape (A, E, Dc)
dd3[:, :, dparam.csqidx[name]] = values
yield aids, dd3 # dd3 has shape (A, E, Dc)


def event_based_damage(df, oq, dstore, monitor):
Expand All @@ -157,12 +161,9 @@ def event_based_damage(df, oq, dstore, monitor):
dmg_csq = crmodel.get_dmg_csq()
csqidx = {dc: i + 1 for i, dc in enumerate(dmg_csq)}
dmgcsq = zero_dmgcsq(len(assetcol), oq.R, crmodel)
_A, R, Dc = dmgcsq.shape
_A, _R, Dc = dmgcsq.shape
D = Dc - len(crmodel.get_consequences())
if R > 1:
allrlzs = dstore['events']['rlz_id']
else:
allrlzs = U32([0])
rlzs = dstore['events']['rlz_id']
with mon_risk:
dddict = general.AccumDict(accum=numpy.zeros(Dc, F32)) # eid, kid
for sid, asset_df in assetcol.to_dframe().groupby('site_id'):
Expand All @@ -172,29 +173,22 @@ def event_based_damage(df, oq, dstore, monitor):
continue
oq = crmodel.oqparam
eids = gmf_df.eid.to_numpy()
if R > 1:
rlzs = allrlzs[eids]
else:
rlzs = allrlzs
if oq.secondary_simulations or not oq.float_dmg_dist:
rng = scientific.MultiEventRNG(
oq.master_seed, numpy.unique(eids))
else:
rng = None
dparam = Dparam(eids, aggids, csqidx, D, Dc, rng)
for aids, d3 in _gen_d3(asset_df, gmf_df, crmodel, dparam):
if R == 1:
dmgcsq[aids, 0] += d3.sum(axis=1)
else:
for e, rlz in enumerate(rlzs):
dmgcsq[aids, rlz] += d3[:, e]
tot = d3.sum(axis=0) # sum on the assets
for aids, dd3 in _gen_dd3(asset_df, gmf_df, crmodel, dparam):
for e, rlz in enumerate(rlzs[eids]):
dmgcsq[aids, rlz] += dd3[:, e]
tot = dd3.sum(axis=0) # sum on the assets
for e, eid in enumerate(eids):
dddict[eid, oq.K] += tot[e]
if oq.K:
for kids in dparam.aggids:
for a, aid in enumerate(aids):
dddict[eid, kids[aid]] += d3[a, e]
dddict[eid, kids[aid]] += dd3[a, e]
try:
[lt] = oq.loss_types
except ValueError:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-git03f37349ed', start_date='2024-10-23T10:42:23', checksum=2614000066, investigation_time=None, risk_investigation_time=None"
#,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-gitafc5cab7ae', start_date='2024-10-26T05:43:19', checksum=2614000066, 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,9.66667E-01,0.00000E+00,0.00000E+00,0.00000E+00,1.00000E-01,1.00000E-01,1.00000E-01
structural,A,1,9.33333E-01,0.00000E+00,0.00000E+00,0.00000E+00,2.00000E-01,2.00000E-01,2.00000E-01
structural,B,0,1.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00
structural,B,1,1.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00
structural,E1,0,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,3.00000E+00,3.00000E+00,3.00000E+00
structural,E1,1,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,3.00000E+00,3.00000E+00,3.00000E+00
structural,E1,0,6.66667E-01,0.00000E+00,0.00000E+00,0.00000E+00,1.00000E+00,3.00000E+00,3.00000E+00
structural,E1,1,6.66667E-01,0.00000E+00,0.00000E+00,0.00000E+00,1.00000E+00,3.00000E+00,3.00000E+00
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#,,,,,,,,"generated_by='OpenQuake engine 3.22.0-git03f37349ed', start_date='2024-10-23T10:42:23', checksum=2614000066, investigation_time=None, risk_investigation_time=None"
#,,,,,,,,"generated_by='OpenQuake engine 3.22.0-gitafc5cab7ae', start_date='2024-10-26T05:43:19', checksum=2614000066, 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.96667E+00,0.00000E+00,0.00000E+00,0.00000E+00,3.10000E+00,3.10000E+00,1.03333E+00
structural,1,1.93333E+00,0.00000E+00,0.00000E+00,0.00000E+00,3.20000E+00,3.20000E+00,1.06667E+00
structural,0,2.63333E+00,0.00000E+00,0.00000E+00,0.00000E+00,1.10000E+00,3.10000E+00,1.03333E+00
structural,1,2.60000E+00,0.00000E+00,0.00000E+00,0.00000E+00,1.20000E+00,3.20000E+00,1.06667E+00
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#,,,,,,,"generated_by='OpenQuake engine 3.22.0-git03f37349ed', start_date='2024-10-23T10:42:25', checksum=2138001544, investigation_time=None, risk_investigation_time=None"
#,,,,,,,"generated_by='OpenQuake engine 3.22.0-gitafc5cab7ae', start_date='2024-10-26T05:43:19', checksum=4025559538, investigation_time=None, risk_investigation_time=None"
loss_type,no_damage,slight,moderate,extreme,complete,losses_value,losses_ratio
structural,1.07656E+01,1.41803E+00,1.39786E+00,9.49061E-01,8.93820E+00,1.84217E+04,1.08299E-01
structural,1.17053E+01,1.41803E+00,1.39786E+00,9.49061E-01,6.11920E+00,1.84217E+04,1.08299E-01
24 changes: 12 additions & 12 deletions openquake/qa_tests_data/scenario_damage/case_22/expected/dmg.csv
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#,,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-gita2d5dedadb', start_date='2024-10-25T07:10:31', checksum=4025559538"
#,,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-gitafc5cab7ae', start_date='2024-10-26T05:43:19', checksum=4025559538"
asset_id,NAME_1,taxonomy,lon,lat,no_damage,slight,moderate,extreme,complete,losses
a1,a,Wood1,83.31382,29.46117,9.267137E-01,4.338550E-02,2.389134E-02,4.983470E-03,1.026009E-03,1.492097E+02
a2,a,Wood1,83.31382,29.23617,9.323701E-01,4.003692E-02,2.204736E-02,4.598836E-03,9.468192E-04,1.376935E+02
a3,a,Wood1,83.53882,29.08617,8.998597E-01,5.549773E-02,3.362454E-02,8.460128E-03,2.557828E-03,2.299460E+02
a1,a,Wood1,83.31382,29.46117,9.267137E-01,4.338550E-02,2.389134E-02,4.983467E-03,1.026009E-03,1.492097E+02
a2,a,Wood1,83.31382,29.23617,9.323701E-01,4.003692E-02,2.204736E-02,4.598836E-03,9.468218E-04,1.376935E+02
a3,a,Wood1,83.53882,29.08617,8.998597E-01,5.549773E-02,3.362454E-02,8.460126E-03,2.557830E-03,2.299460E+02
a4,a,Wood1,80.68882,28.93617,1.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00,0.000000E+00
a5,a,Wood1,83.53882,29.01117,8.605580E-01,7.386525E-02,4.770378E-02,1.326894E-02,4.604037E-03,3.436947E+02
a5,a,Wood1,83.53882,29.01117,8.605580E-01,7.386524E-02,4.770378E-02,1.326893E-02,4.604037E-03,3.436947E+02
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,7.521410E-01,8.566735E-02,8.970005E-02,4.155788E-02,3.093375E-02,9.213160E+02
a8,a,Concrete1,83.23882,29.38617,4.697937E-01,2.247916E-02,3.195053E-02,3.080165E-02,4.449750E-01,5.401368E+02
a9,a,Concrete1,83.01382,29.08617,4.619936E-01,2.497276E-02,3.440397E-02,3.081745E-02,4.478122E-01,5.061439E+02
a10,a,Concrete1,83.31382,28.71117,1.616082E-01,4.471088E-02,6.344895E-02,6.095122E-02,6.692807E-01,1.065684E+03
a11,a,Concrete1,86.91382,27.73617,6.076256E-01,1.408595E-02,1.940564E-02,1.738266E-02,3.415002E-01,2.854917E+02
a12,a,Concrete1,83.16382,29.31117,5.451100E-01,2.104819E-02,2.899724E-02,2.597436E-02,3.788701E-01,4.266012E+02
a7,a,Wood1,83.98882,28.48617,7.524759E-01,8.566736E-02,8.970005E-02,4.155788E-02,3.059878E-02,9.213160E+02
a8,a,Concrete1,83.23882,29.38617,5.814353E-01,2.247916E-02,3.195053E-02,3.080165E-02,3.333333E-01,5.401368E+02
a9,a,Concrete1,83.01382,29.08617,5.764725E-01,2.497276E-02,3.440397E-02,3.081745E-02,3.333333E-01,5.061439E+02
a10,a,Concrete1,83.31382,28.71117,4.975556E-01,4.471088E-02,6.344895E-02,6.095122E-02,3.333333E-01,1.065684E+03
a11,a,Concrete1,86.91382,27.73617,6.157925E-01,1.408595E-02,1.940564E-02,1.738266E-02,3.333333E-01,2.854917E+02
a12,a,Concrete1,83.16382,29.31117,5.906469E-01,2.104818E-02,2.899724E-02,2.597436E-02,3.333333E-01,4.266012E+02
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,1.478413E-01,4.692676E-02,7.078027E-02,7.755719E-02,6.568946E-01,1.534645E+03
a14,a,Concrete1,83.91382,29.01117,4.714025E-01,4.692676E-02,7.078026E-02,7.755719E-02,3.333333E-01,1.534645E+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
Loading
Loading