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

Send more specific error message #97

Merged
merged 1 commit into from
Oct 17, 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
5 changes: 4 additions & 1 deletion sample_size/multiple_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def get_multiple_sample_size(
if np.isclose(self.power, expected_power, atol=epsilon):
return candidate
elif lower == upper:
raise RecursionError(f"Couldn't find a sample size that satisfies the power you requested: {self.power}")
if expected_power > self.power:
raise RecursionError("Unusually small sample size. Please verify input parameters")
else:
raise RecursionError("Unusually large sample size. Please verify input parameters")

if expected_power > self.power:
return self.get_multiple_sample_size(lower, candidate, random_state, depth + 1)
Expand Down
18 changes: 9 additions & 9 deletions tests/sample_size/test_multiple_testing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
from itertools import cycle
from itertools import product
from unittest.mock import patch

Expand Down Expand Up @@ -92,26 +91,27 @@ def test_get_multiple_sample_size_no_recursion(self, mock_expected_average_power
mock_expected_average_power.assert_called_once_with(geom_mean, RANDOM_STATE, DEFAULT_REPLICATION)
self.assertEqual(sample_size, geom_mean)

@parameterized.expand([(1.0, "small"), (0.0, "large")])
@patch("sample_size.sample_size_calculator.SampleSizeCalculator._get_single_sample_size")
@patch("sample_size.multiple_testing.MultipleTestingMixin._expected_average_power")
def test_get_multiple_sample_size_converges_without_solution(self, mock_expected_power):
mock_expected_power.return_value = 0

def test_get_multiple_sample_size_converges_without_solution(
self, power, error, mock_expected_power, mock__expected_average_power
):
mock_expected_power.return_value = power
mock__expected_average_power.return_value = 1000.0
calculator = SampleSizeCalculator()
calculator.register_metrics([self.test_metric, self.test_metric])

with self.assertRaises(Exception) as context:
calculator.get_sample_size()
self.assertEqual(
str(context.exception),
f"Couldn't find a sample size that satisfies the power you requested: {DEFAULT_POWER}",
f"Unusually {error} sample size. Please verify input parameters",
)

@patch("sample_size.multiple_testing.MultipleTestingMixin._expected_average_power")
def test_get_multiple_sample_size_does_not_converge(self, mock_expected_power):
delta = 2 * DEFAULT_EPSILON
alternating_power = cycle([DEFAULT_POWER - delta, DEFAULT_POWER + delta])
mock_expected_power.side_effect = lambda *_: next(alternating_power)

mock_expected_power.return_value = 0.0
calculator = SampleSizeCalculator()
calculator.register_metrics([self.test_metric, self.test_metric])

Expand Down
Loading