def test_initial_parameter_outside(self): MP = ModelParameters() MP.add_model_parameter('aa', 0, minimum = -10., maximum = 10) MP.add_model_parameter('bb', 11, minimum = -10., maximum = 10) MP._openparameterstructure(nbatch=1) with self.assertRaises(SystemExit, msg = 'Initial value outside of bounds.'): MP._check_initial_values_wrt_parameter_limits()
def test_not_all_adapt(self): MP = ModelParameters() parind, adapt, no_adapt = MP.setup_adaptation_indices( adapt=np.array([False, True, True]), parind=np.array([True, True, True])) self.assertTrue(np.array_equal(parind, np.array([0, 1, 2])), msg=str('Expect arrays to match: {} neq {}'.format( parind, np.array([0, 1, 2])))) self.assertTrue(np.array_equal(adapt, np.array([1, 2])), msg=str('Expect arrays to match: {} neq {}'.format( adapt, np.array([1, 2])))) self.assertTrue(np.array_equal(no_adapt, np.array([True, False, False])), msg=str('Expect arrays to match: {} neq {}'.format( no_adapt, np.array([True, False, False])))) parind, adapt, no_adapt = MP.setup_adaptation_indices( adapt=np.array([False, True, False]), parind=np.array([True, True, True])) self.assertTrue(np.array_equal(parind, np.array([0, 1, 2])), msg=str('Expect arrays to match: {} neq {}'.format( parind, np.array([0, 1, 2])))) self.assertTrue(np.array_equal(adapt, np.array([1])), msg=str('Expect arrays to match: {} neq {}'.format( adapt, np.array([1])))) self.assertTrue(np.array_equal(no_adapt, np.array([True, False, True])), msg=str('Expect arrays to match: {} neq {}'.format( no_adapt, np.array([True, False, True]))))
def test_setup_prior_sigma(self): MP = ModelParameters() self.assertEqual(MP.setup_prior_sigma(sigma=np.array([0.1])), np.array([0.1]), msg='Expect = sigma') self.assertEqual(MP.setup_prior_sigma(sigma=0.), np.inf, msg='Expect = inf')
def test_setup_prior_mu(self): MP = ModelParameters() self.assertEqual(MP.setup_prior_mu(mu=np.array([0.1]), value=np.array([0.3])), np.array([0.1]), msg='Expect = mu') self.assertEqual(MP.setup_prior_mu(mu=np.nan, value=np.array([0.3])), np.array([0.3]), msg='Expect = value')
def standard_check(self, theta0=1.0, name='$p_{0}$'): MP = ModelParameters() MP.add_model_parameter(name=name, theta0=theta0) self.assertEqual(MP.parameters[0]['theta0'], theta0) self.assertEqual(MP.parameters[0]['name'], name) self.assertEqual(MP.parameters[0]['minimum'], -np.inf) self.assertEqual(MP.parameters[0]['maximum'], np.inf) self.assertEqual(MP.parameters[0]['prior_mu'], np.zeros([1])) self.assertEqual(MP.parameters[0]['prior_sigma'], np.inf) self.assertEqual(MP.parameters[0]['sample'], 1) self.assertEqual(MP.parameters[0]['local'], 0)
def test_raises_error(self): MP = ModelParameters() MP.add_model_parameter('aa', 0, minimum=-10., maximum=10) MP.add_model_parameter('bb', 5, minimum=100., maximum=10) MP._openparameterstructure(nbatch=1) with self.assertRaises(SystemExit, msg='Min > Max'): MP._check_parameter_limits()
def test_scan_for_local_variables_with_sample_false(self): MP = ModelParameters() MP.add_model_parameter(name='m', theta0=0.2, local=0, sample=False) MP.add_model_parameter(name='b', theta0=-0.5, local=1) local = MP.scan_for_local_variables(nbatch=2, parameters=MP.parameters) self.assertTrue(np.array_equal(local, np.array([1, 2])), msg=str('Expect arrays to match: {} neq {}'.format( local, np.array([1, 2]))))
def test_setup_adapting(self): MP = ModelParameters() self.assertTrue(MP.setup_adapting(adapt=True, sample=True), msg='Expect True') self.assertFalse(MP.setup_adapting(adapt=False, sample=True), msg='Expect False') self.assertFalse(MP.setup_adapting(adapt=True, sample=False), msg='Expect False') self.assertFalse(MP.setup_adapting(adapt=False, sample=False), msg='Expect False')
def test_get_parameter_features(self): MP = ModelParameters() MP.add_model_parameter(name='a1', theta0=0.1, minimum=0, maximum=1) MP.add_model_parameter(name='a2', theta0=0.2, minimum=0, maximum=1) MP.add_model_parameter(name='a3', theta0=0.3, minimum=0, maximum=1) npar, low_lim, upp_lim = get_parameter_features(MP.parameters) self.assertEqual(npar, 3, msg='Expect to find 3 parameters') self.assertTrue(np.array_equal(low_lim, np.zeros([3])), msg=str('Expect low_lim all zero: {} neq {}'.format( low_lim, np.zeros([3])))) self.assertTrue(np.array_equal(upp_lim, np.ones([3])), msg=str('Expect upp_lim all one: {} neq {}'.format( upp_lim, np.ones([3]))))
def test_get_parameter_features_with_inf_bounds(self): MP = ModelParameters() MP.add_model_parameter(name='a1', theta0=0.1, minimum=-np.inf, maximum=1) MP.add_model_parameter(name='a2', theta0=0.2, minimum=0, maximum=1) MP.add_model_parameter(name='a3', theta0=0.3, minimum=0, maximum=np.inf) npar, low_lim, upp_lim = get_parameter_features(MP.parameters) expect_low_lim = np.array([0.1 - 100 * 0.1, 0, 0]) expect_upp_lim = np.array([1, 1, 0.3 + 100 * 0.3]) self.assertEqual(npar, 3, msg='Expect to find 3 parameters') self.assertTrue(np.array_equal(low_lim, expect_low_lim), msg=str('Expect low_lim finite: {} neq {}'.format( low_lim, expect_low_lim))) self.assertTrue(np.array_equal(upp_lim, expect_upp_lim), msg=str('Expect upp_lim finite: {} neq {}'.format( upp_lim, expect_upp_lim)))
def test_results_to_params(self): MP = ModelParameters() MP.add_model_parameter('aa', 0) MP._openparameterstructure(nbatch=1) # define minimal results dictionary results = { 'parind': MP._parind, 'names': MP._names, 'local': MP._local, 'theta': [1.2] } # initialize default options SO = SimulationOptions() SO.define_simulation_options(verbosity=0) MP.display_parameter_settings(verbosity=SO.verbosity, no_adapt=MP._no_adapt) MP._results_to_params(results, 1) MP._openparameterstructure(nbatch=1) MP.display_parameter_settings(verbosity=SO.verbosity, no_adapt=MP._no_adapt) self.assertEqual(MP.parameters[0]['theta0'], results['theta'][0])
def test_initial_parameter_okay(self): MP = ModelParameters() MP.add_model_parameter('aa', 0, minimum=-10., maximum=10) MP.add_model_parameter('bb', 9, minimum=-10., maximum=10) MP._openparameterstructure(nbatch=1) self.assertTrue(MP._check_initial_values_wrt_parameter_limits())
def test_parameter_display_set_2(self): MP = ModelParameters() MP.add_model_parameter(name='m', theta0=2., minimum=-10, maximum=np.inf, sample=True) MP.add_model_parameter(name='b', theta0=-5., minimum=-10, maximum=100, sample=False) MP.add_model_parameter(name='b2', theta0=-5.3e6, minimum=-1e7, maximum=1e6, sample=True) MP._openparameterstructure(nbatch=1) MP.display_parameter_settings(verbosity=None, no_adapt=None)
'theta': [1.2] } # initialize default options SO = SimulationOptions() SO.define_simulation_options(verbosity=0) MP.display_parameter_settings(verbosity=SO.verbosity, no_adapt=MP._no_adapt) MP._results_to_params(results, 1) MP._openparameterstructure(nbatch=1) MP.display_parameter_settings(verbosity=SO.verbosity, no_adapt=MP._no_adapt) self.assertEqual(MP.parameters[0]['theta0'], results['theta'][0]) # -------------------------- MP = ModelParameters() class LessThanOrEqualToZero(unittest.TestCase): def test_x_lt_0(self): self.assertTrue(less_than_or_equal_to_zero(x=-1)) def test_x_eq_0(self): self.assertTrue(less_than_or_equal_to_zero(x=0)) def test_x_gt_0(self): self.assertFalse(less_than_or_equal_to_zero(x=1)) # -------------------------- class ReplaceListElements(unittest.TestCase):