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

Computing all loss types together #10109

Merged
merged 5 commits into from
Oct 31, 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
17 changes: 9 additions & 8 deletions openquake/risklib/riskmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,11 @@ def loss_types(self):
"""
return sorted(self.risk_functions)

def __call__(self, loss_type, assets, gmf_df, rndgen=None):
def __call__(self, assets, gmf_df, rndgen=None):
meth = getattr(self, self.calcmode)
res = meth(loss_type, assets, gmf_df, rndgen)
return res # for event_based_risk this is a DataFrame (eid, aid, loss)
res = {lt: meth(lt, assets, gmf_df, rndgen) for lt in self.loss_types}
# for event_based_risk this is a map loss_type -> DataFrame(eid, aid, loss)
return res

def __toh5__(self):
return self.risk_functions, {'taxonomy': self.taxonomy}
Expand Down Expand Up @@ -440,6 +441,8 @@ def get_riskcomputer(dic, alias):
rc.asset_df = pandas.DataFrame(dic['asset_df'])
rc.wdic = {}
rfs = AccumDict(accum=[])
steps = dic.get('lrem_steps_per_interval', 1)
mal = dic.get('minimum_asset_loss', {lt: 0. for lt in dic['loss_types']})
for rlk, func in dic['risk_functions'].items():
riskid, lt = rlk.split('#')
rf = hdf5.json_to_obj(json.dumps(func))
Expand All @@ -451,16 +454,14 @@ def get_riskcomputer(dic, alias):
rf.retro.init()
rf.retro.loss_type = lt
rfs[riskid].append(rf)
steps = dic.get('lrem_steps_per_interval', 1)
mal = dic.get('minimum_asset_loss', {lt: 0. for lt in dic['loss_types']})
for rlt, weight in dic['wdic'].items():
riskid, lt = rlt.split('#')
rm = RiskModel(dic['calculation_mode'], 'taxonomy',
group_by_lt(rfs[riskid]),
lrem_steps_per_interval=steps,
minimum_asset_loss=mal)
rm.alias = alias
rc[riskid, lt] = rm
rc[riskid] = rm
for rlt, weight in dic['wdic'].items():
riskid, lt = rlt.split('#')
rc.wdic[riskid, lt] = weight
rc.loss_types = dic['loss_types']
rc.minimum_asset_loss = mal
Expand Down
11 changes: 5 additions & 6 deletions openquake/risklib/scientific.py
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ def __init__(self, crm, asset_df):
for lt in self.minimum_asset_loss:
if loss_type in ('*', lt):
if country == '?' or country_str in country:
self[riskid, lt] = crm._riskmodels[riskid]
self[riskid] = crm._riskmodels[riskid]
self.wdic[riskid, lt] = weight

def output(self, haz, sec_losses=(), rndgen=None):
Expand All @@ -1682,11 +1682,10 @@ def output(self, haz, sec_losses=(), rndgen=None):
"""
dic = collections.defaultdict(list) # lt -> outs
weights = collections.defaultdict(list) # lt -> weights
for riskid, lt in self:
rm = self[riskid, lt]
out = rm(lt, self.asset_df, haz, rndgen)
weights[lt].append(self.wdic[riskid, lt])
dic[lt].append(out)
for riskid, rm in self.items():
for lt, res in rm(self.asset_df, haz, rndgen).items():
weights[lt].append(self.wdic[riskid, lt])
dic[lt].append(res)
out = {}
for lt in self.minimum_asset_loss:
outs = dic[lt]
Expand Down
Loading