Good question. Python has arrays, lists, and tuples because each of these things is good for a particular purpose. It's the purpose that determines which of these is best to use for managing a collection of objects.
patoorio wrote:How do I translate a code like the following into python?
Code: Select all
objref GABAinp[N_in], GABApre[N_in], GABAconn[N_in]
for ii= 0,N_in-1 {
GABAinp[ii] = new Exp2Syn(0.5)
GABApre[ii] = new NetStim(0.5)
GABAconn[ii] = new NetCon(GABApre[ii],GABAinp[ii])
GABAconn[ii].weight = 0.0005
}
I'm still not sure how to make indexed arrays of objects in python, as I'm still beggining to understand arrays, lists and tuples.
It's almost never a good idea to use an indexed array of objects. Lists of objects are usually a far superior way to manage collections of objects, whether you're writing hoc or Python.
The first question to ask is, which type of object is best to use for managing collections of synaptic mechanisms and NetCons. The problem with arrays is that you have to know how many elements are in the array. You have to define a symbolic constant, and every time you want to iterate over every element in the array you have to remember the name of the symbolic constant. What happens if SomeObject[j] is destroyed, where 0<=j<number_of_objects_in_this_array ? Will the objects whose indices are > j automatically be given new indices? If not, what happens when your iterator statement gets to ii = jj -- code fails, right? And what if you forget to assign a new value to the symbolic constant that keeps track of how many objects are in the array? Another code failure. And what if your model contains many different kinds of synaptic mechanisms, and you want to iterate over all of them--you can't just write
Code: Select all
for ii=0,total_number_of_synaptic_mechanism_instances-1 {
statements that involve SomeName[ii]
}
because there isn't a single class name that refers to each of the different kinds of synaptic mechanisms.
These are some of the reasons why it's better to manage collections of objects with a List. A hoc implementation that uses Lists might look like this
Code: Select all
objref gabasyn, gabapre, gabanc
gabasyn = new List()
gabapre = new List()
gabanc = new List()
for ii=0,N_in-1 {
gabasyn.append(new Exp2Syn(0.5))
gabapre.append(new NetStim())
gabanc.append(new NetCon(gabapre.o(ii), gabasyn.o(ii))
gabanc.o(ii).weight = 0.0005
}
Then if you ever need to iterate over all elements of any of these lists, do something like this
Code: Select all
for ii=0,gabanc.count-1 {
gabanc.o(ii).weight = 0.0004
}
The Python syntax for creating and using Lists is a bit different, but your transition from hoc to Python will be smoother if you start by using Lists rather than indexed arrays.
A final comment: these examples are a bit contrived, because if each gabasyn is going to have the same time constants and reversal potential, and they are all going to be attached to the same location (compartment) in a model, it would save runtime and memory to use a single instance of Exp2Syn. The code would then become
Code: Select all
objref gabasyn, gabapre, gabanc
gabasyn = Exp2Syn(0.5)
gabapre = new List()
gabanc = new List()
for ii=0,N_in-1 {
gabapre.append(new NetStim())
gabanc.append(new NetCon(gabapre.o(ii), gabasyn)
gabanc.o(ii).weight = 0.0005
}