-
Notifications
You must be signed in to change notification settings - Fork 4
added the controlsClass #10
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
15 commits
Select commit
Hold shift + click to select a range
1ce7da4
added the test file for controls class
RabiyaF 3fa598d
adds enums
RabiyaF 79336c4
adds the procedure classes with doc strings and typings
RabiyaF 71d257c
added controls class with input validation
RabiyaF ecef5f6
added the display methods for all the classes
RabiyaF 26f3fa9
added tests for the procedure classes
RabiyaF 4df8fb0
added tests for controls class
RabiyaF d591f71
updating typing in control classes
RabiyaF 57c9406
updated enums in controls and tests
RabiyaF 9100ac1
updated typings to literal and added tests to check property types an…
RabiyaF 01d29de
converted procedures to pydantic classes
RabiyaF 6ebe7c2
added model verification
RabiyaF e51f11a
added __repr__ method for control class
RabiyaF bbf5253
addressed the review comments
RabiyaF 8448f28
addressed comments
RabiyaF 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,110 @@ | ||
| import tabulate | ||
| from typing import Union | ||
| from pydantic import BaseModel, Field, field_validator | ||
| from RAT.utils.enums import ParallelOptions, Procedures, DisplayOptions, BoundHandlingOptions, StrategyOptions | ||
|
|
||
|
|
||
| class BaseProcedure(BaseModel, validate_assignment = True, extra = 'forbid'): | ||
| """ | ||
| Defines the base class with properties used in all five procedures. | ||
| """ | ||
| parallel: ParallelOptions = ParallelOptions.Single | ||
| calcSldDuringFit: bool = False | ||
| resamPars: list[float] = Field([0.9, 50], min_length = 2, max_length = 2) | ||
| display: DisplayOptions = DisplayOptions.Iter | ||
|
|
||
| @field_validator("resamPars") | ||
| def check_resamPars(cls, resamPars): | ||
| if not 0 < resamPars[0] < 1: | ||
| raise ValueError('resamPars[0] must be between 0 and 1') | ||
| if resamPars[1] < 0: | ||
| raise ValueError('resamPars[1] must be greater than or equal to 0') | ||
| return resamPars | ||
|
|
||
|
|
||
| class Calculate(BaseProcedure, validate_assignment = True, extra = 'forbid'): | ||
| """ | ||
| Defines the class for the calculate procedure. | ||
| """ | ||
| procedure: Procedures = Field(Procedures.Calculate, frozen = True) | ||
RabiyaF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class Simplex(BaseProcedure, validate_assignment = True, extra = 'forbid'): | ||
| """ | ||
| Defines the class for the simplex procedure. | ||
| """ | ||
| procedure: Procedures = Field(Procedures.Simplex, frozen = True) | ||
| tolX: float = Field(1e-6, gt = 0) | ||
| tolFun: float = Field(1e-6, gt = 0) | ||
| maxFunEvals: int = Field(10000, gt = 0) | ||
| maxIter: int = Field(1000, gt = 0) | ||
| updateFreq: int = -1 | ||
| updatePlotFreq: int = -1 | ||
|
|
||
|
|
||
| class DE(BaseProcedure, validate_assignment = True, extra = 'forbid'): | ||
| """ | ||
| Defines the class for the Differential Evolution procedure. | ||
| """ | ||
| procedure: Procedures = Field(Procedures.DE, frozen = True) | ||
| populationSize: int = Field(20, ge = 1) | ||
| fWeight: float = 0.5 | ||
| crossoverProbability: float = Field(0.8, gt = 0, lt = 1) | ||
| strategy: StrategyOptions = StrategyOptions.RandomWithPerVectorDither | ||
| targetValue: float = Field(1.0, ge = 1) | ||
| numGenerations: int = Field(500, ge = 1) | ||
|
|
||
|
|
||
| class NS(BaseProcedure, validate_assignment = True, extra = 'forbid'): | ||
| """ | ||
| Defines the class for the Nested Sampler procedure. | ||
| """ | ||
| procedure: Procedures = Field(Procedures.NS, frozen = True) | ||
| Nlive: int = Field(150, ge = 1) | ||
| Nmcmc: float = Field(0.0, ge = 0) | ||
| propScale: float = Field(0.1, gt = 0, lt = 1) | ||
| nsTolerance: float = Field(0.1, ge = 0) | ||
|
|
||
|
|
||
| class Dream(BaseProcedure, validate_assignment = True, extra = 'forbid'): | ||
| """ | ||
| Defines the class for the Dream procedure | ||
| """ | ||
| procedure: Procedures = Field(Procedures.Dream, frozen = True) | ||
| nSamples: int = Field(50000, ge = 0) | ||
| nChains: int = Field(10, gt = 0) | ||
| jumpProb: float = Field(0.5, gt = 0, lt = 1) | ||
| pUnitGamma:float = Field(0.2, gt = 0, lt = 1) | ||
| boundHandling: BoundHandlingOptions = BoundHandlingOptions.Fold | ||
|
|
||
|
|
||
| class ControlsClass: | ||
|
|
||
| def __init__(self, | ||
| procedure: Procedures = Procedures.Calculate, | ||
| **properties) -> None: | ||
|
|
||
| if procedure == Procedures.Calculate: | ||
| self.controls = Calculate(**properties) | ||
| elif procedure == Procedures.Simplex: | ||
| self.controls = Simplex(**properties) | ||
| elif procedure == Procedures.DE: | ||
| self.controls = DE(**properties) | ||
| elif procedure == Procedures.NS: | ||
| self.controls = NS(**properties) | ||
| elif procedure == Procedures.Dream: | ||
| self.controls = Dream(**properties) | ||
|
|
||
| @property | ||
| def controls(self) -> Union[Calculate, Simplex, DE, NS, Dream]: | ||
| return self._controls | ||
|
|
||
| @controls.setter | ||
| def controls(self, value: Union[Calculate, Simplex, DE, NS, Dream]) -> None: | ||
| self._controls = value | ||
|
|
||
| def __repr__(self) -> str: | ||
| properties = [["Property", "Value"]] +\ | ||
| [[k, v] for k, v in self._controls.__dict__.items()] | ||
| table = tabulate.tabulate(properties, headers="firstrow") | ||
| return table | ||
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,48 @@ | ||
| from enum import Enum | ||
| try: | ||
| from enum import StrEnum | ||
| except ImportError: | ||
| from strenum import StrEnum | ||
|
|
||
|
|
||
| class ParallelOptions(StrEnum): | ||
| """Defines the avaliable options for parallelization""" | ||
| Single = 'single' | ||
| Points = 'points' | ||
| Contrasts = 'contrasts' | ||
| All = 'all' | ||
|
|
||
|
|
||
| class Procedures(StrEnum): | ||
| """Defines the avaliable options for procedures""" | ||
| Calculate = 'calculate' | ||
| Simplex = 'simplex' | ||
| DE = 'de' | ||
| NS = 'ns' | ||
| Dream = 'dream' | ||
|
|
||
|
|
||
| class DisplayOptions(StrEnum): | ||
| """Defines the avaliable options for display""" | ||
| Off = 'off' | ||
| Iter = 'iter' | ||
| Notify = 'notify' | ||
| Final = 'final' | ||
|
|
||
|
|
||
| class BoundHandlingOptions(StrEnum): | ||
| """Defines the avaliable options for bound handling""" | ||
| Off = 'off' | ||
| Reflect = 'reflect' | ||
| Bound = 'bound' | ||
| Fold = 'fold' | ||
|
|
||
|
|
||
| class StrategyOptions(Enum): | ||
RabiyaF marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Defines the avaliable options for strategies""" | ||
| Random = 1 | ||
| LocalToBest = 2 | ||
| BestWithJitter = 3 | ||
| RandomWithPerVectorDither = 4 | ||
| RandomWithPerGenerationDither = 5 | ||
| RandomEitherOrAlgorithm = 6 | ||
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.