Skip to content

Made error message for create_study's direction easier to understand optuna.study#6021

Merged
nabenabe0928 merged 7 commits intooptuna:masterfrom
sinano1107:master
Apr 1, 2025
Merged

Made error message for create_study's direction easier to understand optuna.study#6021
nabenabe0928 merged 7 commits intooptuna:masterfrom
sinano1107:master

Conversation

@sinano1107
Copy link
Copy Markdown
Contributor

@sinano1107 sinano1107 commented Mar 27, 2025

issue: #5806

Motivation

Users sometimes makes the mistake of passing the list of directions to the direction argument. The error message for this mistake is hard to understand what is the problem.

  • Correct
optuna.create_study(directions=["minimize", "minimize"])
  • Wrong

Using direction instead of directions

optuna.create_study(direction=["minimize", "minimize"])
ValueError: Please set either 'minimize' or 'maximize' to direction. You can also set the corresponding `StudyDirection` member.

Description of the changes

The error message displayed when a list object, is assigned to the direction parameter of create_study has been changed as follows.

  • previous
ValueError: Please set either 'minimize' or 'maximize' to direction. You can also set the corresponding `StudyDirection` member.
  • new
ValueError: For multi-objective optimization, using `directions` instead of `direction`.

@nzw0301 nzw0301 added the code-fix Change that does not change the behavior, such as code refactoring. label Mar 27, 2025
@sinano1107
Copy link
Copy Markdown
Contributor Author

For some reason docs/readthedocs.org:optuna now fails.
If you know anything about this, I would appreciate it.

@sinano1107 sinano1107 force-pushed the master branch 2 times, most recently from 3d4ca56 to b2ba40f Compare March 28, 2025 00:37
@sinano1107
Copy link
Copy Markdown
Contributor Author

I thought the docs/readthedocs.org:optuna might be failing because I used git push -force-with-lease, so I created a new #6022, it also has the same error.

Sorry for the mess in the history and PR section.

In the end I don't know the cause either.

@sinano1107
Copy link
Copy Markdown
Contributor Author

I don't know why, but it didn't seem to pinch this time!

@sinano1107 sinano1107 requested a review from nzw0301 March 28, 2025 01:32
Copy link
Copy Markdown
Member

@nzw0301 nzw0301 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, CI jobs sometimes fail due to an external service. Sorry for confusing you.

In terms of PR, could you add a test?

@sinano1107
Copy link
Copy Markdown
Contributor Author

sinano1107 commented Mar 28, 2025

I added a test in the form of an extension to test_optimize_with_direction test_create_study_with_multi_objectives, but I feel it is a little less prospective.

Let's change it to create a new function like test_create_study_with_invalid_args to test it if necessary. Which would you prefer?

@HideakiImamura
Copy link
Copy Markdown
Member

@nzw0301 Could you continue to review this PR?

Copy link
Copy Markdown
Member

@HideakiImamura HideakiImamura left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I have several comments. PTAL.

Comment on lines +153 to +156
assert str(e.value) == (
"Please set either 'minimize' or 'maximize' to direction. You can also set the "
+ "corresponding `StudyDirection` member."
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, we should not test the exact contents of the error message since it is not a specification, so might be changed later. It would be great to confirm that the error is emitted.

Comment on lines +160 to +163
assert (
str(e.value)
== "For multi-objective optimization, using `directions` instead of `direction`."
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

with pytest.raises(ValueError) as e:
# Empty `direction` isn't allowed.
_ = create_study(directions=[])
assert str(e.value) == "The number of objectives must be greater than 0."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

with pytest.raises(ValueError):
with pytest.raises(ValueError) as e:
_ = create_study(direction="minimize", directions=["maximize"])
assert str(e.value) == "Specify only one of `direction` and `directions`."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

with pytest.raises(ValueError):
with pytest.raises(ValueError) as e:
_ = create_study(direction="minimize", directions=[])
assert str(e.value) == "Specify only one of `direction` and `directions`."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@sinano1107
Copy link
Copy Markdown
Contributor Author

@HideakiImamura
Yes, I agree, but such a test would not detect the changes made in this PR. The code before the changes are made will still pass the test.

Do you think it is ok for us to ignore it?

@sinano1107 sinano1107 requested a review from nzw0301 March 28, 2025 07:45
Copy link
Copy Markdown
Member

@nzw0301 nzw0301 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The added test looks perfect. Let me leave a minor comment.

@nzw0301 nzw0301 changed the title Made error message for create_study's direction easier to understand optuna.study Made error message for create_study's direction easier to understand optuna.study Mar 28, 2025
Co-authored-by: Kento Nozawa <k_nzw@klis.tsukuba.ac.jp>
@sinano1107 sinano1107 requested a review from nzw0301 March 28, 2025 09:44
Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this part should also be modified.

optuna/optuna/study/study.py

Lines 1258 to 1261 in 896e0ca

raise ValueError(
"Please set either 'minimize' or 'maximize' to direction. You can also set the "
"corresponding `StudyDirection` member."
)

Could you, @sinano1107 , please check the comment here as well?

@sinano1107
Copy link
Copy Markdown
Contributor Author

@nabenabe0928
Does this edit captured your intentions?

Copy link
Copy Markdown
Member

@nzw0301 nzw0301 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@nabenabe0928
Copy link
Copy Markdown
Contributor

@sinano1107
Yes, the change looks good to me:)
Could you add the unit test for this change as well?
Namely, could you add a test using directions="minimize", which is an invalid input.

@sinano1107
Copy link
Copy Markdown
Contributor Author

@nabenabe0928
I'm happy to hear that from you.
I have added a test, please check it out.

Copy link
Copy Markdown
Contributor

@nabenabe0928 nabenabe0928 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@nabenabe0928 nabenabe0928 merged commit d31ae17 into optuna:master Apr 1, 2025
14 checks passed
@nabenabe0928 nabenabe0928 added this to the v4.3.0 milestone Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code-fix Change that does not change the behavior, such as code refactoring.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make error message for create_study's direction easier to understand

4 participants