forked from andersonwinkler/PALM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palm_viewsub.m
328 lines (306 loc) · 12.3 KB
/
palm_viewsub.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
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
function fighandle = palm_viewsub(varargin)
% Simple visualisation of subcortical surface results still in Octave/MATLAB.
% The principles are generally similar to those of the tool "dpx2map.m",
% described here:
% https://brainder.org/2013/07/28/displaying-vertexwise-and-facewise-brain-maps/
%
% Usage:
% h = palm_viewsub( ...
% dat_file.csv, ...
% { srf_file1, srf_file2, srf_file3, ... }, ...
% Name, Value, ...)
%
% The first argument is .csv file with one column containing the values for
% each subcortical region. The second is a cell-array with the file names
% for the surfaces, in .srf format, and in the same order as the values
% were entered in the .csv file.
%
% File formats for these inputs are those accepted by PALM.
%
% All subsequent arguments are supplied as pairs "Name,Value", as typical
% in many MATLAB/Octave commands. Accepted Names and descriptions are:
%
% MapName : A MATLAB/Octave colourmap (default: 'viridis').
% DataRange : Interval to be used to define the colourscale [min max].
% If not specified, it uses the min and max of the DPX file.
% ShowRange : Interval to be shown (coloured) [min max]. If not specified,
% it uses the same as datarange.
% Dual : True/False. If true, applies the map to the values of no
% overlap between datarange and showrange. Useful for
% thresholded positive+negative maps. Default is false.
% ColourGap : Colour for values that off the colourscale, including NaN.
% Default is light gray, 25%, i.e. [.75 .75 .75];
% COption : True/False. The behavior varies if 'dual' is true or not.
% - For 'dual' = true:
% If coption is false, don't rescale the extremities of the
% colourmap. Default is true, i.e., produce a higher contrast.
% - For 'dual' = false:
% If coption is false, show the out-of-range values with the
% colour specified by 'colourgap'. Default is true, so the
% out-of-range values are shown with the extremities of the
% colourmap.
% MapSize : Maximum number of colours in the colourmap. Default 2^16.
% Title : Title of the figure (to appear at the top of the colourbar).
% Layout : How to distribute the brain views in the page? The
% following layouts are available:
% - 'Simple' : A simple layout meant to be interactively
% explored using the MATLAB/Octave figure tools.
% - 'Cardinal' : Show 4 views (lateral and medial of left and right
% hemispheres), plus colorbar in the centre.
% - 'Left' : Left hemisphere only (lateral and medial).
% - 'Right' : Right hemisphere only (lateral and medial).
% - 'Strip' : Similar to cardinal but shows all views in a
% single row. Useful for stacking subjects.
% - 'Publication' : A well rounded layout, meant to be (nearly) ready
% for publication. It always includes a colourbar.
% - 'Worsley' : The layout proposed by K. Worsley. Always includes
% a colourbar.
% Background : A 3-element vector of the RGB color of the background.
% Default is [1 1 1] (white).
% CamLight : Boolean indicating whether a light should be placed in the
% same location as the camera.
% Lightning : Lighting mode. Valid options are:
% - 'None'
% - 'Flat'
% - 'Gouraud'
% Material : Material mode. Valid options are:
% - 'Dull'
% - 'Shiny'
% - 'Metal'
% Shading : Shading mode. Valid options are:
% - 'Flat'
% - 'Interp'
% - 'Faceted'
% ColourBar : Boolean indicating whether the colour bar should be shown.
% Note that for the 'publication' and 'worsley' layouts, the
% colour bar is always shown.
%
% Example:
% freesurfer_dir = getenv('FREESURFER_DIR');
% data = 'mydata.csv';
% surfs = { ...
% fullfile(srfdir,'Left-Accumbens-area.srf'),...
% fullfile(srfdir,'Left-Amygdala.srf'),...
% fullfile(srfdir,'Left-Caudate.srf'),...
% fullfile(srfdir,'Left-Hippocampus.srf'),...
% fullfile(srfdir,'Left-Pallidum.srf'),...
% fullfile(srfdir,'Left-Putamen.srf'),...
% fullfile(srfdir,'Left-Thalamus-Proper.srf'),...
% fullfile(srfdir,'Left-VentralDC.srf'),...
% fullfile(srfdir,'Right-Accumbens-area.srf'),...
% fullfile(srfdir,'Right-Amygdala.srf'),...
% fullfile(srfdir,'Right-Caudate.srf'),...
% fullfile(srfdir,'Right-Hippocampus.srf'),...
% fullfile(srfdir,'Right-Pallidum.srf'),...
% fullfile(srfdir,'Right-Putamen.srf'),...
% fullfile(srfdir,'Right-Thalamus-Proper.srf'),...
% fullfile(srfdir,'Right-VentralDC.srf') };
% palm_viewsurf(data,surfs,'layout','top','background',[.5 .5 .5],'colormap','inferno');
%
% _____________________________________
% A. Winkler and K. Kircanski
% National Institutes of Health
% May/2021
% http://brainder.org
% Defaults
opts.mapname = 'viridis';
opts.datarange = []; % clim, datarange
opts.showrange = [];
opts.dual = false;
opts.colourgap = [.75 .75 .75];
opts.coption = true;
opts.mapsize = 2^14;
opts.title = '';
opts.layout = 'superior';
opts.background = [1 1 1];
opts.camlight = true;
opts.lightning = 'gouraud'; % {'none', 'flat', 'gouraud'}
opts.material = 'dull'; % {'shiny', 'dull', 'metal'}
opts.shading = 'interp'; % {'flat', 'interp', 'faceted'}
opts.colourbar = true;
% Parse inputs and check for most common errors.
dat = varargin{1};
srf = varargin{2};
for a = 3:2:nargin
opts.(lower(varargin{a})) = varargin{a + 1};
end
if numel(opts.datarange) ~= 2 && ~ isempty(opts.datarange)
error('The supplied DataRange has to be a two-element vector, or be empty.');
end
if numel(opts.showrange) ~= 2 && ~ isempty(opts.showrange)
error('The supplied ShowRange has to be a two-element vector, or be empty.');
end
if numel(opts.background) ~= 3 || numel(opts.colourgap) ~= 3
error('The supplied ColourGap or Background has to be a two-element vector, or be empty.');
end
if ~ islogical(opts.dual) || ~ islogical(opts.coption)
error('The supplied Dual or COption must be a logical type (i.e., true/false).');
end
if ~ ischar(opts.title)
error('The supplied Title must be a string.');
end
if ~ islogical(opts.camlight)
error('The CamLight option must be a logical type (i.e., true/false).');
end
if ~ any(strcmpi(opts.lightning,{'none','flat','gouraud'}))
error('Unknown Lightning style: %s',opts.lightning);
end
if ~ any(strcmpi(opts.material,{'shiny','dull','metal'}))
error('Unknown Material style: %s',opts.material);
end
if ~ any(strcmpi(opts.shading,{'flat','interp','faceted'}))
error('Unknown Shading style: %s',opts.shading);
end
if ~ islogical(opts.colourbar)
error('The Colourbar option must be a logical type (i.e., true/false).');
end
if strcmpi(opts.layout,'worsley')
opts.mapname = 'spectral';
end
if palm_isoctave && opts.mapsize > 2^14
error('Octave cannot plot more than 2^14 = 32768 colors.');
end
% Ensure we gave the programs needed to read the data
palm_checkprogs;
% Read input files
for s = 1:numel(srf)
srf{s} = palm_miscread(srf{s});
end
dat = palm_miscread(dat);
% Open a new figure window. The handle will be returned.
fighandle = figure;
% Ensure data and colour ranges are well behaved
if isempty(opts.datarange)
iinf = isinf(dat.data);
inan = isnan(dat.data);
iidx = ~(iinf | inan);
opts.datarange = [min(dat.data(iidx)) max(dat.data(iidx))];
end
if isempty(opts.showrange)
opts.showrange = opts.datarange;
end
opts.showrange(1) = max(opts.showrange(1),opts.datarange(1));
opts.showrange(2) = min(opts.showrange(2),opts.datarange(2));
for h = 1:numel(dat)
infneg = isinf(dat.data) & dat.data < 0;
infpos = isinf(dat.data) & dat.data > 0;
dat.data(infneg) = opts.datarange(1);
dat.data(infpos) = opts.datarange(2);
end
% Define the colourmap
if opts.dual
if opts.coption
neg = opts.showrange(1) - opts.datarange(1);
cen = opts.showrange(2) - opts.showrange(1);
pos = opts.datarange(2) - opts.showrange(2);
tot = opts.datarange(2) - opts.datarange(1);
fracs = round([neg cen pos]./tot*opts.mapsize);
maptmp = eval(sprintf('%s(%d)',opts.mapname,fracs(1)+fracs(3)));
map = nan(opts.mapsize,3);
map(1:fracs(1),:) = maptmp(1:fracs(1),:);
map(fracs(1)+1:fracs(1)+fracs(2),:) = repmat(opts.colourgap,[fracs(2) 1]);
map(fracs(1)+fracs(2)+1:sum(fracs),:) = maptmp(fracs(1)+1:fracs(1)+fracs(3),:);
else
map = eval(sprintf('%s(%d)',opts.mapname,opts.mapsize));
idxmin = round((opts.showrange(1) - opts.datarange(1)) / (opts.datarange(2) - opts.datarange(1)) * (opts.mapsize - 1) + 1);
idxmax = round((opts.showrange(2) - opts.datarange(1)) / (opts.datarange(2) - opts.datarange(1)) * (opts.mapsize - 1) + 1);
map(idxmin:idxmax,:) = repmat(opts.colourgap,[idxmax-idxmin+1 1]);
end
else
neg = opts.showrange(1) - opts.datarange(1);
cen = opts.showrange(2) - opts.showrange(1);
pos = opts.datarange(2) - opts.showrange(2);
tot = opts.datarange(2) - opts.datarange(1);
fracs = round([neg cen pos]./tot*opts.mapsize);
maptmp = eval(sprintf('%s(%d)',opts.mapname,fracs(2)));
map = nan(opts.mapsize,3);
map(fracs(1)+1:fracs(1)+fracs(2),:) = maptmp;
if opts.coption
map(1:fracs(1),:) = repmat(maptmp(1,:),[fracs(1) 1]);
map(fracs(1)+fracs(2)+1:sum(fracs),:) = repmat(maptmp(fracs(2),:),[fracs(3) 1]);
else
map(1:fracs(1),:) = repmat(opts.colourgap,[fracs(1) 1]);
map(fracs(1)+fracs(2)+1:sum(fracs),:) = repmat(opts.colourgap,[fracs(3) 1]);
end
end
colormap(map)
% Render using the selected layout
switch opts.layout
case 'superior'
% Top view
ax(1) = axes();
for s = 1:numel(srf)
trisurf(...
srf{s}.data.fac,...
srf{s}.data.vtx(:,1),srf{s}.data.vtx(:,2),srf{s}.data.vtx(:,3),...
dat.data(s)*ones(size(srf{s}.data.vtx,1),1),...
'EdgeColor','None');
hold on
end
hold off
view(0,90);
useopts(opts,fighandle);
% If a colorbar was asked
if opts.colourbar
cb = colorbar('Location','South');
if isempty(opts.title)
set(cb,'Position',[0.41 0.085 0.3 0.03]);
else
set(cb,'Position',[0.41 0.05 0.3 0.03]);
end
end
case 'inferior'
% Bottom view
ax(1) = axes();
for s = 1:numel(srf)
trisurf(...
srf{s}.data.fac,...
srf{s}.data.vtx(:,1),srf{s}.data.vtx(:,2),srf{s}.data.vtx(:,3),...
dat.data(s)*ones(size(srf{s}.data.vtx,1),1),...
'EdgeColor','None');
hold on
end
hold off
view(180,-90);
useopts(opts,fighandle);
% If a colorbar was asked
if opts.colourbar
cb = colorbar('Location','South');
if isempty(opts.title)
set(cb,'Position',[0.32 0.085 0.3 0.03]);
else
set(cb,'Position',[0.32 0.05 0.3 0.03]);
end
end
otherwise
error('Unknown layout style %s.\n',opts.layout)
end
% Deal with the colour limits
for i=1:length(ax)
set(ax(i),'CLim',opts.datarange);
end
% This layout always includes a colourbar
% Deal with the colourbar
if opts.colourbar
set(get(cb,'Title'),'String',opts.title);
end
function useopts(opts,fighandle) % ==================================================
daspect([1 1 1]);
axis tight;
axis vis3d off;
box off
if strcmpi(opts.layout,'worsley')
set(fighandle,'Color',[1 1 1],'InvertHardcopy','Off');
camlight;
lighting ('gouraud');
material ('shiny');
shading ('interp');
else
set(fighandle,'Color',opts.background,'InvertHardcopy','Off');
if opts.camlight
camlight;
end
lighting (opts.lightning);
material (opts.material);
shading (opts.shading);
end