forked from ax3l/pydantic-mad
-
Notifications
You must be signed in to change notification settings - Fork 3
Implement ElectricMultipoleParameters #49
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
EZoni
wants to merge
5
commits into
pals-project:main
Choose a base branch
from
EZoni:electric_multipole_parameters
base: main
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3313a3a
Add test of ElectricMultipoleParameters
EZoni 05d626a
Implement draft of ElectricMultipoleParameters
EZoni ec21f26
Quadrupole requires either MagneticMultipoleP or ElectricMultipoleP o…
EZoni b4b4957
Make Sextupole, Octupole, Multipole tests more symmetric
EZoni 14f0f74
Merge branch 'main' into electric_multipole_parameters
EZoni 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
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,63 @@ | ||
| from pydantic import BaseModel, ConfigDict | ||
| from pydantic import BaseModel, ConfigDict, model_validator | ||
| from typing import Any | ||
|
|
||
| # Valid parameter prefixes, their expected format and description | ||
| _PARAMETER_PREFIXES = { | ||
| "tilt": ("tiltN", "Tilt"), | ||
| "En": ("EnN", "Normal component"), | ||
| "Es": ("EsN", "Skew component"), | ||
| } | ||
|
|
||
|
|
||
| def _validate_order( | ||
| key_num: str, parameter_name: str, prefix: str, expected_format: str | ||
| ) -> None: | ||
| """Validate that the order number is a non-negative integer without leading zeros.""" | ||
| error_msg = ( | ||
| f"Invalid {parameter_name}: '{prefix}{key_num}'. " | ||
| f"Parameter must be of the form '{expected_format}', where 'N' is a non-negative integer without leading zeros." | ||
| ) | ||
| if not key_num.isdigit() or (key_num.startswith("0") and key_num != "0"): | ||
| raise ValueError(error_msg) | ||
|
|
||
|
|
||
| class ElectricMultipoleParameters(BaseModel): | ||
| """Electric multipole parameters""" | ||
| """Electric multipole parameters | ||
|
|
||
| Valid parameter formats: | ||
| - tiltN: Tilt of Nth order multipole | ||
| - EnN: Normal component of Nth order multipole | ||
| - EsN: Skew component of Nth order multipole | ||
| - *NL: Length-integrated versions of components (e.g., En3L, EsNL) | ||
|
|
||
| Where N is a positive integer without leading zeros (except "0" itself). | ||
| """ | ||
|
|
||
| # Allow arbitrary fields (TODO: remove this) | ||
| model_config = ConfigDict(extra="allow") | ||
|
|
||
| # TODO: add ElectricMultipoleParameters in a follow-up RP | ||
| # https://pals-project.readthedocs.io/en/latest/element-parameters.html#electricmultipolep-electric-multipole-parameters | ||
| @model_validator(mode="before") | ||
| @classmethod | ||
| def validate(cls, values: dict[str, Any]) -> dict[str, Any]: | ||
| """Validate all parameter names match the expected multipole format.""" | ||
| for key in values: | ||
| # Check if key ends with 'L' for length-integrated values | ||
| is_length_integrated = key.endswith("L") | ||
| base_key = key[:-1] if is_length_integrated else key | ||
|
|
||
| # No length-integrated values allowed for tilt parameter | ||
| if is_length_integrated and base_key.startswith("tilt"): | ||
| raise ValueError(f"Invalid electric multipole parameter: '{key}'. ") | ||
|
|
||
| # Find matching prefix | ||
| for prefix, (expected_format, description) in _PARAMETER_PREFIXES.items(): | ||
| if base_key.startswith(prefix): | ||
| key_num = base_key[len(prefix) :] | ||
| _validate_order(key_num, description, prefix, expected_format) | ||
| break | ||
| else: | ||
| raise ValueError( | ||
| f"Invalid electric multipole parameter: '{key}'. " | ||
| f"Parameters must be of the form 'tiltN', 'EnN', or 'EsN' " | ||
| f"(with optional 'L' suffix for length-integrated), where 'N' is a non-negative integer." | ||
| ) | ||
| return values |
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
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.
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.
Claude Haiku 4.5 through Visual Studio Code thinks that it is appropriate to include the
"Quadrupole"type annotation here: