[Tex/LaTex] Draw a Kohonen SOM feature map

diagramstikz-pgf

I have never used the drawing packages available to TeX but they seem like the ideal solution for creating re-usable technical drawings, and might be better than spending a week in inkscape (I'm terrible at inkscape). This time I want to draw an explanatory diagram of a Kohonen network / SOM feature map, showing the input nodes and a 2D map. I have seen one that I like, but have no idea how I would come close to making it.

Here is the figure, it is an SVG:

enter image description here

Inkscape provides a conversion to PSTricks, but it is overwhelming considering the repeated hexagonal shapes of the feature map. Is it a diagram that lends itself to PGF/Tikz?

Best Answer

A more complete answer is given below, but first...

An attempt at the feature map using the nonlineartransformations library in the CVS version of PGF. I shamelessly steal Tom Bombadil's idea for specifying the map colors:

\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}

\usepgfmodule{nonlineartransformations}
\begin{document}

\makeatletter
% This is executed for every point
%
% \pgf@x will contain the x-coordinate
% \pgf@y will contain the y-coordinate
%
% This should then be transformed to their
% final values
\def\nonlineartransform{%
 \pgf@xa=\pgf@x%
 \divide\pgf@xa by 256\relax%
 \advance\pgf@xa by 0.5pt\relax%
 \pgf@y=\pgfmath@tonumber{\pgf@xa}\pgf@y%
 \pgf@xa=0.625\pgf@xa
 \pgf@x=\pgfmath@tonumber{\pgf@xa}\pgf@x
}
\makeatother

\begin{tikzpicture}[x=10pt,y=10pt]

\begin{scope}[shift=(0:5)]
\pgftransformnonlinear{\nonlineartransform}
\foreach \c [count=\n from 0, evaluate={%
  \i=mod(\n,9); \j=int(\n/9);
  \x=(2*\i+mod(\j,2))*cos 30;
  \y=\j*1.5;
  \s=\c*10+10;}] in 
{   2,2,2,2,2,4,4,4,4,
    2,2,2,2,4,4,4,4,4,
    5,5,2,2,2,4,4,4,4,
    5,5,2,2,0,4,4,4,4,
    5,5,5,0,0,0,0,0,0,
    5,5,0,0,0,0,0,0,0,
    5,5,1,0,0,0,0,0,0,
    1,1,1,1,0,0,0,3,3,
    1,1,1,1,1,0,3,3,3,
    1,1,1,1,1,3,3,3,3,
    1,1,1,1,1,3,3,3,3
}
\draw [fill=black!\s, shift={(\x,6-\y)}] 
    (-30:1) -- (30:1) -- (90:1) -- (150:1) -- (210:1) -- (270:1) -- cycle;
\end{scope}
\end{tikzpicture}

\end{document}

enter image description here

Nothing of course to do with the OPs requirements, but I couldn't resist. This takes a long time to compile:

\documentclass[border=0.125cm,tikz]{standalone}
\usepackage{tikz}


\makeatletter
% This is executed for every point
%
% \pgf@x will contain the x-coordinate
% \pgf@y will contain the y-coordinate
%
% This should then be transformed to their
% final values
\def\nonlineartransform{%
 \pgf@xa=\pgf@x%
 \advance\pgf@xa by\k pt\relax%
 \pgfmathcos@{\pgfmath@tonumber{\pgf@xa}}%
 \pgf@xa=\pgfmathresult pt\relax%
 \advance\pgf@xa by 1pt\relax%
 \pgf@y=\pgfmath@tonumber{\pgf@xa}\pgf@y%
 \pgf@x=\pgf@x
}
\makeatother

\usepgfmodule{nonlineartransformations}
\begin{document}

\foreach \k in {0,-5,-10,...,-355}{
\begin{tikzpicture}[x=10pt,y=10pt]
\begin{scope}
\pgftransformnonlinear{\nonlineartransform}
\foreach \c [count=\n from 0, evaluate={%
  \i=mod(\n,9); \j=int(\n/9);
  \x=(2*\i+mod(\j,2))*cos 30;
  \y=\j*1.5;
  \s=\c*10+10;}] in 
{   2,2,2,2,2,4,4,4,4,
    2,2,2,2,4,4,4,4,4,
    5,5,2,2,2,4,4,4,4,
    5,5,2,2,0,4,4,4,4,
    5,5,5,0,0,0,0,0,0,
    5,5,0,0,0,0,0,0,0,
    5,5,1,0,0,0,0,0,0,
    1,1,1,1,0,0,0,3,3,
    1,1,1,1,1,0,3,3,3,
    1,1,1,1,1,3,3,3,3,
    1,1,1,1,1,3,3,3,3
}
\draw [fill=black!\s, shift={(\x,6-\y)}] 
    (-30:1) -- (30:1) -- (90:1) -- (150:1) -- (210:1) -- (270:1) -- cycle;
\end{scope}
\useasboundingbox (-5,-25) rectangle (20,20);
\end{tikzpicture}
}
\end{document}

enter image description here

Of course, we don't actually need the nonlienartranformations library at all, as tikz provides the facility for defining coordinate systems:

\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{positioning}
\begin{document}

\tikzset{feature map/.cd,
    x/.initial=0,
    y/.initial=0,
}

\tikzdeclarecoordinatesystem{feature map}{
    \tikzset{feature map/.cd, #1}%
    \pgfpointxy{\pgfkeysvalueof{/tikz/feature map/x}}{\pgfkeysvalueof{/tikz/feature map/y}}%
    \pgfgetlastxy{\fx}{\fy}%
    \pgfmathparse{\fx/256+1}\let\f=\pgfmathresult%
    \pgfpoint{\f*6/8*\fx}{\f*\fy}%
}

\tikzset{%
  every weight/.style={
    circle,
    draw,
    fill=gray!50,
    minimum size=0.25cm
  },
  weight missing/.style={
    draw=none,
    fill=none,
    execute at begin node=\color{black}$\vdots$
  },
  every neuron/.style={
    circle,
    draw,
    minimum size=0.75cm
  },
  neuron missing/.style={
    draw=none,
    execute at begin node=$\vdots$
  }
}
\begin{tikzpicture}[x=10pt,y=10pt, >=stealth]

\foreach \m [count=\y] in {1,2,missing,3,4}
  \node [every weight/.try, weight \m/.try ] (weight-\m) at (0,-\y*2) {};

\foreach \m [count=\y] in {1,2,3,missing,4,5}
  \node [every neuron/.try, neuron \m/.try ] (neuron-\m) at (8,4-\y*3) {};

\node [draw, inner xsep=0.25cm, fit={(weight-1.west) (neuron-1) (neuron-5)}] {};

\foreach \i in {1,...,4}
  \foreach \j in {1,...,5}
    \draw (weight-\i.east) -- (neuron-\j.west);

\foreach \l [count=\i] in {1,2,i-1,i}{
    \node [left=1cm of weight-\i] (input-\i) {$x_{\l}$};
    \draw [->, thick] (input-\i) -- (weight-\i);
}

\foreach \i in {1,...,5}
  \draw [->, thick] (neuron-\i) -- ++(4,0);

\begin{scope}[shift={(14,-5)}]
\foreach \c [count=\n from 0, evaluate={%
  \i=mod(\n,9); \j=int(\n/9);
  \x=(2*\i+mod(\j,2))*cos 30;
  \y=6-\j*1.5;
  \s=\c*10+10;}] in 
{   2,2,2,2,2,4,4,4,4,
    2,2,2,2,4,4,4,4,4,
    5,5,2,2,2,4,4,4,4,
    5,5,2,2,0,4,4,4,4,
    5,5,5,0,0,0,0,0,0,
    5,5,0,0,0,0,0,0,0,
    5,5,1,0,0,0,0,0,0,
    1,1,1,1,0,0,0,3,3,
    1,1,1,1,1,0,3,3,3,
    1,1,1,1,1,3,3,3,3,
    1,1,1,1,1,3,3,3,3
}
\draw [fill=black!\s] 
    (feature map cs:x=\x+cos -30, y=\y+sin -30) \foreach \a in {30,90,...,270}
      { -- (feature map cs:x=\x+cos \a, y=\y+sin \a)} -- cycle;
\end{scope}
\end{tikzpicture}

\end{document}

enter image description here