示例#1
0
 def run(self):
     logger.debug('Invoking TrackVis in a Thread.')
     
     from subprocess import call
     import os.path
     if os.path.exists(self.tvpath):
         command = os.path.join(self.tvpath, "trackvis")
         if os.path.isfile(command):
             logger.debug('Invoking command: ' + str(command) + " " + self.fname)
             try:
                 retcode = call(os.path.join(self.tvpath, "trackvis") + " " + self.fname, shell=True)
                 if retcode < 0:
                     logger.debug("Trackvis Thread was terminated by signal " + str(retcode))
                 else:
                     logger.debug("Trackvis Thread returned with returncode " + str(retcode))
                     
                 logger.debug('Removing unused files from temporary folder.')
                 os.unlink(self.fname)
                 os.unlink(self.trkfname)
                 os.unlink(self.volfname)
                 
             except OSError, e:
                 logger.error("Execution of trackvis failed:" + e)
         else:
             from enthought.traits.ui.api import message
             message(message = 'In the given Trackvis path in the preference, Trackvis executable not found.', \
                     title = "Error")
示例#2
0
    def _trackvis_launch(self):
        """ Generates scene file and launch Trackvis on the selected nodes """
        import tempfile
        
        logger.debug('Starting TrackVis ...')

        # extract selected subgraph
        selectionlist = self.get_selectiongraph_list()
        
        if len(selectionlist) == 0:
            # message            
            from enthought.traits.ui.message import message
            message(message = 'No nodes selected for ROI creation!', title = 'Infomessage', buttons = [ 'OK' ], parent = None)

        tmpgraph = self.graph.subgraph(selectionlist)

        # extract trackfile temporarily
        if len(self.tracks) == 0:
            logger.info('No trackfile found to invoke Trackvis.')
            return
        else:

            # load the first trackfile
            trackfname = self.tracks[0].load_trackfile_to_file()

            # find the first valid segmentation volume in the self.volumes list
            for vol in self.volumes:
                if vol.segmentation:
                    logger.debug('Found a segmentation volume file. Assume labels are corresponding.')
                    volumefname = vol.load_volume_to_file()
                    break

        # generate the scene file in the temporary folder
        tmpscenefile=tempfile.mkstemp(prefix='tmp', suffix='.scene')
            
        # generate trackfile        
        generate_scene_file(scenefname=tmpscenefile[1], \
                          trackfname = trackfname, \
                          volumefname = volumefname, \
                          selectiongraph = tmpgraph)
        
        # execute trackvis in a thread
        pref = preference_manager.preferences       
        action = ThreadedTrackvis(tvpath = pref.get('cviewer.plugins.ui.trackvispath'), \
                                    fname = tmpscenefile[1], \
                                    trkfname = trackfname,\
                                    volfname = volumefname)
        action.start()
示例#3
0
        logger.debug('Invoking TrackVis in a Thread.')
        
        from subprocess import call
        import os.path
        if os.path.exists(self.tvpath):
            command = os.path.join(self.tvpath, "trackvis")
            if os.path.isfile(command):
                logger.debug('Invoking command: ' + str(command) + " " + self.fname)
                try:
                    retcode = call(os.path.join(self.tvpath, "trackvis") + " " + self.fname, shell=True)
                    if retcode < 0:
                        logger.debug("Trackvis Thread was terminated by signal " + str(retcode))
                    else:
                        logger.debug("Trackvis Thread returned with returncode " + str(retcode))
                        
                    logger.debug('Removing unused files from temporary folder.')
                    os.unlink(self.fname)
                    os.unlink(self.trkfname)
                    os.unlink(self.volfname)
                    
                except OSError, e:
                    logger.error("Execution of trackvis failed:" + e)
            else:
                from enthought.traits.ui.api import message
                message(message = 'In the given Trackvis path in the preference, Trackvis executable not found.', \
                        title = "Error")
        else:
            from enthought.traits.ui.api import message
            message(message = 'You have to choose the path to the Trackvis executable in the preferences!', \
                    title = "Error")
示例#4
0
文件: tocff.py 项目: Ascronia/Keepy
    def _create_cff_changed(self, value):
    import os
    from os.path import join, exists, split
    
    # Generate Connectome File
    # -----
    if self.convert == 'Subjects':
        suffix = '_subjects'
    elif self.convert == 'Statistics: CorrCoeff':
        suffix = '_statistics'
    elif self.convert == 'Statistics: t-Statistics':
        suffix = '_tstatistics'
    
    def removeDir(path):
        import shutil
        import os.path
        if os.path.isdir(path):
            shutil.rmtree(path, ignore_errors = True)
    
    # create folder CFF
    if not exists(join(self.rootfolder, 'CFF'+ suffix)):
        os.mkdir(join(self.rootfolder, 'CFF'+ suffix))
    else:
        # remove folder if it exists
        removeDir(join(self.rootfolder, 'CFF'+ suffix))
        os.mkdir(join(self.rootfolder, 'CFF'+ suffix))
    # create subfolder Networks
    if not exists(join(self.rootfolder, 'CFF'+ suffix, 'Network')):
        os.mkdir(join(self.rootfolder, 'CFF'+ suffix, 'Network'))
    # create subfolder Volumes
    if not exists(join(self.rootfolder, 'CFF'+ suffix, 'Nifti')):
        os.mkdir(join(self.rootfolder, 'CFF'+ suffix, 'Nifti'))
    
    # create electrode position network
    if self.write_electrode_network:
        outcontent, nodes = electrode_pos_to_graphml(filename = self.electrode_coordinates_file )
        fi = open(join(self.rootfolder, 'CFF'+ suffix, 'Network', 'electrodes.graphml'), 'w')
        fi.write(outcontent)
        fi.close()
    
    # create ROI segmentation volume nifti file
    if not self.ROI_slor_file == '':
        nim = slor2nifti(filename = self.ROI_slor_file, merge_roi = True)
        filename = split(self.ROI_slor_file)[1].split('.')[0]
        nim.to_filename(join(self.rootfolder, 'CFF'+ suffix, 'Nifti', filename + '.nii.gz'))

    
    # create meta.xml
    fim = open(join(self.rootfolder, 'CFF' + suffix, 'meta.xml'), 'w')
    content = """<?xml version="1.0" encoding="UTF-8"?>
<viewer xmlns="http://www.connectome.ch/2009/Connectome/xmlns"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.connectome.ch/2009/Connectome/xmlns
                        connectome.xsd">

<viewer-meta version="1.0">"""
    content = content + self.metainfo.get_metaxml() + """
    <nr_of_networks>%s</nr_of_networks>
</viewer-meta>\n""" % str(len(self.subjects) + 1)
    
    # add networks
    # electrode
    if self.write_electrode_network:
        content = content + """<viewer-network name="Electrodes" src="Network/electrodes.graphml" hierarchical="0" hypergraph="0" directed="0" />\n"""

    # -----
    if self.convert == 'Statistics: CorrCoeff':
        
        for mes in self.stats_measure:
        #print 'Calculating measure: ', mes
        
        for i, name in enumerate(self.stats_name):
            #print 'For name: ', name
            
            # number of external variables has to be an integer
            val = int(self.stats_extvars[i])
            
            # loop through all the independent variables and create graphml            
            for extvar_i in range(1, val + 1):
            
            #print 'Independent Variable: ', extvar_i
            # correct zero-padding
            if val > 0:
                extvar_name = '%02d' % extvar_i
            elif val > 99:
                extvar_name = '%03d' % extvar_i
            
            fi = open(join(self.rootfolder, 'CFF'+ suffix, 'Network', 'statistic_'+name+'_indvar'+extvar_name+'.graphml'), 'wb')
            content2 = statistics2graphml(rootfolder = self.rootfolder, nr_rois = self.nr_rois, \
                             name = name, measure = mes, \
                             extvar = extvar_name, freq_bands = self.freq_bands, \
                             el_cord_file = self.electrode_coordinates_file)
            
            fi.write(content2)
            fi.close()
    
                # output network in metaxml
            content = content + """<viewer-network name="%s-%s-IndVar%s" src="Network/statistic_%s_indvar%s.graphml" hierarchical="0" hypergraph="0" directed="0">""" \
                  % (mes, name, extvar_name, name, extvar_name)
            if not self.ROI_slor_file == '':
                content = content + """
          <network-volume name="%s" src="Nifti/%s.nii.gz" fileformat="nifti" segmentation="true">
            <description>The segmentation of the brain in Regions of Interest.</description>
          </network-volume>\n""" % (filename, filename)
            content = content + """</viewer-network>\n"""
            
    elif self.convert == 'Statistics: t-Statistics':
        
        for mes in self.stats_measure:
        #print 'Calculating measure: ', mes
        
        for i, name in enumerate(self.stats_name):
            #print 'For name: ', name
            
            fi = open(join(self.rootfolder, 'CFF'+ suffix, 'Network', 'tstatistic_'+name+'_'+mes+'.graphml'), 'wb')
            content3 = tstatistics2graphml(rootfolder = self.rootfolder, nr_rois = self.nr_rois, \
                         name = name, measure = mes, \
                         freq_bands = self.freq_bands, \
                         el_cord_file = self.electrode_coordinates_file)
        
            fi.write(content3)
            fi.close()
    
            # output network in metaxml
            content = content + """<viewer-network name="%s-%s" src="Network/tstatistic_%s_%s.graphml" hierarchical="0" hypergraph="0" directed="0">""" \
                  % (name, mes, name, mes)
            if not self.ROI_slor_file == '':
            content = content + """
          <network-volume name="%s" src="Nifti/%s.nii.gz" fileformat="nifti" segmentation="true">
        <description>The segmentation of the brain in Regions of Interest.</description>
          </network-volume>\n""" % (filename, filename)
            content = content + """</viewer-network>\n"""
        
    elif self.convert == 'Subjects':
        
        # loop through all the subjects and create graphml
        for subj in self.subjects:
        fi = open(join(self.rootfolder, 'CFF'+ suffix, 'Network', 'subject_'+subj+'.graphml'), 'wb')
        content2 = coherences2graphml(rootfolder = self.rootfolder, nr_rois = self.nr_rois, \
                         subject = subj, conditions = self.conditions, \
                         measures = self.measures, freq_bands = self.freq_bands, \
                         el_cord_file = self.electrode_coordinates_file)
        fi.write(content2)
        fi.close()
    
        # subjects
        for subj in self.subjects:
        content = content + """<viewer-network name="Subject %s" src="Network/subject_%s.graphml" hierarchical="0" hypergraph="0" directed="0">""" \
              % (subj, subj)
        content = content + """
      <network-volume name="%s" src="Nifti/%s.nii.gz" fileformat="nifti" segmentation="true">
        <description>The segmentation of the brain in Regions of Interest.</description>
      </network-volume>\n""" % (filename, filename)
        content = content + """</viewer-network>\n"""
    
    
    # ending
    content = content + "</viewer>"
    fim.write(content)
    fim.close()
    
    # create .cff with Zip
    import zipfile as zip
    filen = self.metainfo.meta_name.replace(' ', '_')
    filen = filen.lower()
    mycff = zip.ZipFile(join(self.rootfolder, filen + suffix + '.cff'), mode='w')
    
    os.chdir(join(self.rootfolder, 'CFF' + suffix))
    for root, dirs, files in os.walk('.'):
        for name in files:       
        filename = os.path.join(root, name)
        # print 'Writing to Connectome File: ', filename
        mycff.write(filename)
    mycff.close()
    
    message(message = 'Connectome File generated in Root Folder!', title = 'Done.')


#####

    
def electrode_pos_to_graphml(filename):
    """ Converts the electrode position file to a network without edges """
    
    f = open(filename, 'r')
    nodes = ''
    for i, line in enumerate(f):
        
        if i == 0:
            nr_of_electrodes = line
        else:
            lsp = line.split(' ')
            x, y, z, name = float(lsp[0]), float(lsp[1]), float(lsp[2]), lsp[3].rstrip('\r\n')
            nodes = nodes + """<node id="n%s">
    <data key="dn_label">%s</data>
    <data key="dn_position">%s,%s,%s</data>
</node>
""" % (i, name, x, y, z)
            
    outfile = startstring % nr_of_electrodes
    outfile = outfile + nodes
    outfile = outfile + endstring
    f.close()
    
    return outfile, nodes
    
startstring = """<?xml version="1.0" encoding="UTF-8"?>

<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xlink="http://www.w3.org/1999/xlink"

    xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns 
    http://www.connectome.ch/graphml+xlink+gifti.xsd">

<key id="dn_label" for="node" attr.name="label" attr.type="string">
    <desc>The label of the electrode</desc>
</key>
<key id="dn_position" for="node" attr.name="position" attr.type="string">
    <desc>The position of the electrode</desc>
</key>
<graph id="%s electrodes" edgedefault="undirected">
"""
endstring = """</graph>
</graphml>
"""



def tstatistics2graphml(rootfolder, nr_rois, name = None, \
                       measure = None, freq_bands = None, el_cord_file = None):
    """ Converts sLORETA t-statistic files into GraphML
    Naming convention: name-measure.txt
    E.g. A-B-EEGLaggedConn.txt
        
    """
    
    from os.path import exists, isfile, join
    from numpy import zeros, array
    
    if not exists(rootfolder):
        raise Exception('Path %s does not exist.' % rootfolder)
        return

    outfile = startstring_coh
    
    # write edge keys
    for freq in freq_bands:
    key = '<key id="%s" for="edge" attr.name="%s" attr.type="double"><default>0</default></key>\n'
    outfile = outfile + key % (freq, freq)
    
    # start graph
    outfile = outfile + '<graph id="%s" edgedefault="undirected">\n' % name
    
    # write nodes (ROIs)
    # use the electrode positions preliminary
    out, nodes = electrode_pos_to_graphml(filename = join(rootfolder, el_cord_file))
    outfile = outfile + nodes
    
    # write edges

    file = join(rootfolder, name+'-'+measure+'.txt')
    if isfile(file):
    print 'Converting file %s ...' % str(file)
    nr_elect, nr_mat = extract_metadata_stats(filename = file)

    # number of electrodes (ROIs) from the file header must be equal as the one given in the form
    assert nr_elect == nr_rois
    # number of matrices must equal number of frequency bands given
    assert nr_mat == len(freq_bands)
    
    f = open(file, 'r')
    # skip header
    a = f.readline()
    a = f.readline()
    while ' = ' in a  or a == '':
        a = f.readline()

    # create numpy array for all frequency bands
    data_arr = zeros((nr_elect, nr_elect, nr_mat))

    for mat_i in range(nr_mat):
        for elect_i in range(nr_elect):
        line = f.readline()
        lsp = line.split(' ')
        
        while lsp.count('') != 0:
            lsp.remove('')

#        print 'lenlsp',len(lsp)
        #print nr_elect
        assert len(lsp) == nr_elect

        data_arr[elect_i, :, mat_i] = array(lsp)
        # skip empty line
        f.readline()
    #print 'Done.'
    else:
    #print 'No file %s existing.' % file
    return
    
                
    # now loop over bigarray and write the the edges
    for i in range(0, nr_rois):
        for j in range(i, nr_rois):
            if i == j:
                # skipping self-loops
                continue
            
            outfile = outfile + '<edge id="e%s_%s" source="n%s" target="n%s">\n' \
                      % (str(i+1), str(j+1), str(i+1), str(j+1))
            
        for freq_i, frequency_label in enumerate(freq_bands):                            
        # loop over all frequency bands
        
        label, value = frequency_label, data_arr[i,j,freq_i]
        if value == 0.0:
            continue
        # convert value (float) to truncated string
        value = value
        outfile = outfile + '    <data key="%s">%s</data>\n'\
                    % (label, str(value))
            
        outfile = outfile + '</edge>\n'

    outfile = outfile + endstring
    return outfile

def statistics2graphml(rootfolder, nr_rois, name = None, extvar = None, \
                       measure = None, freq_bands = None, el_cord_file = None):
    """ Converts sLORETA statistic files (correlations) into GraphML
        
    """
    
    from os.path import exists, isfile, join
    from numpy import zeros, array
    
    if not exists(rootfolder):
        raise Exception('Path %s does not exist.' % rootfolder)
        return

    outfile = startstring_coh
    
    # write edge keys
    for freq in freq_bands:
    key = '<key id="%s" for="edge" attr.name="%s" attr.type="double"><default>0</default></key>\n'
    outfile = outfile + key % (freq, freq)
    
    # start graph
    outfile = outfile + '<graph id="%s" edgedefault="undirected">\n' % name
    
    # write nodes (ROIs)
    # use the electrode positions preliminary
    out, nodes = electrode_pos_to_graphml(filename = join(rootfolder, el_cord_file))
    outfile = outfile + nodes
    
    # write edges

    file = join(rootfolder, name+'-'+measure+'-IndVar'+extvar+'.txt')
    if isfile(file):
    #print 'Converting file %s ...' % str(file)
    nr_elect, nr_mat = extract_metadata_stats(filename = file)

    # number of electrodes (ROIs) from the file header must be equal as the one given in the form
    assert nr_elect == nr_rois
    # number of matrices must equal number of frequency bands given
    assert nr_mat == len(freq_bands)
    
    f = open(file, 'r')
    # skip header
    a = f.readline()
    a = f.readline()
    while ' = ' in a  or a == '':
        a = f.readline()

    # create numpy array for all frequency bands
    data_arr = zeros((nr_elect, nr_elect, nr_mat))

    for mat_i in range(nr_mat):
        for elect_i in range(nr_elect):
        line = f.readline()
        lsp = line.split(' ')
        
        while lsp.count('') != 0:
            lsp.remove('')

        assert len(lsp) == nr_elect

        data_arr[elect_i, :, mat_i] = array(lsp)
        # skip empty line
        f.readline()
    #print 'Done.'
    else:
    #print 'No file %s existing.' % file
    return
    
                
    # now loop over bigarray and write the the edges
    for i in range(0, nr_rois):
        for j in range(i, nr_rois):
            if i == j:
                # skipping self-loops
                continue
            
            outfile = outfile + '<edge id="e%s_%s" source="n%s" target="n%s">\n' \
                      % (str(i+1), str(j+1), str(i+1), str(j+1))
            
        for freq_i, frequency_label in enumerate(freq_bands):                            
        # loop over all frequency bands
        
        label, value = frequency_label, data_arr[i,j,freq_i]
        if value == 0.0:
            continue
        # convert value (float) to truncated string
        value = value
        outfile = outfile + '    <data key="%s">%s</data>\n'\
                    % (label, str(value))
            
        outfile = outfile + '</edge>\n'

    outfile = outfile + endstring
    return outfile

def coherences2graphml(rootfolder, nr_rois, subject = None, conditions = None, \
                       measures = None, freq_bands = None, el_cord_file = None):
    """ Given a proper folder structure, all subjects, conditions and measures are
    searched and converted to GraphML files for all frequency bands.
    
    If allinone is True, all the files must be stored only in the root folder, but
    subjects, conditions, measures and frequency_bands have to be given according
    to the file name convention.
    
    Example:
    Files like VP07R1-ROIlaggedCoh.txt with the classical frequency bands amounts to calling
    (number of ROIs and number of frequency_bands is according to sLORETA selections)
    
    rootfolder = '/', nr_rois, subjects = 'VP07', conditions = ['R1'], \
        measures = ['ROIlaggedCoh'], freq_band['alpha1', 'alpha2'], el_cord_file = 'List19e.sxyz'
    
    """

    from os.path import exists, isfile, join
    from numpy import zeros, array
    
    if not exists(rootfolder):
        raise Exception('Path %s does not exist.' % rootfolder)
        return

    outfile = startstring_coh
    
    # write edge keys
    for cond in conditions:
        for meas in measures:
            for freq in freq_bands:
                key = '<key id="%s-%s-%s" for="edge" attr.name="%s.%s.%s" attr.type="double"><default>0</default></key>\n'
                outfile = outfile + key % (cond, meas, freq, cond, meas, freq)
    
    # start graph
    outfile = outfile + '<graph id="%s" edgedefault="undirected">\n' % subject
    
    # write nodes (ROIs)
    # use the electrode positions preliminary
    out, nodes = electrode_pos_to_graphml(filename = join(rootfolder, el_cord_file))
    outfile = outfile + nodes
    
    # write edges
    bigarray = {}
    for cond in conditions:
        for meas in measures:
            file = join(rootfolder, subject+cond+'-'+meas+'.txt')
            if isfile(file):
                #print 'Converting file %s ...' % str(file)
                nr_elect, nr_mat = extract_metadata(filename = file)
        # number of electrodes (ROIs) from the file header must be equal as the one given in the form
                assert nr_elect == nr_rois
        # number of matrices must equal number of frequency bands given
        assert nr_mat == len(freq_bands)
                
                f = open(file, 'r')
                # skip header
                a = f.readline()
                while ' = ' in a  or 'File name' in a or a == '':
                    a = f.readline()
                # create numpy array for all frequency bands
                data_arr = zeros((nr_elect, nr_elect, nr_mat))
                
                for mat_i in range(nr_mat):
            #print 'Matrix: ', str(mat_i)
                    for elect_i in range(nr_elect):
            #print 'ROI:', str(elect_i)
                        line = f.readline()

                        lsp = line.split('  ')
            try:
                lsp.remove('') # remove first empty character
            except ValueError:
                break

                        assert len(lsp) == nr_elect
                        data_arr[elect_i, :, mat_i] = array(lsp)
                        
                #print 'Done.'
                
                bigarray[cond + '-' + meas] = data_arr
                # now store data_arr
                
    # now loop over bigarray and write the the edges
    for i in range(0, nr_rois):
        for j in range(i, nr_rois):
            if i == j:
                # skipping self-loops
                continue
            
            outfile = outfile + '<edge id="e%s_%s" source="n%s" target="n%s">\n' \
                      % (str(i+1), str(j+1), str(i+1), str(j+1))
            
            for k, v in bigarray.items():
                # number of matrices has to be the same as the number of frequency bands
                assert v.shape[2] == len(freq_bands)
                for freq_i, frequency_label in enumerate(freq_bands):
                    # loop over all frequency bands
                    label, value = k+'-'+frequency_label, v[i,j,freq_i]
            if value == 0.0:
            continue

                    # convert value (float) to truncated string
                    value = value
                    outfile = outfile + '    <data key="%s">%s</data>\n'\
                                        % (label, str(value))
            outfile = outfile + '</edge>\n'

    outfile = outfile + endstring
    return outfile

startstring_coh = """<?xml version="1.0" encoding="UTF-8"?>

<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xlink="http://www.w3.org/1999/xlink"

    xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns 
    http://www.connectome.ch/graphml+xlink+gifti.xsd">

<key id="dn_label" for="node" attr.name="label" attr.type="string">
    <desc>The label of the ROI</desc>
</key>
<key id="dn_position" for="node" attr.name="position" attr.type="string">
    <desc>The position of the ROI</desc>
</key>
"""


def extract_metadata_stats(filename):
    """ Extract metadata for statistics file, namely number of electrodes and nr of matrices
    and band type """
        
    f = open(filename, 'r')
    for line in f:
    
        lsp = line.split(' = ')
        if len(lsp) == 1:
            continue

        if 'Number of regions of interest' in lsp[1]:
            nr_elect = int(lsp[0])
        elif 'Number of matrices' in lsp[1]:
            nr_mat = int(lsp[0])
        
    f.close()
    return nr_elect, nr_mat
示例#5
0
    def _create_cff_changed(self, value):
	import os
	from os.path import join, exists, split
	
	# Generate Connectome File
	# -----
	if self.convert == 'Subjects':
	    suffix = '_subjects'
	elif self.convert == 'Statistics: CorrCoeff':
	    suffix = '_statistics'
	elif self.convert == 'Statistics: t-Statistics':
	    suffix = '_tstatistics'
	
	def removeDir(path):
	    import shutil
	    import os.path
	    if os.path.isdir(path):
	        shutil.rmtree(path, ignore_errors = True)
	
	# create folder CFF
	if not exists(join(self.rootfolder, 'CFF'+ suffix)):
	    os.mkdir(join(self.rootfolder, 'CFF'+ suffix))
	else:
	    # remove folder if it exists
	    removeDir(join(self.rootfolder, 'CFF'+ suffix))
	    os.mkdir(join(self.rootfolder, 'CFF'+ suffix))
	# create subfolder Networks
	if not exists(join(self.rootfolder, 'CFF'+ suffix, 'Network')):
	    os.mkdir(join(self.rootfolder, 'CFF'+ suffix, 'Network'))
	# create subfolder Volumes
	if not exists(join(self.rootfolder, 'CFF'+ suffix, 'Nifti')):
	    os.mkdir(join(self.rootfolder, 'CFF'+ suffix, 'Nifti'))
	
	# create electrode position network
	if self.write_electrode_network:
	    outcontent, nodes = electrode_pos_to_graphml(filename = self.electrode_coordinates_file )
	    fi = open(join(self.rootfolder, 'CFF'+ suffix, 'Network', 'electrodes.graphml'), 'w')
	    fi.write(outcontent)
	    fi.close()
	
	# create ROI segmentation volume nifti file
	if not self.ROI_slor_file == '':
	    nim = slor2nifti(filename = self.ROI_slor_file, merge_roi = True)
	    filename = split(self.ROI_slor_file)[1].split('.')[0]
	    nim.to_filename(join(self.rootfolder, 'CFF'+ suffix, 'Nifti', filename + '.nii.gz'))

	
	# create meta.xml
	fim = open(join(self.rootfolder, 'CFF' + suffix, 'meta.xml'), 'w')
	content = """<?xml version="1.0" encoding="UTF-8"?>
<viewer xmlns="http://www.connectome.ch/2009/Connectome/xmlns"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.connectome.ch/2009/Connectome/xmlns
					    connectome.xsd">

<viewer-meta version="1.0">"""
	content = content + self.metainfo.get_metaxml() + """
    <nr_of_networks>%s</nr_of_networks>
</viewer-meta>\n""" % str(len(self.subjects) + 1)
    
	# add networks
	# electrode
	if self.write_electrode_network:
	    content = content + """<viewer-network name="Electrodes" src="Network/electrodes.graphml" hierarchical="0" hypergraph="0" directed="0" />\n"""

	# -----
	if self.convert == 'Statistics: CorrCoeff':
	    
	    for mes in self.stats_measure:
		#print 'Calculating measure: ', mes
		
		for i, name in enumerate(self.stats_name):
		    #print 'For name: ', name
		    
		    # number of external variables has to be an integer
		    val = int(self.stats_extvars[i])
		    
		    # loop through all the independent variables and create graphml		    
		    for extvar_i in range(1, val + 1):
			
			#print 'Independent Variable: ', extvar_i
			# correct zero-padding
			if val > 0:
			    extvar_name = '%02d' % extvar_i
			elif val > 99:
			    extvar_name = '%03d' % extvar_i
			
			fi = open(join(self.rootfolder, 'CFF'+ suffix, 'Network', 'statistic_'+name+'_indvar'+extvar_name+'.graphml'), 'wb')
			content2 = statistics2graphml(rootfolder = self.rootfolder, nr_rois = self.nr_rois, \
						     name = name, measure = mes, \
						     extvar = extvar_name, freq_bands = self.freq_bands, \
						     el_cord_file = self.electrode_coordinates_file)
		    
			fi.write(content2)
			fi.close()
	
		        # output network in metaxml
			content = content + """<viewer-network name="%s-%s-IndVar%s" src="Network/statistic_%s_indvar%s.graphml" hierarchical="0" hypergraph="0" directed="0">""" \
				  % (mes, name, extvar_name, name, extvar_name)
			if not self.ROI_slor_file == '':
			    content = content + """
		  <network-volume name="%s" src="Nifti/%s.nii.gz" fileformat="nifti" segmentation="true">
		    <description>The segmentation of the brain in Regions of Interest.</description>
		  </network-volume>\n""" % (filename, filename)
			content = content + """</viewer-network>\n"""
			
	elif self.convert == 'Statistics: t-Statistics':
	    
	    for mes in self.stats_measure:
		#print 'Calculating measure: ', mes
		
		for i, name in enumerate(self.stats_name):
		    #print 'For name: ', name
		    
		    fi = open(join(self.rootfolder, 'CFF'+ suffix, 'Network', 'tstatistic_'+name+'_'+mes+'.graphml'), 'wb')
		    content3 = tstatistics2graphml(rootfolder = self.rootfolder, nr_rois = self.nr_rois, \
						 name = name, measure = mes, \
						 freq_bands = self.freq_bands, \
						 el_cord_file = self.electrode_coordinates_file)
		
		    fi.write(content3)
		    fi.close()
    
		    # output network in metaxml
		    content = content + """<viewer-network name="%s-%s" src="Network/tstatistic_%s_%s.graphml" hierarchical="0" hypergraph="0" directed="0">""" \
			      % (name, mes, name, mes)
		    if not self.ROI_slor_file == '':
			content = content + """
	      <network-volume name="%s" src="Nifti/%s.nii.gz" fileformat="nifti" segmentation="true">
		<description>The segmentation of the brain in Regions of Interest.</description>
	      </network-volume>\n""" % (filename, filename)
		    content = content + """</viewer-network>\n"""
		
	elif self.convert == 'Subjects':
	    
	    # loop through all the subjects and create graphml
	    for subj in self.subjects:
		fi = open(join(self.rootfolder, 'CFF'+ suffix, 'Network', 'subject_'+subj+'.graphml'), 'wb')
		content2 = coherences2graphml(rootfolder = self.rootfolder, nr_rois = self.nr_rois, \
					     subject = subj, conditions = self.conditions, \
					     measures = self.measures, freq_bands = self.freq_bands, \
					     el_cord_file = self.electrode_coordinates_file)
		fi.write(content2)
		fi.close()
	
	    # subjects
	    for subj in self.subjects:
		content = content + """<viewer-network name="Subject %s" src="Network/subject_%s.graphml" hierarchical="0" hypergraph="0" directed="0">""" \
			  % (subj, subj)
		content = content + """
	  <network-volume name="%s" src="Nifti/%s.nii.gz" fileformat="nifti" segmentation="true">
	    <description>The segmentation of the brain in Regions of Interest.</description>
	  </network-volume>\n""" % (filename, filename)
		content = content + """</viewer-network>\n"""
	
	
	# ending
	content = content + "</viewer>"
	fim.write(content)
	fim.close()
	
	# create .cff with Zip
	import zipfile as zip
	filen = self.metainfo.meta_name.replace(' ', '_')
	filen = filen.lower()
	mycff = zip.ZipFile(join(self.rootfolder, filen + suffix + '.cff'), mode='w')
	
	os.chdir(join(self.rootfolder, 'CFF' + suffix))
	for root, dirs, files in os.walk('.'):
	    for name in files:       
		filename = os.path.join(root, name)
		# print 'Writing to Connectome File: ', filename
		mycff.write(filename)
	mycff.close()
    
	message(message = 'Connectome File generated in Root Folder!', title = 'Done.')