Changing hoc variables after xopen but before instantiation

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

Moderator: hines

Post Reply
dopadelic

Changing hoc variables after xopen but before instantiation

Post by dopadelic » Thu Aug 07, 2014 6:13 pm

I'm using python to import hoc commands from a hoc file
h('xopen("CA1PyrCmHSpikeErrorAHPDepth.hoc")')

However, before instantiating a cell from the template within the hoc file, I want to change some of the variables inside that hoc file.

Here's the code I'm using to instantiate a new cell from the template.
h('cell = new celltemplate(econ)')

I tried h('variableval=10'), and this doesn't seem to access the variable inside the template. So how do I do so?
Thanks for this wonderful community!

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

Re: Changing hoc variables after xopen but before instantiat

Post by ramcdougal » Thu Aug 07, 2014 10:23 pm

One solution is to declare a top-level variable that is used for initialization and set that.

For example, consider example.hoc's use of initvalue:

Code: Select all

// default value for creating new Example objects
initvalue = 17

begintemplate Example
    public value, print_value
    external initvalue
    proc print_value() {
        printf("value = %g\n", value)
    }

    proc init() {
        value = initvalue
    }
endtemplate Example
Here's a Python routine that creates two Example instances, both of which have independent "value" variables, but both of which are initially set to a default value specified in the Python:

Code: Select all

from neuron import h, gui

# load the template
h.load_file('example.hoc')

# change the default value (was 17)
h.initvalue = 32

obj = h.Example()

# show the default value (32)
obj.print_value()

# change the value and re-show
obj.value = 5.15
obj.print_value()

#
# or to give the object a direct HOC name
#
h('objref cell')

h.cell = h.Example()
# prints the (Python-specified as above) default value of 32
h.cell.print_value()
As an aside, note that you don't need to write a string to access HOC functions and variables; they're accessible directly as members of the h module.

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

Re: Changing hoc variables after xopen but before instantiat

Post by ted » Fri Aug 08, 2014 2:49 pm

Parameters can also be passed at the time of instance creation.

Code: Select all

begintemplate Example
    public print_value
    proc print_value() {
        printf("value = %g\n", value)
    }
    proc init() {
        value = $1
    }
endtemplate Example

objref elist
elist = new List()
for i=0,3 elist.append(new Example(i*i))

for i=0,elist.count()-1 {
  print "item ", i
  elist.o(i).print_value
}
I believe you can also write templates using numarg so that 0 or more parameters can be passed.

dopadelic

Re: Changing hoc variables after xopen but before instantiat

Post by dopadelic » Sat Aug 09, 2014 8:20 pm

Thank you ramcdougal, it works perfectly!

ted: good to see alternative ways of doing it!

Post Reply