Exemplo n.º 1
0
    def test_calc_sensitivity_morris_pass(self):
        """Test compute sensitivity default"""

        exp_unc, impf_unc, _ = make_input_vars()
        haz = haz_dem()
        unc_calc = CalcImpact(exp_unc, impf_unc, haz)
        unc_data = unc_calc.make_sample(N=4, sampling_method='latin')
        unc_data = unc_calc.uncertainty(unc_data,
                                        calc_eai_exp=True,
                                        calc_at_event=True)

        unc_data = unc_calc.sensitivity(unc_data, sensitivity_method='morris')

        self.assertEqual(unc_data.sensitivity_method, 'morris')
        self.assertTupleEqual(unc_data.sensitivity_kwargs, tuple({}.items()))

        for name, attr in unc_data.__dict__.items():
            if 'sens_df' in name:
                np.testing.assert_array_equal(
                    attr.param.unique(), np.array(['x_exp', 'x_paa', 'x_mdd']))
                np.testing.assert_array_equal(
                    attr.si.unique(),
                    np.array(['mu', 'mu_star', 'sigma', 'mu_star_conf']))
                if 'eai' in name:
                    self.assertEqual(
                        attr.size,
                        len(unc_data.param_labels) * 4 *
                        (len(exp_unc.evaluate().gdf) + 3))
                elif 'at_event' in name:
                    self.assertEqual(
                        attr.size,
                        len(unc_data.param_labels) * 4 * (haz.size + 3))
                else:
                    self.assertEqual(len(attr), len(unc_data.param_labels) * 4)
Exemplo n.º 2
0
    def test_calc_uncertainty_pool_pass(self):
        """Test parallel compute the uncertainty distribution for an impact"""

        exp_unc, impf_unc, _ = make_input_vars()
        haz = haz_dem()
        unc_calc = CalcImpact(exp_unc, impf_unc, haz)
        unc_data = unc_calc.make_sample(N=2)

        pool = Pool(nodes=2)
        try:
            unc_data = unc_calc.uncertainty(unc_data,
                                            calc_eai_exp=False,
                                            calc_at_event=False,
                                            pool=pool)
        finally:
            pool.close()
            pool.join()
            pool.clear()

        self.assertEqual(unc_data.unit, exp_dem().value_unit)
        self.assertListEqual(unc_calc.rp, [5, 10, 20, 50, 100, 250])
        self.assertEqual(unc_calc.calc_eai_exp, False)
        self.assertEqual(unc_calc.calc_at_event, False)

        self.assertEqual(unc_data.aai_agg_unc_df.size, unc_data.n_samples)
        self.assertEqual(unc_data.tot_value_unc_df.size, unc_data.n_samples)

        self.assertEqual(unc_data.freq_curve_unc_df.size,
                         unc_data.n_samples * len(unc_calc.rp))
        self.assertTrue(unc_data.eai_exp_unc_df.empty)
        self.assertTrue(unc_data.at_event_unc_df.empty)
Exemplo n.º 3
0
    def test_make_sample_pass(self):
        """Test generate sample"""

        exp_unc, _, haz_unc = make_input_vars()
        impf = impf_dem()

        unc_calc = CalcImpact(exp_unc, impf, haz_unc)

        #default sampling saltelli
        unc_data = unc_calc.make_sample(
            N=2, sampling_kwargs={'calc_second_order': True})
        self.assertEqual(unc_data.n_samples,
                         2 * (2 * 2 + 2))  # N * (2 * D + 2)
        self.assertTrue(isinstance(unc_data.samples_df, pd.DataFrame))
        np.testing.assert_array_equal(unc_data.samples_df.columns.values,
                                      np.array(['x_exp', 'x_haz']))

        # #latin sampling
        unc_data = unc_calc.make_sample(N=1,
                                        sampling_method='latin',
                                        sampling_kwargs={'seed': 11245})
        self.assertEqual(unc_data.n_samples, 1)
        self.assertTrue(isinstance(unc_data.samples_df, pd.DataFrame))
        np.testing.assert_array_equal(unc_data.samples_df.columns.values,
                                      np.array(['x_exp', 'x_haz']))
Exemplo n.º 4
0
    def test_save_load_pass(self):
        """Test save and load output data"""

        exp_unc, impf_unc, _ = make_input_vars()
        haz = haz_dem()
        unc_calc = CalcImpact(exp_unc, impf_unc, haz)

        unc_data_save = unc_calc.make_sample(
            N=2, sampling_kwargs={'calc_second_order': True})
        filename = unc_data_save.to_hdf5()
        unc_data_load = UncOutput.from_hdf5(filename)
        for attr_save, val_save in unc_data_save.__dict__.items():
            if isinstance(val_save, pd.DataFrame):
                df_load = getattr(unc_data_load, attr_save)
                self.assertTrue(df_load.equals(val_save))
        self.assertEqual(unc_data_load.sampling_method,
                         unc_data_save.sampling_method)
        self.assertEqual(unc_data_load.sampling_kwargs,
                         unc_data_save.sampling_kwargs)
        filename.unlink()

        unc_data_save = unc_calc.uncertainty(unc_data_save,
                                             calc_eai_exp=True,
                                             calc_at_event=False)
        filename = unc_data_save.to_hdf5()
        unc_data_load = UncOutput.from_hdf5(filename)
        for attr_save, val_save in unc_data_save.__dict__.items():
            if isinstance(val_save, pd.DataFrame):
                df_load = getattr(unc_data_load, attr_save)
                self.assertTrue(df_load.equals(val_save))
        self.assertEqual(unc_data_load.sampling_method,
                         unc_data_save.sampling_method)
        self.assertEqual(unc_data_load.sampling_kwargs,
                         unc_data_save.sampling_kwargs)
        filename.unlink()

        unc_data_save = unc_calc.sensitivity(
            unc_data_save, sensitivity_kwargs={'calc_second_order': True})
        filename = unc_data_save.to_hdf5()
        unc_data_load = UncOutput.from_hdf5(filename)
        for attr_save, val_save in unc_data_save.__dict__.items():
            if isinstance(val_save, pd.DataFrame):
                df_load = getattr(unc_data_load, attr_save)
                self.assertTrue(df_load.equals(val_save))
        self.assertEqual(unc_data_load.sampling_method,
                         unc_data_save.sampling_method)
        self.assertEqual(unc_data_load.sampling_kwargs,
                         unc_data_save.sampling_kwargs)
        self.assertEqual(unc_data_load.sensitivity_method,
                         unc_data_save.sensitivity_method)
        self.assertEqual(unc_data_load.sensitivity_kwargs,
                         unc_data_save.sensitivity_kwargs)
        filename.unlink()
Exemplo n.º 5
0
    def test_calc_sensitivity_pass(self):
        """Test compute sensitivity default"""

        exp_unc, impf_unc, _ = make_input_vars()
        haz = haz_dem()
        unc_calc = CalcImpact(exp_unc, impf_unc, haz)
        unc_data = unc_calc.make_sample(
            N=4, sampling_kwargs={'calc_second_order': True})
        unc_data = unc_calc.uncertainty(unc_data,
                                        calc_eai_exp=False,
                                        calc_at_event=False)

        unc_data = unc_calc.sensitivity(
            unc_data, sensitivity_kwargs={'calc_second_order': True})

        self.assertEqual(unc_data.sensitivity_method, 'sobol')
        self.assertTupleEqual(unc_data.sensitivity_kwargs,
                              tuple({'calc_second_order': 'True'}.items()))

        for name, attr in unc_data.__dict__.items():
            if 'sens_df' in name:
                if 'eai' in name:
                    self.assertTrue(attr.empty)
                elif 'at_event' in name:
                    self.assertTrue(attr.empty)
                else:
                    np.testing.assert_array_equal(
                        attr.param.unique(),
                        np.array(['x_exp', 'x_paa', 'x_mdd']))

                    np.testing.assert_array_equal(
                        attr.si.unique(),
                        np.array([
                            'S1', 'S1_conf', 'ST', 'ST_conf', 'S2', 'S2_conf'
                        ]))

                    self.assertEqual(len(attr),
                                     len(unc_data.param_labels) * (4 + 3 + 3))
Exemplo n.º 6
0
    def test_init_pass(self):
        """Test initiliazation uncertainty"""

        exp_iv, impf_iv, haz_iv = make_input_vars()
        unc_calc = CalcImpact(exp_iv, impf_iv, haz_iv)

        self.assertTupleEqual(
            unc_calc.input_var_names,
            ('exp_input_var', 'impf_input_var', 'haz_input_var'))
        self.assertTupleEqual(
            unc_calc.metric_names,
            ('aai_agg', 'freq_curve', 'at_event', 'eai_exp', 'tot_value'))
        self.assertEqual(unc_calc.value_unit, exp_iv.evaluate().value_unit)
        self.assertTrue(
            unc_calc.exp_input_var.evaluate(x_exp=1).gdf.equals(
                exp_dem(1).gdf))
        impf1 = unc_calc.impf_input_var.evaluate(x_paa=1,
                                                 x_mdd=1).get_func()['TC'][1]
        impf2 = impf_dem(1, 1).get_func()['TC'][1]
        np.testing.assert_array_almost_equal(impf1.calc_mdr(impf1.intensity),
                                             impf2.calc_mdr(impf2.intensity))
        haz1 = unc_calc.haz_input_var.evaluate(x_haz=1)
        haz2 = haz_dem(1)
        self.assertListEqual(haz1.event_name, haz2.event_name)