Skip to main content

ResPredAI

Antimicrobial resistance predictions via artificial intelligence models

PyPI Python Version CI License: MIT DOI

Implementation of the pipeline described in:

Bonazzetti, C., Rocchi, E., Toschi, A. et al. Artificial Intelligence model to predict resistances in Gram-negative bloodstream infections. npj Digit. Med. 8, 319 (2025). https://doi.org/10.1038/s41746-025-01696-x

ResPredAI Logo

Website | Documentation | Installation | Quick Start | Pipeline Overview | CLI Commands | Citation

A reproducible machine learning framework designed to accelerate clinical decision-making by predicting antimicrobial resistance patterns from patient data.

  • Project Website - Overview of the project, original paper, and related work
  • Documentation - Installation guides, CLI reference, tutorials, and API documentation

Installation

Install from PyPI:

pip install respredai

Or install from source:

git clone https://github.com/EttoreRocchi/ResPredAI.git
cd ResPredAI
# For development (includes pytest)
pip install -e ".[dev]"

Optional: TabPFN support

TabPFN is shipped as an optional extra to keep the base install lean:

pip install "respredai[tabpfn]"

TabPFN v3 also requires a PriorLabs API token. Export it before running any pipeline that includes TabPFN in [Pipeline] models:

export TABPFN_TOKEN="your-token-here"

If TabPFN is requested without the extra installed or without the token set, ResPredAI fails at the start of the run.

Testing the Installation

Verify the installation:

respredai --version

Quick Start

1. Create a configuration file

respredai create-config my_config.ini

2. Edit the configuration file

Edit my_config.ini with your data paths and parameters:

[Data]
data_path = ./data/my_data.csv
targets = Target1,Target2
continuous_features = Feature1,Feature2,Feature3

[Metadata]
# group_column = PatientID  # Optional: prevents data leakage in CV
# temporal_column = collection_date  # Date column for temporal validation
# subgroup_columns = ward, sex  # Columns for subgroup performance analysis

[Pipeline]
models = LR,RF,XGB,CatBoost
outer_folds = 5
inner_folds = 3
# Repeated CV: set >1 for more robust estimates
outer_cv_repeats = 1
# Probability calibration: post-hoc calibration on best estimator
calibrate_probabilities = false
probability_calibration_method = sigmoid  # sigmoid or isotonic
probability_calibration_cv = 5
# Threshold optimization
calibrate_threshold = false
threshold_method = auto
# Threshold optimization objective: youden (default), f1, f2, cost_sensitive
threshold_objective = youden
# Cost weights for cost_sensitive objective (VME = false susceptible, ME = false resistant)
vme_cost = 1.0
me_cost = 1.0
# Confidence level for bootstrap CIs (between 0.5 and 1.0, default: 0.95)
confidence_level = 0.95
# Number of bootstrap resamples for CIs (>= 100, default: 1000)
n_bootstrap = 1000
# Number of bins for ECE/MCE calibration metrics (>= 2, default: 10)
calibration_bins = 10

[Uncertainty]
# Miscoverage rate for conformal prediction (default 0.1 = 90% coverage)
alpha = 0.1

[Reproducibility]
seed = 42

[Log]
verbosity = 1
log_basename = respredai.log

[Resources]
n_jobs = -1

[ModelSaving]
enable = true
compression = 3

[Preprocessing]
ohe_min_frequency = 0.05

[Imputation]
method = none
strategy = mean
n_neighbors = 5
estimator = bayesian_ridge

[Output]
out_folder = ./output/

[Validation]
# Validation strategy: cv (default), temporal (prospective-style), or both
validation_strategy = cv
# temporal_split_date = 2023-01-01  # Cutoff date (train < date, test >= date)
# temporal_split_ratio = 0.8  # Alternative: fraction for training (by date order)

Tip: Comment out optional parameters with # to disable them. Empty values (e.g., group_column =) are treated as absent.

3. Run the pipeline

respredai run --config my_config.ini

Pipeline Overview

respredai run - Nested Cross-Validation

flowchart LR
    A[Configuration Loading] --> B[Data Loading & Validation]
    B --> C[OHE Template]
    C --> D{Validation Strategy}

    D -->|cv / both| E["Outer CV Loop (group-aware if configured)"]
    D -->|temporal / both| F[Temporal Split]

    E --> G["OHE (fit on train, transform test)"]
    G --> H["Scaling (fit on train, transform test)"]
    H --> I[Hyperparameter Tuning - Inner CV]
    I --> J[Calibration & Threshold]
    J --> K[Predict on Test Fold]
    K --> L[Metrics + Bootstrap CIs]
    L --> M[Subgroup Analysis]

    F --> T1["OHE (fit on train, transform test)"]
    T1 --> T2["Scaling (fit on train, transform test)"]
    T2 --> T3[Hyperparameter Tuning - Inner CV]
    T3 --> T6[Calibration & Threshold]
    T6 --> T4[Predict on Test Split]
    T4 --> T5[Metrics + Bootstrap CIs]

    M --> R[Generate Reports]
    T5 --> R
    R --> R1[Summary CSVs + HTML Report]
    R --> R2[Confusion Matrices]
    R --> R3[Calibration Curves]
    R --> R4[Feature Importance]

    style A fill:#e8e8e8,stroke:#999,color:#333
    style B fill:#e8e8e8,stroke:#999,color:#333
    style C fill:#b2ebf2,stroke:#0dafb5,color:#333
    style D fill:#ffe0b2,stroke:#f5a623,color:#333
    style E fill:#b2ebf2,stroke:#0dafb5,color:#333
    style F fill:#b2ebf2,stroke:#0dafb5,color:#333
    style G fill:#b2ebf2,stroke:#0dafb5,color:#333
    style H fill:#b2ebf2,stroke:#0dafb5,color:#333
    style I fill:#b2ebf2,stroke:#0dafb5,color:#333
    style J fill:#fff3e0,stroke:#f5a623,color:#333
    style K fill:#b2ebf2,stroke:#0dafb5,color:#333
    style L fill:#b2ebf2,stroke:#0dafb5,color:#333
    style M fill:#fff3e0,stroke:#f5a623,color:#333
    style T1 fill:#b2ebf2,stroke:#0dafb5,color:#333
    style T2 fill:#b2ebf2,stroke:#0dafb5,color:#333
    style T3 fill:#b2ebf2,stroke:#0dafb5,color:#333
    style T6 fill:#fff3e0,stroke:#f5a623,color:#333
    style T4 fill:#b2ebf2,stroke:#0dafb5,color:#333
    style T5 fill:#b2ebf2,stroke:#0dafb5,color:#333
    style R fill:#0dafb5,stroke:#098a8f,color:#fff
    style R1 fill:#0dafb5,stroke:#098a8f,color:#fff
    style R2 fill:#0dafb5,stroke:#098a8f,color:#fff
    style R3 fill:#0dafb5,stroke:#098a8f,color:#fff
    style R4 fill:#fff3e0,stroke:#f5a623,color:#333

respredai train - Train for Deployment

flowchart LR
    A[Configuration Loading] --> B[Data Loading & Validation]
    B --> C[OHE on Full Data]
    C --> E[Feature Scaling]
    E --> F[Hyperparameter Tuning - Inner CV]
    F --> G[Calibration & Threshold]
    G --> H[Save Model Bundle]
    H --> I["Output: model + transformer + OHE + threshold + metadata"]

    style A fill:#e8e8e8,stroke:#999,color:#333
    style B fill:#e8e8e8,stroke:#999,color:#333
    style C fill:#b2ebf2,stroke:#0dafb5,color:#333
    style E fill:#b2ebf2,stroke:#0dafb5,color:#333
    style F fill:#b2ebf2,stroke:#0dafb5,color:#333
    style G fill:#fff3e0,stroke:#f5a623,color:#333
    style H fill:#0dafb5,stroke:#098a8f,color:#fff
    style I fill:#0dafb5,stroke:#098a8f,color:#fff

respredai evaluate - Cross-Dataset Evaluation

flowchart LR
    A[Load Training Metadata] --> B[Load New Data + Validate Features]
    B --> D["OHE (fitted on training data, transform new data)"]
    D --> E["Scaling (fitted on training data, transform new data)"]
    E --> F[Predict with Saved Threshold]
    F --> G[Metrics vs Ground Truth]
    G --> H[Uncertainty Scores]
    H --> I[Predictions + Metrics CSVs]
    I --> J[Evaluation Summary]

    style A fill:#e8e8e8,stroke:#999,color:#333
    style B fill:#e8e8e8,stroke:#999,color:#333
    style D fill:#b2ebf2,stroke:#0dafb5,color:#333
    style E fill:#b2ebf2,stroke:#0dafb5,color:#333
    style F fill:#b2ebf2,stroke:#0dafb5,color:#333
    style G fill:#b2ebf2,stroke:#0dafb5,color:#333
    style H fill:#fff3e0,stroke:#f5a623,color:#333
    style I fill:#0dafb5,stroke:#098a8f,color:#fff
    style J fill:#0dafb5,stroke:#098a8f,color:#fff

Amber nodes indicate optional steps controlled by configuration parameters. All pipelines are executed for each model × target combination.

CLI Commands

Run the pipeline

respredai run --config path/to/config.ini [--quiet]

Train models using nested cross-validation with the specified configuration.

📖 Detailed Documentation - Complete guide with all configuration options and workflow details.

Train models for cross-dataset validation

respredai train --config path/to/config.ini [--models LR,RF] [--output ./trained/]

Train models on the entire dataset using GridSearchCV for hyperparameter tuning. Saves one model file per model-target combination for later use with evaluate.

📖 Detailed Documentation - Complete guide with output structure and workflow.

Evaluate on new data

respredai evaluate --models-dir ./output/trained_models --data new_data.csv --output ./eval/

Apply trained models to new data with ground truth. Outputs predictions and metrics.

📖 Detailed Documentation - Complete guide with data requirements and output format.

Extract feature importance

respredai feature-importance --output <output_folder> --model <model_name> --target <target_name> [--top-n 20]

Extract and visualize feature importance/coefficients from trained models across all outer cross-validation iterations. Uses SHAP as fallback for models without native feature importance.

📖 Detailed Documentation - Complete guide with interpretation, examples, and statistical considerations.

List available models

respredai list-models

Display all available machine learning models with descriptions.

Available Models:
┌────────────┬──────────────────────────┐
│ Code       │ Name                     │
├────────────┼──────────────────────────┤
│ LR         │ Logistic Regression      │
│ MLP        │ Multi-Layer Perceptron   │
│ XGB        │ XGBoost                  │
│ RF         │ Random Forest            │
│ CatBoost   │ CatBoost                 │
│ TabPFN     │ TabPFN                   │
│ RBF_SVC    │ RBF SVM                  │
│ Linear_SVC │ Linear SVM               │
│ KNN        │ K-Nearest Neighbors      │
└────────────┴──────────────────────────┘

Create a template configuration file

respredai create-config output_path.ini

Generate a template configuration file that you can edit for your data.

📖 Detailed Documentation - Complete guide to configuration file structure and customization.

Validate a configuration file

respredai validate-config <path_to_config.ini> [--check-data]

Validate a configuration file without running the pipeline. It can also check that the dataset load without errors.

📖 Detailed Documentation - Complete guide to configuration file validation.

Show information

respredai info

Display information about ResPredAI including scientific paper citation and version details.

Or just:

respredai --version

to show the installed version of ResPredAI.

Output

The pipeline generates:

  • Confusion matrices: PNG files with heatmaps showing model performance for each target
  • Detailed metrics tables: CSV files with comprehensive metrics (precision, recall, F1, MCC, balanced accuracy, AUROC, VME, ME, Brier Score, ECE, MCE) with mean, std, and 95% CI
  • Calibration diagnostics: Reliability curves (calibration plots) per fold and aggregate
  • Trained models: Saved models for resumption and feature importance extraction (if model saving enabled)
  • Feature importance: Plots and CSV files showing feature importance/coefficients (generated separately)
  • Log files: Detailed execution logs (if verbosity > 0)

Output Structure

output_folder/
├── models/                                         # Trained models (if model saving enabled)
│   └── {Model}_{Target}_models.joblib
├── trained_models/                                 # Models for cross-dataset validation (from train command)
│   ├── {Model}_{Target}.joblib
│   └── training_metadata.json
├── metrics/                                        # Detailed performance metrics
│   ├── {target_name}/
│   │   ├── {model_name}_metrics_detailed.csv      # Includes Brier Score, ECE, MCE
│   │   └── summary.csv                            # Summary across all models
│   └── summary_all.csv                            # Global summary
├── calibration/                                    # Calibration diagnostics
│   └── reliability_curve_{model}_{target}.png     # Reliability curves per fold + aggregate
├── feature_importance/                             # Feature importance (if extracted)
│   └── {target_name}/
│       ├── {model_name}_feature_importance.csv    # Importance values (mean/std across folds)
│       ├── {model_name}_feature_importance_per_fold.csv  # Per-fold values (+ intercept for linear models)
│       └── {model_name}_feature_importance.png    # Barplot visualization
├── subgroup_analysis/                               # Subgroup performance metrics (if configured)
│   └── {target_name}/
│       └── {model_name}_{subgroup_col}_subgroup.csv
├── confusion_matrices/                             # Confusion matrix heatmaps
│   └── Confusion_matrix_{model_name}_{target_name}.png
├── report.html                                     # Comprehensive HTML report (includes calibration section)
└── respredai.log                                   # Execution log (if verbosity > 0)

Notes

Changelog

See the full history of changes in the CHANGELOG.md file.

Citation

If you use ResPredAI in your research, please cite:

@article{Bonazzetti2025,
  author = {Bonazzetti, Cecilia and Rocchi, Ettore and Toschi, Alice and Derus, Nicolas Riccardo and Sala, Claudia and Pascale, Renato and Rinaldi, Matteo and Campoli, Caterina and Pasquini, Zeno Adrien Igor and Tazza, Beatrice and Amicucci, Armando and Gatti, Milo and Ambretti, Simone and Viale, Pierluigi and Castellani, Gastone and Giannella, Maddalena},
  title = {Artificial Intelligence model to predict resistances in Gram-negative bloodstream infections},
  journal = {npj Digital Medicine},
  volume = {8},
  pages = {319},
  year = {2025},
  doi = {10.1038/s41746-025-01696-x},
  url = {https://doi.org/10.1038/s41746-025-01696-x}
}

Funding

This research was supported by EU funding within the NextGenerationEU-MUR PNRR Extended Partnership initiative on Emerging Infectious Diseases (Project no. PE00000007, INF-ACT).

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines on setting up a development environment, running tests, and submitting pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for ResPredAI 1.9.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ResPredAI 1.9.4
File Size Uploaded
respredai-1.9.4.tar.gz 79.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ResPredAI 1.9.4
File Interpreter ABI Platform
respredai-1.9.4-py3-none-any.whl Python 3 none any Details

Total release size: 165.4 kB

Release files / respredai-1.9.4.tar.gz

Download URL respredai-1.9.4.tar.gz
Size 79.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6441950b2dbdfa3ee0d8c9bc3d01b72a0c8ba1381ba7e9a57fd0e7045dae69fb
BLAKE2b-256 checksum
How to use checksums
5d731a30979032f126aed0ee8418c9581451f28225da43e61b979805c4867ab9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / respredai-1.9.4-py3-none-any.whl

Download URL respredai-1.9.4-py3-none-any.whl
Size 85.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
401ea283178424b486335054a1fc2a2fc4c8818ecb0bef7c8dba350661ef2de6
BLAKE2b-256 checksum
How to use checksums
10c1ad5c685f632c4a0e6ffdc4fd0d2a1184ef14ebb78b770de3da00849173c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

1.9.4 This release

2 release files

1.9.3

2 release files

1.9.2

2 release files

1.9.1

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.2

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page