def test_make_sample_pass(self):
        """Test generate sample"""

        exp_unc, _, haz_unc = make_imp_uncs()

        unc = Uncertainty({'exp': exp_unc, 'haz': haz_unc})

        #default sampling saltelli
        unc.make_sample(N=1, sampling_kwargs={'calc_second_order': True})
        self.assertEqual(unc.n_samples, 1 * (2 * 2 + 2))  # N * (2 * D + 2)
        self.assertTrue(isinstance(unc.samples_df, pd.DataFrame))
        self.assertTrue(
            np.allclose(unc.samples_df['x_exp'],
                        np.array([
                            1.239453, 1.837109, 1.239453, 1.239453, 1.837109,
                            1.837109
                        ]),
                        rtol=1e-05))
        self.assertListEqual(list(unc.samples_df['x_haz']),
                             [0.0, 0.0, 1.0, 1.0, 0.0, 1.0])

        #latin sampling
        unc.make_sample(N=1,
                        sampling_method='latin',
                        sampling_kwargs={'seed': 11245})
        self.assertEqual(unc.n_samples, 1)
        self.assertTrue(isinstance(unc.samples_df, pd.DataFrame))
        self.assertTrue(
            np.allclose(unc.samples_df['x_exp'],
                        np.array([2.58309]),
                        rtol=1e-05))
        self.assertListEqual(list(unc.samples_df['x_haz']), [2.0])
Пример #2
0
    def test_plot_sample_pass(self):

        exp_unc, _, haz_unc = make_imp_uncs()

        unc = Uncertainty({'exp': exp_unc, 'haz': haz_unc})

        unc.make_sample(N=1)
        unc.plot_sample()
        plt.close()
    def test_save_pass(self):
        """Test save samples"""

        exp_unc, impf_unc, haz_unc = make_imp_uncs()

        unc = Uncertainty({'exp': exp_unc, 'impf': impf_unc, 'haz': haz_unc})
        unc.make_sample(1)
        filename = unc.save_samples_df()

        unc_imp = UncImpact(exp_unc, impf_unc, haz_unc)
        unc_imp.load_samples_df(filename)

        unc_imp.calc_distribution()
        unc_imp.calc_sensitivity()
Пример #4
0
    def test_est_comp_time_pass(self):

        exp_unc, _, haz_unc = make_imp_uncs()

        unc = Uncertainty({'exp': exp_unc, 'haz': haz_unc})

        unc.make_sample(N=1, sampling_kwargs={'calc_second_order': False})
        est = unc.est_comp_time(0.12345)
        self.assertEqual(est, 1 * (2 + 2) * 0.123)  # N * (D + 2)

        pool = Pool(nodes=4)
        est = unc.est_comp_time(0.12345, pool)
        self.assertEqual(est, 1 * (2 + 2) * 0.123 / 4)  # N * (D + 2)
        pool.close()
        pool.join()
        pool.clear()