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

env_int

Retrieve the value of an environment variable as an integer.

load_mat_to_np

Load MATLAB data into a Python dictionary.

parsing_arguments

Parse command-line arguments for the imaging entry point.

parsing_parameters

Merge configuration-file parameters with CLI overrides.

print_dict

Print key-value pairs from a dictionary.

resolve_existing_file_path

Resolve a file path against common project roots and validate existence.

verbose_print

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:
  • name (str) – Environment variable name.

  • default (int) – Fallback value used when the variable is unset.

Returns:

Parsed integer value from the environment variable, or default when the variable does not exist.

Return type:

int

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:
  • path (str) – Raw file path.

  • library_root (str) – Package/library root used as a fallback search location for relative paths.

  • relative_anchor (str, optional) – Additional root used before library_root for relative resolution. Default: None.

Returns:

Absolute resolved path to an existing file.

Return type:

str

Raises:

FileNotFoundError – If no candidate path resolves to an existing file.

Note

Relative paths are resolved in this order: current working directory, relative_anchor when provided, then library_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.

Parameters:
  • curr_dict (Dict[str, Any]) – Dictionary whose items are printed.

  • indentation (int, optional) – Number of leading spaces per printed line. Default: 4.

  • flush (bool, optional) – Whether to flush stdout after each print. Default: True.

hyri.interface.io.verbose_print(args: str, verbose: bool = True, end='\n') → None#

Print a message only when verbose mode is enabled.

Parameters:
  • args (str) – Message text to print.

  • verbose (bool, optional) – If True, print args to stdout. Default: True.

  • end (str, optional) – String to append after the message. Default: \n.

Returns:

This function does not return a value.

Return type:

None

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 with scipy.io.loadmat(). Complex-valued v7.3 HDF5 datasets stored with real and imag fields are converted to complex NumPy arrays.

Parameters:

file_name (str) – Path to the .mat file.

Returns:

Mapping from MATLAB variable names to loaded arrays or values.

Return type:

Dict[str, Any]

Raises:
  • FileNotFoundError – If file_name does 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 real and imag fields, this function reconstructs complex arrays as real + 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:

argparse.Namespace

Raises:

SystemExit – If command-line parsing fails or --help is requested.

Note

This function parses values from sys.argv using argparse.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 main config 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 None or 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 main section or command-line inputs.