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

Numpy casting rule error #29

Closed joriswitstok closed 1 year ago

joriswitstok commented 1 year ago

Hi Adam,

After having some trouble when using the default plotting functions in bagpipes (see full traceback below), I dug into the code to see what was causing the issue.

I found that if I change the y_scale parameter in the add_spectrum function to become a float (i.e. replace line 26 in bagpipes/plotting/plot_spectrum.py to y_scale = float(int(np.log10(ymax))-1)), the error goes away. For some reason, the multiplication of a float64 array with an integer (in lines 46 and 47) produced an object array, which then causes an error when used together with the wavelength array (spectrum[:, 0], which is still float64) in fill_between (line 52).

I am not entirely sure why this is producing an error for me specifically (and apparently not for others, maybe it has something to do with the numpy version I have installed?), but I thought I'd flag it in case anyone else runs into the same problem.

Cheers, Joris

Traceback (most recent call last): File "specz_R100_irr_dust.py", line 82, in <module> fig = gal.plot() File "/Users/Joris/anaconda3/envs/SED3/lib/python3.7/site-packages/bagpipes/input/galaxy.py", line 230, in plot y_scale_spec=y_scale_spec) File "/Users/Joris/anaconda3/envs/SED3/lib/python3.7/site-packages/bagpipes/plotting/plot_galaxy.py", line 35, in plot_galaxy y_scale=y_scale_spec) File "/Users/Joris/anaconda3/envs/SED3/lib/python3.7/site-packages/bagpipes/plotting/plot_spectrum.py", line 53, in add_spectrum zorder=zorder-1, alpha=0.75, linewidth=0) File "/Users/Joris/anaconda3/envs/SED3/lib/python3.7/site-packages/matplotlib/__init__.py", line 1414, in inner return func(ax, *map(sanitize_sequence, args), **kwargs) File "/Users/Joris/anaconda3/envs/SED3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 5253, in fill_between where=where, interpolate=interpolate, step=step, **kwargs) File "/Users/Joris/anaconda3/envs/SED3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 5158, in _fill_between_x_or_y [(ind_dir, ind), (dep_dir, dep1), (dep_dir, dep2)], kwargs)) File "/Users/Joris/anaconda3/envs/SED3/lib/python3.7/site-packages/numpy/ma/core.py", line 2369, in masked_invalid condition = ~(np.isfinite(a)) TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

ACCarnall commented 1 year ago

Hi Joris,

Hmm, sorry, not entirely sure what's going on with that. I mostly see the plotting codes as a best-effort basis addition for basic visualisation of results, they're unfortunately far from universal. I have trouble with them and modify things for specific projects on a pretty regular basis. Did you check for nans, negative errors or other weird behaviour in your data array?

Cheers, Adam

joriswitstok commented 1 year ago

Makes sense, just thought I'd raise the issue in case it comes up again! I've extensively checked the data and it turns out there was a problem with unit conversion (I am plotting the results of someone else's fits), meaning that in the correct units the fitted spectrum was accidentally a factor 10^2 higher than the data. In this (hopefully rare) case, the error is somehow caused by the y_scale parameter being an integer, as mentioned – if it is a float instead of an int, the code runs without errors and importantly, the plots are produced fine so you can see there's an offset (though of course the y-axis label reads 10^-20.0 as opposed to 10^-20)...

ACCarnall commented 1 year ago

Excellent, glad you managed to figure it out! To be honest, the adaptive y axis limits for the plots have been a headache from the beginning. Maybe they were a mistake.