2

I am trying to plot neuron's spikes which are stored in a csv file the following way:

time, neuron1, neuron2, neuron3,...
1,1,0,1,...
2,1,0,0,...
3,0,0,1,...

and so on...

Therefore the data provides if a spike has happened at a time. 1 means a spike happened while 0 means it didn't.

I have followed this thread: Add Raster Points Into Another Plot which produces the style I want. Which would be something like what it is shown in the picture.

However in that thread they randomly generate the spiking pattern. In short, I would like something like that but with the data provided by the csv file.

3
  • 1
    Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – albert
    Aug 11, 2018 at 13:16
  • How do you want the plotted data to look like? Please at least provide some kind of example. Aug 11, 2018 at 13:47
  • I cited the thread I had followed which shows an image of what I wanted and also provides useful code which just lacks reading from a .csv file. So, I thought that the other thread would serve as both a MWE and an example of how it should look like.
    – Fabio
    Aug 20, 2018 at 0:09

1 Answer 1

4

I hope I interpret this question correctly. This answer processes tables just containing the data, i.e. I do not see the point why one wants to put neuron 1, neuron 2 ..., but this can of course be changed.

\documentclass[border=3.14mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents*}{neurondata.csv}
0,0,0,0,0,0,0,0
0,0,0,0,0,1,0,0
0,0,0,0,0,1,0,0
0,0,0,0,1,1,0,0
0,0,0,0,1,1,0,0
0,0,0,1,0,1,0,0
0,1,0,0,0,1,0,0
0,0,0,0,0,0,0,0
\end{filecontents*}
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
    \let#4\pgfplotsretval
}
\newsavebox\Flat
\sbox\Flat{\tikz{\path[use as bounding box](-0.5,0) rectangle (0.5,1);
\draw[blue] (-0.5,0.1) -- ++ (1,0);}}
\newsavebox\Spike
\sbox\Spike{\tikz{\path[use as bounding box](-0.5,0) rectangle (0.5,1);
\draw[blue] plot[variable=\x,smooth,samples=50,domain=-0.5:0.5]
(\x,{0.07+0.8*exp(-15*\x*\x)});}}
\begin{document}
\pgfplotstableread[header=false,col sep=comma]{neurondata.csv}\datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
\begin{tikzpicture}
\foreach \Y [evaluate=\Y as \PrevY using {int(\Y-1)}] in {1,...,\numrows}
{\node[anchor=east] at (-0.1,-\Y+0.5) {neuron \Y};
\foreach \X  [evaluate=\X as \PrevX using {int(\X-1)}] in {1,...,\numcols}
{
\ReadOutElement{\datatable}{\PrevY}{\PrevX}{\Current}
\ifnum\Current=1
\node at (\X-0.5,-\Y+0.5) {\usebox\Spike};
\else
\node at (\X-0.5,-\Y+0.5) {\usebox\Flat};
\fi
}
}
\foreach \X  in {1,...,\numcols}
{\node[anchor=south] at (\X-0.5,0.1) {\X};}
\draw (0,0) grid (\numcols,-\numrows);
\end{tikzpicture}
\end{document}

enter image description here

5
  • This answer shed enough light on the path to get what I wanted. However, my .csv file has thousands of points to be depicted which exceeds the capacity of latex. In addition the command \pgfplotstablegetelem really slows down the compilation if used within a loop. Now I am wondering if there exist a way to load the whole column to a "kind of vector" and then access every component like in C# or any other language. This way, I guess it may solve the performance issues.
    – Fabio
    Aug 19, 2018 at 23:35
  • @Fabio I am sorry, I really do not know that. If it is really the \pgfplotstablegetelem then you may try to load this binary data directly in a large \foreach loop. You just need to wrap the binary list into a \foreach \Z [count=\N] in in {<your list} and then inside the loop \pgfmathtruncatemacro{\X}{1+mod(\N,8)}\pgfmathtruncatemacro{\Y}{(\N-1)/8} (or replace 8 by the appropriate dimension). (BTW, I saw you posted another answer, just to let you know that the downvote is not from me.)
    – user121799
    Aug 20, 2018 at 0:36
  • Mmm... What do you exactly mean by the binary list? My Data is in a .csv file. Sorry but I do not really get what you are suggesting here. Could you explain it a bit further?. (P.S. Thank you for letting me know)
    – Fabio
    Aug 21, 2018 at 12:43
  • @Fabio I mean a list of 0 and 1 entries. Maybe binary is not the right adjective.
    – user121799
    Aug 21, 2018 at 14:12
  • The columns are lists of 0 and 1 yes. They are inside the csv file. Like what I show at the beginning. I do not generate the data in these columns, it is read from somewhere else. I mean, it is real data, not simulated. Do you still know if there is an alternative way to access the elements that does not slow down the compilation process so much? And what's more important, that it does not exceed the latex capacity?
    – Fabio
    Aug 21, 2018 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.