Ejemplo n.º 1
0
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')
Ejemplo n.º 2
0
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')
Ejemplo n.º 3
0
    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',
                                  map_type=imagesets[imgset][2],
                                  target_template_image='GenericMNI')
Ejemplo n.º 4
0
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'
)