Closed orifox closed 2 years ago
Welcome to Astropy 👋 and thank you for your first issue!
A project member will respond to you as soon as possible; in the meantime, please double-check the guidelines for submitting issues and make sure you've provided the requested details.
GitHub issues in the Astropy repository are used to track bug reports and feature requests; If your issue poses a question about how to use Astropy, please instead raise your question in the Astropy Discourse user forum and close this issue.
If you feel that this issue has not been responded to in a timely manner, please leave a comment mentioning our software support engineer @embray, or send a message directly to the development mailing list. If the issue is urgent or sensitive in nature (e.g., a security vulnerability) please send an e-mail directly to the private e-mail feedback@astropy.org.
@orifox , what version of photutils?
@larrybradley and/or @eteq , I transferred this issue from astropy
to photutils
in case this is something that downstream (photutils) need to fix. Also cc @WilliamJamieson since he has been refactoring modeling
.
@pllim Nothing has changed in IterativelySubtractedPSFPhotometry
recently. I suspect the change was in in astropy modeling.
This looks like the change in astropy that is causing the error: https://github.com/astropy/astropy/pull/12811
Does this mean photutils need to adapt to new fitting algorithm or is this astropy bug?
This was my fear with about going with an error rather than a warning in astropy/astropy#12811, technically as soon as non-finite values are encountered during fitting the results of the fit become unreliable.
The short answer this is when NaN
values are present, scipy.optimize.leastsq
fails to actually do any optimization leading to fitting "stalling" at some non-optimized value. For example if a NaN
is present in the initial step no actual fitting takes place, such as what was reported in astropy/astropy#12809.
My suggestion is that the model being fit have a filter which removes NaN
(or other non-finite values) from the output of the model.
This maybe a good place to revisit the issue of whether the error being raised should instead be a warning.
If fitting with NaN inputs effectively does nothing, why not have the modeling tools automatically remove/mask the NaN values (with a warning that such values were excluded)? This is what we currently do in sigma_clip
/ SigmaClip
.
If fitting with NaN inputs effectively does nothing, why not have the modeling tools automatically remove/mask the NaN values (with a warning that such values were excluded)? This is what we currently do in
sigma_clip
/SigmaClip
.
I proposed that as an optional feature (better to be optional in case someone does not want it?), and it was rejected with the comment that persons could just wrap their models in such a feature.
Does this block v5.1 release? If so, please update photutils entry at https://github.com/astropy/astropy/wiki/v5.1-RC-testing
@orifox when you said "your script previously worked" do you just mean "It run without and error" or "it gave sensible fits"? I was one of the people that argued that it's better to fail explicitly, than to give silently wrong results. My understanding is that with an older version of astropy, your script passed, but in fact it would not have executed the fit, but just returned your input values or some random value the fitter happened to try when it first encountered a nan or inf.
I imagine that could be bad, let's say you end up with a catalog of fitted sources and believe their magnitudes, when in fact, the fit never fully run.
Do you know if the numbers you got with the previous astropy are good (maybe I'm not understanding correctly how the fitting code works)? If not, would be agree that an error is the best way to alert a user (as it's not in the new astropy version) or what would you prefer to happen?
Hi @hamogu Good question. By "previously worked", I meant it "ran without error." I am not particularly in the business (yet) of science validation of photutils. But you bring up a great point. If I was getting bad photometry, I didn't know it. Of course, I'd prefer to know, but perhaps that output can be as a NAN or upper limit or huge error bar rather than crashing the code. My data sets are so large, there is no way I can fix every pixel/star so that the models work every time. In other words, I need the code to complete the fitting in some capacity so that I can view the results rather than just crash. I'm happy to discuss other feasible solutions.
My suggestion is that the model being fit have a filter which removes NaN (or other non-finite values) from the output of the model.
@WilliamJamieson How would that work exactly? For example, if I'm trying to fit my data (with NaNs) with the astropy Gaussian2D
model, then I would need to rewrite the Gaussian2D
model to add a filter? Wouldn't every model in astropy need to be rewritten to filter non-finite values? If that's the case, why not do it for every model (even user-defined custom models) in a single place at the fitting stage?
IMHO, I think the sensible thing to do here is have the fitting code automatically remove the non-finite values (with a warning). I see that @astrofrog proposed exactly that 7 years ago (https://github.com/astropy/astropy/issues/3575), but apparently it never went anywhere.
As I said 7 years ago, I still think that removing nans silently in general is not a good idea since usually a nan or inf means that something went wrong. However, in the specific case of photutils it's probably safe to do that. In images, we know that nans will comes from cosmics, masked overexposed regions etc. and the rest of the image is fine. So, I suggest that photutils, not astropy, does something like (pseudo-code):
index = ~np.isnan(image)
m_best = fit(m_init, xy[index], image[index])
or, more precisely, that we insert one new line before https://github.com/astropy/photutils/blob/main/photutils/psf/photometry.py#L425 :
usepixel = usepixel & np.isfinite(image)
fit_model = self.fitter(group_psf, x[usepixel], y[usepixel],
image[usepixel])
(This code was written 6 years ago by @mirca as part of a GSOC project.)
@WilliamJamieson How would that work exactly? For example, if I'm trying to fit my data (with NaNs) with the astropy
Gaussian2D
model, then I would need to rewrite theGaussian2D
model to add a filter? Wouldn't every model in astropy need to be rewritten to filter non-finite values? If that's the case, why not do it for every model (even user-defined custom models) in a single place at the fitting stage?
I think the simplest place to put this is in the fitter itself, as I originally proposed in astropy/astropy#12811, that is one can add an option to the fitter __call__
, say filter_non_finite=True
, which turns on the fitter. I do not think it should be enabled by default; however, because filtering out some data points changes the fitting problem itself meaning users should be aware of this.
astropy/astropy#13257 is merged. Is there a PR here that should go in first before RC2 or actual v5.1 release?
I think @WilliamJamieson is planning to submit a PR on adding the filter_non_finite
option, but that can wait until after the 5.1 release.
I see that #1350 is merged. Does that mean we no longer need https://github.com/astropy/astropy/pull/13259 ?
No, I think https://github.com/astropy/astropy/pull/13259 is a useful addition!
I'm working on PSF Fitting using Photutils. My script previously worked (astropy == 5.0.4 ) worked. I upgraded astropy, and now it fails. Although I'm not exactly sure why. Error message below. Nothing has changed.
Now (astropy == 5.2.dev75+ga3c27114b):