-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·1363 lines (944 loc) · 39.6 KB
/
utils.py
File metadata and controls
executable file
·1363 lines (944 loc) · 39.6 KB
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 random
import yaml
from os.path import join
import csv
import ast
import random
import argparse
#####
import copy
import datetime
import time
import numpy as np
import random
#############
import os
from openai import OpenAI
# Load environment variables from .env
from dotenv import load_dotenv
from env_utils import doublecheck_env
# Load environment variables from .env
load_dotenv()
# Check and print results
doublecheck_env("example.env")
########################################
########################################
def get_args():
argParser = argparse.ArgumentParser()
argParser.add_argument("-t", "--task", help=" print_train,print_test")
argParser.add_argument("-s", "--solution", help=" solutino")
argParser.add_argument("-e", "--existing",default=None,type=str, help=" existing question")
argParser.add_argument("-c", "--cross",default='True',type=str, help=" cross")
argParser.add_argument("-v", "--version",default='origninal',type=str, help=" origninal")
args = argParser.parse_args()
print("args=%s" % args)
return args
def shuflle_row(l1,l2):
c = list(zip(l1, l2))
random.shuffle(c)
l1, l2 = zip(*c)
return l1, l2
def shuflle_many_columns(L,nodes):
L.append(nodes)
c = list(zip(*L))
random.shuffle(c)
#zip(*[[1,2], [3,4], [5,6]])
L_=[l for l in zip(*c)]
return L_[:-1],L_[-1]
def shuflle_many_columns_(L):
c = list(zip(*L))
random.shuffle(c)
#zip(*[[1,2], [3,4], [5,6]])
L_=[l for l in zip(*c)]
return L_[:]
###
def shuflle_two(l1,l2):
c = list(zip(l1, l2))
random.shuffle(c)
l1, l2 = zip(*c)
return l1, l2
###
def crossing_columns(L,N):
index=[k for k in range(len(L))]
random.shuffle(index)
N_new=[N[i] for i in index]
L_New=[]
for l in L:
l_=[l[i] for i in index]
L_New.append(l_)
return L_New, N_new
# def crossing_rows_and_v2(L,N):
# index=[k for k in range(len(L))]
# random.shuffle(index)
# N_new=[N[i] for i in index]
# L_New=[]
# L_New_=[]
# for i in index:
# l=L[i]
# l_=[l[i] for i in index]
# L_New_.append(l_)
# return L_New_, N_new
def crossing_rows_and_v2(L,N,cross='no'):
index_row=[k for k in range(len(L))]
random.shuffle(index_row)
index_col=[k for k in range(len(L))]
random.shuffle(index_col)
row_order=[copy.deepcopy(N[i]) for i in index_row]
column_order=[copy.deepcopy(N[i]) for i in index_col]
if cross=='False':
column_order=row_order
index_col=index_row
#print('cross',cross)
#exit()
adj_dic={}
for i,l in enumerate(L):
a=N[i]
for j,li in enumerate(l):
b=N[j]
adj_dic[(a,b)]=li
L_New=[]
#print('adj_dic',adj_dic)
adj_dic_new={}
for a in row_order:
l=[]
for b in column_order:
v=adj_dic[(a,b)]
l.append(v)
L_New.append(l)
####################
for i,l in enumerate(L_New):
a=row_order[i]
for j,li in enumerate(l):
b=column_order[j]
adj_dic_new[(a,b)]=li
for k in adj_dic_new.keys():
old=adj_dic[k]
new=adj_dic_new[k]
if old!=new:
print('wrong')
exit()
# print('row_order',row_order)
# print('column_order',column_order)
# print('cross',cross)
# exit()
return L_New,row_order,column_order
def load_config(file_name="config.yaml", concatenate=False):
try:
with open(file_name) as yaml_file:
doc = yaml.safe_load(yaml_file)
if concatenate:
doc = {**doc[1], **doc[2], **doc[3], **doc[4]}
return doc
except:
raise Exception("Can't load config file")
#####
#def find_mask(masked,ordered):
def find_mask(variables,masked,variable_values_list):
masked_ids={}
masked_=masked.copy()
for v in variables:
for mi,m in enumerate(masked):
if v==m:
masked_ids[v]=variable_values_list[mi]
return masked_ids
def load_trials(file_name, randomize_graphs=False):
file_path =file_name# join("trials", file_name)
#print('file_name',file_name)
#print('file_path',file_path)
with open(file_path) as f:
reader = csv.reader(f, delimiter=';')
#print('reader')
header = None
data_train = []
data_exp = []
for row in reader:
if not header:
header = row
else:
#print('header',header)
#print(row)
data_row = {k: v for k, v in zip(header, row)}
for k in ["NR", "FEED", "TRAIN", "Number_of_nodes", "Number_of_edges","Bidirectional"]:
data_row[k] = int(data_row[k])
for k in ["Nodes_A", "Nodes_B"]:
data_row[k] = [int(elem) for elem in data_row[k].split(",")]
for k in ["Edges_A", "Edges_B", "Left_button_targets", "Right_button_targets"]:
data_row[k] = ast.literal_eval(data_row[k])
if randomize_graphs and random.choice([True, False]):
data_row["Nodes_A"], data_row["Nodes_B"] = data_row["Nodes_B"], data_row["Nodes_A"]
data_row["Edges_A"], data_row["Edges_B"] = data_row["Edges_B"], data_row["Edges_A"]
data_row["Left_button_targets"], data_row["Right_button_targets"] = data_row["Right_button_targets"], data_row["Left_button_targets"]
if data_row["TRAIN"] == 1:
data_train.append(data_row)
else:
data_exp.append(data_row)
# block_nr = set([info["Block"] for info in data_exp])
# data_exp = [[info for info in data_exp if info["Block"] == i] for i in block_nr]
return data_train, data_exp
#####
def mask_nodes(d):
source=d['source']
target=d['target']
Type_=d['Type_']
Crossed_edges=d['Crossed_edges']
#print('Crossed_edges',Crossed_edges)
if len(str(Crossed_edges))==0 or Crossed_edges==False:
Crossed_edges='no'
source_adj=source['adj']
##
source_nodes_row=source['nodes_in_row']
source_nodes_column=source['nodes_in_column']
target_adj=target['adj']
target_nodes_row=target['nodes_in_row']
target_nodes_row=list(target_nodes_row)
target_nodes_in_column=target['nodes_in_column']
test_list = [i for i in range(len(target_nodes_row))]
unknown_nodes=len(target_nodes_row)-1 if len(target_nodes_row) ==3 else len(target_nodes_row)-2 \
if len(target_nodes_row)<=4 else len(target_nodes_row)-3
if len(target_nodes_row) <=2:
unknown_nodes=0
test_list=random.sample(test_list, unknown_nodes)
X=['r1','r2','r3','r4','r5','r6','r7','r8','r9','r10','r11','r12','r13']
Y=X
X_=random.sample(X, len(target_nodes_row)-1)
target_nodes_row=list(target_nodes_row)
target_nodes_row_=target_nodes_row.copy()
if len(target_nodes_row_)>=2:
j_=0
for i in test_list:
target_nodes_row_[i]=X_[j_]
j_=j_+1
else:
print(len(target_nodes_row_))
unknown_nodes=len(target_nodes_in_column)-1 if len(target_nodes_in_column) <=3 else len(target_nodes_in_column)-2 \
if len(target_nodes_in_column)<=4 else len(target_nodes_in_column)-3
if len(target_nodes_in_column) <=2:
unknown_nodes=0
test_list = [i for i in range(len(target_nodes_in_column))]
test_list=random.sample(test_list, unknown_nodes)
X=['c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','c11','c12','c13']
X=random.sample(X, len(target_nodes_in_column)-1)
target_nodes_in_column=list(target_nodes_in_column)
target_nodes_in_column_=target_nodes_in_column.copy()
j=0
if len(target_nodes_in_column_)>2:
j=0
for i in test_list:
target_nodes_in_column_[i]=X[j]
j=j+1
#
column_variable=X[:j]
row_variables=X_[:j_]
######
sol_row=find_mask(row_variables,target_nodes_row_,target_nodes_row)
sol_column=find_mask(column_variable,target_nodes_in_column_,target_nodes_in_column)
for ni,n in enumerate(target_nodes_row_):
if n not in Y:
target_nodes_row_[ni]='S'+str(ni)
for ni,n in enumerate(target_nodes_in_column_):
if n not in X:
target_nodes_in_column_[ni]='T'+str(ni)
item={'source_nodes':source_nodes_row,'source_adj':source_adj,'target_adj':target_adj,\
'source_nodes_order_in_row':source_nodes_row,'source_nodes_order_in_column':source_nodes_column,\
'target_nodes_order_in_row':target_nodes_row,'target_nodes_order_in_column':target_nodes_in_column,\
'masked_target_nodes_order_in_row':target_nodes_row_,
'masked_target_nodes_order_in_column':target_nodes_in_column_,\
'column_variable':column_variable,'row_variables':row_variables,'sol_row':sol_row,'sol_column':sol_column,'Type_':Type_}
return item
def get_type(E):
t={'incoming':0,'outgoing':0}
Nodes_income_out={}
for e1 in E:
n1,n2=e1[0],e1[1]
if n1 not in Nodes_income_out.keys():
Nodes_income_out[n1]={'incoming':0,'outgoing':0}
if n2 not in Nodes_income_out.keys():
Nodes_income_out[n2]={'incoming':0,'outgoing':0}
Nodes_income_out[n1]['outgoing']+=1
Nodes_income_out[n2]['incoming']+=1
type_=[]
pattern={}
for k in Nodes_income_out.keys():
incoming=Nodes_income_out[k]['incoming']
outgoing=Nodes_income_out[k]['outgoing']
if str(incoming)+'-'+str(outgoing) in pattern.keys():
continue
pattern[str(incoming)+'-'+str(outgoing)]=0
for k2 in Nodes_income_out.keys():
incoming_=Nodes_income_out[k2]['incoming']
outgoing_=Nodes_income_out[k2]['outgoing']
if incoming==incoming_ and outgoing==outgoing_:
pattern[str(incoming)+'-'+str(outgoing)]+=1
pattern=[p+'*'+str(pattern[p])+'#' for p in pattern.keys()]
pattern=''.join(pattern)
return pattern
def v2_graphmapping():
number_of_nodes=random.randint(5, 8)
Nodes_A=[i for i in range(number_of_nodes)]
Edges=[]
edges_dic={'-1#-1#':True}
for node in Nodes_A:
max_edge=2 if len(Nodes_A)>4 else 2
number_of_edges=2#random.randint(1, max_edge)
no = number_of_edges
a = 0
b = len(Nodes_A)
ed=[-1,-1]
key=[str(t)+'#' for t in ed]
key = ''.join(key)
while key in list(edges_dic.keys()) or node in ed :
ed = random.sample(Nodes_A, no)
key=[str(t)+'#' for t in ed]
key = ''.join(key)
print('key',key)
edges_dic[key]=True
for n in ed:
if n==node:
continue
temp=(node,n)
Edges.append(temp)
# if n len(Nodes_A)>5:
# inDi_Nodes = random.sample(Nodes_A, 2)
##
e=random.sample(Edges, 1)[0]
Edges.remove(e)
n2=n1=0
n3=0
while n1==n2 and (n2,n1) in Edges and (n2,n1)!=e and (n3,n1)!=e:
n2 = random.sample(Nodes_A, 1)[0]
n1 = random.sample(Nodes_A, 1)[0]
n3 = random.sample(Nodes_A, 1)[0]
temp=(n2,n1)
print('Edges',Edges)
print(temp in Edges)
Edges.append(temp)
temp=(n3,n1)
Edges.append(temp)
###########################
# n2=n1=0
# while n1==n2 and (n1,n2) in Edges and (n1,n2)!=e:
# n2 = random.sample(Nodes_A, 1)[0]
# n1 = random.sample(Nodes_A, 1)[0]
# temp=(n1,n2)
# print('Edges',Edges)
# print(temp in Edges)
# Edges.append(temp)
# row=[0 for n in Nodes_A ]
# adj_A=[row for n in Nodes_A]
# adj_A=np.array(adj_A)
# nodes_A_dic={}
# temp=0
# for n in Nodes_A:
# nodes_A_dic[n]=temp
# temp=temp+1
# for e in Edges:
# i,j=e[0],e[1]
# i,j=nodes_A_dic[i],nodes_A_dic[j]
# adj_A[i,j]=1
# adj_source=adj_A.tolist()
# print('adj_source',adj_source)
# exit()
######################
Edges_A=Edges
Nodes_B=copy.deepcopy(Nodes_A)
Edges_B=copy.deepcopy(Edges_A)
print('Nodes_B',Nodes_B)
print('Edges_B',Edges_B)
# check
type_=get_type(Edges_A)
return Nodes_A,Nodes_B,Edges_A,Edges_B,type_
def prepare_data(data,cross,version,Overwrite_cross):
data_processes=[]
if version=='v2':
alphabet=['a','b','c','d','e','f','g','h','k','l','m','n','p','q','o','w','i', 'j', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z']
alphabet2=copy.deepcopy(alphabet)
alphabet2=[
"α", "β", "γ", "δ", "ε", "ζ", "η", "θ",
"ι", "κ", "λ", "μ", "ν", "ξ", "ο", "π",
"ρ", "σ", "ς", "τ", "υ", "φ", "χ", "ψ", "ω"
]
else:
alphabet=['a','b','c','d','e','f','g','h',]
alphabet2=['k','l','m','n','p','q','o','w']
random.shuffle(alphabet)
print('cross',cross)
for d in data:
#print('+++++++')
#print('d',d)
nodes_A_dic={}
nodes_B_dic={}
Nodes_A=d['Nodes_A']
Edges_A=d['Edges_A']
Nodes_B=d['Nodes_B']
Edges_B=d['Edges_B']
Type_=d['Type']
if version=='v2':
# print('Nodes_A',Nodes_A)
# print('Edges_A',Edges_A)
# print('Nodes_B',Nodes_B)
# print('Edges_B',Edges_B)
# continue
#exit()
Nodes_A,Nodes_B,Edges_A,Edges_B,Type_=v2_graphmapping()
temp=0
for n in Nodes_A:
nodes_A_dic[n]=temp
temp=temp+1
temp=0
for n in Nodes_B:
nodes_B_dic[n]=temp
temp=temp+1#
if version!='v2':
print(nodes_B_dic,nodes_B_dic)
Right_button_targets=d['Right_button_targets']
Left_button_targets=d['Left_button_targets']
#print('Right_button_targets',Right_button_targets)
#print('Left_button_targets',Left_button_targets)
m,n=Right_button_targets[0],Right_button_targets[1]
Right_button_targets=[nodes_A_dic[m],nodes_B_dic[n]]
m,n=Left_button_targets[0],Left_button_targets[1]
Left_button_targets=[nodes_A_dic[m],nodes_B_dic[n]]
else:
Left_button_targets=[0,0]
Right_button_targets=[0,0]
#print('Right_button_targets',Right_button_targets)
#print('Left_button_targets',Left_button_targets)
Crossed_edges=d['Crossed_edges']
if version=='v2':
Crossed_edges='True'
if Overwrite_cross==False:
if len(Crossed_edges)==0 or Crossed_edges=='no':
cross='False'
Crossed_edges='False'
else:
Crossed_edges='True'
cross='True'
if Overwrite_cross==True:
Crossed_edges=cross
# print('cross',cross)
# print('Overwrite_cross',Overwrite_cross)
# exit()
nodes_source=[alphabet[i] for i,n in enumerate(Nodes_A)]
nodes_target=[alphabet2[i] for i,n in enumerate(Nodes_B)]
if version=='v2':
nodes_target=copy.deepcopy(nodes_source)
n1,n2=Left_button_targets[0],Left_button_targets[1]
Left_button_targets=[nodes_source[n1],nodes_target[n2]]
####
n1,n2=Right_button_targets[0],Right_button_targets[1]
Right_button_targets=[nodes_source[n1],nodes_target[n2]]
row=[0 for n in nodes_source ]
adj_A=[row for n in nodes_source]
adj_A=np.array(adj_A)
for e in Edges_A:
i,j=e[0],e[1]
i,j=nodes_A_dic[i],nodes_A_dic[j]
adj_A[i,j]=1
adj_source=adj_A.tolist()
##
adj_B=[row for n in nodes_source]
adj_B=np.array(adj_B)
for e in Edges_B:
i,j=e[0],e[1]
i,j=nodes_B_dic[i],nodes_B_dic[j]
adj_B[i,j]=1
adj_target=adj_B.tolist()
# print('adj_B',adj_B)
# print('adj_A',adj_A)
if version=='v2':
adj_target=adj_source
Crossed_edges=cross
adj_A=list(adj_A)
adj_B=list(adj_B)
target_nodes_in_column=copy.deepcopy(nodes_target)
print('Crossed_edges:',Crossed_edges)
adj_target,nodes_target,target_nodes_in_column=crossing_rows_and_v2(adj_target,nodes_target,cross=cross)
item= {'Type_':Type_,'Right_button_targets':Right_button_targets,'Left_button_targets':Left_button_targets,\
'Crossed_edges':Crossed_edges,'source':{'adj':adj_source,'nodes_in_row':nodes_source,\
'nodes_in_column':nodes_source},'target':{'adj':adj_target,'nodes_in_row':nodes_target,\
'nodes_in_column':target_nodes_in_column}
}
if version!='v2':
data_processes.append(item)
else:
item=mask_nodes(item)
# print('item',item)
# exit()
#continue
data_processes.append(item)
#exit()
return data_processes
##
def shuflle_given__in_columns(adj,nodes_in_column):
#print('nodes_in_column',nodes_in_column)
nodes_in_column=nodes_in_column.copy()
adj=adj.copy()
adj=np.array(adj)
for i in range(2):
n,m=random.sample([i for i in range(len(nodes_in_column))], 2)
adj[:, [n, m]] = adj[:, [m, n]]
temp=nodes_in_column[n]
nodes_in_column[n]=nodes_in_column[m]
nodes_in_column[m]=temp
#print('nodes_in_column',nodes_in_column)
return adj.tolist(),nodes_in_column
###
def return_logic(adj,c_order,r_order):
column_dic={}
row_di={}
logic={}
adj_np=np.array(adj)
for i in range(len(r_order)):
node=r_order[i]
row_di[node]=i
for i in range(len(c_order)):
node=c_order[i]
column_dic[node]=i
for i in range(len(adj)):
node=r_order[i]
node_column=column_dic[node]
node_row=row_di[node]
incoming_edge=[]
outgoing_edge=[]
for j in range(len(adj)):
node_to=r_order[j]
#node_column=column_dic[node]
#node_row=row_di[node]
if adj_np[node_row,j]==1:
outgoing_edge.append(node_to)
for j in range(len(adj)):
node_from=c_order[j]
#node_column=column_dic[node]
#node_row=row_di[node]
if adj_np[j,node_column]==1:
incoming_edge.append(node_to)
logic[node]={'outgoing':outgoing_edge,'incoming':incoming_edge}
return logic
def organize_item(d,id_No,l,train,di):
Right_button_targets=d['Right_button_targets']
Left_button_targets=d['Left_button_targets']
Crossed_edges=d['Crossed_edges']
source=d['source']
target=d['target']
Type_=d['Type_']
print('Type_',Type_)
#exit()
Crossed_edges=d['Crossed_edges']
#print('Crossed_edges',Crossed_edges)
if Crossed_edges=='False':
Crossed_edges='no'
source_adj=source['adj']
source_nodes=source['nodes_in_row']
target_adj=target['adj']
target_nodes=target['nodes_in_row']
target_nodes_row=list(target_nodes)
target_nodes_in_column=target['nodes_in_column']
nodes_to_map=[Right_button_targets[0],Left_button_targets[0]]
map_solution=Right_button_targets,Left_button_targets
map_solution={Right_button_targets[0]:Right_button_targets[1],Left_button_targets[0]:Left_button_targets[1]}
#print('*****')
#print(str(l)+'_sample_'+str(di))
if train:
name=str(l)+'_train_'+str(di)
else:
name=str(l)+'_test_'+str(di)
target_column_order=target_nodes_in_column
item={'name':name,'source_nodes':source_nodes,'source_adj':source_adj,'target_adj':target_adj,\
'row_nodes_order_source':source_nodes,\
'column_nodes_order_source':source_nodes,\
'row_nodes_order_target':target_nodes_row,\
'column_nodes_order_target':target_nodes_in_column,
'map_solution':map_solution,\
'nodes_to_map':nodes_to_map,'Crossed_edges':Crossed_edges,'Type_':Type_}
return item
def print_as_question(id_No,l,data,solution,train=False):
Questions=''
List_questions={}
for di,d in enumerate(data):
item=organize_item(d,id_No,l,train,di)
temp_=''
temp_='\n'+temp_ +f'\n ***********************'
source_adj=item['source_adj']
target_adj=item['target_adj']
row_nodes_order_source=item['row_nodes_order_source']
row_nodes_order_target=item['row_nodes_order_target']
column_nodes_order_source=item['column_nodes_order_source']
column_nodes_order_target=item['column_nodes_order_target']
map_solution=item['map_solution']
nodes_to_map=item['nodes_to_map']
Crossed_edges=item['Crossed_edges']
name=item['name']
type_=item['Type_']
temp_='\n'+temp_ +f'\n question no: {name}'
##
temp_=temp_ +f'\n Initially: '
temp_=temp_ +f'\n source_adj: {source_adj}'
temp_=temp_ +f'\n nodes order in row, source adj = : {row_nodes_order_source}'
temp_=temp_ +f'\n nodes order in column, source adj = {column_nodes_order_source}'
temp_=temp_ +f'\n after renaming nodes,'
temp_=temp_ +f'\n target_adj= {target_adj}'
temp_=temp_ +f'\n nodes order in row, target adj = : {row_nodes_order_target}'
temp_=temp_ +f'\n nodes order in column, target adj = {column_nodes_order_target}'
temp_=temp_ +f'\n Crossed_edges= {Crossed_edges}'
temp_=temp_ +f'\n Quesetion: map given source nodes to appropriate target nodes: {nodes_to_map}'
##
if solution:
temp_=temp_ +f'\n Solution as dic: '
temp_=temp_ +f'\n solution map: {map_solution}'
Questions=Questions+temp_
#List_questions[name]= copy.copy(temp_)
List_questions[name]= {'q':copy.copy(temp_),'map_solution':map_solution,'eval_d':item}
return Questions,List_questions
def print_as_question_v2(data,l,train,solution):
Questions=''
List_questions={}
Questions=Questions +'\n ***********************'
for di,d in enumerate(data):
source_adj=d['source_adj']
target_adj=d['target_adj']
source_nodes_order_in_row=d['source_nodes_order_in_row']
target_nodes_order_in_row=d['target_nodes_order_in_row']
source_nodes_order_in_column=d['source_nodes_order_in_column']
target_nodes_order_in_column=d['target_nodes_order_in_column']
masked_target_nodes_order_in_row=d['masked_target_nodes_order_in_row']
masked_target_nodes_order_in_column=d['masked_target_nodes_order_in_column']
sol_row=d['sol_row']
sol_column=d['sol_column']
column_variable=d['column_variable']
row_variables=d['row_variables']
Type_=d['Type_']
if train==True:
name=str(l)+'_train_'+str(di)
else:
name=str(l)+'_test_'+str(di)
if len(source_nodes_order_in_row)<=2:
continue
temp_=''
temp_=temp_ +f'\n ***********************'
temp_=temp_ +f'\n {name}:'
temp_=temp_ +f'\n Initially: '
temp_=temp_ +f'\n nodes_order_in_row = : {source_nodes_order_in_row}'
temp_=temp_ +f'\n nodes_order_in_column = : {source_nodes_order_in_column}'
temp_=temp_ +f'\n source_adj = : {source_adj}'
temp_=temp_ +f'\n after swapping rows, columns'
temp_=temp_ +f'\n target_adj = : {target_adj}'
temp_=temp_ +f'\n maksed_target_row_order = : {masked_target_nodes_order_in_row}'
temp_=temp_ +f'\n maksed_target_column_order = : {masked_target_nodes_order_in_column}'
temp_=temp_ +f'\n Question:'
temp_=temp_ +f'\n map the mask variables in column and row to source nodes:'
temp_=temp_ +f'\n mask variabbles in row : {row_variables}'
temp_=temp_ +f'\n mask variabbles in column : {column_variable}'
#print('temp_',temp_)
if solution:
temp_=temp_+f'\n solution is as below: '
temp_=temp_+f'\n sol_row: {sol_row}'
temp_=temp_+f'\n sol_column: {sol_column}'
Questions=Questions+temp_
List_questions[name]= {'q':copy.copy(temp_),'sol_row':sol_row,'sol_column':sol_column,'eval_d':d,'Type_':Type_}
#print('Questions',Questions)
#print('sol_column',sol_column)
#exit()
return Questions,List_questions
def get_prompt(q,train,version):
if version=='original':
prompt_for_original_questions = f"""
Given two isomorphic graphs, namely source and target, your task is to map some of thr target nodes to the coressponding source nodes. Note the following
Rules:
- First graph is called source.
- Second graph is called target.
- For each graph, they are specified using adjacency matrix
- The order of nodes in the row and column of adj matrix is given for each graph.
- The target graph is obtained from the source graph by renaming the nodes, and probably shuffling the orders in row and column of adjacency matrix.
- To help you, with provide some sample training questions, and then you answer the test question.
- Samples to learn from are below:
{train}
- Question to answer is below:
{q}
- make the mappings variables to nodes as dictionary of key and vlaues, with key being variable, and value the node it coresponds to!
"""
return prompt_for_original_questions
else:
prompt_for_v2_questions = f"""
Given two isomorphic graphs, namely source and target, your task is to map some of thr target nodes to the coressponding source nodes. Note the following
Rules:
- First graph is called source.
- Second graph is called target.
- For each graph, they are specified using adjacency matrix
- The order of nodes in the row and column of adj matrix is given for each graph.
- The target graph is obtained from the source graph by renaming the nodes, and probably shuffling the orders in row and column of adjacency matrix.
- To help you, with provide some sample training questions, and then you answer the test question.
- Samples to learn from are below:
{train}
- Question to answer is below:
{q}
- make the mappings variables to nodes as dictionary of key and vlaues, with key being variable, and value the node it coresponds to!
"""
return prompt_for_v2_questions
def get_prompt_hf():
p = f"""
Given two isomorphic graphs, namely source and target, your task is to map some of thr target nodes to the coressponding source nodes. Note the following
Rules:
- First graph is called source.
- Second graph is called target.
- For each graph, they are specified using adjacency matrix
- The order of nodes in the row and column of adj matrix is given for each graph.
- The target graph is obtained from the source graph by renaming the nodes, and probably shuffling the orders in row and column of adjacency matrix.
- make the mappings variables to nodes as dictionary of key and vlaues, with key being variable, and value the node it coresponds to!
"""
return p
def get_prompt_(q,train,version):
if version=='original':
prompt_for_original_questions = f"""
Given two isomorphic graphs, namely source and target, your task is to map some of thr target nodes to the coressponding source nodes. Note the following
Rules:
- First graph is called source.
- Second graph is called target.
- For each graph, they are specified using adjacency matrix
- The order of nodes in the row and column of adj matrix is given for each graph.
- The target graph is obtained from the source graph by renaming the nodes, and probably shuffling the orders in row and column of adjacency matrix.
- Question to answer is below: