-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathosl_braingraph.m
286 lines (238 loc) · 7.79 KB
/
osl_braingraph.m
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
function osl_braingraph(G, Glims, NodeValues, NodeLims, mnipos, labels, thresh, spherecols, scaleEdges)
% Pretty graph plotting
% osl_braingraph(G, Glims, NodeValues, NodeLims, mnipos, labels, thresh, spherecols, edgeScales)
% --------------------------------------------------------------
% G - a matrix describing the graph connections (or [] for no connections)
% Glims - threshold for G colourmap (or [] for default size)
% NodeValues - scalar value for each node size
% NodeLims - size limits for NodeValues
% mnipos - mni coordinates of nodes
% labels - text labels for nodes (or [] for no labels)
% thresh - percentile threshold above which graph connections are displayed
% spherecols - [r g b] colours for each node
% edgeScales - limits for linewidths when drawing edges - leave empty to
% have constant thickness, or set a range such as [2 15].
% --------------------------------------------------------------
% Adam Baker 2012
OSLDIR = getenv('OSLDIR');
cla, hold on
% prettiness = 0.1; % not very pretty
prettiness = 0.5; % quite pretty
% prettiness = 1; % very pretty
brain_shading = repmat(0.5,1,3); brain_alpha = 0.1;
mesh = export(gifti([OSLDIR '/spm12/canonical/cortex_5124.surf.gii']), 'spm');
mesh = struct('faces',mesh.face,'vertices',mesh.vert);
mesh = reducepatch(mesh,prettiness);
trisurf(mesh.faces,mesh.vertices(:,1),mesh.vertices(:,2),mesh.vertices(:,3),'facecolor',brain_shading,'edgecolor','none','FaceAlpha',brain_alpha)
if ~isempty(G)
G(logical(eye(size(G)))) = 0;
if islogical(G)
G = single(G);
G(G==0) = nan;
else
if exist('thresh','var') && ~isempty(thresh)
Gt = G(~tril(ones(size(G))));
G(abs(G)<percentile(abs(Gt),thresh))=nan;
end
end
if isempty(Glims)
Glims = [-max(abs(G(:))) max(abs(G(:)))];
end
if Glims(1) >= Glims(2)
Glims(2) = Glims(1)+0.1;
end
end
if isempty(NodeLims)
NodeLims = [0 max(abs(NodeValues(:)))];
end
NodeValues = (NodeValues - NodeLims(1)) ./ (NodeLims(2) - NodeLims(1));
NodeValues = NodeValues*8;
cmap = colormap(bluewhitered(256));
% allow conversion to one-colour
if Glims(1) >= 0,
cmap = cmap(129:end,:);
isSingleColour = true;
elseif Glims(2) <= 0,
cmap = cmap(1:128,:);
isSingleColour = true;
else
isSingleColour = false;
end
if ~exist('scaleEdges', 'var') || isempty(scaleEdges),
scaleEdges = [5 5];
end
weightMap = linspace(scaleEdges(1), scaleEdges(2), size(cmap,1));
if ~isempty(G)
[i,j] = find(~isnan(G));
for p=length(i):-1:1,
colorInd(p) = closest(G(i(p),j(p)),linspace(Glims(1),Glims(2),size(cmap,1)));
end
for p=1:length(i),
edgecolour = cmap(colorInd(p),:);
if exist('scaleEdges', 'var') && ~isempty(scaleEdges),
if isSingleColour,
weightInd = closest(colorInd(p), linspace(min(colorInd), max(colorInd), size(cmap, 1)));
else
weightInd = 2*closest(abs(colorInd(p) - round(size(cmap,1)./2)), linspace(0, round(max(colorInd)./2), round(size(cmap, 1)./2)));
end
edgeWeight = weightMap(weightInd);
else
edgeWeight = 3;
end
line(mnipos([i(p) j(p)],1),mnipos([i(p) j(p)],2),mnipos([i(p) j(p)],3),'color',edgecolour,'linewidth',edgeWeight)
end
set(gca,'clim',Glims);
colormap(bluewhitered(256));
hc = colorbar;
FONTSIZE = 14;
if isSingleColour,
YTicks = [Glims(1) Glims(2)];
else
YTicks = [Glims(1) 0 Glims(2)];
end%if
set(hc, ...
'FontName', 'Helvetica', ...
'FontSize', FONTSIZE, ...
'Box', 'on', ...
'TickDir', 'in', ...
'XColor', [0.3 0.3 0.3], ...
'YColor', [0.3 0.3 0.3], ...
'LineWidth', 2, ...
'YTick', YTicks, ...
'TickLength', [0 0]);
% these bits don't work in Matlab 2015
% 'XMinorTick', 'off', ...
% 'YMinorTick', 'off', ...
% 'YGrid', 'off', ...
% 'XGrid', 'off', ...
YTL = get(hc,'yticklabel');
% set(hc,'yticklabel',[repmat(' ',size(YTL,1),1), YTL]);
end
% Draw coloured spheres for nodes
if ~exist('spherecols','var')
spherecols = [];
end
[X,Y,Z] = sphere(10);
for n = 1:length(NodeValues)
x = abs(NodeValues(n))*X; y = abs(NodeValues(n))*Y; z = abs(NodeValues(n))*Z;
if isempty(spherecols)
if NodeValues(n) < 0, spherecolour = [0 1 1]; else spherecolour = [1 0 1]; end
else spherecolour = spherecols(n,:);
end
surf(x+mnipos(n,1),y+mnipos(n,2),z+mnipos(n,3),'edgecolor','none','facecolor',spherecolour,'facealpha',0.2);
end
if ~isempty(labels)
text(1.1*mnipos(:,1),1.1*mnipos(:,2),1.1*mnipos(:,3), labels, 'FontSize',12,'HorizontalAlignment','center')
end
set(gcf,'renderer','openGL')
handles = get(gca,'children');
is_text = strcmp(get(handles,'type'),'text');
arrayfun(@uistack,handles(is_text))
set(gca,'xColor','w')
set(gca,'yColor','w')
set(gca,'zColor','w')
set(gcf,'color','w')
axis image
axis vis3d
hold off
rotate3d('on')
view(-40,80);
end
function i = closest(a,k)
%CLOSEST finds index of vector a closest to k
assert(isscalar(k) | isscalar(a));
[~,i] = min(abs(a-k));
end%closest
function newmap = bluewhitered(m)
%BLUEWHITERED Blue, white, and red color map.
% BLUEWHITERED(M) returns an M-by-3 matrix containing a blue to white
% to red colormap, with white corresponding to the CAXIS value closest
% to zero. This colormap is most useful for images and surface plots
% with positive and negative values. BLUEWHITERED, by itself, is the
% same length as the current colormap.
%
% Examples:
% ------------------------------
% figure
% imagesc(peaks(250));
% colormap(bluewhitered(256)), colorbar
%
% figure
% imagesc(peaks(250), [0 8])
% colormap(bluewhitered), colorbar
%
% figure
% imagesc(peaks(250), [-6 0])
% colormap(bluewhitered), colorbar
%
% figure
% surf(peaks)
% colormap(bluewhitered)
% axis tight
%
% See also HSV, HOT, COOL, BONE, COPPER, PINK, FLAG,
% COLORMAP, RGBPLOT.
if nargin < 1
m = size(get(gcf,'colormap'),1);
end
% RcolorBrewer RdBu
bottom = [5 113 176] / 255;
botmiddle = [146 197 222] / 255;
middle = [247 247 247] / 255;
topmiddle = [244 165 130] / 255;
top = [202 0 32] / 255;
% Find middle
lims = get(gca, 'CLim');
% Find ratio of negative to positive
if (lims(1) < 0) && (lims(2) > 0)
% It has both negative and positive
% Find ratio of negative to positive
ratio = abs(lims(1)) / (abs(lims(1)) + lims(2));
neglen = round(m*ratio);
poslen = m - neglen;
% Just negative
new = [bottom; botmiddle; middle];
len = length(new);
oldsteps = linspace(0, 1, len);
newsteps = linspace(0, 1, neglen);
newmap1 = zeros(neglen, 3);
for i=1:3
% Interpolate over RGB spaces of colormap
newmap1(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
end
% Just positive
new = [middle; topmiddle; top];
len = length(new);
oldsteps = linspace(0, 1, len);
newsteps = linspace(0, 1, poslen);
newmap = zeros(poslen, 3);
for i=1:3
% Interpolate over RGB spaces of colormap
newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
end
% And put 'em together
newmap = [newmap1; newmap];
elseif lims(1) >= 0
% Just positive
new = [middle; topmiddle; top];
len = length(new);
oldsteps = linspace(0, 1, len);
newsteps = linspace(0, 1, m);
newmap = zeros(m, 3);
for i=1:3
% Interpolate over RGB spaces of colormap
newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
end
else
% Just negative
new = [bottom; botmiddle; middle];
len = length(new);
oldsteps = linspace(0, 1, len);
newsteps = linspace(0, 1, m);
newmap = zeros(m, 3);
for i=1:3
% Interpolate over RGB spaces of colormap
newmap(:,i) = min(max(interp1(oldsteps, new(:,i), newsteps)', 0), 1);
end
end
end%bluewhitered