Ejemplo n.º 1
0
    def makePrediction(self, pfisGraph, navPath, navNumber):
        if navNumber < 1 or navNumber >= navPath.getLength():
            raise RuntimeError(
                'makePrediction: navNumber must be > 0 and less than the length of navPath'
            )

        methods = self.__getOrderedRecentMethods(pfisGraph, navPath, navNumber)
        navToPredict = navPath.getNavigation(navNumber)

        if not navToPredict.isToUnknown():
            # methodToPredict is the method we want to predict
            methodToPredict = navToPredict.toFileNav.methodFqn
            fromMethodFqn = navToPredict.fromFileNav.methodFqn

            topPrediction = []
            if self.includeTop:
                topPrediction = [methods[0]]

            methodToPredictEquiv = pfisGraph.getFqnOfEquivalentNode(
                methodToPredict)
            if methodToPredictEquiv in methods:
                rank = methods.index(methodToPredictEquiv)
                return Prediction(navNumber, rank, len(methods), 0,
                                  fromMethodFqn, methodToPredict,
                                  navToPredict.toFileNav.timestamp,
                                  topPrediction)

        return Prediction(navNumber, 999999, len(methods), 0,
                          str(navToPredict.fromFileNav),
                          str(navToPredict.toFileNav),
                          navToPredict.toFileNav.timestamp)
Ejemplo n.º 2
0
    def makePrediction(self, pfisGraph, navPath, navNumber):
        if navNumber < 1 or navNumber >= navPath.getLength():
            raise RuntimeError(
                'makePrediction: navNumber must be > 0 and less than the length of navPath'
            )

        navToPredict = navPath.getNavigation(navNumber)
        sortedMethods = []

        if not navToPredict.isToUnknown():
            startTimestamp = 0
            endTimestamp = navToPredict.toFileNav.timestamp
            fromMethodFqn = navToPredict.fromFileNav.methodFqn
            methodToPredict = navToPredict.toFileNav.methodFqn

            # The reason we do the query from one extra backwards is because of
            # the duplication nonsense in PFIG
            # TODO: David to double check if this approach is feasible, do not
            # delete the below yet
            #             if navPath.navigations[navNumber - 1].fromFileNav is not None:
            #                 startTimestamp = navPath.navigations[navNumber - 1].fromFileNav.timestamp

            self.addDocumentsToCorpus(startTimestamp, endTimestamp, pfisGraph)
            model = self.getModel()
            sorted_sims = self.getSortedSimilarityMatrix(model, fromMethodFqn)

            mapMethodsToScore = {}

            for i in range(0, len(sorted_sims)):
                _, score = sorted_sims[i]
                sortedMethods.append(self.corpus.methodFqns[i])
                mapMethodsToScore[self.corpus.methodFqns[i]] = score

            value = mapMethodsToScore[methodToPredict]
            firstIndex = self.getFirstIndex(sortedMethods, mapMethodsToScore,
                                            value)
            lastIndex = self.getLastIndex(sortedMethods, mapMethodsToScore,
                                          value)
            numTies = lastIndex - firstIndex + 1
            rankWithTies = self.getRankConsideringTies(firstIndex + 1, numTies)

            topPredictions = []
            if self.includeTop:
                topPredictions = self.getTopPredictions(
                    sortedMethods, mapMethodsToScore)

            return Prediction(navNumber, rankWithTies, len(sortedMethods),
                              numTies, str(navToPredict.fromFileNav),
                              str(navToPredict.toFileNav),
                              navToPredict.toFileNav.timestamp, topPredictions)

        return Prediction(navNumber, 999999, len(sortedMethods), 0,
                          str(navToPredict.fromFileNav),
                          str(navToPredict.toFileNav),
                          navToPredict.toFileNav.timestamp)
Ejemplo n.º 3
0
 def initialise_stats(self):
     if self.has_scrapper_links and self.has_valid_predictions:
         self.predictions = Prediction().initialise_prediction(
         ).get_all_prediction()
         self.driver_standings = Scrapper().initialise_links(
         ).scrape_driver()
         self.team_standings = Scrapper().initialise_links(
         ).scrape_constructor()
         return self
     else:
         print("Links and predictions not initialised properly")
         return self
Ejemplo n.º 4
0
    def makePrediction(self, pfisGraph, navPath, navNumber):
        if navNumber < 1 or navNumber >= navPath.getLength():
            raise RuntimeError(
                'makePrediction: navNumber must be > 0 and less than the length of navPath'
            )

        self.__getOrderedFrequentMethods(pfisGraph, navPath, navNumber)
        navToPredict = navPath.getNavigation(navNumber)

        if not navToPredict.isToUnknown():
            # methodToPredict is the method we want to predict
            methodToPredict = navToPredict.toFileNav.methodFqn
            fromMethodFqn = navToPredict.fromFileNav.methodFqn
            methodToPredictEquivalentNode = pfisGraph.getFqnOfEquivalentNode(
                methodToPredict)

            if methodToPredictEquivalentNode in self.__methodFrequencies.keys(
            ):
                result = self.__methodFrequencies[
                    methodToPredictEquivalentNode]
                sortedRanks = sorted(
                    self.__methodFrequencies,
                    key=lambda freq: self.__methodFrequencies[freq])
                firstIndex = self.getFirstIndex(sortedRanks,
                                                self.__methodFrequencies,
                                                result)
                lastIndex = self.getLastIndex(sortedRanks,
                                              self.__methodFrequencies, result)
                numTies = lastIndex - firstIndex + 1
                rankWithTies = self.getRankConsideringTies(
                    firstIndex + 1, numTies)
                topPredictions = []

                if self.includeTop:
                    topPredictions = self.getTopPredictions(
                        sortedRanks, self.__methodFrequencies)

                return Prediction(navNumber, rankWithTies,
                                  len(self.__methodFrequencies), 0,
                                  fromMethodFqn, methodToPredict,
                                  navToPredict.toFileNav.timestamp,
                                  topPredictions)

        return Prediction(navNumber, 999999, len(self.__methodFrequencies), 0,
                          str(navToPredict.fromFileNav),
                          str(navToPredict.toFileNav),
                          navToPredict.toFileNav.timestamp)
Ejemplo n.º 5
0
    def makePrediction(self, pfisGraph, navPath, navNumber):
        if navNumber < 1 or navNumber >= navPath.getLength():
            raise RuntimeError(
                'makePrediction: navNumber must be > 0 and less than the length of navPath'
            )

        navToPredict = navPath.getNavigation(navNumber)
        sortedMethods = []

        if not navToPredict.isToUnknown():
            fromMethodFqn = navToPredict.fromFileNav.methodFqn
            methodToPredict = navToPredict.toFileNav.methodFqn

            self.initialize(fromMethodFqn, navNumber, navPath, pfisGraph)
            self.spreadActivation(pfisGraph)

            if self.mapNodesToActivation == None:
                print "Map was empty!!!!!!!!"
                print self.name

            sortedMethods = self.__getMethodNodesFromGraph(
                pfisGraph, pfisGraph.getFqnOfEquivalentNode(fromMethodFqn))
            topPredictions = []

            if self.includeTop:
                topPredictions = self.getTopPredictions(
                    sortedMethods, self.mapNodesToActivation)

            equivalentMethod = pfisGraph.getFqnOfEquivalentNode(
                methodToPredict)
            if equivalentMethod in sortedMethods:
                ranking = self.getRankForMethod(equivalentMethod,
                                                sortedMethods,
                                                self.mapNodesToActivation)

                return Prediction(navNumber, ranking["rankWithTies"],
                                  len(sortedMethods), ranking["numTies"],
                                  str(navToPredict.fromFileNav),
                                  str(navToPredict.toFileNav),
                                  navToPredict.toFileNav.timestamp,
                                  topPredictions)

        return Prediction(navNumber, 999999, len(sortedMethods), 0,
                          str(navToPredict.fromFileNav),
                          str(navToPredict.toFileNav),
                          navToPredict.toFileNav.timestamp)
Ejemplo n.º 6
0
    def makePrediction(self, pfisGraph, navPath, navNumber):
        if navNumber < 1 or navNumber >= navPath.getLength():
            raise RuntimeError(
                'makePrediction: navNumber must be > 0 and less than the length of navPath'
            )

        navToPredict = navPath.getNavigation(navNumber)
        fromMethodFqn = navToPredict.fromFileNav.methodFqn
        methodToPredict = navToPredict.toFileNav.methodFqn
        self.nodeDistances = {}
        sortedRanksMethodsOnly = []

        if not navToPredict.isToUnknown() and pfisGraph.containsNode(
                methodToPredict):
            result = self.__breadthFirstSearch(pfisGraph, fromMethodFqn,
                                               methodToPredict)
            if result > 0:
                sortedRanks = sorted(self.nodeDistances,
                                     key=lambda node: self.nodeDistances[node])
                sortedRanksMethodsOnly = self.getRanksForMethodsOnly(
                    sortedRanks, pfisGraph)

                firstIndex = self.getFirstIndex(sortedRanksMethodsOnly,
                                                self.nodeDistances, result)
                lastIndex = self.getLastIndex(sortedRanksMethodsOnly,
                                              self.nodeDistances, result)
                numTies = lastIndex - firstIndex + 1
                rankWithTies = self.getRankConsideringTies(
                    firstIndex + 1, numTies)
                topPredictions = []

                if self.includeTop:
                    topPredictions = self.getTopPredictions(
                        sortedRanksMethodsOnly, self.nodeDistances)

                return Prediction(navNumber, rankWithTies,
                                  len(sortedRanksMethodsOnly), numTies,
                                  fromMethodFqn, methodToPredict,
                                  navToPredict.toFileNav.timestamp,
                                  topPredictions)

        return Prediction(navNumber, 999999, len(sortedRanksMethodsOnly), 0,
                          str(navToPredict.fromFileNav),
                          str(navToPredict.toFileNav),
                          navToPredict.toFileNav.timestamp)
Ejemplo n.º 7
0
class Statistics:
    def __init__(self):
        self.has_scrapper_links = False
        self.has_valid_predictions = False
        self.driver_standings = []
        self.team_standings = []
        self.predictions = {}

    def check_stats(self):
        try:
            open("links.txt")
            open("predictions.txt")
            self.has_scrapper_links = True
            self.has_valid_predictions = True
        except FileNotFoundError:
            print(
                "Statistics cannot be initialised, predictions and links not properly configured"
            )
        return self

    def initialise_stats(self):
        if self.has_scrapper_links and self.has_valid_predictions:
            self.predictions = Prediction().initialise_prediction(
            ).get_all_prediction()
            self.driver_standings = Scrapper().initialise_links(
            ).scrape_driver()
            self.team_standings = Scrapper().initialise_links(
            ).scrape_constructor()
            return self
        else:
            print("Links and predictions not initialised properly")
            return self

    def count_driver_zeros(self):
        counter = 0
        for info in self.driver_standings:
            formatted_info = info.split(" ")
            if formatted_info[3] == "0":
                counter += 1
        return counter

    def min_driver_zero(self):
        counter = 1
        for info in self.driver_standings:
            formatted_info = info.split(" ")
            if formatted_info[3] == "0":
                break
            counter += 1
        return counter

    def count_team_zeros(self):
        counter = 0
        for info in self.team_standings:
            formatted_info = info.split(" ")
            if formatted_info[-1] == "0":
                counter += 1
        return counter

    def min_team_zero(self):
        counter = 1
        for info in self.team_standings:
            formatted_info = info.split(" ")
            if formatted_info[-1] == "0":
                break
            counter += 1
        return counter

    def calculate_deviation(self, name, who):
        if who == "driver":
            driver_prediction = self.predictions[name]["drivers"]
            inaccuracy_score = 0
            current_predicted_position = 1
            current_actual_position = 1
            for cd in driver_prediction:
                for ranking in self.driver_standings:
                    if cd.lower() in ranking.lower():
                        if ranking.split(" ")[3] == "0":
                            if current_predicted_position < self.min_driver_zero(
                            ):
                                score = abs(self.min_driver_zero() -
                                            current_predicted_position)
                                inaccuracy_score += score
                                current_actual_position = 1
                            else:
                                inaccuracy_score += 0
                                current_actual_position = 1
                        else:
                            score = abs(current_actual_position -
                                        current_predicted_position)
                            inaccuracy_score += score
                            current_actual_position = 1
                        break
                    current_actual_position += 1
                current_predicted_position += 1
            return inaccuracy_score
        elif who == "team":
            driver_prediction = self.predictions[name]["teams"]
            inaccuracy_score = 0
            current_predicted_position = 1
            current_actual_position = 1
            for cd in driver_prediction:
                for ranking in self.team_standings:
                    if cd.lower() in ranking.lower():
                        if ranking.split(" ")[2] == "0":
                            if current_predicted_position < self.min_team_zero(
                            ):
                                score = abs(self.min_team_zero() -
                                            current_predicted_position)
                                inaccuracy_score += score
                                current_actual_position = 1
                            else:
                                inaccuracy_score += 0
                                current_actual_position = 1
                        else:
                            score = abs(current_actual_position -
                                        current_predicted_position)
                            inaccuracy_score += score
                            current_actual_position = 1
                        break
                    current_actual_position += 1
                current_predicted_position += 1
            return inaccuracy_score

    def calculate_all_deviation(self, who):
        for person in self.predictions.keys():
            print(person)
            print(self.calculate_deviation(person, who))