|
1 | 1 | from neuroscout_cli.commands.base import Command |
2 | 2 | from neuroscout_cli import API_URL |
3 | | -from datalad.api import install |
| 3 | +from datalad.api import install, get |
4 | 4 | from pathlib import Path |
5 | 5 | import requests |
6 | 6 | import json |
@@ -59,31 +59,35 @@ def download_bundle(self): |
59 | 59 | def download_data(self): |
60 | 60 | self.download_bundle() |
61 | 61 |
|
62 | | - # logging.info("Installing dataset...") |
63 | | - # Use datalad to install the raw BIDS dataset |
64 | | - # install(source=self.resources['dataset_address'], |
65 | | - # path=(self.dataset_dir).as_posix()).path |
| 62 | + remote_files = self.resources['func_paths'] + self.resources['mask_paths'] |
| 63 | + remote_path = self.resources['preproc_address'] |
66 | 64 |
|
67 | | - # Pre-fetch specific files from the original dataset? |
68 | | - logging.info("Fetching remote resources...") |
| 65 | + deriv_dir = Path(self.dataset_dir) / 'derivatives' |
69 | 66 |
|
70 | | - # Fetch remote preprocessed files |
71 | | - remote_path = self.resources['preproc_address'] |
72 | | - remote_files = self.resources['func_paths'] + self.resources['mask_paths'] |
| 67 | + try: |
| 68 | + if not (deriv_dir / 'fmriprep').exists(): |
| 69 | + # Use datalad to install the raw BIDS dataset |
| 70 | + install(source=remote_path, |
| 71 | + path=(deriv_dir / 'fmriprep').as_posix()) |
| 72 | + |
| 73 | + preproc_dir = deriv_dir / 'fmriprep' / 'fmriprep' |
| 74 | + get([(preproc_dir / f).as_posix() for f in remote_files]) |
| 75 | + except Exception as e: |
| 76 | + message = e.failed[0]['message'] |
| 77 | + if 'Failed to clone data from any candidate source URL' not in message[0]: |
| 78 | + raise ValueError("Datalad failed. Reason: {}".format(message)) |
73 | 79 |
|
74 | | - preproc_dir = Path(self.dataset_dir) / 'derivatives' / 'fmriprep' |
75 | | - preproc_dir.mkdir(exist_ok=True, parents=True) |
| 80 | + logging.info("Attempting HTTP download...") |
| 81 | + preproc_dir = deriv_dir / 'fmriprep' |
| 82 | + for i, resource in enumerate(remote_files): |
| 83 | + filename = preproc_dir / resource |
| 84 | + logging.info("{}/{}: {}".format(i+1, len(remote_files), resource)) |
76 | 85 |
|
77 | | - for i, resource in enumerate(remote_files): |
78 | | - logging.info("{}/{}: {}".format(i+1, len(remote_files), resource)) |
79 | | - filename = preproc_dir / resource |
80 | | - if not filename.exists(): |
81 | | - filename.parents[0].mkdir(exist_ok=True, parents=True) |
82 | | - url = remote_path + '/' + resource |
83 | | - download_file(url, filename) |
| 86 | + if not filename.exists(): |
| 87 | + filename.parents[0].mkdir(exist_ok=True, parents=True) |
| 88 | + download_file(remote_path + '/' + resource, filename) |
84 | 89 |
|
85 | | - desc = {'Name': self.dataset_dir.parts[0], |
86 | | - 'BIDSVersion': '1.0'} |
| 90 | + desc = {'Name': self.dataset_dir.parts[0], 'BIDSVersion': '1.0'} |
87 | 91 |
|
88 | 92 | with (self.dataset_dir / 'dataset_description.json').open('w') as f: |
89 | 93 | json.dump(desc, f) |
|
0 commit comments