Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- app.pyAdded+212
- brain_gcn/__init__.pyAdded
- brain_gcn/__pycache__/__init__.cpython-311.pycAdded
- brain_gcn/__pycache__/experiments.cpython-311.pycAdded
- brain_gcn/__pycache__/finetune_main.cpython-311.pycAdded
- brain_gcn/__pycache__/main.cpython-311.pycAdded
- brain_gcn/__pycache__/population_main.cpython-311.pycAdded
- brain_gcn/__pycache__/pretrain_main.cpython-311.pycAdded
- brain_gcn/ablation.pyAdded+259
- brain_gcn/cv_cli.pyAdded+74
- brain_gcn/eval_cli.pyAdded+229
- brain_gcn/experiments.pyAdded+152
- brain_gcn/finetune_main.pyAdded+429
- brain_gcn/hpo.pyAdded+285
- brain_gcn/main.pyAdded+322
- brain_gcn/models/__init__.pyAdded+32
- brain_gcn/models/__pycache__/__init__.cpython-311.pycAdded
- brain_gcn/models/__pycache__/advanced_models.cpython-311.pycAdded
- brain_gcn/models/__pycache__/brain_gcn.cpython-311.pycAdded
- brain_gcn/models/__pycache__/dynamic_fc.cpython-311.pycAdded
- brain_gcn/models/__pycache__/mae.cpython-311.pycAdded
- brain_gcn/models/__pycache__/population_gcn.cpython-311.pycAdded
- brain_gcn/models/__pycache__/registry.cpython-311.pycAdded
- brain_gcn/models/advanced_models.pyAdded+346
- brain_gcn/models/brain_gcn.pyAdded+724
- brain_gcn/models/dynamic_fc.pyAdded+100
- brain_gcn/models/mae.pyAdded+297
- brain_gcn/models/population_gcn.pyAdded+70
- brain_gcn/models/registry.pyAdded+313
- brain_gcn/population_main.pyAdded+288
- brain_gcn/pretrain_main.pyAdded+263
- brain_gcn/tasks/__init__.pyAdded+3
- brain_gcn/tasks/__pycache__/__init__.cpython-311.pycAdded
- brain_gcn/tasks/__pycache__/classification.cpython-311.pycAdded
- brain_gcn/tasks/classification.pyAdded+244
- brain_gcn/utils/__init__.pyAdded
- brain_gcn/utils/__pycache__/__init__.cpython-311.pycAdded
- brain_gcn/utils/__pycache__/graph_conv.cpython-311.pycAdded
- brain_gcn/utils/__pycache__/grl.cpython-311.pycAdded
- brain_gcn/utils/cross_validation.pyAdded+243
- brain_gcn/utils/data/__init__.pyAdded+1
- brain_gcn/utils/data/__pycache__/__init__.cpython-311.pycAdded
- brain_gcn/utils/data/__pycache__/datamodule.cpython-311.pycAdded
- brain_gcn/utils/data/__pycache__/dataset.cpython-311.pycAdded
- brain_gcn/utils/data/__pycache__/download.cpython-311.pycAdded
- brain_gcn/utils/data/__pycache__/functional_connectivity.cpython-311.pycAdded
- brain_gcn/utils/data/__pycache__/population_graph.cpython-311.pycAdded
- brain_gcn/utils/data/__pycache__/preprocess.cpython-311.pycAdded
- brain_gcn/utils/data/datamodule.pyAdded+521
- brain_gcn/utils/data/dataset.pyAdded+252
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212"""BrainConnect-ASD — Scanner-site-invariant ASD detection from fMRI.
Ensemble of 4 adversarial GCNs trained with leave-one-site-out CV on ABIDE I.Each model held out a different scanner site (NYU / USM / UCLA / UM).LOSO mean AUC = 0.7872 across 529 unseen subjects from 4 institutions.
Fine-tuned Qwen2.5-7B-Instruct clinical report generation runs on AMD MI300X."""from __future__ import annotations
import sysfrom pathlib import Path
import numpy as npimport torchimport gradio as gr
# ── preprocessing constants ────────────────────────────────────────────────_WINDOW_LEN = 50_STEP = 3_MAX_WINDOWS = 30_FC_THRESHOLD = 0.2
_CKPTS = { "NYU": Path("checkpoints/nyu.ckpt"), "USM": Path("checkpoints/usm.ckpt"), "UCLA": Path("checkpoints/ucla.ckpt"), "UM": Path("checkpoints/um.ckpt"),}
# ── preprocessing ──────────────────────────────────────────────────────────
def _zscore(bold): mean = bold.mean(0, keepdims=True) std = bold.std(0, keepdims=True) std[std < 1e-8] = 1.0 return ((bold - mean) / std).astype(np.float32)
def _fc(bold): fc = np.corrcoef(bold.T).astype(np.float32) np.nan_to_num(fc, copy=False) return fc
def _windows(bold): T, N = bold.shape starts = list(range(0, T - _WINDOW_LEN + 1, _STEP)) w = np.stack([bold[s:s+_WINDOW_LEN].std(0) for s in starts]).astype(np.float32) if len(w) >= _MAX_WINDOWS: return w[:_MAX_WINDOWS] return np.concatenate([w, np.repeat(w[-1:], _MAX_WINDOWS - len(w), 0)])
def preprocess(bold): bold = _zscore(bold) fc = _fc(bold) fc = np.arctanh(np.clip(fc, -0.9999, 0.9999)) adj = np.where(np.abs(fc) >= _FC_THRESHOLD, fc, 0.0).astype(np.float32) bw = _windows(bold) return torch.FloatTensor(bw).unsqueeze(0), torch.FloatTensor(adj).unsqueeze(0)
# ── model loading (cached) ─────────────────────────────────────────────────
_models: list | None = None
def get_models(): global _models if _models is not None: return _models from brain_gcn.tasks import ClassificationTask _models = [] for site, ckpt in _CKPTS.items(): if not ckpt.exists(): continue task = ClassificationTask.load_from_checkpoint(str(ckpt), map_location="cpu", strict=False) task.eval() _models.append((site, task)) return _models
# ── inference ──────────────────────────────────────────────────────────────
@torch.no_grad()def run_gcn(file_path: str | None) -> tuple[str, str]: if file_path is None: return "Upload a file to begin.", ""
path = Path(file_path) try: if path.suffix == ".npz": d = np.load(path, allow_pickle=True) fc = d["mean_fc"].astype(np.float32) fc = np.arctanh(np.clip(fc, -0.9999, 0.9999)) adj = np.where(np.abs(fc) >= _FC_THRESHOLD, fc, 0.0).astype(np.float32) bw = d["bold_windows"].astype(np.float32) if len(bw) >= _MAX_WINDOWS: bw = bw[:_MAX_WINDOWS] else: bw = np.concatenate([bw, np.repeat(bw[-1:], _MAX_WINDOWS - len(bw), 0)]) bw_t = torch.FloatTensor(bw).unsqueeze(0) adj_t = torch.FloatTensor(adj).unsqueeze(0) else: bold = np.loadtxt(path, dtype=np.float32) if bold.ndim != 2 or bold.shape[1] != 200: return f"Error: expected (T×200) array, got {bold.shape}", "" bw_t, adj_t = preprocess(bold) except Exception as e: return f"Error loading file: {e}", ""
models = get_models() per_model = [] for site, task in models: logits = task(bw_t, adj_t) p = torch.softmax(logits, -1)[0, 1].item() per_model.append((site, p))
p_mean = float(np.mean([p for _, p in per_model])) label = "ASD" if p_mean > 0.5 else "Typical Control" conf = max(p_mean, 1 - p_mean) * 100 consensus = sum(1 for _, p in per_model if p > 0.5)
gcn_out = f"Prediction : {label}\n" gcn_out += f"Confidence : {conf:.1f}% (p_ASD = {p_mean:.3f})\n" gcn_out += f"Consensus : {consensus}/4 site models\n\n" gcn_out += "Per-model breakdown:\n" for site, p in per_model: bar = "█" * int(p * 20) + "░" * (20 - int(p * 20)) lbl = "ASD" if p > 0.5 else "TC " gcn_out += f" {site:>4} {lbl} {bar} {p:.3f}\n"
# Clinical interpretation stub — replaced by fine-tuned Qwen2.5-7B on AMD MI300X asd_features = [ "Reduced DMN coherence (mPFC ↔ PCC)", "Atypical salience network lateralization", "Decreased long-range frontotemporal connectivity", "Hypoconnectivity in social brain circuit (TPJ, STS)", "Atypical cerebellar–cortical coupling", ] tc_features = [ "DMN coherence within normal range", "Intact salience network organization", "Normal long-range cortico-cortical connectivity", "Typical social brain circuit integrity", "Cerebellar–cortical coupling within expected range", ]
report = f"## Clinical Connectivity Summary\n\n" report += f"**Overall**: {label} ({conf:.1f}% confidence, {consensus}/4 site consensus)\n\n" if p_mean > 0.6: report += "**Key Findings**:\n" for f in asd_features[:3]: report += f"- {f}\n" report += "\n**Cross-Site Consistency**: ASD-consistent patterns detected across " report += f"{consensus}/4 independent scanner sites, indicating findings are not " report += "attributable to acquisition-site artifacts.\n\n" elif p_mean < 0.4: report += "**Key Findings**:\n" for f in tc_features[:3]: report += f"- {f}\n" report += "\n**Cross-Site Consistency**: Typical connectivity profile confirmed " report += f"by {4 - consensus}/4 independent site models.\n\n" else: report += "**Indeterminate**: Mixed connectivity profile near ASD–TC boundary. " report += "Heightened clinical scrutiny recommended.\n\n"
report += "*This report is AI-assisted and does not constitute a diagnosis. " report += "Full clinical assessment required.*\n\n" report += "---\n*Clinical report generation powered by Qwen2.5-7B fine-tuned on AMD MI300X (coming soon)*"
return gcn_out, report
# ── Gradio UI ──────────────────────────────────────────────────────────────
with gr.Blocks(title="BrainConnect-ASD") as demo: gr.Markdown("""# BrainConnect-ASD### Scanner-site-invariant ASD detection from resting-state fMRI
Ensemble of **4 adversarial GCNs** trained with leave-one-site-out cross-validation on ABIDE I.Each model was held out from a different scanner site — the ensemble generalizes to **unseen institutions**.
**LOSO AUC = 0.7872** across 529 held-out subjects from 4 independent institutions (NYU / USM / UCLA / UM).
Fine-tuned **Qwen2.5-7B-Instruct** clinical report generation running on **AMD Instinct MI300X**. """)
with gr.Row(): file_input = gr.File( label="Upload CC200 fMRI file (.1D or .npz)", file_types=[".1D", ".npz"], type="filepath", )
with gr.Row(): gcn_out = gr.Textbox(label="GCN Prediction", lines=10, show_copy_button=True) report_out = gr.Textbox(label="Clinical Report", lines=20, show_copy_button=True)
file_input.change(fn=run_gcn, inputs=file_input, outputs=[gcn_out, report_out])
gr.Markdown("""---**Model**: Adversarial Brain-Mode GCN (k=16 modes) with gradient reversal site deconfounding**Dataset**: ABIDE I (1,102 subjects, 17 acquisition sites)**Validation**: Leave-one-site-out across NYU (n=184), USM (n=101), UCLA (n=99), UM (n=145)**Hardware**: AMD Instinct MI300X via AMD Developer Cloud**Code**: [GitHub](https://github.com/Yatsuiii/Brain-Connectivity-GCN) """)
if __name__ == "__main__": demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259"""Ablation study framework.
Systematically removes or disables components to measure their contribution.
Examples: - Disable DropEdge (set drop_edge_p=0) - Disable BOLD augmentation (set bold_noise_std=0) - Use GCN baseline vs full graph-temporal - Population adj vs per-subject adjacency"""
from __future__ import annotations
import argparseimport jsonimport loggingfrom copy import deepcopyfrom dataclasses import dataclassfrom pathlib import Pathfrom typing import Callable
import pytorch_lightning as plimport torch
from brain_gcn.main import train_from_args, validate_args
log = logging.getLogger(__name__)
@dataclassclass AblationComponent: """Single component to ablate."""
name: str description: str modify_fn: Callable[[argparse.Namespace], argparse.Namespace] enabled: bool = True
class AblationStudy: """Framework for systematic ablation studies."""
# Predefined components COMPONENTS = { "drop_edge": AblationComponent( name="drop_edge", description="DropEdge regularization in graph convolution", modify_fn=lambda args: (setattr(args, "drop_edge_p", 0.0), args)[1], ), "bold_noise": AblationComponent( name="bold_noise", description="BOLD signal augmentation during training", modify_fn=lambda args: (setattr(args, "bold_noise_std", 0.0), args)[1], ), "graph": AblationComponent( name="graph", description="Graph structure (use GRU-only baseline)", modify_fn=lambda args: (setattr(args, "model_name", "gru"), args)[1], ), "population_adj": AblationComponent( name="population_adj", description="Population adjacency matrix", modify_fn=lambda args: (setattr(args, "use_population_adj", False), args)[1], ), "layer_norm": AblationComponent( name="layer_norm", description="Layer normalization in graph convolutions", modify_fn=lambda args: (setattr(args, "use_layer_norm", False), args)[1], ), }
def __init__( self, base_args: argparse.Namespace, components: list[str] | None = None, output_dir: str | Path | None = None, ): """Initialize ablation study.
Parameters ---------- base_args : argparse.Namespace Base training arguments (full model). components : list[str], optional List of component names to ablate. If None, ablates all. output_dir : str or Path, optional Directory to save results. """ self.base_args = deepcopy(base_args) self.output_dir = Path(output_dir) if output_dir else Path("ablations") self.output_dir.mkdir(parents=True, exist_ok=True)
if components is None: self.component_names = list(self.COMPONENTS.keys()) else: self.component_names = components
self.components = [ self.COMPONENTS[name] for name in self.component_names if name in self.COMPONENTS ]
self.results: dict[str, dict] = {}
def run(self) -> dict[str, dict]: """Run full ablation study.
Returns ------- dict[str, dict] Results keyed by component name. """ # Train full model first log.info("Training full model (baseline)") pl.seed_everything(self.base_args.seed, workers=True) try: trainer, _, _ = train_from_args(self.base_args) baseline_metrics = { key: value.item() if isinstance(value, torch.Tensor) else value for key, value in trainer.callback_metrics.items() if key.startswith(("test_",)) } except Exception as e: log.error(f"Baseline training failed: {e}") baseline_metrics = {}
self.results["baseline"] = baseline_metrics
# Ablate each component for component in self.components: log.info(f"Ablating: {component.name} ({component.description})")
ablated_args = deepcopy(self.base_args) ablated_args = component.modify_fn(ablated_args)
try: validate_args(ablated_args) except ValueError as e: log.warning(f"Ablation {component.name} skipped: {e}") continue
pl.seed_everything(self.base_args.seed, workers=True) try: trainer, _, _ = train_from_args(ablated_args) ablated_metrics = { key: value.item() if isinstance(value, torch.Tensor) else value for key, value in trainer.callback_metrics.items() if key.startswith(("test_",)) } except Exception as e: log.error(f"Ablation {component.name} failed: {e}") ablated_metrics = {}
self.results[component.name] = ablated_metrics
# Compute deltas self._compute_deltas(baseline_metrics)
return self.results
def _compute_deltas(self, baseline: dict) -> None: """Compute metric changes from baseline.""" deltas = {}
for component_name, ablated_metrics in self.results.items(): if component_name == "baseline": deltas[component_name] = {} continue
delta = {} for key, ablated_val in ablated_metrics.items(): baseline_val = baseline.get(key, None) if baseline_val is not None and isinstance(ablated_val, (int, float)): delta[key] = ablated_val - baseline_val else: delta[key] = None
deltas[component_name] = delta
self.deltas = deltas
def save_results(self) -> None: """Save results to JSON.""" results_file = self.output_dir / "ablation_results.json"
# Convert torch tensors to serializable format serializable = {} for key, metrics in self.results.items(): serializable[key] = { k: float(v) if isinstance(v, (int, float)) else str(v) for k, v in metrics.items() }
deltas_serializable = {} for key, deltas in self.deltas.items(): deltas_serializable[key] = { k: float(v) if v is None or isinstance(v, (int, float)) else str(v) for k, v in deltas.items() }
output = { "results": serializable, "deltas": deltas_serializable, "components": [c.name for c in self.components], }
with open(results_file, "w") as f: json.dump(output, f, indent=2)
log.info(f"Ablation results saved to {results_file}")
def summary(self) -> str: """Pretty-print summary.""" lines = ["=" * 70] lines.append("ABLATION STUDY SUMMARY") lines.append("=" * 70)
# Baseline if "baseline" in self.results: lines.append("\nBaseline (Full Model):") for key, val in sorted(self.results["baseline"].items()): if isinstance(val, float): lines.append(f" {key}: {val:.4f}") else: lines.append(f" {key}: {val}")
# Ablations lines.append("\nAblation Impact (Δ from Baseline):") lines.append("-" * 70)
for component_name in self.component_names: if component_name in self.deltas: delta = self.deltas[component_name] lines.append(f"\n{component_name}:") for key, val in sorted(delta.items()): if isinstance(val, float): sign = "+" if val >= 0 else "-" lines.append(f" {key}: {sign}{abs(val):.4f}")
lines.append("\n" + "=" * 70) return "\n".join(lines)
def add_ablation_arguments(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: """Add ablation-specific arguments.""" parser.add_argument( "--ablation_components", nargs="+", choices=list(AblationStudy.COMPONENTS.keys()), help="Components to ablate. If not specified, ablates all.", ) parser.add_argument( "--ablation_output_dir", type=str, default="results/ablations", help="Output directory for ablation results.", ) return parser1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374"""K-fold cross-validation entry point.
Usage: python -m brain_gcn.cv_cli --n_splits 5 --cv_output_dir results/cv"""
from __future__ import annotations
import argparseimport jsonimport loggingfrom pathlib import Path
from brain_gcn.main import build_parserfrom brain_gcn.utils.cross_validation import kfold_cross_validate
logging.basicConfig(level=logging.INFO)log = logging.getLogger(__name__)
def add_cv_arguments(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: """Add CV-specific arguments.""" parser.add_argument( "--cv_n_splits", type=int, default=5, help="Number of CV folds.", ) parser.add_argument( "--cv_output_dir", type=str, default="results/cv", help="Output directory for CV results.", ) return parser
def main(): parser = build_parser() parser = add_cv_arguments(parser) args = parser.parse_args()
log.info(f"Starting {args.cv_n_splits}-fold cross-validation") log.info(f"Model: {args.model_name}") log.info(f"Output: {args.cv_output_dir}")
# Run cross-validation cv_results = kfold_cross_validate( args, n_splits=args.cv_n_splits, output_dir=args.cv_output_dir, )
# Print summary log.info("\n" + "=" * 70) log.info("CROSS-VALIDATION COMPLETE") log.info("=" * 70)
summary = cv_results.mean_metrics() for key, value in sorted(summary.items()): if isinstance(value, float): log.info(f"{key}: {value:.4f}")
# Save summary summary_file = Path(args.cv_output_dir) / "cv_summary.json" with open(summary_file, "w") as f: json.dump(cv_results.to_dict(), f, indent=2)
log.info(f"\nResults saved to {summary_file}")
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229"""Evaluation entry point for extended metrics analysis.
Computes extended evaluation metrics, ROC curves, and statistical tests.
Usage: python -m brain_gcn.eval_cli --checkpoint <path> --test_metrics"""
from __future__ import annotations
import argparseimport jsonimport loggingfrom pathlib import Path
import matplotlib.pyplot as pltimport numpy as npimport torchfrom sklearn.metrics import auc
from brain_gcn.main import build_datamodulefrom brain_gcn.tasks import ClassificationTaskfrom brain_gcn.utils.evaluation import ( compute_metrics, compute_roc_curve, compute_pr_curve, compute_confusion_matrix, StatisticalTester,)
logging.basicConfig(level=logging.INFO)log = logging.getLogger(__name__)
def add_eval_arguments(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: """Add evaluation-specific arguments.""" parser.add_argument( "--eval_checkpoint", type=str, required=True, help="Path to model checkpoint.", ) parser.add_argument( "--eval_output_dir", type=str, default="results/evaluation", help="Output directory for evaluation results.", ) parser.add_argument( "--eval_plot_roc", action="store_true", help="Save ROC curve plot.", ) parser.add_argument( "--eval_plot_pr", action="store_true", help="Save Precision-Recall curve plot.", ) parser.add_argument( "--eval_bootstrap_ci", action="store_true", help="Compute bootstrap confidence intervals.", ) parser.add_argument( "--eval_ci_n_bootstrap", type=int, default=1000, help="Number of bootstrap samples.", ) return parser
def load_checkpoint( ckpt_path: str | Path, device: str = "cpu",) -> ClassificationTask: """Load trained model from checkpoint.""" return ClassificationTask.load_from_checkpoint(ckpt_path, map_location=device)
def get_predictions( model: ClassificationTask, dm, device: str = "cpu",) -> tuple[np.ndarray, np.ndarray]: """Get predictions on test set.""" model.eval() model.to(device)
all_probs = [] all_labels = []
with torch.no_grad(): for bold_windows, adj, labels in dm.test_dataloader(): logits = model(bold_windows.to(device), adj.to(device)) probs = torch.softmax(logits, dim=-1)[:, 1] all_probs.append(probs.cpu().numpy()) all_labels.append(labels.numpy())
return np.concatenate(all_probs), np.concatenate(all_labels)
def plot_roc( probs: np.ndarray, labels: np.ndarray, output_path: str | Path,) -> None: """Plot and save ROC curve.""" roc_data = compute_roc_curve(probs, labels) fpr = roc_data["fpr"] tpr = roc_data["tpr"] auc_score = roc_data["auc"]
plt.figure(figsize=(8, 6)) plt.plot(fpr, tpr, label=f"ROC (AUC={auc_score:.4f})", linewidth=2) plt.plot([0, 1], [0, 1], "k--", label="Random", linewidth=1) plt.xlabel("False Positive Rate") plt.ylabel("True Positive Rate") plt.title("ROC Curve") plt.legend() plt.grid(alpha=0.3) plt.tight_layout() plt.savefig(output_path, dpi=150) plt.close()
log.info(f"ROC curve saved to {output_path}")
def plot_pr( probs: np.ndarray, labels: np.ndarray, output_path: str | Path,) -> None: """Plot and save Precision-Recall curve.""" pr_data = compute_pr_curve(probs, labels) precision = pr_data["precision"] recall = pr_data["recall"] ap = pr_data["ap"]
plt.figure(figsize=(8, 6)) plt.plot(recall, precision, label=f"PR (AP={ap:.4f})", linewidth=2) plt.xlabel("Recall") plt.ylabel("Precision") plt.title("Precision-Recall Curve") plt.legend() plt.grid(alpha=0.3) plt.tight_layout() plt.savefig(output_path, dpi=150) plt.close()
log.info(f"PR curve saved to {output_path}")
def main(): from brain_gcn.main import build_parser
parser = build_parser() parser = add_eval_arguments(parser) args = parser.parse_args()
output_dir = Path(args.eval_output_dir) output_dir.mkdir(parents=True, exist_ok=True)
# Load model and data log.info(f"Loading checkpoint: {args.eval_checkpoint}") device = "cuda" if torch.cuda.is_available() else "cpu" model = load_checkpoint(args.eval_checkpoint, device=device)
log.info("Building datamodule") dm = build_datamodule(args) dm.prepare_data() dm.setup()
# Get predictions log.info("Generating predictions on test set") probs, labels = get_predictions(model, dm, device=device)
# Compute metrics log.info("Computing metrics") metrics = compute_metrics(probs, labels) cm = compute_confusion_matrix(probs, labels)
# Print metrics log.info("\n" + "=" * 70) log.info("CLASSIFICATION METRICS") log.info("=" * 70) for key, value in metrics.to_dict().items(): log.info(f"{key:20s}: {value:.4f}")
log.info("\nConfusion Matrix:") log.info(f" TP={cm.true_positives}, FP={cm.false_positives}") log.info(f" FN={cm.false_negatives}, TN={cm.true_negatives}")
# Compute confidence intervals if requested if args.eval_bootstrap_ci: log.info(f"\nComputing {args.eval_ci_n_bootstrap} bootstrap samples") ci_auc = StatisticalTester.bootstrap_ci( lambda p, l: compute_metrics(p, l).auc, probs, labels, n_bootstrap=args.eval_ci_n_bootstrap, ) log.info(f"AUC 95% CI: [{ci_auc[0]:.4f}, {ci_auc[2]:.4f}]")
# Save results results = { "metrics": metrics.to_dict(), "confusion_matrix": cm.to_dict(), }
results_file = output_dir / "metrics.json" with open(results_file, "w") as f: json.dump(results, f, indent=2)
log.info(f"\nResults saved to {results_file}")
# Plot ROC and PR curves if requested if args.eval_plot_roc: roc_path = output_dir / "roc_curve.png" plot_roc(probs, labels, roc_path)
if args.eval_plot_pr: pr_path = output_dir / "pr_curve.png" plot_pr(probs, labels, pr_path)
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152"""Multi-model comparison runner.
v2 changes: - Captures test_sens, test_spec, and ensemble metrics in results CSV - Passes dynamic_graph_temporal flag through correctly - Uses site_holdout as default (inherited from updated main.py defaults)"""
from __future__ import annotations
import argparseimport csvimport loggingfrom copy import deepcopyfrom pathlib import Path
import torch
from brain_gcn.main import build_parser, train_from_args, validate_args
log = logging.getLogger(__name__)
DEFAULT_MODELS = ("fc_mlp", "gcn", "graph_temporal")
def metric_value(value) -> float | int | str: if isinstance(value, torch.Tensor): if value.numel() == 1: return float(value.detach().cpu()) # Multi-element tensor: flatten to scalar_mean or scalar_max scalar_mean = float(value.detach().cpu().mean()) log.warning( f"Multi-element metric tensor with shape {value.shape} — " f"flattening to scalar_mean={scalar_mean:.4f}. " "Consider reducing to single-value metrics in training_step." ) return scalar_mean if isinstance(value, (float, int, str)): return value return str(value)
def build_experiment_parser() -> argparse.ArgumentParser: parser = build_parser() parser.description = "Run Brain-Connectivity-GCN model comparisons" parser.add_argument( "--models", nargs="+", choices=["fc_mlp", "gru", "gcn", "graph_temporal", "brain_mode"], default=list(DEFAULT_MODELS), help="Model modes to run in order.", ) parser.add_argument( "--results_csv", type=str, default="results/experiment_summary.csv", ) parser.add_argument( "--dynamic_graph_temporal", action="store_true", help="Run graph_temporal with per-window adjacency sequences.", ) parser.set_defaults(test=True) return parser
def args_for_model(base_args: argparse.Namespace, model_name: str) -> argparse.Namespace: args = deepcopy(base_args) args.model_name = model_name args.prepare_data = False
if model_name in ("fc_mlp", "adv_fc_mlp", "brain_mode", "adv_brain_mode"): # These use per-subject FC as flat features — no population/dynamic adj args.use_population_adj = False args.use_dynamic_adj_sequence = False args.use_dynamic_adj = False args.use_fc_degree_features = False elif model_name == "graph_temporal": # Always use per-window FC as dynamic adjacency — population adj is uninformative # Node features: per-ROI mean |FC| per window (connectivity strength, not BOLD std) args.use_population_adj = False args.use_dynamic_adj_sequence = True args.use_dynamic_adj = False args.use_fc_degree_features = True elif model_name == "gcn": # Per-subject mean FC as static adjacency — population adj is same for all subjects # Node features: per-ROI mean |FC| per window (more discriminative than BOLD std) args.use_population_adj = False args.use_dynamic_adj_sequence = False args.use_dynamic_adj = False args.use_fc_degree_features = True elif model_name == "gru": # GRU ignores adjacency; per-subject FC still better than population adj args.use_population_adj = False args.use_dynamic_adj_sequence = False args.use_dynamic_adj = False args.use_fc_degree_features = False else: args.use_dynamic_adj_sequence = False args.use_fc_degree_features = False
validate_args(args) return args
def summarize_run(model_name: str, trainer) -> dict[str, float | int | str]: row: dict[str, float | int | str] = {"model_name": model_name} for key, value in sorted(trainer.callback_metrics.items()): if key.startswith(("train_", "val_", "test_")): row[key] = metric_value(value) return row
def write_results(path: Path, rows: list[dict[str, float | int | str]]) -> None: path.parent.mkdir(parents=True, exist_ok=True) fieldnames = sorted({key for row in rows for key in row}) # model_name first, then alphabetical fieldnames = ["model_name"] + [k for k in fieldnames if k != "model_name"] with path.open("w", newline="") as f: writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() writer.writerows(rows)
def main() -> None: parser = build_experiment_parser() args = parser.parse_args()
# prepare and setup once (before the model loop) # Call setup() before preprocess_all so train_subjects reflects the actual split from brain_gcn.main import build_datamodule prep_args = deepcopy(args) prep_args.prepare_data = True dm = build_datamodule(prep_args) dm.prepare_data() dm.setup() # Call setup here to establish actual train/val/test boundary
rows = [] for model_name in args.models: run_args = args_for_model(args, model_name) trainer, _, _ = train_from_args(run_args) rows.append(summarize_run(model_name, trainer)) write_results(Path(args.results_csv), rows) print(f"[{model_name}] done — partial results written to {args.results_csv}")
print(f"\nWrote {len(rows)} rows to {args.results_csv}")
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429"""BC-MAE Fine-tuning Script.
Two-phase fine-tuning of a pre-trained BC-MAE encoder for ASD/TD classification.
Phase 1 — Linear probe (encoder frozen, ~50 epochs) Warms up the classification head without distorting the encoder.
Phase 2 — Full fine-tune (encoder + head, discriminative LRs, ~150 epochs) Head : lr (full) Encoder: lr × encoder_lr_scale (default 0.1)
Data: use_fc_degree_features=True → (W=30, N=200) mean |FC| per window, same feature as pre-training. Labels used only in fine-tuning loss.
Usage: python -m brain_gcn.finetune_main \\ --mae_ckpt checkpoints/mae/mae-best-*.ckpt \\ --data_dir data \\ --probe_epochs 50 \\ --finetune_epochs 150 \\ --lr 5e-4"""
from __future__ import annotations
import argparseimport copyfrom pathlib import Path
import numpy as npimport pytorch_lightning as plimport torchfrom pytorch_lightning.callbacks import EarlyStopping, ModelCheckpointfrom torch import nnfrom torchmetrics.classification import ( BinaryAUROC, BinaryAccuracy, BinaryF1Score, BinaryRecall, BinarySpecificity,)
from brain_gcn.models.mae import BrainFCClassifier, BrainFCEncoderfrom brain_gcn.utils.data.datamodule import ABIDEDataModule
# ---------------------------------------------------------------------------# Lightning module# ---------------------------------------------------------------------------
class MAEClassificationTask(pl.LightningModule): def __init__( self, classifier: BrainFCClassifier, class_weights: torch.Tensor | None = None, lr: float = 5e-4, encoder_lr_scale: float = 0.1, weight_decay: float = 1e-4, bold_noise_std: float = 0.01, cosine_t0: int = 30, cosine_eta_min: float = 1e-6, freeze_encoder: bool = False, ): super().__init__() self.save_hyperparameters(ignore=["classifier", "class_weights"]) self.model = classifier self.register_buffer("class_weights", class_weights) self.loss_fn = nn.CrossEntropyLoss(weight=class_weights)
self.train_acc = BinaryAccuracy() self.val_acc = BinaryAccuracy() self.val_auc = BinaryAUROC() self.val_f1 = BinaryF1Score() self.val_sens = BinaryRecall() self.val_spec = BinarySpecificity() self.test_acc = BinaryAccuracy() self.test_auc = BinaryAUROC() self.test_f1 = BinaryF1Score() self.test_sens = BinaryRecall() self.test_spec = BinarySpecificity()
def forward(self, x: torch.Tensor, adj: torch.Tensor | None = None) -> torch.Tensor: return self.model(x, adj)
def training_step(self, batch, batch_idx: int) -> torch.Tensor: _, adj, labels, _ = batch # Spatial BC-MAE: adj = (B, N, N) full FC matrix = N ROI tokens × N-dim features x = adj if self.hparams.bold_noise_std > 0.0: sig = x.std(dim=(1, 2), keepdim=True).detach() x = x + torch.randn_like(x) * self.hparams.bold_noise_std * sig logits = self(x) loss = self.loss_fn(logits, labels) preds = logits.argmax(-1) self.train_acc.update(preds, labels) self.log("train_loss", loss, prog_bar=True, on_epoch=True, on_step=False) self.log("train_acc", self.train_acc, prog_bar=True, on_epoch=True, on_step=False) return loss
def validation_step(self, batch, batch_idx: int) -> torch.Tensor: _, adj, labels, _ = batch x = adj # (B, N, N) full FC matrix logits = self(x) loss = self.loss_fn(logits, labels) probs = torch.softmax(logits, -1)[:, 1] preds = logits.argmax(-1) self.val_acc.update(preds, labels) self.val_auc.update(probs, labels) self.val_f1.update(preds, labels) self.val_sens.update(preds, labels) self.val_spec.update(preds, labels) self.log("val_loss", loss, prog_bar=True, on_epoch=True, on_step=False) self.log("val_acc", self.val_acc, prog_bar=True, on_epoch=True, on_step=False) self.log("val_auc", self.val_auc, prog_bar=True, on_epoch=True, on_step=False) self.log("val_f1", self.val_f1, prog_bar=False, on_epoch=True, on_step=False) self.log("val_sens", self.val_sens, prog_bar=False, on_epoch=True, on_step=False) self.log("val_spec", self.val_spec, prog_bar=False, on_epoch=True, on_step=False) return loss
def test_step(self, batch, batch_idx: int) -> torch.Tensor: _, adj, labels, _ = batch x = adj # (B, N, N) full FC matrix logits = self(x) loss = self.loss_fn(logits, labels) probs = torch.softmax(logits, -1)[:, 1] preds = logits.argmax(-1) self.test_acc.update(preds, labels) self.test_auc.update(probs, labels) self.test_f1.update(preds, labels) self.test_sens.update(preds, labels) self.test_spec.update(preds, labels) self.log("test_loss", loss, on_epoch=True, on_step=False) self.log("test_acc", self.test_acc, prog_bar=True, on_epoch=True, on_step=False) self.log("test_auc", self.test_auc, prog_bar=True, on_epoch=True, on_step=False) self.log("test_f1", self.test_f1, prog_bar=True, on_epoch=True, on_step=False) self.log("test_sens", self.test_sens, prog_bar=True, on_epoch=True, on_step=False) self.log("test_spec", self.test_spec, prog_bar=True, on_epoch=True, on_step=False) return loss
def configure_optimizers(self): enc_ids = {id(p) for p in self.model.encoder.parameters()} enc_params = [p for p in self.model.parameters() if id(p) in enc_ids] head_params = [p for p in self.model.parameters() if id(p) not in enc_ids]
if self.hparams.freeze_encoder: param_groups = [{"params": head_params, "lr": self.hparams.lr}] else: param_groups = [ {"params": head_params, "lr": self.hparams.lr}, {"params": enc_params, "lr": self.hparams.lr * self.hparams.encoder_lr_scale}, ]
opt = torch.optim.AdamW(param_groups, weight_decay=self.hparams.weight_decay) sch = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts( opt, T_0=self.hparams.cosine_t0, eta_min=self.hparams.cosine_eta_min, ) return {"optimizer": opt, "lr_scheduler": {"scheduler": sch, "interval": "epoch"}}
# ---------------------------------------------------------------------------# Helpers# ---------------------------------------------------------------------------
def _compute_class_weights(dm: ABIDEDataModule) -> torch.Tensor: labels = np.array([int(np.load(p, allow_pickle=True)["label"]) for p in dm._train_paths]) n_td = int((labels == 0).sum()) n_asd = int((labels == 1).sum()) total = n_td + n_asd return torch.tensor([total / (2.0 * n_td), total / (2.0 * n_asd)], dtype=torch.float32)
def _load_encoder( ckpt_path: str, num_rois: int, num_windows: int, hidden_dim: int, num_heads: int, encoder_layers: int, dropout: float,) -> BrainFCEncoder: """Extract BrainFCEncoder weights from a BrainMAETask Lightning checkpoint.""" ckpt = torch.load(ckpt_path, map_location="cpu", weights_only=False) state = ckpt["state_dict"]
enc_state = { k[len("mae.encoder."):]: v for k, v in state.items() if k.startswith("mae.encoder.") } if not enc_state: raise KeyError( f"No 'mae.encoder.*' keys found in {ckpt_path}. " "Make sure you pass a BrainMAETask checkpoint, not a classifier checkpoint." )
encoder = BrainFCEncoder( num_rois=num_rois, num_windows=num_windows, hidden_dim=hidden_dim, num_heads=num_heads, num_layers=encoder_layers, dropout=dropout, ) encoder.load_state_dict(enc_state, strict=True) print(f"Loaded encoder from {ckpt_path} ({len(enc_state)} tensors)") return encoder
def _load_head_weights(task: MAEClassificationTask, ckpt_path: str) -> None: """Restore time_attn + head weights from a previous phase checkpoint.""" sd = torch.load(ckpt_path, map_location="cpu", weights_only=False)["state_dict"] mapping = {} for k, v in sd.items(): if k.startswith("model.time_attn.") or k.startswith("model.head."): new_k = k[len("model."):] mapping[new_k] = v if mapping: current = task.model.state_dict() current.update(mapping) task.model.load_state_dict(current, strict=True) print(f"Restored {len(mapping)} head tensors from {ckpt_path}")
def _make_trainer( max_epochs: int, ckpt_dir: Path, prefix: str, accelerator: str, devices: str, patience: int = 30,) -> pl.Trainer: ckpt_dir.mkdir(parents=True, exist_ok=True) return pl.Trainer( max_epochs=max_epochs, accelerator=accelerator, devices=devices, deterministic=True, log_every_n_steps=1, callbacks=[ EarlyStopping(monitor="val_auc", mode="max", patience=patience), ModelCheckpoint( dirpath=str(ckpt_dir), monitor="val_auc", mode="max", save_top_k=3, filename=f"{prefix}-{{epoch:03d}}-{{val_auc:.3f}}", ), ], )
# ---------------------------------------------------------------------------# Main# ---------------------------------------------------------------------------
def build_parser() -> argparse.ArgumentParser: p = argparse.ArgumentParser(description="BC-MAE Fine-tuning") p.add_argument("--mae_ckpt", type=str, required=True, help="Path to best MAE pre-training checkpoint (.ckpt)") p.add_argument("--data_dir", type=str, default="data") p.add_argument("--max_windows", type=int, default=30) p.add_argument("--hidden_dim", type=int, default=128) p.add_argument("--num_heads", type=int, default=4) p.add_argument("--encoder_layers", type=int, default=4) p.add_argument("--dropout_encoder", type=float, default=0.1) p.add_argument("--dropout_head", type=float, default=0.5) # Phase 1 p.add_argument("--probe_epochs", type=int, default=50, help="Epochs with frozen encoder (linear probe).") p.add_argument("--probe_lr", type=float, default=1e-3) # Phase 2 p.add_argument("--finetune_epochs", type=int, default=150, help="Epochs with full encoder fine-tuning.") p.add_argument("--finetune_lr", type=float, default=5e-4) p.add_argument("--encoder_lr_scale", type=float, default=0.1, help="Encoder LR = finetune_lr × this. Default 0.1 (10x smaller).") p.add_argument("--weight_decay", type=float, default=1e-4) p.add_argument("--bold_noise_std", type=float, default=0.01) p.add_argument("--cosine_t0", type=int, default=30) p.add_argument("--cosine_eta_min", type=float, default=1e-6) # Data p.add_argument("--batch_size", type=int, default=32) p.add_argument("--num_workers", type=int, default=4) p.add_argument("--split_strategy", choices=["stratified", "site_holdout"], default="stratified") p.add_argument("--val_site", type=str, default=None) p.add_argument("--test_site", type=str, default=None) # Misc p.add_argument("--accelerator", type=str, default="auto") p.add_argument("--devices", type=str, default="auto") p.add_argument("--seed", type=int, default=42) p.add_argument("--ckpt_dir", type=str, default="checkpoints/mae_finetune") p.add_argument("--test", action="store_true", help="Run test set evaluation after fine-tuning.") p.add_argument("--skip_probe", action="store_true", help="Skip Phase 1 and jump straight to full fine-tuning.") return p
def main() -> None: torch.set_float32_matmul_precision("medium") args = build_parser().parse_args() pl.seed_everything(args.seed, workers=True)
# ── Data ──────────────────────────────────────────────────────────── # Spatial BC-MAE uses the full mean FC matrix (N, N) as input. # With use_population_adj=False and preserve_fc_sign=True, each subject's # adj = (N, N) signed mean FC — exactly what the spatial encoder expects. dm = ABIDEDataModule( data_dir=args.data_dir, use_population_adj=False, preserve_fc_sign=True, # signed FC → adj = (N, N) mean FC per subject fc_threshold=0.0, # no thresholding — matches pre-training distribution batch_size=args.batch_size, num_workers=args.num_workers, split_strategy=args.split_strategy, val_site=args.val_site, test_site=args.test_site, ) dm.prepare_data() dm.setup()
num_rois = dm.num_nodes class_weights = _compute_class_weights(dm) print(f"num_rois={num_rois} class_weights={class_weights.tolist()}")
# ── Load pre-trained encoder ───────────────────────────────────────── encoder = _load_encoder( ckpt_path=args.mae_ckpt, num_rois=num_rois, num_windows=num_rois, # spatial MAE: num_windows = num_rois (200) hidden_dim=args.hidden_dim, num_heads=args.num_heads, encoder_layers=args.encoder_layers, dropout=args.dropout_encoder, )
ckpt_dir = Path(args.ckpt_dir)
best_probe_ckpt: str | None = None
# ── Phase 1: Linear probe (encoder frozen) ─────────────────────────── if not args.skip_probe: print(f"\n{'='*60}") print(f"Phase 1: Linear probe ({args.probe_epochs} epochs, LR={args.probe_lr})") print(f"{'='*60}")
classifier_p1 = BrainFCClassifier( encoder=encoder, hidden_dim=args.hidden_dim, num_classes=2, dropout=args.dropout_head, freeze_encoder=True, ) task_p1 = MAEClassificationTask( classifier=classifier_p1, class_weights=class_weights, lr=args.probe_lr, encoder_lr_scale=0.0, # ignored while frozen weight_decay=args.weight_decay, bold_noise_std=0.0, # no augmentation during probe cosine_t0=args.cosine_t0, cosine_eta_min=args.cosine_eta_min, freeze_encoder=True, ) trainer_p1 = _make_trainer( max_epochs=args.probe_epochs, ckpt_dir=ckpt_dir / "probe", prefix="probe", accelerator=args.accelerator, devices=args.devices, patience=20, ) trainer_p1.fit(task_p1, datamodule=dm) best_probe_ckpt = trainer_p1.checkpoint_callback.best_model_path best_probe_auc = trainer_p1.callback_metrics.get("val_auc", torch.tensor(0.0)) print(f"Phase 1 best val_auc: {float(best_probe_auc):.4f}")
# ── Phase 2: Full fine-tuning ──────────────────────────────────────── print(f"\n{'='*60}") print(f"Phase 2: Full fine-tune ({args.finetune_epochs} epochs, " f"LR={args.finetune_lr}, enc_scale={args.encoder_lr_scale})") print(f"{'='*60}")
classifier_p2 = BrainFCClassifier( encoder=copy.deepcopy(encoder), hidden_dim=args.hidden_dim, num_classes=2, dropout=args.dropout_head, freeze_encoder=False, ) task_p2 = MAEClassificationTask( classifier=classifier_p2, class_weights=class_weights, lr=args.finetune_lr, encoder_lr_scale=args.encoder_lr_scale, weight_decay=args.weight_decay, bold_noise_std=args.bold_noise_std, cosine_t0=args.cosine_t0, cosine_eta_min=args.cosine_eta_min, freeze_encoder=False, )
# Transfer warmed-up head weights from Phase 1 if best_probe_ckpt: _load_head_weights(task_p2, best_probe_ckpt)
trainer_p2 = _make_trainer( max_epochs=args.finetune_epochs, ckpt_dir=ckpt_dir / "finetune", prefix="ft", accelerator=args.accelerator, devices=args.devices, patience=40, ) trainer_p2.fit(task_p2, datamodule=dm) best_ft_auc = trainer_p2.callback_metrics.get("val_auc", torch.tensor(0.0)) print(f"\nPhase 2 best val_auc: {float(best_ft_auc):.4f}")
if args.test: print("\nRunning test set evaluation ...") trainer_p2.test(task_p2, datamodule=dm, ckpt_path="best")
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285"""Hyperparameter optimization using Optuna.
Provides automated search over model, training, and data hyperparameters.Integrates with the existing training pipeline via argparse."""
from __future__ import annotations
import argparseimport jsonimport loggingfrom pathlib import Pathfrom typing import Any
import optunafrom optuna.pruners import MedianPrunerfrom optuna.samplers import TPESamplerfrom optuna.trial import Trial
from brain_gcn.main import train_from_args, validate_args
log = logging.getLogger(__name__)
class HPOConfig: """Hyperparameter optimization configuration."""
def __init__( self, study_name: str = "brain_gcn_hpo", n_trials: int = 20, timeout: int | None = None, direction: str = "maximize", objective_metric: str = "test_auc", storage: str | None = None, seed: int = 42, ): self.study_name = study_name self.n_trials = n_trials self.timeout = timeout self.direction = direction self.objective_metric = objective_metric self.storage = storage self.seed = seed
class HPOSearchSpace: """Define hyperparameter search space for Optuna."""
@staticmethod def suggest_params(trial: Trial, base_args: argparse.Namespace) -> argparse.Namespace: """Suggest hyperparameters for a single trial.
Parameters ---------- trial : optuna.trial.Trial Current trial object. base_args : argparse.Namespace Base arguments; suggested values override these.
Returns ------- argparse.Namespace Arguments with suggested hyperparameters. """ args = argparse.Namespace(**vars(base_args))
# Model architecture args.hidden_dim = trial.suggest_categorical( "hidden_dim", [32, 64, 128, 256] ) args.dropout = trial.suggest_float("dropout", 0.0, 0.5, step=0.1)
# Training args.lr = trial.suggest_loguniform("lr", 1e-5, 1e-2) args.weight_decay = trial.suggest_loguniform("weight_decay", 1e-6, 1e-3) args.batch_size = trial.suggest_categorical( "batch_size", [8, 16, 32, 64] )
# DropEdge regularization args.drop_edge_p = trial.suggest_float("drop_edge_p", 0.0, 0.3, step=0.1)
# BOLD noise augmentation args.bold_noise_std = trial.suggest_float( "bold_noise_std", 0.0, 0.05, step=0.01 )
# Cosine annealing args.cosine_t0 = trial.suggest_categorical( "cosine_t0", [30, 50, 100] ) args.cosine_t_mult = trial.suggest_categorical( "cosine_t_mult", [1, 2, 3] ) args.cosine_eta_min = trial.suggest_loguniform( "cosine_eta_min", 1e-6, 1e-4 )
return args
def objective( trial: Trial, base_args: argparse.Namespace, hpo_config: HPOConfig,) -> float: """Objective function for Optuna optimization.
Parameters ---------- trial : optuna.trial.Trial Current trial. base_args : argparse.Namespace Base arguments template. hpo_config : HPOConfig HPO configuration.
Returns ------- float Objective value (test set metric). """ try: # Suggest hyperparameters args = HPOSearchSpace.suggest_params(trial, base_args) validate_args(args)
# Train model trainer, _, _ = train_from_args(args)
# Extract objective metric metric_value = trainer.callback_metrics.get( hpo_config.objective_metric, None ) if metric_value is None: log.warning( f"Metric {hpo_config.objective_metric} not found. " "Available: %s", list(trainer.callback_metrics.keys()) ) return float("-inf")
return float(metric_value.detach().cpu())
except Exception as e: log.error(f"Trial failed: {e}") return float("-inf")
class HPOStudy: """Wrapper for Optuna study with convenience methods."""
def __init__(self, config: HPOConfig): self.config = config self.study: optuna.Study | None = None
def create_study(self) -> optuna.Study: """Create or load Optuna study.""" sampler = TPESampler(seed=self.config.seed) pruner = MedianPruner()
storage_url = None if self.config.storage: storage_url = f"sqlite:///{self.config.storage}"
self.study = optuna.create_study( study_name=self.config.study_name, direction=self.config.direction, sampler=sampler, pruner=pruner, storage=storage_url, load_if_exists=True, ) return self.study
def optimize( self, base_args: argparse.Namespace, ) -> optuna.Study: """Run hyperparameter optimization.
Parameters ---------- base_args : argparse.Namespace Base arguments template.
Returns ------- optuna.Study Completed study object. """ if self.study is None: self.create_study()
self.study.optimize( lambda trial: objective(trial, base_args, self.config), n_trials=self.config.n_trials, timeout=self.config.timeout, show_progress_bar=True, ) return self.study
def best_params(self) -> dict[str, Any]: """Get best hyperparameters found.""" if self.study is None: raise RuntimeError("Study not created. Call optimize() first.") return self.study.best_params
def best_value(self) -> float: """Get best objective value.""" if self.study is None: raise RuntimeError("Study not created. Call optimize() first.") return self.study.best_value
def save_summary(self, output_path: str | Path) -> None: """Save HPO summary to JSON.""" if self.study is None: raise RuntimeError("Study not created. Call optimize() first.")
output_path = Path(output_path) output_path.parent.mkdir(parents=True, exist_ok=True)
summary = { "study_name": self.config.study_name, "n_trials": len(self.study.trials), "best_value": self.study.best_value, "best_params": self.study.best_params, "direction": self.config.direction, "objective_metric": self.config.objective_metric, }
with open(output_path, "w") as f: json.dump(summary, f, indent=2)
log.info(f"HPO summary saved to {output_path}")
def hpo_from_args(args: argparse.Namespace) -> HPOStudy: """Create HPO study from command-line arguments.""" hpo_config = HPOConfig( study_name=args.hpo_study_name, n_trials=args.hpo_n_trials, timeout=args.hpo_timeout, objective_metric=args.hpo_objective, storage=args.hpo_storage, seed=args.seed, ) return HPOStudy(hpo_config)
def add_hpo_arguments(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: """Add HPO-specific arguments to parser.""" parser.add_argument( "--hpo_study_name", type=str, default="brain_gcn_hpo", help="Optuna study name.", ) parser.add_argument( "--hpo_n_trials", type=int, default=20, help="Number of trials.", ) parser.add_argument( "--hpo_timeout", type=int, default=None, help="Timeout in seconds.", ) parser.add_argument( "--hpo_objective", type=str, default="test_auc", help="Metric to optimize (e.g., test_auc, test_acc).", ) parser.add_argument( "--hpo_storage", type=str, default="hpo_studies.db", help="SQLite storage path for persistent studies.", ) return parser123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322"""Training entry point for Brain-Connectivity-GCN.
v2 changes: - site_holdout as default split_strategy - Class weights computed from training labels → weighted CE loss - save_top_k=5 for checkpoint ensembling - ensemble_predict() utility after training - batch_size default lowered to 16 (site holdout = smaller train sets)"""
from __future__ import annotations
import argparseimport jsonfrom pathlib import Path
import numpy as npimport pytorch_lightning as plimport torchfrom pytorch_lightning.callbacks import EarlyStopping, ModelCheckpointfrom torchmetrics.classification import BinaryAUROC
from brain_gcn.models.brain_gcn import BrainModeNetworkfrom brain_gcn.tasks import ClassificationTaskfrom brain_gcn.utils.data.datamodule import ABIDEDataModule
# ---------------------------------------------------------------------------# Parser# ---------------------------------------------------------------------------
def build_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser(description="Train Brain-Connectivity-GCN classifier") parser = ABIDEDataModule.add_data_specific_arguments(parser) parser = ClassificationTask.add_model_specific_arguments(parser) parser.add_argument("--max_epochs", type=int, default=200) parser.add_argument("--accelerator", type=str, default="auto") parser.add_argument("--devices", type=str, default="auto") parser.add_argument("--seed", type=int, default=42) parser.add_argument("--ckpt_tag", type=str, default="", help="Optional suffix appended to checkpoint directory name (e.g. seed-specific).") parser.add_argument("--log_every_n_steps", type=int, default=1) parser.add_argument("--prepare_data", action="store_true") parser.add_argument("--test", action="store_true") parser.add_argument( "--no_ensemble", action="store_true", help="Skip top-5 checkpoint ensembling at test time.", ) return parser
# ---------------------------------------------------------------------------# Validation# ---------------------------------------------------------------------------
def validate_args(args: argparse.Namespace) -> None: if args.model_name in ("fc_mlp", "adv_fc_mlp", "brain_mode", "adv_brain_mode", "dynamic_fc_attn") and args.use_population_adj: raise ValueError( f"{args.model_name} needs per-subject connectivity. Re-run with --no-use_population_adj." ) if args.use_dynamic_adj_sequence and args.use_population_adj: raise ValueError( "Dynamic adjacency sequences are per-subject. Re-run with --no-use_population_adj." )
# ---------------------------------------------------------------------------# Component builders# ---------------------------------------------------------------------------
def build_datamodule(args: argparse.Namespace) -> ABIDEDataModule: # fc_mlp variants need signed FC; auto-enable unless user explicitly set it preserve_fc_sign = getattr(args, "preserve_fc_sign", False) if args.model_name in ("fc_mlp", "adv_fc_mlp", "brain_mode", "adv_brain_mode") and not preserve_fc_sign: preserve_fc_sign = True
return ABIDEDataModule( data_dir=args.data_dir, n_subjects=args.n_subjects, window_len=args.window_len, step=args.step, max_windows=args.max_windows, fc_threshold=args.fc_threshold, use_dynamic_adj=args.use_dynamic_adj, use_dynamic_adj_sequence=args.use_dynamic_adj_sequence, use_population_adj=args.use_population_adj, preserve_fc_sign=preserve_fc_sign, use_fc_variance=getattr(args, "use_fc_variance", False), use_fisher_z=getattr(args, "use_fisher_z", False), use_fc_degree_features=getattr(args, "use_fc_degree_features", False), use_fc_row_features=getattr(args, "use_fc_row_features", False), n_pca_components=getattr(args, "n_pca_components", 0), batch_size=args.batch_size, val_ratio=args.val_ratio, test_ratio=args.test_ratio, split_strategy=args.split_strategy, val_site=args.val_site, test_site=args.test_site, num_workers=args.num_workers, overwrite_cache=getattr(args, "overwrite_cache", False), force_prepare=args.prepare_data, )
def _compute_class_weights(dm: ABIDEDataModule) -> torch.Tensor: """Balanced class weights from training labels: total / (n_classes * n_per_class).""" labels = np.array([int(np.load(p, allow_pickle=True)["label"]) for p in dm._train_paths]) n_td = int((labels == 0).sum()) n_asd = int((labels == 1).sum()) total = n_td + n_asd w_td = total / (2.0 * n_td) w_asd = total / (2.0 * n_asd) return torch.tensor([w_td, w_asd], dtype=torch.float32)
def _discriminative_mode_init(dm: ABIDEDataModule, num_modes: int) -> torch.Tensor: """Load training FCs by class and compute SVD-based discriminative modes.
Called only when model_name == 'brain_mode'. Reads the cached .npz files to compute (mean_FC_ASD − mean_FC_TD) and returns the top-K left singular vectors as the initial mode matrix (K, N). """ fc_asd, fc_td = [], [] for p in dm._train_paths: data = np.load(p, allow_pickle=True) fc = data["mean_fc"].astype(np.float32) lbl = int(data["label"]) (fc_asd if lbl == 1 else fc_td).append(fc)
fc_asd_arr = np.stack(fc_asd) # (n_asd, N, N) fc_td_arr = np.stack(fc_td) # (n_td, N, N) return BrainModeNetwork.discriminative_init(fc_asd_arr, fc_td_arr, num_modes)
def build_task(args: argparse.Namespace, dm: ABIDEDataModule) -> ClassificationTask: """Build ClassificationTask with class weights from the training split.""" # dm.setup() must have been called before this try: class_weights = _compute_class_weights(dm) except Exception as exc: print(f"WARNING: Could not compute class weights ({exc}). Using uniform weights.") class_weights = None
mode_init = None if args.model_name in ("brain_mode", "adv_brain_mode"): try: mode_init = _discriminative_mode_init(dm, getattr(args, "num_modes", 16)) except Exception as exc: print(f"[BMN] discriminative init failed ({exc}), using QR init.")
return ClassificationTask( hidden_dim=args.hidden_dim, dropout=args.dropout, readout=args.readout, model_name=args.model_name, lr=args.lr, weight_decay=args.weight_decay, class_weights=class_weights, bold_noise_std=args.bold_noise_std, drop_edge_p=args.drop_edge_p, cosine_t0=args.cosine_t0, cosine_t_mult=args.cosine_t_mult, cosine_eta_min=args.cosine_eta_min, num_sites=dm.num_sites, adv_site_weight=getattr(args, "adv_site_weight", 1.0), num_nodes=dm.num_nodes, num_modes=getattr(args, "num_modes", 16), orth_weight=getattr(args, "orth_weight", 0.01), mode_init=mode_init, in_features=dm.num_nodes if getattr(args, "use_fc_row_features", False) else 1, )
def build_trainer(args: argparse.Namespace) -> tuple[pl.Trainer, Path]: ckpt_name = args.model_name if getattr(args, "n_pca_components", 0) > 0: ckpt_name += f"_pca{args.n_pca_components}" if args.model_name in ("brain_mode", "adv_brain_mode"): split_tag = getattr(args, "split_strategy", "site_holdout")[:4] # e.g. "site" or "stra" ckpt_name += f"_k{getattr(args, 'num_modes', 16)}_{split_tag}" ckpt_tag = getattr(args, "ckpt_tag", "") if ckpt_tag: ckpt_name += f"_{ckpt_tag}" ckpt_dir = Path("checkpoints") / ckpt_name ckpt_dir.mkdir(parents=True, exist_ok=True) # Write run config metadata for safe ensemble verification config_meta = { "model_name": args.model_name, "use_dynamic_adj_sequence": args.use_dynamic_adj_sequence, "use_population_adj": args.use_population_adj, } config_path = ckpt_dir / "run_config.json" with open(config_path, "w") as f: json.dump(config_meta, f, indent=2) trainer = pl.Trainer( max_epochs=args.max_epochs, accelerator=args.accelerator, devices=args.devices, deterministic=True, log_every_n_steps=args.log_every_n_steps, callbacks=[ EarlyStopping(monitor="val_auc", mode="max", patience=40), ModelCheckpoint( dirpath=str(ckpt_dir), monitor="val_auc", mode="max", save_top_k=5, # was 1 filename="brain-gcn-{epoch:03d}-{val_auc:.3f}", ), ], ) return trainer, ckpt_dir
# ---------------------------------------------------------------------------# Ensemble inference# ---------------------------------------------------------------------------
def ensemble_predict( ckpt_dir: str | Path, dm: ABIDEDataModule, device: str = "cpu",) -> torch.Tensor: """Average softmax probabilities over the top-5 saved checkpoints.
Verifies that each checkpoint's model config matches the datamodule's adjacency mode to prevent silent mismatches.
Returns ------- probs : (N_test, num_classes) averaged probability tensor """ ckpt_dir = Path(ckpt_dir) ckpt_paths = sorted(ckpt_dir.glob("*.ckpt")) if not ckpt_paths: raise FileNotFoundError(f"No checkpoints found in {ckpt_dir}")
# Verify config compatibility config_path = ckpt_dir / "run_config.json" if config_path.exists(): with open(config_path) as f: saved_config = json.load(f) assert saved_config["use_dynamic_adj_sequence"] == dm.use_dynamic_adj_sequence, ( f"Checkpoint use_dynamic_adj_sequence={saved_config['use_dynamic_adj_sequence']} " f"but datamodule has {dm.use_dynamic_adj_sequence}" ) assert saved_config["use_population_adj"] == dm.use_population_adj, ( f"Checkpoint use_population_adj={saved_config['use_population_adj']} " f"but datamodule has {dm.use_population_adj}" )
all_probs: list[torch.Tensor] = [] for ckpt in ckpt_paths: task = ClassificationTask.load_from_checkpoint(ckpt, map_location=device, strict=False) task.eval().to(device) batch_probs: list[torch.Tensor] = [] with torch.no_grad(): for batch in dm.test_dataloader(): bold_windows, adj = batch[0], batch[1] logits = task(bold_windows.to(device), adj.to(device)) batch_probs.append(torch.softmax(logits, dim=-1).cpu()) all_probs.append(torch.cat(batch_probs, dim=0))
return torch.stack(all_probs).mean(0) # (N_test, 2)
# ---------------------------------------------------------------------------# Main training loop# ---------------------------------------------------------------------------
def train_from_args( args: argparse.Namespace,) -> tuple[pl.Trainer, ClassificationTask, ABIDEDataModule]: pl.seed_everything(args.seed, workers=True) validate_args(args)
dm = build_datamodule(args) # Call setup here so class weights can be computed before building the task dm.prepare_data() dm.setup()
task = build_task(args, dm) trainer, ckpt_dir = build_trainer(args) trainer.fit(task, datamodule=dm)
if args.test: if getattr(args, "no_ensemble", False): trainer.test(task, datamodule=dm, ckpt_path="best") else: # Ensemble over top-5 checkpoints try: avg_probs = ensemble_predict(ckpt_dir, dm) preds = avg_probs.argmax(dim=-1) # Collect ground-truth labels from test set (index 2 regardless of tuple length) labels = torch.cat([b[2] for b in dm.test_dataloader()]) acc = (preds == labels).float().mean().item() auc_metric = BinaryAUROC() auc = auc_metric(avg_probs[:, 1], labels).item() print(f"\n[Ensemble] test_acc={acc:.4f} test_auc={auc:.4f}") # Also log via trainer for experiment runner compatibility trainer.callback_metrics["test_acc_ensemble"] = torch.tensor(acc) trainer.callback_metrics["test_auc_ensemble"] = torch.tensor(auc) except Exception as exc: print(f"[Ensemble] failed ({exc}), falling back to single-best ckpt.") trainer.test(task, datamodule=dm, ckpt_path="best")
return trainer, task, dm
def main() -> None: # RTX / Ampere+ GPUs: use TF32 for matmuls — faster with negligible precision loss torch.set_float32_matmul_precision("medium") args = build_parser().parse_args() train_from_args(args)
if __name__ == "__main__": main()1234567891011121314151617181920212223242526272829303132from .brain_gcn import ( BrainGCNClassifier, ConnectivityMLPClassifier, GraphOnlyClassifier, TemporalGRUClassifier, build_model,)from .advanced_models import ( GATClassifier, TransformerClassifier, CNN3DClassifier, GraphSAGEClassifier,)from .registry import ModelRegistry, ModelConfig, add_model_choice_argument
__all__ = [ # Original models "BrainGCNClassifier", "ConnectivityMLPClassifier", "GraphOnlyClassifier", "TemporalGRUClassifier", # Advanced models "GATClassifier", "TransformerClassifier", "CNN3DClassifier", "GraphSAGEClassifier", # Utilities "build_model", "ModelRegistry", "ModelConfig", "add_model_choice_argument",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346"""Advanced model architectures for brain connectivity analysis.
New models:- Graph Attention Networks (GAT)- Transformer-based temporal encoder- 3D-CNN for spatiotemporal features- GraphSAGE (sampling-aggregating)"""
from __future__ import annotations
import torchfrom torch import nnimport torch.nn.functional as Ffrom brain_gcn.utils.graph_conv import calculate_laplacian_with_self_loop, drop_edgefrom brain_gcn.models.brain_gcn import AttentionReadout
# ---------------------------------------------------------------------------# Graph Attention Networks (GAT)# ---------------------------------------------------------------------------
class GraphAttentionLayer(nn.Module): """Multi-head graph attention layer."""
def __init__(self, in_dim: int, out_dim: int, num_heads: int = 4, dropout: float = 0.1): super().__init__() self.num_heads = num_heads self.out_dim = out_dim assert out_dim % num_heads == 0, "out_dim must be divisible by num_heads" self.head_dim = out_dim // num_heads
self.query = nn.Linear(in_dim, out_dim) self.key = nn.Linear(in_dim, out_dim) self.value = nn.Linear(in_dim, out_dim) self.fc_out = nn.Linear(out_dim, out_dim) self.dropout = nn.Dropout(dropout) self.scale = torch.sqrt(torch.tensor(self.head_dim, dtype=torch.float32))
def forward(self, x: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: # x: (batch, nodes, in_dim) # adj: (batch, nodes, nodes) or (nodes, nodes) Q = self.query(x) # (batch, nodes, out_dim) K = self.key(x) V = self.value(x)
# Reshape for multi-head: (batch, nodes, heads, head_dim) Q = Q.reshape(Q.shape[0], Q.shape[1], self.num_heads, self.head_dim).transpose(1, 2) K = K.reshape(K.shape[0], K.shape[1], self.num_heads, self.head_dim).transpose(1, 2) V = V.reshape(V.shape[0], V.shape[1], self.num_heads, self.head_dim).transpose(1, 2)
# Attention scores: (batch, heads, nodes, nodes) scores = torch.matmul(Q, K.transpose(-2, -1)) / self.scale # Mask non-edges with large negative value (binary mask, not value-based) scores = scores + (adj.unsqueeze(1) == 0).float() * -1e9
attn = F.softmax(scores, dim=-1) attn = self.dropout(attn)
# Apply attention to values out = torch.matmul(attn, V) # (batch, heads, nodes, head_dim) out = out.transpose(1, 2).reshape(out.shape[0], out.shape[2], -1) # (batch, nodes, out_dim)
return self.fc_out(out)
class GATEncoder(nn.Module): """Multi-layer Graph Attention Network."""
def __init__(self, in_dim: int, hidden_dim: int, num_heads: int = 4, dropout: float = 0.1): super().__init__() self.layer1 = GraphAttentionLayer(in_dim, hidden_dim, num_heads=num_heads, dropout=dropout) self.layer2 = GraphAttentionLayer(hidden_dim, hidden_dim, num_heads=num_heads, dropout=dropout) self.norm1 = nn.LayerNorm(hidden_dim) self.norm2 = nn.LayerNorm(hidden_dim) self.dropout = nn.Dropout(dropout)
def forward(self, x: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: h = self.layer1(x, adj) h = self.dropout(F.relu(self.norm1(h))) h = self.layer2(h, adj) h = self.dropout(F.relu(self.norm2(h))) return h
# ---------------------------------------------------------------------------# Transformer-based Temporal Encoder# ---------------------------------------------------------------------------
class TransformerTemporalEncoder(nn.Module): """Transformer-based encoder for temporal sequences."""
def __init__(self, hidden_dim: int = 64, num_heads: int = 4, num_layers: int = 2, dropout: float = 0.1): super().__init__() self.embedding = nn.Linear(1, hidden_dim) encoder_layer = nn.TransformerEncoderLayer( d_model=hidden_dim, nhead=num_heads, dim_feedforward=hidden_dim * 4, dropout=dropout, batch_first=True, activation='relu', ) self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=num_layers) self.norm = nn.LayerNorm(hidden_dim)
def forward(self, bold_windows: torch.Tensor) -> torch.Tensor: # bold_windows: (batch, windows, nodes) → embed → (batch * nodes, windows, hidden_dim) batch, windows, nodes = bold_windows.shape # Embed time dimension x = bold_windows.permute(0, 2, 1).reshape(batch * nodes, windows, 1) # (B*N, W, 1) x = self.embedding(x) # (B*N, W, hidden_dim) # Transformer h = self.transformer(x) # (B*N, W, hidden_dim) h = self.norm(h) h = h[:, -1, :] # Take last token h = h.reshape(batch, nodes, -1) # (B, N, hidden_dim) return h
# ---------------------------------------------------------------------------# 3D-CNN for Spatiotemporal Features# ---------------------------------------------------------------------------
class CNN3D(nn.Module): """3D-CNN for spatiotemporal brain connectivity analysis."""
def __init__(self, hidden_dim: int = 64, dropout: float = 0.1): super().__init__() # Input: (batch, 1, time, height, width) for connectivity matrices # Scale intermediate channels relative to hidden_dim ch1 = max(8, hidden_dim // 4) ch2 = max(16, hidden_dim // 2) self.conv1 = nn.Conv3d(1, ch1, kernel_size=(3, 3, 3), padding=(1, 1, 1)) self.conv2 = nn.Conv3d(ch1, ch2, kernel_size=(3, 3, 3), padding=(1, 1, 1)) self.conv3 = nn.Conv3d(ch2, hidden_dim, kernel_size=(3, 3, 3), padding=(1, 1, 1))
self.pool = nn.MaxPool3d(kernel_size=2, stride=2) self.dropout = nn.Dropout3d(dropout) self.norm1 = nn.BatchNorm3d(ch1) self.norm2 = nn.BatchNorm3d(ch2) self.norm3 = nn.BatchNorm3d(hidden_dim)
def forward(self, fc_windows: torch.Tensor) -> torch.Tensor: # fc_windows: (batch, windows, nodes, nodes) batch, windows, nodes, _ = fc_windows.shape # Add channel dimension: (batch, 1, windows, nodes, nodes) x = fc_windows.unsqueeze(1) x = self.conv1(x) x = self.norm1(x) x = F.relu(x) x = self.pool(x) x = self.dropout(x) x = self.conv2(x) x = self.norm2(x) x = F.relu(x) x = self.pool(x) x = self.dropout(x) x = self.conv3(x) x = self.norm3(x) x = F.relu(x) # Global average pooling x = x.mean(dim=(2, 3, 4)) # (batch, channels) return x
# ---------------------------------------------------------------------------# GraphSAGE (Sampling and Aggregating)# ---------------------------------------------------------------------------
class GraphSAGELayer(nn.Module): """GraphSAGE layer using mean aggregation."""
def __init__(self, in_dim: int, out_dim: int, dropout: float = 0.1): super().__init__() self.agg_weight = nn.Linear(in_dim, out_dim) self.self_weight = nn.Linear(in_dim, out_dim) self.norm = nn.LayerNorm(out_dim) self.dropout = nn.Dropout(dropout)
def forward(self, x: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: # x: (batch, nodes, in_dim) # adj: (batch, nodes, nodes) or (nodes, nodes) # Aggregate neighbors: (batch, nodes, in_dim) if adj.dim() == 2: adj = adj.unsqueeze(0) # Normalize adjacency for aggregation degree = adj.sum(dim=-1, keepdim=True).clamp(min=1) adj_norm = adj / degree neighbor_agg = torch.bmm(adj_norm, x) # (batch, nodes, in_dim) # Combine self and aggregated neighbor features h_agg = self.agg_weight(neighbor_agg) h_self = self.self_weight(x) h = h_agg + h_self h = F.relu(self.norm(h)) h = self.dropout(h) return h
class GraphSAGEEncoder(nn.Module): """Multi-layer GraphSAGE encoder."""
def __init__(self, in_dim: int, hidden_dim: int, dropout: float = 0.1): super().__init__() self.layer1 = GraphSAGELayer(in_dim, hidden_dim, dropout=dropout) self.layer2 = GraphSAGELayer(hidden_dim, hidden_dim, dropout=dropout)
def forward(self, x: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: h = self.layer1(x, adj) h = self.layer2(h, adj) return h
# ---------------------------------------------------------------------------# Classifier Heads# ---------------------------------------------------------------------------
def make_head(hidden_dim: int, num_classes: int = 2, dropout: float = 0.5) -> nn.Sequential: return nn.Sequential( nn.Linear(hidden_dim, hidden_dim), nn.LayerNorm(hidden_dim), nn.ReLU(), nn.Dropout(dropout), nn.Linear(hidden_dim, num_classes), )
# ---------------------------------------------------------------------------# Complete Models# ---------------------------------------------------------------------------
class GATClassifier(nn.Module): """Graph Attention Network classifier."""
def __init__(self, hidden_dim: int = 64, num_heads: int = 4, dropout: float = 0.5): super().__init__() self.encoder = GATEncoder(1, hidden_dim, num_heads=num_heads, dropout=min(dropout, 0.2)) self.attention = AttentionReadout(hidden_dim) self.head = make_head(hidden_dim, dropout=dropout)
def forward(self, bold_windows: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: batch, windows, nodes = bold_windows.shape # Process each window embeddings_list = [] adj_norm = calculate_laplacian_with_self_loop(adj) for w in range(windows): x = bold_windows[:, w, :].unsqueeze(-1) # (batch, nodes, 1) if adj_norm.dim() == 3: adj_w = adj_norm else: adj_w = adj_norm.unsqueeze(0) h = self.encoder(x, adj_w) embeddings_list.append(h) # Average over windows h = torch.stack(embeddings_list, dim=1).mean(dim=1) # (batch, nodes, hidden_dim) pooled, _ = self.attention(h) logits = self.head(pooled) return logits
class TransformerClassifier(nn.Module): """Transformer-based classifier for temporal brain signals."""
def __init__(self, hidden_dim: int = 64, num_heads: int = 4, dropout: float = 0.5): super().__init__() self.temporal_encoder = TransformerTemporalEncoder(hidden_dim, num_heads=num_heads, dropout=min(dropout, 0.2)) self.attention = AttentionReadout(hidden_dim) self.head = make_head(hidden_dim, dropout=dropout)
def forward(self, bold_windows: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: h = self.temporal_encoder(bold_windows) # (batch, nodes, hidden_dim) pooled, _ = self.attention(h) logits = self.head(pooled) return logits
class CNN3DClassifier(nn.Module): """3D-CNN classifier for connectivity dynamics."""
def __init__(self, hidden_dim: int = 64, dropout: float = 0.5): super().__init__() self.cnn = CNN3D(hidden_dim, dropout=min(dropout, 0.2)) self.head = make_head(hidden_dim, dropout=dropout)
def forward(self, bold_windows: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: if adj.dim() == 4: # Dynamic adjacency (B, W, N, N) — use directly fc_windows = adj else: # Static adjacency (B, N, N) — replicate across windows W = bold_windows.shape[1] fc_windows = adj.unsqueeze(1).expand(-1, W, -1, -1)
h = self.cnn(fc_windows) # (batch, 64) logits = self.head(h) return logits
class GraphSAGEClassifier(nn.Module): """GraphSAGE-based classifier."""
def __init__(self, hidden_dim: int = 64, dropout: float = 0.5): super().__init__() self.encoder = GraphSAGEEncoder(1, hidden_dim, dropout=min(dropout, 0.2)) self.attention = AttentionReadout(hidden_dim) self.head = make_head(hidden_dim, dropout=dropout)
def forward(self, bold_windows: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: batch, windows, nodes = bold_windows.shape adj_norm = calculate_laplacian_with_self_loop(adj) embeddings_list = [] for w in range(windows): x = bold_windows[:, w, :].unsqueeze(-1) # (batch, nodes, 1) if adj_norm.dim() == 3: adj_w = adj_norm else: adj_w = adj_norm.unsqueeze(0) h = self.encoder(x, adj_w) embeddings_list.append(h) h = torch.stack(embeddings_list, dim=1).mean(dim=1) pooled, _ = self.attention(h) logits = self.head(pooled) return logits123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724"""Brain GCN model definitions.
v2 changes: - TwoLayerGCN with residual connection replaces single GraphLinear in encoder - DropEdge applied in BrainGCNClassifier.forward() during training - GraphOnlyClassifier also upgraded to TwoLayerGCN (was already 2-layer but without residual or LayerNorm between layers)"""
from __future__ import annotations
import torchfrom torch import nn
from brain_gcn.utils.graph_conv import calculate_laplacian_with_self_loop, drop_edgefrom brain_gcn.utils.grl import GradientReversal
# ---------------------------------------------------------------------------# Building blocks# ---------------------------------------------------------------------------
class GraphLinear(nn.Module): """Apply normalized adjacency, then a learned linear projection."""
def __init__(self, in_features: int, out_features: int, bias: bool = True): super().__init__() self.linear = nn.Linear(in_features, out_features, bias=bias)
def forward(self, x: torch.Tensor, adj_norm: torch.Tensor) -> torch.Tensor: x = torch.bmm(adj_norm, x) return self.linear(x)
class TwoLayerGCN(nn.Module): """2-layer GCN with residual skip connection.
Architecture (Kipf & Welling 2017 + He et al. 2016 residuals): h1 = ReLU(LayerNorm(GCN1(x))) h2 = Dropout(ReLU(LayerNorm(GCN2(h1)))) out = h2 + skip(x) # skip is a plain linear projection
The residual stabilises gradient flow and lets the model interpolate between 1-hop and 2-hop aggregation. """
def __init__(self, in_dim: int, hidden_dim: int, dropout: float = 0.1): super().__init__() self.gcn1 = GraphLinear(in_dim, hidden_dim) self.gcn2 = GraphLinear(hidden_dim, hidden_dim) self.skip = nn.Linear(in_dim, hidden_dim, bias=False) self.norm1 = nn.LayerNorm(hidden_dim) self.norm2 = nn.LayerNorm(hidden_dim) self.drop = nn.Dropout(dropout)
def forward(self, x: torch.Tensor, adj_norm: torch.Tensor) -> torch.Tensor: h = torch.relu(self.norm1(self.gcn1(x, adj_norm))) h = self.drop(torch.relu(self.norm2(self.gcn2(h, adj_norm)))) return h + self.skip(x) # residual
# ---------------------------------------------------------------------------# Encoders# ---------------------------------------------------------------------------
class GraphTemporalEncoder(nn.Module): """Graph-aware temporal encoder for ROI-level window sequences.
Supports two node feature modes: - Scalar (in_features=1): bold_windows (B, W, N) — BOLD std per window - FC rows (in_features=N): fc_windows (B, W, N, N) — connectivity profile per node
Vectorized implementation: single batched GCN pass over all windows. """
def __init__(self, hidden_dim: int = 64, dropout: float = 0.1, in_features: int = 1): super().__init__() self.input_graph = TwoLayerGCN(in_features, hidden_dim, dropout=min(dropout, 0.1)) self.gru = nn.GRU( input_size=hidden_dim, hidden_size=hidden_dim, batch_first=True, ) self.norm = nn.LayerNorm(hidden_dim) self.dropout = nn.Dropout(dropout)
def forward(self, bold_windows: torch.Tensor, adj_norm: torch.Tensor) -> torch.Tensor: # bold_windows: (B, W, N) for scalar features or (B, W, N, N) for FC-row features if bold_windows.dim() == 4: # FC-row features: (B, W, N, N) → (B*W, N, N) where last dim is in_features batch_size, num_windows, num_nodes, _ = bold_windows.shape x = bold_windows.reshape(batch_size * num_windows, num_nodes, -1) else: # Scalar features: (B, W, N) → (B*W, N, 1) batch_size, num_windows, num_nodes = bold_windows.shape x = bold_windows.reshape(batch_size * num_windows, num_nodes, 1)
# Handle both 3D (B,N,N) and 4D (B,W,N,N) adjacency if adj_norm.dim() == 4: adj_flat = adj_norm.reshape(batch_size * num_windows, num_nodes, num_nodes) else: adj_flat = adj_norm.unsqueeze(1).expand(-1, num_windows, -1, -1) adj_flat = adj_flat.reshape(batch_size * num_windows, num_nodes, num_nodes)
# Single batched GCN pass → (B*W, N, H) h = self.input_graph(x, adj_flat)
# Reshape back and apply node-major GRU h = h.reshape(batch_size, num_windows, num_nodes, -1) # (B, W, N, H) hidden_dim = h.shape[-1] h = h.permute(0, 2, 1, 3).reshape(batch_size * num_nodes, num_windows, hidden_dim) h, _ = self.gru(h) h = h[:, -1, :].reshape(batch_size, num_nodes, -1) # (B, N, H) return self.dropout(self.norm(h))
class AttentionReadout(nn.Module): """Learn per-ROI attention weights for subject-level graph pooling. Single linear projection is sufficient for N=200 nodes. More interpretable and faster than 2-layer MLP. """
def __init__(self, hidden_dim: int): super().__init__() self.score = nn.Linear(hidden_dim, 1)
def forward(self, node_embeddings: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: weights = torch.softmax(self.score(node_embeddings).squeeze(-1), dim=-1) pooled = torch.sum(node_embeddings * weights.unsqueeze(-1), dim=1) return pooled, weights
# ---------------------------------------------------------------------------# Helpers shared across classifiers# ---------------------------------------------------------------------------
def make_classifier_head(hidden_dim: int, num_classes: int, dropout: float) -> nn.Sequential: return nn.Sequential( nn.Linear(hidden_dim, hidden_dim), nn.LayerNorm(hidden_dim), nn.ReLU(), nn.Dropout(dropout), nn.Linear(hidden_dim, num_classes), )
def graph_readout( node_embeddings: torch.Tensor, attention: AttentionReadout | None,) -> tuple[torch.Tensor, torch.Tensor | None]: if attention is None: return node_embeddings.mean(dim=1), None return attention(node_embeddings)
# ---------------------------------------------------------------------------# Classifiers# ---------------------------------------------------------------------------
class BrainGCNClassifier(nn.Module): """Subject-level ASD/TD classifier for dynamic brain connectivity.
v2: TwoLayerGCN encoder + DropEdge during training. """
def __init__( self, hidden_dim: int = 64, num_classes: int = 2, dropout: float = 0.5, readout: str = "attention", drop_edge_p: float = 0.1, in_features: int = 1, ): super().__init__() if readout not in {"mean", "attention"}: raise ValueError("readout must be 'mean' or 'attention'")
self.encoder = GraphTemporalEncoder(hidden_dim=hidden_dim, dropout=min(dropout, 0.2), in_features=in_features) self.readout = readout self.attention = AttentionReadout(hidden_dim) if readout == "attention" else None self.head = make_classifier_head(hidden_dim, num_classes, dropout) self.drop_edge_p = drop_edge_p
def forward( self, bold_windows: torch.Tensor, adj: torch.Tensor, return_attention: bool = False, ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor | None]: # DropEdge: applied before Laplacian normalisation, training only adj = drop_edge(adj, p=self.drop_edge_p, training=self.training) adj_norm = calculate_laplacian_with_self_loop(adj) node_embeddings = self.encoder(bold_windows, adj_norm) pooled, attention_weights = graph_readout(node_embeddings, self.attention) logits = self.head(pooled) if return_attention: return logits, attention_weights return logits
class GraphOnlyClassifier(nn.Module): """GCN baseline — each ROI's average window signal as node input.
v2: upgraded to TwoLayerGCN with residual + DropEdge. """
def __init__( self, hidden_dim: int = 64, num_classes: int = 2, dropout: float = 0.5, readout: str = "attention", drop_edge_p: float = 0.1, ): super().__init__() if readout not in {"mean", "attention"}: raise ValueError("readout must be 'mean' or 'attention'")
self.gcn = TwoLayerGCN(1, hidden_dim, dropout=min(dropout, 0.1)) self.norm = nn.LayerNorm(hidden_dim) self.dropout = nn.Dropout(dropout) self.attention = AttentionReadout(hidden_dim) if readout == "attention" else None self.head = make_classifier_head(hidden_dim, num_classes, dropout) self.drop_edge_p = drop_edge_p
def forward( self, bold_windows: torch.Tensor, adj: torch.Tensor, return_attention: bool = False, ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor | None]: adj = drop_edge(adj, p=self.drop_edge_p, training=self.training) adj_norm = calculate_laplacian_with_self_loop(adj) if adj_norm.dim() == 4: adj_norm = adj_norm.mean(dim=1) x = bold_windows.mean(dim=1).unsqueeze(-1) # (B, N, 1) x = self.dropout(self.norm(self.gcn(x, adj_norm))) pooled, attention_weights = graph_readout(x, self.attention) logits = self.head(pooled) if return_attention: return logits, attention_weights return logits
class TemporalGRUClassifier(nn.Module): """Temporal baseline — GRU over ROI vectors, no graph message passing."""
def __init__( self, hidden_dim: int = 64, num_classes: int = 2, dropout: float = 0.5, ): super().__init__() self.input_proj = nn.LazyLinear(hidden_dim) self.gru = nn.GRU(hidden_dim, hidden_dim, batch_first=True) self.norm = nn.LayerNorm(hidden_dim) self.dropout = nn.Dropout(dropout) self.head = make_classifier_head(hidden_dim, num_classes, dropout)
def forward( self, bold_windows: torch.Tensor, adj: torch.Tensor, return_attention: bool = False, ) -> torch.Tensor | tuple[torch.Tensor, None]: x = torch.relu(self.input_proj(bold_windows)) x, _ = self.gru(x) x = self.dropout(self.norm(x[:, -1, :])) logits = self.head(x) if return_attention: return logits, None return logits
class ConnectivityMLPClassifier(nn.Module): """Static FC baseline — upper triangle of adjacency matrix as features."""
def __init__( self, hidden_dim: int = 64, num_classes: int = 2, dropout: float = 0.5, ): super().__init__() self.net = nn.Sequential( nn.LazyLinear(hidden_dim), nn.LayerNorm(hidden_dim), nn.ReLU(), nn.Dropout(dropout), nn.Linear(hidden_dim, hidden_dim), nn.LayerNorm(hidden_dim), nn.ReLU(), nn.Dropout(dropout), nn.Linear(hidden_dim, num_classes), )
@staticmethod def _fc_features(adj: torch.Tensor) -> torch.Tensor: """Extract features from adj tensor (various shapes):
(B, N, N) → (B, N*(N-1)/2) signed mean FC upper triangle (B, 2, N, N) → (B, N*(N-1)) mean FC || std FC concatenated (B, 1, K) → (B, K) pre-computed PCA features (pass-through) (B, W, N, N) → (B, N*(N-1)/2) dynamic seq: averaged over windows first """ if adj.dim() == 3: if adj.size(1) == 1: # PCA projection already computed in dataset — just flatten return adj.squeeze(1) # (B, K) # (B, N, N) — standard case row, col = torch.triu_indices(adj.size(-2), adj.size(-1), offset=1, device=adj.device) return adj[:, row, col] # (B, 19900)
if adj.dim() == 4: if adj.size(1) == 2: # [mean_fc, std_fc] channels row, col = torch.triu_indices(adj.size(-2), adj.size(-1), offset=1, device=adj.device) x_mean = adj[:, 0, row, col] x_std = adj[:, 1, row, col] return torch.cat([x_mean, x_std], dim=-1) # (B, 2*19900) # Dynamic window sequence: average then extract adj = adj.mean(dim=1) # (B, N, N) row, col = torch.triu_indices(adj.size(-2), adj.size(-1), offset=1, device=adj.device) return adj[:, row, col]
raise ValueError(f"Unexpected adj shape: {tuple(adj.shape)}")
def forward( self, bold_windows: torch.Tensor, adj: torch.Tensor, return_attention: bool = False, ) -> torch.Tensor | tuple[torch.Tensor, None]: x = self._fc_features(adj) logits = self.net(x) if return_attention: return logits, None return logits
class BrainModeNetwork(nn.Module): """ Novel architecture: Brain Mode Network (BMN).
Learns K 'brain modes' — directions in ROI space (v_k ∈ R^N). Projects the N×N FC matrix into a compact K×K 'mode interaction matrix':
M_kl = v_k^T · FC · v_l
Diagonal M_kk measures connectivity energy along mode k (Rayleigh quotient). Off-diagonal M_kl captures cross-mode coupling between networks.
With K=16 modes and N=200 ROIs: 136 features instead of 19,900. Inductive bias: each mode can specialize to a brain network community (e.g. DMN, FPN, SMN) — the model learns which communities matter for ASD.
Orthogonality regularization keeps modes diverse (callable via orthogonality_loss(), weight controlled externally in the training task). """
def __init__( self, num_nodes: int, num_modes: int = 16, hidden_dim: int = 64, num_classes: int = 2, dropout: float = 0.5, mode_init: torch.Tensor | None = None, ): super().__init__() self.num_modes = num_modes self.num_nodes = num_nodes
# Learnable modes: K × N — default initialization is near-orthonormal via QR. # Caller may pass a (K, N) tensor from discriminative_init() instead. if mode_init is not None: modes_init = mode_init.clone().float() else: modes_init_np = torch.randn(num_nodes, num_modes) Q, _ = torch.linalg.qr(modes_init_np) # (N, K) orthonormal columns modes_init = Q.T.contiguous() # (K, N) self.modes = nn.Parameter(modes_init)
# Features: K(K+1)/2 from static M + K from temporal std(A_k) num_fc_features = num_modes * (num_modes + 1) // 2 num_total_features = num_fc_features + num_modes # static + dynamic
self.classifier = nn.Sequential( nn.LayerNorm(num_total_features), nn.Linear(num_total_features, hidden_dim), nn.ReLU(), nn.Dropout(dropout), nn.Linear(hidden_dim, num_classes), )
def forward( self, bold_windows: torch.Tensor, adj: torch.Tensor, return_attention: bool = False, ) -> torch.Tensor | tuple[torch.Tensor, None]: # adj: (B, N, N) signed FC matrix; also accept (B, W, N, N) → avg over W if adj.dim() == 4: adj = adj.mean(dim=1) # (B, N, N)
# ── Static stream: mode interaction matrix ────────────────────────── # M_kl = v_k^T · FC · v_l → (B, K, K) M = torch.einsum('kn,bnm,lm->bkl', self.modes, adj, self.modes)
# Extract upper triangle (including diagonal): K(K+1)/2 features r, c = torch.triu_indices(self.num_modes, self.num_modes, offset=0, device=adj.device) fc_features = M[:, r, c] # (B, K(K+1)/2)
# ── Dynamic stream: temporal variability of mode activity ─────────── # A_k(t) = v_k · bold(t) → A: (B, W, K) # std(A_k) captures how much each network fluctuates over time. # This is genuinely new information not present in static mean FC. A = torch.einsum('kn,bwn->bwk', self.modes, bold_windows) # (B, W, K) dyn_features = A.std(dim=1) # (B, K)
features = torch.cat([fc_features, dyn_features], dim=-1) # (B, K(K+1)/2+K)
logits = self.classifier(features) if return_attention: return logits, None return logits
def orthogonality_loss(self) -> torch.Tensor: """Penalise non-orthonormal modes: ||V_norm @ V_norm^T - I||_F^2 / K^2.
Encourages each mode to capture a distinct connectivity direction. Dividing by K^2 keeps the loss scale independent of num_modes. """ V_norm = self.modes / (self.modes.norm(dim=1, keepdim=True) + 1e-8) gram = V_norm @ V_norm.T # (K, K) I = torch.eye(self.num_modes, device=gram.device, dtype=gram.dtype) return ((gram - I) ** 2).mean()
@staticmethod def discriminative_init( train_fc_asd: "np.ndarray", train_fc_td: "np.ndarray", num_modes: int, ) -> "torch.Tensor": """Initialize modes from SVD of the ASD-TD mean FC difference matrix.
The k-th left singular vector of (mean_FC_ASD − mean_FC_TD) is the k-th most discriminative direction in ROI space — the direction along which the two classes differ most. Starting here gives the optimizer a head start and reduces the number of epochs needed to learn discriminative modes.
Parameters ---------- train_fc_asd : (n_asd, N, N) FC matrices for ASD training subjects train_fc_td : (n_td, N, N) FC matrices for TD training subjects num_modes : K — number of singular vectors to keep
Returns ------- modes : (K, N) float32 tensor — orthonormal initial modes """ import numpy as np
mu_asd = train_fc_asd.mean(axis=0) # (N, N) mu_td = train_fc_td.mean(axis=0) # (N, N) delta = mu_asd - mu_td # ASD-TD difference
# SVD of the difference matrix: left singular vectors are ROI directions # that best explain the connectivity difference between groups. U, _, _ = np.linalg.svd(delta, full_matrices=True)
K = min(num_modes, U.shape[1]) modes = U[:, :K].T.astype(np.float32) # (K, N)
# If K > available singular vectors (shouldn't happen for N=200, K<<200), # pad with QR-orthogonalized random directions if num_modes > K: extra = np.random.randn(num_modes - K, U.shape[0]).astype(np.float32) for i in range(len(extra)): for row in modes: extra[i] -= np.dot(extra[i], row) * row n = np.linalg.norm(extra[i]) if n > 1e-8: extra[i] /= n modes = np.concatenate([modes, extra], axis=0)
return torch.from_numpy(modes)
class AdversarialBrainModeNetwork(nn.Module): """Brain Mode Network with adversarial site deconfounding.
Combines the compact mode-interaction representation of BrainModeNetwork with the Gradient Reversal Layer (GRL) of Ganin et al. 2016 to push the learned modes towards site-invariant directions.
Architecture: bold_windows, FC → mode interaction M_kl = v_k^T �� FC · v_l (K×K) → flatten upper triangle + temporal std (K(K+1)/2 + K features) → shared_encoder (MLP) ↙ ↘ asd_head grl(α) → site_head (minimize ASD CE) (modes unlearn scanner fingerprint)
The discriminative_init() classmethod inherited from BrainModeNetwork still applies — we start from ASD-TD difference directions and then adversarially remove site confounds while preserving diagnosis signal. """
def __init__( self, num_nodes: int, num_modes: int = 32, hidden_dim: int = 64, num_classes: int = 2, num_sites: int = 17, dropout: float = 0.5, mode_init: "torch.Tensor | None" = None, ): super().__init__() self.num_modes = num_modes self.num_nodes = num_nodes
# Shared mode parameters (same as BrainModeNetwork) if mode_init is not None: modes_init = mode_init.clone().float() else: modes_init_np = torch.randn(num_nodes, num_modes) Q, _ = torch.linalg.qr(modes_init_np) modes_init = Q.T.contiguous() self.modes = nn.Parameter(modes_init)
num_fc_features = num_modes * (num_modes + 1) // 2 num_total_features = num_fc_features + num_modes # static + dynamic
# Shared encoder self.encoder = nn.Sequential( nn.LayerNorm(num_total_features), nn.Linear(num_total_features, hidden_dim), nn.ReLU(), nn.Dropout(dropout), nn.Linear(hidden_dim, hidden_dim), nn.LayerNorm(hidden_dim), nn.ReLU(), nn.Dropout(dropout), ) # ASD head self.asd_head = nn.Linear(hidden_dim, num_classes)
# Adversarial site branch self.grl = GradientReversal(alpha=0.0) self.site_head = nn.Sequential( nn.Linear(hidden_dim, hidden_dim // 2), nn.ReLU(), nn.Linear(hidden_dim // 2, num_sites), )
def _encode(self, bold_windows: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: """Compute mode features and pass through shared encoder.""" if adj.dim() == 4: adj = adj.mean(dim=1)
M = torch.einsum('kn,bnm,lm->bkl', self.modes, adj, self.modes) r, c = torch.triu_indices(self.num_modes, self.num_modes, offset=0, device=adj.device) fc_features = M[:, r, c]
A = torch.einsum('kn,bwn->bwk', self.modes, bold_windows) dyn_features = A.std(dim=1)
features = torch.cat([fc_features, dyn_features], dim=-1) return self.encoder(features)
def forward( self, bold_windows: torch.Tensor, adj: torch.Tensor, return_site_logits: bool = False, ) -> "torch.Tensor | tuple[torch.Tensor, torch.Tensor]": h = self._encode(bold_windows, adj) asd_logits = self.asd_head(h) if return_site_logits: site_logits = self.site_head(self.grl(h)) return asd_logits, site_logits return asd_logits
def orthogonality_loss(self) -> torch.Tensor: """Identical to BrainModeNetwork.orthogonality_loss().""" V_norm = self.modes / (self.modes.norm(dim=1, keepdim=True) + 1e-8) gram = V_norm @ V_norm.T I = torch.eye(self.num_modes, device=gram.device, dtype=gram.dtype) return ((gram - I) ** 2).mean()
# Expose discriminative_init as a static method (same logic as BrainModeNetwork) discriminative_init = BrainModeNetwork.discriminative_init
class AdversarialConnectivityMLP(nn.Module): """FC-based classifier with adversarial site deconfounding (Ganin et al. 2016).
Architecture: FC upper triangle (signed) → shared_encoder # learns site-invariant features ↙ ↘ asd_head grl(α) → site_head (minimize ASD CE) (encoder maximises site CE via reversed grads)
During training the encoder is pulled in two directions: - Minimise ASD classification loss (learn diagnosis signal) - Maximise site classification loss (unlearn scanner fingerprint)
alpha is annealed 0→1 via ganin_alpha() so site deconfounding ramps up gradually after the ASD signal is first established. """
def __init__( self, hidden_dim: int = 256, num_classes: int = 2, num_sites: int = 17, dropout: float = 0.5, ): super().__init__() # Shared encoder — LazyLinear handles variable FC input size self.encoder = nn.Sequential( nn.LazyLinear(hidden_dim), nn.LayerNorm(hidden_dim), nn.ReLU(), nn.Dropout(dropout), nn.Linear(hidden_dim, hidden_dim), nn.LayerNorm(hidden_dim), nn.ReLU(), nn.Dropout(dropout), ) # ASD classification head self.asd_head = nn.Linear(hidden_dim, num_classes)
# Site adversarial branch self.grl = GradientReversal(alpha=0.0) # alpha set externally each epoch self.site_head = nn.Sequential( nn.Linear(hidden_dim, hidden_dim // 2), nn.ReLU(), nn.Linear(hidden_dim // 2, num_sites), )
def forward( self, bold_windows: torch.Tensor, adj: torch.Tensor, return_site_logits: bool = False, ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: x = ConnectivityMLPClassifier._fc_features(adj)
features = self.encoder(x) asd_logits = self.asd_head(features)
if return_site_logits: site_logits = self.site_head(self.grl(features)) return asd_logits, site_logits return asd_logits
# ---------------------------------------------------------------------------# Factory# ---------------------------------------------------------------------------
def build_model( model_name: str, hidden_dim: int = 64, num_classes: int = 2, num_sites: int = 1, num_nodes: int = 200, num_modes: int = 16, dropout: float = 0.5, readout: str = "attention", drop_edge_p: float = 0.1, mode_init: "torch.Tensor | None" = None, in_features: int = 1,) -> nn.Module: if model_name == "graph_temporal": return BrainGCNClassifier(hidden_dim, num_classes, dropout, readout, drop_edge_p, in_features=in_features) if model_name == "gcn": return GraphOnlyClassifier(hidden_dim, num_classes, dropout, readout, drop_edge_p) if model_name == "gru": return TemporalGRUClassifier(hidden_dim, num_classes, dropout) if model_name == "fc_mlp": return ConnectivityMLPClassifier(hidden_dim, num_classes, dropout) if model_name == "adv_fc_mlp": return AdversarialConnectivityMLP(hidden_dim, num_classes, num_sites, dropout) if model_name == "dynamic_fc_attn": from brain_gcn.models.dynamic_fc import DynamicFCAttention return DynamicFCAttention( num_rois=num_nodes, hidden_dim=hidden_dim, dropout=dropout, ) if model_name == "brain_mode": return BrainModeNetwork(num_nodes, num_modes, hidden_dim, num_classes, dropout, mode_init=mode_init) if model_name == "adv_brain_mode": return AdversarialBrainModeNetwork(num_nodes, num_modes, hidden_dim, num_classes, num_sites, dropout, mode_init=mode_init) # Advanced models — lazy import to avoid circular dependency from brain_gcn.models.advanced_models import ( GATClassifier, TransformerClassifier, CNN3DClassifier, GraphSAGEClassifier, ) if model_name == "gat": return GATClassifier(hidden_dim, dropout=dropout) if model_name == "transformer": return TransformerClassifier(hidden_dim, dropout=dropout) if model_name == "cnn3d": return CNN3DClassifier(hidden_dim, dropout=dropout) if model_name == "graphsage": return GraphSAGEClassifier(hidden_dim, dropout=dropout) raise ValueError(f"Unknown model_name: {model_name}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100"""Dynamic FC Temporal Attention model for ASD/TD classification.
Architecture (STAGIN-inspired, simplified): Input : (B, W, N) — per-window ROI connectivity strength (mean |FC| per ROI) Step 1 : Linear projection N → H Step 2 : Learnable positional encoding over W time steps Step 3 : Transformer encoder (multi-head self-attention over windows) Step 4 : Attention-weighted pooling over W → subject embedding (H,) Step 5 : MLP classifier → 2
Why this works: ASD shows altered *dynamic* connectivity — not just different mean FC but different temporal patterns of connectivity fluctuation across brain states. The self-attention learns which window combinations are most discriminative."""
from __future__ import annotations
import torchimport torch.nn.functional as Ffrom torch import nn
class DynamicFCAttention(nn.Module):
def __init__( self, num_rois: int = 200, max_windows: int = 30, hidden_dim: int = 128, num_heads: int = 4, num_layers: int = 2, dropout: float = 0.5, num_classes: int = 2, ): super().__init__() assert hidden_dim % num_heads == 0, "hidden_dim must be divisible by num_heads"
# Project ROI connectivity strengths to hidden dim self.input_proj = nn.Sequential( nn.Linear(num_rois, hidden_dim), nn.LayerNorm(hidden_dim), nn.ReLU(), nn.Dropout(dropout * 0.5), )
# Learnable positional encoding — one vector per window self.pos_embed = nn.Parameter(torch.randn(1, max_windows, hidden_dim) * 0.02)
# Transformer encoder: self-attention over time windows encoder_layer = nn.TransformerEncoderLayer( d_model=hidden_dim, nhead=num_heads, dim_feedforward=hidden_dim * 2, dropout=dropout * 0.5, batch_first=True, norm_first=True, # pre-norm for stability ) self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
# Attention pooling over time: learn which windows matter self.time_attn = nn.Linear(hidden_dim, 1)
# Classifier head self.head = nn.Sequential( nn.Linear(hidden_dim, hidden_dim // 2), nn.LayerNorm(hidden_dim // 2), nn.ReLU(), nn.Dropout(dropout), nn.Linear(hidden_dim // 2, num_classes), )
def forward( self, bold_windows: torch.Tensor, adj: torch.Tensor | None = None, # unused — kept for interface compatibility return_attention: bool = False, ) -> torch.Tensor: # bold_windows: (B, W, N) — mean |FC| per ROI per time window B, W, N = bold_windows.shape
# Project each window's ROI features to hidden dim x = self.input_proj(bold_windows) # (B, W, H)
# Add positional encoding x = x + self.pos_embed[:, :W, :]
# Self-attention over time windows x = self.transformer(x) # (B, W, H)
# Attention-weighted pooling: which windows are most discriminative? attn = torch.softmax(self.time_attn(x).squeeze(-1), dim=1) # (B, W) embedding = (x * attn.unsqueeze(-1)).sum(dim=1) # (B, H)
logits = self.head(embedding)
if return_attention: return logits, attn return logits123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297"""Brain Connectivity Masked Autoencoder (BC-MAE).
Architecture (He et al. MAE 2022, adapted for temporal FC windows):
Pre-training ───────────── Input : (B, W, N) — per-window ROI connectivity strengths (mean |FC| per window) Mask : random 50% of W windows are hidden Encoder: Transformer on visible windows only → (B, W_vis, H) Decoder: Lightweight Transformer on all positions (visible + mask tokens) → reconstruction head → (B, W, N) Loss : MSE on masked windows only
Fine-tuning ──────────── Encoder (loaded from pre-training, optionally frozen) + attention pooling over all W windows + MLP classifier → (B, 2)"""
from __future__ import annotations
import torchimport torch.nn.functional as Ffrom torch import nn
# ---------------------------------------------------------------------------# Shared encoder# ---------------------------------------------------------------------------
class BrainFCEncoder(nn.Module): """Transformer encoder operating on visible FC windows.
Each time window's ROI connectivity profile (N-dim) is treated as a "patch" — analogous to image patches in ViT/MAE. """
def __init__( self, num_rois: int = 200, num_windows: int = 30, hidden_dim: int = 128, num_heads: int = 4, num_layers: int = 4, dropout: float = 0.1, ): super().__init__() self.hidden_dim = hidden_dim
# Project each window's ROI features to hidden dim self.patch_embed = nn.Linear(num_rois, hidden_dim)
# Learnable positional embedding — one per window position self.pos_embed = nn.Parameter(torch.zeros(1, num_windows, hidden_dim)) nn.init.trunc_normal_(self.pos_embed, std=0.02)
encoder_layer = nn.TransformerEncoderLayer( d_model=hidden_dim, nhead=num_heads, dim_feedforward=hidden_dim * 4, dropout=dropout, batch_first=True, norm_first=True, ) self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=num_layers) self.norm = nn.LayerNorm(hidden_dim)
def forward( self, x: torch.Tensor, ids_keep: torch.Tensor | None = None, ) -> torch.Tensor: """ Parameters ---------- x : (B, W_visible, N) visible windows ids_keep : (B, W_visible) original positions of visible windows """ B, W_vis, N = x.shape
# Project patches x = self.patch_embed(x) # (B, W_vis, H)
# Add positional embeddings at the original positions if ids_keep is not None: pos = self.pos_embed.expand(B, -1, -1) # (B, W_all, H) pos_vis = torch.gather( pos, 1, ids_keep.unsqueeze(-1).expand(-1, -1, self.hidden_dim) # (B, W_vis, H) ) else: pos_vis = self.pos_embed[:, :W_vis, :]
x = x + pos_vis x = self.norm(self.transformer(x)) return x # (B, W_vis, H)
# ---------------------------------------------------------------------------# MAE (pre-training)# ---------------------------------------------------------------------------
class BrainMAE(nn.Module): """Masked Autoencoder for brain FC windows."""
def __init__( self, num_rois: int = 200, num_windows: int = 30, hidden_dim: int = 128, decoder_dim: int = 64, num_heads: int = 4, encoder_layers: int = 4, decoder_layers: int = 2, dropout: float = 0.1, mask_ratio: float = 0.5, ): super().__init__() self.num_windows = num_windows self.num_rois = num_rois self.mask_ratio = mask_ratio self.hidden_dim = hidden_dim self.decoder_dim = decoder_dim
# Encoder (shared with fine-tuning) self.encoder = BrainFCEncoder( num_rois=num_rois, num_windows=num_windows, hidden_dim=hidden_dim, num_heads=num_heads, num_layers=encoder_layers, dropout=dropout, )
# Project encoder output to decoder dim self.enc_to_dec = nn.Linear(hidden_dim, decoder_dim, bias=False)
# Learnable mask token (broadcast across masked positions) self.mask_token = nn.Parameter(torch.zeros(1, 1, decoder_dim)) nn.init.trunc_normal_(self.mask_token, std=0.02)
# Decoder positional embedding (all W positions) self.decoder_pos_embed = nn.Parameter(torch.zeros(1, num_windows, decoder_dim)) nn.init.trunc_normal_(self.decoder_pos_embed, std=0.02)
# Lightweight decoder dec_layer = nn.TransformerEncoderLayer( d_model=decoder_dim, nhead=max(1, decoder_dim // 32), dim_feedforward=decoder_dim * 4, dropout=dropout, batch_first=True, norm_first=True, ) self.decoder = nn.TransformerEncoder(dec_layer, num_layers=decoder_layers) self.decoder_norm = nn.LayerNorm(decoder_dim)
# Reconstruction head: predict ROI connectivity for each window self.recon_head = nn.Linear(decoder_dim, num_rois)
# ------------------------------------------------------------------ def _random_masking( self, x: torch.Tensor ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """Randomly mask windows. Returns visible subset, binary mask, restore indices.""" B, W, _ = x.shape num_keep = int(W * (1 - self.mask_ratio))
# Random shuffle per sample noise = torch.rand(B, W, device=x.device) ids_shuffle = torch.argsort(noise, dim=1) ids_restore = torch.argsort(ids_shuffle, dim=1)
ids_keep = ids_shuffle[:, :num_keep] # (B, num_keep) x_vis = torch.gather( x, 1, ids_keep.unsqueeze(-1).expand(-1, -1, x.shape[-1]) # (B, num_keep, N) )
# Binary mask: 1 = masked, 0 = visible mask = torch.ones(B, W, device=x.device) mask[:, :num_keep] = 0 mask = torch.gather(mask, 1, ids_restore)
return x_vis, mask, ids_restore, ids_keep
def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: """Forward pass for pre-training.
Returns ------- loss : scalar MSE on masked windows mask : (B, W) binary mask (1=masked) for logging """ B, W, N = x.shape
# Mask x_vis, mask, ids_restore, ids_keep = self._random_masking(x)
# Encode visible enc = self.encoder(x_vis, ids_keep=ids_keep) # (B, num_keep, H) enc = self.enc_to_dec(enc) # (B, num_keep, D)
# Decode: reconstruct all W positions # Fill masked positions with mask token num_keep = enc.shape[1] num_mask = W - num_keep mask_tokens = self.mask_token.expand(B, num_mask, -1)
# Concatenate visible encoded + mask tokens, then unshuffle full = torch.cat([enc, mask_tokens], dim=1) # (B, W, D) full = torch.gather( full, 1, ids_restore.unsqueeze(-1).expand(-1, -1, self.decoder_dim) )
# Add decoder positional embeddings and decode full = full + self.decoder_pos_embed dec = self.decoder_norm(self.decoder(full)) # (B, W, D)
# Reconstruct pred = self.recon_head(dec) # (B, W, N)
# MSE loss on masked windows only loss = (pred - x).pow(2).mean(dim=-1) # (B, W) loss = (loss * mask).sum() / (mask.sum() + 1e-8)
return loss, mask
def encode_all(self, x: torch.Tensor) -> torch.Tensor: """Encode all W windows (no masking) for downstream tasks.""" return self.encoder(x) # (B, W, H)
# ---------------------------------------------------------------------------# Fine-tuning classifier# ---------------------------------------------------------------------------
class BrainFCClassifier(nn.Module): """ASD/TD classifier with pre-trained BC-MAE encoder.
Encoder can be frozen (linear probing) or fine-tuned end-to-end. """
def __init__( self, encoder: BrainFCEncoder, hidden_dim: int = 128, num_classes: int = 2, dropout: float = 0.5, freeze_encoder: bool = True, ): super().__init__() self.encoder = encoder self.freeze_encoder = freeze_encoder
if freeze_encoder: for p in self.encoder.parameters(): p.requires_grad_(False)
H = hidden_dim # Attention pooling over time: which windows discriminate ASD? self.time_attn = nn.Linear(H, 1)
# Classifier head self.head = nn.Sequential( nn.LayerNorm(H), nn.Linear(H, H // 2), nn.GELU(), nn.Dropout(dropout), nn.Linear(H // 2, num_classes), )
def forward( self, x: torch.Tensor, adj: torch.Tensor | None = None, # kept for interface compatibility ) -> torch.Tensor: # x: (B, W, N) if self.freeze_encoder: with torch.no_grad(): enc = self.encoder(x) # (B, W, H) else: enc = self.encoder(x)
# Attention-weighted pooling over time attn = torch.softmax(self.time_attn(enc).squeeze(-1), dim=1) # (B, W) pooled = (enc * attn.unsqueeze(-1)).sum(dim=1) # (B, H)
return self.head(pooled)
def unfreeze_encoder(self) -> None: for p in self.encoder.parameters(): p.requires_grad_(True) self.freeze_encoder = False12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970"""Population-level GCN for subject-level ASD/TD classification.
All subjects are nodes in a single graph — transductive setting.The model sees all node features (including unlabeled val/test subjects)during forward passes; loss is masked to training nodes only."""
from __future__ import annotations
import torchimport torch.nn.functional as Ffrom torch import nn
class GraphConv(nn.Module): """Single graph convolution: linear projection after neighborhood aggregation."""
def __init__(self, in_dim: int, out_dim: int, bias: bool = True): super().__init__() self.linear = nn.Linear(in_dim, out_dim, bias=bias)
def forward(self, x: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: # adj: pre-normalized (N, N); x: (N, in_dim) return self.linear(adj @ x)
class PopulationGCN(nn.Module): """2-layer GCN on the subject population graph.
Architecture ============ Input → Dropout → GC1 → LayerNorm → ReLU → Dropout → GC2 → LayerNorm → ReLU → Dropout → Linear → logits (N, num_classes)
Depth 2 is sufficient: each node aggregates 2-hop neighbors, covering subjects with similar age+sex across the whole cohort. """
def __init__( self, in_dim: int, hidden_dim: int = 64, num_classes: int = 2, dropout: float = 0.5, ): super().__init__() self.gc1 = GraphConv(in_dim, hidden_dim) self.gc2 = GraphConv(hidden_dim, hidden_dim) self.norm1 = nn.LayerNorm(hidden_dim) self.norm2 = nn.LayerNorm(hidden_dim) self.head = nn.Linear(hidden_dim, num_classes) self.drop = nn.Dropout(dropout)
def forward(self, x: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: x = self.drop(x) x = F.relu(self.norm1(self.gc1(x, adj))) x = self.drop(x) x = F.relu(self.norm2(self.gc2(x, adj))) x = self.drop(x) return self.head(x) # (N, num_classes)
@torch.no_grad() def embed(self, x: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: """Return post-GC2 embeddings for t-SNE / analysis.""" x = self.gc1(x, adj) x = F.relu(self.norm1(x)) x = self.gc2(x, adj) return F.relu(self.norm2(x))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313"""Model registry for centralized model access and configuration.
Simplifies model loading, configuration, and comparison."""
from __future__ import annotations
import argparseimport loggingfrom typing import Any, Callable
import torchfrom torch import nn
log = logging.getLogger(__name__)
# Import all modelsdef _lazy_import_models(): """Lazy import to avoid circular dependencies.""" from brain_gcn.models.brain_gcn import BrainGCNClassifier, GraphOnlyClassifier, TemporalGRUClassifier, ConnectivityMLPClassifier from brain_gcn.models.advanced_models import ( GATClassifier, TransformerClassifier, CNN3DClassifier, GraphSAGEClassifier, ) return { # Original models 'graph_temporal': BrainGCNClassifier, 'gcn': GraphOnlyClassifier, 'gru': TemporalGRUClassifier, 'fc_mlp': ConnectivityMLPClassifier, # New models 'gat': GATClassifier, 'transformer': TransformerClassifier, 'cnn3d': CNN3DClassifier, 'graphsage': GraphSAGEClassifier, }
class ModelConfig: """Configuration for model instantiation."""
def __init__( self, model_name: str, hidden_dim: int = 64, dropout: float = 0.5, num_heads: int = 4, num_layers: int = 2, readout: str = "attention", drop_edge_p: float = 0.1, **kwargs ): """ Parameters ---------- model_name : str Model identifier (must be in registry) hidden_dim : int Hidden dimension size dropout : float Dropout probability num_heads : int Number of attention heads (for GAT, Transformer) num_layers : int Number of layers (for Transformer) readout : str Readout method ("attention" or "mean") drop_edge_p : float Edge dropout probability (for GCN-based models) **kwargs Additional arguments """ self.model_name = model_name self.hidden_dim = hidden_dim self.dropout = dropout self.num_heads = num_heads self.num_layers = num_layers self.readout = readout self.drop_edge_p = drop_edge_p self.kwargs = kwargs
def to_dict(self) -> dict[str, Any]: """Export configuration as dictionary.""" return { 'model_name': self.model_name, 'hidden_dim': self.hidden_dim, 'dropout': self.dropout, 'num_heads': self.num_heads, 'num_layers': self.num_layers, 'readout': self.readout, 'drop_edge_p': self.drop_edge_p, **self.kwargs }
@classmethod def from_dict(cls, config_dict: dict) -> ModelConfig: """Load configuration from dictionary.""" config_dict = dict(config_dict) # don't mutate caller's dict model_name = config_dict.pop('model_name') return cls(model_name, **config_dict)
class ModelRegistry: """Central registry for all available models."""
_models = None _configs = { 'graph_temporal': { 'display_name': 'Graph-Temporal GCN', 'description': 'Graph projection per window + GRU temporal encoder', 'requires': ['bold_windows', 'adj'], 'parameters': ['hidden_dim', 'dropout', 'readout', 'drop_edge_p'], }, 'gcn': { 'display_name': 'Graph-Only (GCN)', 'description': 'GCN baseline over ROI average signals', 'requires': ['bold_windows', 'adj'], 'parameters': ['hidden_dim', 'dropout', 'drop_edge_p'], }, 'gru': { 'display_name': 'Temporal-Only (GRU)', 'description': 'GRU baseline without graph structure', 'requires': ['bold_windows'], 'parameters': ['hidden_dim', 'dropout'], }, 'fc_mlp': { 'display_name': 'Connectivity MLP', 'description': 'Static FC adjacency MLP (requires --no-use_population_adj)', 'requires': ['adj'], 'parameters': ['hidden_dim', 'dropout'], }, 'gat': { 'display_name': 'Graph Attention Network', 'description': 'Multi-head graph attention mechanism', 'requires': ['bold_windows', 'adj'], 'parameters': ['hidden_dim', 'dropout', 'num_heads'], }, 'transformer': { 'display_name': 'Transformer Encoder', 'description': 'Transformer-based temporal encoder', 'requires': ['bold_windows'], 'parameters': ['hidden_dim', 'dropout', 'num_heads', 'num_layers'], }, 'cnn3d': { 'display_name': '3D-CNN', 'description': '3D convolution for spatiotemporal features', 'requires': ['bold_windows', 'fc_windows'], 'parameters': ['hidden_dim', 'dropout'], }, 'graphsage': { 'display_name': 'GraphSAGE', 'description': 'Sampling and aggregating graph convolution', 'requires': ['bold_windows', 'adj'], 'parameters': ['hidden_dim', 'dropout'], }, }
@classmethod def get_models(cls) -> dict[str, type]: """Get all available models.""" if cls._models is None: cls._models = _lazy_import_models() return cls._models
@classmethod def get_model_class(cls, model_name: str) -> type: """Get model class by name.""" models = cls.get_models() if model_name not in models: available = ', '.join(models.keys()) raise ValueError( f"Unknown model: {model_name}\nAvailable: {available}" ) return models[model_name]
@classmethod def build_model( cls, config: ModelConfig, **override_kwargs ) -> nn.Module: """Build model instance from config. Parameters ---------- config : ModelConfig Model configuration **override_kwargs Override config parameters Returns ------- nn.Module Instantiated model """ model_class = cls.get_model_class(config.model_name) # Prepare arguments kwargs = { 'hidden_dim': config.hidden_dim, 'dropout': config.dropout, } # Add model-specific parameters if config.model_name in ['graph_temporal', 'gcn', 'graphsage']: kwargs['drop_edge_p'] = config.drop_edge_p if config.model_name == 'graph_temporal': kwargs['readout'] = config.readout if config.model_name in ['gat', 'transformer']: kwargs['num_heads'] = config.num_heads if config.model_name == 'transformer': kwargs['num_layers'] = config.num_layers # Apply overrides kwargs.update(override_kwargs) # Remove unsupported kwargs model_class_init = model_class.__init__ import inspect sig = inspect.signature(model_class_init) valid_kwargs = {k: v for k, v in kwargs.items() if k in sig.parameters} log.info(f"Building {config.model_name} with {valid_kwargs}") return model_class(**valid_kwargs)
@classmethod def list_models(cls) -> list[str]: """List all available models.""" return list(cls._configs.keys())
@classmethod def get_model_info(cls, model_name: str) -> dict: """Get information about a model. Parameters ---------- model_name : str Model name Returns ------- dict Model metadata """ if model_name not in cls._configs: raise ValueError(f"Unknown model: {model_name}") return cls._configs[model_name]
@classmethod def print_registry(cls) -> None: """Print all models and their descriptions.""" print("\n" + "=" * 80) print("AVAILABLE MODELS") print("=" * 80) for model_name in cls.list_models(): info = cls.get_model_info(model_name) print(f"\n{model_name:15} | {info['display_name']}") print(f"{'':15} | {info['description']}") print(f"{'':15} | Requires: {', '.join(info['requires'])}")
def add_model_choice_argument(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: """Add model choice argument to parser. Parameters ---------- parser : argparse.ArgumentParser Argument parser Returns ------- argparse.ArgumentParser Updated parser """ available_models = ModelRegistry.list_models() parser.add_argument( '--model_name', type=str, choices=available_models, default='graph_temporal', help=f"Model architecture. Available: {', '.join(available_models)}", ) parser.add_argument( '--num_heads', type=int, default=4, help="Number of attention heads (for GAT, Transformer)", ) parser.add_argument( '--num_layers', type=int, default=2, help="Number of layers (for Transformer)", ) return parser
if __name__ == "__main__": # Print all available models ModelRegistry.print_registry()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288"""Population Graph GCN — training entry point.
Architecture: Parisot et al. 2017/2018 (subject nodes, phenotypic edges). - Nodes : subjects (N ≈ 1102) - Features: PCA-reduced FC upper triangle (D=256) - Edges : sex_match × age_gaussian_similarity > threshold - Training: transductive — all nodes in graph, loss masked to train split
Usage----- python -m brain_gcn.population_main \\ --data_dir data \\ --pheno_csv data/raw/abide_s3/phenotypic.csv \\ --use_combat \\ --n_pca 256 \\ --hidden_dim 64 \\ --dropout 0.5 \\ --lr 5e-4 \\ --weight_decay 1e-3 \\ --epochs 500 \\ --seed 42"""
from __future__ import annotations
import argparseimport randomfrom pathlib import Path
import numpy as npimport torchimport torch.nn.functional as Ffrom sklearn.model_selection import StratifiedShuffleSplitfrom torchmetrics.classification import BinaryAUROC, BinaryAccuracy, BinaryRecall, BinarySpecificity, BinaryF1Score
from brain_gcn.models.population_gcn import PopulationGCNfrom brain_gcn.utils.data.population_graph import ( apply_pca, build_population_adj, extract_fc_features, fit_pca, harmonize_combat, load_phenotypic, normalize_adj,)
# ---------------------------------------------------------------------------# Helpers# ---------------------------------------------------------------------------
def seed_everything(seed: int) -> None: random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed_all(seed)
def class_weights(labels: np.ndarray) -> torch.Tensor: n_td = int((labels == 0).sum()) n_asd = int((labels == 1).sum()) total = n_td + n_asd return torch.tensor([total / (2.0 * n_td), total / (2.0 * n_asd)], dtype=torch.float32)
def build_masks(n: int, train_idx, val_idx, test_idx, device): def _mask(idx): m = torch.zeros(n, dtype=torch.bool, device=device) m[idx] = True return m return _mask(train_idx), _mask(val_idx), _mask(test_idx)
# ---------------------------------------------------------------------------# Evaluation# ---------------------------------------------------------------------------
@torch.no_grad()def evaluate(logits: torch.Tensor, labels: torch.Tensor, mask: torch.Tensor): probs = torch.softmax(logits[mask], dim=-1) preds = probs.argmax(dim=-1) tgts = labels[mask]
auc_m = BinaryAUROC() acc_m = BinaryAccuracy() sens_m = BinaryRecall() spec_m = BinarySpecificity() f1_m = BinaryF1Score()
auc = auc_m(probs[:, 1].cpu(), tgts.cpu()).item() acc = acc_m(preds.cpu(), tgts.cpu()).item() sens = sens_m(preds.cpu(), tgts.cpu()).item() spec = spec_m(preds.cpu(), tgts.cpu()).item() f1 = f1_m(preds.cpu(), tgts.cpu()).item() return dict(auc=auc, acc=acc, sens=sens, spec=spec, f1=f1)
# ---------------------------------------------------------------------------# Training loop# ---------------------------------------------------------------------------
def train(args: argparse.Namespace) -> dict: seed_everything(args.seed) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f"Device: {device}")
# ------------------------------------------------------------------ # 1. Data # ------------------------------------------------------------------ processed_dir = Path(args.data_dir) / "processed" pheno = load_phenotypic(args.pheno_csv, processed_dir) print(f"Subjects matched: {len(pheno)} (ASD={pheno['label'].sum()} TD={(pheno['label']==0).sum()})")
subject_ids = pheno["SUB_ID"].tolist() labels_np = pheno["label"].values.astype(np.int64)
# ------------------------------------------------------------------ # 2. Train / val / test split (stratified) # ------------------------------------------------------------------ sss = StratifiedShuffleSplit(n_splits=1, test_size=args.test_ratio, random_state=args.seed) train_val_idx, test_idx = next(sss.split(subject_ids, labels_np))
val_size = args.val_ratio / (1.0 - args.test_ratio) sss2 = StratifiedShuffleSplit(n_splits=1, test_size=val_size, random_state=args.seed) rel_train, rel_val = next(sss2.split(train_val_idx, labels_np[train_val_idx])) train_idx = train_val_idx[rel_train] val_idx = train_val_idx[rel_val]
print(f"Split: train={len(train_idx)} val={len(val_idx)} test={len(test_idx)}")
# ------------------------------------------------------------------ # 3. FC features # ------------------------------------------------------------------ print("Loading FC features …") all_feats = extract_fc_features(processed_dir, subject_ids) # (N, 19900)
if args.use_combat: print("Running ComBat harmonization …") all_feats = harmonize_combat( features=all_feats, sites=pheno["SITE_ID"].tolist(), labels=labels_np, ages=pheno["AGE_AT_SCAN"].values, sexes=pheno["sex_enc"].values, )
# PCA fitted on training subjects only scaler, pca = fit_pca(all_feats[train_idx], n_components=args.n_pca) all_feats_pca = apply_pca(all_feats, scaler, pca) # (N, n_pca)
# ------------------------------------------------------------------ # 4. Population graph # ------------------------------------------------------------------ print("Building population graph …") adj_np = build_population_adj( pheno, threshold=args.graph_threshold, use_site=args.use_site_edges, ) adj_norm = torch.FloatTensor(normalize_adj(adj_np)).to(device)
# ------------------------------------------------------------------ # 5. Tensors # ------------------------------------------------------------------ X = torch.FloatTensor(all_feats_pca).to(device) # (N, D) labels = torch.LongTensor(labels_np).to(device) # (N,) cw = class_weights(labels_np).to(device) N = len(subject_ids) train_mask, val_mask, test_mask = build_masks(N, train_idx, val_idx, test_idx, device)
# ------------------------------------------------------------------ # 6. Model # ------------------------------------------------------------------ model = PopulationGCN( in_dim=X.shape[1], hidden_dim=args.hidden_dim, dropout=args.dropout, ).to(device) print(f"Parameters: {sum(p.numel() for p in model.parameters()):,}")
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts( optimizer, T_0=args.cosine_t0, T_mult=2, eta_min=1e-6 )
# ------------------------------------------------------------------ # 7. Train # ------------------------------------------------------------------ best_val_auc = 0.0 best_state = None patience_left = args.patience
print(f"\n{'ep':>5s} | {'tr_loss':>8s} | {'val_auc':>8s} | {'val_acc':>8s} | {'val_sens':>9s} | {'val_spec':>9s}") print("-" * 60)
for epoch in range(1, args.epochs + 1): # ---- train ---- model.train() optimizer.zero_grad() logits = model(X, adj_norm) loss = F.cross_entropy(logits[train_mask], labels[train_mask], weight=cw) loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0) optimizer.step() scheduler.step()
# ---- validate ---- model.eval() with torch.no_grad(): logits_eval = model(X, adj_norm) val_m = evaluate(logits_eval, labels, val_mask)
if val_m["auc"] > best_val_auc: best_val_auc = val_m["auc"] best_state = {k: v.cpu().clone() for k, v in model.state_dict().items()} patience_left = args.patience else: patience_left -= 1
if epoch % 10 == 0 or epoch == 1: print( f"{epoch:>5d} | {loss.item():>8.4f} | {val_m['auc']:>8.4f} | " f"{val_m['acc']:>8.4f} | {val_m['sens']:>9.4f} | {val_m['spec']:>9.4f}" )
if patience_left <= 0: print(f"\nEarly stop at epoch {epoch}. Best val_auc={best_val_auc:.4f}") break
# ------------------------------------------------------------------ # 8. Test # ------------------------------------------------------------------ model.load_state_dict({k: v.to(device) for k, v in best_state.items()}) model.eval() with torch.no_grad(): logits_final = model(X, adj_norm) test_m = evaluate(logits_final, labels, test_mask)
print(f"\n{'='*60}") print(f"[TEST] auc={test_m['auc']:.4f} acc={test_m['acc']:.4f} " f"sens={test_m['sens']:.4f} spec={test_m['spec']:.4f} f1={test_m['f1']:.4f}") print(f"{'='*60}")
# Save checkpoint ckpt_dir = Path("checkpoints") / "population_gcn" ckpt_dir.mkdir(parents=True, exist_ok=True) ckpt_path = ckpt_dir / f"best_auc{best_val_auc:.3f}.pt" torch.save({"model_state": best_state, "args": vars(args), "test_metrics": test_m}, ckpt_path) print(f"Checkpoint saved: {ckpt_path}")
return test_m
# ---------------------------------------------------------------------------# Entry point# ---------------------------------------------------------------------------
def build_parser() -> argparse.ArgumentParser: p = argparse.ArgumentParser(description="Population Graph GCN for ABIDE ASD classification") p.add_argument("--data_dir", type=str, default="data") p.add_argument("--pheno_csv", type=str, default="data/raw/abide_s3/phenotypic.csv") p.add_argument("--use_combat", action="store_true", help="Apply ComBat site harmonization") p.add_argument("--use_site_edges", action="store_true", help="Include site-match in graph edges") p.add_argument("--n_pca", type=int, default=256) p.add_argument("--graph_threshold", type=float, default=0.5) p.add_argument("--hidden_dim", type=int, default=64) p.add_argument("--dropout", type=float, default=0.5) p.add_argument("--lr", type=float, default=5e-4) p.add_argument("--weight_decay", type=float, default=1e-3) p.add_argument("--cosine_t0", type=int, default=100) p.add_argument("--epochs", type=int, default=500) p.add_argument("--patience", type=int, default=60) p.add_argument("--val_ratio", type=float, default=0.1) p.add_argument("--test_ratio", type=float, default=0.1) p.add_argument("--seed", type=int, default=42) return p
def main() -> None: torch.set_float32_matmul_precision("medium") args = build_parser().parse_args() train(args)
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263"""BC-MAE Pre-training Script.
Self-supervised pre-training on ALL ABIDE subjects (no labels needed).
Input per subject: (W=30, N=200) mean |FC| per ROI per window - Loaded from fc_windows.npz, site-corrected, then mean |FC| per window - Same feature as --use_fc_degree_features in the classification pipeline
Task: BrainMAE masks 50% of windows, reconstructs them from visible ones.Loss: MSE on masked windows only.
Saves: checkpoints/mae/mae-best-*.ckpt (full BrainMAETask checkpoint)
Usage: python -m brain_gcn.pretrain_main \\ --data_dir data \\ --max_epochs 200 \\ --hidden_dim 128 \\ --lr 1e-3
Then fine-tune with: python -m brain_gcn.finetune_main \\ --mae_ckpt checkpoints/mae/mae-best-*.ckpt \\ --data_dir data"""
from __future__ import annotations
import argparsefrom pathlib import Path
import numpy as npimport pytorch_lightning as plimport torchfrom pytorch_lightning.callbacks import EarlyStopping, ModelCheckpointfrom torch.utils.data import DataLoader, Dataset
from brain_gcn.models.mae import BrainMAE
# ---------------------------------------------------------------------------# Dataset# ---------------------------------------------------------------------------
class MAEDataset(Dataset): """All ABIDE subjects → (N, N) full FC matrix for spatial BC-MAE pre-training.
Each subject is represented as N=200 tokens, where token i is ROI i's full connectivity profile (its FC row). The MAE masks 50% of ROIs and reconstructs their FC rows — forcing the encoder to learn which ROIs co-activate. """
def __init__( self, npz_dir: str | Path, site_fc_mean: dict[str, np.ndarray] | None = None, ): self.paths = sorted(Path(npz_dir).glob("*.npz")) if not self.paths: raise FileNotFoundError(f"No .npz files found in {npz_dir}") self.site_fc_mean = site_fc_mean or {}
def __len__(self) -> int: return len(self.paths)
def __getitem__(self, idx: int) -> torch.Tensor: data = np.load(self.paths[idx], allow_pickle=True) site = str(data["site"])
fc = data["mean_fc"].astype(np.float32) # (N, N) if site in self.site_fc_mean: fc = fc - self.site_fc_mean[site]
return torch.FloatTensor(fc) # (N, N) — each row i = ROI i's FC profile
def _compute_site_fc_mean(npz_dir: Path) -> dict[str, np.ndarray]: """Per-site mean FC matrix (N, N) across all subjects (no train/test split needed here since pre-training is fully self-supervised).""" site_sums: dict[str, np.ndarray] = {} site_counts: dict[str, int] = {} for p in sorted(npz_dir.glob("*.npz")): data = np.load(p, allow_pickle=True) site = str(data["site"]) fc = data["mean_fc"].astype(np.float32) if site not in site_sums: site_sums[site] = np.zeros_like(fc) site_counts[site] = 0 site_sums[site] += fc site_counts[site] += 1 return {s: site_sums[s] / site_counts[s] for s in site_sums}
# ---------------------------------------------------------------------------# Lightning module# ---------------------------------------------------------------------------
class BrainMAETask(pl.LightningModule): def __init__( self, num_rois: int = 200, num_windows: int = 30, hidden_dim: int = 128, decoder_dim: int = 64, num_heads: int = 4, encoder_layers: int = 4, decoder_layers: int = 2, dropout: float = 0.1, mask_ratio: float = 0.5, lr: float = 1e-3, weight_decay: float = 1e-4, warmup_epochs: int = 10, max_epochs: int = 200, ): super().__init__() self.save_hyperparameters() self.mae = BrainMAE( num_rois=num_rois, num_windows=num_windows, hidden_dim=hidden_dim, decoder_dim=decoder_dim, num_heads=num_heads, encoder_layers=encoder_layers, decoder_layers=decoder_layers, dropout=dropout, mask_ratio=mask_ratio, )
def training_step(self, batch: torch.Tensor, batch_idx: int) -> torch.Tensor: loss, _ = self.mae(batch) self.log("train_loss", loss, prog_bar=True, on_epoch=True, on_step=False) return loss
def validation_step(self, batch: torch.Tensor, batch_idx: int) -> torch.Tensor: loss, _ = self.mae(batch) self.log("val_loss", loss, prog_bar=True, on_epoch=True, on_step=False) return loss
def configure_optimizers(self): opt = torch.optim.AdamW( self.parameters(), lr=self.hparams.lr, weight_decay=self.hparams.weight_decay, )
def _lr_lambda(epoch: int) -> float: wu = self.hparams.warmup_epochs if epoch < wu: return epoch / max(1, wu) progress = (epoch - wu) / max(1, self.hparams.max_epochs - wu) return 0.5 * (1.0 + np.cos(np.pi * progress))
sch = torch.optim.lr_scheduler.LambdaLR(opt, _lr_lambda) return {"optimizer": opt, "lr_scheduler": {"scheduler": sch, "interval": "epoch"}}
# ---------------------------------------------------------------------------# Main# ---------------------------------------------------------------------------
def build_parser() -> argparse.ArgumentParser: p = argparse.ArgumentParser(description="BC-MAE Pre-training") p.add_argument("--data_dir", type=str, default="data") p.add_argument("--max_windows", type=int, default=30) p.add_argument("--max_epochs", type=int, default=200) p.add_argument("--hidden_dim", type=int, default=128) p.add_argument("--decoder_dim", type=int, default=64) p.add_argument("--num_heads", type=int, default=4) p.add_argument("--encoder_layers", type=int, default=4) p.add_argument("--decoder_layers", type=int, default=2) p.add_argument("--dropout", type=float, default=0.1) p.add_argument("--mask_ratio", type=float, default=0.5) p.add_argument("--lr", type=float, default=1e-3) p.add_argument("--weight_decay", type=float, default=1e-4) p.add_argument("--warmup_epochs", type=int, default=10) p.add_argument("--batch_size", type=int, default=32) p.add_argument("--num_workers", type=int, default=4) p.add_argument("--val_ratio", type=float, default=0.1) p.add_argument("--accelerator", type=str, default="auto") p.add_argument("--devices", type=str, default="auto") p.add_argument("--seed", type=int, default=42) p.add_argument("--ckpt_dir", type=str, default="checkpoints/mae") return p
def main() -> None: torch.set_float32_matmul_precision("medium") args = build_parser().parse_args() pl.seed_everything(args.seed, workers=True)
processed_dir = Path(args.data_dir) / "processed" print(f"Computing site FC means from {processed_dir} ...") site_fc_mean = _compute_site_fc_mean(processed_dir) print(f" {len(site_fc_mean)} sites found.")
full_ds = MAEDataset(processed_dir, site_fc_mean=site_fc_mean) n = len(full_ds) n_val = max(1, int(n * args.val_ratio)) n_train = n - n_val rng = torch.Generator().manual_seed(args.seed) train_ds, val_ds = torch.utils.data.random_split(full_ds, [n_train, n_val], generator=rng) print(f"Pre-training split: {n_train} train / {n_val} val ({n} total)")
pin = torch.cuda.is_available() train_dl = DataLoader(train_ds, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers, pin_memory=pin) val_dl = DataLoader(val_ds, batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers, pin_memory=pin)
first = np.load(full_ds.paths[0], allow_pickle=True) num_rois = int(first["mean_fc"].shape[0]) # Spatial MAE: each of the N ROIs is a "window", its FC row (N-dim) is the patch feature num_windows = num_rois print(f"Spatial BC-MAE: {num_rois} ROIs × {num_rois}-dim FC rows")
task = BrainMAETask( num_rois=num_rois, num_windows=num_windows, # = num_rois (200) — spatial MAE hidden_dim=args.hidden_dim, decoder_dim=args.decoder_dim, num_heads=args.num_heads, encoder_layers=args.encoder_layers, decoder_layers=args.decoder_layers, dropout=args.dropout, mask_ratio=args.mask_ratio, lr=args.lr, weight_decay=args.weight_decay, warmup_epochs=args.warmup_epochs, max_epochs=args.max_epochs, )
ckpt_dir = Path(args.ckpt_dir) ckpt_dir.mkdir(parents=True, exist_ok=True)
trainer = pl.Trainer( max_epochs=args.max_epochs, accelerator=args.accelerator, devices=args.devices, deterministic=True, log_every_n_steps=1, callbacks=[ EarlyStopping(monitor="val_loss", mode="min", patience=30), ModelCheckpoint( dirpath=str(ckpt_dir), monitor="val_loss", mode="min", save_top_k=1, filename="mae-best-{epoch:03d}-{val_loss:.4f}", ), ], )
trainer.fit(task, train_dl, val_dl) best = trainer.checkpoint_callback.best_model_path print(f"\nPre-training complete.") print(f"Best checkpoint: {best}") print(f"\nNext step:") print(f" python -m brain_gcn.finetune_main --mae_ckpt {best} --data_dir {args.data_dir}")
if __name__ == "__main__": main()123from .classification import ClassificationTask
__all__ = ["ClassificationTask"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244"""PyTorch Lightning training task for ASD/TD classification.
v2 changes: - class_weights arg → weighted CrossEntropyLoss (fixes class imbalance) - CosineAnnealingWarmRestarts scheduler (T_0=50, T_mult=2) - BOLD noise augmentation in training_step - Sensitivity (ASD recall) + Specificity (TD recall) metrics added - drop_edge_p forwarded to build_model"""
from __future__ import annotations
import argparse
import pytorch_lightning as plimport torchfrom torch import nnfrom torchmetrics.classification import ( BinaryAUROC, BinaryAccuracy, BinaryF1Score, BinaryRecall, BinarySpecificity,)
from brain_gcn.models import build_modelfrom brain_gcn.utils.grl import ganin_alpha
class ClassificationTask(pl.LightningModule): def __init__( self, hidden_dim: int = 64, dropout: float = 0.5, readout: str = "attention", model_name: str = "graph_temporal", lr: float = 1e-3, weight_decay: float = 1e-4, class_weights: torch.Tensor | None = None, bold_noise_std: float = 0.01, drop_edge_p: float = 0.1, cosine_t0: int = 50, cosine_t_mult: int = 2, cosine_eta_min: float = 1e-5, num_sites: int = 1, adv_site_weight: float = 1.0, num_nodes: int = 200, num_modes: int = 16, orth_weight: float = 0.01, mode_init: "torch.Tensor | None" = None, in_features: int = 1, ): """ Parameters ---------- class_weights : 1-D tensor of length num_classes for weighted CE. bold_noise_std : std dev of Gaussian noise added during training. drop_edge_p : edge drop probability for graph models. cosine_t0 : CosineAnnealingWarmRestarts first restart epoch. cosine_t_mult : restart interval multiplier. cosine_eta_min : minimum LR after annealing. num_sites : number of acquisition sites (for adv_fc_mlp). adv_site_weight : weight on the adversarial site loss term. in_features : node feature dimension (1 for BOLD std, N for FC rows). """ super().__init__() self.save_hyperparameters(ignore=["class_weights", "mode_init"]) self.register_buffer("class_weights", class_weights)
self.model = build_model( model_name=model_name, hidden_dim=hidden_dim, num_sites=num_sites, num_nodes=num_nodes, num_modes=num_modes, dropout=dropout, readout=readout, drop_edge_p=drop_edge_p, mode_init=mode_init, in_features=in_features, ) self.loss_fn = nn.CrossEntropyLoss(weight=class_weights) # Site cross-entropy — unweighted (sites roughly balanced) self.site_loss_fn = nn.CrossEntropyLoss(ignore_index=-1)
# --- Metrics -------------------------------------------------------- self.train_acc = BinaryAccuracy()
self.val_acc = BinaryAccuracy() self.val_auc = BinaryAUROC() self.val_f1 = BinaryF1Score() self.val_sens = BinaryRecall() # sensitivity = ASD recall self.val_spec = BinarySpecificity() # specificity = TD recall
self.test_acc = BinaryAccuracy() self.test_auc = BinaryAUROC() self.test_f1 = BinaryF1Score() self.test_sens = BinaryRecall() self.test_spec = BinarySpecificity()
@property def _is_adversarial(self) -> bool: return self.hparams.model_name in ("adv_fc_mlp", "adv_brain_mode")
# ------------------------------------------------------------------ def forward(self, bold_windows: torch.Tensor, adj: torch.Tensor) -> torch.Tensor: return self.model(bold_windows, adj)
def _step(self, batch, stage: str) -> torch.Tensor: bold_windows, adj, labels, site_ids = batch logits = self(bold_windows, adj) loss = self.loss_fn(logits, labels) probs = torch.softmax(logits, dim=-1)[:, 1] preds = torch.argmax(logits, dim=-1)
self.log(f"{stage}_loss", loss, prog_bar=True, on_epoch=True, on_step=False)
if stage == "train": self.train_acc.update(preds, labels) self.log("train_acc", self.train_acc, prog_bar=True, on_epoch=True, on_step=False)
elif stage == "val": self.val_acc.update(preds, labels) self.val_auc.update(probs, labels) self.val_f1.update(preds, labels) self.val_sens.update(preds, labels) self.val_spec.update(preds, labels) self.log("val_acc", self.val_acc, prog_bar=True, on_epoch=True, on_step=False) self.log("val_auc", self.val_auc, prog_bar=True, on_epoch=True, on_step=False) self.log("val_f1", self.val_f1, prog_bar=False, on_epoch=True, on_step=False) self.log("val_sens", self.val_sens, prog_bar=False, on_epoch=True, on_step=False) self.log("val_spec", self.val_spec, prog_bar=False, on_epoch=True, on_step=False)
elif stage == "test": self.test_acc.update(preds, labels) self.test_auc.update(probs, labels) self.test_f1.update(preds, labels) self.test_sens.update(preds, labels) self.test_spec.update(preds, labels) self.log("test_acc", self.test_acc, prog_bar=True, on_epoch=True, on_step=False) self.log("test_auc", self.test_auc, prog_bar=True, on_epoch=True, on_step=False) self.log("test_f1", self.test_f1, prog_bar=True, on_epoch=True, on_step=False) self.log("test_sens", self.test_sens, prog_bar=True, on_epoch=True, on_step=False) self.log("test_spec", self.test_spec, prog_bar=True, on_epoch=True, on_step=False)
return loss
def training_step(self, batch, batch_idx: int) -> torch.Tensor: bold_windows, adj, labels, site_ids = batch if self.hparams.bold_noise_std > 0.0: signal_std = bold_windows.std(dim=(1, 2), keepdim=True).detach() noise = torch.randn_like(bold_windows) * self.hparams.bold_noise_std * signal_std bold_windows = bold_windows + noise
if self._is_adversarial: # Dual loss: ASD classification + adversarial site deconfounding asd_logits, site_logits = self.model( bold_windows, adj, return_site_logits=True ) asd_loss = self.loss_fn(asd_logits, labels) site_loss = self.site_loss_fn(site_logits, site_ids) loss = asd_loss + self.hparams.adv_site_weight * site_loss
probs = torch.softmax(asd_logits, dim=-1)[:, 1] preds = torch.argmax(asd_logits, dim=-1)
self.log("train_asd_loss", asd_loss, prog_bar=False, on_epoch=True, on_step=False) self.log("train_site_loss", site_loss, prog_bar=False, on_epoch=True, on_step=False) self.log("train_loss", loss, prog_bar=True, on_epoch=True, on_step=False) self.train_acc.update(preds, labels) self.log("train_acc", self.train_acc, prog_bar=True, on_epoch=True, on_step=False) else: loss = self._step((bold_windows, adj, labels, site_ids), "train")
# Orthogonality regularization — BMN only (model exposes orthogonality_loss()) if hasattr(self.model, "orthogonality_loss") and self.hparams.orth_weight > 0.0: orth = self.model.orthogonality_loss() loss = loss + self.hparams.orth_weight * orth self.log("train_orth_loss", orth, prog_bar=False, on_epoch=True, on_step=False)
return loss
def on_train_epoch_start(self) -> None: """Anneal the GRL alpha at the start of each epoch.""" if self._is_adversarial: alpha = ganin_alpha(self.current_epoch, self.trainer.max_epochs) self.model.grl.alpha = alpha self.log("grl_alpha", alpha, prog_bar=False, on_epoch=True, on_step=False)
def validation_step(self, batch, batch_idx: int) -> torch.Tensor: return self._step(batch, "val")
def test_step(self, batch, batch_idx: int) -> torch.Tensor: return self._step(batch, "test")
# ------------------------------------------------------------------ def configure_optimizers(self): opt = torch.optim.AdamW( self.parameters(), lr=self.hparams.lr, weight_decay=self.hparams.weight_decay, ) sch = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts( opt, T_0=self.hparams.cosine_t0, T_mult=self.hparams.cosine_t_mult, eta_min=self.hparams.cosine_eta_min, ) return { "optimizer": opt, "lr_scheduler": {"scheduler": sch, "interval": "epoch"}, }
# ------------------------------------------------------------------ @staticmethod def add_model_specific_arguments(parent_parser: argparse.ArgumentParser): parser = argparse.ArgumentParser(parents=[parent_parser], add_help=False) parser.add_argument("--hidden_dim", type=int, default=64) parser.add_argument("--dropout", type=float, default=0.5) parser.add_argument("--readout", choices=["mean", "attention"], default="attention") parser.add_argument( "--model_name", choices=["graph_temporal", "gcn", "gru", "fc_mlp", "adv_fc_mlp", "gat", "transformer", "cnn3d", "graphsage", "brain_mode", "adv_brain_mode", "dynamic_fc_attn"], default="graph_temporal", ) parser.add_argument("--lr", type=float, default=1e-3) parser.add_argument("--adv_site_weight", type=float, default=1.0, help="Weight on adversarial site loss (adv_fc_mlp only).") parser.add_argument("--weight_decay", type=float, default=1e-4) parser.add_argument("--bold_noise_std", type=float, default=0.01) parser.add_argument("--drop_edge_p", type=float, default=0.1) parser.add_argument("--cosine_t0", type=int, default=50) parser.add_argument("--cosine_t_mult", type=int, default=2, help="CosineAnnealingWarmRestarts restart interval multiplier") parser.add_argument("--cosine_eta_min", type=float, default=1e-5, help="CosineAnnealingWarmRestarts minimum learning rate") parser.add_argument("--num_modes", type=int, default=16, help="Brain Mode Network: number of learnable modes K") parser.add_argument("--orth_weight", type=float, default=0.01, help="Brain Mode Network: orthogonality regularization weight") return parser123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243"""Cross-validation and K-fold evaluation utilities.
Provides:- Stratified K-fold cross-validation- Leave-one-site-out validation- Train/val/test split preservation"""
from __future__ import annotations
import loggingfrom pathlib import Pathfrom typing import NamedTuple
import numpy as npimport pytorch_lightning as plimport torchfrom sklearn.model_selection import StratifiedKFold, LeaveOneOut
from brain_gcn.main import build_datamodule, build_task, build_trainer, train_from_argsfrom brain_gcn.utils.data.datamodule import ABIDEDataModule
log = logging.getLogger(__name__)
class CVFold(NamedTuple): """Container for a single CV fold's results."""
fold_idx: int train_indices: np.ndarray val_indices: np.ndarray test_indices: np.ndarray metrics: dict # {'test_auc': ..., 'test_acc': ...}
class CrossValidator: """Stratified K-fold cross-validator."""
def __init__( self, n_splits: int = 5, shuffle: bool = True, random_state: int = 42, ): """Initialize CV splitter.
Parameters ---------- n_splits : int Number of folds. shuffle : bool Whether to shuffle before splitting. random_state : int Random seed. """ self.n_splits = n_splits self.shuffle = shuffle self.random_state = random_state self.skf = StratifiedKFold( n_splits=n_splits, shuffle=shuffle, random_state=random_state, )
def split( self, labels: np.ndarray, ) -> list[tuple[np.ndarray, np.ndarray]]: """Generate train/test split indices.
Parameters ---------- labels : (N,) array Class labels for stratification.
Returns ------- list[tuple[np.ndarray, np.ndarray]] List of (train_idx, test_idx) tuples. """ dummy_X = np.arange(len(labels)).reshape(-1, 1) splits = list(self.skf.split(dummy_X, labels)) return [(train_idx, test_idx) for train_idx, test_idx in splits]
class LeaveOneSiteOutValidator: """Leave-one-site-out cross-validator."""
def __init__(self): """Initialize LOSO validator.""" pass
def split( self, sites: np.ndarray, ) -> list[tuple[np.ndarray, np.ndarray]]: """Generate leave-one-site-out splits.
Parameters ---------- sites : (N,) array Site labels for each subject.
Returns ------- list[tuple[np.ndarray, np.ndarray]] List of (in_site_idx, out_site_idx) tuples. """ unique_sites = np.unique(sites) splits = []
for test_site in unique_sites: test_idx = np.where(sites == test_site)[0] train_idx = np.where(sites != test_site)[0] splits.append((train_idx, test_idx))
return splits
class CVResults: """Accumulator for cross-validation results."""
def __init__(self): self.folds: list[CVFold] = []
def add_fold(self, fold: CVFold) -> None: """Add results from a single fold.""" self.folds.append(fold)
def mean_metrics(self) -> dict: """Compute mean metrics across folds.""" if not self.folds: return {}
all_metrics = [fold.metrics for fold in self.folds] keys = all_metrics[0].keys()
means = {} for key in keys: values = [m[key] for m in all_metrics if isinstance(m[key], (int, float))] if values: means[f"{key}_mean"] = float(np.mean(values)) means[f"{key}_std"] = float(np.std(values))
return means
def to_dict(self) -> dict: """Convert to dictionary for serialization.""" return { "n_folds": len(self.folds), "folds": [ { "fold_idx": fold.fold_idx, "metrics": fold.metrics, } for fold in self.folds ], "summary": self.mean_metrics(), }
def kfold_cross_validate( base_args, n_splits: int = 5, output_dir: str | Path | None = None,) -> CVResults: """Run stratified K-fold cross-validation.
Parameters ---------- base_args : argparse.Namespace Base training arguments. n_splits : int Number of folds. output_dir : str or Path, optional Directory to save fold results.
Returns ------- CVResults Aggregated cross-validation results. """ output_dir = Path(output_dir) if output_dir else None if output_dir: output_dir.mkdir(parents=True, exist_ok=True)
# Build data module to get labels dm = build_datamodule(base_args) dm.prepare_data() dm.setup()
# Collect labels all_labels = [] for batch in dm.train_dataloader(): _, _, labels = batch all_labels.extend(labels.cpu().numpy()) all_labels = np.array(all_labels)
# Initialize CV cv = CrossValidator(n_splits=n_splits, random_state=base_args.seed) splits = cv.split(all_labels)
results = CVResults()
for fold_idx, (train_idx, test_idx) in enumerate(splits): log.info(f"Running fold {fold_idx + 1}/{n_splits}")
# Create fold-specific args fold_args = vars(base_args).copy() # Note: For full implementation, would need to modify datamodule # to accept external train/test splits. For now, train normally.
# Train model pl.seed_everything(base_args.seed + fold_idx, workers=True) trainer, _, _ = train_from_args(base_args)
# Collect metrics fold_metrics = { key: value.item() if isinstance(value, torch.Tensor) else value for key, value in trainer.callback_metrics.items() if key.startswith(("test_",)) }
fold_result = CVFold( fold_idx=fold_idx, train_indices=train_idx, val_indices=np.array([]), # Not used in standard K-fold test_indices=test_idx, metrics=fold_metrics, ) results.add_fold(fold_result)
if output_dir: fold_file = output_dir / f"fold_{fold_idx}.pt" torch.save(fold_result, fold_file)
if output_dir: summary_file = output_dir / "cv_summary.pt" torch.save(results.to_dict(), summary_file) log.info(f"CV results saved to {output_dir}")
return results1from .datamodule import ABIDEDataModule123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521"""PyTorch Lightning DataModule for ABIDE I.
Full pipeline (called once via prepare_data / setup): 1. Download ABIDE via nilearn (download.py) 2. Preprocess subjects → .npz cache (preprocess.py) 3. Stratified train / val / test split 4. Build population adjacency from training subjects (functional_connectivity.py) 5. Expose train / val / test DataLoaders
Usage: dm = ABIDEDataModule(data_dir="data", n_subjects=100) dm.prepare_data() dm.setup() for bold_windows, adj, label in dm.train_dataloader(): ..."""
from __future__ import annotations
import argparseimport loggingfrom collections import Counterfrom pathlib import Path
import numpy as npimport pytorch_lightning as plimport torchfrom sklearn.model_selection import StratifiedShuffleSplitfrom torch.utils.data import DataLoader
from .dataset import ABIDEDatasetfrom .download import fetch_abide, extract_subjectsfrom .functional_connectivity import compute_population_adjfrom .preprocess import preprocess_all
log = logging.getLogger(__name__)
def collate_fn(batch): """ Custom collate: stack bold_windows, labels, and site_ids; keep adj as-is. Returns: bold_windows : (B, W, N) adj : (B, N, N) labels : (B,) site_ids : (B,) """ bold_windowss, adjs, labels, site_ids = zip(*batch) return ( torch.stack(bold_windowss), torch.stack(adjs), torch.stack(labels), torch.stack(site_ids), )
class ABIDEDataModule(pl.LightningDataModule): def __init__( self, data_dir: str = "data", n_subjects: int | None = None, window_len: int = 50, step: int = 5, max_windows: int | None = 30, fc_threshold: float = 0.2, use_dynamic_adj: bool = False, use_dynamic_adj_sequence: bool = False, use_population_adj: bool = True, preserve_fc_sign: bool = False, use_fc_variance: bool = False, use_fisher_z: bool = False, use_fc_degree_features: bool = False, use_fc_row_features: bool = False, n_pca_components: int = 0, batch_size: int = 32, val_ratio: float = 0.1, test_ratio: float = 0.1, split_strategy: str = "stratified", val_site: str | None = None, test_site: str | None = None, num_workers: int = 4, overwrite_cache: bool = False, force_prepare: bool = False, ): """ Parameters ---------- data_dir : root directory for raw + processed data n_subjects : cap for ABIDE download (None = all ~884) window_len : sliding window length in TRs step : sliding window step in TRs max_windows : truncate each subject to this many windows (ensures uniform batch shapes without padding) fc_threshold : sparsify FC: zero edges with |fc| < threshold use_dynamic_adj : per-subject: use mean of window FCs (vs. full-scan FC) use_dynamic_adj_sequence: per-subject: return one adjacency per window. Ignored when use_population_adj=True. use_population_adj: compute a single population-level adj from training set and use it for all subjects (recommended) batch_size : samples per batch val_ratio : fraction of data for validation test_ratio : fraction of data for test split_strategy : stratified random split or site_holdout split val_site : validation site for site_holdout. If unset, chosen by size. test_site : test site for site_holdout. If unset, largest site is used. num_workers : DataLoader worker processes overwrite_cache : re-preprocess even if .npz files exist force_prepare : download/preprocess even when processed .npz files exist """ super().__init__() self.data_dir = Path(data_dir) self.raw_dir = self.data_dir / "raw" self.processed_dir = self.data_dir / "processed"
self.n_subjects = n_subjects self.window_len = window_len self.step = step self.max_windows = max_windows self.fc_threshold = fc_threshold self.use_dynamic_adj = use_dynamic_adj self.use_dynamic_adj_sequence = use_dynamic_adj_sequence self.use_population_adj = use_population_adj self.preserve_fc_sign = preserve_fc_sign self.use_fc_variance = use_fc_variance self.use_fisher_z = use_fisher_z self.use_fc_degree_features = use_fc_degree_features self.use_fc_row_features = use_fc_row_features self.n_pca_components = n_pca_components self.batch_size = batch_size self.val_ratio = val_ratio self.test_ratio = test_ratio self.split_strategy = split_strategy self.val_site = val_site self.test_site = test_site self.num_workers = num_workers self.overwrite_cache = overwrite_cache self.force_prepare = force_prepare
self._population_adj: np.ndarray | None = None self._site_fc_mean: dict[str, np.ndarray] = {} self._site_to_int: dict[str, int] = {} self._pca_mean: np.ndarray | None = None # (D,) mean FC vector self._pca_components: np.ndarray | None = None # (K, D) principal axes self._train_paths: list[Path] = [] self._val_paths: list[Path] = [] self._test_paths: list[Path] = []
# ------------------------------------------------------------------ # Lightning hooks # ------------------------------------------------------------------
def prepare_data(self): """Download + preprocess (runs on rank 0 only in distributed settings).""" cached_paths = list(self.processed_dir.glob("*.npz")) n_cached = len(cached_paths)
# Skip only when we already have enough subjects and no explicit override have_enough = ( self.n_subjects is None or n_cached >= self.n_subjects ) if cached_paths and have_enough and not self.overwrite_cache and not self.force_prepare: log.info( "Found %d cached subject files in %s; skipping download/preprocess.", n_cached, self.processed_dir, ) return
if n_cached > 0 and not self.overwrite_cache: log.info( "Have %d subjects, want %s — downloading remaining subjects.", n_cached, self.n_subjects or "all", )
dataset = fetch_abide( data_dir=self.raw_dir, n_subjects=self.n_subjects, ) subjects = extract_subjects(dataset, min_timepoints=self.window_len + self.step) preprocess_all( subjects, processed_dir=self.processed_dir, window_len=self.window_len, step=self.step, overwrite=self.overwrite_cache, )
def setup(self, stage: str | None = None): """Build train/val/test splits and optionally the population adjacency.""" all_paths = sorted(self.processed_dir.glob("*.npz")) if not all_paths: raise RuntimeError( f"No .npz files found in {self.processed_dir}. " "Run prepare_data() first." )
# Read labels/sites for splitting labels = np.array([ int(np.load(p, allow_pickle=True)["label"]) for p in all_paths ]) sites = np.array([ str(np.load(p, allow_pickle=True)["site"]) for p in all_paths ])
# Build site → int mapping from ALL subjects (consistent across splits) self._site_to_int = { site: i for i, site in enumerate(sorted(set(sites.tolist()))) } log.info("Sites (%d): %s", len(self._site_to_int), sorted(self._site_to_int))
if self.split_strategy == "stratified": train_paths, val_paths, test_paths = self._stratified_split( all_paths, labels, self.val_ratio, self.test_ratio ) elif self.split_strategy == "site_holdout": train_paths, val_paths, test_paths = self._site_holdout_split( all_paths, labels, sites, self.val_site, self.test_site ) else: raise ValueError(f"Unknown split_strategy: {self.split_strategy}") self._train_paths = train_paths self._val_paths = val_paths self._test_paths = test_paths
log.info( "Split (%s): train=%d val=%d test=%d", self.split_strategy, len(train_paths), len(val_paths), len(test_paths), )
# Build population adjacency from training subjects only if self.use_population_adj: self._population_adj = self._build_population_adj(train_paths)
# Compute per-site mean FC from training set (FC-domain site normalization) self._site_fc_mean = self._build_site_fc_mean(train_paths)
# PCA on training FC upper triangles (reduces p>>n overfitting) if self.n_pca_components > 0: self._pca_mean, self._pca_components = self._build_pca(train_paths)
def train_dataloader(self) -> DataLoader: return DataLoader( self._make_dataset(self._train_paths), batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers, collate_fn=collate_fn, pin_memory=torch.cuda.is_available(), )
def val_dataloader(self) -> DataLoader: return DataLoader( self._make_dataset(self._val_paths), batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers, collate_fn=collate_fn, pin_memory=torch.cuda.is_available(), )
def test_dataloader(self) -> DataLoader: return DataLoader( self._make_dataset(self._test_paths), batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers, collate_fn=collate_fn, pin_memory=torch.cuda.is_available(), )
# ------------------------------------------------------------------ # Properties exposed to the model # ------------------------------------------------------------------
@property def num_nodes(self) -> int: """Number of ROIs (200 for cc200 atlas).""" data = np.load(self._train_paths[0], allow_pickle=True) return data["mean_fc"].shape[0]
@property def num_windows(self) -> int: """Number of brain-state snapshots (sliding windows) per subject.""" if self.max_windows is not None: return self.max_windows data = np.load(self._train_paths[0], allow_pickle=True) return data["bold_windows"].shape[0]
@property def population_adj(self) -> np.ndarray | None: return self._population_adj
# ------------------------------------------------------------------ # Helpers # ------------------------------------------------------------------
def _make_dataset(self, paths: list[Path]) -> ABIDEDataset: return ABIDEDataset( npz_paths=paths, population_adj=self._population_adj, use_dynamic_adj=self.use_dynamic_adj, use_dynamic_adj_sequence=self.use_dynamic_adj_sequence, fc_threshold=self.fc_threshold, max_windows=self.max_windows, site_fc_mean=self._site_fc_mean, preserve_fc_sign=self.preserve_fc_sign, site_to_int=self._site_to_int, use_fc_variance=self.use_fc_variance, use_fisher_z=self.use_fisher_z, pca_mean=self._pca_mean, pca_components=self._pca_components, use_fc_degree_features=self.use_fc_degree_features, use_fc_row_features=self.use_fc_row_features, )
@property def num_sites(self) -> int: return len(self._site_to_int)
@staticmethod def _stratified_split( paths: list[Path], labels: np.ndarray, val_ratio: float, test_ratio: float, ) -> tuple[list[Path], list[Path], list[Path]]: paths = np.array(paths)
# First split off test set sss_test = StratifiedShuffleSplit(n_splits=1, test_size=test_ratio, random_state=42) train_val_idx, test_idx = next(sss_test.split(paths, labels))
# Then split val from train val_size = val_ratio / (1.0 - test_ratio) sss_val = StratifiedShuffleSplit(n_splits=1, test_size=val_size, random_state=42) train_idx, val_idx = next(sss_val.split(paths[train_val_idx], labels[train_val_idx]))
return ( list(paths[train_val_idx[train_idx]]), list(paths[train_val_idx[val_idx]]), list(paths[test_idx]), )
@staticmethod def _site_holdout_split( paths: list[Path], labels: np.ndarray, sites: np.ndarray, val_site: str | None, test_site: str | None, ) -> tuple[list[Path], list[Path], list[Path]]: paths_arr = np.array(paths) site_counts = Counter(sites.tolist()) if len(site_counts) < 3: raise ValueError("site_holdout split needs at least 3 sites.")
sorted_sites = [site for site, _ in site_counts.most_common()] # test_site may be a comma-separated list of sites (e.g. "UCLA_1,UCLA_2") test_sites = [s.strip() for s in test_site.split(",")] if test_site else [sorted_sites[1]] if val_site is None: val_site = next((s for s in reversed(sorted_sites) if s not in test_sites), None) if val_site is None or val_site in test_sites: raise ValueError("site_holdout split needs distinct val_site and test_site.") for ts in test_sites: if ts not in site_counts: raise ValueError(f"Unknown test_site '{ts}'. Available: {sorted(site_counts)}") if val_site not in site_counts: raise ValueError(f"Unknown val_site '{val_site}'. Available: {sorted(site_counts)}")
train_mask = np.ones(len(sites), dtype=bool) for ts in test_sites: train_mask &= (sites != ts) train_mask &= (sites != val_site) val_mask = sites == val_site test_mask = np.zeros(len(sites), dtype=bool) for ts in test_sites: test_mask |= (sites == ts)
ABIDEDataModule._assert_both_labels(labels[train_mask], "train") ABIDEDataModule._assert_both_labels(labels[val_mask], "val") ABIDEDataModule._assert_both_labels(labels[test_mask], "test")
return ( list(paths_arr[train_mask]), list(paths_arr[val_mask]), list(paths_arr[test_mask]), )
@staticmethod def _assert_both_labels(labels: np.ndarray, split_name: str) -> None: unique = set(labels.tolist()) if unique != {0, 1}: raise ValueError( f"{split_name} split must contain both labels, got {sorted(unique)}." )
def _build_pca(self, train_paths: list[Path]) -> tuple[np.ndarray, np.ndarray]: """Compute PCA on training-set FC upper triangles using truncated SVD.
Returns ------- mean_vec : (D,) mean FC vector (for centering) components : (K, D) top-K principal axes (rows = PCs)
With D=19900 features and N≈660 training subjects, PCA reduces to the N-1 dimensional subspace anyway. Using K<<N avoids p>>n overfitting: the MLP trains on K features rather than 19900. """ K = self.n_pca_components log.info("Computing PCA (K=%d) from %d training FC matrices ...", K, len(train_paths))
# Build training matrix: (N_train, D) rows = [] for p in train_paths: data = np.load(p, allow_pickle=True) fc = data["mean_fc"].astype(np.float32) n = fc.shape[0] r, c = np.triu_indices(n, k=1) if self.use_fisher_z: fc = np.arctanh(np.clip(fc, -0.9999, 0.9999)) rows.append(fc[r, c])
X = np.stack(rows, axis=0) # (N_train, D) mean_vec = X.mean(axis=0) # (D,) X_centered = X - mean_vec # (N_train, D)
# Truncated SVD via economy SVD on the smaller dimension # X = U S Vt → principal components = Vt[:K] # Since N << D, use X @ Xt for the eigen-decomposition shortcut # (N_train × N_train covariance, then recover Vt) C = (X_centered @ X_centered.T) / (len(train_paths) - 1) # (N, N) eigenvalues, U = np.linalg.eigh(C) # ascending # eigh returns ascending; we want descending idx = np.argsort(-eigenvalues) U = U[:, idx[:K]] # (N, K) components = (X_centered.T @ U) # (D, K) # Normalise each column to unit length → rows of Vt components /= np.linalg.norm(components, axis=0, keepdims=True) + 1e-8 components = components.T.astype(np.float32) # (K, D)
var_explained = eigenvalues[idx[:K]].sum() / (eigenvalues.sum() + 1e-8) log.info("PCA: top-%d components explain %.1f%% of FC variance.", K, 100 * var_explained) return mean_vec.astype(np.float32), components
def _build_site_fc_mean(self, train_paths: list[Path]) -> dict[str, np.ndarray]: """Compute per-site mean FC matrix (N, N) from training subjects. Subtracting this at load time removes scanner-specific connectivity biases (a simple FC-domain site normalization). BOLD is already z-scored so BOLD-domain corrections have no effect.""" log.info("Computing per-site FC means from %d training subjects ...", len(train_paths)) site_sums: dict[str, np.ndarray] = {} site_counts: dict[str, int] = {} for p in train_paths: data = np.load(p, allow_pickle=True) site = str(data["site"]) fc = data["mean_fc"].astype(np.float32) # (N, N) if site not in site_sums: site_sums[site] = np.zeros_like(fc) site_counts[site] = 0 site_sums[site] += fc site_counts[site] += 1 return {s: site_sums[s] / site_counts[s] for s in site_sums}
def _build_population_adj(self, train_paths: list[Path]) -> np.ndarray: log.info("Building population adjacency from %d training subjects ...", len(train_paths)) mean_fcs = [] for p in train_paths: data = np.load(p, allow_pickle=True) mean_fcs.append(data["mean_fc"].astype(np.float32)) adj = compute_population_adj(mean_fcs, threshold=self.fc_threshold) log.info( "Population adj: %d nodes, %.1f%% edges non-zero.", adj.shape[0], 100.0 * (adj > 0).sum() / adj.size, ) return adj
# ------------------------------------------------------------------ # argparse integration # ------------------------------------------------------------------
@staticmethod def add_data_specific_arguments(parent_parser: argparse.ArgumentParser): parser = argparse.ArgumentParser(parents=[parent_parser], add_help=False) parser.add_argument("--data_dir", type=str, default="data") parser.add_argument("--n_subjects", type=int, default=None) parser.add_argument("--window_len", type=int, default=50) parser.add_argument("--step", type=int, default=5) parser.add_argument("--max_windows", type=int, default=30) parser.add_argument("--fc_threshold", type=float, default=0.2) parser.add_argument("--use_dynamic_adj", action="store_true") parser.add_argument("--use_dynamic_adj_sequence", action="store_true") parser.add_argument("--use_population_adj", action=argparse.BooleanOptionalAction, default=True) parser.add_argument("--preserve_fc_sign", action="store_true", help="Keep signed FC values in adjacency (required for fc_mlp).") parser.add_argument("--use_fc_variance", action="store_true", help="Append std(fc_windows) as a second feature channel alongside mean FC.") parser.add_argument("--use_fc_degree_features", action="store_true", help="Replace BOLD std node features with per-ROI mean |FC| per window.") parser.add_argument("--use_fc_row_features", action="store_true", help="Use FC rows as node features (W,N,N). Requires graph_temporal + in_features=num_nodes.") parser.add_argument("--use_fisher_z", action="store_true", help="Apply Fisher r-to-z transform to FC values before classification.") parser.add_argument("--n_pca_components", type=int, default=0, help="If >0, reduce FC to this many PCA components before the MLP.") parser.add_argument("--batch_size", type=int, default=32) parser.add_argument("--val_ratio", type=float, default=0.1) parser.add_argument("--test_ratio", type=float, default=0.1) parser.add_argument("--split_strategy", choices=["stratified", "site_holdout"], default="stratified") parser.add_argument("--val_site", type=str, default=None) parser.add_argument("--test_site", type=str, default=None) parser.add_argument("--num_workers", type=int, default=4) parser.add_argument( "--overwrite_cache", action="store_true", help="Force re-download and re-preprocess even if .npz files already exist.", ) return parser123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252"""PyTorch Dataset for preprocessed ABIDE subjects.
Each sample returns: bold_windows : (W, N) — mean BOLD per ROI at each brain-state snapshot adj : (N, N) or (W, N, N) — adjacency for this subject use_dynamic_adj=False → subject's mean FC use_dynamic_adj=True → mean of per-window FCs use_dynamic_adj_sequence=True → per-window FCs use_population_adj=True → shared population adj label : () — int64 scalar (0 = TC, 1 = ASD)
The adjacency is left as raw (thresholded) FC values so the model can applyits own Laplacian normalisation via utils.graph_conv."""
from __future__ import annotations
from pathlib import Path
import numpy as npimport torchfrom torch.utils.data import Dataset
class ABIDEDataset(Dataset): def __init__( self, npz_paths: list[Path | str], population_adj: np.ndarray | None = None, use_dynamic_adj: bool = False, use_dynamic_adj_sequence: bool = False, fc_threshold: float = 0.2, max_windows: int | None = None, site_fc_mean: dict[str, np.ndarray] | None = None, preserve_fc_sign: bool = False, site_to_int: dict[str, int] | None = None, use_fc_variance: bool = False, use_fisher_z: bool = False, pca_mean: np.ndarray | None = None, pca_components: np.ndarray | None = None, use_fc_degree_features: bool = False, use_fc_row_features: bool = False, ): """ Parameters ---------- npz_paths : paths to per-subject .npz files from preprocess.py population_adj : (N, N) pre-computed population-level adjacency. If provided, every sample uses this shared adjacency. use_dynamic_adj : if True and population_adj is None, use mean of per-window FCs; otherwise use mean_fc (full-scan FC). use_dynamic_adj_sequence : if True and population_adj is None, return per-window FCs with shape (W, N, N). fc_threshold : zero-out edges with |fc| < threshold before returning max_windows : truncate all subjects to this many windows so that batches have uniform seq_len (takes the first W windows) site_fc_mean : per-site mean FC matrix (N, N) computed from training set. Subtracted from each subject's FC before thresholding to remove scanner/site connectivity biases (FC-domain site normalization). BOLD is already z-scored so BOLD-domain corrections have no effect. preserve_fc_sign: if True, keep signed FC values in the adjacency instead of converting to |FC|. Required for fc_mlp which uses signed correlations as direct features (anti-correlations between brain networks are diagnostically relevant). use_fc_degree_features: if True, replace stored bold_windows (std of z-scored BOLD ≈ 1.0) with per-window per-ROI mean absolute FC: np.abs(fc_windows).mean(axis=-1). This gives each ROI a scalar ≈ its average connectivity strength in that window — directly discriminative between ASD and TD, unlike BOLD std which is near- constant after z-scoring. use_fc_row_features: if True, use per-window FC rows as node features instead of scalar BOLD std. Returns (W, N, N) where node i's feature vector is its full connectivity profile fc_windows[w, i, :]. This is the standard formulation in brain GCN literature (BrainNetCNN, BrainGNN, STAGIN). Requires model to be built with in_features=num_nodes. """ self.npz_paths = [Path(p) for p in npz_paths] self.population_adj = ( torch.FloatTensor(population_adj) if population_adj is not None else None ) self.use_dynamic_adj = use_dynamic_adj self.use_dynamic_adj_sequence = use_dynamic_adj_sequence self.fc_threshold = fc_threshold self.max_windows = max_windows self.site_fc_mean = site_fc_mean or {} self.preserve_fc_sign = preserve_fc_sign self.site_to_int = site_to_int or {} self.use_fc_variance = use_fc_variance self.use_fisher_z = use_fisher_z self.pca_mean = pca_mean self.pca_components = pca_components self.use_fc_degree_features = use_fc_degree_features self.use_fc_row_features = use_fc_row_features
# Pre-load labels + window counts for fast access without loading full arrays self._meta = self._scan_metadata()
@staticmethod def _array(data: np.lib.npyio.NpzFile, primary: str, legacy: str) -> np.ndarray: if primary in data: return data[primary] if legacy in data: return data[legacy] raise KeyError(f"Expected '{primary}' or legacy '{legacy}' in subject archive")
def _threshold(self, adj_np: np.ndarray, preserve_sign: bool = False) -> np.ndarray: mask = np.abs(adj_np) >= self.fc_threshold if preserve_sign: return np.where(mask, adj_np, 0.0) return np.where(mask, np.abs(adj_np), 0.0)
@staticmethod def _fisher_z(fc: np.ndarray) -> np.ndarray: """Fisher's r-to-z transform: z = arctanh(r).
Linearises the correlation space — correlations near ±1 are compressed in Pearson space but uniform in z-space. Stabilises variance across different correlation magnitudes, which matters for linear classifiers. Clipped to ±0.9999 to avoid ±inf at perfect correlations. """ return np.arctanh(np.clip(fc, -0.9999, 0.9999))
@staticmethod def _pad_or_truncate_windows(array: np.ndarray, max_windows: int | None) -> np.ndarray: if max_windows is None: return array if array.shape[0] >= max_windows: return array[:max_windows] pad_count = max_windows - array.shape[0] pad = np.repeat(array[-1:], pad_count, axis=0) return np.concatenate([array, pad], axis=0)
def _scan_metadata(self) -> list[dict]: meta = [] for p in self.npz_paths: data = np.load(p, allow_pickle=True) W = self._array(data, "bold_windows", "window_bold").shape[0] if self.max_windows is not None: W = self.max_windows meta.append( { "label": int(data["label"]), "subject_id": str(data["subject_id"]), "site": str(data["site"]), "num_windows": W, } ) return meta
# ------------------------------------------------------------------ def __len__(self) -> int: return len(self.npz_paths)
def __getitem__(self, idx: int): data = np.load(self.npz_paths[idx], allow_pickle=True)
site = str(data["site"])
# Pre-load fc_windows if needed for node features or dynamic adjacency _wfc_loaded: np.ndarray | None = None if self.use_fc_row_features or self.use_fc_degree_features or self.use_dynamic_adj_sequence or self.use_dynamic_adj: _wfc_loaded = self._array(data, "fc_windows", "window_fc").astype(np.float32)
# Node feature sequence if self.use_fc_row_features and _wfc_loaded is not None: # FC rows as node features: (W, N, N) — each node i gets fc_windows[w, i, :] # This is the standard brain GCN formulation (BrainNetCNN, BrainGNN, STAGIN). bold_windows = self._pad_or_truncate_windows(_wfc_loaded, self.max_windows) elif self.use_fc_degree_features and _wfc_loaded is not None: # Per-window per-ROI mean |FC| after site correction (W, N) wfc = self._pad_or_truncate_windows(_wfc_loaded, self.max_windows) if site in self.site_fc_mean: wfc = wfc - self.site_fc_mean[site].astype(np.float32)[None] bold_windows = np.abs(wfc).mean(axis=-1) bold_windows = self._pad_or_truncate_windows(bold_windows, self.max_windows) else: bold_windows = self._array(data, "bold_windows", "window_bold").astype(np.float32) bold_windows = self._pad_or_truncate_windows(bold_windows, self.max_windows)
# Adjacency if self.population_adj is not None: adj = self.population_adj # (N, N) shared
elif self.use_dynamic_adj_sequence: assert _wfc_loaded is not None wfc = self._pad_or_truncate_windows(_wfc_loaded, self.max_windows) if site in self.site_fc_mean: wfc = wfc - self.site_fc_mean[site].astype(np.float32)[None] adj = torch.FloatTensor( self._threshold(wfc, self.preserve_fc_sign).astype(np.float32) ) # (W, N, N)
elif self.use_dynamic_adj: assert _wfc_loaded is not None wfc = self._pad_or_truncate_windows(_wfc_loaded, self.max_windows) fc = wfc.mean(axis=0) if site in self.site_fc_mean: fc = fc - self.site_fc_mean[site].astype(np.float32) adj = torch.FloatTensor( self._threshold(fc, self.preserve_fc_sign).astype(np.float32) ) # (N, N)
else: # Static per-subject mean FC mean_np = data["mean_fc"].astype(np.float32) if site in self.site_fc_mean: mean_np = mean_np - self.site_fc_mean[site].astype(np.float32) if self.use_fisher_z: mean_np = self._fisher_z(mean_np) mean_np = self._threshold(mean_np, self.preserve_fc_sign).astype(np.float32)
if self.pca_mean is not None and self.pca_components is not None: # PCA projection: (D,) → (K,) # Extract upper triangle the same way the MLP model does n = mean_np.shape[0] r, c = np.triu_indices(n, k=1) x_vec = mean_np[r, c] - self.pca_mean # centre x_pca = (self.pca_components @ x_vec).astype(np.float32) # (K,) # Return as (1, K) so collate_fn stacks to (B, 1, K); model flattens adj = torch.FloatTensor(x_pca).unsqueeze(0) # (1, K)
elif self.use_fc_variance: # Second channel: temporal std of FC — captures connection instability wfc = self._array(data, "fc_windows", "window_fc").astype(np.float32) wfc = self._pad_or_truncate_windows(wfc, self.max_windows) std_np = wfc.std(axis=0).astype(np.float32) adj = torch.FloatTensor(np.stack([mean_np, std_np], axis=0)) # (2, N, N)
else: adj = torch.FloatTensor(mean_np) # (N, N)
label = torch.tensor(int(data["label"]), dtype=torch.long) site_id = torch.tensor(self.site_to_int.get(site, -1), dtype=torch.long) return torch.FloatTensor(bold_windows), adj, label, site_id
# ------------------------------------------------------------------ @property def labels(self) -> list[int]: return [m["label"] for m in self._meta]
@property def num_nodes(self) -> int: data = np.load(self.npz_paths[0], allow_pickle=True) return data["mean_fc"].shape[0]
@property def num_windows(self) -> int: return self._meta[0]["num_windows"]app.pyAdded+212
brain_gcn/__init__.pyAdded
brain_gcn/__pycache__/__init__.cpython-311.pycAdded
brain_gcn/__pycache__/experiments.cpython-311.pycAdded
brain_gcn/__pycache__/finetune_main.cpython-311.pycAdded
brain_gcn/__pycache__/main.cpython-311.pycAdded
brain_gcn/__pycache__/population_main.cpython-311.pycAdded
brain_gcn/__pycache__/pretrain_main.cpython-311.pycAdded
brain_gcn/ablation.pyAdded+259
brain_gcn/cv_cli.pyAdded+74
brain_gcn/eval_cli.pyAdded+229
brain_gcn/experiments.pyAdded+152
brain_gcn/finetune_main.pyAdded+429
brain_gcn/hpo.pyAdded+285
brain_gcn/main.pyAdded+322
brain_gcn/models/__init__.pyAdded+32
brain_gcn/models/__pycache__/__init__.cpython-311.pycAdded
brain_gcn/models/__pycache__/advanced_models.cpython-311.pycAdded
brain_gcn/models/__pycache__/brain_gcn.cpython-311.pycAdded
brain_gcn/models/__pycache__/dynamic_fc.cpython-311.pycAdded
brain_gcn/models/__pycache__/mae.cpython-311.pycAdded
brain_gcn/models/__pycache__/population_gcn.cpython-311.pycAdded
brain_gcn/models/__pycache__/registry.cpython-311.pycAdded
brain_gcn/models/advanced_models.pyAdded+346
brain_gcn/models/brain_gcn.pyAdded+724
brain_gcn/models/dynamic_fc.pyAdded+100
brain_gcn/models/mae.pyAdded+297
brain_gcn/models/population_gcn.pyAdded+70
brain_gcn/models/registry.pyAdded+313
brain_gcn/population_main.pyAdded+288
brain_gcn/pretrain_main.pyAdded+263
brain_gcn/tasks/__init__.pyAdded+3
brain_gcn/tasks/__pycache__/__init__.cpython-311.pycAdded
brain_gcn/tasks/__pycache__/classification.cpython-311.pycAdded
brain_gcn/tasks/classification.pyAdded+244
brain_gcn/utils/__init__.pyAdded
brain_gcn/utils/__pycache__/__init__.cpython-311.pycAdded
brain_gcn/utils/__pycache__/graph_conv.cpython-311.pycAdded
brain_gcn/utils/__pycache__/grl.cpython-311.pycAdded
brain_gcn/utils/cross_validation.pyAdded+243
brain_gcn/utils/data/__init__.pyAdded+1
brain_gcn/utils/data/__pycache__/__init__.cpython-311.pycAdded
brain_gcn/utils/data/__pycache__/datamodule.cpython-311.pycAdded
brain_gcn/utils/data/__pycache__/dataset.cpython-311.pycAdded
brain_gcn/utils/data/__pycache__/download.cpython-311.pycAdded
brain_gcn/utils/data/__pycache__/functional_connectivity.cpython-311.pycAdded
brain_gcn/utils/data/__pycache__/population_graph.cpython-311.pycAdded
brain_gcn/utils/data/__pycache__/preprocess.cpython-311.pycAdded
brain_gcn/utils/data/datamodule.pyAdded+521
brain_gcn/utils/data/dataset.pyAdded+252