Skip to content

Commit 929d143

Browse files
authored
Merge pull request #106 from neuroscout/upload_only
Upload to Neurovault only command
2 parents a187346 + b63381f commit 929d143

File tree

5 files changed

+105
-87
lines changed

5 files changed

+105
-87
lines changed

neuroscout_cli/cli.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Usage:
55
neuroscout run [-dfuv -i <dir> -s <k> -w <dir> -c <n> -n <nv>] <outdir> <bundle_id>...
66
neuroscout install [-ui <dir>] <bundle_id>...
7+
neuroscout upload [-f -n <nv>] <outdir> <bundle_id>...
78
neuroscout ls <bundle_id>
89
neuroscout -h | --help
910
neuroscout --version
@@ -25,6 +26,7 @@
2526
Commands:
2627
run Runs analysis.
2728
install Installs a bundle and/or dataset.
29+
upload Upload existing analysis results to Neurovault.
2830
ls Lists the available files in a bundle's dataset.
2931
3032
Examples:
@@ -54,11 +56,11 @@ def main():
5456
if hasattr(ncl, k) and val:
5557
k = k[0].upper() + k[1:]
5658
command = getattr(ncl, k)
57-
if k in ['Run', 'Install']:
59+
if k in ['Run', 'Install', 'Upload']:
5860
bundles = args.pop('<bundle_id>')
5961
# Loop over bundles
6062
for bundle in bundles:
61-
logging.info("Running analysis : {}".format(bundle))
63+
logging.info("Analysis ID : {}".format(bundle))
6264
args['<bundle_id>'] = bundle
6365
command(deepcopy(args)).run()
6466
sys.exit(0)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from .install import Install
22
from .ls import Ls
33
from .run import Run
4+
from .upload import Upload
45

56
__all__ = [
67
'Install',
78
'Ls',
8-
'Run'
9+
'Run',
10+
'Upload'
911
]

neuroscout_cli/commands/install.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,8 @@ def _check_version(self):
109109
)
110110
sys.exit(1)
111111

112-
def run(self):
113-
return self.download_data()
112+
def run(self, download_data=True):
113+
if download_data:
114+
return self.download_data()
115+
else:
116+
return self.download_bundle()

neuroscout_cli/commands/run.py

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -15,95 +15,98 @@
1515
class Run(Command):
1616
''' Command for running neuroscout workflows. '''
1717

18-
def run(self):
18+
def run(self, upload_only=False):
1919
# Download bundle and install dataset if necessary
2020
install = Install(self.options.copy())
21-
bundle_path = install.run()
22-
preproc_path = str(install.preproc_dir.absolute())
21+
bundle_path = install.run(download_data=(not upload_only))
22+
2323
out_dir = Path(self.options.pop('<outdir>')) / install.bundle_id
24-
smoothing = self.options.pop('--smoothing')
2524
model_path = (bundle_path / 'model.json').absolute()
26-
fitlins_args = [
27-
preproc_path,
28-
str(out_dir),
29-
'dataset',
30-
f'--model={model_path}',
31-
'--ignore=/(.*desc-confounds_regressors.tsv)/',
32-
f'--derivatives={bundle_path} {preproc_path}',
33-
f'--smoothing={smoothing}:Dataset'
34-
]
35-
36-
verbose = self.options.pop('--verbose')
37-
if verbose:
38-
fitlins_args.append('-vvv')
39-
work_dir = self.options.pop('--work-dir', None)
40-
if work_dir:
41-
work_dir = str(Path(work_dir).absolute() / self.bundle_id)
42-
fitlins_args.append(f"--work-dir={work_dir}")
43-
4425
neurovault = self.options.pop('--neurovault', 'group')
4526
nv_force = self.options.pop('--force-neurovault', False)
4627
if neurovault not in ['disable', 'group', 'all']:
4728
raise ValueError("Invalid neurovault option.")
4829

49-
# Fitlins invalid keys
50-
for k in INVALID:
51-
self.options.pop(k, None)
52-
53-
# Add remaining optional arguments
54-
for name, value in self.options.items():
55-
if name.startswith('--'):
56-
if value is True:
57-
fitlins_args.append(f'{name}')
58-
elif value is not None and value is not False:
59-
fitlins_args.append(f'{name}={value}')
60-
else:
61-
if value is not False and value is not None:
62-
fitlins_args.append(f'{name} {value}')
63-
64-
# Call fitlins as if CLI
65-
retcode = run_fitlins(fitlins_args)
66-
67-
if retcode == 0:
68-
if neurovault != 'disable':
69-
70-
model = json.load(open(model_path, 'r'))
71-
n_subjects = len(model['Input']['Subject'])
72-
73-
logging.info("Uploading results to NeuroVault...")
74-
75-
# Find files
76-
images = out_dir / 'fitlins'
77-
78-
ses_dirs = [a for a in images.glob('ses*') if a.is_dir()]
79-
if ses_dirs: # If session, look for stat files in session fld
80-
images = images / ses_dirs[0]
81-
82-
group = [i for i in images.glob('task*statmap.nii.gz')
83-
if re.match(
84-
'.*stat-[t|F|variance|effect]+.*', i.name)]
85-
86-
if neurovault == 'all':
87-
sub = [i for i in images.glob('sub*/*statmap.nii.gz')
88-
if re.match('.*stat-[variance|effect]+.*', i.name)]
30+
if not upload_only:
31+
preproc_path = str(install.preproc_dir.absolute())
32+
smoothing = self.options.pop('--smoothing')
33+
34+
fitlins_args = [
35+
preproc_path,
36+
str(out_dir),
37+
'dataset',
38+
f'--model={model_path}',
39+
'--ignore=/(.*desc-confounds_regressors.tsv)/',
40+
f'--derivatives={bundle_path} {preproc_path}',
41+
f'--smoothing={smoothing}:Dataset'
42+
]
43+
44+
verbose = self.options.pop('--verbose')
45+
if verbose:
46+
fitlins_args.append('-vvv')
47+
work_dir = self.options.pop('--work-dir', None)
48+
if work_dir:
49+
work_dir = str(Path(work_dir).absolute() / self.bundle_id)
50+
fitlins_args.append(f"--work-dir={work_dir}")
51+
52+
# Fitlins invalid keys
53+
for k in INVALID:
54+
self.options.pop(k, None)
55+
56+
# Add remaining optional arguments
57+
for name, value in self.options.items():
58+
if name.startswith('--'):
59+
if value is True:
60+
fitlins_args.append(f'{name}')
61+
elif value is not None and value is not False:
62+
fitlins_args.append(f'{name}={value}')
8963
else:
90-
sub = None
91-
92-
# Upload results NeuroVault
93-
self.api.analyses.upload_neurovault(
94-
id=self.bundle_id,
95-
validation_hash=install.resources['validation_hash'],
96-
group_paths=group, subject_paths=sub,
97-
force=nv_force,
98-
n_subjects=n_subjects)
99-
else:
100-
logging.error(
101-
"\n"
102-
"-----------------------------------------------------------\n"
103-
"Model execution failed! \n"
104-
f"neuroscout-cli version: {VERSION}\n"
105-
"Update this program or revise your model, and try again.\n"
106-
"If you believe there is a bug, please report it:\n"
107-
"https://github.com/neuroscout/neuroscout-cli/issues\n"
108-
"-----------------------------------------------------------\n"
109-
)
64+
if value is not False and value is not None:
65+
fitlins_args.append(f'{name} {value}')
66+
67+
# Call fitlins as if CLI
68+
retcode = run_fitlins(fitlins_args)
69+
70+
if retcode != 0:
71+
logging.error(
72+
"\n"
73+
"-------------------------------------------------------\n"
74+
"Model execution failed! \n"
75+
f"neuroscout-cli version: {VERSION}\n"
76+
"Update neuroscout-cli or revise your model, "
77+
"and try again \n"
78+
"If you believe there is a bug, please report it:\n"
79+
"https://github.com/neuroscout/neuroscout-cli/issues\n"
80+
"-------------------------------------------------------\n"
81+
)
82+
83+
if neurovault != 'disable':
84+
model = json.load(open(model_path, 'r'))
85+
n_subjects = len(model['Input']['Subject'])
86+
87+
logging.info("Uploading results to NeuroVault...")
88+
89+
# Find files
90+
images = out_dir / 'fitlins'
91+
92+
ses_dirs = [a for a in images.glob('ses*') if a.is_dir()]
93+
if ses_dirs: # If session, look for stat files in session fld
94+
images = images / ses_dirs[0]
95+
96+
group = [i for i in images.glob('task*statmap.nii.gz')
97+
if re.match(
98+
'.*stat-[t|F|variance|effect]+.*', i.name)]
99+
100+
if neurovault == 'all':
101+
sub = [i for i in images.glob('sub*/*statmap.nii.gz')
102+
if re.match('.*stat-[variance|effect]+.*', i.name)]
103+
else:
104+
sub = None
105+
106+
# Upload results NeuroVault
107+
self.api.analyses.upload_neurovault(
108+
id=self.bundle_id,
109+
validation_hash=install.resources['validation_hash'],
110+
group_paths=group, subject_paths=sub,
111+
force=nv_force,
112+
n_subjects=n_subjects)

neuroscout_cli/commands/upload.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from neuroscout_cli.commands.run import Run
2+
3+
4+
class Upload(Run):
5+
''' Command for running neuroscout workflows. '''
6+
7+
def run(self):
8+
return super().run(upload_only=True)

0 commit comments

Comments
 (0)