class TestOptimCollagen(unittest.TestCase): """test a known value for a large database""" def setUp(self): from pele.utils.optim_compatibility import OptimDBConverter from pele.storage import Database ndof = 10 # wrong, but who cares. self.db = Database() current_dir = os.path.dirname(__file__) converter = OptimDBConverter(self.db, ndof=ndof, mindata=current_dir+"/collagen.min.data", tsdata=current_dir+"/collagen.ts.data", assert_coords=False) converter.convert_no_coords() def test1(self): m1 = self.db.getMinimum(1) m2 = self.db.getMinimum(2) m3 = self.db.getMinimum(3) m4 = self.db.getMinimum(4) rcalc = RateCalculation(self.db.transition_states(), [m1], [m2], T=0.592) rcalc.compute_rates() self.assertAlmostEqual(rcalc.get_rate_AB(), 7106337458., delta=1e4) self.assertAlmostEqual(rcalc.get_rate_BA(), 1955395816., delta=1e4) rcalc = RateCalculation(self.db.transition_states(), [m1,m3], [m2, m4], T=0.592) rcalc.compute_rates() self.assertAlmostEqual(rcalc.get_rate_AB(), 8638736600., delta=1e4) self.assertAlmostEqual(rcalc.get_rate_BA(), 3499625167., delta=1e4) rla = RatesLinalg(self.db.transition_states(), [m1], [m2], T=0.592) rAB = rla.compute_rates() self.assertAlmostEqual(rAB, 7106337458., delta=1e4) rla = RatesLinalg(self.db.transition_states(), [m1, m3], [m2, m4], T=0.592) rAB = rla.compute_rates() self.assertAlmostEqual(rAB, 8638736600., delta=1e4)
class TestDB(unittest.TestCase): def setUp(self): self.db = Database() self.nminima = 10 for i in range(self.nminima): e = float(i) self.db.addMinimum(e, [e]) self.nts = 3 self.db.addTransitionState(0., [0.], self.db.minima()[0], self.db.minima()[1], eigenval=0., eigenvec=[0.]) self.db.addTransitionState(0., [0.], self.db.minima()[1], self.db.minima()[2], eigenval=0., eigenvec=[0.]) self.db.addTransitionState(0., [0.], self.db.minima()[0], self.db.minima()[2], eigenval=0., eigenvec=[0.]) def test_size(self): self.assertEqual(len(self.db.minima()), self.nminima) def test_energy(self): m = self.db.minima()[0] self.assertEqual(m.energy, 0.) def test_coords(self): m = self.db.minima()[0] self.assertEqual(m.coords, [0.]) def test_sizets(self): self.assertEqual(len(self.db.transition_states()), self.nts) def test_energyts(self): ts = self.db.transition_states()[0] self.assertEqual(ts.energy, 0.) def test_coordsts(self): ts = self.db.transition_states()[0] self.assertEqual(ts.coords, [0.]) def test_remove_minimum(self): m = self.db.minima()[0] self.db.removeMinimum(m) self.assertEqual(len(self.db.minima()), self.nminima-1) self.assertNotIn(m, self.db.minima()) # m should have 2 minima. both of those should be gone self.assertEqual(len(self.db.transition_states()), self.nts-2) def test_remove_ts(self): ts = self.db.transition_states()[0] self.db.remove_transition_state(ts) self.assertEqual(self.db.number_of_transition_states(), self.nts-1) self.assertNotIn(ts, self.db.transition_states()) # m should have 2 minima. both of those should be gone self.assertEqual(self.db.number_of_minima(), self.nminima) def test_getTransitionState(self): m1 = self.db.minima()[0] m2 = self.db.minima()[1] m3 = self.db.minima()[-1] self.assertIsNotNone(self.db.getTransitionState(m1, m2)) self.assertIsNone(self.db.getTransitionState(m1, m3)) def test_getMinimum(self): m = self.db.minima()[0] self.assertEqual(m, self.db.getMinimum(m._id)) def test_minimum_adder(self): ma = self.db.minimum_adder() ma(101., [101.]) self.assertEqual(len(self.db.minima()), self.nminima+1) def test_merge_minima(self): m1 = self.db.minima()[0] m2 = self.db.minima()[1] self.db.mergeMinima(m1, m2) self.assertEqual(len(self.db.minima()), self.nminima-1) # transition states shouldn't be deleted self.assertEqual(len(self.db.transition_states()), self.nts) def test_number_of_minima(self): self.assertEqual(self.nminima, self.db.number_of_minima()) def test_number_of_transition_states(self): self.assertEqual(self.nts, self.db.number_of_transition_states()) def test_highest_energy_minimum(self): m1 = self.db._highest_energy_minimum() m2 = self.db.minima()[-1] self.assertEqual(m1, m2) def test_maximum_number_of_minima(self): m = self.db.addMinimum(-1., [-1.], max_n_minima=self.nminima) self.assertEqual(self.nminima, self.db.number_of_minima()) self.assertIn(m, self.db.minima()) def test_maximum_number_of_minima_largestE(self): e = float(self.nminima + 1) m = self.db.addMinimum(e, [e], max_n_minima=self.nminima) self.assertEqual(self.nminima, self.db.number_of_minima()) self.assertIsNone(m) #ensure the highest energy minimum is still in the database mmax = self.db._highest_energy_minimum() self.assertEqual(mmax.energy, float(self.nminima-1)) def test_maximum_number_of_minima_minima_adder(self): ma = self.db.minimum_adder(max_n_minima=self.nminima) m = ma(-1., [-1.]) self.assertEqual(self.nminima, self.db.number_of_minima()) self.assertIn(m, self.db.minima()) def test_getTSfromID(self): ts = self.db.transition_states()[0] ts1 = self.db.getTransitionStateFromID(ts._id) self.assertEqual(ts, ts1) def test_property(self): # add some system properties and ensure they were added correctly self.db.add_property("natoms", 10) p = self.db.get_property("natoms") self.assertEqual(p.value(), 10) self.db.add_property("eps", 2.1) p = self.db.get_property("eps") self.assertEqual(p.value(), 2.1) self.db.add_property("author", "Jake") p = self.db.get_property("author") self.assertEqual(p.value(), "Jake") self.db.add_property("data", [1, 2]) p = self.db.get_property("data") self.assertEqual(p.value(), [1, 2]) # assert that the not set values are None p = self.db.get_property("natoms") self.assertIsNone(p.string_value) self.assertIsNone(p.float_value) self.assertIsNone(p.pickle_value) p = self.db.get_property("noprop") self.assertIsNone(p) props = self.db.properties(as_dict=True) self.assertIsInstance(props, dict) self.assertDictContainsSubset(dict(natoms=10), props) self.assertEqual(len(props.items()), 4) props = self.db.properties(as_dict=False) self.assertIn(("natoms", 10), [p.item() for p in props]) self.assertEqual(len(props), 4) def test_property_dtype(self): # add some system properties and ensure they were added correctly self.db.add_property("natoms", 10, dtype="int") p = self.db.get_property("natoms") self.assertEqual(p.value(), 10) self.db.add_property("eps", 2.1, dtype="float") p = self.db.get_property("eps") self.assertEqual(p.value(), 2.1) self.db.add_property("author", "Jake", dtype="string") p = self.db.get_property("author") self.assertEqual(p.value(), "Jake") self.db.add_property("data", [1, 2], dtype="pickle") p = self.db.get_property("data") self.assertEqual(p.value(), [1, 2]) def test_bad_property(self): self.db.add_property("natoms", 10, dtype="string") p = self.db.get_property("natoms") self.assertEqual(p.value(), "10") def test_property_overwrite_false(self): self.db.add_property("natoms", 10) # overwriting with the same value should not raise error self.db.add_property("natoms", 10, overwrite=False) # overwriting with a different value should raise error with self.assertRaises(RuntimeError): self.db.add_property("natoms", 11, overwrite=False) def test_add_properties(self): props = dict(natoms=10, author="jake") self.db.add_properties(props) for name, value in props.iteritems(): p = self.db.get_property(name) self.assertEqual(p.value(), value) def test_load_wrong_schema(self): current_dir = os.path.dirname(__file__) dbname = current_dir + "/lj6_schema1.sqlite" with self.assertRaises(IOError): db = Database(dbname, createdb=False) def test_load_right_schema(self): current_dir = os.path.dirname(__file__) dbname = current_dir + "/lj6_schema2.sqlite" db = Database(dbname, createdb=False) def test_invalid(self): m = self.db.minima()[0] self.assertFalse(m.invalid) m.invalid = True self.db.session.commit() self.assertTrue(m.invalid) # now with a ts m = self.db.transition_states()[0] self.assertFalse(m.invalid) m.invalid = True self.db.session.commit() self.assertTrue(m.invalid) def test_user_data(self): m = self.db.minima()[0] m.user_data = dict(key="value") self.db.session.commit() v = m.user_data["key"] # now with a transition state m = self.db.transition_states()[0] m.user_data = dict(key="value") self.db.session.commit() v = m.user_data["key"]
class TestDB(unittest.TestCase): def setUp(self): self.db = Database() self.nminima = 10 for i in range(self.nminima): e = float(i) self.db.addMinimum(e, [e]) self.nts = 3 self.db.addTransitionState(0., [0.], self.db.minima()[0], self.db.minima()[1], eigenval=0., eigenvec=[0.]) self.db.addTransitionState(0., [0.], self.db.minima()[1], self.db.minima()[2], eigenval=0., eigenvec=[0.]) self.db.addTransitionState(0., [0.], self.db.minima()[0], self.db.minima()[2], eigenval=0., eigenvec=[0.]) def test_add_minimum(self): # add a duplicate minimum and ensure the db doesn't change self.db.addMinimum(0, [0]) self.assertEqual(self.db.number_of_minima(), self.nminima) def test_size(self): self.assertEqual(len(self.db.minima()), self.nminima) def test_energy(self): m = self.db.minima()[0] self.assertEqual(m.energy, 0.) def test_coords(self): m = self.db.minima()[0] self.assertEqual(m.coords, [0.]) def test_sizets(self): self.assertEqual(len(self.db.transition_states()), self.nts) def test_energyts(self): ts = self.db.transition_states()[0] self.assertEqual(ts.energy, 0.) def test_coordsts(self): ts = self.db.transition_states()[0] self.assertEqual(ts.coords, [0.]) def test_remove_minimum(self): m = self.db.minima()[0] self.db.removeMinimum(m) self.assertEqual(len(self.db.minima()), self.nminima - 1) self.assertNotIn(m, self.db.minima()) # m should have 2 minima. both of those should be gone self.assertEqual(len(self.db.transition_states()), self.nts - 2) def test_remove_ts(self): ts = self.db.transition_states()[0] self.db.remove_transition_state(ts) self.assertEqual(self.db.number_of_transition_states(), self.nts - 1) self.assertNotIn(ts, self.db.transition_states()) # m should have 2 minima. both of those should be gone self.assertEqual(self.db.number_of_minima(), self.nminima) def test_getTransitionState(self): m1 = self.db.minima()[0] m2 = self.db.minima()[1] m3 = self.db.minima()[-1] self.assertIsNotNone(self.db.getTransitionState(m1, m2)) self.assertIsNone(self.db.getTransitionState(m1, m3)) def test_getMinimum(self): m = self.db.minima()[0] self.assertEqual(m, self.db.getMinimum(m._id)) def test_minimum_adder(self): ma = self.db.minimum_adder() ma(101., [101.]) self.assertEqual(len(self.db.minima()), self.nminima + 1) def test_minimum_adder_Ecut(self): ma = self.db.minimum_adder(Ecut=0) n0 = self.db.number_of_minima() ma(101., [101.]) self.assertEqual(n0, self.db.number_of_minima()) ma(-101., [-101.]) self.assertEqual(n0 + 1, self.db.number_of_minima()) def test_minimum_adder_commit_interval(self): ma = self.db.minimum_adder(commit_interval=2) # replace db.session.commit with a wrapper that keeps track of how # many times it's been called self.real_commit = self.db.session.commit self.count = 0 def commit_wrapper(): self.count += 1 self.real_commit() self.db.session.commit = commit_wrapper # commit should be called for the first minimum ma(101., [101.]) self.assertEqual(self.count, 1) self.assertEqual(self.nminima + 1, self.db.number_of_minima()) # commit should not be called for the second minimum ma(102., [102.]) self.assertEqual(self.count, 1) self.assertEqual(self.nminima + 2, self.db.number_of_minima()) # yes for the third, no for the 4th ma(103., [103.]) self.assertEqual(self.count, 2) ma(104., [104.]) self.assertEqual(self.count, 2) # commit should be called when minimum adder is deleted del ma self.assertEqual(self.count, 3) self.assertEqual(self.nminima + 4, self.db.number_of_minima()) def test_merge_minima(self): m1 = self.db.minima()[0] m2 = self.db.minima()[1] self.db.mergeMinima(m1, m2) self.assertEqual(len(self.db.minima()), self.nminima - 1) # transition states shouldn't be deleted self.assertEqual(len(self.db.transition_states()), self.nts) def test_number_of_minima(self): self.assertEqual(self.nminima, self.db.number_of_minima()) def test_number_of_transition_states(self): self.assertEqual(self.nts, self.db.number_of_transition_states()) def test_highest_energy_minimum(self): m1 = self.db._highest_energy_minimum() m2 = self.db.minima()[-1] self.assertEqual(m1, m2) def test_maximum_number_of_minima(self): m = self.db.addMinimum(-1., [-1.], max_n_minima=self.nminima) self.assertEqual(self.nminima, self.db.number_of_minima()) self.assertIn(m, self.db.minima()) def test_maximum_number_of_minima_largestE(self): e = float(self.nminima + 1) m = self.db.addMinimum(e, [e], max_n_minima=self.nminima) self.assertEqual(self.nminima, self.db.number_of_minima()) self.assertIsNone(m) #ensure the highest energy minimum is still in the database mmax = self.db._highest_energy_minimum() self.assertEqual(mmax.energy, float(self.nminima - 1)) def test_maximum_number_of_minima_minima_adder(self): ma = self.db.minimum_adder(max_n_minima=self.nminima) m = ma(-1., [-1.]) self.assertEqual(self.nminima, self.db.number_of_minima()) self.assertIn(m, self.db.minima()) def test_getTSfromID(self): ts = self.db.transition_states()[0] ts1 = self.db.getTransitionStateFromID(ts._id) self.assertEqual(ts, ts1) def test_property(self): # add some system properties and ensure they were added correctly self.db.add_property("natoms", 10) p = self.db.get_property("natoms") self.assertEqual(p.value(), 10) self.db.add_property("eps", 2.1) p = self.db.get_property("eps") self.assertEqual(p.value(), 2.1) self.db.add_property("author", "Jake") p = self.db.get_property("author") self.assertEqual(p.value(), "Jake") self.db.add_property("data", [1, 2]) p = self.db.get_property("data") self.assertEqual(p.value(), [1, 2]) # assert that the not set values are None p = self.db.get_property("natoms") self.assertIsNone(p.string_value) self.assertIsNone(p.float_value) self.assertIsNone(p.pickle_value) p = self.db.get_property("noprop") self.assertIsNone(p) props = self.db.properties(as_dict=True) self.assertIsInstance(props, dict) self.assertDictContainsSubset(dict(natoms=10), props) self.assertEqual(len(list(props.items())), 4) props = self.db.properties(as_dict=False) self.assertIn(("natoms", 10), [p.item() for p in props]) self.assertEqual(len(props), 4) def test_property_dtype(self): # add some system properties and ensure they were added correctly self.db.add_property("natoms", 10, dtype="int") p = self.db.get_property("natoms") self.assertEqual(p.value(), 10) self.db.add_property("eps", 2.1, dtype="float") p = self.db.get_property("eps") self.assertEqual(p.value(), 2.1) self.db.add_property("author", "Jake", dtype="string") p = self.db.get_property("author") self.assertEqual(p.value(), "Jake") self.db.add_property("data", [1, 2], dtype="pickle") p = self.db.get_property("data") self.assertEqual(p.value(), [1, 2]) def test_bad_property(self): self.db.add_property("natoms", 10, dtype="string") p = self.db.get_property("natoms") self.assertEqual(p.value(), "10") def test_property_overwrite_false(self): self.db.add_property("natoms", 10) # overwriting with the same value should not raise error self.db.add_property("natoms", 10, overwrite=False) # overwriting with a different value should raise error with self.assertRaises(RuntimeError): self.db.add_property("natoms", 11, overwrite=False) def test_add_properties(self): props = dict(natoms=10, author="jake") self.db.add_properties(props) for name, value in props.items(): p = self.db.get_property(name) self.assertEqual(p.value(), value) def test_load_wrong_schema(self): current_dir = os.path.dirname(__file__) dbname = current_dir + "/lj6_schema1.sqlite" with self.assertRaises(IOError): db = Database(dbname, createdb=False) def test_load_right_schema(self): current_dir = os.path.dirname(__file__) dbname = current_dir + "/lj6_schema2.sqlite" db = Database(dbname, createdb=False) def test_invalid(self): m = self.db.minima()[0] self.assertFalse(m.invalid) m.invalid = True self.db.session.commit() self.assertTrue(m.invalid) # now with a ts m = self.db.transition_states()[0] self.assertFalse(m.invalid) m.invalid = True self.db.session.commit() self.assertTrue(m.invalid) def test_user_data(self): m = self.db.minima()[0] m.user_data = dict(key="value") self.db.session.commit() v = m.user_data["key"] # now with a transition state m = self.db.transition_states()[0] m.user_data = dict(key="value") self.db.session.commit() v = m.user_data["key"]
def main(): if len(sys.argv) < 2: usage() exit(1) # The default is to use the largest connected component of the graph, # rather than the component which includes the global minimum. kwargs = {"include_gmin":False, "center_gmin":False} outfile, colourfile, groupcolourfile, idfile = None, None, None, None aspect = 6.0/7.0 width = 6 OPTIM = False opts, args = getopt.gnu_getopt(sys.argv[1:], "ho:", ["help", "nlevels=", "subgraph_size=", "OPTIM", "order_by_basin_size", "order_by_energy", "include_gmin", "center_gmin", "Emax=", "colourfile=", "groupcolourfile=", "idfile=", "shape=", "width=" ]) for o, a in opts: if o == "-h" or o == "--help": usage() exit(1) if o == "-o": outfile = a elif o == "--nlevels": kwargs["nlevels"] = int(a) elif o == "--Emax": kwargs["Emax"] = float(a) elif o == "--subgraph_size": kwargs["subgraph_size"] = int(a) elif o == "--order_by_basin_size": kwargs["order_by_basin_size"] = True elif o == "--order_by_energy": kwargs["order_by_energy"] = True elif o == "--include_gmin": kwargs["include_gmin"] = True elif o == "--center_gmin": kwargs["center_gmin"] = True elif o == "--OPTIM": OPTIM = True elif o == "--colourfile": colourfile = a print "Setting colourfile to ", colourfile elif o == "--groupcolourfile": if colourfile: raise AttributeError("Can't specify both colourfile and groupcolourfile") groupcolourfile = a elif o == '--idfile': idfile = a elif o == '--shape': aspect = float(a) elif o == '--width': width = float(a) else: print "don't understand", o, a print "" usage() exit(1) groups = None if OPTIM: #make database from min.data ts.data db = Database() converter = OptimDBConverter(db) converter.convert_no_coords() groups = read_AB(db) else: if len(args) == 0: print "you must specify database file" print "" usage() exit() dbfile = args[0] if not os.path.exists(dbfile): print "database file doesn't exist", dbfile exit() db = Database(dbfile) if outfile is None and use_gui: app = QApplication(sys.argv) kwargs["show_minima"] = False md = DGraphDialog(db, params=kwargs) md.rebuild_disconnectivity_graph() if groups is not None: md.dgraph_widget.dg.color_by_group(groups) md.dgraph_widget.redraw_disconnectivity_graph() md.show() sys.exit(app.exec_()) # make graph from database t0 = time.time() if "Emax" in kwargs and use_gui: graph = reduced_db2graph(db, kwargs['Emax']) else: graph = dg.database2graph(db) t1 = time.time() print "loading the data into a transition state graph took", t1-t0, "seconds" # do the disconnectivity graph analysis mydg = dg.DisconnectivityGraph(graph, **kwargs) print "doing disconnectivity graph analysis" sys.stdout.flush() t1 = time.time() mydg.calculate() t2 = time.time() print "d-graph analysis finished in", t2-t1, "seconds" print "number of minima:", mydg.tree_graph.number_of_leaves() print "plotting disconnectivity graph" sys.stdout.flush() if colourfile: print "Colouring tree according to file ", colourfile colourfetcher = get_colours_from_file(db, colourfile) colouredtree = dg.ColorDGraphByValue(mydg.tree_graph,colourfetcher,normalize_values=True) print "tree set up" colouredtree.run() print "Finished colouring" elif groupcolourfile: print "Colouring tree according to file ", colourfile grouplists = get_group_lists(groupcolourfile) colouredtree = dg.ColorDGraphByGroups(mydg.tree_graph,grouplists) colouredtree.run() print "Finished colouring" if idfile: labelminima = [] labels = {} fin = open(idfile,'r') for line in fin: minID = line.split()[0] labelminima.append(db.getMinimum(minID)) # labels[labelminima[-1]]=str(labelminim fin.close() print "Labelling ", len(labelminima), "minima" print "Creating axes with dimensions ", width, width/aspect fig = plt.figure(figsize=(width, width/aspect)) fig.set_facecolor('white') ax = fig.add_subplot(111, adjustable='box') # make the figure and save it mydg.plot(axes=ax) if idfile: print "Going into draw_minima" mydg.draw_minima(labelminima,labels=True) if outfile is None: plt.show() else: plt.savefig(outfile) t3 = time.time() print "plotting finished in", t3-t2, "seconds"
from rates_linalg import MfptLinalgSparse, TwoStateRates, EstimateRates #db = Database("db.cf.sqlite") db = Database() if db.number_of_minima() == 0: converter = OptimDBConverter(db, mindata="min.data", tsdata="ts.data") converter.pointsmin_data = None converter.pointsts_data = None converter.ReadMinDataFast() converter.ReadTSdataFast() m1 = db.getMinimum(1) m2 = db.getMinimum(3) print "m1, m2", m1._id, m2._id A = [m1] B = [m2] print "energy of mA", m1.energy print "energy of mB", m2.energy # remove disconnected components # graph = nx.Graph() # for ts in db.transition_states(): # m1 = ts.minimum1 # m2 = ts.minimum2 # graph.add_edge(m1, m2) # cc = nx.node_connected_component(graph, A[0]) # nodes = set(cc)