Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some docstrings and add validation testing for deterministic.py #342

Merged
merged 3 commits into from
Jul 30, 2024
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
12 changes: 8 additions & 4 deletions model/src/pyrenew/deterministic/deterministic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ def __init__(
None
"""
self.name = name
self.value = jnp.atleast_1d(value)
self.validate(value)
self.value = jnp.atleast_1d(value)
self.set_timeseries(t_start, t_unit)

return None

@staticmethod
def validate(value: ArrayLike) -> None:
"""
Validates input to DeterministicPMF
Validates input to DeterministicVariable
Parameters
----------
Expand All @@ -63,10 +63,14 @@ def validate(value: ArrayLike) -> None:
Raises
------
Exception
If the input value object is not a ArrayLike.
If the input value object is not an ArrayLike object.
"""
if not isinstance(value, ArrayLike):
raise Exception("value is not a ArrayLike")
raise ValueError(
f"value {value} passed to a DeterministicVariable "
f"is of type {type(value).__name__}, expected "
"an ArrayLike object"
)
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved

return None

Expand Down
50 changes: 50 additions & 0 deletions model/src/test/test_deterministic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# numpydoc ignore=GL08

import re

import jax.numpy as jnp
import numpy as np
import numpy.testing as testing
import pytest
from pyrenew.deterministic import (
DeterministicPMF,
DeterministicProcess,
Expand Down Expand Up @@ -62,3 +66,49 @@ def test_deterministic():

testing.assert_equal(var4()[0].value, None)
testing.assert_equal(var5(duration=1)[0].value, None)


def test_deterministic_validation():
"""
Check that validation methods for DeterministicVariable
work as expected.
"""
# validation should fail on construction
some_non_array_likes = [
{"a": jnp.array([1, 2.5, 3])},
# a valid pytree, but not an arraylike
"a string",
]
some_array_likes = [
5,
-3.023523,
np.array([1, 3.32, 5]),
jnp.array([-32, 23]),
jnp.array(-32),
np.array(5),
]

for non_arraylike_val in some_non_array_likes:
matchval = re.escape(
f"value {non_arraylike_val} passed to a "
"DeterministicVariable is of type "
f"{type(non_arraylike_val).__name__}, expected "
"an ArrayLike object"
)

with pytest.raises(ValueError, match=matchval):
# the class's validation function itself
# should raise an error when passed a
# non arraylike value
DeterministicVariable.validate(non_arraylike_val)

with pytest.raises(ValueError, match=matchval):
# validation should fail on constructor call
DeterministicVariable(
value=non_arraylike_val, name="invalid_variable"
)

# validation should succeed with ArrayLike
for arraylike_val in some_array_likes:
DeterministicVariable.validate(arraylike_val)
DeterministicVariable(value=arraylike_val, name="valid_variable")