-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOfficeWaitingTime.py
160 lines (142 loc) · 4.68 KB
/
OfficeWaitingTime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#This file builds hashmap on office: waiting time list
#Also draw cdf for a certain office
import numpy as np
from Mocapy import *
from numpy import *
from scipy import *
import pandas
from patsy import dmatrices
from decimal import *
from pandas import *
import pandas as pd
from cluster import KMeansClustering
import numpy as np
from numpy import vstack,array
from scipy.cluster.vq import kmeans,vq
from cluster import KMeansClustering
from sklearn import hmm
from sklearn.hmm import MultinomialHMM
from sklearn import tree
import csv
import time
import re
from datetime import datetime
from pytz import timezone
import pytz
#import xlwt
import pdb
import matplotlib
import matplotlib.pyplot as plt
import pylab
from pylab import plot, show
from matplotlib.dates import date2num
from time import mktime
import matplotlib.pylab as mp
from matplotlib.dates import MinuteLocator, DateFormatter, HourLocator
from pylab import figure
from sklearn import svm
from itertools import *
import json
from bisect import bisect_left
from scipy.stats import norm
'''
#read two files
with open('20130128_offices.csv','rb') as csvfile:
offices = csv.reader(csvfile)
officelist = list()
for row in offices:
officelist.append(row)
officelist.pop(0)
OfficeId = list()
for item in officelist:
OfficeId.append(item[0])
Num_Office = len(OfficeId)
print "The number of office is ",
print Num_Office
'''
OfficeWaitingTime = dict()
with open('20130323_waiting_times.csv') as csvfile:
waiting_times = csv.reader(csvfile)
for row in waiting_times:
if(row[3]!='wo_appointment'):
office = row[1]
waitingtime = row[3]
if(OfficeWaitingTime.has_key(office)):
OfficeWaitingTime[office].append(waitingtime)
else:
OfficeWaitingTime[office] = list()
OfficeWaitingTime[office].append(waitingtime)
AverageWaitingTime = dict()
for key in OfficeWaitingTime:
tempList = [float(x) for x in OfficeWaitingTime[key]]
AverageWaitingTime[key] = list()
AverageWaitingTime[key].append(sum(tempList)/len(tempList))
class discrete_cdf:
def __init__(self,data):
self._data = data # must be sorted
self._data_len = float(len(data))
def __call__(self,point):
return (len(self._data[:bisect_left(self._data, point)]) /
self._data_len)
matplotlib.rc('xtick',labelsize=19)
matplotlib.rc('ytick',labelsize=19)
#####################################Change the office here
officeId = '632'
waitTimeList = [float(x) for x in OfficeWaitingTime[officeId]]
waitTimeList.sort()
cdf = discrete_cdf(waitTimeList)
xvalues = np.arange(0, max(waitTimeList),0.1)
yvalues = [cdf(point) for point in xvalues]
plt.plot(xvalues, yvalues,linewidth=4)
plt.xlabel("Wait time (hours)", fontsize=19)
plt.savefig("cdfoffice"+officeId+".eps")
plt.show()
'''
waiting_timeslist.pop(0)
number = len(waiting_timeslist)
#define a hashfunction to map the same bucket (ignoring weekday) to a unique value
#Then put the hash value in item[6]
def Hashfunction(item):
Hash = (str(item[4].weekday())+str(item[4].hour).zfill(2)+str((item[4].minute)/10))
return Hash
#Define a PutInMap function : put the item with the same hash value into the same key
#the key is the has value
#the value is a list
#0:total waiting time 1: total times
def PutInMap(item,Map):
temp = item[6]
if(temp not in Map):
Map[temp] = list()
Map[temp].append(float(item[3])) #if temp is not in hashmap yet
Map[temp].append(1)
else:
Map[temp][0] = Map[temp][0] + float(item[3]) #add the waiting time
Map[temp][1] = Map[temp][1] + 1 #count the times
#This function deals with test data set
#The key is the hash value
#the value is a list
#put the corresponding wo_appointment into the list
def PutInMap2(item,Map):
temp = item[6]
if(temp not in Map):
Map[temp] = list()
Map[temp].append(item[3])
else:
Map[temp].append(item[3])
def ComKeyDic(dic1,dic2):
return (set(dic1.keys()) == set(dic2.keys()))
# Convert String to Datetime
# each id:
# 0:d 1:office_id 2:w_appointment 3:wo_appointment 4:created_at 5:updated_at 6:Hash Value
col = 4
OfficeWaitingTime = dict()
for i in xrange(number):
#extract the time string and convert them to standard PST datetime
#temp = datetime.strptime(waiting_timeslist[i][col], "%Y-%m-%d %H:%M:%S")
#utc = pytz.UTC
#ams = pytz.timezone('US/Pacific')
#waiting_timeslist[i][col] = utc.localize(temp)
#waiting_timeslist[i][col] = waiting_timeslist[i][col].astimezone(ams)
#temp = waiting_timeslist[i]
#waiting_timeslist[i].append(Hashfunction(temp))
'''