-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPCGtoPython-Example.pl
More file actions
254 lines (193 loc) · 8.05 KB
/
PCGtoPython-Example.pl
File metadata and controls
254 lines (193 loc) · 8.05 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
use strict;
use warnings;
use Graph;
use Moose;
use Math::Round;
# my $g1 = Graph->new(multiedged => 1);
# $g1->add_edge_by_id("Koala", "Eucalyptus", "diet");
# $g1->add_edge_by_id("Koala", "Acacia", "diet");
# $g1->add_edge_by_id("Eucalyptus", "Acacia", "at-risk");
#
# my $g2 = Graph->new(multiedged => 1);
# $g2->add_edge_by_id("Panda", "Bamboo", "diet");
# $g2->add_edge_by_id("Panda", "Emu", "at-risk");
# $g2->add_edge_by_id("Emu", "Bamboo", "at-risk");
my $g1 = Graph->new(multiedged => 1);
$g1->add_edge_by_id("span1", "component1", "LEFT_SUPPORT");
$g1->add_edge_by_id("component1", "inelastic1", "ELASTICITY");
$g1->add_edge_by_id("component1", "open1", "FILLEDNESS");
$g1->add_edge_by_id("component1", "stable1", "STABILITY");
$g1->add_edge_by_id("span1", "component2", "RIGHT_SUPPORT");
$g1->add_edge_by_id("component2", "inelastic2", "ELASTICITY");
$g1->add_edge_by_id("component2", "open2", "FILLEDNESS");
$g1->add_edge_by_id("component2", "unstable1", "STABILITY");
my $g2 = Graph->new(multiedged => 1);
$g2->add_edge_by_id("span2", "component3", "LEFT_SUPPORT");
$g2->add_edge_by_id("span2", "component4", "RIGHT_SUPPORT");
$g2->add_edge_by_id("component3", "elastic1", "ELASTICITY");
$g2->add_edge_by_id("component3", "filled1", "FILLEDNESS");
$g2->add_edge_by_id("component3", "unstable2", "STABILITY");
$g2->add_edge_by_id("component4", "elastic2", "ELASTICITY");
$g2->add_edge_by_id("component4", "open3", "FILLEDNESS");
$g2->add_edge_by_id("component4", "stable2", "STABILITY");
my $pcg = Graph->new(multiedged => 1);
my $pcgNoLabels = Graph->new(multiedged => 1); # No two-way arrows
# First, collect source, destination node and label
# The is for Graph1
my %m1;
my %labels;
for my $v1 ($g1->vertices){
for my $p1 ($g1->predecessors($v1)){
for my $label ($g1->get_multiedge_ids($p1, $v1)){
# {"LABEL"}{"SOURCE NODE"}{"DESTINATION NODE"}
$m1{$label}{$p1}{$v1} = 1; # There is no meaing to put 1. Just want to pickup unique key later
$labels{$label} = 1;
}
}
}
# For Graph2
my %m2;
my @labels;
for my $v2 ($g2->vertices){
for my $p2 ($g2->predecessors($v2)){
for my $label ($g2->get_multiedge_ids($p2, $v2)){
# {"LABEL"}{"SOURCE NODE"}{"DESTINATION NODE"}
$m2{$label}{$p2}{$v2} = 1;
$labels{$label} = 1;
}
}
}
# Second, add pairwise node.
# Node name is src1(from graph1)/src2(from graph2) or dest1(from graph1)/dest2(from graph2)
# %edges used for counting the label of neighbors
my %edges;
for my $label (keys %labels) {
#print $label, "------\n";
for my $src1 (keys %{$m1{$label}}){
for my $src2 (keys %{$m2{$label}}){
for my $dest1 (keys %{$m1{$label}{$src1}}){
for my $dest2 (keys %{$m2{$label}{$src2}}){
#print "src - $src1,$src2\n";
#print "dest - $dest1,$dest2\n";
$pcg->add_edge_by_id("$src1/$src2", "$dest1/$dest2", $label ); # JG: The algorithm has bidirectional flooding, so we enter as two-way edge
$pcg->add_edge_by_id("$dest1/$dest2", "$src1/$src2", $label );
$edges{"$src1/$src2"}{$label}++; #JG" For a given edge label figure out how many lead to/from that node
$edges{"$dest1/$dest2"}{$label}++;
}
}
}
}
}
# Second, add pairwise node but for illustration, we don't go whole way to final PCG
# Node name is src1(from graph1)/src2(from graph2) or dest1(from graph1)/dest2(from graph2)
# %edges used for counting the label of neighbors
for my $label (keys %labels) {
#print $label, "------\n";
for my $src1 (keys %{$m1{$label}}){
for my $src2 (keys %{$m2{$label}}){
for my $dest1 (keys %{$m1{$label}{$src1}}){
for my $dest2 (keys %{$m2{$label}{$src2}}){
#print "src - $src1,$src2\n";
#print "dest - $dest1,$dest2\n";
$pcgNoLabels->add_edge_by_id("$src1/$src2", "$dest1/$dest2", $label ); # JG: The algorithm has bidirectional flooding, so we enter as two-way edge
# $pcgNoLabels->add_edge_by_id("$dest1/$dest2", "$src1/$src2", $label );# Keep the original direction only for this one.
}
}
}
}
}
open (DOT, '>PCGtoDOT-Example.py'); #Erase file
print DOT "from pygraph.classes.digraph import digraph\n";
print DOT "from pygraph.readwrite.markup import write\n";
print DOT "import pydot\n";
print DOT "import tdag\n";
print DOT "from tdag import draw_graphs\n\n";
print DOT "pcgweighted = tdag.tdag(\"example-pcgweighted\")\n";
print DOT "pcglabels = tdag.tdag(\"example-pcglabels\")\n";
print DOT "g1 = tdag.tdag(\"example-g1\")\n";
print DOT "g2 = tdag.tdag(\"example-g2\")\n\n";
my $printgraph = $pcg;
my @alledges = $printgraph->edges();
# Get outgoing arc counts
my %outgoing;
for my $edgeArray (@alledges) {
my ($n1, $n2) = @{$edgeArray};
$outgoing{$n1}++;
}
for my $edgeArray (@alledges) {
my ($n1, $n2) = @{$edgeArray};
my @edgeLabels = $printgraph->get_multiedge_ids($n1,$n2);
for my $edgeLabel (@edgeLabels) {
print DOT "try: pcgweighted.add_node(\"$n1\", \"\")\n";
print DOT "except: pass\n";
print DOT "try: pcgweighted.add_node(\"$n2\", \"\")\n";
print DOT "except: pass\n\n";
my $numEdges = @edgeLabels;
my $weighting = $numEdges * (1/$outgoing{$n1});
print DOT "try: pcgweighted.add_edge((\"$n1\", \"$n2\"), label=\" ".nearest(.01, $weighting)."\")\nexcept: pass\n\n";
}
}
$printgraph = $pcgNoLabels;
@alledges = $printgraph->edges();
for my $edgeArray (@alledges) {
my ($n1, $n2) = @{$edgeArray};
my @edgeLabels = $printgraph->get_multiedge_ids($n1,$n2);
for my $edgeLabel (@edgeLabels) {
print DOT "try: pcglabels.add_node(\"$n1\", \"\")\n";
print DOT "except: pass\n";
print DOT "try: pcglabels.add_node(\"$n2\", \"\")\n";
print DOT "except: pass\n\n";
my $numEdges = @edgeLabels;
my $weighting = $numEdges * (1/$outgoing{$n1});
print DOT "try: pcglabels.add_edge((\"$n1\", \"$n2\"), label=\"$edgeLabel\")\nexcept: pass\n\n";
}
}
# Write out $g1 and $g2, too for display
$printgraph = $g1;
@alledges = $printgraph->edges();
my %seeng1edges;
for my $edgeArray (@alledges) {
my ($n1, $n2) = @{$edgeArray};
my @edgeLabels = $printgraph->get_multiedge_ids($n1,$n2);
for my $edgeLabel (@edgeLabels) {
print DOT "try: g1.add_node(\"$n1\", \"\")\n";
print DOT "except: pass\n";
print DOT "try: g1.add_node(\"$n2\", \"\")\n";
print DOT "except: pass\n\n";
my $numEdges = @edgeLabels;
my $weighting = 1/$numEdges;
unless ($seeng1edges{"$n1-$n2-$edgeLabel"})
{ print DOT "try: g1.add_edge((\"$n1\", \"$n2\"), label=\" ".$edgeLabel."\")\nexcept: pass\n\n"; }
$seeng1edges{"$n1-$n2-$edgeLabel"} = 1;
}
}
# Write out $g1 and $g2, too for display
$printgraph = $g2;
@alledges = $printgraph->edges();
my %seeng2edges;
for my $edgeArray (@alledges) {
my ($n1, $n2) = @{$edgeArray};
my @edgeLabels = $printgraph->get_multiedge_ids($n1,$n2);
for my $edgeLabel (@edgeLabels) {
print DOT "try: g2.add_node(\"$n1\", \"\")\n";
print DOT "except: pass\n";
print DOT "try: g2.add_node(\"$n2\", \"\")\n";
print DOT "except: pass\n\n";
my $numEdges = @edgeLabels;
my $weighting = 1/$numEdges;
unless ($seeng2edges{"$n1-$n2-$edgeLabel"})
{ print DOT "try: g2.add_edge((\"$n1\", \"$n2\"), label=\" ".$edgeLabel."\")\nexcept: pass\n\n"; }
$seeng2edges{"$n1-$n2-$edgeLabel"} = 1;
}
}
print DOT "# To self: I spent a long time working out why .dot files had attributes\n";
print DOT "# such as width and pos that I didn't have in my representation.\n";
print DOT "# This \"write_dot\" function is a special pydot feature that doesn't\n";
print DOT "# exist as an actual method but is, rather, generated automatically based on the extension (sort of).\n";
print DOT "# The internal representation is passed through the dot program and the output adds these extra features.\n";
print DOT "# This seems to be the same as the \"default\" ones if they aren't added.\n";
print DOT "# I looked into this to see how I should be customizing node placement.\n";
print DOT "# I guess I'd have to delve deep into dot to find out at this point.\n";
print DOT "# See comparison method in tdag for where this write_dot function is called.\n";
print DOT "pcgfolder = \"/Users/jcgood/Dropbox/TemplatesBook/PCGs/\"\n";
print DOT "draw_graphs([pcgweighted, pcglabels, g1, g2], pcgfolder)\n";