annotate toolboxes/FullBNT-1.0.7/GraphViz/draw_dot.m @ 0:cc4b1211e677 tip

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