def __init__(self, target, target_label, target_mids, target_surface): # Load input data, and initialie data attribtues self.ID = target # we load the surface as a source of control # we need to determine if our label file is the same size as the # surface or if it needs to be adjusted verts = ld.loadGii(target_surface, 0) # get correct number of vertices self.N = verts.shape[0] self.label = ld.loadGii(target_label, 0) self.mids = ld.loadMat(target_mids) - 1 if self.N != len(self.label): print('Warning: Target surface has more vertices that '\ 'label file. Adjusting label file.') self.label = ld.fixLabelSize(self.mids, self.label, self.N) # Initialize library attribtues # vertCounts contains the labels a vertex-of-interest maps to self.vertCounts = {} self.vertCounts = self.vertCounts.fromkeys(list(np.arange(self.N))) # labCounts contains the labels a label-of-interest maps to self.labCounts = {} self.labCounts = self.labCounts.fromkeys(list(self.label)) self.matchedSubjects = set([])
def labelErrorDistances(surfaceAdjFile,trueFile,midlineFile,predictedFile,maxValue): """ Compute how far away the misclassified label is from the predicted label. Distance is based on the geodesic distance map of labels on the cortical map. Parameters: - - - - - surfaceAdj : surface adjacency file of vertices trueFile : true cortical parcellation map file midlineFile : midline index file predictedFile : predicted cortical parcellation map file """ reIndexed = np.zeros((maxValue+1,maxValue+1)) pred = ld.loadGii(predictedFile,0).astype(np.int32) true = ld.loadGii(trueFile,0).astype(np.int32) errors = np.where(true != pred)[0] print len(errors) L = aj.LabelAdjacency(trueFile,surfaceAdjFile,midlineFile) L.generate_adjList() labelAdj = L.adj_list # create graph of label adjacency GL = nx.from_dict_of_lists(labelAdj) # get matrix of pairwise shortest paths between all labels apsp = nx.floyd_warshall_numpy(GL) res_apsp = np.asarray(np.reshape(apsp,np.product(apsp.shape)).T) unraveled = np.unravel_index(np.arange(len(res_apsp)),apsp.shape) newInds = (np.asarray(GL.nodes())[unraveled[0]], np.asarray(GL.nodes())[unraveled[1]]) nans = list(set(np.arange(maxValue+1)).difference(set(GL.nodes()))) reIndexed[newInds] = apsp[unraveled] reIndexed[nans,:] = np.nan reIndexed[:,nans] = np.nan errorDistance = reIndexed[true[errors],pred[errors]] return errorDistance
def populationRegionSize(subjectList, labelDirectory, extension): """ Method to compute the sizes of each region across the training set. Parameters: - - - - - subjectList : (list,.txt) list of subjects to include labelDirectory : directory containing the label files extension : label file extension """ if isinstance(subjectList, str): with open(subjectList, 'r') as inFile: subjects = inFile.readlines() subjects = [x.strip() for x in subjects] elif isinstance(subjectList, list): subjects = subjectList else: print 'Incorrect subject list type.' labelSizes = {k: [] for k in np.arange(1, 181)} for subj in subjects: inLabel = labelDirectory + str(subj) + extension if os.path.isfile(inLabel): pred = ld.loadGii(inLabel) for k in labelSizes.keys(): if k in set(pred): labelSizes[k].append(len(np.where(pred == k)[0])) return labelSizes
def computeLabelLayers(labelFile, surfaceAdjacency, borderFile): """ Method to find level structures of vertices, where each structure is a set of vertices that are a distance k away from the border vertices. """ label = ld.loadGii(labelFile, 0) surfAdj = ld.loadPick(surfaceAdjacency) borders = ld.loadPick(borderFile) # get set of non-zero labels in label file L = set(label) - set([0]) layers = {}.fromkeys(L) """ fullList = Parallel(n_jobs=NUM_CORES)(delayed(labelLayers)(lab, np.where(label == lab)[0], surfAdj,borders[lab]) for lab in L) """ for i, labelValue in enumerate(L): if borders.has_key(labelValue): inds = np.where(label == labelValue)[0] bm = borders[labelValue] layers[labelValue] = labelLayers(labelValue, inds, surfAdj, bm) return layers
def __init__(self, source, source_label, source_mids, source_surface): self.ID = source verts = ld.loadGii(source_surface, 0) self.N = verts.shape[0] self.label = ld.loadGii(source_label, 0) self.mids = ld.loadMat(source_mids) - 1 if self.N != len(self.label): print('Warning: Surface has more vertices that '\ 'label file. Adjusting label file.') self.label = ld.fixLabelSize(self.mids, self.label, self.N) self.vertLib = {}
def processLabelResizing(subjectList, dataDir, hemi): """ """ with open(subjectList, 'r') as inFile: subjects = inFile.readlines() subjects = [x.strip() for x in subjects] hcpDir = dataDir + 'Haynor/Connectome_4_With_MMP/Labels/' midDir = dataDir + 'parcellearning/Data/Midlines/' funDir = dataDir + 'HCP/Connectome_4/' outDir = dataDir + 'parcellearning/Data/Labels/HCP/' lExt = '.' + hemi + '.CorticalAreas_dil_NoTask_Final_Individual.32k_fs_LR.dlabel.nii' fExt = '.' + hemi + '.MyelinMap.32k_fs_LR.func.gii' mExt = '.' + hemi + '.Midline_Indices.mat' N = 32492 inCMAP = dataDir + 'parcellearning/Data/Labels/' + 'Label_Lookup_300.txt' for s in subjects: inLabel = hcpDir + s + lExt inMid = midDir + s + mExt inFunc = funDir + s + '/Surface/MNI/' + s + fExt if os.path.isfile(inLabel) and os.path.isfile( inMid) and os.path.isfile(inFunc): outFunc = outDir + s + '.' + hemi + '.CorticalAreas.fixed.32k_fs_LR.func.gii' outLabel = outDir + s + '.' + hemi + '.CorticalAreas.fixed.32k_fs_LR.label.gii' label = ld.loadGii(inLabel) mids = ld.loadMat(inMid) fixed = ld.fixLabelSize(mids, label, N) M = nb.load(inFunc) M.darrays[0].data = np.asarray(fixed.astype(np.float32)) nb.save(M, outFunc) cmd = '/usr/bin/wb_command -metric-label-import {} {} {}'.format( outFunc, inCMAP, outLabel) os.system(cmd)
labelFile = ''.join([lbd, subj, le]) surfAdj = ''.join([sj, subj, se]) outFile = ''.join([od, subj, oe]) print labelFile print surfAdj print outFile if os.path.exists(labelFile) and os.path.exists(surfAdj): print 'Label file and surface adjacency list exist.' if not os.path.exists(outFile): print labelFile.split('/')[-1] print surfAdj.split('/')[-1] print outFile.split('/')[-1] print 'Computing topological matrix.\n' label = ld.loadGii(labelFile, darray=0) with open(surfAdj, 'r') as inJ: adj = json.load(inJ) adj = {int(k): map(int, adj[k]) for k in adj.keys()} # Compute the topology structure of a given label map # topMat is a row-normalized data matrix _, topMat = tv.labelCounts(label, values, adj) tm = {'topology': topMat} sio.savemat(outFile, tm)
def neighborhoodErrorMap(labVal, labelAdjacency, truthLabFile, predLabFile, labelLookup, outputColorMap): """ Method to visualize the results of a prediction map, focusing in on a spefic core label. Parameters: - - - - - core : region of interest labelAdjacency : label adjacency list truthLabFile : ground truth label file predLabFile : predicted label file labelLookup : label color lookup table outputColorMap : new color map for label files """ # load files labAdj = ld.loadPick(labelAdjacency) truth = ld.loadGii(truthLabFile, 0) pred = ld.loadGii(predLabFile, 0) # extract current colors from colormap parsedColors = parseColorLookUpFile(labelLookup) # initialize new color map file color_file = open(outputColorMap, "w") trueColors = ' '.join(map(str, [255, 255, 255])) trueName = 'Label {}'.format(labVal) trueRGBA = '{} {} {}\n'.format(labVal, trueColors, 255) trueStr = '\n'.join([trueName, trueRGBA]) color_file.writelines(trueStr) # get labels that neighbor core neighbors = labAdj[labVal] # get indices of core label in true map truthInds = np.where(truth == labVal)[0] # initialize new map visualizeMap = np.zeros((truth.shape)) visualizeMap[truthInds] = labVal # get predicted label values existing at truthInds predLabelsTruth = pred[truthInds] for n in neighbors: # get color code for label, adjust and write text to file oriName = 'Label {}'.format(n) oriCode = parsedColors[n] oriColors = ' '.join(map(str, oriCode)) oriRGBA = '{} {} {}\n'.format(n, oriColors, 255) oriStr = '\n'.join([oriName, oriRGBA]) color_file.writelines(oriStr) adjLabel = n + 180 adjName = 'Label {}'.format(adjLabel) adjColors = shiftColor(oriCode, mag=30) adjColors = ' '.join(map(str, adjColors)) adjRGBA = '{} {} {}\n'.format(adjLabel, adjColors, 255) adjStr = '\n'.join([adjName, adjRGBA]) color_file.writelines(adjStr) # find where true map == n and set this value n_inds = np.where(truth == n)[0] visualizeMap[n_inds] = n # find where prediction(core) == n, and set to adjusted value n_inds = np.where(predLabelsTruth == n)[0] visualizeMap[truthInds[n_inds]] = adjLabel color_file.close() return visualizeMap
def buildLibraries(self, source, source_label, source_mids, source_surf, matching): """ Updates the vertex and label libraries with matching results. Parameters: - - - - - source : source subject ID source_label : source label file matching : source-to-target matching file produed by DiffeoSpectralMatching (corr12 or corr21) """ # vert vertices of source surface verts = ld.loadGii(source_surf, 0) # get number of vertices in source surface sN = verts.shape[0] # load source label and midline vertices sLab = ld.loadGii(source_label, 0) sMids = ld.loadMat(source_mids) - 1 # check to make sure label file has same number of vertices as # surface if len(sLab) != sN: sLab = ld.fixLabelSize(sMids, sLab, sN) # load source-to-target matching s2t = np.squeeze(ld.loadMat(matching) - 1).astype(int) # fix matching # target vertices in target space, source vertices correct length # s2t is of length (sN-mids), with indices in that range # will become length sN with indices in correct range fixed = ld.fixMatching(s2t, sN, sMids, self.N, self.mids).astype(int) # check if source subject already added if source not in self.matchedSubjects: # check to make sure matching / source label file same length if len(fixed) == len(sLab): # add matched subject to list of seen self.matchedSubjects.add(source) for node in np.arange(len(fixed)): # get target vertex to which source vertex is mapped vertex = fixed[node] # get label of source vertex sl = sLab[node] # if target vertex and source label not midline if vertex != -1 and sl > 0: # get target vertex label tl = self.label[vertex] # check if current vertex already in vertexLibrary.keys if not self.vertCounts[vertex]: self.vertCounts[vertex] = {sl: 1} else: if sl not in self.vertCounts[vertex].keys(): self.vertCounts[vertex].update({sl: 1}) else: self.vertCounts[vertex][sl] += 1 if not self.labCounts[tl]: self.labCounts[tl] = {sl: 1} else: if sl not in self.labCounts[tl].keys(): self.labCounts[tl].update({sl: 1}) else: self.labCounts[tl][sl] += 1 else: print('Warning: Matching not the same length as '\ 'source label file.') else: print('Source has already been included in the libraries.')
featureFile = ''.join([featureDir, subj, featureExt]) labelFile = ''.join([labelDir, subj, labelExt]) if os.path.exists(featureFile) and os.path.exists(labelFile): print 'Processing %s' % subj features = h5py.File(featureFile, mode='r') features = h5py.File(featureFile, mode='r') dataDict = features[subj] dataArray = du.mergeValueArrays(dataDict, keys=featureKeys) features.close() label = ld.loadGii(labelFile, darray=np.arange(1)) regSim = hmg.regionalSimilarity(dataArray, label) regSize = {}.fromkeys(regSim.keys()) for k in regSize.keys(): indx = np.where(label == k)[0] regSize[k] = len(indx) df_hmg = df_hmg.append(regSim, ignore_index=True) df_size = df_size.append(regSize, ignore_index=True) if df_hmg.shape[0] != 0: df_hmg.to_csv(output) if df_size.shape[0] != 0: df_size.to_csv(''.join([f_path, '.size', f_ext]))
trueDir = args.trueDir trueExt = args.trueExt predDir = args.predDir predExt = args.predExt output = args.output with open(args.subjectList, 'r') as inSubj: subjects = inSubj.readlines() subjects = [x.strip() for x in subjects] dice = [] for subj in subjects: trueFile = ''.join([trueDir, subj, trueExt]) predFile = ''.join([predDir, subj, predExt]) if os.path.exists(trueFile) and os.path.exists(predFile): true = ld.loadGii(trueFile, 0) pred = ld.loadGii(predFile, 0) inds = np.where(true > 0)[0] d = hmg.diceLabel(true[inds], pred[inds]) dice.append(d) DF = pd.DataFrame(columns=['Dice']) DF['Dice'] = np.asarray(dice) DF.to_csv(output)
labDir = args.labelDir labExt = args.labelExt trueDir = args.trueDir trueExt = args.trueExt output = args.output df = pd.DataFrame(columns=['accuracy']) with open(args.subjectList, 'r') as inSubj: subjects = inSubj.readlines() subjects = [x.strip() for x in subjects] for subj in subjects: predLabel = ''.join([labDir, subj, labExt]) trueLabel = ''.join([trueDir, subj, trueExt]) if os.path.exists(predLabel) and os.path.exists(trueLabel): pred = ld.loadGii(predLabel, darray=np.arange(1)) true = ld.loadGii(trueLabel, darray=np.arange(1)) trueInds = np.where(true > 0)[0] accuracy = np.mean(pred[trueInds] == true[trueInds]) df = df.append({'accuracy': accuracy}, ignore_index=True) df.to_csv(output)