Ejemplo n.º 1
0
 def __init__(self, hwId, partId, astId, numASTs=500):
     octave = Octave.objects.get(homework_id=hwId, part_id, ast_id=astId)
     self.assn = Assignment(hwId, partId)
     self.astId = astId
     self.ast = OctaveAST(octave)
     self.statementListIds = self.ast.allNodes('STATEMENT_LIST')
     self.numASTs = numASTs
     self._runMatchings()
Ejemplo n.º 2
0
class Segmenter(object):
    def __init__(self, hwId, partId, astId, numASTs=500):
        octave = Octave.objects.get(homework_id=hwId, part_id, ast_id=astId)
        self.assn = Assignment(hwId, partId)
        self.astId = astId
        self.ast = OctaveAST(octave)
        self.statementListIds = self.ast.allNodes('STATEMENT_LIST')
        self.numASTs = numASTs
        self._runMatchings()

    def _runMatchings(self):
        self.totalMatch = {}
        self.consecutiveMatch = {}
        self.normalized = {}
        for i in range(self.numASTs):
            srcAST = self.ast
            targetAST = Octave.objects.get(homework_id = self.assn.getHomework(), \
                     part_id = self.assn.getPart(), \
                     ast_id = i)
            matcher = Matching.fromOctaveAST(self.assn, srcAST, targetAST)
            for stmtListId in self.statementListIds:
                srcStatementIds = self.ast.getChildIds(stmtListId)
                targetStatementIds = [
                    matcher.getDest(i) for i in srcStatementIds
                ]
                for currSrc, nextSrc, currTarget, nextTarget in zip(srcStatementIds[:-1], srcStatementIds[1:], \
                             targetStatementIds[:-1], targetStatementIds[1:]):
                    if currTarget != None and nextTarget != None:
                        self.totalMatch[(currSrc, nextSrc)] += 1
                        if targetAST.isConsecutiveSibling(
                                currTarget, nextTarget):
                            self.consecutiveMatch[(currSrc, nextSrc)] += 1
        for k in self.consecutiveMatch:
            self.normalized[k] = self.consecutiveMatch[k] / float(
                self.totalMatch[k])

    def topConsecutive(self, k=5):
        sortedPairs = sorted(self.consecutiveMatch.iteritems(),
                             key=itemgetter(1),
                             reverse=True)
        topPairs = [pair for (pair, val) in sortedPairs[:k]]
        return topPairs

    def topNormalizedConsecutive(self, k=5):
        sortedPairs = sorted(self.normalized.iteritems(),
                             key=itemgetter(1),
                             reverse=True)
        topPairs = [pair for (pair, val) in sortedPairs[:k]]
        return topPairs
Ejemplo n.º 3
0
    def __init__(self):
        assignmentList = [Assignment(HOMEWORK_ID, PART_ID)]

        self.assnExemplars = {}
        for assn in assignmentList:
            feedbackDict = self._getFeedbackDict(assn)
            self.assnExemplars[assn] = feedbackDict
Ejemplo n.º 4
0
 def getAllParts(self):
     return [Assignment(1, 1)]
     return [Assignment(1,1),\
             Assignment(1,2),\
             Assignment(1,3),\
             Assignment(1,4),\
             Assignment(1,5),\
             Assignment(1,6),\
             Assignment(1,7)]
Ejemplo n.º 5
0
    def run(self):
        self.initializeLog()
        for (h, p) in self.getAllParts():
            assn = Assignment(h, p)
            logging.info('PrecomputeNN (hw,part): ' + str(assn))
            corrects = self.getASTs(assn, 'corrects')
            incorrects = self.getASTs(assn, 'incorrects')
            distanceMatrix = FileSystem.loadDistanceMatrix(
                assn.getTuple(), False)
            subIdMap = FileSystem.loadSubmissionIdMap(assn.getTuple())
            astNetwork = AstNetwork(assn.getTuple(), distanceMatrix, subIdMap)
            NNmap = self.getNN(corrects, incorrects, astNetwork)

            outputDir = os.path.join(FileSystem.getDataDir(),
                                     'nearestNeighbors')
            if not os.path.exists(outputDir):
                os.makedirs(outputDir)
            outputPath = os.path.join(outputDir, 'NNmap_' + str(assn) + '.txt')
            self.writeNN(outputPath, NNmap)
    def run(self):
        dirname = 'DumpNumSubmissions'
        FileSystem.initializeLogging(dirname)

        outputDir = os.path.join(FileSystem.getDataDir(), dirname)
        if not os.path.exists(outputDir):
            os.makedirs(outputDir)

        for (h, p) in MLClass.allProblems():
            assn = Assignment(h, p)
            path = os.path.join(outputDir,\
                    'NumSubmissions_' + str(assn) + '.txt')
            #self.loadOutputs(assn, astOutputPath, mapOutputPath)
            self.loadNumSubmissions(assn, path)
Ejemplo n.º 7
0
    print('Result: ' + resultstr[sourceResult])
    print('------------------------')
    Printer.mask(M.source, diffSourceMap)
    print('\nProblem: ' + str(assn) + ', AST #' + str(targetId))
    print('Result: ' + resultstr[targetResult])
    print('------------------------')
    Printer.mask(M.target, diffTargetMap)


if __name__ == '__main__':
    try:
        hwId = int(sys.argv[1])
        partId = int(sys.argv[2])
        sourceId = int(sys.argv[3])
        if len(sys.argv) > 4:
            targetId = int(sys.argv[4])
        else:
            print('asdf')
            NNmap = FileSystem.loadNearestNeighbors((hwId, partId))
            targetId = NNmap[sourceId][0]
    except:
        print('Usage: python compareCode.py hwId partId sourceId [targetId]')
        sys.exit(1)

    run(Assignment(hwId, partId), sourceId, targetId)

#hwId = 3
#partId = 3
#sourceId = 0
#targetId = 7221
Ejemplo n.º 8
0
        astFile = open(path)
        for line in astFile.readlines():
            astList.append(int(line))
        return Set(astList)

    def run(self, assn, threshold):
        logDir = os.path.join(FileSystem.getLogDir(), 'MakeGraph')
        if not os.path.exists(logDir):
            os.makedirs(logDir)
        logFileName = os.path.join(logDir, 'log')
        logging.basicConfig(filename = logFileName, format = '%(asctime)s %(message)s', \
                                datefmt = '%m/%d/%Y %I:%M:%S %p', level = logging.INFO)

        labels = ['incorrects', 'corrects']
        for label in labels:
            asts = self.getAsts(assn, label)
            graph = self.getGraph(asts, assn, threshold, label)
            outPath = self.getOutputFilePath(assn, threshold, label)
            logging.info('write graph: ' + outPath)
            graph.save(outPath)
            logging.info('done.')
        #nx.write_gml(graph, outPath)


if __name__ == "__main__":
    homework = 1
    numParts = 7
    for part in range(1, numParts + 1):
        assn = Assignment(homework, part)
        MakeGraph().run(assn, THRESHOLD)
Ejemplo n.º 9
0
                s += self.endDiv(3)
                for subtree in subtrees:
                    # subtree['info'], subtree['code']
                    s += self.startDiv(
                        3, 'class_' + str(level) + '_' + str(classId), '')
                    s += self.renderCode(subtree['code'])
                    s += self.endDiv(3)
                s += self.endDiv(2)
            s += self.endDiv(1)
        hierarchy = s
        script = self.scriptSegment('.equivClass', 'this')
        for classId in classIdList:
            script += self.scriptSegment('.showClass' + str(classId), \
                        '"#class_' + str(classId) + '"')
        return (hierarchy, script)

    def run(self):
        self.readTemplate()
        self.loadAllClasses()
        for level in self.levels:
            print('Level ' + str(level))
            for classId in self.classes[level]:
                (hierarchy, script) = self.genCode(level, classId)
                self.writeToFile(level, classId, hierarchy, script)


if __name__ == '__main__':
    assn = Assignment(1, 3)
    CV = CreateVis(assn)
    CV.run()
Ejemplo n.º 10
0
                                    part_id = assn.getPart(), \
                                    ast_id = astId)[0]
        print('-------------------------------------------------')
        print(octave.code)
        print('-------------------------------------------------')
        print('AST Id: ' + str(astId))
        print('Number of submissions: ' + str(octave.numSubmissions()))

    def run(self, assn, clusterId, label):
        clusterPath = self.GetClusterPath(assn, clusterId, label)
        astIds = self.loadClusterFile(clusterPath)
        for astId in astIds:
            os.system('clear')
            self.printAST(astId, assn)
            raw_input('Press key.')


if __name__ == '__main__':
    if len(sys.argv) == 5:
        label = sys.argv[1]  ## this is `corrects' or 'incorrects'
        hwId = int(sys.argv[2])
        partId = int(sys.argv[3])
        clusterId = int(sys.argv[4])
    else:
        print(
            'Usage: python ViewClusters.py [label] [hwId] [partId] [clusterId]'
        )
        sys.exit(1)
    assn = Assignment(hwId, partId)
    ClusterViewer().run(assn, clusterId, label)