-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprt_plot_1D_weights.m
More file actions
154 lines (136 loc) · 4.76 KB
/
Copy pathprt_plot_1D_weights.m
File metadata and controls
154 lines (136 loc) · 4.76 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
function [h] = prt_plot_1D_weights(parent,weights,roimat)
% Function to plot 1D weights from .mat or MEEG modalities
% Inputs: Parent graph handle and weights, the vector of values
% Output: Handle to obtained plot
%__________________________________________________________________________
% Copyright (C) 2011 Machine Learning & Neuroimaging Laboratory
% Written by J. Schrouff
% $Id$
% Remove figure axes and plots
gch = get(parent,'Children');
for i=1:numel(gch)
if strcmp(gch(i).Tag,'uipanelmat_topoplots')
continue
end
delete(gch(i));
end
% Define colormap - setting grey for zero, in the middle
minw = min(weights);
maxw = max(weights);
if minw<0 && maxw>0 % Both negative and positive, diverging colormap
colneg = cbrewer('seq','Blues',128,'PCHIP');
colpos = cbrewer('seq','Reds',128,'PCHIP');
cols = [flip(colneg,1);colpos];
valspos = round((weights(weights>=0) ./ maxw) *127)+1;
valsneg = -(round((weights(weights<0) ./ minw) *127));
valsN = weights;
valsN(weights>=0) = valspos;
valsN(weights<0) = valsneg;
valsN = valsN + 128;
% Colorbar ticks and labels
newticks = [1 128 256];
labels = [minw;0;maxw];
elseif minw>=0 && maxw>0 % Only positive, sequential red colormap
colpos = cbrewer('seq','Reds',256);
cols = colpos;
valsN = round(((weights) ./ maxw) .* 255)+1;
newticks = [1 256];
labels = [minw;maxw];
elseif minw<0 && maxw<=0 % Only negative, sequential blue colormap
colneg = cbrewer('seq','Blues',256);
% cols = flip(colneg,1);
cols = colneg;
valsN = round(((weights) ./ minw) .* 255)+1;
newticks = [1 256];
labels = [minw;maxw];
elseif (minw==0 && maxw==0) || ...
(isnan(minw) && isnan(maxw)) % All zeros or NaNs, just gray
weights = zeros(size(weights));
cols = [0.5 0.5 0.5; cbrewer('seq','Reds',255)];
valsN = ones(size(weights));
newticks = [1 256];
labels = [0; 1];
end
colormap(cols);
% Mask weights if a cell was selected in the ROI table
weights = weights(roimat==1);
valsN = valsN(roimat==1);
% Draw axes and x-slider
step = min(1,100/numel(weights));
valsl = 0;
lowval = floor((valsl)*numel(weights));
minsl = max(lowval,1);
highval = ceil((valsl + (step))*numel(weights));
maxsl = min(highval,length(weights));
% Draw axes (try-catch on Matlab version)
try
axmat = axes(parent,'XLim', [-5 numel(weights)+5], 'units','normalized', ...
'position',[0.2 0.2 0.6 0.7], 'NextPlot', 'add');
catch % for older Matlab versions
axmat = axes('XLim', [-5 numel(weights)+5], 'units','normalized', ...
'position',[0.2 0.2 0.6 0.7],'NextPlot', 'add','parent',parent);
end
set(gcf,'CurrentAxes',axmat);
% Plot values
h = plot_data(axmat,minsl:maxsl,weights(minsl:maxsl),cols,valsN(minsl:maxsl),[minw maxw]);
try
hc = colorbar(axmat,'Units','normalized','Position',[0.85 0.2 0.02 0.7]);
catch
hc = colorbar('peer',axmat,'Units','normalized','Position',[0.85 0.2 0.02 0.7]);
end
if minw<0 && maxw<=0
colormap(flipud(cols))
end
% Change colorbar limits and tick labels
try
limhc = get(hc,'Limits');
if limhc(end) >1
newticksproj = ceil(newticks * (max(limhc)/size(cols,1)));
else
newticksproj = newticks/size(cols,1);
end
set(hc,'Ticks',newticksproj);
set(hc,'TickLabelsMode','manual');
set(hc,'TickLabels',labels);
catch
newticksproj = [0 1];
set(hc,'Ticks',newticksproj);
set(hc,'TickLabelsMode','manual');
set(hc,'YTickLabels',cellstr(num2str(labels)))
end
if step<1 % set slider
slide = uicontrol(parent,'style','slider','units','normalized',...
'position',[0.2 0.05 0.6 .05],...
'Min',0,'Max',1,'SliderStep',[step step],...
'callback',@hscroll_Callback);
% Pass data to slider
sliderdata = struct('weights',weights,'axes',axmat,'cols',cols,...
'valsN',valsN,'range',[minw maxw]);
set(slide,'UserData',sliderdata);
end
%--------------------------------------------------------------------------
% Subfunctions
%--------------------------------------------------------------------------
function hscroll_Callback(src,evt)
valsl = get(src,'Value');
step = get(src,'SliderStep');
data = get(src,'UserData');
step = step(1);
lowval = floor((valsl)*numel(data.weights));
minsl = max(lowval,1);
highval = ceil((valsl + (step))*numel(data.weights));
maxsl = min(highval,length(weights));
plot_data(data.axes,minsl:maxsl,data.weights(minsl:maxsl),data.cols,data.valsN(minsl:maxsl),data.range);
end
end
function [h] = plot_data(ax,xval,yval,cols,valsN,range)
h = bar(ax,xval,diag(yval),'stacked'); % Plot true values
for i=1:length(h)
rgb = cols(valsN(i),:);
set(h(i),'FaceColor',rgb);
end
xlim(ax,[min(xval)-1 max(xval)+1])
if range(2)>range(1)
ylim(ax,range)
end
end