function plotData = plotsomplanes(param,graphicsAdapter,plotData,net) %#ok<*INUSL>
% plotsomplanes Internal function for plotsomplanes
%
% This function allows creation of the plot created by the user-facing
% function plotsomplanes(), but specifying a figure handle as the parent.
%
% Arguments to the internal function are handled differently to the
% user-facing function.
%
% The arguments provided to this function are:
% - param: unused
% - graphicsAdapter: an nnet.internal.plot.GraphicsAdapter wrapping the
% figure.
% - plotData: a struct whose fields are various graphics handles (e.g. line
% objects). Those handles will be updated with the new data. If no
% plotData struct is provided, one will be created.
% - net: a Self-Organizing Map network.
% Copyright 2021 The MathWorks, Inc.
if isempty(plotData)
plotData = setup_plot(graphicsAdapter);
end
numInputs = net.inputs{1}.processedSize;
numNeurons = net.layers{1}.size;
topologyFcn = net.layers{1}.topologyFcn;
if strcmp(topologyFcn,'gridtop')
shapex = [-1 1 1 -1]*0.5;
shapey = [1 1 -1 -1]*0.5;
dx = 1;
dy = 1;
elseif strcmp(topologyFcn,'hextop')
z = sqrt(0.75);
shapex = [-1 0 1 1 0 -1]*0.5;
shapey = [1 2 1 -1 -2 -1]*(z/3);
dx = 1;
dy = sqrt(0.75);
end
if (plotData.numInputs ~= numInputs) || (plotData.numNeurons ~= numNeurons) ...
|| ~strcmp(plotData.topologyFcn,topologyFcn)
graphicsAdapter.setNextPlotReplace();
plotData.numInputs = numInputs;
plotData.numNeurons = numNeurons;
plotData.topologyFcn = topologyFcn;
pos = net.layers{1}.positions;
dim = net.layers{1}.dimensions;
numDimensions = length(dim);
if (numDimensions == 1)
dim1 = dim(1);
dim2 = 1;
pos = [pos; zeros(1,size(pos,2))];
elseif (numDimensions > 2)
pos = pos(1:2,:);
dim1 = dim(1);
dim2 = dim(2);
else
dim1 = dim(1);
dim2 = dim(2);
end
plotcols = ceil(sqrt(numInputs));
plotrows = ceil(numInputs/plotcols);
plotData.patches = cell(numInputs);
for i=1:plotrows
for j=1:plotcols
k = (i-1)*plotcols+j;
if (k<=numInputs)
a = graphicsAdapter.subplot(plotrows,plotcols,k);
cla(a);
set(a,...
'DataAspectRatio',[1 1 1], ...
'Box','on',...
'Color',[1 1 1]*0.5)
graphicsAdapter.holdOn(a);
plotData.patches{k} = zeros(1,numNeurons);
for ii=1:numNeurons
z = fill(a,pos(1,ii)+shapex,pos(2,ii)+shapey,[1 1 1]);
set(z,'EdgeColor','none');
plotData.patches{k}(ii) = z;
end
set(a,'XLim',[-1 (dim1-0.5)*dx + 1]);
set(a,'YLim',[-1 (dim2-0.5)*dy + 0.5]);
title(a,getString(message('nnet:nnplots:PlotsomplanesWeightsTitle', num2str(k))));
end
end
end
screenSize = get(0,'ScreenSize');
screenSize = screenSize(3:4);
windowSize = 700 * [1 (plotrows/plotcols)];
pos = [(screenSize-windowSize)/2 windowSize];
graphicsAdapter.setWindowPosition(pos);
end
iw = net.IW{1,1};
%min_neg = min(0,min(iw,[],1));
%max_pos = max(0,max(iw,[],1));
mn = min(iw,[],1);
mx = max(iw,[],1);
rng = mx-mn;
for i=1:numInputs
for j=1:numNeurons
level = net.IW{1,1}(j,i);
%if level<0, level = -level/min_neg(i); end
%if level>0, level = level/max_pos(i); end
%red = min(1,max(0,level)*2); % positive
%blue = -max(-1,min(0,level)*2); % negative
%green = max(0,abs(level)*2-1); % very positive/negative
level = (level-mn(i))/rng(i);
red = min(1,level*2);
green = max(0,level*2-1);
blue = 0;
c = [red green blue];
set(plotData.patches{i}(j),'FaceColor',c);
end
end
end
function plotData = setup_plot(graphicsAdapter)
plotData.axis = graphicsAdapter.subplot(1,1,1);
plotData.numInputs = 0;
plotData.numNeurons = 0;
plotData.topologyFcn = '';
end