comparison toolboxes/FullBNT-1.0.7/GraphViz/draw_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 draw_dot(adj,varargin);
2 %DRAW_DOT Draw a graph.
3 % DRAW_DOT(ADJ) plots the graph ADJ in the current figure window, using
4 % 'neato' to optimize the layout.
5 %
6 % Optional arguments can be passed as name/value pairs: [default]
7 %
8 % 'isbox' - a vector specifying which nodes should be boxed [0]
9 % 'rotate' - rotate the graph so that nodes are vertically aligned [1]
10 % 'tolerance' - alignment tolerance for 'rotate' [0.001]
11 % 'start' - a random seed (to select different solutions)
12 % 'options' - a string of command-line options for 'neato' ['']
13 % All of the optional arguments to graph_to_dot are also supported, such as
14 % 'node_label'.
15 %
16 % See also GRAPH_TO_DOT.
17 %
18 % Example:
19 % size=15; Adj = rand(size) > .8;
20 % Adj2 = triu(Adj,1)+ triu(Adj,1)' + diag(zeros(size,1));
21 % draw_dot(Adj2)
22
23 % Original: Leon Peshkin
24 % Modified by Tom Minka
25
26 % minka
27 N = size(adj,1);
28 unique_labels = cellstr(num2str((1:N)','%-1d'));
29 labels = unique_labels;
30 isbox = zeros(N,1);
31 rotate_flag = 1;
32 tolerance = 0.001;
33 options = '';
34 for i = 1:2:length(varargin)
35 switch varargin{i}
36 case 'node_label', labels = varargin{i+1};
37 % replace with unique labels
38 varargin{i+1} = unique_labels;
39 case 'isbox', isbox = varargin{i+1};
40 case 'rotate', rotate_flag = varargin{i+1};
41 case 'tolerance', tolerance = varargin{i+1};
42 case 'start', start = varargin{i+1};
43 options = [options ' -Gstart=' num2str(start)];
44 case 'options', options = [options ' ' varargin{i+1}];
45 end
46 end
47
48 if ispc, shell = 'dos'; else, shell = 'unix'; end % Which OS ?
49
50 cmdline = strcat(shell,'(''neato -V'')');
51 status = eval(cmdline);
52 %[status, result] = dos('neato -V'); % request version to check NEATO
53 if status == 1, fprintf('Complaining \n'); exit, end
54
55 tmpDOTfile = '_GtDout.dot'; % to be platform independant no use of directories
56 tmpLAYOUT = '_LAYout.dot';
57 graph_to_dot(adj > 0, 'filename', tmpDOTfile, 'node_label', unique_labels, varargin{:}); % save in file
58
59 cmdline = strcat([shell '(''neato -Tdot ' tmpDOTfile options ' -o ' tmpLAYOUT ''')']); % preserve trailing spaces
60 status = eval(cmdline); % get NEATO todo layout
61
62 [adj, permuted_labels, x, y] = dot_to_graph(tmpLAYOUT); % load layout
63 delete(tmpLAYOUT); delete(tmpDOTfile); % clean up temporary files
64
65 % permute the original arguments to match permuted_labels.
66 order = [];
67 for i = 1:length(permuted_labels)
68 j = strmatch(permuted_labels{i},unique_labels,'exact');
69 order(i) = j(1);
70 end
71 labels = labels(order);
72 isbox = isbox(order);
73 if rotate_flag
74 [x,y] = best_rotation(x,y,tolerance);
75 end
76
77 figure(1); clf; axis square % now plot
78 [x, y, h] = draw_graph(adj>0, labels, isbox, x, y, varargin{:});
79
80
81 function [x,y] = best_rotation(x,y,h)
82 % Rotate the points to maximize the horizontal and vertical alignment.
83 % Written by Tom Minka.
84
85 xm = mean(x);
86 ym = mean(y);
87 xr = max(x)-min(x);
88 yr = max(y)-min(y);
89 x = (x-xm)/xr;
90 y = (y-ym)/yr;
91
92 xy = [x(:) y(:)];
93 if 1
94 angle = fminbnd(@rotation_cost,-pi/4,pi/4,[],xy,h);
95 else
96 angles = linspace(-pi/4,pi/4,40);
97 e = [];
98 for i = 1:length(angles)
99 e(i) = rotation_cost(angles(i),xy,h);
100 end
101 %figure(2)
102 %plot(angles*180/pi,e)
103 angle = angles(argmin(e));
104 end
105 %angle*180/pi
106 c = cos(angle); s = sin(angle);
107 xy = xy*[c s; -s c];
108
109 x = xy(:,1)*xr+xm;
110 y = xy(:,2)*yr+ym;
111
112
113 function e = rotation_cost(angle,xy,h)
114 % xy is 2-column matrix.
115 % e is small if many x's and y's are aligned.
116
117 c = cos(angle); s = sin(angle);
118 xy = xy*[c s; -s c];
119 dx = sqdist(xy(:,1)',xy(:,1)');
120 dy = sqdist(xy(:,2)',xy(:,2)');
121 dx = setdiag(dx,Inf);
122 dy = setdiag(dy,Inf);
123 e = sum(exp(-dx(:)/h))+sum(exp(-dy(:)/h));
124 e = -e;