def plot_activity(self): euler = EulerEstimator( derivatives=[self.dv_dt, self.dn_dt, self.dm_dt, self.dh_dt], point=(0, (self.v_0, self.n_0, self.m_0, self.h_0))) plt.plot([n / 2 for n in range(160)], [self.s_t(n / 2) for n in range(160)]) euler.plot([0, 80], step_size=0.02, file_name="photo.png")
def plot_activity(self): estimator = EulerEstimator( derivatives=[self.dV, self.dn_dt, self.dm_dt, self.dh_dt], point=(0, (0, self.n_0, self.m_0, self.h_0))) plt.plot([n / 2 for n in range(160)], [self.s_t(n / 2) for n in range(160)]) estimator.plot([0, 80], step_size=0.02, filename="hodgen_huxley.png")
class Neuron: def __init__(self, stimulus): self.stimulus = stimulus self.estimator = EulerEstimator(self.get_derivatives(), self.get_starting_point()) def plot_activity(self, filename): plt.plot([n / 2 for n in range(160)], [s_t(n / 2) for n in range(160)], zorder=1) self.estimator.plot([0, 80], step_size=0.02, filename=filename) def get_starting_point(self): x = 0.07 * (math.e**3 + 1) h_0 = x / (x + 1) n_0 = 1 / (1.25 * (math.e - 1) + 1) m_0 = 2.5 / (2.5 + 4 * (math.e**2.5 - 1)) V_0 = 0 return 0, (V_0, n_0, m_0, h_0) def get_derivatives(self): return [self.dV(), Neuron.dn, Neuron.dm, Neuron.dh] @staticmethod def a_n(t, x): return 0.01 * (10 - x[0]) / (math.exp(0.1 * (10 - x[0])) - 1) @staticmethod def b_n(t, x): return 0.125 * math.exp(-x[0] / 80) @staticmethod def a_m(t, x): return 0.1 * (25 - x[0]) / (math.exp(0.1 * (25 - x[0])) - 1) @staticmethod def b_m(t, x): return 4 * math.exp(-x[0] / 18) @staticmethod def a_h(t, x): return 0.07 * math.exp(-x[0] / 20) @staticmethod def b_h(t, x): return 1 / (math.exp(0.1 * (30 - x[0])) + 1) @staticmethod def dn(t, x): return Neuron.a_n(t, x) * (1 - x[1]) - Neuron.b_n(t, x) * x[1] @staticmethod def dm(t, x): return Neuron.a_m(t, x) * (1 - x[2]) - Neuron.b_m(t, x) * x[2] @staticmethod def dh(t, x): return Neuron.a_h(t, x) * (1 - x[3]) - Neuron.b_h(t, x) * x[3] C = 1 Vna = 115 Vk = -12 VL = 10.6 _gna = 120 _gk = 36 _gl = 0.3 @staticmethod def INa(t, x): return Neuron.gna(t, x) * (x[0] - Neuron.Vna) @staticmethod def gna(t, x): return Neuron._gna * x[2]**3 * x[3] @staticmethod def Ik(t, x): return Neuron.gk(t, x) * (x[0] - Neuron.Vk) @staticmethod def gk(t, x): return Neuron._gk * x[1]**4 @staticmethod def IL(t, x): return Neuron.gl(t, x) * (x[0] - Neuron.VL) @staticmethod def gl(t, x): return Neuron._gl def dV(self): return lambda t, x: 1 / Neuron.C * (self.stimulus(t) - Neuron.INa( t, x) - Neuron.Ik(t, x) - Neuron.IL(t, x))
def plot_activity(self): plt.plot([x/2 for x in range(160)], [self.s_t(n/2) for n in range(160)]) estimator = EulerEstimator(derivatives = {"dV":(self.dV), "dn_dt":(self.dn_dt), "dm_dt":(self.dm_dt), "dh_dt":(self.dh_dt)}, point = (0, (0, self.n_zero, self.m_zero, self.h_zero))) estimator.plot([0, 80], step_size=0.02,filename="plot.png")
import sys sys.path.append('src') from euler_estimator import EulerEstimator derivatives = { 'S': (lambda t, x: -0.01 * 0.03 * x['S'] * x['I']), 'I': (lambda t, x: 0.01 * 0.03 * x['S'] * x['I'] - 0.02 * x['I']), 'R': (lambda t, x: 0.02 * x['I']) } euler = EulerEstimator(derivatives) initial_values = {'S': 1000, 'I': 1, 'R': 0} initial_point = (0, initial_values) euler.plot(point=initial_point, step_size=0.05, num_steps=4000)
"suseptible": (lambda t, x: -0.001 * x["suseptible"] * x["infected"]), "infected": (lambda t, x: 0.001 * x["suseptible"] * x["infected"] - 0.05 * x["infected"]), "recovered": (lambda t, x: 0.05 * x["infected"]) }, point=(0, { "suseptible": 500, "infected": 5, "recovered": 0 })) print("Derivitive: " + str(euler.calc_derivative_at_point())) # print("Point: "+str(euler.point)) # #(0, (0, 0, 0)) # print("Derivitive: "+str(euler.calc_derivative_at_point())) # #[1, 0, 0] # euler.step_forward(0.1) # print("Point: "+str(euler.point)) # print("Derivitive: "+str(euler.calc_derivative_at_point())) # [1.1, 0.1, 0] # euler.step_forward(-0.5) # print("Point: "+str(euler.point)) # (-0.4, (-0.45, -0.05, 0)) # euler.go_to_input(5, step_size = 2) # print(euler.point) # (5, (10.88, 1.09, -9.58)) euler.plot([0, 50], 0.1)
elif t > 20 and t < 21: return 50 elif t > 30 and t < 40: return 50 elif t > 50 and t < 51: return 50 elif t > 53 and t < 54: return 50 elif t > 56 and t < 57: return 50 elif t > 59 and t < 60: return 50 elif t > 62 and t < 63: return 50 elif t > 65 and t < 66: return 50 return 0 neuron_0 = BiologicalNeuron(stimulus=electrode_voltage) neuron_1 = BiologicalNeuron() neuron_2 = BiologicalNeuron() neurons = [neuron_0, neuron_1, neuron_2] synapses = [(0, 1), (1, 2)] network = BiologicalNeuralNetwork(neurons, synapses) euler = EulerEstimator(derivatives=network.get_derivatives(), point=network.get_starting_point()) plt.plot([n / 2 for n in range(160)], [electrode_voltage(n / 2) for n in range(160)]) euler.plot([0, 80], step_size=0.001, filename="biological_neural_network")
import sys sys.path.append('src') from euler_estimator import EulerEstimator derivatives = {'deer': (lambda t,x: 0.6*x['deer']-0.05*x['wolves']*x['deer']),'wolves': (lambda t,x: -0.9*x['wolves']+0.02*x['wolves']*x['deer'])} euler = EulerEstimator(derivative = derivatives) initial_values = {'deer': 100, 'wolves': 10} initial_point = (0, initial_values) euler.plot(point=initial_point, step_size=0.001, num_steps=100000)
print("\n Testing point after first step") euler.step_forward(0.1) assert euler.point == (1.1, {'a':4.2}), 'Incorrect point after first step' print(" passed") print("\n Testing derivative after first step") assert euler.calc_derivative_at_point() == {'a':2.1},'Incorrect derivative after first step' print(" passed") print("\n Testing point after 2nd step") euler.step_forward(-0.5) assert (round(euler.point[0],3),{item:round(euler.point[1][item],3) for item in euler.point[1]})== (0.6, {'a':3.15}), 'Incorrect point after 2nd step' print(" passed") print("\n Testing go_to_input") euler.go_to_input(3, step_size = 0.5) assert (round(euler.point[0],3),{item:round(euler.point[1][item],3) for item in euler.point[1]}) == (3, {'a':9.229}), 'Incorrect final result' print(" passed") euler = EulerEstimator( derivatives = { 'susceptible': (lambda x: -0.0003*x['susceptible']*x['infected']), 'infected': (lambda x: 0.0003*x['susceptible']*x['infected'] - 0.02*x['infected']), 'recovered': (lambda x: 0.02*x['infected']) }, point = (0, {'susceptible': 1000, 'recovered': 0, 'infected': 1})) euler.plot([0,100], step_size = 0.001, filename = 'test_plot.png')
from euler_estimator import EulerEstimator import sys sys.path.append('src/python') euler = EulerEstimator([(lambda x, y: x + 1)], (1, [4])) euler.plot([-5, 5])
from euler_estimator import EulerEstimator euler = EulerEstimator(derivatives=[ (lambda t, x: -0.0003 * x[1] * x[0] + 0.01 * x[1] + 0.001), (lambda t, x: -0.02 * x[1] + 0.0003 * x[1] * x[0]), (lambda t, x: 0.01 * x[1]) ], point=(0, (1000, 1, 0))) euler.plot([0, 150], step_size=0.001, filename='plot.png')
print('passed') print('testing step forward...') point_2 = euler.step_forward(point=initial_point, step_size=0.1) assert point_2 == (0.1, {'A': 0.1, 'B': 0, 'C': 0}) print('passed') print('testing calc_derivative_at_point...') assert euler.calc_derivative_at_point(point_2) == {'A': 1.1, 'B': 0.1, 'C': 0} print('passed') print('testing step forward...') point_3 = euler.step_forward(point=point_2, step_size=-0.5) point_3 = (point_3[0], {key: round(value, 5) for key, value in point_3[1].items()}) assert point_3 == (-0.4, {'A': -0.45, 'B': -0.05, 'C': 0}) print('passed') #assertion error, need to round # print('testing calc_derivative_at_point...') # assert euler.calc_estimated_points(point=point_3, step_size=2, num_steps=3) == [ # (-0.4, {'A': -0.45, 'B': -0.05, 'C': 0}), # starting point # (1.6, {'A': 0.65, 'B': -1.05, 'C': -0.2}), # after 1st step # (3.6, {'A': 3.95, 'B': -1.85, 'C': -4.4}), # after 2nd step # (5.6, {'A': 13.85, 'B': 2.35, 'C': -11.8}) # after 3rd step # ] print('passed') euler.plot(initial_point, 0.01, 500)
def plot_activity(self): euler = EulerEstimator(self.derivatives, self.initial_positions) plt.plot([n / 2 for n in range(160)], [self.stimulus(n / 2) for n in range(160)]) euler.plot([0, 80], stepsize=0.02)
import sys sys.path.append('src') from euler_estimator import EulerEstimator derivatives = { 'S': (lambda t, x: -0.0003 * x['S'] * x['I']), 'I': (lambda t, x: 0.0003 * x['S'] * x['I'] - 0.02 * x['I']), 'R': (lambda t, x: 0.02 * x['I']) } euler = EulerEstimator(derivative=derivatives) initial_values = {'S': 1000, 'I': 1, 'R': 0} initial_point = (0, initial_values) euler.plot(point=initial_point, step_size=1, num_steps=300)
import sys sys.path.append('src/python') from euler_estimator import EulerEstimator euler = EulerEstimator([(lambda t, x: -0.0003 * x[0] * x[1]), (lambda t, x: -0.02 * x[1] + 0.0003 * x[0] * x[1]), (lambda t, x: 0.02 * x[1])], (0, [1000, 1, 0])) euler.plot([0, 350], stepsize=0.001, filename='assignment_50-2.png')
assert point_2 == (0.1, {'A': 0.1, 'B': 0, 'C': 0}), point_2 assert euler.calc_derivative_at_point(point_2) == { 'A': 1.1, 'B': 0.1, 'C': 0 }, euler.calc_derivative_at_point(point_2) point_3 = euler.step_forward(point_2, -0.5) assert point_3 == (-0.4, {'A': -0.45, 'B': -0.05, 'C': 0}), point_3 assert euler.calc_estimated_points(point_3, 2, 3) == [(-0.4, { 'A': -0.45, 'B': -0.05, 'C': 0.0 }), (1.6, { 'A': 0.65, 'B': -1.05, 'C': -0.2 }), (3.6, { 'A': 3.95, 'B': -1.85, 'C': -4.4 }), (5.6, { 'A': 13.85, 'B': 2.35, 'C': -11.8 })] euler.plot((0, {'A': 0, 'B': 0, 'C': 0}), 0.01, 5)
int_6 = t >= 56 and t <= 57 int_7 = t >= 59 and t <= 60 int_8 = t >= 62 and t <= 63 int_9 = t >= 65 and t <= 66 if int_1 or int_2 or int_3 or int_4 or int_5 or int_6 or int_7 or int_8 or int_9: return 150 return 0 ################################ ### input into EulerEstimator V_0 = 0 n_0 = alpha_n(0, {'V':V_0})/(alpha_n(0, {'V':V_0})+beta_n(0, {'V':V_0})) m_0 = alpha_m(0, {'V':V_0})/(alpha_m(0, {'V':V_0})+beta_m(0, {'V':V_0})) h_0 = alpha_h(0, {'V':V_0})/(alpha_h(0, {'V':V_0})+beta_h(0, {'V':V_0})) derivatives = { 'V': dV_dt, 'n': dn_dt, 'm': dm_dt, 'h': dh_dt, } euler = EulerEstimator(derivatives) initial_values = {'V':V_0, 'n':n_0, 'm':m_0, 'h':h_0} initial_point = (0, initial_values) euler.plot(point=initial_point, step_size=0.01, num_steps=8000, additional_functions={'stimulus':s})
from euler_estimator import EulerEstimator derivatives = [(lambda t, x: 0.6 * x[0] - 0.05 * x[0] * x[1]), (lambda t, x: -0.9 * x[1] + 0.02 * x[0] * x[1])] starting_point = (0, (100, 10)) estimator = EulerEstimator(derivatives, starting_point) estimator.plot([0, 100], step_size=0.001, filename="epic_assignment_48_plot.png")
assert euler.calc_derivative_at_point(point_2) == {'A': 1.1, 'B': 0.1, 'C': 0} point_3 = euler.step_forward(point=point_2, step_size=-0.5) assert point_3 == (-0.4, {'A': -0.45, 'B': -0.05, 'C': 0}) assert euler.calc_estimated_points(point=point_3, step_size=2, num_steps=3) == [ (-0.4, { 'A': -0.45, 'B': -0.05, 'C': 0 }), # starting point (1.6, { 'A': 0.65, 'B': -1.05, 'C': -0.2 }), # after 1st step (3.6, { 'A': 3.95, 'B': -1.85, 'C': -4.4 }), # after 2nd step (5.6, { 'A': 13.85, 'B': 2.35, 'C': -11.8 }) # after 3rd step ] euler.plot()
from euler_estimator import EulerEstimator derivatives = [ (lambda t, x: -0.0003 * x[0] * x[1]), (lambda t, x: 0.0003 * x[0] * x[1] - 0.02 * x[1] - 0.001 * x[1]), (lambda t, x: 0.02 * x[1]), (lambda t, x: 0.001 * x[1]) ] starting_point = (0, (1000, 1, 0, 0)) estimator = EulerEstimator(derivatives, starting_point) estimator.plot([0, 365], step_size=0.001, filename="quiz_4.png")
import sys sys.path.append('src/python') from euler_estimator import EulerEstimator euler = EulerEstimator([(lambda t, x: 0.6 * x[0] - 0.05 * x[0] * x[1]), (lambda t, x: -0.9 * x[1] + 0.02 * x[0] * x[1])], (0, [100, 10])) euler.plot([0, 100], stepsize=0.001, filename='assignment_48-1.png')
import sys sys.path.append('src') from euler_estimator import EulerEstimator derivatives = { 'D': (lambda t, x: 0.6 * x['D'] - 0.05 * x['D'] * x['W']), 'W': (lambda t, x: -0.9 * x['W'] + 0.02 * x['D'] * x['W']), } euler = EulerEstimator(derivatives=derivatives) initial_values = {'D': 100, 'W': 10} initial_point = (0, initial_values) euler.plot(initial_point, 0.001, 100000)
elif t > 20 and t < 21: return 150 elif t > 30 and t < 40: return 150 elif t > 50 and t < 51: return 150 elif t > 53 and t < 54: return 150 elif t > 56 and t < 57: return 150 elif t > 59 and t < 60: return 150 elif t > 62 and t < 63: return 150 elif t > 65 and t < 66: return 150 return 0 neuron_0 = BiologicalNeuron(stimulus=electrode_voltage) neuron_1 = BiologicalNeuron() neuron_2 = BiologicalNeuron() neurons = [neuron_0, neuron_1, neuron_2] synapses = [(0, 1), (1, 2)] network = BiologicalNeuralNetwork(neurons, synapses) euler = EulerEstimator(derivatives=network.get_derivatives(), point=network.get_starting_point()) plt.plot([n / 2 for n in range(160)], [electrode_voltage(n / 2) for n in range(160)]) euler.plot([0, 80], step_size=0.001, file_name="neural_network.png")
(0.1, {'1': 0.1, '2': 0, '3': 0})) do_assert("new derivative", euler.calc_derivative_at_point(), {'1': 1.1, '2': 0.1, '3': 0}) euler.step_forward(-0.5) do_assert("step forward 2", euler.point, (-0.4, {'1': -0.45, '2': -0.05, '3': 0})) euler.go_to_input(5, step_size=2) # notes to help you debug: # point: (-0.4, (-0.45, -0.05, 0)) # derivative: (0.55, -0.5, -0.1) # deltas: (2, (1.1, -1, -0.2)) # point: (1.6, (0.65, -1.05, -0.2)) # derivative: (1.65, -0.4, -2.1) # deltas: (2, (3.3, -0.8, 4.2)) # point: (3.6, (3.95, -1.85, 4)) # derivative: (4.95, 2.1, -3.7) # deltas: (1.4, (9.8, 4.2, -7.4)) do_assert("go to input", euler.point[1], {'1': 10.88, '2': 1.09, '3': -9.58}) euler.plot([-5, 5], step_size=0.1, filename='plot.png') print(cstring("&6All Tests Passed!"))
from euler_estimator import EulerEstimator derivatives = { 'susceptible': (lambda t, x: -0.0003 * x['susceptible'] * x['infected']), 'infected': (lambda t, x: 0.0003 * x['susceptible'] * x['infected'] - 0.02 * x['infected']), 'recovered': (lambda t, x: 0.02 * x['infected']) } starting_point = (0, {'susceptible': 1000, 'infected': 1, 'recovered': 0}) estimator = EulerEstimator(derivatives, starting_point) estimator.plot([0, 365], step_size=0.001, filename="epic_assignment_50_plot.png")