# Implementing condition v value in STN (AdEx)

**URL:** <https://brian.discourse.group/t/implementing-condition-v-value-in-stn-adex/1397>\
**Category:** Projects\
**Created:** [14 February 2025 05:15 UTC](https://brian.discourse.group/t/implementing-condition-v-value-in-stn-adex/1397 "2025-02-14T05:15:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![keun](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@keun](https://brian.discourse.group/u/keun)\
**Post date:** [14 February 2025 05:15 UTC](https://brian.discourse.group/t/implementing-condition-v-value-in-stn-adex/1397/1 "2025-02-14T05:15:25Z")

</div>

I am building an STN neuron model using Brian2 to simulate burst activity. The burst mechanism involves resetting the membrane potential (`v`) after a spike using the rule:

v\_reset+max⁡(u−15,20) (when u\<0)

I tried implementing this condition in the `reset` argument of `NeuronGroup` within the `create_neurons` function. However, Brian2 does not seem to support `max()` directly. What is the proper way to apply this reset condition in Brian2? I followed the instructions in the image below and applied.

 ![image](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/67c61a90e627962b0085df727b217efe8822c127.png)

````auto
class STN(NeuronModel):
    def __init__ (self, N, params):
        super(). __init__ (N, params)
        self.neurons = None

    def create_neurons(self):
        eqs = AdEx.eqs
        reset = '''
        v = vr + ((u < 0) * ((u - 15*pA) * (u - 15*pA > 20*pA) + 20*pA * (u - 15*pA <= 20*pA)));
        u += d
        '''
        self.neurons = NeuronGroup(
            self.N, eqs, threshold='v > th', reset=reset, method='euler'
        )

        self.neurons.g_L = self.params['g_L']['value'] * eval(self.params['g_L']['unit'])
        self.neurons.E_L = self.params['E_L']['value'] * eval(self.params['E_L']['unit'])
        self.neurons.Delta_T = self.params['Delta_T']['value'] * eval(self.params['Delta_T']['unit'])
        self.neurons.vt = self.params['vt']['value'] * eval(self.params['vt']['unit'])
        self.neurons.vr = self.params['vr']['value'] * eval(self.params['vr']['unit'])
        self.neurons.tau_w = self.params['tau_w']['value'] * eval(self.params['tau_w']['unit'])
        self.neurons.th = self.params['th']['value'] * eval(self.params['th']['unit'])
        self.neurons.a = self.params['a']['value'] * eval(self.params['a']['unit'])
        self.neurons.d = self.params['d']['value'] * eval(self.params['d']['unit'])
        self.neurons.C = self.params['C']['value'] * eval(self.params['C']['unit'])

        return self.neurons```
````

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [14 February 2025 09:40 UTC](https://brian.discourse.group/t/implementing-condition-v-value-in-stn-adex/1397/2 "2025-02-14T09:40:36Z")

</div>

Hi @keun, the absence of `min`/`max` functions comes up regularly, I think we should fix this ! Brian _does_ support the more powerful `clip` function, though, which can replace both `min` and `max`. In your case, you should be able to use:

```auto
reset = 'vr + clip(w - 15*pA, 20*pA, inf*pA)'  

```

which clips values below 20pA to 20pA. I’m a bit confused about the “with w \< 0” part, though. What should happen if w ≥ 0? Also, if w \< 0, then max(w - 15, 20) will always be 20, no?

---

<div class="post-metadata">

**Author:** ![keun](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@keun](https://brian.discourse.group/u/keun)\
**Post date:** [17 February 2025 06:57 UTC](https://brian.discourse.group/t/implementing-condition-v-value-in-stn-adex/1397/3 "2025-02-17T06:57:42Z")

</div>

Hi @mstimberg , thank you for your reply.

If w\<0, I want to select the larger of (w-15, 20), but it’s hard to apply that condition with clip because of the unit issue. I tried with the code below, but it doesn’t work the way I want because CLIP is not comparing the specified values, but specifying the position of the min, max values. Is there any way to modify it differently?

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [17 February 2025 09:44 UTC](https://brian.discourse.group/t/implementing-condition-v-value-in-stn-adex/1397/4 "2025-02-17T09:44:05Z")

</div>

Hi @keun.

> [@keun](#):
>
> If w\<0, I want to select the larger of (w-15, 20)

This is the part that I did not understand: if w \< 0, then w-15 will be always lower than -15, so 20 will _always_ be the larger value.

> [@keun](#):
>
> but it’s hard to apply that condition with clip because of the unit issue. I tried with the code below, but it doesn’t work the way I want because CLIP is not comparing the specified values, but specifying the position of the min, max values. Is there any way to modify it differently?

I am sorry, but I do not understand this either. What do you mean by “CLIP is not comparing the specified values, but specifying the position of the min, max values”? The `clip` functions clips a value between a minimum and a maximum value. If you replace the minimum or maximum value by `-inf` or `inf` respectively, it will be exactly equivalent to `min` or `max`. Regarding units, all three elements should have the same units, so e.g. use `clip(w - 15*pA, 20*pA, inf*pA)` if `w` has units of Ampère or use `clip(w -15, 20, inf)` if `w` is unitless.

---

<div class="post-metadata">

**Author:** ![keun](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@keun](https://brian.discourse.group/u/keun)\
**Post date:** [18 February 2025 04:24 UTC](https://brian.discourse.group/t/implementing-condition-v-value-in-stn-adex/1397/5 "2025-02-18T04:24:44Z")

</div>

Thank you for your kind response. According to the paper, I initially understood that _w_ could also take values less than zero depending on the conditions, but it seems I misunderstood. Since _v_ is reset based on `max(w-15, 20)` regardless of the range of _w_, I plan to use `clip`, as you explained.

However, I have one question: The STN model uses the AdEx model, and the units are in pA (amperes). Would there be any issues in calculations when performing operations with values that do not have explicitly multiplied units?

Once again, I really appreciate your help. It was very useful!

 ![image](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/f13b9dff5c0c23c0b98a8de25e850fb8f8462fd1.png)

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [19 February 2025 10:39 UTC](https://brian.discourse.group/t/implementing-condition-v-value-in-stn-adex/1397/6 "2025-02-19T10:39:43Z")

</div>

Hi @keun

> [@keun](#):
>
> However, I have one question: The STN model uses the AdEx model, and the units are in pA (amperes). Would there be any issues in calculations when performing operations with values that do not have explicitly multiplied units?

From the code you posted it already looks as if you did take care of the units correctly? Everything has to be consistent, so if your model equations describe e.g. `w` as having units of `amp`, then you’ll have to write things as `clip(w - 15*pA, 20*pA, inf*pA)` so that everything matches. But you can also make your variables unitless, and use conventions as in many papers (voltages are in mV, currents are in pA, etc.). The only dimension that you cannot remove is the unit of time, which means that e.g. time constants would still have to be quantities in seconds (or ms, etc.).

---

<div class="post-metadata">

**Author:** ![keun](https://avatars.discourse-cdn.com/v4/letter/k/3da27b/32.png) [@keun](https://brian.discourse.group/u/keun)\
**Post date:** [20 February 2025 05:20 UTC](https://brian.discourse.group/t/implementing-condition-v-value-in-stn-adex/1397/7 "2025-02-20T05:20:31Z")

</div>

Hi @mstimberg,

Thank you for your thoughtful response. Your answer was extremely helpful and has allowed me to resolve my issue.
