Page 1 of 1

Best way(s) to advance() in Python

Posted: Fri May 25, 2018 11:39 am
by tommct
I am building a network with ARTIFICIAL CELLs, but I also want to efficiently capture and plot underlying "membrane" or other variables of some arbitrary subset of cells, both in real-time as well as through recording into vectors to analyze post-simulation.

I have the following code that plots the "M" function in IntFire1 as an example that I want to ensure is following best practices and to clarify:

Code: Select all

from neuron import h
import numpy as np
import matplotlib.pyplot as plt

h.load_file("stdrun.hoc")

h('proc advance() {nrnpython("myadvance()")}')

t_idx = 0
def myadvance():
    global t_idx, t_vec, m_vec
    t_vec[t_idx] = h.t
    m_vec[t_idx] = cell.M()
    t_idx += 1
    h.fadvance()

cell = h.IntFire1()
ns = h.NetStim(cell)
ns.number = 2
ns.start = 15
ns.interval = 10
nc = h.NetCon(ns, cell)
nc.weight[0] = .5
nc.delay = 0

h.init()
h.tstop = 40
t_vec = np.zeros(int(np.ceil(h.tstop/h.dt)))
m_vec = np.zeros_like(t_vec)
h.run()

plt.figure(figsize=(8,4))
plt.plot(t_vec, m_vec)
Here are the questions:
  • Is this the best way?
    Is there a way to pass variables to myadvance() without them being global?
    I am defining t_idx, which is really the simulation step counter. Is there such a counter in Hoc?
Thanks!

Re: Best way(s) to advance() in Python

Posted: Mon May 28, 2018 4:20 pm
by ted
If I had a single IntFire1 I'd try executing something like this after model setup

Code: Select all

h(' \
  proc advance() { \
    advance() \
    m1 = IntFire1[0].M() \
  } \
')
and then use Vector recording to capture the time course of h.m1.

Re: Best way(s) to advance() in Python

Posted: Mon May 28, 2018 6:47 pm
by ramcdougal
Minor comments: the inside of proc advance should invoke fadvance not advance. Also note you can get slightly more readable code by using a multiline string:

Code: Select all

h(r"""
proc advance() {
    fadvance()
    m1 = IntFire1[0].M()
}""")
The r in front of the multi-line string opening is not needed here, but allows you to encode things like \n for the HOC command.

Re: Best way(s) to advance() in Python

Posted: Mon May 28, 2018 9:22 pm
by ted
Actually the use of fadvance is key, not just minor. But then I only promised "something like," not a real, working solution.

(Can one call "weasel words" on oneself?)