Best way(s) to advance() in Python

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

Moderator: hines

Post Reply
tommct
Posts: 1
Joined: Mon May 21, 2018 12:17 am

Best way(s) to advance() in Python

Post by tommct » Fri May 25, 2018 11:39 am

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!

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

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

Post by ted » Mon May 28, 2018 4:20 pm

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.

ramcdougal
Posts: 141
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Medicine

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

Post by ramcdougal » Mon May 28, 2018 6:47 pm

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.

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

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

Post by ted » Mon May 28, 2018 9:22 pm

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?)

Post Reply