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
1from argparse import ArgumentParser
2from pathlib import Path
3import pickle
5import numpy as np
7from ase.utils.filecache import MultiFileJSONCache
9description = """
10Convert legacy pickle files from ASE vibrations calculations to JSON.
12Pickles are no longer supported for file storage.
14WARNING: Only run this command on trusted pickles since unpickling
15a maliciously crafted pickle allows arbitrary code execution.
16Indeed that is why pickles are no longer used.
17"""
20def port(picklefile):
21 picklefile = Path(picklefile)
23 name = picklefile.name
25 vibname, key, pckl = name.rsplit('.', 3)
26 assert pckl == 'pckl'
28 cache = MultiFileJSONCache(picklefile.parent / vibname)
30 obj = pickle.loads(picklefile.read_bytes())
31 if isinstance(obj, np.ndarray): # vibrations
32 dct = {'forces': obj}
33 else: # Infrared
34 forces, dipole = obj
35 assert isinstance(forces, np.ndarray), f'not supported: {type(forces)}'
36 assert isinstance(dipole, np.ndarray), f'not supported: {type(dipole)}'
37 dct = {'forces': forces, 'dipole': dipole}
39 outfilename = cache._filename(key)
41 if key in cache:
42 del cache[key]
44 cache[key] = dct
45 print(f'wrote {picklefile} ==> {outfilename}')
48def main(argv=None):
49 parser = ArgumentParser(description=description)
50 parser.add_argument('picklefile', nargs='+')
51 args = parser.parse_args(argv)
53 for fname in args.picklefile:
54 port(fname)
57if __name__ == '__main__':
58 main()