-
Notifications
You must be signed in to change notification settings - Fork 2
Unify quantitative actions interface #111
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
Open
shaharbar1
wants to merge
1
commit into
develop
Choose a base branch
from
feature/unified_quantitative_interface
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
|
|
@@ -20,8 +20,22 @@ | |
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| # SOFTWARE. | ||
|
|
||
| from typing import Any, Dict, List, Mapping, NewType, Optional, Tuple, Union, _GenericAlias, get_args, get_origin | ||
| from typing import ( | ||
| Any, | ||
| Callable, | ||
| Dict, | ||
| List, | ||
| Mapping, | ||
| NewType, | ||
| Optional, | ||
| Tuple, | ||
| Union, | ||
| _GenericAlias, | ||
| get_args, | ||
| get_origin, | ||
| ) | ||
|
|
||
| import numpy as np | ||
| from typing_extensions import Self | ||
|
|
||
| from pybandits.pydantic_version_compatibility import ( | ||
|
|
@@ -45,10 +59,12 @@ | |
| MOProbability = List[Probability] | ||
| MOProbabilityWeight = List[ProbabilityWeight] | ||
| # QuantitativeProbability generalizes probability to include both action quantities and their associated probability | ||
| QuantitativeProbability = Tuple[Tuple[Tuple[Float01, ...], Probability], ...] | ||
| QuantitativeProbabilityWeight = Tuple[Tuple[Tuple[Float01, ...], ProbabilityWeight], ...] | ||
| QuantitativeMOProbability = Tuple[Tuple[Tuple[Float01, ...], List[Probability]], ...] | ||
| QuantitativeMOProbabilityWeight = Tuple[Tuple[Tuple[Float01, ...], List[ProbabilityWeight]], ...] | ||
| QuantitativeProbability = Callable[[np.ndarray], Probability] | ||
| QuantitativeWeight = Callable[[np.ndarray], float] | ||
| QuantitativeProbabilityWeight = Tuple[QuantitativeProbability, QuantitativeWeight] | ||
| QuantitativeMOProbability = Callable[[np.ndarray], MOProbability] | ||
| QuantitativeMOProbabilityWeight = Tuple[Callable[[np.ndarray], MOProbability], Callable[[np.ndarray], float]] | ||
|
|
||
| UnifiedProbability = Union[Probability, QuantitativeProbability] | ||
| UnifiedProbabilityWeight = Union[ProbabilityWeight, QuantitativeProbabilityWeight] | ||
| UnifiedMOProbability = Union[MOProbability, QuantitativeMOProbability] | ||
|
|
@@ -79,10 +95,10 @@ | |
| ActionRewardLikelihood = NewType( | ||
| "ActionRewardLikelihood", | ||
| Union[ | ||
| Dict[UnifiedActionId, float], | ||
| Dict[UnifiedActionId, List[float]], | ||
| Dict[UnifiedActionId, Probability], | ||
| Dict[UnifiedActionId, List[Probability]], | ||
| Dict[ActionId, Union[float, Callable[[np.ndarray], float]]], | ||
| Dict[ActionId, Union[List[float], Callable[[np.ndarray], List[float]]]], | ||
| Dict[ActionId, Union[Probability, Callable[[np.ndarray], Probability]]], | ||
| Dict[ActionId, Union[List[Probability], Callable[[np.ndarray], List[Probability]]]], | ||
| ], | ||
| ) | ||
| ACTION_IDS_PREFIX = "action_ids_" | ||
|
|
@@ -190,6 +206,28 @@ def _get_field_type(cls, key: str) -> Any: | |
| annotation = get_args(annotation) | ||
| return annotation | ||
|
|
||
| @classmethod | ||
| def _normalize_field(cls, v: Any, field_name: str) -> Any: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use it in validators of "pre" behavior to get the default value of an ungiven attribute |
||
| """ | ||
| Normalize a field value to its default if None. | ||
|
|
||
| This utility method ensures that optional fields receive their default | ||
| values when not explicitly provided. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| v : Any | ||
| The field value to normalize. | ||
| field_name : str | ||
| Name of the field in the model. | ||
|
|
||
| Returns | ||
| ------- | ||
| Any | ||
| The original value if not None, otherwise the field's default value. | ||
| """ | ||
| return v if v is not None else cls.model_fields[field_name].default | ||
|
|
||
| if pydantic_version == PYDANTIC_VERSION_1: | ||
|
|
||
| @classproperty | ||
|
|
||
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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of the type is misleading...
maybe it should be: QuantitativeProbabilityFunction?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ronshiff1 Maybe ContinuousProbability? I just didn't want it to be too long.