Sinusoid current implementation

Anything that doesn't fit elsewhere.
Post Reply
SimoNeuroBios
Posts: 2
Joined: Tue May 16, 2023 4:21 am

Sinusoid current implementation

Post by SimoNeuroBios »

First of all, it is a pleasure for me to finally be able to write on this forum. After taking courses, studying, and researching with NEURON, it is satisfying to be able to ask a challenging question that could potentially help everyone. Thank you first and foremost for the quality of your training. =)

Currently, I am using the version 3.last of the Brain Scaffold Builder that utilizes NEURON to simulate realistic neurons (in this case, I am working on a cerebellum scaffold). Ideally, I would like to implement a sinusoidal current clamp so that I can use it as if it were a native tool of NEURON (similar to what is already available with IClamp).

NEURON is implemented through this patch: https://github.com/dbbs-lab/patch.

Based on this topic in the NEURON forum: viewtopic.php?p=14760&hilit=sinusoid#p14760

I'm trying to implement a variant of the IClamp tool in BSB that generates a sinusoidal current pattern:

Code: Select all

NEURON {
        POINT_PROCESS sinclamp
        RANGE delay, dur, pkamp, freq, phase, bias
        ELECTRODE_CURRENT i
}

UNITS {
        (nA) = (nanoamp)
             }

PARAMETER {
        delay=5	(ms)
        dur=200	(ms)
        pkamp=1 (nA)
        freq=1  (Hz)
        phase=0 (rad)
        bias=0  (nA)
        PI=3.14159265358979323846
}

ASSIGNED {
        i (nA)
}

BREAKPOINT {
       at_time(delay)
       at_time(delay + dur)

       if (t < delay) {
	   i=0	
	}else{  
            if (t < delay+dur) {
	        i = pkamp*sin(2*PI*freq*(t-delay)/1000+phase)+bias
	   }else{  
	        i = 0
}}}
After creating the .mod file, I ran the command

Code: Select all

nrnivmodl 
and then made the following modifications.

I have modified all the entries in the devices and adapter files with their respective __init__ referencing the new tool.
I have also made the necessary changes for the tool in the interpreter and object files of the path module.

mod in interpreter.py:

Code: Select all

    def IClamp(self, x=0.5, sec=None):
        sec = sec if sec is not None else self.cas()
        clamp = IClamp(self, self.__h.IClamp(x, sec=transform(sec)))
        clamp.__ref__(sec)
        if hasattr(sec, "__ref__"):
            sec.__ref__(clamp)
        return clamp
    
    def SinClamp(self, x=0.5, sec=None):                                                # <<<<<<<<<<<<<<<============
        sec = sec if sec is not None else self.cas()
        clamp = SinClamp(self, self.__h.SinClamp(x, sec=transform(sec)))
        clamp.__ref__(sec)
        if hasattr(sec, "__ref__"):
            sec.__ref__(clamp)
        return clamp

    def SEClamp(self, sec, x=0.5):
        clamp = SEClamp(self, self.__h.SEClamp(transform(sec(x))))
        clamp.__ref__(sec)
        if hasattr(sec, "__ref__"):
            sec.__ref__(clamp)
        return clamp
mod in object.py:

Code: Select all

    def iclamp(self, x=0.5, delay=0, duration=100, amplitude=0):
        """
        Create a current clamp on the section.
        """
        clamp = self._interpreter.IClamp(x=x, sec=self)
        clamp.delay = delay
        clamp.dur = duration
        if _is_sequence(amplitude):
            # If its a sequence play it as a vector into the clamp
            dt = self._interpreter.dt
            t = self._interpreter.Vector([delay + dt * i for i in range(len(amplitude))])
            v = self._interpreter.Vector(amplitude, t)
            v.play(clamp._ref_amp, t.__neuron__())
            clamp.__ref__(v)
            clamp.__ref__(t)
        else:
            clamp.amp = amplitude
        return clamp

    def sinclamp(self, x=0.5, delay=5, dur=100, pkamp=0, freq=1, phase=0, bias=0.35):  # <<<<<<<<<<<<<<<==
        """
        Create a sinusoidal current clamp on the section.
        """
        clamp = self._interpreter.SinClamp(x=x, sec=self)
        clamp.delay = delay
        clamp.dur = dur
        clamp.pkamp = pkamp
        clamp.freq = freq
        clamp.phase = phase
        clamp.phase = phase
        clamp.bias = bias
        if _is_sequence(pkamp):
            # If its a sequence play it as a vector into the clamp
            dt = self._interpreter.dt
            t = self._interpreter.Vector([delay + dt * i for i in range(len(pkamp))])
            v = self._interpreter.Vector(pkamp, t)
            v.play(clamp._ref_amp, t.__neuron__())
            clamp.__ref__(v)
            clamp.__ref__(t)
        else:
            clamp.pkamp = pkamp
        return clamp
In order to obtain the settings inside the .json configuration file for BSB simulation:

Code: Select all

               "devices": {
                    "sinusoid": {
                         "io": "input",
                         "device": "sin_clamp",
                         "targetting": "cell_type",
                         "cell_types": [
                              "stellate_cell"
                         ],
                         "section_types": [
                              "soma"
                         ],
                         "parameters": {
                              "delay": 5,
                              "dur": 500,
                              "pkamp": 0.35,
                              "freq": 1,
                              "phase": 0,
                              "bias": 0.35
                         }
                    },
Unfortunately, after compiling the code, the simulation freezes, returning this error:

Code: Select all

AttributeError: 'hoc.HocObject' object has no attribute 'SinClamp'
    device.implement(target, location)
  File "/home/simone/.pyenv/versions/3.9.0/envs/BSB_tool/lib/python3.9/site-packages/bsb/simulators/neuron/devices/sin_clamp.py", line 7, in implement
    location.section.sinclamp(**pattern)
  File "/home/simone/.pyenv/versions/3.9.0/envs/BSB_tool/lib/python3.9/site-packages/patch/objects.py", line 318, in sinclamp
    clamp = self._interpreter.SinClamp(x=x, sec=self)
  File "/home/simone/.pyenv/versions/3.9.0/envs/BSB_tool/lib/python3.9/site-packages/patch/interpreter.py", line 239, in SinClamp
    clamp = SinClamp(self, self.__h.SinClamp(x, sec=transform(sec)))
AttributeError: 'hoc.HocObject' object has no attribute 'SinClamp'
Any suggestions on how to implement this tool?
I know that I may be asking for something quite challenging, and I apologize if I have posted it in the wrong section
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Sinusoid current implementation

Post by ted »

Currently, I am using the version 3.last of the Brain Scaffold Builder
That's interesting in itself. Where can people learn more about the Brain Scaffold Builder?
I would like to implement a sinusoidal current clamp
You have come to the right place. Get https://www.neuron.yale.edu/ftp/ted/neuron/izap.zip, unzip it, run nrnivmodl (mknrndll), then execute
nrngui initizap.hoc

The NMODL code in izap.mod specifies the properties of a class called Izap which is a current clamp that delivers a sinusoidally oscillating current. Its parameters are
del the time at which the current starts
dur the duration of the oscillating current
f0 the instantaneous frequency at t == del
f1 the instantaneous frequencey at t == del + dur
amp the maximum amplitude of the injected current

f0 and f1 are both constrained to be nonnegative.
If f0 == f1 its output is just an ordinary sine wave.

Notice the use of events to control the onset and duration of the oscillation. The resulting code, which avoids the deprecated at_time(), is much easier to understand and debug, and works nicely with adaptive integration.

The demo program is written in hoc, but could probably be implemented much more elegantly in Python.

One might well ask "where has izap.mod been hiding?" The answer is: in plain sight. Since 2015 it has been lurking in the Forum's Hot tips area viewforum.php?f=28 in the thread mod files: what they are, and how to use them.

And in ModelDB I see that an earlier version was used by Proddutur et al. 2013 (modeldb.science/155601) but that version contains a bug: the argument to net_send() should be dur, not t+dur. Fortunately that model entry does not seem to use Izap at all, so we can just delete it (and notify that model's authors about the bug, and how it was dealt with).
SimoNeuroBios
Posts: 2
Joined: Tue May 16, 2023 4:21 am

Re: Sinusoid current implementation

Post by SimoNeuroBios »

ted wrote: Tue May 16, 2023 6:42 pm
Currently, I am using the version 3.last of the Brain Scaffold Builder
That's interesting in itself. Where can people learn more about the Brain Scaffold Builder?
Thank you very much for the response!
The BSB is accessible through this website:
https://bsb.readthedocs.io/en/v3-last/
And this repository:
https://github.com/dbbs-lab/bsb

I used it for my thesis and it is extremely convenient for working with complex and heterogeneous networks.
(The research group of my professor is the same that developed the Purkinje cell that you used in some of your training videos)
ted wrote: Tue May 16, 2023 6:42 pm
I would like to implement a sinusoidal current clamp
You have come to the right place. Get https://www.neuron.yale.edu/ftp/ted/neuron/izap.zip, unzip it, run nrnivmodl (mknrndll), then execute
nrngui initizap.hoc

Code: Select all

  File "/home/simone/.pyenv/versions/3.9.0/envs/BSB_tool/lib/python3.9/site-packages/patch/interpreter.py", line 239, in Izap
    clamp = Izap(self, self.__h.Izap(x, sec=transform(sec)))
[b]AttributeError: 'hoc.HocObject' object has no attribute 'Izap'[/b]

  File "/home/simone/.pyenv/versions/3.9.0/envs/BSB_tool/lib/python3.9/site-packages/bsb/simulators/neuron/devices/izap.py", line 7, in implement
    location.section.Izap(**pattern)
  File "/home/simone/.pyenv/versions/3.9.0/envs/BSB_tool/lib/python3.9/site-packages/patch/objects.py", line 41, in __getattr__
    return getattr(self.__dict__["_neuron_ptr"], attr)
[b]AttributeError: 'nrn.Section' object has no attribute 'Izap'[/b]
I'm afraid that in my case, the situation is more complicated than anticipated.
In fact, what I need to do is not just introduce a tool in NEURON, but integrate it within the BSB so that the simulation proceeds synchronously, just like with IClamp.

I have opened an issue in the repository, and I hope they will respond to me!
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Sinusoid current implementation

Post by ted »

What is there about Izap that would interfere with a simulation "proceeding synchronously"? (answer: nothing at all)
What is there about BSB that prevents it from using a point process that works perfectly well with NEURON? (answer pending . . . )
Post Reply