diff --git a/openquake/risklib/riskmodels.py b/openquake/risklib/riskmodels.py index dc174559673..618ff390520 100644 --- a/openquake/risklib/riskmodels.py +++ b/openquake/risklib/riskmodels.py @@ -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} @@ -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)) @@ -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 diff --git a/openquake/risklib/scientific.py b/openquake/risklib/scientific.py index 4e0c0960d2f..36efe6a8bcd 100644 --- a/openquake/risklib/scientific.py +++ b/openquake/risklib/scientific.py @@ -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): @@ -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]