-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
executable file
·169 lines (155 loc) · 5.23 KB
/
plot.py
File metadata and controls
executable file
·169 lines (155 loc) · 5.23 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
import matplotlib
matplotlib.use('Agg')
from matplotlib import patches
import matplotlib.pyplot as plt
import sys
import shapely
from shapely import geometry
import numpy as np
import load_bookshelf
def plot_circuit(circuit_name, components, comp2rot, nets, board_dim,figname=None, stats = None):
"""
board dim: [[xs],[ys]]
"""
board_lower_left_corner = [min(board_dim[0]), min(board_dim[1])]
board_upper_right_corner = [max(board_dim[0]), max(board_dim[1])]
fig, ax = plt.subplots(1)
if stats is None:
ax.set_title(circuit_name)
else:
ax.set_title("circuit: " + circuit_name+ " wirelength: " + str(round(stats[0],2)) + " overlap: " + str(round(stats[1],2)))
boundary = patches.Rectangle((min(board_dim[0]), min(board_dim[1])), \
max(board_dim[0]) - min(board_dim[0]), max(board_dim[1]) - min(board_dim[1]), \
linewidth=1,edgecolor='b',facecolor='none')
ax.add_patch(boundary)
for name,shape in components.items():
if isinstance(shape, geometry.Point):
x = shape.x
y = shape.y
ax.scatter(x,y, c='b',s=5)
label = ax.annotate(name, xy=(x, y), fontsize=5, ha="right", va="top", )
else:
x, y = shape.exterior.coords.xy
c = shape.centroid
points = np.array([x, y], np.int32).T
polygon_shape = patches.Polygon(points, linewidth=1, edgecolor='r', facecolor='none')
ax.add_patch(polygon_shape)
label = ax.annotate(name, xy=(c.x, c.y), fontsize=5, ha="right", va="top")
# draw nets
for net in nets:
netlist = []
c = [np.random.rand(3)] # random color
for pin in net:
if pin[0] not in components:
try:
cx = pin[1].x
cy = pin[1].y
except:
cx = pin[1][0]
cy = pin[1][1]
else:
cx, cy = pin_pos2(pin,components,comp2rot)
netlist.append([cx,cy])
xmax = max([p[0] for p in netlist])
xmin = min([p[0] for p in netlist])
ymax = max([p[1] for p in netlist])
ymin = min([p[1] for p in netlist])
center = [(xmax + xmin)/2,(ymax + ymin)/2]
for i in range(len(netlist)):
ax.plot([netlist[i][0],center[0]],[netlist[i][1],center[1]], color=tuple(map(tuple, c))[0] + (255,), linewidth=1, alpha=0.25, linestyle='dashed')
xs= [ x[0] for x in netlist ]
ys= [ x[1] for x in netlist ]
ax.scatter(xs,ys,marker='.',c=c)
ax.scatter(center[0],center[1],marker='.',c=c)
plt.xlim(board_lower_left_corner[0] - 50,board_upper_right_corner[0] + 50)
plt.ylim(board_lower_left_corner[1] - 50,board_upper_right_corner[1] + 50)
plt.gca().set_aspect('equal', adjustable='box')
#plt.show()
if figname:
plt.savefig(figname)
else:
plt.show()
def pin_pos2(pin_loc, modules,comp2rot):
"""
Convert localized pin positions to position wrt
global coordinates
:param pin_loc: pin location of the form [pinname, [xoffset, yoffset]]
:param modules: list of modules
"""
module_name, local_pin_loc = pin_loc
cx = modules[module_name].centroid.x
cy = modules[module_name].centroid.y
if module_name in comp2rot:
r = comp2rot[module_name]
else:
r = 'N'
if r == 'N':
pinx = cx + local_pin_loc[0]
piny = cy + local_pin_loc[1]
elif r == 'S':
pinx = cx - local_pin_loc[0]
piny = cy - local_pin_loc[1]
elif r == 'E':
pinx = cx - local_pin_loc[1]
piny = cy + local_pin_loc[0]
elif r == 'W':
pinx = cx + local_pin_loc[1]
piny = cy - local_pin_loc[1]
return pinx, piny
def pin_pos(pin_loc, modules):
"""
Convert localized pin positions to position wrt
global coordinates
:param pin_loc: pin location of the form [pinname, [%x, %y]]
:param modules: list of modules
"""
module_name, local_pin_loc = pin_loc
minx, miny, maxx, maxy = modules[module_name].bounds
pinx = (maxx - minx) * local_pin_loc[0] + minx
piny = (maxy - miny) * local_pin_loc[1] + miny
return pinx, piny
"""
arg 1: dir/circuitname
arg 2: bookshelf version (0,1) = (old,new)
arg 3: figname
optional
arg 4: min plot dimension
arg 5: max plot dimension
"""
circuitname = sys.argv[1]
plfile = circuitname + '.pl'
nodesfile = circuitname + '.nodes'
netsfile = circuitname + '.nets'
bversion = int(sys.argv[2])
boarddim = None
figname = sys.argv[3]
if len(sys.argv) > 4:
boarddimmin = int(sys.argv[4])
boarddimmax = int(sys.argv[5])
if bversion == 0: # old version of bookshelf
components, board_pins = load_bookshelf.read_pl(plfile)
nets,mod2net = load_bookshelf.read_nets(netsfile,components,board_pins)
if board_pins is not None and len(board_pins) > 0:
xs = [pin[1].x for pin in board_pins.items()]
ys = [pin[1].y for pin in board_pins.items()]
board_dim = [xs,ys]
else:
board_dim = [[-500,500],[-500,500]]
plot_circuit(plfile.split('.')[0], components,{},nets,board_dim,figname)
elif bversion == 1: # new version of bookshelf
#components,_ = load_bookshelf.read_pl(plfile)
components = load_bookshelf.read_nodes(nodesfile)
components,comp2rot,board_pins = load_bookshelf.read_pl2(plfile,components)
nets,mod2net = load_bookshelf.read_nets2(netsfile,components,board_pins)
if boarddimmin is None:
if board_pins is not None and len(board_pins) > 0:
xs = [pin[1].x for pin in board_pins.items()]
ys = [pin[1].y for pin in board_pins.items()]
board_dim = [xs,ys]
else:
board_dim = [[-500,500],[-500,500]]
else:
board_dim = [[boarddimmin,boarddimmax],[boarddimmin, boarddimmax]]
plot_circuit(plfile.split('.')[0], components,comp2rot,nets,board_dim,figname)
else:
print('bookshelf version must be 0 or 1')