📄 visualize_map.m
字号:
function varargout = visualize_map(varargin)
% visualize_maps opens a graphical interface for visaulising CPANNs and kohonen maps
% visualize_maps is called by each visualize file (visualize_kohonen and visualize_cpann)
%
% see the HTML HELP files (help.htm) for details
% Edit the above text to modify the response to help visualize_map
% Last Modified by GUIDE v2.5 02-Jul-2007 13:31:46
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @visualize_map_OpeningFcn, ...
'gui_OutputFcn', @visualize_map_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before visualize_map is made visible.
function visualize_map_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
movegui(handles.visualize,'center');
handles.model = varargin{1};
% set temp arrays
handles.temp.top_map = handles.model.res.top_map;
handles.temp.W = handles.model.net.W;
if strcmp(handles.model.type,'cpann')
handles.temp.W_out = handles.model.net.W_out;
handles.temp.neuron_ass = handles.model.net.neuron_ass;
end
% set variable combo
num_var = size(handles.model.net.W,3);
str_layer{1} = 'none';
for j=1:num_var
str_layer{j+1} = ['variable' num2str(j)];
end
if strcmp(handles.model.type,'cpann')
num_class = size(handles.model.net.W_out,3);
s = length(str_layer);
for j=1:num_class
str_layer{j+s} = ['weight class ' num2str(j)];
end
str_layer{end + 1} = ['assignations'];
end
set(handles.pop_layer,'String',str_layer);
set(handles.pop_layer,'Value',1);
% set label combo
str_disp{1} = 'none';
str_disp{2} = 'ID';
handles.sample_label = [];
count = 3;
if size(varargin{2},1) > 0
str_disp{count} = 'sample labels';
handles.sample_label = varargin{2};
count = count + 1;
end
if strcmp(handles.model.type,'cpann')
str_disp{count} = 'class labels';
handles.class = handles.model.res.class_true;
end
set(handles.pop_label,'String',str_disp);
set(handles.pop_label,'Value',2);
plot([0 0],'r.')
disp_grid(size(handles.model.net.W,1))
handles = update_plot(handles);
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = visualize_map_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in update_botton.
function update_botton_Callback(hObject, eventdata, handles)
handles = update_plot(handles);
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function pop_layer_CreateFcn(hObject, eventdata, handles)
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
guidata(hObject,handles)
% --- Executes on selection change in popupmenu
function pop_layer_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.function pop_label_CreateFcn(hObject, eventdata, handles)if ispc set(hObject,'BackgroundColor','white');else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));end
% --- Executes during object creation, after setting all properties.
function list_labels_CreateFcn(hObject, eventdata, handles)
if ispc
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% --- Executes on selection change in pop_label.function pop_label_Callback(hObject, eventdata, handles)
% --- Executes on selection change in pop_label.
function list_labels_Callback(hObject, eventdata, handles)
% --- Executes on button press in button_up.
function button_up_Callback(hObject, eventdata, handles)
handles = move_map(handles,'up');
guidata(hObject,handles)
% --- Executes on button press in botton_down.
function button_down_Callback(hObject, eventdata, handles)
handles = move_map(handles,'down');
guidata(hObject,handles)
% --- Executes on button press in botton_right.
function button_right_Callback(hObject, eventdata, handles)
handles = move_map(handles,'right');
guidata(hObject,handles)
% --- Executes on button press in button_left.
function button_left_Callback(hObject, eventdata, handles)
handles = move_map(handles,'left');
guidata(hObject,handles)
% --- Executes on button press in get_lab_neuron.
function get_lab_neuron_Callback(hObject, eventdata, handles)
handles = set_list_labels(handles);
guidata(hObject,handles)
% --- Executes on button press in get_weight_neuron.
function get_weight_neuron_Callback(hObject, eventdata, handles)
handles = show_weight_neuron(handles);
guidata(hObject,handles)
% --- Executes on button press in save_figure.
function save_figure_Callback(hObject, eventdata, handles)
handles = update_plot(handles);
figure
plot([0 0],'r.')
handles = update_plot(handles);
guidata(hObject,handles)
% ---------------------------------------------------------
function disp_grid(n)
axis([0.5 (n + 0.4999) 0.5 (n + 0.5)])
tick_pos = 0.5:1:(n + 0.5);
set(gca,'YTick',tick_pos)
set(gca,'XTick',tick_pos)
grid on
set(gca,'YTickLabel','')
set(gca,'XTickLabel','')
% ---------------------------------------------------------
function handles = update_plot(handles)
cla;
pop_layer_index = get(handles.pop_layer, 'Value');
pop_label_index = get(handles.pop_label, 'Value');
% display weights
if pop_layer_index > 1
var_in = pop_layer_index - 1;
disp_weights(handles,var_in);
end
% display samples
if pop_label_index > 1
handles = disp_samples(handles,pop_label_index);
else
handles.disp_label = [];
end
% display grid
disp_grid(size(handles.model.net.W,1))
% ---------------------------------------------------------function handles = disp_samples(handles,pop_label_index)
pos = handles.temp.top_map;
col_in = get(handles.pop_layer, 'Value');
if col_in > 1
text_color = 'red';
else
text_color = 'black';
end
disp_label = [];
if pop_label_index == 2
for j=1:size(pos,1)
store_label(j) = j;
end
elseif strcmp(handles.model.type,'cpann')
if pop_label_index == 3 & length(handles.sample_label) > 0
store_label = handles.sample_label;
elseif pop_label_index == 3 & length(handles.sample_label) == 0
store_label = handles.class;
else
store_label = handles.class;
end
elseif strcmp(handles.model.type,'kohonen_map')
store_label = handles.sample_label;
end
for j=1:length(store_label)
if iscell(store_label(j))
disp_label{j} = store_label(j);
else
disp_label{j} = num2str(store_label(j));
end
end
for i=1:size(pos,1)
y_pos = size(handles.model.net.W,1) + 1 - pos(i,1) + 0.3 - rand*0.6;
x_pos = pos(i,2) + 0.3 - rand*0.6;
text(x_pos,y_pos,disp_label{i},'color',text_color,'FontSize',8);
end
handles.disp_label = disp_label;
% ---------------------------------------------------------
function disp_weights(handles,var_in)
if strcmp(handles.model.type,'cpann')
num_var = size(handles.temp.W,3);
num_cla = size(handles.temp.W_out,3);
if var_in <= num_var
W = squeeze(handles.temp.W(:,:,var_in));
type = 'var';
elseif var_in <= num_cla + num_var
W = squeeze(handles.temp.W_out(:,:,var_in - num_var));
type = 'cla';
else
W = handles.temp.neuron_ass;
type = 'ass';
col_ass(1,:) = [1 1 1]; % white, not assigned
col_ass(2,:) = [1 1 0]; % yellow
col_ass(3,:) = [0 1 0]; % green
col_ass(4,:) = [0 0 1]; % blue
col_ass(5,:) = [0 1 1]; % cyan
col_ass(6,:) = [1 0 1]; % magenta
col_ass(7,:) = [0.5 0.5 0.5];
col_ass(8,:) = [1 0.5 0.5];
col_ass(9,:) = [0.5 0.5 1];
col_ass(10,:) = [0.5 1 0.5];
col_ass(11,:) = [0 0.5 1];
end
elseif strcmp(handles.model.type,'kohonen_map')
W = squeeze(handles.temp.W(:,:,var_in));
type = 'var';
end
hold on
for k=1:size(W,1)
for j=1:size(W,2)
if strcmp(type,'ass')
color_in = col_ass(handles.temp.neuron_ass(j,k) + 1,:);
else
color_in = [(1-W(j,k)) (1-W(j,k)) (1-W(j,k))];
end
pos_x = k;
pos_y = size(W,2) + 1 - j;
area([(pos_x - 0.5) (pos_x + 0.5)],[(pos_y + 0.5) (pos_y + 0.5)],pos_y - 0.5,'FaceColor',color_in);
end
end
hold off
% ---------------------------------------------------------function handles = move_map(handles,where)
if strcmp(where,'up')
for i=1:size(handles.temp.top_map,1)
handles.temp.top_map(i,1) = handles.temp.top_map(i,1) - 1;
if handles.temp.top_map(i,1) == 0
handles.temp.top_map(i,1) = size(handles.model.net.W,1);
end
end
for i=1:size(handles.temp.W,1) - 1
temp_W(i,:,:) = handles.temp.W(i + 1,:,:);
end
temp_W(size(handles.temp.W,1),:,:) = handles.temp.W(1,:,:);
handles.temp.W = temp_W;
elseif strcmp(where,'down')
for i=1:size(handles.temp.top_map,1)
handles.temp.top_map(i,1) = handles.temp.top_map(i,1) + 1;
if handles.temp.top_map(i,1) > size(handles.model.net.W,1)
handles.temp.top_map(i,1) = 1;
end
end
for i=1:size(handles.temp.W,1) - 1
temp_W(i+1,:,:) = handles.temp.W(i,:,:);
end
temp_W(1,:,:) = handles.temp.W(size(handles.temp.W,1),:,:);
handles.temp.W = temp_W;
elseif strcmp(where,'right')
for i=1:size(handles.temp.top_map,1)
handles.temp.top_map(i,2) = handles.temp.top_map(i,2) + 1;
if handles.temp.top_map(i,2) > size(handles.model.net.W,2)
handles.temp.top_map(i,2) = 1;
end
end
for i=1:size(handles.temp.W,2) - 1
temp_W(:,i+1,:) = handles.temp.W(:,i,:);
end
temp_W(:,1,:) = handles.temp.W(:,size(handles.temp.W,2),:);
handles.temp.W = temp_W;
elseif strcmp(where,'left')
for i=1:size(handles.temp.top_map,1)
handles.temp.top_map(i,2) = handles.temp.top_map(i,2) - 1;
if handles.temp.top_map(i,2) == 0
handles.temp.top_map(i,2) = size(handles.model.net.W,2);
end
end
for i=1:size(handles.temp.W,2) - 1
temp_W(:,i,:) = handles.temp.W(:,i+1,:);
end
temp_W(:,size(handles.temp.W,2),:) = handles.temp.W(:,1,:);
handles.temp.W = temp_W;
endif strcmp(handles.model.type,'cpann')
if strcmp(where,'up')
for i=1:size(handles.temp.W_out,1) - 1
temp_W_out(i,:,:) = handles.temp.W_out(i + 1,:,:);
temp_neuron_ass(i,:) = handles.temp.neuron_ass(i + 1,:);
end
temp_W_out(size(handles.temp.W_out,1),:,:) = handles.temp.W_out(1,:,:);
temp_neuron_ass(size(handles.temp.W_out,1),:) = handles.temp.neuron_ass(1,:);
handles.temp.W_out = temp_W_out;
handles.temp.neuron_ass = temp_neuron_ass;
elseif strcmp(where,'down')
for i=1:size(handles.temp.W,1) - 1
temp_W_out(i+1,:,:) = handles.temp.W_out(i,:,:);
temp_neuron_ass(i+1,:) = handles.temp.neuron_ass(i,:);
end
temp_W_out(1,:,:) = handles.temp.W_out(size(handles.temp.W_out,1),:,:);
temp_neuron_ass(1,:) = handles.temp.neuron_ass(size(handles.temp.W_out,1),:);
handles.temp.W_out = temp_W_out;
handles.temp.neuron_ass = temp_neuron_ass;
elseif strcmp(where,'right')
for i=1:size(handles.temp.W_out,2) - 1
temp_W_out(:,i+1,:) = handles.temp.W_out(:,i,:);
temp_neuron_ass(:,i+1) = handles.temp.neuron_ass(:,i);
end
temp_W_out(:,1,:) = handles.temp.W_out(:,size(handles.temp.W_out,2),:);
temp_neuron_ass(:,1) = handles.temp.neuron_ass(:,size(handles.temp.W_out,1));
handles.temp.W_out = temp_W_out;
handles.temp.neuron_ass = temp_neuron_ass;
elseif strcmp(where,'left')
for i=1:size(handles.temp.W_out,2) - 1
temp_W_out(:,i,:) = handles.temp.W_out(:,i+1,:);
temp_neuron_ass(:,i) = handles.temp.neuron_ass(:,i+1);
end
temp_W_out(:,size(handles.temp.W_out,2),:) = handles.temp.W_out(:,1,:);
temp_neuron_ass(:,size(handles.temp.W_out,2)) = handles.temp.neuron_ass(:,1);
handles.temp.W_out = temp_W_out;
handles.temp.neuron_ass = temp_neuron_ass;
end
end
handles = update_plot(handles);% ---------------------------------------------------------
function handles = set_list_labels(handles)
[y,x] = ginput(1);
[cord_y,cord_x] = find_neuron(y,x,handles);
in = find(handles.temp.top_map(:,1) == cord_x & handles.temp.top_map(:,2) == cord_y);
if length(in > 0) & length(handles.disp_label) > 0
cur_labs = handles.disp_label(in);
visualize_labels(cur_labs);
end
% ---------------------------------------------------------
function handles = show_weight_neuron(handles)
[y,x] = ginput(1);
[cord_y,cord_x] = find_neuron(y,x,handles);
if cord_y > 0 & cord_y <= size(handles.temp.W,1) & cord_x > 0 & cord_x <= size(handles.temp.W,1)
w = squeeze(handles.temp.W(cord_x,cord_y,:));
figure
if length(w) < 20
bar(w,'r')
else
plot(w,'r')
end
ind = som_which_col(cord_x,cord_y,size(handles.model.net.W,1)^2);
title(['kohonen weights of neuron ' num2str(ind) ' (row ' num2str(cord_x) ' - column ' num2str(cord_y) ')'])
axis([0.5 length(w)+0.5 0 1])
xlabel('variables')
ylabel('kohonen weight')
% plot of class weight (output weight)
if strcmp(handles.model.type,'cpann')
w_out = squeeze(handles.temp.W_out(cord_x,cord_y,:));
figure
bar(w_out,'b')
title(['output weights of neuron ' num2str(ind) ' (row ' num2str(cord_x) ' - column ' num2str(cord_y) ')'])
axis([0.5 length(w_out)+0.5 0 1])
xlabel('classes')
ylabel('output weight')
end
end
% ---------------------------------------------------------
function [cord_y,cord_x] = find_neuron(y,x,handles)
x = abs(x - size(handles.model.net.W,1)) + 1;
cord_x = round(x);
cord_y = round(y);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -