# 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: utility/dumpload.py
# class DumpLoad:
#     def __init__(self, pickle_filepath):
#         self.pickle_filepath = pickle_filepath
#         dir_path = os.path.dirname(pickle_filepath)
#         if not os.path.exists(dir_path):
#             os.makedirs(dir_path)
#         
#         return
#     def load(self):
#         with open(self.pickle_filepath, 'rb') as handle:
#             dataset = pickle.load(handle)
#         return dataset
#     def isExisiting(self):
#         return os.path.exists(self.pickle_filepath) 
#     def run(self):
#         self.dump((np.arange(10), np.arange(20)))
#         print(self.load())
#         return
#     
#     def dump(self, dataset):
#         with open(self.pickle_filepath, 'wb') as f:
#             pickle.dump(dataset, f, pickle.HIGHEST_PROTOCOL)
#         return


import pandas as pd
import os.path
import numpy as np
from  districtid import singletonDistricId
from timeslot import singletonTimeslot
from os import walk
from utility.datafilepath import g_singletonDataFilePath
from time import time
from utility.dumpload import DumpLoad
from seaborn.utils import iqr
        data_dir = g_singletonDataFilePath.getTest1Dir()
        gap_dict = self.get_gap_dict(data_dir)
        assert [0,1,0] == self.find_prev_gap(pd.Series([54, '2016-01-24-81']), pre_num = 3, gap_dict = gap_dict).tolist()
        assert [6,4,0] == self.find_prev_gap(pd.Series([7, '2016-01-30-141']), pre_num = 3, gap_dict = gap_dict).tolist()
        assert [0,0] == self.find_prev_gap(pd.Series([7, '2016-01-30-138']), pre_num = 2, gap_dict = gap_dict).tolist()
        assert [0,0,0] == self.find_prev_gap(pd.Series([7, '2016-01-30-139']), pre_num = 3, gap_dict = gap_dict).tolist()
        assert [0,0,1] == self.find_prev_gap(pd.Series([50, '2016-01-30-143']), pre_num = 3, gap_dict = gap_dict).tolist()
        assert [245,282,0] == self.find_prev_gap(pd.Series([51, '2016-01-22-141']), pre_num = 3, gap_dict = gap_dict).tolist()
        
        gap_meanmedian_dict = self.get_gap_meanmedian_dict()
        self.find_gap_meanmedian(pd.Series([5,55]),gap_meanmedian_dict = gap_meanmedian_dict)

        print "unit test passed"
        return
    def find_prev_gap(self, row, pre_num = 3, gap_dict = None):
        start_district_id = row.iloc[0]
        time_slotid = row.iloc[1]
        index = ['gap' + str(i + 1) for i in range(pre_num)]
        res = []
        prevSlots = singletonTimeslot.getPrevSlots(time_slotid, pre_num)
        for prevslot in prevSlots:
            try:
                res.append(gap_dict[(start_district_id, prevslot)])
            except:
                res.append(0)
        res =pd.Series(res, index = index)
        return res
    def get_gap_meanmedian_dict(self):
        data_dir = g_singletonDataFilePath.getTrainDir()
        filename = data_dir + 'order_data/temp/gap_meanmedian.dict.pickle'

# 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: utility/dumpload.py
# class DumpLoad:
#     def __init__(self, pickle_filepath):
#         self.pickle_filepath = pickle_filepath
#         dir_path = os.path.dirname(pickle_filepath)
#         if not os.path.exists(dir_path):
#             os.makedirs(dir_path)
#         
#         return
#     def load(self):
#         with open(self.pickle_filepath, 'rb') as handle:
#             dataset = pickle.load(handle)
#         return dataset
#     def isExisiting(self):
#         return os.path.exists(self.pickle_filepath) 
#     def run(self):
#         self.dump((np.arange(10), np.arange(20)))
#         print(self.load())
#         return
#     
#     def dump(self, dataset):
#         with open(self.pickle_filepath, 'wb') as f:
#             pickle.dump(dataset, f, pickle.HIGHEST_PROTOCOL)
#         return


# Path: exploredata/order.py
import pandas as pd
import os.path
import numpy as np
from  districtid import singletonDistricId
from timeslot import singletonTimeslot
from os import walk
from utility.datafilepath import g_singletonDataFilePath
from time import time
from utility.dumpload import DumpLoad
from seaborn.utils import iqr

        data_dir = g_singletonDataFilePath.getTest1Dir()
        gap_dict = self.get_gap_dict(data_dir)
        assert [0,1,0] == self.find_prev_gap(pd.Series([54, '2016-01-24-81']), pre_num = 3, gap_dict = gap_dict).tolist()
        assert [6,4,0] == self.find_prev_gap(pd.Series([7, '2016-01-30-141']), pre_num = 3, gap_dict = gap_dict).tolist()
        assert [0,0] == self.find_prev_gap(pd.Series([7, '2016-01-30-138']), pre_num = 2, gap_dict = gap_dict).tolist()
        assert [0,0,0] == self.find_prev_gap(pd.Series([7, '2016-01-30-139']), pre_num = 3, gap_dict = gap_dict).tolist()
        assert [0,0,1] == self.find_prev_gap(pd.Series([50, '2016-01-30-143']), pre_num = 3, gap_dict = gap_dict).tolist()
        assert [245,282,0] == self.find_prev_gap(pd.Series([51, '2016-01-22-141']), pre_num = 3, gap_dict = gap_dict).tolist()
        
        gap_meanmedian_dict = self.get_gap_meanmedian_dict()
        self.find_gap_meanmedian(pd.Series([5,55]),gap_meanmedian_dict = gap_meanmedian_dict)

        print "unit test passed"
        return
    def find_prev_gap(self, row, pre_num = 3, gap_dict = None):
        start_district_id = row.iloc[0]
        time_slotid = row.iloc[1]
        index = ['gap' + str(i + 1) for i in range(pre_num)]
        res = []
        prevSlots = singletonTimeslot.getPrevSlots(time_slotid, pre_num)
        for prevslot in prevSlots:
            try:
                res.append(gap_dict[(start_district_id, prevslot)])
            except:
                res.append(0)
        res =pd.Series(res, index = index)
        return res
    def get_gap_meanmedian_dict(self):
        data_dir = g_singletonDataFilePath.getTrainDir()
        filename = data_dir + 'order_data/temp/gap_meanmedian.dict.pickle'

        dumpload = DumpLoad( filename)