|
1 | 1 | # CLAUDE.md |
2 | 2 |
|
3 | | -This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 3 | +This file provides guidance to Claude Code when working with this repository. |
4 | 4 |
|
5 | 5 | ## Essential Commands |
6 | 6 |
|
7 | 7 | ### Environment Setup |
8 | 8 | ```bash |
9 | | -# Create virtual environment and install package |
10 | 9 | uv venv |
11 | | -source .venv/bin/activate # On Windows: .venv\Scripts\activate |
12 | | -uv pip install -e . |
13 | | - |
14 | | -# Install development dependencies |
15 | | -uv pip install -e ".[dev]" |
16 | | - |
17 | | -# Install documentation dependencies |
18 | | -uv pip install -e ".[docs]" |
| 10 | +source .venv/bin/activate |
| 11 | +uv sync |
19 | 12 | ``` |
20 | 13 |
|
21 | | -**Important:** Always activate the virtual environment before running any Python scripts: |
| 14 | +Always activate the virtual environment before running scripts: |
22 | 15 | ```bash |
23 | 16 | source .venv/bin/activate && python scripts/qa/... |
24 | 17 | ``` |
25 | 18 |
|
26 | | -### Quality Assurance Pipeline |
| 19 | +### QA Pipeline |
27 | 20 |
|
28 | | -#### tSNR Analysis |
29 | 21 | ```bash |
30 | | -# Generate tSNR plots and HTML reports (volume space) |
31 | | -python scripts/qa/qa-plot-tsnr.py # All subjects |
32 | | -python scripts/qa/qa-plot-tsnr.py --subjects sub-001 # Specific subjects |
33 | | -python scripts/qa/qa-generate-html-reports-tsnr.py # Generate HTML reports |
34 | | - |
35 | | -# Example scripts for reference (from Budapest dataset) |
36 | | -python scripts/examples/compute-tsnr-volume.py sub-001 |
37 | | -python scripts/examples/compute-tsnr-fsaverage.py sub-001 |
38 | | -python scripts/examples/compute-isc-fsaverage.py |
39 | | -python scripts/examples/plot-tsnr-fsaverage.py |
40 | | -python scripts/examples/plot-isc-fsaverage.py |
41 | | -``` |
| 22 | +# tSNR analysis |
| 23 | +python scripts/qa/qa-save-tsnr-volume.py # Compute tSNR maps |
| 24 | +python scripts/qa/qa-plot-tsnr.py # Generate plots |
| 25 | +python scripts/qa/qa-generate-html-reports-tsnr.py # HTML reports |
42 | 26 |
|
43 | | -#### Motion Analysis |
44 | | -```bash |
45 | | -# Generate motion QA plots and HTML reports |
46 | | -python scripts/qa/qa-plot-motion.py # All subjects |
47 | | -python scripts/qa/qa-plot-motion.py --subjects sub-001 # Specific subjects |
48 | | -python scripts/qa/qa-generate-html-reports-motion.py # Generate HTML reports |
49 | | - |
50 | | -# Outputs: |
51 | | -# - Motion parameter traces (translation/rotation) per run |
52 | | -# - Framewise displacement (FD) traces per run |
53 | | -# - FD violin plots across runs and subjects |
54 | | -# - Interactive HTML reports with session navigation |
55 | | -# - Files saved to: data/derivatives/qa/motion/ |
56 | | -``` |
| 27 | +# Motion analysis |
| 28 | +python scripts/qa/qa-plot-motion.py # Generate plots |
| 29 | +python scripts/qa/qa-generate-html-reports-motion.py # HTML reports |
57 | 30 |
|
58 | | -### Documentation |
59 | | -```bash |
60 | | -# Build Jupyter Book documentation |
61 | | -jupyter-book build docs/ |
| 31 | +# ISC analysis |
| 32 | +python scripts/qa/qa-save-isc.py # Compute ISC |
| 33 | +python scripts/qa/qa-plot-isc.py # Surface plots |
62 | 34 |
|
63 | | -# Clean previous builds |
64 | | -jupyter-book clean docs/ |
| 35 | +# Process specific subjects |
| 36 | +python scripts/qa/qa-plot-tsnr.py --subjects sub-sid000005 |
65 | 37 | ``` |
66 | 38 |
|
67 | 39 | ### Code Quality |
68 | 40 | ```bash |
69 | | -# Format code with Black |
70 | | -black src/ scripts/qa/ notebooks/ |
71 | | - |
72 | | -# Sort imports |
73 | | -isort src/ scripts/qa/ |
74 | | - |
75 | | -# Type checking |
76 | | -mypy src/ |
77 | | - |
78 | | -# Linting |
79 | | -flake8 src/ scripts/qa/ |
| 41 | +ruff check src/ scripts/qa/ |
| 42 | +ruff format src/ scripts/qa/ |
80 | 43 | ``` |
81 | 44 |
|
82 | 45 | ## Code Architecture |
83 | 46 |
|
84 | | -### Package Structure |
85 | | -- **`src/hyperface/`**: Core analysis package with two main modules: |
86 | | - - `utils.py`: fMRI data processing utilities including `compute_tsnr()` and `clean_data()` functions |
87 | | - - `viz.py`: Visualization utilities for creating mosaic plots of brain volumes |
88 | | - |
89 | | -### Data Organization (BIDS Compliant) |
90 | | -- **`data/`**: Git submodule containing BIDS-formatted dataset (managed separately with datalad) |
91 | | - - `data/derivatives/fmriprep/`: Preprocessed fMRI data from fMRIprep |
92 | | - - `data/derivatives/freesurfer/`: FreeSurfer structural processing outputs |
93 | | - - **`data/derivatives/qa/`**: All QA metrics and figures generated by this pipeline |
94 | | - |
95 | | -### Analysis Pipeline Architecture |
96 | | -The QA pipeline follows a structured workflow: |
97 | | -1. **Data cleaning**: Uses fMRIprep confounds (motion, global signal, aCompCor, framewise displacement) + polynomial regressors |
98 | | -2. **tSNR computation**: Temporal signal-to-noise ratio after confound regression |
99 | | -3. **Motion analysis**: Extract and visualize motion parameters and framewise displacement from fMRIprep confounds |
100 | | -4. **Surface mapping**: Projects volume data to fsaverage cortical surface |
101 | | -5. **Inter-subject correlation**: Leave-one-out analysis for stimulus consistency |
102 | | -6. **Visualization**: Volume mosaics, cortical surface plots, motion traces, and interactive HTML reports |
103 | | - |
104 | | -### Script Organization |
105 | | -- **`scripts/qa/`**: Quality assurance pipeline scripts that output to `data/derivatives/qa/` |
106 | | -- **`scripts/stimuli/`**: Stimuli analysis and visualization scripts |
107 | | -- **`scripts/presentation/`**: Stimulus presentation code |
108 | | -- **`scripts/examples/`**: Example scripts from Budapest dataset (for reference) |
| 47 | +### Package Structure (`src/hyperface/`) |
| 48 | +- `utils.py` - fMRI processing (`compute_tsnr()`, `clean_data()`) |
| 49 | +- `viz.py` - Visualization (mosaic plots, surface plots) |
| 50 | +- `io.py` - Data loading and BIDS I/O |
| 51 | +- `isc.py` - Inter-subject correlation |
| 52 | +- `qa/` - QA pipeline (config, BIDS utilities, plotting) |
| 53 | + |
| 54 | +### Data Organization (BIDS) |
| 55 | +- `data/derivatives/fmriprep/` - Preprocessed fMRI data |
| 56 | +- `data/derivatives/freesurfer/` - FreeSurfer outputs |
| 57 | +- `data/derivatives/qa/` - QA outputs (generated) |
| 58 | + |
| 59 | +### Scripts |
| 60 | +- `scripts/qa/` - Main QA pipeline scripts |
| 61 | +- `scripts/presentation/` - Legacy stimulus presentation (do not modify) |
109 | 62 |
|
110 | 63 | ### Writing New Scripts |
111 | | -New scripts should use the centralized configuration system for maintainability: |
| 64 | + |
| 65 | +Use the centralized configuration: |
112 | 66 |
|
113 | 67 | ```python |
114 | 68 | from hyperface.qa import create_qa_argument_parser, get_config |
115 | 69 |
|
116 | 70 | def main(): |
117 | | - parser = create_qa_argument_parser( |
118 | | - description="Script description here.", |
119 | | - include_subjects=False, # Set True if processing per-subject |
120 | | - ) |
| 71 | + parser = create_qa_argument_parser(description="Script description.") |
121 | 72 | args = parser.parse_args() |
122 | | - |
123 | | - # Load configuration (handles --config and --data-dir CLI args) |
124 | 73 | config = get_config(config_path=args.config, data_dir=args.data_dir) |
125 | 74 |
|
126 | | - # Use paths from config instead of hardcoding |
127 | | - input_path = config.paths.stimuli_labels_dir / "behavioral-ratings.tsv" |
128 | | - output_dir = config.paths.stimuli_dir / "figures" |
| 75 | + # Use config.paths for all paths |
| 76 | + output_dir = config.paths.qa_base_dir / "output" |
129 | 77 | ``` |
130 | 78 |
|
131 | | -**Available config paths** (see `src/hyperface/qa/config.py`): |
132 | | -- `config.paths.data_dir` - Root BIDS dataset directory |
133 | | -- `config.paths.derivatives_dir` - BIDS derivatives directory |
134 | | -- `config.paths.fmriprep_dir` - fMRIprep output directory |
135 | | -- `config.paths.qa_base_dir` - Base QA output directory |
136 | | -- `config.paths.tsnr_dir`, `motion_dir`, `isc_dir`, `stimuli_dir` - QA subdirectories |
137 | | -- `config.paths.stimuli_labels_dir` - Stimuli labels input directory |
| 79 | +Config paths available: `data_dir`, `derivatives_dir`, `fmriprep_dir`, `qa_base_dir`, `tsnr_dir`, `motion_dir`, `isc_dir`, `stimuli_dir` |
138 | 80 |
|
139 | | -**Config file**: `src/hyperface/assets/qa_config.yaml` - Add new paths here as needed |
| 81 | +## Key Details |
140 | 82 |
|
141 | | -### Jupyter Book Integration |
142 | | -- **`docs/`**: Jupyter Book configuration for web documentation |
143 | | -- **`notebooks/`**: Analysis notebooks referenced in the book's table of contents |
144 | | -- Notebooks execute with `execute_notebooks: "off"` to avoid re-execution during builds |
145 | | - |
146 | | -## Key Implementation Details |
147 | | - |
148 | | -### fMRI Data Processing |
149 | | -The `hyperface.utils.compute_tsnr()` function implements a standardized confound regression approach: |
150 | | -- Removes 6 motion parameters + derivatives, global signal, framewise displacement |
151 | | -- Uses 6 aCompCor components for physiological noise |
152 | | -- Adds polynomial regressors (up to 2nd order) for trend removal |
153 | | -- Computes tSNR as mean/std after cleaning |
154 | | - |
155 | | -### Surface Data Handling |
156 | | -Scripts distinguish between volume (T1w space) and surface (fsaverage) processing: |
157 | | -- Volume scripts work with NIfTI files and create mosaic visualizations |
158 | | -- Surface scripts work with GIFTI files and create cortical surface plots using pycortex |
159 | | -- Both output standardized BIDS derivative filenames |
| 83 | +### fMRI Processing |
| 84 | +`compute_tsnr()` uses confound regression: |
| 85 | +- 6 motion parameters + derivatives |
| 86 | +- Global signal, framewise displacement |
| 87 | +- 6 aCompCor components |
| 88 | +- Polynomial regressors (2nd order) |
160 | 89 |
|
161 | 90 | ### Path Conventions |
162 | | -All scripts assume BIDS derivatives structure: |
163 | | -- Input paths: `data/derivatives/fmriprep/{subject}/func/` |
164 | | -- Output paths: `data/derivatives/qa/{analysis_type}/` |
165 | | -- Figure outputs: `data/derivatives/qa/{analysis_type}/figures/` |
166 | | - |
167 | | -## Data Dependencies |
| 91 | +- Inputs: `data/derivatives/fmriprep/{subject}/func/` |
| 92 | +- Outputs: `data/derivatives/qa/{analysis_type}/` |
| 93 | +- Figures: `data/derivatives/qa/{analysis_type}/figures/` |
168 | 94 |
|
169 | | -The repository expects preprocessed fMRI data from fMRIprep with specific naming conventions: |
170 | | -- Volume data: `*_space-T1w_desc-preproc_bold.nii.gz` |
171 | | -- Surface data: `*_space-fsaverage_hemi-{L,R}_bold.func.gii` |
| 95 | +### Expected File Naming (BIDS) |
| 96 | +- Volume: `*_space-T1w_desc-preproc_bold.nii.gz` |
| 97 | +- Surface: `*_space-fsaverage_hemi-{L,R}_bold.func.gii` |
172 | 98 | - Confounds: `*_desc-confounds_timeseries.tsv` |
173 | | -- Brain masks: `*_space-T1w_desc-brain_mask.nii.gz` |
0 commit comments