Building a automatic control for opening and closinhoc files

The basics of how to develop, test, and use models.
Post Reply
pass1945
Posts: 21
Joined: Mon May 21, 2012 2:44 pm

Building a automatic control for opening and closinhoc files

Post by pass1945 »

Dear Ted, I'm wondering if I can create a "automatic control" file in NEURON. Specifically, I want to run several .hoc files simultaneously, and for each .hoc file I want them to open and then close on their own. Right now I'm doing the work of changing a parameter, then opening each file and closing it only to update my data files. It would be nice if I have a button that let me press and then all of them .hoc files will just run by themselves. Please let me know how I can achieve this. Thank you very much!
ted
Site Admin
Posts: 6300
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Building a automatic control for opening and closinhoc f

Post by ted »

As long as you aren't doing anything that might require exiting NEURON after each run to start from a clean slate, it's easy to generate a family of simulations and save their results to disk. Assuming that your program

--launches a single run by calling run()
and
--uses the Vector class's record() method to capture the time course of one or more variables of interest to Vectors

you could do this (in semi-pseudocode):

Code: Select all

proc batchrun() { local i
  if ($1>=1) {
    for i=0,$1-1 {
      whatever_parameter = f(i)
      run()
      create a new file name
      write results to a file with this name
    } else {
      print "error:  batchrun() argument < 1"
    }
  }
}
where
whatever_parameter is the parameter that you want to change from one run to the next
and
f() is a func that accepts an integer argument and returns the corresponding parameter value

A few more details are needed to flesh this out, e.g.

Code: Select all

// declare these outside of proc batchrun()
NAMEROOT = "foo" // or whatever you like
strdef fn
objref fil
The pseudocode
"create a new file name
write results to a file with this name"
then becomes

Code: Select all

      write results to a file with this name
      sprint(fn, "%s%d.dat", NAMEROOT, i)
      fil.wopen(fn)
      statements that write the parameter value and corresponding results to fil
      fil.close()
pass1945
Posts: 21
Joined: Mon May 21, 2012 2:44 pm

Re: Building a automatic control for opening and closinhoc f

Post by pass1945 »

Dear Ted!
Thank you for the response! Its been a while since I posted this thread, and I haven't gotten a chance to do multiple simulations until now.
The code you provided to me seemed really helpful. However, this seems to work only within one hoc file. Now for my case, I actually need to change the parameter and run multiple hoc files to get the result. So it's not only within one file. To make myself clear, here is what i mean:

change the same parameter in file 1, 2, 3, 4, and 5 -> run file 1-5 -> run file 6-10 based on the results of file 1-5 -> record results from file 6-10-> change the parameter in file 1-5 again-> repeat.

So it's not within one hoc file. Rather, I need to open multiple files and I want this entire process to be automatic so I don't have to manually open and close files. Would you please tell me how to do this? Thank you very much!

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

Re: Building a automatic control for opening and closinhoc f

Post by ted »

pass1945 wrote:I actually need to change the parameter and run multiple hoc files to get the result. So it's not only within one file. To make myself clear, here is what i mean:

change the same parameter in file 1, 2, 3, 4, and 5 -> run file 1-5 -> run file 6-10 based on the results of file 1-5 -> record results from file 6-10-> change the parameter in file 1-5 again-> repeat.

So it's not within one hoc file. Rather, I need to open multiple files and I want this entire process to be automatic so I don't have to manually open and close files.
I'd suggest you do this with whatever scripting language is available for your computer's operating system. Python might be a good choice. Beyond that I have no further suggestion.
pass1945
Posts: 21
Joined: Mon May 21, 2012 2:44 pm

Re: Building a automatic control for opening and closinhoc f

Post by pass1945 »

Dear Ted,
Thank you for your suggestions! Now I was able to build a bash script to control the opening and closing of Neuron. Now for collecting the results, can I create something inside of NEURON so that every time I start a simulation by opening the file, I can append my results? It seems like in NEURON, the results cannot be appended into the file. Instead, it just re-write the entire file. Would you tell me if this is true? Thank you!
ted
Site Admin
Posts: 6300
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Building a automatic control for opening and closinhoc f

Post by ted »

Read about the File class's append() method in the Programmer's Reference
http://www.neuron.yale.edu/neuron/stati ... html#aopen
Post Reply