Esempio n. 1
0
class BaseTest(unittest.TestCase):
    def setUp(self):
        """
        Set default start setting for all tests.
        """
        self.intger_range = IntegerRange(2, 6)
        self.float_range = FloatRange(0.1, 5.7)
        self.categorical = Categorical(
            ["a", "b", "c", "d", "e", "f", "g", "h"])
        self.bool = BooleanSwitch()

    def test_rand_success(self):

        for _ in range(100):
            self.assertIn(self.intger_range.get_random_value(),
                          list(range(2, 6)))

            self.assertGreaterEqual(self.float_range.get_random_value(), 0.1)
            self.assertLess(self.float_range.get_random_value(), 5.7)

            self.assertIn(
                self.categorical.get_random_value(),
                ["a", "b", "c", "d", "e", "f", "g", "h"],
            )

            self.assertIn(self.bool.get_random_value(), [True, False])

        self.float_range.transform()
        self.intger_range.transform()

        for _ in range(100):
            self.assertIn(
                self.intger_range.get_random_value(definite_list=True),
                self.intger_range.values,
            )
            self.assertIn(
                self.float_range.get_random_value(definite_list=True),
                self.float_range.values,
            )

    def test_rand_error(self):
        with self.assertRaises(ValueError):
            self.intger_range.get_random_value(definite_list=True)
            self.float_range.get_random_value(definite_list=True)
            self.bool.get_random_value(definite_list=True)
            self.categorical.get_random_value(definite_list=True)
Esempio n. 2
0
class HyperparameterBaseTest(unittest.TestCase):
    def setUp(self):
        """
        Set default start setting for all tests.
        """
        self.intger_range = IntegerRange(2, 6)
        self.float_range = FloatRange(0.1, 5.7)
        self.cateogrical_truth = ["a", "b", "c", "d", "e", "f", "g", "h"]
        self.categorical = Categorical(self.cateogrical_truth)
        self.bool = BooleanSwitch()

    def test_rand_success(self):

        for _ in range(100):
            self.assertIn(self.intger_range.get_random_value(),
                          list(range(2, 6)))

            self.assertGreaterEqual(self.float_range.get_random_value(), 0.1)
            self.assertLess(self.float_range.get_random_value(), 5.7)

            self.assertIn(self.categorical.get_random_value(),
                          self.cateogrical_truth)

            self.assertIn(self.bool.get_random_value(), [True, False])

        self.float_range.transform()
        self.intger_range.transform()

        for _ in range(100):
            self.assertIn(
                self.intger_range.get_random_value(definite_list=True),
                self.intger_range.values)
            self.assertIn(
                self.float_range.get_random_value(definite_list=True),
                self.float_range.values)

    def test_domain(self):

        self.float_range.transform()
        self.intger_range.transform()
        self.assertListEqual(self.intger_range.values, list(np.arange(2, 6)))
        self.assertListEqual(self.float_range.values,
                             list(np.linspace(0.1, 5.7, dtype=np.float64)))

        big_float_range = FloatRange(-300.57, np.pi * 4000)
        big_float_range.transform()
        self.assertListEqual(big_float_range.values,
                             list(np.linspace(-300.57, np.pi * 4000)))
        self.assertListEqual(self.categorical.values,
                             ["a", "b", "c", "d", "e", "f", "g", "h"])
        self.assertListEqual(self.bool.values, [True, False])

    def test_rand_error(self):
        with self.assertRaises(ValueError):
            self.intger_range.get_random_value(definite_list=True)
        with self.assertRaises(ValueError):
            self.float_range.get_random_value(definite_list=True)
        with self.assertRaises(NotImplementedError):
            self.categorical.get_random_value(definite_list=False)
        with self.assertRaises(NotImplementedError):
            self.categorical.get_random_value(definite_list=False)

    def test_categorical(self):
        self.assertEqual(self.categorical[2], self.cateogrical_truth[2])