def test_setup_number_of_weights(self): "Test setup weight with number of weights" for n in range(1, 20): p = IMP.Particle(self.m) w = Weight.setup_particle(p, n) self.assertTrue(Weight.get_is_setup(p)) self.assertEqual(w.get_number_of_weights(), n) for k in range(n): self.assertAlmostEqual(w.get_weight(k), 1.0 / n, delta=1e-6)
def test_setup_weights(self): "Test setup weight with initial values" for n in range(1, 20): p = IMP.Particle(self.m) ws = np.random.uniform(size=n) w = Weight.setup_particle(p, ws) self.assertTrue(Weight.get_is_setup(p)) self.assertEqual(w.get_number_of_weights(), n) self.assertSequenceAlmostEqual( w.get_weights(), IMP.algebra.get_projected( w.get_unit_simplex(), IMP.algebra.VectorKD(ws) ), )
def test_set_weights_zero(self): p = IMP.Particle(self.m) n = 5 w = Weight.setup_particle(p, n) ws = np.zeros(n) w.set_weights(ws) for k in range(n): self.assertAlmostEqual(w.get_weight(k), 1.0 / n, delta=1e-6)
def test_get_unit_simplex(self): for n in range(1, 20): p = IMP.Particle(self.m) ws = np.random.uniform(size=n) w = Weight.setup_particle(p, ws) s = w.get_unit_simplex() self.assertIsInstance(s, IMP.algebra.UnitSimplexKD) self.assertEqual(s.get_dimension(), n)
def test_change_nweights_for_optimized_raises_error(self): for n in range(1, 20): w = Weight.setup_particle(IMP.Particle(self.m), n) self.assertFalse(w.get_weights_are_optimized()) w.set_number_of_weights(n + 1) w.set_number_of_weights(n) w.set_weights_are_optimized(True) self.assertRaisesUsageException(w.set_number_of_weights, n + 1)
def test_Setup_iw(self): "Test weight_initial_values" w = Weight.setup_particle(IMP.Particle(self.m)) w.add_weight() for n in range(19): for k in range(n + 1): self.assertEqual(w.get_weight(k), 1.0 / (n + 1)) w.add_weight()
def test_setup_empty_add_weight(self): "Test setup weight as empty and add weights" w = Weight.setup_particle(IMP.Particle(self.m)) for n in range(1, 20): w.add_weight() self.assertEqual(w.get_number_of_weights(), n) self.assertSequenceAlmostEqual( list(w.get_weights()), [1] + [0] * (n - 1) )
def test_Setup_number(self): "Test weights_initial_values" w = Weight.setup_particle(IMP.Particle(self.m)) w.add_weight() for n in range(19): ws = w.get_weights() self.assertEqual(len(ws), n + 1) for k in range(n + 1): self.assertEqual(ws[k], 1.0 / (n + 1)) w.add_weight()
def test_Setup_size(self): "Test weights_size" w = Weight.setup_particle(IMP.Particle(self.m)) w.add_weight() for n in range(19): ws = w.get_weights() ns = w.get_number_of_states() self.assertEqual(n + 1, ns) self.assertEqual(len(ws), ns) w.add_weight()
def test_add_weight(self): "Test add_weight" w = Weight.setup_particle(IMP.Particle(self.m), 1) for n in range(2, 20): ws = list(w.get_weights()) wi = np.random.uniform() w.add_weight(wi) ws.append(wi) self.assertEqual(w.get_number_of_weights(), n) ws = list(IMP.algebra.get_projected(w.get_unit_simplex(), ws)) self.assertSequenceAlmostEqual(list(w.get_weights()), ws)
def test_add_to_weights_derivatives(self): for n in range(1, 20): w = Weight.setup_particle(IMP.Particle(self.m), n) ws = np.random.uniform(size=n) ws /= np.sum(ws) w.set_weights(ws) dw = np.random.normal(size=n) w.add_to_weights_derivatives(dw, IMP.DerivativeAccumulator()) dw2 = w.get_weights_derivatives() dw2 = [dw2[i] for i in range(n)] self.assertSequenceAlmostEqual(list(dw), dw2, delta=1e-6)
def test_set_weights(self): for n in range(1, 20): p = IMP.Particle(self.m) w = Weight.setup_particle(p, n) ws = np.random.uniform(size=n) w.set_weights(ws) self.assertSequenceAlmostEqual( w.get_weights(), IMP.algebra.get_projected( w.get_unit_simplex(), IMP.algebra.VectorKD(ws) ), )
def test_assign_values(self): "Test weights_set" w = Weight.setup_particle(IMP.Particle(self.m)) w.add_weight() ws = [1.0] for n in range(19): w.set_weights(ws) ws2 = w.get_weights() for k in range(n + 1): self.assertEqual(ws[k], ws2[k]) ws.append(1.0) w.add_weight()
def test_add_to_weight_derivative(self): for n in range(1, 20): w = Weight.setup_particle(IMP.Particle(self.m), n) ws = np.random.uniform(size=n) ws /= np.sum(ws) w.set_weights(ws) for k in range(0, n): dwk = np.random.normal() w.add_to_weight_derivative(k, dwk, IMP.DerivativeAccumulator()) self.assertAlmostEqual( w.get_weight_derivative(k), dwk, delta=1e-6 )
def test_set_optimized(self): "Test weights_optimized" w = Weight.setup_particle(IMP.Particle(self.m)) w.add_weight() for n in range(19): w.set_weights_are_optimized(True) for k in range(n + 1): b = w.get_is_optimized(w.get_weight_key(k)) self.assertEqual(b, True) w.set_weights_are_optimized(False) for k in range(n + 1): b = w.get_is_optimized(w.get_weight_key(k)) self.assertEqual(b, False) w.add_weight()
def setUp(self): IMP.test.TestCase.setUp(self) # IMP.set_log_level(IMP.MEMORY) IMP.set_log_level(0) self.m = IMP.kernel.Model() self.w = Weight.setup_particle(IMP.kernel.Particle(self.m)) self.w.set_weights_are_optimized(True) self.w.add_weight() self.w.add_weight() self.wm = WeightMover(self.w, 0.1) self.mc = IMP.core.MonteCarlo(self.m) self.mc.set_return_best(False) self.mc.set_kt(1.0) self.mc.add_mover(self.wm)
def test_set_nweights_lazy(self): for n in range(1, 20): w = Weight.setup_particle(IMP.Particle(self.m), n) ws = IMP.algebra.get_random_vector_on(w.get_unit_simplex()) w.set_weights(ws) w.set_number_of_weights_lazy(n + 1) self.assertEqual(w.get_number_of_weights(), n + 1) self.assertSequenceAlmostEqual( list(w.get_weights()), list(ws) + [0] ) w.set_number_of_weights_lazy(n) self.assertEqual(w.get_number_of_weights(), n) self.assertSequenceAlmostEqual(list(w.get_weights()), list(ws))
def test_set_optimized(self): "Test weights_optimized" for n in range(1, 20): w = Weight.setup_particle(IMP.Particle(self.m), n) self.assertFalse(w.get_weights_are_optimized()) w.set_weights_are_optimized(True) for k in range(n): b = w.get_is_optimized(w.get_weight_key(k)) self.assertEqual(b, True) self.assertTrue(w.get_weights_are_optimized()) w.set_weights_are_optimized(False) for k in range(n): b = w.get_is_optimized(w.get_weight_key(k)) self.assertEqual(b, False) self.assertFalse(w.get_weights_are_optimized())
def test_set_weight_lazy(self): for n in range(1, 20): p = IMP.Particle(self.m) w = Weight.setup_particle(p, n) ws = np.random.uniform(size=n) for k in range(n): w.set_weight_lazy(k, ws[k]) self.assertAlmostEqual(w.get_weight(k), ws[k], delta=1e-6) self.m.update() self.assertSequenceAlmostEqual( w.get_weights(), IMP.algebra.get_projected( w.get_unit_simplex(), IMP.algebra.VectorKD(ws) ), )
def test_get_weight_keys(self): for n in range(1, 20): w = Weight.setup_particle(IMP.Particle(self.m), n) wks = [w.get_weight_key(i) for i in range(n)] self.assertSequenceEqual(wks, list(w.get_weight_keys()))
def setUp(self): IMP.test.TestCase.setUp(self) # IMP.set_log_level(IMP.MEMORY) IMP.set_log_level(0) self.m = IMP.Model() self.w = Weight.setup_particle(IMP.Particle(self.m))