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
15 changes: 12 additions & 3 deletions src/pyrecest/filters/discrete_state/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,25 @@ def sparse_gaussian_transition_matrix(
valid_state_mask=None,
):
states = _validated_state_vectors(state_vectors)
if states.ndim == 1:
states = states[:, None]
elif states.ndim != 2:
raise ValueError(
"state_vectors must have shape (n_states,) or (n_states, state_dim)"
)
n_states = states.shape[0]
if n_states == 0:
raise ValueError("state_vectors must contain at least one state")
if states.shape[1] == 0:
raise ValueError("state_vectors must contain at least one coordinate per state")

sigma = _validated_positive_scalar(sigma, "sigma")
max_step_sigma = _validated_positive_scalar(
max_step_sigma,
"max_step_sigma",
allow_infinite=True,
)

if states.ndim == 1:
states = states[:, None]
n_states = states.shape[0]
valid_mask = _module_globals["_coerce_valid_state_mask"](
valid_state_mask,
n_states,
Expand Down
23 changes: 23 additions & 0 deletions tests/filters/test_sparse_gaussian_transition_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import numpy as np
import pytest

from pyrecest.filters.discrete_state import sparse_gaussian_transition_matrix


@pytest.mark.parametrize(
("state_vectors", "message"),
[
(np.array(1.0), "state_vectors must have shape"),
(np.empty((2, 1, 1)), "state_vectors must have shape"),
(np.empty((0,)), "state_vectors must contain at least one state"),
(
np.empty((2, 0)),
"state_vectors must contain at least one coordinate per state",
),
],
)
def test_sparse_gaussian_transition_matrix_rejects_invalid_state_shapes(
state_vectors, message
):
with pytest.raises(ValueError, match=message):
sparse_gaussian_transition_matrix(state_vectors, sigma=1.0)
Loading