-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimage_representation.py
More file actions
218 lines (176 loc) · 8.01 KB
/
image_representation.py
File metadata and controls
218 lines (176 loc) · 8.01 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
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 14 12:42:55 2020
@author: Zeki
"""
import numpy as np
import pycparser
import csv
from pycparser import c_parser, c_ast
from nodeencoding import map
# This script generates AST-based image representation of code
def ast2img(root, x,y,z, deltab, childnumbers):
img = np.full((x, y,z), 255, dtype=int) #blank image initilization
delta2 = deltab #interval, i.e. the number of pixels between nodes on the same level
visited = [] # List to keep track of visited nodes.
queue = [] #Initialize a queue
tot = 0
visited.append((root,0,0,0)) # second element is the level, the third element is its parent's level, the fourth is its parents horizontal position
queue.append((root,0,0,0))
previous = (0,0)
placehorizontal = 0
level = 0
childno = 0
nodechildno = 0
flag = 0
flag2 = True
last = 0
while queue:
s = queue.pop(0)
current_level = s[1]
index0 = 2*sum(childnumbers[k]+1 for k in range(current_level+1))
if current_level == 0: # i.e. root
childno = childno + 1
img[index0][childno*delta2] = map(s[0])
img[index0][childno*delta2-1] = map(s[0])
img[index0][childno*delta2+1] = map(s[0])
horizontal = childno*delta2
elif current_level == level: # we are proceeding on the same level
childno = childno + 1
if s[3] <= childno*delta2: # s[3] is the horizontal location of the parent node
index = max(childno*delta2,placehorizontal+delta2)
if index0 < x: #draw only if it is in the image frame
if index < y:
img[index0][index] = map(s[0])
if index-1 < y:
img[index0][index-1] = map(s[0])
if index+1 < y:
img[index0][index+1] = map(s[0])
horizontal = index
flag = 0
else:
if (s[2],s[3]) == previous: # if the previous node is my sibling
flag = flag + 1
index = max(s[3] + flag*delta2,placehorizontal+delta2)
if index0 < x: #draw only if it is in the image frame
if index < y:
img[index0][index] = map(s[0])
if index-1 < y:
img[index0][index-1] = map(s[0])
if index+1 < y:
img[index0][index+1] = map(s[0])
horizontal = index
else:
index = max(s[3],placehorizontal+delta2)
if index0 < x:
if index < y:
img[index0][index] = map(s[0])
if index-1 < y:
img[index0][index-1] = map(s[0])
if index+1 < y:
img[index0][index+1] = map(s[0])
flag = 0
horizontal = index
else: # we pass to a new level
level = current_level
childno = 1
flag = 0
if s[3] <= childno*delta2:
index = childno*delta2
if index0 < x:
if index < y:
img[index0][index] = map(s[0])
if index-1 < y:
img[index0][index-1] = map(s[0])
if index+1 < y:
img[index0][index+1] = map(s[0])
horizontal = index
flag = 0
else:
if (s[2],s[3]) == previous:
flag = flag + 1
#print("flag ", flag)
index = s[3] + flag*delta2
if index0 < x:
if index < y:
img[index0][index] = map(s[0])
if index-1 < y:
img[index0][index-1] = map(s[0])
if index+1 < y:
img[index0][index+1] = map(s[0])
horizontal = index
else:
index = max(s[3],placehorizontal)
if index0 < x:
if index < y:
img[index0][index] = map(s[0])
if index-1 < y:
img[index0][index-1] = map(s[0])
if index+1 < y:
img[index0][index+1] = map(s[0])
flag = 0
horizontal = index
# draw lines between the nodes
if current_level != 0:
if (s[2],s[3]) != previous:
for i in range(1,childno*2):
if index0-i < x and horizontal < y:
img[index0-i][horizontal] = 0
else:
for i in range(1,3):
if index0-i < x and horizontal < y:
img[index0-i][horizontal] = 0
if s[3] > horizontal:
direction = 1
elif s[3] == horizontal:
direction = 0
else:
direction = -1
last = horizontal
flag2 = True
for i in range(abs(placehorizontal-horizontal)):
if (s[2],s[3]) == previous:
if flag2:
flag2 = False
if index0-2 < x and horizontal+direction*i < y:
img[index0-2][horizontal+direction*i] = 0
last = horizontal+direction*(i+1)
else:
if index0-childno*2 < x and horizontal+direction*i < y:
img[index0-childno*2][horizontal+direction*i] = 0
last = horizontal+direction*(i+1)
if (s[2],s[3]) != previous and s[3] < horizontal:
for i in range(abs(s[3]-horizontal)):
if index0-childno*2 < x and horizontal+direction*i < y:
img[index0-childno*2][horizontal+direction*i] = 0
last = horizontal+direction*(i+1)
lastver = index0-childno*2
if (s[2],s[3]) != previous: # draw vertical line to parent node
for i in range(0,abs(index0-2*sum(childnumbers[k]+1 for k in range(current_level))-childno*2)):
if lastver-i < x and last < y:
img[lastver-i][last] = 0
for neighbour in s[0]:
if neighbour not in visited:
visited.append((neighbour,current_level+1,current_level, horizontal)) # the third and the fourth elements represent location of its parent
queue.append((neighbour,current_level+1,current_level, horizontal))
previous = (s[2],s[3])
placehorizontal = horizontal
return img
def rowdelete(image, h, w):
imagecompact = np.full((h,w,3), 255, dtype=int)
k= 0
for i in range(len(image)-1):
temp = image[i] == image[i+1]
if temp.all():
continue
else:
imagecompact[k] = image[i]
k = k + 1
for i in range(len(imagecompact)):
if i > k:
imagecompact = np.delete(imagecompact,(k), axis=0)
return imagecompact
def columndelete(imagecompact):
idx = np.argwhere(np.all(imagecompact[..., :] == [255,255,255], axis=0))
imagecompact2 = np.delete(imagecompact, idx, axis = 1)
return imagecompact2