Skip to content

Commit 76a7c13

Browse files
committed
Enhance documentation for array backend functions and improve host array conversion
1 parent d95687c commit 76a7c13

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

setigen/voltage/_array_backend.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,40 @@
1414

1515

1616
def _env_backend() -> ArrayBackend:
17+
"""Resolve the legacy environment-selected backend.
18+
19+
Returns:
20+
``"cupy"`` when ``SETIGEN_ENABLE_GPU=1``; otherwise ``"numpy"``.
21+
"""
1722
return "cupy" if os.getenv("SETIGEN_ENABLE_GPU", "0") == "1" else "numpy"
1823

1924

2025
def _validate_backend(backend: str) -> ArrayBackend:
26+
"""Validate and normalize an array backend name.
27+
28+
Args:
29+
backend: Backend name to validate.
30+
31+
Returns:
32+
Validated backend name.
33+
34+
Raises:
35+
ValueError: If the backend name is unsupported.
36+
"""
2137
if backend not in ("auto", "numpy", "cupy"):
2238
raise ValueError("backend must be one of 'auto', 'numpy', or 'cupy'.")
2339
return backend # type: ignore[return-value]
2440

2541

2642
def _import_cupy() -> Any:
43+
"""Import and cache CuPy.
44+
45+
Returns:
46+
Imported CuPy module.
47+
48+
Raises:
49+
ImportError: If CuPy is unavailable.
50+
"""
2751
if "cupy" not in _modules:
2852
try:
2953
_modules["cupy"] = importlib.import_module("cupy")
@@ -94,12 +118,23 @@ def get_array_module(backend: str | None = None) -> Any:
94118

95119

96120
def get_backend() -> str:
97-
"""Return the currently active concrete backend name."""
121+
"""Return the currently active concrete backend name.
122+
123+
Returns:
124+
Concrete backend name, either ``"numpy"`` or ``"cupy"``.
125+
"""
98126
return "cupy" if get_array_module().__name__ == "cupy" else "numpy"
99127

100128

101129
def asnumpy(array: Any) -> np.ndarray:
102-
"""Return ``array`` as a NumPy array, copying from the GPU when needed."""
130+
"""Return ``array`` as a NumPy array, copying from the GPU when needed.
131+
132+
Args:
133+
array: NumPy-like or CuPy-like array.
134+
135+
Returns:
136+
Host-side NumPy array.
137+
"""
103138
module = get_array_module()
104139
if module is not np and hasattr(module, "asnumpy"):
105140
return module.asnumpy(array)
@@ -111,9 +146,22 @@ class _ArrayModuleProxy:
111146

112147
@property
113148
def __name__(self) -> str:
149+
"""Return the active array module name.
150+
151+
Returns:
152+
Active module name.
153+
"""
114154
return get_array_module().__name__
115155

116156
def __getattr__(self, name: str) -> Any:
157+
"""Forward attribute lookups to the active array module.
158+
159+
Args:
160+
name: Attribute name to resolve.
161+
162+
Returns:
163+
Attribute from the active array module.
164+
"""
117165
return getattr(get_array_module(), name)
118166

119167
def __repr__(self) -> str:

setigen/voltage/spectrogram.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,15 @@ def _make_direct_input_spec(backend: Any) -> Any:
199199

200200

201201
def _to_host_array(array: Any, *, xp: Any) -> Any:
202-
"""Return an array on the host, copying from CuPy when needed."""
202+
"""Return an array on the host, copying from CuPy when needed.
203+
204+
Args:
205+
array: NumPy-like or CuPy-like array.
206+
xp: Numerical array module used to create ``array``.
207+
208+
Returns:
209+
Host-side array when ``xp`` supports GPU arrays; otherwise ``array``.
210+
"""
203211
try:
204212
return xp.asnumpy(array)
205213
except AttributeError:

0 commit comments

Comments
 (0)