1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
function [x, y, h] = draw_graph(adj, labels, node_t, x, y, varargin)
% DRAW_LAYOUT Draws a layout for a graph
%
% [X, Y, H] = DRAW_LAYOUT(ADJ, <LABELS, ISBOX, X, Y>)
%
% Inputs :
% ADJ : Adjacency matrix (source, sink)
% LABELS : Cell array containing labels <Default : '1':'N'>
% ISBOX : 1 if node is a box, 0 if oval <Default : zeros>
% X, Y, : Coordinates of nodes on the unit square <Default : calls make_layout>
%
% Outputs :
% X, Y : Coordinates of nodes on the unit square
% H : Object handles
%
% Usage Example : [x, y] = draw_layout([0 1;0 0], {'Hidden','Visible'}, [1 0]');
%
% h(i,1) is the text handle - color
% h(i,2) is the circle handle - facecolor
%
% See also MAKE_LAYOUT
% Change History :
% Date Time Prog Note
% 13-Apr-2000 9:06 PM ATC Created under MATLAB 5.3.1.29215a (R11.1)
%
% ATC = Ali Taylan Cemgil,
% SNN - University of Nijmegen, Department of Medical Physics and Biophysics
% e-mail : cemgil@mbfys.kun.nl
adj = double(adj);
N = size(adj,1);
if nargin<2,
labels = cellstr(int2str((1:N)'));
end
if nargin<3,
node_t = zeros(N,1);
else
node_t = node_t(:);
end;
axis([0 1 0 1]);
set(gca,'XTick',[],'YTick',[],'box','on');
% axis('square');
%colormap(flipud(gray));
if nargin<4,
[x y] = make_layout(adj);
end;
idx1 = find(node_t==0); h1 = []; wd1=[];
if ~isempty(idx1)
[h1 wd1] = textoval(x(idx1), y(idx1), labels(idx1), varargin{:});
end;
idx2 = find(node_t~=0); h2 = []; wd2 = [];
if ~isempty(idx2)
[h2 wd2] = textbox(x(idx2), y(idx2), labels(idx2), varargin{:});
end;
wd = zeros(size(wd1,1)+size(wd2,1),2);
if ~isempty(idx1), wd(idx1, :) = wd1; end;
if ~isempty(idx2), wd(idx2, :) = wd2; end;
% coloring
color.box = 'black';
color.text = color.box;
color.edge = [1 1 1]*3/4;
%color.edge = 'green';
if ~isempty(idx1)
set(h1(:,1),'Color',color.text)
set(h1(:,2),'EdgeColor',color.box)
end
if ~isempty(idx2)
set(h2(:,1),'Color',color.text)
set(h2(:,2),'EdgeColor',color.box)
end
% bug: this code assumes [x y] is the center of each box and oval, which
% isn't exactly true.
h_edge = [];
for i=1:N,
j = find(adj(i,:)==1);
for k=j,
if x(k)-x(i)==0,
sign = 1;
if y(i)>y(k), alpha = -pi/2; else alpha = pi/2; end;
else
alpha = atan((y(k)-y(i))/(x(k)-x(i)));
if x(i)<x(k), sign = 1; else sign = -1; end;
end;
dy1 = sign.*wd(i,2).*sin(alpha); dx1 = sign.*wd(i,1).*cos(alpha);
dy2 = sign.*wd(k,2).*sin(alpha); dx2 = sign.*wd(k,1).*cos(alpha);
if adj(k,i)==0, % if directed edge
h = arrow([x(i)+dx1 y(i)+dy1],[x(k)-dx2 y(k)-dy2],'BaseAngle',30);
set(h, 'FaceColor', color.edge)
set(h, 'EdgeColor', color.edge)
else
h = line([x(i)+dx1 x(k)-dx2],[y(i)+dy1 y(k)-dy2]);
set(h, 'Color', color.edge)
adj(k,i)=-1; % Prevent drawing lines twice
end;
h_edge = [h_edge h];
end;
end;
if nargout>2,
h = zeros(length(wd),2);
if ~isempty(idx1),
h(idx1,:) = h1;
end;
if ~isempty(idx2),
h(idx2,:) = h2;
end;
end;
%%%%%
function [t, wd] = textoval(x, y, str, varargin)
% TEXTOVAL Draws an oval around text objects
%
% [T, WIDTH] = TEXTOVAL(X, Y, STR)
% [..] = TEXTOVAL(STR) % Interactive
%
% Inputs :
% X, Y : Coordinates
% TXT : Strings
%
% Outputs :
% T : Object Handles
% WIDTH : x and y Width of ovals
%
% Usage Example : [t] = textoval('Visit to Asia?');
%
%
% Note :
% See also TEXTBOX
% Uses :
% Change History :
% Date Time Prog Note
% 15-Jun-1998 10:36 AM ATC Created under MATLAB 5.1.0.421
% 12-Mar-2004 10:00 AM minka Changed placement/sizing.
%
% ATC = Ali Taylan Cemgil,
% SNN - University of Nijmegen, Department of Medical Physics and Biophysics
% e-mail : cemgil@mbfys.kun.nl
temp = [];
textProperties = {'BackgroundColor','Color','FontAngle','FontName','FontSize','FontUnits','FontWeight','Rotation'};
varargin = argfilter(varargin,textProperties);
if nargin == 1
str = x;
end
if ~isa(str,'cell') str=cellstr(str); end;
N = length(str);
wd = zeros(N,2);
for i=1:N,
if nargin == 1
[x, y] = ginput(1);
end
tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:});
% minka
[ptc wx wy] = draw_oval(tx);
wd(i,:) = [wx wy];
% draw_oval will paint over the text, so need to redraw it
delete(tx);
tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:});
temp = [temp; tx ptc];
end
if nargout>0, t = temp; end;
%%%%%%%%%
function [ptc, wx, wy] = draw_oval(tx, x, y)
% Draws an oval box around a tex object
sz = get(tx,'Extent');
% minka
wy = 2/3*sz(4);
wx = 2/3*sz(3);
x = sz(1)+sz(3)/2;
y = sz(2)+sz(4)/2;
ptc = ellipse(x, y, wx, wy);
set(ptc, 'FaceColor','w');
%%%%%%%%%%%%%
function [p] = ellipse(x, y, rx, ry, c)
% ELLIPSE Draws Ellipse shaped patch objects
%
% [<P>] = ELLIPSE(X, Y, Rx, Ry, C)
%
% Inputs :
% X : N x 1 vector of x coordinates
% Y : N x 1 vector of y coordinates
% Rx, Ry : Radii
% C : Color index
%
%
% Outputs :
% P = Handles of Ellipse shaped path objects
%
% Usage Example : [] = ellipse();
%
%
% Note :
% See also
% Uses :
% Change History :
% Date Time Prog Note
% 27-May-1998 9:55 AM ATC Created under MATLAB 5.1.0.421
% ATC = Ali Taylan Cemgil,
% SNN - University of Nijmegen, Department of Medical Physics and Biophysics
% e-mail : cemgil@mbfys.kun.nl
if (nargin < 2) error('Usage Example : e = ellipse([0 1],[0 -1],[1 0.5],[2 0.5]); '); end;
if (nargin < 3) rx = 0.1; end;
if (nargin < 4) ry = rx; end;
if (nargin < 5) c = 1; end;
if length(c)==1, c = ones(size(x)).*c; end;
if length(rx)==1, rx = ones(size(x)).*rx; end;
if length(ry)==1, ry = ones(size(x)).*ry; end;
n = length(x);
p = zeros(size(x));
t = 0:pi/30:2*pi;
for i=1:n,
px = rx(i)*cos(t)+x(i);
py = ry(i)*sin(t)+y(i);
p(i) = patch(px,py,c(i));
end;
if nargout>0, pp = p; end;
%%%%%
function [t, wd] = textbox(x,y,str,varargin)
% TEXTBOX Draws A Box around the text
%
% [T, WIDTH] = TEXTBOX(X, Y, STR)
% [..] = TEXTBOX(STR)
%
% Inputs :
% X, Y : Coordinates
% TXT : Strings
%
% Outputs :
% T : Object Handles
% WIDTH : x and y Width of boxes
%%
% Usage Example : t = textbox({'Ali','Veli','49','50'});
%
%
% Note :
% See also TEXTOVAL
% Uses :
% Change History :
% Date Time Prog Note
% 09-Jun-1998 11:43 AM ATC Created under MATLAB 5.1.0.421
% 12-Mar-2004 10:00 AM minka Changed placement/sizing.
%
% ATC = Ali Taylan Cemgil,
% SNN - University of Nijmegen, Department of Medical Physics and Biophysics
% e-mail : cemgil@mbfys.kun.nl
temp = [];
textProperties = {'BackgroundColor','Color','FontAngle','FontName','FontSize','FontUnits','FontWeight','Rotation'};
varargin = argfilter(varargin,textProperties);
if nargin == 1
str = x;
end
if ~isa(str,'cell') str=cellstr(str); end;
N = length(str);
wd = zeros(N,2);
for i=1:N,
if nargin == 1
[x, y] = ginput(1);
end
tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:});
% minka
[ptc wx wy] = draw_box(tx);
wd(i,:) = [wx wy];
% draw_box will paint over the text, so need to redraw it
delete(tx);
tx = text(x(i),y(i),str{i},'HorizontalAlignment','center',varargin{:});
temp = [temp; tx ptc];
end;
if nargout>0, t = temp; end;
function [ptc, wx, wy] = draw_box(tx)
% Draws a box around a text object
sz = get(tx,'Extent');
% minka
wy = 1/2*sz(4);
wx = 1/2*sz(3);
x = sz(1)+sz(3)/2;
y = sz(2)+sz(4)/2;
ptc = patch([x-wx x+wx x+wx x-wx], [y+wy y+wy y-wy y-wy],'w');
set(ptc, 'FaceColor','w');
function args = argfilter(args,keep)
%ARGFILTER Remove unwanted arguments.
% ARGFILTER(ARGS,KEEP), where ARGS = {'arg1',value1,'arg2',value2,...},
% returns a new argument list where only the arguments named in KEEP are
% retained. KEEP is a character array or cell array of strings.
% Written by Tom Minka
if ischar(keep)
keep = cellstr(keep);
end
i = 1;
while i < length(args)
if ~ismember(args{i},keep)
args = args(setdiff(1:length(args),[i i+1]));
else
i = i + 2;
end
end
|