def genGraph(infname, outfname, roixmlname=None, roirawname=None, bigGraph=False, outformat="graphml", numfibers=0, centroids=True, **atlases): """ Generate a sparse igraph from an MRI file based on input and output names. Outputs a graphml formatted graph by default positional args: ================ infname - file name of _fiber.dat file outfname - Dir+fileName of output .mat file optional args: ============== roixmlname - file name of _roi.xml file roirawname - file name of _roi.raw file bigGraph - boolean True or False on whether to process a bigraph=True or smallgraph=False numfibers - the number of fibers read_shape - **NOTE** Temp arg used compensate for incorrect reading of the adj mat dims from the xml. Will disappear in future releases. """ start = time() # Determine size of graph to be processed i.e pick a fibergraph module to import if bigGraph: from fibergraph_bg import FiberGraph else: from fibergraph_sm import FiberGraph # If these filenames are undefined then, # assume that there are ROI files in ../roi if not(roixmlname and roirawname): [ inpathname, inbasename ] = os.path.split ( infname ) inbasename = str(inbasename).rpartition ( "_" )[0] roifp = os.path.normpath ( inpathname + "/../roi/" + inbasename ) roixmlname = roifp + "_roi.xml" roirawname = roifp + "_roi.raw" # # Assume that there are mask files in ../mask # maskfp = os.path.normpath ( inpathname + "/../mask/" + inbasename ) # maskxmlname = maskfp + "_mask.xml" # maskrawname = maskfp + "_mask.raw" # Get the ROIs try: roix = roi.ROIXML( roixmlname ) # Create object of type ROIXML rois = roi.ROIData ( roirawname, roix.getShape() ) except: raise Exception("ROI files not found at: %s, %s" % (roixmlname, roirawname)) # Get the mask # try: # maskx = mask.MaskXML( maskxmlname ) # masks = mask.MaskData ( maskrawname, maskx.getShape() ) # except: # print "Mask files not found at: ", maskxmlname, maskrawname # sys.exit (-1) # Create fiber reader reader = FiberReader( infname ) # Create the graph object # get dims from reader fbrgraph = FiberGraph ( reader.shape, rois, None ) print "Parsing MRI studio file {0}".format ( infname ) # Print the high-level fiber information print(reader) count = 0 # iterate over all fibers for fiber in reader: count += 1 # add the contribution of this fiber to the fbrgraph.add(fiber) if numfibers > 0 and count >= numfibers: break if count % 10000 == 0: print ("Processed {0} fibers".format(count) ) #if count == 10000: #break del reader # Done adding edges fbrgraph.complete(centroids, atlases) fbrgraph.saveToIgraph(outfname, gformat=outformat) del fbrgraph print "\nGraph building complete in %.3f secs" % (time() - start) return
def genGraph(infname, data_atlas_fn, outfname, bigGraph=False, \ outformat="graphml", numfibers=0, centroids=True, graph_attrs={}, compress=False, **atlases): """ Generate a sparse igraph from an MRI file based on input and output names. We traverse fiber streamlines and map the voxels which they pass through to a brain region, as determined by an atlas. Big graphs have ROIs which are single voxels, whereas small graphs' ROIs are much larger and can be determined either by size, function, or any other method. Outputs a graphml formatted graph by default. **Positional Arguments** infname: [.dat; binary file] - MRIStudio format fiber streamlines. data_atlas_fn: [.nii; nifti image] - Region labels which will be used to parcellate the fibers. **Returns** outfname: [.graphml; XML file] - Generated graph from fiber tracts. **Optional Arguments** bigGraph: [boolean] (default = False) - Flag indicating where to process a bigGraph=True or smallGraph=False. outformat: [string] (default = graphml) - Requested output format of graph. numfibers: [int] (default = 0) - The number of fibers to read/process. If 0, process all fibers. centroids: [boolean] (default = True) - Flag indicating whether to add centroids for the graph. graph_attrs: [dictionary] (default = {}) - Dict with graph attributes to be added with key=attr_name, value=attr_value. atlases: [dictionary] (default = {}) - Dict with key=atlas_fn, value=region_name_fn compress: [boolean] (default = False) - Flag for compressing graphs (useful for processing large quantities of big graphs) """ start = time() # Determine size of graph to be processed i.e pick a fibergraph module to import if bigGraph: from fibergraph_bg import FiberGraph else: from fibergraph_sm import FiberGraph # This ensures there is at least one atlas by default now if data_atlas_fn not in atlases: print "Adding default atlas" atlases[data_atlas_fn] = None rois = roi.ROIData(data_atlas_fn) # Create fiber reader reader = FiberReader(infname) # DM FIXME: Hacky -- we no longer read shape from fiber file so this happens :-/ reader.shape = rois.data.shape # Create the graph object # get dims from reader fbrgraph = FiberGraph (reader.shape, rois) print "Parsing MRI studio file {0}".format (infname) # Print the high-level fiber information print "Reader Header", reader count = 0 # iterate over all fibers for fiber in reader: count += 1 # add the contribution of this fiber to the fbrgraph.add(fiber) if numfibers > 0 and count >= numfibers: break if count % 10000 == 0: print ("Processed {0} fibers".format(count)) del reader # Done adding edges fbrgraph.complete(centroids, graph_attrs, atlases) fbrgraph.saveToIgraph(outfname, gformat=outformat, compress=compress) del fbrgraph print "\nGraph building complete in %.3f secs" % (time() - start) return