Skip to content

Commit 09db3ec

Browse files
authored
Merge pull request #10095 from gem/ebd
Composing probabilities in event_based_damage
2 parents 2ad40e3 + 9f80d91 commit 09db3ec

File tree

7 files changed

+248
-254
lines changed

7 files changed

+248
-254
lines changed

openquake/calculators/event_based_damage.py

+25-31
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def damage_from_gmfs(gmfslices, oqparam, dstore, monitor):
7878
return event_based_damage(df, oqparam, dstore, monitor)
7979

8080

81-
def _gen_d3(asset_df, gmf_df, crmodel, dparam):
82-
# yields (aids, d3) triples
81+
def _gen_dd3(asset_df, gmf_df, crmodel, dparam):
82+
# yields (aids, dd3) triples
8383
oq = crmodel.oqparam
8484
sec_sims = oq.secondary_simulations.items()
8585
for prob_field, num_sims in sec_sims:
@@ -97,19 +97,19 @@ def _gen_d3(asset_df, gmf_df, crmodel, dparam):
9797
number = assets['value-number']
9898
else:
9999
number = assets['value-number'] = U32(assets['value-number'])
100-
d4 = numpy.zeros((L, A, E, dparam.Dc), F32)
100+
dd4 = numpy.zeros((L, A, E, dparam.Dc), F32)
101101
D = dparam.D
102102
for lti, lt in enumerate(oq.loss_types):
103103
fractions = out[lt]
104104
if oq.float_dmg_dist:
105-
d4[lti, :, :, :D] = fractions
105+
dd4[lti, :, :, :D] = fractions
106106
for a in range(A):
107-
d4[lti, a] *= number[a]
107+
dd4[lti, a] *= number[a]
108108
else:
109109
# this is a performance distaster; for instance
110110
# the Messina test in oq-risk-tests becomes 12x
111111
# slower even if it has only 25_736 assets
112-
d4[lti, :, :, :D] = dparam.rng.discrete_dmg_dist(
112+
dd4[lti, :, :, :D] = dparam.rng.discrete_dmg_dist(
113113
dparam.eids, fractions, number)
114114

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

125125
df = crmodel.tmap_df[crmodel.tmap_df.taxi == assets[0]['taxonomy']]
126126
if 'losses' in crmodel.get_consequences():
127127
loss_types = oq.total_loss_types
128128
else:
129129
loss_types = {lt: i for i, lt in enumerate(oq.loss_types)}
130+
if L > 1:
131+
# compose probabilities
132+
dd3 = numpy.zeros((A, E, dparam.Dc), F32)
133+
for a in range(A):
134+
dd3[a] = general.pprod(dd4[:, a] / number[a], axis=0) * number[a]
135+
else:
136+
dd3 = dd4[0]
130137
csq = crmodel.compute_csq(
131-
assets, d4[:, :, :, :D], df, loss_types, oq.time_event)
132-
d3 = numpy.zeros((A, E, dparam.Dc), F32)
133-
for li, lt in enumerate(oq.loss_types):
134-
d3[:] += d4[li]
138+
assets, dd4[:, :, :, :D], df, loss_types, oq.time_event)
135139
for name, values in csq.items():
136-
d3[:, :, dparam.csqidx[name]] = values
137-
yield aids, d3 # d3 has shape (A, E, Dc)
140+
dd3[:, :, dparam.csqidx[name]] = values
141+
yield aids, dd3 # dd3 has shape (A, E, Dc)
138142

139143

140144
def event_based_damage(df, oq, dstore, monitor):
@@ -157,12 +161,9 @@ def event_based_damage(df, oq, dstore, monitor):
157161
dmg_csq = crmodel.get_dmg_csq()
158162
csqidx = {dc: i + 1 for i, dc in enumerate(dmg_csq)}
159163
dmgcsq = zero_dmgcsq(len(assetcol), oq.R, crmodel)
160-
_A, R, Dc = dmgcsq.shape
164+
_A, _R, Dc = dmgcsq.shape
161165
D = Dc - len(crmodel.get_consequences())
162-
if R > 1:
163-
allrlzs = dstore['events']['rlz_id']
164-
else:
165-
allrlzs = U32([0])
166+
rlzs = dstore['events']['rlz_id']
166167
with mon_risk:
167168
dddict = general.AccumDict(accum=numpy.zeros(Dc, F32)) # eid, kid
168169
for sid, asset_df in assetcol.to_dframe().groupby('site_id'):
@@ -172,29 +173,22 @@ def event_based_damage(df, oq, dstore, monitor):
172173
continue
173174
oq = crmodel.oqparam
174175
eids = gmf_df.eid.to_numpy()
175-
if R > 1:
176-
rlzs = allrlzs[eids]
177-
else:
178-
rlzs = allrlzs
179176
if oq.secondary_simulations or not oq.float_dmg_dist:
180177
rng = scientific.MultiEventRNG(
181178
oq.master_seed, numpy.unique(eids))
182179
else:
183180
rng = None
184181
dparam = Dparam(eids, aggids, csqidx, D, Dc, rng)
185-
for aids, d3 in _gen_d3(asset_df, gmf_df, crmodel, dparam):
186-
if R == 1:
187-
dmgcsq[aids, 0] += d3.sum(axis=1)
188-
else:
189-
for e, rlz in enumerate(rlzs):
190-
dmgcsq[aids, rlz] += d3[:, e]
191-
tot = d3.sum(axis=0) # sum on the assets
182+
for aids, dd3 in _gen_dd3(asset_df, gmf_df, crmodel, dparam):
183+
for e, rlz in enumerate(rlzs[eids]):
184+
dmgcsq[aids, rlz] += dd3[:, e]
185+
tot = dd3.sum(axis=0) # sum on the assets
192186
for e, eid in enumerate(eids):
193187
dddict[eid, oq.K] += tot[e]
194188
if oq.K:
195189
for kids in dparam.aggids:
196190
for a, aid in enumerate(aids):
197-
dddict[eid, kids[aid]] += d3[a, e]
191+
dddict[eid, kids[aid]] += dd3[a, e]
198192
try:
199193
[lt] = oq.loss_types
200194
except ValueError:
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-git03f37349ed', start_date='2024-10-23T10:42:23', checksum=2614000066, investigation_time=None, risk_investigation_time=None"
1+
#,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-gitafc5cab7ae', start_date='2024-10-26T05:43:19', checksum=2614000066, investigation_time=None, risk_investigation_time=None"
22
loss_type,parent_id,rlz_id,no_damage,slight,moderate,extensive,complete,non_operational_value,non_operational_ratio
33
structural,A,0,9.66667E-01,0.00000E+00,0.00000E+00,0.00000E+00,1.00000E-01,1.00000E-01,1.00000E-01
44
structural,A,1,9.33333E-01,0.00000E+00,0.00000E+00,0.00000E+00,2.00000E-01,2.00000E-01,2.00000E-01
55
structural,B,0,1.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00
66
structural,B,1,1.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00
7-
structural,E1,0,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,3.00000E+00,3.00000E+00,3.00000E+00
8-
structural,E1,1,0.00000E+00,0.00000E+00,0.00000E+00,0.00000E+00,3.00000E+00,3.00000E+00,3.00000E+00
7+
structural,E1,0,6.66667E-01,0.00000E+00,0.00000E+00,0.00000E+00,1.00000E+00,3.00000E+00,3.00000E+00
8+
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 numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#,,,,,,,,"generated_by='OpenQuake engine 3.22.0-git03f37349ed', start_date='2024-10-23T10:42:23', checksum=2614000066, investigation_time=None, risk_investigation_time=None"
1+
#,,,,,,,,"generated_by='OpenQuake engine 3.22.0-gitafc5cab7ae', start_date='2024-10-26T05:43:19', checksum=2614000066, investigation_time=None, risk_investigation_time=None"
22
loss_type,rlz_id,no_damage,slight,moderate,extensive,complete,non_operational_value,non_operational_ratio
3-
structural,0,1.96667E+00,0.00000E+00,0.00000E+00,0.00000E+00,3.10000E+00,3.10000E+00,1.03333E+00
4-
structural,1,1.93333E+00,0.00000E+00,0.00000E+00,0.00000E+00,3.20000E+00,3.20000E+00,1.06667E+00
3+
structural,0,2.63333E+00,0.00000E+00,0.00000E+00,0.00000E+00,1.10000E+00,3.10000E+00,1.03333E+00
4+
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 numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#,,,,,,,"generated_by='OpenQuake engine 3.22.0-git03f37349ed', start_date='2024-10-23T10:42:25', checksum=2138001544, investigation_time=None, risk_investigation_time=None"
1+
#,,,,,,,"generated_by='OpenQuake engine 3.22.0-gitafc5cab7ae', start_date='2024-10-26T05:43:19', checksum=4025559538, investigation_time=None, risk_investigation_time=None"
22
loss_type,no_damage,slight,moderate,extreme,complete,losses_value,losses_ratio
3-
structural,1.07656E+01,1.41803E+00,1.39786E+00,9.49061E-01,8.93820E+00,1.84217E+04,1.08299E-01
3+
structural,1.17053E+01,1.41803E+00,1.39786E+00,9.49061E-01,6.11920E+00,1.84217E+04,1.08299E-01
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
#,,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-gita2d5dedadb', start_date='2024-10-25T07:10:31', checksum=4025559538"
1+
#,,,,,,,,,,"generated_by='OpenQuake engine 3.22.0-gitafc5cab7ae', start_date='2024-10-26T05:43:19', checksum=4025559538"
22
asset_id,NAME_1,taxonomy,lon,lat,no_damage,slight,moderate,extreme,complete,losses
3-
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
4-
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
5-
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
3+
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
4+
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
5+
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
66
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
7-
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
7+
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
88
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
9-
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
10-
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
11-
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
12-
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
13-
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
14-
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
9+
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
10+
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
11+
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
12+
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
13+
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
14+
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
1515
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
16-
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
16+
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
1717
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

0 commit comments

Comments
 (0)