Beispiel #1
0
def _get_database_params(dbname):
    db = Database(dbname, createdb=False)
    interactions = db.get_property("interactions").value()
    db_nspins = db.get_property("nspins").value()
    db_p = db.get_property("p").value()
    params = (db_nspins, db_p, interactions)
    return db, params
Beispiel #2
0
def get_database_params(dbname):
    db = Database(dbname, createdb=False)
    interactions = db.get_property("interactions").value()
    db_nspins = db.get_property("nspins").value()
    db_p = db.get_property("p").value()
    params = (db_nspins, db_p, interactions)
    return db, params
Beispiel #3
0
def get_database_params(dbname, nspins, p):
    db = Database(dbname, createdb=False)
    interactions = db.get_property("interactions").value()
    db_nspins = db.get_property("nspins").value()
    db_p = db.get_property("p").value()
    # check that parameters match
    assert db_nspins == nspins
    assert db_p == p
    return db, interactions
Beispiel #4
0
def get_database_params(dbname, nspins, p):
    db = Database(dbname, createdb=False)
    interactions = db.get_property("interactions").value()
    db_nspins = db.get_property("nspins").value()
    db_p = db.get_property("p").value()
    # check that parameters match
    assert db_nspins == nspins
    assert db_p == p
    return db, interactions
Beispiel #5
0
def get_database_params(dbname):
    db = Database(dbname, createdb=False)
    x_train_data = db.get_property("x_train_data").value()
    y_train_data = db.get_property("y_train_data").value()
    hnodes = db.get_property("hnodes").value()
    hnodes2 = db.get_property("hnodes2").value()
    reg = db.get_property("reg").value()
    params = (x_train_data, y_train_data, hnodes, hnodes2, reg)
    return db, params
def run_gui_db(dbname="pspin_spherical_p3_N20.sqlite"):
    from pele.gui import run_gui
    try:
        db = Database(dbname, createdb=False)
        interactions = db.get_property("interactions").value()
        nspins = db.get_property("nspins").value()
        p = db.get_property("p").value()
    except IOError:
        interactions=None
    system = MeanFieldPSpinSphericalSystem(nspins, p=p, interactions=interactions)
    run_gui(system, db=dbname)
Beispiel #7
0
def create_soft_sphere_system_from_db(dbname):
    from pele.storage import Database
    db = Database(dbname, createdb=False)
    
    radii = db.get_property("radii").value()
    boxvec = db.get_property("boxvec").value()
    power = db.get_property("power").value()
    print(radii)
    
    system = SoftSphereSystem(radii, boxvec, power=power)
    db = system.create_database(dbname, createdb=False)
    
    return system, db
Beispiel #8
0
def create_soft_sphere_system_from_db(dbname):
    from pele.storage import Database
    db = Database(dbname, createdb=False)
    
    radii = db.get_property("radii").value()
    boxvec = db.get_property("boxvec").value()
    power = db.get_property("power").value()
    print radii
    
    system = SoftSphereSystem(radii, boxvec, power=power)
    db = system.create_database(dbname, createdb=False)
    
    return system, db
Beispiel #9
0
def create_system(L, dbname):
    print("testing whether", dbname, "exists")
    try:
        # if the database already exists get the phases
        db = Database(dbname, createdb=False)
        print(dbname, "exists.  getting phases")
        phases = db.get_property("phases").value()
        dim = db.get_property("dim").value()
        assert dim[0] == L
    except IOError:
        print(dbname, "doesn't exist, generating random phases")
        phases=None
        dim=[L,L]
    system = XYModlelSystem(dim=dim, phi_disorder=np.pi, phases=phases)
    return system
Beispiel #10
0
def run_gui_db(dbname="regression_logit_mnist.sqlite", scale=2.5, device='cpu'):
    from pele.gui import run_gui
    try:
        db = Database(dbname, createdb=False)
        x_train_data=db.get_property("x_train_data").value(),
        y_train_data=db.get_property("y_train_data").value(),
        hnodes=db.get_property("hnodes").value(),
        reg=db.get_property("reg").value(),
    except IOError:
        pass
    hnodes, reg = hnodes[0], reg[0]
    x_train_data, y_train_data = np.array(np.array(x_train_data)[0,:,:]), np.array(np.array(y_train_data)[0,:,:])
    print np.array(x_train_data).shape, np.array(y_train_data).shape
    # system = Mlp3System(x_train_data, y_train_data, hnodes, reg=reg, device=device)
    system = Elu2NNSystem(x_train_data, y_train_data, hnodes, reg=reg, scale=scale, device=device)
    run_gui(system, db=dbname)
Beispiel #11
0
def run_gui_db(dbname="xy_10x10.sqlite"):
    from pele.gui import run_gui
    from pele.storage import Database
    try:
        db = Database(dbname, createdb=False)
        phases = db.get_property("phases").value()
    except IOError:
        phases=None
    system = XYModlelSystem(dim=[10,10], phi_disorder=np.pi, phases=phases)
    run_gui(system, db=dbname)
Beispiel #12
0
def run_gui_db(dbname="xy_10x10.sqlite"):
    from pele.gui import run_gui
    from pele.storage import Database
    try:
        db = Database(dbname, createdb=False)
        phases = db.get_property("phases").value()
    except IOError:
        phases = None
    system = XYModlelSystem(dim=[10, 10], phi_disorder=np.pi, phases=phases)
    run_gui(system, db=dbname)
Beispiel #13
0
def create_BLJ_system_from_db(dbname):
    from pele.storage import Database
    db = Database(dbname, createdb=False)
    
    natoms = db.get_property("natoms").value()
    boxvec = db.get_property("boxvec").value()
    ntypeA = db.get_property("ntypeA").value()
    initial_coords = db.get_property("initial_coords").value()
    #print radii
    
    system = BLJBulk(natoms, boxvec, ntypeA=ntypeA)

    # The next line is a nasty hack to avoid an odd error.
    # When we call create_database on an exisiting database, it compares all the system properties
    # against the ones that are saved in the database. It then commits any changes to the database,
    # but that step seems to fail when trying to overwrite a sequence object (in this case the kwargs
    # dictionary) with another sequence. Instead, I overwrite with a None-type object first. Then in
    # the next command we are overwriting a None-type with a dictionary, which doesn't cause an error.
    db.add_property("potential_kwargs",{})

    db = system.create_database(dbname, createdb=False, overwrite_properties=True)
    
    return system, db, initial_coords
Beispiel #14
0
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"]
Beispiel #15
0
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"]