def generateExamples(dataset, predClasses): '''Given the entire dataset and a predicted label, print TOP_EG number of example pairs.''' # Init labels = [dataset.dict_Index_Cluster[i] for i in predClasses] prettyLabels = [i.replace('\n', ' ') for i in labels] examplesList = [] print 'Top-{} Predicted Class-IDs:\n{}'.format(TOP_K, predClasses) print 'TOP-{} Predicted Class-Labels (Error-IDs and Unique Repairs):\n{}\nExamples:'.format(TOP_K, prettyLabels) # For each label numC = 0 for label in labels: dataSamples = dataset.dict_ClusterRaw_DataSamples[label] # Fetch all training examples srcTrgtPairs = [(H.joinList(i.src, ''), H.joinList(i.trgt, '')) for i in dataSamples] # Create list of (before, after) topEgs = H.getTop_K(srcTrgtPairs, TOP_EG) # Sort them based on frequency and fetch Top-EG #examples for i in range(len(topEgs)): # For each example eg = topEgs[i] before, after = eg[0].replace('\n', ''), eg[1].replace('\n', '') # Remove new-line characters if before.strip() == '': before = '// Empty Line' # Deal with empty lines if after.strip() == '': after = '// Empty Line' # Deal with empty lines print 'Eg #{} class-{} before: {}'.format(i+1, predClasses[numC], before) # Print them print 'Eg #{} class-{} after : {}'.format(i+1, predClasses[numC], after) examplesList.append(eg) numC += 1 # Extend to the examplesList, proportionately return examplesList
def getBuggyAbsLine(codeText): '''Given codeText, return the buggy abstract lines (abstraction of erroneous lines) and their line numbers''' codeObj = Code(codeText) absLines = getProgAbstraction(codeObj) errs = codeObj.getSevereErrors() absLinesBuggy, lineNums = [], [] if len(errs) > 0: for err in errs: lineNum = err.line # Pick the first error line Num if lineNum > 0 and lineNum <= len(absLines): # If line-num reported by compiler doesn't exceeds #absLines (and is >=1) absLine = H.joinList(absLines[lineNum - 1], ' ') if lineNum not in lineNums: # Add unique lineNum / absLine absLinesBuggy.append(absLine) lineNums.append(lineNum) return absLinesBuggy, lineNums
def printProgAbstraction(fnamePath): codeText = open(fnamePath).read() codeObj = Code(codeText) absLines = getProgAbstraction(codeObj) for line in absLines: print H.joinList(line, ' ')
def calcConfMat(self): print colored('\n\tConfusion Matrix: ...', 'magenta') predAtK = [] for topK in [1, 3, 5]: countM, countN = 0, 0 predClasses_Tests = self.deepModel.getPrediction(topK) for actClasses_bin, predClasses_bin in zip(self.y_test, predClasses_Tests): if self.multiClass: # Add +1 to index since off-by-one with dict_index2class actClasses_indices = [ index + 1 for index in range(len(actClasses_bin)) if actClasses_bin[index] == 1 ] predClasses_indices = [ index + 1 for index in range(len(predClasses_bin)) if predClasses_bin[index] == 1 ] else: actClasses_indices, predClasses_indices = [ np.argmax(actClasses_bin) ], predClasses_bin actClasses = [ self.dict_index2class[index] for index in actClasses_indices ] predClasses = [ self.dict_index2class[index] for index in predClasses_indices ] for actClass in actClasses: if actClass in predClasses: # True-Positive countM += 1 if topK == 1: # Conf Matrix only for Pred@1 self.confMatrix[actClass].truePos += 1 self.confMatrix[actClass].updatePred(actClass) else: # False-Negative: Not predicted at all countN += 1 if topK == 1: # Conf Matrix only for Pred@1 self.confMatrix[actClass].falseNeg += 1 for predClass in predClasses: # Add all confusion labels self.confMatrix[actClass].updatePred(predClass) if topK == 1: # Conf Matrix only for Pred@1 for predClass in predClasses: if predClass not in actClasses: # False-Positive: Predicted, but falsely self.confMatrix[predClass].falsePos += 1 if topK == 1: # Conf Matrix only for Pred@1 sortedConfMat = [ self.confMatrix[i] for i in self.getSortedConfMat()[0] ] strConf = H.joinList(sortedConfMat) self.writeConfMat(sortedConfMat) prec_at_k = round(100 * float(countM) / (countM + countN), 2) predAtK.append(prec_at_k) print 'Pred@{}= {}'.format(topK, prec_at_k) return strConf, predAtK
def __str__(self): return H.joinList(self.abstractTokens, ' ')