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_data_directory(self): """Test key aliases""" k0 = IMP.IntKey("0") k1 = IMP.IntKey("1") ak1 = IMP.IntKey.add_alias(k0, "0p") k2 = IMP.IntKey("2") self.assertEqual(IMP.IntKey("0p"), k0) self.assertEqual(IMP.IntKey("2"), k2) self.assertNotEqual(k2, ak1)
def test_global_min1(self): """Testing branch and bound sampler""" m = IMP.kernel.Model() m.set_log_level(IMP.base.SILENT) IMP.base.set_log_level(IMP.base.VERBOSE) ps = [] ns = 5 np = 4 for i in range(0, np): ps.append(IMP.kernel.Particle(m)) pst = IMP.domino.ParticleStatesTable() print(m.get_number_of_score_states()) print(m.get_number_of_restraints()) dsst = IMP.domino.BranchAndBoundSampler(m, pst) for p in ps: pst.set_particle_states(p, TrivialParticleStates(ns)) cs = dsst.create_sample() self.assertEqual(cs.get_number_of_configurations(), ns**len(ps)) all_states = [] for i in range(0, cs.get_number_of_configurations()): cs.load_configuration(i) s = [] for p in ps: s.append(p.get_value(IMP.IntKey("hi"))) ss = IMP.domino.Assignment(s) # print all_states self.assertNotIn(s, all_states) all_states.append(s)
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 _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 test_set(self): """Testing ClassnamesConstraint""" # write increment an int field # call evaluate and check that it is incremented IMP.set_log_level(IMP.VERBOSE) print("start") m = IMP.Model() print("hi") c = IMP.container.ListClassnameContainer(m) cs = [] for i in range(0, 30): t = self.create_FUNCTIONNAME(m) if 'FUNCTIONNAME' == 'particle': c.add(t.get_index()) else: c.add([x.get_index() for x in t]) cs.append(t) print("dl") k = IMP.IntKey("thevalue") f = ClassnameTestModifier(k) print("apply") s = IMP.container.ClassnamesConstraint(f, None, c) print("add") m.add_score_state(s) m.update() for p in cs: self.assertTrue(FUNCTIONNAME_has_attribute(p, k)) print("done")
def test_get_ints_numpy(self): """Test _get_ints_numpy method""" m1 = IMP.Model("numpy get_ints") p1 = IMP.Particle(m1) p2 = IMP.Particle(m1) p3 = IMP.Particle(m1) m2 = IMP.Model("numpy no get_ints") p12 = IMP.Particle(m2) k = IMP.IntKey("myf") p1.add_attribute(k, 1) p2.add_attribute(k, 2) if IMP.IMP_KERNEL_HAS_NUMPY: n = m1._get_ints_numpy(k) self.assertIs(n.base, m1) self.assertEqual(len(n), 2) # no int attribute for p3 self.assertEqual(n[0], 1) self.assertEqual(n[1], 2) n[0] = 42 n[1] = 24 self.assertEqual(p1.get_value(k), 42) self.assertEqual(p2.get_value(k), 24) n = m2._get_ints_numpy(k) self.assertIs(n.base, m2) self.assertEqual(len(n), 0) # no int key for this model else: self.assertRaises(NotImplementedError, m1._get_ints_numpy, k)
def test_set(self): """Testing ClassnamesConstraint""" # write increment an int field # call evaluate and check that it is incremented IMP.base.set_log_level(IMP.base.VERBOSE) print "start" m = IMP.kernel.Model() print "hi" c = IMP.container.ListClassnameContainer(m) cs = [] for i in range(0, 30): t = self.create_FUNCTIONNAME(m) c.add_FUNCTIONNAME(t) cs.append(t) print "dl" k = IMP.IntKey("thevalue") f = ClassnameTestModifier(k) print "apply" s = IMP.container.ClassnamesConstraint(f, None, c) print "add" m.add_score_state(s) m.update() for p in cs: self.assertTrue(FUNCTIONNAME_has_attribute(p, k)) print "done"
def _create_stuff(self): m = IMP.kernel.Model() ps = [IMP.kernel.Particle(m) for i in range(0, 10)] r = IMP.kernel._ConstRestraint(1, ps) r.set_name("const restraint") pst = IMP.domino.ParticleStatesTable() ik = IMP.IntKey("key") for p in ps: p.add_attribute(ik, 0) pst.set_particle_states(p, IMP.domino.IndexStates(4, ik)) cache = IMP.domino.RestraintCache(pst, 4) return (m, ps, r, pst, ik, cache)
def test_no_model(self): """Check access of attributes from python""" m = IMP.kernel.Model() p = IMP.kernel.Particle(m) ik = IMP.IntKey("hi") m.add_attribute(ik, p.get_index(), 1) self.assertEqual(m.get_attribute(ik, p.get_index()), 1) pisk = IMP.kernel.ParticleIndexesKey("hi") m.add_attribute(pisk, p.get_index(), [p.get_index()]) self.assertEqual(m.get_attribute(pisk, p.get_index()), [p.get_index()]) pik = IMP.kernel.ParticleIndexKey("hi") m.add_attribute(pik, p.get_index(), p.get_index()) self.assertEqual(m.get_attribute(pik, p.get_index()), p.get_index())
def test_particles(self): """Test that particle attributes are available and correct""" (model, particles) = self.setup() for (i, p) in enumerate(particles): self.assertTrue(p.has_attribute(xkey)) # A Float "x" exists; make sure that has_attribute doesn't get # confused between different types of attribute: self.assertFalse(p.has_attribute(IMP.IntKey("x"))) self.assertFalse(p.has_attribute(IMP.IntKey("notexist"))) self.assertEqual(p.get_value(xkey), i * 2) self.assertEqual(p.get_value(ykey), i * 3) self.assertEqual(p.get_value(zkey), i * 4) self.assertEqual(p.get_value(idkey), i) self.assertEqual(p.get_value(IMP.StringKey("id_str")), "name_" + str(i)) self.assertEqual(p.get_value(IMP.IntKey("six")), 6) self.assertEqual(p.get_value(IMP.StringKey("six")), "b:0110") # test additional attributes in particle 11 p = particles[11] for i in range(0, 6): val = p.get_value(IMP.FloatKey("attr_" + str(i))) self.assertEqual(val, 3.5 * i)
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 test_sset(self): """Testing ClassnameConstraint""" # write increment an int field # call evaluate and check that it is incremented IMP.set_log_level(IMP.VERBOSE) print("start") m = IMP.Model() print("hi") t = self.create_FUNCTIONNAME(m) print("dl") k = IMP.IntKey("thevalue") f = ClassnameTestModifier(k) print("apply") s = self.create_CLASSFUNCTIONNAME_score_state(f, None, m, t) m.add_score_state(s) print("add") m.update() self.assertTrue(FUNCTIONNAME_has_attribute(t, k)) print("done")
def test_constraint(self): """Test example Constraint""" k = IMP.IntKey("Constraint key") # Test both implementations: C++ and Python for typ in (IMP.example.ExampleConstraint, IMP.example.PythonExampleConstraint): m = IMP.Model() p = IMP.Particle(m) c = typ(p) self.assertEqual(p.get_value(k), 0) m.update() self.assertEqual(p.get_value(k), 1) m.update() self.assertEqual(p.get_value(k), 2) self.assertIn("ExampleConstraint", str(c)) self.assertIn("ExampleConstraint", repr(c)) self.assertIn("example", c.get_version_info().get_module()) self.assertEqual(len(c.get_inputs()), 1) self.assertEqual(len(c.get_outputs()), 1)
class Price(IMP.Decorator): price_key = IMP.IntKey("price") def __init__(self, p): if not self.get_is_setup(p): self.setup_particle(p) self.particle = p def setup_particle(self, p): p.add_attribute(self.price_key, 0) self.particle = p def get_price(self): return self.particle.get_value(self.price_key) def set_price(self, x): self.particle.set_value(self.price_key, int(x)) def get_is_setup(self, p): return p.has_attribute(self.price_key)
def test_deriv(self): """Test calculated derivatives for a distorted model's map""" if modeller is None: self.skipTest("modeller module unavailable") modeller.log.level = (0, 0, 0, 0, 1) self.env = modeller.environ() self.env.edat.dynamic_sphere = False self.env.libs.topology.read(file='$(LIB)/top_heav.lib') self.env.libs.parameters.read(file='$(LIB)/par.lib') # init IMP model ( the environment) self.imp_model = IMP.Model() self.particles = [] # - create a set of three particles in imp for i in range(3): self.particles.append(IMP.Particle(self.imp_model)) # Load the same model into Modeller self.modeller_model = copy_to_modeller(self.env, self.particles) # - add the particles attributes rad = 1.0 wei = 1.0 wei_key = IMP.FloatKey("weight") prot_key = IMP.IntKey("protein") id_key = IMP.IntKey("id") for i, p_data in enumerate([[9.0, 9.0, 9.0, rad, wei, 1], [12.0, 3.0, 3.0, rad, wei, 1], [3.0, 12.0, 12.0, rad, wei, 1]]): p = self.particles[i] center = IMP.algebra.Vector3D(*p_data[0:3]) sphere = IMP.algebra.Sphere3D(center, p_data[3]) IMP.core.XYZR.setup_particle(p, sphere) p.add_attribute(wei_key, p_data[4]) p.add_attribute(prot_key, p_data[5]) p.add_attribute(id_key, i) self.atmsel = modeller.selection(self.modeller_model) print("initialization done ...") resolution = 3. voxel_size = 1. model_map = IMP.em.SampledDensityMap(self.particles, resolution, voxel_size, wei_key) erw = IMP.em.EMReaderWriter() xorigin = model_map.get_header().get_xorigin() yorigin = model_map.get_header().get_yorigin() zorigin = model_map.get_header().get_zorigin() print(("x= " + str(xorigin) + " y=" + str(yorigin) + " z=" + str(zorigin))) mapfile = IMP.create_temporary_file_name('xxx.em') IMP.em.write_map(model_map, mapfile, erw) # EM restraint em_map = IMP.em.read_map(mapfile, erw) em_map.get_header_writable().set_xorigin(xorigin) em_map.get_header_writable().set_yorigin(yorigin) em_map.get_header_writable().set_zorigin(zorigin) em_map.get_header_writable().compute_xyz_top() em_map.get_header_writable().set_resolution(resolution) print("rms_calc", em_map.get_rms_calculated()) em_map.calcRMS() print("rms_calc", em_map.get_rms_calculated()) ind_emrsr = [] ind_emrsr.append( IMP.em.FitRestraint(self.particles, em_map, [0., 0.], wei_key, 1.0)) sf = IMP.core.RestraintsScoringFunction(ind_emrsr) # add IMP Restraints into the modeller scoring function t = self.modeller_model.env.edat.energy_terms t.append(IMP.modeller.IMPRestraints(self.particles, sf)) print(("EM-score score: " + str(self.atmsel.energy()))) self.atmsel.randomize_xyz(1.0) nviol = self.atmsel.debug_function(debug_function_cutoff=(.010, 0.010, 0.01), detailed_debugging=True) self.assertLess(nviol, 1, "at least one partial derivative is wrong!") print(" derivs done ...") os.unlink(mapfile)
def test_dir_tms(self): """Test that decorator particle methods can be called""" m = IMP.Model("decorator particle methos") p0 = IMP.Particle(m) d = IMP._TrivialDecorator.setup_particle(p0) d.add_attribute(IMP.IntKey("Hi"), 1)
import IMP import IMP.test import IMP.domino import IMP.core import random key = IMP.IntKey("assignment") class Tests(IMP.test.TestCase): def _create_stuff(self): m = IMP.kernel.Model() ps = [IMP.kernel.Particle(m) for i in range(0, 10)] r = IMP.kernel._ConstRestraint(1, ps) r.set_name("const restraint") pst = IMP.domino.ParticleStatesTable() ik = IMP.IntKey("key") for p in ps: p.add_attribute(ik, 0) pst.set_particle_states(p, IMP.domino.IndexStates(4, ik)) cache = IMP.domino.RestraintCache(pst, 4) return (m, ps, r, pst, ik, cache) def test_decomposition(self): """Test cache with decomposition with no max is empty""" (m, ps, r, pst, ik, cache) = self._create_stuff() cache.add_restraints([r]) subset = IMP.domino.Subset(random.sample(ps, 2)) print subset rsb = cache.get_restraints(subset, []) self.assertEqual(len(rsb), 0)
def __init__(self, n): IMP.domino.ParticleStates.__init__(self) self.key = IMP.IntKey("hi") self.n = n
from __future__ import print_function import IMP import IMP.test import IMP.core import IMP.algebra import IMP.container import random tk = IMP.IntKey("type") class Pred(IMP.PairPredicate): def __init__(self): IMP.PairPredicate.__init__(self) def get_value(self, pp): return pp[0].get_value(tk) + pp[1].get_value(tk) def do_get_inputs(self, m, pis): return [m.get_particle(i) for i in pis] class Score(IMP.PairScore): def __init__(self, v): self._value = v self._pred = Pred() self._pred.set_was_used(True) IMP.PairScore.__init__(self) def evaluate(self, pp, da): if self._pred.get_value(pp) == self._value:
from __future__ import print_function, division import IMP import IMP.test import IMP.container import math ik = IMP.IntKey("hi") class Odd(IMP.SingletonPredicate): def get_value(self, p): return p.get_value(ik) % 2 def do_get_inputs(self, m, pis): return [m.get_particle(i) for i in pis] class Mod5(IMP.SingletonPredicate): def get_value(self, p): return p.get_value(ik) % 5 def do_get_inputs(self, m, pis): return [m.get_particle(i) for i in pis] class Tests(IMP.test.TestCase): """Tests for all pairs pair container""" def test_allp(self): """Checking distribute particles""" m = IMP.Model() ps = []
import IMP import IMP.test import IMP.core import io typekey = IMP.IntKey('mytype') class Tests(IMP.test.TestCase): """Class to test TypedPairScore""" def _make_particles(self, m, types): """Make particles with the given types""" ps = [IMP.kernel.Particle(m) for i in types] for p, typ in zip(ps, types): p.add_attribute(typekey, typ) return ps def test_evaluate(self): """Check TypedPairScore::evaluate()""" ps = IMP.core.TypedPairScore(typekey) cps = IMP.kernel._ConstPairScore(5) ps.set_pair_score(cps, 0, 1) # Keep Python reference to the model so that the particles # aren't destroyed m = IMP.kernel.Model() pa, pb = self._make_particles(m, (0, 1)) da = IMP.DerivativeAccumulator() # The ordering of the particles should not matter: pab = (pa, pb)
from __future__ import print_function import IMP import IMP.test xkey = IMP.FloatKey("x") ykey = IMP.FloatKey("y") zkey = IMP.FloatKey("z") idkey = IMP.IntKey("id") radkey = IMP.FloatKey("radius") class Tests(IMP.test.TestCase): """Test particles""" 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")
from __future__ import print_function import IMP import IMP.test fk0 = IMP.FloatKey("f0") fk1 = IMP.FloatKey("f1") fk2 = IMP.FloatKey("f2") ik0 = IMP.IntKey("i0") ik1 = IMP.IntKey("i1") ik2 = IMP.IntKey("i2") sk0 = IMP.StringKey("s0") sk1 = IMP.StringKey("s1") sk2 = IMP.StringKey("s2") pk0 = IMP.kernel.ParticleIndexKey("p0") pk1 = IMP.kernel.ParticleIndexKey("p1") pk2 = IMP.kernel.ParticleIndexKey("p2") class Tests(IMP.test.TestCase): """Test particles""" def _force_set(self, p, k, v): if p.has_attribute(k): p.set_value(k, v) else: p.add_attribute(k, v) def _force_remove(self, p, k): if p.has_attribute(k): p.remove_attribute(k)
# probably be implemented in C++, for speed but implementing the filter in # python is good for prototyping. from __future__ import print_function import IMP import IMP.container import IMP.core import IMP.algebra import sys IMP.setup_from_argv(sys.argv, "filter close pairs") np = 10 bb = IMP.algebra.BoundingBox3D(IMP.algebra.Vector3D(0, 0, 0), IMP.algebra.Vector3D(5, 5, 5)) ik = IMP.IntKey("num") IMP.set_log_level(IMP.SILENT) m = IMP.Model() l = [] for i in range(0, np): p = m.add_particle("p%d" % i) m.add_attribute(ik, p, i) IMP.core.XYZR.setup_particle( m, p, IMP.algebra.Sphere3D(IMP.algebra.get_random_vector_in(bb), 1)) l.append(p) lsc = IMP.container.ListSingletonContainer(m, l) cpc = IMP.container.ClosePairContainer(lsc, 0.0) m.update() print("without", [(x[0].get_name(), x[1].get_name()) for x in cpc.get_particle_pairs()])