Skip to content

Commit 8877e79

Browse files
authored
Merge pull request #117 from neuroscout/no-task
Look for all stat maps when uploading
2 parents 5ff59a4 + 2c2c6bc commit 8877e79

File tree

3 files changed

+43
-34
lines changed

3 files changed

+43
-34
lines changed

neuroscout_cli/commands/install.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from neuroscout_cli.commands.base import Command
2-
from neuroscout_cli import __version__ as VERSION
3-
from datalad.api import install, get, unlock
4-
from pathlib import Path
5-
from shutil import copy
61
import json
72
import tarfile
83
import logging
4+
import sys
5+
6+
from pathlib import Path
7+
from shutil import copy
8+
from packaging import version
9+
from neuroscout_cli.commands.base import Command
10+
from neuroscout_cli import __version__ as VERSION
11+
from datalad.api import install, get, unlock
912
from bids.utils import convert_JSON
1013
from bids import BIDSLayout
11-
from packaging import version
12-
import sys
1314

1415

1516
class Install(Command):
@@ -20,17 +21,22 @@ class Install(Command):
2021
def __init__(self, options, *args, **kwargs):
2122
super().__init__(options, *args, **kwargs)
2223
self.install_dir = Path(self.options.pop('--install-dir'))
24+
self.resources = None
25+
self.dataset_dir = None
26+
self.bundle_dir = None
27+
self.preproc_dir = None
2328

2429
def download_bundle(self):
30+
""" Download analysis bundle and setup preproc dir """
2531
# Download bundle
2632
if not self.bundle_cache.exists():
2733
logging.info("Downloading bundle...")
2834
self.api.analyses.get_bundle(self.bundle_id, self.bundle_cache)
2935

3036
# Un-tarzip, and read in JSON files
31-
with tarfile.open(self.bundle_cache) as tf:
37+
with tarfile.open(self.bundle_cache) as tF:
3238
self.resources = json.loads(
33-
tf.extractfile('resources.json').read().decode("utf-8"))
39+
tF.extractfile('resources.json').read().decode("utf-8"))
3440

3541
self.dataset_dir = self.install_dir / \
3642
self.resources['dataset_name']
@@ -40,33 +46,36 @@ def download_bundle(self):
4046
# Extract to bundle_dir
4147
if not self.bundle_dir.exists():
4248
self.bundle_dir.mkdir(parents=True, exist_ok=True)
43-
tf.extractall(self.bundle_dir)
49+
tF.extractall(self.bundle_dir)
4450
logging.info(
45-
"Bundle installed at {}".format(
46-
self.bundle_dir.absolute()))
51+
"Bundle installed at %s", self.bundle_dir.absolute()
52+
)
53+
54+
# Set up preproc dir w/ DataLad (but don't download)
55+
self.preproc_dir = Path(self.dataset_dir) / 'derivatives'
56+
57+
if not self.preproc_dir.exists():
58+
# Use datalad to install the preproc dataset
59+
install(source=self.resources['preproc_address'],
60+
path=str(self.preproc_dir))
61+
62+
for option in ['preproc', 'fmriprep']:
63+
if (self.preproc_dir / option).exists():
64+
self.preproc_dir = self.preproc_dir / option
65+
break
4766

4867
# Check version
4968
self._check_version()
5069

5170
return self.bundle_dir.absolute()
5271

5372
def download_data(self):
73+
""" Use DataLad to download necessary data to disk """
5474
bundle_dir = self.download_bundle()
55-
self.preproc_dir = Path(self.dataset_dir) / 'derivatives'
5675
with (bundle_dir / 'model.json').open() as f:
5776
model = convert_JSON(json.load(f))
5877

5978
try:
60-
if not self.preproc_dir.exists():
61-
# Use datalad to install the preproc dataset
62-
install(source=self.resources['preproc_address'],
63-
path=str(self.preproc_dir))
64-
65-
for option in ['preproc', 'fmriprep']:
66-
if (self.preproc_dir / option).exists():
67-
self.preproc_dir = self.preproc_dir / option
68-
break
69-
7079
# Get all JSON files
7180
jsons = list(self.preproc_dir.rglob('*.json'))
7281
if jsons:
@@ -87,12 +96,12 @@ def download_data(self):
8796
if self.options.pop('--unlock', False):
8897
unlock(paths)
8998

90-
except Exception as e:
91-
if hasattr(e, 'failed'):
92-
message = e.failed[0]['message']
99+
except Exception as exp:
100+
if hasattr(exp, 'failed'):
101+
message = exp.failed[0]['message']
93102
raise ValueError("Datalad failed. Reason: {}".format(message))
94103
else:
95-
raise(e)
104+
raise exp
96105

97106
# Copy meta-data to root of dataset_dir
98107
copy(list(self.bundle_dir.glob('task-*json'))[0], self.preproc_dir)
@@ -108,7 +117,7 @@ def _check_version(self):
108117
"\n"
109118
"-----------------------------------------------------------\n"
110119
"Insufficient version of neurosout-cli! \n"
111-
f"This bundle requires v{str(req)} or greater, and you current have v{VERSION} \n"
120+
f"This bundle requires v{str(req)}+, and you have v{VERSION}\n"
112121
"Please upgrade neuroscout-cli by running: \n"
113122
"'docker pull neuroscout/neuroscout-cli' to continue. \n"
114123
"-----------------------------------------------------------\n"

neuroscout_cli/commands/run.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import logging
2+
import re
3+
import json
4+
from pathlib import Path
15
from neuroscout_cli.commands.base import Command
26
from neuroscout_cli import __version__ as VERSION
37
from neuroscout_cli.commands.install import Install
48
from fitlins.cli.run import run_fitlins
59
from bids.layout import BIDSLayout
6-
from pathlib import Path
7-
import logging
8-
import re
9-
import json
1010

1111
# Options not to be passed onto fitlins
1212
INVALID = ['--unlock', '--version', '--help', '--install-dir',
@@ -105,7 +105,7 @@ def run(self, upload_only=False):
105105
if ses_dirs: # If session, look for stat files in session fld
106106
images = images / ses_dirs[0]
107107

108-
group = [str(i) for i in images.glob('task*statmap.nii.gz')
108+
group = [str(i) for i in images.glob('*statmap.nii.gz')
109109
if re.match(
110110
'.*stat-[t|F|variance|effect]+.*', i.name)]
111111

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
docopt==0.6.2
22
datalad==0.10.2
33
tqdm
4-
pyns==0.4.4
4+
pyns==0.4.5

0 commit comments

Comments
 (0)