sMRI Skill (Modality Layer)
Overview
smri-skill is the NeuroClaw modality-layer interface skill responsible for structural MRI processing (T1w/T2w/FLAIR) and feature extraction.
It strictly follows NeuroClaw hierarchical design principles:
- This skill describes WHAT needs to be done and which tool skill to delegate to.
- It contains no implementation code and no direct shell commands.
- All concrete execution is delegated to tool skills and routed through
claw-shell.
Core workflow (never bypassed):
- Identify input type (DICOM / NIfTI / BIDS), modalities available (T1w only vs T1w+T2w vs T1w+FLAIR).
- Generate a numbered execution plan (steps, tools, outputs, runtime, risks).
- Present the plan and wait for explicit user confirmation (“YES” / “execute” / “proceed”).
- On confirmation, delegate each step via
claw-shell.
- Save outputs into a clean folder structure (
smri_output/).
Benchmark-Facing Default Mainline
For benchmark-style structural MRI tasks, start from the narrowest valid anatomical mainline and only add optional branches when the prompt or inputs explicitly require them.
- If the task is full structural MRI processing with no explicit T2w or FLAIR dependency:
- Default to
DICOM -> NIfTI if needed -> T1w mainline -> FreeSurfer recon-all -> feature/stat table export.
- Keep T2w, FLAIR, WMH, HCP structural, and DICOM re-export as optional branches, not default branches.
- If the task is only DICOM conversion:
- Delegate to
dcm2nii and stop there.
- If the task asks for quick volumetric preprocessing only:
- Prefer the
fsl-tool route rather than mixing FreeSurfer and HCP options in the mainline.
- If optional modalities or branches are missing:
- Mark them as skipped or blocked.
- Do not widen the task into unrelated structural subpipelines.
Avoid listing unrelated modality-adjacent tools in the primary plan for T1-only structural benchmarks.
Research use only.
Quick Reference (Common sMRI Tasks → Delegation Map)
| Task |
What needs to be done (high level) |
Delegate to which skill |
Expected outputs |
| DICOM → NIfTI |
Convert DICOM series to NIfTI (+ JSON) |
dcm2nii |
*_T1w.nii.gz, *_T2w.nii.gz, *_FLAIR.nii.gz, *.json |
| Organize to BIDS |
Create valid BIDS layout (anat/) |
bids-organizer |
bids/sub-*/anat/sub-*_T1w.nii.gz etc. |
| Fast structural preprocessing |
Brain extraction, bias correction, tissue segmentation, MNI registration |
fsl-tool (fsl_anat, BET/FAST/FLIRT/FNIRT) |
brain mask, tissue maps, transforms, QC |
| FreeSurfer Autorecon1 (volumetric preprocessing) |
Image conversion, motion correction, intensity normalization, registration to Talairach, bias correction, skull stripping |
freesurfer-tool (recon-all -autorecon1) |
orig.mgz, T1.mgz, brainmask.mgz, transforms/talairach.xfm |
| FreeSurfer Autorecon2 (subcortical segmentation & surface extraction) |
Tissue classification, white matter segmentation, surface tessellation, topology repair, white matter & pial surface generation |
freesurfer-tool (recon-all -autorecon2) |
?h.orig, ?h.white, ?h.pial, aseg.mgz, wm.mgz, surface QC |
| FreeSurfer Autorecon3 (spherical registration & parcellation) |
Spherical surface registration, cortical parcellation (Desikan-Killiany, Destrieux, DKT), anatomical statistics extraction, Brodmann area mapping |
freesurfer-tool (recon-all -autorecon3) |
?h.sphere.reg, ?h.aparc.annot, stats/?h.aparc.stats, ROI morphology tables |
| Full FreeSurfer pipeline (all 3 stages) |
Complete T1/T2 preprocessing with optional T2-pial refinement |
freesurfer-tool (recon-all -all -T2pial) |
Full FreeSurfer subject directory with surfaces, atlases, stats |
| Surface-based morphometry (quick) |
Cortical surfaces, parcellation, thickness, aseg/aparc stats (simplified) |
freesurfer-tool |
FreeSurfer subject dir, stats tables |
| HCP-grade structural pipeline |
PreFreeSurfer → FreeSurfer → PostFreeSurfer |
hcppipeline-tool |
HCP-style derivatives, surfaces, QC |
| BIDS anatomical derivatives (standardized) |
Run BIDS-App anatomical-only workflow |
fmriprep-tool (--anat-only) |
BIDS derivatives + QC report |
| WMH lesion segmentation |
Segment WMH from FLAIR+T1 |
wmh-segmentation (+ docker-env-manager if Docker ops needed) |
WMH mask NIfTI + run log |
| ROI-wise feature extraction |
Extract ROI stats from derived maps (GM prob, WMH mask, thickness maps in NIfTI, cortical thickness, surface-based stats) |
nilearn-tool (or fsl-tool fslstats, FreeSurfer mris_anatomical_stats) |
roi_stats_*.csv, morphology tables |
| Export results to DICOM |
Convert final NIfTI outputs back to DICOM series |
nii2dcm |
DICOM series for PACS/viewers |
Recommended Strategy (Decision Logic)
If the goal is quick brain extraction + tissue segmentation + MNI alignment (fast baseline, ~6 minutes):
- Prefer
fsl-tool (fsl_anat).
- Best for: quick QC, preprocessing, multi-subject batches.
If the goal is cortical thickness / surface parcellation / aseg-aparc volumetry (detailed surface morphometry):
- Prefer
freesurfer-tool (recon-all) with 3-stage execution (recommended for flexibility):
- Stage 1:
-autorecon1 (volumetric preprocessing, ~15-30 min)
- Produces: intensity-normalized brain image (
T1.mgz), Talairach registration (talairach.xfm), brain mask (brainmask.mgz).
- Use when: you need just preprocessing, quality control, or pial surface refinement before running surface extraction.
- Stage 2:
-autorecon2 (white matter segmentation & surface extraction, ~30-60 min)
- Produces: white matter mask (
wm.mgz), initial surfaces (?h.orig, ?h.white, ?h.pial), segmentation (aseg.mgz).
- Use when: you need cortical surfaces for thickness measurement, but haven't registered to standard space yet.
- Stage 3:
-autorecon3 (spherical registration & parcellation, ~15-30 min)
- Produces: registered sphere (
?h.sphere.reg), cortical parcellations (?h.aparc.annot, ?h.aparc.a2009s.annot, ?h.aparc.DKTatlas.annot), morphometric statistics (stats/?h.aparc.stats).
- Use when: you need full atlas-based ROI labels, cortical thickness maps, and anatomical statistics for group-level analysis.
- Quick execution: Run
recon-all -all -T2pial (if T2 available, ~2-3 hours total) for immediate full results.
- Best for: surface-based group analysis, cortical thickness studies, clinico-anatomical correlation.
If the goal is highest-quality, HCP-style surfaces and multimodal alignment:
- Prefer
hcppipeline-tool (structural stages).
- Best for: HCP datasets, publication-grade preprocessing, maximal anatomical detail.
If the dataset is already BIDS and you want standardized derivatives + QC (and future fMRI integration):
- Prefer
fmriprep-tool --anat-only (or full fMRIPrep if fMRI exists).
- Best for: reproducible BIDS-compliant preprocessing, multi-modal (fMRI-ready), open science.
If the goal is WMH lesion segmentation (vascular burden, aging, MS-like WM lesions):
- Use
wmh-segmentation (Docker-based); ensure Docker readiness via docker-env-manager if needed.
- Best for: FLAIR+T1 pathological lesion mapping.
If the goal is ROI-level tables from any NIfTI scalar map (thickness, volume, WMH count, etc.):
- Use
nilearn-tool to generate reproducible CSV feature tables.
- Best for: downstream statistical analysis, machine learning pipelines.
FreeSurfer Setup & Prerequisites (Ubuntu)
System Dependencies & Installation
For Ubuntu 22.04+ systems, freesurfer-tool must ensure:
1. System-level Dependencies
sudo apt-get update
sudo apt-get install -y \
tcsh bc perl tar libgomp1 build-essential \
wget vim-common libxmu-dev libxi-dev libxt-dev \
libx11-dev libglu1-mesa-dev libjpeg62-dev
2. FreeSurfer Installation & License
3. Environment Configuration (in shell profile, e.g., .bashrc)
export FREESURFER_HOME=/usr/local/freesurfer
export SUBJECTS_DIR=/path/to/your/freesurfer/subjects
source $FREESURFER_HOME/SetUpFreeSurfer.sh
When to Use Each Stage
| Stage |
Command |
Input |
Output |
Runtime |
Use Case |
| Autorecon1 |
recon-all -autorecon1 -i <T1.nii.gz> -subjid <sub> |
T1w NIfTI (mandatory) |
orig.mgz, T1.mgz, brainmask.mgz, Talairach xfm |
15–30 min |
Preprocessing only, QC checkpoints, T2-pial setup |
| Autorecon2 |
recon-all -autorecon2 -subjid <sub> |
(uses autorecon1 outputs) |
wm.mgz, surfaces (?h.orig, ?h.white, ?h.pial) |
30–60 min |
Cortical surface extraction, thickness measurement |
| Autorecon3 |
recon-all -autorecon3 -subjid <sub> |
(uses autorecon2 outputs) |
?h.sphere.reg, ?h.aparc.annot, stats tables |
15–30 min |
Atlas registration, ROI labels, group analysis ready |
| All (1-click) |
recon-all -all -T2pial -i <T1.nii.gz> -T2 <T2.nii.gz> -subjid <sub> |
T1w (required), T2w (optional but improves pial surface) |
Complete subject dir |
2–3 hours |
Full pipeline; T2-pial refines pial boundary |
Standard Output Layout (Recommended)
All outputs must be written under ./smri_output/:
smri_output/nifti/ (converted inputs if needed: *_T1w.nii.gz, *_T2w.nii.gz)
smri_output/bids/ (optional staging BIDS: bids/sub-*/anat/)
smri_output/fsl_anat/ (FSL structural outputs: brain mask, tissue maps, transforms)
smri_output/freesurfer/ (FreeSurfer SUBJECTS_DIR structure)
freesurfer/sub-01/mri/
orig.mgz, T1.mgz, T2.mgz (if T2 available)
brainmask.mgz, wm.mgz, norm.mgz
aseg.mgz, aparc+aseg.mgz, wmparc.mgz (after autorecon2+3)
transforms/talairach.xfm, cc_up.lta, etc.
freesurfer/sub-01/surf/
?h.orig, ?h.white, ?h.pial (surfaces)
?h.sphere.reg (registered sphere, after autorecon3)
?h.inflated, ?h.sphere (topological surfaces)
freesurfer/sub-01/label/
?h.aparc.annot, ?h.aparc.a2009s.annot, ?h.aparc.DKTatlas.annot (parcellations, after autorecon3)
?h.cortex.label, ?h.BA*.label (Brodmann areas, after autorecon3)
freesurfer/sub-01/stats/
?h.aparc.stats, ?h.aparc.a2009s.stats, ?h.aparc.DKTatlas.stats (cortical morphometry)
aseg.stats, wmparc.stats (subcortical volumes)
?h.curv.stats (curvature statistics)
smri_output/hcp/ (HCP structural outputs)
smri_output/fmriprep/ (fMRIPrep derivatives/QC pointers)
smri_output/wmh/ (WMH masks + logs)
smri_output/roi/ (ROI feature CSVs extracted from FreeSurfer stats or NIfTI-based ROIs)
smri_output/logs/ (claw-shell log tags / pointers, FreeSurfer recon-all logs)
Safety / Execution Rules (NeuroClaw)
- No execution without explicit user confirmation of the full numbered plan.
- All execution must be routed through
claw-shell.
- If a required dependency is missing, delegate installation planning to
dependency-planner.
- If Docker is required (e.g., WMH segmentation containers), coordinate via
docker-env-manager (plan → confirm → run).
Important Notes & Limitations
- Structural pipelines are long-running (especially FreeSurfer/HCP). Always provide realistic runtime + disk estimates in the plan:
- Autorecon1: 15–30 min, ~2 GB disk
- Autorecon2: 30–60 min, ~1 GB additional
- Autorecon3: 15–30 min, ~500 MB additional
- Full pipeline (
-all): 2–3 hours total, ~4–5 GB disk per subject
- FreeSurfer License: Required and must be placed at
$FREESURFER_HOME/license.txt. Obtain from https://surfer.nmr.mgh.harvard.edu/fswiki/License (free registration).
- T2-pial optimization: Include T2w image with
-T2 <file> -T2pial flags to refine pial surface in cortical regions with ambiguous GM/CSF boundaries. Recommended for HCP and high-resolution clinical datasets.
- System dependencies: Unix/Linux-only (macOS with Rosetta2 for ARM; Windows via WSL2). Requires X11 forwarding for visualization tools.
- ROI extraction: Surface-based ROIs from FreeSurfer
.annot files can be extracted via mris_anatomical_stats (built-in) or converted to NIfTI via mri_aparc2aseg for volumetric ROI analysis.
- Registration outputs: FreeSurfer surfaces (
?h.sphere.reg) are registered to average template space; enables cross-subject statistical inference via QDEC or nilearn.
- This skill is for research workflows; not for clinical decision-making.
When to Call This Skill
- Any request involving: T1w/T2w/FLAIR preprocessing, brain extraction, tissue segmentation, MNI registration, cortical thickness, FreeSurfer recon-all, HCP structural pipeline, WMH segmentation, or ROI-wise structural features.
Post-Execution Verification (Harness Integration)
After structural MRI processing completes, this skill automatically invokes harness-core's VerificationRunner to validate structural derivatives:
Integrated verification checks:
from skills.harness_core import VerificationRunner, AuditLogger
import nibabel as nib
import numpy as np
verifier = VerificationRunner(task_type="structural_mri_processing")
# 1. Structural brain extraction quality
verifier.add_check("brain_extraction_mask",
checker=lambda: verify_brain_mask_exists(output_dir),
severity="error"
)
# 2. Tissue segmentation (GM/WM/CSF) available and reasonable
verifier.add_check("tissue_segmentation",
checker=lambda: verify_tissue_maps_integrity(output_dir),
severity="error"
)
# 3. MNI registration transforms
verifier.add_check("mni_registration",
checker=lambda: verify_mni_transforms(output_dir),
severity="warning"
)
# 4. Cortical surface files (if FreeSurfer)
verifier.add_check("cortical_surfaces",
checker=lambda: verify_freesurfer_surfaces(output_dir),
severity="warning"
)
# 5. No NaN/Inf in structural maps
verifier.add_check("structural_data_integrity",
checker=lambda: verify_structural_no_nan_inf(output_dir),
severity="error"
)
# 6. Cortical thickness reasonable range (if available)
verifier.add_check("cortical_thickness_bounds",
checker=lambda: verify_thickness_range(output_dir, min_mm=1.0, max_mm=4.0),
severity="warning"
)
# 7. Volume statistics plausible
verifier.add_check("volume_statistics",
checker=lambda: verify_tissue_volume_ratios(output_dir),
severity="warning"
)
report = verifier.run(output_dir)
# Log verification results
logger = AuditLogger(log_file=f"{output_dir}/structural_verification.jsonl")
logger.log_validation(
task_name="structural_mri_processing",
checks_passed=len([r for r in report.results if r.passed]),
checks_failed=len([r for r in report.results if not r.passed]),
warnings=len([r for r in report.results if r.severity == "warning" and not r.passed]),
report_summary=report.to_dict()
)
if report.failed:
raise ValueError(f"Structural MRI verification failed: {report.summary}")
Output files generated:
{output_dir}/structural_verification.jsonl — structured audit log
{output_dir}/.structural_verification_timestamp — completion marker
Complementary / Related Skills
dcm2nii → DICOM → NIfTI
fsl-tool → fsl_anat / BET / FAST / FIRST / registration utilities
freesurfer-tool → cortical & subcortical morphometry + thickness/parcellation
hcppipeline-tool → HCP-style structural pipeline
fmriprep-tool → standardized BIDS-App anatomical-only derivatives + QC
wmh-segmentation → WMH lesion mask from FLAIR+T1 (Docker)
docker-env-manager → safe Docker operations (when needed)
nilearn-tool → ROI feature extraction from structural-derived NIfTI maps
nii2dcm → export final NIfTI results back to DICOM
dependency-planner + conda-env-manager → installation/environment management
claw-shell → mandatory safe execution layer
harness-core → automated verification and audit logging
Reference
Aligned with NeuroClaw modality-skill pattern (see fmri-skill, dwi-skill, eeg-skill).
Common sMRI toolchain: FSL (fast structural utilities), FreeSurfer (surface morphometry), HCP pipelines (HCP-grade structural processing), fMRIPrep (BIDS anatomical derivatives), Nilearn (ROI features on NIfTI maps), MARS-WMH (WMH segmentation via Docker).
Created At: 2026-03-26 01:09 HKT
Last Updated At: 2026-04-05 02:01 HKT
Author: chengwang96
1---2name: smri-skill3description: Use this skill whenever the user wants to process structural MRI (sMRI) such as T1w/T2w/FLAIR for brain extraction, bias correction, tissue segmentation (GM/WM/CSF), registration to MNI, cortical/subcortical parcellation, cortical thickness/volumetry (FreeSurfer), HCP-style structural preprocessing, WMH lesion segmentation (FLAIR+T1), ROI-wise feature extraction, or converting results back to DICOM. This is the NeuroClaw modality-layer interface: it plans WHAT to do and delegates execution to tool skills.4license: MIT License (NeuroClaw custom skill – freely modifiable within t5---6# sMRI Skill (Modality Layer)
7
8## Overview
9`smri-skill` is the NeuroClaw **modality-layer** interface skill responsible for **structural MRI** processing (T1w/T2w/FLAIR) and feature extraction.
10
11It strictly follows NeuroClaw hierarchical design principles:
12- This skill describes **WHAT needs to be done** and **which tool skill to delegate to**.
13- It contains **no implementation code** and **no direct shell commands**.
14- All concrete execution is delegated to tool skills and routed through `claw-shell`.
15
16**Core workflow (never bypassed):**
171. Identify input type (DICOM / NIfTI / BIDS), modalities available (T1w only vs T1w+T2w vs T1w+FLAIR).
182. Generate a **numbered execution plan** (steps, tools, outputs, runtime, risks).
193. Present the plan and wait for explicit user confirmation (“YES” / “execute” / “proceed”).
204. On confirmation, delegate each step via `claw-shell`.
215. Save outputs into a clean folder structure (`smri_output/`).
22
23## Benchmark-Facing Default Mainline
24
25For benchmark-style structural MRI tasks, start from the narrowest valid anatomical mainline and only add optional branches when the prompt or inputs explicitly require them.
26
27- If the task is full structural MRI processing with no explicit T2w or FLAIR dependency:
28 - Default to `DICOM -> NIfTI if needed -> T1w mainline -> FreeSurfer recon-all -> feature/stat table export`.
29 - Keep T2w, FLAIR, WMH, HCP structural, and DICOM re-export as optional branches, not default branches.
30- If the task is only DICOM conversion:
31 - Delegate to `dcm2nii` and stop there.
32- If the task asks for quick volumetric preprocessing only:
33 - Prefer the `fsl-tool` route rather than mixing FreeSurfer and HCP options in the mainline.
34- If optional modalities or branches are missing:
35 - Mark them as skipped or blocked.
36 - Do not widen the task into unrelated structural subpipelines.
37
38Avoid listing unrelated modality-adjacent tools in the primary plan for T1-only structural benchmarks.
39
40**Research use only.**
41
42---
43
44## Quick Reference (Common sMRI Tasks → Delegation Map)
45
46| Task | What needs to be done (high level) | Delegate to which skill | Expected outputs |
47|---|---|---|---|
48| DICOM → NIfTI | Convert DICOM series to NIfTI (+ JSON) | `dcm2nii` | `*_T1w.nii.gz`, `*_T2w.nii.gz`, `*_FLAIR.nii.gz`, `*.json` |
49| Organize to BIDS | Create valid BIDS layout (anat/) | `bids-organizer` | `bids/sub-*/anat/sub-*_T1w.nii.gz` etc. |
50| Fast structural preprocessing | Brain extraction, bias correction, tissue segmentation, MNI registration | `fsl-tool` (`fsl_anat`, BET/FAST/FLIRT/FNIRT) | brain mask, tissue maps, transforms, QC |
51| FreeSurfer Autorecon1 (volumetric preprocessing) | Image conversion, motion correction, intensity normalization, registration to Talairach, bias correction, skull stripping | `freesurfer-tool` (`recon-all -autorecon1`) | `orig.mgz`, `T1.mgz`, `brainmask.mgz`, `transforms/talairach.xfm` |
52| FreeSurfer Autorecon2 (subcortical segmentation & surface extraction) | Tissue classification, white matter segmentation, surface tessellation, topology repair, white matter & pial surface generation | `freesurfer-tool` (`recon-all -autorecon2`) | `?h.orig`, `?h.white`, `?h.pial`, `aseg.mgz`, `wm.mgz`, surface QC |
53| FreeSurfer Autorecon3 (spherical registration & parcellation) | Spherical surface registration, cortical parcellation (Desikan-Killiany, Destrieux, DKT), anatomical statistics extraction, Brodmann area mapping | `freesurfer-tool` (`recon-all -autorecon3`) | `?h.sphere.reg`, `?h.aparc.annot`, `stats/?h.aparc.stats`, ROI morphology tables |
54| Full FreeSurfer pipeline (all 3 stages) | Complete T1/T2 preprocessing with optional T2-pial refinement | `freesurfer-tool` (`recon-all -all -T2pial`) | Full FreeSurfer subject directory with surfaces, atlases, stats |
55| Surface-based morphometry (quick) | Cortical surfaces, parcellation, thickness, aseg/aparc stats (simplified) | `freesurfer-tool` | FreeSurfer subject dir, stats tables |
56| HCP-grade structural pipeline | PreFreeSurfer → FreeSurfer → PostFreeSurfer | `hcppipeline-tool` | HCP-style derivatives, surfaces, QC |
57| BIDS anatomical derivatives (standardized) | Run BIDS-App anatomical-only workflow | `fmriprep-tool` (`--anat-only`) | BIDS derivatives + QC report |
58| WMH lesion segmentation | Segment WMH from FLAIR+T1 | `wmh-segmentation` (+ `docker-env-manager` if Docker ops needed) | WMH mask NIfTI + run log |
59| ROI-wise feature extraction | Extract ROI stats from derived maps (GM prob, WMH mask, thickness maps in NIfTI, cortical thickness, surface-based stats) | `nilearn-tool` (or `fsl-tool` `fslstats`, FreeSurfer `mris_anatomical_stats`) | `roi_stats_*.csv`, morphology tables |
60| Export results to DICOM | Convert final NIfTI outputs back to DICOM series | `nii2dcm` | DICOM series for PACS/viewers |
61
62---
63
64## Recommended Strategy (Decision Logic)
65- If the goal is **quick brain extraction + tissue segmentation + MNI alignment** (fast baseline, ~6 minutes):
66 - Prefer `fsl-tool` (`fsl_anat`).
67 - Best for: quick QC, preprocessing, multi-subject batches.
68
69- If the goal is **cortical thickness / surface parcellation / aseg-aparc volumetry** (detailed surface morphometry):
70 - Prefer `freesurfer-tool` (`recon-all`) with **3-stage execution** (recommended for flexibility):
71 - **Stage 1: `-autorecon1`** (volumetric preprocessing, ~15-30 min)
72 - Produces: intensity-normalized brain image (`T1.mgz`), Talairach registration (`talairach.xfm`), brain mask (`brainmask.mgz`).
73 - Use when: you need just preprocessing, quality control, or pial surface refinement before running surface extraction.
74 - **Stage 2: `-autorecon2`** (white matter segmentation & surface extraction, ~30-60 min)
75 - Produces: white matter mask (`wm.mgz`), initial surfaces (`?h.orig`, `?h.white`, `?h.pial`), segmentation (`aseg.mgz`).
76 - Use when: you need cortical surfaces for thickness measurement, but haven't registered to standard space yet.
77 - **Stage 3: `-autorecon3`** (spherical registration & parcellation, ~15-30 min)
78 - Produces: registered sphere (`?h.sphere.reg`), cortical parcellations (`?h.aparc.annot`, `?h.aparc.a2009s.annot`, `?h.aparc.DKTatlas.annot`), morphometric statistics (`stats/?h.aparc.stats`).
79 - Use when: you need full atlas-based ROI labels, cortical thickness maps, and anatomical statistics for group-level analysis.
80 - **Quick execution**: Run `recon-all -all -T2pial` (if T2 available, ~2-3 hours total) for immediate full results.
81 - Best for: surface-based group analysis, cortical thickness studies, clinico-anatomical correlation.
82
83- If the goal is **highest-quality, HCP-style surfaces and multimodal alignment**:
84 - Prefer `hcppipeline-tool` (structural stages).
85 - Best for: HCP datasets, publication-grade preprocessing, maximal anatomical detail.
86
87- If the dataset is already **BIDS** and you want **standardized derivatives + QC** (and future fMRI integration):
88 - Prefer `fmriprep-tool --anat-only` (or full fMRIPrep if fMRI exists).
89 - Best for: reproducible BIDS-compliant preprocessing, multi-modal (fMRI-ready), open science.
90
91- If the goal is **WMH lesion segmentation** (vascular burden, aging, MS-like WM lesions):
92 - Use `wmh-segmentation` (Docker-based); ensure Docker readiness via `docker-env-manager` if needed.
93 - Best for: FLAIR+T1 pathological lesion mapping.
94
95- If the goal is **ROI-level tables** from any NIfTI scalar map (thickness, volume, WMH count, etc.):
96 - Use `nilearn-tool` to generate reproducible CSV feature tables.
97 - Best for: downstream statistical analysis, machine learning pipelines.
98
99---
100
101## FreeSurfer Setup & Prerequisites (Ubuntu)
102
103### System Dependencies & Installation
104For Ubuntu 22.04+ systems, `freesurfer-tool` must ensure:
105
106#### 1. System-level Dependencies
107```bash
108sudo apt-get update
109sudo apt-get install -y \
110 tcsh bc perl tar libgomp1 build-essential \
111 wget vim-common libxmu-dev libxi-dev libxt-dev \
112 libx11-dev libglu1-mesa-dev libjpeg62-dev
113```
114
115#### 2. FreeSurfer Installation & License
116- **Download** FreeSurfer 7.4.1 (or newer): Install to `/usr/local/freesurfer/`
117- **License file**: Obtain from https://surfer.nmr.mgh.harvard.edu/fswiki/License → place at `/usr/local/freesurfer/license.txt`
118
119#### 3. Environment Configuration (in shell profile, e.g., `.bashrc`)
120```bash
121export FREESURFER_HOME=/usr/local/freesurfer
122export SUBJECTS_DIR=/path/to/your/freesurfer/subjects
123source $FREESURFER_HOME/SetUpFreeSurfer.sh
124```
125
126### When to Use Each Stage
127
128| Stage | Command | Input | Output | Runtime | Use Case |
129|---|---|---|---|---|---|
130| **Autorecon1** | `recon-all -autorecon1 -i <T1.nii.gz> -subjid <sub>` | T1w NIfTI (mandatory) | `orig.mgz`, `T1.mgz`, `brainmask.mgz`, Talairach xfm | 15–30 min | Preprocessing only, QC checkpoints, T2-pial setup |
131| **Autorecon2** | `recon-all -autorecon2 -subjid <sub>` | (uses autorecon1 outputs) | `wm.mgz`, surfaces (`?h.orig`, `?h.white`, `?h.pial`) | 30–60 min | Cortical surface extraction, thickness measurement |
132| **Autorecon3** | `recon-all -autorecon3 -subjid <sub>` | (uses autorecon2 outputs) | `?h.sphere.reg`, `?h.aparc.annot`, stats tables | 15–30 min | Atlas registration, ROI labels, group analysis ready |
133| **All (1-click)** | `recon-all -all -T2pial -i <T1.nii.gz> -T2 <T2.nii.gz> -subjid <sub>` | T1w (required), T2w (optional but improves pial surface) | Complete subject dir | 2–3 hours | Full pipeline; T2-pial refines pial boundary |
134
135---
136
137## Standard Output Layout (Recommended)
138All outputs must be written under `./smri_output/`:
139- `smri_output/nifti/` (converted inputs if needed: `*_T1w.nii.gz`, `*_T2w.nii.gz`)
140- `smri_output/bids/` (optional staging BIDS: `bids/sub-*/anat/`)
141- `smri_output/fsl_anat/` (FSL structural outputs: brain mask, tissue maps, transforms)
142- `smri_output/freesurfer/` (FreeSurfer SUBJECTS_DIR structure)
143 - `freesurfer/sub-01/mri/`
144 - `orig.mgz`, `T1.mgz`, `T2.mgz` (if T2 available)
145 - `brainmask.mgz`, `wm.mgz`, `norm.mgz`
146 - `aseg.mgz`, `aparc+aseg.mgz`, `wmparc.mgz` (after autorecon2+3)
147 - `transforms/talairach.xfm`, `cc_up.lta`, etc.
148 - `freesurfer/sub-01/surf/`
149 - `?h.orig`, `?h.white`, `?h.pial` (surfaces)
150 - `?h.sphere.reg` (registered sphere, after autorecon3)
151 - `?h.inflated`, `?h.sphere` (topological surfaces)
152 - `freesurfer/sub-01/label/`
153 - `?h.aparc.annot`, `?h.aparc.a2009s.annot`, `?h.aparc.DKTatlas.annot` (parcellations, after autorecon3)
154 - `?h.cortex.label`, `?h.BA*.label` (Brodmann areas, after autorecon3)
155 - `freesurfer/sub-01/stats/`
156 - `?h.aparc.stats`, `?h.aparc.a2009s.stats`, `?h.aparc.DKTatlas.stats` (cortical morphometry)
157 - `aseg.stats`, `wmparc.stats` (subcortical volumes)
158 - `?h.curv.stats` (curvature statistics)
159- `smri_output/hcp/` (HCP structural outputs)
160- `smri_output/fmriprep/` (fMRIPrep derivatives/QC pointers)
161- `smri_output/wmh/` (WMH masks + logs)
162- `smri_output/roi/` (ROI feature CSVs extracted from FreeSurfer stats or NIfTI-based ROIs)
163- `smri_output/logs/` (claw-shell log tags / pointers, FreeSurfer recon-all logs)
164
165---
166
167## Safety / Execution Rules (NeuroClaw)
168- **No execution without explicit user confirmation** of the full numbered plan.
169- All execution must be routed through `claw-shell`.
170- If a required dependency is missing, delegate installation planning to `dependency-planner`.
171- If Docker is required (e.g., WMH segmentation containers), coordinate via `docker-env-manager` (plan → confirm → run).
172
173---
174
175## Important Notes & Limitations
176- **Structural pipelines are long-running** (especially FreeSurfer/HCP). Always provide realistic runtime + disk estimates in the plan:
177 - Autorecon1: 15–30 min, ~2 GB disk
178 - Autorecon2: 30–60 min, ~1 GB additional
179 - Autorecon3: 15–30 min, ~500 MB additional
180 - Full pipeline (`-all`): 2–3 hours total, ~4–5 GB disk per subject
181- **FreeSurfer License**: Required and must be placed at `$FREESURFER_HOME/license.txt`. Obtain from https://surfer.nmr.mgh.harvard.edu/fswiki/License (free registration).
182- **T2-pial optimization**: Include T2w image with `-T2 <file> -T2pial` flags to refine pial surface in cortical regions with ambiguous GM/CSF boundaries. Recommended for HCP and high-resolution clinical datasets.
183- **System dependencies**: Unix/Linux-only (macOS with Rosetta2 for ARM; Windows via WSL2). Requires X11 forwarding for visualization tools.
184- **ROI extraction**: Surface-based ROIs from FreeSurfer `.annot` files can be extracted via `mris_anatomical_stats` (built-in) or converted to NIfTI via `mri_aparc2aseg` for volumetric ROI analysis.
185- **Registration outputs**: FreeSurfer surfaces (`?h.sphere.reg`) are registered to average template space; enables cross-subject statistical inference via QDEC or nilearn.
186- **This skill is for research workflows**; not for clinical decision-making.
187
188---
189
190## When to Call This Skill
191- Any request involving: T1w/T2w/FLAIR preprocessing, brain extraction, tissue segmentation, MNI registration, cortical thickness, FreeSurfer recon-all, HCP structural pipeline, WMH segmentation, or ROI-wise structural features.
192
193## Post-Execution Verification (Harness Integration)
194
195After structural MRI processing completes, this skill **automatically invokes harness-core's VerificationRunner** to validate structural derivatives:
196
197**Integrated verification checks**:
198
199```python
200from skills.harness_core import VerificationRunner, AuditLogger
201import nibabel as nib
202import numpy as np
203
204verifier = VerificationRunner(task_type="structural_mri_processing")
205
206# 1. Structural brain extraction quality
207verifier.add_check("brain_extraction_mask",
208 checker=lambda: verify_brain_mask_exists(output_dir),
209 severity="error"
210)
211
212# 2. Tissue segmentation (GM/WM/CSF) available and reasonable
213verifier.add_check("tissue_segmentation",
214 checker=lambda: verify_tissue_maps_integrity(output_dir),
215 severity="error"
216)
217
218# 3. MNI registration transforms
219verifier.add_check("mni_registration",
220 checker=lambda: verify_mni_transforms(output_dir),
221 severity="warning"
222)
223
224# 4. Cortical surface files (if FreeSurfer)
225verifier.add_check("cortical_surfaces",
226 checker=lambda: verify_freesurfer_surfaces(output_dir),
227 severity="warning"
228)
229
230# 5. No NaN/Inf in structural maps
231verifier.add_check("structural_data_integrity",
232 checker=lambda: verify_structural_no_nan_inf(output_dir),
233 severity="error"
234)
235
236# 6. Cortical thickness reasonable range (if available)
237verifier.add_check("cortical_thickness_bounds",
238 checker=lambda: verify_thickness_range(output_dir, min_mm=1.0, max_mm=4.0),
239 severity="warning"
240)
241
242# 7. Volume statistics plausible
243verifier.add_check("volume_statistics",
244 checker=lambda: verify_tissue_volume_ratios(output_dir),
245 severity="warning"
246)
247
248report = verifier.run(output_dir)
249
250# Log verification results
251logger = AuditLogger(log_file=f"{output_dir}/structural_verification.jsonl")
252logger.log_validation(
253 task_name="structural_mri_processing",
254 checks_passed=len([r for r in report.results if r.passed]),
255 checks_failed=len([r for r in report.results if not r.passed]),
256 warnings=len([r for r in report.results if r.severity == "warning" and not r.passed]),
257 report_summary=report.to_dict()
258)
259
260if report.failed:
261 raise ValueError(f"Structural MRI verification failed: {report.summary}")
262```
263
264**Output files generated**:
265- `{output_dir}/structural_verification.jsonl` — structured audit log
266- `{output_dir}/.structural_verification_timestamp` — completion marker
267
268## Complementary / Related Skills
269
270- `dcm2nii` → DICOM → NIfTI
271- `fsl-tool` → fsl_anat / BET / FAST / FIRST / registration utilities
272- `freesurfer-tool` → cortical & subcortical morphometry + thickness/parcellation
273- `hcppipeline-tool` → HCP-style structural pipeline
274- `fmriprep-tool` → standardized BIDS-App anatomical-only derivatives + QC
275- `wmh-segmentation` → WMH lesion mask from FLAIR+T1 (Docker)
276- `docker-env-manager` → safe Docker operations (when needed)
277- `nilearn-tool` → ROI feature extraction from structural-derived NIfTI maps
278- `nii2dcm` → export final NIfTI results back to DICOM
279- `dependency-planner` + `conda-env-manager` → installation/environment management
280- `claw-shell` → mandatory safe execution layer
281- `harness-core` → automated verification and audit logging
282
283---
284
285## Reference
286Aligned with NeuroClaw modality-skill pattern (see `fmri-skill`, `dwi-skill`, `eeg-skill`).
287Common sMRI toolchain: FSL (fast structural utilities), FreeSurfer (surface morphometry), HCP pipelines (HCP-grade structural processing), fMRIPrep (BIDS anatomical derivatives), Nilearn (ROI features on NIfTI maps), MARS-WMH (WMH segmentation via Docker).
288
289Created At: 2026-03-26 01:09 HKT
290Last Updated At: 2026-04-05 02:01 HKT
291Author: chengwang96