def test_discrete_ff(self): fragility_model = { 'RC': [ scientific.FragilityFunctionDiscrete( 'LS1', [0.1, 0.2, 0.3, 0.5], [0.0073, 0.35, 0.74, 0.99]), scientific.FragilityFunctionDiscrete( 'LS2', [0.1, 0.2, 0.3, 0.5], [0.001, 0.02, 0.25, 0.72])], 'RM': [ scientific.FragilityFunctionDiscrete( 'LS1', [0.1, 0.2, 0.3, 0.5], [0.01, 0.64, 0.95, 1.0]), scientific.FragilityFunctionDiscrete( 'LS2', [0.1, 0.2, 0.3, 0.5], [0.0003, 0.05, 0.40, 0.86])]} calculator_rm = workflows.Damage( 'PGA', 'RM', dict(damage=fragility_model['RM'])) [asset_output_a1] = calculator_rm( 'damage', ['a1'], [self.hazard['a1']]).damages expected_means = [875.81078203, 1448.29628694, 675.89293103] expected_stdevs = [757.54019289, 256.15319254, 556.76593931] self.assert_ok(asset_output_a1 * 3000, expected_means, expected_stdevs) [asset_output_a3] = calculator_rm( 'damage', ['a3'], [self.hazard['a3']]).damages expected_means = [224.4178072, 465.64396155, 309.93823125] expected_stdevs = [220.65161409, 136.92817619, 246.84424913] self.assert_ok(asset_output_a3 * 1000, expected_means, expected_stdevs) rm = asset_output_a1 * 3000 + asset_output_a3 * 1000 calculator_rc = workflows.Damage( 'PGA', 'RC', dict(damage=fragility_model['RC'])) [asset_output_a2] = calculator_rc( 'damage', ['a2'], [self.hazard['a2']]).damages expected_means = [344.90849228, 747.62412976, 907.46737796] expected_stdevs = [300.61123079, 144.64852962, 417.30737837] self.assert_ok(asset_output_a2 * 2000, expected_means, expected_stdevs) rc = asset_output_a2 * 2000 assert_close( rm.mean(0), [1100.2285892246, 1913.9402484967, 985.8311622787]) assert_close( rm.std(0, ddof=1), [880.2774984768, 296.2197411105, 616.5632580754]) assert_close( rc.mean(0), [344.9084922789, 747.6241297573, 907.4673779638]) assert_close( rc.std(0, ddof=1), [300.6112307894, 144.6485296163, 417.307378365])
def test_continuous_ff(self): fragility_model = { 'RC': [scientific.FragilityFunctionContinuous('LS1', 0.2, 0.05), scientific.FragilityFunctionContinuous('LS2', 0.35, 0.10)], 'RM': [scientific.FragilityFunctionContinuous('LS1', 0.25, 0.08), scientific.FragilityFunctionContinuous('LS2', 0.40, 0.12)]} calculator_rm = workflows.Damage( 'PGA', 'RM', dict(damage=fragility_model['RM'])) calculator_rc = workflows.Damage( 'PGA', 'RC', dict(damage=fragility_model['RC'])) out = calculator_rm( 'damage', ['a1'], [self.hazard['a1']]) [asset_output_a1] = out.damages expected_means = [1562.6067550208, 1108.0189275488, 329.3743174305] expected_stdevs = [968.93502576, 652.7358505746, 347.3929450270] self.assert_ok(asset_output_a1 * 3000, expected_means, expected_stdevs) out = calculator_rm('damage', ['a3'], [self.hazard['a3']]) [asset_output_a3] = out.damages expected_means = [417.3296948271, 387.2084383654, 195.4618668074] expected_stdevs = [304.4769498434, 181.1415598664, 253.91309010185] self.assert_ok(asset_output_a3 * 1000, expected_means, expected_stdevs) rm = asset_output_a1 * 3000 + asset_output_a3 * 1000 [asset_output_a2] = calculator_rc( 'damage', ['a2'], [self.hazard['a2']]).damages expected_means = [56.7201291212, 673.1047565606, 1270.1751143182] expected_stdevs = [117.7802813522, 485.2023172324, 575.8724057319] self.assert_ok(asset_output_a2 * 2000, expected_means, expected_stdevs) rc = asset_output_a2 * 2000 assert_close( rm.mean(0), [1979.9364498479, 1495.2273659142, 524.8361842379]) assert_close( rm.std(0, ddof=1), [1103.6005152909, 745.3252495731, 401.9195159565]) assert_close( rc.mean(0), [56.7201291212, 673.1047565606, 1270.1751143182]) assert_close( rc.std(0, ddof=1), [117.7802813522, 485.2023172324, 575.8724057319])
def test_generator(self): with mock.patch('openquake.risklib.scientific.scenario_damage') as m: fragility_functions = mock.Mock() calc = workflows.Damage( 'PGA', 'TAXO', dict(damage=fragility_functions)) calc('damage', 'assets', 'hazard', None) self.assertEqual(m.call_count, 6) # called 3 x 2 times
def get_damage_states_and_risk_models(content): """ Parse the fragility XML file and return a list of damage_states and a dictionary {taxonomy: risk model} """ iterparse = iter(parsers.FragilityModelParser(content)) fmt, limit_states = iterparse.next() damage_states = ['no_damage'] + limit_states fragility_functions = collections.defaultdict(dict) tax_imt = dict() for taxonomy, iml, params, no_damage_limit in iterparse: tax_imt[taxonomy] = iml['IMT'] if fmt == "discrete": if no_damage_limit is None: fragility_functions[taxonomy] = [ scientific.FragilityFunctionDiscrete( iml['imls'], poes, iml['imls'][0]) for poes in params ] else: fragility_functions[taxonomy] = [ scientific.FragilityFunctionDiscrete( [no_damage_limit] + iml['imls'], [0.0] + poes, no_damage_limit) for poes in params ] else: fragility_functions[taxonomy] = [ scientific.FragilityFunctionContinuous(*mean_stddev) for mean_stddev in params ] risk_models = {} for taxonomy, ffs in fragility_functions.items(): dic = dict(damage=List(ffs, imt=tax_imt[taxonomy])) risk_models[taxonomy] = workflows.RiskModel(taxonomy, workflows.Damage(dic)) return damage_states, risk_models