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

feat(arviz): add arviz import and export #32

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions la_forge/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import logging
from typing import Type

import datetime
from functools import cached_property

import arviz as az
import h5py
import numpy as np
import pandas as pd
import xarray as xr
from astropy.io import fits
from astropy.table import Table

Expand Down Expand Up @@ -109,6 +115,18 @@ def __init__(self, chaindir=None, corepath=None, burn=0.25, label=None,
self.params = table.colnames
self.chain = np.array([table[p] for p in self.params]).T
self.chainpath = chaindir + '/chain.fits'
# Check if it's in a common arviz format
elif os.path.isfile(chaindir + '/chain.nc') or os.path.isfile(chaindir + '/chain.zarr'):
self.chainpath = chaindir + '/chain.nc' if os.path.isfile(chaindir + '/chain.nc') else chaindir + '/chain.zarr'
extension = self.chainpath.split(".")[-1]
try:
inf_data = az.from_netcdf(self.chainpath) if extension=="nc" else az.from_zarr(self.chainpath)
except:
msg = f"{self.chainpath} is not a valid ArviZ InferenceData object."
raise ValueError(msg)
stacked = az.extract(inf_data) # combines chains
self.chain = stacked.to_array().to_numpy().T # ArviZ uses dimension 1 for samples, we want it to be 0
self.params = [param for param in stacked.variables if param not in ['sample', 'chain', 'draw']]
else:
# Load chain
if os.path.isfile(chaindir + '/chain_1.txt'):
Expand Down Expand Up @@ -666,6 +684,35 @@ def map_params(self):
"""Return all Maximum a posteri parameters."""
return self.chain[self.burn + self.map_idx, :]

@cached_property
def arviz(self) -> az.InferenceData:
"""Create an arviz.InferenceData object from a Core."""

# Easiest to make a dataframe first
df = pd.DataFrame(data=self.chain, columns=self.params)

# ArviZ wants to see `chain` and `draw` dimensions
df["chain"] = 0
df["draw"] = np.arange(len(df), dtype=int)
df = df.set_index(["chain", "draw"])

# Make an xarray `Dataset` to give ArviZ
xdata = xr.Dataset.from_dataframe(df)

# Store some metadata
xdata.attrs.update(
source="la_forge_core",
created_at=datetime.datetime.now(datetime.timezone.utc)
.replace(microsecond=0)
.isoformat(),
)

# Make the ArviZ object
dataset = az.InferenceData(posterior=xdata)

return dataset


# --------------------------------------------#
# ---------------HyperModel Core--------------#
# --------------------------------------------#
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ astropy>=3.0
corner
six
h5py>=3.4.0
arviz>=0.19.0
26 changes: 17 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@
with open('HISTORY.rst') as history_file:
history = history_file.read()

requirements = ['numpy>=1.16',
'scipy>=1.0.0',
'matplotlib>=2.0.0',
'corner',
'h5py>=3.4.0',
'astropy>=3.0',
'six',

]
requirements = [
"numpy>=1.16",
"scipy>=1.0.0",
"matplotlib>=2.0.0",
"corner",
"h5py>=3.4.0",
"astropy>=3.0",
"six",
"arviz>=0.19.0",
"zarr>=2.5.0,<3",
"netcdf4",
"xarray-datatree",
"dm-tree",
"contourpy",
"bokeh>=3",

]

test_requirements = ['pytest', ]

Expand Down
Loading