Ejemplo n.º 1
0
    def getPrecisionRecallFalseRate(self, resultSimulation, kMAX, plot=False, output='dict'):

        """
        The function receives the detections/false alarms of all the nodes
        and computes the Precision/Recall for all the K.
        """

        precisionConfInterval = {}
        recallConfInterval = {}
        falseConfInterval = {}

        for k in range(1, kMAX + 1):
            precisionConfInterval[k] = []
            recallConfInterval[k] = []
            falseConfInterval[k] = []

        for key, value in resultSimulation.iteritems():

            detections = value['detections']
            falsePositives = value['falsePositives']

            for k in range(1, kMAX + 1):
                
                if detections[k]['events'] != 0:
                    recallConfInterval[k].append(detections[k]['detection']/float(detections[k]['events']))
                            
                if (detections[k]['detection'])!= 0 and falsePositives != 0:
                    precisionConfInterval[k].append((detections[k]['detection'])/(float(detections[k]['detection']) + falsePositives[k] ))    
                
                falseConfInterval[k].append(falsePositives[k]/float(len(self.truth.clears)))

        errorRecall = np.ndarray(kMAX)
        errorPrecision = np.ndarray(kMAX)
        errorFalse = np.ndarray(kMAX)

        meanRecall = np.ndarray(kMAX)
        meanPrecision = np.ndarray(kMAX)
        meanFalseRate = np.ndarray(kMAX)

        for k in range(1, kMAX + 1):
            a = recallConfInterval[k]
            meanRecall[k-1] = np.mean(a)
            interval = sms.DescrStatsW(a).tconfint_mean()    
            errorRecall[k-1] = interval[1] - np.mean(a)
            
            a = precisionConfInterval[k]
            meanPrecision[k-1] = np.mean(a)
            interval = sms.DescrStatsW(a).tconfint_mean()  
            errorPrecision[k-1] = interval[1] - np.mean(a)
            
            a = falseConfInterval[k]
            meanFalseRate[k-1] = np.mean(a)
            interval = sms.DescrStatsW(a).tconfint_mean()    
            errorFalse[k-1] = interval[1] - np.mean(a)

        if plot:
            visual = Visualization()
            visual.barRecallPrecisionvsK2(meanRecall, meanFalseRate, meanPrecision, errorRecall, errorPrecision, errorFalse)

        if output == 'dict':

            if meanFalseRate[0] > 1:
                meanFalseRate[0] = 1

            result = {

                'Precision': np.nan_to_num(meanPrecision).tolist(),
                'errPrecision': errorPrecision.tolist(),
                'Recall': np.nan_to_num(meanRecall).tolist(),
                'errRecall': errorRecall.tolist(),
                'FalseRate': np.nan_to_num(meanFalseRate).tolist(),
                'errFalseRate': errorFalse.tolist()
            }

            return result

        elif output == 'tuple':

            return meanPrecision, errorPrecision, meanRecall, errorRecall, meanFalseRate, errorFalse

        else:
            return