From da19a662238c0c478ef16807ad8b618aaa5bc2ee Mon Sep 17 00:00:00 2001 From: Florian Pfaff <6773539+FlorianPfaff@users.noreply.github.com> Date: Tue, 14 Jul 2026 13:54:14 +0200 Subject: [PATCH 1/2] Validate and copy cost adjustment results --- src/pyrecest/utils/cost_matrix_adjustments.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/pyrecest/utils/cost_matrix_adjustments.py b/src/pyrecest/utils/cost_matrix_adjustments.py index f2a56cbaef..1e1f6f83e3 100644 --- a/src/pyrecest/utils/cost_matrix_adjustments.py +++ b/src/pyrecest/utils/cost_matrix_adjustments.py @@ -39,6 +39,11 @@ class CostMatrixAdjustmentResult: diagnostics: Mapping[str, Any] | None = field(default_factory=dict) def __post_init__(self) -> None: + object.__setattr__( + self, + "adjusted_cost_matrix", + _as_cost_matrix(self.adjusted_cost_matrix).copy(), + ) object.__setattr__( self, "diagnostics", From 3c25b4aefb8494001063868eb65c1a27bbaebabc Mon Sep 17 00:00:00 2001 From: Florian Pfaff <6773539+FlorianPfaff@users.noreply.github.com> Date: Tue, 14 Jul 2026 13:54:29 +0200 Subject: [PATCH 2/2] Add cost adjustment result regression tests --- ...ost_matrix_adjustment_result_validation.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/test_cost_matrix_adjustment_result_validation.py diff --git a/tests/test_cost_matrix_adjustment_result_validation.py b/tests/test_cost_matrix_adjustment_result_validation.py new file mode 100644 index 0000000000..679ce365a0 --- /dev/null +++ b/tests/test_cost_matrix_adjustment_result_validation.py @@ -0,0 +1,34 @@ +import unittest + +import numpy as np +import numpy.testing as npt + +from pyrecest.utils.cost_matrix_adjustments import CostMatrixAdjustmentResult + + +class TestCostMatrixAdjustmentResultValidation(unittest.TestCase): + def test_result_takes_ownership_of_adjusted_matrix(self): + matrix = np.array([[1.0, 2.0]], dtype=float) + + result = CostMatrixAdjustmentResult(matrix) + matrix[:] = -7.0 + + npt.assert_allclose(result.adjusted_cost_matrix, np.array([[1.0, 2.0]])) + + def test_result_rejects_invalid_direct_construction(self): + invalid_matrices = ( + np.array([1.0, 2.0]), + np.array([[np.nan]]), + np.array([[-np.inf]]), + np.array([[True]]), + np.array([[1.0 + 0.0j]]), + ) + + for matrix in invalid_matrices: + with self.subTest(matrix=matrix): + with self.assertRaises(ValueError): + CostMatrixAdjustmentResult(matrix) + + +if __name__ == "__main__": + unittest.main()