-
Notifications
You must be signed in to change notification settings - Fork 0
Replace Enums with string literals #4
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
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -17,3 +17,5 @@ wheels/ | |
| .idea/ | ||
| .vscode/ | ||
|
|
||
| # OS dependent files | ||
| .DS_Store | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,7 @@ | ||
| """bbt-test: Bayesian Bradley-Terry model for algorithm comparison.""" | ||
|
|
||
| from .bbt import HyperPrior, PyBBT, ReportedProperty, TieSolver | ||
| from .bbt import PyBBT | ||
|
|
||
| __all__ = [ | ||
| "HyperPrior", | ||
| "PyBBT", | ||
| "ReportedProperty", | ||
| "TieSolver", | ||
| ] |
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 |
|---|---|---|
| @@ -1,11 +1,7 @@ | ||
| """bbt module: Bayesian Bradley-Terry model implementation.""" | ||
|
|
||
| from .params import HyperPrior, ReportedProperty, TieSolver | ||
| from .py_bbt import PyBBT | ||
|
|
||
| __all__ = [ | ||
| "HyperPrior", | ||
| "PyBBT", | ||
| "ReportedProperty", | ||
| "TieSolver", | ||
| ] |
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,27 @@ | ||
| from typing import Literal, get_args | ||
|
|
||
| HyperPriorType = Literal[ | ||
| "log_normal", | ||
| "cauchy", | ||
| "normal", | ||
| ] | ||
|
|
||
| TieSolverType = Literal["add", "spread", "forget", "davidson"] | ||
|
|
||
| ReportedPropertyColumnType = Literal[ | ||
| "left_model", | ||
| "right_model", | ||
| "median", | ||
| "mean", | ||
| "hdi_low", | ||
| "hdi_high", | ||
| "delta", | ||
| "above_50", | ||
| "in_rope", | ||
| "weak_interpretation", | ||
| "strong_interpretation", | ||
| ] | ||
|
|
||
| ALL_PROPERTIES_COLUMNS: list[ReportedPropertyColumnType] = list( | ||
| get_args(ReportedPropertyColumnType) | ||
| ) | ||
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,64 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| from functools import wraps | ||
| from typing import ( | ||
| Literal, | ||
| get_args, | ||
| get_origin, | ||
| ) | ||
|
|
||
| from pymc.distributions import Cauchy, LogNormal, Normal | ||
|
|
||
| if sys.version_info >= (3, 12): | ||
| from typing import TypeAliasType | ||
| else: | ||
| from typing_extensions import TypeAliasType | ||
|
|
||
|
|
||
| def is_literal_value(value: object, typx: object) -> bool: | ||
| if isinstance(typx, TypeAliasType): | ||
| typx = typx.__value__ | ||
| if get_origin(typx) is Literal: | ||
| return value in get_args(typx) | ||
| return False | ||
|
|
||
|
|
||
| def _validate_params(func): | ||
| from inspect import signature | ||
|
|
||
| sig = signature(func) | ||
|
|
||
| @wraps(func) | ||
| def wrapper(*args, **kwargs): | ||
| bound_args = sig.bind(*args, **kwargs) | ||
| bound_args.apply_defaults() | ||
| for name, value in bound_args.arguments.items(): | ||
| param = sig.parameters[name] | ||
| # If type annotation is a Literal, validate the value | ||
| if param.annotation is not param.empty and is_literal_value( | ||
| value, param.annotation | ||
| ): | ||
| continue # Valid value, continue to next parameter | ||
| elif ( | ||
| param.annotation is not param.empty | ||
| and get_origin(param.annotation) is Literal | ||
| ): | ||
| raise ValueError( | ||
| f"Invalid value '{value}' for parameter '{name}'. Expected one of {get_args(param.annotation)}." | ||
| ) | ||
| return func(*args, **kwargs) | ||
|
|
||
| return wrapper | ||
Thematiq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def _get_distribution_for_prior(prior: str, scale: float): | ||
| match prior: | ||
| case "log_normal": | ||
| return LogNormal("sigma", mu=0, sigma=scale) | ||
| case "cauchy": | ||
| return Cauchy("sigma", alpha=0, beta=scale) | ||
| case "normal": | ||
| return Normal("sigma", mu=0, sigma=scale) | ||
| case _: | ||
| raise ValueError(f"Unsupported hyperprior: {prior}") | ||
Thematiq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
This file was deleted.
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.