Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import optparse
2import os
4import numpy as np
6from ase.db import connect
7from ase.db.sqlite import index_statements
10def convert(name, opts):
11 con1 = connect(name, use_lock_file=False)
12 con1._allow_reading_old_format = True
13 newname = name[:-2] + 'new.db'
14 with connect(newname, create_indices=False, use_lock_file=False) as con2:
15 row = None
16 for row in con1.select():
17 kvp = row.get('key_value_pairs', {})
18 if opts.convert_strings_to_numbers:
19 for key, value in kvp.items():
20 if isinstance(value, str):
21 try:
22 value = float(value)
23 except ValueError:
24 pass
25 else:
26 kvp[key] = value
27 if opts.convert_minus_to_not_a_number:
28 for key, value in kvp.items():
29 if value == '-':
30 kvp[key] = np.nan
32 atoms = row.toatoms()
33 if opts.remove_constrints:
34 atoms.constraints = []
35 con2.write(atoms, data=row.get('data'), **kvp)
37 assert row is not None, 'Your database is empty!'
39 c = con2._connect()
40 for statement in index_statements:
41 c.execute(statement)
42 c.commit()
44 os.rename(name, name[:-2] + 'old.db')
45 os.rename(newname, name)
48def main():
49 parser = optparse.OptionParser()
50 parser.add_option('-S', '--convert-strings-to-numbers',
51 action='store_true')
52 parser.add_option('-N', '--convert-minus-to-not-a-number',
53 action='store_true')
54 parser.add_option('-C', '--remove-constraints',
55 action='store_true')
56 opts, args = parser.parse_args()
57 for name in args:
58 convert(name, opts)
61if __name__ == '__main__':
62 main()