Skip to content

Commit 69764bf

Browse files
authored
Merge pull request #55 from openneuropet/no-anatomical
If no T1w is present, attempt to deface with a template t1w contained in petdeface/data/*
2 parents 7b469aa + b8b7517 commit 69764bf

File tree

15 files changed

+1146
-3392
lines changed

15 files changed

+1146
-3392
lines changed

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ This software can be installed via source or via pip from PyPi with `pip install
3636

3737
```bash
3838
usage: petdeface.py [-h] [--anat_only] [--participant_label PARTICIPANT_LABEL [PARTICIPANT_LABEL ...]] [--docker] [--singularity] [--n_procs N_PROCS]
39-
[--skip_bids_validator] [--version] [--placement PLACEMENT] [--remove_existing] [--preview_pics]
39+
[--skip_bids_validator] [--version] [--placement PLACEMENT] [--remove_existing]
4040
[--participant_label_exclude participant_label_exclude [participant_label_exclude ...]] [--session_label SESSION [SESSION ...]]
41-
[--session_label_exclude session_label_exclude [session_label_exclude ...]]
41+
[--session_label_exclude session_label_exclude [session_label_exclude ...]] [--open_browser]
4242
bids_dir [output_dir] [analysis_level]
4343

4444
PetDeface
@@ -63,15 +63,18 @@ options:
6363
Where to place the defaced images. Options are 'adjacent': next to the bids_dir (default) in a folder appended with _defaced'inplace':
6464
defaces the dataset in place, e.g. replaces faced PET and T1w images w/ defaced at bids_dir'derivatives': does all of the defacing within
6565
the derivatives folder in bids_dir.
66-
--remove_existing, -r
67-
Remove existing output files in output_dir.
68-
--preview_pics Create preview pictures of defacing, defaults to false for docker
66+
--remove_existing, -r Remove existing output files in output_dir.
67+
--preview_pics Create preview pictures of defacing, defaults to false for docker (only works if FreeView is installed on machine)
6968
--participant_label_exclude participant_label_exclude [participant_label_exclude ...]
7069
Exclude a subject(s) from the defacing workflow. e.g. --participant_label_exclude sub-01 sub-02
7170
--session_label SESSION [SESSION ...]
7271
Select only a specific session(s) to include in the defacing workflow
7372
--session_label_exclude session_label_exclude [session_label_exclude ...]
7473
Select a specific session(s) to exclude from the defacing workflow
74+
--use_template_anat Use template anatomical image when no T1w is available for PET scans.
75+
Options: 't1' (included T1w template), 'mni' (MNI template), or 'pet'
76+
(averaged PET image).
77+
--open_browser Open browser to show QA reports after completion
7578
```
7679
7780
Working example usage:
@@ -80,6 +83,26 @@ Working example usage:
8083
petdeface /inputfolder /outputfolder --n_procs 16 --skip_bids_validator --placement adjacent
8184
```
8285
86+
### Template Anatomical Images
87+
88+
When PET scans lack corresponding T1w anatomical images, PETdeface can use template anatomical images for registration and defacing. Three options are available:
89+
90+
- **`--use_template_anat t1`**: Uses a T1w template included with the PETdeface library
91+
- **`--use_template_anat mni`**: Uses the MNI standard brain template
92+
- **`--use_template_anat pet`**: Creates a template by averaging the PET data across time
93+
94+
**Important**: When using template anatomical images, it's crucial to validate the defacing quality. Inspect the output using the generated HTML report (with `--open_browser`) or a NIfTI viewer to ensure the defacing is valid for your data.
95+
96+
**Recommended workflow for subjects missing T1w images**:
97+
1. First, exclude subjects missing T1w using `--participant_label_exclude`
98+
2. Run defacing on subjects with T1w images
99+
3. Then run defacing separately on subjects missing T1w using `--participant_label` and test different templates (`t1`, `mni`, `pet`) to determine which works best for your data
100+
101+
Example usage with template anatomical:
102+
```bash
103+
petdeface /inputfolder /outputfolder --use_template_anat t1 --n_procs 16
104+
```
105+
83106
### Docker Usage
84107

85108
Requirements:

data/participants.tsv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
participant_id height weight age gender
22
sub-01 178 58 28 male
3+
sub-mni305 200 65 10 n/a
34
sub-02 178 58 28 male
-26 MB
Binary file not shown.
11.5 MB
Binary file not shown.
3.13 MB
Binary file not shown.

docs/usage.rst

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ options:
123123
Select only a specific session(s) to include in the defacing workflow
124124
--session_label_exclude session_label_exclude [session_label_exclude ...]
125125
Select a specific session(s) to exclude from the defacing workflow
126+
--use_template_anat Use template anatomical image when no T1w is available for PET scans.
127+
Options: 't1' (included T1w template), 'mni' (MNI template), or 'pet'
128+
(averaged PET image).
129+
--open_browser Following defacing this flag will open the browser to view the defacing results
126130

127131
Docker Based
128132
------------
@@ -174,4 +178,31 @@ PETdeface will do it's best to locate a valid FreeSurfer license file on the hos
174178
to the container by checking `FREESURFER_HOME` and `FREESURFER_LICENSE` environment variables. If you
175179
receive an error message relating to the FreeSurfer license file, try setting and exporting the
176180
`FREESURFER_LICENSE` environment variable to the location of the FreeSurfer license file on the host
177-
machine.
181+
machine.
182+
183+
Template Anatomical Images
184+
-------------------------
185+
186+
When PET scans lack corresponding T1w anatomical images, PETdeface can use template anatomical images for
187+
registration and defacing. Three options are available:
188+
189+
- **`--use_template_anat t1`**: Uses a T1w template included with the PETdeface library
190+
- **`--use_template_anat mni`**: Uses the MNI standard brain template
191+
- **`--use_template_anat pet`**: Creates a template by averaging a subjects PET image and using that averaged image in place of a T1 image
192+
193+
**Important**: When using template anatomical images, it's crucial to validate the defacing quality.
194+
Inspect the output using the generated HTML report (with `--open_browser`) or a NIfTI viewer to ensure
195+
the defacing is valid for your data.
196+
197+
**Recommended workflow for subjects missing T1w images**:
198+
199+
1. First, exclude subjects missing T1w using `--participant_label_exclude`
200+
2. Run defacing on subjects with T1w images
201+
3. Then run defacing separately on subjects missing T1w using `--participant_label` and test
202+
different templates (`t1`, `mni`, `pet`) to determine which works best for your data
203+
204+
Example usage with template anatomical:
205+
206+
.. code-block:: bash
207+
208+
petdeface /inputfolder /outputfolder --use_template_anat t1 --n_procs 16

petdeface/__init__.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""
2+
PET Deface - A nipype PET and MR defacing pipeline for BIDS datasets.
3+
4+
This package provides tools for defacing PET and MR images using FreeSurfer's MiDeFace.
5+
"""
6+
7+
import sys
8+
from pathlib import Path
9+
10+
# Add the package directory to sys.path when running as script
11+
if __name__ == "__main__" or (
12+
len(sys.argv) > 0 and sys.argv[0].endswith("petdeface.py")
13+
):
14+
# Running as script - add current directory to path
15+
package_dir = Path(__file__).parent
16+
if str(package_dir) not in sys.path:
17+
sys.path.insert(0, str(package_dir))
18+
19+
# Import main components
20+
try:
21+
from .petdeface import PetDeface, deface, cli, main
22+
from .mideface import ApplyMideface, Mideface
23+
from .pet import WeightedAverage
24+
from .qa import run_qa
25+
from .utils import run_validator
26+
from .noanat import copy_default_anat_to_subject, remove_default_anat
27+
except ImportError:
28+
# Fallback for when running as script
29+
try:
30+
from petdeface import PetDeface, deface, cli, main
31+
from mideface import ApplyMideface, Mideface
32+
from pet import WeightedAverage
33+
from qa import run_qa
34+
from utils import run_validator
35+
from noanat import copy_default_anat_to_subject, remove_default_anat
36+
except ImportError:
37+
# Last resort - import from current directory
38+
import os
39+
40+
sys.path.insert(0, os.path.dirname(__file__))
41+
from petdeface import PetDeface, deface, cli, main
42+
from mideface import ApplyMideface, Mideface
43+
from pet import WeightedAverage
44+
from qa import run_qa
45+
from utils import run_validator
46+
from noanat import copy_default_anat_to_subject, remove_default_anat
47+
48+
# Version info
49+
try:
50+
from importlib.metadata import version
51+
52+
__version__ = version("petdeface")
53+
except ImportError:
54+
__version__ = "unknown"
55+
56+
__bids_version__ = "1.8.0"
57+
58+
# Main exports
59+
__all__ = [
60+
"PetDeface",
61+
"deface",
62+
"cli",
63+
"main",
64+
"ApplyMideface",
65+
"Mideface",
66+
"WeightedAverage",
67+
"run_qa",
68+
"run_validator",
69+
"copy_default_anat_to_subject",
70+
"remove_default_anat",
71+
"__version__",
72+
"__bids_version__",
73+
]

0 commit comments

Comments
 (0)