diff --git a/python_tests/test_cached_extra_study_property.py b/python_tests/test_cached_extra_study_property.py new file mode 100644 index 00000000..2ab8395a --- /dev/null +++ b/python_tests/test_cached_extra_study_property.py @@ -0,0 +1,177 @@ +from unittest import TestCase +import warnings + +import optuna +from optuna import create_trial +from optuna.distributions import UniformDistribution +from optuna.exceptions import ExperimentalWarning +from optuna.trial import TrialState + +from optuna_dashboard._cached_extra_study_property import _CachedExtraStudyProperty + + +class _CachedExtraStudyPropertyTestCase(TestCase): + def setUp(self) -> None: + optuna.logging.set_verbosity(optuna.logging.ERROR) + warnings.simplefilter("ignore", category=ExperimentalWarning) + + def test_same_distributions(self) -> None: + distributions = [ + { + "x0": UniformDistribution(low=0, high=10), + "x1": UniformDistribution(low=0, high=10), + }, + { + "x0": UniformDistribution(low=0, high=10), + "x1": UniformDistribution(low=0, high=10), + }, + ] + params = [ + { + "x0": 0.5, + "x1": 0.5, + }, + { + "x0": 0.5, + "x1": 0.5, + }, + ] + trials = [ + create_trial(state=TrialState.COMPLETE, value=0, distributions=d, params=p) + for d, p in zip(distributions, params) + ] + cached_extra_study_property = _CachedExtraStudyProperty() + cached_extra_study_property.update(trials) + + self.assertEqual(len(cached_extra_study_property.intersection), 2) + self.assertEqual(len(cached_extra_study_property.union), 2) + + def test_different_distributions(self) -> None: + distributions = [ + { + "x0": UniformDistribution(low=0, high=10), + "x1": UniformDistribution(low=0, high=10), + }, + { + "x0": UniformDistribution(low=0, high=5), + "x1": UniformDistribution(low=0, high=10), + }, + ] + params = [ + { + "x0": 0.5, + "x1": 0.5, + }, + { + "x0": 0.5, + "x1": 0.5, + }, + ] + trials = [ + create_trial(state=TrialState.COMPLETE, value=0, distributions=d, params=p) + for d, p in zip(distributions, params) + ] + cached_extra_study_property = _CachedExtraStudyProperty() + cached_extra_study_property.update(trials) + + self.assertEqual(len(cached_extra_study_property.intersection), 1) + self.assertEqual(len(cached_extra_study_property.union), 3) + + def test_dynamic_search_space(self) -> None: + distributions = [ + { + "x0": UniformDistribution(low=0, high=10), + "x1": UniformDistribution(low=0, high=10), + }, + { + "x0": UniformDistribution(low=0, high=5), + }, + { + "x0": UniformDistribution(low=0, high=10), + "x1": UniformDistribution(low=0, high=10), + }, + ] + params = [ + { + "x0": 0.5, + "x1": 0.5, + }, + { + "x0": 0.5, + }, + { + "x0": 0.5, + "x1": 0.5, + }, + ] + trials = [ + create_trial(state=TrialState.COMPLETE, value=0, distributions=d, params=p) + for d, p in zip(distributions, params) + ] + + cached_extra_study_property = _CachedExtraStudyProperty() + cached_extra_study_property.update(trials) + + self.assertEqual(len(cached_extra_study_property.intersection), 0) + self.assertEqual(len(cached_extra_study_property.union), 3) + + def test_no_intermediate_value(self) -> None: + intermediate_values = [ + {}, + {}, + ] + trials = [ + create_trial( + state=TrialState.COMPLETE, + value=0, + distributions={"x0": UniformDistribution(low=0, high=10)}, + intermediate_values=iv, + params={"x0": 0.5}, + ) + for iv in intermediate_values + ] + cached_extra_study_property = _CachedExtraStudyProperty() + cached_extra_study_property.update(trials) + self.assertFalse(cached_extra_study_property.has_intermediate_values) + + def test_some_trials_has_no_intermediate_value(self) -> None: + intermediate_values = [ + {0: 0.3, 1: 1.2}, + {}, + {0: 0.3, 1: 1.2}, + ] + trials = [ + create_trial( + state=TrialState.COMPLETE, + value=0, + distributions={"x0": UniformDistribution(low=0, high=10)}, + intermediate_values=iv, + params={"x0": 0.5}, + ) + for iv in intermediate_values + ] + cached_extra_study_property = _CachedExtraStudyProperty() + cached_extra_study_property.update(trials) + self.assertTrue(cached_extra_study_property.has_intermediate_values) + + def test_all_trials_has_intermediate_value(self) -> None: + intermediate_values = [{0: 0.3, 1: 1.2}, {0: 0.3, 1: 1.2}] + trials = [ + create_trial( + state=TrialState.COMPLETE, + value=0, + distributions={"x0": UniformDistribution(low=0, high=10)}, + intermediate_values=iv, + params={"x0": 0.5}, + ) + for iv in intermediate_values + ] + cached_extra_study_property = _CachedExtraStudyProperty() + cached_extra_study_property.update(trials) + self.assertTrue(cached_extra_study_property.has_intermediate_values) + + def test_no_trials(self) -> None: + trials = [] + cached_extra_study_property = _CachedExtraStudyProperty() + cached_extra_study_property.update(trials) + self.assertFalse(cached_extra_study_property.has_intermediate_values) diff --git a/python_tests/test_search_space.py b/python_tests/test_search_space.py deleted file mode 100644 index df7fe9e2..00000000 --- a/python_tests/test_search_space.py +++ /dev/null @@ -1,115 +0,0 @@ -from unittest import TestCase -import warnings - -import optuna -from optuna import create_trial -from optuna.distributions import UniformDistribution -from optuna.exceptions import ExperimentalWarning -from optuna.trial import TrialState - -from optuna_dashboard._search_space import _SearchSpace - - -class SearchSpaceTestCase(TestCase): - def setUp(self) -> None: - optuna.logging.set_verbosity(optuna.logging.ERROR) - warnings.simplefilter("ignore", category=ExperimentalWarning) - - def test_same_distributions(self) -> None: - distributions = [ - { - "x0": UniformDistribution(low=0, high=10), - "x1": UniformDistribution(low=0, high=10), - }, - { - "x0": UniformDistribution(low=0, high=10), - "x1": UniformDistribution(low=0, high=10), - }, - ] - params = [ - { - "x0": 0.5, - "x1": 0.5, - }, - { - "x0": 0.5, - "x1": 0.5, - }, - ] - trials = [ - create_trial(state=TrialState.COMPLETE, value=0, distributions=d, params=p) - for d, p in zip(distributions, params) - ] - search_space = _SearchSpace() - search_space.update(trials) - - self.assertEqual(len(search_space.intersection), 2) - self.assertEqual(len(search_space.union), 2) - - def test_different_distributions(self) -> None: - distributions = [ - { - "x0": UniformDistribution(low=0, high=10), - "x1": UniformDistribution(low=0, high=10), - }, - { - "x0": UniformDistribution(low=0, high=5), - "x1": UniformDistribution(low=0, high=10), - }, - ] - params = [ - { - "x0": 0.5, - "x1": 0.5, - }, - { - "x0": 0.5, - "x1": 0.5, - }, - ] - trials = [ - create_trial(state=TrialState.COMPLETE, value=0, distributions=d, params=p) - for d, p in zip(distributions, params) - ] - search_space = _SearchSpace() - search_space.update(trials) - - self.assertEqual(len(search_space.intersection), 1) - self.assertEqual(len(search_space.union), 3) - - def test_dynamic_search_space(self) -> None: - distributions = [ - { - "x0": UniformDistribution(low=0, high=10), - "x1": UniformDistribution(low=0, high=10), - }, - { - "x0": UniformDistribution(low=0, high=5), - }, - { - "x0": UniformDistribution(low=0, high=10), - "x1": UniformDistribution(low=0, high=10), - }, - ] - params = [ - { - "x0": 0.5, - "x1": 0.5, - }, - { - "x0": 0.5, - }, - { - "x0": 0.5, - "x1": 0.5, - }, - ] - trials = [ - create_trial(state=TrialState.COMPLETE, value=0, distributions=d, params=p) - for d, p in zip(distributions, params) - ] - search_space = _SearchSpace() - search_space.update(trials) - - self.assertEqual(len(search_space.intersection), 0) - self.assertEqual(len(search_space.union), 3)