Mechanisms in nrnmech.dll do not seem to be loading in python script

NMODL and the Channel Builder.
Post Reply
Liz_Stan_88
Posts: 10
Joined: Thu Jul 04, 2019 4:58 am

Mechanisms in nrnmech.dll do not seem to be loading in python script

Post by Liz_Stan_88 »

Good Day Mr. Carnevale and/ Mr. Hines,

I have recently for some unknown reason run into an error when trying to run my python script with NEURON. It seems as though python is not loading my nrnmech.dll file all of a sudden as I am receiving the below error:

Code: Select all

    soma.insert('klt')
ValueError: argument not a density mechanism name.
I have been trying to debug this error for a good day now, by attempting all the suggestions from the forum by people that have previously had the same or similar issue of loading nrnmech.dll errors, but nothing has seemed to work so far. What baffles me the most is it was working perfectly previously. Let me step through what I have attempted:
  • Making sure the nrnmech.dll file is created properly, I basically recreated nrnmech.dll and checked if I got any new errors in the terminal. Terminal said it was successfully created no errors.
  • Checking if the nrnmech.dll is loaded in nrniv by running a .hoc file within the same directory, as you can see below this was a success.

    Code: Select all

    NEURON -- VERSION 7.8.2 release/7.8 (09b151ec) 2020-12-16
    Duke, Yale, and the BlueBrain Project -- Copyright 1984-2019
    See http://neuron.yale.edu/neuron/credits
    
    loading membrane mechanisms from C:\Users\lizzy\Desktop\OCResearch\Neuroncode\Validation\yr2021\ExperimentalCheckFiles\ModFiles\nrnmech.dll
    Additional mechanisms from files
     hcnobo.mod ipulse1.mod ipulse2.mod ipulse3.mod jsrnaf.mod kht.mod klt.mod leak.mod vecevent.mod
  • Uninstalling and reinstalling NEURON. I was previously using version 7.6.7, so I uninstalled it using the nrn uninstall.exe and then installed version 7.8.2. After which I reran the above two steps.
  • I then made a simple model .py file just to debug the insert(mech) error and ran it and still same error for all the mechanisms I would like to insert that were compiled in nrnmech.dll. The code of the simple model in python is given below:

    Code: Select all

    """This code is used to experiment with the NetCon Weight for the synaptic inputs."""
    
    """Python Libraries:"""
    from neuron import h, gui
    
    """Load the NEURON MECHS:"""
    h.nrn_load_dll("C:/Users/lizzy/Desktop/OCResearch/Neuroncode/Validation/yr2021/ExperimentalCheckFiles/ModFiles")
    
    """Single Dendrite Octopus Cell:"""
    #Create OCell Sections:
    soma = h.Section(name = 'soma')
    dend = h.Section(name = 'dend')
    axon = h.Section(name = 'axon')
    
    #Define Topology:
    dend.connect(soma(0), 0)
    axon.connect(soma(1))
    
    #Define Geometry of Sections:
    """Dendrite"""
    dend.diam = 3   #Diameter [um]
    dend.L = 250    #Length [um]
    dend.nseg = 21  #Number of Segments
    """Soma"""
    soma.diam = 25  #Diameter [um]
    soma.L = 25     #Length [um]
    soma.nseg = 1   #Number of Segments
    """Axon"""
    axon.diam = 3   #Diameter [um]
    axon.L = 32     #Length [um]
    axon.nseg = 1   #Number of Segments
    h.define_shape()
    
    #Define the Biophysics of each Section:
    for sec in h.allsec():
        sec.Ra = 100            #Axial Resistance [ohm*cm]
        sec.cm = 0.9            #Membrane Capacitance [uF/cm^2]
    for sec in h.allsec():
        if h.ismembrane("k_ion", sec=sec):
            sec.ek  = -70       #Initial voltage membrane value for potassium ion channel
        if h.ismembrane("na_ion", sec=sec):
            sec.ena = 55 # mV (SPENCER ET AL)               #Ena = 50 mV (ROTHMAN & MANIS)
            
     """Soma"""
     #Insert Soma biophysics
    soma.insert('klt')
    soma.insert('kht')
    soma.insert('jsrna')
    soma.insert('hcnobo')
    soma.insert('leak')
    # soma.insert('pas')
    soma.insert('extracellular')
    for seg in soma:
        seg.klt.gbar = 0.04070
        seg.kht.gbar = 0.00                               # Low Threshold Potassium conductance in S/cm2
        seg.jsrna.gbar = 0.0                                   # Sodium conductance in S/cm2
        seg.hcnobo.eh = -38                                 # Hyperpolarizaton potential in mV
        seg.hcnobo.gbar = 0.00                            # Hyperpolarizaton conductance in S/cm2
        seg.leak.gbar = 0.00                              # Leak conductance in S/cm2
        seg.leak.erev = -62     
        # seg.pas.e = -62
        # seg.pas.g = 0.001
                   
    """Dendrite"""
    #Insert Dendrite biophysics
    dend.insert('klt')
    dend.insert('kht')
    dend.insert('jsrna')
    dend.insert('hcnobo')
    dend.insert('leak')
    # dend.insert('pas')
    dend.insert('extracellular')
    for seg in dend:
        seg.klt.gbar = 0.0027   
        seg.kht.gbar = 0.0000                               # Low Threshold Potassium conductance in S/cm2
        seg.jsrna.gbar = 0.0                                   # Sodium conductance in S/cm2
        seg.hcnobo.eh = -38                                 # Hyperpolarizaton potential in mV
        seg.hcnobo.gbar = 0#0.0006                            # Hyperpolarizaton conductance in S/cm2
        seg.leak.gbar = 0#0.0020                              # Leak conductance in S/cm2
        seg.leak.erev = -62 
        # seg.pas.g = 0.001
        # seg.pas.e = -62
    
    """Axon"""
    #Insert Axon biophysics
    axon.insert('pas')
    axon.insert('extracellular')
    for seg in axon:
        seg.pas.g = 0.001
        seg.pas.e = -62
    
    h.topology()
    ps = h.PlotShape()                                                                 # False tells h.PlotShape not to use NEURON's gui. Shows/draws the generated Octopus Cell.
    ps.show(0) 
I am currently using python version 3.7.0 and coding in visual studio code as my IDE and using a 64 bit windows 10 operating system.
Any guidance in solving this particular issue will be greatly appreciated. In the meantime, I am going to see if there is not a windows 10 update that I need to do or undo.

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

Re: Mechanisms in nrnmech.dll do not seem to be loading in python script

Post by ted »

You have already done a lot of important tests. Here's one more test I'd like you to do.

Create a new empty directory.
Put a copy of the NMODL file that defines klt (looks like that would be klt.mod) in that directory.
Run mknrndll in that directory and check the printed messages to make sure there is no error message.
Open a "Command" terminal and cd to the directory that contains klt.mod and the new nrnmech.dll file.
In this new directory execute the command
python

After python's >>> prompt appears, type the following commands into the terminal, one at a time, and tell me what messages appear:

from neuron import h

soma = h.Section(name='soma')

soma.insert('klt')

soma.psection()
Liz_Stan_88
Posts: 10
Joined: Thu Jul 04, 2019 4:58 am

Re: Mechanisms in nrnmech.dll do not seem to be loading in python script

Post by Liz_Stan_88 »

Thank you for getting back to me so promptly Ted. I really appreciate it.

I have done what you have suggested and this is what I have received:
  • Putting the NMODL file in its own directory and compiling it with mknrndll, the terminal outputs as follows:

    Code: Select all

    NEURON -- VERSION 7.8.2 release/7.8 (09b151ec) 2020-12-16
    Duke, Yale, and the BlueBrain Project -- Copyright 1984-2019
    See http://neuron.yale.edu/neuron/credits
    
            0
            0
            0
            0
            1
    c:\nrn/mingw/usr/bin/sh c:\nrn/lib/mknrndll.sh /cygdrive/c\nrn
    x86_64-w64-mingw32-gcc.exe -DDLL_EXPORT -DPIC -I/cygdrive/c\nrn/include -I/cygdrive/c\nrn/src/scopmath -I/cygdrive/c\nrn/src/nrnoc -I/cygdrive/c\nrn/src/oc  -c mod_func.c
    nocmodl klt
    Translating klt.mod into klt.c
    Thread Safe
    x86_64-w64-mingw32-gcc.exe -DDLL_EXPORT -DPIC -I/cygdrive/c\nrn/include -I/cygdrive/c\nrn/src/scopmath -I/cygdrive/c\nrn/src/nrnoc -I/cygdrive/c\nrn/src/oc  -c klt.c
    x86_64-w64-mingw32-gcc.exe  -shared mod_func.o klt.o \
      -L/cygdrive/c\nrn/bin -lnrniv -lpthread -o 145.nrnmech.dll
    #rebase -b 0x64000000 -v nrnmech.dll
    
    nrnmech.dll was built successfully.
    Press Return key to exit
    I assume that is a successful compile of the .mod file.
  • Opening up "Command Prompt" and cd to the directory where klt.mod is and running the commands suggested the terminal outputs as follows:

    Code: Select all

    C:\Users\lizzy>cd Desktop
    
    C:\Users\lizzy\Desktop>cd test_mod
    
    C:\Users\lizzy\Desktop\test_mod>python
    Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from neuron import h
    >>> soma = h.Section(name='soma')
    >>> soma.insert('klt')
    soma
    >>> soma.psection()
    {'point_processes': {}, 'density_mechs': {'klt': {'gbar': [0.01592], 'q10g': [2.0], 'ik': [0.0], 'gklt': [0.0], 'w': [0.0], 'z': [0.0]}}, 'ions': {'k': {'ek': [-77.0], 'ki': [54.4], 'ko': [2.5], 'ik': [0.0], 'dik_dv_': [0.0]}}, 'morphology': {'L': 100.0, 'diam': [500.0], 'pts3d': [], 'parent': None, 'trueparent': None}, 'nseg': 1, 'Ra': 35.4, 'cm': [1.0], 'regions': set(), 'species': set(), 'name': 'soma', 'hoc_internal_name': '__nrnsec_000001f8ee2a04f0', 'cell': None}
    >>>
    I assume that is a successful insertion of the klt.mod mechanism.
This is rather peculiar, that command prompt does not give me an error, but in visual studio code it does.

Currently, I am downloading the new windows 10 version 20H2 update and then awaiting the install.
ted
Site Admin
Posts: 6286
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Mechanisms in nrnmech.dll do not seem to be loading in python script

Post by ted »

Thank you for your prompt reply. Also, and I should have written this sooner, thank you for using NEURON in your work.

Previously you demonstrated that NEURON running by itself can discover and load a nrnmech.dll file from the current working directory ("Checking if the nrnmech.dll is loaded in nrniv by running a .hoc file . . . ").
And now, in addition to (once again) establishing that mknrndll is compiling klt.mod and generating a corresponding nrnmech.dll file without problems. you have also shown that, when NEURON is used as a module under Python, it can find a nrnmech.dll file in the current working directory, load that file, and then use the compiled mechanism(s) in that file.

All very good. So where's the problem? Probably in the h.nrn_load_dll() statement. According to the Programmer's Reference https://neuronsimulator.github.io/nrn/h ... n_load_dll
nrn_load_dll(dll_file_name)
. . .
Loads a dll containing membrane mechanisms.
where dll_file_name should include not just the path to the directory that contains the nrnmech.dll you want to load, but also the name of the file itself.

You might be able to fix this by appending nrnmech.dll to the very long argument that is already in your code, but the result would be code that only works in a highly specific environment: a PC that actually has such a file in such a path. Also, I can't think of the last time I saw anyone actually use nrn_load_dll in their source code. Out of the almost 700 NEURON models whose code is hosted in ModelDB (as distinct from models whose code is on other servers), only 18 use nrn_load_dll, and several of those 18 fall into the category that I would call "source code with egregiously elaborate directory structure schemes" (i.e. making a vice out of a virtue). There are many other opportunities to test (and demonstrate) one's ability to orgnize code in ways that are rewarded by real improvements in function. Just my opinion.

It is usually best to put the mod files that a model needs into the same directory as the model's main NEURON or Python file, then run mknrndll in that directory. Then you can eliminate the nrn_load_dll statement entirely. Just cd to the directory that contains the nrnmech.dll file, and NEURON will find and load the dll automatically, regardless of whether you are executing NEURON by itself or instead launching Python and using neuron as a module.
Liz_Stan_88
Posts: 10
Joined: Thu Jul 04, 2019 4:58 am

Re: Mechanisms in nrnmech.dll do not seem to be loading in python script

Post by Liz_Stan_88 »

Thank you once again Ted. And I love NEURON, its been fun working with it and using it in my research.
All very good. So where's the problem? Probably in the h.nrn_load_dll() statement. According to the Programmer's Reference https://neuronsimulator.github.io/nrn/h ... n_load_dll, where dll_file_name should include not just the path to the directory that contains the nrnmech.dll you want to load, but also the name of the file itself.
I would just like to thank you this did end up fixing my problem. Such a simple fix, which was a relief, but it also totally made me feel a little incompetent at the same time. It still is just strange to me that I had previously been using it without the actual filename itself in the directory path and it was working just fine for over a year, except for two days ago when the error began, but I am very relieved its sorted now.
It is usually best to put the mod files that a model needs into the same directory as the model's main NEURON or Python file, then run mknrndll in that directory. Then you can eliminate the nrn_load_dll statement entirely. Just cd to the directory that contains the nrnmech.dll file, and NEURON will find and load the dll automatically, regardless of whether you are executing NEURON by itself or instead launching Python and using neuron as a module.
As for the suggestion you made, I will be doing that from now on, especially if my work is to be published as this will just make it user friendly to anyone who decides to use it, so thank you again.
ted
Site Admin
Posts: 6286
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Mechanisms in nrnmech.dll do not seem to be loading in python script

Post by ted »

As for the suggestion you made, I will be doing that from now on, especially if my work is to be published as this will just make it user friendly to anyone who decides to use it
Excellent. Future potential users will appreciate that and be more likely to cite your work.

BTW, we're always on the lookout for computational neuroscience models that can be added to ModelDB modeldb.yale.edu. Doesn't matter how they were implemented, as long as the source code is available. ModelDB also accepts models whose code is already online, either at GitHub or some other resource (Zenodo etc.); the value added by having such models in ModelDB is that ModeDB is a domain-specific online database whose contents are searchable in ways that are not provided by other resources, e.g. keywords drawn from domain-specific ontologies and vocabularies.
Post Reply