Repository: rhayes777/PyAutoFit
Pull request #909 (2024-01-22 11:57:11)

feature/fix covariance serialisation
- fix test
- samples.covariance_matrix as a property for consistency and serialisation



Patch:
diff --git a/autofit/interpolator/covariance.py b/autofit/interpolator/covariance.py
index b35a0677a..fd090d287 100644
--- a/autofit/interpolator/covariance.py
+++ b/autofit/interpolator/covariance.py
@@ -122,7 +122,7 @@ class CovarianceInterpolator(AbstractInterpolator):
 
         This comprises covariance matrices for each sample, subsumed along the diagonal
         """
-        matrices = [samples.covariance_matrix() for samples in self.samples_list]
+        matrices = [samples.covariance_matrix for samples in self.samples_list]
         prior_count = self.samples_list[0].model.prior_count
         size = prior_count * len(self.samples_list)
         array = np.zeros((size, size))
diff --git a/autofit/non_linear/samples/pdf.py b/autofit/non_linear/samples/pdf.py
index 0fcf278c3..1ec05794f 100644
--- a/autofit/non_linear/samples/pdf.py
+++ b/autofit/non_linear/samples/pdf.py
@@ -60,7 +60,7 @@ class SamplesPDF(Samples):
 
     def summary(self):
         try:
-            covariance_matrix = self.covariance_matrix()
+            covariance_matrix = self.covariance_matrix
         except Exception as e:
             logging.warning(f"Could not create covariance matrix: {e}")
             covariance_matrix = None
@@ -389,6 +389,7 @@ class SamplesPDF(Samples):
             )
         )
 
+    @property
     def covariance_matrix(self) -> np.ndarray:
         """
         Compute the covariance matrix of the non-linear search samples, using the method `np.cov()` which is described
@@ -418,7 +419,7 @@ class SamplesPDF(Samples):
             The filename the covariance matrix is saved to.
         """
         # noinspection PyTypeChecker
-        np.savetxt(filename, self.covariance_matrix(), delimiter=",")
+        np.savetxt(filename, self.covariance_matrix, delimiter=",")
 
     @property
     def log_evidence(self):
diff --git a/autofit/non_linear/samples/summary.py b/autofit/non_linear/samples/summary.py
index c34e248e3..f9630248f 100644
--- a/autofit/non_linear/samples/summary.py
+++ b/autofit/non_linear/samples/summary.py
@@ -28,12 +28,9 @@ class SamplesSummary(SamplesInterface):
         """
         super().__init__(model=model)
         self._max_log_likelihood_sample = max_log_likelihood_sample
-        self._covariance_matrix = covariance_matrix
+        self.covariance_matrix = covariance_matrix
         self._log_evidence = log_evidence
 
-    def covariance_matrix(self) -> np.ndarray:
-        return self._covariance_matrix
-
     @property
     def max_log_likelihood_sample(self):
         return self._max_log_likelihood_sample


Test patch:
diff --git a/test_autofit/non_linear/samples/test_pdf.py b/test_autofit/non_linear/samples/test_pdf.py
index ee6a840b8..5f54dabc9 100644
--- a/test_autofit/non_linear/samples/test_pdf.py
+++ b/test_autofit/non_linear/samples/test_pdf.py
@@ -477,7 +477,7 @@ def test__covariance_matrix(make_samples):
         parameters=[[2.0, 2.0], [1.0, 1.0], [0.0, 0.0]],
     )
 
-    assert samples_x5.covariance_matrix() == pytest.approx(
+    assert samples_x5.covariance_matrix == pytest.approx(
         np.array([[1.0, 1.0], [1.0, 1.0]]), 1.0e-4
     )
 
@@ -485,7 +485,7 @@ def test__covariance_matrix(make_samples):
 
     samples_x5 = make_samples(parameters)
 
-    assert samples_x5.covariance_matrix() == pytest.approx(
+    assert samples_x5.covariance_matrix == pytest.approx(
         np.array([[1.0, -1.0], [-1.0, 1.0]]), 1.0e-4
     )
 
@@ -494,6 +494,6 @@ def test__covariance_matrix(make_samples):
         weight_list=[0.1, 0.2, 0.3],
     )
 
-    assert samples_x5.covariance_matrix() == pytest.approx(
+    assert samples_x5.covariance_matrix == pytest.approx(
         np.array([[0.90909, -0.90909], [-0.90909, 0.90909]]), 1.0e-4
     )
diff --git a/test_autofit/serialise/test_samples.py b/test_autofit/serialise/test_samples.py
index 372f5f7cc..a999e10c0 100644
--- a/test_autofit/serialise/test_samples.py
+++ b/test_autofit/serialise/test_samples.py
@@ -46,7 +46,7 @@ def make_summary(samples_pdf):
 def test_summary(summary, model, sample):
     assert summary.model is model
     assert summary.max_log_likelihood_sample == sample
-    assert isinstance(summary.covariance_matrix(), np.ndarray)
+    assert isinstance(summary.covariance_matrix, np.ndarray)
 
 
 @pytest.fixture(name="summary_dict")
@@ -116,7 +116,7 @@ def test_from_dict(summary_dict):
     assert isinstance(summary, SamplesSummary)
     assert isinstance(summary.model, af.Model)
     assert isinstance(summary.max_log_likelihood_sample, af.Sample)
-    assert isinstance(summary.covariance_matrix(), np.ndarray)
+    assert isinstance(summary.covariance_matrix, np.ndarray)
 
     assert isinstance(summary.max_log_likelihood(), af.Gaussian)
 
@@ -126,7 +126,7 @@ def test_generic_from_dict(summary_dict):
     assert isinstance(summary, SamplesSummary)
     assert isinstance(summary.model, af.Model)
     assert isinstance(summary.max_log_likelihood_sample, af.Sample)
-    assert isinstance(summary.covariance_matrix(), np.ndarray)
+    assert isinstance(summary.covariance_matrix, np.ndarray)
 
 
 def test_covariance_interpolator(summary):