def _create_collection(analysis, force=False): collection_name = analysis.name if force is True: timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%d_%H:%M') collection_name += f"_{timestamp}" url = f"https://{current_app.config['SERVER_NAME']}"\ f"/builder/{analysis.hash_id}" try: api = Client( access_token=current_app.config['NEUROVAULT_ACCESS_TOKEN']) collection = api.create_collection(collection_name, description=analysis.description, full_dataset_url=url) except Exception: abort( 422, "Error creating collection, " "perhaps one with that name already exists?") # Create new NV collection upload = NeurovaultCollection( analysis_id=analysis.hash_id, collection_id=collection['id'], ) db.session.add(upload) db.session.commit() upload_dir = Path(current_app.config['FILE_DIR']) / 'uploads' / str( collection['id']) upload_dir.mkdir(exist_ok=True) return upload
def upload_neurovault(flask_app, img_tarball, hash_id, upload_id, timestamp=None, n_subjects=None): """ Upload results to NeuroVault Args: img_tarball (str): tarball path containg images hash_id (str): Analysis hash_id upload_id (int): NeurovaultCollection object id timestamp (str): Current server timestamp n_subjects (int): Number of subjects in analysis """ upload_object = NeurovaultCollection.query.filter_by(id=upload_id).one() timestamp = "_" + timestamp if timestamp is not None else '' api = Client(access_token=flask_app.config['NEUROVAULT_ACCESS_TOKEN']) try: # Untar: tmp_dir = Path(mkdtemp()) with tarfile.open(img_tarball, mode="r:gz") as tf: tf.extractall(tmp_dir) except Exception as e: update_record(upload_object, exception=e, traceback='Error decompressing image bundle') raise try: collection = api.create_collection('{}{}'.format(hash_id, timestamp)) for img_path in tmp_dir.glob('*stat-t_statmap.nii.gz'): contrast_name = re.findall('contrast-(.*)_', str(img_path))[0] api.add_image(collection['id'], img_path, name=contrast_name, modality="fMRI-BOLD", map_type='T', analysis_level='G', cognitive_paradigm_cogatlas='None', number_of_subjects=n_subjects, is_valid=True) except Exception as e: update_record(upload_object, exception=e, traceback='Error uploading, perhaps a \ collection with the same name exists?') raise return update_record(upload_object, collection_id=collection['id'], status='OK')
for c in my_collections: if k in c: if c[k] == kwargs[k]: matches.append(c['id']) return (matches) collection = {} for imgset in imagesets: if upload and imgset not in collection: cname = 'NARPS_%s' % imgset c = find_collection(api, name=cname) if len(c) > 0: collection[imgset] = api.get_collection(c[0]) else: collection[imgset] = api.create_collection( cname, description=imagesets[imgset][3]) imgs = glob.glob( os.path.join(output_dir, imagesets[imgset][0], imagesets[imgset][1])) imgs.sort() for img in imgs: # get hypothesis number hypnum = int([ i for i in os.path.basename(img).split('_') if i.find('hyp') > -1 ][0].split('.')[0].replace('hypo', 'hyp').replace('hyp', '')) print(imgset, hypnum) if upload: image = api.add_image(collection[imgset]['id'], img, name='%s_Hyp%d' % (imgset, hypnum), modality='fMRI-BOLD',
from pynv import Client api=Client(access_token='uctb6zhxK6BUY4JgPZZzGZK22tk2zxbX8U5QLNSl') collection=api.create_collection('Gender') image_file_path='D:/FaceData/func_img/wrarun1.nii' image=api.add_image( collection['9850'], image_file_path, name='Gender', modality='fMRI_BOLD', map_type='Other' )