-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
177 lines (154 loc) · 5.99 KB
/
app.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
from flask import *
from train1 import train
from predict1 import predict
from predict1 import pre1
from indicator import indicate
from indicator import getLossAndMAPE
import numpy as np
import pymysql
import dataOpt
app = Flask(__name__)
db=pymysql.connect("localhost", "root", "admin", "traffic_flow")
#cursor=db.cursor()
#cursor.execute("select * from sjb")
#data=cursor.fetchone()
#print(data)
'''全局变量'''
train_file_path=''
test_file_path=''
choose_set=''
'''训练参数'''
t_lag=6
t_batch=16
t_epochs=2
t_bit=1
t_validaton=0.1
t_model_unit=[6,64,64,1]
@app.route('/')
def menu():
return render_template('menu.html')
@app.route('/train')
def url_train1(trainFile_path='nothing',testFile_path='nothing'):
#file_path={'trainFile_path':'未选择','textFile_path':'未选择'}
return render_template('train1.html')
@app.route('/predict')
def url_predict1():
return render_template('predict1.html')
@app.route('/dataManager')
def data_mannager():
return render_template('dataManager.html')
@app.route('/show')
def show():
return render_template('show.html')
@app.route('/show_1')
def show_1():
return render_template('show_1.html')
@app.route('/show_2')
def show_2():
return render_template('show_2.html')
@app.route('/show_3')
def show_3():
return render_template('show_3.html')
@app.route('/train1_sendFile',methods=['POST'])
def train1_sendFile():
global train_file_path #修改全局变量前声明
global test_file_path
request_file=request.form.to_dict()
train_file_path='data/'+request_file['train_file']
test_file_path='data/'+request_file['test_file']
return json.dumps({'trainFile_path':train_file_path,'testFile_path':test_file_path})
@app.route('/train1_train',methods=['POST'])
def train1_train():
global t_model_unit #修改全局变量前声明
global t_batch
global t_epochs
global t_validaton
global t_bit
global t_lag
request_file=request.form.to_dict()
t_batch=eval(request_file['batch'])
t_epochs=eval(request_file['epochs'])
t_bit=eval(request_file['bit'])
t_lag=6
choose_set='test_file'
# choose_set=request_file['choose_set']
#t_validaton=request_file['validaton']
t_model_unit=[t_lag,64,64,1] #网络结构参数
#train(trainFile=train_file_path,testFile=test_file_path,model_unit=t_model_unit,lag=6,batch=t_batch,epochs=t_epochs,bit=1,validaton=0.1,set=choose_set) #训练模型
train(trainFile=train_file_path,testFile=test_file_path,model_unit=[6, 64, 64, 1], lag=6,batch=16, epochs=t_epochs, bit=1, validaton=0.1, set='test_file')
pre_list,y_list=predict('model/lstm.h5',trainFile=train_file_path,testFile=test_file_path) #pre_list:预测序列,y_list真实序列
ind=indicate(pre_list,y_list)
loss,mape=getLossAndMAPE()
return json.dumps({'predictList':pre_list.tolist(),'trueList':y_list.tolist(),'indicationList':ind,'loss':loss.tolist(),'mape':mape.tolist()})
@app.route('/predict_pre1',methods=['POST'])
def predict_pre1():
model_file=request.form.get('modelFile')
data_list=list(map(int, request.form.getlist("dataList[]")))
bit=eval(request.form.get('bit'))
pre=pre1('model/lstm.h5',data_list,bit)
return json.dumps({'prelist':pre.tolist()})
@app.route('/dataManager_importData',methods=['POST'])
def dataManager_importData():
request_file = request.form.to_dict()
fpath = request_file['filePath']
dataOpt.readCSV(fpath)
print(1)
return json.dumps({'list':1})
@app.route('/dataManager_deleteAllData',methods=['POST'])
def dataManager_deleteAllData():
request_file = request.form.to_dict()
fpath = request_file['opt']
dataOpt.deleteAll()
return json.dumps({'list':1})
@app.route('/dataManager_search',methods=['POST'])
def dataManager_search():
request_file = request.form.to_dict()
region = request_file['region']
time=request_file['time']
data=dataOpt.searchByRegionAndTime(region,time)
return json.dumps({'datalist':list(data)})
@app.route('/chart2_load_byregion',methods=['POST'])
def chart2_load_byregion():
request_file = request.form.to_dict()
region = request_file['region']
timelist,flowlist=dataOpt.chart2ReadByRegion(region)
return json.dumps({'timelist':timelist,'flowlist':flowlist})
@app.route('/chart3_load_byregion',methods=['POST'])
def chart3_load_byregion():
request_file = request.form.to_dict()
region = request_file['region']
daylist,hourlist,flowlist=dataOpt.chart3ReadByRegion(region)
return json.dumps({'daylist':daylist,'hourlist':hourlist,'flowlist':flowlist})
@app.route('/chart4_load_byday',methods=['POST'])
def chart4_load_byday():
request_file = request.form.to_dict()
day = eval(request_file['day'])
regionlist,hourlist,flowlist=dataOpt.chart4ReadByDay(day)
return json.dumps({'regionlist':regionlist,'hourlist':hourlist,'flowlist':flowlist})
class MyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(MyEncoder, self).default(obj)
@app.route('/predict_pre2',methods=['POST'])
def predict_pre2():
model_file=request.form.get('modelFile')
data_list=list(map(int, request.form.getlist("dataList[]")))
bit=eval(request.form.get('bit'))
data_list=np.array(data_list)
prelist=[]
for i in range(48):
print('%s/48'%i)
flowlist=data_list[-6:]
flowlist=list(flowlist)
pre=pre1(model_file,flowlist,bit)
prelist.append(pre[0])
data_list=np.append(data_list,int(pre[0]))
return json.dumps({'prelist':prelist},cls=MyEncoder)
if __name__ == '__main__':
app.run(debug=False)