-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.py
168 lines (101 loc) · 4.35 KB
/
graphics.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
import pygame, sys
from pygame.locals import *
import matplotlib
matplotlib.use("Agg")
import matplotlib.backends.backend_agg as agg
import pylab
import numpy as np
from activations import *
from neural import lossHistory
#GRAPH ACTIVATION FUNCTION
actvFig = pylab.figure(figsize=[2, 2],dpi=100,)
lossFig = pylab.figure(figsize=[3.5, 3.5], dpi=100,)
actv = actvFig.gca()
lossg = lossFig.gca()
valueRange = []
index = -5.0
while index <= 5.0:
valueRange.append(johnStep(index))
index += .1
actv.plot(valueRange)
actvCanvas = agg.FigureCanvasAgg(actvFig)
actvCanvas.draw()
actvRenderer = actvCanvas.get_renderer()
actvRawData = actvRenderer.tostring_rgb()
######################################
from neural import *
def matrixToInputVec(inputM):
outputV = [[0 for row in range(0,4)] for col in range(0,4)]
outputV[1] = inputM[0]
outputV[2] = inputM[1]
outputV[0] = inputM[2]
outputV[3] = inputM[3]
output2V = [[0 for row in range(0,4)] for col in range(0,4)]
for i in range(4):
output2V[i][1] = outputV[i][0]
output2V[i][2] = outputV[i][1]
output2V[i][0] = outputV[i][2]
output2V[i][3] = outputV[i][3]
return np.array(output2V).flatten()
def graph():
pygame.init()
pygame.display.set_caption("John's GUI NN Project")
screenHeight = 700
screenWidth = 1000
DISPLAY=pygame.display.set_mode((screenWidth, screenHeight),0,32)
screen = pygame.display.get_surface()
WHITE=(255,255,255)
BLACK = (0,0,0)
BLUE=(0,0,255)
DISPLAY.fill(WHITE)
#GRAPH ACTIVATION
screen.blit(pygame.image.fromstring(actvRawData, actvCanvas.get_width_height(), "RGB"), (20,20))
#RENDER BLOCKS
btnDim = 60
positions = [[0 for row in range(0,4)] for col in range(0,4)]
colors = [[BLACK for row in range(0,4)] for col in range(0,4)]
color = BLACK
levels = [-35.0, 35.0, -105.0, 105.0]
for i in range(4):
for j in range(4):
positions[i][j] = (screenWidth/2 - btnDim/2 + levels[j], screenHeight/2 - btnDim/2 + levels[i], btnDim, btnDim)
button = pygame.Rect((screenWidth/2) - 50, screenHeight - 120, 100, 50)
#############################
clicked = [[0 for row in range(0,4)] for col in range(0,4)]
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mousePos = event.pos
if button.collidepoint(mousePos):
output = predict(matrixToInputVec(clicked))
print("Output2: ", output)
lossg.plot(lossHistory)
lossCanvas = agg.FigureCanvasAgg(lossFig)
lossCanvas.draw()
lossRenderer = lossCanvas.get_renderer()
lossRawData = lossRenderer.tostring_rgb()
screen.blit(pygame.image.fromstring(lossRawData, lossCanvas.get_width_height(), "RGB"), (1,screenHeight/2 - 120))
# DISPLAY OUTPUT
ouputText = pygame.font.SysFont(None, 28).render( (str(output[1])) + " : " + str(output[0]), True, (0, 0, 255), (255, 255, 255))
ouputTextRect = ouputText.get_rect()
ouputTextRect.centerx = screenWidth - 170
ouputTextRect.centery = screenHeight / 2
DISPLAY.blit(ouputText, ouputTextRect)
for i in range(4):
for j in range(4):
if pygame.Rect(positions[i][j]).collidepoint(mousePos):
color = BLUE
if clicked[i][j] == 0:
clicked[i][j] = 1
colors[i][j] = BLUE
else:
clicked[i][j] = 0
colors[i][j] = BLACK
for i in range(4):
for j in range(4):
pygame.draw.rect(DISPLAY, colors[i][j], positions[i][j])
pygame.draw.rect(screen, [55, 55, 0], button)
pygame.display.update()