def braineacData(self, probeList, naVal="NA"): """ Get braineac data for the specified list of probes. """ # create a brain object self.b = mbo.brainObj() # the list of brineac nodes nodeList = [v for v in self.regValDict.keys()] nodeList.sort() # iterate through the braineac regions for n, k in enumerate(nodeList): # open the data file for the appropriate regions inFile = "expr_" + self.regValDict[k] + '.txt' f = open(path.join(self.braineacDir, inFile), "r") lines = f.readlines() # have a look through each line of the data file to look for the appropriate probe data for line in lines: bits = line.split() if bits[0] in probeList: pMean = np.mean( [float(v) for v in bits[1:] if not v == naVal]) self.b.G.add_node(n, attr_dict={bits[0]: pMean})
def comparison(self, assMat, delim=" ", spatialFile="parcel_500.txt", parcellation="parcels/parcel_500.nii", outFile="braineacNodes.txt", midLine=44.5): """ now let's match up the imaging data to the braineac data to create a brain object and import an association matrix. """ self.a = mbo.brainObj() self.a.importAdjFile(assMat, delimiter=delim) # get the spatial information and parcellation scheme self.a.importSpatialInfo(spatialFile) self.a.importISO(parcellation) self.a.parcels(self.a.G.nodes()) # set up output file outFile = open(outFile, "w") for node in self.a.G.nodes(): self.a.G.node['braineac'] = None self.nodeDict = {n:{"L":[], "R":[]} for n in range(1,8)} for n in self.nodeDict.keys(): tmp = self.a.parcelList[np.where(self.out==n)] nodeList = [int(v) for v in np.unique(tmp.data.flatten()) if not v==0] # check if 50% of parcel is in the braineac region for node in nodeList: rat = float(len(self.a.parcelList[np.where(self.a.parcelList==node) and np.where(self.a.parcelList==n)])) / float(len(self.a.parcelList[np.where(self.a.parcelList==node)])) if rat < 0.5: print str(node)+" less than 50% in "+str(n) elif self.a.G.node[node-1]["xyz"][0] < midLine: self.nodeDict[n]["L"].append(node-1) self.a.G.node[node-1]['braineac'] = self.regValDict[n]+"L" else: self.nodeDict[n]["R"].append(node-1) self.a.G.node[node-1]['braineac'] = self.regValDict[n]+"R" # write the output in to a file outFile.writelines(self.regValDict[n]+"L" + ' ' + ' '.join([str(v) for v in self.nodeDict[n]["L"]])+'\n') outFile.writelines(self.regValDict[n]+"R" + ' ' + ' '.join([str(v) for v in self.nodeDict[n]["R"]])+'\n') outFile.close()
def braineacData(self, probeList, naVal="NA"): """ Get braineac data for the specified list of probes. """ # create a brain object self.b = mbo.brainObj() # the list of brineac nodes nodeList = [v for v in self.regValDict.keys()] nodeList.sort() # iterate through the braineac regions for n,k in enumerate(nodeList): # open the data file for the appropriate regions inFile = "expr_"+self.regValDict[k]+'.txt' f = open(path.join(self.braineacDir,inFile), "r") lines = f.readlines() # have a look through each line of the data file to look for the appropriate probe data for line in lines: bits = line.split() if bits[0] in probeList: pMean = np.mean([float(v) for v in bits[1:] if not v==naVal]) self.b.G.add_node(n, attr_dict={bits[0]:pMean})
# 355, 250, 296, 491, 423, 140, 205, 434, 26, 244, # 21, 142, 493, 76, 232, 234, 422] # excluded nodes for Tim's original analysis #excludedNodes = [28, 303, 355, 339, 131, 250, 491, 205, 423, 140, 434, 142, 235, # 244, 493, 21, 26, 232, 76, 234, 422] # nodes with insufficient coverage excludedNodes = [21, 26, 28, 35, 54, 73, 75, 76, 129, 133, 135, 140, 142, 160, 169, 177, 189, 232, 234, 235, 244, 245, 294, 304, 322, 339, 363, 386, 396, 419, 422, 423, 434, 476, 481, 493] parcelFile = "parcel_500.txt" thresholdtype = "local" dVal = "2" adjMatFile = "wave_cor_mat_level_"+dVal+"d_500_z.txt" # = "mean_2d_500.txt" delim=" " a = mbt.brainObj() appVal = False # unweighted measures for e in edgePCCons: ofb = '_'.join(["brain", thresholdtype, str(e), "d"+dVal+"_"]) propDict = {"edgePC":str(e)} a.importAdjFile(adjMatFile, delimiter=delim, exclnodes=excludedNodes) a.localThresholding(edgePC=e) a.removeUnconnectedNodes() # degs = a.G.degree(weight='weight') # extras.writeResults(degs, "degreeWt", ofb, append=appVal) a.binarise()
def __init__(self, assocMat, nodesToExclude=[], delim=" ", subjList=None, spatialFile="parcel_500.txt", symmetrise=False, convertMNI=False, mirror=True): if subjList: self.subjList = subjList else: self.subjList = [v for v in glob("17823*") if path.isdir(v)] self.fName = "SampleAnnot.csv" self.maFile = "MicroarrayExpression.csv" self.probeFile = "Probes.csv" self.mirror = mirror # if symmetrise is true then regions are identified by the structure name, # if symmetrise is false, then regions are identified by the structural acronym # these refer to headers in the SampleAnnot.csv file if symmetrise: self.sLab = "structure_acronym" else: self.sLab = "structure_name" # set up brain for expression data self.a = mbo.brainObj() node_counter = 0 self.headers = {} self.sIDDict = { } # dictionary storing the list of nodes for each structural ID by subject - for use later in averaging across all subjects for subj in self.subjList: self.sIDDict[subj] = {} # import probe data f = open(path.join(subj, self.fName), "rb") reader = csv.DictReader(f, delimiter=",", quotechar='"') self.headers[subj] = ['probe'] for l in reader: # n = int(l["structure_id"]) sID = l[self.sLab] n = node_counter # GIVE NODES UNIQUE INCREMENTAL ID (across all subjects) if not self.a.G.has_node(n): self.a.G.add_node(n) self.a.G.node[n] = l self.a.G.node[n][ 'sID'] = sID # store the structure_acronym/structure_name for the node node_counter += 1 # self.headers[subj].append(l["structure_id"]) self.headers[subj].append( sID ) #STORE structure_acronym or structure_name depending on symmetrise if not sID in self.sIDDict[subj].keys(): self.sIDDict[subj][sID] = [n] else: self.sIDDict[subj][sID].append(n) f.close() if convertMNI: # convert location data for Allen brain from MNI space for n in self.a.G.nodes(): x = 45 - (float(self.a.G.node[n]['mni_x']) / 2) y = 63 + (float(self.a.G.node[n]['mni_y']) / 2) z = 36 + (float(self.a.G.node[n]['mni_z']) / 2) self.a.G.node[n]['xyz'] = (x, y, z) else: for n in self.a.G.nodes(): x = float(self.a.G.node[n]['mni_x']) y = float(self.a.G.node[n]['mni_y']) z = float(self.a.G.node[n]['mni_z']) self.a.G.node[n]['xyz'] = (x, y, z) if self.mirror and len(self.a.G.nodes()) < 600: self.a.copyHemisphere() # f.write('%s, %s, %s, %s \n' % (str(n), str(self.a.G.node[n]['xyz'][0]),str(self.a.G.node[n]['xyz'][1]),str(self.a.G.node[n]['xyz'][2]))) #f.close() # set up brain with graph properties self.c = mbo.brainObj() self.c.importAdjFile(assocMat, delimiter=delim, exclnodes=nodesToExclude) self.c.importSpatialInfo(spatialFile)
def __init__(self, allenSubj, assocMat, delim=",", spatialFile="atlas471_xyz_flip_xy.txt", nodesToExclude=[], symmetrise=False, mirror=False, convertMNI=False): """ This object contains two 'brain' network objects, one for the imaging data and one for the Allen data. Embedded functions make a comparison between the imaging and Allen data by pairing nodes between the two network objects. """ self.subj = allenSubj self.fName = "SampleAnnot.csv" self.maFile = "MicroarrayExpression.csv" self.probeFile = "Probes.csv" # if symmetrise is true then regions are identified by the structure name, # if symmetrise is false, then regions are identified by the structural acronym # these refer to headers in the SampleAnnot.csv file if symmetrise: self.sLab = "structure_acronym" else: self.sLab = "structure_name" self.mirror = mirror if symmetrise and self.mirror: print "Please select either a symmetrised or mirrored graph, ignoring mirror=True" self.mirror = False # set up brain for expression data self.a = mbo.brainObj() node_counter = 0 # import probe data f = open(path.join(self.subj, self.fName), "rb") reader = csv.DictReader(f, delimiter=",", quotechar='"') self.headers = ['probe'] self.sIDDict = {} ### deprecated code for removal #### # for l in reader: # n = int(l[self.sLab]) # self.a.G.add_node(n) # self.a.G.node[n] = l # self.headers.append(l["structure_id"]) # f.close() for l in reader: # n = int(l["structure_id"]) sID = l[self.sLab] n = node_counter # GIVE NODES UNIQUE INCREMENTAL ID (across all subjects) if not self.a.G.has_node(n): self.a.G.add_node(n) self.a.G.node[n] = l self.a.G.node[n][ 'sID'] = sID # store the structure_acronym/structure_name for the node node_counter += 1 # self.headers[subj].append(l["structure_id"]) self.headers.append( sID ) #STORE structure_acronym or structure_name depending on symmetrise if not sID in self.sIDDict.keys(): self.sIDDict[sID] = [n] else: self.sIDDict[sID].append(n) f.close() if convertMNI: # convert location data for Allen brain from MNI space for n in self.a.G.nodes(): x = 45 - (float(self.a.G.node[n]['mni_x']) / 2) y = 63 + (float(self.a.G.node[n]['mni_y']) / 2) z = 36 + (float(self.a.G.node[n]['mni_z']) / 2) self.a.G.node[n]['xyz'] = (x, y, z) else: for n in self.a.G.nodes(): x = float(self.a.G.node[n]['mni_x']) y = float(self.a.G.node[n]['mni_y']) z = float(self.a.G.node[n]['mni_z']) self.a.G.node[n]['xyz'] = (x, y, z) # copy hemisphere if required if self.mirror and len(self.a.G.nodes()) < 600: self.a.copyHemisphere() # set up brain with graph properties self.c = mbo.brainObj() self.c.importAdjFile(assocMat, delimiter=delim, exclnodes=nodesToExclude) self.c.importSpatialInfo(spatialFile)
def __init__(self, assocMat, nodesToExclude=[], delim=" ", subjList=None, spatialFile="parcel_500.txt", symmetrise=False, convertMNI=False, mirror=True): if subjList: self.subjList = subjList else: self.subjList = [v for v in glob("17823*") if path.isdir(v)] self.fName = "SampleAnnot.csv" self.maFile = "MicroarrayExpression.csv" self.probeFile = "Probes.csv" self.mirror=mirror # if symmetrise is true then regions are identified by the structure name, # if symmetrise is false, then regions are identified by the structural acronym # these refer to headers in the SampleAnnot.csv file if symmetrise: self.sLab = "structure_acronym" else: self.sLab = "structure_name" # set up brain for expression data self.a = mbo.brainObj() node_counter = 0 self.headers={} self.sIDDict = {} # dictionary storing the list of nodes for each structural ID by subject - for use later in averaging across all subjects for subj in self.subjList: self.sIDDict[subj] = {} # import probe data f = open(path.join(subj, self.fName), "rb") reader = csv.DictReader(f, delimiter=",", quotechar='"') self.headers[subj] = ['probe'] for l in reader: # n = int(l["structure_id"]) sID = l[self.sLab] n = node_counter # GIVE NODES UNIQUE INCREMENTAL ID (across all subjects) if not self.a.G.has_node(n): self.a.G.add_node(n) self.a.G.node[n] = l self.a.G.node[n]['sID'] = sID # store the structure_acronym/structure_name for the node node_counter += 1 # self.headers[subj].append(l["structure_id"]) self.headers[subj].append(sID) #STORE structure_acronym or structure_name depending on symmetrise if not sID in self.sIDDict[subj].keys(): self.sIDDict[subj][sID] = [n] else: self.sIDDict[subj][sID].append(n) f.close() if convertMNI: # convert location data for Allen brain from MNI space for n in self.a.G.nodes(): x = 45 - (float(self.a.G.node[n]['mni_x'])/2) y = 63 + (float(self.a.G.node[n]['mni_y'])/2) z = 36 + (float(self.a.G.node[n]['mni_z'])/2) self.a.G.node[n]['xyz'] = (x,y,z) else: for n in self.a.G.nodes(): x = float(self.a.G.node[n]['mni_x']) y = float(self.a.G.node[n]['mni_y']) z = float(self.a.G.node[n]['mni_z']) self.a.G.node[n]['xyz'] = (x,y,z) if self.mirror and len(self.a.G.nodes()) < 600: self.a.copyHemisphere() # f.write('%s, %s, %s, %s \n' % (str(n), str(self.a.G.node[n]['xyz'][0]),str(self.a.G.node[n]['xyz'][1]),str(self.a.G.node[n]['xyz'][2]))) #f.close() # set up brain with graph properties self.c = mbo.brainObj() self.c.importAdjFile(assocMat, delimiter=delim, exclnodes=nodesToExclude) self.c.importSpatialInfo(spatialFile)
def __init__(self, allenSubj, assocMat, delim=",", spatialFile="atlas471_xyz_flip_xy.txt", nodesToExclude=[], symmetrise=False, mirror=False, convertMNI=False): """ This object contains two 'brain' network objects, one for the imaging data and one for the Allen data. Embedded functions make a comparison between the imaging and Allen data by pairing nodes between the two network objects. """ self.subj = allenSubj self.fName = "SampleAnnot.csv" self.maFile = "MicroarrayExpression.csv" self.probeFile = "Probes.csv" # if symmetrise is true then regions are identified by the structure name, # if symmetrise is false, then regions are identified by the structural acronym # these refer to headers in the SampleAnnot.csv file if symmetrise: self.sLab = "structure_acronym" else: self.sLab = "structure_name" self.mirror = mirror if symmetrise and self.mirror: print "Please select either a symmetrised or mirrored graph, ignoring mirror=True" self.mirror = False # set up brain for expression data self.a = mbo.brainObj() node_counter = 0 # import probe data f = open(path.join(self.subj, self.fName), "rb") reader = csv.DictReader(f, delimiter=",", quotechar='"') self.headers = ['probe'] self.sIDDict = {} ### deprecated code for removal #### # for l in reader: # n = int(l[self.sLab]) # self.a.G.add_node(n) # self.a.G.node[n] = l # self.headers.append(l["structure_id"]) # f.close() for l in reader: # n = int(l["structure_id"]) sID = l[self.sLab] n = node_counter # GIVE NODES UNIQUE INCREMENTAL ID (across all subjects) if not self.a.G.has_node(n): self.a.G.add_node(n) self.a.G.node[n] = l self.a.G.node[n]['sID'] = sID # store the structure_acronym/structure_name for the node node_counter += 1 # self.headers[subj].append(l["structure_id"]) self.headers.append(sID) #STORE structure_acronym or structure_name depending on symmetrise if not sID in self.sIDDict.keys(): self.sIDDict[sID] = [n] else: self.sIDDict[sID].append(n) f.close() if convertMNI: # convert location data for Allen brain from MNI space for n in self.a.G.nodes(): x = 45 - (float(self.a.G.node[n]['mni_x'])/2) y = 63 + (float(self.a.G.node[n]['mni_y'])/2) z = 36 + (float(self.a.G.node[n]['mni_z'])/2) self.a.G.node[n]['xyz'] = (x,y,z) else: for n in self.a.G.nodes(): x = float(self.a.G.node[n]['mni_x']) y = float(self.a.G.node[n]['mni_y']) z = float(self.a.G.node[n]['mni_z']) self.a.G.node[n]['xyz'] = (x,y,z) # copy hemisphere if required if self.mirror and len(self.a.G.nodes()) < 600: self.a.copyHemisphere() # set up brain with graph properties self.c = mbo.brainObj() self.c.importAdjFile(assocMat, delimiter=delim, exclnodes=nodesToExclude) self.c.importSpatialInfo(spatialFile)
""" from maybrain import brainObjs as mbt from datetime import datetime startTime = datetime.now() # record the starting time # define some variables parcelFile = "parcel_500.txt" # the file containing the spatial position of nodes in the brain degenName = "randomAttack" # name of the degenerative model dVal = "2" # this refers to the wavelet scale used to construct the association matrix thresholdtype = "local" # the type of thresholding to use am = "mean_500.txt" # the association matrix nodesToExclude = [28, 303, 355, 339, 131, 250, 491, 205, 423, 140, 434, 142, 235, 244, 493, 21, 26, 232, 76, 234, 422] # a list of nodes that are not covered in most/all subjects on the imaging a = mbt.brainObj() # create a brain object a.importAdjFile(am, exclnodes=nodesToExclude, delimiter=",") # import the association matrix a.localThresholding(edgePC=3) # apply local thresholding with 3 percent of all possible connections retained a.removeUnconnectedNodes() # remove any unconnected nodes a.importSpatialInfo(parcelFile) # import the spatial information ## total weights weights = [mbt.np.absolute(a.G.edge[v[0]][v[1]]['weight']) for v in a.G.edges() ] # list all the weights in the graph T_start = mbt.np.sum(weights) # starting sum of all graph weights print T_start # record the starting time wtLossPC = 5. # five percent loss of connections at each iteration wtLoss = T_start * (wtLossPC/100) # calculate 5% of the connections for n in range(3,4): print "doing degenerative process"