Interfacing NEURON with other software on runtime

NMODL and the Channel Builder.
Post Reply
Ricardo_Molinari
Posts: 4
Joined: Tue Jan 28, 2020 7:36 am

Interfacing NEURON with other software on runtime

Post by Ricardo_Molinari »

Dear all,
I'm trying to implement a neuromuscular system that has some afferent structures that depend on other softwares besides NEURON. On this effort, I found in the forum the following .mod, which implements a python callback function.

Code: Select all

NEURON {
  POINT_PROCESS PyBeforeStep
  POINTER callback
}

ASSIGNED {callback}

VERBATIM
extern int (*nrnpy_hoccommand_exec)(Object*);
ENDVERBATIM

BEFORE STEP {
VERBATIM
  if (nrnpy_hoccommand_exec && _p_callback) {
    Object** po = (Object**)(&(_p_callback));
    (*nrnpy_hoccommand_exec)(*po);
  }
ENDVERBATIM
}

PROCEDURE set_callback() {
VERBATIM
  Object** po = (Object**)(&(_p_callback));
  Object* old = *po;
  *po = (Object*)0;
  if (ifarg(1)) {
    *po = *hoc_objgetarg(1);
    hoc_obj_ref(*po);
  }
  if (old) { hoc_obj_unref(old); }
ENDVERBATIM
}
In this sense, the afferent mechanism would be modelled as an artificial cell, with the spike interval being controlled by an outside NEURON process on runtime. An example implementation follows:

Code: Select all

from neuron import h
h.load_file('stdrun.hoc')
import pylab as plt

def foo(ns): #to be called at every time step
	if h.t % 50 == 0.0: 
		ns.interval = ns.interval + 10

soma = h.Section(name = 'soma')
soma.insert('hh')
syn = h.ExpSyn(soma(0.5))
syn.tau = 20
syn.e = 0

ns = h.NetStim()
ns.interval = 20 #[ms]
ns.start = 0
ns.noise = 0
ns.number = 1e9

nc = h.NetCon(ns,syn)
nc.delay = 0
nc.weight[0] = 0.1

vx = h.Vector().record(soma(0.5)._ref_v) #Record soma voltage
t = h.Vector().record(h._ref_t)

a = h.PyBeforeStep(soma(0.5))
a.set_callback(foo(ns))

h.tstop= 500
h.run()

plt.figure()
plt.plot(t,vx)
plt.show()
The problem is when the ns argument in passed to foo(), NEURON access foo() just on the first interaction and 'ignores' it on the following steps.
I'm not sure if this is the best implementation strategy and if it would work as expected.
Do you have any sugestions to solve this muscle afferent feedback problem? What could I do have runtime control over artificial cell's spike times from other software?

Thanks in advance.
ramcdougal
Posts: 214
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Interfacing NEURON with other software on runtime

Post by ramcdougal »

Your specific code doesn't work because you are evaluating the function foo(ns) at the time of your callback (it returns None) and that's what's being set as your callback. (By the order of operations, we evaluate foo(ns) before calling set_callback.) You could fix this by passing in a lambda that evaluates foo(ns); keep the reference around to be safe; e.g.

Code: Select all

my_lambda = lambda: foo(ns)
a.set_callback(my_lambda)
But... there's no need for the mod file and its complexities.

NEURON provides built-in support for Python callbacks at initialization (FInitializeHandler), at specific times during simulations (CVode.event), and at every time step (CVode.extra_scatter_gather).

Here's a simple example using each:

Code: Select all

from neuron import h
from neuron.units import mV, ms
h.load_file('stdrun.hoc')

def called_once():
    print("called_once, t={:g}".format(h.t))

def called_often():
    print("called_often, t={:g}".format(h.t))

def called_on_event():
    print('called_on_event, t={:g}'.format(h.t))

fih = h.FInitializeHandler(called_once)
esg = h.CVode().extra_scatter_gather(0, called_often)

h.finitialize(-65 * mV)
# note: events are cleared at initialization
event = h.CVode().event(0.1 * ms, called_on_event)
h.continuerun(0.2 * ms)
And the output:

Code: Select all

called_once, t=0
called_often, t=0
called_often, t=0.025
called_often, t=0.05
called_often, t=0.075
called_on_event, t=0.1
called_often, t=0.1
called_often, t=0.125
called_often, t=0.15
called_often, t=0.175
There are options (see the documentation) to control when exactly the callbacks occur relative to the initialization sequences and fadvances.
Ricardo_Molinari
Posts: 4
Joined: Tue Jan 28, 2020 7:36 am

Re: Interfacing NEURON with other software on runtime

Post by Ricardo_Molinari »

Dear Ted,
the solution worked very well! Thanks for the fast response.
[]'s
ted
Site Admin
Posts: 5797
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Interfacing NEURON with other software on runtime

Post by ted »

It would have been my pleasure to have written the post that got your thanks, but it was Robert McDougal who wrote that clear and very effective reply to your question. Thank you, Robert! That should go in the FAQ list, or perhaps better, in the Forum's own Hot tips area.
Ricardo_Molinari
Posts: 4
Joined: Tue Jan 28, 2020 7:36 am

Re: Interfacing NEURON with other software on runtime

Post by Ricardo_Molinari »

Sorry about the confusion. Thank you Robert for the help!
[]'s
Post Reply