-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsuit_plotflatmap.m
More file actions
executable file
·220 lines (203 loc) · 8.75 KB
/
suit_plotflatmap.m
File metadata and controls
executable file
·220 lines (203 loc) · 8.75 KB
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
function varargout=suit_plotflatmap(data,varargin)
% suit_plotflatmap('overlay',file,thresholds);
% Visualised cerebellar cortical acitivty on a flatmap in a matlab window
% INPUT:
% data Vector(s) to be plotted. If overlay is NaN,
% underlay is shown. Needs to be a 28935x1 vector
% VARARGIN:
% 'underlay',file Specific a metric or surface_shape file that
% dictates the shape of the grey underlay
% 'underscale',[min max] Color scale to determine the value to color mapping
% for the underlay
% 'undermap',map Color map for the underlay,a N x 3 Matrix specifying RGB values [0..1]
% 'type': Data type to be shown, either
% 'func': Functional data
% 'label': discrete labels - integer values
% 'rgb': A Nx3 matrix of specifying RGB values [0..1] directly
% 'cmap',C Color map for overlap,a N x 3 Matrix specifying RGB values [0..1]
% 'cscale',[min max] Color scale: determines the mapping of values to
% color map
% 'border',borderfile Specifies a borderfile to plot. Use [] for no borders
% 'borderstyle','k.' Color and marker style string for the borders
% 'bordersize',pt Border point size in pt (default 8)
% 'threshold',val Shows only values above a certain threshold
% 'xlims',[xmin xmax] Limits of the x-coordinate
% 'ylims',[ymin ymax] Limits of the y-coordinate
% 'alpha',val Alpha-value (Opacity) of the overlay
%
% (c) joern.diedrichsen@googlemail.com, 2014, 2019
% EXAMPLES:
% 1. Plot a functional volume at a certain treshold + and color scale
% Data = suit_map2surf('name.nii','space','SUIT');
% suit_plotflatmap(Data,'threshold',0.01,'cscale',[0.01 0.5]);
% 2. Plot flatmap overlayed with the lobules
% Data = suit_map2surf('Cerebellum-lobules.paint','stats',@(x)nanmedian(x,2));
% suit_plotflatmap(Data,'interpol',0,'cmap','Cerebellum-lobules.color.txt');
% 3. Plot flatmap with the 17 resting state networks by Buckner
% Data = suit_map2surf('Buckner_17Networks.paint');
% suit_plotflatmap('overlay',Data,'cmap','Buckner_17Networks.color.txt');
% -----------------------------------------------------------------
% -----------------------------------------------------------------
% Set Default values and deal with user options
% -----------------------------------------------------------------
global defaults;
if (isempty(defaults))
spm('Defaults','fmri');
global defaults;
end;
flat_dir = []; %
surf = 'FLAT.surf.gii'; % Surface file for flat map
underlay = 'SUIT.shape.gii'; % File determining colring of underlay
underscale = [-1 0.5]; % Color scale [min max] for the underlay
undermap = gray; % Color map for underlay
type = 'func'; % 'func': funtional activation 'label': categories 'rgb': RGB values
threshold = []; % Threshold for functional overlay
cscale = []; % Color scale [min max] for the overlay
cmap = colormap; % Use current colormap by default
border = 'fissures_flat.mat'; % File containing fissure information
bordersize = 8; % Size of the border points
borderstyle = 'k.'; % Color and symbol for border points
xlims = [-100 100]; % X-limits for Figure
ylims = [-100 100]; % Y-limits for Figure
alpha = 1; % Opacity of the overlay
vararginoptions(varargin,{'coord','topo','underlay','underscale','undermap',...
'type','threshold','cscale','cmap','border','bordersize','borderstyle','xlims','ylims',...
'flat_dir','alpha'});
SCCSid = '3.1';
SPMid = spm('FnBanner',mfilename,SCCSid);
% -----------------------------------------------------------------
% Determine directory for the flatmap files
% -----------------------------------------------------------------
spmVer=spm('Ver');
if ~strcmp(spmVer,'SPM12')
error('plot flatmap requires SPM12 (for gifti support)');
end;
if (isempty(flat_dir))
flat_dir=fullfile(defaults.tbx.dir{1},'suit','flatmap');
end;
% -----------------------------------------------------------------
% Load the surface and determine X,Y coordinates for all tiles
% -----------------------------------------------------------------
C=gifti(fullfile(flat_dir,surf));
P=size(C.vertices,1);
X(1,:)=C.vertices(C.faces(:,1),1);
X(2,:)=C.vertices(C.faces(:,2),1);
X(3,:)=C.vertices(C.faces(:,3),1);
Y(1,:)=C.vertices(C.faces(:,1),2);
Y(2,:)=C.vertices(C.faces(:,2),2);
Y(3,:)=C.vertices(C.faces(:,3),2);
% Find all tiles that have a single vertex (or more) in the image
k=find(any(X>xlims(1) & X<xlims(2),1) & any(Y>ylims(1) & Y<ylims(2),1));
X=X(:,k);
Y=Y(:,k);
% -----------------------------------------------------------------
% Determine the underlay and assign color
% -----------------------------------------------------------------
UN=gifti(fullfile(flat_dir,underlay));
% Determine the shading of the faces by the vertices and scale the color assignment
d=[UN.cdata(C.faces(k(:),1),1) UN.cdata(C.faces(k(:),2),1) UN.cdata(C.faces(k(:),3),1)]';
M=size(undermap,1);
dindx=ceil((d-underscale(1))/(underscale(2)-underscale(1))*M);
dindx(dindx<1)=1;
dindx(dindx>M)=M;
% Now assign the RGB color to each face
for i=1:3 % Color channel
for j=1:size(dindx,1) % 1-3rd vertex
COL(j,:,i) = undermap(dindx(j,:),i)';
end;
end;
% -----------------------------------------------------------------
% determine the overlay and assign color
% -----------------------------------------------------------------
% If input data is empty, make vector of NaNs;
if (isempty(data))
data=nan(P,1);
end;
% Check input data
if (size(data,1))~=P
error(sprintf('Input data must be a numVert (%d) x 1 vector',P));
end;
% Determine colormap
if (ischar(cmap))
CM=load(cmap);
cmap=[CM(:,2) CM(:,3) CM(:,4)]/255;
end;
colormax = size(cmap,1);
% Determine the color of the overlay
OCOL = nan(size(COL)); % set all values to NaN
switch (type)
case 'label'
% if plotting labels, use the numerical vlaues themselves
dindx=[data(C.faces(k(:),1),1) data(C.faces(k(:),2),1) data(C.faces(k(:),3),1)]';
for i=1:3 % Color channnel
for j=1:size(dindx,1) % Over the three vertices
indx = find(dindx(j,:)>0 & dindx(j,:)<=colormax);
OCOL(j,indx,i) = cmap(dindx(j,indx),i)';
end;
end;
case 'func'
% Otherwise take the mean value
d=[data(C.faces(k(:),1),1) data(C.faces(k(:),2),1) data(C.faces(k(:),3),1)]';
if (isempty(cscale) || any(isnan(cscale)))
cscale=[min(d(:)) max(d(:))];
end;
dindx=ceil((d-cscale(1))/(cscale(2)-cscale(1))*colormax);
dindx(dindx<=0)=1;
dindx(dindx>colormax)=colormax;
for i=1:3 % Color channnel
for j=1:size(dindx,1)
% Apply map thresholding
if (isempty(threshold) || isnan(threshold))
indx = find(dindx(j,:)>0 & dindx(j,:)<=colormax);
else
indx = find(d(j,:)>threshold);
end;
OCOL(j,indx,i) = cmap(dindx(j,indx),i)';
end;
end;
case 'rgb'
for i=1:3 % over color channels
OCOL(:,:,i)=[data(C.faces(k(:),1),i) data(C.faces(k(:),2),i) data(C.faces(k(:),3),i)]';
end;
otherwise
error('unknown data type');
end;
% Now blend the overlay with the underlay:
indx = ~isnan(OCOL);
if (isscalar(alpha))
COL(indx) = alpha.*OCOL(indx)+(1-alpha)*COL(indx); % Set opacacy of overlay
else
for i=1:3
ALPHA(:,:,i) = [alpha(C.faces(k(:),1),1) alpha(C.faces(k(:),2),1) alpha(C.faces(k(:),3),1)]';
end;
COL(indx) = ALPHA(indx).*OCOL(indx)+(1-ALPHA(indx)).*COL(indx);
end;
% -----------------------------------------------------------------
% Draw the actual patches
% -----------------------------------------------------------------
drawmode=get(gca,'NextPlot');
if (~strcmp(drawmode,'add'))
cla;
end;
p=patch(X,Y,COL);
set(gca,'XLim',xlims,'YLim',ylims,'XTick',[],'YTick',[]);
set(p,'LineStyle','none');
set(p,'EdgeAlpha',0);
axis equal;
% -----------------------------------------------------------------
% Plot the border
% -----------------------------------------------------------------
if (~isempty(border))
hold on;
load(fullfile(flat_dir,border));
for i=1:length(Border)
xB=Border(i).data(:,1);
yB=Border(i).data(:,2);
indx=find(xB>xlims(1) & xB<xlims(2) & yB>ylims(1) & yB<ylims(2));
p=plot(xB(indx),yB(indx),borderstyle);
set(p,'MarkerSize',bordersize);
end;
if (~strcmp(drawmode,'add'))
hold off;
end;
end;