Skip to content

Commit

Permalink
Simplified RuptureGetter
Browse files Browse the repository at this point in the history
  • Loading branch information
micheles committed Dec 16, 2024
1 parent 01e324f commit 7d502ad
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
41 changes: 20 additions & 21 deletions openquake/calculators/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,12 @@ def get_rupture_getters(dstore, ct=0, srcfilter=None, rupids=None):
rup_array = rup_array[numpy.isin(rup_array['id'], rupids)]
if len(rup_array) == 0:
raise NotFound('There are no ruptures in %s' % dstore)
proxies = [RuptureProxy(rec) for rec in rup_array]
maxweight = rup_array['n_occ'].sum() / (ct / 2 or 1)
maxweight = len(rup_array) / (ct / 2 or 1)
rgetters = []
for block in general.block_splitter(
proxies, maxweight, operator.itemgetter('n_occ'),
key=operator.itemgetter('trt_smr')):
rup_array, maxweight, key=operator.itemgetter('trt_smr')):
trt_smr = block[0]['trt_smr']
rg = RuptureGetter(block, dstore.filename, trt_smr,
rg = RuptureGetter(numpy.array(block), dstore.filename, trt_smr,
full_lt.trt_by(trt_smr))
rgetters.append(rg)
return rgetters
Expand Down Expand Up @@ -426,30 +424,29 @@ def get_rupture_from_dstore(dstore, rup_id=0):
# this is never called directly; get_rupture_getters is used instead
class RuptureGetter(object):
"""
:param proxies:
a list of RuptureProxies
:param array:
an array of rupture records
:param filename:
path to the HDF5 file containing a 'rupgeoms' dataset
:param trt_smr:
source group index
:param trt:
tectonic region type string
"""
def __init__(self, proxies, filename, trt_smr, trt):
self.proxies = proxies
self.weight = sum(proxy['n_occ'] for proxy in proxies)
def __init__(self, array, filename, trt_smr, trt):
self.array = array
self.filename = filename
self.trt_smr = trt_smr
self.trt = trt
self.num_events = sum(int(proxy['n_occ']) for proxy in proxies)
self.num_events = array['n_occ'].sum()

@property
def num_ruptures(self):
return len(self.proxies)

@property
def seeds(self):
return [p['seed'] for p in self.proxies]
return self.array['seed']

def get_proxies(self, min_mag=0):
"""
Expand All @@ -458,7 +455,8 @@ def get_proxies(self, min_mag=0):
proxies = []
with datastore.read(self.filename) as dstore:
rupgeoms = dstore['rupgeoms']
for proxy in self.proxies:
for rec in self.array:
proxy = RuptureProxy(rec)
if proxy['mag'] < min_mag:
# discard small magnitudes
continue
Expand All @@ -471,21 +469,22 @@ def split(self, srcfilter, maxw):
"""
:returns: RuptureProxies with weight < maxw
"""
proxies = []
for proxy in self.proxies:
recs = []
for rec in self.array:
proxy = RuptureProxy(rec)
sids = srcfilter.close_sids(proxy.rec, self.trt)
if len(sids):
proxies.append(proxy)
recs.append(rec)
rgetters = []
for block in general.block_splitter(proxies, maxw, weight):
rg = RuptureGetter(block, self.filename, self.trt_smr, self.trt)
for block in general.block_splitter(rec, maxw):
rg = RuptureGetter(numpy.array(block), self.filename,
self.trt_smr, self.trt)
rgetters.append(rg)
return rgetters

def __len__(self):
return len(self.proxies)

def __repr__(self):
wei = ' [w=%d]' % self.weight if hasattr(self, 'weight') else ''
return '<%s trt_smr=%d, %d rupture(s)%s>' % (
self.__class__.__name__, self.trt_smr, len(self), wei)
return '<%s trt_smr=%d, %d rupture(s)>' % (
self.__class__.__name__, self.trt_smr, len(self))
2 changes: 1 addition & 1 deletion openquake/commonlib/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def _save_events(self, rup_array, rgetters):
iterargs = []
rlzs_by_gsim = self.full_lt.get_rlzs_by_gsim_dic()
for i, rg in enumerate(rgetters):
iterargs.append((rg.proxies, rlzs_by_gsim[rg.trt_smr], i))
iterargs.append((rg.get_proxies(), rlzs_by_gsim[rg.trt_smr], i))
if len(events) < 1E5:
acc = general.AccumDict() # ordinal -> eid_rlz
for args in iterargs:
Expand Down

0 comments on commit 7d502ad

Please sign in to comment.