hyri.interface.io#
Provide interface IO helpers for configuration and MAT-file loading.
This file implements environment/config parsing utilities used by the imaging entry points, plus compatibility loading for MATLAB files stored in either legacy formats or HDF5-based v7.3 format.
Functions
Retrieve the value of an environment variable as an integer. |
|
Load MATLAB data into a Python dictionary. |
|
Parse command-line arguments for the imaging entry point. |
|
Merge configuration-file parameters with CLI overrides. |
|
Print key-value pairs from a dictionary. |
|
Resolve a file path against common project roots and validate existence. |
|
Print a message only when verbose mode is enabled. |
- hyri.interface.io.env_int(name: str, default: int) int#
Retrieve the value of an environment variable as an integer.
- Parameters:
- Returns:
Parsed integer value from the environment variable, or
defaultwhen the variable does not exist.- Return type:
- Raises:
ValueError – If the environment variable exists but cannot be parsed as an integer.
Examples
>>> import os >>> os.environ["RI_NUM_WORKERS"] = "4" >>> env_int("RI_NUM_WORKERS", 1) 4 >>> env_int("RI_UNSET_KEY", 1) 1
- hyri.interface.io.resolve_existing_file_path(path: str, library_root: str, relative_anchor: str | None = None) str#
Resolve a file path against common project roots and validate existence.
The input path may be absolute, relative, or include environment variable and user-home expansions. Relative paths are resolved in priority order against the current working directory, an optional anchor, and the provided library root.
- Parameters:
- Returns:
Absolute resolved path to an existing file.
- Return type:
- Raises:
FileNotFoundError – If no candidate path resolves to an existing file.
Note
Relative paths are resolved in this order: current working directory,
relative_anchorwhen provided, thenlibrary_root.Examples
>>> resolve_existing_file_path("config/default.json", ".") '/abs/path/to/config/default.json'
- hyri.interface.io.print_dict(curr_dict: dict[str, Any], indentation: int = 4, flush: bool = True) None#
Print key-value pairs from a dictionary.
- hyri.interface.io.verbose_print(args: str, verbose: bool = True, end='\n') None#
Print a message only when verbose mode is enabled.
- hyri.interface.io.load_mat_to_np(file_name: str) dict[str, Any]#
Load MATLAB data into a Python dictionary.
Files with MATLAB v7.3 format are read with
h5py, while older formats are read withscipy.io.loadmat(). Complex-valued v7.3 HDF5 datasets stored withrealandimagfields are converted to complex NumPy arrays.- Parameters:
file_name (str) – Path to the
.matfile.- Returns:
Mapping from MATLAB variable names to loaded arrays or values.
- Return type:
Dict[str, Any]
- Raises:
FileNotFoundError – If
file_namedoes not exist.OSError – If the file cannot be opened or is not a valid MAT file.
Note
MATLAB v7.3 files are HDF5 containers and are read dataset-by-dataset. For structured datasets with
realandimagfields, this function reconstructs complex arrays asreal + 1j * imag.
- hyri.interface.io.parsing_arguments() Namespace#
Parse command-line arguments for the imaging entry point.
- Returns:
Parsed CLI arguments including configuration path, dataset selection, imaging dimensions, algorithm choice, and channel-selection options.
- Return type:
- Raises:
SystemExit – If command-line parsing fails or
--helpis requested.
Note
This function parses values from
sys.argvusingargparse.ArgumentParser.Examples
Parse arguments from
sys.argv:>>> import sys >>> sys.argv = [ ... "hyri-imaging", ... "--data_file", ... "./data/example_meas", ... "--algorithm", ... "hyperairi", ... "--ch_start", ... "1", ... "--ch_last", ... "36", ... ] >>> args = parsing_arguments() >>> args.config is None True >>> args.algorithm 'hyperairi' >>> args.ch_last 36
- hyri.interface.io.parsing_parameters(config_file: str | None, input_param: Namespace | None = None) dict[str, Any]#
Merge configuration-file parameters with CLI overrides.
The returned dictionary starts from the
mainconfig section, applies non-null CLI overrides for matching keys, then extends algorithm-specific and shared parameter groups.- Parameters:
config_file (str, optional) – Path to the JSON configuration file. If
Noneor empty, the default configuration bundled with the package is used.input_param (argparse.Namespace, optional) – Parsed CLI arguments used as overrides when values are not
None. Default:None.
- Returns:
Flattened runtime parameter dictionary used by the imaging pipeline.
- Return type:
Dict[str, Any]
- Raises:
NotImplementedError – If the selected algorithm is not supported by the
parameter-routing logic. –
FileNotFoundError – If the specified configuration file does not exist.
ValueError – If the required data file parameter is missing or does not exist.
Note
Command-line inputs have the highest priority. Algorithm-specific parameters are expected not to overlap with keys from the
mainsection or command-line inputs.