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 get_collection_image_metadata(collection=None, data_dir=None, limit=10): ''' Get image metadata associated with collection Args: collection: (int) collection id data_dir: (str) data directory limit: (int) number of images to increment Returns: metadata: (pd.DataFrame) Dataframe with full image metadata from collection ''' if os.path.isfile(os.path.join(data_dir, 'metadata.csv')): dat = pd.read_csv(os.path.join(data_dir, 'metadata.csv')) else: offset = 0 api = Client() i = api.get_collection_images(collection_id=collection, limit=limit, offset=offset) dat = pd.DataFrame(columns=i['results'][0].keys()) while int(offset) < int(i['count']): for x in i['results']: dat = dat.append(x, ignore_index=True) offset = offset + limit i = api.get_collection_images(collection_id=collection, limit=limit, offset=offset) dat.to_csv(os.path.join(data_dir, 'metadata.csv'), index=False) return dat
def get_collection_image_metadata(collection=None, data_dir=None, limit=10): """ Get image metadata associated with collection Args: collection (int, optional): collection id. Defaults to None. data_dir (str, optional): data directory. Defaults to None. limit (int, optional): number of images to increment. Defaults to 10. Returns: pd.DataFrame: Dataframe with full image metadata from collection """ if os.path.isfile(os.path.join(data_dir, "metadata.csv")): dat = pd.read_csv(os.path.join(data_dir, "metadata.csv")) else: offset = 0 api = Client() i = api.get_collection_images(collection_id=collection, limit=limit, offset=offset) dat = pd.DataFrame(columns=i["results"][0].keys()) while int(offset) < int(i["count"]): for x in i["results"]: dat = dat.append(x, ignore_index=True) offset = offset + limit i = api.get_collection_images(collection_id=collection, limit=limit, offset=offset) dat.to_csv(os.path.join(data_dir, "metadata.csv"), index=False) return dat
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')
def upload_neurovault(flask_app, file_id, n_subjects=None): """ Upload image file to NeuroVault Args: file_id (int): NeurovaultFileUpload object id n_subjects (int): Number of subjects in analysis """ api = Client(access_token=flask_app.config['NEUROVAULT_ACCESS_TOKEN']) file_object = NeurovaultFileUpload.query.filter_by(id=file_id).one() basename = Path(file_object.path).parts[-1] contrast_name = re.findall('contrast-(.*)_', str(basename))[0] map_type = re.findall('stat-(.*)_', str(basename))[0] map_type = MAP_TYPE_CHOICES[map_type] if file_object.level == 'GROUP': analysis_level = 'G' else: analysis_level = 'S' n_subjects = None try: api.add_image(file_object.collection.collection_id, file_object.path, name=contrast_name, modality="fMRI-BOLD", map_type=map_type, analysis_level=analysis_level, cognitive_paradigm_cogatlas='None', number_of_subjects=n_subjects, is_valid=True) except Exception as e: update_record(file_object, exception=e, traceback='Error adding image to collection') raise return update_record(file_object, status='OK')
""" create neurovault collection with results """ import os import glob from pynv import Client assert 'NEUROVAULT_API_KEY' in os.environ access_token = os.environ['NEUROVAULT_API_KEY'] assert 'NARPS_BASEDIR' in os.environ basedir = os.environ['NARPS_BASEDIR'] output_dir = os.path.join(basedir, 'output') api = Client(access_token=access_token) # tuple contains directory name and wildcard imagesets = { 'overlap': ('overlap_binarized_thresh', 'hypo*.nii.gz', 'Other', 'NARPS Study: Overlap maps, showing roportion of teams with significant activity at any voxel (Figure 1)' ), # noqa 'clustermaps': ('cluster_maps', 'hyp*_cluster*_mean.nii.gz', 'Other', 'NARPS Study: Cluster maps, showing mean Z statistic across teams for each hypothesis/cluster (Figure 2, Supplementary Figures 2-7)' ), # noqa 'CBMA': ('ALE', 'hypo*_fdr_oneminusp.nii.gz', 'Other', 'NARPS Study: Coordinate-based meta-analysis results (1 - P after FDR correction) (Supplementary Figure 1)' ), # noqa
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' )
def client(): return Client(access_token=ACCESS_TOKEN)
def anonymous_client(): return Client()