-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternActionSimulation_FourierTransform_06-18-2015_ForWeb.py~
255 lines (208 loc) · 11.5 KB
/
PatternActionSimulation_FourierTransform_06-18-2015_ForWeb.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/home3/lafontai/python/Python-2.7.2/python
# -*- coding: UTF-8 -*-
from __future__ import division
print "Content-Type: text/plain; charset=utf-8"
print
import cgi
import cgitb
cgitb.enable(display=1,logdir="/home3/lafontai/public_html/logs/pythonlog.txt")
import datetime as dt
import numpy as np
import scipy as sp
from numpy.fft import *
import calendar
import itertools
from scipy import signal
from sklearn.cluster import DBSCAN
def transformCalendar(startDate,endDate,fuzzyness,cycle,cycleFreq):
if cycle == 1:
dateList = [startDate + dt.timedelta(days=x*cycleFreq) for x in range(0, 1+np.floor((endDate-startDate).days/cycleFreq).astype('int'))]
elif cycle == 2:
dateList = [dt.datetime(startDate.year+int((startDate.month+x*cycleFreq-1)//12), int((startDate.month+x*cycleFreq-1)%12+1), min(startDate.day,(calendar.monthrange(startDate.year+int((startDate.month+x*cycleFreq-1)//12),int((startDate.month+x*cycleFreq-1)%12+1)))[1])) for x in range(0, 1+np.floor(round((endDate-startDate).days/30)/cycleFreq).astype('int'))]
elif cycle == 3:
dateList = [dt.datetime(startDate.year+int(x*cycleFreq), startDate.month, startDate.day) for x in range(0, 1+np.floor(round((endDate-startDate).days/365)/cycleFreq).astype('int'))]
return dateList
def add2Calendar(startDate,endDate,fuzzyness,cycle,cycleFreq):
if cycle == 1:
dateList = [startDate + dt.timedelta(days=x*cycleFreq) for x in range(0, 2+np.floor((endDate-startDate).days/cycleFreq).astype('int'))]
elif cycle == 2:
dateList = [dt.datetime(startDate.year+int((startDate.month+x*cycleFreq-1)//12), int((startDate.month+x*cycleFreq-1)%12+1), min(startDate.day,calendar.monthrange(startDate.year+int((startDate.month+x*cycleFreq-1)//12),int((startDate.month+x*cycleFreq-1)%12+1))[1] )) for x in range(0, 2+np.floor(round((endDate-startDate).days/30)/cycleFreq).astype('int'))]
elif cycle == 3:
dateList = [dt.datetime(startDate.year+int(x*cycleFreq), startDate.month, startDate.day) for x in range(0, 2+np.floor(round((endDate-startDate).days/365)/cycleFreq).astype('int'))]
return dateList[-1]
def patternScore2(realCalendar,startDate,endDate,fuzzyness,cycle,cycleFreq):
w = 1 # Penalty for missing a prediction
if cycle==1 and cycleFreq-fuzzyness <= 2: #Periods cannot overlap
return 0
noCycles = np.floor(SubDate(startDate,endDate)[cycle]/cycleFreq).astype('int')
hitDays = 1+noCycles*(1+2*fuzzyness) #Total days there might be events including fuzzyness
totalDays = (endDate-startDate).days
precision = 2*cycle/fuzzyness
patternCalendar = transformCalendar(startDate,endDate,fuzzyness,cycle,cycleFreq)
predictions = [(np.min(np.abs((date-realCalendar)))).days<=fuzzyness for date in patternCalendar]
trueMfalse = (1+w)*np.sum(predictions)-w*len(predictions)-1 # True positives - False positives -1
# because the first one is meaningless
return (precision*trueMfalse)
def SubDate(date1, date2):
differences = [0 ,
(date2-date1).days,
date2.month - date1.month + (date2.year-date1.year)*12,
date2.year-date1.year]
return differences
def calendar2Signal(calendar):
signals = np.zeros((max(calendar)-min(calendar)).days+1)
for date in calendar:
signals[(date-min(calendar)).days] = 1
return signals
def findPeriod_Fourier(realCalendar):
signal=calendar2Signal(realCalendar)
Transform = np.fft.rfft(signal)
n = len(signal)
L = 1
k = np.linspace(0.0, 1.0/(2.0*L), n/2)
# Find the index of the frequency with the highest strength
if len(Transform)>1:
indHigh = 1+np.argmax(np.square(np.real(Transform[1:])) + np.square(np.imag(Transform[1:])))
else:
return 0
#Period corresponding to the above index
Period = (1/np.fft.fftfreq(n,1))[indHigh]
peakind = sp.signal.find_peaks_cwt(np.square(np.real(Transform)), np.arange(1,10))
Periods = np.unique(np.round((1/np.fft.rfftfreq(n,1))[peakind]))
#plt.plot(1/np.fft.rfftfreq(n,1), np.square(np.real(Transform)))
return Periods
#return plotScatter(1/np.fft.rfftfreq(n,1), np.square(np.real(Transform)))
def findPeriod_Differences(realCalendar):
DateCombinations = list(itertools.combinations(realCalendar,2))
return np.unique(np.abs([a-b for a,b in DateCombinations]))
def monthDivider(curDate, iniDate, period):
MonthDiff = curDate.month - iniDate.month + (curDate.year-iniDate.year)*12
monthMod = MonthDiff%period
monthPer = np.floor(MonthDiff/period)
Mod = min(curDate.day, 28) + 28*monthMod
return [Mod/(28*period*2), monthPer/12]
def yearDivider(curDate, iniDate, period):
yearDiff = curDate.year-iniDate.year
yearMod = yearDiff%period
yearPer = np.floor(yearDiff/period)
Mod = curDate.timetuple().tm_yday + 365*yearMod
return [Mod/(365*period*2), yearPer/12]
def findPattern(realCalendar):
# Function to step through time making predictions but blind to the future
# Algorithm will always make a prediction based on a pattern score
# Function will return all predictions made, the score that made it and if it was correct
Predictions = [] # Need to make sure I overwrite everything but just for now
Scores = []
FuzzynessS = []
Results = []
endDate = realCalendar[-1]
BestScore = 0
BestSet = (None,None,None,None,None,None)
Periods = findPeriod_Fourier(realCalendar)
#Always try a yearly cycle, fourier isn't good at checking this with the given parameters
#changing parameters will just slow it down so it's not worth it (from observation)
CycleFreq = 1
X = np.array([yearDivider(date, realCalendar[0], CycleFreq) for date in realCalendar])
xShift = np.zeros(X.shape)
xShift[:,0] = .5
X = np.vstack((X,X+xShift))
#X = StandardScaler().fit_transform(X) # I'm normalizing in yearDivider
DB = DBSCAN(eps=.2, min_samples=1).fit(X)
#return X,CycleFreq,DB
mainLabel = (sp.stats.mode(DB.labels_))[0]
realCalendar2 = np.hstack((realCalendar, realCalendar))
startDate = np.min(realCalendar2[DB.labels_==mainLabel])
# Find fuzziness. A perfect pattern is currently not allowed/ min(fuzziness) = 1
mods = X[DB.labels_==mainLabel,0]
cyclePos = X[DB.labels_==mainLabel,1]
meanMod = np.mean(mods)
modDiff = np.abs(mods-meanMod)
trimMods = []
#Correct for two dates in same cycle
for pos in np.unique(cyclePos):
trimMods.append((mods[cyclePos==pos])[np.argmin(modDiff[cyclePos==pos])])
meanMod = np.mean(trimMods)
# Start date should be at the mean modulus
startDate = startDate+dt.timedelta(days=365*CycleFreq*2*(meanMod-(yearDivider(startDate, realCalendar[0], CycleFreq))[0]))
fuzziness = max(np.ceil((np.max(trimMods) - np.min(trimMods))*365*CycleFreq*2/2),1)
if fuzziness<180: #Limit on fuzziness is half a year
Score = patternScore2(realCalendar,startDate, endDate,fuzziness,3,max(CycleFreq,1))
if Score>BestScore:
BestScore = Score
BestSet = (BestScore,startDate, endDate,fuzziness,3,CycleFreq)
if np.any(Periods):
for Period in Periods[5:]:
if Period>20: # If relevant cycle through months
CycleFreq = round(Period/30)
X = np.array([monthDivider(date, realCalendar[0], CycleFreq) for date in realCalendar])
xShift = np.zeros(X.shape)
xShift[:,0] = .5
X = np.vstack((X,X+xShift))
#X = StandardScaler().fit_transform(X) # I'm normalizing in monthDivider
DB = DBSCAN(eps=.2, min_samples=1).fit(X)
mainLabel = (sp.stats.mode(DB.labels_))[0]
realCalendar2 = np.hstack((realCalendar, realCalendar))
startDate = np.min(realCalendar2[DB.labels_==mainLabel])
# Find fuzziness. A perfect pattern is currently not allowed/ min(fuzziness) = 1
mods = X[DB.labels_==mainLabel,0]
cyclePos = X[DB.labels_==mainLabel,1]
meanMod = np.mean(mods)
modDiff = np.abs(mods-meanMod)
trimMods = []
#Correct for two dates in same cycle
for pos in np.unique(cyclePos):
trimMods.append((mods[cyclePos==pos])[np.argmin(modDiff[cyclePos==pos])])
meanMod = np.mean(trimMods)
# Start date should be at the mean modulus
startDate = startDate+dt.timedelta(days=28*CycleFreq*2*(meanMod-(monthDivider(startDate, realCalendar[0], CycleFreq))[0]))
fuzziness = max(np.ceil((np.max(trimMods) - np.min(trimMods))*28*CycleFreq*2/2),1)
if fuzziness<Period:
Score = patternScore2(realCalendar,startDate, endDate,fuzziness,2,max(CycleFreq,1))
if Score>BestScore:
BestScore = Score
BestSet = (BestScore,startDate, endDate,fuzziness,2,CycleFreq)
#Always cycle through days (for a given period)
DateDiffs = [(date-realCalendar[0]).days for date in realCalendar]
Mods = np.array(DateDiffs%Period)
Mods = np.hstack((Mods,Period+Mods))
Floors = np.array(np.floor(DateDiffs/Period))
Floors = np.hstack((Floors,Floors))
X = (np.vstack((Mods,Floors))).T
X[:,0] = X[:,0]/(2*Period)
X[:,1] = X[:,1]/24 # At the day scale every cycle becomes less significant? 24>12
#X = StandardScaler().fit_transform(X)
DB = DBSCAN(eps=.2, min_samples=1).fit(X)
#return X, Period, DB
mainLabel = (sp.stats.mode(DB.labels_))[0]
realCalendar2 = np.hstack((realCalendar, realCalendar))
startDate = np.min(realCalendar2[DB.labels_==mainLabel]) #TODO: Correct for median
# Find fuzziness. A perfect pattern is currently not allowed/ min(fuzziness) = 1
mods = X[DB.labels_==mainLabel,0]
cyclePos = X[DB.labels_==mainLabel,1]
meanMod = np.mean(mods)
modDiff = np.abs(mods-meanMod)
trimMods = []
#Correct for two dates in same cycle
for pos in np.unique(cyclePos):
trimMods.append((mods[cyclePos==pos])[np.argmin(modDiff[cyclePos==pos])])
meanMod = np.mean(trimMods)
# Start date should be at the mean modulus
startDate = startDate+dt.timedelta(days=(Period*2*meanMod-(startDate-realCalendar[0]).days%Period))
fuzziness = max(np.ceil((np.max(trimMods) - np.min(trimMods))*Period*2/2),1)
fuzziness = max(np.ceil((np.max(Mods[DB.labels_==mainLabel]) - np.min(Mods[DB.labels_==mainLabel]))/2), 1)
if fuzziness<Period:
Score = patternScore2(realCalendar,startDate, endDate,fuzziness,1,max(Period,1))
if Score>BestScore:
BestScore = Score
BestSet = (BestScore,startDate, endDate,fuzziness,1,Period)
return BestSet
postData = cgi.FieldStorage()
textCalendar=np.nonzero(np.array(Filters.getvalue("realCalendar").split(','))=='true')
#json_data=open("/media/Libraries/Documents/Insight/Data/user0.json").read()
#userData = json.loads(json_data)
#flightData = pd.DataFrame([Datum['data'] for Datum in userData])
#flightData['end_dt'] = pd.to_datetime(flightData['end_dt'])
#flightData['start_dt'] = pd.to_datetime(flightData['start_dt'])
#realCalendar = np.array(sorted(flightData[flightData.end_airport=='SNA'].start_dt))
#BestSet = findPattern(realCalendar)
print textCalendar