Example #1
0
def add_finaltractandlabels(connectome):

    resolution = gconf.parcellation.keys()
    for r in resolution:

        log.info("Adding final fiber labels array (%s) to connectome..." % str(r))

        cda = cf.CData(name="Final fiber labels (%s)" % str(r),
                       src=op.join(gconf.get_cmp_fibers(), 'final_fiberlabels_%s.npy' % str(r)),
                       fileformat='NumPy',
                       dtype='FiberLabels')
        connectome.add_connectome_data(cda)


        log.info("Adding final tractography for resolution %s" % str(r))

        ctr = cf.CTrack(name='Final Tractography (%s)' % str(r),
                         src=op.join(gconf.get_cmp_fibers(), 'streamline_final_%s.trk' % str(r)),
                          fileformat='TrackVis', dtype='FinalFibers')

        connectome.add_connectome_track(ctr)
        
        log.info("Adding final fiber length array")
        fiberlabels_fname  = op.join(gconf.get_cmp_fibers(), 'final_fiberslength_%s.npy' % str(r))

        cda = cf.CData(name="Final fiber lengths (%s)" % str(r),
                       src=fiberlabels_fname,
                       fileformat='NumPy',
                       dtype='FiberLength')
        connectome.add_connectome_data(cda)
Example #2
0
def add_fibersoriginal2connectome(connectome):

    log.info("Adding original fibers to connectome...")

    fibers_path = gconf.get_cmp_fibers()
    
    ctr = cf.CTrack(name='Original Tractography',
                     src=op.join(fibers_path, 'streamline.trk'),
                      fileformat='TrackVis', dtype='OriginalFibers')
    
    connectome.add_connectome_track(ctr)
Example #3
0
def add_fibers2connectome(connectome):
    
    log.info("Adding filtered fibers to connectome...")
    
    fibers_path = gconf.get_cmp_fibers()
    
    ctr = cf.CTrack(name='Filtered Tractography',
                     src=op.join(fibers_path, 'streamline_filtered.trk'),
                      fileformat='TrackVis',dtype='FilteredFibers')
    # add filter settings as metadata
    ctr.update_metadata({'spline_filtered' : str(gconf.apply_splinefilter),
                        'fiber_cutoff_lower': gconf.fiber_cutoff_lower,
                        'fiber_cutoff_upper': gconf.fiber_cutoff_upper})

    connectome.add_connectome_track(ctr)
Example #4
0
    def _run_interface(self, runtime):
        a = cf.connectome()

        if isdefined(self.inputs.title):
            a.connectome_meta.set_title(self.inputs.title)
        else:
            a.connectome_meta.set_title(self.inputs.out_file)

        if isdefined(self.inputs.creator):
            a.connectome_meta.set_creator(self.inputs.creator)
        else:
            #Probably only works on some OSes...
            a.connectome_meta.set_creator(os.getenv('USER'))

        if isdefined(self.inputs.email):
            a.connectome_meta.set_email(self.inputs.email)

        if isdefined(self.inputs.publisher):
            a.connectome_meta.set_publisher(self.inputs.publisher)

        if isdefined(self.inputs.license):
            a.connectome_meta.set_license(self.inputs.license)

        if isdefined(self.inputs.rights):
            a.connectome_meta.set_rights(self.inputs.rights)

        if isdefined(self.inputs.references):
            a.connectome_meta.set_references(self.inputs.references)

        if isdefined(self.inputs.relation):
            a.connectome_meta.set_relation(self.inputs.relation)

        if isdefined(self.inputs.species):
            a.connectome_meta.set_species(self.inputs.species)

        if isdefined(self.inputs.description):
            a.connectome_meta.set_description(self.inputs.description)

        a.connectome_meta.set_created(datetime.date.today())

        count = 0
        if isdefined(self.inputs.graphml_networks):
            for ntwk in self.inputs.graphml_networks:
                # There must be a better way to deal with the unique name problem
                #(i.e. tracks and networks can't use the same name, and previously we were pulling them both from the input files)
                ntwk_name = 'Network {cnt}'.format(cnt=count)
                a.add_connectome_network_from_graphml(ntwk_name, ntwk)
                count += 1

        if isdefined(self.inputs.gpickled_networks):
            unpickled = []
            for ntwk in self.inputs.gpickled_networks:
                _, ntwk_name, _ = split_filename(ntwk)
                unpickled = nx.read_gpickle(ntwk)
                cnet = cf.CNetwork(name = ntwk_name)
                cnet.set_with_nxgraph(unpickled)
                a.add_connectome_network(cnet)
                count += 1

        count = 0
        if isdefined(self.inputs.tract_files):
            for trk in self.inputs.tract_files:
                _, trk_name, _ = split_filename(trk)
                ctrack = cf.CTrack(trk_name, trk)
                a.add_connectome_track(ctrack)
                count += 1

        count = 0
        if isdefined(self.inputs.gifti_surfaces):
            for surf in self.inputs.gifti_surfaces:
                _, surf_name, _ = split_filename(surf)
                csurf = cf.CSurface.create_from_gifti("Surface %d - %s" % (count,surf_name), surf)
                csurf.fileformat='Gifti'
                csurf.dtype='Surfaceset'
                a.add_connectome_surface(csurf)
                count += 1

        count = 0
        if isdefined(self.inputs.gifti_labels):
            for label in self.inputs.gifti_labels:
                _, label_name, _ = split_filename(label)
                csurf = cf.CSurface.create_from_gifti("Surface Label %d - %s" % (count,label_name), label)
                csurf.fileformat='Gifti'
                csurf.dtype='Labels'
                a.add_connectome_surface(csurf)
                count += 1

        if isdefined(self.inputs.nifti_volumes):
            for vol in self.inputs.nifti_volumes:
                _, vol_name, _ = split_filename(vol)
                cvol = cf.CVolume.create_from_nifti(vol_name,vol)
                a.add_connectome_volume(cvol)

        if isdefined(self.inputs.script_files):
            for script in self.inputs.script_files:
                _, script_name, _ = split_filename(script)
                cscript = cf.CScript.create_from_file(script_name, script)
                a.add_connectome_script(cscript)

        if isdefined(self.inputs.data_files):
            for data in self.inputs.data_files:
                _, data_name, _ = split_filename(data)
                cda = cf.CData(name=data_name, src=data, fileformat='NumPy')
                if not string.find(data_name,'lengths') == -1:
                    cda.dtype = 'FinalFiberLengthArray'
                if not string.find(data_name,'endpoints') == -1:
                    cda.dtype = 'FiberEndpoints'
                if not string.find(data_name,'labels') == -1:
                    cda.dtype = 'FinalFiberLabels'
                a.add_connectome_data(cda)

        a.print_summary()
        _, name, ext = split_filename(self.inputs.out_file)
        if not ext == '.cff':
            ext = '.cff'
        cf.save_to_cff(a,op.abspath(name + ext))

        return runtime
    def perform(self, event, cfile=None):
        """ Performs the action. """

        logger.info('Performing open connectome file action')

        # helper variable to use this function not only in the menubar
        exec_as_funct = True

        if cfile is None:
            # get the instance of the current CFile
            # with the help of the Service Registry
            cfile = self.window.application.get_service(
                'cviewer.plugins.cff2.cfile.CFile')
            exec_as_funct = False

        wildcard = "All files (*.*)|*.*|" \
                   "Nifti-1 (*.nii.gz)|*.nii.gz|" \
                   "Gifti (*.gii)|*.gii|" \
                   "TrackVis tracks (*.trk)|*.trk|" \
                   "Network Pickle (*.gpickle)|*.gpickle|" \
                   "Network GraphML (*.graphml)|*.graphml|" \
                   "Numpy Data (*.npy)|*.npy|" \
                   "Pickle Data (*.pkl)|*.pkl|" \
                   "Text Data (*.txt)|*.txt|" \
                   "CSV Data (*.csv)|*.csv|"

        dlg = FileDialog(wildcard=wildcard,title="Choose a file",\
                         resizeable=False, \
                         default_directory=preference_manager.cviewerui.cffpath,)

        if dlg.open() == OK:

            if not os.path.isfile(dlg.path):
                logger.error("File '%s' does not exist!" % dlg.path)
                return

            logger.info('Read file: %s' % dlg.path)
            fname = os.path.split(dlg.paths[0])[-1]

            if os.path.exists(dlg.paths[0]) and (fname.endswith('.nii.gz')
                                                 or fname.endswith('.nii')):
                cvol = cfflib.CVolume.create_from_nifti(
                    name=fname, nii_filename=dlg.paths[0])
                cfile.obj.add_connectome_volume(cvol)

            elif os.path.exists(dlg.paths[0]) and fname.endswith('.gii'):
                csurf = cfflib.CSurface.create_from_gifti(
                    name=fname, gii_filename=dlg.paths[0])
                cfile.obj.add_connectome_surface(csurf)

            elif os.path.exists(dlg.paths[0]) and fname.endswith('.trk'):
                ctrk = cfflib.CTrack(name=fname, src=dlg.paths[0])
                cfile.obj.add_connectome_track(ctrk)

            elif os.path.exists(dlg.paths[0]) and fname.endswith('.gpickle'):
                ctrk = cfflib.CNetwork(name=fname,
                                       src=dlg.paths[0],
                                       fileformat="NXGPickle")
                cfile.obj.add_connectome_network(ctrk)

            elif os.path.exists(dlg.paths[0]) and fname.endswith('.graphml'):
                ctrk = cfflib.CNetwork.create_from_graphml(
                    name=fname, ml_filename=dlg.paths[0])
                cfile.obj.add_connectome_network(ctrk)

            elif os.path.exists(dlg.paths[0]) and fname.endswith('.npy'):
                cdat = cfflib.CData(name=fname,
                                    src=dlg.paths[0],
                                    fileformat='NumPy')
                cfile.obj.add_connectome_data(cdat)

            elif os.path.exists(dlg.paths[0]) and fname.endswith('.csv'):
                cdat = cfflib.CData(name=fname,
                                    src=dlg.paths[0],
                                    fileformat='CSV')
                cfile.obj.add_connectome_data(cdat)

            elif os.path.exists(dlg.paths[0]) and fname.endswith('.txt'):
                cdat = cfflib.CData(name=fname,
                                    src=dlg.paths[0],
                                    fileformat='TXT')
                cfile.obj.add_connectome_data(cdat)

            elif os.path.exists(dlg.paths[0]) and fname.endswith('.pkl'):
                cdat = cfflib.CData(name=fname,
                                    src=dlg.paths[0],
                                    fileformat='Pickle')
                cfile.obj.add_connectome_data(cdat)

            else:
                logger.info('Could not load file: ' + dlg.paths)

            cfile.update_children()