01d. Thresholding#

Preliminaries#

A fundamental analysis in neural engineering is to determine the threshold stimulus required to elicit activation in a neuron. Differences in threshold between neurons can provide insights into their functional properties, and can often help explain outcomes in neuromodulation related to efficacy and side-effect. This notebook demonstrates how to perform thresholding analyses using Dendra.

Thresholding requires 5 things:

  1. A neuron model that can spike.

  2. A field that encodes the coupling strength between the neuron and the extracellular stimulus.

  3. A ‘unit’ stimulus that represents the extracellular stimulus at a unit amplitude.

  4. A criterion to determine activation.

  5. A tolerance for the threshold search.

Dendra first brackets activation between an inactive lower amplitude and an active upper amplitude. Bisection narrows that bracket to the requested tolerance for each fiber. Use a range with a single inactive-to-active transition to estimate its onset threshold. If the response switches between inactive and active more than once in the range, see Validating hard transition scans (experimental).

Examples#

We’ve seen previously how to create spiking neuron models, fields, and stimuli (see here). We will use the same principles here.

# To start, we will use a simple myelinated axon with Hodgkin-Huxley dynamics.
import dendra as dn
from dendra.models.mod import hh
from dendra.units import mm, um

dn.set_jit_enabled(True)  # True to enable jit compilation globally

# define the model
model = dn.Myelinated(
    diameters=[6.0], n_node=201, node_length=1.0, celsius=6.3, v_init=-65.0
)
model.insert(hh)

# define the field
field = dn.isotropic_point(z=200.0)(model)

# define the waveform
waveform = dn.mono_rect(amp=-1.0, pw=0.1, delay=0.5)

# define the criterion
criterion = dn.callbacks.Active(threshold=0.0, node_check=[5, -5])

This will check nodes 5 and -5 for activation, defined as at least one node crossing 0 mV custom criteria can also be defined by subclassing dn.callbacks.ThresholdCallback for example, dn.callbacks.ActiveAL is another built-in criterion that determines activation based on at least some specified number of nodes crossing threshold.

We then use the Thresholder instrument to perform the threshold search. This instrument takes as input the neuron model, field, stimulus, activation criterion, and tolerance, and returns the threshold values.

from dendra.models.instruments import Thresholder
thresholder = Thresholder(model, criterion, field, waveform, ub=0.1) 
# initial upper bound 0.1 mA

ub, lb = thresholder.calculate_thresholds(tstop=5.0, dt=0.005, rtol=0.01)
print(f"Threshold found between {lb.item():.4f} mA and {ub.item():.4f} mA")
Fixing bounds.
.Done.
Threshold found between 0.1289 mA and 0.1297 mA

The industry standard for mammalian myelinated axons is the McIntyre-Richardson-Grill (MRG) model [1]. This and many other neuron models are available in the dendra-models package. All models expose the same core interfaces, so switching between models is straightforward.

from dendra_models.models import bigMRG

model = bigMRG(diameters=[6.0], n_node=201)
field = dn.isotropic_point(z=500.0)(model)
waveform = dn.mono_rect(amp=-1.0, pw=0.1, delay=0.5)
node_indices = model.find('node', as_list=True)
criterion = dn.callbacks.Active(
    threshold=0.0, node_check=[node_indices[5], node_indices[-5]]
) # check nodes 5 and -5
thresholder = Thresholder(
    model, criterion, field, waveform, ub=0.1, rtol=0.01
)
ub, lb = thresholder.calculate_thresholds(tstop=5.0, dt=0.005)
print(f"Threshold found between {lb.item():.4f} mA and {ub.item():.4f} mA")
Fixing bounds.
.
Done.
Threshold found between 0.1016 mA and 0.1023 mA

Activation threshold and fiber diameter are closely related; larger diameter fibers have lower thresholds. Here we will create a batch of MRG fibers with diameters ranging from 5.7 (the smallest diameter in the original MRG paper) to 20 microns, and compute their activation thresholds to a simple monophasic cathodic pulse.

from dendra_models.models import bigMRG
import torch
import matplotlib.pyplot as plt

diameters = torch.linspace(5.7, 20.0, steps=10) * um
model = bigMRG(diameters=diameters, n_node=201).double()
field = dn.isotropic_point(z=500.0)(model)
waveform = dn.mono_rect(amp=-1.0, pw=0.05, delay=0.5) # shorter pulse width of 0.05 ms
node_indices = model.find('node', as_list=True)
criterion = dn.callbacks.Active(
    threshold=0.0, node_check=[node_indices[5], node_indices[-5]]
) # check nodes 5 and -5
thresholder = Thresholder(
    model, criterion, field, waveform, ub=0.1, rtol=0.01
)
ub, _ = thresholder.calculate_thresholds(tstop=5.0, dt=0.005)

plt.figure(figsize=(5,5), dpi=300)
plt.plot(diameters, ub.squeeze(), marker='o', linewidth=2.0)
plt.xlabel("Fiber Diameter (μm)")
plt.ylabel("Activation Threshold (mA)")
plt.title("Activation Threshold vs Fiber Diameter for MRG Fibers")
plt.show()
Fixing bounds.
.
Done.
../_images/11e610c2cb6686c1be8233c0073ff21f40cc578a0ead658af41fc471eda509ea.png

Activation threshold also varies as a function of pulse width. We can vary the pulse width to see how threshold changes.

pws = torch.logspace(-2, 0, steps=10)
diameter = [6.0]  # fix diameter at 6 μm
model = bigMRG(diameters=diameter*len(pws), n_node=201).double()
field = dn.isotropic_point(z=500.0)(model)

# we can pass in a different pulse width for each fiber 
# by making pw a (num_fiber, 1) tensor
waveform = dn.mono_rect(amp=-1.0, pw=pws[:, None], delay=0.5)
node_indices = model.find('node', as_list=True)
criterion = dn.callbacks.Active(
    threshold=0.0, node_check=[node_indices[5], node_indices[-5]]
) # check nodes 5 and -5
thresholder = Thresholder(
    model, criterion, field, waveform, ub=0.1, rtol=0.01
)

ub, _ = thresholder.calculate_thresholds(tstop=5.0, dt=0.005) 

plt.figure(figsize=(5,5), dpi=300)
plt.plot(pws.squeeze(), ub.squeeze(), marker='o', linewidth=2.0)
plt.xlabel("Pulse Width (ms)")
plt.ylabel("Activation Threshold (mA)")
plt.title("Activation Threshold vs Pulse Width for MRG Fibers")
plt.show()
Fixing bounds.
.
.
.
Done.
../_images/ebf93c7e00b8b107f72581de1f6e16a9e5f490077fa1f94c5381742d974e3456.png

Dendra’s Thresholder instrument will automatically adjust the initial upper bound for the threshold search based on whether activation is detected at the initial upper bound. By default, the thresholder assumes that lack of activation at the upper bound indicates that the upper bound is too low, and will increase it iteratively by multiplying by thresholder.fix_bound_up until activation is detected. However, in some cases (e.g. when blocking is possible), lack of activation at the upper bound may indicate that the upper bound is too high. In these cases, we can set the block_possible flag to True, which will cause the thresholder to use a \(V_{peak}\) heuristic to determine if lack of activation is due to block (the criterion states no activation but \(V_m\) exceeded threshold at some point somewhere along the fiber) and adjust the upper bound downwards (my multiplying by thresholder.fix_bound_down) when activation is not detected at the upper bound.

As an example, let’s re-run the previous analysis but move the source closer to the fiber. For now, we’ll leave the default block_possible=False.

pws = torch.logspace(-2, 0, steps=10)
diameter = [6.0]  # fix diameter at 6 μm
model = bigMRG(diameters=diameter*len(pws), n_node=201).double()
field = dn.isotropic_point(z=100.0)(model)

# we can pass in a different pulse width for each fiber 
# by making pw a (num_fiber, 1) tensor
waveform = dn.mono_rect(amp=-1.0, pw=pws[:, None], delay=0.5)
node_indices = model.find('node', as_list=True)
criterion = dn.callbacks.Active(
    threshold=0.0, node_check=[node_indices[5], node_indices[-5]]
) # check nodes 5 and -5
thresholder = Thresholder(
    model, criterion, field, waveform, ub=0.1, rtol=0.01
)

ub, _ = thresholder.calculate_thresholds(tstop=5.0, dt=0.005) 

plt.figure(figsize=(5,5), dpi=300)
plt.plot(pws.squeeze(), ub.squeeze(), marker='o', linewidth=2.0)
plt.xlabel("Pulse Width (ms)")
plt.ylabel("Activation Threshold (mA)")
plt.title("Activation Threshold vs Pulse Width for MRG Fibers")
plt.show()
Fixing bounds.
.
.
.
.
.
.
.
.
.
.
Unable to fix bounds within 10 iterations, ignoring some.
../_images/ea00775d2c865e2e07d7cb32b15ea4977a7321a7397d8e7159d4a18ffd6a55d2.png

You’ll see that Dendra failed to set an appropriate upper bound for some pulse widths, resulting in failed threshold searches (indicated by the missing values in the plot), and a generally unintuitive strength-duration curve. Now, let’s set block_possible=True and re-run the analysis.

ub, _ = thresholder.calculate_thresholds(tstop=5.0, dt=0.005, block_possible=True) 

plt.figure(figsize=(5,5), dpi=300)
plt.plot(pws.squeeze(), ub.squeeze(), marker='o', linewidth=2.0)
plt.xlabel("Pulse Width (ms)")
plt.ylabel("Activation Threshold (mA)")
plt.title("Activation Threshold vs Pulse Width for MRG Fibers")
plt.show()
Fixing bounds.
.
Done.
../_images/69c1325071442f4813eaec43dbbb0c4d0e82b29991c53330ce6d62fd4173f6d9.png

That makes much more sense! The thresholder was able to automatically adjust the upper bounds appropriately when we instructed it that block was possible, resulting in a complete strength-duration curve.

[1] McIntyre, C. C., Richardson, A. G., & Grill, W. M. (2002). Modeling the excitability of mammalian nerve fibers: influence of afterpotentials on the recovery cycle. Journal of neurophysiology, 87(2), 995-1006.