rhayes777 / PyAutoFit

PyAutoFit: Classy Probabilistic Programming
https://pyautofit.readthedocs.io/
MIT License
60 stars 11 forks source link

Issue with model linking due to identical class names #684

Closed Jammy2211 closed 1 year ago

Jammy2211 commented 1 year ago

I am running the following autolens pipeline:

https://github.com/Jammy2211/autolens_workspace_test/blob/main/slam/integration/source_pix/mass_total/linear_parametric.py

My intention is for the two lens galaxy light profiles to be linear light profiles, as specified by this code (note the lp_linear):

source_lp_results = slam.source_lp.run(
    settings_autofit=settings_autofit,
    analysis=analysis,
    setup_hyper=setup_hyper,
    lens_bulge=af.Model(al.lp_linear.Sersic), # < This
    lens_disk=af.Model(al.lp_linear.Sersic), # < This
    mass=af.Model(al.mp.Isothermal),
    shear=af.Model(al.mp.ExternalShear),
    source_bulge=af.Model(al.lp_linear.Sersic),
    mass_centre=(0.0, 0.0),
    redshift_lens=redshift_lens,
    redshift_source=redshift_source,
)

The fit runs as expected, with linear light profiles used for the lens_bulge and lens_disk`.

I then run a second pipeline, where the lens_bulge and lens_disk` are setup via prior passing:

    model_1 = af.Collection(
        galaxies=af.Collection(
            lens=af.Model(
                al.Galaxy,
                redshift=source_lp_results.last.instance.galaxies.lens.redshift,
                bulge=source_lp_results.last.instance.galaxies.lens.bulge, # < This
                disk=source_lp_results.last.instance.galaxies.lens.disk, # < This
                mass=source_lp_results.last.instance.galaxies.lens.mass,
                shear=source_lp_results.last.instance.galaxies.lens.shear,
                hyper_galaxy=setup_hyper.hyper_galaxy_lens_from(
                    result=source_lp_results.last
                ),
            ),
            source=af.Model(
                al.Galaxy,
                redshift=source_lp_results.last.instance.galaxies.source.redshift,
                pixelization=af.Model(
                    al.Pixelization, mesh=mesh, regularization=regularization
                ),
            ),
        ),
        clumps=slam_util.clumps_from(result=source_lp_results.last),
    )
    model_2 = af.Collection(
        galaxies=af.Collection(
            lens=af.Model(
                al.Galaxy,
                redshift=result_1.instance.galaxies.lens.redshift,
                bulge=result_1.instance.galaxies.lens.bulge, # < This
                disk=result_1.instance.galaxies.lens.disk, # < This
                mass=mass,
                shear=source_lp_results.last.model.galaxies.lens.shear,
                hyper_galaxy=result_1.instance.galaxies.lens.hyper_galaxy,
            ),
            source=af.Model(
                al.Galaxy,
                redshift=result_1.instance.galaxies.source.redshift,
                pixelization=result_1.instance.galaxies.source.pixelization,
            ),
        ),
        clumps=slam_util.clumps_from(result=source_lp_results.last),
    )

The problem, is that in the fit performed using model_2 (named "source_pix[2]_light[fixed]_mass[total]_source[pix]") in the pipeline, the bulge and disk are no longer linear light profiles. They are instead ordinary light profiles -- their type has changed!

The problem is due to how I decided to structure the light profile package in PyAutoGalaxy:

https://github.com/Jammy2211/PyAutoGalaxy/tree/main/autogalaxy/profiles/light

The linear light profiles (e.g. lp_linear) are in the folder linear whereas the ordinary light profiles are in the folder standard.

Note that light profiles in both folders, although different, has the same names, like the Sersic profiles used in this example:

https://github.com/Jammy2211/PyAutoGalaxy/blob/main/autogalaxy/profiles/light/linear/sersic.py https://github.com/Jammy2211/PyAutoGalaxy/blob/main/autogalaxy/profiles/light/standard/sersic.py

After inspecting the .identifer file of these fits, it looks like the path to the light profile is not accounted for by PyAutoFit, hence why it does not know which type of light profile to pass and why it is changing in these fits.

So, the question is, should I change the PyAutoGalaxy API so that the class names of light profiles are always unique (e.g. Sersic and SersicLinear)? Which is a bit of an eye-sore.

Or, can we update PyAutoFit to account for path when prior passing?

Jammy2211 commented 1 year ago

The issue is not identical class names as autofit uses unique class ids, it is instead code in autogalaxy.