-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNARSGUI.py
1112 lines (932 loc) · 47.8 KB
/
NARSGUI.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
from math import sin
from tkinter import filedialog
import Config
import NALSyntax
import NARSDataStructures
import NARSMemory
import NALInferenceRules
from PIL import Image, ImageTk, ImageOps
import Global
import tkinter as tk
import numpy as np
import NALGrammar.Terms
"""
GUI code
Runs on its own separate process, and communicate with the NARS process using commands.
"""
class NARSGUI:
# Interface vars
gui_output_textbox = None # primary output gui
gui_working_cycle_duration_slider = None # duration slider
gui_total_cycles_lbl = None
gui_total_cycles_stringvar = None
gui_play_pause_button = None
gui_show_atomic_concepts = False
# Internal Data vars
# listboxes
gui_narsese_buffer_listbox = None # output for tasks in global buffer
gui_vision_buffer_listbox = None
gui_memory_listbox = None # output for concepts in memory bag
gui_temporal_module_listbox = None # output for tasks in event buffer
# arrays
gui_global_buffer_full_contents = []
gui_memory_full_contents = []
gui_event_buffer_full_contents = []
# labels
gui_temporal_module_output_label = None
gui_narsese_buffer_output_label = None
gui_vision_buffer_output_label = None
gui_concepts_bag_output_label = None
# dictionary of data structure name to listbox
dict_listbox_from_id = {}
gui_object_pipe = None # two-way object request communication
gui_string_pipe = None # one way string communication
# use GUI?
gui_use_interface = None
# Keys
KEY_STRING = "String"
KEY_TRUTH_VALUE = "TruthValue"
KEY_TIME_PROJECTED_TRUTH_VALUE = "TimeProjectedTruthValue"
KEY_EXPECTATION = "ProjectedExpectation"
KEY_IS_ARRAY = "IsArray"
KEY_STRING_NOID = "StringNoID"
KEY_ID = "ID"
KEY_OCCURRENCE_TIME = "OccurrenceTime"
KEY_SENTENCE_TYPE = "SentenceType"
KEY_DERIVED_BY = "DerivedBy"
KEY_PARENT_PREMISES = "ParentPremises"
KEY_LIST_EVIDENTIAL_BASE = "ListEvidentialBase"
KEY_LIST_INTERACTED_SENTENCES = "ListInteractedSentences"
KEY_ARRAY_IMAGE = "ArrayImage"
KEY_ARRAY_ELEMENT_STRINGS = "ArrayElementStrings"
KEY_KEY = "Key"
KEY_CLASS_NAME = "ClassName"
KEY_OBJECT_STRING = "ObjectString"
KEY_TERM_TYPE = "TermType"
KEY_IS_POSITIVE = "IsPositive"
KEY_PASSES_DECISION = "PassesDecision"
KEY_LIST_BELIEFS = "ListBeliefs"
KEY_LIST_DESIRES = "ListDesires"
KEY_LIST_TERM_LINKS = "ListTermLinks"
KEY_LIST_PREDICTION_LINKS = "ListPredictionLinks"
KEY_LIST_EXPLANATION_LINKS = "ListExplanationLinks"
KEY_CAPACITY_BELIEFS = "CapacityBeliefs"
KEY_CAPACITY_DESIRES = "CapacityDesires"
KEY_CAPACITY_TERM_LINKS = "CapacityTermLinks"
KEY_CAPACITY_PREDICTION_LINKS = "CapacityPredictionLinks"
KEY_CAPACITY_EXPLANATION_LINKS = "CapacityExplanationLinks"
KEY_SENTENCE_STRING = "SentenceString"
def __init__(self):
pass
def print_to_output(self, msg, data_structure_info=None, length=0):
"""
Print a message to an output GUI box
"""
if data_structure_info is None:
# output to interface or shell
if self.gui_use_interface:
self.gui_output_textbox.configure(state="normal")
self.gui_output_textbox.insert(tk.END, msg + "\n")
self.gui_output_textbox.configure(state="disabled")
return
listbox = None
if data_structure_info is not None and data_structure_info[0] in self.dict_listbox_from_id:
data_structure_id, data_structure_name = data_structure_info
listbox = self.dict_listbox_from_id[data_structure_id]
else:
assert False, 'ERROR: Data structure name invalid ' + str(data_structure_info)
# internal data output
# insert item sorted by priority
if listbox is self.gui_temporal_module_listbox:
idx_to_insert = tk.END
else:
string_list = listbox.get(0, tk.END) # get all items in the listbox
msg_priority = NARSGUI.get_priority_from_string(msg)
idx_to_insert = tk.END # by default insert at the end
i = 0
for row in string_list:
row_priority = NARSGUI.get_priority_from_string(row)
if msg_priority > row_priority:
idx_to_insert = i
break
i += 1
if listbox is self.gui_memory_listbox:
self.gui_memory_full_contents.insert(
len(self.gui_memory_full_contents) if idx_to_insert == tk.END else idx_to_insert, msg)
if NARSGUI.is_statement_string(msg) or self.gui_show_atomic_concepts:
listbox.insert(idx_to_insert, msg)
elif listbox is self.gui_temporal_module_listbox:
self.gui_event_buffer_full_contents.insert(
len(self.gui_event_buffer_full_contents) if idx_to_insert == tk.END else idx_to_insert, msg)
listbox.insert(idx_to_insert, msg)
elif listbox is self.gui_narsese_buffer_listbox:
self.gui_global_buffer_full_contents.insert(
len(self.gui_global_buffer_full_contents) if idx_to_insert == tk.END else idx_to_insert, msg)
listbox.insert(idx_to_insert, msg)
self.update_datastructure_labels(data_structure_info, length=length)
@classmethod
def get_priority_from_string(cls, msg):
return float(msg[msg.find(NALSyntax.StatementSyntax.BudgetMarker.value) + 1:msg.find(
NALSyntax.StatementSyntax.ValueSeparator.value)])
@classmethod
def is_statement_string(cls, msg):
return NALSyntax.Copula.contains_top_level_copula(msg) or NALSyntax.TermConnector.contains_higher_level_connector(msg)
def remove_from_output(self, msg, data_structure_info=None, length=0):
"""
Remove a message from an output GUI box
"""
if data_structure_info is not None and data_structure_info[0] in self.dict_listbox_from_id:
data_structure_id, data_structure_name = data_structure_info
listbox = self.dict_listbox_from_id[data_structure_id]
else:
assert False, 'ERROR: Data structure name invalid ' + str(data_structure_info)
msg_id = msg[len(Global.Global.MARKER_ITEM_ID):msg.rfind(
Global.Global.MARKER_ID_END)] # assuming ID is at the beginning, get characters from ID: to first spacebar
if listbox is self.gui_memory_listbox:
# if memory listbox, non-statement concept
# remove it from memory contents
for i,row in enumerate(self.gui_memory_full_contents):
row_id = row[len(Global.Global.MARKER_ITEM_ID):row.rfind(Global.Global.MARKER_ID_END)]
if msg_id == row_id:
break
del self.gui_memory_full_contents[i]
# if non-statement and not showing non-statements, don't bother trying to remove it from memory GUI output
if not NARSGUI.is_statement_string(msg) and not self.gui_show_atomic_concepts: return
string_list = listbox.get(0, tk.END)
idx_to_remove = -1
i = 0
for row in string_list:
row_id = row[len(Global.Global.MARKER_ITEM_ID):row.rfind(Global.Global.MARKER_ID_END)]
if msg_id == row_id:
idx_to_remove = i
break
i = i + 1
if idx_to_remove == -1:
assert False, "GUI Error: cannot find msg to remove: " + msg
listbox.delete(idx_to_remove)
contents = None
if listbox is self.gui_narsese_buffer_listbox:
contents = self.gui_global_buffer_full_contents
elif listbox is self.gui_memory_listbox:
contents = None # already removed at beginning of function
elif listbox is self.gui_temporal_module_listbox:
contents = self.gui_event_buffer_full_contents
if contents is not None: contents.pop(idx_to_remove)
self.update_datastructure_labels(data_structure_info, length=length)
def update_datastructure_labels(self, data_structure_info, length=0):
assert data_structure_info is not None, "Cannot update label for Null data structure!"
data_structure_id, data_structure_name = data_structure_info
label_txt = ""
label = None
listbox = self.dict_listbox_from_id[data_structure_id]
if listbox is self.gui_memory_listbox:
label_txt = "Memory - Concepts "
label = self.gui_concepts_bag_output_label
elif listbox is self.gui_temporal_module_listbox:
label = self.gui_temporal_module_output_label
elif listbox is self.gui_narsese_buffer_listbox:
label_txt = "Narsese "
label = self.gui_narsese_buffer_output_label
if label is None: return
label.config(
text=(label_txt + data_structure_name + ": " + str(length) + " / " + str(
self.dict_listbox_from_id[data_structure_id + "capacity"])))
def clear_listbox(self, listbox=None):
listbox.delete(0, tk.END)
def toggle_show_atomic_concepts(self):
"""
Toggles showing atomic concepts in the memory listbox
:return:
"""
self.gui_show_atomic_concepts = not self.gui_show_atomic_concepts
self.clear_listbox(self.gui_memory_listbox)
if self.gui_show_atomic_concepts:
for concept_string in self.gui_memory_full_contents:
self.gui_memory_listbox.insert(tk.END, concept_string)
else:
self.clear_listbox(self.gui_memory_listbox)
for concept_string in self.gui_memory_full_contents:
if NARSGUI.is_statement_string(concept_string):
self.gui_memory_listbox.insert(tk.END, concept_string)
def execute_gui(self, gui_use_interface, data_structure_IDs, data_structure_capacities, pipe_gui_objects,
pipe_gui_strings):
"""
Setup and run 2 windows on a single thread
"""
self.gui_object_pipe = pipe_gui_objects
self.gui_string_pipe = pipe_gui_strings
self.gui_use_interface = gui_use_interface
# launch internal data GUI
window = tk.Tk()
window.title("NARS in Python - Interface")
window.geometry("1900x700")
self.execute_main_interface_gui(window, data_structure_IDs, data_structure_capacities)
# main GUI loop
def handle_pipes(self):
while self.gui_string_pipe.poll(): # check if there are messages to be received
(command, msg, data_structure_info, data_structure_length) = self.gui_string_pipe.recv()
if command == "print":
self.print_to_output(msg=msg,
data_structure_info=data_structure_info,
length=data_structure_length)
elif command == "remove":
self.remove_from_output(msg=msg,
data_structure_info=data_structure_info,
length=data_structure_length)
elif command == "clear":
self.clear_listbox(data_structure_id=data_structure_info)
elif command == "paused":
self.set_paused(msg)
elif command == "cycles":
self.gui_total_cycles_stringvar.set(msg)
else:
assert False, "ERROR: INCORRECT COMMAND!"
window.after(1, handle_pipes, self)
window.after(1, handle_pipes, self)
pipe_gui_objects.send('ready')
window.mainloop()
def set_paused(self, paused):
"""
Sets the Global paused parameter and changes the GUI button
Does nothing if GUI is not enabled
"""
if not self.gui_use_interface: return
self.paused = paused
if self.paused:
self.gui_play_pause_button.config(text="PLAY")
else:
self.gui_play_pause_button.config(text="PAUSE")
def toggle_paused(self):
"""
Sets the Global paused parameter and changes the GUI button
Does nothing if GUI is not enabled
"""
self.set_paused(not self.paused)
self.gui_string_pipe.send(("paused", self.paused))
def execute_main_interface_gui(self, window, data_structure_IDs, data_structure_capacities):
"""
Setup the interface GUI window, displaying the system's components
"""
output_width = 3
output_height = 4
# row 0
output_lbl = tk.Label(window, text="Output: ")
output_lbl.grid(row=0, column=0, columnspan=output_width)
# row 1
output_scrollbar = tk.Scrollbar(window)
output_scrollbar.grid(row=1, column=3, rowspan=output_height, sticky='ns')
self.gui_output_textbox = tk.Text(window, height=35, width=75, yscrollcommand=output_scrollbar.set)
self.gui_output_textbox.grid(row=1, column=0, columnspan=output_width, rowspan=output_height)
self.gui_output_textbox.configure(state="disabled")
# row 2
self.gui_total_cycles_stringvar = tk.StringVar()
self.gui_total_cycles_stringvar.set("Cycle #0")
self.gui_total_cycles_lbl = tk.Label(window, textvariable=self.gui_total_cycles_stringvar)
self.gui_total_cycles_lbl.grid(row=1, column=4, columnspan=2, sticky='n')
speed_slider_lbl = tk.Label(window, text="Working Cycle Duration in ms: ")
speed_slider_lbl.grid(row=2, column=4, columnspan=2, sticky='s')
self.gui_play_pause_button = tk.Button(window, text="", command=self.toggle_paused)
self.gui_play_pause_button.grid(row=3, column=4, sticky='s')
max_slider = 1000 # in milliseconds
min_slider = 50 # in milliseconds
gui_duration_slider = tk.Scale(window, from_=max_slider, to=min_slider)
self.gui_working_cycle_duration_slider = gui_duration_slider
self.gui_working_cycle_duration_slider.grid(row=3, column=5, sticky='ns')
strings_pipe = self.gui_string_pipe
def slider_changed(event=None):
strings_pipe.send(("duration", gui_duration_slider.get()))
self.gui_working_cycle_duration_slider.bind('<ButtonRelease-1>', slider_changed)
self.gui_working_cycle_duration_slider.set(min_slider)
slider_changed() # set delay to max value
checkbutton = tk.Checkbutton(window, text='Show atomic concepts', onvalue=1,
offvalue=0, command=self.toggle_show_atomic_concepts)
checkbutton.grid(row=4, column=5)
# input GUI
def input_clicked(event=None):
"""
Callback when clicking button to send input
"""
# put input into NARS input buffer
userinput = input_field.get(index1=1.0, index2=tk.END)
strings_pipe.send(("userinput", userinput)) # send user input to NARS
input_field.delete(1.0, tk.END) # empty input field
input_lbl = tk.Label(window, text="Input: ")
input_lbl.grid(column=0, row=1 + output_height)
input_field = tk.Text(window, width=50, height=4)
input_field.grid(column=1, row=1 + output_height)
input_field.focus()
send_input_btn = tk.Button(window, text="Send input.", command=input_clicked) # send input when click button
send_input_btn.grid(column=2, row=1 + output_height)
listbox_height = 40
listbox_width = 80
((narsese_buffer_ID, _), (vision_buffer_ID, _), (temporal_module_ID, _), (memory_bag_ID, _)) = data_structure_IDs
"""
Event buffer internal contents GUI
"""
row = 0
column = 6
self.gui_temporal_module_output_label = tk.Label(window, borderwidth=1, relief="solid")
self.gui_temporal_module_output_label.grid(row=row, column=column, sticky='w')
row += 1
buffer_scrollbar = tk.Scrollbar(window)
buffer_scrollbar.grid(row=row, column=column + 1, sticky='ns')
self.gui_temporal_module_listbox = tk.Listbox(window,
height=listbox_height // 3,
width=listbox_width, font=('', 8),
yscrollcommand=buffer_scrollbar.set)
self.gui_temporal_module_listbox.grid(row=row, column=column, columnspan=1)
self.dict_listbox_from_id[temporal_module_ID] = self.gui_temporal_module_listbox
"""
Global Buffer internal contents GUI
"""
row += 1
self.gui_narsese_buffer_output_label = tk.Label(window,borderwidth=1, relief="solid")
self.gui_narsese_buffer_output_label.grid(row=row,
column=column,
sticky='w')
row += 1
buffer_scrollbar = tk.Scrollbar(window)
buffer_scrollbar.grid(row=row,
column=column + 1,
sticky='ns')
self.gui_narsese_buffer_listbox = tk.Listbox(window,
height= 2* listbox_height // 3,
width=listbox_width,
font=('', 8),
yscrollcommand=buffer_scrollbar.set)
self.gui_narsese_buffer_listbox.grid(row=row,
column=column,
columnspan=1)
self.dict_listbox_from_id[narsese_buffer_ID] = self.gui_narsese_buffer_listbox
"""
Memory internal contents GUI
"""
row -= 3
self.gui_concepts_bag_output_label = tk.Label(window, borderwidth=1, relief="solid")
self.gui_concepts_bag_output_label.grid(row=row,
column=column + 3,
sticky='w')
row += 1
column += 3
concept_bag_scrollbar = tk.Scrollbar(window)
concept_bag_scrollbar.grid(row=row,
column=column + 1,
rowspan=6,
sticky='ns')
self.gui_memory_listbox = tk.Listbox(window,
height=listbox_height,
width=listbox_width,
font=('', 8),
yscrollcommand=concept_bag_scrollbar.set)
self.gui_memory_listbox.grid(row=row,
column=column,
columnspan=1,
rowspan=6)
self.dict_listbox_from_id[memory_bag_ID] = self.gui_memory_listbox
# define callbacks when clicking items in any box
self.gui_memory_listbox.bind("<<ListboxSelect>>", self.listbox_datastructure_item_click_callback)
for i, (id, name) in enumerate(data_structure_IDs):
self.dict_listbox_from_id[id + "capacity"] = data_structure_capacities[i]
if i == len(data_structure_IDs) - 1:
# final name is the memory
length = 1 # system starts with self concept
else:
length = 0
if id != vision_buffer_ID:
self.update_datastructure_labels((id, name), length)
window.focus()
def listbox_sentence_item_click_callback(self, event):
selection = event.widget.curselection()
if selection:
index = selection[0]
sentence_string = event.widget.get(index)
self.gui_object_pipe.send(("getsentence", sentence_string, None))
rcv = self.gui_object_pipe.recv()
if rcv is not None:
(object, content) = rcv
if object == "sentence":
self.draw_sentence_internal_data(content)
elif object == "concept":
self.draw_concept_internal_data(content)
def listbox_concept_item_click_callback(self, event):
selection = event.widget.curselection()
if selection:
index = selection[0]
concept_term_string = event.widget.get(index)
self.gui_object_pipe.send(("getconcept", concept_term_string, None))
concept_item = self.gui_object_pipe.recv()
if concept_item is not None:
self.draw_concept_internal_data(concept_item)
def listbox_datastructure_item_click_callback(self, event):
"""
Presents a window describing the clicked data structure's item's internal data.
Locks the interface until the window is closed.
This function is called when the user clicks on an item in the internal data.
"""
selection = event.widget.curselection()
if selection:
index = selection[0]
item_string = event.widget.get(index)
data_structure_name = ""
if event.widget is self.gui_memory_listbox:
ID = item_string[ item_string.rfind(Global.Global.MARKER_ID_END) + len(Global.Global.MARKER_ID_END):item_string.rfind(
NALSyntax.StatementSyntax.End.value) + 1] # extract term string
data_structure_name = self.get_data_structure_name_from_listbox(self.gui_memory_listbox)
else:
# clicked concept within another concept
ID = item_string
data_structure_name = self.get_data_structure_name_from_listbox(self.gui_memory_listbox)
self.gui_object_pipe.send(("getitem", ID, data_structure_name))
item = self.gui_object_pipe.recv()
if item is None:
self.print_to_output("ERROR: could not get item with key: " + ID)
return
classname = item[NARSGUI.KEY_CLASS_NAME]
assert classname == NARSMemory.Concept.__name__ or classname == NARSDataStructures.Other.Task.__name__, "ERROR: Data Structure clickback only defined for Concept and Task"
if classname == NARSMemory.Concept.__name__:
self.draw_concept_internal_data(item)
elif classname == NARSDataStructures.Other.Task.__name__:
# window
item_info_window = tk.Toplevel()
item_info_window.title(classname + " Internal Data: " + item_string)
item_info_window.geometry('1100x700')
# item_info_window.grab_set() # lock the other windows until this window is exited
row = 0
column = 0
# name
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label=classname + " Name: ",
value_label=item[NARSGUI.KEY_OBJECT_STRING])
# term type
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Term Type: ",
value_label=item[NARSGUI.KEY_TERM_TYPE])
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=0,
key_label="Sentence: ",
value_label=item[NARSGUI.KEY_SENTENCE_STRING])
row += 1
label = tk.Label(item_info_window, text="", justify=tk.LEFT)
label.grid(row=row, column=column)
# Listboxes
row += 1
# Evidential base listbox
create_clickable_listbox(parent=item_info_window,
row=row,
column=column,
title_label="Sentence Evidential Base",
listbox_contents=item[NARSGUI.KEY_LIST_EVIDENTIAL_BASE],
content_click_callback=self.listbox_sentence_item_click_callback)
# Interacted sentences listbox
column += 2
create_clickable_listbox(parent=item_info_window,
row=row,
column=column,
title_label="Sentence Interacted Sentences",
listbox_contents=item[NARSGUI.KEY_LIST_INTERACTED_SENTENCES],
content_click_callback=self.listbox_sentence_item_click_callback)
def draw_concept_internal_data(self, item):
# window
classname = item[NARSGUI.KEY_CLASS_NAME]
item_info_window = tk.Toplevel()
item_info_window.title(classname + "Internal Data: " + item[NARSGUI.KEY_OBJECT_STRING])
item_info_window.geometry('1100x700')
# item_info_window.grab_set() # lock the other windows until this window is exited
row = 0
column = 0
# name
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label=classname + " Name: ",
value_label=item[NARSGUI.KEY_OBJECT_STRING])
# term type
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Term Type: ",
value_label=item[NARSGUI.KEY_TERM_TYPE])
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Expectation: ",
value_label=item[NARSGUI.KEY_EXPECTATION])
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Is Positive? ",
value_label=item[NARSGUI.KEY_IS_POSITIVE])
if item[NARSGUI.KEY_PASSES_DECISION] is not None:
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Passes Decision-Making Rule? ",
value_label=item[NARSGUI.KEY_PASSES_DECISION])
row += 1
if classname == NARSMemory.Concept.__name__:
label = tk.Label(item_info_window, text="", justify=tk.LEFT)
label.grid(row=row, column=column)
# Row 1
# beliefs table listbox
row += 1
beliefs_capacity_text = "Beliefs (" + str(len(item[NARSGUI.KEY_LIST_BELIEFS])) + "/" + item[
NARSGUI.KEY_CAPACITY_BELIEFS] + ")"
create_clickable_listbox(parent=item_info_window,
row=row,
column=column,
title_label=beliefs_capacity_text,
listbox_contents=item[NARSGUI.KEY_LIST_BELIEFS],
content_click_callback=self.listbox_sentence_item_click_callback)
# desires table listbox
column += 2
desires_capacity_text = "Desires (" + str(len(item[NARSGUI.KEY_LIST_DESIRES])) + "/" + item[
NARSGUI.KEY_CAPACITY_DESIRES] + ")"
create_clickable_listbox(parent=item_info_window,
row=row,
column=column,
title_label=desires_capacity_text,
listbox_contents=item[NARSGUI.KEY_LIST_DESIRES],
content_click_callback=self.listbox_sentence_item_click_callback)
# Term Links listbox
column += 2
term_links_text = "Term Links (" + str(len(item[NARSGUI.KEY_LIST_TERM_LINKS])) + ")"
create_clickable_listbox(parent=item_info_window,
row=row,
column=column,
title_label=term_links_text,
listbox_contents=item[NARSGUI.KEY_LIST_TERM_LINKS],
content_click_callback=self.listbox_concept_item_click_callback)
# Next row
column = 0
row += 2
# Prediction Links Listbox
prediction_links_text = "Prediction Links (" + str(len(item[NARSGUI.KEY_LIST_PREDICTION_LINKS])) + ")"
create_clickable_listbox(parent=item_info_window,
row=row,
column=column,
title_label=prediction_links_text,
listbox_contents=item[NARSGUI.KEY_LIST_PREDICTION_LINKS],
content_click_callback=self.listbox_concept_item_click_callback)
# Explanation Links Listbox
column += 2
explanation_links_text = "Explanation Links (" + str(len(item[NARSGUI.KEY_LIST_EXPLANATION_LINKS])) + ")"
create_clickable_listbox(parent=item_info_window,
row=row,
column=column,
title_label=explanation_links_text,
listbox_contents=item[NARSGUI.KEY_LIST_EXPLANATION_LINKS],
content_click_callback=self.listbox_concept_item_click_callback)
def draw_sentence_internal_data(self, sentence_to_draw):
item_info_window = tk.Toplevel()
item_info_window.title("Sentence Internal Data: " + sentence_to_draw[NARSGUI.KEY_STRING])
is_array = sentence_to_draw[NARSGUI.KEY_IS_ARRAY]
if is_array:
item_info_window.geometry('1300x500')
else:
item_info_window.geometry('1000x500')
row = 0
column = 0
# sentence
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Sentence: ",
value_label=sentence_to_draw[NARSGUI.KEY_STRING_NOID])
# sentence ID
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Sentence ID: ",
value_label=sentence_to_draw[NARSGUI.KEY_ID])
# original truth value
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Truth Value: ",
value_label=sentence_to_draw[NARSGUI.KEY_TRUTH_VALUE])
# sentence occurrence time
row += 1
oc_time = sentence_to_draw[NARSGUI.KEY_OCCURRENCE_TIME]
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Occurrence Time: ",
value_label=str("Eternal" if oc_time is None else oc_time))
if oc_time is not None:
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Time projected Truth Value: ",
value_label=sentence_to_draw[NARSGUI.KEY_TIME_PROJECTED_TRUTH_VALUE])
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Expectation: ",
value_label=sentence_to_draw[NARSGUI.KEY_EXPECTATION])
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Is Positive? ",
value_label=sentence_to_draw[NARSGUI.KEY_IS_POSITIVE])
if sentence_to_draw[NARSGUI.KEY_PASSES_DECISION] is not None:
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Passes Decision-Making Rule? ",
value_label=sentence_to_draw[NARSGUI.KEY_PASSES_DECISION])
# sentence type
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Sentence Type: ",
value_label=sentence_to_draw[NARSGUI.KEY_SENTENCE_TYPE])
# sentence type
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Derived By: ",
value_label=sentence_to_draw[NARSGUI.KEY_DERIVED_BY])
if len(sentence_to_draw[NARSGUI.KEY_PARENT_PREMISES]) > 0:
row += 1
create_key_value_label(parent=item_info_window,
row=row,
column=column,
key_label="Parent Premises: ",
value_label=sentence_to_draw[NARSGUI.KEY_PARENT_PREMISES][0])
if len(sentence_to_draw[NARSGUI.KEY_PARENT_PREMISES]) == 2:
row += 1
create_value_label(parent=item_info_window,
row=row,
column=column + 1,
value_label=sentence_to_draw[NARSGUI.KEY_PARENT_PREMISES][1])
# blank
row += 1
label = tk.Label(item_info_window, text="")
label.grid(row=row, column=1)
# Listboxes
row += 1
# Evidential base listbox
create_clickable_listbox(parent=item_info_window,
row=row,
column=column,
title_label="Sentence Evidential Base",
listbox_contents=sentence_to_draw[NARSGUI.KEY_LIST_EVIDENTIAL_BASE],
content_click_callback=self.listbox_sentence_item_click_callback)
# Interacted sentences listbox
column += 2
create_clickable_listbox(parent=item_info_window,
row=row,
column=column,
title_label="Sentence Interacted Sentences",
listbox_contents=sentence_to_draw[NARSGUI.KEY_LIST_INTERACTED_SENTENCES],
content_click_callback=self.listbox_sentence_item_click_callback)
MAX_IMAGE_SIZE = 2000
if is_array:
image_array = sentence_to_draw[NARSGUI.KEY_ARRAY_IMAGE].subterms
column += 2
# set image defaults
gui_array_image_dimensions = [300, 300]
gui_array_use_confidence_opacity = [True]
# Percept elements label
label = tk.Label(item_info_window, text="Array Visualization (scroll to zoom in/out)", font=('bold'))
label.grid(row=row, column=column, columnspan=2)
if not Config.ARRAY_SENTENCES_DRAW_INDIVIDUAL_ELEMENTS:
"""
Array - Draw entire image (faster)
"""
# create image frame
image_frame = tk.Frame(item_info_window, width=MAX_IMAGE_SIZE, height=MAX_IMAGE_SIZE,
name="array image frame")
image_frame.grid(row=row + 1, column=column, columnspan=2, rowspan=2)
# create image container
img_container = tk.Label(image_frame, image=None)
img_container.grid(row=row + 1, column=column, columnspan=2, rowspan=2)
def zoom_image_array(event):
# zoom the image array depending on how the user scrolled
if event is None:
offset = 0 # initialize
else:
offset = 3 if event.delta > 0 else -3
pil_image = Image.fromarray(np.swapaxes(image_array, axis1=1, axis2=2))
# pil_image = ImageOps.flip(pil_image).rotate(angle=90)
if gui_array_use_confidence_opacity[0]:
pil_alpha = Image.fromarray(image_alpha_array, mode="L")
pil_image.putalpha(pil_alpha)
pil_image = pil_image.convert(mode="RGBA")
gui_array_image_dimensions[0] += offset
if gui_array_image_dimensions[0] < 1: gui_array_image_dimensions[0] = 1
gui_array_image_dimensions[1] += offset
if gui_array_image_dimensions[1] < 1: gui_array_image_dimensions[1] = 1
pil_image = pil_image.resize(gui_array_image_dimensions, resample=Image.NEAREST)
render = ImageTk.PhotoImage(pil_image)
img_container.config(image=render)
img_container.image = render
image_frame.bind_all("<MouseWheel>", zoom_image_array)
zoom_image_array(None)
else:
"""
Array - Draw Individual Cells (slower)
"""
PIXELS_PER_ELEMENT = [int(300 / image_array.shape[0])] # use an array to keep a pointer of the integer
if PIXELS_PER_ELEMENT[0] < 1: PIXELS_PER_ELEMENT[0] = 1 # minimum size 1 pixel
def create_array_element_click_lambda(sentence):
return None # lambda: self.draw_sentence_internal_data(sentence) #todo
image_frame = [None]
def create_image_array():
if image_frame[0] is not None:
image_frame[0].destroy()
image_frame[0] = tk.Frame(item_info_window, width=MAX_IMAGE_SIZE, height=MAX_IMAGE_SIZE,
name="array image frame")
image_frame[0].grid(row=row, column=column, columnspan=2, rowspan=2)
image_frame[0].bind_all("<MouseWheel>", zoom_image_array)
# iterate over each element and draw a pixel for it
if len(image_array.shape) == 2:
for (y, x), pixel_value in np.ndenumerate(image_array):
f = tk.Frame(image_frame[0], width=PIXELS_PER_ELEMENT[0],
height=PIXELS_PER_ELEMENT[0])
f.grid(row=y, column=x, columnspan=1, rowspan=1)
f.rowconfigure(0, weight=1)
f.columnconfigure(0, weight=1)
f.grid_propagate(0)
element_string = sentence_to_draw[NARSGUI.KEY_ARRAY_ELEMENT_STRINGS][y, x]
img_array = np.array(
[
[255 if isinstance(pixel_value,StatementTerm) else 0]
])
# img_array = img_array.T
img = Image.fromarray(img_array, mode="L")
# if not gui_array_use_confidence_opacity[0]:
img = img.resize((PIXELS_PER_ELEMENT[0], PIXELS_PER_ELEMENT[0]), Image.NEAREST)
button = tk.Button(f,
command=create_array_element_click_lambda(element_string))
render = ImageTk.PhotoImage(img)
button.config(image=render)
button.image = render
button.config(relief='solid', borderwidth=0)
button.grid(sticky="NWSE")
CreateToolTip(button, text=(element_string))
elif len(image_array.shape) == 3:
for (x, y, z), pixel_value in np.ndenumerate(image_array):
if z > 0: continue # only iterate through first layer
f = tk.Frame(image_frame[0], width=PIXELS_PER_ELEMENT[0],
height=PIXELS_PER_ELEMENT[0])
f.grid(row=y, column=x, columnspan=1, rowspan=1)
f.rowconfigure(0, weight=1)
f.columnconfigure(0, weight=1)
f.grid_propagate(0)
element_string = sentence_to_draw[NARSGUI.KEY_ARRAY_ELEMENT_STRINGS][x, y, z]
img_array = np.array([
[
[
[image_array[x, y, 0]]
]
],
[
[
[image_array[x, y, 1]]
]
],
[
[
[image_array[x, y, 2]]
]
],
[
[
[image_alpha_array[(x, y)]]
]
]
])
img_array = img_array.T
img = Image.fromarray(img_array, mode="RGBA")
if not gui_array_use_confidence_opacity[0]:
img = img.convert(mode="RGB")
img = img.resize((PIXELS_PER_ELEMENT[0], PIXELS_PER_ELEMENT[0]), Image.NEAREST)
button = tk.Button(f,
command=create_array_element_click_lambda(element_string))
render = ImageTk.PhotoImage(img)
button.config(image=render)
button.image = render
button.config(relief='solid', borderwidth=0)
button.grid(sticky="NWSE")
CreateToolTip(button, text=(element_string))
def zoom_image_array(event):
# zoom the image array depending on how the user scrolled
if event is not None:
if event.delta > 0:
PIXELS_PER_ELEMENT[0] += 1
else:
PIXELS_PER_ELEMENT[0] -= 1
create_image_array()
# checkbox to toggle array confidence opacity
def toggle_confidence_opacity():
gui_array_use_confidence_opacity[0] = not gui_array_use_confidence_opacity[0]
zoom_image_array(None)
check_var = tk.Variable()
row += 1