def to_pickle(self, path: Union[str, Path]) -> None: """ Save BayesianMetropolis instance as serialized pickle file. Parameters ---------- path : Union[str, Path] File path where the pickled object will be stored. Raises ------ RuntimeError If file already exists RuntimeError If parent dir does not exist """ assert isinstance(path, (str, Path)) path = Path(path) # make sure directory exists directory = path.parent if not directory.is_dir(): raise RuntimeError( f"Trying to write to non-existing directory {directory}") # make sure file does not exist if path.exists(): raise RuntimeError( f"File already exists {path}. Refusing to overwrite!") # pickle # save a ref to the model priors_model = self.priors_model self.priors_model = None to_pickle(self, path) self.priors_model = priors_model
def to_pickle(self, path: Union[str, Path]) -> None: """ Save Metropolis instance as serialized pickle file. Parameters ---------- path : Union[str, Path] File path where the pickled object will be stored. Raises ------ RuntimeError If file already exists RuntimeError If parent dir does not exist """ assert isinstance(path, (str, Path)) path = Path(path) # make sure directory exists directory = path.parent if not directory.is_dir(): raise RuntimeError( f"Trying to write to non-existing directory {directory}") # make sure file does not exist if path.exists(): raise RuntimeError( f"File already exists {path}. Refusing to overwrite!") # pickle # avoid using protocol 5 if available (python 3.8 only feature) to_pickle(self, path, protocol=min(HIGHEST_PROTOCOL, 4))
def saveWork(self, obj, quick_descr, overwrite, ntries, _path=_PATH): if _path != FileIO._PATH: FileIO._PATH = _path for i in range(0, ntries): _path = abspath(FileIO._PATH.format(quick_descr, i)) if overwrite or not exists(_path): pickle.to_pickle(obj, _path) return None
def Write(outfile, datastruct, exceptiononmissingdir=False): dname, _ = os.path.split(outfile) if dname != '' and not os.path.exists(dname): assert not exceptiononmissingdir os.makedirs(dname) try: with open(outfile, 'wb') as pkl_file: dill.dump(datastruct, pkl_file, -1) except: to_pickle(datastruct, outfile)
def to_pickle(self, path): """ Pickle (serialize) object to input file path Parameters ---------- path : string File path """ from pandas.io.pickle import to_pickle return to_pickle(self, path)
def save(self, path): # TODO remove in 0.13 import warnings from pandas.io.pickle import to_pickle warnings.warn("save is deprecated, use to_pickle", FutureWarning) return to_pickle(self, path)