-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
534 lines (436 loc) · 18.8 KB
/
main.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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import arrow
import menu
import input_block
import write_code
import mode
click_listen = None
num = 0 # index of blocks + input box.
shape = 1 # shape of blocks.
block_list = [] # save all the list of blocks that are exist in main screen.
connection_list = [[-1, -1], [-1, -1]] # 2-dimensional list that save the connections of blocks.
layer_names = []
layer_functions = []
layer_sizes = []
num_layer = 0
num_classes = 0
directory = ""
learning_rate = 0
training_steps = 0
batch_size = 0
'''
weights = []
biases = []
time = []
spikes_in = []
spikes_network = []
spikes_out = []
potential = []
'''
optimizer = ""
loss_function = ""
display_step = 0
# Spike Neural Network
stimulus_epoch = 0
f = open("test.py", "w+")
class pallete_part(QGraphicsObject): # pallete part : right corner of Window.
def __init__(self, parent=None):
super(pallete_part, self).__init__(parent)
self.color = QColor(Qt.lightGray)
self.dragOver = True
self.shape = 0
self.setAcceptDrops(False)
def mousePressEvent(self, event):
self.setCursor(Qt.ClosedHandCursor)
def mouseMoveEvent(self, event):
if QLineF(QPointF(event.screenPos()), QPointF(event.buttonDownScreenPos(Qt.LeftButton))).length() < QApplication.startDragDistance():
return
drag = QDrag(event.widget())
mime = QMimeData()
drag.setMimeData(mime)
drag.setHotSpot(event.pos().toPoint())
mime.setText(str(self.shape))
pixmap = QPixmap(60, 60)
drag.setHotSpot(QPoint(30, 30))
if(self.shape == 2): # rec : self.shape == 1
pixmap = QPixmap(120, 60) # cir : self.shape == 2
drag.setHotSpot(QPoint(100, 30))
pixmap.fill(Qt.white)
painter = QPainter(pixmap)
painter.translate(0, 0)
painter.setRenderHint(QPainter.Antialiasing)
self.paint(painter, None, None)
painter.end()
pixmap.setMask(pixmap.createHeuristicMask())
drag.setPixmap(pixmap)
drag.exec_()
self.setCursor(Qt.OpenHandCursor)
def mouseReleaseEvent(self, event):
self.setCursor(Qt.OpenHandCursor)
class neuron_rec(pallete_part): # rectangular shape of pallete
def __init__(self, parent=None):
super(neuron_rec, self).__init__(parent)
self.setCursor(Qt.OpenHandCursor)
self.shape = 1
def boundingRect(self):
return QRectF(0, 0, 60, 60)
def paint(self, painter, option, widget=None):
painter.setBrush(self.color.lighter(130) if self.dragOver else self.color)
painter.drawRect(0, 0, 60, 60)
class neuron_cir(pallete_part): # circle shape of pallete
def __init__(self, parent=None):
super(neuron_cir, self).__init__(parent)
self.setCursor(Qt.OpenHandCursor)
self.shape = 2
def boundingRect(self):
return QRectF(80, 0, 40, 60)
def paint(self, painter, option, widget=None):
painter.setBrush(self.color.lighter(130) if self.dragOver else self.color)
painter.drawEllipse(80, 0, 40, 60)
class input_holder(pallete_part): # Input box
def __init__(self, parent=None):
super(input_holder, self).__init__(parent)
self.setCursor(Qt.OpenHandCursor)
self.shape = 0
def boundingRect(self):
return QRectF(140, 0, 80, 40)
def paint(self, painter, option, widget=None):
painter.setBrush(self.color.lighter(130) if self.dragOver else self.color)
painter.drawRect(140, 0, 80, 40)
painter.drawText(165, 25, "input")
class output_holder(pallete_part): # Output box
def __init__(self, parent=None):
super(output_holder, self).__init__(parent)
self.setCursor(Qt.OpenHandCursor)
self.shape = 3
def boundingRect(self):
return QRectF(140, 60, 80, 40)
def paint(self, painter, option, widget=None):
painter.setBrush(self.color.lighter(130) if self.dragOver else self.color)
painter.drawRect(140, 60, 80, 40)
painter.drawText(160, 85, "output")
class pallete(pallete_part): # pallete activating part
def __init__(self):
super(pallete, self).__init__()
self.setFlag(self.ItemHasNoContents)
self.rectangle = neuron_rec(self) # activate rectangle button
self.circle = neuron_cir(self) # activate circle button
self.input_box = input_holder(self)
self.output_box = output_holder(self)
def boundingRect(self):
return QRectF()
def paint(self, painter, option, widget=None):
pass
class qgraphicsView(QGraphicsView): # Main board Graphic View (Drop zone)
def __init__(self, scene):
super(qgraphicsView, self).__init__()
self.scene = scene
self.setScene(self.scene)
self.setAcceptDrops(True)
arrow.load_scene(self.scene)
def dragMoveEvent(self, event):
event.setAccepted(True)
def dragEnterEvent(self, event):
event.setAccepted(True)
self.dragOver = True
self.dragMoveEvent(event)
self.update()
def dropEvent(self, event):
self.dragOver = True
global num
event.setDropAction(Qt.MoveAction)
event.setAccepted(True)
pos = event.pos()
new_block = graphics(pos, int(event.mimeData().text()), self.scene) # Drop(Add) on the graphics
block_list.append(new_block)
block_list[len(block_list) - 1].shape = shape
if shape == 0: # When input box dropped.
type_text.append_input_text() # type code to plain text of 'Code' part.
compile_text(1) # copy the 'Code' to test.py and compile it to dest.pyc
elif shape < 3: # When layer box dropped.
tmp = layer_info()
layer_info.get_layer_info(tmp)
else: # When output box dropped.
output_block()
event.acceptProposedAction()
def resizeEvent(self, event):
pass
class type_text():
def __init__(self):
super().__init__()
global num_classes
global directory
global learning_rate
global training_steps
global batch_size
@staticmethod
def append_input_text(parent = None):
block_len = len(block_list) - 1
input_size, num_classes, directory, learning_rate, training_steps, batch_size, ok = input_block.input_name.getOutput()
block_list[block_len].name = "input"
block_list[block_len].function = "None"
block_list[block_len].index = block_len
block_list[block_len].size = input_size
block_list[0].num_classes = num_classes
layer_names.append("input")
layer_sizes.append(input_size)
print(layer_sizes[0])
print(num_classes)
print(directory)
print(learning_rate)
print(training_steps)
print(batch_size)
write_code.write_input_box_process(window, "input", input_size, num_classes, directory, learning_rate, training_steps, batch_size)
class layer_info():
def __init__(self):
super().__init__()
global optimizer
global loss_function
global display_step
def get_layer_info(self):
block_len = len(block_list) - 1
name, size, ok = input_block.input_layer.getOutput()
block_list[block_len].name = name
#block_list[block_len].function = func
block_list[block_len].index = block_len
block_list[block_len].size = size
layer_names.append(name)
layer_sizes.append(size)
#layer_functions.append(func)
class output_block():
def __init__(self):
super().__init__()
#global num_classes
global directory
global learning_rate
global training_steps
global batch_size
block_len = len(block_list) - 1
layer_names.append("output") # output
layer_sizes.append(block_list[0].num_classes)
block_list[block_len].name = "output"
block_list[block_len].function = None
block_list[block_len].index = block_len
block_list[block_len].size = block_list[0].num_classes
layer_names.append("output")
layer_sizes.append(block_list[0].num_classes)
class compile_text():
def __init__(self, flag_for_show_output):
f.seek(0)
f.truncate(0)
f.write(window.dock1.plaintext.toPlainText())
f.flush()
import py_compile # convert plain text of 'code' to 'test.py'.
try: # Then, 'test.py' is compiled to 'dest.pyc'
py_compile.compile("test" + ".py" , "dest" + ".pyc")
print("compile complete")
except py_compile.PyCompileError:
print("error")
if flag_for_show_output:
import shlex # Show output of 'dest.pyc' to cmd.
from subprocess import Popen, PIPE
args = shlex.split("python dest.pyc")
proc = Popen(args, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
#exitcode = proc.returncode
print(out)
# Dock widget needed to separate window panel efficiently.
class Dock_Graphics(QDockWidget): # Graphics (Drop Zone) Dock widget
def __init__(self):
super(Dock_Graphics, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Graphics')
self.scene = QGraphicsScene(0, 0, 650, 400)
self.graphic = qgraphicsView(self.scene)
self.setWidget(self.graphic)
self.show()
def mousePressEvent(self, event):
click_listen.flag = 0
print("Click empty screen")
class Dock_Code(QDockWidget): # Code (Code writing Zone) Dock widget
def __init__(self):
super(Dock_Code, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Code')
self.plaintext = QTextEdit()
self.plaintext.setPlainText("import tensorflow as tf\nimport numpy as np\nimport matplotlib.pyplot as plt")
self.plaintext.setAcceptDrops(False)
self.plaintext.textChanged.connect(lambda : compile_text(0))
self.setWidget(self.plaintext)
self.show()
class Dock_Constraints(QDockWidget): # Menu / Constraints widget
def __init__(self):
super(Dock_Constraints, self).__init__()
self.setWindowTitle('Constraints')
self.show()
class Window(QMainWindow): # Main window
def __init__(self):
QMainWindow.__init__(self)
global click_listen
pallete_buttons = pallete()
scene = QGraphicsScene(0, 0, 200, 400)
scene.addItem(pallete_buttons)
graphic = QGraphicsView(scene)
graphic.show()
self.setCentralWidget(graphic) # Drop zone
self.dock = Dock_Graphics()
self.addDockWidget(Qt.LeftDockWidgetArea, self.dock)
self.dock1 = Dock_Code()
self.addDockWidget(Qt.BottomDockWidgetArea, self.dock1)
self.dock2 = Dock_Constraints()
self.addDockWidget(Qt.BottomDockWidgetArea, self.dock2)
click_listen = click_listener() # Manage click event.
'''
When the block dragged and dropped to the main screen(graphics),
classes below are activated.
'''
class graphics_part(QGraphicsObject): #
def __init__(self, parent=None):
super(graphics_part, self).__init__(parent)
global num
self.color = QColor(Qt.lightGray)
self.dragOver = True
self.pos_ = None
self.setAcceptDrops(False)
self.index = num
self.connect_list = [] # List that save connected blocks
self.table = menu.menu_block(self.index, self.connect_list)
self.table.itemChanged.connect(self.refresh_table)
self.__mousePressPos = None
self.setFlag(self.ItemIsMovable, True)
def mousePressEvent(self, event):
self.__mousePressPos = event.pos() # delete?
if event.button() == Qt.LeftButton:
self.setCursor(Qt.ClosedHandCursor)
self.table.setItem(0, 3, QTableWidgetItem(str((self.shape))))
self.table.setItem(0, 0, QTableWidgetItem(str((block_list[self.index].name))))
self.table.setItem(0, 1, QTableWidgetItem(str((block_list[self.index].function))))
print(str(self.index) + " " + str(self.shape) + " " + str(self.connect_list))
window.dock2.setWidget(self.table)
elif event.button() == Qt.RightButton: # right click menu
print("right clicked")
window.dock2.setWidget(menu.right_click_table(block_list, self, connection_list, window))
self.update()
def refresh_table(self):
#self.index = self.table.item(0,0).text()
#self.shape = self.table.item(0,1).text()
self.connect_list.append(self.table.item(0,4).text())
class neuron_rec_(graphics_part):
def __init__(self, pos_):
super(neuron_rec_, self).__init__()
self.setCursor(Qt.OpenHandCursor)
self.shape = 1
self.pos_ = pos_
def boundingRect(self):
return QRectF(self.pos_.x() - 30, self.pos_.y() - 60, 60, 60)
def paint(self, painter, option, widget=None):
painter.setBrush(self.color.lighter(130) if self.dragOver else self.color)
painter.drawRect(self.pos_.x() - 30, self.pos_.y() - 60, 60, 60)
painter.drawText(self.pos_.x() - 4, self.pos_.y() - 22, str(self.index))
class neuron_cir_(graphics_part):
def __init__(self, pos_):
super(neuron_cir_, self).__init__()
self.setCursor(Qt.OpenHandCursor)
self.shape = 2
self.pos_ = pos_
def boundingRect(self):
return QRectF(self.pos_.x()-20, self.pos_.y()-60, 40, 60)
def paint(self, painter, option, widget=None):
painter.setBrush(self.color.lighter(130) if self.dragOver else self.color)
painter.drawEllipse(self.pos_.x() - 20, self.pos_.y() - 60, 40, 60)
painter.drawText(self.pos_.x() - 4, self.pos_.y() - 22, str(self.index))
class input_holder_(graphics_part):
def __init__(self, pos_):
super(input_holder_, self).__init__()
self.setCursor(Qt.OpenHandCursor)
self.shape = 0
self.pos_ = pos_
def boundingRect(self):
return QRectF(self.pos_.x()-20, self.pos_.y()-60, 80, 40)
def paint(self, painter, option, widget=None):
painter.setBrush(self.color.lighter(130) if self.dragOver else self.color)
painter.drawRect(self.pos_.x() - 20, self.pos_.y() - 60, 80, 40)
painter.drawText(self.pos_.x() + 4, self.pos_.y() - 32, "input")
class output_holder_(graphics_part):
def __init__(self, pos_):
super(output_holder_, self).__init__()
self.setCursor(Qt.OpenHandCursor)
self.shape = 3
self.pos_ = pos_
def boundingRect(self):
return QRectF(self.pos_.x()-20, self.pos_.y()-60, 80, 40)
def paint(self, painter, option, widget=None):
painter.setBrush(self.color.lighter(130) if self.dragOver else self.color)
painter.drawRect(self.pos_.x() - 20, self.pos_.y() - 60, 80, 40)
painter.drawText(self.pos_.x(), self.pos_.y() - 32, "output")
class graphics(graphics_part):
def __init__(self, pos, shape_, scene):
super(graphics, self).__init__()
self.setFlag(self.ItemHasNoContents)
self.scene = scene
self.pos = pos
global num
global num_layer
global shape
global connection_list
shape = shape_
if shape_ == 1 :
print("Add Rectangle")
self.scene.addItem(neuron_rec_(pos))
elif shape_ == 2 :
print("Add Circle")
self.scene.addItem(neuron_cir_(pos))
elif shape == 0 :
print("Add Input Holder")
self.scene.addItem(input_holder_(pos))
else:
print("Add Output Holder")
self.scene.addItem(output_holder_(pos))
if shape_:
num_layer += 1
num += 1 # index number for blocks
if(num == len(connection_list)):
menu.extend_list(connection_list)
def boundingRect(self):
return QRectF()
def paint(self, painter, option, widget=None):
pass
class click_listener():
global num
def __init__(self):
super(click_listener, self).__init__()
self.flag = 0
self.from_ = None
self.to_ = None
def connect(self, dest):
flag = 0 # Avoid duplication
for i in self.from_.connect_list:
if i == dest.index:
flag = 1
break
if flag == 0:
self.from_.connect_list.append(dest.index)
print("dest : ", self.from_.index)
print(self.from_.connect_list)
if __name__ == '__main__':
global window
f.truncate(0)
app = QApplication(sys.argv)
window = Window()
window.setGeometry(500, 200, 1000, 700)
window.setWindowTitle('Mango')
window.show()
mode_flag , ok = mode.input_mode.getOutput()
if mode_flag == 1: # Basic Neural Network
pass
elif mode_flag == 2: # Uncompleted...
write_code.cnn_process(window)
compile_text(1)
else: # Spike Neural Network
stimulus_epoch, ok = mode.stimulus_epoch.getOutput()
sys.exit(app.exec_())
f.close()