(objective: Callable[[Trial], float], output_format: str | None)
| 531 | @pytest.mark.parametrize("objective", (objective_func, objective_func_branched_search_space)) |
| 532 | @output_formats |
| 533 | def test_trials_command(objective: Callable[[Trial], float], output_format: str | None) -> None: |
| 534 | with NamedTemporaryFilePool() as fp, StorageSupplier("journal", file=fp) as storage: |
| 535 | assert isinstance(storage, JournalStorage) |
| 536 | storage_url = fp.name |
| 537 | study_name = "test_study" |
| 538 | n_trials = 10 |
| 539 | |
| 540 | study = optuna.create_study(storage=storage, study_name=study_name) |
| 541 | study.optimize(objective, n_trials=n_trials) |
| 542 | attrs = ( |
| 543 | "number", |
| 544 | "value", |
| 545 | "datetime_start", |
| 546 | "datetime_complete", |
| 547 | "duration", |
| 548 | "params", |
| 549 | "user_attrs", |
| 550 | "state", |
| 551 | ) |
| 552 | |
| 553 | # Run command. |
| 554 | command = [ |
| 555 | "optuna", |
| 556 | "trials", |
| 557 | "--storage", |
| 558 | storage_url, |
| 559 | "--study-name", |
| 560 | study_name, |
| 561 | ] |
| 562 | |
| 563 | if output_format is not None: |
| 564 | command += ["--format", output_format] |
| 565 | |
| 566 | trials = _get_output(command, output_format or "table") |
| 567 | |
| 568 | assert len(trials) == n_trials |
| 569 | |
| 570 | df = study.trials_dataframe(attrs, multi_index=True) |
| 571 | |
| 572 | for i, trial in enumerate(trials): |
| 573 | for key in df.columns: |
| 574 | expected_value = df.loc[i][key] |
| 575 | |
| 576 | # The param may be NaN when the objective function has branched search space. |
| 577 | if ( |
| 578 | key[0] == "params" |
| 579 | and isinstance(expected_value, float) |
| 580 | and np.isnan(expected_value) |
| 581 | ): |
| 582 | if output_format in (None, "table", "value"): |
| 583 | assert key[1] not in eval(trial["params"]) |
| 584 | else: |
| 585 | assert key[1] not in trial["params"] |
| 586 | continue |
| 587 | |
| 588 | if key[1] == "": |
| 589 | value = trial[key[0]] |
| 590 | else: |
nothing calls this directly
no test coverage detected
searching dependent graphs…