def test_safemode(self): fname = data_filename("cv_storage_safemode_test.nc") if os.path.isfile(fname): os.remove(fname) cv = paths.CoordinateFunctionCV('cv', lambda x: x) traj = paths.Trajectory(list(self.traj)) template = traj[0] storage_w = paths.Storage(fname, "w") storage_w.snapshots.save(template) storage_w.cvs.save(cv) storage_w.close() storage_r = paths.Storage(fname, 'r') # default safemode = False assert (storage_r.simplifier.safemode is False) cv_r = storage_r.cvs[0] assert (cv_r == cv) assert (cv.cv_callable is not None) storage_r.close() storage_r = paths.Storage(fname, 'r') storage_r.simplifier.safemode = True cv_r = storage_r.cvs[0] assert (cv_r == cv) assert (cv_r.cv_callable is None) storage_r.close()
def sequential_main(self): tps_storage = paths.Storage(self.tps_file, mode='r') # get initial conditions, and stick them in files for each task initial_snapshots = self.select_snapshots(tps_storage) engine = self.most_probable_engine(initial_snapshots) randomizer = create_randomizer(engine) if self.output: out_storage = paths.Storage(self.output, mode='w') out_storage.save(engine) out_storage.tags['randomizer'] = randomizer out_storage.save(paths.Trajectory(initial_snapshots)) task_files = self.write_task_files(initial_snapshots, engine, randomizer) # tps_storage.close() # this is the part that will be executed remotely result_list = [ file_snapshots_committor(task_file, self.n_per_snapshot) for task_file in task_files ] runs = self.create_individual_runs(tps_storage, result_list) tps_storage.close() if self.output: out_storage.tags['individual_runs'] = runs out_storage.close() return initial_snapshots, runs
def setup(self): if os.path.isfile(data_filename(self.fname)): os.remove(data_filename(self.fname)) setup_storage = paths.Storage(data_filename("tps_setup.nc"), "r") network = setup_storage.networks[0] tps_ensemble = network.sampling_ensembles[0] initial_sample = paths.Sample( replica=0, trajectory=common.initial_tps_sample.trajectory, ensemble=tps_ensemble ) template = initial_sample.trajectory[0] setup_storage.close() shoot = oink.ShootingStub(tps_ensemble) self.storage = paths.Storage(data_filename(self.fname), "w", template) self.pseudosim = oink.ShootingPseudoSimulator( storage=self.storage, initial_conditions=paths.SampleSet([initial_sample]), mover=shoot, network=network ) nojoin = oink.ShootingStub(tps_ensemble, pre_joined=False) self.nojoin_pseudosim = oink.ShootingPseudoSimulator( storage=self.storage, initial_conditions=paths.SampleSet([initial_sample]), mover=nojoin, network=network )
def test_cannot_guess(self): filename = self._filename('no-guess') storage = paths.Storage(filename, 'w') storage.save(self.traj) storage.save(self.other_traj) storage.close() storage = paths.Storage(filename, 'r') with pytest.raises(RuntimeError): self.PARAMETER.get(storage, None)
def test_storage_attribute_function(self): import os # test all combinations of (1) with and without UUIDs, # (2) using partial yes, no all of these must work for allow_incomplete in (True, False): # print('ALLOW INCOMPLETE', allow_incomplete) fname = data_filename("attr_storage_test.nc") if os.path.isfile(fname): os.remove(fname) traj = paths.Trajectory(list(self.traj_simple)) template = traj[0] storage_w = paths.Storage(fname, "w") storage_w.snapshots.save(template) storage_w.trajectories.save(traj) # compute distance in x[0] attr1 = FunctionPseudoAttribute( 'f1', paths.Trajectory, lambda x: x[0].coordinates[0] - x[-1].coordinates[ 0]).with_diskcache(allow_incomplete=allow_incomplete) storage_w.save(attr1) # let's mess up the order in which we save and # include reversed ones as well storage_w.trajectories.save(traj[3:]) storage_w.snapshots.save(traj[1].reversed) storage_w.trajectories.save(traj.reversed) # this should be ignored for all is saved already storage_w.trajectories.save(traj) storage_w.close() storage_r = paths.Storage(fname, 'r') rattr1 = storage_r.attributes['f1'] assert (rattr1._store_dict) attr_cache = rattr1._store_dict.value_store assert (attr_cache.allow_incomplete == allow_incomplete) for idx, traj in enumerate(storage_r.trajectories): if not allow_incomplete or attr_cache[traj] is not None: assert_close_unit(attr_cache[traj], attr1(traj)) storage_r.close() if os.path.isfile(fname): os.remove(fname)
def test_storage(self): self.storage.tag['simulation'] = self.simulation self.storage.close() read_store = paths.Storage(self.filename, 'r') sim = read_store.tag['simulation'] new_filename = data_filename("test2.nc") sim.storage = paths.Storage(new_filename, 'w') sim.output_stream = open(os.devnull, 'w') sim.run(n_per_snapshot=2) if os.path.isfile(new_filename): os.remove(new_filename)
def file_snapshots_committor(task_file_name, n_per_snapshot): """Run the committor for all snapshots in an input file. Returns ------- list tuples of ``(uuid, state_name)``, where ``uuid`` is the UUID of the original snapshot and ``state_name`` is the name of the final volume the shot landed in """ tmp_filename = "tmp_out_" + task_file_name task_file = paths.Storage(task_file_name, mode='r') states = task_file.tags['states'] initial_snapshots = task_file.tags['initial_snapshots'] storage = paths.Storage(tmp_filename, mode='w') sim = paths.CommittorSimulation(storage=storage, engine=task_file.tags['engine'], states=states, randomizer=task_file.tags['randomizer'], initial_snapshots=initial_snapshots) sim.output_stream.write("Running file: " + task_file_name + "\n") sim.run(n_per_snapshot) # now we convert the results to a format that can be returned over dask analyzer = paths.ShootingPointAnalysis(steps=None, states=states) key_to_init_uuid = { analyzer.hash_function(snap): snap.__uuid__ for snap in initial_snapshots } def final_state_name(step): state_list = analyzer.analyze_single_step(step) if len(state_list) > 1: raise RuntimeError("State definitions are overlapping!") return state_list[0].name def initial_snapshot_uuid(step): key = analyzer.step_key(step) return key_to_init_uuid[analyzer.hash_function(key)] # return pairs (initial_snapshot_uuid, final_state_name) # this is and (int, str) tuple for each step; should be trivial to # communicate back return_value = [(initial_snapshot_uuid(step), final_state_name(step)) for step in storage.steps] # cleanup task_file.close() storage.close() os.remove(tmp_filename) return return_value
def test_store_nonperiodic(self): testsystem = openmmtools.testsystems.AlanineDipeptideVacuum() snap = paths.engines.openmm.snapshot_from_testsystem(testsystem, periodic=False) storage = paths.Storage("test.nc", 'w') storage.save(snap) storage.close() load = paths.Storage("test.nc", 'r') reloaded = load.snapshots[0] npt.assert_array_equal(snap.coordinates, reloaded.coordinates) npt.assert_array_equal(snap.box_vectors, reloaded.box_vectors) assert snap.box_vectors is None assert reloaded.box_vectors is None
def test_storage(self): analyzer = paths.ChannelAnalysis(steps=None, channels=self.channels) analyzer._results = self.toy_results analyzer.treat_multiples = 'newest' storage = paths.Storage(data_filename('test.nc'), 'w') storage.tag['analyzer'] = analyzer storage.sync() storage.close() new_store = paths.Storage(data_filename('test.nc'), 'r') reloaded = storage.tag['analyzer'] assert_equal(reloaded._results, self.toy_results) if os.path.isfile(data_filename('test.nc')): os.remove(data_filename('test.nc'))
def test_md_main(md_fixture, inp): tempdir = tempfile.mkdtemp() try: store_name = os.path.join(tempdir, "md.nc") storage = paths.Storage(store_name, mode='w') engine, ens, snapshot = md_fixture if inp == 'nsteps': nsteps, ensembles = 5, None elif inp == 'ensemble': nsteps, ensembles = None, [ens] else: # -no-cov- raise RuntimeError("pytest went crazy") traj, foo = md_main(output_storage=storage, engine=engine, ensembles=ensembles, nsteps=nsteps, initial_frame=snapshot) assert isinstance(traj, paths.Trajectory) assert foo is None assert len(traj) == 5 assert len(storage.trajectories) == 1 storage.close() finally: os.remove(store_name) os.rmdir(tempdir)
def setup(self): # As a test system, let's use 1D motion on a flat potential. If the # velocity is positive, you right the state on the right. If it is # negative, you hit the state on the left. pes = toys.LinearSlope(m=[0.0], c=[0.0]) # flat line topology = toys.Topology(n_spatial=1, masses=[1.0], pes=pes) integrator = toys.LeapfrogVerletIntegrator(0.1) options = { 'integ': integrator, 'n_frames_max': 100000, 'n_steps_per_frame': 5 } self.engine = toys.Engine(options=options, topology=topology) self.snap0 = toys.Snapshot(coordinates=np.array([[0.0]]), velocities=np.array([[1.0]]), engine=self.engine) cv = paths.FunctionCV("Id", lambda snap : snap.coordinates[0][0]) self.left = paths.CVDefinedVolume(cv, float("-inf"), -1.0) self.right = paths.CVDefinedVolume(cv, 1.0, float("inf")) self.state_labels = {"Left" : self.left, "Right" : self.right, "None" : ~(self.left | self.right)} randomizer = paths.NoModification() self.filename = data_filename("committor_test.nc") self.storage = paths.Storage(self.filename, mode="w") self.storage.save(self.snap0) self.simulation = CommittorSimulation(storage=self.storage, engine=self.engine, states=[self.left, self.right], randomizer=randomizer, initial_snapshots=self.snap0) self.simulation.output_stream = open(os.devnull, 'w')
def test_storage(self): import os fname = data_filename("interface_set_storage_test.nc") if os.path.isfile(fname): os.remove(fname) template_traj = make_1d_traj([0.0]) template = template_traj[0] storage_w = paths.Storage(fname, "w") storage_w.save(template_traj) storage_w.save(self.increasing_set) storage_w.sync_all() storage_r = paths.AnalysisStorage(fname) reloaded = storage_r.interfacesets[0] assert_items_equal(reloaded.lambdas, self.increasing_set.lambdas) assert_equal(reloaded.period_min, self.increasing_set.period_min) assert_equal(reloaded.period_max, self.increasing_set.period_max) for (truth, beauty) in zip(self.increasing_set, reloaded): assert_equal(truth, beauty) for (v, l) in zip(reloaded.volumes, reloaded.lambdas): assert_equal(reloaded.get_lambda(v), l) if os.path.isfile(fname): os.remove(fname)
def create_file(self, getter): filename = self._filename(getter) storage = paths.Storage(filename, 'w') storage.save(self.traj) storage.save(self.other_traj) get_type, getter_style = self._parse_getter(getter) main, other = { 'traj': (self.traj, self.other_traj), 'sset': (self.sample_set, self.other_sample_set) }[get_type] if get_type == 'sset': storage.save(self.sample_set) storage.save(self.other_sample_set) tag, other_tag = { 'name': ('traj', None), 'number': (None, None), 'tag-final': ('final_conditions', 'initial_conditions'), 'tag-initial': ('initial_conditions', None) }[getter_style] if tag: storage.tags[tag] = main if other_tag: storage.tags[other_tag] = other storage.close() return filename
def test_contents_table_error(): runner = CliRunner() with runner.isolated_filesystem(): storage = paths.Storage("temp.nc", mode='w') storage.close() results = runner.invoke(contents, ['temp.nc', '--table', 'foo']) assert results.exit_code != 0
def _getter_test(self, getter): test_file = self.create_file(getter) storage = paths.Storage(self._filename(getter), mode='r') get_arg = self.get_arg[getter] obj = self.PARAMETER.get(storage, get_arg) assert obj.__uuid__ == self.obj.__uuid__ assert obj == self.obj
def test_contents(tps_fixture): # we just do a full integration test of this one scheme, network, engine, init_conds = tps_fixture runner = CliRunner() with runner.isolated_filesystem(): storage = paths.Storage("setup.nc", 'w') for obj in tps_fixture: storage.save(obj) storage.tags['initial_conditions'] = init_conds results = runner.invoke(contents, ['setup.nc']) cwd = os.getcwd() expected = [ f"Storage @ '{cwd}/setup.nc'", "CVs: 1 item", "* x", "Volumes: 8 items", "* A", "* B", "* plus 6 unnamed items", "Engines: 2 items", "* flat", "* plus 1 unnamed item", "Networks: 1 item", "* 1 unnamed item", "Move Schemes: 1 item", "* 1 unnamed item", "Simulations: 0 items", "Tags: 1 item", "* initial_conditions", "", "Data Objects:", "Steps: 0 unnamed items", "Move Changes: 0 unnamed items", "SampleSets: 1 unnamed item", "Trajectories: 1 unnamed item", f"Snapshots: {2*len(init_conds[0])} unnamed items", "" ] assert_click_success(results) assert results.output.split('\n') == expected for truth, beauty in zip(expected, results.output.split('\n')): assert truth == beauty
def test_contents_table(tps_fixture, table): scheme, network, engine, init_conds = tps_fixture runner = CliRunner() with runner.isolated_filesystem(): storage = paths.Storage("setup.nc", 'w') for obj in tps_fixture: storage.save(obj) storage.tags['initial_conditions'] = init_conds results = runner.invoke(contents, ['setup.nc', '--table', table]) cwd = os.getcwd() expected = { 'volumes': [ f"Storage @ '{cwd}/setup.nc'", "volumes: 8 items", "* A", "* B", "* plus 6 unnamed items", "" ], 'trajectories': [ f"Storage @ '{cwd}/setup.nc'", "trajectories: 1 unnamed item", "" ], 'tags': [ f"Storage @ '{cwd}/setup.nc'", "tags: 1 item", "* initial_conditions", "" ], }[table] assert results.output.split("\n") == expected assert_click_success(results)
def setup(self): # As a test system, let's use 1D motion on a flat potential. If the # velocity is positive, you right the state on the right. If it is # negative, you hit the state on the left. pes = toys.LinearSlope(m=[0.0], c=[0.0]) # flat line topology = toys.Topology(n_spatial=1, masses=[1.0], pes=pes) integrator = toys.LeapfrogVerletIntegrator(0.1) options = { 'integ': integrator, 'n_frames_max': 100000, 'n_steps_per_frame': 5 } self.engine = toys.Engine(options=options, topology=topology) self.snap0 = toys.Snapshot(coordinates=np.array([[0.0]]), velocities=np.array([[1.0]]), engine=self.engine) cv = paths.FunctionCV("Id", lambda snap: snap.coordinates[0][0]) starting_volume = paths.CVDefinedVolume(cv, -0.01, 0.01) forward_ensemble = paths.LengthEnsemble(5) backward_ensemble = paths.LengthEnsemble(3) randomizer = paths.NoModification() self.filename = data_filename("shoot_from_snaps.nc") self.storage = paths.Storage(self.filename, 'w') self.simulation = ShootFromSnapshotsSimulation( storage=self.storage, engine=self.engine, starting_volume=starting_volume, forward_ensemble=forward_ensemble, backward_ensemble=backward_ensemble, randomizer=randomizer, initial_snapshots=self.snap0) self.simulation.output_stream = open(os.devnull, "w")
def test_storage(self): plmd = PLUMEDInterface(self.topology) dist = PLUMEDCV("dist", plmd, "DISTANCE ATOMS=7,9") store = paths.Storage("test.nc", "w") store.save(self.trajectory[:2]) store.save(dist) store.sync() store.close() store2 = paths.Storage('test.nc', 'r') dist2 = store2.cvs[dist.name] store2.close() np.testing.assert_almost_equal(dist(self.trajectory), dist2(self.trajectory), decimal=6)
def setup(self): test_dir = "one_way_tps_examples" self.data_filename = lambda f : \ data_filename(os.path.join(test_dir, f)) old_store = paths.Storage(data_filename("tps_setup.nc"), "r") self.network = old_store.networks[0] tps_ensemble = self.network.sampling_ensembles[0] self.shoot = oink.ShootingStub(tps_ensemble, pre_joined=False) self.converter = StupidOneWayTPSConverter( storage=paths.Storage(self.data_filename("output.nc"), "w"), initial_file="file0.data", mover=self.shoot, network=self.network, options=oink.TPSConverterOptions(includes_shooting_point=False, trim=False)) old_store.close()
def test_storage(self): plmd = PLUMEDInterface(self.topology) plmd.set("", "UNITS LENGTH=nm") plmd.set( "", "MOLINFO STRUCTURE=" + data_filename("plumed_wrapper/AD_initial_frame.pdb")) store = paths.Storage("test.nc", "w") store.save(self.trajectory[:2]) store.tags['a'] = plmd store.sync() store.close() store2 = paths.Storage('test.nc', 'r') plmd2 = store2.tags['a'] store2.close() np.testing.assert_array_equal(plmd.get(), plmd2.get())
def test_store_and_reload(self): if os.path.isfile(data_filename("output.nc")): os.remove(data_filename("output.nc")) storage = paths.Storage(data_filename("output.nc"), 'w') annotated = AnnotatedTrajectory(self.traj, self.annotations) storage.tag['traj1'] = annotated storage.close() analysis = paths.Storage(data_filename("output.nc"), 'r') reloaded = analysis.tag['traj1'] assert len(reloaded.trajectory) == 13 assert len(reloaded.annotations) == 4 self._check_standard_annotated_trajectory(reloaded) if os.path.isfile(data_filename("output.nc")): os.remove(data_filename("output.nc"))
def make_input_file(tps_network_and_traj): input_file = paths.Storage("setup.nc", mode='w') for obj in tps_network_and_traj: input_file.save(obj) input_file.tags['template'] = input_file.snapshots[0] input_file.close() return "setup.nc"
def setup(self): # PES is one-dimensional linear slope (y(x) = x) pes = toys.LinearSlope(m=[-1.0], c=[0.0]) # one particle with mass 1.0 topology = toys.Topology(n_spatial=1, masses=[1.0], pes=pes) integrator = toys.LeapfrogVerletIntegrator(0.02) options = { 'integ': integrator, 'n_frames_max': 1000, 'n_steps_per_frame': 5 } self.engine = toys.Engine(options=options, topology=topology) # test uses three snapshots with different velocities # 0: direction ok, velocity too low => falls back to dividing surface # 1: wrong direction => backward shot towards B # 2: direction ok, velocity high enough => successfull new trajectory self.initial_snapshots = [ toys.Snapshot(coordinates=np.array([[0.0]]), velocities=np.array([[1.0]]), engine=self.engine), toys.Snapshot(coordinates=np.array([[0.0]]), velocities=np.array([[-1.0]]), engine=self.engine), toys.Snapshot(coordinates=np.array([[0.0]]), velocities=np.array([[2.0]]), engine=self.engine) ] # reaction coordinate is just x coordinate rc = paths.FunctionCV("Id", lambda snap: snap.coordinates[0][0]) # state A: [-inf, -1] self.state_A = paths.CVDefinedVolume(rc, float("-inf"), -1.0) # area between A and dividing surface: [-1, 0] self.towards_A = paths.CVDefinedVolume(rc, -1.0, 0.0) # state B: [1, inf] self.state_B = paths.CVDefinedVolume(rc, 1.0, float("inf")) # define state labels self.state_labels = { "A": self.state_A, "B": self.state_B, "ToA": self.towards_A, "None": ~(self.state_A | self.state_B | self.towards_A) } # velocities are not randomized randomizer = paths.NoModification() self.filename = data_filename("rf_test.nc") self.storage = paths.Storage(self.filename, mode="w") self.storage.save(self.initial_snapshots) self.simulation = ReactiveFluxSimulation( storage=self.storage, engine=self.engine, states=[self.state_A, self.state_B], randomizer=randomizer, initial_snapshots=self.initial_snapshots, rc=rc) self.simulation.output_stream = open(os.devnull, 'w')
def test_mention_only(self): storage_w = paths.Storage(self.filename, "w") template = self.template_snapshot storage_w.snapshots.add_type(template) test_snap = self.traj[2] # only touch a new snapshot storage_w.snapshots.only_mention = True storage_w.snapshots.save(test_snap) # check that the snapshot is there assert (len(storage_w.snapshots) == 4) # in the memory uuid index assert (test_snap.__uuid__ in storage_w.snapshots.index) # and stored assert (test_snap.__uuid__ == storage_w.snapshots.vars['uuid'][1]) # but no real snapshot has been stored # print len(storage_w.objects['snapshot0']) assert (len(storage_w.objects['snapshot0']) == 2) # switch on normal saving storage_w.snapshots.only_mention = False test_snap = self.traj[4] storage_w.snapshots.mention(test_snap) # check that the snapshot is there assert (len(storage_w.snapshots) == 6) # in the memory uuid index assert (test_snap.__uuid__ in storage_w.snapshots.index) # and stored assert (test_snap.__uuid__ == storage_w.snapshots.vars['uuid'][2]) # but no real snapshot has been stored assert (len(storage_w.objects['snapshot0']) == 2) # try to now add it storage_w.snapshots.save(test_snap) # check that the snapshot is not stored again (only 3 snapshots) assert (len(storage_w.snapshots) == 6) assert (len(storage_w.objects['snapshot0']) == 4) # print storage_w.objects['snapshot0'][1].coordinates # print template.coordinates # print storage_w.objects['snapshot0'][0].coordinates # print test_snap.coordinates # print storage_w.objects['snapshot0'].vars['statics'][0].coordinates # print storage_w.objects['snapshot0'].vars['statics'][1].coordinates # print storage_w.objects['snapshot0'].index compare_snapshot(storage_w.objects['snapshot0'][4], test_snap) storage_w.close()
def create_file(self, getter): filename = self._filename(getter) storage = paths.Storage(filename, 'w') storage.save(self.other_snap) if getter != 'none-num': storage.save(self.init_snap) storage.tags['initial_snapshot'] = self.init_snap storage.close() return filename
def setup(self): self.tmpdir = tempfile.mkdtemp() self.storage_filename = os.path.join(self.tmpdir, "test.nc") self.storage = paths.Storage(self.storage_filename, mode='w') snap = make_1d_traj([1])[0] self.storage.save(snap) self.cv_x = paths.CoordinateFunctionCV("x", lambda s: s.xyz[0][0]) self.cv_y = paths.CoordinateFunctionCV("y", lambda s: s.xyz[0][1]) self.storage.save([self.cv_x, self.cv_y])
def _workaround(self, name): # this is messed up... for some reason, storage doesn't create a new # file in append mode. That may be a bug import openpathsampling as paths needs_workaround = (self.mode == 'a' and not os.path.exists(name) and not self._is_simstore(name)) if needs_workaround: st = paths.Storage(name, mode='w') st.close()
def test_pathsampling_main(tps_fixture): scheme, _, _, init_conds = tps_fixture with CliRunner().isolated_filesystem(): storage = paths.Storage("tps.nc", mode='w') final, sim = pathsampling_main(storage, scheme, init_conds, 10) assert isinstance(final, paths.SampleSet) assert isinstance(sim, paths.PathSampling) assert len(storage.steps) == 11 assert len(storage.schemes) == 1
def test_save_initial_scheme(self, tmpdir): # check that we actually save scheme when we save this filename = tmpdir.join("temp.nc") storage = paths.Storage(str(filename), mode='w') assert len(storage.schemes) == 0 sim = paths.PathSampling(storage=storage, move_scheme=self.scheme, sample_set=self.init_cond) assert len(storage.schemes) == 1