Detecting the location of the current file

The basics of how to develop, test, and use models.
Post Reply
sl
Posts: 21
Joined: Wed Nov 28, 2007 12:52 am
Location: Cornell University

Detecting the location of the current file

Post by sl »

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.
ted
Site Admin
Posts: 6305
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Detecting the location of the current file

Post by ted »

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.
Is there a way in NEURON to detect the directory that the current file is in?
Read about getcwd in the Programmer's Reference function list http://www.neuron.yale.edu/neuron/stati ... #functions
sl
Posts: 21
Joined: Wed Nov 28, 2007 12:52 am
Location: Cornell University

Re: Detecting the location of the current file

Post by sl »

getcwd() is unsuitable for my purposes, because its return value is not constant.

E.g. a small hoc file like this:

Code: Select all

print getcwd()
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.
ted
Site Admin
Posts: 6305
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Detecting the location of the current file

Post by ted »

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:

Code: Select all

home/ted/bah/init.hoc
        /foo/test.hoc
            /reply.hoc
        /fap/  (empty)
i.e. test.hoc and reply.hoc are both in /home/ted/foo

where the file contents are:
init.hoc

Code: Select all

print "read init.hoc"
load_file("../foo/test.hoc")
test.hoc

Code: Select all

print "read test.hoc"
load_file("reply.hoc")
reply.hoc

Code: Select all

print "found it"
Then running from the command line inside /home/ted/fap

Code: Select all

[/home/ted/fap]$ nrngui ../bah/init.hoc
 . . . omitting NEURON's "banner" . . .
read init.hoc
read test.hoc
found it
Granted, this is under Linux and from the command line, but I don't see why it wouldn't work under another OS.
sl
Posts: 21
Joined: Wed Nov 28, 2007 12:52 am
Location: Cornell University

Re: Detecting the location of the current file

Post by sl »

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!
ted
Site Admin
Posts: 6305
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Detecting the location of the current file

Post by ted »

Care to share the solution, or point to it?
sl
Posts: 21
Joined: Wed Nov 28, 2007 12:52 am
Location: Cornell University

Re: Detecting the location of the current file

Post by sl »

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.
Post Reply