How to export the spike train data from .ses file to matlab

The basics of how to develop, test, and use models.
Post Reply
Kim Ha
Posts: 18
Joined: Sun Jun 09, 2013 11:26 pm

How to export the spike train data from .ses file to matlab

Post by Kim Ha »

Hi Ted,
I build a neural network of fish and obtain the spike train of left and right neurons.
Could you show me how can I export the spike train data from .ses file to matlab automatically?
Thank you!
ted
Site Admin
Posts: 5786
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: How to export the spike train data from .ses file to mat

Post by ted »

A .ses file will only contain information that will recreate the graph itself, without the data. You can get the spike times from a graph by using the Print & File Window Manager to select the graph, then in the Print & File Window Manager click on Print / Ascii. If the graph was a Network Builder's "SpikePlot", the ascii file's contents will look something like this:

Code: Select all

PolyLines
Line Manifest:
3
2
End of Line Manifest
3
0.50145 2
0.536129        2
1.66421 2
2
1       1
2       1
This data is from a model with two cells. Cell 2 generated 3 spikes at irregular times, and cell 1 generated 2 spikes (one at 1 ms and the other at 2 ms).

That's not very convenient if your network has many cells, or if you have to do this many times. You may instead prefer to use the NetCon class's record method to capture the spike times and cell IDs to a pair of Vectors, then after the simulation ends, write the Vectors to an output file. Example: if your cells are artificial spiking cells called cell1 and cell2, execute this code

Code: Select all

objref tvec, idvec
tvec = new Vector()
idvec = new Vector()
objref tmp, nil
tmp = new NetCon(cell1, nil)
tmp.record(tvec, idvec, 1)
tmp = new NetCon(cell2, nil)
tmp.record(tvec, idvec, 2)
objref tmp
after model setup is complete but before calling run(), and every time you run a simulation, the cell spike times and IDs will be recorded to tvec and idvec. For the spike data example I showed above, the vectors will look like this:

Code: Select all

tvec           idvec
0.50145        2
0.536129      2
1                  1
1.66421        2
2                  1
To write these data to an output file you could use the File class to open a file, then use the File class's printf method, e.g.
for i=0,tvec.size-1 fil.printf("%f \t%d\g", tvec.x, idvec.x)
Kim Ha
Posts: 18
Joined: Sun Jun 09, 2013 11:26 pm

Re: How to export the spike train data from .ses file to mat

Post by Kim Ha »

Hi Ted,
Thank again for your detail instruction.
I have executed your code in the command window. However it appears like this:

Code: Select all

objref tvec, idvec
oc>tvec = new Vector()
oc>idvec = new Vector()
oc>objref tmp, nil
oc>tmp = new NetCon(L1, nil)
nrniv: undefined variable L1
 near line 7
 tmp = new NetCon(L1, nil)
I build the model with some neurons (I choose "network cell/ from cell builder" to build them. So are they called artificial cell?) and name them L1, L2 and so on.
Could you help me to figure out the mistake here?
Thank a lot!
ted
Site Admin
Posts: 5786
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: How to export the spike train data from .ses file to mat

Post by ted »

Kim Ha wrote:I build the model with some neurons (I choose "network cell/ from cell builder" to build them.
So your model cells are built from sections, and they have membrane capacitance, ion channels, and membrane potential. I'd call these "biophysical model cells" because they involve direct representations of the biophysical properties of real cells. Artificial spiking cells are much simpler--they are "integrate and fire models" that mathematicians and physicists prefer because the equations that describe them often have analytical solutions, unlike the equations that describe biophysical model cells.

In NEURON, artificial spiking cells generate events, so in order to monitor such a cell for spikes it is sufficient to tell the NetCon the name of the cell, and that's what this statement does:
tmp = new NetCon(cell1, nil)
But to monitor a biophysical model cell for spikes, you have to tell the NetCon which of the cell's variables it should watch. Usually the variable is membrane potential. If you have a single compartment model called soma, the syntax would be
soma tmp = new NetCon(&v(0.5), nil)
and if the cell is an instance of a cell class called Cell and has a section called axon, the syntax would be something like
Cell[0].axon tmp = new NetCon(&v(1), nil)
(v(1) because you probably want to watch membrane potential at the distal end--the 1 end--of axon).

There is one more detail that I must mention. To save space on the Network Builder's canvas, that tool shows "nicknames" instead of the real hoc names of the model cells. For example, right now I made a toy network with just one class of biophysical model cell. The NetReadyCellGUI tool tells me the "Cell Name" is "L" and that's also the name I see on the Network Builder's canvas (well, I see L0, L1 etc.). But hoc doesn't know about L0 or L1; it knows these cells by other names. To find out what those names are, click on the Network Builder's "Net Variables" button and select "Show Map". This pops up a panel that has the string "NetGUI[0] name <-> Instance Name" in its drag bar, and below that I see

Code: Select all

L0    L_Cell[0]
L1    L_Cell[1]
so the hoc names for my cells are really L_Cell[0] and L_Cell[1] and I'd attach a NetCon to L_Cell[0] with a syntax like this
L_Cell[0].soma tmp = new NetCon(&v(0.5), nil)

I'm sorry if this seems complicated.
Kim Ha
Posts: 18
Joined: Sun Jun 09, 2013 11:26 pm

Re: How to export the spike train data from .ses file to mat

Post by Kim Ha »

Hi Ted,
Your instruction is quite clear. I got the code worked but not automatically. I execute the following code to export the data to a file:

Code: Select all

oc>objref f1
oc>f1 = new File("cell3.txt")
oc>f1.wopen("cell3.txt")
        1 
oc>for i=0, tvec.size-1 nil {
>       oc>f1.printf("%f\t%d\g", tvec.x[i], idvec.x[i])
>       oc>}
oc>f1.close()
        0 
oc>run()
When I change the delay time in the model, the spike train changes its values. However, the values recorded in the "cell3.txt" file do not change.
Can you show me the mistakes in my code?
Your help means a lot to me. Thanh you so much!
ted
Site Admin
Posts: 5786
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: How to export the spike train data from .ses file to mat

Post by ted »

First, a comment:
I'm surprised that the "nil" in this line
oc>for i=0, tvec.size-1 nil {
didn't cause an error message.

If you generate new results, you have to write the new results to a file. Why don't you write a procedure, based on the code you already developed (but leaving out the "nil"), that does this for you?

Code: Select all

objref f1
proc saveresults() { local i
  f1 = new File("$s1")
  f1.wopen("$s1")
  for i=0, tvec.size-1 {
    f1.printf("%f\t%d\g", tvec.x[i], idvec.x[i])
  }
  f1.close()
}
Then if you change a parameter and run a new simulation, you can save the new results by entering a command like this
saveresults("newcell3.txt")
at the oc> prompt.

If you get tired of typing run() and then typing saveresults("somefilename") you could write another procedure

Code: Select all

proc myrun() {
  run()
  saveresults($s1)
}
so now you would only have to type
myrun("somefilename")
to run a simulation and automatically save results.
Kim Ha
Posts: 18
Joined: Sun Jun 09, 2013 11:26 pm

Re: How to export the spike train data from .ses file to mat

Post by Kim Ha »

Hi Ted,
I got your idea.
Instead of saving the spike trains of all cells in a same file, I now want to save them separately. For example, "cell1.txt" contains the spike train of the cell 1 only and "cell2.txt" contains the spike train of cell 2 only and so on. Therefore, I use string to name the files. I execute the following code:

Code: Select all

oc>objref f1
oc>for i=0, tvec.size()-1 {
oc>name = sprint(tmpspr, "d.dat", idvec.x[i])
oc>f1 = new File(name)
oc>f1.wopen(name)
oc>f1.printf("%f\t", tvec.x[i])
>       oc>f1.close()
>       oc>}
oc>run()
However, it gives me nothing. Could you help me to figure out?
Kim Ha
Posts: 18
Joined: Sun Jun 09, 2013 11:26 pm

Re: How to export the spike train data from .ses file to mat

Post by Kim Ha »

Hi Ted,
I have rechecked my code and found many silly mistakes. I have fixed my code:

Code: Select all

oc>objref f2
oc>for i=0, 4 {
oc>sprint(name, "%d", idvec.x[i])
oc>f2 = new File("name")
oc>f2.wopen("name")
oc>for j=0, tvec.size()-1 {
oc>f2.printf("%f\n", tvec.x[j])
>               oc>}
>       oc>f2.close()
>       oc>}
oc>run()
However, I still cannot separate the spike trains of different cells into different files. Could you help me to find out why?
Thank you!
ted
Site Admin
Posts: 5786
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: How to export the spike train data from .ses file to mat

Post by ted »

Are you at least able to write all spike times (of all cells) to a file?
Kim Ha
Posts: 18
Joined: Sun Jun 09, 2013 11:26 pm

Re: How to export the spike train data from .ses file to mat

Post by Kim Ha »

Hi Ted,
Yes I am able to save all spike times to a file now.
ted
Site Admin
Posts: 5786
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: How to export the spike train data from .ses file to mat

Post by ted »

Network modelers typically save all spike times to a single file that has a simple format:
each line contains two numbers--
spiketime cellidentifier
where
spiketime is the time at which the spike happened
and
cellidentifier is an integer that is unique for each cell (we call it a "global identifier" or "gid" pronounced "gee-eye-dee"--see
Hines, M.L. and Carnevale, N.T.
Translating network models to parallel hardware in NEURON.
J. Neurosci. Methods 169:425-455, 2008
preprint available from http://www.neuron.yale.edu/neuron/nrnpubs)
Example: in a network with N cells, each cell will have a gid in the range 0..N-1. This is sufficient for all data analysis purposes, whether one is using Matlab, R, or anything else.
Post Reply