Skip to content
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

add the saving of posteriors #35

Merged
merged 6 commits into from
Sep 29, 2020
Merged

add the saving of posteriors #35

merged 6 commits into from
Sep 29, 2020

Conversation

avivajpeyi
Copy link
Collaborator

Closes #25

@avivajpeyi avivajpeyi marked this pull request as ready for review September 28, 2020 00:24
@avivajpeyi
Copy link
Collaborator Author

For easier viewing, main changes in template.ipynb:

#%%

def validate_posterior_filename(filename):
    suffix = Path(filename).suffix
    if suffix != ".h5":
        raise ValueError(f"{suffix} is an invalid extension.")

def save_posteriors(trace, filename):   
    """Save posteriors as a HDF5 file"""
    validate_posterior_filename(filename)
    posteriors = pm.trace_to_dataframe(trace)
    posteriors.to_hdf(filename, mode='w', key='posterior')


def load_posteriors(filename):
    """Load posteriors from HDF5 file and return posteriors as a pd.DataFrame"""
    validate_posterior_filename(filename)
    return pd.read_hdf(filename, key="posterior")

#%%

posterior_filename = os.path.basename(FILENAME.replace(".ipynb", ".h5"))
save_posteriors(trace, posterior_filename)

Note that we could compress the H5 file to save space.

@avivajpeyi
Copy link
Collaborator Author

Changed the above pm.trace_to_dataframe to az.to_netcdf, so we are now storing the trace as an arviz.data.inference_data.InferenceData.

arviz is cool! hadn't come across it until now :)

@avivajpeyi avivajpeyi merged commit ad36c02 into main Sep 29, 2020
@avivajpeyi avivajpeyi deleted the save_posterior branch September 10, 2021 00:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Save posteriors as a FITS/HDF file
2 participants