Deep Brain Stimulation (DBS) of the Subthalamic Nucleus (STN) is an effective treatment for motor symptoms in people with Parkinson's disease. Accurate placement of the stimulating electrode is essential for obtaining the best therapeutic outcome. During surgery, Microelectrode Recordings (MERs) provide neurophysiological information that helps clinicians identify whether the electrode is inside or outside the STN.
XAI-STIM is a deep-learning pipeline for classifying MERs as inside the STN or outside the STN. The model combines convolutional patch encoding, multi-head self-attention, and a multilayer perceptron. One-dimensional Grad-CAM maps provide explanations of the model predictions and can be reviewed together with the MER trajectories.
- Loads a NumPy signal matrix (
.npz) and its metadata table (.csv). - Prepares each MER using the pipeline described in [1]:
- resampling to 10 kHz;
- division into one-second windows;
- z-score normalization;
- 200 Hz high-pass filtering;
- notch filtering at higher harmonics of 50 Hz.
- Classifies each window as outside-STN (
0) or inside-STN (1). - Computes class probabilities and classification metrics.
- Generates one-dimensional Grad-CAM relevance maps.
- Exports figures and provides an interactive tool for explanations validation.
The complete sample workflow is available in main.ipynb. It uses the included sample_data.npz, sample_meta.csv, and trained model.
Python 3.12 is supported. To install the pinned requirements, run:
python -m pip install -r requirements.txtRun the notebook cells in order. The notebook performs:
data import
↓
resampling and filtering
↓
one-second windowing and inference
↓
1D Grad-CAM generation
The resulting figures are written to results/main_figures/:
01_raw_signals.png02_processed_signals.png03_inference.png04_gradcam.png
Inference automatically uses CUDA when a compatible GPU is available and otherwise runs on the CPU.
XAI-STIM/
├── main.ipynb
├── requirements.txt
├── sample_data.npz
├── sample_meta.csv
├── modules/
│ ├── 01_data_preparation/
│ │ ├── import_datasets.py
│ │ ├── compute_overlap.py
│ │ ├── processing.py
│ │ └── split_data.py
│ ├── 02_XAI-STIM/
│ │ ├── xai_stim.py
│ │ ├── inference.py
│ │ ├── gradcam1D.py
│ │ ├── compute_performances.py
│ │ └── trained_models/
│ │ ├── all_in_one.pt
│ │ ├── leave_AlphaMap_out.pt
│ │ ├── leave_LeadPoint_out.pt
│ │ └── leave_NeuroSet_out.pt
│ └── 03_expert_validation/
│ ├── traj2json.py
│ ├── sample_data.json
│ └── XAI_STIM_validation_tool.html
├── results/
│ ├── overlap.csv
│ ├── AiO_data_split.csv
│ └── main_figures/
└── README.md
This repository is intended to support checkpoint-based result replication: it distributes trained XAI-STIM checkpoints and utilities for data preparation, inference, performance evaluation, Grad-CAM generation, and expert-review data collection.
main.ipynb: complete demonstration of data import, resampling and filtering, window-level inference, probability-by-depth visualization, and Grad-CAM generation.sample_data.npz: MER signals from a representative patient hemisphere, stored as rows of a zero-padded matrix.sample_meta.csv: metadata associated with the sample recordings.
import_datasets.py: downloads and standardizes the LeadPoint, NeuroSet, and AlphaMap datasets into paired NPZ/CSV files.compute_overlap.py: calculates patient- and class-specific window overlaps to balance the number of training examples within patients and across datasets.processing.py: contains signal-loading, resampling, windowing, normalization, high-pass, and notch-filtering functions. Its normalization and filtering functions accept individual recordings or row-wise matrices of any non-zero sample length.split_data.py: organizes processed patient folders into the all-in-one training, validation, and test partitions defined inresults/AiO_data_split.csv.
xai_stim.py: defines the XAI-STIM neural-network architecture.inference.py: splits a complete 10 kHz recording into one-second windows with a configurable overlap (50% by default), applies the model preprocessing, loads a trained checkpoint, and returns one probability pair per window. It also supports matrices of prepared windows for batch validation exports.gradcam1D.py: computes one-dimensional Grad-CAM maps and provides the trajectory plotting function used by the sample notebook.compute_performances.py: computes overall and patient-wise AUC, F1 score, sensitivity, and specificity from prediction tables.trained_models/: contains the all-in-one checkpoint and the three leave-one-dataset-out checkpoints.
traj2json.py: converts an NPZ/CSV trajectory pair into the signal, prediction, and saliency JSON format required by the validation interface.XAI_STIM_validation_tool.html: browser-based interface for loading trajectories, reviewing model explanations, annotating STN-typical regions, and exporting expert responses to Likert-scale questionnaires.sample_data.json: ready-to-load example for the validation interface.
overlap.csv: patient- and class-specific overlap values used for one-second window extraction.AiO_data_split.csv: patient allocation and window counts for the all-in-one experiment.main_figures/: figures generated by the sample notebook.
Each trajectory requires one .npz signal file and one .csv metadata file with matching names or explicitly supplied paths.
The NPZ file must contain a two-dimensional matrix with shape N × M, where:
Nis the number of MERs;Mis the length of the longest MER in samples;- each MER occupies one row;
- shorter recordings are zero-padded to
M; - the unpadded length is stored in the metadata
lengthcolumn.
The CSV file must contain one row per signal and the following columns:
patient: patient identifier, for exampleP018;side: hemisphere,LEFTorRIGHT;dataset: dataset name used to infer the original sampling frequency;depth: estimated distance from target in micrometres;electrode: recording electrode identifier;length: signal length in samples before zero-padding;class: reference label, with0for outside-STN and1for inside-STN.
Example:
patient,side,dataset,depth,electrode,length,class
P018,LEFT,NeuroSet,-8000,Electrode1,100000,0
P018,LEFT,NeuroSet,-2000,Electrode1,100000,1
P018,LEFT,NeuroSet,4000,Electrode1,100000,0The predefined dataset sampling frequencies are:
| Dataset | Sampling frequency |
|---|---|
| LeadPoint | 24 kHz |
| NeuroSet | 20 kHz |
| AlphaMap | 12 kHz |
All signals are resampled to 10 kHz before classification.
Add the modules paths:
from pathlib import Path
import sys
root = Path("path/to/XAI-STIM")
sys.path.insert(0, str(root / "modules" / "01_data_preparation"))
sys.path.insert(0, str(root / "modules" / "02_XAI-STIM"))Pass a single recording sampled at 10 kHz to inference. It creates complete one-second windows, applies the preprocessing internally, and returns one class-probability pair per window. Set overlap to the fraction shared by consecutive windows; the default is 0.0 (0%).
from processing import load_matrix
from inference import inference
recording = load_matrix(root / "path" / "to" / "recording_10khz.npz")[0]
probabilities, starts = inference(
recording,
overlap=0.5,
return_window_starts=True,
)
predicted_classes = probabilities.argmax(axis=1)gradcam1D accepts complete recordings and resamples them when necessary:
from gradcam1D import gradcam1D
signals = load_matrix(root / "sample_data.npz")
maps, probabilities = gradcam1D(
signals,
sampling_frequency=20000,
target_class=1,
return_probabilities=True,
)For the complete, executable workflow and plotting code, refer to main.ipynb.
Generate a validation trajectory from the sample files with:
python modules/03_expert_validation/traj2json.pyThen open modules/03_expert_validation/XAI_STIM_validation_tool.html in a web browser and select the generated JSON file. The interface supports trajectory review, manual annotation of STN-typical regions, comparison with Grad-CAM explanations, and JSON export of the expert responses.
If you use XAI-STIM in your research, please cite the accompanying XAI-STIM publication. The complete bibliographic reference and DOI will be added upon publication.
[1] Sciscenti, F., Agostini, V., Rizzi, L., Lanotte, M., & Ghislieri, M. Interpretable Deep Learning for SubThalamic Nucleus Targeting from Microelectrode Recordings: A Multicentre Cross-Dataset Study - submitted for publication
[2] Sciscenti, F., Agostini, V., Rizzi, L., Lanotte, M., Ghislieri, M. (2026). Validation of an Interpretable AI System for Localizing the SubThalamic Nucleus During DBS Neurosurgery. Artificial Intelligence in Medicine (AIME) 2026. https://doi.org/10.1007/978-3-032-30710-1_30
The code and checkpoints in this repository support evaluation and explanation workflows with the released models. The open datasets used by the project are available at:
[3] Sciscenti, F., Agostini, V., Rizzi, L., Lanotte, M., & Ghislieri, M. (2026). ML-STIM: Machine Learning for SubThalamic nucleus Intraoperative Mapping [Dataset]. Zenodo, v2. https://doi.org/10.5281/zenodo.23259894
[4] Ciecierski, K., Mandat, T. (2023). RAW Deep Brain Stmulation Recordings [Dataset]. Mendeley Data, v1. https://doi.org/10.17632/tr93krswn2.1
Contributions are welcome. When reporting a bug, please include:
- A concise and descriptive title.
- Your operating system and Python version.
- Whether
main.ipynbruns successfully with the included sample data. - The complete error message and the pipeline step where it occurred.
Fabrizio Sciscenti, Ph.D. Candidate — H-MoVe
@FabrizioSciscenti — fabrizio.sciscenti@polito.it
Marco Ghislieri, Ph.D. — H-MoVe
@MarcoGhislieri — marco.ghislieri@polito.it
