def test_calc_no_change_pass(self):
        """Test calc without future change"""
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        entity = Entity()
        entity.read_excel(ENT_DEMO_TODAY)
        entity.check()
        entity.exposures.ref_year = 2018
        cost_ben = CostBenefit()
        cost_ben.calc(hazard, entity, future_year=2040)

        self.assertEqual(cost_ben.imp_meas_present, dict())
        self.assertEqual(len(cost_ben.imp_meas_future), 5)
        self.assertEqual(cost_ben.present_year, 2018)
        self.assertEqual(cost_ben.future_year, 2040)

        self.assertEqual(cost_ben.cost_ben_ratio['Mangroves'],
                         0.04230714690616641)
        self.assertEqual(cost_ben.cost_ben_ratio['Beach nourishment'],
                         0.06998836431681373)
        self.assertEqual(cost_ben.cost_ben_ratio['Seawall'],
                         0.2679741183248266)
        self.assertEqual(cost_ben.cost_ben_ratio['Building code'],
                         0.30286828677985717)

        self.assertEqual(cost_ben.benefit['Mangroves'], 3.100583368954022e+10)
        self.assertEqual(cost_ben.benefit['Beach nourishment'],
                         2.468981832719974e+10)
        self.assertEqual(cost_ben.benefit['Seawall'], 3.3132973770502796e+10)
        self.assertEqual(cost_ben.benefit['Building code'],
                         3.0376240767284798e+10)

        self.assertEqual(cost_ben.tot_climate_risk, 1.2150496306913972e+11)
Exemplo n.º 2
0
    def test_remove_measure(self):
        """Test remove_measure method"""
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        entity = Entity()
        entity.read_excel(ENT_DEMO_TODAY)
        entity.check()
        entity.exposures.ref_year = 2018
        cost_ben = CostBenefit()
        cost_ben.calc(hazard,
                      entity,
                      future_year=2040,
                      risk_func=risk_aai_agg,
                      imp_time_depen=None,
                      save_imp=True)

        to_remove = 'Mangroves'
        self.assertTrue(to_remove in cost_ben.benefit.keys())
        cost_ben.remove_measure(to_remove)
        self.assertTrue(to_remove not in cost_ben.color_rgb.keys())
        self.assertTrue(to_remove not in cost_ben.benefit.keys())
        self.assertTrue(to_remove not in cost_ben.cost_ben_ratio.keys())
        self.assertTrue(to_remove not in cost_ben.imp_meas_future.keys())
        self.assertTrue(to_remove not in cost_ben.imp_meas_present.keys())
        self.assertEqual(len(cost_ben.imp_meas_present), 0)
        self.assertEqual(len(cost_ben.imp_meas_future), 4)
        self.assertEqual(len(cost_ben.color_rgb), 4)
        self.assertEqual(len(cost_ben.cost_ben_ratio), 3)
        self.assertEqual(len(cost_ben.benefit), 3)
    def test_combine_current_pass(self):
        """ Test combine_measures with only future"""
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        entity = Entity()
        entity.read_excel(ENT_DEMO_TODAY)
        entity.check()
        entity.exposures.ref_year = 2018
        cost_ben = CostBenefit()
        cost_ben.calc(hazard, entity, future_year=2040, risk_func=risk_aai_agg,
                      imp_time_depen=None, save_imp=True)

        new_name = 'combine'
        new_color = np.array([0.1, 0.1, 0.1])
        new_cb = cost_ben.combine_measures(['Mangroves', 'Seawall'], new_name, new_color,
            entity.disc_rates, imp_time_depen=None, risk_func=risk_aai_agg)

        self.assertTrue(np.allclose(new_cb.color_rgb[new_name], new_color))
        self.assertEqual(len(new_cb.imp_meas_present), 0)
        new_imp = cost_ben.imp_meas_future['no measure']['impact'].at_event - \
            cost_ben.imp_meas_future['Mangroves']['impact'].at_event
        new_imp += cost_ben.imp_meas_future['no measure']['impact'].at_event - \
            cost_ben.imp_meas_future['Seawall']['impact'].at_event
        new_imp = np.maximum(cost_ben.imp_meas_future['no measure']['impact'].at_event - new_imp, 0)
        self.assertTrue(np.allclose(new_cb.imp_meas_future[new_name]['impact'].at_event, new_imp))
        self.assertAlmostEqual(new_cb.imp_meas_future[new_name]['risk'],
                               np.sum(new_imp*cost_ben.imp_meas_future['no measure']['impact'].frequency), 5)
        self.assertAlmostEqual(new_cb.imp_meas_future[new_name]['cost'][0],
                               cost_ben.imp_meas_future['Mangroves']['cost'][0]+cost_ben.imp_meas_future['Seawall']['cost'][0])
        self.assertAlmostEqual(new_cb.imp_meas_future[new_name]['cost'][1], 1)
        self.assertTrue(np.allclose(new_cb.imp_meas_future[new_name]['efc'].impact,
                                    new_cb.imp_meas_future[new_name]['impact'].calc_freq_curve().impact))
        self.assertAlmostEqual(new_cb.imp_meas_future[new_name]['risk_transf'], 0)
        self.assertAlmostEqual(new_cb.benefit[new_name], 51781337529.07264)
        self.assertAlmostEqual(new_cb.cost_ben_ratio[new_name], 0.19679962474434248)
    def test_calc_change_pass(self):
        """Test calc with future change"""
        # present
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        entity = Entity()
        entity.read_excel(ENT_DEMO_TODAY)
        entity.exposures.rename(columns={'if_': 'if_TC'}, inplace=True)
        entity.check()
        entity.exposures.ref_year = 2018

        # future
        ent_future = Entity()
        ent_future.read_excel(ENT_DEMO_FUTURE)
        ent_future.check()
        ent_future.exposures.ref_year = 2040

        haz_future = copy.deepcopy(hazard)
        haz_future.intensity.data += 25

        cost_ben = CostBenefit()
        cost_ben.calc(hazard, entity, haz_future, ent_future)

        self.assertEqual(cost_ben.present_year, 2018)
        self.assertEqual(cost_ben.future_year, 2040)
        self.assertEqual(cost_ben.tot_climate_risk, 5.768659152882021e+11)

        self.assertEqual(cost_ben.imp_meas_present['no measure']['risk'], 6.51220115756442e+09)
        self.assertEqual(cost_ben.imp_meas_present['Mangroves']['risk'], 4.850407096284983e+09)
        self.assertEqual(cost_ben.imp_meas_present['Beach nourishment']['risk'], 5.188921355413834e+09)
        self.assertEqual(cost_ben.imp_meas_present['Seawall']['risk'], 4.736400526119911e+09)
        self.assertEqual(cost_ben.imp_meas_present['Building code']['risk'], 4.884150868173321e+09)

        self.assertEqual(cost_ben.imp_meas_future['no measure']['risk'], 5.9506659786664024e+10)
        self.assertEqual(cost_ben.imp_meas_future['Mangroves']['risk'], 4.826231151473135e+10)
        self.assertEqual(cost_ben.imp_meas_future['Beach nourishment']['risk'], 5.0647250923231674e+10)
        self.assertEqual(cost_ben.imp_meas_future['Seawall']['risk'], 21089567135.7345)
        self.assertEqual(cost_ben.imp_meas_future['Building code']['risk'], 4.462999483999791e+10)

        self.assertAlmostEqual(cost_ben.benefit['Mangroves'], 113345027690.81276)
        self.assertAlmostEqual(cost_ben.benefit['Beach nourishment'], 89444869971.53653)
        self.assertAlmostEqual(cost_ben.benefit['Seawall'], 347977469896.1333)
        self.assertAlmostEqual(cost_ben.benefit['Building code'], 144216478822.05154)

        self.assertAlmostEqual(cost_ben.cost_ben_ratio['Mangroves'], 0.011573232523528404)
        self.assertAlmostEqual(cost_ben.cost_ben_ratio['Beach nourishment'], 0.01931916274851638)
        self.assertAlmostEqual(cost_ben.cost_ben_ratio['Seawall'], 0.025515385913577368)
        self.assertAlmostEqual(cost_ben.cost_ben_ratio['Building code'], 0.06379298728650741)

        self.assertEqual(cost_ben.tot_climate_risk, 576865915288.2021)
    def test_calc_change_pass(self):
        """Test calc with future change"""
        # present
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        entity = Entity()
        entity.read_excel(ENT_DEMO_TODAY)
        entity.exposures.rename(columns={'if_': 'if_TC'}, inplace=True)
        entity.check()
        entity.exposures.ref_year = 2018

        # future
        ent_future = Entity()
        ent_future.read_excel(ENT_DEMO_FUTURE)
        ent_future.check()
        ent_future.exposures.ref_year = 2040

        haz_future = copy.deepcopy(hazard)
        haz_future.intensity.data += 25

        cost_ben = CostBenefit()
        cost_ben.calc(hazard, entity, haz_future, ent_future)

        self.assertEqual(cost_ben.present_year, 2018)
        self.assertEqual(cost_ben.future_year, 2040)
        self.assertEqual(cost_ben.tot_climate_risk, 5.768659152882021e+11)

        self.assertEqual(cost_ben.imp_meas_present['no measure']['risk'],
                         6.51220115756442e+09)
        self.assertEqual(cost_ben.imp_meas_present['Mangroves']['risk'],
                         4.850407096284983e+09)
        self.assertEqual(
            cost_ben.imp_meas_present['Beach nourishment']['risk'],
            5.188921355413834e+09)
        self.assertEqual(cost_ben.imp_meas_present['Seawall']['risk'],
                         4.736400526119911e+09)
        self.assertEqual(cost_ben.imp_meas_present['Building code']['risk'],
                         4.884150868173321e+09)

        self.assertEqual(cost_ben.imp_meas_future['no measure']['risk'],
                         5.9506659786664024e+10)
        self.assertEqual(cost_ben.imp_meas_future['Mangroves']['risk'],
                         4.826231151473135e+10)
        self.assertEqual(cost_ben.imp_meas_future['Beach nourishment']['risk'],
                         5.0647250923231674e+10)
        self.assertEqual(cost_ben.imp_meas_future['Seawall']['risk'],
                         21089567135.7345)
        self.assertEqual(cost_ben.imp_meas_future['Building code']['risk'],
                         4.462999483999791e+10)
    def test_apply_transf_future_pass(self):
        """ Test apply_risk_transfer with present and future """
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        entity = Entity()
        entity.read_excel(ENT_DEMO_TODAY)
        entity.check()
        entity.exposures.ref_year = 2018

        fut_ent = copy.deepcopy(entity)
        fut_ent.exposures.ref_year = 2040

        cost_ben = CostBenefit()
        cost_ben.calc(hazard, entity, ent_future=fut_ent, risk_func=risk_aai_agg,
                      imp_time_depen=None, save_imp=True)

        new_name = 'combine'
        new_color = np.array([0.1, 0.1, 0.1])
        risk_transf=(1.0e7, 15.0e11, 1)
        new_cb = cost_ben.combine_measures(['Mangroves', 'Seawall'], new_name, new_color,
            entity.disc_rates, imp_time_depen=None, risk_func=risk_aai_agg)
        new_cb.apply_risk_transfer(new_name, risk_transf[0], risk_transf[1],
            entity.disc_rates, cost_fix=0, cost_factor=risk_transf[2], imp_time_depen=1,
            risk_func=risk_aai_agg)

        tr_name = 'risk transfer (' + new_name + ')'
        new_imp = cost_ben.imp_meas_future['no measure']['impact'].at_event - \
            cost_ben.imp_meas_future['Mangroves']['impact'].at_event
        new_imp += cost_ben.imp_meas_future['no measure']['impact'].at_event - \
            cost_ben.imp_meas_future['Seawall']['impact'].at_event
        new_imp = np.maximum(cost_ben.imp_meas_future['no measure']['impact'].at_event - new_imp, 0)
        imp_layer = np.minimum(np.maximum(new_imp - risk_transf[0], 0), risk_transf[1])
        risk_transfer = np.sum(imp_layer * cost_ben.imp_meas_future['no measure']['impact'].frequency)
        new_imp = np.maximum(new_imp - imp_layer, 0)

        self.assertTrue(np.allclose(new_cb.color_rgb[new_name], new_color))
        self.assertEqual(len(new_cb.imp_meas_present), 3)
        self.assertTrue(np.allclose(new_cb.imp_meas_future[tr_name]['impact'].at_event, new_imp))
        self.assertTrue(np.allclose(new_cb.imp_meas_present[tr_name]['impact'].at_event, new_imp))
        self.assertAlmostEqual(new_cb.imp_meas_future[tr_name]['risk'],
                               np.sum(new_imp*cost_ben.imp_meas_future['no measure']['impact'].frequency), 5)
        self.assertAlmostEqual(new_cb.imp_meas_present[tr_name]['risk'],
                               np.sum(new_imp*cost_ben.imp_meas_future['no measure']['impact'].frequency), 5)
        self.assertAlmostEqual(new_cb.cost_ben_ratio[tr_name]*new_cb.benefit[tr_name], 69715165679.7042)
        self.assertTrue(np.allclose(new_cb.imp_meas_future[tr_name]['efc'].impact,
                                    new_cb.imp_meas_future[tr_name]['impact'].calc_freq_curve().impact))
        self.assertAlmostEqual(new_cb.imp_meas_future[tr_name]['risk_transf'], risk_transfer)
        self.assertAlmostEqual(new_cb.benefit[tr_name], 69715165679.7042, 4) # benefit = impact layer
        self.assertAlmostEqual(new_cb.cost_ben_ratio[tr_name], 1)
    def _map_costben_calc(self, param_sample, **kwargs):
        """
        Map to compute cost benefit for all parameter samples in parallel

        Parameters
        ----------
        param_sample : pd.DataFrame.iterrows()
            Generator of the parameter samples

        Returns
        -------
         : list
            icost benefit metrics list for all samples containing
            imp_meas_present, imp_meas_future, tot_climate_risk,
            benefit, cost_ben_ratio

        """

        # [1] only the rows of the dataframe passed by pd.DataFrame.iterrows()
        haz_samples = param_sample[1][self.unc_vars['haz'].labels].to_dict()
        ent_samples = param_sample[1][self.unc_vars['ent'].labels].to_dict()
        haz_fut_samples = param_sample[1][
            self.unc_vars['haz_fut'].labels].to_dict()
        ent_fut_samples = param_sample[1][
            self.unc_vars['ent_fut'].labels].to_dict()

        haz = self.unc_vars['haz'].uncvar_func(**haz_samples)
        ent = self.unc_vars['ent'].uncvar_func(**ent_samples)
        haz_fut = self.unc_vars['haz_fut'].uncvar_func(**haz_fut_samples)
        ent_fut = self.unc_vars['ent_fut'].uncvar_func(**ent_fut_samples)

        cb = CostBenefit()
        cb.calc(hazard=haz,
                entity=ent,
                haz_future=haz_fut,
                ent_future=ent_fut,
                save_imp=False,
                **kwargs)

        # Extract from climada.impact the chosen metrics
        return [
            cb.imp_meas_present, cb.imp_meas_future, cb.tot_climate_risk,
            cb.benefit, cb.cost_ben_ratio
        ]