test_wireplots.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 from wirecell import units
3 from wirecell.util.wires import apa, graph
4 from itertools import chain
5 from collections import defaultdict
6 import networkx
7 import matplotlib.pyplot as plt
8 from matplotlib.backends.backend_pdf import PdfPages
9 
10 desc = apa.Description();
11 G,P = apa.graph(desc)
12 
13 xkcd_color = ['xkcd:red', 'xkcd:tangerine', 'xkcd:goldenrod',
14  'xkcd:green', 'xkcd:blue', 'xkcd:purple',
15  'xkcd:dark purple',
16  'xkcd:violet',
17  'xkcd:indigo',
18  'xkcd:black',
19  'xkcd:bright blue',
20  'xkcd:baby blue',
21  'xkcd:royal blue',
22  'xkcd:periwinkle',
23  'xkcd:fuchsia',
24  'xkcd:aqua green',
25  ]
26 
27 
29  inp = graph.wires_in_plane(G, P.plane[0])
30  inc = graph.wires_in_chip(G, P.chip[8])
31  wires = inp.intersection(inc)
32  wg,wpos = graph.wires_graph(G, wires)
33  networkx.draw(wg, pos=wpos, with_labels=True,width=0.1)
34  plt.savefig('test_plot_plane_chip.pdf')
35 
37  wg, wpos = graph.conductors_graph(G, [P.conductor[0]])
38  networkx.draw(wg, pos=wpos, with_labels=True)
39  plt.savefig('test_plot_conductor.pdf')
40 
42  with PdfPages('test_plot_chip.pdf') as pdf:
43  for chip in P.chip:
44  plt.title(chip)
45  channels = graph.neighbors_by_type(G, chip, 'channel')
46  conductors = list(chain.from_iterable([graph.neighbors_by_type(G, ch, 'conductor') for ch in channels]))
47  wg, wpos = graph.conductors_graph(G, conductors)
48 
49  edges = wg.edges(data=True)
50  styles = list()
51  colors = list()
52  for n1,n2,dat in edges:
53  styles.append(dat['style'])
54  icolor = dat['icolor']
55  colors.append(xkcd_color[icolor%len(xkcd_color)])
56  networkx.draw_networkx_edges(wg, pos=wpos, style=styles,
57  width=0.1, arrows=False,
58  edge_color=colors)
59  print chip,len(channels),len(conductors)
60  pdf.savefig()
61  plt.close()
62 
63 
64 def test_plot_board(debug=False):
65  penwidth=0.05
66  if debug:
67  penwidth=0.5
68  with PdfPages('test_plot_board.pdf') as pdf:
69  for iboard, board in enumerate(P.board):
70 
71  plt.title("Board %d" % (iboard+1,))
72 
73  bg = networkx.DiGraph()
74  bpos = dict()
75  chips = graph.neighbors_by_type(G, board, 'chip')
76  edges = list()
77  colors = list()
78  styles = list()
79  print 'board:',board, len(chips), chips
80  for ichip, chip in enumerate(chips):
81  channels = graph.neighbors_by_type(G, chip, 'channel')
82  conductors = chain.from_iterable([graph.neighbors_by_type(G, ch, 'conductor') for ch in channels])
83  cg, cpos = graph.conductors_graph(G, conductors)
84 
85  cstyles = [e[2]['style'] for e in cg.edges(data=True)]
86  cedges = list(cg.edges())
87  n_edges = len(cedges)
88  colors += [xkcd_color[ichip]]*n_edges
89  styles += cstyles
90  edges += cedges
91 
92  bg = networkx.compose(bg, cg)
93  bpos.update(cpos)
94 
95  networkx.draw_networkx_edges(bg, edgelist=edges,
96  pos=bpos, style=styles,
97  edge_color=colors,
98  width=penwidth, arrows=False)
99  assert(len(edges) == len(colors))
100  if debug:
101  plt.xlim(-1300,-700)
102  plt.ylim(2300,3000)
103  return
104  plt.axes().set_aspect('equal', 'box')
105  plt.xlabel('$z_c$ (mm)')
106  plt.ylabel('$y_c$ (mm)')
107  pdf.savefig(dpi=900)
108  plt.close()
109 
110 
112  with PdfPages('test_plot_wib_wires.pdf') as pdf:
113 
114  boards_by_connector = defaultdict(list)
115  for wib in P.wib:
116  boards_on_wib = graph.neighbors_by_type(G, wib, 'board')
117  for b in boards_on_wib:
118  iconnector = G[wib][b]['connector']
119  boards_by_connector[iconnector].append((wib,b))
120  print boards_by_connector
121  for iconnector, wib_boards in sorted(boards_by_connector.items()):
122  for wib,board in wib_boards:
123  plt.title("%s connector %s %s" % (wib, iconnector, board))
124  bg = networkx.DiGraph()
125  bpos = dict()
126  chips = graph.neighbors_by_type(G, board, 'chip')
127  for chip in chips:
128  channels = graph.neighbors_by_type(G, chip, 'channel')
129  print chip, len(channels)
130  conductors = chain.from_iterable([graph.neighbors_by_type(G, ch, 'conductor') for ch in channels])
131  cg, cpos = graph.conductors_graph(G, conductors)
132  bg = networkx.compose(bg, cg)
133  bpos.update(cpos)
134  networkx.draw(bg, pos=bpos, arrows=False, width=0.1)
135  pdf.savefig()
136  plt.close()
137 
139  'Plot all wib connections'
140 
141  newG = networkx.Graph()
142  newP = dict()
143 
144  conn_colors = ['xkcd:sky blue', 'xkcd:deep pink',
145  'xkcd:sky blue', 'xkcd:deep pink']
146 
147  def wib_pos(slot):
148  return (-2 + slot - 0.5, 0)
149  def conn_pos(slot, conn):
150  scale=0.4
151  square_pts = [(-1,-1), (1,-1), (1,1), (-1,1)]
152  line_pts = [(0,-2),(0,-1),(0,1),(0,2)]
153  pts = line_pts
154 
155  wp = wib_pos(slot)
156  pt = pts[conn]
157  return (wp[0]+scale*pt[0], wp[1]+scale*pt[1])
158  def board_pos(side, spot):
159  if side == 0:
160  return (-5 + spot, -3)
161  return (4 - spot, +3)
162 
163  apa = P.apa
164 
165  for wib in P.wib:
166  nwib = "w%d"%(int(wib[3:])+1,)
167  newG.add_node(nwib, color="xkcd:white", shape='o')
168 
169 
170  islot = G[apa][wib]['slot']
171  newP[nwib] = wib_pos(islot)
172  boards_on_wib = graph.neighbors_by_type(G, wib, 'board')
173  for board in boards_on_wib:
174  face = list(graph.neighbors_by_type(G, board, 'face'))[0]
175  ispot = G[face][board]['spot']
176 
177  iside = G[face][apa]['side']
178  nboard = "b%d"%(int(board[5:])+1,)
179 
180  iconn = G[wib][board]['connector']
181  nconn = "%d%d" % (islot+1, iconn+1)
182  newG.add_node(nconn, color=conn_colors[iconn], shape='d')
183  newP[nconn] = conn_pos(islot, iconn)
184 
185  newG.add_node(nboard, color=conn_colors[iconn], shape='s')
186  newP[nboard] = board_pos(iside, ispot)
187 
188  newG.add_edge(nwib,nconn)
189  newG.add_edge(nconn,nboard)
190 
191 
192  colors = [n[1]['color'] for n in newG.nodes(data=True)]
193  shapes = [n[1]['shape'] for n in newG.nodes(data=True)]
194 
195  networkx.draw(newG, pos=newP,
196  node_color=colors,
197 # node_shape=shapes,
198  with_labels=True)
199 
200  plt.savefig('test_plot_wib.pdf')
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def test_plot_board(debug=False)
def test_plot_conductor()
def test_plot_wib_wires()
def test_plot_plane_chip()