def test_compute_probability(self): impact_function = ITBBayesianFatalityFunction.instance() total_fatalities = numpy.array( [1, 9, 10, 99, 101, 999, 9999, 10000, 100001, 999999]) result = impact_function.compute_probability(total_fatalities) expected_result = numpy.array([20., 20., 20., 10., 10., 20.]) numpy.testing.assert_allclose(expected_result, result, rtol=1.0e-3)
def test_compute_probability(self): impact_function = ITBBayesianFatalityFunction.instance() total_fatalities = numpy.array([ 1, 9, 10, 99, 101, 999, 9999, 10000, 100001, 999999]) result = impact_function.compute_probability(total_fatalities) expected_result = numpy.array([20., 20., 20., 10., 10., 20.]) numpy.testing.assert_allclose(expected_result, result, rtol=1.0e-3)
def test_parameter(self): """Test for checking parameter is carried out""" eq_path = standard_data_path('hazard', 'earthquake.tif') population_path = standard_data_path('exposure', 'pop_binary_raster_20_20.asc') # For EQ on Pops we need to clip the hazard and exposure first to the # same dimension clipped_hazard, clipped_exposure = clip_layers(eq_path, population_path) # noinspection PyUnresolvedReferences eq_layer = read_layer(str(clipped_hazard.source())) # noinspection PyUnresolvedReferences population_layer = read_layer(str(clipped_exposure.source())) impact_function = ITBBayesianFatalityFunction.instance() impact_function.hazard = SafeLayer(eq_layer) impact_function.exposure = SafeLayer(population_layer) expected = { 'postprocessors': { 'Age': { 'Age': { 'Adult ratio': 0.659, 'Elderly ratio': 0.078, 'Youth ratio': 0.263 } }, 'Gender': { 'Gender': True }, 'MinimumNeeds': { 'MinimumNeeds': True } } } self.assertDictEqual(expected, impact_function.parameters_value())
def test_parameter(self): """Test for checking parameter is carried out""" eq_path = standard_data_path('hazard', 'earthquake.tif') population_path = standard_data_path( 'exposure', 'pop_binary_raster_20_20.asc') # For EQ on Pops we need to clip the hazard and exposure first to the # same dimension clipped_hazard, clipped_exposure = clip_layers( eq_path, population_path) # noinspection PyUnresolvedReferences eq_layer = read_layer( str(clipped_hazard.source())) # noinspection PyUnresolvedReferences population_layer = read_layer( str(clipped_exposure.source())) impact_function = ITBBayesianFatalityFunction.instance() impact_function.hazard = SafeLayer(eq_layer) impact_function.exposure = SafeLayer(population_layer) expected = { 'postprocessors': { 'Age': { 'Age': { 'Adult ratio': 0.659, 'Elderly ratio': 0.078, 'Youth ratio': 0.263 } }, 'Gender': {'Gender': True}, 'MinimumNeeds': {'MinimumNeeds': True} } } self.assertDictEqual(expected, impact_function.parameters_value())
def test_run(self): """TestITBBayesianEarthquakeFatalityFunction: Test running the IF.""" # FIXME(Hyeuk): test requires more realistic hazard and population data eq_path = standard_data_path('hazard', 'earthquake.tif') population_path = standard_data_path('exposure', 'pop_binary_raster_20_20.asc') # For EQ on Pops we need to clip the hazard and exposure first to the # same dimension clipped_hazard, clipped_exposure = clip_layers(eq_path, population_path) # noinspection PyUnresolvedReferences eq_layer = read_layer(str(clipped_hazard.source())) # noinspection PyUnresolvedReferences population_layer = read_layer(str(clipped_exposure.source())) impact_function = ITBBayesianFatalityFunction.instance() impact_function.hazard = SafeLayer(eq_layer) impact_function.exposure = SafeLayer(population_layer) impact_function.run() impact_layer = impact_function.impact # Check the question expected_question = ( 'In the event of earthquake how many population might die or ' 'be displaced according itb bayesian model?') self.assertEqual(expected_question, impact_function.question) expected_result = { 'total_population': 200, 'total_fatalities': 0, 'total_displaced': 200 } for key_ in expected_result.keys(): result = impact_layer.get_keywords(key_) message = 'Expecting %s, but it returns %s' % ( expected_result[key_], result) self.assertEqual(expected_result[key_], result, message) expected_result = {} expected_result['exposed_per_mmi'] = { 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 200, 9: 0, 10: 0 } expected_result['displaced_per_mmi'] = { 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 199.6297, # FIXME should be 200.0 9: 0, 10: 0 } for key_ in expected_result.keys(): result = impact_layer.get_keywords(key_) for item in expected_result[key_].keys(): message = 'Expecting %s, but it returns %s' % ( expected_result[key_][item], result[item]) self.assertAlmostEqual(expected_result[key_][item], result[item], places=4, msg=message) expected_result = [100.0, 0.0, 0.0, 0.0, 0.0, 0.0] result = impact_layer.get_keywords('prob_fatality_mag') message = 'Expecting %s, but it returns %s' % (expected_result, result) self.assertEqual(expected_result, result, message)
def test_run(self): """TestITBBayesianEarthquakeFatalityFunction: Test running the IF.""" # FIXME(Hyeuk): test requires more realistic hazard and population data eq_path = test_data_path('hazard', 'earthquake.tif') population_path = test_data_path( 'exposure', 'pop_binary_raster_20_20.asc') # For EQ on Pops we need to clip the hazard and exposure first to the # same dimension clipped_hazard, clipped_exposure = clip_layers( eq_path, population_path) # noinspection PyUnresolvedReferences eq_layer = read_layer( str(clipped_hazard.source())) # noinspection PyUnresolvedReferences population_layer = read_layer( str(clipped_exposure.source())) impact_function = ITBBayesianFatalityFunction.instance() impact_function.hazard = SafeLayer(eq_layer) impact_function.exposure = SafeLayer(population_layer) impact_function.run() impact_layer = impact_function.impact # Check the question expected_question = ( 'In the event of earthquake how many population might die or ' 'be displaced according itb bayesian model') message = 'The question should be %s, but it returns %s' % ( expected_question, impact_function.question) self.assertEqual(expected_question, impact_function.question, message) expected_result = { 'total_population': 200, 'total_fatalities': 0, 'total_displaced': 200 } for key_ in expected_result.keys(): result = impact_layer.get_keywords(key_) message = 'Expecting %s, but it returns %s' % ( expected_result[key_], result) self.assertEqual(expected_result[key_], result, message) expected_result = {} expected_result['exposed_per_mmi'] = { 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 200, 9: 0, 10: 0 } expected_result['displaced_per_mmi'] = { 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 199.6297, # FIXME should be 200.0 9: 0, 10: 0 } for key_ in expected_result.keys(): result = impact_layer.get_keywords(key_) for item in expected_result[key_].keys(): message = 'Expecting %s, but it returns %s' % ( expected_result[key_][item], result[item]) self.assertAlmostEqual( expected_result[key_][item], result[item], places=4, msg=message) expected_result = [ 100.0, 0.0, 0.0, 0.0, 0.0, 0.0] result = impact_layer.get_keywords('prob_fatality_mag') message = 'Expecting %s, but it returns %s' % ( expected_result, result) self.assertEqual(expected_result, result, message)