Bilal Shihab / Projects

From MRI Scanner to Electrode Target

A pipeline that starts with the files an MRI scanner writes and ends with an electrode target in a robot's coordinates. It finds the hand area of motor cortex from a finger-tapping fMRI scan, lines that map up with the anatomy, and lets you plan the insertion path in a browser:

Solo projectNov 2026
Spec
Input
DICOM (GE, Siemens; uncompressed, JPEG lossless, JPEG 2000) and NIfTI
Data
OpenNeuro ds000114, 10 volunteers, 2 sessions each; dcm_qa test series
Frames
Voxel, DICOM LPS, scanner RAS, MNI (approximate), robot
Registration
Rigid: boundary-based, from the scanner alignment and from a mutual-information fit; MNI template: 12-parameter affine
fMRI model
GLM, SPM HRF, AR(1) noise; matches nilearn at r = 0.99997
Planner
Plain JavaScript and canvas, local Python server
Checks
27 unit tests, 10 validation scripts with bars set before each run
The planner in a browser. Axial, coronal and sagittal slices of a T1 scan with an orange fMRI map on the right motor strip; a target marker T on the map and an entry marker E on the scalp; below, a slice along the 34 mm path and a slice across it at the tip; a side panel with RAS, LPS, voxel and approximate MNI coordinates.
The planner on volunteer 1, first session. Target at the finger-movement map's peak, entry snapped to the scalp, 34 mm path.

Why I built it

In January 2025 I wrote a small tool that uploaded medical scans to Google Cloud. It stored the files but never used what was inside them: where each voxel sits in the patient. This project is the step after storage, from the scanner's files to a point a surgical robot could aim for, with every stage checked against tools I didn't write.

How it works

Every image is an array plus one 4x4 matrix that maps voxel indices to millimetres in the scanner's frame. DICOM stores that geometry in its own patient frame (x to the patient's left, y to the back), NIfTI in the opposite sign for x and y, and most bugs in this kind of software come from mixing them. Every point in a plan carries the name of its frame.

The fMRI goes through motion correction, then a general linear model that compares finger blocks against the foot and lips blocks. The fMRI is registered to the anatomical scan by boundary-based registration (the method behind FreeSurfer's bbregister): it finds the white-matter surface in the anatomy and turns the fMRI until the grey/white contrast lines up along the whole surface. It starts twice, from the scanner's own alignment and from a mutual-information fit, and keeps the better answer. A template brain from MNI gives a brain mask and approximate MNI coordinates for each point.

The planner reslices the anatomy on the fly and draws the fMRI map by carrying each pixel's position through the registration into the map's own grid, so nothing is resampled ahead of time. You drop a target and an entry point; it warns when the entry is off the scalp and can slide it onto the scalp along the planned line. The exported plan goes through a fiducial registration into a simulated robot frame, with the target error predicted by Fitzpatrick's formula.

The hard part

I wrote pass bars before each run and kept the failures. Two registration runs failed before the current method. The first disagreed with SimpleITK by 14 to 19 mm; overlays showed SimpleITK pulled onto the scalp, but my own answer also moved when I started it from different places, by up to 2.4 mm. A crude skull strip didn't fix it. Boundary-based registration with a template brain mask did: the same answer from every start, and a loop through both scanning days that closes to about a millimetre.

The worst failures were silent. When I ran the two-day comparison across all ten volunteers, a loop check showed that some registrations had quietly gone wrong: one template fit sat over 100 mm off, and in four scans the fMRI-to-anatomy registration was rotated between 21 and 121 degrees. My stability checks had only ever been run on the first volunteer, who happened to be fine. The fix was to start the boundary search from the scanner's own alignment as well, keep the better answer, and have the pipeline refuse to build a plan when a registration fails a plausibility check. I wrote a new pass bar for all twenty scans before rerunning; nine of ten volunteers met it and the tenth missed by 0.21 mm.

Before that, I had an independent agent review the code cold. It found 11 problems. The worst was a coordinate bug: exported plans gave the target's voxel index in the cropped volume the planner draws, while naming the original scan file, about 3 cm off. My checks hadn't caught it because they compared a formula with itself. The replacement checks recompute every matrix from the source files and render a left-right phantom through the planner's real drawing code; swapping left and right now fails six of them.

A second review, run after the fixes for the silent failures, found seven more. The one that mattered most was about my best-looking number. Inside the hand area, the centre of the active cluster landed within 10 mm across the two days for 8 of 10 volunteers, but the region alone does most of that: replace the cluster with the whole region and all 10 pass, and cluster centres from two different people are within 10 mm about half the time. The same person was still closer (a median 3 mm apart against 10 mm), so the map does carry something individual, but much less than "8 of 10" suggests. It also found that my loop check measured on a sphere in the wrong session's frame; fixing it moved the volunteer who missed from 2.12 to 2.21 mm.

One mistake was my own reasoning. The first finger map lit up most of the brain in one session, and I blamed head and jaw movement during the foot and lips blocks. The real cause was the first volume of each run, 26% brighter because the scanner hadn't settled, which was also my motion-correction reference. I withdrew the first explanation in the log.

What it doesn't do yet

The fMRI isn't corrected for EPI distortion, because this dataset has no field map for the run, so near the sinuses the map can sit a few millimetres off. Registration is rigid and nothing accounts for the brain shifting once the skull is open. The robot and tracker are simulated. Left and right come from the scan headers: my code's handling is tested, but nothing in this dataset can confirm the headers themselves.

I built this with Claude Code (Claude Opus 5.5), which wrote the code and the first draft of this write-up. The data sources, code, every number and every failed run are in the repository.