Skip to content

Commit 4a826f5

Browse files
authored
Merge pull request #10104 from gem/rfs
Refactoring of get_risk_functions
2 parents 27bbcbf + 73fff0e commit 4a826f5

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

openquake/risklib/riskmodels.py

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ def get_risk_files(inputs):
6767
rfs['fragility/structural'] = inputs[
6868
'structural_fragility'] = inputs[key]
6969
del inputs['fragility']
70-
elif key.endswith(('_fragility', '_vulnerability')):
70+
elif key.endswith(('_fragility', '_vulnerability', '_vulnerability_retrofitted')):
7171
match = RISK_TYPE_REGEX.match(key)
72-
if match and 'retrofitted' not in key:
72+
if match:
7373
rfs['%s/%s' % (match.group(2), match.group(1))] = inputs[key]
7474
elif match is None:
7575
raise ValueError('Invalid key in %s: %s_file' % (job_ini, key))
@@ -135,45 +135,38 @@ def groupby_id(self):
135135
return {riskid: group_by_lt(rfs) for riskid, rfs in ddic.items()}
136136

137137

138-
def get_risk_functions(oqparam, kind='vulnerability fragility '
139-
'vulnerability_retrofitted'):
138+
def get_risk_functions(oqparam):
140139
"""
141140
:param oqparam:
142141
an OqParam instance
143-
:param kind:
144-
a space-separated string with the kinds of risk models to read
145142
:returns:
146143
a list of risk functions
147144
"""
148-
kinds = kind.split()
145+
job_ini = oqparam.inputs['job_ini']
149146
rmodels = AccumDict()
150-
for kind in kinds:
151-
for key in sorted(oqparam.inputs):
152-
mo = re.match('(occupants|%s)_%s$' % (LTYPE_REGEX, kind), key)
153-
if mo:
154-
loss_type = mo.group(1) # the cost_type in the key
155-
# can be occupants, structural, nonstructural, ...
156-
rmodel = nrml.to_python(oqparam.inputs[key])
157-
if len(rmodel) == 0:
158-
raise InvalidFile('%s is empty!' % oqparam.inputs[key])
159-
rmodels[loss_type, kind] = rmodel
160-
if rmodel.lossCategory is None: # NRML 0.4
161-
continue
162-
cost_type = str(rmodel.lossCategory)
163-
rmodel_kind = rmodel.__class__.__name__
164-
kind_ = kind.replace('_retrofitted', '') # strip retrofitted
165-
if not rmodel_kind.lower().startswith(kind_):
166-
raise ValueError(
167-
'Error in the file "%s_file=%s": is '
168-
'of kind %s, expected %s' % (
169-
key, oqparam.inputs[key], rmodel_kind,
170-
kind.capitalize() + 'Model'))
171-
if cost_type != loss_type:
172-
raise ValueError(
173-
'Error in the file "%s_file=%s": lossCategory is of '
174-
'type "%s", expected "%s"' %
175-
(key, oqparam.inputs[key],
176-
rmodel.lossCategory, loss_type))
147+
for key, fname in get_risk_files(oqparam.inputs).items():
148+
kind, loss_type = key.split('/') # ex. vulnerability/structural
149+
rmodel = nrml.to_python(fname)
150+
if len(rmodel) == 0:
151+
raise InvalidFile(f'{job_ini}: {fname} is empty!')
152+
rmodels[loss_type, kind] = rmodel
153+
if rmodel.lossCategory is None: # NRML 0.4
154+
continue
155+
cost_type = str(rmodel.lossCategory)
156+
rmodel_kind = rmodel.__class__.__name__
157+
kind_ = kind.replace('_retrofitted', '') # strip retrofitted
158+
if not rmodel_kind.lower().startswith(kind_):
159+
raise ValueError(
160+
'Error in the file "%s_file=%s": is '
161+
'of kind %s, expected %s' % (
162+
key, oqparam.inputs[key], rmodel_kind,
163+
kind.capitalize() + 'Model'))
164+
if cost_type != loss_type:
165+
raise ValueError(
166+
'Error in the file "%s_file=%s": lossCategory is of '
167+
'type "%s", expected "%s"' %
168+
(key, oqparam.inputs[key],
169+
rmodel.lossCategory, loss_type))
177170
cl_risk = oqparam.calculation_mode in ('classical', 'classical_risk')
178171
rlist = RiskFuncList()
179172
rlist.limit_states = []

0 commit comments

Comments
 (0)