def test_run(self): function = AshRasterPopulationFunction.instance() hazard_path = standard_data_path('hazard', 'ash_raster_wgs84.tif') exposure_path = standard_data_path( 'exposure', 'pop_binary_raster_20_20.asc') # We need clipping for both layers to be in the same dimension clipped_hazard, clipped_exposure = clip_layers( hazard_path, exposure_path) hazard_layer = read_layer(clipped_hazard.source()) exposure_layer = read_layer(clipped_exposure.source()) # Let's set the extent to the hazard extent function.hazard = SafeLayer(hazard_layer) function.exposure = SafeLayer(exposure_layer) function.run() impact = function.impact expected = [ [u'Population in very low hazard zone', 0], [u'Population in medium hazard zone', 1374], [u'Population in high hazard zone', 20], [u'Population in very high hazard zone', 0], [u'Population in low hazard zone', 8443], [u'Total affected population', 9837], [u'Unaffected population', 0], [u'Total population', 9837], [u'Population needing evacuation <sup>1</sup>', 9837] ] self.assertListEqual( expected, impact.impact_data['impact summary']['fields'])
def test_run(self): """TestPagerEarthquakeFatalityFunction: Test running the IF.""" 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 = PAGFatalityFunction.instance() impact_function.hazard = eq_layer impact_function.exposure = 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 ' 'pager 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_exposed_per_mmi = { 2.0: 0, 2.5: 0, 3.0: 0, 3.5: 0, 4.0: 0, 4.5: 0, 5.0: 0, 5.5: 0, 6.5: 0, 6.0: 0, 7.0: 0, 7.5: 60, 8.0: 140, 8.5: 0, 9.0: 0, 9.5: 0} result = impact_layer.get_keywords('exposed_per_mmi') message = 'Expecting %s, but it returns %s' % ( expected_exposed_per_mmi, result) self.assertEqual(expected_exposed_per_mmi, result, message)
def test_run(self): """TestPagerEarthquakeFatalityFunction: Test running the IF.""" 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 = PAGFatalityFunction.instance() impact_function.hazard = eq_layer impact_function.exposure = 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 ' 'pager 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_exposed_per_mmi = { 2.0: 0, 2.5: 0, 3.0: 0, 3.5: 0, 4.0: 0, 4.5: 0, 5.0: 0, 5.5: 0, 6.5: 0, 6.0: 0, 7.0: 0, 7.5: 60, 8.0: 140, 8.5: 0, 9.0: 0, 9.5: 0 } result = impact_layer.get_keywords('exposed_per_mmi') message = 'Expecting %s, but it returns %s' % ( expected_exposed_per_mmi, result) self.assertEqual(expected_exposed_per_mmi, result, message)
def test_run(self): function = TsunamiEvacuationFunction.instance() hazard_path = test_data_path('hazard', 'tsunami_wgs84.tif') exposure_path = test_data_path( 'exposure', 'pop_binary_raster_20_20.asc') # We need clipping for both layers to be in the same dimension clipped_hazard, clipped_exposure = clip_layers( hazard_path, exposure_path) hazard_layer = read_layer(clipped_hazard.source()) exposure_layer = read_layer(clipped_exposure.source()) # Let's set the extent to the hazard extent function.parameters['thresholds'].value = [0.7, 0.8, 0.9] function.hazard = SafeLayer(hazard_layer) function.exposure = SafeLayer(exposure_layer) function.run() impact = function.impact # Count of flooded objects is calculated "by the hands" # print "keywords", keywords keywords = impact.get_keywords() evacuated = float(keywords['evacuated']) total_needs_full = keywords['total_needs'] total_needs_weekly = OrderedDict([ [x['table name'], x['amount']] for x in total_needs_full['weekly'] ]) total_needs_single = OrderedDict([ [x['table name'], x['amount']] for x in total_needs_full['single'] ]) # #FIXME: This doesn't make sense due to clipping above. Update # clip_layers expected_evacuated = 1198 self.assertEqual(evacuated, expected_evacuated) self.assertEqual(total_needs_weekly['Rice [kg]'], 3355) self.assertEqual(total_needs_weekly['Family Kits'], 240) self.assertEqual(total_needs_weekly['Drinking Water [l]'], 20965) self.assertEqual(total_needs_weekly['Clean Water [l]'], 80266) self.assertEqual(total_needs_single['Toilets'], 60)
def test_run(self): function = TsunamiEvacuationFunction.instance() hazard_path = test_data_path('hazard', 'tsunami_wgs84.tif') exposure_path = test_data_path('exposure', 'pop_binary_raster_20_20.asc') # We need clipping for both layers to be in the same dimension clipped_hazard, clipped_exposure = clip_layers(hazard_path, exposure_path) hazard_layer = read_layer(clipped_hazard.source()) exposure_layer = read_layer(clipped_exposure.source()) # Let's set the extent to the hazard extent function.parameters['thresholds'].value = [0.7, 0.8, 0.9] function.hazard = SafeLayer(hazard_layer) function.exposure = SafeLayer(exposure_layer) function.run() impact = function.impact # Count of flooded objects is calculated "by the hands" # print "keywords", keywords keywords = impact.get_keywords() evacuated = float(keywords['evacuated']) total_needs_full = keywords['total_needs'] total_needs_weekly = OrderedDict([[x['table name'], x['amount']] for x in total_needs_full['weekly']]) total_needs_single = OrderedDict([[x['table name'], x['amount']] for x in total_needs_full['single']]) # #FIXME: This doesn't make sense due to clipping above. Update # clip_layers expected_evacuated = 1198 self.assertEqual(evacuated, expected_evacuated) self.assertEqual(total_needs_weekly['Rice [kg]'], 3355) self.assertEqual(total_needs_weekly['Family Kits'], 240) self.assertEqual(total_needs_weekly['Drinking Water [l]'], 20965) self.assertEqual(total_needs_weekly['Clean Water [l]'], 80266) self.assertEqual(total_needs_single['Toilets'], 60)
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)
def test_run(self): """TestITEarthquakeFatalityFunction: 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 = ITBFatalityFunction.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?') self.assertEqual(expected_question, impact_function.question) expected_result = { 'total_population': 200, 'total_fatalities': 0, # should be zero FIXME 'total_displaced': 200 } for key in expected_result.keys(): result = impact_layer.get_keywords(key) self.assertEqual(expected_result[key], result) expected_result = {} expected_result['fatalities_per_mmi'] = { 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0.17778, 9: 0, 10: 0 } 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.82221, 9: 0, 10: 0 } for key in expected_result.keys(): result = impact_layer.get_keywords(key) for item in expected_result[key].keys(): self.assertAlmostEqual(expected_result[key][item], result[item], places=4) expected_result = None result = impact_layer.get_keywords('prob_fatality_mag') self.assertEqual(expected_result, result) self.assertEqual(numpy.nansum(impact_layer.data), 200)
def test_run(self): """TestITEarthquakeFatalityFunction: 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 = ITBFatalityFunction.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') self.assertEqual(expected_question, impact_function.question) expected_result = { 'total_population': 200, 'total_fatalities': 0, # should be zero FIXME 'total_displaced': 200 } for key in expected_result.keys(): result = impact_layer.get_keywords(key) self.assertEqual(expected_result[key], result) expected_result = {} expected_result['fatalities_per_mmi'] = { 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0.17778, 9: 0, 10: 0 } 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.82221, 9: 0, 10: 0 } for key in expected_result.keys(): result = impact_layer.get_keywords(key) for item in expected_result[key].keys(): self.assertAlmostEqual( expected_result[key][item], result[item], places=4) expected_result = None result = impact_layer.get_keywords('prob_fatality_mag') self.assertEqual(expected_result, result) self.assertEqual(numpy.nansum(impact_layer.data), 200)
def test_run(self): """TestPagerEarthquakeFatalityFunction: 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 = PAGFatalityFunction.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 pager 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, # should be zero FIXME '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['fatalities_per_mmi'] = { 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0.083498, # FIXME should be rounded to zero!! not 10. 9: 0, 10: 0 } 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.91650, # 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 = [ # 8.0, 42.0, 42.0, 8.0, 0.0, 0.0, 0.0] # corresponds to 10 expected_result = [100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] # corresponds to <= 1 result = impact_function.compute_probability( impact_layer.get_keywords('total_fatalities_raw')) message = 'Expecting %s, but it returns %s' % (expected_result, result) self.assertEqual(expected_result, result, message)