nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: rhayes777/PyAutoFit
Pull request #839 (2023-10-09 14:35:39)

feature/efficienct samples fix
- set assertions on instance created from database regardless of whether any exist
- fix for efficient samples



Patch:
diff --git a/autofit/non_linear/samples/efficient.py b/autofit/non_linear/samples/efficient.py
index 0003767e1..662354a07 100644
--- a/autofit/non_linear/samples/efficient.py
+++ b/autofit/non_linear/samples/efficient.py
@@ -21,7 +21,10 @@ class EfficientSamples:
         self.search_internal = samples.search_internal
 
         sample_list = samples.sample_list
-        self._kwargs = sample_list[0].kwargs
+        self._keys = list(sample_list[0].kwargs.keys())
+        self._values = np.asarray(
+            [[sample.kwargs[key] for key in self._keys] for sample in sample_list]
+        )
         self._log_likelihoods = np.asarray(
             [sample.log_likelihood for sample in sample_list]
         )
@@ -50,9 +53,22 @@ class EfficientSamples:
                 log_likelihood=log_likelihood,
                 log_prior=log_prior,
                 weight=weight,
-                kwargs=self._kwargs,
+                kwargs=kwargs,
             )
-            for log_likelihood, log_prior, weight in zip(
-                self._log_likelihoods, self._log_priors, self._weights
+            for log_likelihood, log_prior, weight, kwargs in zip(
+                self._log_likelihoods,
+                self._log_priors,
+                self._weights,
+                self._kwargs,
             )
         ]
+
+    @property
+    def _kwargs(self) -> List[dict]:
+        """
+        Convert the efficient samples back to a list of dictionaries of kwargs.
+        """
+        return [
+            {key: value for key, value in zip(self._keys, values)}
+            for values in self._values
+        ]


Test patch:
diff --git a/test_autofit/non_linear/samples/test_efficient.py b/test_autofit/non_linear/samples/test_efficient.py
index bc7c2aa82..75600a3bd 100644
--- a/test_autofit/non_linear/samples/test_efficient.py
+++ b/test_autofit/non_linear/samples/test_efficient.py
@@ -16,7 +16,13 @@ def make_samples():
                 log_prior=2.0,
                 weight=4.0,
                 kwargs={"centre": 1.0, "normalization": 2.0, "sigma": 3.0},
-            )
+            ),
+            af.Sample(
+                log_likelihood=1.0,
+                log_prior=2.0,
+                weight=4.0,
+                kwargs={"centre": 4.0, "normalization": 5.0, "sigma": 6.0},
+            ),
         ],
     )
 
@@ -35,6 +41,20 @@ def test_conversion(efficient):
     assert sample.weight == 4.0
 
 
+def test_values(efficient):
+    samples = efficient.samples
+
+    sample_1, sample_2 = samples.sample_list
+
+    assert sample_1.kwargs["centre"] == 1.0
+    assert sample_1.kwargs["normalization"] == 2.0
+    assert sample_1.kwargs["sigma"] == 3.0
+
+    assert sample_2.kwargs["centre"] == 4.0
+    assert sample_2.kwargs["normalization"] == 5.0
+    assert sample_2.kwargs["sigma"] == 6.0
+
+
 def test_database(efficient, session):
     recovered = Object.from_object(efficient)()
     samples = recovered.samples
Repository: rhayes777/PyAutoFit
Pull request #885 (2023-11-29 14:25:02)

fix bug where integer keys in dict caused identifier creation to fail


Patch:
diff --git a/autofit/mapper/identifier.py b/autofit/mapper/identifier.py
index fc1a78817..5a0f050c9 100644
--- a/autofit/mapper/identifier.py
+++ b/autofit/mapper/identifier.py
@@ -128,6 +128,7 @@ class Identifier:
             self.add_value_to_hash_list(d)
         elif isinstance(value, dict):
             for key, value in value.items():
+                key = str(key)
                 if not (key.startswith("_") or key in ("id", "paths")):
                     self.hash_list.append(key)
                     self.add_value_to_hash_list(value)
diff --git a/autofit/non_linear/search/abstract_search.py b/autofit/non_linear/search/abstract_search.py
index a25597155..e3ac8f15d 100644
--- a/autofit/non_linear/search/abstract_search.py
+++ b/autofit/non_linear/search/abstract_search.py
@@ -530,7 +530,9 @@ class NonLinearSearch(AbstractFactorOptimiser, ABC):
                 from pathlib import Path
 
                 search = DynestyStatic(
-                    paths=self.paths,
+                    name=self.name,
+                    unique_tag=self.unique_tag,
+                    path_prefix=Path(self.paths.path_prefix),
                     number_of_cores=self.number_of_cores,
                 )
 


Test patch:
diff --git a/test_autofit/database/identifier/test_identifiers.py b/test_autofit/database/identifier/test_identifiers.py
index 4f908f80b..692ea96f5 100644
--- a/test_autofit/database/identifier/test_identifiers.py
+++ b/test_autofit/database/identifier/test_identifiers.py
@@ -443,3 +443,7 @@ def test_dynesty_static():
         "max_move",
         "100",
     ]
+
+
+def test_integer_keys():
+    assert str(Identifier({1: 1}))
Repository: rhayes777/PyAutoFit
Pull request #1048 (2024-09-13 14:11:47)

feature/fit attrs fix
- **test illustrating issue**
- **fix attribute relationships**



Patch:
diff --git a/autofit/database/model/fit.py b/autofit/database/model/fit.py
index e97c47abd..30b493a4c 100644
--- a/autofit/database/model/fit.py
+++ b/autofit/database/model/fit.py
@@ -319,19 +319,22 @@ class Fit(Base):
     pickles: Mapped[List[Pickle]] = sa.orm.relationship(
         "Pickle",
         lazy="joined",
+        foreign_keys=[Pickle.fit_id],
+    )
+    jsons: Mapped[List[JSON]] = sa.orm.relationship(
+        "JSON",
+        lazy="joined",
+        foreign_keys=[JSON.fit_id],
     )
-    jsons: Mapped[List[JSON]] = sa.orm.relationship("JSON", lazy="joined")
     arrays: Mapped[List[Array]] = sa.orm.relationship(
         "Array",
         lazy="joined",
         foreign_keys=[Array.fit_id],
-        viewonly=True,
     )
     hdus: Mapped[List[HDU]] = sa.orm.relationship(
         "HDU",
         lazy="joined",
         foreign_keys=[HDU.fit_id],
-        viewonly=True,
     )
 
     def __getitem__(self, item: str):


Test patch:
diff --git a/test_autofit/database/test_regression.py b/test_autofit/database/test_regression.py
index c05ef92ba..119731d8a 100644
--- a/test_autofit/database/test_regression.py
+++ b/test_autofit/database/test_regression.py
@@ -1,7 +1,8 @@
 import pytest
+import numpy as np
 
 import autofit as af
-from autofit import database as db
+from autofit import database as db, Fit
 
 
 @pytest.fixture(name="model")
@@ -53,3 +54,18 @@ def test_samples_summary_model():
 def test_dict_with_tuple_keys():
     d = {("a", "b"): 1}
     assert db.Object.from_object(d)() == d
+
+
+def test_persist_values(session):
+    fit = Fit(id=1)
+
+    fit.set_pickle("pickle", "test")
+    fit.set_array("array", np.array([1, 2, 3]))
+
+    session.add(fit)
+    session.commit()
+
+    fit = session.query(Fit).first()
+
+    assert fit["pickle"] == "test"
+    assert fit["array"].tolist() == [1, 2, 3]
Repository: rhayes777/PyAutoFit
Pull request #1098 (2025-01-28 15:02:52)

Revert "feature/constant hash bug"
Reverts rhayes777/PyAutoFit#1097


Patch:
diff --git a/autofit/mapper/model_object.py b/autofit/mapper/model_object.py
index a7ee5cd17..fd2fbd123 100644
--- a/autofit/mapper/model_object.py
+++ b/autofit/mapper/model_object.py
@@ -44,18 +44,9 @@ class ModelObject:
             A label which can optionally be set for visualising this object in a
             graph.
         """
-        self._id = None
-        self.id = id_
+        self.id = int(self.next_id() if id_ is None else 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 d5a95e964..9eb1a05f3 100644
--- a/test_autofit/mapper/test_constant.py
+++ b/test_autofit/mapper/test_constant.py
@@ -141,18 +141,3 @@ 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)
Repository: rhayes777/PyAutoFit
Pull request #1101 (2025-02-14 13:30:17)

Revert "Revert "feature/constant hash bug""
Reverts rhayes777/PyAutoFit#1098


Patch:
diff --git a/autofit/mapper/prior/constant.py b/autofit/mapper/prior/constant.py
index 3b180ab1f..3e1db0c96 100644
--- a/autofit/mapper/prior/constant.py
+++ b/autofit/mapper/prior/constant.py
@@ -32,7 +32,7 @@ class Constant(ModelObject, Number):
         return super().__eq__(other)
 
     def __hash__(self):
-        return self.id
+        return hash(self.id)
 
     def dict(self):
         return {"type": "Constant", "value": self.value}


Test patch:
diff --git a/test_autofit/mapper/test_constant.py b/test_autofit/mapper/test_constant.py
index 9eb1a05f3..671227cb9 100644
--- a/test_autofit/mapper/test_constant.py
+++ b/test_autofit/mapper/test_constant.py
@@ -141,3 +141,29 @@ 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)
+
+
+def test_model_with_constants():
+    instance = af.Model(
+        af.Gaussian,
+        centre=af.Constant(1.0),
+    ).instance_from_prior_medians()
+
+    assert instance.centre == 1.0
+    assert isinstance(instance.centre, float)
+    assert not isinstance(instance.centre, Constant)