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

Warning and error when including a variable resolution in the fitting #43

Closed Vladan1986 closed 9 months ago

Vladan1986 commented 1 year ago

Hi Adam,

Thank you for the latest update and including the option for convolving the spectrum with a resolution curve.

I updated Bagpipes to the latest version and I tried to implement this in the part of the code for fitting the JWST spectra. Basically, what I add is:

`hdul = fits.open(PATH+"jwst_nirspec_prism_disp.fits")

fit_instructions["Rcurve"] = np.c[10000*hdul[1].data["WAVELENGTH"], hdul[1].data["R"]] `

and then:

fit = pipes.fit(galaxy, fit_instructions, run="spectroscopy")

I receive a couple of warnings and errors:

`/home/vladan/anaconda3/lib/python3.9/site-packages/bagpipes/fitting/fitted_model.py:98: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison if all_vals[i] in all_keys: /home/vladan/anaconda3/lib/python3.9/site-packages/bagpipes/fitting/fitted_model.py:101: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison if all_vals[i] == "dirichlet":


AttributeError Traceback (most recent call last) Cell In[18], line 1 ----> 1 fit = pipes.fit(galaxy, fit_instructions, run="spectroscopy") 2 fit.fit(verbose=False)

File ~/anaconda3/lib/python3.9/site-packages/bagpipes/fitting/fit.py:82, in fit.init(self, galaxy, fit_instructions, run, time_calls, n_posterior) 80 if os.path.exists(self.fname[:-1] + ".h5"): 81 self.results = dd.io.load(self.fname[:-1] + ".h5") ---> 82 self.posterior = posterior(self.galaxy, run=run, 83 n_samples=n_posterior) 84 self.fit_instructions = dd.io.load(self.fname[:-1] + ".h5", 85 group="/fit_instructions") 87 if rank == 0:

File ~/anaconda3/lib/python3.9/site-packages/bagpipes/fitting/posterior.py:80, in posterior.init(self, galaxy, run, n_samples) 76 self.samples[param_name] = self.samples2d[self.indices, i] 78 self.get_dirichlet_tx(dirichlet_comps) ---> 80 self.get_basic_quantities()

File ~/anaconda3/lib/python3.9/site-packages/bagpipes/fitting/posterior.py:147, in posterior.get_basic_quantities(self) 144 self.sfh.update(self.fitted_model.model_components) 146 for q in quantity_names: --> 147 self.samples[q][i] = getattr(self.sfh, q)

AttributeError: 'star_formation_history' object has no attribute 'mass_weighted_metallicity'

`

I do not change anything in my code apart from the two lines and I do not modify anything related to SFH. So, I am not sure If I am missing something or doing something wrong, but any help is more than welcome.

I tried also to run the Further examples 3 and it works although I get an output line:

Spectres: new_wavs contains values outside the range in spec_wavs, new_fluxes and new_errs will be filled with the value set in the 'fill' keyword argument.

Cheers, Vladan

ACCarnall commented 1 year ago

Hi Vladan,

Sorry, at least one of these is a bug I've introduced with an unrelated small change I made, I'll aim to release a fix today.

Cheers, Adam

ACCarnall commented 1 year ago

I've just released v1.0.2 which I believe should fix the error. The first warning is nothing to worry about, the second implies that your supplied resolution curve doesn't cover the whole wavelength range of your supplied spectroscopic data. I didn't get this warning when I was running the notebook I believe, but I will double check in the morning.

Vladan1986 commented 1 year ago

Thank you so much for a quick reply. I installed the new version and I am weighting for the code to finish.

For the second I warning, could it be that the spectrum that I am using doesn't cover the whole wavelength range of the R_curve (e.g., the spectrum goes from ~9500-53000, but the resolution curve covers everything from 5000-60000).

Cheers, V.

Vladan1986 commented 1 year ago

The code has finished, and now I have (besides the warnings that I mentioned), another error:

`NameError Traceback (most recent call last) Cell In[28], line 2 1 fit = pipes.fit(galaxy, fit_instructions, run="spectroscopy") ----> 2 fit.fit(verbose=False)

File ~/anaconda3/lib/python3.9/site-packages/bagpipes/fitting/fit.py:168, in fit.fit(self, verbose, n_live, use_MPI) 165 self._print_results() 167 # Create a posterior object to hold the results of the fit. --> 168 self.posterior = posterior(self.galaxy, run=self.run, 169 n_samples=self.n_posterior)

File ~/anaconda3/lib/python3.9/site-packages/bagpipes/fitting/posterior.py:53, in posterior.init(self, galaxy, run, n_samples) 50 # Reconstruct the fitted model. 51 file = h5py.File(fname, "r") ---> 53 self.fit_instructions = eval(file.attrs["fit_instructions"]) 54 self.fitted_model = fitted_model(self.galaxy, self.fit_instructions) 56 # 2D array of samples for the fitted parameters only.

File :1

NameError: name 'array' is not defined`

I have attached the code that produces this error. It is largely based on your Further Examples 3, it produces a model and then fits the model following fit instructions.

These warnings and the error disappear if I comment the line:

fit_instructions["Rcurve"] = np.c[10000*hdul[1].data["WAVELENGTH"], hdul[1].data["R"]] Further_example_3.zip

Cheers,

ACCarnall commented 9 months ago

Hi Vladan,

Really sorry I forgot to come back to you on this, I've just posted a potential solution in #50 that will hopefully fix the issue, feel free to give it a try.

Cheers, Adam

ACCarnall commented 9 months ago

This should now be fixed in v1.0.3 (see #50)