clusters.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 '''
3 Handle cluster graphs.
4 
5 A cluster graph is a networkx graph with nodes holding a letter "type
6 code" (c,w,b,s,m) and a code-dependent data structure
7 '''
8 
9 def match_dict(have, want):
10  '''
11  return True if all keys of want are in have and all their values are equal.
12  '''
13  for w in want:
14  if w not in have:
15  return False
16  if have[w] != want[w]:
17  return False
18  return True
19 
20 class ClusterMap(object):
21  '''
22  Add some indexing and lookups to meta data on cluster graph vertices
23  '''
24 
25  def __init__(self, gr):
26  self.gr = gr
27  self._id2ch = dict()
28  self._pi2ch = dict()
29  self._cs2wire = dict()
30  self._wip2wire = dict()
31  self._wid2wire = dict()
32 
33  for node, data in gr.nodes.data():
34  if data['code'] == 'c':
35  self._id2ch[data['ident']] = node
36  self._pi2ch[(data['wpid'], data['index'])] = node;
37  continue;
38  if data['code'] == 'w':
39  self._cs2wire[(data['chid'], data['seg'])] = node
40  self._wip2wire[(data['wpid'], data['index'])] = node;
41  self._wid2wire[(data['wpid'], data['ident'])] = node;
42  continue
43 
44  def channel(self, key):
45  '''
46  Return a channel node by a key. If key is scalar it is a
47  channel ident number, else assumed to be a pair of (wpid,
48  index).
49  '''
50  if type(key) == tuple:
51  return self._pi2ch[key];
52  return self._id2ch[key];
53 
54  def wire_chanseg(self, chan, seg):
55  '''
56  Return a wire node by its channel and segment
57  '''
58  return self._cs2wire[(chan,seg)]
59 
60  def wire_wip(self, wpid, wip):
61  '''
62  Return a wire node by its wire-in-plane number in the given wire-plane ID
63  '''
64  return self._wip2wire[(wpid, wip)];
65 
66  def wire_wid(self, wpid, wid):
67  '''
68  Return a wire node by its wire-ident and wire-plane ID.
69  '''
70  return self._wid2wire[(wpid, wip)];
71 
72 
73  def find(self, typecode=None, **kwds):
74  '''
75  Return nodes with data matching kwds. If typecode is given,
76  only consider nodes of that type.
77  '''
78  ret = list()
79  for node,data in self.gr.nodes.data():
80  if typecode and self.gr.nodes[node]['code'] != typecode:
81  continue;
82  if not match_dict(data,kwds):
83  continue
84  ret.append(node)
85  return ret
86 
87 
88  def nodes_oftype(self, typecode):
89  '''
90  Return a list of nodes of given type code
91  '''
92  return [n for n,d in self.gr.nodes.data() if d['code'] == typecode]
93 
94  def neighbors_oftype(self, node, typecode):
95  '''
96  Return all connected nodes of the given node and given type.
97  '''
98  ret = list()
99  for nn in self.gr[node]:
100  if self.gr.nodes[nn]['code'] == typecode:
101  ret.append(nn)
102  return ret
def neighbors_oftype(self, node, typecode)
Definition: clusters.py:94
def nodes_oftype(self, typecode)
Definition: clusters.py:88
def wire_wip(self, wpid, wip)
Definition: clusters.py:60
def find(self, typecode=None, kwds)
Definition: clusters.py:73
def match_dict(have, want)
Definition: clusters.py:9
def wire_wid(self, wpid, wid)
Definition: clusters.py:66
def wire_chanseg(self, chan, seg)
Definition: clusters.py:54