# Path: evaluation/evaluate.py
# class Evaluate(GenerateResultCsv):
#     def __init__(self):
#         GenerateResultCsv.__init__(self)
#         return
#     def loadResultFiles(self, testSetNum):
# 
#         actualFile = 'actual_' + str(testSetNum) + '.csv'
#         predictionFile = 'prediction_' + str(testSetNum) + '.csv'
#         self.actualDict = self.loadDict(actualFile)
#         self.predictonDict = self.loadDict(predictionFile)
#         return
#     
#     def loadDict(self, filename):
#         df = pd.read_csv(filename, header=None)
#         res = {}
#         for _, row in df.iterrows():
#             res[(row[0], row[1])] = row[2]
#         return res
#     def calFinalResult(self, testSetNum):
#         self.loadResultFiles(testSetNum)
#         res = []
#         for key, value in self.actualDict.iteritems():
#             actual = value
#             if actual == 0:
#                 print "record {} is 0, not included in final calculation".format(key)
#                 continue
#             prediction = self.predictonDict[key]
#             temp = (actual - prediction)/float(actual)
#             if math.isnan(temp):
#                 print temp
#             res.append(abs(temp))
#         res = np.array(res)
#         pd.DataFrame(res).to_csv('result.csv')
#         print "final result: {}".format(res.mean())
#         return np.mean(res)
#     
#     def run(self):
#         self.generateActual_0()
#         self.generatePrediction_0()
#         self.calFinalResult(0)
#         return
# 
# Path: utility/datafilepath.py
# class DataFilePath:
#     def __init__(self):
#     def getOrderDir_Train(self):  
#     def getOrderDir_Test1(self):  
#     def getTest1Dir(self):
#     def getTest2Dir(self):
#     def getTrainDir(self):
#     def getGapCsv_Train(self):
#     def getGapCsv_Test1(self):
#     def getTestset1Readme(self):
#     def getTestset2Readme(self):
#     def getGapFilename(self):
#     def getGapPredictionFileName(self):
#     def getPrevGapFileName(self):
#     def get_dir_name(self, data_dir):


from  evaluation.evaluate import Evaluate
from utility.datafilepath import g_singletonDataFilePath
import pandas as pd
        for i in range(preNum):
            item = date + [str(slot -i -1)]
            res.append("-".join(item))
        return res
    def generatePrediction(self, allOderFilePath, testSlots,resultFileName):
        res = []
        df = pd.read_csv(allOderFilePath)
        testDistricts = self.generateTestDistrict()
        for timeslot in testSlots:
            for district in testDistricts:
                prevSlots = self.getPrevSlots(timeslot)
                sel = df['time_slotid'].isin(prevSlots) & (df['start_district_id'] == district)
                item = df[sel]['gap']    
                assert item.shape[0] <= 3, item.shape[0]
                if item.empty:
                    # no previous records found, assume zero
                    item = 0
                else:
                    item = item.mean()
                res.append((district, timeslot, item))
        df = pd.DataFrame(res, columns=['start_district_id', 'time_slotid', 'gap'])
        self.saveResultCsv(df, resultFileName)
        return
        
    def generatePrediction_0(self):
        testSlots = self.generateSlotSet_0()
        self.generatePrediction(g_singletonDataFilePath.getGapCsv_Train(), testSlots, 'prediction_0.csv')
        return
    def generatePrediction_1(self):
        testSlots = self.generateSlotSet_1()

# Path: evaluation/evaluate.py
# class Evaluate(GenerateResultCsv):
#     def __init__(self):
#         GenerateResultCsv.__init__(self)
#         return
#     def loadResultFiles(self, testSetNum):
# 
#         actualFile = 'actual_' + str(testSetNum) + '.csv'
#         predictionFile = 'prediction_' + str(testSetNum) + '.csv'
#         self.actualDict = self.loadDict(actualFile)
#         self.predictonDict = self.loadDict(predictionFile)
#         return
#     
#     def loadDict(self, filename):
#         df = pd.read_csv(filename, header=None)
#         res = {}
#         for _, row in df.iterrows():
#             res[(row[0], row[1])] = row[2]
#         return res
#     def calFinalResult(self, testSetNum):
#         self.loadResultFiles(testSetNum)
#         res = []
#         for key, value in self.actualDict.iteritems():
#             actual = value
#             if actual == 0:
#                 print "record {} is 0, not included in final calculation".format(key)
#                 continue
#             prediction = self.predictonDict[key]
#             temp = (actual - prediction)/float(actual)
#             if math.isnan(temp):
#                 print temp
#             res.append(abs(temp))
#         res = np.array(res)
#         pd.DataFrame(res).to_csv('result.csv')
#         print "final result: {}".format(res.mean())
#         return np.mean(res)
#     
#     def run(self):
#         self.generateActual_0()
#         self.generatePrediction_0()
#         self.calFinalResult(0)
#         return
# 
# Path: utility/datafilepath.py
# class DataFilePath:
#     def __init__(self):
#     def getOrderDir_Train(self):  
#     def getOrderDir_Test1(self):  
#     def getTest1Dir(self):
#     def getTest2Dir(self):
#     def getTrainDir(self):
#     def getGapCsv_Train(self):
#     def getGapCsv_Test1(self):
#     def getTestset1Readme(self):
#     def getTestset2Readme(self):
#     def getGapFilename(self):
#     def getGapPredictionFileName(self):
#     def getPrevGapFileName(self):
#     def get_dir_name(self, data_dir):


# Path: implement/statistics.py
from  evaluation.evaluate import Evaluate
from utility.datafilepath import g_singletonDataFilePath
import pandas as pd

        for i in range(preNum):
            item = date + [str(slot -i -1)]
            res.append("-".join(item))
        return res
    def generatePrediction(self, allOderFilePath, testSlots,resultFileName):
        res = []
        df = pd.read_csv(allOderFilePath)
        testDistricts = self.generateTestDistrict()
        for timeslot in testSlots:
            for district in testDistricts:
                prevSlots = self.getPrevSlots(timeslot)
                sel = df['time_slotid'].isin(prevSlots) & (df['start_district_id'] == district)
                item = df[sel]['gap']    
                assert item.shape[0] <= 3, item.shape[0]
                if item.empty:
                    # no previous records found, assume zero
                    item = 0
                else:
                    item = item.mean()
                res.append((district, timeslot, item))
        df = pd.DataFrame(res, columns=['start_district_id', 'time_slotid', 'gap'])
        self.saveResultCsv(df, resultFileName)
        return
        
    def generatePrediction_0(self):
        testSlots = self.generateSlotSet_0()
        self.generatePrediction(g_singletonDataFilePath.getGapCsv_Train(), testSlots, 'prediction_0.csv')
        return
    def generatePrediction_1(self):
        testSlots = self.generateSlotSet_1()

        self.generatePrediction(g_singletonDataFilePath.getGapCsv_Test1(), testSlots, 'temp/prediction_1.csv')