def test_allowed_units(self): """Test for allowed_units API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata() .allowed_units( 'structure', 'polygon') expected_result = [unit_building_type_type, unit_building_generic] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertItemsEqual(result, expected_result, message) result = impact_function.metadata() .allowed_units( 'structure', 'continuous') expected_result = [] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) impact_function = FloodRasterBuildingFunction result = impact_function.metadata().allowed_units( 'structure', 'polygon') expected_result = [unit_building_type_type, unit_building_generic] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertItemsEqual(result, expected_result, message) result = impact_function.metadata()\ .allowed_units('flood', 'continuous') expected_result = [unit_metres_depth, unit_feet_depth] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message)
def test_purposes_for_layer(self): """Test for purposes_for_layer.""" impact_function = EarthquakeBuildingFunction() layer_purpose = impact_function.metadata().purposes_for_layer( 'polygon') self.assertIsNotNone(layer_purpose) expected_result = [layer_purpose_exposure] self.assertItemsEqual(layer_purpose, expected_result)
def test_exposures_for_layer(self): """Test exposures_for_layer.""" impact_function = EarthquakeBuildingFunction() exposures = impact_function.metadata().exposures_for_layer('polygon') expected = [exposure_structure] self.assertItemsEqual(exposures, expected) exposures = impact_function.metadata().exposures_for_layer('raster') expected = [] self.assertItemsEqual(exposures, expected)
def test_inner_class(self): """Test call inner class.""" impact_function = EarthquakeBuildingFunction() # call from an object metadata = impact_function.metadata() metadata_dictionary = metadata.as_dict() assert isinstance(metadata_dictionary, dict), 'I did not got a dict' # call from the class metadata = impact_function.metadata() metadata_dictionary = metadata.as_dict() assert isinstance(metadata_dictionary, dict), 'I did not got a dict'
def test_allowed_data_types(self): """Test for allowed_data_types API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().allowed_data_types('structure') expected_result = ['polygon', 'point'] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertItemsEqual(result, expected_result, message) result = impact_function.metadata().allowed_data_types('earthquake') expected_result = ['continuous'] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertItemsEqual(result, expected_result, message)
def test_get_exposures(self): """Test for get_exposures API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().get_exposures() expected_result = [exposure_structure] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) impact_function = FloodRasterBuildingFunction() result = impact_function.metadata().get_exposures() expected_result = [] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertNotEqual(result, expected_result, message)
def test_has_exposure(self): """Test for has_exposure API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().has_exposure(exposure_structure) expected_result = True message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) impact_function = FloodRasterBuildingFunction() result = impact_function.metadata().has_exposure(exposure_structure) expected_result = True message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message)
def test_get_hazards(self): """Test for get_hazards API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().get_hazards() expected_result = [hazard_earthquake] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) impact_function = FloodRasterBuildingFunction() result = impact_function.metadata().get_hazards() expected_result = [hazard_flood, hazard_tsunami] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message)
def test_subcategories_for_layer(self): """Test for subcategories_for_layer API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().subcategories_for_layer( category='hazard', layer_type='raster', data_type='continuous') expected_result = [hazard_earthquake] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().subcategories_for_layer( category='exposure', layer_type='vector', data_type='polygon') expected_result = [exposure_structure] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message)
def test_has_hazard_id(self): """Test for has_hazard_id API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().has_hazard_id( hazard_earthquake['id']) expected_result = True message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) impact_function = FloodRasterBuildingFunction() result = impact_function.metadata().has_hazard_id( hazard_earthquake['id']) expected_result = False message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message)
def test_run(self): """TestEarthquakeBuildingFunction: Test running the IF.""" eq_path = test_data_path("hazard", "earthquake.tif") building_path = test_data_path("exposure", "buildings.shp") eq_layer = read_layer(eq_path) building_layer = read_layer(building_path) impact_function = EarthquakeBuildingFunction.instance() impact_function.hazard = SafeLayer(eq_layer) impact_function.exposure = SafeLayer(building_layer) impact_function.run() impact_layer = impact_function.impact # Check the question expected_question = "In the event of earthquake how many buildings might be affected" message = "The question should be %s, but it returns %s" % (expected_question, impact_function.question) self.assertEqual(expected_question, impact_function.question, message) # Count by hand, # 1 = low, 2 = medium, 3 = high impact = {1: 0, 2: 181, 3: 0} result = {1: 0, 2: 0, 3: 0} impact_features = impact_layer.get_data() for i in range(len(impact_features)): impact_feature = impact_features[i] level = impact_feature.get(impact_function.target_field) result[level] += 1 message = "Expecting %s, but it returns %s" % (impact, result) self.assertEqual(impact, result, 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_run(self): """TestEarthquakeBuildingFunction: Test running the IF.""" eq_path = test_data_path('hazard', 'earthquake.tif') building_path = test_data_path('exposure', 'buildings.shp') eq_layer = read_layer(eq_path) building_layer = read_layer(building_path) impact_function = EarthquakeBuildingFunction.instance() impact_function.hazard = eq_layer impact_function.exposure = building_layer impact_function.run() impact_layer = impact_function.impact # Check the question expected_question = ('In the event of earthquake how many ' 'buildings might be affected') message = 'The question should be %s, but it returns %s' % ( expected_question, impact_function.question) self.assertEqual(expected_question, impact_function.question, message) # Count by hand, # 1 = low, 2 = medium, 3 = high impact = {1: 0, 2: 181, 3: 0} result = {1: 0, 2: 0, 3: 0} impact_features = impact_layer.get_data() for i in range(len(impact_features)): impact_feature = impact_features[i] level = impact_feature.get('Shake_cls') result[level] += 1 message = 'Expecting %s, but it returns %s' % (impact, result) self.assertEqual(impact, result, message)
def test_hazards_for_layer(self): """Test hazards_for_layer""" impact_function = EarthquakeBuildingFunction() hazards = impact_function.metadata().hazards_for_layer( 'raster', 'single_event') expected = [hazard_earthquake] self.assertItemsEqual(hazards, expected) hazards = impact_function.metadata().hazards_for_layer('raster',) expected = [hazard_earthquake] self.assertItemsEqual(hazards, expected) hazards = impact_function.metadata().hazards_for_layer( 'polygon', 'single_event') expected = [] self.assertItemsEqual(hazards, expected)
def test_hazards_for_layer(self): """Test hazards_for_layer""" impact_function = EarthquakeBuildingFunction() hazards = impact_function.metadata().hazards_for_layer( 'raster', 'single_event') expected = [hazard_earthquake] self.assertItemsEqual(hazards, expected) hazards = impact_function.metadata().hazards_for_layer('raster', ) expected = [hazard_earthquake] self.assertItemsEqual(hazards, expected) hazards = impact_function.metadata().hazards_for_layer( 'polygon', 'single_event') expected = [] self.assertItemsEqual(hazards, expected)
def test_categories_for_layer(self): """Test for categories_for_layer API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().categories_for_layer( layer_type='raster', data_type='continuous') expected_result = ['hazard'] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().categories_for_layer( layer_type='vector', data_type='line') expected_result = [] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().categories_for_layer( layer_type='vector', data_type='polygon') expected_result = ['exposure'] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertItemsEqual(result, expected_result, message) impact_function = ContinuousHazardPopulationFunction() result = impact_function.metadata().categories_for_layer( layer_type='raster', data_type='continuous') expected_result = ['exposure', 'hazard'] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertListEqual(result, expected_result, message) result = impact_function.metadata().categories_for_layer( layer_type='vector', data_type='line') expected_result = [] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().categories_for_layer( layer_type='vector', data_type='polygon') expected_result = [] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertItemsEqual(result, expected_result, message) result = impact_function.metadata().categories_for_layer( layer_type='vector', data_type='point') expected_result = [] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertItemsEqual(result, expected_result, message)
def test_allowed_subcategories(self): """Test for allowed_subcategories API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().allowed_subcategories( category='hazard') expected_result = [hazard_earthquake] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().allowed_subcategories( category='exposure') expected_result = [exposure_structure] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().allowed_subcategories() expected_result = [exposure_structure, hazard_earthquake] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) impact_function = ContinuousHazardPopulationFunction() result = impact_function.metadata().allowed_subcategories( category='hazard') expected_result = hazard_all message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message)
def test_hazard_categories_for_layer(self): """Test for hazard_categories_for_layer""" impact_function = EarthquakeBuildingFunction() hazard_categories = impact_function.metadata()\ .hazard_categories_for_layer('raster') expected = [ hazard_category_single_event, hazard_category_multiple_event] self.assertItemsEqual(hazard_categories, expected) hazard_categories = impact_function.metadata() \ .hazard_categories_for_layer('raster', 'earthquake') expected = [ hazard_category_single_event, hazard_category_multiple_event] self.assertItemsEqual(hazard_categories, expected) hazard_categories = impact_function.metadata() \ .hazard_categories_for_layer('polygon') expected = [] self.assertItemsEqual(hazard_categories, expected)
def test_allowed_layer_constraints(self): """Test for allowed_layer_constraints API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().allowed_layer_constraints() expected_result = [ layer_raster_continuous, layer_vector_polygon, layer_vector_point ] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().allowed_layer_constraints('hazard') expected_result = [layer_raster_continuous] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().allowed_layer_constraints( 'exposure') expected_result = [layer_vector_polygon, layer_vector_point] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message)
def test_allowed_layer_constraints(self): """Test for allowed_layer_constraints API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().allowed_layer_constraints() expected_result = [layer_raster_continuous, layer_vector_polygon, layer_vector_point] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().allowed_layer_constraints( 'hazard') expected_result = [layer_raster_continuous] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().allowed_layer_constraints( 'exposure') expected_result = [layer_vector_polygon, layer_vector_point] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message)
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_allowed_units(self): """Test for allowed_units API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().allowed_units( 'structure', 'polygon') expected_result = [unit_building_type_type, unit_building_generic] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertItemsEqual(result, expected_result, message) result = impact_function.metadata().allowed_units( 'structure', 'continuous') expected_result = [] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) impact_function = FloodRasterBuildingFunction result = impact_function.metadata().allowed_units( 'structure', 'polygon') expected_result = [unit_building_type_type, unit_building_generic] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertItemsEqual(result, expected_result, message) result = impact_function.metadata()\ .allowed_units('flood', 'continuous') expected_result = [unit_metres_depth, unit_feet_depth] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message)
def test_calculate_impact(self): """Test calculating impact.""" eq_path = test_data_path('hazard', 'earthquake.tif') building_path = test_data_path('exposure', 'buildings.shp') eq_layer = read_layer(eq_path) building_layer = read_layer(building_path) impact_function = EarthquakeBuildingFunction.instance() impact_function.hazard = SafeLayer(eq_layer) impact_function.exposure = SafeLayer(building_layer) impact_layer = calculate_impact(impact_function) self.assertIsNotNone(impact_layer)
def test_run(self): """TestEarthquakeBuildingFunction: Test running the IF.""" eq_path = test_data_path('hazard', 'earthquake.tif') building_path = test_data_path( 'exposure', 'buildings.shp') eq_layer = read_layer(eq_path) building_layer = read_layer(building_path) impact_function = EarthquakeBuildingFunction.instance() impact_function.hazard = eq_layer impact_function.exposure = building_layer impact_function.run() impact_layer = impact_function.impact # Check the question expected_question = ('In the event of earthquake how many ' 'buildings might be affected') message = 'The question should be %s, but it returns %s' % ( expected_question, impact_function.question) self.assertEqual(expected_question, impact_function.question, message) # Count by hand, # 1 = low, 2 = medium, 3 = high impact = { 1: 0, 2: 181, 3: 0 } result = { 1: 0, 2: 0, 3: 0 } impact_features = impact_layer.get_data() for i in range(len(impact_features)): impact_feature = impact_features[i] level = impact_feature.get('Shake_cls') result[level] += 1 message = 'Expecting %s, but it returns %s' % (impact, result) self.assertEqual(impact, result, message)
def test_units_for_layer(self): """Test for units_for_layer API.""" impact_function = EarthquakeBuildingFunction() result = impact_function.metadata().units_for_layer( subcategory='earthquake', layer_type='raster', data_type='continuous') expected_result = [unit_mmi] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().units_for_layer( subcategory='flood', layer_type='raster', data_type='continuous') expected_result = [] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().units_for_layer( subcategory='earthquake', layer_type='vector', data_type='continuous') expected_result = [] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) result = impact_function.metadata().units_for_layer( subcategory='earthquake', layer_type='raster', data_type='polygon') expected_result = [] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message) impact_function = ContinuousHazardPopulationFunction() result = impact_function.metadata().units_for_layer( subcategory='flood', layer_type='raster', data_type='continuous') expected_result = [] message = ('I expect %s but I got %s.' % (expected_result, result)) self.assertEqual(result, expected_result, message)
def test_run(self): """TestEarthquakeBuildingFunction: Test running the IF.""" eq_path = standard_data_path('hazard', 'earthquake.tif') building_path = standard_data_path('exposure', 'buildings.shp') eq_layer = read_layer(eq_path) building_layer = read_layer(building_path) impact_function = EarthquakeBuildingFunction.instance() impact_function.hazard = SafeLayer(eq_layer) impact_function.exposure = SafeLayer(building_layer) impact_function.run() impact_layer = impact_function.impact # Check the question expected_question = ( 'In the event of earthquake how many buildings might be affected?') self.assertEqual(expected_question, impact_function.question) # Count by hand, # 1 = low, 2 = medium, 3 = high impact = { 1: 0, 2: 181, 3: 0 } result = { 1: 0, 2: 0, 3: 0 } impact_features = impact_layer.get_data() for i in range(len(impact_features)): impact_feature = impact_features[i] level = impact_feature.get(impact_function.target_field) result[level] += 1 message = 'Expecting %s, but it returns %s' % (impact, result) self.assertEqual(impact, result, message) # Check the mixin works too... self.maxDiff = None action_checklist = impact_function.action_checklist() expected = [ 'Which structures have warning capacity (eg. sirens, speakers, ' 'etc.)?', 'Are the water and electricity services still operating?', 'Are the health centres still open?', 'Are the other public services accessible?', 'Which buildings will be evacuation centres?', 'Where will we locate the operations centre?', 'Where will we locate warehouse and/or distribution centres?', 'Are the schools and hospitals still active?' ] self.assertListEqual(expected, action_checklist) not_expected = [ 'Where will the students from the 0 closed schools go to study?', 'Where will the patients from the 0 closed hospitals go for ' 'treatment and how will we transport them?', ] for item in not_expected: self.assertNotIn(item, action_checklist)