ACCarnall / bagpipes

Bagpipes is a state of the art code for generating realistic model galaxy spectra and fitting these to spectroscopic and photometric observations. Users should install with pip, not by cloning the repository.
http://bagpipes.readthedocs.io
GNU General Public License v3.0
71 stars 37 forks source link

Error in Plotting #71

Closed akritiasto closed 1 month ago

akritiasto commented 1 month ago

Hi Adam! I am a new user of BAGPIPES and I am exploring the various options of bagpipes. I have fitted a double-power law SFH to the SED of a quiescent galaxy and I am having some trouble in plotting my results. My code is:

     galaxy = pipes.galaxy(911001, load_data, spectrum_exists=False, filt_list=goodss_filt_list)
     fit_info["redshift"] = phot[i]
     sampler = pipes.fit(galaxy, fit_info,run="n501_finalbreak")
     sampler.fit(verbose=False)
     fig = plt.figure(figsize=(12, 7))
     gs = mpl.gridspec.GridSpec(7, 5, hspace=3., wspace=0.1)

     ax1 = plt.subplot(gs[:4, :])

     pipes.plotting.add_observed_photometry(sampler.galaxy, ax1, zorder=10)
     pipes.plotting.add_photometry_posterior(sampler, ax1)

     labels = ["sfr", "mass_weighted_age", "stellar_mass"]

     post_quantities = dict(zip(labels, [sampler.posterior.samples[l] for l in labels]))

     axes = []
     for i in range(3):
           axes.append(plt.subplot(gs[4:, i]))
           pipes.plotting.hist1d(post_quantities[labels[i]], axes[-1], smooth=True, label=labels[I])

The error I get is: Results loaded from pipes/posterior/n501_finalbreak/911001.h5

Fitting not performed as results have already been loaded from pipes/posterior/n501_finalbreak/911001.h5. To start over delete this file or change run.

Traceback (most recent call last):

File ~/anaconda3/lib/python3.10/site-packages/spyder_kernels/py3compat.py:356 in compat_exec exec(code, globals, locals)

File ~/allcodes_odin/bagpipes/plotb.py:135 a=load_saved(IDs)

File ~/allcodes_odin/bagpipes/plotb.py:103 in load_saved pipes.plotting.add_photometry_posterior(sampler, ax1)

File ~/anaconda3/lib/python3.10/site-packages/bagpipes/plotting/plot_spectrum_posterior.py:69 in add_photometry_posterior log_wavs = np.log10(fit.posterior.model_galaxy.wavelengths*(1.+redshift))

AttributeError: 'posterior' object has no attribute 'model_galaxy' I am not able to resolve this error.. Could you please help me with this. Thanks!

ACCarnall commented 1 month ago

Hi there,

If you're loading a previously fitted model from a save file you'd need to add the command sampler.posterior.get_advanced_quantities() after sampler.fit() in order to get the code to re-generate the quantities you're trying to plot.

Cheers, Adam

akritiasto commented 1 month ago

Thank you!