-
Notifications
You must be signed in to change notification settings - Fork 95
Abstract syntax tree parser for ConfigSpace #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
469d059
First version
thijssnelleman 7642320
Expanding tests
thijssnelleman 753fa8d
Simplifying print statement as ast.unparse is only available from Pyt…
thijssnelleman 92d450d
Bugfixes
thijssnelleman de35995
Updating documentation
thijssnelleman c209f0f
Merge branch 'main' of https://github.com/automl/ConfigSpace into abs…
thijssnelleman be3db2d
Cleaning up code, expanding docs
thijssnelleman 4b344dc
bugfix
thijssnelleman 83119dd
docs typo
thijssnelleman 76d6617
Adding docs, minor fixes
thijssnelleman f165c9e
Merge branch 'main' of https://github.com/automl/ConfigSpace into abs…
thijssnelleman 03a9790
Fixing example
thijssnelleman 2f1c563
Remove mistake file
thijssnelleman 995f02e
docfix
thijssnelleman 96f6dca
Potential fix for pull request finding
thijssnelleman a9c9f36
Potential fix for pull request finding
thijssnelleman bb199bb
Updating copilot fix, adding tests
thijssnelleman 1f1a609
Bugfixes
thijssnelleman 3d1f4ba
docfix
thijssnelleman 2192f7f
reference fix
thijssnelleman fc11a3b
Parameter rename
thijssnelleman d2ebe05
Rename function
thijssnelleman b839258
Separating the examples as two subsections
thijssnelleman ca5d3b7
Fixing pytest message check
thijssnelleman 88bbcdf
docstring fix
thijssnelleman af3d63f
seperating test in two parts
thijssnelleman 6f16277
Adding test
thijssnelleman 2f769e7
Clarification for possible ambiguity, adding tests
thijssnelleman fa9ea17
Updating method to be more strict on HP names
thijssnelleman b5eef6f
Docs clarification
thijssnelleman 1c9965c
removing commented out code
thijssnelleman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| ## Utilities | ||
|
|
||
| ### Expression to Configspace | ||
|
|
||
| In some cases we may have (highly) complex conditions or forbidden expressions that are already denoted as a regular expression. In that case, `ConfigSpace` can automatically convert them into a `ConfigSpace` expression using the [`parse_expression_from_string`][ConfigSpace.util.parse_expression_from_string]`parse_expression_from_string`. This function interprets the expression using the Python `Abstract Syntax Tree` parser and recursively converts it into the appropriate structure. | ||
|
|
||
| !!! note | ||
| The converted expression is not added to ConfigSpace, only returned to the user. | ||
|
|
||
| !!! note | ||
| If the expression contains illegal values, errors, or requires functionalities not available in `ConfigSpace`, appriopriate exceptions will be raised. | ||
|
|
||
| !!! note | ||
| Expressions differentiate variables (Hyperparameter names) from constants (Categorical values) based on quotation marks; "a != b" implies hyperparameter a does not equal hyperparameter b, "a != 'b'" implies hyperparameter a does not equal categorical/ordinal value b. | ||
|
|
||
| #### Adding a condition | ||
|
|
||
| In this code example we show how you can add a hyperparameter condition to ConfigSpace from a string. Note that the conditional hyperparameter is specified as a seperate argument and is not part of the expression string! | ||
|
|
||
| ```python exec="True" result="python" source="tabbed-left" | ||
| from ConfigSpace import ConfigurationSpace | ||
| from ConfigSpace.util import parse_expression_from_string | ||
|
|
||
| cs = ConfigurationSpace( | ||
| { | ||
| "a": (0, 10), # Integer from 0 to 10 | ||
| "b": ["cat", "dog"], # Categorical with choices "cat" and "dog" | ||
| "c": (0.0, 1.0), # Float from 0.0 to 1.0 | ||
| } | ||
| ) | ||
| print(cs) | ||
|
|
||
| # Now we add a condition and forbidden using regular expressions | ||
| condition = "b != 'cat' && c > 0.001" | ||
| condition = parse_expression_from_string(condition, cs, conditional_hyperparameter=cs["a"]) # We have to specify the conditional HP seperately here as the final argument | ||
|
|
||
| print(condition) | ||
|
|
||
| cs.add(condition) | ||
|
|
||
| print(cs) | ||
| ``` | ||
|
|
||
| #### Adding a forbidden expression | ||
|
|
||
| In this example we add a forbidden expression to ConfigSpace from string. Note that the conditional hyperparameter remains unspecified; this leads to ConfigSpace interpreting the expression as a forbidden expression. | ||
|
|
||
| ```python exec="True" result="python" source="tabbed-left" | ||
| from ConfigSpace import ConfigurationSpace | ||
| from ConfigSpace.util import parse_expression_from_string | ||
|
|
||
| cs = ConfigurationSpace( | ||
| { | ||
| "a": (0, 10), # Integer from 0 to 10 | ||
| "b": ["cat", "dog"], # Categorical with choices "cat" and "dog" | ||
| "c": (0.0, 1.0), # Float from 0.0 to 1.0 | ||
| } | ||
| ) | ||
| print(cs) | ||
| forbidden = "a > 5 && c >= 0.94" | ||
| forbidden = parse_expression_from_string(forbidden, cs) | ||
|
|
||
| print(forbidden) | ||
|
|
||
| cs.add(forbidden) | ||
|
|
||
| print(cs) | ||
| ``` |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.