-
Notifications
You must be signed in to change notification settings - Fork 53
Add AlphaMode enum for stringly-typed alpha_mode field #705
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| import logging | ||
| from dataclasses import dataclass | ||
| from typing import List | ||
| from typing import List, Optional, Union | ||
|
|
||
| import numpy as np | ||
|
|
||
| import kwave.utils.checks | ||
| from kwave.enums import AlphaMode | ||
|
|
||
|
|
||
| @dataclass | ||
|
|
@@ -20,8 +21,8 @@ class kWaveMedium(object): | |
| # power law absorption exponent | ||
| alpha_power: np.array = None | ||
| # optional input to force either the absorption or dispersion terms in the equation of state to be excluded; | ||
| # valid inputs are 'no_absorption' or 'no_dispersion' | ||
| alpha_mode: np.array = None | ||
| # valid inputs are AlphaMode.NO_ABSORPTION, AlphaMode.NO_DISPERSION, or the equivalent strings | ||
| alpha_mode: Optional[Union[AlphaMode, str]] = None | ||
| # frequency domain filter applied to the absorption and dispersion terms in the equation of state | ||
| alpha_filter: np.array = None | ||
| # two element array used to control the sign of absorption and dispersion terms in the equation of state | ||
|
|
@@ -43,6 +44,8 @@ class kWaveMedium(object): | |
|
|
||
| def __post_init__(self): | ||
| self.sound_speed = np.atleast_1d(self.sound_speed) | ||
| if isinstance(self.alpha_mode, str) and not isinstance(self.alpha_mode, AlphaMode): | ||
| self.alpha_mode = AlphaMode(self.alpha_mode) | ||
|
|
||
| def check_fields(self, kgrid_shape: np.ndarray) -> None: | ||
| """ | ||
|
|
@@ -54,13 +57,12 @@ def check_fields(self, kgrid_shape: np.ndarray) -> None: | |
| Returns: | ||
| None | ||
| """ | ||
| # check the absorption mode input is valid | ||
| if self.alpha_mode is not None: | ||
| assert self.alpha_mode in [ | ||
| "no_absorption", | ||
| "no_dispersion", | ||
| "stokes", | ||
| ], "medium.alpha_mode must be set to 'no_absorption', 'no_dispersion', or 'stokes'." | ||
| # check the absorption mode input is valid (already normalized to AlphaMode in __post_init__) | ||
| if self.alpha_mode is not None and not isinstance(self.alpha_mode, AlphaMode): | ||
| raise ValueError( | ||
| f"medium.alpha_mode must be an AlphaMode enum value or one of " | ||
| f"'no_absorption', 'no_dispersion', 'stokes', got {self.alpha_mode!r}" | ||
| ) | ||
|
Comment on lines
+61
to
+65
Contributor
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.
The type hint # check the absorption mode input is valid (already normalized to AlphaMode in __post_init__)
if self.alpha_mode is not None:
if isinstance(self.alpha_mode, str) and not isinstance(self.alpha_mode, AlphaMode):
self.alpha_mode = AlphaMode(self.alpha_mode) # late-normalise post-construction strings
elif not isinstance(self.alpha_mode, AlphaMode):
raise ValueError(
f"medium.alpha_mode must be an AlphaMode enum value or one of "
f"'no_absorption', 'no_dispersion', 'stokes', got {self.alpha_mode!r}"
) |
||
|
|
||
| # check the absorption filter input is valid | ||
| if self.alpha_filter is not None and not (self.alpha_filter.shape == kgrid_shape).all(): | ||
|
|
||
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.
ValueErrorfrom enum constructorWhen an invalid string is passed at construction time (e.g.
kWaveMedium(sound_speed=1500, alpha_mode="typo")),AlphaMode("typo")raisesValueError: 'typo' is not a valid AlphaMode— the more descriptive message incheck_fieldsis never reached. Consider wrapping the conversion to re-raise with a clearer hint: