From cd64d37933735caf4f71a6816584833919ae013b Mon Sep 17 00:00:00 2001 From: Michele Simionato Date: Wed, 30 Oct 2024 04:52:34 +0100 Subject: [PATCH 1/3] Refactored reading the risk functions --- openquake/risklib/riskmodels.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/openquake/risklib/riskmodels.py b/openquake/risklib/riskmodels.py index 6d311cb1e04..e224aeb81f9 100644 --- a/openquake/risklib/riskmodels.py +++ b/openquake/risklib/riskmodels.py @@ -135,21 +135,17 @@ def groupby_id(self): return {riskid: group_by_lt(rfs) for riskid, rfs in ddic.items()} -def get_risk_functions(oqparam, kind='vulnerability fragility ' - 'vulnerability_retrofitted'): +def get_risk_functions(oqparam): """ :param oqparam: an OqParam instance - :param kind: - a space-separated string with the kinds of risk models to read :returns: a list of risk functions """ - kinds = kind.split() rmodels = AccumDict() - for kind in kinds: + for kind in 'vulnerability fragility vulnerability_retrofitted'.split(): for key in sorted(oqparam.inputs): - mo = re.match('(occupants|%s)_%s$' % (LTYPE_REGEX, kind), key) + mo = re.match('(%s)_%s$' % (LTYPE_REGEX, kind), key) if mo: loss_type = mo.group(1) # the cost_type in the key # can be occupants, structural, nonstructural, ... From fe5cb0e70ee1197fe50024408b825c081767fe89 Mon Sep 17 00:00:00 2001 From: Michele Simionato Date: Wed, 30 Oct 2024 05:00:39 +0100 Subject: [PATCH 2/3] Reading also _vulnerability_retrofitted --- openquake/risklib/riskmodels.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/openquake/risklib/riskmodels.py b/openquake/risklib/riskmodels.py index e224aeb81f9..2738e8fc217 100644 --- a/openquake/risklib/riskmodels.py +++ b/openquake/risklib/riskmodels.py @@ -67,9 +67,9 @@ def get_risk_files(inputs): rfs['fragility/structural'] = inputs[ 'structural_fragility'] = inputs[key] del inputs['fragility'] - elif key.endswith(('_fragility', '_vulnerability')): + elif key.endswith(('_fragility', '_vulnerability', '_vulnerability_retrofitted')): match = RISK_TYPE_REGEX.match(key) - if match and 'retrofitted' not in key: + if match: rfs['%s/%s' % (match.group(2), match.group(1))] = inputs[key] elif match is None: raise ValueError('Invalid key in %s: %s_file' % (job_ini, key)) @@ -143,6 +143,7 @@ def get_risk_functions(oqparam): a list of risk functions """ rmodels = AccumDict() + #for key, fname in get_risk_files(oqparam.inputs): for kind in 'vulnerability fragility vulnerability_retrofitted'.split(): for key in sorted(oqparam.inputs): mo = re.match('(%s)_%s$' % (LTYPE_REGEX, kind), key) From 73fff0e11322270a73d547115f30d44b11d1eda7 Mon Sep 17 00:00:00 2001 From: Michele Simionato Date: Wed, 30 Oct 2024 05:05:57 +0100 Subject: [PATCH 3/3] Simplified get_risk_functions --- openquake/risklib/riskmodels.py | 52 +++++++++++++++------------------ 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/openquake/risklib/riskmodels.py b/openquake/risklib/riskmodels.py index 2738e8fc217..b2813e02d53 100644 --- a/openquake/risklib/riskmodels.py +++ b/openquake/risklib/riskmodels.py @@ -142,35 +142,31 @@ def get_risk_functions(oqparam): :returns: a list of risk functions """ + job_ini = oqparam.inputs['job_ini'] rmodels = AccumDict() - #for key, fname in get_risk_files(oqparam.inputs): - for kind in 'vulnerability fragility vulnerability_retrofitted'.split(): - for key in sorted(oqparam.inputs): - mo = re.match('(%s)_%s$' % (LTYPE_REGEX, kind), key) - if mo: - loss_type = mo.group(1) # the cost_type in the key - # can be occupants, structural, nonstructural, ... - rmodel = nrml.to_python(oqparam.inputs[key]) - if len(rmodel) == 0: - raise InvalidFile('%s is empty!' % oqparam.inputs[key]) - rmodels[loss_type, kind] = rmodel - if rmodel.lossCategory is None: # NRML 0.4 - continue - cost_type = str(rmodel.lossCategory) - rmodel_kind = rmodel.__class__.__name__ - kind_ = kind.replace('_retrofitted', '') # strip retrofitted - if not rmodel_kind.lower().startswith(kind_): - raise ValueError( - 'Error in the file "%s_file=%s": is ' - 'of kind %s, expected %s' % ( - key, oqparam.inputs[key], rmodel_kind, - kind.capitalize() + 'Model')) - if cost_type != loss_type: - raise ValueError( - 'Error in the file "%s_file=%s": lossCategory is of ' - 'type "%s", expected "%s"' % - (key, oqparam.inputs[key], - rmodel.lossCategory, loss_type)) + for key, fname in get_risk_files(oqparam.inputs).items(): + kind, loss_type = key.split('/') # ex. vulnerability/structural + rmodel = nrml.to_python(fname) + if len(rmodel) == 0: + raise InvalidFile(f'{job_ini}: {fname} is empty!') + rmodels[loss_type, kind] = rmodel + if rmodel.lossCategory is None: # NRML 0.4 + continue + cost_type = str(rmodel.lossCategory) + rmodel_kind = rmodel.__class__.__name__ + kind_ = kind.replace('_retrofitted', '') # strip retrofitted + if not rmodel_kind.lower().startswith(kind_): + raise ValueError( + 'Error in the file "%s_file=%s": is ' + 'of kind %s, expected %s' % ( + key, oqparam.inputs[key], rmodel_kind, + kind.capitalize() + 'Model')) + if cost_type != loss_type: + raise ValueError( + 'Error in the file "%s_file=%s": lossCategory is of ' + 'type "%s", expected "%s"' % + (key, oqparam.inputs[key], + rmodel.lossCategory, loss_type)) cl_risk = oqparam.calculation_mode in ('classical', 'classical_risk') rlist = RiskFuncList() rlist.limit_states = []