Skip to content

Commit f5f8def

Browse files
authored
Pre-cache MNI2009 and MNIInfant templates (#5)
* Add fetch_templates script. * Use the new script. * Remove unused template script. * Don't install TemplateFlow twice.
1 parent 7b466be commit f5f8def

File tree

3 files changed

+109
-58
lines changed

3 files changed

+109
-58
lines changed

Dockerfile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,23 +204,26 @@ ENV \
204204

205205
WORKDIR /root/
206206

207-
# Precaching atlases + AtlasPack
207+
# Precaching templates
208+
COPY scripts/fetch_templates.py fetch_templates.py
209+
RUN ${CONDA_PYTHON} fetch_templates.py && \
210+
rm fetch_templates.py && \
211+
find $HOME/.cache/templateflow -type d -exec chmod go=u {} + && \
212+
find $HOME/.cache/templateflow -type f -exec chmod go=u {} +
213+
214+
# Precaching AtlasPack atlases
208215
RUN mkdir /AtlasPack
209216
COPY --from=atlaspack /AtlasPack/tpl-fsLR_*.dlabel.nii /AtlasPack/
210217
COPY --from=atlaspack /AtlasPack/tpl-MNI152NLin6Asym_*.nii.gz /AtlasPack/
211218
COPY --from=atlaspack /AtlasPack/tpl-MNI152NLin2009cAsym_*.nii.gz /AtlasPack/
212219
COPY --from=atlaspack /AtlasPack/atlas-4S*.tsv /AtlasPack/
213220
COPY --from=atlaspack /AtlasPack/*.json /AtlasPack/
214-
ADD docker/scripts/get_templates.sh get_templates.sh
215-
RUN mkdir $CRN_SHARED_DATA && \
216-
/root/get_templates.sh && \
217-
chmod -R a+rX $CRN_SHARED_DATA
218221

219222
# Make it ok for singularity on CentOS
220223
RUN strip --remove-section=.note.ABI-tag /opt/qt512/lib/libQt5Core.so.5.12.8 \
221224
&& ldconfig
222225

223-
# Download the atlases
226+
# Download the built-in atlases
224227
ENV QSIRECON_ATLAS /atlas/qsirecon_atlases
225228
RUN bash -c \
226229
'mkdir /atlas \

docker/scripts/get_templates.sh

Lines changed: 0 additions & 52 deletions
This file was deleted.

scripts/fetch_templates.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env python
2+
#
3+
# STATEMENT OF CHANGES: This file is derived from sources licensed under the Apache-2.0 terms,
4+
# and uses the following portion of the original code:
5+
# https://github.com/nipreps/fmriprep/blob/fe7c9ff8731635d7f25749f2afd99eb77d26305d/scripts/
6+
# fetch_templates.py#L10-L136
7+
#
8+
# ORIGINAL WORK'S ATTRIBUTION NOTICE:
9+
#
10+
# Copyright The NiPreps Developers <[email protected]>
11+
#
12+
# Licensed under the Apache License, Version 2.0 (the "License");
13+
# you may not use this file except in compliance with the License.
14+
# You may obtain a copy of the License at
15+
#
16+
# http://www.apache.org/licenses/LICENSE-2.0
17+
#
18+
# Unless required by applicable law or agreed to in writing, software
19+
# distributed under the License is distributed on an "AS IS" BASIS,
20+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
# See the License for the specific language governing permissions and
22+
# limitations under the License.
23+
#
24+
# We support and encourage derived works from this project, please read
25+
# about our expectations at
26+
#
27+
# https://www.nipreps.org/community/licensing/
28+
"""
29+
Standalone script to facilitate caching of required TemplateFlow templates.
30+
31+
To download and view how to use this script, run the following commands inside a terminal:
32+
1. wget https://raw.githubusercontent.com/pennlinc/qsirecon_build/main/scripts/fetch_templates.py
33+
2. python fetch_templates.py -h
34+
"""
35+
36+
import argparse
37+
import os
38+
39+
40+
def fetch_MNI2009():
41+
"""
42+
Expected templates:
43+
44+
tpl-MNI152NLin2009cAsym/tpl-MNI152NLin2009cAsym_res-01_T1w.nii.gz
45+
tpl-MNI152NLin2009cAsym/tpl-MNI152NLin2009cAsym_res-02_T1w.nii.gz
46+
tpl-MNI152NLin2009cAsym/tpl-MNI152NLin2009cAsym_res-01_desc-brain_mask.nii.gz
47+
tpl-MNI152NLin2009cAsym/tpl-MNI152NLin2009cAsym_res-02_desc-brain_mask.nii.gz
48+
tpl-MNI152NLin2009cAsym/tpl-MNI152NLin2009cAsym_res-01_desc-carpet_dseg.nii.gz
49+
tpl-MNI152NLin2009cAsym/tpl-MNI152NLin2009cAsym_res-01_label-brain_probseg.nii.gz
50+
"""
51+
template = "MNI152NLin2009cAsym"
52+
53+
tf.get(template, resolution=(1, 2), desc=None, suffix="T1w")
54+
tf.get(template, resolution=(1, 2), desc="brain", suffix="mask")
55+
tf.get(template, resolution=1, atlas=None, desc="carpet", suffix="dseg")
56+
tf.get(template, resolution=1, label="brain", suffix="probseg")
57+
58+
59+
def fetch_MNIInfant(cohort=2):
60+
"""
61+
Expected templates:
62+
63+
tpl-MNIInfant/cohort-2/tpl-MNIInfant_cohort-2_res-1_T1w.nii.gz
64+
tpl-MNIInfant/cohort-2/tpl-MNIInfant_cohort-2_res-1_T2w.nii.gz
65+
tpl-MNIInfant/cohort-2/tpl-MNIInfant_cohort-2_res-1_desc-brain_mask.nii.gz
66+
tpl-MNIInfant/cohort-2/tpl-MNIInfant_cohort-2_res-2_T1w.nii.gz
67+
tpl-MNIInfant/cohort-2/tpl-MNIInfant_cohort-2_res-2_T1w.nii.gz
68+
tpl-MNIInfant/cohort-2/tpl-MNIInfant_cohort-2_res-2_desc-brain_mask.nii.gz
69+
"""
70+
template = "MNIInfant"
71+
72+
tf.get(template, cohort=cohort, suffix="T1w")
73+
tf.get(template, cohort=cohort, suffix="T2w")
74+
tf.get(template, cohort=cohort, desc="brain", suffix="mask")
75+
76+
77+
def fetch_all():
78+
fetch_MNI2009()
79+
fetch_MNIInfant()
80+
81+
82+
if __name__ == "__main__":
83+
parser = argparse.ArgumentParser(
84+
description="Helper script for pre-caching required templates to run QSIRecon",
85+
)
86+
parser.add_argument(
87+
"--tf-dir",
88+
type=os.path.abspath,
89+
help="Directory to save templates in. If not provided, templates will be saved to"
90+
" `${HOME}/.cache/templateflow`.",
91+
)
92+
opts = parser.parse_args()
93+
94+
# set envvar (if necessary) prior to templateflow import
95+
if opts.tf_dir is not None:
96+
os.environ["TEMPLATEFLOW_HOME"] = opts.tf_dir
97+
98+
import templateflow.api as tf
99+
100+
fetch_all()

0 commit comments

Comments
 (0)