Skip to content

Commit

Permalink
v1.0.2 - bug fix and change from deepdish to h5py
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Carnall authored and Adam Carnall committed Apr 13, 2023
1 parent a8bd640 commit bb70d3c
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 31 deletions.
29 changes: 20 additions & 9 deletions bagpipes/fitting/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import time
import warnings
import deepdish as dd
import h5py

from copy import deepcopy

Expand Down Expand Up @@ -74,15 +74,21 @@ def __init__(self, galaxy, fit_instructions, run=".", time_calls=False,
self.fname = "pipes/posterior/" + run + "/" + self.galaxy.ID + "_"

# A dictionary containing properties of the model to be saved.
self.results = {"fit_instructions": self.fit_instructions}
self.results = {}

# If a posterior file already exists load it.
if os.path.exists(self.fname[:-1] + ".h5"):
self.results = dd.io.load(self.fname[:-1] + ".h5")
file = h5py.File(self.fname[:-1] + ".h5", "r")

self.posterior = posterior(self.galaxy, run=run,
n_samples=n_posterior)
self.fit_instructions = dd.io.load(self.fname[:-1] + ".h5",
group="/fit_instructions")

self.fit_instructions = eval(file.attrs["fit_instructions"])

for k in file.keys():
self.results[k] = np.array(file[k])
if np.sum(self.results[k].shape) == 1:
self.results[k] = self.results[k][0]

if rank == 0:
print("\nResults loaded from " + self.fname[:-1] + ".h5\n")
Expand Down Expand Up @@ -136,18 +142,23 @@ def fit(self, verbose=False, n_live=400, use_MPI=True):
samples2d = np.loadtxt(self.fname + "post_equal_weights.dat")
lnz_line = open(self.fname + "stats.dat").readline().split()

file = h5py.File(self.fname[:-1] + ".h5", "w")

file.attrs["fit_instructions"] = str(self.fit_instructions)

self.results["samples2d"] = samples2d[:, :-1]
self.results["lnlike"] = samples2d[:, -1]
self.results["lnz"] = float(lnz_line[-3])
self.results["lnz_err"] = float(lnz_line[-1])
self.results["median"] = np.median(samples2d, axis=0)
self.results["conf_int"] = np.percentile(self.results["samples2d"],
(16, 84), axis=0)
for k in self.results.keys():
file.create_dataset(k, data=self.results[k])

self.results["fit_instructions"] = self.fit_instructions

# Save re-formatted outputs as HDF5 and remove MultiNest output.
with warnings.catch_warnings():
warnings.simplefilter("ignore")
dd.io.save(self.fname[:-1] + ".h5", self.results)
file.close()

os.system("rm " + self.fname + "*")

Expand Down
10 changes: 6 additions & 4 deletions bagpipes/fitting/posterior.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np

import os
import deepdish as dd
import h5py

from copy import deepcopy

Expand Down Expand Up @@ -48,11 +48,13 @@ def __init__(self, galaxy, run=".", n_samples=500):
raise IOError("Fit results not found for " + self.galaxy.ID + ".")

# Reconstruct the fitted model.
self.fit_instructions = dd.io.load(fname, group="/fit_instructions")
file = h5py.File(fname, "r")

self.fit_instructions = eval(file.attrs["fit_instructions"])
self.fitted_model = fitted_model(self.galaxy, self.fit_instructions)

# 2D array of samples for the fitted parameters only.
self.samples2d = dd.io.load(fname, group="/samples2d")
self.samples2d = np.array(file["samples2d"])

# If fewer than n_samples exist in posterior, reduce n_samples
if self.samples2d.shape[0] < self.n_samples:
Expand Down Expand Up @@ -128,7 +130,7 @@ def get_basic_quantities(self):

quantity_names = ["stellar_mass", "formed_mass", "sfr", "ssfr", "nsfr",
"mass_weighted_age", "tform", "tquench",
"mass_weighted_metallicity"]
"mass_weighted_zmet"]

for q in quantity_names:
self.samples[q] = np.zeros(self.n_samples)
Expand Down
8 changes: 4 additions & 4 deletions bagpipes/models/star_formation_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ def _calculate_derived_quantities(self):
self.mass_weighted_age = np.sum(self.sfh*self.age_widths*self.ages)
self.mass_weighted_age /= np.sum(self.sfh*self.age_widths)

self.mass_weighted_met = np.sum(self.live_frac_grid*self.ceh.grid,
self.mass_weighted_zmet = np.sum(self.live_frac_grid*self.ceh.grid,
axis=1)
self.mass_weighted_met /= np.sum(self.live_frac_grid*self.ceh.grid)
self.mass_weighted_met *= config.metallicities
self.mass_weighted_met = np.sum(self.mass_weighted_met)
self.mass_weighted_zmet /= np.sum(self.live_frac_grid*self.ceh.grid)
self.mass_weighted_zmet *= config.metallicities
self.mass_weighted_zmet = np.sum(self.mass_weighted_zmet)

self.tform = self.age_of_universe - self.mass_weighted_age

Expand Down
4 changes: 2 additions & 2 deletions bagpipes/plotting/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ def auto_axis_label(ax, y_scale, z_non_zero=True, log_x=False):
if tex_on:
if z_non_zero:
ax.set_ylabel("$\\mathrm{f_{\\lambda}}\\ \\mathrm{/\\ 10^{"
+ str(y_scale)
+ str(int(y_scale))
+ "}\\ erg\\ s^{-1}\\ cm^{-2}\\ \\AA^{-1}}$")

else:
ax.set_ylabel("$\\mathrm{f_{\\lambda}}\\ \\mathrm{/\\ 10^{"
+ str(y_scale)
+ str(int(y_scale))
+ "}\\ erg\\ s^{-1}\\ \\AA^{-1}}$")

if log_x:
Expand Down
2 changes: 1 addition & 1 deletion bagpipes/plotting/plot_galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def plot_galaxy(galaxy, show=True, return_y_scale=False, y_scale_spec=None):
# Add observed photometry to plot
if galaxy.photometry_exists and galaxy.spectrum_exists:
phot_ax = plt.subplot(gs[1, 0])
y_scale_phot = add_observed_photometry(galaxy, phot_ax)
y_scale_phot = add_observed_photometry(galaxy, phot_ax).astype(float)
y_scale.append(y_scale_phot)
axes.append(phot_ax)

Expand Down
2 changes: 1 addition & 1 deletion bagpipes/plotting/plot_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def add_spectrum(spectrum, ax, x_ticks=None, zorder=4, z_non_zero=True,
ymax = 1.05*np.nanmax(spectrum[:, 1])

if y_scale is None:
y_scale = int(np.log10(ymax))-1
y_scale = float(int(np.log10(ymax))-1)

ax.set_ylim(0., ymax*10**-y_scale)
ax.set_xlim(spectrum[0, 0], spectrum[-1, 0])
Expand Down
4 changes: 2 additions & 2 deletions bagpipes/plotting/plot_spectrum_posterior.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def add_photometry_posterior(fit, ax, zorder=4, y_scale=None, color1=None,
ymax = 1.05*np.max(upper_lims[mask])

if not y_scale:
y_scale = int(np.log10(ymax))-1
y_scale = float(int(np.log10(ymax))-1)

# Calculate posterior median redshift.
if "redshift" in fit.fitted_model.params:
Expand Down Expand Up @@ -106,7 +106,7 @@ def add_spectrum_posterior(fit, ax, zorder=4, y_scale=None):
ymax = 1.05*np.max(fit.galaxy.spectrum[:, 1])

if not y_scale:
y_scale = int(np.log10(ymax))-1
y_scale = float(int(np.log10(ymax))-1)

wavs = fit.galaxy.spectrum[:, 0]
spec_post = np.copy(fit.posterior.samples["spectrum"])
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
# built documents.
#
# The short X.Y version.
version = u'1.0.1'
version = u'1.0.2'
# The full version, including alpha/beta/rc tags.
release = u'1.0.1'
release = u'1.0.2'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
6 changes: 3 additions & 3 deletions rtd-environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ dependencies:
- astropy
- scipy
- matplotlib>=2.2.2
- numpy>=1.14.2
- numpy
- pybind11
- h5py
- pip:
- corner
- pymultinest
- msgpack
- deepdish
- pandas<=1.1.5
- pandas
- spectres
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
setup(
name='bagpipes',

version='1.0.1',
version='1.0.2',

description='Galaxy spectral fitting',

Expand All @@ -29,9 +29,9 @@

include_package_data=True,

install_requires=["numpy>=1.14.2", "corner", "pymultinest>=2.11",
install_requires=["numpy", "corner", "pymultinest>=2.11", "h5py", "pandas",
"astropy", "matplotlib>=2.2.2", "scipy", "msgpack",
"deepdish", "pandas<=1.1.5", "spectres"],
"spectres"],

project_urls={
"readthedocs": "https://bagpipes.readthedocs.io",
Expand Down

0 comments on commit bb70d3c

Please sign in to comment.