Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/GraphViz/Old/pre_pesha_graph_to_dot.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function graph_to_dot(G, varargin) | |
2 % DAG_TO_DOT Make a file representing the directed graph in dotty format. | |
3 % dag_to_dot(G, ...) | |
4 % | |
5 % Optional arguments should be passed as name/value pairs [default] | |
6 % | |
7 % 'filename' - if omitted, we write to 'tmp.dot', convert this to 'tmp.ps', | |
8 % and then call ghostview automatically | |
9 % 'arc_label' - arc_label{i,j} is a string attached to the i->j arc. [""] | |
10 % 'node_label' - node_label{i} is a string attached to node i. ["i"] | |
11 % 'width' - width in inches [10] | |
12 % 'height' - height in inches [10] | |
13 % 'leftright' - 1 means layout left-to-right, 0 means top-to-bottom [0] | |
14 % 'directed' - 1 means use directed arcs, 0 means undirected [1] | |
15 % | |
16 % For details on dotty, See http://www.research.att.com/sw/tools/graphviz | |
17 % | |
18 % Example: | |
19 % G = rand(5,5); | |
20 % names = cell(5,5); | |
21 % names{1,2} = 'arc 1-2'; | |
22 % graph_to_dot(G, 'arc_label', names) | |
23 % or graph_to_dot(G, 'arc_label', 'numbers') % prints value of G(i,j) on i->j arc | |
24 | |
25 % Kevin Murphy, 1998 | |
26 | |
27 % set default args | |
28 filename = []; | |
29 node_label = []; | |
30 arc_label = []; | |
31 width = 10; | |
32 height = 10; | |
33 leftright = 0; | |
34 directed = 1; | |
35 % get optional args | |
36 args = varargin; | |
37 for i=1:2:length(args) | |
38 switch args{i} | |
39 case 'filename', filename = args{i+1}; | |
40 case 'node_label', node_label = args{i+1}; | |
41 case 'arc_label', arc_label = args{i+1}; | |
42 case 'width', width = args{i+1}; | |
43 case 'height', height = args{i+1}; | |
44 case 'leftright', leftright = args{i+1}; | |
45 case 'directed', directed = args{i+1}; | |
46 end | |
47 end | |
48 | |
49 if isstr(arc_label) & strcmp(arc_label, 'numbers') | |
50 N = length(G); | |
51 arc_label = cell(N,N); | |
52 for i=1:N | |
53 for j=1:N | |
54 arc_label{i,j} = sprintf('%4.2f', G(i,j)); | |
55 end | |
56 end | |
57 end | |
58 | |
59 if isempty(filename) | |
60 make_file(G, 'tmp.dot', node_label, arc_label, width, height, leftright, directed); | |
61 if isunix | |
62 !dot -Tps tmp.dot -o tmp.ps | |
63 | |
64 !gs tmp.ps & | |
65 else | |
66 dos('dot -Tps tmp.dot -o tmp.ps'); | |
67 dos('gsview32 tmp.ps &'); | |
68 end | |
69 else | |
70 | |
71 | |
72 make_file(G, filename, node_label, arc_label, width, height, leftright, directed); | |
73 end | |
74 | |
75 | |
76 %%%%%% | |
77 | |
78 function make_file(G, filename, node_label, arc_label, width, height, leftright, directed) | |
79 | |
80 n = length(G); | |
81 fid = fopen(filename, 'w'); | |
82 if directed | |
83 fprintf(fid, 'digraph G {\n'); | |
84 else | |
85 fprintf(fid, 'graph G {\n'); | |
86 end | |
87 fprintf(fid, 'center = 1;\n'); | |
88 fprintf(fid, 'size=\"%d,%d\";\n', width, height); | |
89 if leftright | |
90 fprintf(fid, 'rankdir=LR;\n'); | |
91 end | |
92 for i=1:n | |
93 if isempty(node_label) | |
94 fprintf(fid, '%d;\n', i); | |
95 else | |
96 fprintf(fid, '%d [ label = "%s" ];\n', i, node_label{i}); | |
97 end | |
98 end | |
99 if directed | |
100 for i=1:n | |
101 cs = children(G,i); | |
102 for j=1:length(cs) | |
103 c = cs(j); | |
104 if isempty(arc_label) | |
105 fprintf(fid, '%d -> %d;\n', i, c); | |
106 else | |
107 fprintf(fid, '%d -> %d [label="%s"];\n', i, c, arc_label{i,c}); | |
108 end | |
109 end | |
110 end | |
111 else | |
112 for i=1:n | |
113 ns = intersect(neighbors(G,i), i+1:n); % remove duplicate arcs | |
114 for j=1:length(ns) | |
115 c = ns(j); | |
116 if isempty(arc_label) | |
117 fprintf(fid, '%d -- %d [dir=none];\n', i, c); | |
118 else | |
119 fprintf(fid, '%d -- %d [label="%s",dir=none];\n', i, c, arc_label{i,c}); | |
120 end | |
121 end | |
122 end | |
123 end | |
124 fprintf(fid, '\n}'); | |
125 fclose(fid); | |
126 | |
127 | |
128 | |
129 %%%%%%%%%%%%%%% | |
130 | |
131 function cs = children(adj_mat, i, t) | |
132 % CHILDREN Return the indices of a node's children in sorted order | |
133 % c = children(adj_mat, i, t) | |
134 % | |
135 % t is an optional argument: if present, dag is assumed to be a 2-slice DBN | |
136 | |
137 if nargin < 3 | |
138 cs = find(adj_mat(i,:)); | |
139 else | |
140 if t==1 | |
141 cs = find(adj_mat(i,:)); | |
142 else | |
143 ss = length(adj_mat)/2; | |
144 j = i+ss; | |
145 cs = find(adj_mat(j,:)) + (t-2)*ss; | |
146 end | |
147 end | |
148 | |
149 %%%%%%%%%%%% | |
150 | |
151 function ps = parents(adj_mat, i) | |
152 % PARENTS Return the list of parents of node i | |
153 % ps = parents(adj_mat, i) | |
154 | |
155 ps = find(adj_mat(:,i))'; | |
156 | |
157 %%%%%%%%%%%%% | |
158 | |
159 function ns = neighbors(adj_mat, i) | |
160 % NEIGHBORS Find the parents and children of a node in a graph. | |
161 % ns = neighbors(adj_mat, i) | |
162 | |
163 ns = union(children(adj_mat, i), parents(adj_mat, i)); | |
164 | |
165 | |
166 |