def ase_decoder_hook(dct): """JSON decoder hook for ase.Atoms de-serialization.""" if "__datetime__" in dct: return datetime.datetime.strptime(dct["__datetime__"], "%Y-%m-%dT%H:%M:%S.%f") if "__complex__" in dct: return complex(*dct["__complex__"]) if "__ndarray__" in dct: return create_ndarray(*dct["__ndarray__"]) # No longer used (only here for backwards compatibility): if "__complex_ndarray__" in dct: r, i = (np.array(x) for x in dct["__complex_ndarray__"]) return r + i * 1j if "__ase_objtype__" in dct: objtype = dct.pop("__ase_objtype__") dct = numpyfy(dct) if objtype == "cell": from ase.cell import Cell pbc = dct.pop("pbc", None) obj = Cell(**dct) if pbc is not None: obj._pbc = pbc else: raise RuntimeError("Do not know how to decode object type {} " "into an actual object".format(objtype)) assert obj.ase_objtype == objtype return obj return dct
def create_ase_object(objtype, dct): # We just try each object type one after another and instantiate # them manually, depending on which kind it is. # We can formalize this later if it ever becomes necessary. if objtype == 'cell': from ase.cell import Cell pbc = dct.pop('pbc') obj = Cell(**dct) if pbc is not None: obj._pbc = pbc elif objtype == 'bandstructure': from ase.dft.band_structure import BandStructure obj = BandStructure(**dct) elif objtype == 'bandpath': from ase.dft.kpoints import BandPath obj = BandPath(path=dct.pop('labelseq'), **dct) else: raise ValueError('Do not know how to decode object type {} ' 'into an actual object'.format(objtype)) assert obj.ase_objtype == objtype return obj