diff --git a/bagpipes/fitting/fit.py b/bagpipes/fitting/fit.py index cb76717..7ebd8b4 100644 --- a/bagpipes/fitting/fit.py +++ b/bagpipes/fitting/fit.py @@ -4,7 +4,7 @@ import os import time import warnings -import deepdish as dd +import h5py from copy import deepcopy @@ -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") @@ -136,6 +142,10 @@ 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]) @@ -143,11 +153,12 @@ def fit(self, verbose=False, n_live=400, use_MPI=True): 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 + "*") diff --git a/bagpipes/fitting/posterior.py b/bagpipes/fitting/posterior.py index 050c893..c148eb1 100644 --- a/bagpipes/fitting/posterior.py +++ b/bagpipes/fitting/posterior.py @@ -3,7 +3,7 @@ import numpy as np import os -import deepdish as dd +import h5py from copy import deepcopy @@ -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: @@ -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) diff --git a/bagpipes/models/star_formation_history.py b/bagpipes/models/star_formation_history.py index 1fd8eda..3aa71e4 100644 --- a/bagpipes/models/star_formation_history.py +++ b/bagpipes/models/star_formation_history.py @@ -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 diff --git a/bagpipes/plotting/general.py b/bagpipes/plotting/general.py index 7be9f44..d7d5b8c 100644 --- a/bagpipes/plotting/general.py +++ b/bagpipes/plotting/general.py @@ -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: diff --git a/bagpipes/plotting/plot_galaxy.py b/bagpipes/plotting/plot_galaxy.py index c47bfb1..6ec21c3 100644 --- a/bagpipes/plotting/plot_galaxy.py +++ b/bagpipes/plotting/plot_galaxy.py @@ -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) diff --git a/bagpipes/plotting/plot_spectrum.py b/bagpipes/plotting/plot_spectrum.py index 4f46173..378e554 100644 --- a/bagpipes/plotting/plot_spectrum.py +++ b/bagpipes/plotting/plot_spectrum.py @@ -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]) diff --git a/bagpipes/plotting/plot_spectrum_posterior.py b/bagpipes/plotting/plot_spectrum_posterior.py index 6a414d8..968a33d 100644 --- a/bagpipes/plotting/plot_spectrum_posterior.py +++ b/bagpipes/plotting/plot_spectrum_posterior.py @@ -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: @@ -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"]) diff --git a/docs/conf.py b/docs/conf.py index b14b142..9062e48 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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. diff --git a/rtd-environment.yml b/rtd-environment.yml index 1bbb892..2bfa126 100644 --- a/rtd-environment.yml +++ b/rtd-environment.yml @@ -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 diff --git a/setup.py b/setup.py index 64d603c..6101516 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ setup( name='bagpipes', - version='1.0.1', + version='1.0.2', description='Galaxy spectral fitting', @@ -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",