forsec doesn't seem to work in templates

Anything that doesn't fit elsewhere.

forsec doesn't seem to work in templates

Postby TWA » Sun Jan 11, 2009 3:03 am

Can anyone explain why this doesn't work? I can't use forsec to change the section access in a template. Right now I think I found a work around by using SectionRef, but it seems really weird that this didn't work. This is a completely stripped down example, most lines are comments to explain what is happening. Here the template should be able to return the number of compartments in the section which it stores the name of.

Code: Select all
create soma, dend

begintemplate SectionInfo
   external soma, dend
   public name, access_nseg
   objref sref
   strdef name

   proc init() {
      name = $s1
      print "In init, section name is: ",name
   }

   proc access_nseg() {local n
      print "In access_nseg, section name is: ",name
      print "In access_nseg, currently accessed section is ",secname(),", with nseg = ",nseg
      print "   which is why we need to change the currently accessed section."
      print "   Trying to use forsec to print the right name and nseg for section ", name, "..."
      forsec name print "In access, using forsec, section ", secname(), " has nseg =  ",nseg
      print "   .... didn't work. Change access manually to show that external worked..."
      dend print "In access_nseg, with access changed manually, section ", secname(), " has nseg =  ",nseg
   }

endtemplate SectionInfo

access soma
nseg=3
dend.nseg=11

objref SD
dend SD = new SectionInfo(secname())
SD.access_nseg()

// Sanity check the offending line from the template "access" function
strdef name
name = "dend"
proc try_offending_line_in_base() {
   forsec name print "Using forsec, section ", secname(), " has nseg =  ",nseg
}
//try_offending_line_in_base()


Thank you for your attention!
TWA
 
Posts: 8
Joined: Mon Apr 24, 2006 10:33 am
Location: UCONN Health Center

Re: forsec doesn't seem to work in templates

Postby csh » Sun Jan 11, 2009 12:01 pm

Hi,
from the online documentation of forall (http://www.neuron.yale.edu/neuron/static/docs/help/neuron/neuron/secspec.html#forall):
Within an object, forall refers to all the sections declared in the object. This is generally the right thing to do when a template creates sections but is inconvenient when a template is constructed which needs to compute using sections external to it. In this case, one can pass a collection of sections into a template function as a SectionList object argument.
Since
Code: Select all
forsec string
is equivalent to
Code: Select all
forall ifsec string
, I believe that the same should apply to forsec.
csh
 
Posts: 45
Joined: Mon Feb 06, 2006 1:45 pm
Location: University College London

Re: forsec doesn't seem to work in templates

Postby ted » Sun Jan 11, 2009 2:40 pm

I bet csh is right.

One possible workaround might be to construct and execute a command string.
Code: Select all
   strdef cstr
    . . .
   proc access_nseg() {
      sprintf(cstr, "%s print secname(), nseg\n", name)
      execute(cstr)
   }
If this works, it would allow you to continue to create instances of your SectionInfo class by passing a string argument.

A better idea might be to abandon the idea of string arguments, in favor of using the currently accessed section. You would create an instance with this syntax
secname objrefname = new SectionInfo()
where the template for SectionInfo starts off something like this:
Code: Select all
begintemplate SectionInfo
   public name, access_nseg, seclist
   objref sref
   strdef name

   proc init() {
      seclist = new SectionList()
      seclist.append()
   }

Then
forsec seclist
within the SectionInfo template would refer to the section that was "currently accessed" at the time that instance was created, and
forsec SectionInfo.seclist
would be the syntax for referring to that section at the "top level" (i.e. outside the template).

An even better approach would be to abandon SectionList and use SectionRef instead. This would eliminate the need for forsec.
Code: Select all
   proc init() {
      secref = new SectionRef()
   }
Within the template, refer to secref.sec, and at the top level refer to SectionInfo.secref.sec
ted
Site Admin
 
Posts: 3588
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine

Re: forsec doesn't seem to work in templates

Postby TWA » Mon Jan 12, 2009 1:08 am

Thanks for the extremely fast replies, everything is now working. I thought I would post my final observations and conclusions.
  • It is still weird that you can't make forsec work with a string arguement within a template when you make external sections visible using the external declaration.
  • Passing or creating a section list for use with forsec works as the documentation states.
  • SectionRefs worked nicely in this application, combined with using the simple currently-accessed-section method as demonstrated by Ted.
Thanks again!
TWA
 
Posts: 8
Joined: Mon Apr 24, 2006 10:33 am
Location: UCONN Health Center


Return to Other questions

Who is online

Users browsing this forum: Google [Bot] and 0 guests

cron