Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/pyrecest/utils/cost_matrix_adjustments.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def compose_cost_matrix_adjustments(

matrix = _as_cost_matrix(cost_matrix)
diagnostics: dict[str, Any] = {"adjustment_order": []}
diagnostic_keys: set[str] = set()
diagnostic_keys: set[str] = set(diagnostics)
current = matrix
for index, adjustment in enumerate(adjustments):
base_name = _adjustment_name(adjustment, index=index)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_cost_matrix_adjustments.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,35 @@ def test_compose_adjustments_disambiguates_duplicate_diagnostics(self):
self.assertEqual(result.diagnostics["shared"], {"step": 1})
self.assertEqual(result.diagnostics["shared_2"], {"step": 2})

def test_compose_adjustments_protects_reserved_order_diagnostics_key(self):
reserved_name = CallableCostMatrixAdjustment(
name="adjustment_order",
function=lambda matrix, _metadata: CostMatrixAdjustmentResult(
matrix + 1.0,
{"step": 1},
),
)
following = CallableCostMatrixAdjustment(
name="following",
function=lambda matrix, _metadata: CostMatrixAdjustmentResult(
matrix + 2.0,
{"step": 2},
),
)

result = compose_cost_matrix_adjustments(
np.array([[0.0]]),
[reserved_name, following],
)

npt.assert_allclose(result.adjusted_cost_matrix, np.array([[3.0]]))
self.assertEqual(
result.diagnostics["adjustment_order"],
["adjustment_order_2", "following"],
)
self.assertEqual(result.diagnostics["adjustment_order_2"], {"step": 1})
self.assertEqual(result.diagnostics["following"], {"step": 2})

def test_additive_adjustment_rejects_shape_mismatch(self):
adjustment = additive_cost_matrix_adjustment(np.ones((2, 2)))

Expand Down
Loading