Skip to content

Commit c63323c

Browse files
waltsimsclaude
andcommitted
Add AlphaMode enum for stringly-typed alpha_mode field
- AlphaMode(str, Enum) with NO_ABSORPTION, NO_DISPERSION, STOKES - kWaveMedium.__post_init__ normalizes string inputs to enum - Backward compatible: AlphaMode == "no_dispersion" works via str inheritance - __str__ returns the value so f-strings render cleanly Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 9201dea commit c63323c

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

kwave/enums.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
from enum import Enum
22

3+
4+
class AlphaMode(str, Enum):
5+
"""Controls which absorption/dispersion terms are included in the equation of state."""
6+
7+
NO_ABSORPTION = "no_absorption"
8+
NO_DISPERSION = "no_dispersion"
9+
STOKES = "stokes"
10+
11+
def __str__(self):
12+
return self.value
13+
14+
315
################################################################
416
# literals that link the discrete cosine and sine transform types with
517
# their type definitions in the functions dtt1D, dtt2D, and dtt3D

kwave/kmedium.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import logging
22
from dataclasses import dataclass
3-
from typing import List
3+
from typing import List, Optional, Union
44

55
import numpy as np
66

77
import kwave.utils.checks
8+
from kwave.enums import AlphaMode
89

910

1011
@dataclass
@@ -20,8 +21,8 @@ class kWaveMedium(object):
2021
# power law absorption exponent
2122
alpha_power: np.array = None
2223
# optional input to force either the absorption or dispersion terms in the equation of state to be excluded;
23-
# valid inputs are 'no_absorption' or 'no_dispersion'
24-
alpha_mode: np.array = None
24+
# valid inputs are AlphaMode.NO_ABSORPTION, AlphaMode.NO_DISPERSION, or the equivalent strings
25+
alpha_mode: Optional[Union[AlphaMode, str]] = None
2526
# frequency domain filter applied to the absorption and dispersion terms in the equation of state
2627
alpha_filter: np.array = None
2728
# 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):
4344

4445
def __post_init__(self):
4546
self.sound_speed = np.atleast_1d(self.sound_speed)
47+
if isinstance(self.alpha_mode, str) and not isinstance(self.alpha_mode, AlphaMode):
48+
self.alpha_mode = AlphaMode(self.alpha_mode)
4649

4750
def check_fields(self, kgrid_shape: np.ndarray) -> None:
4851
"""
@@ -54,13 +57,12 @@ def check_fields(self, kgrid_shape: np.ndarray) -> None:
5457
Returns:
5558
None
5659
"""
57-
# check the absorption mode input is valid
58-
if self.alpha_mode is not None:
59-
assert self.alpha_mode in [
60-
"no_absorption",
61-
"no_dispersion",
62-
"stokes",
63-
], "medium.alpha_mode must be set to 'no_absorption', 'no_dispersion', or 'stokes'."
60+
# check the absorption mode input is valid (already normalized to AlphaMode in __post_init__)
61+
if self.alpha_mode is not None and not isinstance(self.alpha_mode, AlphaMode):
62+
raise ValueError(
63+
f"medium.alpha_mode must be an AlphaMode enum value or one of "
64+
f"'no_absorption', 'no_dispersion', 'stokes', got {self.alpha_mode!r}"
65+
)
6466

6567
# check the absorption filter input is valid
6668
if self.alpha_filter is not None and not (self.alpha_filter.shape == kgrid_shape).all():

0 commit comments

Comments
 (0)