diff --git a/coursera_compneuro/week5/intfire.py b/coursera_compneuro/week5/intfire.py new file mode 100644 index 0000000..53863c8 --- /dev/null +++ b/coursera_compneuro/week5/intfire.py @@ -0,0 +1,55 @@ +# %% + +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 100000 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 200 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + + V_trace += [V] + +total_spikes = 0 +for v in V_trace: + if v == 50: + total_spikes += 1 + +print(total_spikes) +plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/intfire.py b/coursera_compneuro/week5/intfire.py new file mode 100644 index 0000000..53863c8 --- /dev/null +++ b/coursera_compneuro/week5/intfire.py @@ -0,0 +1,55 @@ +# %% + +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 100000 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 200 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + + V_trace += [V] + +total_spikes = 0 +for v in V_trace: + if v == 50: + total_spikes += 1 + +print(total_spikes) +plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/intfire_noise.py b/coursera_compneuro/week5/intfire_noise.py new file mode 100644 index 0000000..6a763d8 --- /dev/null +++ b/coursera_compneuro/week5/intfire_noise.py @@ -0,0 +1,61 @@ +# %% +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 1 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 2000 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold +spiketimes = [] # list of spike times + +# input current +noiseamp = 1 # amplitude of added noise +I += noiseamp*np.random.normal(0, 1, (tstop,)) # nA; Gaussian noise + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I[t]/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + spiketimes += [t] + V_trace += [V] + +isi = np.diff(spiketimes) + +import seaborn as sns + +sns.histplot(isi,kde=True) + +# plt.hist(isi) + +# plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/intfire.py b/coursera_compneuro/week5/intfire.py new file mode 100644 index 0000000..53863c8 --- /dev/null +++ b/coursera_compneuro/week5/intfire.py @@ -0,0 +1,55 @@ +# %% + +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 100000 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 200 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + + V_trace += [V] + +total_spikes = 0 +for v in V_trace: + if v == 50: + total_spikes += 1 + +print(total_spikes) +plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/intfire_noise.py b/coursera_compneuro/week5/intfire_noise.py new file mode 100644 index 0000000..6a763d8 --- /dev/null +++ b/coursera_compneuro/week5/intfire_noise.py @@ -0,0 +1,61 @@ +# %% +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 1 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 2000 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold +spiketimes = [] # list of spike times + +# input current +noiseamp = 1 # amplitude of added noise +I += noiseamp*np.random.normal(0, 1, (tstop,)) # nA; Gaussian noise + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I[t]/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + spiketimes += [t] + V_trace += [V] + +isi = np.diff(spiketimes) + +import seaborn as sns + +sns.histplot(isi,kde=True) + +# plt.hist(isi) + +# plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/membrane.py b/coursera_compneuro/week5/membrane.py new file mode 100644 index 0000000..1037fa7 --- /dev/null +++ b/coursera_compneuro/week5/membrane.py @@ -0,0 +1,64 @@ +#%% + +from __future__ import print_function +""" +Created on Wed Apr 22 15:53:00 2015 + +Charging and discharging curves for passive membrane patch +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + +# input current +I = 10 # nA + +# capacitance and leak resistance + +C = 0.1 # nF +R = 100 # M ohms +tau = R*C # = 0.1*100 nF-Mohms = 100*100 pF Mohms = 10 ms +print('C = %.3f nF' % C) +print('R = %.3f M ohms' % R) +print('tau = %.3f ms' % tau) +print('(Theoretical)') + +# membrane potential equation dV/dt = - V/RC + I/C + +tstop = 150 # ms + +V_inf = I*R # peak V (in mV) +tau = 0 # experimental (ms) + +h = 0.2 # ms (step size) + +V = 0 # mV +V_trace = [V] # mV + +for t in np.arange(h, tstop, h): + + # Euler method: V(t+h) = V(t) + h*dV/dt + V = V +h*(- (V/(R*C)) + (I/C)) + + # Verify membrane time constant + if (not tau and (V > 0.6321*V_inf)): + tau = t + print('tau = %.3f ms' % tau) + print('(Experimental)') + + + # Stop current injection + # if t >= 0.6*tstop: + # I = 0 + + V_trace += [V] + if t % 10 == 0: + plt.plot(np.arange(0,t+h, h), V_trace, color='r') + plt.xlim(0, tstop) + plt.ylim(0, V_inf) + plt.draw() + +plt.show() diff --git a/coursera_compneuro/week5/intfire.py b/coursera_compneuro/week5/intfire.py new file mode 100644 index 0000000..53863c8 --- /dev/null +++ b/coursera_compneuro/week5/intfire.py @@ -0,0 +1,55 @@ +# %% + +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 100000 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 200 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + + V_trace += [V] + +total_spikes = 0 +for v in V_trace: + if v == 50: + total_spikes += 1 + +print(total_spikes) +plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/intfire_noise.py b/coursera_compneuro/week5/intfire_noise.py new file mode 100644 index 0000000..6a763d8 --- /dev/null +++ b/coursera_compneuro/week5/intfire_noise.py @@ -0,0 +1,61 @@ +# %% +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 1 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 2000 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold +spiketimes = [] # list of spike times + +# input current +noiseamp = 1 # amplitude of added noise +I += noiseamp*np.random.normal(0, 1, (tstop,)) # nA; Gaussian noise + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I[t]/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + spiketimes += [t] + V_trace += [V] + +isi = np.diff(spiketimes) + +import seaborn as sns + +sns.histplot(isi,kde=True) + +# plt.hist(isi) + +# plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/membrane.py b/coursera_compneuro/week5/membrane.py new file mode 100644 index 0000000..1037fa7 --- /dev/null +++ b/coursera_compneuro/week5/membrane.py @@ -0,0 +1,64 @@ +#%% + +from __future__ import print_function +""" +Created on Wed Apr 22 15:53:00 2015 + +Charging and discharging curves for passive membrane patch +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + +# input current +I = 10 # nA + +# capacitance and leak resistance + +C = 0.1 # nF +R = 100 # M ohms +tau = R*C # = 0.1*100 nF-Mohms = 100*100 pF Mohms = 10 ms +print('C = %.3f nF' % C) +print('R = %.3f M ohms' % R) +print('tau = %.3f ms' % tau) +print('(Theoretical)') + +# membrane potential equation dV/dt = - V/RC + I/C + +tstop = 150 # ms + +V_inf = I*R # peak V (in mV) +tau = 0 # experimental (ms) + +h = 0.2 # ms (step size) + +V = 0 # mV +V_trace = [V] # mV + +for t in np.arange(h, tstop, h): + + # Euler method: V(t+h) = V(t) + h*dV/dt + V = V +h*(- (V/(R*C)) + (I/C)) + + # Verify membrane time constant + if (not tau and (V > 0.6321*V_inf)): + tau = t + print('tau = %.3f ms' % tau) + print('(Experimental)') + + + # Stop current injection + # if t >= 0.6*tstop: + # I = 0 + + V_trace += [V] + if t % 10 == 0: + plt.plot(np.arange(0,t+h, h), V_trace, color='r') + plt.xlim(0, tstop) + plt.ylim(0, V_inf) + plt.draw() + +plt.show() diff --git a/coursera_compneuro/week6/alpha_neuron.py b/coursera_compneuro/week6/alpha_neuron.py new file mode 100644 index 0000000..c1283b4 --- /dev/null +++ b/coursera_compneuro/week6/alpha_neuron.py @@ -0,0 +1,120 @@ +# %% +""" +Created on Wed Apr 22 16:13:18 2015 + +Fire a neuron via alpha function synapse and random input spike train +R Rao 2007 + +translated to python by rkp 2015 +""" +from __future__ import print_function, division + +import time +import numpy as np +from numpy import concatenate as cc +import matplotlib.pyplot as plt + +np.random.seed(0) +# I & F implementation dV/dt = - V/RC + I/C +h = 1. # step size, Euler method, = dt ms +t_max= 200 # ms, simulation time period +tstop = int(t_max/h) # number of time steps +ref = 0 # refractory period counter + +# Generate random input spikes +# Note: This is not entirely realistic - no refractory period +# Also: if you change step size h, input spike train changes too... +thr = 0.9 # threshold for random spikes +spike_train = np.random.rand(tstop) > thr + +# alpha func synaptic conductance +t_a = 100 # Max duration of syn conductance +t_peak = 0.5 # ms +g_peak = 0.05 # nS (peak synaptic conductance) + +spikes_traces = [0] + +for t_peak in np.arange(0.5, 10.5, 0.5): + const = g_peak / (t_peak*np.exp(-1)); + t_vec = np.arange(0, t_a + h, h) + alpha_func = const * t_vec * (np.exp(-t_vec/t_peak)) + + # plt.plot(t_vec[:80], alpha_func[:80]) + # plt.xlabel('t (in ms)') + # plt.title('Alpha Function (Synaptic Conductance for Spike at t=0)') + # plt.draw() + # time.sleep(2) + + # capacitance and leak resistance + C = 0.5 # nF + R = 40 # M ohms +# print('C = {}'.format(C)) +# print('R = {}'.format(R)) + + # conductance and associated parameters to simulate spike rate adaptation + g_ad = 0 + G_inc = 1/h + tau_ad = 2 + + # Initialize basic parameters + E_leak = -60 # mV, equilibrium potential + E_syn = 0 # Excitatory synapse (why is this excitatory?) + g_syn = 0 # Current syn conductance + V_th = -40 # spike threshold mV + V_spike = 50 # spike value mV + ref_max = 4/h # Starting value of ref period counter + t_list = np.array([], dtype=int) + V = E_leak + V_trace = [V] + t_trace = [0] + +# fig, axs = plt.subplots(2, 1) +# axs[0].plot(np.arange(0,t_max,h), spike_train) +# axs[0].set_title('Input spike train') + + for t in range(tstop): + + # Compute input + if spike_train[t]: # check for input spike + t_list = cc([t_list, [1]]) + + # Calculate synaptic current due to current and past input spikes + g_syn = np.sum(alpha_func[t_list]) + I_syn = g_syn*(E_syn - V) + + # Update spike times + if np.any(t_list): + t_list = t_list + 1 + if t_list[0] == t_a: # Reached max duration of syn conductance + t_list = t_list[1:] + + # Compute membrane voltage + # Euler method: V(t+h) = V(t) + h*dV/dt + if not ref: + V = V + h*(-((V-E_leak)*(1+R*g_ad)/(R*C)) + (I_syn/C)) + g_ad = g_ad + h*(-g_ad/tau_ad) # spike rate adaptation + else: + ref -= 1 + V = V_th - 10 # reset voltage after spike + g_ad = 0 + + # Generate spike + if (V > V_th) and not ref: + V = V_spike + ref = ref_max + g_ad = g_ad + G_inc + + V_trace += [V] + t_trace += [t*h] + + spikes_traces += [np.count_nonzero(np.array(V_trace) == V_spike)] + +# axs[1].plot(t_trace,V_trace) +# plt.draw() +# axs[1].set_title('Output spike train') +# plt.show() + +print(spikes_traces) +plt.plot([.0,10.],[0.,36.]) +plt.plot(spikes_traces) +plt.show() diff --git a/coursera_compneuro/week5/intfire.py b/coursera_compneuro/week5/intfire.py new file mode 100644 index 0000000..53863c8 --- /dev/null +++ b/coursera_compneuro/week5/intfire.py @@ -0,0 +1,55 @@ +# %% + +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 100000 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 200 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + + V_trace += [V] + +total_spikes = 0 +for v in V_trace: + if v == 50: + total_spikes += 1 + +print(total_spikes) +plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/intfire_noise.py b/coursera_compneuro/week5/intfire_noise.py new file mode 100644 index 0000000..6a763d8 --- /dev/null +++ b/coursera_compneuro/week5/intfire_noise.py @@ -0,0 +1,61 @@ +# %% +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 1 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 2000 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold +spiketimes = [] # list of spike times + +# input current +noiseamp = 1 # amplitude of added noise +I += noiseamp*np.random.normal(0, 1, (tstop,)) # nA; Gaussian noise + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I[t]/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + spiketimes += [t] + V_trace += [V] + +isi = np.diff(spiketimes) + +import seaborn as sns + +sns.histplot(isi,kde=True) + +# plt.hist(isi) + +# plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/membrane.py b/coursera_compneuro/week5/membrane.py new file mode 100644 index 0000000..1037fa7 --- /dev/null +++ b/coursera_compneuro/week5/membrane.py @@ -0,0 +1,64 @@ +#%% + +from __future__ import print_function +""" +Created on Wed Apr 22 15:53:00 2015 + +Charging and discharging curves for passive membrane patch +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + +# input current +I = 10 # nA + +# capacitance and leak resistance + +C = 0.1 # nF +R = 100 # M ohms +tau = R*C # = 0.1*100 nF-Mohms = 100*100 pF Mohms = 10 ms +print('C = %.3f nF' % C) +print('R = %.3f M ohms' % R) +print('tau = %.3f ms' % tau) +print('(Theoretical)') + +# membrane potential equation dV/dt = - V/RC + I/C + +tstop = 150 # ms + +V_inf = I*R # peak V (in mV) +tau = 0 # experimental (ms) + +h = 0.2 # ms (step size) + +V = 0 # mV +V_trace = [V] # mV + +for t in np.arange(h, tstop, h): + + # Euler method: V(t+h) = V(t) + h*dV/dt + V = V +h*(- (V/(R*C)) + (I/C)) + + # Verify membrane time constant + if (not tau and (V > 0.6321*V_inf)): + tau = t + print('tau = %.3f ms' % tau) + print('(Experimental)') + + + # Stop current injection + # if t >= 0.6*tstop: + # I = 0 + + V_trace += [V] + if t % 10 == 0: + plt.plot(np.arange(0,t+h, h), V_trace, color='r') + plt.xlim(0, tstop) + plt.ylim(0, V_inf) + plt.draw() + +plt.show() diff --git a/coursera_compneuro/week6/alpha_neuron.py b/coursera_compneuro/week6/alpha_neuron.py new file mode 100644 index 0000000..c1283b4 --- /dev/null +++ b/coursera_compneuro/week6/alpha_neuron.py @@ -0,0 +1,120 @@ +# %% +""" +Created on Wed Apr 22 16:13:18 2015 + +Fire a neuron via alpha function synapse and random input spike train +R Rao 2007 + +translated to python by rkp 2015 +""" +from __future__ import print_function, division + +import time +import numpy as np +from numpy import concatenate as cc +import matplotlib.pyplot as plt + +np.random.seed(0) +# I & F implementation dV/dt = - V/RC + I/C +h = 1. # step size, Euler method, = dt ms +t_max= 200 # ms, simulation time period +tstop = int(t_max/h) # number of time steps +ref = 0 # refractory period counter + +# Generate random input spikes +# Note: This is not entirely realistic - no refractory period +# Also: if you change step size h, input spike train changes too... +thr = 0.9 # threshold for random spikes +spike_train = np.random.rand(tstop) > thr + +# alpha func synaptic conductance +t_a = 100 # Max duration of syn conductance +t_peak = 0.5 # ms +g_peak = 0.05 # nS (peak synaptic conductance) + +spikes_traces = [0] + +for t_peak in np.arange(0.5, 10.5, 0.5): + const = g_peak / (t_peak*np.exp(-1)); + t_vec = np.arange(0, t_a + h, h) + alpha_func = const * t_vec * (np.exp(-t_vec/t_peak)) + + # plt.plot(t_vec[:80], alpha_func[:80]) + # plt.xlabel('t (in ms)') + # plt.title('Alpha Function (Synaptic Conductance for Spike at t=0)') + # plt.draw() + # time.sleep(2) + + # capacitance and leak resistance + C = 0.5 # nF + R = 40 # M ohms +# print('C = {}'.format(C)) +# print('R = {}'.format(R)) + + # conductance and associated parameters to simulate spike rate adaptation + g_ad = 0 + G_inc = 1/h + tau_ad = 2 + + # Initialize basic parameters + E_leak = -60 # mV, equilibrium potential + E_syn = 0 # Excitatory synapse (why is this excitatory?) + g_syn = 0 # Current syn conductance + V_th = -40 # spike threshold mV + V_spike = 50 # spike value mV + ref_max = 4/h # Starting value of ref period counter + t_list = np.array([], dtype=int) + V = E_leak + V_trace = [V] + t_trace = [0] + +# fig, axs = plt.subplots(2, 1) +# axs[0].plot(np.arange(0,t_max,h), spike_train) +# axs[0].set_title('Input spike train') + + for t in range(tstop): + + # Compute input + if spike_train[t]: # check for input spike + t_list = cc([t_list, [1]]) + + # Calculate synaptic current due to current and past input spikes + g_syn = np.sum(alpha_func[t_list]) + I_syn = g_syn*(E_syn - V) + + # Update spike times + if np.any(t_list): + t_list = t_list + 1 + if t_list[0] == t_a: # Reached max duration of syn conductance + t_list = t_list[1:] + + # Compute membrane voltage + # Euler method: V(t+h) = V(t) + h*dV/dt + if not ref: + V = V + h*(-((V-E_leak)*(1+R*g_ad)/(R*C)) + (I_syn/C)) + g_ad = g_ad + h*(-g_ad/tau_ad) # spike rate adaptation + else: + ref -= 1 + V = V_th - 10 # reset voltage after spike + g_ad = 0 + + # Generate spike + if (V > V_th) and not ref: + V = V_spike + ref = ref_max + g_ad = g_ad + G_inc + + V_trace += [V] + t_trace += [t*h] + + spikes_traces += [np.count_nonzero(np.array(V_trace) == V_spike)] + +# axs[1].plot(t_trace,V_trace) +# plt.draw() +# axs[1].set_title('Output spike train') +# plt.show() + +print(spikes_traces) +plt.plot([.0,10.],[0.,36.]) +plt.plot(spikes_traces) +plt.show() diff --git a/coursera_compneuro/week6/w6q9_var1.py b/coursera_compneuro/week6/w6q9_var1.py new file mode 100644 index 0000000..b5ec13b --- /dev/null +++ b/coursera_compneuro/week6/w6q9_var1.py @@ -0,0 +1,37 @@ +#%% +import numpy as np +W= np.array([ + [0.6, 0.1, 0.1, 0.1, 0.1], + [0.1, 0.6, 0.1, 0.1, 0.1], + [0.1, 0.1, 0.6, 0.1, 0.1], + [0.1, 0.1, 0.1, 0.6, 0.1], + [0.1, 0.1, 0.1, 0.1, 0.6]]) + +u = np.array([.6, .5, .6, .2, .1]) + +# var1 +M = np.array([ + [-.25, 0, .25, .25, 0], + [0, -.25, 0, .25, .25], + [.25, 0, -.25, 0, .25], + [.25, .25, 0, -.25, 0], + [0, .25, .25, 0, -.25]]) + +# other variants (var4) +# M = M * 2 + +# v_ss = sigmal ((h . e_i) / 1-lambda_i) * e_i + +h = np.dot(W,u) + +evals, e = np.linalg.eigh(M) + +print(e[:,1]) + +v_ss = np.zeros_like(u).reshape(1,-1) +for i in range(M.shape[0]): + # print(evecs[i].reshape(1,-1).T) + coeff = np.dot(h.T, e[:,i]) / (1-evals[i]) + v_ss += coeff * e[:,i] + +print(v_ss.T) diff --git a/coursera_compneuro/week5/intfire.py b/coursera_compneuro/week5/intfire.py new file mode 100644 index 0000000..53863c8 --- /dev/null +++ b/coursera_compneuro/week5/intfire.py @@ -0,0 +1,55 @@ +# %% + +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 100000 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 200 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + + V_trace += [V] + +total_spikes = 0 +for v in V_trace: + if v == 50: + total_spikes += 1 + +print(total_spikes) +plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/intfire_noise.py b/coursera_compneuro/week5/intfire_noise.py new file mode 100644 index 0000000..6a763d8 --- /dev/null +++ b/coursera_compneuro/week5/intfire_noise.py @@ -0,0 +1,61 @@ +# %% +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 1 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 2000 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold +spiketimes = [] # list of spike times + +# input current +noiseamp = 1 # amplitude of added noise +I += noiseamp*np.random.normal(0, 1, (tstop,)) # nA; Gaussian noise + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I[t]/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + spiketimes += [t] + V_trace += [V] + +isi = np.diff(spiketimes) + +import seaborn as sns + +sns.histplot(isi,kde=True) + +# plt.hist(isi) + +# plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/membrane.py b/coursera_compneuro/week5/membrane.py new file mode 100644 index 0000000..1037fa7 --- /dev/null +++ b/coursera_compneuro/week5/membrane.py @@ -0,0 +1,64 @@ +#%% + +from __future__ import print_function +""" +Created on Wed Apr 22 15:53:00 2015 + +Charging and discharging curves for passive membrane patch +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + +# input current +I = 10 # nA + +# capacitance and leak resistance + +C = 0.1 # nF +R = 100 # M ohms +tau = R*C # = 0.1*100 nF-Mohms = 100*100 pF Mohms = 10 ms +print('C = %.3f nF' % C) +print('R = %.3f M ohms' % R) +print('tau = %.3f ms' % tau) +print('(Theoretical)') + +# membrane potential equation dV/dt = - V/RC + I/C + +tstop = 150 # ms + +V_inf = I*R # peak V (in mV) +tau = 0 # experimental (ms) + +h = 0.2 # ms (step size) + +V = 0 # mV +V_trace = [V] # mV + +for t in np.arange(h, tstop, h): + + # Euler method: V(t+h) = V(t) + h*dV/dt + V = V +h*(- (V/(R*C)) + (I/C)) + + # Verify membrane time constant + if (not tau and (V > 0.6321*V_inf)): + tau = t + print('tau = %.3f ms' % tau) + print('(Experimental)') + + + # Stop current injection + # if t >= 0.6*tstop: + # I = 0 + + V_trace += [V] + if t % 10 == 0: + plt.plot(np.arange(0,t+h, h), V_trace, color='r') + plt.xlim(0, tstop) + plt.ylim(0, V_inf) + plt.draw() + +plt.show() diff --git a/coursera_compneuro/week6/alpha_neuron.py b/coursera_compneuro/week6/alpha_neuron.py new file mode 100644 index 0000000..c1283b4 --- /dev/null +++ b/coursera_compneuro/week6/alpha_neuron.py @@ -0,0 +1,120 @@ +# %% +""" +Created on Wed Apr 22 16:13:18 2015 + +Fire a neuron via alpha function synapse and random input spike train +R Rao 2007 + +translated to python by rkp 2015 +""" +from __future__ import print_function, division + +import time +import numpy as np +from numpy import concatenate as cc +import matplotlib.pyplot as plt + +np.random.seed(0) +# I & F implementation dV/dt = - V/RC + I/C +h = 1. # step size, Euler method, = dt ms +t_max= 200 # ms, simulation time period +tstop = int(t_max/h) # number of time steps +ref = 0 # refractory period counter + +# Generate random input spikes +# Note: This is not entirely realistic - no refractory period +# Also: if you change step size h, input spike train changes too... +thr = 0.9 # threshold for random spikes +spike_train = np.random.rand(tstop) > thr + +# alpha func synaptic conductance +t_a = 100 # Max duration of syn conductance +t_peak = 0.5 # ms +g_peak = 0.05 # nS (peak synaptic conductance) + +spikes_traces = [0] + +for t_peak in np.arange(0.5, 10.5, 0.5): + const = g_peak / (t_peak*np.exp(-1)); + t_vec = np.arange(0, t_a + h, h) + alpha_func = const * t_vec * (np.exp(-t_vec/t_peak)) + + # plt.plot(t_vec[:80], alpha_func[:80]) + # plt.xlabel('t (in ms)') + # plt.title('Alpha Function (Synaptic Conductance for Spike at t=0)') + # plt.draw() + # time.sleep(2) + + # capacitance and leak resistance + C = 0.5 # nF + R = 40 # M ohms +# print('C = {}'.format(C)) +# print('R = {}'.format(R)) + + # conductance and associated parameters to simulate spike rate adaptation + g_ad = 0 + G_inc = 1/h + tau_ad = 2 + + # Initialize basic parameters + E_leak = -60 # mV, equilibrium potential + E_syn = 0 # Excitatory synapse (why is this excitatory?) + g_syn = 0 # Current syn conductance + V_th = -40 # spike threshold mV + V_spike = 50 # spike value mV + ref_max = 4/h # Starting value of ref period counter + t_list = np.array([], dtype=int) + V = E_leak + V_trace = [V] + t_trace = [0] + +# fig, axs = plt.subplots(2, 1) +# axs[0].plot(np.arange(0,t_max,h), spike_train) +# axs[0].set_title('Input spike train') + + for t in range(tstop): + + # Compute input + if spike_train[t]: # check for input spike + t_list = cc([t_list, [1]]) + + # Calculate synaptic current due to current and past input spikes + g_syn = np.sum(alpha_func[t_list]) + I_syn = g_syn*(E_syn - V) + + # Update spike times + if np.any(t_list): + t_list = t_list + 1 + if t_list[0] == t_a: # Reached max duration of syn conductance + t_list = t_list[1:] + + # Compute membrane voltage + # Euler method: V(t+h) = V(t) + h*dV/dt + if not ref: + V = V + h*(-((V-E_leak)*(1+R*g_ad)/(R*C)) + (I_syn/C)) + g_ad = g_ad + h*(-g_ad/tau_ad) # spike rate adaptation + else: + ref -= 1 + V = V_th - 10 # reset voltage after spike + g_ad = 0 + + # Generate spike + if (V > V_th) and not ref: + V = V_spike + ref = ref_max + g_ad = g_ad + G_inc + + V_trace += [V] + t_trace += [t*h] + + spikes_traces += [np.count_nonzero(np.array(V_trace) == V_spike)] + +# axs[1].plot(t_trace,V_trace) +# plt.draw() +# axs[1].set_title('Output spike train') +# plt.show() + +print(spikes_traces) +plt.plot([.0,10.],[0.,36.]) +plt.plot(spikes_traces) +plt.show() diff --git a/coursera_compneuro/week6/w6q9_var1.py b/coursera_compneuro/week6/w6q9_var1.py new file mode 100644 index 0000000..b5ec13b --- /dev/null +++ b/coursera_compneuro/week6/w6q9_var1.py @@ -0,0 +1,37 @@ +#%% +import numpy as np +W= np.array([ + [0.6, 0.1, 0.1, 0.1, 0.1], + [0.1, 0.6, 0.1, 0.1, 0.1], + [0.1, 0.1, 0.6, 0.1, 0.1], + [0.1, 0.1, 0.1, 0.6, 0.1], + [0.1, 0.1, 0.1, 0.1, 0.6]]) + +u = np.array([.6, .5, .6, .2, .1]) + +# var1 +M = np.array([ + [-.25, 0, .25, .25, 0], + [0, -.25, 0, .25, .25], + [.25, 0, -.25, 0, .25], + [.25, .25, 0, -.25, 0], + [0, .25, .25, 0, -.25]]) + +# other variants (var4) +# M = M * 2 + +# v_ss = sigmal ((h . e_i) / 1-lambda_i) * e_i + +h = np.dot(W,u) + +evals, e = np.linalg.eigh(M) + +print(e[:,1]) + +v_ss = np.zeros_like(u).reshape(1,-1) +for i in range(M.shape[0]): + # print(evecs[i].reshape(1,-1).T) + coeff = np.dot(h.T, e[:,i]) / (1-evals[i]) + v_ss += coeff * e[:,i] + +print(v_ss.T) diff --git a/coursera_compneuro/week7/c10p1.pickle b/coursera_compneuro/week7/c10p1.pickle new file mode 100644 index 0000000..a196e6c --- /dev/null +++ b/coursera_compneuro/week7/c10p1.pickle Binary files differ diff --git a/coursera_compneuro/week5/intfire.py b/coursera_compneuro/week5/intfire.py new file mode 100644 index 0000000..53863c8 --- /dev/null +++ b/coursera_compneuro/week5/intfire.py @@ -0,0 +1,55 @@ +# %% + +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 100000 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 200 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + + V_trace += [V] + +total_spikes = 0 +for v in V_trace: + if v == 50: + total_spikes += 1 + +print(total_spikes) +plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/intfire_noise.py b/coursera_compneuro/week5/intfire_noise.py new file mode 100644 index 0000000..6a763d8 --- /dev/null +++ b/coursera_compneuro/week5/intfire_noise.py @@ -0,0 +1,61 @@ +# %% +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 1 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 2000 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold +spiketimes = [] # list of spike times + +# input current +noiseamp = 1 # amplitude of added noise +I += noiseamp*np.random.normal(0, 1, (tstop,)) # nA; Gaussian noise + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I[t]/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + spiketimes += [t] + V_trace += [V] + +isi = np.diff(spiketimes) + +import seaborn as sns + +sns.histplot(isi,kde=True) + +# plt.hist(isi) + +# plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/membrane.py b/coursera_compneuro/week5/membrane.py new file mode 100644 index 0000000..1037fa7 --- /dev/null +++ b/coursera_compneuro/week5/membrane.py @@ -0,0 +1,64 @@ +#%% + +from __future__ import print_function +""" +Created on Wed Apr 22 15:53:00 2015 + +Charging and discharging curves for passive membrane patch +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + +# input current +I = 10 # nA + +# capacitance and leak resistance + +C = 0.1 # nF +R = 100 # M ohms +tau = R*C # = 0.1*100 nF-Mohms = 100*100 pF Mohms = 10 ms +print('C = %.3f nF' % C) +print('R = %.3f M ohms' % R) +print('tau = %.3f ms' % tau) +print('(Theoretical)') + +# membrane potential equation dV/dt = - V/RC + I/C + +tstop = 150 # ms + +V_inf = I*R # peak V (in mV) +tau = 0 # experimental (ms) + +h = 0.2 # ms (step size) + +V = 0 # mV +V_trace = [V] # mV + +for t in np.arange(h, tstop, h): + + # Euler method: V(t+h) = V(t) + h*dV/dt + V = V +h*(- (V/(R*C)) + (I/C)) + + # Verify membrane time constant + if (not tau and (V > 0.6321*V_inf)): + tau = t + print('tau = %.3f ms' % tau) + print('(Experimental)') + + + # Stop current injection + # if t >= 0.6*tstop: + # I = 0 + + V_trace += [V] + if t % 10 == 0: + plt.plot(np.arange(0,t+h, h), V_trace, color='r') + plt.xlim(0, tstop) + plt.ylim(0, V_inf) + plt.draw() + +plt.show() diff --git a/coursera_compneuro/week6/alpha_neuron.py b/coursera_compneuro/week6/alpha_neuron.py new file mode 100644 index 0000000..c1283b4 --- /dev/null +++ b/coursera_compneuro/week6/alpha_neuron.py @@ -0,0 +1,120 @@ +# %% +""" +Created on Wed Apr 22 16:13:18 2015 + +Fire a neuron via alpha function synapse and random input spike train +R Rao 2007 + +translated to python by rkp 2015 +""" +from __future__ import print_function, division + +import time +import numpy as np +from numpy import concatenate as cc +import matplotlib.pyplot as plt + +np.random.seed(0) +# I & F implementation dV/dt = - V/RC + I/C +h = 1. # step size, Euler method, = dt ms +t_max= 200 # ms, simulation time period +tstop = int(t_max/h) # number of time steps +ref = 0 # refractory period counter + +# Generate random input spikes +# Note: This is not entirely realistic - no refractory period +# Also: if you change step size h, input spike train changes too... +thr = 0.9 # threshold for random spikes +spike_train = np.random.rand(tstop) > thr + +# alpha func synaptic conductance +t_a = 100 # Max duration of syn conductance +t_peak = 0.5 # ms +g_peak = 0.05 # nS (peak synaptic conductance) + +spikes_traces = [0] + +for t_peak in np.arange(0.5, 10.5, 0.5): + const = g_peak / (t_peak*np.exp(-1)); + t_vec = np.arange(0, t_a + h, h) + alpha_func = const * t_vec * (np.exp(-t_vec/t_peak)) + + # plt.plot(t_vec[:80], alpha_func[:80]) + # plt.xlabel('t (in ms)') + # plt.title('Alpha Function (Synaptic Conductance for Spike at t=0)') + # plt.draw() + # time.sleep(2) + + # capacitance and leak resistance + C = 0.5 # nF + R = 40 # M ohms +# print('C = {}'.format(C)) +# print('R = {}'.format(R)) + + # conductance and associated parameters to simulate spike rate adaptation + g_ad = 0 + G_inc = 1/h + tau_ad = 2 + + # Initialize basic parameters + E_leak = -60 # mV, equilibrium potential + E_syn = 0 # Excitatory synapse (why is this excitatory?) + g_syn = 0 # Current syn conductance + V_th = -40 # spike threshold mV + V_spike = 50 # spike value mV + ref_max = 4/h # Starting value of ref period counter + t_list = np.array([], dtype=int) + V = E_leak + V_trace = [V] + t_trace = [0] + +# fig, axs = plt.subplots(2, 1) +# axs[0].plot(np.arange(0,t_max,h), spike_train) +# axs[0].set_title('Input spike train') + + for t in range(tstop): + + # Compute input + if spike_train[t]: # check for input spike + t_list = cc([t_list, [1]]) + + # Calculate synaptic current due to current and past input spikes + g_syn = np.sum(alpha_func[t_list]) + I_syn = g_syn*(E_syn - V) + + # Update spike times + if np.any(t_list): + t_list = t_list + 1 + if t_list[0] == t_a: # Reached max duration of syn conductance + t_list = t_list[1:] + + # Compute membrane voltage + # Euler method: V(t+h) = V(t) + h*dV/dt + if not ref: + V = V + h*(-((V-E_leak)*(1+R*g_ad)/(R*C)) + (I_syn/C)) + g_ad = g_ad + h*(-g_ad/tau_ad) # spike rate adaptation + else: + ref -= 1 + V = V_th - 10 # reset voltage after spike + g_ad = 0 + + # Generate spike + if (V > V_th) and not ref: + V = V_spike + ref = ref_max + g_ad = g_ad + G_inc + + V_trace += [V] + t_trace += [t*h] + + spikes_traces += [np.count_nonzero(np.array(V_trace) == V_spike)] + +# axs[1].plot(t_trace,V_trace) +# plt.draw() +# axs[1].set_title('Output spike train') +# plt.show() + +print(spikes_traces) +plt.plot([.0,10.],[0.,36.]) +plt.plot(spikes_traces) +plt.show() diff --git a/coursera_compneuro/week6/w6q9_var1.py b/coursera_compneuro/week6/w6q9_var1.py new file mode 100644 index 0000000..b5ec13b --- /dev/null +++ b/coursera_compneuro/week6/w6q9_var1.py @@ -0,0 +1,37 @@ +#%% +import numpy as np +W= np.array([ + [0.6, 0.1, 0.1, 0.1, 0.1], + [0.1, 0.6, 0.1, 0.1, 0.1], + [0.1, 0.1, 0.6, 0.1, 0.1], + [0.1, 0.1, 0.1, 0.6, 0.1], + [0.1, 0.1, 0.1, 0.1, 0.6]]) + +u = np.array([.6, .5, .6, .2, .1]) + +# var1 +M = np.array([ + [-.25, 0, .25, .25, 0], + [0, -.25, 0, .25, .25], + [.25, 0, -.25, 0, .25], + [.25, .25, 0, -.25, 0], + [0, .25, .25, 0, -.25]]) + +# other variants (var4) +# M = M * 2 + +# v_ss = sigmal ((h . e_i) / 1-lambda_i) * e_i + +h = np.dot(W,u) + +evals, e = np.linalg.eigh(M) + +print(e[:,1]) + +v_ss = np.zeros_like(u).reshape(1,-1) +for i in range(M.shape[0]): + # print(evecs[i].reshape(1,-1).T) + coeff = np.dot(h.T, e[:,i]) / (1-evals[i]) + v_ss += coeff * e[:,i] + +print(v_ss.T) diff --git a/coursera_compneuro/week7/c10p1.pickle b/coursera_compneuro/week7/c10p1.pickle new file mode 100644 index 0000000..a196e6c --- /dev/null +++ b/coursera_compneuro/week7/c10p1.pickle Binary files differ diff --git a/coursera_compneuro/week7/w7q1.py b/coursera_compneuro/week7/w7q1.py new file mode 100644 index 0000000..dfcff7e --- /dev/null +++ b/coursera_compneuro/week7/w7q1.py @@ -0,0 +1,10 @@ + +#%% +import numpy as np + +# varient 3 +Q=np.array([0.2, 0.1, 0.1, 0.15 ]).reshape(2,2) + +print(Q) +ev, e = np.linalg.eig(Q) +e[:,0] * -2 diff --git a/coursera_compneuro/week5/intfire.py b/coursera_compneuro/week5/intfire.py new file mode 100644 index 0000000..53863c8 --- /dev/null +++ b/coursera_compneuro/week5/intfire.py @@ -0,0 +1,55 @@ +# %% + +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 100000 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 200 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + + V_trace += [V] + +total_spikes = 0 +for v in V_trace: + if v == 50: + total_spikes += 1 + +print(total_spikes) +plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/intfire_noise.py b/coursera_compneuro/week5/intfire_noise.py new file mode 100644 index 0000000..6a763d8 --- /dev/null +++ b/coursera_compneuro/week5/intfire_noise.py @@ -0,0 +1,61 @@ +# %% +from __future__ import print_function +""" +Created on Wed Apr 22 16:02:53 2015 + +Basic integrate-and-fire neuron +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + + +# input current +I = 1 # nA + +# capacitance and leak resistance +C = 1 # nF +R = 40 # M ohms + +# I & F implementation dV/dt = - V/RC + I/C +# Using h = 1 ms step size, Euler method + +V = 0 +tstop = 2000 +abs_ref = 5 # absolute refractory period +ref = 0 # absolute refractory period counter +V_trace = [] # voltage trace for plotting +V_th = 10 # spike threshold +spiketimes = [] # list of spike times + +# input current +noiseamp = 1 # amplitude of added noise +I += noiseamp*np.random.normal(0, 1, (tstop,)) # nA; Gaussian noise + +for t in range(tstop): + + if not ref: + V = V - (V/(R*C)) + (I[t]/C) + else: + ref -= 1 + V = 0.2 * V_th # reset voltage + + if V > V_th: + V = 50 # emit spike + ref = abs_ref # set refractory counter + spiketimes += [t] + V_trace += [V] + +isi = np.diff(spiketimes) + +import seaborn as sns + +sns.histplot(isi,kde=True) + +# plt.hist(isi) + +# plt.plot(V_trace) +plt.show() diff --git a/coursera_compneuro/week5/membrane.py b/coursera_compneuro/week5/membrane.py new file mode 100644 index 0000000..1037fa7 --- /dev/null +++ b/coursera_compneuro/week5/membrane.py @@ -0,0 +1,64 @@ +#%% + +from __future__ import print_function +""" +Created on Wed Apr 22 15:53:00 2015 + +Charging and discharging curves for passive membrane patch +R Rao 2007 + +translated to Python by rkp 2015 +""" + +import numpy as np +import matplotlib.pyplot as plt + +# input current +I = 10 # nA + +# capacitance and leak resistance + +C = 0.1 # nF +R = 100 # M ohms +tau = R*C # = 0.1*100 nF-Mohms = 100*100 pF Mohms = 10 ms +print('C = %.3f nF' % C) +print('R = %.3f M ohms' % R) +print('tau = %.3f ms' % tau) +print('(Theoretical)') + +# membrane potential equation dV/dt = - V/RC + I/C + +tstop = 150 # ms + +V_inf = I*R # peak V (in mV) +tau = 0 # experimental (ms) + +h = 0.2 # ms (step size) + +V = 0 # mV +V_trace = [V] # mV + +for t in np.arange(h, tstop, h): + + # Euler method: V(t+h) = V(t) + h*dV/dt + V = V +h*(- (V/(R*C)) + (I/C)) + + # Verify membrane time constant + if (not tau and (V > 0.6321*V_inf)): + tau = t + print('tau = %.3f ms' % tau) + print('(Experimental)') + + + # Stop current injection + # if t >= 0.6*tstop: + # I = 0 + + V_trace += [V] + if t % 10 == 0: + plt.plot(np.arange(0,t+h, h), V_trace, color='r') + plt.xlim(0, tstop) + plt.ylim(0, V_inf) + plt.draw() + +plt.show() diff --git a/coursera_compneuro/week6/alpha_neuron.py b/coursera_compneuro/week6/alpha_neuron.py new file mode 100644 index 0000000..c1283b4 --- /dev/null +++ b/coursera_compneuro/week6/alpha_neuron.py @@ -0,0 +1,120 @@ +# %% +""" +Created on Wed Apr 22 16:13:18 2015 + +Fire a neuron via alpha function synapse and random input spike train +R Rao 2007 + +translated to python by rkp 2015 +""" +from __future__ import print_function, division + +import time +import numpy as np +from numpy import concatenate as cc +import matplotlib.pyplot as plt + +np.random.seed(0) +# I & F implementation dV/dt = - V/RC + I/C +h = 1. # step size, Euler method, = dt ms +t_max= 200 # ms, simulation time period +tstop = int(t_max/h) # number of time steps +ref = 0 # refractory period counter + +# Generate random input spikes +# Note: This is not entirely realistic - no refractory period +# Also: if you change step size h, input spike train changes too... +thr = 0.9 # threshold for random spikes +spike_train = np.random.rand(tstop) > thr + +# alpha func synaptic conductance +t_a = 100 # Max duration of syn conductance +t_peak = 0.5 # ms +g_peak = 0.05 # nS (peak synaptic conductance) + +spikes_traces = [0] + +for t_peak in np.arange(0.5, 10.5, 0.5): + const = g_peak / (t_peak*np.exp(-1)); + t_vec = np.arange(0, t_a + h, h) + alpha_func = const * t_vec * (np.exp(-t_vec/t_peak)) + + # plt.plot(t_vec[:80], alpha_func[:80]) + # plt.xlabel('t (in ms)') + # plt.title('Alpha Function (Synaptic Conductance for Spike at t=0)') + # plt.draw() + # time.sleep(2) + + # capacitance and leak resistance + C = 0.5 # nF + R = 40 # M ohms +# print('C = {}'.format(C)) +# print('R = {}'.format(R)) + + # conductance and associated parameters to simulate spike rate adaptation + g_ad = 0 + G_inc = 1/h + tau_ad = 2 + + # Initialize basic parameters + E_leak = -60 # mV, equilibrium potential + E_syn = 0 # Excitatory synapse (why is this excitatory?) + g_syn = 0 # Current syn conductance + V_th = -40 # spike threshold mV + V_spike = 50 # spike value mV + ref_max = 4/h # Starting value of ref period counter + t_list = np.array([], dtype=int) + V = E_leak + V_trace = [V] + t_trace = [0] + +# fig, axs = plt.subplots(2, 1) +# axs[0].plot(np.arange(0,t_max,h), spike_train) +# axs[0].set_title('Input spike train') + + for t in range(tstop): + + # Compute input + if spike_train[t]: # check for input spike + t_list = cc([t_list, [1]]) + + # Calculate synaptic current due to current and past input spikes + g_syn = np.sum(alpha_func[t_list]) + I_syn = g_syn*(E_syn - V) + + # Update spike times + if np.any(t_list): + t_list = t_list + 1 + if t_list[0] == t_a: # Reached max duration of syn conductance + t_list = t_list[1:] + + # Compute membrane voltage + # Euler method: V(t+h) = V(t) + h*dV/dt + if not ref: + V = V + h*(-((V-E_leak)*(1+R*g_ad)/(R*C)) + (I_syn/C)) + g_ad = g_ad + h*(-g_ad/tau_ad) # spike rate adaptation + else: + ref -= 1 + V = V_th - 10 # reset voltage after spike + g_ad = 0 + + # Generate spike + if (V > V_th) and not ref: + V = V_spike + ref = ref_max + g_ad = g_ad + G_inc + + V_trace += [V] + t_trace += [t*h] + + spikes_traces += [np.count_nonzero(np.array(V_trace) == V_spike)] + +# axs[1].plot(t_trace,V_trace) +# plt.draw() +# axs[1].set_title('Output spike train') +# plt.show() + +print(spikes_traces) +plt.plot([.0,10.],[0.,36.]) +plt.plot(spikes_traces) +plt.show() diff --git a/coursera_compneuro/week6/w6q9_var1.py b/coursera_compneuro/week6/w6q9_var1.py new file mode 100644 index 0000000..b5ec13b --- /dev/null +++ b/coursera_compneuro/week6/w6q9_var1.py @@ -0,0 +1,37 @@ +#%% +import numpy as np +W= np.array([ + [0.6, 0.1, 0.1, 0.1, 0.1], + [0.1, 0.6, 0.1, 0.1, 0.1], + [0.1, 0.1, 0.6, 0.1, 0.1], + [0.1, 0.1, 0.1, 0.6, 0.1], + [0.1, 0.1, 0.1, 0.1, 0.6]]) + +u = np.array([.6, .5, .6, .2, .1]) + +# var1 +M = np.array([ + [-.25, 0, .25, .25, 0], + [0, -.25, 0, .25, .25], + [.25, 0, -.25, 0, .25], + [.25, .25, 0, -.25, 0], + [0, .25, .25, 0, -.25]]) + +# other variants (var4) +# M = M * 2 + +# v_ss = sigmal ((h . e_i) / 1-lambda_i) * e_i + +h = np.dot(W,u) + +evals, e = np.linalg.eigh(M) + +print(e[:,1]) + +v_ss = np.zeros_like(u).reshape(1,-1) +for i in range(M.shape[0]): + # print(evecs[i].reshape(1,-1).T) + coeff = np.dot(h.T, e[:,i]) / (1-evals[i]) + v_ss += coeff * e[:,i] + +print(v_ss.T) diff --git a/coursera_compneuro/week7/c10p1.pickle b/coursera_compneuro/week7/c10p1.pickle new file mode 100644 index 0000000..a196e6c --- /dev/null +++ b/coursera_compneuro/week7/c10p1.pickle Binary files differ diff --git a/coursera_compneuro/week7/w7q1.py b/coursera_compneuro/week7/w7q1.py new file mode 100644 index 0000000..dfcff7e --- /dev/null +++ b/coursera_compneuro/week7/w7q1.py @@ -0,0 +1,10 @@ + +#%% +import numpy as np + +# varient 3 +Q=np.array([0.2, 0.1, 0.1, 0.15 ]).reshape(2,2) + +print(Q) +ev, e = np.linalg.eig(Q) +e[:,0] * -2 diff --git a/coursera_compneuro/week7/w7q7.py b/coursera_compneuro/week7/w7q7.py new file mode 100644 index 0000000..5982c0a --- /dev/null +++ b/coursera_compneuro/week7/w7q7.py @@ -0,0 +1,54 @@ +# %% + +import pickle +import matplotlib.pyplot as plt +import numpy as np + +with open('c10p1.pickle', 'rb') as f: + data = pickle.load(f) + +c10p1 = np.array(data['c10p1']) +c10p1 = c10p1 - c10p1.mean(axis=0) +eta = 1.0 +alpha = 1.0 +dt = 0.01 + +plt.scatter(c10p1[:,0],c10p1[:,1]) + +w = np.random.rand(1,2) +for i in range(100000): + u = c10p1[i%c10p1.shape[1],:] + v = np.dot(w,u) + dw = dt * eta * (v * u - alpha * v * v * w) + w = w + dw + +print (w) + +d = c10p1 + [6,5] + +# print('mean:', m) +plt.scatter(d[:,0],d[:,1]) + +w = np.random.rand(1,2) +for i in range(100000): + u = d[i%d.shape[1],:] + v = np.dot(w,u) + # Oja + dw = dt * eta * (v * u - alpha * v * v * w) + # Hebb + # dw = dt * eta * (v * u) + w = w + dw + +print (w) + +Q = c10p1.T @ c10p1 / 100 +evals, evecs = np.linalg.eig(Q) +pc_index = np.argmax(np.abs(evals)) +plt.plot([0,evecs[0,0]],[0,evecs[1,0]],c='green') + +Q = d.T @ d / 100 +evals, evecs = np.linalg.eig(Q) +plt.plot([0,evecs[0,0]],[0,evecs[1,0]],c='red') +print('>>>',evecs[:,0]) + +plt.show()