diff --git a/src/pyrecest/smoothers/abstract_smoother.py b/src/pyrecest/smoothers/abstract_smoother.py index 6c9f1e9e9..1af479d23 100644 --- a/src/pyrecest/smoothers/abstract_smoother.py +++ b/src/pyrecest/smoothers/abstract_smoother.py @@ -58,7 +58,7 @@ def _normalize_matrix_sequence( # pylint: disable=too-many-return-statements,to try: values_arr = asarray(values) - except (TypeError, ValueError): + except (TypeError, ValueError, RuntimeError): values_arr = None if values_arr is not None: if ndim(values_arr) == 0: diff --git a/tests/smoothers/test_abstract_smoother_matrix_sequence_runtime_error.py b/tests/smoothers/test_abstract_smoother_matrix_sequence_runtime_error.py new file mode 100644 index 000000000..c38b3f8c4 --- /dev/null +++ b/tests/smoothers/test_abstract_smoother_matrix_sequence_runtime_error.py @@ -0,0 +1,30 @@ +import numpy as np + +import pyrecest.smoothers.abstract_smoother as abstract_smoother_module +from pyrecest.smoothers.abstract_smoother import AbstractSmoother + + +def test_matrix_sequence_falls_back_after_backend_runtime_error(monkeypatch): + matrices = [np.eye(2), 2.0 * np.eye(2)] + backend_asarray = abstract_smoother_module.asarray + + def asarray_with_outer_conversion_failure(value): + if value is matrices: + raise RuntimeError("backend cannot stack the matrix sequence") + return backend_asarray(value) + + monkeypatch.setattr( + abstract_smoother_module, + "asarray", + asarray_with_outer_conversion_failure, + ) + + normalized = AbstractSmoother._normalize_matrix_sequence( + matrices, + length=2, + name="transition_matrices", + matrix_dim=2, + ) + + np.testing.assert_allclose(normalized[0], matrices[0]) + np.testing.assert_allclose(normalized[1], matrices[1])