scxpand.util.inference_utils

scxpand.util.inference_utils#

Core inference functions for scXpand models.

This module contains only the core inference functionality: - Model loading - Running inference on data - Environment setup

Higher-level orchestration is handled by scxpand.core.prediction. I/O operations are handled by scxpand.util.io. Evaluation logic is handled by scxpand.core.evaluation.

Functions

load_model(model_type, model_path, device)

Load a trained model from disk.

run_model_inference(model_type, model, ...)

Run inference using a trained model.

setup_inference_environment(model_type, ...)

Setup the inference environment by loading data format, model, and determining device.

scxpand.util.inference_utils.load_model(model_type, model_path, device)#

Load a trained model from disk.

Parameters:
  • model_type (ModelType | str) – Type of model to load

  • model_path (Path) – Path to directory containing the trained model

  • device (device | str) – Device to load the model onto (for neural networks)

Return type:

BaseEstimator | Module

Returns:

Loaded model (scikit-learn estimator or PyTorch module)

Raises:

ValueError – If model_type is not supported

scxpand.util.inference_utils.run_model_inference(model_type, model, data_format, adata=None, data_path=None, device=None, batch_size=1024, num_workers=0, eval_row_inds=None)#

Run inference using a trained model.

This is the internal inference function that handles the actual model execution. For the public API, use scxpand.run_inference() instead.

Parameters:
  • model_type (ModelType | str) – Type of model being used

  • model (BaseEstimator | Module) – Trained model instance

  • data_format (DataFormat) – Data format specification for preprocessing

  • adata (AnnData | None (default: None)) – In-memory AnnData object (alternative to data_path)

  • data_path (str | Path | None (default: None)) – Path to data file (alternative to adata)

  • device (device | str | None (default: None)) – Device for computation (neural networks only)

  • batch_size (int (default: 1024)) – Batch size for inference

  • num_workers (int (default: 0)) – Number of workers for data loading

  • eval_row_inds (ndarray | None (default: None)) – Specific cell indices to process (None for all)

Return type:

ndarray

Returns:

Array of prediction probabilities

Raises:

ValueError – If neither adata nor data_path is provided, or if model_type is unsupported

scxpand.util.inference_utils.setup_inference_environment(model_type, model_path)#

Setup the inference environment by loading data format, model, and determining device.

Parameters:
  • model_type (ModelType | str) – Type of model to setup

  • model_path (str | Path) – Path to directory containing trained model and data format

Return type:

tuple[DataFormat, BaseEstimator | Module, device | str]

Returns:

Tuple of (data_format, model, device)

Raises:

FileNotFoundError – If data format file is not found