-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNNScene.py
1980 lines (1376 loc) · 75.9 KB
/
NNScene.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import pygame
import pygame.gfxdraw
import ManagerObject
import NNNode
import Timer
from NNGlobals import *
import NNPixelArray
import NNModel
import NNXmlConnector
import colorsys
import math
import os
import numpy
# BASE CLASS
# noinspection PyPackageRequirements
class NNScene(object):
"""
NNScene CLASS (by sy)
THIS CLASS IS BASE CLASS FOR IMPLEMENTATION OF SCENE.
"""
# ENTER THE SCENE NUMBER TO DISPLAY THE NODES AND LAYERS
display_nn_visuals = {
"Weight_V": [5, 6, 7, 8, 9, 10, 11, 12, 13, 21, 24, 25, 26, 27, 28, 29, 30, 45, 48, 58, 59],
"Hidden_A": [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 21, 25, 26, 48, 50, 52, 53],
"Hidden_Relu": [5, 6, 13, 14, 21, 25, 26, 48, 50, 52, 53],
"Hidden_B": [5, 6, 13, 14, 15, 16, 17, 18, 21, 25, 26, 48, 50, 52, 53],
"Weight_W": [5, 17, 18, 21, 24, 25, 26, 27, 28, 45, 46, 47],
"Output_U": [5, 17, 18, 19, 20, 21, 25, 26, 46, 47, 50],
"Softmax": [5, 19, 20, 21, 22, 25, 26, 35, 41, 42, 46, 47, 50],
"Weight_v_newtrain": [49],
"Weight_w_newtrain": [49],
}
def __init__(self, surface):
"""
INITIALIZING SCENE OBJECT
:param surface: pygame'S SURFACE OBJECT
"""
# INITIALIZING STATE ID
self.stateID = ''
# SCENE NUMBER ** NOT THE INDEX OF SCENE OBJECT LIST !!!!
self.scene_number = -1
# pygame SURFACE DATA
# : DELIVERED FROM OUTSIDE
self.surface = surface
# GATE FOR UPDATE FUNCTION
# : THIS IS MAIN GATE FOR 'ALL PROCESSES' IN THE UPDATE FUNCTION
self.can_update = False
# INITIALIZING ObjectManager OBJECT
# : DELIVERING PYGAME SURFACE OBJECT
self.obj_manager = ManagerObject.ManagerObject(surface)
# THE VALUE FOR NNModel ENGINE
# : 'BLANK' DICTIONARY
self.the_value_dict = {}
# ELAPSED TIME FROM MAIN TIMELINE
# : ELAPSED TIME CALUE FOR ANIMATING
# (DELIVERED FROM OUTSIDE INTO update() FUNCTION IN THIS CLASS)
self.elapsed_time = 0
# TOTAL DURATION
self.duration = 0
# TEMPORARY the_value
self.value_now = 0
# FOR INTERACTION
self.click_left_occured = False
self.click_right_occured = False
# COLOR
self.textcolor_main = (160, 50, 90)
# NNModel
self.nnmodel = NNModel.NN.get_instance()
self.input_for_render_weight_v = ()
# XML CONNECTOR
self.xmlconnector = NNXmlConnector.NNXmlConnector('xml_scenes/scenes_xml.xml', self.surface)
self.count_scenes = len(self.xmlconnector.get_all_scene_layer())
# PROCESS PIPES
#self.p_receive_sub_to_main = None
#self.p_send_main_to_sub = None
# TEXT TO DISPLAY THE REALTIME PREDICTION IN SCENE 1
self.text_nn_result = None
# FOR NN MODEL
self.input_for_creating_pixels_weight_v = None
self.input_for_creating_pixels_hidden_a = None
self.input_for_creating_pixels_hidden_relu = None
self.input_for_creating_pixels_hidden_b = None
self.input_for_creating_pixels_output_u = None
self.input_for_creating_pixels_softmax = None
self.input_for_creating_pixels_weight_v_newtrain = None
self.input_for_creating_pixels_weight_w_newtrain = None
# VARIABLES FOR DISPLAYING NN TRAINING
# FLAG TO EXECUTE TRAINING WITH NN MODEL
self.train_flag = False
# self.epoch = None
# self.loss = None
# self.score = None
self.weight_v_new_train = None
self.weight_w_new_train = None
def setup(self, the_value_dict, duration):
self.duration = duration
def start(self):
"""
START NNAnim ENGINE
"""
# RESET OBJECT'S NNAnim ENGINE
# : IF ACTUAL OBJECTS IS CREATED...
if len(self.obj_manager.obj_list) is not 0:
# RESET AND START THE ANIMATION ENGINE
# FOR EVERY OBJECTS
for obj in self.obj_manager.obj_list:
obj.anim.reset()
obj.anim.start()
# GATE FOR update() FUNCTION 'OPENED' HERE
self.can_update = True
#####print(f"{getframeinfo(currentframe()).filename} -- {getframeinfo(currentframe()).lineno} NNScene::start() -- len(self.obj_manager.obj_list) -- {len(self.obj_manager.obj_list)}")
def update(self, elapsed_time):
"""
UPDATE FUNCTION
:param elapsed_time: ELAPSED TIME FROM OUTSIDE OF THIS OBJECT
"""
self.elapsed_time = elapsed_time
# MANAGING THE EVENT WITH MOUSE
self.click_left_occured = pygame.mouse.get_pressed(3)[0]
self.click_right_occured = pygame.mouse.get_pressed(3)[1]
#print(self.click_left_occured)
# TODO :: BELOW IS CRITICAL !
# UPDATING the_value_dict
self.exchange_value_dict()
if self.can_update is True:
self.obj_manager.update()
# IF THE KEY WAS PRESSED, COPY THE MOUSE POSITION TO CLIPBOARD !
keypressed = pygame.key.get_pressed()
if keypressed[pygame.K_LCTRL] and keypressed[pygame.K_c] and keypressed[pygame.K_LSHIFT]:
#####print("CTRL+SHIFT+C WAS PRESSED !!!!")
coord = str(pygame.mouse.get_pos()).replace("(", "").replace(")", "").replace(" ", "")
self.addToClipBoard(coord)
# -------------------------------------------------------------------------------------------
# < TRAINING PHASE VISUALIZATION >
#
# TODO :: SCENE NUMBER SHOULD BE CHANGED !
# TRAINING NN MODEL IF THE FLAG IS ON
if self.train_flag and self.scene_number >= 2:
self.nnmodel.train_to_visualize_only()
if self.nnmodel.next_flag:
# UPDATING TRAINING RESULT
# self.epoch = self.nnmodel.epoch
# self.loss = self.nnmodel.loss
# self.score = self.nnmodel.score
self.weight_v_new_train = self.nnmodel.V
self.weight_w_new_train = self.nnmodel.W
#print(self.nnmodel.next_flag)
#####print("IN SCENE DATA :: epoch = %d loss = %f score = %f" % (self.nnmodel.epoch, self.nnmodel.loss, self.nnmodel.score))
# IF self.epoch IS BIGGER THAN 1, WE CAN START TO UPDATE
# UPDATING PIXEL ARRAY OF TWO WEIGHTS (V AND W NEWLY TRAINED)
if self.nnmodel.epoch > 1 and \
self.weight_v_new_train is not None and \
self.weight_w_new_train is not None:
self.weights_new_train_update()
def render(self):
#print(f"{getframeinfo(currentframe()).filename} -- {getframeinfo(currentframe()).lineno} NNScene::render() -- ")
self.surface.fill(self.hsv2rgb(NNGlobals.SCENE_INDEX / self.count_scenes, 0.2, 0.8))
# < Check if element exists in list in Python >
# < How to Iterate Through a Dictionary in Python >
# https://realpython.com/iterate-through-dictionary-python/
# https://www.geeksforgeeks.org/check-if-element-exists-in-list-in-python/
for key in self.display_nn_visuals:
#print(self.display_nn_visuals[key])
if self.scene_number in self.display_nn_visuals[key]:
self.nodes_layers_render(key)
self.display_time_progressbar()
self.obj_manager.render()
# ADD THE DUMMY INPUT DATA 1.0 RECTANGLE
if self.scene_number >= 2:
self.add_input_dummy()
def add_input_dummy(self):
pygame.draw.rect(self.surface, (0, 0, 0), (20, NNGlobals.SCREEN_HEIGHT // 2 + 500 - 9, 18, 18))
def on_exit(self):
self.disable_update()
del self.obj_manager.obj_list[:]
#print(f"{getframeinfo(currentframe()).filename} -- {getframeinfo(currentframe()).lineno} NNScene::on_exit() -- self.can_update -- {self.can_update}")
#print(f"{getframeinfo(currentframe()).filename} -- {getframeinfo(currentframe()).lineno} NNScene::on_exit() -- len(self.obj_manager.obj_list) -- {len(self.obj_manager.obj_list)}")
# ----------------------------------------------------------------------
# BUTTON FUNCTIONS
def start_button(self):
""" THIS BUTTON IS FOR SCENE 1 ONLY !!! """
#####print(f"{getframeinfo(currentframe()).filename} -- {getframeinfo(currentframe()).lineno} NNScene::start_button() -- start FUNCTION IS EXECUTED")
# GLOBAL DEMO MODE OFF
NNGlobals.DEMO_MODE_NOW = False
NNGlobals.ALL_BUTTON_DISABLE = False
NNGlobals.TIMER.reset()
NNGlobals.SCENE_INDEX = 1
NNGlobals.TIMER.timeline_index = 1
NNGlobals.STATEMACHINE.changeState(NNGlobals.SCENE_INDEX)
# SEND MESSAGE TO SUB PROCESS !
# if self.p_send_main_to_sub is not None:
# self.p_send_main_to_sub.send('SCENE_CHANGED_TO_' + str(2))
NNGlobals.USER_RESULT = self.nnmodel.get_final_prediction(NNGlobals.USER_INPUT)
#####print(NNGlobals.USER_RESULT)
# TODO :: ARCHIVE BELOW !!!!
# ----------------------------------------------------------------------------------------
# < WRITING GAME RECORDS TO FILE >
# https://www.geeksforgeeks.org/reading-writing-text-files-python/
#
filename = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
file = open("GAME_RECORD/" + filename + ".txt", "w")
file.write("\n")
file.close()
# ADDING GAME COUNT
NNGlobals.GAME_COUNT += 1
# Program to show various ways to read and
# write data in a file.
file = open("GAME_RECORD/" + filename + ".txt", "w")
# DATA TO WRITIE
date = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
newline = ["GAME_COUNT : " + str(NNGlobals.GAME_COUNT)]
# \n is placed to indicate EOL (End of Line)
file.writelines(self.list_to_string(NNGlobals.USER_INPUT))
file.write("\n")
file.writelines(date)
file.write("\n")
file.writelines(newline)
file.write("\n")
file.close() # to change file access modes
def prev_button(self):
#####print(f'PREV BUTTON CLICKED -- {self.scene_number}')
#print(f' -- NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
if self.scene_number > 2:
# GLOBAL DEMO MODE OFF
NNGlobals.DEMO_MODE_NOW = False
NNGlobals.ALL_BUTTON_DISABLE = False
NNGlobals.TIMER.reset()
NNGlobals.SCENE_INDEX -= 2
NNGlobals.TIMER.timeline_index -= 2
NNGlobals.STATEMACHINE.changeState(NNGlobals.SCENE_INDEX)
# SEND MESSAGE TO SUB PROCESS !
# if self.p_send_main_to_sub is not None:
# self.p_send_main_to_sub.send('SCENE_CHANGED_TO_' + str(self.scene_number - 1))
else:
#print(f'** THERE IS NO NEXT SCENE TO MOVE ! ** NOW NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
pass
NNGlobals.USER_RESULT = self.nnmodel.get_final_prediction(NNGlobals.USER_INPUT)
#####print(NNGlobals.USER_RESULT)
def next_button(self):
#####print(f'NEXT BUTTON CLICKED -- {self.scene_number}')
#print(f' -- NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
if self.scene_number < self.count_scenes:
# GLOBAL DEMO MODE OFF
NNGlobals.DEMO_MODE_NOW = False
NNGlobals.ALL_BUTTON_DISABLE = False
NNGlobals.TIMER.reset()
NNGlobals.SCENE_INDEX += 2
NNGlobals.TIMER.timeline_index += 2
NNGlobals.STATEMACHINE.changeState(NNGlobals.SCENE_INDEX)
# SEND MESSAGE TO SUB PROCESS !
# if self.p_send_main_to_sub is not None:
# self.p_send_main_to_sub.send('SCENE_CHANGED_TO_' + str(self.scene_number + 1))
else:
#print(f'** THERE IS NO NEXT SCENE TO MOVE ! ** NOW NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
pass
NNGlobals.USER_RESULT = self.nnmodel.get_final_prediction(NNGlobals.USER_INPUT)
#####print(NNGlobals.USER_RESULT)
def reset_all_flow_button(self):
#####print(f'ALL RESET BUTTON CLICKED -- {self.scene_number}')
# SCENE JUMPING
NNGlobals.SCENE_INDEX = 0
# GLOBAL FLAGS RESET
NNGlobals.ALL_BUTTON_DISABLE = False
NNGlobals.DEMO_MODE_NOW = True
NNGlobals.USER_INPUT = []
NNGlobals.USER_RESULT = 0
# TIMER RESETTING
NNGlobals.TIMER.reset()
NNGlobals.TIMER.timeline_index = 0
NNGlobals.STATEMACHINE.changeState(NNGlobals.SCENE_INDEX)
# SEND MESSAGE TO SUB PROCESS !
# if self.p_send_main_to_sub is not None:
# self.p_send_main_to_sub.send('SCENE_CHANGED_TO_' + str(1))
# RESET PREVIOUS nnmodel'S PREDICTION
self.nnmodel.reset_prediction()
def prev_button_5(self):
#####print(f'PREV BUTTON CLICKED -- {self.scene_number}')
#print(f' -- NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
if self.scene_number > 2 and self.scene_number - 5 >= 2:
# GLOBAL DEMO MODE OFF
NNGlobals.DEMO_MODE_NOW = False
NNGlobals.ALL_BUTTON_DISABLE = False
NNGlobals.TIMER.reset()
NNGlobals.SCENE_INDEX -= 10
NNGlobals.TIMER.timeline_index -= 10
NNGlobals.STATEMACHINE.changeState(NNGlobals.SCENE_INDEX)
# SEND MESSAGE TO SUB PROCESS !
# if self.p_send_main_to_sub is not None:
# self.p_send_main_to_sub.send('SCENE_CHANGED_TO_' + str(self.scene_number - 5))
else:
#print(f'** THERE IS NO NEXT SCENE TO MOVE ! ** NOW NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
pass
NNGlobals.USER_RESULT = self.nnmodel.get_final_prediction(NNGlobals.USER_INPUT)
#####print(NNGlobals.USER_RESULT)
def next_button_5(self):
#####print(f'NEXT BUTTON CLICKED -- {self.scene_number}')
#print(f' -- NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
if self.scene_number < self.count_scenes and self.scene_number + 5 < self.count_scenes:
# GLOBAL DEMO MODE OFF
NNGlobals.DEMO_MODE_NOW = False
NNGlobals.ALL_BUTTON_DISABLE = False
NNGlobals.TIMER.reset()
NNGlobals.SCENE_INDEX += 10
NNGlobals.TIMER.timeline_index += 10
NNGlobals.STATEMACHINE.changeState(NNGlobals.SCENE_INDEX)
# SEND MESSAGE TO SUB PROCESS !
# if self.p_send_main_to_sub is not None:
# self.p_send_main_to_sub.send('SCENE_CHANGED_TO_' + str(self.scene_number + 5))
#
else:
#print(f'** THERE IS NO NEXT SCENE TO MOVE ! ** NOW NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
pass
NNGlobals.USER_RESULT = self.nnmodel.get_final_prediction(NNGlobals.USER_INPUT)
#####print(NNGlobals.USER_RESULT)
def prev_button_10(self):
#####print(f'PREV BUTTON CLICKED -- {self.scene_number}')
#print(f' -- NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
if self.scene_number > 2 and self.scene_number - 10 >= 2:
# GLOBAL DEMO MODE OFF
NNGlobals.DEMO_MODE_NOW = False
NNGlobals.ALL_BUTTON_DISABLE = False
NNGlobals.TIMER.reset()
NNGlobals.SCENE_INDEX -= 20
NNGlobals.TIMER.timeline_index -= 20
NNGlobals.STATEMACHINE.changeState(NNGlobals.SCENE_INDEX)
# SEND MESSAGE TO SUB PROCESS !
# if self.p_send_main_to_sub is not None:
# self.p_send_main_to_sub.send('SCENE_CHANGED_TO_' + str(self.scene_number - 10))
else:
#print(f'** THERE IS NO NEXT SCENE TO MOVE ! ** NOW NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
pass
NNGlobals.USER_RESULT = self.nnmodel.get_final_prediction(NNGlobals.USER_INPUT)
#####print(NNGlobals.USER_RESULT)
def next_button_10(self):
#####print(f'NEXT BUTTON CLICKED -- {self.scene_number}')
#print(f' -- NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
if self.scene_number < self.count_scenes and self.scene_number + 10 < self.count_scenes:
# GLOBAL DEMO MODE OFF
NNGlobals.DEMO_MODE_NOW = False
NNGlobals.ALL_BUTTON_DISABLE = False
NNGlobals.TIMER.reset()
NNGlobals.SCENE_INDEX += 20
NNGlobals.TIMER.timeline_index += 20
NNGlobals.STATEMACHINE.changeState(NNGlobals.SCENE_INDEX)
# SEND MESSAGE TO SUB PROCESS !
# if self.p_send_main_to_sub is not None:
# self.p_send_main_to_sub.send('SCENE_CHANGED_TO_' + str(self.scene_number + 10))
#
else:
#print(f'** THERE IS NO NEXT SCENE TO MOVE ! ** NOW NNGlobals.TIMER.timeline_index -- {NNGlobals.TIMER.timeline_index}')
pass
NNGlobals.USER_RESULT = self.nnmodel.get_final_prediction(NNGlobals.USER_INPUT)
#####print(NNGlobals.USER_RESULT)
# ----------------------------------------------------------------------
# DISPLAYING FUNCTIONS
def display_loading(self):
#self.surface.fill((170, 170, 170))
#surface = pygame.Surface((NNGlobals.SCREEN_WIDTH, NNGlobals.SCREEN_HEIGHT))
#self.surface.blit(surface, (0, 0))
# FONT NAME
font_name = 'GenEiChikugoMin2-R.ttf'
# SO, BELOW WILL CREATE THE FONT OBJECT
font = pygame.font.Font(font_name, 50)
textSurface = font.render('- LOADING -', True, (10,120,170))
textRect = textSurface.get_rect()
textRect.x = NNGlobals.SCREEN_WIDTH // 2 - textSurface.get_width() // 2
textRect.y = 40
self.surface.blit(textSurface, textRect)
def display_result_categories(self):
text_result_numbers = []
for i in range(10):
number = NNNode.NNNodeText(self.surface,
'txt_final_categories_' + str(i),
str(i),
(NNGlobals.SCREEN_WIDTH//2 - 900 + (200 * i), NNGlobals.SCREEN_HEIGHT//2 - 390),
(255, 255, 255),
50,
8000,
'Fixed',
0.5,
1.0,
True)
text_result_numbers.append(number)
self.obj_manager.obj_list.append(number)
def display_time_progressbar(self):
# DRAW ELAPSED TIME GAUGE
pygame.draw.line(self.surface, (255, 0, 0), (0, 5), (int(NNGlobals.SCREEN_WIDTH * (self.elapsed_time / self.duration)), 5), 2)
#print( self.duration )
# < DISPLAYING INPUT LAYER >
# : 3 STEPS --> THE COMBINATIONS OF ABOVE FUNCTIONS
#
def display_input_layer(self, count, the_value_dict):
# CREATING OBJECTS
self.drawpad_create(count, 18, True, 10, the_value_dict)
# POSITIONING OBJECTS
self.drawpad_spread(count, 18, the_value_dict)
#print(f"{getframeinfo(currentframe()).filename} -- {getframeinfo(currentframe()).lineno} NNScene_2::setup() -- {self.the_value_dict}")
# SETTING TEXT-OBJECTS WITH the_value
self.set_text_visible_with_value()
def drawpad_spread(self, count, size, the_value_dict):
# CONSTRUCT DRAWING PAD
drawpad_size = count
drawpad_button_size = size
# < ABOUT ERROR 'TypeError: 'tuple' object does not support item assignment' >
# https://stackoverflow.com/questions/7735838/typeerror-tuple-object-does-not-support-item-assignment-when-swapping-values
# : Evaluating "1,2,3" results in (1, 2, 3), a tuple. As you've discovered, tuples are immutable. Convert to a list before processing.
#
# CONVERTING LIST TO TUPLE
# https://www.geeksforgeeks.org/python-convert-a-list-into-a-tuple/
# POSITIONING OF DRAWPAD BUTTONS
gap_for_dummy = 15
center_position = [NNGlobals.SCREEN_WIDTH // 2, NNGlobals.SCREEN_HEIGHT // 2] # LIST TYPE
left_top_position = [center_position[0] - (drawpad_button_size * drawpad_size * 6.9) + gap_for_dummy, center_position[1] + 500] # LIST TYPE
# FOR EVERY OBJECTS...
for i in range(len(self.obj_manager.obj_list)):
# ADJUSTING HORIZONTAL POSITIONS
modified_position = [left_top_position[0] + (drawpad_button_size * (i/1.433)), left_top_position[1]] # LIST TYPE
self.obj_manager.obj_list[i].position = tuple(modified_position) # TUPLE TYPE
# FOR THE OBJECTS WHICH IS NOT THE 'TEXT'...
if type(self.obj_manager.obj_list[i]) is not NNNode.NNNodeText and \
type(self.obj_manager.obj_list[i]) is not NNNode.NNNodeMultiText and \
type(self.obj_manager.obj_list[i]) is not NNNode.NNNodeIndicator and \
type(self.obj_manager.obj_list[i]) is not NNNode.NNNodeButton:
self.obj_manager.obj_list[i].the_value = the_value_dict[self.obj_manager.obj_list[i].obj_id]
else:
self.obj_manager.obj_list[i].the_value = -1
# < SEARCHING OBJECTS USING ID (STRING) >
#
# BUILT-IN OBJECTS ARE THE NUMBERS OF WEIGHTS PLACED ABOVE THE INPUT NODES
#
# IF THERE ARE BUILT-IN TEXT OBJECTS,
# RE-POSITION IT
got_id = self.obj_manager.obj_list[i].obj_id
# ACCORDING TO ABOVE SETTING ABOUT OBJECT ID,
# IF THE ID INCLUDES 'txt_', RE-POSITION IT
trimmed_id = got_id[:4]
# IF THE ID IS STARTING FROM 'txt_'...
if trimmed_id == 'txt_':
##print(got_id)
# RE-POSITION
repositioned = list(self.obj_manager.obj_list[i].position) # TUPLE TO LIST
repositioned = [repositioned[0] - drawpad_button_size - 5, repositioned[1] - drawpad_button_size - 5] # LIST
self.obj_manager.obj_list[i].position = tuple(repositioned) # CONVERTED TO TUPLE
self.obj_manager.obj_list[i].set_style('0', drawpad_button_size * 2, self.obj_manager.obj_list[i].color)
def visible_texts(self):
for i in range(len(self.obj_manager.obj_list)):
# TODO :: FIX THE DUPLICATION BELOW
# IF THERE ARE BUILT-IN TEXT OBJECTS,
got_id = self.obj_manager.obj_list[i].obj_id
# ACCORDING TO ABOVE SETTING ABOUT OBJECT ID,
# IF THE ID INCLUDES 'txt_', RE-POSITION IT
trimmed_id = got_id[:4]
if trimmed_id is 'txt_':
##print(got_id)
# RE-POSITION
self.obj_manager.obj_list[i].visible = True
# ----------------------------------------------------------------------
# CREATION FUNCTIONS
def create_scene_index(self):
scene_num_now = NNNode.NNNodeText(self.surface,
'scene_num_text',
str(self.scene_number) + " / " + str(self.count_scenes),
(NNGlobals.SCREEN_WIDTH // 2, NNGlobals.SCREEN_HEIGHT // 2 - 474),
(90, 90, 90),
25,
8000,
'Fixed',
0.5,
1.0,
True)
self.obj_manager.obj_list.append(scene_num_now)
def create_nav_buttons(self):
margin = 100
# NAV BUTTONS
if self.scene_number is 2:
self.create_nav_next_button('Btn_Next',
(NNGlobals.SCREEN_WIDTH // 2 , 50),
30,
20,
self.next_button)
# FOR ALL OTHER SCENES
else:
self.create_nav_prev_button('Btn_Prev',
(NNGlobals.SCREEN_WIDTH // 2 - margin, 50),
30,
20,
self.prev_button)
self.create_nav_next_button('Btn_Next',
(NNGlobals.SCREEN_WIDTH // 2 + margin, 50),
30,
20,
self.next_button)
self.create_nav_reset_button('Btn_Reset',
(60, 50),
20,
15,
self.reset_all_flow_button)
self.create_nav_prev_button('Btn_Prev_5',
(NNGlobals.SCREEN_WIDTH // 2 - margin*2, 50),
30,
20,
self.prev_button_5)
self.create_nav_next_button('Btn_Next_5',
(NNGlobals.SCREEN_WIDTH // 2 + margin*2, 50),
30,
20,
self.next_button_5)
self.create_nav_prev_button('Btn_Prev_10',
(NNGlobals.SCREEN_WIDTH // 2 - margin*4, 50),
30,
20,
self.prev_button_10)
self.create_nav_next_button('Btn_Next_10',
(NNGlobals.SCREEN_WIDTH // 2 + margin*4, 50),
30,
20,
self.next_button_10)
# TODO :: MAKE BELOW TO NEW CLASS !!!!
# TO DIFFICULT TO GET AND USE THE ATTRIBUTES !
# EX :: GETTING THE AREA EASILY
def drawpad_create(self, count, size, clickable, anim_time, value_dict):
# the_value_dict
the_value_dict = value_dict
# CONSTRUCT DRAWING PAD
drawpad_size = count
drawpad_button_size = size
# POSITIONING OF DRAWPAD BUTTONS
center_position = [NNGlobals.SCREEN_WIDTH//2,
NNGlobals.SCREEN_HEIGHT//2]
left_top_position = [center_position[0]-(drawpad_button_size*4.5),
center_position[1]-(drawpad_button_size*4.5)]
# TIME ATTRIBUTES
duration = anim_time
object_state = 'Fixed'
xscaler = 0.005
yscaler = 300
# CREATING OBJECTS
# WITH TEXT OBJECTS !
#
# BE NOTED !
# : BELOW'S INDEX IS 'j -> i'
#
# BECAUSE WE NEED THIS |
# v
# ( HORIZONTAL WAY 0 1 2 3 4
# 5 6 7 8 9 )
for j in range(drawpad_size):
for i in range(drawpad_size):
obj_id = 'obj' + '_' + str(i) + '_' + str(j)
text_obj_id = 'txt_' + obj_id
# BASE POSITION
position = [left_top_position[0] + (drawpad_button_size * i),
left_top_position[1] + (drawpad_button_size * j)]
# PREPARING the_value
if len(the_value_dict) is not 0:
# CONVERTING LIST TO SET
the_value_dict_set = set(list(the_value_dict.keys()))
# CHECKING THE EXISTENCE OF MEMBER IN THE LIST
# IF OBJECT'S ID IS MATCHED...
if obj_id in the_value_dict_set:
# the_value SHOULD BE SAME AS LIST
self.value_now = the_value_dict[obj_id]
else:
#print(f'{obj_id} IS DOES NOT EXISTS !!!')
# IF THERE IS NO MATCHES...
self.value_now = 0
else:
# IF the_value_list HAS NO LENGTH...
self.value_now = 0
# NODE CREATION
self.obj_manager.obj_list.append(NNNode.NNNodeObject(self.surface,
obj_id,
'Rectangle',
position,
(0, 0),
drawpad_button_size,
duration,
object_state,
xscaler,
yscaler,
clickable,
self.value_now
))
# BUILT-IN TEXT OBJECT CREATION
text_object_position = [position[0], position[1] - drawpad_button_size]
self.obj_manager.obj_list.append(NNNode.NNNodeText(self.surface,
text_obj_id,
'OK',
text_object_position,
self.textcolor_main,
drawpad_button_size,
duration,
object_state,
xscaler,
yscaler,
False))
def create_nav_prev_button(self, btn_prev_id, prev_position, prev_size_w, prev_size_h, prev_callback):
# CREATING BUTTON TO NAVIGATE
self.obj_manager.obj_list.append(NNNode.NNNodeButton(self.surface,
prev_callback,
btn_prev_id,
'Left_Triangle',
prev_position,
(0, 0),
prev_size_w,
prev_size_h,
500,
'Fixed',
0.005,
300,
True,
0
))
def create_nav_next_button(self, btn_next_id, next_position, next_size_w, next_size_h, next_callback):
# CREATING BUTTON TO NAVIGATE
self.obj_manager.obj_list.append(NNNode.NNNodeButton(self.surface,
next_callback,
btn_next_id,
'Right_Triangle',
next_position,
(0, 0),
next_size_w,
next_size_h,
500,
'Fixed',
0.005,
300,
True,
0
))
def create_nav_reset_button(self, btn_prev_id, prev_position, prev_size_w, prev_size_h, prev_callback):
# CREATING BUTTON TO NAVIGATE
self.obj_manager.obj_list.append(NNNode.NNNodeButton(self.surface,
prev_callback,
btn_prev_id,
'Left_Triangle',
prev_position,
(0, 0),
prev_size_w,
prev_size_h,
500,
'Fixed',
0.005,
300,
True,
0
))
txt = NNNode.NNNodeText(self.surface,
'text_reset_button',
"最初に戻る",
(150, 50),
(100, 100, 100),
25,
8000,
'Fixed',
0.5,
1.0,
True)
self.obj_manager.obj_list.append(txt)
# ----------------------------------------------------------------------
# IMPORTING AND DISPLAYING DATA FROM NNModel
def update_nn_data(self, dict_for_input, weight_array):
""" THIS FUNCTION CREATING AREA TO DISPLAY NN INFORMATION """
area_w = dict_for_input["areaWidth"]
area_h = dict_for_input["areaHeight"]
margin_w = dict_for_input["marginWidth"]
margin_h = dict_for_input["marginHeight"]
pos_diff_from_center_x = dict_for_input["posAdjustX"]
pos_diff_from_center_y = dict_for_input["posAdjustY"]
weight_color = dict_for_input["Color"]
# GETTING VALUES FROM NNModel (101 x 128)
weight_data = weight_array
# ---------------------------TEST-------------------------
# area_w = 500
# area_h = 300
# margin_w = 10
# margin_h = 10
# pos_diff_from_center_x = 0
# pos_diff_from_center_y = 0
# weight_color = dict_for_input["Color"]
#weight_data = np.full((4,10), 0.3)
# arr1 = np.arange(0.1, 1.1, 0.1)
#
# weight_data = []
# for i in range(4):
# weight_data.append(arr1)
# weight_data = [
# [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.5, 0.4, 0.3, 0.2],
# [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.5, 0.4, 0.3, 0.2],
# [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.5, 0.4, 0.3, 0.2],
# [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.5, 0.4, 0.3, 0.2],
# ]
#weight_data = np.array(weight_data).T
# ---------------------------TEST-------------------------