Autostronomy / AstroPhot

A fast, flexible, automated, and differentiable astronomical image 2D forward modelling tool for precise parallel multi-wavelength photometry
https://astrophot.readthedocs.io
GNU General Public License v3.0
87 stars 9 forks source link

Modelling gets stuck when re-using the same model object for many targets #200

Closed entaylor closed 3 months ago

entaylor commented 4 months ago

Describe the bug A clear and concise description of what the bug is.

My fits are getting stuck at the boundary of the allowed fitting range, and i cannot find an easy way to reset/recover so that the model object is reusable.

I want to fit many stars with the same PSF model.

My strategy is to define a PSF model like:

        # create the PSF_image object to define shape
        psf_target = astrophot.image.PSF_Image(
            data=empirical_psf, pixelscale=pixelscale )
        # define starting values and limits for (moffat) profile fitting
        psf_model_pars = {
            "n": {"value": 2., "limits": [0.5, 8.]},
            "Rd": {"value:": 2., "limits": [0.5, 20]} }
        # create the astrophot model object that defines the psf model
        psf_model = astrophot.models.AstroPhot_Model(
            name='psf', model_type='moffat psf model',
            parameters=psf_model_pars,
            target=psf_target, pixelscale=pixelscale)

Then, i look over all of my stars, treating each one as a point source with this PSF object, and with psf_mode='full' to get the best fits for the PSF shape parameters for each star.

    for ii, star in enumerate(stars, start=1):
        # now create the star model, including target window
        xcen, ycen = star['x_centroid_pixel'], star['y_centroid_pixel']
        star_model = astrophot.models.AstroPhot_Model(
            name=star['star_name'],
            model_type='point model',
            parameters={"center": [xcen, ycen], "flux": 1.7},
            psf=ppsf_model,
            psf_mode='full',
            target=target,
            pixelscale=1,
            window=window,
        )
        star_model.initialize()

        # do the fit, and update the uncertainties
        star_result = astrophot.fit.LM(star_model).fit()
        star_result.update_uncertainty()

So far, so good.

The issue is that when the fit hits the boundary of allowed values (namely, it hits the maximum allowed value of the shape parameter, which is 8.), then all subsequent fits have the same bad value.

The way that i interpret this is that each subsequent fit is using the previous outcome as its initial guess, and then also when the fit finds itself near to the boundary it cannot easily escape.

One solution would be to create a new PSF object for each star. That would add extra and unnecessary overheads with the initialisation of the same kind of object over and over. But also, what i want to do next is to fit all the stars with the same PSF model, so i would prefer to have each point source use the same PSF model. (Hopefully that's clear?)

I think what i am missing is a way to set or reset the values of my PSF model, so that i can start each star in the loop with the same starting values and the same object.

Is there a simple way to reset the parameters of a (PSF) model so that the same object can be reused for multiple fits?

PS. Sorry i haven't got back to my earlier issues; today is the first day i have managed to get back to this project! But i promise i will, now that the teaching semester has finished ... !

ConnorStoneAstro commented 4 months ago

Hi @entaylor , yes there is a very easy way to reset any parameters you like in this kind of format :) you can just do something like psf_model["n"].value = 2 inside you're loop and that will set the value.

entaylor commented 3 months ago

Awesome; works; thank you! This is what i needed.