Skip to content

Commit

Permalink
Merge pull request #8964 from gem/export-eos
Browse files Browse the repository at this point in the history
Fixed two export bugs in absence of ruptures
  • Loading branch information
micheles authored Aug 30, 2023
2 parents 26e9e35 + 98a51e2 commit c4d43c8
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
2 changes: 2 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[Michele Simionato]
* Fixed a bug when exporting `risk_by_event` in absence of ruptures
* Fixed a bug by not exporting `event_based_mfd` in absence of ruptures
* Implemented Risk Targeted Disaggregations for the AELO project
* Enabled the pointsource_distance approximation by default at 100 km
* Fixed an error with sources below the minimum_magnitude
Expand Down
4 changes: 3 additions & 1 deletion openquake/calculators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,10 @@ def export(self, exports=None):
'hcurves-rlzs' in self.datastore)
if has_hcurves:
keys.add('hcurves')
if 'ruptures' in self.datastore:
if 'ruptures' in self.datastore and len(self.datastore['ruptures']):
keys.add('event_based_mfd')
elif 'ruptures' in keys:
keys.remove('ruptures')
for fmt in fmts:
if not fmt:
continue
Expand Down
15 changes: 9 additions & 6 deletions openquake/calculators/export/risk.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def export_event_loss_table(ekey, dstore):
md.update(dict(investigation_time=oq.investigation_time,
risk_investigation_time=oq.risk_investigation_time
or oq.investigation_time))
events = dstore['events'][()]
events = dstore.read_df('events', 'id')
K = dstore.get_attr('risk_by_event', 'K', 0)
try:
lstates = dstore.get_attr('risk_by_event', 'limit_states').split()
Expand All @@ -269,11 +269,14 @@ def export_event_loss_table(ekey, dstore):
del df['variance']
ren = {'dmg_%d' % i: lstate for i, lstate in enumerate(lstates, 1)}
df.rename(columns=ren, inplace=True)
evs = events[df.event_id.to_numpy()]
if 'scenario' not in oq.calculation_mode:
df['rup_id'] = evs['rup_id']
if 'scenario' not in oq.calculation_mode and 'year' in evs.dtype.names:
df['year'] = evs['year']
df = df.join(events, on='event_id')
if 'ses_id' in df.columns:
del df['ses_id']
del df['rlz_id']
if 'scenario' in oq.calculation_mode:
del df['rup_id']
if 'year' in df.columns:
del df['year']
df.sort_values(['event_id', 'loss_type'], inplace=True)
writer.save(df, dest, comment=md)
return writer.getsaved()
Expand Down
7 changes: 3 additions & 4 deletions openquake/calculators/tests/classical_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,9 @@ def test_case_78(self):
disagg_bin_edges='{"dist": [0, 15, 30]}',
hazard_calculation_id=hc_str)

# skipped because broken on CI due to a mysterious extra space
# dbm = view('disagg:Dist', self.calc.datastore)
# fname = general.gettemp(text_table(dbm, ext='org'))
# self.assertEqualFiles('expected/disagg_by_dist.org', fname)
dbm = view('disagg:Dist', self.calc.datastore)
fname = general.gettemp(text_table(dbm, ext='org'))
self.assertEqualFiles('expected/disagg_by_dist.org', fname)

def test_case_80(self):
# New Madrid cluster with rup_mutex
Expand Down
2 changes: 1 addition & 1 deletion openquake/engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def expose_outputs(dstore, owner=USER, status='complete'):
if 'loss_curves-stats' in dstore:
dskeys.add('loss_maps-stats')
if 'ruptures' in dskeys:
if 'scenario' in calcmode:
if 'scenario' in calcmode or len(dstore['ruptures']) == 0:
# do not export, as requested by Vitor
exportable.remove('ruptures')
else:
Expand Down
1 change: 1 addition & 0 deletions openquake/hazardlib/calc/mean_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def to_rates(probs, itime=1):
"""
pnes = 1. - probs
pnes[pnes == 0] = 1E-45 # mininum 32 bit float
# NB: the test most sensitive to 1E-45 and 1E-12 is case_78
return numpy.clip(- numpy.log(pnes) / itime, 1E-12, 100)


Expand Down

0 comments on commit c4d43c8

Please sign in to comment.