|
| 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