Source code for neurodynex.tools.InputFactory

import brian2 as b2
import numpy as np
import matplotlib.pyplot as plt
import math
import numpy


###############
# Input Currents
###############
[docs]def get_step_current(t_start, t_end, amplitude, unit_current=b2.uamp, unit_time=b2.us, append_zero=True): # creates a step current. assert isinstance(t_start, int), 't_start_ms must be of type int' assert isinstance(t_end, int), 't_end must be of type int' tmp_size = 1 + t_end # +1 for t=0 if append_zero: tmp_size += 1 tmp = np.zeros(tmp_size) * unit_current tmp[t_start: t_end + 1] = amplitude * unit_current curr = b2.TimedArray(tmp, dt=1. * unit_time) return curr
[docs]def get_ramp_current(t_start, amplitude_start, t_end, amplitude_end, unit_current=b2.uamp, unit_time=b2.us, append_zero=True): # creates a ramp current assert isinstance(t_start, int), 't_start_ms must be of type int' assert isinstance(t_end, int), 't_end must be of type int' tmp_size = 1 + t_end # +1 for t=0 if append_zero: tmp_size += 1 tmp = np.zeros(tmp_size) * unit_current if t_end > t_start: # if deltaT is zero, we return a zero current slope = (amplitude_end - amplitude_start) / float((t_end - t_start)) ramp = [amplitude_start + t * slope for t in range(0, (t_end - t_start) + 1)] tmp[t_start: t_end + 1] = ramp * unit_current curr = b2.TimedArray(tmp, dt=1. * unit_time) return curr
[docs]def get_sinusoidal_current(t_start, t_end, amplitude, frequency_Hz, direct_current, unit_current=b2.uamp, unit_time=b2.us, append_zero=True, phase_offset=0.): assert isinstance(t_start, int), 't_start_ms must be of type int' assert isinstance(t_end, int), 't_end must be of type int' tmp_size = 1 + t_end # +1 for t=0 if append_zero: tmp_size += 1 tmp = np.zeros(tmp_size) * unit_current if t_end > t_start: # if deltaT is zero, we return a zero current phi = range(0, (t_end - t_start) + 1) phi = phi * unit_time * b2.Hz * frequency_Hz phi = phi * 2. * math.pi + phase_offset c = numpy.sin(phi) c = (direct_current + c * amplitude) * unit_current tmp[t_start: t_end + 1] = c curr = b2.TimedArray(tmp, dt=1. * unit_time) return curr
############### # Spike trains: ###############
[docs]def plot_step_current_example(): current = get_step_current(10, 30, 5., unit_current=b2.uamp, unit_time=b2.ms, append_zero=False) data = current.values / b2.uamp plt.plot(data, LineWidth=3) plt.title("get_step_current(10, 30, 5, unit_current=uamp, unit_time=ms,append_zero=False)") plt.xlabel("index") plt.ylabel("value")
[docs]def plot_ramp_current_example(): current = get_ramp_current(10, 25, 20, 50, unit_current=b2.uamp, unit_time=b2.us, append_zero=True) data = current.values / b2.uamp plt.plot(data, LineWidth=3) plt.title("get_ramp_current(10, 25, 20, 50, unit_current=uamp, unit_time=us, append_zero=True)") plt.xlabel("index") plt.ylabel("value")
[docs]def plot_sinusoidal_current_example(): current = get_sinusoidal_current(100, 1100, 2, 2000, 1.5, unit_current=b2.uamp, unit_time=b2.us, append_zero=True, phase_offset=math.pi / 4) data = current.values / b2.uamp plt.plot(data, LineWidth=3) plt.title("get_sinusoidal_current(100, 1100, 2, 2000, unit_current=brian2.uamp, unit_time=brian2.us, \n " "append_zero=True, phase_offset=math.pi/4)") plt.xlabel("index") plt.ylabel("value")
if __name__ == '__main__': # plot examples plot_sinusoidal_current_example() plt.show() plot_step_current_example() plt.show() plot_ramp_current_example() plt.show()