def test_make_floating(self): with self.assertRaises(ValueError): self.fixed_param.make_floating() with self.assertRaises(ValueError): self.fixed_param.make_floating(valmin=self.floating_param_valmin) # The current value of fixed_param is outside the valmin and valmax # range of the floating_param. This should raise an exception when no # new initial value is specified. with self.assertRaises(ValueError): self.fixed_param.make_floating(valmin=self.floating_param_valmin, valmax=self.floating_param_valmax) self.fixed_param.make_floating(initial=self.floating_param_initial, valmin=self.floating_param_valmin, valmax=self.floating_param_valmax) self.assertTrue( isAlmostEqual(self.fixed_param.initial, self.floating_param_initial)) self.assertTrue( isAlmostEqual(self.fixed_param.value, self.floating_param_initial)) self.assertTrue( isAlmostEqual(self.fixed_param.valmin, self.floating_param_valmin)) self.assertTrue( isAlmostEqual(self.fixed_param.valmax, self.floating_param_valmax))
def test_update_fixed_param_value_cache(self): self.assertAlmostEqual(self.paramset.params[0].value, 2.3) self.fixed_param.change_fixed_value(3.1) self.assertAlmostEqual(self.paramset.params[0].value, 3.1) self.assertTrue(isAlmostEqual(self.paramset.fixed_param_values, [2.3])) self.paramset.update_fixed_param_value_cache() self.assertTrue(isAlmostEqual(self.paramset.fixed_param_values, [3.1]))
def test_make_fixed(self): self.floating_param.make_fixed(self.fixed_param_initial) self.assertTrue( isAlmostEqual(self.floating_param.initial, self.fixed_param_initial)) self.assertTrue( isAlmostEqual(self.floating_param.value, self.fixed_param_initial))
def test_make_params_fixed(self): # Already fixed parameters cannot be fixed. with self.assertRaises(ValueError): self.paramset.make_params_fixed({'p0': 42}) # Fix the floating parameter outside its current range. self.paramset.make_params_fixed({'p1': 0.4}) self.assertTrue(self.paramset.has_fixed_param('p1')) self.assertEqual(self.paramset.n_fixed_params, 2) self.assertEqual(self.paramset.n_floating_params, 0) self.test_params() values = self.paramset.fixed_param_values self.assertTrue(isAlmostEqual(values, [2.3, 0.4])) self.assertEqual(self.paramset.params[1].valmin, None) self.assertEqual(self.paramset.params[1].valmax, None) self.setUp() # Fix the floating parameter to its current value. self.paramset.make_params_fixed({'p1': None}) self.assertTrue(self.paramset.has_fixed_param('p1')) self.assertEqual(self.paramset.n_fixed_params, 2) self.assertEqual(self.paramset.n_floating_params, 0) self.test_params() values = self.paramset.fixed_param_values self.assertTrue(isAlmostEqual(values, [2.3, 1.1])) self.assertTrue(isAlmostEqual(self.paramset.params[1].valmin, 0.5)) self.assertTrue(isAlmostEqual(self.paramset.params[1].valmax, 1.6))
def test_parameter_permutation_dict_list(self): perm_dict_list = self.paramgridset.parameter_permutation_dict_list self.assertTrue( isAlmostEqual([d['gamma'] for d in perm_dict_list], np.repeat(np.array(GAMMA_GRID), len(ECUT_GRID)))) self.assertTrue( isAlmostEqual([d['Ecut'] for d in perm_dict_list], list(ECUT_GRID) * len(GAMMA_GRID)))
def test_change_fixed_value(self): with self.assertRaises(ValueError): self.floating_param.change_fixed_value(self.fixed_param_initial) self.fixed_param.change_fixed_value(self.floating_param_initial) self.assertTrue( isAlmostEqual(self.fixed_param.initial, self.floating_param_initial)) self.assertTrue( isAlmostEqual(self.fixed_param.value, self.floating_param_initial))
def test_make_params_floating(self): # Already floating parameters cannot be made floating. with self.assertRaises(ValueError): self.paramset.make_params_floating({'p1': None}) # Make the fixed parameter floating. with self.assertRaises(ValueError): self.paramset.make_params_floating({'p0': None}) with self.assertRaises(ValueError): self.paramset.make_params_floating({'p0': 1.2}) self.paramset.make_params_floating({'p0': (1.2, 1.0, 1.3)}) self.assertTrue(self.paramset.has_floating_param('p0')) self.assertTrue(isAlmostEqual(self.paramset.params[0].initial, 1.2)) self.assertTrue(isAlmostEqual(self.paramset.params[0].valmin, 1.0)) self.assertTrue(isAlmostEqual(self.paramset.params[0].valmax, 1.3))
def test_value(self): self.assertTrue( isAlmostEqual(self.fixed_param.value, self.fixed_param_initial)) self.assertTrue( isAlmostEqual(self.floating_param.value, self.floating_param_initial)) # Try to change the value of a fixed parameter. with self.assertRaises(ValueError): self.fixed_param.value = self.floating_param_initial # Try to set the value of a floating parameter to a value outside its # value range. with self.assertRaises(ValueError): self.floating_param.value = self.fixed_param_initial
def test_as_linear_grid(self): grid_delta = 0.1 with self.assertRaises(ValueError): self.fixed_param.as_linear_grid(grid_delta) param_grid = self.floating_param.as_linear_grid(grid_delta) self.assertTrue( np.all(isAlmostEqual(param_grid.grid, self.floating_param_grid)))
def test_round_to_upper_grid_point(self): # Test a value between two grid points. x = 2.4 gp = self.paramgrid_gamma1.round_to_upper_grid_point(x) self.assertTrue(isAlmostEqual(gp, 2.5)) # Test a value at a grid point. x = 2. gp = self.paramgrid_gamma1.round_to_upper_grid_point(x) self.assertTrue(isAlmostEqual(gp, 2.5)) x = 1.6 gp = self.paramgrid_gamma2.round_to_upper_grid_point(x) self.assertTrue(isAlmostEqual(gp, 1.7)) x = [1.05, 1.15, 1.25, 1.35] gp = self.paramgrid_gamma3.round_to_upper_grid_point(x) np.testing.assert_almost_equal(gp, [1.15, 1.25, 1.35, 1.45])
def test_round_to_nearest_grid_point(self): # Test values outside the grid range. x = 1.49999999999 with self.assertRaises(ValueError): gp = self.paramgrid_gamma1.round_to_nearest_grid_point(x) x = 3.50000000001 with self.assertRaises(ValueError): gp = self.paramgrid_gamma1.round_to_nearest_grid_point(x) # Test a value between two grid points. x = [2.1, 2.4, 2.2, 2.3] gp = self.paramgrid_gamma1.round_to_nearest_grid_point(x) np.testing.assert_almost_equal(gp, [2.0, 2.5, 2., 2.5]) x = [1.051, 1.14] gp = self.paramgrid_gamma3.round_to_nearest_grid_point(x) self.assertTrue(isAlmostEqual(gp, [1.05, 1.15])) # Test a value on a grid point. x = [1.05, 1.35] gp = self.paramgrid_gamma3.round_to_nearest_grid_point(x) self.assertTrue(isAlmostEqual(gp, [1.05, 1.35]))
def test_valmin(self): self.assertEqual(self.fixed_param.valmin, None) self.assertTrue( isAlmostEqual(self.floating_param.valmin, self.floating_param_valmin))
def test_initial(self): self.assertTrue( isAlmostEqual(self.fixed_param.initial, self.fixed_param_initial)) self.assertTrue( isAlmostEqual(self.floating_param.initial, self.floating_param_initial))
def test_fixed_param_values(self): values = self.paramset.fixed_param_values self.assertTrue(isAlmostEqual(values, [2.3]))
def test_delta(self): self.assertTrue(isAlmostEqual(self.paramgrid_gamma1.delta, 0.5)) self.assertTrue(isAlmostEqual(self.paramgrid_gamma2.delta, 0.1)) self.assertTrue(isAlmostEqual(self.paramgrid_gamma3.delta, 0.1))
def test_from_BinningDefinition(self): binning = BinningDefinition(name='gamma', binedges=GAMMA_GRID) param_grid = ParameterGrid.from_BinningDefinition(binning) self.assertEqual(param_grid.name, binning.name) self.assertTrue(isAlmostEqual(param_grid.grid, GAMMA_GRID))
def test_floating_param_initials(self): initials = self.paramset.floating_param_initials self.assertTrue(isAlmostEqual(initials, [1.1]))
def test_floating_param_bounds(self): bounds = self.paramset.floating_param_bounds self.assertTrue(isAlmostEqual(bounds[0], [0.5, 1.6]))