|
15 | 15 | class Run(Command): |
16 | 16 | ''' Command for running neuroscout workflows. ''' |
17 | 17 |
|
18 | | - def run(self): |
| 18 | + def run(self, upload_only=False): |
19 | 19 | # Download bundle and install dataset if necessary |
20 | 20 | 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 | + |
23 | 23 | out_dir = Path(self.options.pop('<outdir>')) / install.bundle_id |
24 | | - smoothing = self.options.pop('--smoothing') |
25 | 24 | 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 | | - |
44 | 25 | neurovault = self.options.pop('--neurovault', 'group') |
45 | 26 | nv_force = self.options.pop('--force-neurovault', False) |
46 | 27 | if neurovault not in ['disable', 'group', 'all']: |
47 | 28 | raise ValueError("Invalid neurovault option.") |
48 | 29 |
|
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}') |
89 | 63 | 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) |
0 commit comments