JuliaAstro / PSFModels.jl

Fast, allocation-free PSF representations
http://juliaastro.org/PSFModels.jl/
MIT License
10 stars 2 forks source link

[bug] model does not fit correctly when the image is not normalized #11

Open kunzaatko opened 2 years ago

kunzaatko commented 2 years ago

Hi! Like your work! There is a slight problem when trying to fit a model to bead image in practice... The optimization without specifying the amp parameter does not work with real bead images, because they are usually not normalized to (0,1). For example, I have this image of a bead: unnormalized_bead

and the fitted gaussian PSF model gives: unnormalized_fit

When I normalize the bead image by

bead .-= minimum(bead)
bead ./= maximum(bead)

then the image looks like this: normalized_bead

and the fitted gaussian PSF model looks like this: normalized_fit

Which is a much better fit. Note, that just specifying the amp = 1 parameter would not help in this case, because it makes the optimization much more likely to land at a local optimum. This could be solved only by setting the amp parameter very close to the true value. Here is the result when optimizing against amp also with the same bead: unnormalized_fit_with_amp

kunzaatko commented 2 years ago

The best way to approach this would probably be to predefine the amp before the fitting to maximum(im) or to fit a normalized image.

kunzaatko commented 2 years ago

Alternatively, it could be only noted in the docs so that users are cautious .

mileslucas commented 2 years ago

I'm not sure this is a bug, like you said if you want to fit the amplitude, you need to specify it, otherwise you could do

func_kwargs = (;amps=fixed_amp_value)
PSFModels.fit(...; func_kwargs=func_kwargs)

Alternatively, it could be only noted in the docs so that users are cautious

PRs are welcome! As a note, by the way, all of the astropy PSF models require an amplitude, and don't make any assumptions about normalized or unnormalized data, the same as we do here. Are there other tools you are used to that "automatically" apply these pre-processing steps? If, so, I'd be interested in checking them out!

kunzaatko commented 2 years ago

Right.., I think bug was not the right word. It is just that the 'default' method for fitting does not adhere to the probably most frequent use of it... That is that fitting the normalized PSF model into a most surely unnormalized view into the image. Right? I will go through the docs and add some comment about this. With that, I think that it would be settled. Would that be OK?