Beispiel #1
0
 def findNearestCorrects(self, assn, asts, numASTs, label):
     nn = {}
     if label == 'corrects':
         for ast in asts:
             nn[ast] = ast
         return nn
     corrects = self.getAsts(assn, 'corrects')
     sources = asts[:numASTs]
     distanceMatrix = FileSystem.loadDistanceMatrix(assn.getTuple(), False)
     subIdMap = FileSystem.loadSubmissionIdMap(assn.getTuple())
     astNetwork = AstNetwork(assn.getTuple, distanceMatrix, subIdMap)
     D = astNetwork.getDistanceList(sources, corrects)
     for s in D:
         nn[s] = D[s][np.argmin([y for (x, y) in D[s]])][0]
     return nn
Beispiel #2
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)
Beispiel #3
0
    def getGraph(self, astSet, assn, threshold, label):
        part = assn.getTuple()
        #filteredGraph = nx.Graph()
        filteredGraph = igraph.Graph()

        distanceMatrix = FileSystem.loadDistanceMatrix(part, False)
        subIdMap = FileSystem.loadSubmissionIdMap(part)
        lookup = {}
        for key, idx in zip(subIdMap, range(len(subIdMap))):
            if int(key) in astSet:
                numStudents = len(subIdMap[key])
                #filteredGraph.add_node(key, {'weight': numStudents})
                filteredGraph.add_vertex(label=key, weight=numStudents)
                lookup[key] = filteredGraph.vs.find(label=int(key))
        row = 0
        toAdd = {}
        while True:
            logging.info('assn: ' + str(assn) + ', ' + label + ', row: ' +
                         str(row))
            line = distanceMatrix.readline()
            if not line: break
            if not row in astSet:
                row += 1
                continue

            rowValues = map(int, line.strip().split())
            for col in range(row + 1, len(rowValues)):
                if not col in astSet:
                    continue
                value = rowValues[col]
                if value >= 0 and value <= threshold:
                    toAdd[(lookup[row], lookup[col])] = value
                    #filteredGraph.add_edge(row, col, {'edits': value})
            row += 1
        logging.info('Oh... one more thing.')
        filteredGraph.add_edges(toAdd.keys())
        filteredGraph.es['edits'] = toAdd.values()
        return filteredGraph
 def getDistanceMatrix(self, part):
     sparse = part != (1, 1)
     return FileSystem.loadDistanceMatrix(part, False)