nirs4all.utils.backend module

Backend detection and lazy loading utilities for ML frameworks.

This module provides: - Lazy detection of framework availability (no imports at module load) - Cached availability checks for performance - Clean error messages guiding users to install missing dependencies - GPU availability detection across all frameworks

Usage:

from nirs4all.utils.backend import require_backend, is_available, get_backend_info

# Check availability without importing if is_available(‘tensorflow’):

# Safe to import tensorflow here import tensorflow as tf

# Require a backend (raises helpful ImportError if missing) require_backend(‘torch’) import torch

nirs4all.utils.backend.clear_availability_cache()[source]

Clear the availability cache (useful for testing).

nirs4all.utils.backend.get_backend_info() → dict[str, dict[str, Any]][source]

Get comprehensive info about all backends.

Returns:

Dictionary with availability and GPU info for each backend.

Example

>>> info = get_backend_info()
>>> for name, details in info.items():
...     status = "✓" if details['available'] else "✗"
...     print(f"{status} {name}")
nirs4all.utils.backend.get_gpu_info() → dict[str, Any][source]

Get detailed GPU information for all available backends.

Returns:

Dictionary with GPU info per backend, including device counts and names where available.

Example

>>> info = get_gpu_info()
>>> if info.get('torch', {}).get('available'):
...     print(f"GPU: {info['torch']['device_name']}")
nirs4all.utils.backend.is_ikpls_available() → bool[source]

Check if ikpls’ NumPy backend is installed.

Returns:

True if the IKPLS NumPy backend used by IKPLS is available.

nirs4all.utils.backend.is_jax_available() → bool[source]

Check if JAX is installed.

Returns:

True if JAX is available.

nirs4all.utils.backend.is_keras_available() → bool[source]

Check if Keras is installed.

Returns:

True if Keras is available.

nirs4all.utils.backend.is_tensorflow_available() → bool[source]

Check if TensorFlow is installed.

Returns:

True if TensorFlow is available.

nirs4all.utils.backend.is_torch_available() → bool[source]

Check if PyTorch is installed.

Returns:

True if PyTorch is available.

nirs4all.utils.backend.lazy_import(module_name: str, backend: str | None = None)[source]

Create a lazy import that only loads the module when accessed.

Parameters:
  • module_name – Full module path to import.

  • backend – Optional backend to check before importing.

Returns:

A proxy object that imports the module on first access.

Example

>>> tf = lazy_import('tensorflow', backend='tensorflow')
>>> # tensorflow not imported yet
>>> model = tf.keras.Model()  # Now tensorflow is imported
nirs4all.utils.backend.print_backend_status()[source]

Print a formatted summary of backend availability.

Displays a table showing which backends are installed and whether GPU support is available for deep learning frameworks.