Skip to content

Commit 69d0dbc

Browse files
authored
Merge pull request #46 from neuroscout/enh/nv_ss
Upload single subject files
2 parents b3c53d8 + 9d1c7e3 commit 69d0dbc

8 files changed

+69
-184
lines changed

pyns/models/analysis.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44
from functools import partial
55
from .utils import build_model
6-
6+
import tqdm
77

88
class Analysis:
99
""" Analysis object class. Object representing an analysis that can be
@@ -240,20 +240,42 @@ def get_report(self, id, run_id=None):
240240
"""
241241
return self.get(id=id, sub_route='report', run_id=run_id)
242242

243-
def upload_neurovault(self, id, tarball, validation_hash, force=False,
243+
def upload_neurovault(self, id, validation_hash, subject_paths=None,
244+
group_paths=None, collection_id=None, force=False,
244245
n_subjects=None):
245246
""" Submit analysis for report generation
246247
:param str id: Analysis hash_id.
247-
:param str tarball: Path to tarball.
248248
:param str validation_hash: Validation hash string.
249+
:param list(str) subject_paths: List of image paths.
250+
:param list(str) group_paths: List of image paths.
249251
:param bool force: Force upload with unique timestamped name.
250252
:param int n_subjects: Number of subjects in analysis.
251253
:return: client response object
252254
"""
253-
files = {'tarball': open(tarball, 'rb')}
254-
return self.post(id=id, sub_route='upload', files=files,
255-
validation_hash=validation_hash, force=force,
256-
n_subjects=n_subjects)
255+
# Do group, then subject level
256+
if group_paths is not None:
257+
print("Uploading group images")
258+
for path in tqdm.tqdm(group_paths):
259+
files = {'image_file': open(path, 'rb')}
260+
req = self.post(
261+
id=id, sub_route='upload', files=files, level='GROUP',
262+
validation_hash=validation_hash, force=force,
263+
n_subjects=n_subjects, collection_id=collection_id)
264+
if collection_id is None:
265+
collection_id = req['collection_id']
266+
267+
if subject_paths is not None:
268+
print("Uploading subject images")
269+
for path in tqdm.tqdm(subject_paths):
270+
files = {'image_file': open(path, 'rb')}
271+
req = self.post(
272+
id=id, sub_route='upload', files=files, level='SUBJECT',
273+
validation_hash=validation_hash, force=force,
274+
collection_id=collection_id)
275+
if collection_id is None:
276+
collection_id = req['collection_id']
277+
278+
return req
257279

258280
def get_uploads(self, id):
259281
""" Get NeuroVault uploads associated with this analysis

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pytest
22
requests
33
betamax
44
betamax_serializers
5+
tqdm

tests/cassettes/upload_analysis.json

Lines changed: 29 additions & 173 deletions
Large diffs are not rendered by default.
Binary file not shown.
Binary file not shown.
Binary file not shown.

tests/data/tarball.tar.gz

-934 KB
Binary file not shown.

tests/test_analysis.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,13 @@ def test_upload_analysis(recorder, neuroscout, analysis, get_test_data_path):
216216
analysis_id = 'qxA' # This is an existing analysis on the api
217217

218218
with recorder.use_cassette('upload_analysis'):
219+
group_paths = [str(p) for p in
220+
(get_test_data_path / 'fitlins').glob('task*')]
221+
sub_paths = [str(p) for p in
222+
(get_test_data_path / 'fitlins').glob('sub*')]
219223
resp = neuroscout.analyses.upload_neurovault(
220-
id=analysis_id, tarball=str(get_test_data_path / 'tarball.tar.gz'),
224+
id=analysis_id, group_paths=group_paths,
225+
subject_paths=sub_paths,
221226
validation_hash='8Av1Jbo1aO', force=True,
222227
n_subjects=99)
223228

@@ -231,12 +236,13 @@ def test_upload_analysis(recorder, neuroscout, analysis, get_test_data_path):
231236
newest = [u for u in uploads if u['uploaded_at'] == uploaded_at][0]
232237

233238
# Wait until compiled
234-
while(newest['status'] == 'PENDING'):
235-
sleep(1)
239+
while(
240+
any([True for f in newest['files'] if f['status'] == 'PENDING'])):
241+
sleep(2)
236242
uploads = neuroscout.analyses.get_uploads(id=analysis_id)
237243
newest = [u for u in uploads if u['uploaded_at'] == uploaded_at]
238244
if len(newest) > 0:
239245
newest = newest[0]
240246

241-
assert newest['status'] == 'OK'
247+
assert all([f['status'] == 'OK' for f in newest['files']])
242248
assert newest['collection_id'] is not None

0 commit comments

Comments
 (0)