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:
feature/constant comparison
Resolves #1079


Patch:
diff --git a/autofit/__init__.py b/autofit/__init__.py
index 45df9ff56..3889ebd10 100644
--- a/autofit/__init__.py
+++ b/autofit/__init__.py
@@ -38,6 +38,7 @@ from .mapper.model_mapper import ModelMapper
 from .mapper.model_mapper import ModelMapper as Mapper
 from .mapper.model_object import ModelObject
 from .mapper.operator import DiagonalMatrix
+from .mapper.prior.constant import Constant
 from .mapper.prior.arithmetic.assertion import ComparisonAssertion
 from .mapper.prior.arithmetic.assertion import ComparisonAssertion
 from .mapper.prior.arithmetic.assertion import GreaterThanLessThanAssertion
diff --git a/autofit/mapper/prior/constant.py b/autofit/mapper/prior/constant.py
index cf5fb205b..3b180ab1f 100644
--- a/autofit/mapper/prior/constant.py
+++ b/autofit/mapper/prior/constant.py
@@ -1,8 +1,10 @@
+from numbers import Number
+
 from autofit.mapper.model_object import ModelObject
 
 
-class Constant(ModelObject):
-    def __init__(self, value: float):
+class Constant(ModelObject, Number):
+    def __init__(self, value: float, id_=None):
         """
         Represents a constant value in a model.
 
@@ -16,7 +18,9 @@ class Constant(ModelObject):
         value
             The constant value.
         """
-        super().__init__()
+        super().__init__(
+            id_=id_,
+        )
         self.value = value
 
     def __str__(self):
@@ -62,3 +66,15 @@ class Constant(ModelObject):
 
     def __rpow__(self, other):
         return other**self.value
+
+    def __lt__(self, other):
+        return self.value < other
+
+    def __le__(self, other):
+        return self.value <= other
+
+    def __gt__(self, other):
+        return self.value > other
+
+    def __ge__(self, other):
+        return self.value >= other


Test patch:
diff --git a/test_autofit/mapper/test_constant.py b/test_autofit/mapper/test_constant.py
index 6f61307d9..9eb1a05f3 100644
--- a/test_autofit/mapper/test_constant.py
+++ b/test_autofit/mapper/test_constant.py
@@ -1,3 +1,5 @@
+from numbers import Number
+
 import pytest
 
 import autofit as af
@@ -91,3 +93,51 @@ def test_set_value_in_collection():
     collection[1] = 2.0
     assert collection[1] == 2.0
     assert collection[0] != collection[1]
+
+
+def test_equality_comparison():
+    assert af.Constant(1.0) == 1.0
+    assert 1.0 == af.Constant(1.0)
+
+
+def test_inequality():
+    assert af.Constant(1.0) != 2.0
+    assert 2.0 != af.Constant(1.0)
+    assert af.Constant(1.0) != 2.0
+    assert 2.0 != af.Constant(1)
+
+
+def test_lt():
+    assert af.Constant(1.0) < 2.0
+    assert 1.0 < af.Constant(2.0)
+    assert not af.Constant(2.0) < 1.0
+    assert not 2.0 < af.Constant(1.0)
+    assert af.Constant(1.0) < Constant(2.0)
+
+
+def test_gt():
+    assert af.Constant(2.0) > 1.0
+    assert 2.0 > af.Constant(1.0)
+    assert not af.Constant(1.0) > 2.0
+    assert not 1.0 > af.Constant(2.0)
+    assert af.Constant(2.0) > Constant(1.0)
+
+
+def test_le():
+    assert af.Constant(1.0) <= 2.0
+    assert 1.0 <= af.Constant(2.0)
+    assert not af.Constant(2.0) <= 1.0
+    assert not 2.0 <= af.Constant(1.0)
+    assert af.Constant(1.0) <= Constant(2.0)
+
+
+def test_ge():
+    assert af.Constant(2.0) >= 1.0
+    assert 2.0 >= af.Constant(1.0)
+    assert not af.Constant(1.0) >= 2.0
+    assert not 1.0 >= af.Constant(2.0)
+    assert af.Constant(2.0) >= Constant(1.0)
+
+
+def test_is_number():
+    assert isinstance(Constant(1.0), Number)