def test_ordinate_diffs(self): hazard_curve = Curve( [(0.01, 0.99), (0.08, 0.96), (0.17, 0.89), (0.26, 0.82), (0.36, 0.70), (0.55, 0.40), (0.70, 0.01)] ) expected_pos = [0.0673, 0.1336, 0.2931, 0.4689] pes = [0.05, 0.15, 0.3, 0.5, 0.7] self.assertTrue(allclose(expected_pos, hazard_curve.ordinate_diffs(pes), atol=0.00005))
def test_clip_low_imls_many_values(self): """ Test :py:method:`openquake.risklib.range.Curve.range_clip` to ensure that low values are clipped to the lowest valid value in the IML range. """ expected_imls = numpy.array([0.005, 0.005, 0.005]) test_input = [0.0049, 0.00001, 0.002] numpy.testing.assert_allclose( expected_imls, Curve.range_clip(test_input, self.TEST_IMLS)) # same test, except with a numpy.array-type input: numpy.testing.assert_allclose( expected_imls, Curve.range_clip(numpy.array(test_input), self.TEST_IMLS))
def test_clip_imls_with_many_normal_values(self): """ Test :py:method:`openquake.risklib.range.Curve.range_clip` to ensure that normal values (values within the defined IML range) are not changed. """ valid_imls = [0.005, 0.0269, 0.0051, 0.0268] expected_result = numpy.array(valid_imls) numpy.testing.assert_allclose( expected_result, Curve.range_clip(valid_imls, self.TEST_IMLS)) # same test, except with numpy.array-type input: numpy.testing.assert_allclose( expected_result, Curve.range_clip(numpy.array(valid_imls), self.TEST_IMLS))
def test_clip_high_iml_values(self): """ Test :py:method:`openquake.risklib.range.Curve.range_clip` to ensure that the high values are clipped to the highest valid value in the IML range. """ self.assertEqual(0.0269, Curve.range_clip(0.027, self.TEST_IMLS))
def test_clip_low_iml_values(self): """ Test :py:method:`openquake.risklib.range.Curve.range_clip` to ensure that low values are clipped to the lowest valid value in the IML range. """ self.assertEqual(0.005, Curve.range_clip(0.0049, self.TEST_IMLS))
def test_compute_loss_ratio_curve(self): hazard_curve = [ (0.01, 0.99), (0.08, 0.96), (0.17, 0.89), (0.26, 0.82), (0.36, 0.70), (0.55, 0.40), (0.70, 0.01)] imls = [0.1, 0.2, 0.4, 0.6] covs = [0.5, 0.3, 0.2, 0.1] loss_ratios = [0.05, 0.08, 0.2, 0.4] vulnerability_function = scientific.VulnerabilityFunction( imls, loss_ratios, covs, "LN") # pre computed values just use one intermediate # values between the imls, so steps=2 lrem = [[1., 1., 1., 1.], [8.90868149e-01, 9.99932030e-01, 1., 1.], [4.06642478e-01, 9.27063668e-01, 1., 1.], [2.14297309e-01, 7.12442306e-01, 9.99999988e-01, 1.], [1.09131851e-01, 4.41652761e-01, 9.99997019e-01, 1.], [7.84971008e-03, 2.00321301e-02, 9.55620783e-01, 1.], [7.59869969e-04, 5.41393717e-04, 4.60560758e-01, 1.], [2.79797605e-05, 1.66547090e-06, 1.59210054e-02, 9.97702369e-01], [1.75697664e-06, 9.04938835e-09, 1.59710253e-04, 4.80110732e-01], [2.89163471e-09, 2.43138842e-14, 6.60395072e-11, 7.56938368e-09], [2.38464803e-11, 0., 1.11022302e-16, 0.]] loss_ratio_curve = scientific.classical( vulnerability_function, lrem, hazard_curve, 2) expected_curve = Curve([ (0.0, 0.96), (0.025, 0.96), (0.05, 0.91), (0.065, 0.87), (0.08, 0.83), (0.14, 0.75), (0.2, 0.60), (0.3, 0.47), (0.4, 0.23), (0.7, 0.00), (1.0, 0.00)]) for x_value in expected_curve.abscissae: numpy.testing.assert_allclose( expected_curve.ordinate_for(x_value), loss_ratio_curve.ordinate_for(x_value), atol=0.005)
def test_clip_iml_with_normal_value(self): """ Test :py:method:`openquake.risklib.range.Curve.range_clip` to ensure that normal values (values within the defined IML range) are not changed. """ valid_imls = numpy.array([0.005, 0.0051, 0.0268, 0.0269]) for i in valid_imls: self.assertEqual(i, Curve.range_clip(i, valid_imls))
def test_can_construct_with_unordered_values(self): curve = Curve([(0.5, 1.0), (0.4, 2.0), (0.3, 2.0)]) self.assertEqual(1.0, curve.ordinate_for(0.5)) self.assertEqual(2.0, curve.ordinate_for(0.4)) self.assertEqual(2.0, curve.ordinate_for(0.3))
def test_can_pickle(self): curve = Curve([(0.5, 1.0), (0.4, 2.0), (0.3, 2.0)]) curve.ordinate_for(0.35) curve.abscissa_for(1.35) self.assertEqual(pickle.loads(pickle.dumps(curve)), curve)