-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmenu.py
196 lines (164 loc) · 8.81 KB
/
menu.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
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import main
import arrow
import input_block
import write_code
arrow_num = 0 # count the number of arrows.
'''
Menu / Constraints widget : Bottom right screen of the window.
'''
class menu_set(QTableWidget):
def __init__(self, parent = None):
super(menu_set, self).__init__()
self.cellClicked.connect(self.cellClick)
def cellClick(self, row, col):
print("Click" + str(row) + " " + str(col))
class menu_block(menu_set):
def __init__(self, index, connect_list):
super(menu_block, self).__init__()
self.resize(250, 200)
self.setRowCount(2)
self.setColumnCount(5)
column_headers = ['name', 'activation function ', 'index', 'shape', 'connections']
self.setHorizontalHeaderLabels(column_headers)
self.setItem(0,2, QTableWidgetItem(str(index)))
self.setItem(0,4, QTableWidgetItem(str(connect_list)))
def edititem(self, item):
pass
'''
Check box class.
Every block has it's own connection list.
Initially every blocks are appeared in one's menu screen,
when you check box, blocks are connected.
'''
class check_box_class(QGridLayout):
def __init__(self, block_list, cur_block_index, cur_block_shape, connection_list, window):
super(check_box_class, self).__init__()
check_box_list = []
self.checked_list = [] # 0 : non-checked, 1 : checked
if cur_block_shape == 1:
self.addWidget(QLabel("ID : rectangle " + str(cur_block_index)), 0, 0)
elif cur_block_shape == 2:
self.addWidget(QLabel("ID : circle " + str(cur_block_index)), 0, 0)
else:
self.addWidget(QLabel("ID : iniput " + str(cur_block_index)), 0, 0)
self.addWidget(QLabel("Connection"), 1, 0)
for i in range(len(block_list)):
check_box_list.append(check_box(cur_block_index, block_list, block_list[i].index, block_list[i].shape, self.checked_list, connection_list, window))
self.checked_list.append(0)
self.addWidget(check_box_list[i], i+1, 1)
if (connection_list[cur_block_index][i] > -1):
check_box_list[i].setChecked(True)
else:
check_box_list[i].setChecked(False)
class check_box(QCheckBox):
def __init__(self, cur_block_index, block_list, check_box_index, check_box_shape, checked_list, connection_list, window):
super(check_box, self).__init__()
self.cur_block_index = cur_block_index
self.check_box_index = check_box_index
self.checked_list = checked_list
self.block_list = block_list
self.cur_block_index = cur_block_index
self.check_box_index = check_box_index
if check_box_shape == 1:
self.setText("rectangle " + str(check_box_index))
elif check_box_shape == 2:
self.setText("circle " + str(check_box_index))
elif check_box_shape == 0:
self.setText("input " + str(check_box_index))
else:
self.setText("output " + str(check_box_index))
self.stateChanged.connect(lambda : self.checked(connection_list, window))
def checked(self, connection_list, window):
global arrow_num
# Add arrow.
if self.isChecked() == True:
if connection_list[self.cur_block_index][self.check_box_index] == -1:
self.checked_list[self.check_box_index] = 1
connection_list[self.cur_block_index][self.check_box_index] = arrow_num
arrow.arrows(self.block_list[self.cur_block_index].pos.x(), self.block_list[self.cur_block_index].pos.y(), self.block_list[self.check_box_index].pos.x(), self.block_list[self.check_box_index].pos.y())
arrow_num += 1
activation_function = ""
if self.block_list[self.check_box_index].shape != 3:
activation_function, ok = input_block.activation_function.getOutput()
write_code.write_layer_process(window, self.block_list[self.cur_block_index].name, self.block_list[self.cur_block_index].size, self.block_list[self.check_box_index].name, self.block_list[self.check_box_index].size, activation_function)
if(self.block_list[self.check_box_index].shape == 3): # output box
loss_function, optimizer, display_step, ok = input_block.output_layer.getOutput()
write_code.write_output_box_process(window, self.block_list[self.cur_block_index].name, self.block_list[self.cur_block_index].size, loss_function, optimizer, display_step)
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")
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)
try: # Then, 'test.py' is compiled to 'dest.pyc'
py_compile.compile("brain_test" + ".py" , "dest1" + ".pyc")
print("compile complete")
except py_compile.PyCompileError:
print("error")
import shlex # Show output of 'dest.pyc' to cmd.
from subprocess import Popen, PIPE
args = shlex.split("python dest1.pyc")
proc = Popen(args, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
#exitcode = proc.returncode
print(out)
# remove arrow.
elif connection_list[self.cur_block_index][self.check_box_index] > -1:
self.checked_list[self.check_box_index] = 0
arrow.arrows.remove_arrow(self, connection_list[self.cur_block_index][self.check_box_index])
connection_list[self.cur_block_index][self.check_box_index] = -1
arrow_num -= 1
print(self.block_list[self.cur_block_index].pos)
for i in range(len(connection_list)):
print(connection_list[i])
print("\n")
# If you click right click button this class is activated.
class right_click_table(QWidget):
def __init__(self, block_list, current_block, connection_list, window):
super(right_click_table, self).__init__()
widget = QWidget()
global list_
list_ = block_list
self.curr = current_block
self.connection_list = connection_list
layout = check_box_class(block_list, current_block.index, current_block.shape, connection_list, window)
self.delete_button = QPushButton("Delete")
layout.addWidget(self.delete_button, 0, 1)
self.delete_button.clicked.connect(self.remove_block)
widget.setLayout(layout)
self.scroll = QScrollArea()
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll.setWidgetResizable(False)
self.scroll.setWidget(widget)
gLayout = QGridLayout()
gLayout.addWidget(self.scroll)
self.setLayout(gLayout)
# Remove block... Connected with 'remove' button.
def remove_block(self):
import sip
for i in list_:
if(i.index == self.curr.index):
list_.remove(i)
sip.delete(self.curr)
self.curr = None
class extend_list(list): # Extend the connection list.
def __init__(self, connection_list):
super(extend_list, self).__init__()
if len(connection_list) == 0:
connection_list = [[-1, -1], [-1, -1]]
for i in range(len(connection_list)):
connection_list[i].append(-1)
connection_list[i].append(-1)
connection_list.append([-1] * ((len(connection_list)) + 2))
connection_list.append([-1] * ((len(connection_list) - 1) + 2))