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_run(self):
        function = ContinuousHazardPopulationFunction.instance()

        hazard_path = test_data_path(
            'hazard', 'continuous_flood_20_20.asc')
        exposure_path = test_data_path(
            'exposure', 'pop_binary_raster_20_20.asc')
        hazard_layer = read_layer(hazard_path)
        exposure_layer = read_layer(exposure_path)

        function.hazard = SafeLayer(hazard_layer)
        function.exposure = SafeLayer(exposure_layer)
        function.run()
        impact = function.impact

        # print "keywords", keywords
        keywords = impact.get_keywords()
        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']
        ])

        self.assertEqual(total_needs_weekly['Rice [kg]'], 336)
        self.assertEqual(total_needs_weekly['Drinking Water [l]'], 2100)
        self.assertEqual(total_needs_weekly['Clean Water [l]'], 8040)
        self.assertEqual(total_needs_weekly['Family Kits'], 24)
        self.assertEqual(total_needs_single['Toilets'], 6)
Esempio n. 3
0
    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)
Esempio n. 4
0
    def test_run(self):
        function = ContinuousHazardPopulationFunction.instance()

        hazard_path = test_data_path('hazard', 'continuous_flood_20_20.asc')
        exposure_path = test_data_path('exposure',
                                       'pop_binary_raster_20_20.asc')
        hazard_layer = read_layer(hazard_path)
        exposure_layer = read_layer(exposure_path)

        function.hazard = SafeLayer(hazard_layer)
        function.exposure = SafeLayer(exposure_layer)
        function.run()
        impact = function.impact

        # print "keywords", keywords
        keywords = impact.get_keywords()
        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']])

        self.assertEqual(total_needs_weekly['Rice [kg]'], 336)
        self.assertEqual(total_needs_weekly['Drinking Water [l]'], 2100)
        self.assertEqual(total_needs_weekly['Clean Water [l]'], 8040)
        self.assertEqual(total_needs_weekly['Family Kits'], 24)
        self.assertEqual(total_needs_single['Toilets'], 6)
Esempio n. 5
0
 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_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_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_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_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):
        function = ContinuousHazardPopulationFunction.instance()

        hazard_path = test_data_path("hazard", "continuous_flood_20_20.asc")
        exposure_path = test_data_path("exposure", "pop_binary_raster_20_20.asc")
        hazard_layer = read_layer(hazard_path)
        exposure_layer = read_layer(exposure_path)

        function.hazard = hazard_layer
        function.exposure = exposure_layer
        function.run()
        impact = function.impact

        # print "keywords", keywords
        keywords = impact.get_keywords()
        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"]])

        self.assertEqual(total_needs_weekly["Rice [kg]"], 336)
        self.assertEqual(total_needs_weekly["Drinking Water [l]"], 2100)
        self.assertEqual(total_needs_weekly["Clean Water [l]"], 8040)
        self.assertEqual(total_needs_weekly["Family Kits"], 24)
        self.assertEqual(total_needs_single["Toilets"], 6)
 def test_is_valid(self):
     """Test is_valid."""
     ifm = ContinuousHazardPopulationFunction()
     self.assertTrue(ifm.metadata().is_valid()[0])
 def test_is_valid(self):
     """Test is_valid."""
     ifm = ContinuousHazardPopulationFunction()
     self.assertTrue(ifm.metadata().is_valid()[0])