Page 1 of 1

query export data into matrix

Posted: Sun Jan 18, 2009 12:12 pm
by Nuri
Dear Ted,

I have started using this program very recently, and I would like to ask you about a particular piece of code doesnt work.

I have been implementing McIntyre's axon (from the model DB) and I have created a program to store the potential values of each node (the axon has 21 nodes) in a matrix, to export it to a data file and process it in matlab. However, when I write this in a loop, it does not work, and I do not want to run the program 21 times, because that is computationally inefficient. I am therefore copying 2 pieces of code.

1. This one does not work.

objref recv,m,savdata

recv= new Vector()
m= new Matrix()

savdata= new File()

for i=0,20{
recv.record(&Mcaxons.node.v(0.5))
m.resize((recv.size()+1),20)
m.setcol(i,recv)
}

tstop=105
run()

savdata.wopen("matrixv1.dat")
m.fprint(savdata, "%g")

savdata.close()

2. If I put the run() command before recv.record(...) it does not record anything. Why does this happen?

3. If I dont loop things my program works, why is this?

objref recv,recv1,recv2,m,savdata


recv=new Vector()
recv1=new Vector()
recv2= new Vector()

m= new Matrix()

savdata= new File()

recv.record(&Mcaxons.node[0].v(0.5))
recv1.record(&Mcaxons.node[1].v(0.5))
recv2.record(&Mcaxons.node[2].v(0.5))

tstop=105
run()

m.resize((recv.size()+1),3)
m.setcol(0,recv)
m.setcol(1,recv1)
m.setcol(2,recv2)

savdata.wopen("matrixv.dat")
m.fprint(savdata, "%g")

savdata.close()

Could you kindly suggest a better way of coding this? Thank you.

Re: query export data into matrix

Posted: Mon Jan 19, 2009 11:13 pm
by ted
Good questions. Clean looking code. You're already close to the right answers.
Nuri wrote:1. This one does not work.
While it is essential to have a clear conception of what a completed program should do, it is also essential to develop incrementally, testing at every step, especially when writing code in an unfamiliar programming language. And never more so than when one is already familiar with another programming language (did I guess correctly?), because of a phenomenon similar to what linguists call "false friends" (http://en.wikipedia.org/wiki/False_friends).

It is also important to read and re-read the documentation, especially after one's code does not work as expected. No matter how many times I have read a particular passage, an unexpected outcome often means that yet one more reading is needed to improve my understanding.

Code: Select all

for i=0,20{
recv.record(&Mcaxons.node[i].v(0.5))
. . .
To quote from the Programmers' Reference documentation of Vector record
vdest.record(&var)
. . .
DESCRIPTION
. . .
Previous record and play specifications of this Vector (if any) are destroyed.
So each pass through the loop breaks what the previous pass did. If you want to record from N nodes, you will need N Vectors.

You have the right idea about assembling the recorded Vectors into a Matrix, then writing the Matrix to a file. But the assembly can only be done after the Vectors contain data (otherwise, you'll be assembling a bunch of empty Vectors into a Matrix that will have no elements). Do it after you call run().

The documentation of the Matrix class's setcol method says
The vector must have size msrcdest.mrow
but this statement
m.resize((recv.size()+1),20)
will make the matrix one row too long.
2. If I put the run() command before recv.record(...) it does not record anything. Why does this happen?
To quote again from the documentation of Vector record():
Transfers take place on exit from finitialize() and on exit from fadvance().
This means that they only occur during initialization and when the simulation advances from one time step to the next, i.e. only during a simulation. run() executes a simulation.
Could you kindly suggest a better way of coding this?
You need a good way to manage recording from many nodes. One way is to use an "array" of objrefs, e.g. something like this

Code: Select all

// define important constants near the top of the program, so they're easy to change in the future
NUMNODES = 21
 . . .
// set up Vector recording
objref recv[NUMNODES]
for i=0,NUMNODES-1 {
  recv[i] = new Vector()
  recv.record(&Mcaxons.node[i].v(0.5))  
}
 . . . whatever else you need to do before launching a simulation . . .
run() // runs the simulation
// now there are data to be written to a file
// assemble the records into a matrix
objref m
m = new Matrix(recv[0].size(), NUMNODES)
for i=0,NUMNODES-1 {
  m.setcol(i, recv[i])
}
// ready to open a file and write the matrix to it
 . . .
Of course you should check to make sure I haven't made one of dozens of possible errors (typographical, "off by 1", syntax . . .)

Re: query export data into matrix

Posted: Tue Jan 20, 2009 11:49 am
by Nuri
Many thanks for your helpful reply! It works now.

I am also implementing different ways of counting action potentials. You mentioned that NetCon is a more powerful method than APcount in a different wall post.

Why is this?

Re: query export data into matrix

Posted: Tue Jan 20, 2009 1:58 pm
by ted
Nuri wrote:I am also implementing different ways of counting action potentials. You mentioned that NetCon is a more powerful method than APcount in a different wall post.

Why is this?
The NetCon class's record method captures the time of each spike.
http://www.neuron.yale.edu/neuron/stati ... tml#record

Re: query export data into matrix

Posted: Thu Jan 22, 2009 7:17 am
by Nuri
Many thanks for your help.

Nuri

Re: query export data into matrix

Posted: Thu Jan 22, 2009 7:51 am
by ted
Thank you for using NEURON in your research.