Is there a way in NEURON to detect the directory that the current file is in?
My model contains a bunch of files that include each other, but because the current directory is generally undefined for them (depending on whether you use nrniv.exe from the command line, or double click them in Windows, or load them from nrngui, all result in different current directories), they can't load each other. What I want to do is to make the load_file(...) to load files relative to the location of the current file, and not relative to the location of the current directory as it is done now. I figured that the best way to do so would be to manually change the current directory to a directory that the currently accessed file is in.
Detecting the location of the current file
-
- Site Admin
- Posts: 6384
- Joined: Wed May 18, 2005 4:50 pm
- Location: Yale University School of Medicine
- Contact:
Re: Detecting the location of the current file
Generally it is best to put all the necessary mod files in one directory, and put an "administrative wrapper" file in the same directory. The latter file, often called init.hoc, contains a series of load_file() statements, the string arguments of which can contain paths to whatever files are necessary, e.g.
load_file("./sub1/ . . . /file.ses")
load_file("../../whatever.hoc")
Then executing init.hoc within its home directory guarantees that the proper mechanisms are loaded.
load_file("./sub1/ . . . /file.ses")
load_file("../../whatever.hoc")
Then executing init.hoc within its home directory guarantees that the proper mechanisms are loaded.
Read about getcwd in the Programmer's Reference function list http://www.neuron.yale.edu/neuron/stati ... #functionsIs there a way in NEURON to detect the directory that the current file is in?
Re: Detecting the location of the current file
getcwd() is unsuitable for my purposes, because its return value is not constant.
E.g. a small hoc file like this:
will return (in my case):
/cygdrive/c/cluster/
if I double-click it. However, if I run it like this:
C:\Documents and Settings>C:\nrn61\bin\neuron.exe C:\cluster\test.hoc
It will return:
/cygdrive/c/Documents and Settings/
What I want (if possible) is a function that would always return
/cygdrive/c/cluster/
i.e. the directory path to the file invoking the said function, so I can use chdir() using it so that my model can be run from any location and remain functional.
E.g. a small hoc file like this:
Code: Select all
print getcwd()
/cygdrive/c/cluster/
if I double-click it. However, if I run it like this:
C:\Documents and Settings>C:\nrn61\bin\neuron.exe C:\cluster\test.hoc
It will return:
/cygdrive/c/Documents and Settings/
What I want (if possible) is a function that would always return
/cygdrive/c/cluster/
i.e. the directory path to the file invoking the said function, so I can use chdir() using it so that my model can be run from any location and remain functional.
-
- Site Admin
- Posts: 6384
- Joined: Wed May 18, 2005 4:50 pm
- Location: Yale University School of Medicine
- Contact:
Re: Detecting the location of the current file
Now I understand what you meant by "current file" but I don't see what your problem is. Maybe it's Monday, or maybe it's just me (I generally avoid complex file deployment schemes).
Consider the following directory architecture:
i.e. test.hoc and reply.hoc are both in /home/ted/foo
where the file contents are:
init.hoc
test.hoc
reply.hoc
Then running from the command line inside /home/ted/fap
Granted, this is under Linux and from the command line, but I don't see why it wouldn't work under another OS.
Consider the following directory architecture:
Code: Select all
home/ted/bah/init.hoc
/foo/test.hoc
/reply.hoc
/fap/ (empty)
where the file contents are:
init.hoc
Code: Select all
print "read init.hoc"
load_file("../foo/test.hoc")
Code: Select all
print "read test.hoc"
load_file("reply.hoc")
Code: Select all
print "found it"
Code: Select all
[/home/ted/fap]$ nrngui ../bah/init.hoc
. . . omitting NEURON's "banner" . . .
read init.hoc
read test.hoc
found it
Re: Detecting the location of the current file
I just found a solution for this problem of mine outside of NEURON, that basically reduced to what you did in this last post. This was for an MPI program that had issues setting the current working directory, so I figured I'd try doing that through NEURON. In the end, I managed to coerce it to work as it is supposed to.
Thanks!
Thanks!
-
- Site Admin
- Posts: 6384
- Joined: Wed May 18, 2005 4:50 pm
- Location: Yale University School of Medicine
- Contact:
Re: Detecting the location of the current file
Care to share the solution, or point to it?
Re: Detecting the location of the current file
Naturally. This only applies to DeinoMPI, which I am using for my NEURON MPI work on windows. The idea is that you should set the 'working directory' of the process stager to the location of your primary .hoc file. This encourages NEURON to set its current directory to point to somewhere useful.