3 Wire Cell Data Flow Programming Graph 7 from collections
import namedtuple
9 import pygraphviz
as pgv
11 Graph = nx.MultiDiGraph
13 def key(type, name=None):
15 Return a graph key for a node's type and name. 24 Return (type,name) tuple from key 28 return tuple(key.split(
':',1))
30 def connect(nxgraph, k1, k2, p1=0, p2=0, **kwds):
32 Connect node at key k1, port p1 to node k2, port p2 34 nxgraph.add_edge(k1, k2, tail_port=p1, head_port=p2, **kwds)
38 if got.difference(want):
39 raise ValueError,
"Got more ports for %s got:%d want:%d" % (what, len(got), len(want))
40 if want.difference(got):
41 raise ValueError,
"Want more ports for %s got:%d want:%d" % (what, len(got), len(want))
46 '''Validate a graph against the node descriptors. 47 The <desc> is a dictionary keyed by type with values of wirecell.dfp.nodetype.NodeType.''' 49 for nn
in nxgraph.nodes():
50 nd = desc[
dekey(nn)[0]]
53 set([d[
'head_port']
for t,h,d
in nxgraph.in_edges(nn, data=
True)]),
54 set(range(len(nd.input_types))))
57 set([d[
'tail_port']
for t,h,d
in nxgraph.out_edges(nn, data=
True)]),
58 set(range(len(nd.output_types))))
60 for t,h,dat
in nxgraph.edges(data=
True):
61 tail = desc[
dekey(t)[0]]
62 head = desc[
dekey(h)[0]]
67 if tail.output_types[tp] != head.input_types[hp]:
68 raise ValueError,
'Port data type mismatch for t1=%s[%d] and t2=%s[%d]:\nt1: %s\nt2: %s\n' % \
71 tail.output_types[tp],
79 '''Return nxgraph as a JSON string suitable for inclusion into a 80 wire-cell configuration file for giving to a WireCell::DfpGraph. 83 for t,h,dat
in nxgraph.edges(data=
True):
84 ttype, tname =
dekey(t)
85 htype, hname =
dekey(h)
87 tail = dict(type=ttype, name=tname, port=dat[
'tail_port']),
88 head = dict(type=htype, name=hname, port=dat[
'head_port']))
def port_compare(what, got, want)
def connect(nxgraph, k1, k2, p1=0, p2=0, kwds)
def wirecell_graph(nxgraph)
def validate(nxgraph, desc)