From e13b51b8330cfa8d11f68f82cf0ad7933de23f47 Mon Sep 17 00:00:00 2001 From: Michele Simionato Date: Thu, 31 Oct 2024 04:11:08 +0100 Subject: [PATCH 1/3] By loss_type --- openquake/risklib/riskmodels.py | 4 ++-- openquake/risklib/scientific.py | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/openquake/risklib/riskmodels.py b/openquake/risklib/riskmodels.py index dc1745596733..485c93b7bedb 100644 --- a/openquake/risklib/riskmodels.py +++ b/openquake/risklib/riskmodels.py @@ -256,9 +256,9 @@ 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) + res = {lt: meth(lt, assets, gmf_df, rndgen) for lt in self.loss_types} return res # for event_based_risk this is a DataFrame (eid, aid, loss) def __toh5__(self): diff --git a/openquake/risklib/scientific.py b/openquake/risklib/scientific.py index a8a22a536c5a..a42f29221fda 100644 --- a/openquake/risklib/scientific.py +++ b/openquake/risklib/scientific.py @@ -1668,7 +1668,7 @@ def __init__(self, crm, asset_df): tm.country, tm.loss_type, tm.risk_id, tm.weight): 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] From 95a8b44df8510f295b658cd70bfc635f343812d2 Mon Sep 17 00:00:00 2001 From: Michele Simionato Date: Thu, 31 Oct 2024 04:49:26 +0100 Subject: [PATCH 2/3] Improved comment --- openquake/risklib/riskmodels.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openquake/risklib/riskmodels.py b/openquake/risklib/riskmodels.py index 485c93b7bedb..39563de7479b 100644 --- a/openquake/risklib/riskmodels.py +++ b/openquake/risklib/riskmodels.py @@ -259,7 +259,8 @@ def loss_types(self): def __call__(self, assets, gmf_df, rndgen=None): meth = getattr(self, self.calcmode) res = {lt: meth(lt, assets, gmf_df, rndgen) for lt in self.loss_types} - return res # for event_based_risk this is a DataFrame (eid, aid, loss) + # 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} From 2eae7fea973a08f07f2c31667f55def4f6a0a303 Mon Sep 17 00:00:00 2001 From: Michele Simionato Date: Thu, 31 Oct 2024 06:23:20 +0100 Subject: [PATCH 3/3] Fixed get_riskcomputer --- openquake/risklib/riskmodels.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/openquake/risklib/riskmodels.py b/openquake/risklib/riskmodels.py index 39563de7479b..618ff390520f 100644 --- a/openquake/risklib/riskmodels.py +++ b/openquake/risklib/riskmodels.py @@ -441,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)) @@ -452,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