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")
def __init__(self, stimulus): self.stimulus = stimulus self.estimator = EulerEstimator(self.get_derivatives(), self.get_starting_point())
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')
# # point, derivative, deltas # # (0.6, 3.15), 1.6, (0.5, 0.8) # # (1.1, 3.95), 2.1, (0.5, 1.05) # # (1.6, 5), 2.6, (0.5, 1.3) # # (2.1, 6.3), 3.1, (0.5, 1.55) # # (2.6, 7.85), 3.6, (0.4, 1.44) # print(euler.point) # # (3, 9.29) euler = EulerEstimator(derivatives={ "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))
return 50 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")
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")
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")
int_5 = t >= 53 and t <= 54 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 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')
# (3, 9.5) # after 4th step # ], euler.calc_estimated_points(point=(1,4), step_size=0.5, num_steps=4) # print('passed') # euler = EulerEstimator(derivative = lambda t: t+1) # euler.plot(point=(-5,10), step_size=0.1, num_steps=100) print('_____Assignment 40_____ ') derivatives = { 'A': (lambda t, x: x['A'] + 1), 'B': (lambda t, x: x['A'] + x['B']), 'C': (lambda t, x: 2 * x['B']) } euler = EulerEstimator(derivative=derivatives) initial_values = {'A': 0, 'B': 0, 'C': 0} initial_point = (0, initial_values) print('testing calc_derivative_at_point...') assert euler.calc_derivative_at_point(initial_point) == { 'A': 1, 'B': 0, 'C': 0 } 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})
from euler_estimator import EulerEstimator import sys sys.path.append('src/python') print('\nTesting... \n') euler = EulerEstimator({'a': (lambda x, y: x + 1)}, (1, {'b': 4})) print(" Testing EulerEstimator's calc_derivative()") assert euler.calc_derivative() == [ 2 ], "EulerEstimator's calc_derivative() was not right, it should be [2], but was {}".format( euler.calc_derivative()) print(" EulerEstimator's calc_derivative() Passed!!!\n") print(" Testing EulerEstimator's step_forward()") euler.step(0.1) assert tuple(euler.point) == ( 1.1, [4.2] ), "EulerEstimator's step_forward() was not right, it should be (1.1, [4.2]), but was {}".format( tuple(euler.point)) print(" EulerEstimator's step_forward() Passed!!!\n") print(" Testing EulerEstimator's calc_derivative()") assert euler.calc_derivative() == [ 2.1 ], "EulerEstimator's calc_derivative() was not right, it should be [2.1], but was {}".format( euler.calc_derivative()) print(" EulerEstimator's calc_derivative() Passed!!!\n") print(" Testing EulerEstimator's step_forward()")
import sys sys.path.append('src') from euler_estimator import EulerEstimator euler = EulerEstimator(derivative=(lambda x: x + 1), point=(1, 4)) assert euler.point == (1, 4), 'Wrong Point' assert euler.calc_derivative_at_point() == 2, 'Wrong Derivative' euler.step_forward(0.1) assert euler.point == (1.1, 4.2), 'Wrong Point' assert euler.calc_derivative_at_point() == 2.1, 'Wrong Derivative' euler.step_forward(-0.5) assert euler.point == (0.6, 3.15), 'wrong point' euler.go_to_input(3, 0.5) assert euler.point == (3, 9.29), 'Weong Point' print('ALL TESTS PASSED')
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)
#print("Testing estimated points") #assert euler.calc_estimated_points(point=(1,4), #step_size=0.5, num_steps=4) == [(1, 4), (1.5, 5),(2, 6.25), (2.5, 7.75), (3, 9.5)] #print("Passed") #euler.plot(point=(-5,10), step_size=0.1, num_steps=100) derivatives = { 'A': (lambda t, x: x['A'] + 1), 'B': (lambda t, x: x['A'] + x['B']), 'C': (lambda t, x: 2 * x['B']) } initial_values = {'A': 0, 'B': 0, 'C': 0} initial_point = (0, initial_values) euler = EulerEstimator(derivatives=derivatives) assert euler.calc_derivative_at_point(initial_point) == { 'A': 1, 'B': 0, 'C': 0 } point_2 = euler.step_forward(initial_point, 0.1) 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)
import sys sys.path.append('src') from euler_estimator import EulerEstimator euler = EulerEstimator(derivatives = {'a':(lambda x: x['a']/2)}, point = (1,{'a':4})) print("\n Testing starting point") assert euler.point == (1,{'a':4}), 'Incorrect Starting point' print(" passed") print("\n Testing starting derivative") assert euler.calc_derivative_at_point() == {'a':2}, "Expected: "+str({'a':2})+", Got: "+str(euler.calc_derivative_at_point()) print(" passed") 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'
assert euler.calc_estimated_points(point=(1,4), step_size=0.5, num_steps=4) == [ (1, 4), # starting point (1.5, 5), # after 1st step (2, 6.25), # after 2nd step (2.5, 7.75), # after 3rd step (3, 9.5) # after 4th step ]''' derivatives = { 'A': (lambda t, x: x['A'] + 1), 'B': (lambda t, x: x['A'] + x['B']), 'C': (lambda t, x: 2 * x['B']) } euler = EulerEstimator(derivatives=derivatives) initial_values = {'A': 0, 'B': 0, 'C': 0} initial_point = (0, initial_values) assert euler.calc_derivative_at_point(initial_point) == { 'A': 1, 'B': 0, 'C': 0 } point_2 = euler.step_forward(point=initial_point, step_size=0.1) assert point_2 == (0.1, {'A': 0.1, 'B': 0, 'C': 0}) assert euler.calc_derivative_at_point(point_2) == {'A': 1.1, 'B': 0.1, 'C': 0}
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)
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')
def dm_dt(t, x): V = x['V'] m = x['m'] return alpha_m(V) * (1 - m) - beta_m(V) * m def dh_dt(t, x): V = x['V'] h = x['h'] return alpha_h(V) * (1 - h) - beta_h(V) * h derivatives = {'V': dV_dt, 'n': dn_dt, 'm': dm_dt, 'h': dh_dt} euler = EulerEstimator(derivative=derivatives) initial_values = { 'V': 0, 'n': (alpha_n(0)) / (alpha_n(0) + beta_n(0)), 'm': (alpha_m(0)) / (alpha_m(0) + beta_m(0)), 'h': (alpha_h(0)) / (alpha_h(0) + beta_h(0)), } initial_point = (0, initial_values) def hodgkin_plot(euler, point, step_size, num_steps): plt.style.use('bmh') points = euler.calc_estimated_points(point, step_size, num_steps) legend = []
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)
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")
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))
sys.path.append("src") try: from otest import do_assert, cstring from euler_estimator import EulerEstimator except ImportError as e: print(e) def _round(t): return tuple(round(x, 5) for x in t) euler = EulerEstimator( derivatives={ '1': (lambda t, x: x['1'] + 1), '2': (lambda t, x: x['1'] + x['2']), '3': (lambda t, x: 2*x['2']) }, point=(0, {'1': 0, '2': 0, '3': 0}) ) do_assert("point", euler.point, (0, {'1': 0, '2': 0, '3': 0})) do_assert("calc derivative", euler.calc_derivative_at_point(), {'1': 1, '2': 0, '3': 0}) euler.step_forward(0.1) do_assert("step forward", euler.point, (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})
import sys sys.path.append('src') from euler_estimator import EulerEstimator print("Now graph") euler_graph = EulerEstimator(derivative=(lambda x: x + 1), point=(1, 4)) euler_graph.plot([-5, 5], step_size=0.1, filename='plot.png')