How to create a list of procedures

Anything that doesn't fit elsewhere.
Post Reply
Keivan
Posts: 127
Joined: Sat Apr 22, 2006 4:28 am

How to create a list of procedures

Post by Keivan »

I work on a detailed neuron model fitted to data and I want to build a list of functions and use it to make a dynamic init() function.
Every Channel in my model added to the model with a separate hoc file using a procedure. for example If I want to add HH to my model I make insert_hh.hoc file which contain a init_hh () function. advantage of this method is that I can add or remove mechanism with commenting the load_file("insert_??") at the beginning of the models main file. But to do this my init() function should be dynamic too.
I want a mechanism to update my init() function every time I add a new mechanism or remove a mechanism from the model. for example I'm thinking of a template which stores list of my init_??() functions. I can add a function to the list with template_name.add("function name") and run the list of functions with template_name.run().

1. is there any Class sth like this in the neuron?
2. if answer is no.
2.1 How can I make one myself?
2.2 Is there any array of strings at hoc level? if no how can I store a list of functions?
2.3 Is execute command proper for this job?
3. Is there any better way to do this other than what I propose?

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

Re: How to create a list of procedures

Post by ted »

1. No.
2.1 Would have to think about it. Too busy with other stuff right now.
2.2 No. Use instances of the String class (defined in stdlib.hoc; look in nrn/lib/hoc) and append them to a List.
2.3 execute() would execute a string.
3. See 2.1

Keivan
Posts: 127
Joined: Sat Apr 22, 2006 4:28 am

Re: How to create a list of procedures

Post by Keivan »

Thank you ted. With your guidance I could write a simple FList class.

Code: Select all

load_file("stdlib.hoc")
begintemplate FList
public FCount, add, list, exec
objref Fname[100], FnList
proc init() {
	FCount =0
	FnList = new List()
}
proc add() { //adds a function name to the list
	Fname[FCount] = new String($s1)
	FnList.append(Fname[FCount])
	FCount+=1
}
proc list() { //prints the  list of functions
	for i=0,FCount-1 {
		print FnList.o(i).s
	}
}
proc exec() { //executes the list of functions.
	for i=0,FCount-1 {
		execute(FnList.o(i).s)
	}
}
endtemplate FList
a simple test code

Code: Select all

load_file("FList.hoc")
proc hines() {
	print "hello hines"
}
proc keivan() {
	print "hello keivan"
}
proc ted() {
	print "hello ted"
}

objref FL
FL = new FList()
FL.add("ted()")
FL.add("hines()")
FL.add("keivan()")
FL.list()
FL.exec()
This class works fine and seems to be enough to me. But I'm curious about how I can make "objref Fname[100]" part more intelligent? ie. I want the FList class detect it's required instances of Fname object itself.
Last edited by Keivan on Sun Jun 26, 2011 3:32 pm, edited 1 time in total.

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

Re: How to create a list of procedures

Post by ted »

Good work.
how can I make "objref Fname[100]" part more intelligent?
Four suggestions. First, you can avoid having to keep track of the number of objects that have been appended to the List. The List class has a count() method that will tell you how many items it contains. So you can get rid of FCount and change statements appropriately. For example,

Code: Select all

   for i=0,FCount-1 {
becomes

Code: Select all

   for i=0,FnList.count()-1 {
Second, you don't have to keep the objref that was created by the
... = new String($s1)
statement. Simply appending to the List means that there are two objrefs that point to the String instance, so as long as the List exists you can throw away the other one (or reuse it for something else). So instead of
objref Fname[100]
you could simply declare
objref Fname
and reuse Fname every time you add a new name.

Third, you can get rid of Fname. Instead, you could use a localobj inside proc add()--

Code: Select all

proc add() { localobj tobj //adds a function name to the list
   tobj = new String($s1)
   FnList.append(tobj)
}
Fourth, you can get rid of tobj. This should work:

Code: Select all

proc add() { //adds a function name to the list
   FnList.append(new String($s1))
}

Keivan
Posts: 127
Joined: Sat Apr 22, 2006 4:28 am

Re: How to create a list of procedures

Post by Keivan »

WoW. You are very intelligent.
Thanks
This is the final code. It may help someone else.

Code: Select all

load_file("stdlib.hoc")
begintemplate FList
public add, list, exec
objref FnList
proc init() {
	FnList = new List()
}
proc add() {//adds a function name to the list
	FnList.append(new String($s1))
}
proc list() { local i //prints the  list of functions
	for i=0,FnList.count()-1 {
		print FnList.o(i).s
	}
}
proc exec() { local i //executes the list of functions.
	for i=0,FnList.count()-1 {
		execute(FnList.o(i).s)
	}
}
endtemplate FList

Post Reply