def create_particles(self): x = Nuisance.setup_particle(IMP.Particle(self.m), 2.0) mu = Nuisance.setup_particle(IMP.Particle(self.m), 1.0) sigma = Scale.setup_particle(IMP.Particle(self.m), 2.0) nu = Scale.setup_particle(IMP.Particle(self.m), 3.0) all_nuis = [x, mu, sigma, nu] return all_nuis
def test_nuisance_down(self): n=IMP.kernel.Particle(self.m) Nuisance.setup_particle(n,1.0) Nuisance(n).set_lower(0.5) Nuisance(n).set_upper(1.5) n.set_value(Nuisance.get_nuisance_key(), 0.1) self.m.evaluate(False) self.assertAlmostEqual(n.get_value(Nuisance.get_nuisance_key()), 0.5, delta=1e-7) self.assertAlmostEqual(self.rs.values[0],0.5)
def setUp(self): IMP.test.TestCase.setUp(self) # IMP.base.set_log_level(IMP.MEMORY) IMP.base.set_log_level(0) self.m = IMP.kernel.Model() self.sigma = Scale.setup_particle(IMP.kernel.Particle(self.m), 2.0) self.mu = Nuisance.setup_particle(IMP.kernel.Particle(self.m), 1.0) self.x = Nuisance.setup_particle(IMP.kernel.Particle(self.m), 2.0) self.locations = [self.x, self.mu] self.all = self.locations + [self.sigma] self.DA = IMP.DerivativeAccumulator()
def setUp(self): IMP.test.TestCase.setUp(self) #IMP.base.set_log_level(IMP.MEMORY) IMP.base.set_log_level(0) self.m = IMP.Model() self.sigma = Scale.setup_particle(IMP.Particle(self.m), 2.0) self.mu = Nuisance.setup_particle(IMP.Particle(self.m), 1.0) self.x = Nuisance.setup_particle(IMP.Particle(self.m), 2.0) self.locations=[self.x, self.mu] self.all = self.locations+[self.sigma] self.DA = IMP.DerivativeAccumulator()
def test_nuisance_down(self): n=IMP.Particle(self.m) Nuisance.setup_particle(n,1.0) Nuisance(n).set_lower(0.5) Nuisance(n).set_upper(1.5) self.m.add_score_state(IMP.core.SingletonConstraint( IMP.isd.NuisanceRangeModifier(), None, n)) n.set_value(Nuisance.get_nuisance_key(), 0.1) self.m.evaluate(False) self.assertAlmostEqual(n.get_value(Nuisance.get_nuisance_key()), 0.5, delta=1e-7) self.assertAlmostEqual(self.rs.values[0],0.5)
def test_Switching(self): "Test that a Switching can be converted to a Nuisance" n = Switching.setup_particle(IMP.Particle(self.m)) n.set_lower(-10) n.set_upper(10) self.assertTrue(Nuisance.get_is_setup(n.get_particle())) self.assertFalse(Switching.get_is_setup(n.get_particle()))
def test_Setup2(self): si = Nuisance.setup_particle(IMP.Particle(self.m), 2.0) si.set_lower(0.1) si.set_upper(10) self.assertAlmostEqual(float(si.get_nuisance()),2.0, delta=1e-6) self.assertAlmostEqual(float(si.get_lower()),0.1, delta=1e-6) self.assertAlmostEqual(float(si.get_upper()),10.0, delta=1e-6)
def test_Switching(self): "test that a Switching can be converted to a Nuisance" n=Switching.setup_particle(IMP.Particle(self.m)) n.set_lower(-10) n.set_upper(10) self.assertTrue(Nuisance.particle_is_instance(n.get_particle())) self.assertFalse(Switching.particle_is_instance(n.get_particle()))
def test_GetSet_Particle(self): "Test nuisance get and set (border check) with particles" nuisance = Nuisance.setup_particle(IMP.Particle(self.m), 50.0) lower = Nuisance.setup_particle(IMP.Particle(self.m), 10.0) upper = Nuisance.setup_particle(IMP.Particle(self.m), 80.0) nuisance.set_lower(lower) nuisance.set_upper(upper) for si in range(1, 100): nuisance.set_nuisance(si) if si < 10: est = 10 elif si > 80: est = 80 else: est = si self.assertAlmostEqual(nuisance.get_nuisance(), est, delta=1e-6)
def unprotected_evaluate(self, accum): e = 0 self.values = [ p.get_value(Nuisance.get_nuisance_key()) for p in self.get_model().get_particles() ] return e
def test_nuisance_get_has_lower(self): p = IMP.kernel.Particle(self.m) n = Nuisance.setup_particle(p, 1.0) self.assertFalse(n.get_has_lower()) n.set_lower(0.5) self.assertTrue(n.get_has_lower()) n.remove_lower() self.assertFalse(n.get_has_lower())
def test_Setup2(self): "Test nuisance parameter setup with lower/upper" si = Nuisance.setup_particle(IMP.Particle(self.m), 2.0) si.set_lower(0.1) si.set_upper(10) self.assertAlmostEqual(float(si.get_nuisance()), 2.0, delta=1e-6) self.assertAlmostEqual(float(si.get_lower()), 0.1, delta=1e-6) self.assertAlmostEqual(float(si.get_upper()), 10.0, delta=1e-6)
def test_nuisance_get_has_lower(self): p=IMP.kernel.Particle(self.m) n=Nuisance.setup_particle(p,1.0) self.assertFalse(n.get_has_lower()) n.set_lower(0.5) self.assertTrue(n.get_has_lower()) n.remove_lower() self.assertFalse(n.get_has_lower())
def test_Setup2(self): "Test nuisance parameter setup with lower/upper" si = Nuisance.setup_particle(IMP.kernel.Particle(self.m), 2.0) si.set_lower(0.1) si.set_upper(10) self.assertAlmostEqual(float(si.get_nuisance()),2.0, delta=1e-6) self.assertAlmostEqual(float(si.get_lower()),0.1, delta=1e-6) self.assertAlmostEqual(float(si.get_upper()),10.0, delta=1e-6)
def testdsigma(self): "Test LognormalRestraint(3) sigma derivative" gr = LognormalRestraint(*self.all) for i in range(100): map(self.change_value, self.all) gr.evaluate(True) self.assertAlmostEqual( Nuisance(self.sigma).get_nuisance_derivative(), self.deriv_sigma(*self.all))
def testdmu(self): "Test GaussianRestraint(3) mu derivative" gr = GaussianRestraint(*self.all) for i in range(100): map(self.change_value, self.all) map(self.change_sign, self.locations) gr.evaluate(True) self.assertAlmostEqual( Nuisance(self.mu).get_nuisance_derivative(), self.deriv_mu(*self.all))
def testdmu(self): "Test LognormalRestraint(3) mu derivative" gr = LognormalRestraint(*self.all) self.m.add_restraint(gr) for i in range(100): map(self.change_value, self.all) self.m.evaluate(True) self.assertAlmostEqual( Nuisance(self.mu).get_nuisance_derivative(), self.deriv_mu(*self.all))
def testdx(self): "Test GaussianRestraint(3) x derivative" gr = GaussianRestraint(*self.all) self.m.add_restraint(gr) for i in xrange(100): map(self.change_value, self.all) map(self.change_sign, self.locations) self.m.evaluate(True) self.assertAlmostEqual( Nuisance(self.x).get_nuisance_derivative(), self.deriv_x(*self.all))
def test_NormalMover_MC_ok(self): "Test nuisance scorestate with MonteCarlo mover" nuis = Nuisance.setup_particle(IMP.kernel.Particle(self.m), 50.0) lower = Nuisance.setup_particle(IMP.kernel.Particle(self.m), 10.0) upper = Nuisance.setup_particle(IMP.kernel.Particle(self.m), 90.0) nuis.set_nuisance_is_optimized(True) nuis.set_lower(1.0) nuis.set_lower(lower) nuis.set_upper(upper) nuis.set_upper(80.0) nmv = IMP.core.NormalMover([nuis], IMP.FloatKeys([IMP.FloatKey("nuisance")]), 10.0) mc = IMP.core.MonteCarlo(self.m) mc.set_return_best(False) mc.set_kt(1.0) mc.add_mover(nmv) for i in range(100): mc.optimize(10) self.assertTrue(nuis.get_nuisance() >= nuis.get_lower() and nuis.get_nuisance() <= nuis.get_upper())
def test_NormalMover_MC_fails(self): "Test nuisance scorestate with MonteCarlo mover" nuis = Nuisance.setup_particle(IMP.Particle(self.m), 50.0) lower = Nuisance.setup_particle(IMP.Particle(self.m), 10.0) upper = Nuisance.setup_particle(IMP.Particle(self.m), 90.0) nuis.set_lower(1.0) nuis.set_lower(lower) nuis.set_upper(upper) nuis.set_upper(80.0) nuis.set_nuisance_is_optimized(True) nmv = IMP.core.NormalMover([nuis], IMP.FloatKeys([IMP.FloatKey("nuisance")]), 10.0) mc = IMP.core.MonteCarlo(self.m) mc.set_return_best(False) mc.set_kt(1.0) mc.add_mover(nmv) for i in range(100): mc.optimize(10) self.assertTrue(nuis.get_nuisance() >= nuis.get_lower() and nuis.get_nuisance() <= nuis.get_upper())
def test_GetSet2(self): "tests get and set (border check)" nuisance = Nuisance.setup_particle(IMP.Particle(self.m), 50.0) nuisance.set_lower(10) nuisance.set_upper(80) for si in range(1,100): nuisance.set_nuisance(si) if si < 10: est = 10 elif si > 80: est = 80 else: est = si self.assertAlmostEqual(nuisance.get_nuisance(), est, delta=1e-6)
def test_GetSet2(self): "Test nuisance get and set (border check)" nuisance = Nuisance.setup_particle(IMP.kernel.Particle(self.m), 50.0) nuisance.set_lower(10) nuisance.set_upper(80) for si in range(1, 100): nuisance.set_nuisance(si) if si < 10: est = 10 elif si > 80: est = 80 else: est = si self.assertAlmostEqual(nuisance.get_nuisance(), est, delta=1e-6)
def test_nuisance_down(self): n = IMP.Particle(self.m) Nuisance.setup_particle(n, 1.0) Nuisance(n).set_lower(0.5) Nuisance(n).set_upper(1.5) n.set_value(Nuisance.get_nuisance_key(), 0.1) self.rs.evaluate(False) self.assertAlmostEqual(n.get_value(Nuisance.get_nuisance_key()), 0.5, delta=1e-7) self.assertAlmostEqual(self.rs.values[0], 0.5)
def setUp(self): IMP.test.TestCase.setUp(self) # IMP.set_log_level(IMP.MEMORY) IMP.set_log_level(0) self.m = IMP.Model() self.sigma = Nuisance.setup_particle(IMP.Particle(self.m), 1.0)
def unprotected_evaluate(self, accum): e=0 self.values = [ p.get_value(Nuisance.get_nuisance_key()) for p in self.get_model().get_particles()] return e
def test_Nuisance(self): "Test that a Nuisance can be converted to a Scale" n=Nuisance.setup_particle(IMP.Particle(self.m),3.0) n.set_lower(0) self.assertTrue(Scale.particle_is_instance(n.get_particle()))
def unprotected_evaluate(self, accum): m = self.get_model() e = 0 self.values = [m.get_attribute(Nuisance.get_nuisance_key(), p) for p in m.get_particle_indexes()] return e
def get_value(self, p): try: v = Nuisance(p).get_nuisance() except: v = p return v
def test_Nuisance(self): "Test that a Nuisance can be converted to a Switching" n = Nuisance.setup_particle(IMP.Particle(self.m), 0.5) n.set_lower(0) n.set_upper(1) self.assertTrue(Switching.get_is_setup(n.get_particle()))
def test_Setup1(self): si = Nuisance.setup_particle(IMP.Particle(self.m)) self.assertAlmostEqual(float(si.get_nuisance()),1.0, delta=1e-6) self.assertTrue(math.isinf(-si.get_lower())) self.assertTrue(math.isinf(si.get_upper()))
def test_Setup1(self): "Test nuisance parameter setup without lower/upper" si = Nuisance.setup_particle(IMP.Particle(self.m)) self.assertAlmostEqual(float(si.get_nuisance()), 1.0, delta=1e-6) self.assertEqual(si.get_lower(), -1e3000) # -1e3000 ~= -inf self.assertEqual(si.get_upper(), 1e3000) # 1e3000 ~= inf
def test_Nuisance(self): "test that a Nuisance can be converted to a Switching" n=Nuisance.setup_particle(IMP.Particle(self.m),0.5) n.set_lower(0) n.set_upper(1) self.assertTrue(Switching.particle_is_instance(n.get_particle()))
def change_value(self, p, min=0.1, max=100): try: n = Nuisance(p) except: return n.set_nuisance(random.uniform(min, max))
def change_sign(self, p): try: n = Nuisance(p) except: return n.set_nuisance(n.get_nuisance() * (2 * random.randint(2) - 1))
def test_Nuisance(self): "Test that a Nuisance can be converted to a Scale" n = Nuisance.setup_particle(IMP.kernel.Particle(self.m), 3.0) n.set_lower(0) self.assertTrue(Scale.get_is_setup(n.get_particle()))
def change_value(self, p, min=0.1, max=100): try: n = Nuisance(p) except: return n.set_nuisance(random.uniform(min,max))