def __init__(self, n, seed, modelses, datagenhoc): self.n = n h.load_file(modelses) mrflist = h.List("MulRunFitter") mrf = mrflist.o(int(mrflist.count()) - 1) self.N = mrf.p.pf.generatorlist.o(0).gen.po h('objref nb') h.nb = mrf.p.pf.generatorlist.o(0).gen.po cvodewrap.fs.panel() self.trueParm = self.N.getParm() self.modelses = modelses self.seed = seed if n == 0: G = noisegen.Gen(self.N) G.reseed(seed) self.seed = seed self.Data = G.datasim() else: h.load_file(datagenhoc) tvec = h.Vector(self.N.Eve.collectionTimes) vec = h.ch3ssdata(n, seed, tvec, self.trueParm, self.N.rf.fitnesslist.o(0)) self.Data = [] for i in range(len(vec)): self.Data.append(numpy.matrix(vec[i])) if fitglobals.verbose: h.topology() ss = h.Vector() cvodewrap.states(ss) if fitglobals.verbose: ss.printf() self.N.overwrite(self.Data) self.H = numpy.matrix(self.Hessian())
def paramPanel(self): self.box = h.VBox() self.box.intercept(1) h.xpanel('') h.xlabel('Likelihood numerical parameters') h.xlabel(' Measurement noise') c = self.Eve.Obs.C for o in c: h.xvalue('sigma: ' + o.hpt.s(), (o, 'sigma'), 1) h.xlabel(' Process noise') h.xvalue('Injection interval', (self, 'inj_invl'), 1, self.inj_invl_changed) s = h.Vector() cvodewrap.states(s) sref = h.ref('') for i in range(len(s)): cvodewrap.statename(i, sref, 1) h.xvalue('Diffusion Coeff[%d,%d]: ' % (i, i) + sref[0], (self.processNoise[i], 'x'), 1, (self.fillPB, i)) h.xcheckbox('Fox & Lu Diffusion (for Hodgkin-Huxley)?', (self, 'hhB'), self.hhBButton) h.xvalue(' Fox & Lu: Number Na Channels', (self, 'nNa'), 1, self.hhBButton) h.xvalue(' Fox & Lu: Number K Channels', (self, 'nK'), 1, self.hhBButton) h.xlabel(' Initial Uncertainty') for i in range(len(s)): print i cvodewrap.statename(i, sref, 1) h.xvalue('Initial Stand Dev[%d]: ' % i + sref[0], (self.Sdiag[i], 'x'), 1, (self.fillS, i)) h.xbutton('Show state funnels', self.show_state_funnels) h.xpanel() self.box.intercept(0) self.box.map('Likelihood parameters')
def measJac(): x = h.Vector() cvodewrap.states(x) return [ numpy.matrix(x.x[0]), numpy.matrix([1, 0, 0, 0]), numpy.matrix(sigm) ]
def flow(self, x0, t1, x1): self.ss.restore(1) cvodewrap.yscatter(x0) cvodewrap.re_init() h.initPlot() h.continuerun(t1) # or cvodewrap.solve(t1) cvodewrap.states(x1) return x1
def inc_nsums(self): self.nsums += 1 s = h.Vector() cvodewrap.states(s) nstates = len(s) new = [] for i in range(nstates): new.append(xstruct()) self.sumto1.append(new) print 'nsums', self.nsums print 'sumto1', self.sumto1
def mean(self, time, state): # the observable (under zero noise, ie mean) ss = h.Vector() #save cvodewrap.states(ss) ds = h.Vector() cvodewrap.f(time, h.Vector(state), ds) #measurement goes here x = self.hpt.val # print 'ss[3]', ss[3] # print 'x', x cvodewrap.yscatter(ss) #restore return x
def noiseJac(self, injectionTimes): inject0 = injectionTimes[0] tFinal = injectionTimes[-1] if self.hhB: cvodewrap.states(self.states) h.rates_hh(self.states[0]) alpha_m = h.mtau_hh * h.minf_hh beta_m = h.mtau_hh * (1.0 - h.minf_hh) alpha_h = h.htau_hh * h.hinf_hh beta_h = h.htau_hh * (1.0 - h.hinf_hh) alpha_n = h.ntau_hh * h.ninf_hh beta_n = h.ntau_hh * (1.0 - h.ninf_hh) state_m = copy.deepcopy(self.states[1]) state_h = copy.deepcopy(self.states[2]) state_n = copy.deepcopy(self.states[3]) #if state_m < 0.0: # state_m = 0.0 #if state_h < 0.0: # state_h = 0.0 #if state_n < 0.0: # state_n = 0.0 #if state_m > 1.0: # state_m = 1.0 #if state_h > 1.0: # state_h = 1.0 #if state_n > 1.0: # state_n = 1.0 print 'alpha_h', alpha_h, 'beta_h', beta_h, 'state_h', state_h, 'nNa', self.nNa self.B[0, 0] = 0.0 self.B[1, 1] = math.sqrt( (alpha_m * (1.0 - state_m) + beta_m * state_m) / self.nNa) print 'Inside Sqrt', ((alpha_h * (1.0 - state_h) + beta_h * state_h) / self.nNa) self.B[2, 2] = math.sqrt( (alpha_h * (1.0 - state_h) + beta_h * state_h) / self.nNa) self.B[3, 3] = math.sqrt( (alpha_n * (1.0 - state_n) + beta_n * state_n) / self.nK) return self.scale * self.B * math.sqrt(tFinal - inject0) else: assert (tFinal - inject0 >= 0.0) assert (not numpy.isnan(self.scale)) R = self.scale * self.B * math.sqrt(tFinal - inject0) assert (not numpy.isnan(R).any()) return R
def sim(self): tMax = self.gettMax() tData = 0 tNoise = 0 h.stdinit() x = h.Vector() Data = [] while tData < tMax: noiseTimes = [] tData += self.Mdt if tData > tMax: tData = tMax while tNoise < min(tMax,tData): tNoise += self.Ndt noiseTimes.append(tNoise) if tNoise > tMax: tNoise = tMax h.continuerun(tNoise) cvodewrap.states(x) x.x[0] += self.sp*(self.evalW(tNoise) - self.evalW(tNoise-self.Ndt)) cvodewrap.yscatter(x) cvodewrap.re_init() Data.append([noiseTimes, x.x[0] + self.sm*self.evalM(tData)]) return Data
def constraintsPanel(self): self.box = h.HBox() self.box.intercept(1) self.box.ref(self) s = h.Vector() cvodewrap.states(s) nstates = len(s) sref = h.ref('') h.xpanel("") h.xlabel('0<=') for i in range(nstates): h.xcheckbox('', (self.geq0[i], 'x'), self.constraintsButton) h.xpanel() h.xpanel("") h.xlabel('>=1') for i in range(nstates): cvodewrap.statename(i, sref, 1) h.xcheckbox(sref[0], (self.leq1[i], 'x'), self.constraintsButton) h.xpanel() for j in range(self.nsums): h.xpanel("") h.xlabel("S%d" % j) for i in range(nstates): h.xcheckbox('', (self.sumto1[j][i], 'x'), self.constraintsButton) h.xpanel() h.xpanel("") h.xbutton("Add Sum-to-1", self.constraintsButton) h.xbutton("Remove Empty", self.constraintsButton) h.xbutton("Close", self.constraintsButton) h.xlabel('QP Solver:') h.xstatebutton('cvxopt', (self, 'cvxopt_sel'), self.constraintsButton) h.xstatebutton('custom', (self, 'custom_sel'), self.constraintsButton) h.xpanel() self.box.intercept(0) self.box.map("Constraints")
def printSomeInfo(): if fitglobals.verbose: h.topology() ss = h.Vector() cvodewrap.states(ss) if fitglobals.verbose: ss.printf()
def flowJac(noiseTimes): cvodewrap.states(x) t0 = h.t print 't0' print t0 ss = [None] * (len(noiseTimes) + 1) k = 0 ss[0] = h.SaveState() ss[0].save() cvodewrap.re_init() while k < len(noiseTimes): print noiseTimes[k] h.continuerun(noiseTimes[k]) ss[k + 1] = h.SaveState() ss[k + 1].save() cvodewrap.re_init() k += 1 cvodewrap.states(value) # Derivative with respect to state DFx = pylab.zeros((len(value), len(x))) k = 0 while k < len(x): ss[0].restore(1) cvodewrap.re_init() cvodewrap.states(x) temp = x[k] if abs(temp) > 1: dx = sqrtEps * abs(temp) else: dx = sqrtEps x.x[k] = temp + dx cvodewrap.yscatter(x) cvodewrap.re_init() dx = x[k] - temp # trick to reduce finite precision error h.continuerun(noiseTimes[len(noiseTimes) - 1]) cvodewrap.states(df) df.sub(value) df.div(dx) DFx[:, k] = numpy.array(df) k += 1 # Derivative with respect to noise DFn = pylab.zeros((len(value), len(noiseTimes))) noiseTimes.insert(0, t0) k = 0 while k < len(noiseTimes) - 1: ss[k + 1].restore(1) cvodewrap.re_init() if k < len(noiseTimes) - 2: dx = sqrtEps cvodewrap.states(x) x.x[0] += sigp * math.sqrt(noiseTimes[k + 1] - noiseTimes[k]) * dx cvodewrap.yscatter(x) cvodewrap.re_init() h.continuerun(noiseTimes[len(noiseTimes) - 1]) cvodewrap.states(df) df.sub(value) df.div(dx) DFn[:, k] = numpy.array(df) else: DFn[:, k] = numpy.array( [sigp * math.sqrt(noiseTimes[k + 1] - noiseTimes[k]), 0, 0, 0]) k += 1 return [numpy.matrix(value).T, numpy.matrix(DFx), numpy.matrix(DFn)]
class Flow(object): def __init__(self): self.ss = h.SaveState() self.ss.save() self.t0 = h.t def flow(self, x0, t1, x1): self.ss.restore(1) cvodewrap.yscatter(x0) cvodewrap.re_init() h.initPlot() h.continuerun(t1) # or cvodewrap.solve(t1) cvodewrap.states(x1) return x1 if __name__ == '__main__': h.load_file('hh.ses') h.Graph[0].exec_menu('Keep Lines') h.stdinit() h.continuerun(1.5) flow = Flow() x = h.Vector() x1 = h.Vector() cvodewrap.states(x) for i in range(5): print i, x x.x[0] += 5 flow.flow(x, h.tstop, x1)
def __init__(self, ho): #h.mulfit_after_quad_pycallback = self.after_quad pc = h.ParallelContext() nhost = int(pc.nhost_bbs()) if nhost > 1: fitglobals.verbose = 0 self.xlvec = h.Vector() self.ylvec = h.Vector() self.g = None self.rf = ho ol = [] vl = self.rf.yvarlist fl = self.rf.fitnesslist tlast = 0 self.n_chdat = 0 for i in range(len(vl)): self.n_chdat += fl.o(i).n_chdat tl = list(fl.o(i).xdat_) o = obs.NeuronObservable(vl.o(i), tl) o.sigma = 0.01 ol.append(o) if (tlast < tl[-1]): tlast = tl[-1] s = h.Vector() cvodewrap.active(1) cvodewrap.states(s) assert (len(s) > 0) assert (len(vl) > 0) self.covGrowthTime = 100 self.varTerm = 1 self.processNoise = [] self.Sdiag = [] Sto = sto.StochasticModel(len(s), tlast) for i in range(len(s)): self.Sdiag.append(WrappedVal(Sto.InitialCovSqrt[i, i])) for i in range(len(s)): self.processNoise.append(WrappedVal(Sto.B[i, i])) Obs = obs.ObservationModel(ol) self.Eve = eve.EventTable(Sto, Obs) self.hhB = self.Eve.Sto.hhB self.nNa = self.Eve.Sto.nNa self.nK = self.Eve.Sto.nK self.Sys = detsys.NeuronModel() self.inj_invl = 1.0 self.Eve.newInjectionInterval(self.inj_invl) # self.inj_invl_changed(Sys, P.tstop) # self.M = models.Model(Sys, Obs, P) self.Data = self.__data(fl, self.Eve) self.pf = self.getParmFitness() self.pf.verbose = fitglobals.verbose self.dlikedt = h.Vector() self.likefailed = False #CONSTRAINTS GUI INIT s = h.Vector() cvodewrap.states(s) nstates = len(s) self.geq0 = [] self.leq1 = [] self.sumto1 = [] self.cvxopt_sel = True self.custom_sel = True self.nsums = 2 for j in range(self.nsums): self.sumto1.append([]) for i in range(nstates): self.geq0.append(xstruct()) self.leq1.append(xstruct()) for j in range(self.nsums): self.sumto1[j].append(xstruct()) EKF.constraintsOn(self.geq0, self.leq1, self.sumto1)