def convert_hhgate(gate): """Convert a MOOSE gate into GateHHRates in NeuroML""" hh_rates = neuroml.GateHHRates(id=gate.id_.value, name=gate.name) alpha = gate.tableA.copy() beta = gate.tableB - alpha vrange = np.linspace(gate.min, gate.max, len(alpha)) afn, ap = hhfit.find_ratefn(vrange, alpha) bfn, bp = hhfit.find_ratefn(vrange, beta) if afn is None: raise Exception('could not find a fitting function for `alpha`') if bfn is None: raise Exception('could not find a fitting function for `alpha`') afn_type = fn_rate_map[afn] afn_component_type = None if afn_type is None: afn_type, afn_component_type = define_component_type(afn) hh_rates.forward_rate = neuroml.HHRate(type=afn_type, midpoint='%gmV' % (ap[2]), scale='%gmV' % (ap[1]), rate='%gper_ms' % (ap[0])) bfn_type = fn_rate_map[bfn] bfn_component_type = None if bfn_type is None: bfn_type, bfn_component_type = define_component_type(bfn) hh_rates.reverse_rate = neuroml.HHRate(type=bfn_type, midpoint='%gmV' % (bp[2]), scale='%gmV' % (bp[1]), rate='%gper_ms' % (bp[0])) return hh_rates, afn_component_type, bfn_component_type
def test_sigmoid(self): print('Testing sigmoid') fn, params = hhfit.find_ratefn(self.v_array, self.sigmoid) print('Sigmoid params original:', self.p_sigmoid, 'detected:', params) pylab.plot(self.v_array, self.sigmoid, 'y-', self.v_array, hhfit.sigmoid(self.v_array, *self.p_sigmoid), 'b--', self.v_array, fn(self.v_array, *params), 'r-.') pylab.legend(('original sigmoid', 'computed', 'fitted %s' % (fn))) pylab.show() self.assertEqual(hhfit.sigmoid, fn) rms_error = np.sqrt(np.mean((self.sigmoid - fn(self.v_array, *params))**2)) self.assertAlmostEqual(rms_error/max(abs(self.sigmoid)), 0.0, places=3)
def test_dblexponential(self): print('Testing double exponential') fn, params = hhfit.find_ratefn(self.v_array, self.dblexp) fnval = fn(self.v_array, *params) pylab.plot(self.v_array, self.dblexp, 'y-', self.v_array, hhfit.double_exp(self.v_array, *self.p_dblexp), 'b--', self.v_array, fnval, 'r-.') pylab.legend(('original dblexp', 'computed', 'fitted %s' % (fn))) pylab.show() self.assertEqual(hhfit.double_exp, fn) rms_error = np.sqrt(np.mean((self.dblexp - fnval)**2)) print(params, rms_error) self.assertAlmostEqual(rms_error/max(self.dblexp), 0.0, places=3)
def test_dblexponential(self): print('Testing double exponential') fn, params = hhfit.find_ratefn(self.v_array, self.dblexp) fnval = fn(self.v_array, *params) plt.plot(self.v_array, self.dblexp, 'y-', self.v_array, hhfit.double_exp(self.v_array, *self.p_dblexp), 'b--', self.v_array, fnval, 'r-.') self.assertEqual(hhfit.double_exp, fn) rms_error = np.sqrt(np.mean((self.dblexp - fnval)**2)) print(params, rms_error) self.assertAlmostEqual(rms_error / max(self.dblexp), 0.0, places=3) plt.legend('Original dblexp %s, fitted %s' %(self.dblexp, fn)) out = "__test_dblexponential.png" plt.savefig(out) print('Plot is saved saved to %s' % out)
def test_linoid(self): print('Testing linoid') fn, params = hhfit.find_ratefn(self.v_array, self.linoid) if params is not None: print('Linoid params original:', self.p_linoid, 'detected:', params) self.assertEqual(hhfit.linoid, fn) fnval = fn(self.v_array, *params) rms_error = np.sqrt(np.mean((self.linoid - fnval)**2)) self.assertAlmostEqual(rms_error / max(self.linoid), 0.0, places=3) plt.plot(self.v_array, self.linoid, 'y-', self.v_array, hhfit.linoid(self.v_array, *self.p_linoid), 'b--', self.v_array, fn(self.v_array, *params), 'r-.') plt.legend('Original linoid %s fitted %s' % (self.p_linoid, fn)) out = "__test_linoid.png" plt.savefig(out) print('Plot is saved saved to %s' % out) else: print('Failed to find a suitable fit.')
def test_exponential(self): print('Testing exponential') fn, params = hhfit.find_ratefn(self.v_array, self.exp) print('Exponential params original:', self.p_exp, 'detected:', params) fnval = hhfit.exponential(self.v_array, *params) pylab.plot(self.v_array, self.exp, 'y-', self.v_array, hhfit.exponential(self.v_array, *self.p_exp), 'b--', self.v_array, fnval, 'r-.') pylab.legend(('original exp', 'computed', 'fitted %s' % (fn))) pylab.show() self.assertEqual(hhfit.exponential, fn) # The same exponential can be satisfied by an infinite number # of parameter values. Hence we cannot compare the parameters, # but only the fit rms_error = np.sqrt(np.sum((self.exp - fnval)**2)) # pylab.plot(self.v_array, self.exp, 'b-') # pylab.plot(self.v_array, fnval, 'r-.') # pylab.show() print(rms_error, rms_error/max(self.exp)) self.assertAlmostEqual(rms_error/max(self.exp), 0.0, places=3)
def test_exponential(self): print('Testing exponential') fn, params = hhfit.find_ratefn(self.v_array, self.exp) print('Exponential params original:', self.p_exp, 'detected:', params) if params is not None: # The `find_ratefn` might return a parameter array for different # function sometimes. exponential takes only upto 5 parameters. fnval = hhfit.exponential(self.v_array, *params[:4]) self.assertEqual(hhfit.exponential, fn) # The same exponential can be satisfied by an infinite number # of parameter values. Hence we cannot compare the parameters, # but only the fit rms_error = np.sqrt(np.sum((self.exp - fnval)**2)) print(rms_error, rms_error / max(self.exp)) self.assertAlmostEqual(rms_error / max(self.exp), 0.0, places=3) plt.plot(self.v_array, self.exp, 'y-', self.v_array, hhfit.exponential(self.v_array, *self.p_exp), 'b--', self.v_array, fnval, 'r-.') plt.legend('original exp %s fitted %s' % (self.p_exp, fn)) out = "__test_exponential.png" plt.savefig(out) print('Plot is saved saved to %s' % out) else: print("[INFO ] Failed find a suitable approximation...")