Repository: rhayes777/PyAutoFit (python)

Issue:
Error with new Constant type in database
This example script:

https://github.com/Jammy2211/autolens_workspace_test/blob/main/scripts/database/scrape/slam_pix.py

Gives this error:

```
2024-11-21 17:47:48,638 - autofit.database.aggregator.aggregator - INFO - 1 fit(s) found matching query
TracerAgg with linear light profiles raises correct ProfileException
TracerAgg Checked
2024-11-21 17:47:48,756 - autoarray.dataset.imaging.dataset - INFO - IMAGING - Data masked, contains a total of 716 image-pixels
ImagingAgg Checked
2024-11-21 17:47:49,190 - autoarray.dataset.imaging.dataset - INFO - IMAGING - Data masked, contains a total of 716 image-pixels
Traceback (most recent call last):
  File "/mnt/c/Users/Jammy/Code/PyAuto/autolens_workspace_test/scripts/database/scrape/slam_pix.py", line 388, in <module>
    fit_plotter.subplot_fit()
  File "/mnt/c/Users/Jammy/Code/PyAuto/PyAutoLens/autolens/imaging/plot/fit_imaging_plotters.py", line 435, in subplot_fit
    self.figures_2d(data=True)
  File "/mnt/c/Users/Jammy/Code/PyAuto/PyAutoLens/autolens/imaging/plot/fit_imaging_plotters.py", line 755, in figures_2d
    visuals_2d = self.get_visuals_2d()
  File "/mnt/c/Users/Jammy/Code/PyAuto/PyAutoLens/autolens/imaging/plot/fit_imaging_plotters.py", line 71, in get_visuals_2d
    return self.get_2d.via_fit_imaging_from(fit=self.fit)
  File "/mnt/c/Users/Jammy/Code/PyAuto/PyAutoLens/autolens/plot/get_visuals/two_d.py", line 171, in via_fit_imaging_from
    visuals_2d_via_tracer = self.via_tracer_from(
  File "/mnt/c/Users/Jammy/Code/PyAuto/PyAutoLens/autolens/plot/get_visuals/two_d.py", line 80, in via_tracer_from
    tracer.planes[plane_index].extract_attribute(
  File "/mnt/c/Users/Jammy/Code/PyAuto/PyAutoLens/autolens/lens/tracer.py", line 212, in planes
    galaxies=self.galaxies_ascending_redshift,
  File "/mnt/c/Users/Jammy/Code/PyAuto/PyAutoLens/autolens/lens/tracer.py", line 155, in galaxies_ascending_redshift
    return sorted(self.galaxies, key=lambda galaxy: galaxy.redshift)
TypeError: '<' not supported between instances of 'Constant' and 'float'
```

Presuambly the constants refactor.

Pull request description:
feature/constant hash bug
Resolves #1079


Patch:
diff --git a/autofit/mapper/model_object.py b/autofit/mapper/model_object.py
index fd2fbd123..a7ee5cd17 100644
--- a/autofit/mapper/model_object.py
+++ b/autofit/mapper/model_object.py
@@ -44,9 +44,18 @@ class ModelObject:
             A label which can optionally be set for visualising this object in a
             graph.
         """
-        self.id = int(self.next_id() if id_ is None else id_)
+        self._id = None
+        self.id = id_
         self._label = label
 
+    @property
+    def id(self):
+        return self._id
+
+    @id.setter
+    def id(self, id_):
+        self._id = int(self.next_id() if id_ is None else id_)
+
     def replacing_for_path(self, path: Tuple[str, ...], value) -> "ModelObject":
         """
         Create a new model replacing the value for a given path with a new value


Test patch:
diff --git a/test_autofit/mapper/test_constant.py b/test_autofit/mapper/test_constant.py
index 9eb1a05f3..d5a95e964 100644
--- a/test_autofit/mapper/test_constant.py
+++ b/test_autofit/mapper/test_constant.py
@@ -141,3 +141,18 @@ def test_ge():
 
 def test_is_number():
     assert isinstance(Constant(1.0), Number)
+
+
+def test_hash():
+    assert hash(af.Constant(1.0)) != hash(af.Constant(1.0))
+
+    constant = af.Constant(1.0)
+    deserialized = af.Constant.from_dict(constant.dict())
+    assert hash(deserialized)
+
+
+def test_float_id():
+    constant = af.Constant(1.0)
+    constant.id = 1.0
+
+    assert hash(constant)