Skip to content

Commit

Permalink
Test if a draw function is a function in validate_draw_function (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
twallema authored Jul 15, 2024
1 parent f7d8ec4 commit 09355c1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/pySODM/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ def sim(self, time, warmup=0, N=1, draw_function=None, samples=None, processes=N

# Provinding 'N' but no draw function: wastefull
if ((N != 1) & (draw_function==None)):
raise ValueError('performing N={0} repeated simulations without a `draw_function` is mighty wastefull of computational resources'.format(N))
raise ValueError('performing N={0} repeated simulations without using a `draw_function` is wastefull of computational resources'.format(N))

# Copy parameter dictionary --> dict is global
cp = copy.deepcopy(self.parameters)
Expand Down
9 changes: 7 additions & 2 deletions src/pySODM/models/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ def validate_draw_function(draw_function, parameters, samples):
an updated dictionary of model parameters
"""

# check if it is a function
if not callable(draw_function):
raise TypeError(
f"a 'draw function' must be callable (a function)"
)
# verify that names of draw function input args are 'parameters' and 'samples
sig = inspect.signature(draw_function)
keywords = list(sig.parameters.keys())
# Verify that names of draw function input args are 'parameters' and 'samples
if len(keywords) != 2:
raise ValueError(
f"Your draw function '{draw_function.__name__}' must have 'parameters' and 'samples' as the names of its inputs. Its current inputs are named: {keywords}"
Expand All @@ -95,7 +100,7 @@ def validate_draw_function(draw_function, parameters, samples):
raise ValueError(
f"Your draw function '{draw_function.__name__}' must have 'parameters' and 'samples' as the names of its inputs. Its current inputs are named: {keywords}"
)
# Call draw function
# call draw function and check its outputs
cp_draws=copy.deepcopy(parameters)
d = draw_function(parameters, samples)
parameters = cp_draws
Expand Down
6 changes: 6 additions & 0 deletions src/tests/test_JumpProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,12 @@ def draw_function(parameters, samples):
# assert dimension 'draws' is present in output
assert 'draws' in list(output.sizes.keys())

# wrong draw function: not a function
time = [0, 10]
model = SIRstratified(initial_states, parameters, coordinates=coordinates)
with pytest.raises(TypeError, match="a 'draw function' must be callable"):
model.sim(time, draw_function='bliblablu', samples={}, N=5)

# wrong draw function: not enough input arguments
def draw_function(parameters):
return parameters
Expand Down
6 changes: 6 additions & 0 deletions src/tests/test_ODE.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,12 @@ def draw_function(parameters, samples):
# assert dimension 'draws' is present in output
assert 'draws' in list(output.sizes.keys())

# wrong draw function: not a function
time = [0, 10]
model = SIRstratified(initial_states, parameters, coordinates=coordinates)
with pytest.raises(TypeError, match="a 'draw function' must be callable"):
model.sim(time, draw_function='bliblablu', samples={}, N=5)

# wrong draw function: not enough input arguments
def draw_function(parameters):
return parameters
Expand Down

0 comments on commit 09355c1

Please sign in to comment.