Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. from brian2 import *
  2.  
  3. from brian2.equations.equations import (DIFFERENTIAL_EQUATION, SingleEquation,
  4. PARAMETER, SUBEXPRESSION)
  5.  
  6.  
  7. def evaluate_rhs(eqs, values, namespace=None, level=0):
  8. """
  9. Evaluates the RHS of a system of differential equations for given state
  10. variable values. External constants can be provided via the namespace or
  11. will be taken from the local namespace.
  12.  
  13. This function could be used for example to find a resting state of the
  14. system, i.e. a fixed point where the RHS of all equations are approximately
  15. 0.
  16.  
  17. Parameters
  18. ----------
  19. eqs : `Equations`
  20. The equations
  21. values : dict-like
  22. Values for each of the state variables (differential equations and
  23. parameters).
  24.  
  25. Returns
  26. -------
  27. rhs : dict
  28. A dictionary with the names of all variables defined by differential
  29. equations as keys and the respective RHS of the equations as values.
  30. """
  31. # Make a new set of equations, where differential equations are replaced
  32. # by parameters, and a new subexpression defines their RHS.
  33. # E.g. for 'dv/dt = -v / tau : volt' use:
  34. # '''v : volt
  35. # RHS_v = -v / tau : volt'''
  36. new_equations = []
  37. for eq in eqs.values():
  38. if eq.type == DIFFERENTIAL_EQUATION:
  39. new_equations.append(SingleEquation(PARAMETER, eq.varname,
  40. dimensions=eq.dim,
  41. var_type=eq.var_type))
  42. new_equations.append(SingleEquation(SUBEXPRESSION, 'RHS_'+eq.varname,
  43. dimensions=eq.dim/second.dim,
  44. var_type=eq.var_type,
  45. expr=eq.expr))
  46. else:
  47. new_equations.append(eq)
  48.  
  49. if namespace is None:
  50. namespace = get_local_namespace(level+1)
  51.  
  52. # TODO: Hide this from standalone mode
  53. group = NeuronGroup(1, model=Equations(new_equations),
  54. codeobj_class=NumpyCodeObject,
  55. namespace=namespace)
  56.  
  57. # Set the values of the state variables/parameters
  58. group.set_states(values)
  59.  
  60. # Get the values of all RHS_... subexpressions
  61. states = ['RHS_' + name for name in eqs.diff_eq_names]
  62. return group.get_states(states)
  63.  
  64.  
  65. if __name__ == '__main__':
  66. # Parameters
  67. area = 20000 * umetre ** 2
  68. Cm = 1 * ufarad * cm ** -2 * area
  69. gl = 5e-5 * siemens * cm ** -2 * area
  70. El = -65 * mV
  71. EK = -90 * mV
  72. ENa = 50 * mV
  73. g_na = 100 * msiemens * cm ** -2 * area
  74. g_kd = 30 * msiemens * cm ** -2 * area
  75. VT = -63 * mV
  76. I = 0.01*nA
  77. eqs = Equations('''
  78. dv/dt = (gl*(El-v) - g_na*(m*m*m)*h*(v-ENa) - g_kd*(n*n*n*n)*(v-EK) + I)/Cm : volt
  79. dm/dt = 0.32*(mV**-1)*(13.*mV-v+VT)/
  80. (exp((13.*mV-v+VT)/(4.*mV))-1.)/ms*(1-m)-0.28*(mV**-1)*(v-VT-40.*mV)/
  81. (exp((v-VT-40.*mV)/(5.*mV))-1.)/ms*m : 1
  82. dn/dt = 0.032*(mV**-1)*(15.*mV-v+VT)/
  83. (exp((15.*mV-v+VT)/(5.*mV))-1.)/ms*(1.-n)-.5*exp((10.*mV-v+VT)/(40.*mV))/ms*n : 1
  84. dh/dt = 0.128*exp((17.*mV-v+VT)/(18.*mV))/ms*(1.-h)-4./(1+exp((40.*mV-v+VT)/(5.*mV)))/ms*h : 1
  85. ''')
  86.  
  87. # Find the resting state of this model
  88. def wrapper(args):
  89. rhs = evaluate_rhs(eqs, {'v': args[0]*volt,
  90. 'm': args[1],
  91. 'n': args[2],
  92. 'h': args[3]})
  93. return [float(rhs['RHS_v']),
  94. float(rhs['RHS_m']),
  95. float(rhs['RHS_n']),
  96. float(rhs['RHS_h'])]
  97. from scipy.optimize import root
  98. result = root(wrapper, x0=np.array([float(-70*mV), 0, 0, 0]))
  99.  
  100. # Simulate neuron and compare resting state to calculated resting state
  101. group = NeuronGroup(1, eqs, method='exponential_euler')
  102. group.v = -70*mV
  103. mon = StateMonitor(group, ['v', 'm', 'n', 'h'], record=0)
  104. run(200*ms)
  105.  
  106. fig, axes = plt.subplots(2, 2, sharex='all')
  107. axes[0, 0].plot(mon.t/ms, mon[0].v/mV, label='simulation')
  108. axes[0, 0].plot(200, result.x[0]*1000, 'rx', label='resting state')
  109. axes[0, 0].set(ylabel='v', xlabel='time (ms)')
  110. axes[0, 1].plot(mon.t/ms, mon[0].m, label='simulation')
  111. axes[0, 1].plot(200, result.x[1], 'rx', label='resting state')
  112. axes[0, 1].set(ylabel='m', xlabel='time (ms)')
  113. axes[1, 0].plot(mon.t/ms, mon[0].n, label='simulation')
  114. axes[1, 0].plot(200, result.x[2], 'rx', label='resting state')
  115. axes[1, 0].set(ylabel='n', xlabel='time (ms)')
  116. axes[1, 1].plot(mon.t/ms, mon[0].h, label='simulation')
  117. axes[1, 1].plot(200, result.x[3], 'rx', label='resting state')
  118. axes[1, 1].set(ylabel='h', xlabel='time (ms)')
  119. plt.tight_layout()
  120. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement