Dynamically create vector names

Anything that doesn't fit elsewhere.
Post Reply
jainsanket
Posts: 5
Joined: Wed Apr 07, 2010 1:51 pm

Dynamically create vector names

Post by jainsanket »

Hello Experts,

I am working on code which records the action potential times for the soma and nodes of the cell model. I am using the APCount for the same. In my cell model, I have single soma and 20 nodes. Below is a part of my code which records the action potential times for the soma.

Code: Select all

create soma
objref apc_soma   //Creating AP Counter in the soma. 
access soma
apc_soma=new APCount(.5)
objref soma_timevector
soma_timevector=new Vector()
apc_soma.record(soma_timevector)
Since there is only a single soma, the soma_timevector saves the action potential times from the soma. For the nodes, the case is slightly different. Since, there are multiple nodes and the number of action potentials can be different in different nodes, I cannot save the action potential times in a matrix. Below is the code which creates the AP Counter at all nodes.

Code: Select all

create node[20]  
objref apc_nodes[20]   // Creating AP Counter at the nodes

objref temp_nodes_timevector
temp_nodes_timevector=new Vector()

for j=0, 19 {
	access node[j]
	apc_nodes[j]=new APCount(.5)
	apc_nodes[j].record(temp_nodes_timevector)
}
Is there any way I can dynamically create a variable name, timevector1, timevector2 .... and so on for every iteration?

My final objective is to save these action potential times in a text file. I thought, if I could get the variables with different names, then I could write to a text file.

Thank you,

Sanket Jain
ted
Site Admin
Posts: 6394
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Dynamically create vector names

Post by ted »

First, I must note that the for loop commits "access abuse" (see
Use only one "access" statement
in the Hot tips area of this forum).
It would be better to write this loop as

Code: Select all

for j=0, 19 node[j] {
   apc_nodes[j]=new APCount(.5)
   apc_nodes[j].record(temp_nodes_timevector)
}
The general strategy you describe will work, but it doesn't scale well with increasing problem size. You could indeed create a unique name for each spike time vector by using sprint() to generate a statement, then executing that statement like so

Code: Select all

strdef cstr
for j=... {
  sprint(cstr, "objref timevec%d = new Vector()", j)
  execute(cstr)
}
But when it came time to write the contents of all those vectors to a file, you'd have to iterate over their names, so you'd need another for loop that uses sprint to create each name a second time.

Instead, it's easier to employ the
create name[number]
idiom that you already used to create "arrays" (actually more like indexed lists) of sections and "apc_nodes" objrefs. That is,

Code: Select all

objref timevec[20]
for j=0,19 node[j] {
  apc_nodes[j] = new APCount(0.5)
  timevec[j] = new Vector()
  apc_nodes[j].record(timevec[j])
}
You probably already thought of this in the few hours since you posted your query.

A hint: instead of embedding magic numbers in your code, why not define symbolic constants? For example, if you know the number of nodes is 20, near the top of your program
NNODES = 20
then every time you create that many instances of a particular item
create nodes[NNODES]
or
objref timevec[NNODES]
and every for loop becomes
for i=0, NNODES-1 . . .

Another hint: forget about array notation and use lists instead--Lists for objrefs, SectionLists for sections.
jainsanket
Posts: 5
Joined: Wed Apr 07, 2010 1:51 pm

Re: Dynamically create vector names

Post by jainsanket »

Thanks for your reply Ted. I am follow your suggestions and get back to you if required.
jainsanket
Posts: 5
Joined: Wed Apr 07, 2010 1:51 pm

Re: Dynamically create vector names

Post by jainsanket »

Hey Thanks.The code works. I understood most of your suggestions. Except the last one,
Another hint: forget about array notation and use lists instead--Lists for objrefs, SectionLists for sections.
Can you please expand on the same or provide some link to the neuron documentation?
ted
Site Admin
Posts: 6394
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Dynamically create vector names

Post by ted »

Lists are commonly used in object oriented programming as a means for dealing with collections of objects. An advantage of lists is that they resize dynamically, unlike arrays of objects, so you don't need to know in advance how big to make the list. Suppose next week you want to reuse your spike recording code with a model cell that has a different number places where you want to monitor spike times. With the present implementation of your code, every time you deal with a different model, you have to change a magic number (or at least decide whether it is necessary to change it).

In pseudocode, the idiom for using a List to manage a collection of items is

Code: Select all

create a list object "xlist" to manage class X
for each new object of class X that you need
  create the object and append it to "xlist"
In the context of your particular problem, let's suppose your model cell already exists. It has a bunch of sections, each of which has at least 1 segment. You want to attach an APCount to each segment.
objref tobj, apclist
apclist = new List()

Code: Select all

forall for (x,0) { // a nested for loop. forall iterates over all sections (the outer loop)
  // and for (x,0) (the inner loop) iterates over all internal nodes of the currently accessed section
  tobj = new APCount(x) // create a new APCount attached to location x on current section
  apclist.append(tobj)
}
objref tobj // destroy link between tobj and the last APCount that was created
apclist is now a List that contains all instances of the APCount class. Any time you need to know how many there are, it's just apclist.count(). apclist.o(0) is the first one you created, and apclist.o(apclist.count()-1) is the last one. It's very liberating to not have to remember magic numbers, or the names of symbolic constants that you used to keep track of how many instances of a class you are dealing with.

You'll find forall, for, and the List class documented in the Programmer's Reference.

SectionLists aren't quite as flexible as Lists, but they're very handy for defining subsets of sections (e.g. for managing sections with shared biophysical properties). In those occasional cases where ultimate flexibility is required, use SectionRefs and append those to Lists. See the Programmer's Reference for documentation of these classes and their methods.
jainsanket
Posts: 5
Joined: Wed Apr 07, 2010 1:51 pm

Re: Dynamically create vector names

Post by jainsanket »

Thank you Ted for the detailed explanation. I will refer to the programmer's reference for more details.
Post Reply