-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHistogram.py
138 lines (112 loc) · 3.76 KB
/
Histogram.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
import numpy as np
from Mocapy import *
from numpy import *
from scipy import *
import statsmodels.api as sm
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 RndCoV import RndCov
#from ghmm import *
from sklearn import hmm
from sklearn.hmm import MultinomialHMM
# This file computes the average error against time using only time without weekday
#This file computes the average error over all offices in each bucket.
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
#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
with open('20130128_waiting_times.csv') as csvfile:
waiting_times = csv.reader(csvfile)
waiting_timeslist = list()
for row in waiting_times:
waiting_timeslist.append(row)
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])
# Compare the key sets of two dictionary
# If they are equal, return true
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
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))
WaitTimeList = list()
TimeList = list()
for item in waiting_timeslist:
TimeList.append(item[6])
WaitTimeList.append(float(item[3]))
"The number of elements is: " + repr(len(waiting_timeslist))
WaitingTime = np.array(WaitTimeList)
Histogram = np.histogram(WaitingTime,bin=3000)