def test_volume_1(self): """Testing that volume restraint can separate balls""" if not hasattr(IMP.core, 'VolumeRestraint'): self.skipTest('VolumeRestraint not built (no CGAL)') m = IMP.kernel.Model() ps = [] IMP.base.set_log_level(IMP.base.VERBOSE) for i in range(0, 3): p = IMP.kernel.Particle(m) v = IMP.algebra.get_random_vector_in( IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(0, 0, 0), IMP.algebra.Vector3D(5, 5, 5))) d = IMP.core.XYZR.setup_particle(p, IMP.algebra.Sphere3D(v, 4)) ps.append(p) p.set_is_optimized(IMP.FloatKey("x"), True) p.set_is_optimized(IMP.FloatKey("y"), True) p.set_is_optimized(IMP.FloatKey("z"), True) sc = IMP.container.ListSingletonContainer(ps) vr = IMP.core.VolumeRestraint(IMP.core.Harmonic(0, 1), sc, 4**3 * 3.1415 * 4.0 / 3.0 * len(ps)) m.add_restraint(vr) mc = IMP.core.MonteCarlo(m) mc.add_mover(IMP.core.BallMover(ps, 4)) mc.set_score_threshold(.2) for i in range(5): try: mc.optimize(10) if m.evaluate(False) < .2: break except IMP.base.ValueException: # Catch CG failure pass self.assertLess(m.evaluate(False), .2)
def load_particles(self): self.radius_key = IMP.core.XYZR.get_radius_key() self.weight_key = IMP.atom.Mass.get_mass_key() protein_key = IMP.FloatKey("protein") id_key = IMP.FloatKey("id") self.particles = [] origin = 3.0 self.particles.append( self.create_point_particle(self.imp_model, 9. + origin, 9. + origin, 9. + origin)) self.particles.append( self.create_point_particle(self.imp_model, 12. + origin, 3. + origin, 3. + origin)) self.particles.append( self.create_point_particle(self.imp_model, 3. + origin, 12. + origin, 12. + origin)) p1 = self.particles[0] p1.add_attribute(self.radius_key, 1.0) p1.add_attribute(self.weight_key, 1.0) p1.add_attribute(protein_key, 1) p1.add_attribute(id_key, 1) p1 = self.particles[1] p1.add_attribute(self.radius_key, 1.0) p1.add_attribute(self.weight_key, 1.0) p1.add_attribute(protein_key, 1) p1.add_attribute(id_key, 2) p1 = self.particles[2] p1.add_attribute(self.radius_key, 1.0) p1.add_attribute(self.weight_key, 1.0) p1.add_attribute(protein_key, 1) p1.add_attribute(id_key, 3)
def setUp(self): """initialize IMP environment create particles""" IMP.test.TestCase.setUp(self) IMP.set_log_level(IMP.SILENT) IMP.set_check_level(IMP.NONE) # init IMP model ( the environment) self.imp_model = IMP.Model() self.particles = [] # - create a set of three particles in imp npart = 3 self.rad_key = IMP.FloatKey("radius") self.weight_key = IMP.FloatKey("weight") for i, (x, y, z) in enumerate(((9.0, 9.0, 9.0), (12.0, 3.0, 3.0), (3.0, 12.0, 12.0))): p = self.create_point_particle(self.imp_model, x, y, z) p.add_attribute(self.rad_key, 1.0, False) p.add_attribute(self.weight_key, 10.0) p.add_attribute(IMP.IntKey("id"), i) p.add_attribute(IMP.IntKey("protein"), 1) self.particles.append(p) self.particle_indexes = [] for i in range(npart): self.particle_indexes.append(i) print("initialization done ...")
def _test_bad_attributes(self): """Asking for non-existent attributes should cause an exception""" p1 = particles[0] self.assertRaises(IndexError, p1.get_value, IMP.FloatKey("notexist")) self.assertRaises(IndexError, p1.get_value, IMP.IntKey("notexist")) self.assertRaises(IndexError, p1.get_value, IMP.StringKey("notexist")) self.assertRaises(IndexError, p1.add_attribute, IMP.FloatKey(), 0)
def create_point_particle(self, model, x, y, z): """Make a particle with optimizable x, y and z attributes, and add it to the model.""" p = IMP.Particle(model) p.add_attribute(IMP.FloatKey("x"), x, True) p.add_attribute(IMP.FloatKey("y"), y, True) p.add_attribute(IMP.FloatKey("z"), z, True) return p
def rescale_velocities(self, particles, factor): """rescale the velocities of a bunch of particles having vx vy and vz floatkeys """ keys = [IMP.FloatKey("vx"), IMP.FloatKey("vy"), IMP.FloatKey("vz")] for p in particles: for k in keys: p.set_value(k, p.get_value(k) * factor)
def __init__(self, cont, md, n_md_steps): self.cont = cont self.md = md self.n_md_steps = n_md_steps self.velkeys = [ IMP.FloatKey("vx"), IMP.FloatKey("vy"), IMP.FloatKey("vz") ]
def particle_distance(self, p1, p2): """Return distance between two given particles""" xkey = IMP.FloatKey("x") ykey = IMP.FloatKey("y") zkey = IMP.FloatKey("z") dx = p1.get_value(xkey) - p2.get_value(xkey) dy = p1.get_value(ykey) - p2.get_value(ykey) dz = p1.get_value(zkey) - p2.get_value(zkey) return math.sqrt(dx * dx + dy * dy + dz * dz)
def randomize_particles(self, particles, deviation): """Randomize the xyz coordinates of a list of particles""" # Note: cannot use XYZ here since that pulls in IMP.core xkey = IMP.FloatKey("x") ykey = IMP.FloatKey("y") zkey = IMP.FloatKey("z") for p in particles: p.set_value(xkey, random.uniform(-deviation, deviation)) p.set_value(ykey, random.uniform(-deviation, deviation)) p.set_value(zkey, random.uniform(-deviation, deviation))
def test_ranges(self): """Test float attribute ranges""" m = IMP.kernel.Model("float ranges") IMP.base.set_log_level(IMP.base.VERBOSE) ps = [IMP.kernel.Particle(m) for i in range(0, 2)] for k in [IMP.FloatKey(0), IMP.FloatKey(4), IMP.FloatKey(7)]: ps[0].add_attribute(k, k.get_index()) ps[1].add_attribute(k, k.get_index() + 1) rg = IMP.kernel._get_range(m, k) self.assertEqual(rg[0], k.get_index()) self.assertEqual(rg[1], k.get_index() + 1)
def do_md(self, nsteps): "perform md simulation on protein for nsteps" for i in IMP.atom.get_leaves(self._p['prot']): IMP.core.XYZR(i).set_coordinates_are_optimized(True) self._p['sigma'].set_is_optimized(IMP.FloatKey("scale"), False) self._p['gamma'].set_is_optimized(IMP.FloatKey("scale"), False) self._md.optimize(nsteps) self.do_md_protein_statistics(self.md_key, nsteps, self._md, temperature=1 / (kB * self.inv_temp), prot_coordinates=self.get_pdb( self._p['prot']))
def test_non_xyz(self): """Should skip particles without xyz attributes""" p = IMP.Particle(self.model) p.add_attribute(IMP.FloatKey("attr"), 0.0, True) r = IMP.RestraintSet(self.model) self.md.set_scoring_function(r) self.md.optimize(100)
def test_make_velocities(self): """Test that MD generates particle velocities""" self.md.optimize(0) keys = [IMP.FloatKey(x) for x in ("vx", "vy", "vz")] for p in self.model.get_particles(): for key in keys: self.assertTrue(p.has_attribute(key))
def create_non_rigid_members(self,spec,max_trans=1.0): hiers = get_hierarchies(spec) floatkeys = [IMP.FloatKey(4), IMP.FloatKey(5), IMP.FloatKey(6)] idxs = set(self.rb.get_member_indexes()) for h in hiers: p = h.get_particle() if not p.get_index() in idxs: raise Exception("You tried to create nonrigid members from " "particles that aren't in the RigidBody!") self.rb.set_is_rigid_member(p.get_index(),False) for fk in floatkeys: p.set_is_optimized(fk,True) self.flexible_movers.append(IMP.core.BallMover([p], IMP.FloatKeys(floatkeys), max_trans))
def test_remove_attributes(self): """Test that attributes can be removed""" (model, particles) = self.setup() p = particles[0] fk = IMP.FloatKey("to_remove") p.add_attribute(fk, 0, False) self.assertTrue(p.has_attribute(fk)) self.assertFalse(p.get_is_optimized(fk)) p.set_is_optimized(fk, True) self.assertTrue(p.get_is_optimized(fk)) p.set_is_optimized(fk, False) self.assertFalse(p.get_is_optimized(fk)) self._test_add_remove(p, IMP.FloatKey("something"), 1.0) self._test_add_remove(p, IMP.StringKey("something"), "Hello") self._test_add_remove(p, IMP.IntKey("something"), 1) self._test_add_remove(p, IMP.kernel.ParticleIndexKey("something"), p)
def setup(self): """Build a set of test particles""" IMP.test.TestCase.setUp(self) model = IMP.kernel.Model("particles model") particles = [] # create particles 0 - 11 for i in range(0, 12): particles.append( self.create_point_particle(model, i * 2, i * 3, i * 4)) p1 = particles[i] p1.add_attribute(radkey, 1.5 * i, False) p1.add_attribute(idkey, i) p1.add_attribute(IMP.IntKey("six"), 6) p1.add_attribute(IMP.StringKey("id_str"), "name_" + str(i)) p1.add_attribute(IMP.StringKey("six"), "b:0110") # add additional attributes to particle 11 for i in range(0, 6): p1.add_attribute(IMP.FloatKey("attr_" + str(i)), 3.5 * i, False) # clear derivatives print(model.get_ref_count()) model.evaluate(True) print(model.get_ref_count()) return (model, particles)
def _setUp(self): """ create particles """ IMP.test.TestCase.setUp(self) self.particles = [] mdl = IMP.kernel.Model() self.weight_key = IMP.FloatKey("mass") for val in [[9., 5., 5., 1., 1.], [12., 9., 4., 1., 1.], [4., 5., 5., 1., 1.]]: p = IMP.kernel.Particle(mdl) IMP.core.XYZR.setup_particle( p, IMP.algebra.Sphere3D( IMP.algebra.Vector3D(val[0], val[1], val[2]), val[3])) p.add_attribute(self.weight_key, 5.) self.particles.append(p) resolution = 2.0 voxel_size = 1.0 em_map = IMP.em.SampledDensityMap(self.particles, resolution, voxel_size, self.weight_key) em_map.calcRMS() self.rms = em_map.get_header().rms print("RMSD of original map = " + str(self.rms)) self.erw = IMP.em.MRCReaderWriter() self.out_filename = "aa.mrc" IMP.em.write_map(em_map, self.out_filename, self.erw)
def test_get_derivatives_numpy(self): """Test _get_derivatives_numpy method""" m1 = IMP.Model("numpy get_derivs") p1 = IMP.Particle(m1) p2 = IMP.Particle(m1) m2 = IMP.Model("numpy no get_derivs") p12 = IMP.Particle(m2) k = IMP.FloatKey("myf") p1.add_attribute(k, 1.0) if IMP.IMP_KERNEL_HAS_NUMPY: n = m1._get_derivatives_numpy(k) self.assertIs(n.base, m1) self.assertEqual(len(n), 1) # no derivatives for p2 self.assertAlmostEqual(n[0], 0.0, delta=1e-6) n[0] = 42.0 self.assertAlmostEqual(p1.get_derivative(k), 42.0, delta=1e-6) n = m2._get_derivatives_numpy(k) self.assertIs(n.base, m2) self.assertEqual(len(n), 0) # no float key for this model else: self.assertRaises(NotImplementedError, m1._get_derivatives_numpy, k)
def get_floppy_body_movers(self, fbs, maxtrans): mvs = [] for fb in fbs: # check is that is a rigid body member: if IMP.core.NonRigidMember.get_is_setup(fb): # if so force the particles to move anyway floatkeys = [IMP.FloatKey(4), IMP.FloatKey(5), IMP.FloatKey(6)] for fk in floatkeys: fb.set_is_optimized(fk, True) mvs.append( IMP.core.BallMover(fb.get_model(), fb, IMP.FloatKeys(floatkeys), maxtrans)) else: # otherwise use the normal ball mover mvs.append(IMP.core.BallMover(fb.get_model(), fb, maxtrans)) return mvs
def _are_close(self, a, b, rk, d): da = IMP.core.XYZ(a) db = IMP.core.XYZ(b) r = 0 if rk != IMP.FloatKey(): r = a.get_value(rk) + b.get_value(rk) cd = IMP.core.get_distance(da, db) return (cd - r <= d)
def _are_close(self, m, a, b, rk, d): da = IMP.core.XYZ(m, a) db = IMP.core.XYZ(m, b) r = 0 if rk != IMP.FloatKey(): r = m.get_attribute(rk, a) + m.get_attribute(rk, b) cd = IMP.core.get_distance(da, db) return (cd - r <= d)
def create_particle(self, m): p = m.add_particle("p") d = IMP.core.XYZ.setup_particle(m, p) d.set_coordinates( IMP.algebra.get_random_vector_in( IMP.algebra.get_unit_bounding_box_3d())) m.add_attribute(IMP.FloatKey("thekey"), p, d.get_x()) return p
def setUp(self): """Build test model and optimizer""" IMP.test.TestCase.setUp(self) IMP.set_log_level(IMP.SILENT) self.imp_model = IMP.Model() self.apix = 1.0 self.surf_key = IMP.FloatKey("surf_ind") self.load_protein("1z5s_A.pdb")
def _test_starting_conditions(self, starting_values): """Test the optimizer with given starting conditions""" model = IMP.Model() particles = [] for value in starting_values: p = IMP.Particle(model) particles.append(p) p.add_attribute(IMP.FloatKey("x"), value, True) rsr = WoodsFunc(model, particles) opt = IMP.gsl.QuasiNewton(model) opt.set_scoring_function(rsr) # opt.set_threshold(1e-5) e = opt.optimize(1000) for p in particles: val = p.get_value(IMP.FloatKey("x")) self.assertAlmostEqual(val, 1.0, places=1) self.assertAlmostEqual(e, 0.0, places=2)
def test_make_velocities(self): """Test that MD generates particle velocities""" r = IMP.RestraintSet(self.model) self.md.set_scoring_function(r) self.md.optimize(0) keys = [IMP.FloatKey(x) for x in ("vx", "vy", "vz")] for p in self.model.get_particle_indexes(): for key in keys: self.assertTrue(self.model.get_has_attribute(key, p))
def _make_stuff(self): IMP.test.TestCase.setUp(self) m = IMP.Model() pa = m.add_particle("p") att = IMP.FloatKey("test") m.add_attribute(att, pa, 5.0) m.set_is_optimized(att, pa, True) mv = IMP.core.NormalMover(m, pa, [att], 1.0) return m, mv, pa, att
def _test_starting_conditions(self, starting_values): """Test the optimizer with given starting conditions""" model = IMP.kernel.Model() particles = [] for value in starting_values: p = IMP.kernel.Particle(model) particles.append(p) p.add_attribute(IMP.FloatKey("x"), value, True) rsr = WoodsFunc(model, particles) model.add_restraint(rsr) opt = IMP.gsl.ConjugateGradients(model) # opt.set_threshold(1e-5) e = opt.optimize(500) for p in particles: val = p.get_value(IMP.FloatKey("x")) self.assertAlmostEqual(val, 1.0, places=1) self.assertAlmostEqual(e, 0.0, places=2)
def get_nuisance_movers(self, nuisances, maxstep): mvs = [] for nuisance in nuisances: print(nuisance, maxstep) mvs.append( IMP.core.NormalMover([nuisance], IMP.FloatKeys([IMP.FloatKey("nuisance")]), maxstep)) return mvs
def test_add_surface_attribute(self): """Check that reading a map back in preserves the stdevs""" IMP.set_log_level(IMP.VERBOSE) voxel_size = 1. IMP.multifit.add_surface_index(self.mh, voxel_size) shell_key = IMP.FloatKey("surf_ind") for p in self.particles: # print p.get_particle().get_value(shell_key) self.assertGreater(p.get_particle().get_value(shell_key), 3., "map was not sampled correctly")
def test_score_state_update(self): """Test that unscored particles are updated on evaluate""" m = IMP.kernel.Model() pi = m.add_particle("my particle") k = IMP.FloatKey("hi") ss = TouchSS(m, pi, k) m.add_attribute(k, pi, 0) m.set_is_optimized(k, pi, True) m.evaluate(False) self.assertEqual(m.get_attribute(k, pi), 1)