pycsamt.backends#

Runtime backend detection, selection, compatibility checks, and backend metadata helpers.

Backend abstraction and lazy discovery for deep-learning frameworks.

pycsamt’s AI/ML modules support both PyTorch and TensorFlow. Neither framework is a required dependency of the package — they are loaded lazily only when the AI module is actually used.

Configuration#

The active backend is resolved using this priority chain:

  1. Explicit call to set_backend() in the current Python session.

  2. PYCSAMT_AI_BACKEND environment variable (torch / tensorflow / auto).

  3. ~/.pycsamt/config.json key "ai_backend".

  4. Auto-detection: first available framework in ['torch', 'tensorflow'].

Quick start#

>>> import pycsamt
>>> pycsamt.list_backends()
{'torch': True, 'tensorflow': False}
>>> pycsamt.set_backend('torch')      # or 'tensorflow' / 'auto'
>>> pycsamt.get_backend()
'torch'

Environment variable#

Set before importing pycsamt to override the default:

PYCSAMT_AI_BACKEND=tensorflow python my_script.py
pycsamt.backends.get_backend()[source]#

Return the name of the currently active backend.

Returns:

name'torch', 'tensorflow', or 'none' when no DL framework is installed.

Return type:

str

Examples

>>> from pycsamt.backends import get_backend
>>> get_backend()
'torch'
pycsamt.backends.set_backend(name, *, persist=False)[source]#

Set the active AI backend.

Parameters:
  • name ({'torch', 'tensorflow', 'auto'}) – 'auto' triggers detection and selects the first available framework.

  • persist (bool, default False) – If True, save the choice to ~/.pycsamt/config.json so it survives across Python sessions.

Raises:
  • ValueError – If name is not 'torch', 'tensorflow', or 'auto'.

  • ImportError – If the requested backend is not installed.

Return type:

None

Examples

>>> from pycsamt.backends import set_backend
>>> set_backend('torch')
>>> set_backend('tensorflow', persist=True)   # saves to config file
pycsamt.backends.auto_detect()[source]#

Detect the best available backend and activate it.

Returns:

name – The name of the detected and activated backend.

Return type:

str

Raises:

RuntimeError – If no compatible framework is installed.

Examples

>>> from pycsamt.backends import auto_detect
>>> backend = auto_detect()
>>> print(backend)
torch
pycsamt.backends.list_backends()[source]#

Return availability status for all known backends.

Returns:

availability{'torch': bool, 'tensorflow': bool}

Return type:

dict

Examples

>>> from pycsamt.backends import list_backends
>>> list_backends()
{'torch': True, 'tensorflow': False}
pycsamt.backends.get_backend_instance()[source]#

Return the concrete NeuralBackend instance for the active backend, or None when no DL framework is installed.

Returns:

backendTorchBackend, TensorFlowBackend, or None when no framework is available.

Return type:

NeuralBackend or None

pycsamt.backends.detect_available_backends()#

Return a list of available deep-learning backends.

Returns:

backends – Ordered list of backend names that are currently importable. Possible values: 'torch', 'tensorflow'. Empty list if neither framework is installed.

Return type:

list of str

Examples

>>> from pycsamt.backends._detect import detect_available_backends
>>> available = detect_available_backends()
>>> print(available)          # e.g. ['torch'] or ['torch', 'tensorflow']
pycsamt.backends.probe_backend(name)#

Return True if name is importable in the current environment.

No import side-effects — uses importlib.util.find_spec() only.

Parameters:

name (str) – 'torch' or 'tensorflow'.

Return type:

bool

pycsamt.backends.get_backend_versions()#

Return version strings for installed backends.

Only actually imports the framework if it was already detected; avoids triggering slow framework initialisation.

Returns:

versions{'torch': '2.x.x', 'tensorflow': None}None means not installed.

Return type:

dict