Can't Load .mod files in Python Script

Post Reply
Liz_Stan_88
Posts: 10
Joined: Thu Jul 04, 2019 4:58 am

Can't Load .mod files in Python Script

Post by Liz_Stan_88 »

Good Day Neuron Community,

I am having a slight issue with accessing my mod files in my python script.
I am new to using Neuron and Python on a Mac (M1), and I am struggling in setting up the new method of h.nrn_load_dll for mac. I assume I have done everything correctly namely these steps below:
1. Dragging and dropping the folder containing all the .mod files onto mknrndll. This creates a folder named arm64, containing the .o and .c files for the various .mod files as well as a libnrnmech.dylib, special, makemod2c_inc, mod_func.cpp, mod_func.o, special.dSYM files. The response of the mknrndll terminal is successful as well, it contains no errors.

2. When clicking on the special file it opens the terminal stating:

Code: Select all

lizzy@Lizzys-MacBook-Air ~ % /Users/lizzy/Downloads/2022\ M/Neuron\ Mod\ Files/arm64/special ; exit;
NEURON -- VERSION 8.2.0 HEAD (156b9dee3) 2022-07-01
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2021
See http://neuron.yale.edu/neuron/credits

Additional mechanisms from files
 "hcnobo.mod" "ipulse1.mod" "ipulse2.mod" "ipulse3.mod" "jsrnaf.mod" "kht.mod" "klt.mod" "leak.mod" "vecevent.mod"
Thus my assumption is that the mechanisms are accessible in neuron.

3. In my very basic python script (for debugging purposes) where I want to use these mechanisms I do as follows:

Code: Select all

from neuron import h,gui 
h.nrn_load_dll("/Users/lizzy/Downloads/2022 M/Neuron Mod Files/arm64")
soma = h.Section(name='soma')
soma.insert('klt')
The returning error when running this python script is:

Code: Select all

 File "/Users/lizzy/Downloads/2022 M/test.py", line 4, in <module>
    soma.insert('klt')
ValueError: argument not a density mechanism name.
I am very aware I am most probably loading the mechanisms incorrectly into the python script, but I cannot find a solution on an alternative of how to do this, thus, any guidance or assistance will be greatly appreciated.

Kind regards
Elizabeth
Last edited by Liz_Stan_88 on Wed Jul 20, 2022 2:37 pm, edited 1 time in total.
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Can't Load .mod files in Python Script

Post by ted »

First, for the benefit of future readers of this thread--in the second code example in the previous message, "som.insert('klt')" is just an accidental misspelling. In the actual code that Liz_Stan_88 executed, the statement must have been "soma.insert('klt')"; if not, there would have been an error message roughly equivalent to "som? what's som?"

Now for the problem that prompted your question. I suspect one of two possible causes.

1. Nowhere is it written that "thou shalt give each mod file a name that matches the mechanism that it specifies." klt.mod might define a density mechanism called KLT or KlT or tunasandwich. So examine the contents of klt.mod, and if it contains a NEURON block that declares

SUFFIX tunasandwich

just fix the insert statement's argument.

2. Or maybe the code in klt.mod defines a point process, not a density mechanism. In this case you would create an instance of the point process class and attach it to a particular location on a section like so:

foo = h.classname(seg)

where foo is the name you want to call it
classname is the name of the point process class (e.g. IClamp, SEClamp, ExpSyn, etc.)
and seg refers to the particular section to which you want to attach the point process
(seg is often a name of the form secname(range)
where
secname is the Python name of a section
and range is a numerical value in the range 0..1 that specifies the normalized distance from the 0 end of the section to the location where you want to attach the point process)

And, of course, you need to use the class name that is specified in klt.mod's NEURON block. If you see

POINT_PROCESS Blarg

then you know that it's a point process and you know its class name.
Liz_Stan_88
Posts: 10
Joined: Thu Jul 04, 2019 4:58 am

Re: Can't Load .mod files in Python Script

Post by Liz_Stan_88 »

Oh goodness, thank you Ted for pointing out my typo. I will edit the post and fix it.

Working off your suggestions I will post the code of the klt.mod below, just to ensure that I am not being silly in my understanding, but also to give you insight into the .mod file to help in solving this issue.

1. I opened and reviewed the contents of the klt.mod file to review the NEURON block setup, as you suggested. It seems the SUFFIX line states SUFFIX klt, which corresponds to the same lowercase letter name that I call/refer to in the very simple python script I mentioned in the original post.

Code: Select all

NEURON {
    THREADSAFE
    SUFFIX klt
    USEION k READ ek WRITE ik
    RANGE gbar, gklt, ik, q10g
    GLOBAL winf, zinf, wtau, ztau
}
2. As far as I am aware and my understanding of differences between a point process vs density mechanism, the klt.mod file I am using is a density mechanism. The full code defined/setup of the klt.mod file (Original implementation by Paul B. Manis, April (JHU) and Sept, (UNC)1999.) is found below.

Code: Select all

UNITS {
    (mA) = (milliamp)
    (mV) = (millivolt)
    (nA) = (nanoamp)
}
NEURON {
    THREADSAFE
    SUFFIX klt
    USEION k READ ek WRITE ik
    RANGE gbar, gklt, ik, q10g
    GLOBAL winf, zinf, wtau, ztau
}
INDEPENDENT {t FROM 0 TO 1 WITH 1 (ms)}
PARAMETER {
    v (mV)
    dt (ms)
    gbar = 0.01592 (mho/cm2) <0,1e9>
    zss = 0.5   <0,1>   : steady state inactivation of glt
    q10tau = 3.0
    q10g = 2.0
}
STATE {
    w z
}
ASSIGNED {
    celsius (degC)  : model is defined on measurements made at room temp in Baltimore
    ik (mA/cm2) 
    ek (mV)
    gklt (mho/cm2)
    winf zinf
    wtau (ms) ztau (ms)
    qg ()  : computed q10 for gnabar based on q10g
    q10 ()
}
LOCAL wexp, zexp
BREAKPOINT {
    SOLVE states METHOD cnexp
    
    gklt = qg*gbar*(w^4)*z
    ik = gklt*(v - ek)
}
INITIAL {
    qg = q10g^((celsius-22)/10 (degC))
    q10 = q10tau^((celsius - 22)/10 (degC)) : if you don't like room temp, it can be changed!
    rates(v)
    w = winf
    z = zinf
}

DERIVATIVE states {  :Computes state variables m, h, and n
    rates(v)      :         at the current v and dt.
    w' = (winf - w)/wtau
    z' = (zinf - z)/ztau
}


PROCEDURE rates(v (mV)) {  :Computes rate and other constants at current v.
              :Call once from HOC to initialize inf at resting v.

    winf = (1 / (1 + exp(-(v + 48) / 6 (mV))))^0.25
    zinf = zss + ((1-zss) / (1 + exp((v + 71) / 10 (mV))))

    wtau =  (100 (ms)/ (6*exp((v+60) / 6 (mV)) + 16*exp(-(v+60) / 45 (mV)))) + 1.5
    wtau = wtau/q10
    ztau =  (1000 (ms)/ (exp((v+60) / 20 (mV)) + exp(-(v+60) / 8 (mV)))) + 50
    ztau = ztau/q10
}
Thus I am still stumped. I know on my windows operating system I had to direct my path for h.nrn_load_dll to the directory path/nrnmech.dll file exactly for me to be able to use the mechanisms in my python script. This is obviously different for MacOS as a nrnmech.dll is not created when you compile the .mod files with mknrndll, which is why I think I am incorrectly loading them in the MacOS space. Once again, any help will be greatly appreciated.
Liz_Stan_88
Posts: 10
Joined: Thu Jul 04, 2019 4:58 am

Re: Can't Load .mod files in Python Script

Post by Liz_Stan_88 »

I just wanted to post the solution to the problem, I was correct I was not referring to the file special directly. Thus my solution was simple and seen below:

Code: Select all

h.nrn_load_dll("directory_path/arm64/special")
Where directory_path refers explicitly to the directory path in which the folder created by mknrndll is found.

Thank you for your assistance Ted.
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Can't Load .mod files in Python Script

Post by ted »

That works for now, on a particular hardware platform with a particular directory structure. But it plants a problem that will crop up as soon as you share your code with someone who uses a machine with an Intel CPU, or a GPU. And it may become more of a problem in the future, if you make your code publically available. It's generally best to put all mod files in the same directory as the hoc, py, and ses files that are needed for a given project. Then NEURON will automatically discover the binary file that contains the compiled mechanisms.
Post Reply