Accessing Pointers instantiated through Python

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

Moderator: hines

Post Reply
oitani
Posts: 8
Joined: Fri Feb 21, 2020 7:11 am

Accessing Pointers instantiated through Python

Post by oitani »

Dear forum,

I'm looking to programmatically access NEURON Pointers that were instantiated using "h.setpointer" in Python
Where do Pointers constructed by "h.setpointer" live, and what's the best way to access them?

For example, let's say I have two sections (secA and secB) that are connected with a chemical synapse (synAB, A->B)
The pre-synaptic voltage is set by manually pointing to the appropriate membrane potential reference using h.setpointer :

Code: Select all

 secA = h.Section(name='secA')
 secB = h.Section(name='secB')
 synAB = h.fakeSynapse(secB(0.5)) 
 
 # assuming fakeSynapse comes from a MOD file that has POINTER V_pre in the NEURON block
 
 h.setpointer(secA(0.5)._ref_v, 'V_pre', synAB) 
What would be the cleanest way to access the pointer made in the last line?
If I could access it, would I be able to use it to find the sources/targets? Ie that synAB.V_pre is pointing to secA(0.5)._ref_v?

Appreciate any help/direction here.

Best,
Omar
oitani
Posts: 8
Joined: Fri Feb 21, 2020 7:11 am

Re: Accessing Pointers instantiated through Python

Post by oitani »

Curious if the moderators can point me in a direction here, or advise on if it is more appropriate to move this post to another part of the forum

Feedback would be greatly appreciated

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

Re: Accessing Pointers instantiated through Python

Post by ted »

The best answer to your question depends on what you are trying to do. So what is it that you are trying to do by "programmatically accessing" pointers?
oitani
Posts: 8
Joined: Fri Feb 21, 2020 7:11 am

Re: Accessing Pointers instantiated through Python

Post by oitani »

Thanks Ted, my intentions for programmatically accessing pointers are primarily for model inspection and documentation purposes -- I want to keep track of all variables that are being targeted as well as their sources

The context is that I've built on previous work to develop a set of helper Python scripts that will clarify to users what is happening in their models in mathematical terms by returning equations associated with inserted mechanisms/point processes.
This should a) increase accessibility to those not immediately familiar with NEURON and b) generally help with model validation, debugging, and documentation

The scripts return a dictionary of SymPy equations associated with the mechanisms in a given hoc session, which can then easily be evaluated against expectation, plotted for visualization, exported to LaTeX for documentation, etc.

As we might expect, to correctly implement something like this for a set of equations that has a pointer variable, we should know what the source is for the target to be properly represented.

The cleanest way I can think of doing this is by accessing all constructed pointers in a hoc session, and somehow retrieving the target/source for all POINTER variables (parsed using The NMODL Framework BlueBrain package)

So my main problem is not knowing where to find constructed pointers, which would be the first hurdle to clear

Do we think this is a feasible approach, or perhaps there is a more reasonable direction?
ramcdougal
Posts: 243
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Accessing Pointers instantiated through Python

Post by ramcdougal »

setpointer literally sets a double* that's allocated inside of the MOD file. Once the assignment has been made, there is no explicit semantic information obtainable from the pointer.

That is, h.setpointer(secA(0.5)._ref_v, 'V_pre', synAB) is 100% equivalent to: synAB._ref_V_pre = secA(0.5)._ref_v

Both are valid in Python (as of NEURON 7.8), but the one using an equal sign is clearer to what's being done. Internally NEURON is perfectly happy if the pointer comes from user code, and indeed this is one way to couple NEURON to other solvers.

(setpointer exists as a work-around for the HOC language not being able to assign a pointer to a pointer. Python has no such limitation.)
hines
Site Admin
Posts: 1659
Joined: Wed May 18, 2005 3:32 pm

Re: Accessing Pointers instantiated through Python

Post by hines »

One of the problems with POINTER for implementing gap junctions is that it can't work for parallel MPI simulations where the source may be on a different machine than the target. And for multiple threads, it's more difficult to avoid race conditions. So for connecting pieces of a model together with graded synapses, it's better to use https://nrn.readthedocs.io/en/latest/py ... source_var
along with companion target_var and setup_transfer methods. Of course, behind the scenes these are still pointers into memory locations and a lot of effort goes into recalculating the pointer when what it points to changes its memory location (e.g. when one asks for a cache efficient memory layout or a different number of threads.) There is a lot of other code in the NEURON implementation devoted to trying to keep pointers pointing to the correct location and figuring out the HOC variable they are pointing to. Especially when the old memory location is freed and new memory has been allocated for the same conceptual variable.
oitani
Posts: 8
Joined: Fri Feb 21, 2020 7:11 am

Re: Accessing Pointers instantiated through Python

Post by oitani »

Thanks for the clarification Robert, perhaps then I'm looking in the wrong place -- instead of querying the pointer for targets and sources it sounds like I should instead refer to the double pointers that are being assigned
The equal sign notation for assigning pointers is much clearer, thanks for the tip!

Thanks Michael, that makes sense -- using ParallelContext seems to be the way to go for connecting parts of the model. Appreciate the clarification for how pointers get handled in a parallel context

I'll try a few implementations of tracking targets/sources and see what makes the most sense to use.
Thanks to all for the clarifying discussion
Post Reply