Skip to content

Commit

Permalink
Merge pull request #9182 from gem/sec_imts
Browse files Browse the repository at this point in the history
Introduced a cached_property for `sec_imts`
  • Loading branch information
micheles authored Nov 6, 2023
2 parents 9422898 + a097ffd commit 118115b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
6 changes: 3 additions & 3 deletions openquake/calculators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ def pre_execute(self):
raise ValueError(
'The parent calculation had stats %s != %s' %
(hstats, rstats))
sec_imts = set(oq.get_sec_imts())
sec_imts = set(oq.sec_imts)
missing_imts = set(oq.risk_imtls) - sec_imts - set(oqp.imtls)
if oqp.imtls and missing_imts:
raise ValueError(
Expand Down Expand Up @@ -1039,7 +1039,7 @@ def build_riskinputs(self):
a list of RiskInputs objects, sorted by IMT.
"""
logging.info('Building risk inputs from %d realization(s)', self.R)
imtset = set(self.oqparam.imtls) | set(self.oqparam.get_sec_imts())
imtset = set(self.oqparam.imtls) | set(self.oqparam.sec_imts)
if not set(self.oqparam.risk_imtls) & imtset:
rsk = ', '.join(self.oqparam.risk_imtls)
haz = ', '.join(imtset)
Expand Down Expand Up @@ -1181,7 +1181,7 @@ def import_gmfs_csv(dstore, oqparam, sitecol):
data = numpy.concatenate(gmvlst)
data.sort(order='eid')
create_gmf_data(dstore, oqparam.get_primary_imtls(),
oqparam.get_sec_imts(), data=data)
oqparam.sec_imts, data=data)
dstore['weights'] = numpy.ones(1)
return eids

Expand Down
6 changes: 3 additions & 3 deletions openquake/calculators/event_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def agg_dicts(self, acc, result):
raise MemoryError('You ran out of memory!')
sav_mon = self.monitor('saving gmfs')
primary = self.oqparam.get_primary_imtls()
sec_imts = self.oqparam.get_sec_imts()
sec_imts = self.oqparam.sec_imts
with sav_mon:
gmfdata = result.pop('gmfdata')
if len(gmfdata):
Expand Down Expand Up @@ -634,7 +634,7 @@ def execute(self):

if oq.ground_motion_fields:
imts = oq.get_primary_imtls()
base.create_gmf_data(dstore, imts, oq.get_sec_imts())
base.create_gmf_data(dstore, imts, oq.sec_imts)
dstore.create_dset('gmf_data/sigma_epsilon', sig_eps_dt(oq.imtls))
dstore.create_dset('gmf_data/rup_info', rup_dt)
if self.N >= SLICE_BY_EVENT_NSITES:
Expand Down Expand Up @@ -668,7 +668,7 @@ def save_avg_gmf(self):
rlzs = self.datastore['events']['rlz_id']
self.weights = self.datastore['weights'][:][rlzs]
gmf_df = self.datastore.read_df('gmf_data', 'sid')
for sec_imt in self.oqparam.get_sec_imts(): # ignore secondary perils
for sec_imt in self.oqparam.sec_imts: # ignore secondary perils
del gmf_df[sec_imt]
rel_events = gmf_df.eid.unique()
e = len(rel_events)
Expand Down
2 changes: 1 addition & 1 deletion openquake/calculators/export/hazard.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def export_gmf_data_csv(ekey, dstore):
ren = {'sid': 'site_id', 'eid': 'event_id'}
for m, imt in enumerate(imts):
ren[f'gmv_{m}'] = 'gmv_' + imt
for imt in oq.get_sec_imts():
for imt in oq.sec_imts:
ren[imt] = f'sep_{imt}'
df.rename(columns=ren, inplace=True)
event_id = dstore['events']['id']
Expand Down
11 changes: 6 additions & 5 deletions openquake/commonlib/oqvalidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import itertools

from openquake.baselib import __version__, hdf5, python3compat, config
from openquake.baselib.general import DictArray, AccumDict
from openquake.baselib.general import DictArray, AccumDict, cached_property
from openquake.hazardlib.imt import from_string, sort_by_imt
from openquake.hazardlib import shakemap
from openquake.hazardlib import correlation, cross_correlation, stats, calc
Expand Down Expand Up @@ -1550,7 +1550,7 @@ def get_primary_imtls(self):
"""
:returns: IMTs and levels which are not secondary
"""
sec_imts = set(self.get_sec_imts())
sec_imts = set(self.sec_imts)
return {imt: imls for imt, imls in self.imtls.items()
if imt not in sec_imts}

Expand Down Expand Up @@ -1646,7 +1646,7 @@ def gmf_data_dt(self):
lst = [('sid', U32), ('eid', U32)]
for m, imt in enumerate(self.get_primary_imtls()):
lst.append((f'gmv_{m}', F32))
for out in self.get_sec_imts():
for out in self.sec_imts:
lst.append((out, F32))
return numpy.dtype(lst)

Expand All @@ -1657,7 +1657,7 @@ def all_imts(self):
lst = []
for m, imt in enumerate(self.get_primary_imtls()):
lst.append(f'gmv_{m}')
for out in self.get_sec_imts():
for out in self.sec_imts:
lst.append(out)
return lst

Expand All @@ -1668,7 +1668,8 @@ def get_sec_perils(self):
return SecondaryPeril.instantiate(self.secondary_perils,
self.sec_peril_params)

def get_sec_imts(self):
@cached_property
def sec_imts(self):
"""
:returns: a list of secondary outputs
"""
Expand Down

0 comments on commit 118115b

Please sign in to comment.