def xnat_pull(projectid, subjectid, experimentid, storagepath): """ Pull the complete set of files from a XNAT project, subject and experiment id """ absstoragepath = op.abspath(storagepath) # we define the unique experimental id based on the user input # see push for more comments # subj_id = '%s_%s' % (projectid, subjectid) # exp_id = '%s_%s' % (subj_id, experimentid) subj_id = '%s' % subjectid exp_id = '%s_%s' % (projectid, experimentid) experiment_uri = '/projects/%s/subjects/%s/experiments/%s' % ( projectid, subj_id, exp_id) metacml_uri = '%s/resources/meta/files/meta.cml' % experiment_uri # download meta.cml metacmlpath = op.join(absstoragepath, 'meta.cml') meta_uri = '%s/resources/meta/files/meta.cml' % experiment_uri xnat_interface.select(meta_uri).get(metacmlpath) # parse meta.cml f = open(metacmlpath, 'rb') remote_connectome = cf.parseString(f.read()) f.close() if DEBUG_msg: print "Remote connectome", remote_connectome.to_xml() # loop over objects and download them to pyxnat cache / or path for ele in remote_connectome.get_all(): if DEBUG_msg: print("Downloading connectome object") ele.print_summary() cobj_uri = '%s/assessors/%s/out/resources/data/files/%s' % ( experiment_uri, '%s_%s' % (exp_id, ele.__class__.__name__), quote_for_xnat(ele.name) + ele.get_file_ending()) # download file # does file folder exist? eleobjfolderfname = op.join(absstoragepath, ele.get_unique_relpath()) if not op.exists(op.split(eleobjfolderfname)[0]): os.makedirs(op.split(eleobjfolderfname)[0]) xnat_interface.select(cobj_uri).get(eleobjfolderfname) # update current connectome container print("=============") print("You can load the pulled connectome file with:") print("import cfflib as cf; mycon = cf.load('%s')" % op.join(absstoragepath, 'meta.cml')) print("=============") return True
def xnat_pull( projectid, subjectid, experimentid, storagepath): """ Pull the complete set of files from a XNAT project, subject and experiment id """ absstoragepath = op.abspath(storagepath) # we define the unique experimental id based on the user input # see push for more comments # subj_id = '%s_%s' % (projectid, subjectid) # exp_id = '%s_%s' % (subj_id, experimentid) subj_id = '%s' % subjectid exp_id = '%s_%s' % (projectid, experimentid) experiment_uri = '/projects/%s/subjects/%s/experiments/%s' % (projectid, subj_id, exp_id) metacml_uri = '%s/resources/meta/files/meta.cml' % experiment_uri # download meta.cml metacmlpath = op.join(absstoragepath, 'meta.cml') meta_uri = '%s/resources/meta/files/meta.cml' % experiment_uri xnat_interface.select(meta_uri).get(metacmlpath) # parse meta.cml f = open(metacmlpath, 'rb') remote_connectome = cf.parseString(f.read()) f.close() if DEBUG_msg: print "Remote connectome", remote_connectome.to_xml() # loop over objects and download them to pyxnat cache / or path for ele in remote_connectome.get_all(): if DEBUG_msg: print("Downloading connectome object") ele.print_summary() cobj_uri = '%s/assessors/%s/out/resources/data/files/%s' % ( experiment_uri, '%s_%s' % (exp_id, ele.__class__.__name__), quote_for_xnat(ele.name) + ele.get_file_ending() ) # download file # does file folder exist? eleobjfolderfname = op.join(absstoragepath, ele.get_unique_relpath()) if not op.exists(op.split(eleobjfolderfname)[0]): os.makedirs( op.split(eleobjfolderfname)[0] ) xnat_interface.select(cobj_uri).get(eleobjfolderfname) # update current connectome container print("=============") print("You can load the pulled connectome file with:") print("import cfflib as cf; mycon = cf.load('%s')" % op.join(absstoragepath, 'meta.cml')) print("=============") return True
def _load_from_meta_cml(filename): """ Load connectome file from a meta.cml file. """ with open(filename, 'r') as metacml: metastr = metacml.read() connectome = cf.parseString(metastr) # update references connectome._update_parent_reference() connectome.iszip = False connectome.fname = op.abspath(filename) # check if names are unique! connectome.check_names_unique() return connectome
def _load_from_cff(filename, *args, **kwargs): """ Load connectome file given filename Returns ------- connectome : ``Connectome`` Connectome Instance """ # XXX: take care to have allowZip64 = True (but not supported by unix zip/unzip, same for ubuntu?) ? _zipfile = ZipFile(filename, 'a', ZIP_DEFLATED) try: metadata_string = _zipfile.read('meta.cml') except: # XXX: what is the correct exception for read error? raise RuntimeError('Can not extract meta.cml from connectome file.') # create connectome instance connectome = cf.parseString(metadata_string) # update references connectome._update_parent_reference() # add additional attributes connectome.src = filename # it is from the zip file connectome.iszip = True # if it is a zip file, we can assume that the src paths are given relatively connectome._zipfile = _zipfile # check if all referenced container elements exist in the archive connectome.check_file_in_cff() # check if names are unique! connectome.check_names_unique() return connectome
def xnat_push(connectome_obj, projectid, subjectid, experimentid, overwrite=False): """ Push all the connectome objects to the remote XNAT server. Parameters ---------- connectome_obj : connectome object The connectome object you want to push projectid : string The id of the project, has to be unique across an XNAT server subjectid : string The id of the subject experimentid : string The id of the experiment overwrite : boolean Overwrite remote version of the connectome object with the connectome object contained in the local connectome container """ def _push_metacml(experiment_uri): _, fname = tempfile.mkstemp() f = open(fname, 'wb') f.write(connectome_obj.to_xml()) f.close() # finally update remote meta.cml meta_uri = '%s/resources/meta/files/meta.cml' % experiment_uri xnat_interface.select(meta_uri).insert(fname, experiments = 'xnat:imageSessionData', \ use_label=True) if xnat_interface is None: raise Exception( 'You need to setup the XNAT connection first with set_xnat_connection' ) # we define the unique experimental id based on the user input # user do not expect this composed identifiers, so we directly use # the given parameters to construct the path # originally, we thought that this is required because there have to be # unique subject identifiers across the whole XNAT instance # we seems not to be required anymore # subj_id = '%s_%s' % (projectid, subjectid) # exp_id = '%s_%s' % (subj_id, experimentid) subj_id = '%s' % subjectid exp_id = '%s_%s' % (projectid, experimentid) experiment_uri = '/projects/%s/subjects/%s/experiments/%s' % ( projectid, subj_id, exp_id) metacml_uri = '%s/resources/meta/files/meta.cml' % experiment_uri # does the experiment exists if xnat_interface.select(metacml_uri).exists(): # it exists # compare it to the local object remote_metacml = open(xnat_interface.select(metacml_uri).get(), 'rb') remote_connectome = cf.parseString(remote_metacml.read()) # loop over local connectome objects and check if the exists remotely all_local_cobj = connectome_obj.get_all() # connectome objects we need to add to the remote metacml push_objects = [] for ele in all_local_cobj: if DEBUG_msg: print "Working on element %s" % ele.name if (ele in remote_connectome.get_all() and overwrite) or \ not ele in remote_connectome.get_all(): if DEBUG_msg: print "We push element %s" % ele.name print "Element in remote? " + str( ele in remote_connectome.get_all()) # push connectome object to remote cobj_uri = '%s/assessors/%s/out/resources/data/files/%s' % ( experiment_uri, '%s_%s' % (exp_id, ele.__class__.__name__), quote_for_xnat(ele.name) + ele.get_file_ending()) if DEBUG_msg: print "uri", cobj_uri # insert data file to xnat xnat_interface.select(cobj_uri).insert(ele.get_abs_path(), experiments = 'xnat:imageSessionData', \ assessors = 'xnat:imageAssessorData', use_label=True) # add element for updating metacml later on the remote push_objects.append(ele) else: # we do not push if DEBUG_msg: print "We do nothing with element %s (already on remote and no overwrite)" % ele.name # synchronize meta_cml # we need to retrieve the remote connectome objects and add it to the # local if they no not yet exists for el in remote_connectome.get_all(): if not el in connectome_obj.get_all(): connectome_obj.add_connectome_object(el) #for el in push_objects: # add all push_objects to remote meta_cml # remote_connectome.add_connectome_object(el) # update cmetadata (overwriting remote with local) remote_connectome.connectome_meta = connectome_obj.connectome_meta _push_metacml(experiment_uri) if DEBUG_msg: print "Current local connectome container", connectome_obj.to_xml() print "Current remote connectome container", remote_connectome.to_xml( ) print "Current push objects", push_objects else: # create meta.cml # loop over local connectome objects and check if the exists remotely all_local_cobj = connectome_obj.get_all() for ele in all_local_cobj: print "We push element %s" % ele.name # push connectome object to remote cobj_uri = '%s/assessors/%s/out/resources/data/files/%s' % ( experiment_uri, '%s_%s' % (exp_id, ele.__class__.__name__), quote_for_xnat(ele.name)) # insert data file to xnat xnat_interface.select(cobj_uri).insert(ele.get_abs_path(), experiments = 'xnat:imageSessionData', \ assessors = 'xnat:imageAssessorData', use_label=True) # push the current connectome object to remote _push_metacml(experiment_uri)
def xnat_push(connectome_obj, projectid, subjectid, experimentid, overwrite = False): """ Push all the connectome objects to the remote XNAT server. Parameters ---------- connectome_obj : connectome object The connectome object you want to push projectid : string The id of the project, has to be unique across an XNAT server subjectid : string The id of the subject experimentid : string The id of the experiment overwrite : boolean Overwrite remote version of the connectome object with the connectome object contained in the local connectome container """ def _push_metacml(experiment_uri): _, fname = tempfile.mkstemp() f=open(fname, 'wb') f.write(connectome_obj.to_xml()) f.close() # finally update remote meta.cml meta_uri = '%s/resources/meta/files/meta.cml' % experiment_uri xnat_interface.select(meta_uri).insert(fname, experiments = 'xnat:imageSessionData', \ use_label=True) if xnat_interface is None: raise Exception('You need to setup the XNAT connection first with set_xnat_connection') # we define the unique experimental id based on the user input # user do not expect this composed identifiers, so we directly use # the given parameters to construct the path # originally, we thought that this is required because there have to be # unique subject identifiers across the whole XNAT instance # we seems not to be required anymore # subj_id = '%s_%s' % (projectid, subjectid) # exp_id = '%s_%s' % (subj_id, experimentid) subj_id = '%s' % subjectid exp_id = '%s_%s' % (projectid, experimentid) experiment_uri = '/projects/%s/subjects/%s/experiments/%s' % (projectid, subj_id, exp_id) metacml_uri = '%s/resources/meta/files/meta.cml' % experiment_uri # does the experiment exists if xnat_interface.select(metacml_uri).exists(): # it exists # compare it to the local object remote_metacml = open(xnat_interface.select(metacml_uri).get(), 'rb') remote_connectome = cf.parseString(remote_metacml.read()) # loop over local connectome objects and check if the exists remotely all_local_cobj = connectome_obj.get_all() # connectome objects we need to add to the remote metacml push_objects = [] for ele in all_local_cobj: if DEBUG_msg: print "Working on element %s" % ele.name if (ele in remote_connectome.get_all() and overwrite) or \ not ele in remote_connectome.get_all(): if DEBUG_msg: print "We push element %s" % ele.name print "Element in remote? " + str(ele in remote_connectome.get_all()) # push connectome object to remote cobj_uri = '%s/assessors/%s/out/resources/data/files/%s' % ( experiment_uri, '%s_%s' % (exp_id, ele.__class__.__name__), quote_for_xnat(ele.name) + ele.get_file_ending() ) if DEBUG_msg: print "uri", cobj_uri # insert data file to xnat xnat_interface.select(cobj_uri).insert(ele.get_abs_path(), experiments = 'xnat:imageSessionData', \ assessors = 'xnat:imageAssessorData', use_label=True) # add element for updating metacml later on the remote push_objects.append(ele) else: # we do not push if DEBUG_msg: print "We do nothing with element %s (already on remote and no overwrite)" % ele.name # synchronize meta_cml # we need to retrieve the remote connectome objects and add it to the # local if they no not yet exists for el in remote_connectome.get_all(): if not el in connectome_obj.get_all(): connectome_obj.add_connectome_object(el) #for el in push_objects: # add all push_objects to remote meta_cml # remote_connectome.add_connectome_object(el) # update cmetadata (overwriting remote with local) remote_connectome.connectome_meta = connectome_obj.connectome_meta _push_metacml(experiment_uri) if DEBUG_msg: print "Current local connectome container", connectome_obj.to_xml() print "Current remote connectome container", remote_connectome.to_xml() print "Current push objects", push_objects else: # create meta.cml # loop over local connectome objects and check if the exists remotely all_local_cobj = connectome_obj.get_all() for ele in all_local_cobj: print "We push element %s" % ele.name # push connectome object to remote cobj_uri = '%s/assessors/%s/out/resources/data/files/%s' % ( experiment_uri, '%s_%s' % (exp_id, ele.__class__.__name__), quote_for_xnat(ele.name) ) # insert data file to xnat xnat_interface.select(cobj_uri).insert(ele.get_abs_path(), experiments = 'xnat:imageSessionData', \ assessors = 'xnat:imageAssessorData', use_label=True) # push the current connectome object to remote _push_metacml(experiment_uri)