Is it possible to run nrnivmodl from python?

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Post Reply
RobinDS

Is it possible to run nrnivmodl from python?

Post by RobinDS »

I'm making a package manager for NEURON called Glia, have a look here https://pypi.org/project/nrn-glia/.

I'd like a platform independent way of compiling a set of folders, similar to how `nrnivmodl folder1 folder2 folder3` would work. Can I somehow import this from the neuron package and run in the Python environment or should I fiddle with shell, subprocesses and stdin/stdout pipes?

Some questions in case I'd have to use subprocesses and rely on the operating system:
* On Windows is there a way to compile mod files from outside the bash shell, from the standard cmd? Using the standard windows installer, will there be any PATH or env vars that point me to the neuron installation so that I could locate the files I need to run to execute the compilation?
* On Linux it seems more straightforward: after installation `nrnivmodl` seems to be immediatly accessible to call

Any other pitfalls I would have to look out for for platform-independent automatic compilation of mod files?

I have managed so far to: create a packager that creates ready to upload packages (astrocyte) of mod files and a package manager that when imported checks whether the collection of installed packages changed and whether we'd have to recompile a new .dll/.so. The part that is missing is actually compiling that dll/so from Python

It would be the last piece of the puzzle to have a platform independent easy-to-use package-based name-collision avoiding manager of NEURON assets.
RobinDS

Re: Is it possible to run nrnivmodl from python?

Post by RobinDS »

These have been my shell attempts just to get a proof of concept going on my computer (hence the hardcoded directories)

Using nrniv.exe

Code: Select all

process = subprocess.Popen(["C:\\nrn\\bin\\nrniv.exe", "-nopython", "c:/nrn/lib/hoc/mknrndll.hoc", neuron_mod_path],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Doesn't work because the GUI opens and I can't find a way to pass directories to the hoc script. Don't even know if the hoc script is programmed to accept command line argument directories

Starting bash and executing mknrndll

Code: Select all

        process = subprocess.Popen(["C:\\nrn\\mingw\\usr\\bin\\bash.exe", "--rcfile", "c:/nrn/lib/bshstart.sh", "-i", "-h"],
            stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Doesn't work, can't start the shell in interactive mode, throws this error:

Code: Select all

STDERR:
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
/cygdrive/c/nrn/bin/nrnpyenv.sh: line 141: /cygdrive/c/Users/robin/AppData/Local/Microsoft/WindowsApps/python3: Permission denied
bash-4.4$ exit
RobinDS

Re: Is it possible to run nrnivmodl from python?

Post by RobinDS »

Sorry for the stream of consciousness. If anyone is interested in having Python compile their mod files. Here's how I did it for windows:

Code: Select all

        nrn_path = os.getenv('NEURONHOME')
        os.chdir(neuron_mod_path)
        cyg_path = nrn_path.replace(":\\","\\").replace("\\","/")
        process = subprocess.Popen([
            os.path.join(nrn_path, "mingw/usr/bin/sh"),
            os.path.join(nrn_path, "lib/mknrndll.sh"),
            os.path.join("/cygdrive/", cyg_path)],
            stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        stdout, stderr = process.communicate()
 
I took a peek in mknrndll.hoc to find out which command was being called after selecting a directory.
ramcdougal
Posts: 267
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Is it possible to run nrnivmodl from python?

Post by ramcdougal »

As long as you're using NEURON 7.7+ installed by the regular installer, just use nrnivmodl on every platform.

The Linux (at least with the deb, didn't check rpm), Windows, and macOS installers all default to putting nrnivmodl on the path.

In Windows, in particular, one can open a regular cmd or Power Shell terminal and just type "nrnivmodl".

With correct paths, compiling mod files in the working directory in Python is then as simple as:

Code: Select all

import os
os.system('nrnivmodl')
RobinDS

Re: Is it possible to run nrnivmodl from python?

Post by RobinDS »

Any specific reasons why nrnivmodl would not work after running the standard Windows installer?
Post Reply