def test_get_fg_chain_roots(self): ''' Test that SimulationData.get_fg_chain_roots() works correctly ''' IMP.set_log_level(IMP.SILENT) test_util.test_protobuf_installed(self) cfg_file = self.get_tmp_file_name("barak_config.pb") assign_file = self.get_tmp_file_name("barak_assign.pb") coords = self._create_cfg_file_with_fgs(cfg_file) print("assigning parameter ranges from config") num=assign_ranges( cfg_file, assign_file, 0, False, 10 ) print ("Loading SimData") sd= IMP.npctransport.SimulationData(assign_file, False) sd.set_rmf_file( self.get_tmp_file_name("out.rmf") ) fgs = sd.get_fg_chain_roots( ) # atom.Hierarchies print (" fgs ", fgs) ones = 0 twos = 0 for fg in fgs: chain = get_fg_chain(fg) type_name = IMP.core.Typed(fg.get_child(0)).get_type().get_string() if(type_name == "my_fg1"): self.assert_(chain.get_number_of_beads() == 3) ones = ones + 1 if(type_name == "my_fg2"): self.assert_(chain.get_number_of_beads() == 6) twos = twos + 1 self.assert_(ones == 2 and twos == 3)
def test_get_fg_chain_roots(self): ''' Test that SimulationData.get_fg_chain_roots() works correctly ''' IMP.set_log_level(IMP.SILENT) test_util.test_protobuf_installed(self) cfg_file = self.get_tmp_file_name("barak_config.pb") assign_file = self.get_tmp_file_name("barak_assign.pb") coords = self._create_cfg_file_with_fgs(cfg_file) print("assigning parameter ranges from config") num = assign_ranges(cfg_file, assign_file, 0, False, 10) print("Loading SimData") sd = IMP.npctransport.SimulationData(assign_file, False) sd.set_rmf_file(self.get_tmp_file_name("out.rmf")) fgs = sd.get_fg_chain_roots() # atom.Hierarchies print(" fgs ", fgs) ones = 0 twos = 0 for fg in fgs: chain = get_fg_chain(fg) type_name = IMP.core.Typed(fg.get_child(0)).get_type().get_string() if (type_name == "my_fg1"): self.assertEqual(chain.get_number_of_beads(), 3) ones = ones + 1 if (type_name == "my_fg2"): self.assertEqual(chain.get_number_of_beads(), 6) twos = twos + 1 self.assertTrue(ones == 2 and twos == 3)
def test_copy_fgs(self): test_util.test_protobuf_installed(self) config_fname = "./config_fg_copy.pb" # self.get_tmp_file_name("simple_cfg.pb") output_fname1 = "./output_fg_copy.pb" # self.get_tmp_file_name("test_fg_copy.pb") output_fname2 = "./output_fg_copy.pb" # self.get_tmp_file_name("test_fg_copy.pb") rmf_fname1 = "./rmf_fg_copy.rmf" # self.get_tmp_file_name("test_fg_copy.pb") rmf_fname2 = "./rmf_fg_copy2.rmf" # self.get_tmp_file_name("test_fg_copy.pb") test_util.make_simple_cfg( config_fname, is_slab_on=True, n_particles_factor=3.0, is_obstacles=True) sd1= run_from_config(config_fname, output_fname1, rmf_fname1) sd2= run_from_config(config_fname, output_fname2, rmf_fname2) fg_beads1= get_fg_beads_from_simulation_data(sd1) fg_beads2= get_fg_beads_from_simulation_data(sd2) # Verify that not all are equal initially: are_all_equal= True for p1, p2 in zip(fg_beads1, fg_beads2): if not are_rigid_body_reference_frames_equal \ ( IMP.core.RigidBody(p1), IMP.core.RigidBody(p2) ): are_all_equal=False self.assertFalse(are_all_equal) # Copy then verify that all are equal now: IMP.npctransport.copy_FGs_coordinates(sd1, sd2) for p1, p2 in zip(fg_beads1, fg_beads2): self.assertTrue(are_rigid_body_reference_frames_equal \ ( IMP.core.RigidBody(p1), IMP.core.RigidBody(p2) ) ) if IMP.get_check_level() >= IMP.USAGE_AND_INTERNAL: sd2.get_bd().optimize(1) else: sd2.get_bd().optimize(20000)
def test_copy_fgs(self): test_util.test_protobuf_installed(self) config_fname = "./config_fg_copy.pb" # self.get_tmp_file_name("simple_cfg.pb") output_fname1 = "./output_fg_copy.pb" # self.get_tmp_file_name("test_fg_copy.pb") output_fname2 = "./output_fg_copy.pb" # self.get_tmp_file_name("test_fg_copy.pb") rmf_fname1 = "./rmf_fg_copy.rmf" # self.get_tmp_file_name("test_fg_copy.pb") rmf_fname2 = "./rmf_fg_copy2.rmf" # self.get_tmp_file_name("test_fg_copy.pb") test_util.make_simple_cfg(config_fname, is_slab_on=True, n_particles_factor=3.0, is_obstacles=True) sd1 = run_from_config(config_fname, output_fname1, rmf_fname1) sd2 = run_from_config(config_fname, output_fname2, rmf_fname2) fg_beads1 = get_fg_beads_from_simulation_data(sd1) fg_beads2 = get_fg_beads_from_simulation_data(sd2) # Verify that not all are equal initially: are_all_equal = True for p1, p2 in zip(fg_beads1, fg_beads2): if not are_rigid_body_reference_frames_equal \ ( IMP.core.RigidBody(p1), IMP.core.RigidBody(p2) ): are_all_equal = False self.assertFalse(are_all_equal) # Copy then verify that all are equal now: IMP.npctransport.copy_FGs_coordinates(sd1, sd2) for p1, p2 in zip(fg_beads1, fg_beads2): self.assertTrue(are_rigid_body_reference_frames_equal \ ( IMP.core.RigidBody(p1), IMP.core.RigidBody(p2) ) ) if IMP.get_check_level() >= IMP.USAGE_AND_INTERNAL: sd2.get_bd().optimize(1) else: sd2.get_bd().optimize(20000)
def test_fg_anchoring_through_protobuf(self): ''' Test that FG nups can be anchored properly through protobuf file ''' IMP.set_log_level(IMP.SILENT) test_util.test_protobuf_installed(self) cfg_file = self.get_tmp_file_name("barak_config.pb") assign_file = self.get_tmp_file_name("barak_assign.pb") coords = self._create_cfg_file_with_fg_anchors(cfg_file) print("assigning parameter ranges from config") num = IMP.npctransport.assign_ranges(cfg_file, assign_file, 0, False, 10) sd = IMP.npctransport.SimulationData(assign_file, False, "") # self.get_tmp_file_name("out.rmf")); self._assert_anchors_in_place(sd, coords) # verify that anchors remain intact during optimization if IMP.get_check_level() >= IMP.USAGE_AND_INTERNAL: short_init_factor = 0.00001 print("short position initialization in non-fast mode") else: short_init_factor = 0.1 IMP.npctransport.initialize_positions(sd, [], False, short_init_factor) self._assert_anchors_in_place(sd, coords) sd.get_bd().optimize(1000) self._assert_anchors_in_place(sd, coords)
def _test_harmonic_spring_score_from_protobuf(self, n_beads=2): ''' Test that FG nups can be anchored properly through protobuf file ''' global NS_PER_CHUNK IMP.set_log_level(IMP.SILENT) test_util.test_protobuf_installed(self) cfg_file = self.get_tmp_file_name("barak_config.pb") # assign_file = self.get_tmp_file_name("barak_assign.pb") assign_file = "output2.pb" self._create_cfg_file_with_fg_anchors(cfg_file, n_beads) print("assigning parameter ranges from config") num = IMP.npctransport.assign_ranges(cfg_file, assign_file, 0, False, 10) sd = IMP.npctransport.SimulationData(assign_file, False, "") # "out2.rmf") # self.get_tmp_file_name("out.rmf")); # verify that anchors remain intact during optimization if IMP.get_check_level() >= IMP.USAGE_AND_INTERNAL: short_init_factor = 0.0001 n_opt_cycles = 10 print("short position initialization in non-fast mode") else: n_opt_cycles = 100000 short_init_factor = 1.0 IMP.npctransport.initialize_positions(sd, [], False, short_init_factor) print("Energy before optimization:", sd.get_bd().get_scoring_function().evaluate(False)) print("Optimizing") print("dT %.1f fs" % sd.get_bd().get_maximum_time_step()) sd.get_bd().optimize(n_opt_cycles) print() print("Energy after optimization:", sd.get_bd().get_scoring_function().evaluate(False)) print("Report particles:") self._do_particles_report(sd.get_model(), sd.get_beads()) print() sim_time_ns = 500 * TAU_NS sd.get_bd().set_current_time(0.0) sd.activate_statistics() print("Running for %.1f ns" % sim_time_ns) R = test_util.optimize_in_chunks(sd, sim_time_ns, NS_PER_CHUNK) # print(R[0:100]) self._analyze_run(assign_file, R)
def test_Avro2PBReader(self): """ Testing whether an avro file is read properly by Avro2PBReader """ test_util.test_protobuf_installed(self) in_avro = self.get_input_file_name("avro.sample") print("parsing", in_avro) a = IMP.npctransport.Avro2PBReader([in_avro]) o = IMP.npctransport.Output() s = a.read_next() while len(s) > 0: # while valid output o.ParseFromString(s) rg = o.statistics.fgs[0].radius_of_gyration # print rg s = a.read_next() self.assertAlmostEqual(rg, 40.2645193868, 7)
def test_init_from_old_output_more_recent(self): """ Testing whether a more recent output file (Dec 2013) is loaded at all """ return print("TEST_INIT_FROM_OLD_OUTPUT_MORE_RECENT") test_util.test_protobuf_installed(self) IMP.set_log_level(IMP.SILENT) rt_prev_output = self.get_input_file_name("out_more_recent.pb") out_rmf = self.get_tmp_file_name("movie.rmf") rt_new_output = self.get_tmp_file_name("out.pb") print("reloading from output file ", rt_prev_output) print("New Output: ", rt_new_output) sd = IMP.npctransport.SimulationData(rt_prev_output, False, out_rmf, rt_new_output) sd.activate_statistics() sd.set_rmf_file(out_rmf, False)
def test_init_from_output(self): """ Testing whether positions are loaded properly from output file """ print("TEST_INIT_FROM_OUTPUT") test_util.test_protobuf_installed(self) # random generator initialization IMP.set_log_level(IMP.SILENT) config = self.get_tmp_file_name("simple_cfg.pb") test_util.make_simple_cfg(config, is_slab_on=True, n_particles_factor=1.5, is_obstacles=True) rt_output = self.get_tmp_file_name( "round_trip_output.pb") # "./test.pb" # print("Config ", config) print("RT output: ", rt_output) sd = self.run_from_config(config, rt_output) print("reloading from output file ", rt_output) sdp = IMP.npctransport.SimulationData(rt_output, False) sdp.activate_statistics() sd.set_rmf_file(self.get_tmp_file_name("out1.rmf"), False) print("After reload", time.ctime()) self.assert_almost_equal_sds(sd, sdp)
def test_init_from_output(self): """ Testing whether positions are loaded properly from output file """ print("TEST_INIT_FROM_OUTPUT") test_util.test_protobuf_installed(self) # random generator initialization IMP.set_log_level(IMP.SILENT) config = self.get_tmp_file_name("simple_cfg.pb") test_util.make_simple_cfg( config, is_slab_on=True, n_particles_factor=1.5, is_obstacles=True) rt_output = self.get_tmp_file_name("round_trip_output.pb") # "./test.pb" # print("Config ", config) print("RT output: ", rt_output) sd = self.run_from_config(config, rt_output) print("reloading from output file ", rt_output) sdp = IMP.npctransport.SimulationData(rt_output, False) sdp.activate_statistics() sd.set_rmf_file( self.get_tmp_file_name("out1.rmf"), False ) print("After reload", time.ctime()) self.assert_almost_equal_sds(sd, sdp)
def test_sites_from_simulation_data(self): ''' Test that the site interaction glues particles together in the context of simualtion data optimization ''' # Prepare run: test_util.test_protobuf_installed(self) if IMP.get_check_level() >= IMP.USAGE_AND_INTERNAL: print("SLOW MODE") fast = False short_init_factor=0.0001 opt_cycles_ns=0.05 n_iter = 10 n_good_thresh=1 else: print("FAST MODE") fast = True short_init_factor=0.01 opt_cycles_ns=10.0 n_iter = 100 n_good_thresh=3 if(DEBUG): opt_cycles_ns=opt_cycles_ns*200 n_iter=1 cfg_file = "barak_config.pb" assign_file = "barak_assign.pb" pymol_file = "sites.pym" n_good_thresh=1 else: cfg_file = self.get_tmp_file_name("barak_config.pb") assign_file = self.get_tmp_file_name("barak_assign.pb") pymol_file = self.get_tmp_file_name("sites.pym") self._create_cfg_file_with_fg_anchors( cfg_file ) # print("assigning parameter ranges from config", cfg_file, end=' ') # print("to file", assign_file) num=assign_ranges( cfg_file, assign_file, 0, False, 10 ); sd= IMP.npctransport.SimulationData(assign_file, False) if(DEBUG): rmf_file="tmp.rmf" else: rmf_file = self.get_tmp_file_name("out.rmf"); print("RMF file", rmf_file) sd.set_rmf_file(rmf_file, False) self._assert_kap_in_place(sd, False) # Init and run: IMP.set_log_level(IMP.SILENT) sd.get_bd().set_log_level(IMP.SILENT) IMP.npctransport.initialize_positions( sd, [], False, short_init_factor) opt_cycles_frames=math.ceil(opt_cycles_ns*1E+6/sd.get_bd().get_maximum_time_step()); print() print() # IMP.set_log_level(IMP.PROGRESS) sd.write_geometry(pymol_file) n_good=0 timer= IMP.npctransport.timer(); sd.get_bd().set_current_time(0.0) sd.activate_statistics() sd.get_statistics().reset_statistics_optimizer_states() for i in range(n_iter): sd.get_bd().optimize(opt_cycles_frames) sd.get_statistics().update(timer,opt_cycles_frames) try: self._assert_kap_in_place(sd, True) print ("Asserted kap interacts") print ("total energy", sd.get_bd().get_scoring_function().evaluate(False),) print ("predr", sd.get_scoring().get_predicates_pair_restraint().evaluate(False)) self.assert_(sd.get_scoring().get_predicates_pair_restraint().evaluate(False) < -30.0) n_good=n_good+1 print("NGOOD", n_good) self.assert_(self.is_stats_interact_(assign_file)) print("stats interact asserted") except AssertionError: continue if(n_good >= n_good_thresh): print("total energy", sd.get_bd().get_scoring_function().evaluate(False), end=' ') print("predr", sd.get_scoring().get_predicates_pair_restraint().evaluate(False), end=' ') return True if fast: print("Failed to glue particles after %d iterations x %d opt frames (total %.3f ns)" \ % (n_iter, opt_cycles_frames, opt_cycles_ns*n_iter)) self.assert_(n_good >= n_good_thresh) else: print("Debug mode - couldn't glue particles in such short run")
def test_init_from_old_output1(self): """ Testing whether an old output file is loaded properly """ return print("TEST_INIT_FROM_OLD_OUTPUT1") test_util.test_protobuf_installed(self) expected_sites = [(3.67394e-15, 0, -30), (17.2447, -0.377296, -24.5455), (-1.55764, -23.0892, -19.0909), (-19.7675, 17.9804, -13.6364), (28.589, -3.96571, -8.18182), (-28.2097, -9.8375, -2.72727), (20.6227, 21.6163, 2.72727), (-4.64461, -28.4866, 8.18182), (-17.9874, 19.7612, 13.6364), (17.8043, 14.7833, 19.0909), (13.5084, 10.7259, 24.5455), (0, 0, 30)] expected_time = 12500039289 expected_particles = [ [-253.636, -108.652, 40.4134, 1, 0, 0, 0], #trans + quaternion [-236.08, -127.91, 27.47, 0.23, 0.33, -0.84, -0.36] ] # random generator initialization # RMF.set_log_level("trace") IMP.set_log_level(IMP.SILENT) rt_prev_output = self.get_input_file_name("out149.pb") out_rmf = self.get_tmp_file_name("movie.rmf") rt_new_output = self.get_tmp_file_name("out.pb") print("reloading from output file ", rt_prev_output) print("New Output: ", rt_new_output) sd = IMP.npctransport.SimulationData(rt_prev_output, False, out_rmf, rt_new_output) sd.activate_statistics() sd.set_rmf_file(out_rmf, False) for i, p in enumerate(sd.get_beads()): if (i >= len(expected_particles)): break pc = [x for x in IMP.core.XYZ(p).get_coordinates()] pq = [ x for x in IMP.core.RigidBody(p).get_reference_frame(). get_transformation_to().get_rotation().get_quaternion() ] for x, y in zip(pc + pq, expected_particles[i]): self.assertAlmostEqual(x, y, delta=0.1) # if(not IMP.npctransport.Transporting.get_is_setup(p)): # continue # pt = IMP.npctransport.Transporting(p) # print "T: ", pt.get_is_last_entry_from_top(), # print pt.get_last_tracked_z(), # print pt.get_n_entries_bottom(), # print pt.get_n_entries_top(), # print sites = sd.get_sites(IMP.core.ParticleType("kap")) for i, s in enumerate(sites): self.assertAlmostEqual( (s.get_center() - expected_sites[i]).get_magnitude(), 0, delta=.01) # check timers t = sd.get_bd().get_current_time() self.assertAlmostEqual(t, expected_time, delta=1)
def test_init_from_rmf(self): """ Testing whether initialize_positions_from_rmf indeed restores the beads coordinates correctly """ IMP.set_log_level( IMP.SILENT ); test_util.test_protobuf_installed(self) # First simulation with one seed: output_pb1= self.get_tmp_file_name( "output1.pb" ); output_rmf1= IMP.create_temporary_file_name( "output", ".rmf" ); print("Starting first simulation with RMF file " + output_rmf1) sd = self._make_and_run_simulation( output_pb1, output_rmf1, seed = 1) e1 = sd.get_bd().get_scoring_function().evaluate(False) print ("*** After first simulation") print ("Energy %.2f" % e1) coordsI = self._get_beads_coords( sd ) sd = None # Second simulation with another seed: output_pb2= self.get_tmp_file_name( "output2.pb" ); output_rmf2= IMP.create_temporary_file_name( "output2", ".rmf" ); print("*** Starting second simulation with RMF file " + output_rmf2) print("YYY") sd = self._make_and_run_simulation( output_pb2, output_rmf2, seed = 2) print("XXX") e2 = sd.get_bd().get_scoring_function().evaluate(False) print ("*** After second simulation") print ("Energy %.2f" % e2) coordsII = self._get_beads_coords( sd ) # Restoration from first simulation through rmf file: print("*** Initializing positions from RMF file " + output_rmf1) fl= RMF.open_rmf_file_read_only(output_rmf1) sd.initialize_positions_from_rmf( fl ) e3 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After initializing positions from RMF file " + output_rmf1) print("Energy %.2f" % e3) self.assertAlmostEqual(e1, e3, delta=0.0001) self.assertNotAlmostEqual(e1, e2, delta=0.0001) coordsIII = self._get_beads_coords( sd ) # make sure that all coordinates were restored and as sanity # check control, also that the second simulation is different than # the first one: for i in range( len( coordsI ) ): for j in range(3): self.assertAlmostEqual(coordsI[i][j], coordsIII[i][j], delta=0.00001) self.assertNotAlmostEqual(coordsI[i][j], coordsII[i][j], delta=0.00001) # Simulate more to scramble stuff sd.get_bd().optimize( 1000 ) e4 = sd.get_bd().get_scoring_function().evaluate(False) print ("Energy %.2f" % e4) coordsIV = self._get_beads_coords( sd ) # Restoration from first simulation through output protobuf file: print("*** Initializing positions from ProtoBuf file " + output_pb1) f=open(output_pb1, "rb") config= IMP.npctransport.Output() config.ParseFromString(f.read()) bch = RMF.BufferConstHandle( config.rmf_conformation ) fch= RMF.open_rmf_buffer_read_only( bch ) sd.initialize_positions_from_rmf( fch ) e5 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After initializing positions from RMF file " + output_rmf1) print("Energy %.2f" % e5) self.assertAlmostEqual(e1, e5, delta=0.00001) self.assertNotAlmostEqual(e1, e4, delta=0.00001) coordsV = self._get_beads_coords( sd ) for i in range( len( coordsI ) ): for j in range(3): self.assertAlmostEqual(coordsI[i][j], coordsV[i][j], delta=0.00001) self.assertNotAlmostEqual(coordsI[i][j], coordsIV[i][j], delta=0.00001)
def test_sites_from_simulation_data(self): ''' Test that the site interaction glues particles together in the context of simualtion data optimization ''' # Prepare run: test_util.test_protobuf_installed(self) if IMP.get_check_level() >= IMP.USAGE_AND_INTERNAL: print("SLOW MODE") fast = False short_init_factor = 0.0001 opt_cycles_ns = 0.05 n_iter = 10 n_good_thresh = 1 else: print("FAST MODE") fast = True short_init_factor = 0.01 opt_cycles_ns = 10.0 n_iter = 100 n_good_thresh = 3 if (DEBUG): opt_cycles_ns = opt_cycles_ns * 200 n_iter = 1 cfg_file = "barak_config.pb" assign_file = "barak_assign.pb" pymol_file = "sites.pym" n_good_thresh = 1 else: cfg_file = self.get_tmp_file_name("barak_config.pb") assign_file = self.get_tmp_file_name("barak_assign.pb") pymol_file = self.get_tmp_file_name("sites.pym") self._create_cfg_file_with_fg_anchors(cfg_file) # print("assigning parameter ranges from config", cfg_file, end=' ') # print("to file", assign_file) num = assign_ranges(cfg_file, assign_file, 0, False, 10) sd = IMP.npctransport.SimulationData(assign_file, False) if (DEBUG): rmf_file = "tmp.rmf" else: rmf_file = self.get_tmp_file_name("out.rmf") print("RMF file", rmf_file) sd.set_rmf_file(rmf_file, False) self._assert_kap_in_place(sd, False) # Init and run: IMP.set_log_level(IMP.SILENT) sd.get_bd().set_log_level(IMP.SILENT) IMP.npctransport.initialize_positions(sd, [], False, short_init_factor) opt_cycles_frames = math.ceil(opt_cycles_ns * 1E+6 / sd.get_bd().get_maximum_time_step()) print() print() # IMP.set_log_level(IMP.PROGRESS) sd.write_geometry(pymol_file) n_good = 0 timer = IMP.npctransport.timer() sd.get_bd().set_current_time(0.0) sd.activate_statistics() sd.get_statistics().reset_statistics_optimizer_states() for i in range(n_iter): sd.get_bd().optimize(opt_cycles_frames) sd.get_statistics().update(timer, opt_cycles_frames) try: self._assert_kap_in_place(sd, True) print("Asserted kap interacts") print( "total energy", sd.get_bd().get_scoring_function().evaluate(False), ) ppr = sd.get_scoring().get_predicates_pair_restraint() print("predr", ppr.evaluate(False)) self.assertLess(ppr.evaluate(False), -30.0) n_good = n_good + 1 print("NGOOD", n_good) self.assertTrue(self.is_stats_interact_(assign_file)) print("stats interact asserted") except AssertionError: continue if (n_good >= n_good_thresh): print("total energy", sd.get_bd().get_scoring_function().evaluate(False), end=' ') print( "predr", sd.get_scoring().get_predicates_pair_restraint().evaluate( False), end=' ') return True if fast: print("Failed to glue particles after %d iterations x %d opt frames (total %.3f ns)" \ % (n_iter, opt_cycles_frames, opt_cycles_ns*n_iter)) self.assertGreaterEqual(n_good, n_good_thresh) else: print("Debug mode - couldn't glue particles in such short run")
def test_init_from_old_output1(self): """ Testing whether an old output file is loaded properly """ return print("TEST_INIT_FROM_OLD_OUTPUT1") test_util.test_protobuf_installed(self) expected_sites = [ (3.67394e-15, 0, -30), (17.2447, -0.377296, -24.5455), (-1.55764, -23.0892, -19.0909), (-19.7675, 17.9804, -13.6364), (28.589, -3.96571, -8.18182), (-28.2097, -9.8375, -2.72727), (20.6227, 21.6163, 2.72727), (-4.64461, -28.4866, 8.18182), (-17.9874, 19.7612, 13.6364), (17.8043, 14.7833, 19.0909), (13.5084, 10.7259, 24.5455), (0, 0, 30) ] expected_time = 12500039289 expected_particles=[ [-253.636, -108.652, 40.4134, 1, 0, 0, 0], #trans + quaternion [-236.08, -127.91, 27.47, 0.23, 0.33, -0.84, -0.36] ] # random generator initialization # RMF.set_log_level("trace") IMP.set_log_level(IMP.SILENT) rt_prev_output = self.get_input_file_name("out149.pb") out_rmf = self.get_tmp_file_name("movie.rmf") rt_new_output = self.get_tmp_file_name("out.pb") print("reloading from output file ", rt_prev_output) print("New Output: ", rt_new_output) sd = IMP.npctransport.SimulationData(rt_prev_output, False, out_rmf, rt_new_output) sd.activate_statistics() sd.set_rmf_file(out_rmf, False) for i,p in enumerate(sd.get_beads()): if( i >= len(expected_particles) ): break pc= [x for x in IMP.core.XYZ(p).get_coordinates()] pq = [x for x in IMP.core.RigidBody(p ).get_reference_frame( ).get_transformation_to( ).get_rotation( ).get_quaternion() ] for x,y in zip(pc+pq, expected_particles[i]): self.assertAlmostEqual(x,y, delta=0.1) # if(not IMP.npctransport.Transporting.get_is_setup(p)): # continue # pt = IMP.npctransport.Transporting(p) # print "T: ", pt.get_is_last_entry_from_top(), # print pt.get_last_tracked_z(), # print pt.get_n_entries_bottom(), # print pt.get_n_entries_top(), # print sites = sd.get_sites(IMP.core.ParticleType("kap")) for i,s in enumerate(sites): self.assertAlmostEqual( ( s.get_center() - expected_sites[i]).get_magnitude(), 0, delta=.01 ) # check timers t = sd.get_bd().get_current_time() self.assertAlmostEqual(t, expected_time, delta=1)
def test_init_from_rmf(self): """ Testing whether initialize_positions_from_rmf indeed restores the beads coordinates correctly """ IMP.set_log_level(IMP.SILENT) test_util.test_protobuf_installed(self) # First simulation with one seed: output_pb1 = self.get_tmp_file_name("output1.pb") output_rmf1 = IMP.create_temporary_file_name("output", ".rmf") print("Starting first simulation with RMF file " + output_rmf1) sd = self._make_and_run_simulation(output_pb1, output_rmf1, seed=1) e1 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After first simulation") print("Energy %.2f" % e1) coordsI = self._get_beads_coords(sd) sd = None # Second simulation with another seed: output_pb2 = self.get_tmp_file_name("output2.pb") output_rmf2 = IMP.create_temporary_file_name("output2", ".rmf") print("*** Starting second simulation with RMF file " + output_rmf2) print("YYY") sd = self._make_and_run_simulation(output_pb2, output_rmf2, seed=2) print("XXX") e2 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After second simulation") print("Energy %.2f" % e2) coordsII = self._get_beads_coords(sd) # Restoration from first simulation through rmf file: print("*** Initializing positions from RMF file " + output_rmf1) fl = RMF.open_rmf_file_read_only(output_rmf1) sd.initialize_positions_from_rmf(fl) e3 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After initializing positions from RMF file " + output_rmf1) print("Energy %.2f" % e3) self.assertAlmostEqual(e1, e3, delta=0.0001) self.assertNotAlmostEqual(e1, e2, delta=0.0001) coordsIII = self._get_beads_coords(sd) # make sure that all coordinates were restored and as sanity # check control, also that the second simulation is different than # the first one: for i in range(len(coordsI)): for j in range(3): self.assertAlmostEqual(coordsI[i][j], coordsIII[i][j], delta=0.00001) self.assertNotAlmostEqual(coordsI[i][j], coordsII[i][j], delta=0.00001) # Simulate more to scramble stuff sd.get_bd().optimize(1000) e4 = sd.get_bd().get_scoring_function().evaluate(False) print("Energy %.2f" % e4) coordsIV = self._get_beads_coords(sd) # Restoration from first simulation through output protobuf file: print("*** Initializing positions from ProtoBuf file " + output_pb1) f = open(output_pb1, "rb") config = IMP.npctransport.Output() config.ParseFromString(f.read()) bch = RMF.BufferConstHandle(config.rmf_conformation) fch = RMF.open_rmf_buffer_read_only(bch) sd.initialize_positions_from_rmf(fch) e5 = sd.get_bd().get_scoring_function().evaluate(False) print("*** After initializing positions from RMF file " + output_rmf1) print("Energy %.2f" % e5) self.assertAlmostEqual(e1, e5, delta=0.00001) self.assertNotAlmostEqual(e1, e4, delta=0.00001) coordsV = self._get_beads_coords(sd) for i in range(len(coordsI)): for j in range(3): self.assertAlmostEqual(coordsI[i][j], coordsV[i][j], delta=0.00001) self.assertNotAlmostEqual(coordsI[i][j], coordsIV[i][j], delta=0.00001)