This repository provides SMHI training and external evaluation code, plus a minimal demo dataset and one-click scripts.
- SMHI module (
smhi/): python package (training + external evaluation) bin/: command-line entrypoints (thin wrappers)scripts/: runnable shell scripts (relative paths)demo_data/: minimal demo CSVs
This code implements a Top-(K) feature selection and 1D convolutional neural network with batch normalization (BN) for supervised classification on microbiome abundance profiles. Model training is performed using stratified cross-validation, producing per-fold models and aggregated predictions. External cohort evaluation aligns features to the training feature space, reports missing features, and optionally applies probability calibration before generating standard performance visualizations and per-sample outputs.
- Input (training):
- Abundance table CSV (rows = samples, columns = microbial features; first column is
seq.id) - Metadata CSV containing
disease.type(first column isseq.id;HCis treated as control, all other labels are treated as case)
- Abundance table CSV (rows = samples, columns = microbial features; first column is
- Input (external evaluation):
- External cohort abundance + metadata in the same format
- Training feature list exported by the training run (
model/profile_filtered.csv)
- Outputs:
- Trained models (per fold), cross-validated predictions, threshold table, and summary plots under
outputs/.../<target>/model/ - External-cohort predictions and reports under
outputs/.../<target>/result/<cohort>/
- Trained models (per fold), cross-validated predictions, threshold table, and summary plots under
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
bash scripts/demo_run.shOutputs will be written to ./outputs/demo_run/.
After running bash scripts/demo_run.sh, the following files are expected (paths relative to repository root):
- Training artifacts (
outputs/demo_run/Top100_BN_demo/model/)parameters.json: exact hyperparameters and filtering configuration used for the runprofile_filtered.csv: filtered and transformed feature matrix used for training (also serves as the external feature list)optimal_thresholds.csv: per-fold operating thresholdsTrain_predictions_mean.csv,Val_predictions_mean.csv: mean cross-validated predicted probabilities (and SMHI) per sampleCNN_best_model_0.pt...CNN_best_model_2.pt: per-fold trained model weights (demo uses 3 folds)train_roc.pdf,val_roc.pdf: ROC curvesTrain_probability_violin.pdf,Val_probability_violin.pdf: probability distribution plotsTrain_SMHI_violin.pdf,Val_SMHI_violin.pdf: SMHI distribution plots
- External evaluation artifacts (
outputs/demo_run/Top100_BN_demo/result/External_demo/)External_demo_missing_features.csv: list of training features absent from the external cohort (filled with zeros)External_demo_feature_matching_report.csv: feature overlap statistics (common/missing/extra)External_demo_External_predictions_mean.csv: per-sample mean external predicted probabilities (and SMHI)External_demo_External_roc.pdf: external ROC curve (if both classes exist)External_demo_External_predictions_mean_calibrated.csv: calibrated external mean predictions (enabled in demo)
The software stack below corresponds to the Conda environment python3.11 on macOS (osx-64) used for validation.
- Python: 3.11.10
- numpy: 2.1.2
- pandas: 2.2.3
- PyTorch (
torch): 2.2.2 - scikit-learn: 1.6.1
- SciPy: 1.14.1
- matplotlib: 3.9.2
- seaborn: 0.13.2
Notes:
- The validated PyTorch build is CPU-only on macOS (osx-64).
- Install time: ~3 minutes in a fresh
.venvusing Python 3.11 on macOS (standard broadband; depends on whether PyTorch and NumPy wheels are cached). - Demo runtime: typically < 5 minutes on a laptop/desktop CPU (the demo uses a small dataset, 3-fold CV, and 10 epochs).
- OS: macOS (osx-64) validated; Linux should be supported with the same Python stack.
- RAM: (\ge) 4 GB is sufficient for the demo dataset.
- GPU: not required for the demo.
- Prepare training input files:
train_profile.csv: abundance table withseq.idas sample identifier (first column), features as columns.train_metadata.csv: metadata withseq.id(first column) and adisease.typecolumn.
- Train the model (example):
python -m smhi.train \
--prof_path /path/to/train_profile.csv \
--meta_path /path/to/train_metadata.csv \
--output_dir /path/to/outputs \
--target MyTop100BN \
--top_species 100 \
--model_type bn_enhanced \
--use_mask- Run external cohort evaluation using the exported training feature list:
python -m smhi.external_eval \
--prof_path /path/to/external_profile.csv \
--meta_path /path/to/external_metadata.csv \
--model_dir /path/to/outputs/MyTop100BN/model \
--feature_list /path/to/outputs/MyTop100BN/model/profile_filtered.csv \
--output_dir /path/to/outputs/MyTop100BN/result/ExternalCohort \
--target ExternalCohort \
--train_results /path/to/outputs/MyTop100BN/model/Val_predictions_all.csv \
--calibration_method percentile_mapping \
--use_maskKey points:
- External features are aligned to the training feature space; missing training features are recorded in
*_missing_features.csvand filled with zeros. - If the training run used
--use_mask, external evaluation should also pass--use_maskto ensure input dimensionality matches.