Jammy2211 / PyAutoLens

PyAutoLens: Open Source Strong Gravitational Lensing
https://pyautolens.readthedocs.io/
MIT License
164 stars 32 forks source link

HyperGalaxyPhase not passing results correctly. #97

Closed Jammy2211 closed 4 years ago

Jammy2211 commented 5 years ago

The HyperGalaxyPhase appears functional, with the first galaxy (that had an image in the previous phase) successfully having its parameter space sampled.

This is shown in the integration test found test/integration/test/lens_only/lens_x1_galaxy_hyper.py and test/integration/test/lens_and_source/lens_mass_x1_source_x1_hyper.py.

However, once the phase has run the code is not passing the results to the results, copy, e.g. this part of the code:

        for name, galaxy in results.last.name_galaxy_tuples:

            optimizer = self.optimizer.copy_with_name_extension(extension=name)
            optimizer.variable.hyper_galaxy = g.HyperGalaxy
            galaxy_image_2d = results.last.image_2d_dict[name]

            # If array is all zeros, galaxy did not have image in previous phase and should be ignored
            if not np.all(galaxy_image_2d==0):

                analysis = self.__class__.Analysis(lens_data=lens_data, model_image_2d=model_image_2d,
                                                    galaxy_image_2d=galaxy_image_2d)
                optimizer.fit(analysis)

                **getattr(results_copy.variable, name).hyper_galaxy = optimizer.variable.hyper_galaxy**
                getattr(results_copy.constant, name).hyper_galaxy = optimizer.constant.hyper_galaxy

        return results_copy

The error message is as follows:

Traceback (most recent call last):
  File "/home/jammy/PycharmProjects/PyAutoLens/test/integration/tests/lens_only/lens_x1_galaxy_hyper.py", line 46, in <module>
    pipeline()
  File "/home/jammy/PycharmProjects/PyAutoLens/test/integration/tests/lens_only/lens_x1_galaxy_hyper.py", line 27, in pipeline
    pipeline.run(data=ccd_data)
  File "/home/jammy/PycharmProjects/PyAutoLens/autolens/pipeline/pipeline.py", line 9, in run
    return self.run_function(runner, data_name, assert_optimizer_pickle_matches=assert_optimizer_pickle_matches)
  File "/home/jammy/PycharmProjects/VirtualEnvs/PyAutoLens/lib/python3.6/site-packages/autofit/tools/pipeline.py", line 157, in run_function
    results.add(phase.phase_name, func(phase, results))
  File "/home/jammy/PycharmProjects/PyAutoLens/autolens/pipeline/pipeline.py", line 7, in runner
    return phase.run(data=data, results=results, mask=mask, positions=positions)
  File "/home/jammy/PycharmProjects/PyAutoLens/autolens/pipeline/phase/phase_imaging.py", line 933, in run
    getattr(results_copy.variable, name).hyper_galaxy = optimizer.variable.hyper_galaxy
AttributeError: 'ModelMapper' object has no attribute 'lens_galaxies_lens'
rhayes777 commented 5 years ago

I've pushed a fix that resolves this issue.

Before we used strings to address an object in the model: e.g. "lens_galaxies_lens"

Would be address the galaxy called lens in the lens_galaxies collection.

Now we use tuples instead. e.g. ("lens_galaxies", "lens")

This avoid ambiguity. I've updated AutoFit to 0.25.0 and made changes to AutoLens.

Note that there is still an issue that seems to be a problem with the existing code in the HyperPhase: AttributeError: 'MultiNest' object has no attribute 'constant'

Jammy2211 commented 5 years ago

This does not seem to have fixed the issue. That is, for a phase's results, variable.hyper_galaxy is None after passing. This is seen in lens_only/lens_x1_hyper_galaxy.py integration test when passing the hyper galaxy using variable.

I have fixed the passing of a 'constant' result

Jammy2211 commented 4 years ago

Fixed.