Skip to content

Commit 73fff0e

Browse files
committed
Simplified get_risk_functions
1 parent fe5cb0e commit 73fff0e

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

openquake/risklib/riskmodels.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -142,35 +142,31 @@ def get_risk_functions(oqparam):
142142
:returns:
143143
a list of risk functions
144144
"""
145+
job_ini = oqparam.inputs['job_ini']
145146
rmodels = AccumDict()
146-
#for key, fname in get_risk_files(oqparam.inputs):
147-
for kind in 'vulnerability fragility vulnerability_retrofitted'.split():
148-
for key in sorted(oqparam.inputs):
149-
mo = re.match('(%s)_%s$' % (LTYPE_REGEX, kind), key)
150-
if mo:
151-
loss_type = mo.group(1) # the cost_type in the key
152-
# can be occupants, structural, nonstructural, ...
153-
rmodel = nrml.to_python(oqparam.inputs[key])
154-
if len(rmodel) == 0:
155-
raise InvalidFile('%s is empty!' % oqparam.inputs[key])
156-
rmodels[loss_type, kind] = rmodel
157-
if rmodel.lossCategory is None: # NRML 0.4
158-
continue
159-
cost_type = str(rmodel.lossCategory)
160-
rmodel_kind = rmodel.__class__.__name__
161-
kind_ = kind.replace('_retrofitted', '') # strip retrofitted
162-
if not rmodel_kind.lower().startswith(kind_):
163-
raise ValueError(
164-
'Error in the file "%s_file=%s": is '
165-
'of kind %s, expected %s' % (
166-
key, oqparam.inputs[key], rmodel_kind,
167-
kind.capitalize() + 'Model'))
168-
if cost_type != loss_type:
169-
raise ValueError(
170-
'Error in the file "%s_file=%s": lossCategory is of '
171-
'type "%s", expected "%s"' %
172-
(key, oqparam.inputs[key],
173-
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))
174170
cl_risk = oqparam.calculation_mode in ('classical', 'classical_risk')
175171
rlist = RiskFuncList()
176172
rlist.limit_states = []

0 commit comments

Comments
 (0)