Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Galaxy.dict() not returning parameters correctly #188

Open
samlange04 opened this issue Mar 25, 2022 · 3 comments
Open

Galaxy.dict() not returning parameters correctly #188

samlange04 opened this issue Mar 25, 2022 · 3 comments
Assignees

Comments

@samlange04
Copy link
Collaborator

When calling the .dict() instance to extract the parameters of a galaxy, there are a few errors:

  1. The type instance is saved as a string rather than a callable to the function (same for individual mass/light profiles)
  2. If a parameter is drawn from a numpy array (e.g. when randomising and drawing), the dict returns the type rather than the value of the parameter.

See the example below:

lens = al.Galaxy(redshift=1.,
                 light = al.lp.EllSersic(intensity = np.array([0.1])[0]),
                 mass=al.mp.EllIsothermal())
lens.dict()

{'light': {'type': 'autogalaxy.profiles.light_profiles.light_profiles.EllSersic',
'centre': (0.0, 0.0),
'elliptical_comps': (0.0, 0.0),
'intensity': {'type': 'numpy.float64'},
'effective_radius': 0.6,
'sersic_index': 4.0},
'mass': {'type': 'autogalaxy.profiles.mass_profiles.total_mass_profiles.EllIsothermal',
'centre': (0.0, 0.0),
'elliptical_comps': (0.0, 0.0),
'einstein_radius': 1.0},
'type': 'autogalaxy.galaxy.galaxy.Galaxy',
'redshift': 1.0,
'pixelization': None,
'regularization': None,
'hyper_galaxy': None}

@Jammy2211
Copy link
Owner

The type instance is saved as a string rather than a callable to the function (same for individual mass/light profiles)

Digging deep into the code, I think this is the intended behavior.

To load a Galaxy (or any Dictable object, e.g. a Plane or Tracer) from a .json file you should be able to do the following:

import json

from autoconf.dictable import Dictable
import autogalaxy as ag

json_file = "galaxy.json"

galaxy = ag.Galaxy(
        redshift=1.0, pixelization=ag.pix.VoronoiMagnification(), regularization=ag.reg.AdaptiveBrightness()
    )

with open(json_file, "w+") as f:
    json.dump(galaxy.dict(), f, indent=4)

with open(json_file, "r+") as f:
    galaxy_dict = json.load(f)

galaxy_from_dict = Dictable.from_dict(galaxy_dict)

print(galaxy_from_dict)

It is a bit odd that doing the following:

galaxy_from_dict = ag.Galaxy.from_dict(galaxy_dict)

Raises the following exception:

<autoarray.inversion.regularization.adaptive_brightness.AdaptiveBrightness object at 0x0000021B2F377BB0>
Traceback (most recent call last):
  File "C:/Users/Jammy/Code/PyAuto/autolens_workspace_test/galaxy_dict.py", line 22, in <module>
    galaxy_from_dict = ag.Galaxy.from_dict(galaxy_dict)
  File "C:\Users\Jammy\Code\PyAuto\PyAutoFit\autofit\mapper\model_object.py", line 80, in from_dict
    type_ = d["type"]
KeyError: 'type'

@rhayes777 I guess this is because it is expecting that the Galaxy was output as a Model object, as opposed to an instance of a Galaxy?

@Jammy2211
Copy link
Owner

If a parameter is drawn from a numpy array (e.g. when randomising and drawing), the dict returns the type rather than the value of the parameter.

I didn't clock before that you were inputting parameters as a value of a NumPy array.

I would simply convert these to floats before inputting them, as things like a Galaxy are not inspecting numpy array inputs:

lens = al.Galaxy(redshift=1.,
                 light = al.lp.EllSersic(intensity = float(np.array([0.1])[0])),
                 mass=al.mp.EllIsothermal())

@Jammy2211
Copy link
Owner

I have put up the following PR which will allow the following API to work:

rhayes777/PyAutoConf#18

        tracer.output_to_json(file_path=json_file)

        tracer_from_json = al.Tracer.from_json(file_path=json_file)
        galaxy.output_to_json(file_path=json_file)

        galaxy_from_json = al.Galaxy.from_json(file_path=json_file)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants