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)
Esempio n. 2
0
    def test_cutoff_hazard_pass(self):
        """Test _cutoff_hazard_damage"""
        meas = MeasureSet()
        meas.read_mat(ENT_TEST_MAT)
        act_1 = meas.get_measure(name='Seawall')[0]

        haz = Hazard('TC')
        haz.read_mat(HAZ_TEST_MAT)
        exp = Exposures()
        exp.read_mat(ENT_TEST_MAT)
        exp.gdf.rename(columns={'if_': 'if_TC'}, inplace=True)
        exp.check()

        imp_set = ImpactFuncSet()
        imp_set.read_mat(ENT_TEST_MAT)

        new_haz = act_1._cutoff_hazard_damage(exp, imp_set, haz)

        self.assertFalse(id(new_haz) == id(haz))

        pos_no_null = np.array([6249, 7697, 9134, 13500, 13199, 5944, 9052, 9050, 2429,
                                5139, 9053, 7102, 4096, 1070, 5948, 1076, 5947, 7432,
                                5949, 11694, 5484, 6246, 12147, 778, 3326, 7199, 12498,
                               11698, 6245, 5327, 4819, 8677, 5970, 7101, 779, 3894,
                                9051, 5976, 3329, 5978, 4282, 11697, 7193, 5351, 7310,
                                7478, 5489, 5526, 7194, 4283, 7191, 5328, 4812, 5528,
                                5527, 5488, 7475, 5529, 776, 5758, 4811, 6223, 7479,
                                7470, 5480, 5325, 7477, 7318, 7317, 11696, 7313, 13165,
                                6221])
        all_haz = np.arange(haz.intensity.shape[0])
        all_haz[pos_no_null] = -1
        pos_null = np.argwhere(all_haz > 0).reshape(-1)
        for i_ev in pos_null:
            self.assertEqual(new_haz.intensity[i_ev, :].max(), 0)
    def test_calc_cb_no_change_pass(self):
        """Test _calc_cost_benefit without present value against reference value"""
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        entity = Entity()
        entity.read_mat(ENT_TEST_MAT)
        entity.measures._data['TC'] = entity.measures._data.pop('XX')
        for meas in entity.measures.get_measure('TC'):
            meas.haz_type = 'TC'
        entity.check()

        cost_ben = CostBenefit()
        cost_ben._calc_impact_measures(hazard, entity.exposures, entity.measures,
            entity.impact_funcs, when='future', risk_func=risk_aai_agg, save_imp=True)

        cost_ben.present_year = 2018
        cost_ben.future_year = 2040
        cost_ben._calc_cost_benefit(entity.disc_rates)

        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)
Esempio n. 4
0
    def test_calc_imp_mat_pass(self):
        """Test save imp_mat"""
        # Read default entity values
        ent = Entity()
        ent.read_excel(ENT_DEMO_TODAY)
        ent.check()

        # Read default hazard file
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        # Create impact object
        impact = Impact()

        # Assign centroids to exposures
        ent.exposures.assign_centroids(hazard)

        # Compute the impact over the whole exposures
        impact.calc(ent.exposures, ent.impact_funcs, hazard, save_mat=True)
        self.assertIsInstance(impact.imp_mat, sparse.csr_matrix)
        self.assertEqual(impact.imp_mat.shape, (hazard.event_id.size,
                                                ent.exposures.gdf.value.size))
        np.testing.assert_array_almost_equal_nulp(
            np.array(impact.imp_mat.sum(axis=1)).ravel(), impact.at_event, nulp=5)
        np.testing.assert_array_almost_equal_nulp(
            np.sum(impact.imp_mat.toarray() * impact.frequency[:, None], axis=0).reshape(-1),
            impact.eai_exp)
Esempio n. 5
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_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)
Esempio n. 7
0
 def __init__(self):
     """ Calls the Hazard init dunder. Sets unit to 'm/s'. """
     Hazard.__init__(self, HAZ_TYPE)
     self.units = 'm/s'
     self.ssi = np.array([], float)
     self.ssi_wisc = np.array([], float)
     self.ssi_full_area = np.array([], float)
Esempio n. 8
0
    def test_calc_imp_mat_pass(self):
        """Test save imp_mat"""
        # Read default entity values
        ent = Entity()
        ent.read_excel(ENT_DEMO_TODAY)
        ent.check()

        # Read default hazard file
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        # Create impact object
        impact = Impact()

        # Assign centroids to exposures
        ent.exposures.assign_centroids(hazard)

        # Compute the impact over the whole exposures
        impact.calc(ent.exposures, ent.impact_funcs, hazard, save_mat=True)
        self.assertTrue(isinstance(impact.imp_mat, sparse.csr_matrix))
        self.assertEqual(impact.imp_mat.shape,
                         (hazard.event_id.size, ent.exposures.value.size))
        self.assertTrue(
            np.allclose(
                np.sum(impact.imp_mat, axis=1).reshape(-1), impact.at_event))
        self.assertTrue(
            np.allclose(
                np.array(
                    np.sum(np.multiply(impact.imp_mat.toarray(),
                                       impact.frequency.reshape(-1, 1)),
                           axis=0)).reshape(-1), impact.eai_exp))
Esempio n. 9
0
    def test_write_read_excel_pass(self):
        """Test write and read in excel"""
        ent = Entity()
        ent.read_excel(ENT_DEMO_TODAY)
        ent.check()

        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        imp_write = Impact()
        ent.exposures.assign_centroids(hazard)
        imp_write.calc(ent.exposures, ent.impact_funcs, hazard)
        file_name = os.path.join(DATA_FOLDER, 'test.xlsx')
        imp_write.write_excel(file_name)

        imp_read = Impact()
        imp_read.read_excel(file_name)

        self.assertTrue(np.array_equal(imp_write.event_id, imp_read.event_id))
        self.assertTrue(np.array_equal(imp_write.date, imp_read.date))
        self.assertTrue(np.array_equal(imp_write.coord_exp,
                                       imp_read.coord_exp))
        self.assertTrue(np.allclose(imp_write.eai_exp, imp_read.eai_exp))
        self.assertTrue(np.allclose(imp_write.at_event, imp_read.at_event))
        self.assertTrue(np.array_equal(imp_write.frequency,
                                       imp_read.frequency))
        self.assertEqual(imp_write.tot_value, imp_read.tot_value)
        self.assertEqual(imp_write.aai_agg, imp_read.aai_agg)
        self.assertEqual(imp_write.unit, imp_read.unit)
        self.assertEqual(
            0,
            len([
                i for i, j in zip(imp_write.event_name, imp_read.event_name)
                if i != j
            ]))
        self.assertIsInstance(imp_read.crs, dict)
Esempio n. 10
0
    def test_calc_sector_total_impact(self):
        """Test running total impact calculations."""
        sup = SupplyChain()
        sup.read_wiod16(year='test',
                        range_rows=(5, 117),
                        range_cols=(4, 116),
                        col_iso3=2,
                        col_sectors=1)

        # Tropical cyclone over Florida and Caribbean
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)

        # Read demo entity values
        # Set the entity default file to the demo one
        exp = Exposures()
        exp.read_hdf5(EXP_DEMO_H5)
        exp.check()
        exp.gdf.region_id = 840  #assign right id for USA
        exp.assign_centroids(hazard)

        impf_tc = IFTropCyclone()
        impf_tc.set_emanuel_usa()
        impf_set = ImpactFuncSet()
        impf_set.append(impf_tc)
        impf_set.check()

        sup.calc_sector_direct_impact(hazard, exp, impf_set)
        sup.calc_indirect_impact(io_approach='ghosh')
        sup.calc_total_impact()

        self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]),
                               sup.total_impact.shape)
        self.assertAlmostEqual((sup.mriot_data.shape[0], ),
                               sup.total_aai_agg.shape)
Esempio n. 11
0
    def test_append_new_var_pass(self):
        """ New variable appears if hazard to append is empty. """
        haz = dummy_hazard()
        haz.new_var = np.ones(haz.size)

        app_haz = Hazard('TC')
        app_haz.append(haz)
        self.assertIn('new_var', app_haz.__dict__)
Esempio n. 12
0
 def __init__(self, pool=None):
     """Empty constructor. """
     Hazard.__init__(self, HAZ_TYPE)
     if pool:
         self.pool = pool
         LOGGER.info('Using %s CPUs.', self.pool.ncpus)
     else:
         self.pool = None
Esempio n. 13
0
    def test_same_events_append(self):
        """Append hazard with same events (and diff centroids).
        Events are appended with all new centroids columns. """
        haz1 = dummy_hazard()
        haz2 = Hazard('TC')
        haz2.tag.file_name = 'file2.mat'
        haz2.tag.description = 'Description 2'
        haz2.centroids = Centroids()
        haz2.centroids.set_lat_lon(np.array([7, 9, 11]), np.array([8, 10, 12]))

        haz2.event_id = haz1.event_id
        haz2.event_name = haz1.event_name.copy()
        haz2.frequency = haz1.frequency
        haz2.date = haz1.date
        haz2.fraction = sparse.csr_matrix([[0.22, 0.32, 0.44], \
                                           [0.11, 0.11, 0.11], \
                                           [0.32, 0.11, 0.99], \
                                           [0.32, 0.22, 0.88]])
        haz2.intensity = sparse.csr_matrix([[0.22, 3.33, 6.44], \
                                            [1.11, 0.11, 1.11], \
                                            [8.33, 4.11, 4.4], \
                                            [9.33, 9.22, 1.77]])
        haz2.units = 'm/s'

        haz1.append(haz2)

        # expected values
        haz1_ori = dummy_hazard()
        res_inten = sparse.lil_matrix(np.zeros((8, 6)))
        res_inten[0:4, 0:3] = haz1_ori.intensity
        res_inten[4:, 3:] = haz2.intensity

        res_frac = sparse.lil_matrix(np.zeros((8, 6)))
        res_frac[0:4, 0:3] = haz1_ori.fraction
        res_frac[4:, 3:] = haz2.fraction

        self.assertTrue(np.array_equal(res_inten.todense(),
                                       haz1.intensity.todense()))
        self.assertTrue(sparse.isspmatrix_csr(haz1.intensity))
        self.assertTrue(np.array_equal(res_frac.todense(), \
                                       haz1.fraction.todense()))
        self.assertTrue(sparse.isspmatrix_csr(haz1.fraction))
        self.assertEqual(haz1.event_name,
                         haz1_ori.event_name + haz2.event_name)
        self.assertTrue(np.array_equal(haz1.date,
                                       np.append(haz1_ori.date, haz2.date)))
        self.assertTrue(np.array_equal(haz1.orig,
                                       np.append(haz1_ori.orig, haz2.orig)))
        self.assertTrue(np.array_equal(haz1.event_id, np.arange(1,9)))
        self.assertTrue(np.array_equal(haz1.frequency,
                                       np.append(haz1_ori.frequency, haz2.frequency)))
        self.assertEqual(haz1_ori.units, haz1.units)

        self.assertEqual(haz1.tag.file_name, \
                         [haz1_ori.tag.file_name, haz2.tag.file_name])
        self.assertEqual(haz1.tag.haz_type, haz1_ori.tag.haz_type)
        self.assertEqual(haz1.tag.description, \
                         [haz1_ori.tag.description, haz2.tag.description])
    def test_calib_instance(self):
        """ Test save calib instance """
        # Read default entity values
        ent = Entity()
        ent.read_excel(ENT_DEMO_TODAY)
        ent.check()

        # Read default hazard file
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)

        # get impact function from set
        imp_func = ent.impact_funcs.get_func(hazard.tag.haz_type,
                                             ent.exposures.if_TC.median())

        # Assign centroids to exposures
        ent.exposures.assign_centroids(hazard)

        # create input frame
        df_in = pd.DataFrame.from_dict({
            'v_threshold': [25.7],
            'other_param': [2],
            'hazard': [HAZ_TEST_MAT]
        })
        df_in_yearly = pd.DataFrame.from_dict({
            'v_threshold': [25.7],
            'other_param': [2],
            'hazard': [HAZ_TEST_MAT]
        })

        # Compute the impact over the whole exposures
        df_out = calib_instance(hazard, ent.exposures, imp_func, df_in)
        df_out_yearly = calib_instance(hazard,
                                       ent.exposures,
                                       imp_func,
                                       df_in_yearly,
                                       yearly_impact=True)
        # calc Impact as comparison
        impact = Impact()
        impact.calc(ent.exposures, ent.impact_funcs, hazard)
        IYS = impact.calc_impact_year_set(all_years=True)

        # do the tests
        self.assertTrue(isinstance(df_out, pd.DataFrame))
        self.assertTrue(isinstance(df_out_yearly, pd.DataFrame))
        self.assertEqual(df_out.shape[0], hazard.event_id.size)
        self.assertEqual(df_out_yearly.shape[0], 161)
        self.assertTrue(all(df_out['event_id'] == hazard.event_id))
        self.assertTrue(
            all(df_out[df_in.columns[0]].isin(df_in[df_in.columns[0]])))
        self.assertTrue(
            all(df_out_yearly[df_in.columns[1]].isin(df_in[df_in.columns[1]])))
        self.assertTrue(
            all(df_out_yearly[df_in.columns[2]].isin(df_in[df_in.columns[2]])))
        self.assertTrue(
            all(df_out['impact_CLIMADA'].values == impact.at_event))
        self.assertTrue(
            all(df_out_yearly['impact_CLIMADA'].values == [*IYS.values()]))
Esempio n. 15
0
    def test_hazard_pass(self):
        ''' Read an hazard excel file correctly.'''

        # Read demo excel file
        hazard = Hazard('TC')
        description = 'One single file.'
        hazard.read_excel(HAZ_TEMPLATE_XLS, description)

        # Check results
        n_events = 100
        n_centroids = 45

        self.assertEqual(hazard.units, '')

        self.assertEqual(hazard.centroids.coord.shape, (n_centroids, 2))
        self.assertEqual(hazard.centroids.coord[0][0], -25.95)
        self.assertEqual(hazard.centroids.coord[0][1], 32.57)
        self.assertEqual(hazard.centroids.coord[n_centroids-1][0], -24.7)
        self.assertEqual(hazard.centroids.coord[n_centroids-1][1], 33.88)

        self.assertEqual(len(hazard.event_name), 100)
        self.assertEqual(hazard.event_name[12], 'event013')

        self.assertEqual(hazard.event_id.dtype, int)
        self.assertEqual(hazard.event_id.shape, (n_events,))
        self.assertEqual(hazard.event_id[0], 1)
        self.assertEqual(hazard.event_id[n_events-1], 100)

        self.assertEqual(hazard.date.dtype, int)
        self.assertEqual(hazard.date.shape, (n_events,))
        self.assertEqual(hazard.date[0], 675874)
        self.assertEqual(hazard.date[n_events-1], 676329)

        self.assertEqual(hazard.event_name[0], 'event001')
        self.assertEqual(hazard.event_name[50], 'event051')
        self.assertEqual(hazard.event_name[-1], 'event100')

        self.assertEqual(hazard.frequency.dtype, np.float)
        self.assertEqual(hazard.frequency.shape, (n_events,))
        self.assertEqual(hazard.frequency[0], 0.01)
        self.assertEqual(hazard.frequency[n_events-2], 0.001)

        self.assertEqual(hazard.intensity.dtype, np.float)
        self.assertEqual(hazard.intensity.shape, (n_events, n_centroids))

        self.assertEqual(hazard.fraction.dtype, np.float)
        self.assertEqual(hazard.fraction.shape, (n_events, n_centroids))
        self.assertEqual(hazard.fraction[0, 0], 1)
        self.assertEqual(hazard.fraction[10, 19], 1)
        self.assertEqual(hazard.fraction[n_events-1, n_centroids-1], 1)

        self.assertTrue(np.all(hazard.orig))

        # tag hazard
        self.assertEqual(hazard.tag.file_name, HAZ_TEMPLATE_XLS)
        self.assertEqual(hazard.tag.description, description)
        self.assertEqual(hazard.tag.haz_type, 'TC')
Esempio n. 16
0
    def test_ref_value_insure_pass(self):
        """Test result against reference value"""
        # Read demo entity values
        # Set the entity default file to the demo one
        ent = Entity()
        ent.read_excel(ENT_DEMO_TODAY)
        ent.check()

        # Read default hazard file
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        # Create impact object
        impact = Impact()
        impact.at_event = np.zeros(hazard.intensity.shape[0])
        impact.eai_exp = np.zeros(len(ent.exposures.value))
        impact.tot_value = 0

        # Assign centroids to exposures
        ent.exposures.assign_centroids(hazard)

        # Compute impact for 6th exposure
        iexp = 5
        # Take its impact function
        imp_id = ent.exposures.if_TC[iexp]
        imp_fun = ent.impact_funcs.get_func(hazard.tag.haz_type, imp_id)
        # Compute
        insure_flag = True
        impact._exp_impact(np.array([iexp]), ent.exposures, hazard, imp_fun,
                           insure_flag)

        self.assertEqual(impact.eai_exp.size, ent.exposures.shape[0])
        self.assertEqual(impact.at_event.size, hazard.intensity.shape[0])

        events_pos = hazard.intensity[:, ent.exposures.centr_TC[iexp]].nonzero(
        )[0]
        res_exp = np.zeros((ent.exposures.shape[0]))
        res_exp[iexp] = np.sum(impact.at_event[events_pos] *
                               hazard.frequency[events_pos])
        self.assertTrue(np.array_equal(res_exp, impact.eai_exp))

        self.assertEqual(0, impact.at_event[12])
        # Check first 3 values
        self.assertEqual(0, impact.at_event[12])
        self.assertEqual(0, impact.at_event[41])
        self.assertEqual(1.0626600695059455e+06, impact.at_event[44])

        # Check intermediate values
        self.assertEqual(0, impact.at_event[6281])
        self.assertEqual(0, impact.at_event[4998])
        self.assertEqual(0, impact.at_event[9527])
        self.assertEqual(1.3318063850487845e+08, impact.at_event[7192])
        self.assertEqual(4.667108555054083e+06, impact.at_event[8624])

        # Check last 3 values
        self.assertEqual(0, impact.at_event[14349])
        self.assertEqual(0, impact.at_event[14347])
        self.assertEqual(0, impact.at_event[14309])
Esempio n. 17
0
    def test_hazard_pass(self):
        ''' Read a hazard mat file correctly.'''
        # Read demo excel file
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)

        # Check results
        n_events = 14450
        n_centroids = 100

        self.assertEqual(hazard.units, 'm/s')

        self.assertEqual(hazard.centroids.coord.shape, (n_centroids, 2))

        self.assertEqual(hazard.event_id.dtype, int)
        self.assertEqual(hazard.event_id.shape, (n_events,))

        self.assertEqual(hazard.frequency.dtype, np.float)
        self.assertEqual(hazard.frequency.shape, (n_events,))

        self.assertEqual(hazard.intensity.dtype, np.float)
        self.assertEqual(hazard.intensity.shape, (n_events, n_centroids))
        self.assertEqual(hazard.intensity[12, 46], 12.071393519949979)
        self.assertEqual(hazard.intensity[13676, 49], 17.228323602220616)

        self.assertEqual(hazard.fraction.dtype, np.float)
        self.assertEqual(hazard.fraction.shape, (n_events, n_centroids))
        self.assertEqual(hazard.fraction[8454, 98], 1)
        self.assertEqual(hazard.fraction[85, 54], 0)

        self.assertEqual(len(hazard.event_name), n_events)
        self.assertEqual(hazard.event_name[124], 125)

        self.assertEqual(len(hazard.date), n_events)
        self.assertEqual(dt.datetime.fromordinal(hazard.date[0]).year, 1851)
        self.assertEqual(dt.datetime.fromordinal(hazard.date[0]).month, 6)
        self.assertEqual(dt.datetime.fromordinal(hazard.date[0]).day, 25)
        self.assertEqual(dt.datetime.fromordinal(hazard.date[78]).year, 1852)
        self.assertEqual(dt.datetime.fromordinal(hazard.date[78]).month, 9)
        self.assertEqual(dt.datetime.fromordinal(hazard.date[78]).day, 22)
        self.assertEqual(dt.datetime.fromordinal(hazard.date[-1]).year, 2011)
        self.assertEqual(dt.datetime.fromordinal(hazard.date[-1]).month, 11)
        self.assertEqual(dt.datetime.fromordinal(hazard.date[-1]).day, 6)

        self.assertTrue(hazard.orig[0])
        self.assertTrue(hazard.orig[11580])
        self.assertTrue(hazard.orig[4940])
        self.assertFalse(hazard.orig[3551])
        self.assertFalse(hazard.orig[10651])
        self.assertFalse(hazard.orig[4818])

        # tag hazard
        self.assertEqual(hazard.tag.file_name, HAZ_TEST_MAT)
        self.assertEqual(hazard.tag.description, \
                         ' TC hazard event set, generated 14-Nov-2017 10:09:05')
        self.assertEqual(hazard.tag.haz_type, 'TC')
    def test_calc_cb_change_pass(self):
        """Test _calc_cost_benefit with present value against reference value"""
        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)
        entity = Entity()
        entity.read_mat(ENT_TEST_MAT)
        entity.measures._data['TC'] = entity.measures._data.pop('XX')
        for meas in entity.measures.get_measure('TC'):
            meas.haz_type = 'TC'
        entity.check()

        cost_ben = CostBenefit()
        cost_ben._calc_impact_measures(hazard, entity.exposures, entity.measures,
            entity.impact_funcs, when='present', risk_func=risk_aai_agg, save_imp=False)

        ent_future = Entity()
        ent_future.read_excel(ENT_DEMO_FUTURE)
        ent_future.check()

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

        cost_ben._calc_impact_measures(haz_future, ent_future.exposures, ent_future.measures,
            ent_future.impact_funcs, when='future', risk_func=risk_aai_agg, save_imp=False)

        cost_ben.present_year = 2018
        cost_ben.future_year = 2040
        cost_ben._calc_cost_benefit(entity.disc_rates, imp_time_depen=1)

        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)
Esempio n. 19
0
 def __init__(self, pool=None):
     """Empty constructor."""
     Hazard.__init__(self, HAZ_TYPE)
     self.category = np.array([], int)
     self.basin = list()
     if pool:
         self.pool = pool
         LOGGER.info('Using %s CPUs.', self.pool.ncpus)
     else:
         self.pool = None
Esempio n. 20
0
 def test_impact(self):
     ent = Entity()
     ent.read_excel(ENT_DEMO_TODAY)
     ent.check()
     hazard = Hazard('TC')
     hazard.read_mat(HAZ_TEST_MAT)
     impact = Impact()
     ent.exposures.assign_centroids(hazard)
     impact.calc(ent.exposures, ent.impact_funcs, hazard)
     return impact
Esempio n. 21
0
 def test_assign_raster_same_pass(self):
     """Test assign_centroids with raster hazard"""
     exp = Exposures()
     exp.set_from_raster(HAZ_DEMO_FL, window=Window(10, 20, 50, 60))
     exp.check()
     haz = Hazard('FL')
     haz.set_raster([HAZ_DEMO_FL], window=Window(10, 20, 50, 60))
     exp.assign_centroids(haz)
     np.testing.assert_array_equal(exp.gdf[INDICATOR_CENTR + 'FL'].values,
                                   np.arange(haz.centroids.size, dtype=int))
Esempio n. 22
0
    def test_same_centroids_extend(self):
        """Append hazard with same centroids, different events."""
        haz1 = dummy_hazard()
        haz2 = Hazard('TC')
        haz2.tag.file_name = 'file2.mat'
        haz2.tag.description = 'Description 2'

        haz2.centroids = haz1.centroids
        haz2.event_id = np.array([5, 6, 7, 8])
        haz2.event_name = ['ev5', 'ev6', 'ev7', 'ev8']
        haz2.frequency = np.array([0.9, 0.75, 0.75, 0.22])
        haz2.fraction = sparse.csr_matrix([[0.2, 0.3, 0.4], \
                                           [0.1, 0.1, 0.1], \
                                           [0.3, 0.1, 0.9], \
                                           [0.3, 0.2, 0.8]])
        haz2.intensity = sparse.csr_matrix([[0.2, 3.3, 6.4], \
                                            [1.1, 0.1, 1.01], \
                                            [8.3, 4.1, 4.0], \
                                            [9.3, 9.2, 1.7]])
        haz2.units = 'm/s'

        haz1.append(haz2)
        haz1.check()

        # expected values
        haz1_orig = dummy_hazard()
        exp_inten = np.zeros((8, 3))
        exp_inten[0:4, 0:3] = haz1_orig.intensity.todense()
        exp_inten[4:8, 0:3] = haz2.intensity.todense()
        exp_frac = np.zeros((8, 3))
        exp_frac[0:4, 0:3] = haz1_orig.fraction.todense()
        exp_frac[4:8, 0:3] = haz2.fraction.todense()

        self.assertEqual(haz1.event_id.size, 8)
        self.assertTrue(sparse.isspmatrix_csr(haz1.intensity))
        self.assertTrue(sparse.isspmatrix_csr(haz1.fraction))
        for i_ev in range(haz1.event_id.size):
            self.assertTrue(
                any((haz1.intensity[i_ev].todense() == exp_inten).all(1)))
            self.assertTrue(
                any((haz1.fraction[i_ev].todense() == exp_frac).all(1)))
            self.assertTrue(haz1.event_name[i_ev] in haz1_orig.event_name +
                            haz2.event_name)
            self.assertTrue(
                haz1.date[i_ev] in np.append(haz1_orig.date, haz2.date))
            self.assertTrue(
                haz1.orig[i_ev] in np.append(haz1_orig.orig, haz2.orig))
            self.assertTrue(haz1.event_id[i_ev] in np.append(
                haz1_orig.event_id, haz2.event_id))
            self.assertTrue(haz1.frequency[i_ev] in np.append(
                haz1_orig.frequency, haz2.frequency))

        self.assertEqual(haz1.centroids.size, 3)
        self.assertTrue(
            np.array_equal(haz1.centroids.coord, haz2.centroids.coord))
        self.assertEqual(haz1.tag.file_name, \
                         [haz1_orig.tag.file_name, haz2.tag.file_name])
        self.assertEqual(haz1.tag.haz_type, haz1_orig.tag.haz_type)
        self.assertEqual(haz1.tag.description, \
                         [haz1_orig.tag.description, haz2.tag.description])
Esempio n. 23
0
    def __init__(self, pool=None):
        """Empty constructor. """
        Hazard.__init__(self, HAZ_TYPE)
        if pool:
            self.pool = pool
            LOGGER.info('Using %s CPUs.', self.pool.ncpus)
        else:
            self.pool = None

        self.crop = DFL_CROP
        self.intensity_def = INT_DEF
Esempio n. 24
0
    def test_same_events_same(self):
        """Append hazard with same events and diff centroids. After removing
        duplicate events, initial events are obtained with 0 intensity and
        fraction in new appended centroids."""
        haz1 = dummy_hazard()
        haz2 = Hazard('TC')
        haz2.tag.file_name = 'file2.mat',
        haz2.tag.description = 'Description 2'
        haz2.centroids = Centroids()
        haz2.centroids.set_lat_lon(np.array([7, 9, 11]), np.array([8, 10, 12]))

        haz2.event_id = haz1.event_id
        haz2.event_name = haz1.event_name
        haz2.frequency = haz1.frequency
        haz2.date = haz1.date
        haz2.fraction = sparse.csr_matrix([[0.22, 0.32, 0.44], \
                                           [0.11, 0.11, 0.11], \
                                           [0.32, 0.11, 0.99], \
                                           [0.32, 0.22, 0.88]])
        haz2.intensity = sparse.csr_matrix([[0.22, 3.33, 6.44], \
                                            [1.11, 0.11, 1.11], \
                                            [8.33, 4.11, 4.4], \
                                            [9.33, 9.22, 1.77]])
        haz2.units = 'm/s'

        haz1.append(haz2)
        haz1.remove_duplicates()
        haz1.check()

        # expected values
        haz_res = dummy_hazard()
        haz_res.intensity = sparse.hstack([haz_res.intensity, \
            sparse.lil_matrix((haz_res.intensity.shape[0], 3))], format='lil').tocsr()
        haz_res.fraction = sparse.hstack([haz_res.fraction, \
            sparse.lil_matrix((haz_res.fraction.shape[0], 3))], format='lil').tocsr()
        self.assertTrue(np.array_equal(haz_res.intensity.todense(), \
                                       haz1.intensity.todense()))
        self.assertTrue(sparse.isspmatrix_csr(haz1.intensity))
        self.assertTrue(np.array_equal(haz_res.fraction.todense(), \
                                       haz1.fraction.todense()))
        self.assertTrue(sparse.isspmatrix_csr(haz1.fraction))
        self.assertEqual(haz1.event_name, haz_res.event_name)
        self.assertTrue(np.array_equal(haz1.date, haz_res.date))
        self.assertTrue(np.array_equal(haz1.orig, haz_res.orig))
        self.assertTrue(np.array_equal(haz1.event_id, \
                                       haz_res.event_id))
        self.assertTrue(np.array_equal(haz1.frequency, haz_res.frequency))
        self.assertEqual(haz_res.units, haz1.units)

        self.assertEqual(haz1.tag.file_name, \
                         [haz_res.tag.file_name, haz2.tag.file_name])
        self.assertEqual(haz1.tag.haz_type, haz_res.tag.haz_type)
        self.assertEqual(haz1.tag.description, \
                         [haz_res.tag.description, haz2.tag.description])
Esempio n. 25
0
    def __init__(self, pool=None):
        """Initialize values.

        Parameters
        ----------
        pool : pathos.pool, optional
            Pool that will be used for parallel computation when applicable. Default: None
        """
        Hazard.__init__(self, haz_type=HAZ_TYPE, pool=pool)
        self.category = np.array([], int)
        self.basin = []
        self.windfields = []
Esempio n. 26
0
 def test_assign_raster_pass(self):
     """ Test assign_centroids with raster hazard """
     exp = Exposures()
     exp['longitude'] = np.array([-69.235, -69.2427, -72, -68.8016496, 30])
     exp['latitude'] = np.array([10.235, 10.226, 2, 9.71272097, 50])
     exp.crs = DEF_CRS
     haz = Hazard('FL')
     haz.set_raster([HAZ_DEMO_FL], window=Window(10, 20, 50, 60))
     exp.assign_centroids(haz)
     self.assertEqual(exp[INDICATOR_CENTR + 'FL'][0], 51)
     self.assertEqual(exp[INDICATOR_CENTR + 'FL'][1], 100)
     self.assertEqual(exp[INDICATOR_CENTR + 'FL'][2], -1)
     self.assertEqual(exp[INDICATOR_CENTR + 'FL'][3], 3000 - 1)
     self.assertEqual(exp[INDICATOR_CENTR + 'FL'][4], -1)
    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_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)
Esempio n. 29
0
    def test_calc_impact_transf_pass(self):
        """ Test calc_impact method: apply all measures and insurance """

        hazard = Hazard('TC')
        hazard.read_mat(HAZ_TEST_MAT)

        entity = Entity()
        entity.read_mat(ENT_TEST_MAT)
        entity.exposures.rename(columns={'if_': 'if_TC'}, inplace=True)
        entity.measures._data['TC'] = entity.measures._data.pop('XX')
        for meas in entity.measures.get_measure('TC'):
            meas.haz_type = 'TC'
        meas = entity.measures.get_measure(name='Beach nourishment',
                                           haz_type='TC')
        meas.haz_type = 'TC'
        meas.hazard_inten_imp = (1, 0)
        meas.mdd_impact = (1, 0)
        meas.paa_impact = (1, 0)
        meas.risk_transf_attach = 5.0e8
        meas.risk_transf_cover = 1.0e9
        entity.check()

        imp, risk_transf = entity.measures.get_measure(
            name='Beach nourishment',
            haz_type='TC').calc_impact(entity.exposures, entity.impact_funcs,
                                       hazard)

        self.assertAlmostEqual(imp.aai_agg, 6.280804242609713e+09)
        self.assertAlmostEqual(imp.at_event[0], 0)
        self.assertAlmostEqual(imp.at_event[12], 8.648764833437817e+07)
        self.assertAlmostEqual(imp.at_event[41], 500000000)
        self.assertAlmostEqual(imp.at_event[11890], 6.498096646836635e+07)
        self.assertTrue(
            np.array_equal(imp.coord_exp[:, 0], entity.exposures.latitude))
        self.assertTrue(
            np.array_equal(imp.coord_exp[:, 1], entity.exposures.longitude))
        self.assertTrue(np.array_equal(imp.eai_exp, np.array([])))
        self.assertAlmostEqual(imp.tot_value, 6.570532945599105e+11)
        self.assertEqual(imp.unit, 'USD')
        self.assertEqual(imp.imp_mat, [])
        self.assertTrue(np.array_equal(imp.event_id, hazard.event_id))
        self.assertTrue(np.array_equal(imp.date, hazard.date))
        self.assertEqual(imp.event_name, hazard.event_name)
        self.assertEqual(imp.tag['exp'].file_name,
                         entity.exposures.tag.file_name)
        self.assertEqual(imp.tag['haz'].file_name, hazard.tag.file_name)
        self.assertEqual(imp.tag['if_set'].file_name,
                         entity.impact_funcs.tag.file_name)
        self.assertEqual(risk_transf, 2.3139691495470852e+08)
    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)