utils
Utility functions for the different SRC algorithms.
Functions#
default_rng #
default_rng(seed: int | None) -> np.random.Generator
Return a seeded NumPy Generator.
Always uses NumPy so that the same seed produces identical draws
regardless of the device, and avoids CuPy Generator API
differences (e.g. missing .normal()).
Source code in src/src_method/utils/_backend.py
44 45 46 47 48 49 50 51 52 53 | |
get_xp #
get_xp(device: str) -> ModuleType
Return the array module for the requested device.
| PARAMETER | DESCRIPTION |
|---|---|
device
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ModuleType
|
The numpy or cupy module. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
ImportError
|
If |
Source code in src/src_method/utils/_backend.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
setup_logging #
setup_logging(default_level: str = 'INFO') -> None
Configure logging for the entire application.
| PARAMETER | DESCRIPTION |
|---|---|
default_level
|
Default logging level.
TYPE:
|
The logging level can be overridden by setting the LOG_LEVEL_SRC
environment. For example:
LOG_LEVEL_SRC=DEBUG python -m <your_module>
pytest -o log_cli_level=DEBUG tests/
Source code in src/src_method/utils/logging_config.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
to_numpy #
to_numpy(arr: NDArray) -> np.ndarray
Bring an array onto the host as a numpy array (no-op for numpy).
Source code in src/src_method/utils/_backend.py
56 57 58 59 60 61 62 | |
truncated_qr #
truncated_qr(
matrix: NDArray, cutoff: float, xp: ModuleType = np
) -> NDArray
QR with SVD-based rank truncation, returning only the isometry.
Decomposes matrix = Q @ R, then truncates via SVD on R,
discarding singular values below cutoff * sigma_max.
When cutoff <= 0 this falls back to a plain QR (no truncation).
| PARAMETER | DESCRIPTION |
|---|---|
matrix
|
Input matrix of shape (m, n).
TYPE:
|
cutoff
|
Relative singular-value threshold. Singular values
satisfying
TYPE:
|
xp
|
Array module (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NDArray
|
The truncated isometry Q of shape |
Source code in src/src_method/utils/linalg.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |