Skip to content

Commit 3900c42

Browse files
committed
Update utils.py
1 parent a1e6fa4 commit 3900c42

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/fmripost_template/utils/utils.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,13 @@ def get_transforms(source, target, local_transforms=None):
155155
ValueError
156156
If no chain of transforms can link the source and target spaces.
157157
"""
158+
from pathlib import Path
159+
158160
import templateflow.api as tflow
159161
from bids.layout import Entity, parse_file_entities
160162

163+
from fmripost_template.utils.utils import find_shortest_path, templateflow_get
164+
161165
query = [
162166
Entity('template', 'tpl-([a-zA-Z0-9+]+)'),
163167
Entity('from', 'from-([a-zA-Z0-9+]+)'),
@@ -207,6 +211,40 @@ def get_transforms(source, target, local_transforms=None):
207211

208212
for selected_transform in selected_transforms:
209213
if 'templateflow' in selected_transform:
210-
tflow.get(selected_transform)
214+
templateflow_get(Path(selected_transform))
211215

212216
return selected_transforms, selected_inversions
217+
218+
219+
def templateflow_get(filepath):
220+
"""Get a file from templateflow.
221+
222+
Derived from https://github.com/templateflow/python-client/
223+
blob/9be9c3162a54e6c3dff3c544204ff4eb119df1c8/templateflow/api.py#L162-L172
224+
225+
Parameters
226+
----------
227+
filepath : pathlib.Path
228+
The path to the file to get.
229+
230+
Raises
231+
------
232+
FileNotFoundError
233+
If the file is not found in DataLad or S3.
234+
"""
235+
import templateflow.api as tflow
236+
237+
# Try DataLad first
238+
dl_missing = not filepath.isfile()
239+
if dl_missing:
240+
tflow._datalad_get(filepath)
241+
dl_missing.remove(filepath)
242+
243+
# Fall-back to S3 if some files are still missing
244+
s3_missing = filepath.is_file() and filepath.stat().st_size == 0
245+
if s3_missing:
246+
tflow._s3_get(filepath)
247+
248+
not_fetched = not filepath.is_file() or filepath.stat().st_size == 0
249+
if not_fetched:
250+
raise FileNotFoundError(f'Failed to fetch {filepath}')

0 commit comments

Comments
 (0)