-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.py.bak
463 lines (368 loc) · 17.7 KB
/
GUI.py.bak
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# -*- coding: utf-8 -*-
import wx
import numpy as np
import matplotlib
import pandas as pd
import csv
import tushare as ts
import lstm
import time
from run import plot_results, save_result
# matplotlib采用WXAgg为后台,将matplotlib嵌入wxPython中
matplotlib.use("WXAgg")
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar
from matplotlib.ticker import MultipleLocator, FuncFormatter
import pylab
from matplotlib import pyplot
######################################################################################
class MPL_Panel_base(wx.Panel):
''''' #MPL_Panel_base面板,可以继承或者创建实例'''
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=-1)
self.Figure = matplotlib.figure.Figure(figsize=(4, 3), facecolor='white')
self.axes = self.Figure.add_subplot(111)
#self.axes = self.Figure.add_axes([0.1, 0.1, 0.8, 0.8])
self.FigureCanvas = FigureCanvas(self, -1, self.Figure)
# 继承鼠标移动显示鼠标处坐标的事件
self.FigureCanvas.mpl_connect('motion_notify_event', self.MPLOnMouseMove)
self.NavigationToolbar = NavigationToolbar(self.FigureCanvas)
#显示坐标点
self.StaticText = wx.StaticText(self, -1, label='Show Stock Index')
# =============================================================================
# self.StaticText01 = wx.StaticText(self,-1,label='time:')
# self.StaticText1 = wx.StaticText(self, -1, label='Index ')
# self.StaticText02 = wx.StaticText(self, -1, label='cost')
# self.StaticText2 = wx.StaticText(self, -1, label='show')
# =============================================================================
self.SubBoxSizer = wx.BoxSizer(wx.HORIZONTAL)
self.SubBoxSizer.Add(self.NavigationToolbar, proportion=0, border=2, flag=wx.ALL | wx.EXPAND)
self.SubBoxSizer.Add(self.StaticText, proportion=4, border=4, flag=wx.ALL | wx.EXPAND)
# =============================================================================
# self.SubBoxSizer.Add(self.StaticText01, proportion=-1, border=1, flag=wx.ALL | wx.EXPAND)
# self.SubBoxSizer.Add(self.StaticText1, proportion=-1, border=1, flag=wx.ALL | wx.EXPAND)
# self.SubBoxSizer.Add(self.StaticText02, proportion=-1, border=1, flag=wx.ALL | wx.EXPAND)
# self.SubBoxSizer.Add(self.StaticText2, proportion=-1, border=1, flag=wx.ALL | wx.EXPAND)
# =============================================================================
self.TopBoxSizer = wx.BoxSizer(wx.VERTICAL)
self.TopBoxSizer.Add(self.SubBoxSizer, proportion=4, border=2, flag=wx.ALL | wx.EXPAND)
self.TopBoxSizer.Add(self.FigureCanvas, proportion=-10, border=2, flag=wx.ALL | wx.EXPAND)
self.SetSizer(self.TopBoxSizer)
###方便调用
self.pylab = pylab
self.pl = pylab
#self.pyplot = pyplot
self.numpy = np
self.np = np
self.plt = pyplot
# 显示坐标值
def MPLOnMouseMove(self, event):
ex = event.xdata # 这个数据类型是numpy.float64
ey = event.ydata # 这个数据类型是numpy.float64
if ex and ey:
# 可以将numpy.float64类型转化为float类型,否则格式字符串可能会出错
self.StaticText.SetLabel('%10.5f,%10.5f' % (float(ex), float(ey)))
#self.StaticText1.SetLabel('%10.5f' % (float(ex)))
#self.StaticText2.SetLabel('%10.5f' % (float(ey)))
def UpdatePlot(self):
'''''#修改图形的任何属性后都必须使用self.UpdatePlot()更新GUI界面 '''
self.FigureCanvas.draw()
def plot(self, *args, **kwargs):
'''''#最常用的绘图命令plot '''
self.axes.plot(*args, **kwargs)
self.UpdatePlot()
def semilogx(self, *args, **kwargs):
''''' #对数坐标绘图命令 '''
self.axes.semilogx(*args, **kwargs)
self.UpdatePlot()
def semilogy(self, *args, **kwargs):
''''' #对数坐标绘图命令 '''
self.axes.semilogy(*args, **kwargs)
self.UpdatePlot()
def loglog(self, *args, **kwargs):
''''' #对数坐标绘图命令 '''
self.axes.loglog(*args, **kwargs)
self.UpdatePlot()
def grid(self, flag=True):
''''' ##显示网格 '''
if flag:
self.axes.grid()
else:
self.axes.grid(False)
def title_MPL(self, TitleString="基于长短期循环神经网络的股票预测软件V1.0"):
''''' # 给图像添加一个标题 '''
self.axes.set_title(TitleString)
def xlabel(self, XabelString="X"):
''''' # Add xlabel to the plotting '''
self.axes.set_xlabel(XabelString)
def ylabel(self, YabelString="Y"):
''''' # Add ylabel to the plotting '''
self.axes.set_ylabel(YabelString)
def xticker(self, major_ticker=1.0, minor_ticker=0.1):
''''' # 设置X轴的刻度大小 '''
self.axes.xaxis.set_major_locator(MultipleLocator(major_ticker))
self.axes.xaxis.set_minor_locator(MultipleLocator(minor_ticker))
def yticker(self, major_ticker=1.0, minor_ticker=0.1):
''''' # 设置Y轴的刻度大小 '''
self.axes.yaxis.set_major_locator(MultipleLocator(major_ticker))
self.axes.yaxis.set_minor_locator(MultipleLocator(minor_ticker))
def legend(self, *args, **kwargs):
''''' #图例legend for the plotting '''
self.axes.legend(*args, **kwargs)
def xlim(self, x_min, x_max):
''' # 设置x轴的显示范围 '''
self.axes.set_xlim(x_min, x_max)
def ylim(self, y_min, y_max):
''' # 设置y轴的显示范围 '''
self.axes.set_ylim(y_min, y_max)
def savefig(self, *args, **kwargs):
''' #保存图形到文件 '''
self.Figure.savefig(*args, **kwargs)
def cla(self):
''' # 再次画图前,必须调用该命令清空原来的图形 '''
self.axes.clear()
self.Figure.set_canvas(self.FigureCanvas)
self.UpdatePlot()
def ShowHelpString(self, HelpString="Show Help String"):
''''' #可以用它来显示一些帮助信息,如鼠标位置等 '''
self.StaticText.SetLabel(HelpString)
################################################################
class MPL_Panel(MPL_Panel_base):
''''' #MPL_Panel重要面板,可以继承或者创建实例 '''
def __init__(self, parent):
MPL_Panel_base.__init__(self, parent=parent)
# 测试一下
self.FirstPlot()
# 仅仅用于测试和初始化,意义不大
def FirstPlot(self):
# self.rc('lines',lw=5,c='r')
self.cla()
x = np.arange(-5, 5, 0.25)
y = np.sin(x)
self.yticker(0.5, 0.1)
self.xticker(1.0, 0.2)
self.xlabel('X')
self.ylabel('Y')
self.title_MPL("图像")
self.grid()
self.plot(x, y, '--^g')
###############################################################################
# MPL_Frame添加了MPL_Panel的1个实例
###############################################################################
class MPL_Frame(wx.Frame):
"""MPL_Frame可以继承,并可修改,或者直接使用"""
def __init__(self, title="lstm股票预测软件", size=(1000, 800)):
wx.Frame.__init__(self, parent=None, title=title, size=size)
self.MPL = MPL_Panel_base(self)
# 静态文本
label_name = '股票预测软件'
font = wx.Font(24, wx.ROMAN, wx.NORMAL, wx.LIGHT)
self.header = wx.StaticText(parent=self.MPL, label=label_name,
size=(450, 20), style=wx.ALIGN_CENTER, pos=(350, 1))
self.header.SetFont(font)
# 创建FlexGridSizer
self.FlexGridSizer = wx.FlexGridSizer(rows=9, cols=10, vgap=5, hgap=5)
self.FlexGridSizer.SetFlexibleDirection(wx.BOTH)
#self.RightPanel = wx.Panel(self, -1)
self.downPanel = wx.Panel(self,-1)
#加载背景
#self.RightPanel.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBack)
#self.downPanel.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBack)
# 一个ComboBox控件(下拉菜单)
'''
self.sampleList = ['sp1.csv', 'sp2.csv', 'sp3.csv', \
'sp4.csv', 'sp5.csv', 'sp5000.csv']
self.stocks_cb = wx.Choice(parent=self.downPanel,id=-1, pos=(10, 10), size=(100, 40),
choices=self.sampleList)
'''
#加载按键
self.load_bt = wx.Button(parent=self.downPanel, label="加载股票数据", pos=(10, 10), size=(100, 40))
self.Bind(wx.EVT_BUTTON, self.load, self.load_bt)
#训练按键
self.data_bt = wx.Button(parent=self.downPanel, label="训练预测模型", pos=(10, 10), size=(100, 40))
self.Bind(wx.EVT_BUTTON, self.train_data, self.data_bt)
# 预测按键
self.Button1 = wx.Button(self.downPanel, -1, "预测", size=(100, 40), pos=(10, 10))
self.Button1.Bind(wx.EVT_BUTTON, self.Button1Event)
#保存按键
self.save_bt = wx.Button(parent=self.downPanel, label="保存结果", pos=(10, 10), size=(100, 40))
self.Bind(wx.EVT_BUTTON, self.save, self.save_bt)
# 测试按钮2
self.Button2 = wx.Button(self.downPanel, -1, "关于本软件", size=(100, 40), pos=(10, 10))
self.Button2.Bind(wx.EVT_BUTTON, self.Button2Event)
#股票代码
self.input_text = wx.TextCtrl(self.downPanel,-1,"股票代码",size=(100, 40))
#self.submit_bt = wx.Button(parent=self.downPanel, label="获取股票数据", pos=(10, 10),size=(100, 40))
#self.Bind(wx.EVT_BUTTON, self.submit, self.submit_bt)
# 加入Sizer中
#self.FlexGridSizer.Add(self.stocks_cb, proportion=0, border=5, flag=wx.ALL | wx.EXPAND)
self.FlexGridSizer.Add(self.input_text, proportion=0, border=5, flag=wx.ALL | wx.EXPAND)
#self.FlexGridSizer.Add(self.submit_bt, proportion=0, border=5, flag=wx.ALL | wx.EXPAND)
self.FlexGridSizer.Add(self.load_bt, proportion=0, border=5, flag=wx.ALL | wx.EXPAND)
self.FlexGridSizer.Add(self.data_bt, proportion=0, border=5, flag=wx.ALL | wx.EXPAND)
self.FlexGridSizer.Add(self.Button1, proportion=0, border=5, flag=wx.ALL | wx.EXPAND)
self.FlexGridSizer.Add(self.save_bt, proportion=0, border=5, flag=wx.ALL | wx.EXPAND)
self.FlexGridSizer.Add(self.Button2, proportion=0, border=5, flag=wx.ALL | wx.EXPAND)
self.downPanel.SetSizer(self.FlexGridSizer)
#self.BoxSizer = wx.BoxSizer(wx.HORIZONTAL)
self.BoxSizer = wx.BoxSizer(wx.VERTICAL)
self.BoxSizer.Add(self.MPL, proportion=10, border=2, flag=wx.ALL | wx.EXPAND)
self.BoxSizer.Add(self.downPanel, proportion=0, border=2, flag=wx.ALL | wx.EXPAND)
self.SetSizer(self.BoxSizer)
# 状态栏
#self.StatusBar()
self.makeMenuBar()
# MPL_Frame界面居中显示
self.Centre(wx.BOTH)
def load(self, event):
#stock_name = self.stocks_cb.GetString(self.stocks_cb.GetSelection())
gp_num=self.input_text.GetValue()
a=ts.get_hist_data(gp_num,ktype = 'W')
ar=np.array(a)
br=ar[:,1]
#stock_name=self.input_text.GetValue()
seq_len = 50
print('> 加载数据中... ')
#filename = r'C:\Prectice_project\lstm_gp\stock_data\%'+str(stock_name)+'.csv'
#filename='C:\Prectice_project\lstm_gp\stock_data\ssss.csv'
#print(filename)
filename=br
try:
self.X_train, self.y_train, self.X_test, self.y_test = \
lstm.load_data(filename, seq_len, True)
except:
wx.MessageBox("数据加载失败,请检查操作!!!")
else:
print('> Data Loaded. Compiling...')
wx.MessageBox("数据加载完成")
def train_data(self, event):
global_start_time = time.time()
epochs = 1
try:
self.model = lstm.build_model([1, 50, 100, 1])
self.model.fit(
self.X_train,
self.y_train,
batch_size=512,
nb_epoch=epochs,
validation_split=0.05)
self.predicted = lstm.predict_point_by_point(self.model, self.X_test)
except:
wx.MessageBox("模型训练失败,请检查操作!!!")
else:
wx.MessageBox("模型训练完成, 训练用时: %d s" % (time.time() - global_start_time))
def save(self, event):
try:
save_result(y_test=self.y_test, predicted_values=self.predicted)
except:
wx.MessageBox("结果保存失败,请检查操作!!!")
else:
wx.MessageBox("结果保存成功!")
# 定义菜单栏
def makeMenuBar(self):
"""
A menu bar is composed of menus, which are composed of menu items.
This method builds a set of menus and binds handlers to be called
when the menu item is selected.
"""
menuBar = wx.MenuBar()
self.CreateStatusBar(number=3)
menu = wx.Menu()
self.about_me(menu)
self.support(menu)
self.help_view(menu)
menuBar.Append(menu, "&帮助")
self.SetMenuBar(menuBar)
def help_view(self, menu):
menu.AppendSeparator()
versionItem = menu.Append(-1, "Version", "V 1.0")
menu.AppendSeparator()
exitItem = menu.Append(wx.ID_EXIT)
self.Bind(wx.EVT_MENU, self.OnExit, exitItem)
self.Bind(wx.EVT_MENU, self.version, versionItem)
def version(self, event):
wx.MessageBox("Version: 1.0")
def OnExit(self, event):
"""Close the frame, terminating the application."""
self.Close(True)
def about_me(self, menu):
item = wx.MenuItem(menu, id=122, text="关于我们", kind=wx.ITEM_NORMAL)
self.Bind(wx.EVT_MENU, self.about1_click, item)
menu.Append(item)
def about1_click(self, event):
wx.MessageBox("版本号 V1.0 \n 2018.5.11'")
def support(self, menu):
menu.AppendSeparator()
item = wx.MenuItem(menu, id=122, text="技术支持", kind=wx.ITEM_NORMAL)
self.Bind(wx.EVT_MENU, self.support_click, item)
menu.Append(item)
def support_click(self, event):
wx.MessageBox("1、加载数据:首先带你是")
# 按钮事件,用于测试
def Button1Event(self, event):
self.MPL.cla() # 必须清理图形,才能显示下一幅图
#x = np.arange(-10, 10, 0.25)
x,y=self.predicted, self.y_test
#y = np.cos(x)
self.MPL.axes.plot(x)
self.MPL.axes.plot(y)
self.MPL.xlabel('Time')
self.MPL.ylabel('Range')
#self.MPL.ylim(0,500)
#self.MPL.axes.plot(x, label='True Data')
#self.MPL.plot(y, label='Prediction')
self.MPL.legend(['True', 'Prediction'], loc='upper right')
#self.MPL.xticker([0, 100, 200, 300, 400],
#['2017-01', '2017-03', '2017-05', '2017-07', '2017-09'])
self.MPL.grid()
self.MPL.ShowHelpString()
self.MPL.UpdatePlot() # 必须刷新才能显示
def Button2Event(self, event):
self.AboutDialog()
# 打开文件,用于测试
def DoOpenFile(self):
wildcard = r"Data files (*.dat)|*.dat|Text files (*.txt)|*.txt|ALL Files (*.*)|*.*"
open_dlg = wx.FileDialog(self, message='Choose a file', wildcard=wildcard, style='')
if open_dlg.ShowModal() == wx.ID_OK:
path = open_dlg.GetPath()
try:
file = open(path, 'r')
text = file.read()
file.close()
except:
dlg = wx.MessageDialog(self, 'Error opening file\n')
dlg.ShowModal()
open_dlg.Destroy()
#加载背景
def OnEraseBack(self, event):
dc = event.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap(r"C:\Prectice_project\lstm_end\picture\background.bmp")
dc.DrawBitmap(bmp, 0, 0)
# 自动创建状态栏
def StatusBar(self):
self.statusbar = self.CreateStatusBar()
self.statusbar.SetFieldsCount(5)
self.statusbar.SetStatusWidths([-1, -1, -1,-1,-1])
# About对话框
def AboutDialog(self):
dlg = wx.MessageDialog(self,
'\t基于长短期记忆循环神经网络的股票预测软件'
'\t\n本软件使用深度学习的方法构建预测模型,实现对个股开盘价格走势的准确预测 '
'\n版本号 V1.0 \n 2018.5.11',
'关于此软件', wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
###############################################################################
# 主程序测试
if __name__ == '__main__':
app = wx.App()
# frame = MPL2_Frame()
frame = MPL_Frame()
frame.Center()
frame.Show()
app.MainLoop()