From 549525f81da4ffa93947c41d4a2f077dc7915b56 Mon Sep 17 00:00:00 2001 From: Contramundum Date: Thu, 31 Aug 2023 18:28:19 +0900 Subject: [PATCH 01/17] Separate best_trials and active_trials --- optuna_dashboard/preferential/_study.py | 51 +++++++++++++++++-------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/optuna_dashboard/preferential/_study.py b/optuna_dashboard/preferential/_study.py index 16c81b66..5ea5bf36 100644 --- a/optuna_dashboard/preferential/_study.py +++ b/optuna_dashboard/preferential/_study.py @@ -62,18 +62,20 @@ class PreferentialStudy: def best_trials(self) -> list[FrozenTrial]: """Return the trials that is not dominated by other trials. - .. seealso:: - - See `Study.best_trials`_ for details. - - .. _Study.best_trials: https://optuna.readthedocs.io/en/stable/reference/\ - generated/optuna.study.Study.html#optuna.study.Study.best_trials - Returns: A list of FrozenTrial object """ return get_best_trials(self._study._study_id, self._study._storage) + @property + def active_trials(self) -> list[FrozenTrial]: + """Return the trials that is not reported bad and not marked skipped. + + Returns: + A list of FrozenTrial object + """ + return get_active_trials(self._study._study_id, self._study._storage) + @property def study_name(self) -> str: """Return the name of the study. @@ -283,21 +285,38 @@ class PreferentialStudy: to generate a new trial if this method returns :obj:`True`, and to wait for human evaluation if this method returns :obj:`False`. """ - return len(self.best_trials) < self.n_generate + return len(self.active_trials) < self.n_generate + + +def get_active_trials(study_id: int, storage: optuna.storages.BaseStorage) -> list[FrozenTrial]: + preferences = get_preferences(study_id, storage) + worse_numbers = {worse for _, worse in preferences} + study_system_attrs = storage.get_study_system_attrs(study_id) + active_trials = [] + for t in storage.get_all_trials( + study_id, deepcopy=False, states=(TrialState.COMPLETE, TrialState.RUNNING) + ): + if t.number in worse_numbers: + continue + if is_skipped_trial(t._trial_id, study_system_attrs): + continue + active_trials.append(copy.deepcopy(t)) + return active_trials def get_best_trials(study_id: int, storage: optuna.storages.BaseStorage) -> list[FrozenTrial]: preferences = get_preferences(study_id, storage) worse_numbers = {worse for _, worse in preferences} - study_system_attrs = storage.get_study_system_attrs(study_id) - best_trials = [] - for t in storage.get_all_trials( + nondominated_numbers = {better for better, _ in preferences if better not in worse_numbers} + trials = storage.get_all_trials( study_id, deepcopy=False, states=(TrialState.COMPLETE, TrialState.RUNNING) - ): - if not t.system_attrs.get(_SYSTEM_ATTR_COMPARISON_READY, False): - continue - if t.number in worse_numbers: - continue + ) + + study_system_attrs = storage.get_study_system_attrs(study_id) + + best_trials = [] + for n in nondominated_numbers: + t = trials[n] if is_skipped_trial(t._trial_id, study_system_attrs): continue best_trials.append(copy.deepcopy(t)) From 6553bb5fbdeb25d92ee99099092e589b3596eea5 Mon Sep 17 00:00:00 2001 From: Contramundum Date: Thu, 31 Aug 2023 18:37:46 +0900 Subject: [PATCH 02/17] Remove PreferentialStudy.active_trial --- optuna_dashboard/preferential/_study.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/optuna_dashboard/preferential/_study.py b/optuna_dashboard/preferential/_study.py index 5ea5bf36..57ab8e9f 100644 --- a/optuna_dashboard/preferential/_study.py +++ b/optuna_dashboard/preferential/_study.py @@ -67,15 +67,6 @@ class PreferentialStudy: """ return get_best_trials(self._study._study_id, self._study._storage) - @property - def active_trials(self) -> list[FrozenTrial]: - """Return the trials that is not reported bad and not marked skipped. - - Returns: - A list of FrozenTrial object - """ - return get_active_trials(self._study._study_id, self._study._storage) - @property def study_name(self) -> str: """Return the name of the study. @@ -285,7 +276,7 @@ class PreferentialStudy: to generate a new trial if this method returns :obj:`True`, and to wait for human evaluation if this method returns :obj:`False`. """ - return len(self.active_trials) < self.n_generate + return len(get_active_trials(self._study._study_id, self._study._storage)) < self.n_generate def get_active_trials(study_id: int, storage: optuna.storages.BaseStorage) -> list[FrozenTrial]: From b929bc6b36ec96ff2bf837b4247089a31ef3a272 Mon Sep 17 00:00:00 2001 From: Contramundum Date: Fri, 1 Sep 2023 14:36:07 +0900 Subject: [PATCH 03/17] [WIP] --- optuna_dashboard/preferential/_study.py | 4 +++- python_tests/test_api.py | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/optuna_dashboard/preferential/_study.py b/optuna_dashboard/preferential/_study.py index 57ab8e9f..8895d604 100644 --- a/optuna_dashboard/preferential/_study.py +++ b/optuna_dashboard/preferential/_study.py @@ -276,7 +276,9 @@ class PreferentialStudy: to generate a new trial if this method returns :obj:`True`, and to wait for human evaluation if this method returns :obj:`False`. """ - return len(get_active_trials(self._study._study_id, self._study._storage)) < self.n_generate + return ( + len(get_active_trials(self._study._study_id, self._study._storage)) < self.n_generate + ) def get_active_trials(study_id: int, storage: optuna.storages.BaseStorage) -> list[FrozenTrial]: diff --git a/python_tests/test_api.py b/python_tests/test_api.py index ae50e29a..fc67e12e 100644 --- a/python_tests/test_api.py +++ b/python_tests/test_api.py @@ -108,6 +108,8 @@ class APITestCase(TestCase): study.mark_comparison_ready(trial) study.report_preference(study.trials[0], study.trials[1]) + assert len(study.best_trials) == 1 + app = create_app(storage) study_id = study._study._study_id status, _, body = send_request( @@ -119,9 +121,8 @@ class APITestCase(TestCase): self.assertEqual(status, 200) best_trials = json.loads(body)["best_trials"] - assert len(best_trials) == 2 + assert len(best_trials) == 1 assert best_trials[0]["number"] == 0 - assert best_trials[1]["number"] == 2 def test_report_preference(self) -> None: storage = optuna.storages.InMemoryStorage() From 9c8a0fd71e454cd94a843656032af6b148e369d5 Mon Sep 17 00:00:00 2001 From: Contramundum Date: Mon, 4 Sep 2023 19:13:46 +0900 Subject: [PATCH 04/17] Remove mark_comparison_ready --- python_tests/preferential/test_study.py | 2 +- python_tests/test_api.py | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/python_tests/preferential/test_study.py b/python_tests/preferential/test_study.py index abc1bc4d..7fc1aaba 100644 --- a/python_tests/preferential/test_study.py +++ b/python_tests/preferential/test_study.py @@ -263,7 +263,7 @@ def test_get_trials_state_option(storage_supplier: Callable[[], StorageSupplier] with storage_supplier() as storage: study = create_study(n_generate=4, storage=storage) for _ in range(3): - trial = study.ask() + study.ask() better, worse = study.trials[:2] study.report_preference(better, worse) diff --git a/python_tests/test_api.py b/python_tests/test_api.py index ba3b0dc4..9bdaf4d3 100644 --- a/python_tests/test_api.py +++ b/python_tests/test_api.py @@ -104,7 +104,7 @@ class APITestCase(TestCase): storage = optuna.storages.InMemoryStorage() study = create_study(n_generate=4, storage=storage) for _ in range(3): - trial = study.ask() + study.ask() study.report_preference(study.trials[0], study.trials[1]) assert len(study.best_trials) == 1 @@ -127,7 +127,7 @@ class APITestCase(TestCase): storage = optuna.storages.InMemoryStorage() study = create_study(n_generate=4, storage=storage) for _ in range(3): - trial = study.ask() + study.ask() app = create_app(storage) study_id = study._study._study_id @@ -157,21 +157,22 @@ class APITestCase(TestCase): for _ in range(3): trial = study.ask() trials.append(trial) + study.report_preference(trials[0], trials[1]) + study.report_preference(trials[2], trials[1]) app = create_app(storage) study_id = study._study._study_id status, _, _ = send_request( app, - f"/api/studies/{study_id}/{trials[1]._trial_id}/skip", + f"/api/studies/{study_id}/{trials[0]._trial_id}/skip", "POST", content_type="application/json", ) self.assertEqual(status, 204) best_trials = study.best_trials - assert len(best_trials) == 2 - assert best_trials[0].number == 0 - assert best_trials[1].number == 2 + assert len(best_trials) == 1 + assert best_trials[0].number == 2 def test_create_study(self) -> None: for name, directions, expected_status in [ From d774de65c5c347ddf9aaaadc29a603fd6915e85e Mon Sep 17 00:00:00 2001 From: Contramundum Date: Mon, 4 Sep 2023 19:19:17 +0900 Subject: [PATCH 05/17] Fix mypy error --- python_tests/test_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python_tests/test_api.py b/python_tests/test_api.py index 9bdaf4d3..5085b598 100644 --- a/python_tests/test_api.py +++ b/python_tests/test_api.py @@ -157,8 +157,8 @@ class APITestCase(TestCase): for _ in range(3): trial = study.ask() trials.append(trial) - study.report_preference(trials[0], trials[1]) - study.report_preference(trials[2], trials[1]) + study.report_preference(study.trials[0], study.trials[1]) + study.report_preference(study.trials[2], study.trials[1]) app = create_app(storage) study_id = study._study._study_id From 32ab08954dbb94dbb54eaf1c796dc93d46248c82 Mon Sep 17 00:00:00 2001 From: contramundum53 Date: Wed, 6 Sep 2023 11:04:58 +0900 Subject: [PATCH 06/17] Update optuna_dashboard/preferential/_study.py --- optuna_dashboard/preferential/_study.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optuna_dashboard/preferential/_study.py b/optuna_dashboard/preferential/_study.py index e412d0ee..702145e3 100644 --- a/optuna_dashboard/preferential/_study.py +++ b/optuna_dashboard/preferential/_study.py @@ -245,7 +245,7 @@ def get_best_trials(study_id: int, storage: optuna.storages.BaseStorage) -> list worse_numbers = {worse for _, worse in preferences} nondominated_numbers = {better for better, _ in preferences if better not in worse_numbers} trials = storage.get_all_trials( - study_id, deepcopy=False, states=(TrialState.COMPLETE, TrialState.RUNNING) + study_id, deepcopy=False, ) study_system_attrs = storage.get_study_system_attrs(study_id) From 60ea9a5a59ee8507332e7fafc749ba1b11f56bcd Mon Sep 17 00:00:00 2001 From: contramundum53 Date: Wed, 6 Sep 2023 12:36:17 +0900 Subject: [PATCH 07/17] Update optuna_dashboard/preferential/_study.py Co-authored-by: c-bata --- optuna_dashboard/preferential/_study.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/optuna_dashboard/preferential/_study.py b/optuna_dashboard/preferential/_study.py index 702145e3..4b6f8c2c 100644 --- a/optuna_dashboard/preferential/_study.py +++ b/optuna_dashboard/preferential/_study.py @@ -244,9 +244,7 @@ def get_best_trials(study_id: int, storage: optuna.storages.BaseStorage) -> list preferences = get_preferences(study_id, storage) worse_numbers = {worse for _, worse in preferences} nondominated_numbers = {better for better, _ in preferences if better not in worse_numbers} - trials = storage.get_all_trials( - study_id, deepcopy=False, - ) + trials = storage.get_all_trials(study_id, deepcopy=False) study_system_attrs = storage.get_study_system_attrs(study_id) From 5622eb58a963ab173d876b221af593424b1e5d8c Mon Sep 17 00:00:00 2001 From: Contramundum Date: Thu, 7 Sep 2023 15:31:59 +0900 Subject: [PATCH 08/17] Fix UI to adapt to best_trial change --- optuna_dashboard/preferential/_study.py | 22 ++++++++++++++++--- .../preferential/_system_attrs.py | 16 +++++++++----- optuna_dashboard/preferential/samplers/gp.py | 2 +- .../ts/components/PreferentialTrials.tsx | 12 ++++++---- .../preferential/test_system_attrs.py | 7 +++--- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/optuna_dashboard/preferential/_study.py b/optuna_dashboard/preferential/_study.py index 7da363b2..fb54ed36 100644 --- a/optuna_dashboard/preferential/_study.py +++ b/optuna_dashboard/preferential/_study.py @@ -14,6 +14,7 @@ from optuna.trial import FrozenTrial from optuna.trial import TrialState from optuna_dashboard.preferential._system_attrs import get_n_generate from optuna_dashboard.preferential._system_attrs import get_preferences +from optuna_dashboard.preferential._system_attrs import get_skipped_trial_ids from optuna_dashboard.preferential._system_attrs import is_skipped_trial from optuna_dashboard.preferential._system_attrs import report_preferences from optuna_dashboard.preferential._system_attrs import set_n_generate @@ -243,8 +244,11 @@ class PreferentialStudy: Returns: A list of the pair of FrozenTrial objects. The left trial is better than the right one. """ + + preferences = get_preferences( + self._study._storage.get_study_system_attrs(self._study._study_id) + ) # Must come before study.get_trials() trials = self._study.get_trials(deepcopy=deepcopy) - preferences = get_preferences(self._study._study_id, self._study._storage) return [(trials[better], trials[worse]) for (better, worse) in preferences] def set_user_attr(self, key: str, value: Any) -> None: @@ -269,11 +273,23 @@ class PreferentialStudy: to generate a new trial if this method returns :obj:`True`, and to wait for human evaluation if this method returns :obj:`False`. """ - return len(self.best_trials) < get_n_generate(self._study.system_attrs) + study_system_attrs = self._study._storage.get_study_system_attrs( + self._study._study_id + ) # Must come before _study.get_trials() + trials = self._study.get_trials(deepcopy=False) + + all_trial_ids = {t._trial_id for t in trials} + bad_trial_ids = { + trials[worse]._trial_id for (_, worse) in get_preferences(study_system_attrs) + } + skipped_trial_ids = set(get_skipped_trial_ids(study_system_attrs)) + + active_trial_ids = all_trial_ids - bad_trial_ids - skipped_trial_ids + return len(active_trial_ids) < get_n_generate(self._study.system_attrs) def get_best_trials(study_id: int, storage: optuna.storages.BaseStorage) -> list[FrozenTrial]: - preferences = get_preferences(study_id, storage) + preferences = get_preferences(storage.get_study_system_attrs(study_id)) worse_numbers = {worse for _, worse in preferences} nondominated_numbers = {better for better, _ in preferences if better not in worse_numbers} trials = storage.get_all_trials(study_id, deepcopy=False) diff --git a/optuna_dashboard/preferential/_system_attrs.py b/optuna_dashboard/preferential/_system_attrs.py index 4cb0e288..2f951594 100644 --- a/optuna_dashboard/preferential/_system_attrs.py +++ b/optuna_dashboard/preferential/_system_attrs.py @@ -35,13 +35,9 @@ def report_preferences( return preference_id -def get_preferences( - study_id: int, - storage: BaseStorage, -) -> list[tuple[int, int]]: +def get_preferences(study_system_attrs: dict[str, Any]) -> list[tuple[int, int]]: preferences: list[tuple[int, int]] = [] - system_attrs = storage.get_study_system_attrs(study_id) - for k, v in system_attrs.items(): + for k, v in study_system_attrs.items(): if not k.startswith(_SYSTEM_ATTR_PREFIX_PREFERENCE): continue preferences.extend(v) # type: ignore @@ -65,6 +61,14 @@ def is_skipped_trial(trial_id: int, study_system_attrs: dict[str, Any]) -> bool: return key in study_system_attrs +def get_skipped_trial_ids(study_system_attrs: dict[str, Any]) -> list[int]: + return [ + int(k[len(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL):]) + for k in study_system_attrs.keys() + if k.startswith(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL) + ] + + def get_n_generate(study_system_attrs: dict[str, Any]) -> int: return study_system_attrs[_SYSTEM_ATTR_N_GENERATE] diff --git a/optuna_dashboard/preferential/samplers/gp.py b/optuna_dashboard/preferential/samplers/gp.py index ff4001c1..b90a1de4 100644 --- a/optuna_dashboard/preferential/samplers/gp.py +++ b/optuna_dashboard/preferential/samplers/gp.py @@ -342,7 +342,7 @@ class PreferentialGPSampler(optuna.samplers.BaseSampler): if len(search_space) == 0: return {} - preferences = get_preferences(study._study_id, study._storage) + preferences = get_preferences(study.system_attrs) trials = study.get_trials(deepcopy=False) if len(preferences) == 0: return {} diff --git a/optuna_dashboard/ts/components/PreferentialTrials.tsx b/optuna_dashboard/ts/components/PreferentialTrials.tsx index 93ed2f5a..27873c45 100644 --- a/optuna_dashboard/ts/components/PreferentialTrials.tsx +++ b/optuna_dashboard/ts/components/PreferentialTrials.tsx @@ -182,11 +182,15 @@ export const PreferentialTrials: FC<{ studyDetail: StudyDetail | null }> = ({ return null } const theme = useTheme() + + const running_trials = studyDetail.trials.filter((t) => t.state === "Running") + const active_trials = running_trials.concat(studyDetail.best_trials) + const [displayTrials, setDisplayTrials] = useState({ - numbers: studyDetail.best_trials.map((t) => t.number), - last_number: Math.max(...studyDetail.best_trials.map((t) => t.number), -1), + numbers: active_trials.map((t) => t.number), + last_number: Math.max(...active_trials.map((t) => t.number), -1), }) - const new_trails = studyDetail.best_trials.filter( + const new_trails = active_trials.filter( (t) => displayTrials.last_number < t.number && displayTrials.numbers.find((n) => n === t.number) === undefined @@ -239,7 +243,7 @@ export const PreferentialTrials: FC<{ studyDetail: StudyDetail | null }> = ({ {displayTrials.numbers.map((t, index) => ( trial.number === t)} + trial={active_trials.find((trial) => trial.number === t)} candidates={displayTrials.numbers.filter((n) => n !== -1)} hideTrial={() => { hideTrial(t) diff --git a/python_tests/preferential/test_system_attrs.py b/python_tests/preferential/test_system_attrs.py index 10448d48..34f93200 100644 --- a/python_tests/preferential/test_system_attrs.py +++ b/python_tests/preferential/test_system_attrs.py @@ -18,12 +18,13 @@ def test_report_and_get_preferences(storage_supplier: Callable[[], StorageSuppli study.ask() study_id = study._study_id - assert len(get_preferences(study_id, storage)) == 0 + + assert len(get_preferences(storage.get_study_system_attrs(study_id))) == 0 better, worse = study.trials[0], study.trials[1] report_preferences(study_id, storage, [(better.number, worse.number)]) - assert len(get_preferences(study_id, storage)) == 1 + assert len(get_preferences(storage.get_study_system_attrs(study_id))) == 1 - actual_better, actual_worse = get_preferences(study_id, storage)[0] + actual_better, actual_worse = get_preferences(storage.get_study_system_attrs(study_id))[0] assert actual_better == better.number assert actual_worse == worse.number From 94be880c3f14c824f767efe0f2e66591c9649a01 Mon Sep 17 00:00:00 2001 From: Contramundum Date: Thu, 7 Sep 2023 15:34:22 +0900 Subject: [PATCH 09/17] Run formatter --- optuna_dashboard/ts/components/PreferentialTrials.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optuna_dashboard/ts/components/PreferentialTrials.tsx b/optuna_dashboard/ts/components/PreferentialTrials.tsx index 27873c45..3bdb0cce 100644 --- a/optuna_dashboard/ts/components/PreferentialTrials.tsx +++ b/optuna_dashboard/ts/components/PreferentialTrials.tsx @@ -182,7 +182,7 @@ export const PreferentialTrials: FC<{ studyDetail: StudyDetail | null }> = ({ return null } const theme = useTheme() - + const running_trials = studyDetail.trials.filter((t) => t.state === "Running") const active_trials = running_trials.concat(studyDetail.best_trials) From 3f00b42fa7dd006b504f1d2532aadc0e6e716bce Mon Sep 17 00:00:00 2001 From: contramundum53 Date: Thu, 7 Sep 2023 15:55:56 +0900 Subject: [PATCH 10/17] Update optuna_dashboard/preferential/_system_attrs.py Co-authored-by: c-bata --- optuna_dashboard/preferential/_system_attrs.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/optuna_dashboard/preferential/_system_attrs.py b/optuna_dashboard/preferential/_system_attrs.py index 2f951594..0483eeb7 100644 --- a/optuna_dashboard/preferential/_system_attrs.py +++ b/optuna_dashboard/preferential/_system_attrs.py @@ -62,11 +62,16 @@ def is_skipped_trial(trial_id: int, study_system_attrs: dict[str, Any]) -> bool: def get_skipped_trial_ids(study_system_attrs: dict[str, Any]) -> list[int]: - return [ - int(k[len(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL):]) - for k in study_system_attrs.keys() - if k.startswith(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL) - ] + skipped_trial_ids: list[int] = [] + for k in study_system_attrs: + if not k.startswith(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL): + continue + try: + trial_id = int(k[len(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL):]) + skipped_trial_ids.append(trial_id) + except ValueError as e: + continue + return skipped_trial_ids def get_n_generate(study_system_attrs: dict[str, Any]) -> int: From 49c49623d97efa52b86964867078f953056631a0 Mon Sep 17 00:00:00 2001 From: contramundum53 Date: Thu, 7 Sep 2023 15:56:39 +0900 Subject: [PATCH 11/17] Update optuna_dashboard/ts/components/PreferentialTrials.tsx Co-authored-by: c-bata --- optuna_dashboard/ts/components/PreferentialTrials.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/optuna_dashboard/ts/components/PreferentialTrials.tsx b/optuna_dashboard/ts/components/PreferentialTrials.tsx index 3bdb0cce..c9ab1385 100644 --- a/optuna_dashboard/ts/components/PreferentialTrials.tsx +++ b/optuna_dashboard/ts/components/PreferentialTrials.tsx @@ -183,8 +183,8 @@ export const PreferentialTrials: FC<{ studyDetail: StudyDetail | null }> = ({ } const theme = useTheme() - const running_trials = studyDetail.trials.filter((t) => t.state === "Running") - const active_trials = running_trials.concat(studyDetail.best_trials) + const runningTrials = studyDetail.trials.filter((t) => t.state === "Running") + const activeTrials = running_trials.concat(studyDetail.best_trials) const [displayTrials, setDisplayTrials] = useState({ numbers: active_trials.map((t) => t.number), From 8d5c306ddd16cf33887a202672f6cde0f6641c81 Mon Sep 17 00:00:00 2001 From: contramundum53 Date: Thu, 7 Sep 2023 15:57:54 +0900 Subject: [PATCH 12/17] Update optuna_dashboard/preferential/_study.py Co-authored-by: c-bata --- optuna_dashboard/preferential/_study.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/optuna_dashboard/preferential/_study.py b/optuna_dashboard/preferential/_study.py index fb54ed36..f47f8f9d 100644 --- a/optuna_dashboard/preferential/_study.py +++ b/optuna_dashboard/preferential/_study.py @@ -276,16 +276,11 @@ class PreferentialStudy: study_system_attrs = self._study._storage.get_study_system_attrs( self._study._study_id ) # Must come before _study.get_trials() - trials = self._study.get_trials(deepcopy=False) - - all_trial_ids = {t._trial_id for t in trials} - bad_trial_ids = { - trials[worse]._trial_id for (_, worse) in get_preferences(study_system_attrs) - } + trials = self._study.get_trials(deepcopy=False, states=(TrialState.COMPLETE, TrialState.RUNNING)) + worse_trial_numbers = {worse for _, worse in get_preferences(study_system_attrs)} skipped_trial_ids = set(get_skipped_trial_ids(study_system_attrs)) - - active_trial_ids = all_trial_ids - bad_trial_ids - skipped_trial_ids - return len(active_trial_ids) < get_n_generate(self._study.system_attrs) + active_trials = [t for t in trials if t.number not in worse_trial_number and t._trial_id not in skipped_trial_ids] + return len(active_trials) < get_n_generate(self._study.system_attrs) def get_best_trials(study_id: int, storage: optuna.storages.BaseStorage) -> list[FrozenTrial]: From 39877452a01516c7cbd0d712b08522b3855a8f69 Mon Sep 17 00:00:00 2001 From: Contramundum Date: Thu, 7 Sep 2023 16:02:58 +0900 Subject: [PATCH 13/17] Apply review comments --- optuna_dashboard/preferential/_study.py | 10 ++++++++-- optuna_dashboard/preferential/_system_attrs.py | 2 +- optuna_dashboard/ts/components/PreferentialTrials.tsx | 10 +++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/optuna_dashboard/preferential/_study.py b/optuna_dashboard/preferential/_study.py index f47f8f9d..6e093683 100644 --- a/optuna_dashboard/preferential/_study.py +++ b/optuna_dashboard/preferential/_study.py @@ -276,10 +276,16 @@ class PreferentialStudy: study_system_attrs = self._study._storage.get_study_system_attrs( self._study._study_id ) # Must come before _study.get_trials() - trials = self._study.get_trials(deepcopy=False, states=(TrialState.COMPLETE, TrialState.RUNNING)) + trials = self._study.get_trials( + deepcopy=False, states=(TrialState.COMPLETE, TrialState.RUNNING) + ) worse_trial_numbers = {worse for _, worse in get_preferences(study_system_attrs)} skipped_trial_ids = set(get_skipped_trial_ids(study_system_attrs)) - active_trials = [t for t in trials if t.number not in worse_trial_number and t._trial_id not in skipped_trial_ids] + active_trials = [ + t + for t in trials + if t.number not in worse_trial_numbers and t._trial_id not in skipped_trial_ids + ] return len(active_trials) < get_n_generate(self._study.system_attrs) diff --git a/optuna_dashboard/preferential/_system_attrs.py b/optuna_dashboard/preferential/_system_attrs.py index 0483eeb7..c4454e7e 100644 --- a/optuna_dashboard/preferential/_system_attrs.py +++ b/optuna_dashboard/preferential/_system_attrs.py @@ -67,7 +67,7 @@ def get_skipped_trial_ids(study_system_attrs: dict[str, Any]) -> list[int]: if not k.startswith(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL): continue try: - trial_id = int(k[len(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL):]) + trial_id = int(k[len(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL) :]) skipped_trial_ids.append(trial_id) except ValueError as e: continue diff --git a/optuna_dashboard/ts/components/PreferentialTrials.tsx b/optuna_dashboard/ts/components/PreferentialTrials.tsx index c9ab1385..f3166627 100644 --- a/optuna_dashboard/ts/components/PreferentialTrials.tsx +++ b/optuna_dashboard/ts/components/PreferentialTrials.tsx @@ -184,13 +184,13 @@ export const PreferentialTrials: FC<{ studyDetail: StudyDetail | null }> = ({ const theme = useTheme() const runningTrials = studyDetail.trials.filter((t) => t.state === "Running") - const activeTrials = running_trials.concat(studyDetail.best_trials) + const activeTrials = runningTrials.concat(studyDetail.best_trials) const [displayTrials, setDisplayTrials] = useState({ - numbers: active_trials.map((t) => t.number), - last_number: Math.max(...active_trials.map((t) => t.number), -1), + numbers: activeTrials.map((t) => t.number), + last_number: Math.max(...activeTrials.map((t) => t.number), -1), }) - const new_trails = active_trials.filter( + const new_trails = activeTrials.filter( (t) => displayTrials.last_number < t.number && displayTrials.numbers.find((n) => n === t.number) === undefined @@ -243,7 +243,7 @@ export const PreferentialTrials: FC<{ studyDetail: StudyDetail | null }> = ({ {displayTrials.numbers.map((t, index) => ( trial.number === t)} + trial={activeTrials.find((trial) => trial.number === t)} candidates={displayTrials.numbers.filter((n) => n !== -1)} hideTrial={() => { hideTrial(t) From 226bd53953475422cbd9d5f09019f619768f09f3 Mon Sep 17 00:00:00 2001 From: Contramundum Date: Thu, 7 Sep 2023 17:37:56 +0900 Subject: [PATCH 14/17] Fix linter --- optuna_dashboard/preferential/_system_attrs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/optuna_dashboard/preferential/_system_attrs.py b/optuna_dashboard/preferential/_system_attrs.py index c4454e7e..ab469a74 100644 --- a/optuna_dashboard/preferential/_system_attrs.py +++ b/optuna_dashboard/preferential/_system_attrs.py @@ -69,7 +69,7 @@ def get_skipped_trial_ids(study_system_attrs: dict[str, Any]) -> list[int]: try: trial_id = int(k[len(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL) :]) skipped_trial_ids.append(trial_id) - except ValueError as e: + except ValueError: continue return skipped_trial_ids From 1c01ef0127190462f7173bd1aa25e4c5fadcf66d Mon Sep 17 00:00:00 2001 From: Contramundum Date: Thu, 7 Sep 2023 17:38:55 +0900 Subject: [PATCH 15/17] Fix test --- python_tests/test_api.py | 1 - python_tests/test_preferential_history.py | 1 - 2 files changed, 2 deletions(-) diff --git a/python_tests/test_api.py b/python_tests/test_api.py index 75243f30..e0c2e1c8 100644 --- a/python_tests/test_api.py +++ b/python_tests/test_api.py @@ -161,7 +161,6 @@ class APITestCase(TestCase): study = create_study(storage=storage, n_generate=3) for _ in range(3): trial = study.ask() - study.mark_comparison_ready(trial) app = create_app(storage) study_id = study._study._study_id diff --git a/python_tests/test_preferential_history.py b/python_tests/test_preferential_history.py index ab524b90..51c9b0f8 100644 --- a/python_tests/test_preferential_history.py +++ b/python_tests/test_preferential_history.py @@ -19,7 +19,6 @@ def test_report_and_get_choices(storage_supplier: Callable[[], StorageSupplier]) for _ in range(5): trial = study.ask() trial.suggest_float("x", 0, 1) - study.mark_comparison_ready(trial) study_id = study._study._study_id From 4e8c6051e467f974cde0a4d2f0718ac2c4c7ccea Mon Sep 17 00:00:00 2001 From: Contramundum Date: Thu, 7 Sep 2023 17:42:08 +0900 Subject: [PATCH 16/17] Apply review comment --- optuna_dashboard/ts/components/PreferentialTrials.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/optuna_dashboard/ts/components/PreferentialTrials.tsx b/optuna_dashboard/ts/components/PreferentialTrials.tsx index f3166627..08efd9a7 100644 --- a/optuna_dashboard/ts/components/PreferentialTrials.tsx +++ b/optuna_dashboard/ts/components/PreferentialTrials.tsx @@ -42,6 +42,8 @@ const PreferentialTrial: FC<{ ) } + const isBestTrial = trial.state === "Complete" + return ( true} + isBestTrial={() => isBestTrial} directions={[]} objectiveNames={[]} /> From f152ee19953d040828d42de05f7668022e4ef8c5 Mon Sep 17 00:00:00 2001 From: Contramundum Date: Thu, 7 Sep 2023 17:45:58 +0900 Subject: [PATCH 17/17] Fix linter --- optuna_dashboard/preferential/_system_attrs.py | 2 +- python_tests/test_api.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/optuna_dashboard/preferential/_system_attrs.py b/optuna_dashboard/preferential/_system_attrs.py index ab469a74..47c2a486 100644 --- a/optuna_dashboard/preferential/_system_attrs.py +++ b/optuna_dashboard/preferential/_system_attrs.py @@ -67,7 +67,7 @@ def get_skipped_trial_ids(study_system_attrs: dict[str, Any]) -> list[int]: if not k.startswith(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL): continue try: - trial_id = int(k[len(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL) :]) + trial_id = int(k[len(_SYSTEM_ATTR_PREFIX_SKIP_TRIAL) :]) # noqa: E203 skipped_trial_ids.append(trial_id) except ValueError: continue diff --git a/python_tests/test_api.py b/python_tests/test_api.py index e0c2e1c8..c551e3f2 100644 --- a/python_tests/test_api.py +++ b/python_tests/test_api.py @@ -160,7 +160,7 @@ class APITestCase(TestCase): storage = optuna.storages.InMemoryStorage() study = create_study(storage=storage, n_generate=3) for _ in range(3): - trial = study.ask() + study.ask() app = create_app(storage) study_id = study._study._study_id