A structural MRI-based neural field for subject-specific TES lead-field prediction.
Installation · Quick start · Documentation · Training and evaluation · Citation
🧠 Structural MRI · ⚡ Neural field prediction · 🧭 World-RAS vectors · 🔬 Physics regularization
Head2Field predicts subject-specific electric fields for transcranial electrical stimulation. It uses T1-derived head anatomy, tissue conductivity, and electrode coordinates to build a reusable field basis. You can then combine these fields with different electrode currents without retraining the model for each montage.
Method overview. (A) Subject-specific lead-field prediction and field synthesis. (B) Multiscale anatomical encoding, electrode-conditioned decoding, and shared-response differences. Click the figure for the vector PDF.
🚀 Install · ⚡ Predict fields · 🧪 Train a model · 📖 Read the docs
This repository contains the official PyTorch implementation of:
Head2Field: A Structural MRI-Based Neural Field for Subject-Specific Lead-Field Prediction in Transcranial Electrical Stimulation
The package includes Head2Field v1 (Head2FieldV1), pretrained weights, and
commands for preprocessing, FEM label generation, training, and inference. The
model configuration is head2field_v1.json.
For each subject, the model predicts 75 source-to-Oz fields at Q gray- and
white-matter query locations. The output has shape [75, Q, 3], with world-RAS
components [Ex, Ey, Ez] in V/m. Each field corresponds to +1 mA at the source
and -1 mA at Oz. The model card describes the training
settings, intended use, and limitations.
In the manuscript's 40-subject test cohort, increasing training from 10 to 128 subjects reduced mean full-field relative L2 error from 0.118 to 0.070; omitting physics regularization increased it to 0.078. TIS plans optimized on the predicted basis achieved 94.9% of FEM-optimized focality under FEM re-evaluation. With inputs prepared, lead-field generation was approximately 100 times faster in the representative benchmark; the estimated end-to-end speedup was 4.7 times. Experimental conditions and downstream results are in evaluation and reproducibility.
| Workflow | Capability | Requirements |
|---|---|---|
| Inference | Prepared head inputs and model weights to 75 lead-field maps; no FEM labels required | Base installation |
| Preprocessing | T1 canonicalization, external CHARM head modeling, tissue conductivity, GM/WM queries, and standard electrode cap | preprocess extra and SimNIBS 4.6.0 |
| FEM labels | Oz-referenced lead-field generation, HDF5 validation, and voxel-label export | fem extra and SimNIBS 4.6.0 |
| Training | Data and physics losses, complete epochs, validation, early stopping, and local resume | training extra and CUDA-enabled PyTorch |
Use Python 3.11 or newer. Download the wheel, including the pretrained weights, from Release v0.1.0 and install it:
python -m pip install head2field-0.1.0-py3-none-any.whl
head2field-predict --helpTo install from source, run this in the repository root:
python -m pip install .For optional preprocessing and FEM dependencies, install the local wheel with:
python -m pip install './head2field-0.1.0-py3-none-any.whl[full]'From a source checkout, select the workflows as needed:
python -m pip install '.[preprocess]'
python -m pip install '.[fem]'
python -m pip install '.[training]'
python -m pip install '.[full]'Preprocessing and FEM label generation also need a separate installation of
SimNIBS/CHARM 4.6.0. Real-data training needs CUDA-enabled PyTorch. The
training extra is a convenience name for the training workflow; its Python
dependencies are already included in the base package. The full extra adds
SciPy and h5py.
See getting started for CUDA setup, copying the bundled configuration files, and troubleshooting installation.
Start with a prepared subject directory. The command uses the bundled pretrained weights by default:
head2field-predict \
--prepared-subject prepared_subject \
--output-dir prediction \
--device cpuSet --device cuda to use a GPU. The output directory contains
lead_field_float32.npy ([75, Q, 3]), the query coordinates and electrode-pair
records, and a manifest describing the inputs and outputs. The fields use the
world-RAS components and 1 mA convention described above. See
inference for query chunking and the Python API.
Use --weights /path/to/custom_v1.pth to select your own compatible weights.
head2field-prepare \
--input-t1 raw_t1.nii.gz \
--profile non_hcp_raw \
--template orientation_template.nii.gz \
--canonical-output-dir canonical_stage \
--head-model-output-dir charm_head_model \
--prepared-output-dir prepared_subjectUse non_hcp_raw for an unaligned T1 image. It performs one 6-DOF rigid
alignment; inspect the QC image before using the result. For HCP images that
already have ACPC alignment and distortion correction, use hcp_acpc and omit
--template to skip rigid estimation. Choose new paths for all output directories. The
preprocessing guide explains both profiles.
This example runs a small CPU FP32 training job. It needs neither participant data nor SimNIBS.
Run the commands from the source checkout, or copy the bundled configuration files into your working directory after installing the wheel.
head2field-make-synthetic-training --output-dir generated/synthetic-training
head2field-train-preflight \
--config configs/training_smoke_v1.json \
--synthetic-dataset generated/synthetic-training/synthetic_training_dataset_v1.json
head2field-train \
--config configs/training_smoke_v1.json \
--synthetic-dataset generated/synthetic-training/synthetic_training_dataset_v1.json \
--output-dir generated/synthetic-runThe generated arrays are useful for checking that training runs, but they do not represent head anatomy or measure prediction accuracy. See examples for the available checks.
To train on real data, first prepare the subject inputs and FEM labels. The
default training_v1.json uses AdamW with learning
rate 2e-4 and weight decay 1e-4, CUDA FP16, eight source pairs and 49,152
queries per step. It trains complete epochs for up to 25 epochs, with a
25-epoch cosine schedule and validation at the end of each epoch. Training
stops after five epochs without improvement; testing uses the best validation
checkpoint. The data loss uses 50/100 mm radial regions.
Follow training and resume for data preparation and checkpoint handling. Evaluation and reproducibility explains the data splits, FEM comparisons, and runtime measurements. Participant data and cohort splits are obtained separately.
- Documentation index
- Getting started: installation to prediction
- Input format and coordinate conventions
- T1 preprocessing and CHARM integration
- FEM label generation and validation
- Training, validation, and resume
- Inference and output format
- Model architecture
- Evaluation and reproducibility
- Machine-readable data formats
- Code guide: modules, tensor shapes, and execution flow
- Model card, data use, and weight format
See Contributing for development setup and choosing checks for a change. The code and dependency overview describes the package components, and the changelog records software changes. Maintainers can follow the release checklist when preparing a distribution.
English is the project's primary language. Simplified Chinese translations are available for all public guides; use the language link at the top to switch.
The source code is licensed under GPL-3.0-only. For third-party software and bundled model-weight provenance, see THIRD_PARTY_NOTICES.md and DATA_USE.md.
If you use Head2Field in research, cite the manuscript with this BibTeX entry:
@misc{chen_head2field,
author = {Chen, Xiayu and Wang, Xiaoxiao and Cao, Zixin and
Liu, Senlin and Liu, Zhichen and Wang, Zhengyang and
Du, Huiyu and Qiu, Bensheng and Wang, Yanming},
title = {{Head2Field}: A Structural {MRI}-Based Neural Field for
Subject-Specific Lead-Field Prediction in Transcranial
Electrical Stimulation},
note = {Manuscript}
}The manuscript title and author order follow CITATION.cff. See the citation guide for software citation and references for the methods and data sources.
Model development benefited from WU-Minn Human Connectome Project data; DATA_USE.md provides the acknowledgement and citation requirements.
