def _run_interface(self, runtime): extracted_networks = [] for i, con in enumerate(self.inputs.in_files): mycon = cf.load(con) nets = mycon.get_connectome_network() for ne in nets: # here, you might want to skip networks with a given # metadata information ne.load() contitle = mycon.get_connectome_meta().get_title() ne.set_name( str(i) + ': ' + contitle + ' - ' + ne.get_name() ) ne.set_src(ne.get_name()) extracted_networks.append(ne) # Add networks to new connectome newcon = cf.connectome(title = 'All CNetworks', connectome_network = extracted_networks) # Setting additional metadata metadata = newcon.get_connectome_meta() metadata.set_creator('My Name') metadata.set_email('My Email') _, name, ext = split_filename(self.inputs.out_file) if not ext == '.cff': ext = '.cff' cf.save_to_cff(newcon, op.abspath(name + ext)) return runtime
def analyze_connectome_lengths(cff_file,track_name, endpointsmm_name, labels_name,make_figures,write_text_files,txt_file_out_dir='N/A', txt_file_prefix='N/A'): """ load connectome file Usage: FLs, EDs, FLsubEDs,fib_labels,c_trk_fibres fib_lengths, euclidean_distances, fib_lengths_minus_EuDs,fiber_labels,fibre_arrays = jg_DWI_util.analyze_connectome_lengths(cff_file,track_name, endpointsmm_name, labels_name,make_figures,write_text_files,txt_file_out_dir=<txt_file_name>, txt_file_prefix=<txt_file_prefix>): """ import cfflib from jg_DWI_util import scatter_and_hist, scatter_simple from nipype.interfaces.cmtk.cmtk import length as fib_length import numpy as np c = cfflib.load(cff_file) # Print summary of connectome file print 'printing cff file summary: ' c.print_summary() # Get fibers as a numpy array c_trk = c.get_by_name(track_name) c_trk.load() c_trk_fibers = c_trk.get_fibers_as_numpy() # Get corresponding fiber length and endpoint_mm data arrays c_endpointsmm = c.get_by_name(endpointsmm_name) c_endpointsmm.load() EPs = c_endpointsmm.data c_labels = c.get_by_name(labels_name) c_labels.load() fib_labels = c_labels.data # Calculate Euclidean distances EDs = [] for e in range(0,len(EPs)): dist = np.sqrt(np.square(EPs[e,0,0]-EPs[e,1,0])+np.square(EPs[e,0,1]-EPs[e,1,1])+np.square(EPs[e,0,2]-EPs[e,1,2])) EDs.append(dist) # Calculate fiber lengths FLs = [] for t in c_trk_fibers: FLs.append(fib_length(t)) # Fiber length minus Euclidean distance: FLsubEDs = np.subtract(FLs,EDs) ## write to text files if write_text_files==1: np.savetxt(os.path.join(txt_file_out_dir, txt_file_prefix+'_fibre_lengths.txt'),FLs) np.savetxt(os.path.join(txt_file_out_dir, txt_file_prefix+'_Euclidean_distances.txt'),EDs) np.savetxt(os.path.join(txt_file_out_dir, txt_file_prefix+'_fibre_labels.txt'),fib_labels) # (write all to a single excel file ? ) if make_figures == 1: # Plot Euclidean distance vs. Track length for all fibers x = FLs#FLs[0:10000] y = EDs #EDs[0:10000] print 'length of x = ' + str(len(x)) print 'length of y = ' + str(len(y)) scatter_and_hist(x,y) scatter_simple(x,y) return FLs, EDs, FLsubEDs,fib_labels,c_trk_fibers
def make_trk_file_for_2_connectome_nodes(cff_file,node_indices,trk_file_orig, trk_file_new, colour_array = None): """ takes a .cff connectome file, a corresponding .trk trackvis file, and a pair of node indices. If there are any fibres connecting those two regions, outputs them in a new trackvis file If a colour array is passed, the fibres coloured (with the scalars field) according to the values in that array """ import cfflib import nibabel as nib c = cfflib.load(cff_file) c_labels = c.get_connectome_data()[3] c_labels.load() #c_fibres = c.get_by_name('Tract file 0').load() #c_fibres_array = c_fibres. fibres_orig, hdr_orig = nib.trackvis.read(trk_file_orig, False) node_indices_reversed = [] node_indices_reversed.append(node_indices[1]) node_indices_reversed.append(node_indices[0]) track_indices = [] for i in range(0, len(c_labels.data)): if c_labels.data[i][0] == node_indices[0]: if c_labels.data[i][1] == node_indices[1]: track_indices.append(i) if c_labels.data[i][0] == node_indices[1]: if c_labels.data[i][1] == node_indices[0]: track_indices.append(i) if not track_indices == []: hdr_new = hdr_orig.copy() outstreams = [] for i in track_indices: if not colour_array == None: # trying properties briefly; scalars doesn't read properly atm # scalar_array = np.ones((len(fibres_orig[i][0]),1),dtype='float32')*colour_array[i] scalar_array = np.ones((len(fibres_orig[i][0]),1),dtype='float')*colour_array[i] property_array = np.array([colour_array[i]], dtype='float32') #new_tuple = tuple([fibres_orig[i][0], scalar_array, None]) #new_tuple=tuple([fibres_orig[i][0], scalar_array,property_array]) new_tuple=tuple([fibres_orig[i][0], scalar_array,property_array]) hdr_new['n_scalars'] = np.array(1, dtype='int16') hdr_new['scalar_name'] = np.array(['JG_COLOURS', '', '', '', '', '', '', '', '', ''],dtype='|S20') hdr_new['n_properties'] = np.array(1, dtype='int16') hdr_new['property_name'] = np.array(['JG_PROPERTY', '', '', '', '', '', '', '', '', ''],dtype='|S20') outstreams.append(new_tuple) else: outstreams.append(fibres_orig[i]) n_fib_out = len(outstreams) hdr_new['n_count'] = n_fib_out nib.trackvis.write(trk_file_new, outstreams, hdr_new) else: print ' no tracks found for ROIs ' + str(node_indices[0]) + ' and ' + str(node_indices[1])
def selectopen_callback(self): fname = tkFileDialog.askopenfilename(filetypes=[('cff files','.cff'),('graphml files','.graphml')]) #browse to file self.cffpath.set( fname ) #set the display self.infile = fname if re.search('\.graphml',fname): self.graph = nx.read_graphml(fname) elif re.search('\.cff',fname): self.cfile = cfflib.load(fname) self.cnet = self.cfile.get_by_name('connectome_freesurferaparc') self.cnet.load() self.graph = self.cnet.data self.addoptions()
def load_cfile(self, filepath, ismetacml = False): """ Load a given cfile as path and initializes the attributes """ if not os.path.isfile(filepath): logger.error('Not existing file: %s' %filepath) return # set the fullpath to internal trait self.fullpathtofile = filepath # set the file name and make it visible in the TraitsUIView self.file_name = os.path.split(filepath)[1] self.obj = cfflib.load(filepath)
def launch(self, cff, should_center=False, **kwargs): """ Process the uploaded CFF and convert read data into our internal DataTypes. :param cff: CFF uploaded file to process. """ if cff is None: raise LaunchException( "Please select CFF file which contains data to import") # !! CFF does logging by the means of `print` statements. We don't want these # logged to terminal as sys.stdout since we no longer have any control over them # so just buffer everything to a StringIO object and log them after operation is done. default_stdout = sys.stdout custom_stdout = cStringIO.StringIO() sys.stdout = custom_stdout try: conn_obj = load(cff) network = conn_obj.get_connectome_network() surfaces = conn_obj.get_connectome_surface() warning_message = "" results = [] if network: partial = self._parse_connectome_network( network, warning_message, **kwargs) results.extend(partial) if surfaces: partial = self._parse_connectome_surfaces( surfaces, warning_message, should_center) results.extend(partial) self._cleanup_after_cfflib(conn_obj) current_op = dao.get_operation_by_id(self.operation_id) current_op.user_group = conn_obj.get_connectome_meta().title if warning_message: current_op.additional_info = warning_message dao.store_entity(current_op) return results finally: # Make sure to set sys.stdout back to it's default value so this won't # have any influence on the rest of TVB. print_output = custom_stdout.getvalue() sys.stdout = default_stdout custom_stdout.close() # Now log everything that cfflib2 outputes with `print` statements using TVB logging self.logger.debug("Output from cfflib library: %s" % print_output)
def store_with_version_1(old_cff_path, temp_folder): """ Given a cff file path load all networks and store intermediate nodes and edges for each of them :param old_cff_path: path to cff file from version 1.1 :param temp_folder: path to folder where intermediate results will be stored :return: store all intermediate nodes and graphs in separate files """ if not os.path.exists(temp_folder): os.makedirs(temp_folder) cff_obj = load(old_cff_path) networks = cff_obj.get_connectome_network() if networks: for net in networks: net.load() _store_intermediate_nodes_and_edges(net.data, temp_folder, net.name)
def create_new_version(old_cff_path, temp_folder, new_cff_path): """ Given a cff file path load the cff object, store the new network data and save to a new cff file :param old_cff_path: path to cff file from version 1.1 :param temp_folder: path to folder where intermediate results are stored :param new_cff_path: path where new cff file is stored """ cff_obj = load(old_cff_path) networks = cff_obj.get_connectome_network() if networks: for net in networks: net.data = _build_and_store_new_graph(temp_folder, name=net.name) tmpdir = tempfile.gettempdir() net.tmpsrc = tmpdir + "\\cff_data" save_to_cff(cff_obj, new_cff_path) shutil.rmtree(temp_folder)
def launch(self, cff, should_center=False, **kwargs): """ Process the uploaded CFF and convert read data into our internal DataTypes. :param cff: CFF uploaded file to process. """ if cff is None: raise LaunchException("Please select CFF file which contains data to import") # !! CFF does logging by the means of `print` statements. We don't want these # logged to terminal as sys.stdout since we no longer have any control over them # so just buffer everything to a StringIO object and log them after operation is done. default_stdout = sys.stdout custom_stdout = cStringIO.StringIO() sys.stdout = custom_stdout try: conn_obj = load(cff) network = conn_obj.get_connectome_network() surfaces = conn_obj.get_connectome_surface() warning_message = "" results = [] if network: partial = self._parse_connectome_network(network, warning_message, **kwargs) results.extend(partial) if surfaces: partial = self._parse_connectome_surfaces(surfaces, warning_message, should_center) results.extend(partial) self._cleanup_after_cfflib(conn_obj) current_op = dao.get_operation_by_id(self.operation_id) current_op.user_group = conn_obj.get_connectome_meta().title if warning_message: current_op.additional_info = warning_message dao.store_entity(current_op) return results finally: # Make sure to set sys.stdout back to it's default value so this won't # have any influence on the rest of TVB. print_output = custom_stdout.getvalue() sys.stdout = default_stdout custom_stdout.close() # Now log everything that cfflib2 outputes with `print` statements using TVB logging self.logger.debug("Output from cfflib library: %s" % print_output)
#!/usr/global/bin/python2.7 # this script is for v1.2, NOT cmp beta # # -David Grayson 2012 import networkx as nx import sys, cfflib, scipy.io import numpy as np if (len(sys.argv) == 7): #assign variables to all command line args dirs = {'wave': sys.argv[1], 'sub': sys.argv[2], 'scale': sys.argv[3], 'sub_name': sys.argv[4], 'weight': sys.argv[5], 'outdir': sys.argv[6]} #load .pkl file cfile=cfflib.load('%(sub)s/%(wave)s/CMP/cff/%(sub_name)s_%(wave)s.cff' % dirs) cnet = cfile.get_by_name('connectome_%(scale)s' % dirs) cnet.load() g = cnet.data #fill matrix with desired edge for u,v,d in g.edges_iter(data=True): d['weight'] = d['%(weight)s' % dirs] #assign bb variable to matrix bb=nx.to_numpy_matrix(g) #output as .txt file if using number of fibers or fiber length as weight matweight = '%(weight)s' % dirs if matweight in ['number_of_fibers', 'fiber_length_mean', 'fiber_length_std']: np.savetxt('%(outdir)s/%(sub_name)s_%(wave)s_%(scale)s_%(weight)s.txt' % dirs, bb, fmt='%u')
def launch(self, cff): """ Process the uploaded CFF and convert read data into our internal DataTypes. :param cff: CFF uploaded file to process. """ if cff is None: raise LaunchException("Please select CFF file which contains data to import") # !! CFF does logging by the means of `print` statements. We don't want these # logged to terminal as sys.stdout since we no longer have any control over them # so just buffer everything to a StringIO object and log them after operation is done. default_stdout = sys.stdout custom_stdout = cStringIO.StringIO() sys.stdout = custom_stdout try: conn_obj = load(cff) network = conn_obj.get_connectome_network() surfaces = conn_obj.get_connectome_surface() cdatas = conn_obj.get_connectome_data() warning_message = "" if network: msg = self.__parse_connectome_network(network) if msg is not None: warning_message += msg if surfaces: msg = self.__parse_connectome_surface(surfaces, cdatas) if msg is not None: warning_message += msg #################################################################### # !! CFF doesn't delete temporary folders created, # so we need to track and delete them manually!! temp_files = [] root_folder = gettempdir() for ele in conn_obj.get_all(): if hasattr(ele, 'tmpsrc') and os.path.exists(ele.tmpsrc): full_path = ele.tmpsrc while os.path.split(full_path)[0] != root_folder and os.path.split(full_path)[0] != os.sep: full_path = os.path.split(full_path)[0] #Get the root parent from the $gettempdir()$ temp_files.append(full_path) conn_obj.close_all() conn_obj._zipfile.close() for ele in temp_files: if os.path.isdir(ele): shutil.rmtree(ele) elif os.path.isfile(ele): os.remove(ele) current_op = dao.get_operation_by_id(self.operation_id) current_op.user_group = conn_obj.get_connectome_meta().title if len(warning_message) > 0: current_op.additional_info = warning_message dao.store_entity(current_op) finally: # Make sure to set sys.stdout back to it's default value so this won't # have any influence on the rest of TVB. print_output = custom_stdout.getvalue() sys.stdout = default_stdout custom_stdout.close() # Now log everything that cfflib2 outputes with `print` statements using TVB logging LOGGER.info("Output from cfflib2 library: %s" % (print_output,))
def make_trk_file_for_many_connectome_nodes_from_ROI_list(ROI_list_xl_files,cff_file,trk_file_orig, trk_file_new_base, n_fib_thresh=0): """ Generalization of 'make_trk_file_for_2_connectome_nodes' to 2 sets of nodes. 3 Main differences: 1. Node pairs are read from rows in excel file(s), rather than an indices 2. Only a base outputfile stem, rather than the full output filename, needs to be provided, as because multiple output files are produced 3. No option to colour fibres with scalars in this function In order to find connections within one set of nodes, just supply one excel file, in which case the function will only check half the connections """ import cfflib import nibabel as nib c = cfflib.load(cff_file) c_labels = c.get_connectome_data()[3] c_labels.load() c_net = c.get_connectome_network()[0] c_net.load() #c_fibres = c.get_by_name('Tract file 0').load() #c_fibres_array = c_fibres. fibres_orig, hdr_orig = nib.trackvis.read(trk_file_orig, False) if len(ROI_list_xl_files)==2: two_node_lists = 1 ROI_list_dict1 = jg_DWI_util.read_Lausanne2008_ROI_list(ROI_list_xl_files[0]) ROI_list_dict2 = jg_DWI_util.read_Lausanne2008_ROI_list(ROI_list_xl_files[1]) else: two_node_lists = 0 ROI_list_dict1 = jg_DWI_util.read_Lausanne2008_ROI_list(ROI_list_xl_files) ROI_list_dict2 = ROI_list_dict1 for k in range(0, len(ROI_list_dict1.keys())): for kk in range(0,len(ROI_list_dict2.keys())): ROI1_name = str(ROI_list_dict1.values()[k]) ROI1_number = int(ROI_list_dict1.keys()[k]) ROI2_name = str(ROI_list_dict2.values()[kk]) ROI2_number = int(ROI_list_dict2.keys()[kk]) trk_file_new = trk_file_new_base+'_'+str(ROI1_number)+'_'+ROI1_name+'__to__'+str(ROI2_number)+'_'+ROI2_name+'.trk' node_indices = [ROI1_number,ROI2_number] node_indices_reversed = [ROI2_number, ROI1_number] track_indices = [] a = np.nonzero(c_labels.data==ROI1_number)[0] #print 'a = ' + str(a) b = np.nonzero(c_labels.data==ROI2_number)[0] #print 'b = ' + str(b) if ROI1_number in c_net.data.edge[ROI2_number]: n_fibs = c_net.data.edge[ROI2_number][ROI1_number]['number_of_fibers'] elif ROI2_number in c_net.data.edge[ROI1_number]: n_fibs = c_net.data.edge[ROI1_number][ROI2_number]['number_of_fibers'] else: n_fibs = 0 if n_fibs>=n_fib_thresh: #print 'node indices = ' + str(ROI1_number) + ' ' + str(ROI2_number) for a_int in a: if a_int in b: if two_node_lists == 0: if kk>k: track_indices.append(a_int) print 'found track - index ' + str(a_int) + ' , ROIs ' + str(ROI1_number) + ', ' + str(ROI2_number) else: track_indices.append(a_int) print 'found track - index ' + str(a_int) + ' , ROIs ' + str(ROI1_number) + ', ' + str(ROI2_number) if not track_indices == []: hdr_new = hdr_orig.copy() outstreams = [] for i in track_indices: outstreams.append(fibres_orig[i]) n_fib_out = len(outstreams) hdr_new['n_count'] = n_fib_out nib.trackvis.write(trk_file_new, outstreams, hdr_new)
# This script takes a set of compressed connectome files .cff # and produces a new connectome file containing only the requested # connectome object types. The new connectome object names are composed # of the originial connectome file title and the original connectome # object name import cfflib as cf original_connectomes = ['myconnectome1.cff', 'myconnectome2.cff'] extracted_networks = [] for i, con in enumerate(original_connectomes): mycon = cf.load(con) nets = mycon.get_connectome_network() for ne in nets: # here, you might want to skip networks with a given # metadata information ne.load() contitle = mycon.get_connectome_meta().get_title() ne.set_name(str(i) + ': ' + contitle + ' - ' + ne.get_name()) extracted_networks.append(ne) # add networks to new connectome newcon = cf.connectome(title='All CNetworks', connectome_network=extracted_networks) # Setting additional metadata metadata = newcon.get_connectome_meta() metadata.set_creator('My Name') metadata.set_email('My Email') cf.save_to_cff(newcon, 'merged_cnetworks.cff')
# This script takes a set of compressed connectome files .cff # and produces a new connectome file containing only the requested # connectome object types. The new connectome object names are composed # of the originial connectome file title and the original connectome # object name import cfflib as cf original_connectomes = ['myconnectome1.cff', 'myconnectome2.cff'] extracted_networks = [] for i, con in enumerate(original_connectomes): mycon = cf.load(con) nets = mycon.get_connectome_network() for ne in nets: # here, you might want to skip networks with a given # metadata information ne.load() contitle = mycon.get_connectome_meta().get_title() ne.set_name( str(i) + ': ' + contitle + ' - ' + ne.get_name() ) extracted_networks.append(ne) # add networks to new connectome newcon = cf.connectome(title = 'All CNetworks', connectome_network = extracted_networks ) # Setting additional metadata metadata = newcon.get_connectome_meta() metadata.set_creator('My Name') metadata.set_email('My Email') cf.save_to_cff(newcon, 'merged_cnetworks.cff')
import cfflib as cf a=cf.load('/path/to/meta.cml') cf.set_xnat_connection({'server': 'http://localhost:8080/xnat', 'user':'******', 'password': '******'}) # push connectome object #cf.xnat_push(a, projectid = 'DEB02', subjectid= 'L01', experimentid = 'EXP4', overwrite = True) # pull connectom object cf.xnat_pull( projectid = 'DEB02', subjectid= 'L01', experimentid = 'EXP4', storagepath='/tmp')
import cfflib as cf a = cf.load('/path/to/meta.cml') cf.set_xnat_connection({ 'server': 'http://localhost:8080/xnat', 'user': '******', 'password': '******' }) # push connectome object #cf.xnat_push(a, projectid = 'DEB02', subjectid= 'L01', experimentid = 'EXP4', overwrite = True) # pull connectom object cf.xnat_pull(projectid='DEB02', subjectid='L01', experimentid='EXP4', storagepath='/tmp')