From ec4c69411e2030fa81435aaf5e72ffe40538f6a2 Mon Sep 17 00:00:00 2001 From: norases Date: Fri, 1 Aug 2014 16:36:38 -0700 Subject: [PATCH] Added ValueError test for Epsilon class and comments about raising ValueError. --- moe/bandit/epsilon.py | 3 ++- moe/bandit/epsilon_first.py | 1 + moe/bandit/epsilon_greedy.py | 1 + moe/tests/bandit/epsilon_test.py | 16 ++++++++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/moe/bandit/epsilon.py b/moe/bandit/epsilon.py index a0dce0ae..8de04f6c 100644 --- a/moe/bandit/epsilon.py +++ b/moe/bandit/epsilon.py @@ -54,10 +54,11 @@ def get_winning_arm_names(arms_sampled): :type arms_sampled: dictionary of (String(), SampleArm()) pairs :return: of set of names of the winning arms :rtype: frozenset(String()) + :raise: ValueError when ``arms_sampled`` are empty. """ if not arms_sampled: - raise ValueError('sample_arms is empty!') + raise ValueError('arms_sampled is empty!') avg_payoff_arm_name_list = [] for arm_name, sampled_arm in arms_sampled.iteritems(): diff --git a/moe/bandit/epsilon_first.py b/moe/bandit/epsilon_first.py index a099f877..d5e48b1c 100644 --- a/moe/bandit/epsilon_first.py +++ b/moe/bandit/epsilon_first.py @@ -85,6 +85,7 @@ def allocate_arms(self): :return: the dictionary of (arm, allocation) key-value pairs :rtype: a dictionary of (String(), float64) pairs + :raise: ValueError when ``sample_arms`` are empty. """ arms_sampled = self._historical_info.arms_sampled diff --git a/moe/bandit/epsilon_greedy.py b/moe/bandit/epsilon_greedy.py index ffdad037..d1dfa932 100644 --- a/moe/bandit/epsilon_greedy.py +++ b/moe/bandit/epsilon_greedy.py @@ -60,6 +60,7 @@ def allocate_arms(self): :return: the dictionary of (arm, allocation) key-value pairs :rtype: a dictionary of (String(), float64) pairs + :raise: ValueError when ``sample_arms`` are empty. """ arms_sampled = self._historical_info.arms_sampled diff --git a/moe/tests/bandit/epsilon_test.py b/moe/tests/bandit/epsilon_test.py index 318dc1ac..0ba37d57 100644 --- a/moe/tests/bandit/epsilon_test.py +++ b/moe/tests/bandit/epsilon_test.py @@ -4,6 +4,8 @@ Test functions in :class:`moe.bandit.epsilon.Epsilon` """ +import logging + import testify as T from moe.bandit.epsilon import Epsilon @@ -14,6 +16,20 @@ class EpsilonTest(EpsilonTestCase): """Verify that different sample_arms return correct results.""" + @T.class_setup + def disable_logging(self): + """Disable logging (for the duration of this test case).""" + logging.disable(logging.CRITICAL) + + @T.class_teardown + def enable_logging(self): + """Re-enable logging (so other test cases are unaffected).""" + logging.disable(logging.NOTSET) + + def test_empty_arm_invalid(self): + """Test empty ``sample_arms`` causes an ValueError.""" + T.assert_raises(ValueError, Epsilon.get_winning_arm_names, {}) + def test_two_new_arms(self): """Check that the two-new-arms case always returns both arms as winning arms. This tests num_winning_arms == num_arms > 1.""" T.assert_sets_equal(Epsilon.get_winning_arm_names(self.two_new_arms_test_case.arms_sampled), frozenset(["arm1", "arm2"]))