Skip to content

Fix deprecation warning for save/load_trace #5106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions pymc/backends/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,35 @@ def save_trace(trace: MultiTrace, directory: Optional[str] = None, overwrite=Fal
warnings.warn(
"The `save_trace` function will soon be removed."
"Instead, use `arviz.to_netcdf` to save traces.",
DeprecationWarning,
FutureWarning,
)

if directory is None:
directory = ".pymc_{}.trace"
idx = 1
while os.path.exists(directory.format(idx)):
idx += 1
directory = directory.format(idx)

if os.path.isdir(directory):
if overwrite:
shutil.rmtree(directory)
else:
raise OSError(
"Cautiously refusing to overwrite the already existing {}! Please supply "
"a different directory, or set `overwrite=True`".format(directory)
)
os.makedirs(directory)

for chain, ndarray in trace._straces.items():
SerializeNDArray(os.path.join(directory, str(chain))).save(ndarray)
return directory
if isinstance(trace, MultiTrace):
if directory is None:
directory = ".pymc_{}.trace"
idx = 1
while os.path.exists(directory.format(idx)):
idx += 1
directory = directory.format(idx)

if os.path.isdir(directory):
if overwrite:
shutil.rmtree(directory)
else:
raise OSError(
"Cautiously refusing to overwrite the already existing {}! Please supply "
"a different directory, or set `overwrite=True`".format(directory)
)
os.makedirs(directory)

for chain, ndarray in trace._straces.items():
SerializeNDArray(os.path.join(directory, str(chain))).save(ndarray)
return directory
else:
raise TypeError(
f"You are attempting to save an InferenceData object but this function "
"works only for MultiTrace objects. Use `arviz.to_netcdf` instead"
)


def load_trace(directory: str, model=None) -> MultiTrace:
Expand All @@ -103,7 +109,7 @@ def load_trace(directory: str, model=None) -> MultiTrace:
warnings.warn(
"The `load_trace` function will soon be removed."
"Instead, use `arviz.from_netcdf` to load traces.",
DeprecationWarning,
FutureWarning,
)
straces = []
for subdir in glob.glob(os.path.join(directory, "*")):
Expand All @@ -125,7 +131,7 @@ def __init__(self, directory: str):
warnings.warn(
"The `SerializeNDArray` class will soon be removed. "
"Instead, use ArviZ to save/load traces.",
DeprecationWarning,
FutureWarning,
)
self.directory = directory
self.metadata_path = os.path.join(self.directory, self.metadata_file)
Expand Down