def test_functions_for_constraint(self): """Test functions_for_constraint.""" ifm = ImpactFunctionManager() impact_functions = ifm.functions_for_constraint( 'earthquake', 'population', 'raster', 'raster', 'continuous', 'continuous', ) expected = [ ITBFatalityFunction.metadata().as_dict(), ITBBayesianFatalityFunction.metadata().as_dict(), PAGFatalityFunction.metadata().as_dict(), ContinuousHazardPopulationFunction.metadata().as_dict() ] for key in impact_functions[0].keys(): if key == 'parameters': # We do not check the parameters since they are mutable. continue result = [x[key] for x in impact_functions] hope = [x[key] for x in expected] message = key self.assertItemsEqual(result, hope, message)
def test_compute_probability(self): impact_function = PAGFatalityFunction.instance() result = impact_function.compute_probability(50.0) expected_result = [17.0, 50.0, 30.0, 3.0, 0.0, 0.0] message = 'Expecting %s, but it returns %s' % ( expected_result, result) self.assertEqual(expected_result, result, msg=message)
def test_functions_for_constraint(self): """Test functions_for_constraint.""" ifm = ImpactFunctionManager() impact_functions = ifm.functions_for_constraint( 'earthquake', 'population', 'raster', 'raster', 'continuous', 'continuous', ) expected = [ ITBFatalityFunction.metadata().as_dict(), ITBBayesianFatalityFunction.metadata().as_dict(), PAGFatalityFunction.metadata().as_dict(), ContinuousHazardPopulationFunction.metadata().as_dict()] for key in impact_functions[0].keys(): if key == 'parameters': # We do not check the parameters since they are mutable. continue result = [x[key] for x in impact_functions] hope = [x[key] for x in expected] message = key self.assertItemsEqual(result, hope, message)
def test_is_valid(self): """Test is_valid.""" impact_functions = [ # Earthquake EarthquakeBuildingFunction(), ITBFatalityFunction(), PAGFatalityFunction(), # Generic ClassifiedPolygonHazardBuildingFunction(), ClassifiedPolygonHazardPopulationFunction(), ClassifiedRasterHazardBuildingFunction(), ClassifiedRasterHazardPopulationFunction(), ContinuousHazardPopulationFunction(), # Inundation FloodEvacuationVectorHazardFunction(), FloodPolygonRoadsFunction(), FloodRasterBuildingFunction(), FloodEvacuationRasterHazardFunction(), FloodRasterRoadsFunction(), FloodPolygonBuildingFunction(), TsunamiEvacuationFunction(), # Volcanic VolcanoPointBuildingFunction(), VolcanoPointPopulationFunction(), VolcanoPolygonBuildingFunction(), VolcanoPolygonPopulationFunction() ] for impact_function in impact_functions: valid = impact_function.metadata().is_valid() impact_function_name = impact_function.__class__.__name__ message = '%s is invalid because %s' % (impact_function_name, valid[1]) self.assertTrue(valid[0], message) if valid[0]: print '%s has a valid metadata.' % impact_function_name
def test_is_valid(self): """Test is_valid.""" impact_functions = [ # Earthquake EarthquakeBuildingFunction(), ITBFatalityFunction(), PAGFatalityFunction(), ITBBayesianFatalityFunction(), # Generic ClassifiedPolygonHazardBuildingFunction(), ClassifiedPolygonHazardLandCoverFunction(), ClassifiedPolygonHazardPopulationFunction(), ClassifiedPolygonHazardPolygonPeopleFunction(), ClassifiedRasterHazardBuildingFunction(), ClassifiedRasterHazardPopulationFunction(), ContinuousHazardPopulationFunction(), # Inundation FloodEvacuationVectorHazardFunction(), FloodPolygonRoadsFunction(), FloodRasterBuildingFunction(), FloodEvacuationRasterHazardFunction(), FloodRasterRoadsFunction(), FloodPolygonBuildingFunction(), # Tsunami TsunamiEvacuationFunction(), TsunamiRasterRoadsFunction(), TsunamiRasterLandcoverFunction(), TsunamiRasterBuildingFunction(), # Volcanic VolcanoPointBuildingFunction(), VolcanoPointPopulationFunction(), VolcanoPolygonBuildingFunction(), VolcanoPolygonPopulationFunction(), # Volcanic Ash AshRasterLandCoverFunction(), AshRasterPlacesFunction(), AshRasterPopulationFunction() ] self.assertEqual(len(impact_functions), len(EXPECTED_IF)) for impact_function in impact_functions: valid = impact_function.metadata().is_valid() impact_function_name = impact_function.__class__.__name__ message = '%s is invalid because %s' % (impact_function_name, valid[1]) self.assertTrue(valid[0], message) if valid[0]: # print '%s has a valid metadata.' % impact_function_name continue
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_round_to_sum(self): impact_function = PAGFatalityFunction.instance() result = impact_function.round_to_sum([10.26, 10.5, 29.8, 39.5, 9.94]) expected_result = [10.0, 10.0, 30.0, 40.0, 10.0] message = 'Expecting %s, but it returns %s' % (expected_result, result) self.assertEqual(expected_result, result, msg=message) result = impact_function.round_to_sum( [45.844, 43.02, 10.59, 0.54, 0.0055, 5.e-4]) expected_result = [46.0, 43.0, 11.0, 0.0, 0.0, 0.0] message = 'Expecting %s, but it returns %s' % (expected_result, result) self.assertEqual(expected_result, result, msg=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_round_to_sum(self): impact_function = PAGFatalityFunction.instance() result = impact_function.round_to_sum([ 10.26, 10.5, 29.8, 39.5, 9.94]) expected_result = [10.0, 10.0, 30.0, 40.0, 10.0] message = 'Expecting %s, but it returns %s' % ( expected_result, result) self.assertEqual(expected_result, result, msg=message) result = impact_function.round_to_sum([ 45.844, 43.02, 10.59, 0.54, 0.0055, 5.e-4]) expected_result = [46.0, 43.0, 11.0, 0.0, 0.0, 0.0] message = 'Expecting %s, but it returns %s' % ( expected_result, result) self.assertEqual(expected_result, result, msg=message)
def test_compute_fatality_rate(self): impact_function = PAGFatalityFunction.instance() expected_result = {2: 0, 3: 0, 4: 1.110e-15, 5: 5.463e-11, 6: 7.767e-8, 7: 1.193e-5, 8: 4.174e-4, 9: 5.219e-3, 10: 3.121e-2} result = impact_function.compute_fatality_rate() for item in expected_result.keys(): message = 'Expecting %s, but it returns %s' % ( expected_result[item], result[item]) self.assertAlmostEqual(expected_result[item], result[item], places=4, msg=message)
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)
def test_run(self): """TestPagerEarthquakeFatalityFunction: 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 = 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?') 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_) 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, 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, 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)