LIF Tuning Curve#

import pandas as pd
import numpy as np

import plotly.graph_objects as go
dt = 0.001 # set 1 ms timestep

a = 0.02  # tau_recovery: time scale of the recovery variable
b = 0.25  # coupling: How sensitive recovery is to subthreshold fluctucations of voltage
c = -65   # reset voltage (mV): The voltage to reset to after a spike
d = 2     # reset_recovery: The recovery value to reset to after a spike

input_current = range(28) # constant inputs for each simulation
t = np.arange(0, 1, 0.001) # time samples for 1 second
data = [{
    "voltage": 0,
    "recovery": 0,
}]

current_data = [{
    "current": i,
    "fire": 0
} for i in input_current]
# voltage represents membrane potential of a neuron
# u represents the membrane recovery

# supply a constant input current
for idx, cdata in enumerate(current_data):
    I = cdata["current"]
    
    # loop through timesteps
    for _ in t:
        # get the neuron state of the previous timestep
        voltage = data[-1]["voltage"]
        recovery = data[-1]["recovery"]
        
        # compute change in voltage and recovery
        dv = (0.04 * voltage**2 + 5 * voltage + 140 - recovery + I) * 1000
        du = a*(b * voltage - recovery) * 1000
        
        # apply change
        voltage += dv * dt
        recovery += du * dt
        
        # if voltage threshold reached (spiked), reset
        if voltage > 30: # mV
            cdata.update({
                "fire": cdata["fire"]+1
            })
            voltage = c
            recovery = recovery + d

        # save voltage and recovery for plotting
        data.append({
            "voltage": voltage,
            "recovery": recovery,
        })
current_data_df = pd.DataFrame(current_data)
fig = go.Figure(data=go.Scatter(x=current_data_df["current"], y=current_data_df["fire"]))
fig.update_layout(
    title="Tuning Curve",
    xaxis_title="Input Current (mA)",
    yaxis_title="Firing Rate (Hz)",
    # legend_title="Legend Title",
    # font=dict(
    #     family="Courier New, monospace",
    #     size=18,
    #     color="RebeccaPurple"
    # )
)