def runDoser( kkit, ht, plotPos, doseList, var = "Ca", title = "", BDNF = -1 ): ax = plotBoilerplate( char[plotPos], plotPos, title, xlabel = "[{}] ($\mu$M)".format( var ), ylabel = "aS6K ($\mu$M)" ) ax.set_xscale( "log" ) modelId = moose.loadModel( kkit, 'model', 'gsl' ) tmoose = time.time() if BDNF > 0.0: moose.element( '/model/kinetics/BDNF' ).concInit = BDNF * 1e-3 x, y = doseRespMoose( var, doseList ) tmoose = time.time() - tmoose ax.plot( x , y, label = "aS6K_vs_Ca_moose" ) mvec = np.array( y ) jsonDict = hillTau.loadHillTau( ht ) hillTau.scaleDict( jsonDict, hillTau.getQuantityScale( jsonDict ) ) model = hillTau.parseModel( jsonDict ) outputMolIndex = model.molInfo.get( "aS6K" ).index doseMolIndex = model.molInfo.get( var ).index if BDNF > 0.0: model.conc[ model.molInfo.get( "BDNF" ).index ] = BDNF * 1e-3 model.concInit[ model.molInfo.get( "BDNF" ).index ] = BDNF * 1e-3 tht = time.time() x, y = doseResp( model, doseMolIndex, outputMolIndex, doseList ) tht = time.time() - tht ax.plot( x , y, label = "aS6K_vs_" + var ) ax.set_ylim( 0.0, 0.5 ) print( "dose_resp runtimes: t Moose = {:.2f}; t HillTau = {:.4f}: ".format( tmoose, tht) ) htvec = np.array( y ) dy = htvec - mvec print( char[plotPos], ": Dose_resp normalized rms diff =", np.sqrt( np.mean( dy * dy )) / np.max( htvec ))
def bisBCM(ht, plotPos): ltpAmpl = 2 ltdAmpl = 0.3 jsonDict = hillTau.loadHillTau(ht) qs = hillTau.getQuantityScale(jsonDict) hillTau.scaleDict(jsonDict, qs) model = hillTau.parseModel(jsonDict) model.dt = plotDt inputMolIndex = model.molInfo.get("Ca").index outputMolIndex = model.molInfo.get("synAMPAR").index model.reinit() origConc = model.conc[inputMolIndex] model.advance(20) model.conc[inputMolIndex] = ltpAmpl * qs model.advance(1) model.conc[inputMolIndex] = origConc model.advance(29) model.conc[inputMolIndex] = ltdAmpl * qs model.advance(30) model.conc[inputMolIndex] = origConc model.advance(20) plotvec = np.transpose(np.array(model.plotvec)) x = np.array(range(plotvec.shape[1])) * plotDt reacn = "ht" ax = plotBoilerplate(char[plotPos], plotPos, "", xlabel="Time (s)", ylabel="[synAMPAR] ($\mu$M)") ax.plot(x, plotvec[inputMolIndex] / qs, label="Ca", color="C2") ax.plot(x, plotvec[outputMolIndex] / qs, label="synAMPAR", color="C4") ax.legend()
def runDoser(kkit, ht, plotPos, title=""): ax = plotBoilerplate(char[plotPos], plotPos, title, xlabel="[Ca] ($\mu$M)", ylabel="[synAMPAR] ($\mu$M)") ax.set_xscale("log") ax.set_xlim(0.01, 10) ax.set_ylim(0, 0.6) modelId = moose.loadModel(kkit, 'model', 'gsl') x, y = doseRespMoose() ax.plot(x, y, label="Syn_vs_Ca_moose") moosey = np.array(y) jsonDict = hillTau.loadHillTau(ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) outputMolIndex = model.molInfo.get("synAMPAR").index CaMolIndex = model.molInfo.get("Ca").index x, y = doseResp(model, CaMolIndex, outputMolIndex) ax.plot(x, y, label="Syn_vs_Ca") hty = np.array(y) dy = moosey - hty print("Dose-resp normalized rms diff =", np.sqrt(np.mean(dy * dy)) / np.max(hty))
def makeMash(args, stimVec, referenceOutputs): #jsonDict = hillTau.loadHillTau( "HT_MODELS/opt_fb_inhib.json" ) jsonDict = hillTau.loadHillTau(args.HillTauModel) if len(args.addParams) > 0: pv = args.addParams else: pv = paramVec(jsonDict) for i in args.removeParams: if i in pv: pv.remove(i) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) model.dt = plotDt for i in stimVec: mi = model.molInfo.get(i.hillTauMol) if mi: inputMolIndex = mi.index i.molIndex = inputMolIndex if i.conc < 0: # Hack to specify use of initial conc i.conc = mi.concInit else: raise ValueError("Nonexistent stimulus molecule: ", i.hillTauMol) return Mash(model, referenceOutputs, pv, args.monitor, stimVec, jsonDict)
def runit(f): jsonDict = hillTau.loadHillTau(f[0] + ".json") qs = hillTau.getQuantityScale(jsonDict) hillTau.scaleDict(jsonDict, qs) model = hillTau.parseModel(jsonDict) model.dt = 1.0 ev = [] ev.extend(parseEvents(f[1], f[2], 1)) ev.extend(parseEvents(f[3], f[4], 0)) sev = sorted(ev, key=lambda x: x.t) model.reinit() lastt = 0.0 ans = [] maxconc = 0.0 for s in sev: delta = s.t - lastt if delta > 0.0: model.advance(delta) if s.isStim: model.conc[model.molInfo[s.mol].index] = s.conc else: c = model.conc[model.molInfo[s.mol].index] #print( "Conc @ {} = {}".format( s.t, c ) ) maxconc = max(maxconc, c) ans.append(s.conc - model.conc[model.molInfo[s.mol].index]) lastt = s.t err = 0.0 #print( "ANS = ", ans ) for d in ans: err += d * d / (maxconc * maxconc) return err / len(ans), model
def ts(chem, ht, ampl, plotPos, title=''): tsettle = 500 # This turns out to be needed for both models. tpre = 10 tstim = 1 tpost = 50 modelId = moose.loadModel(chem, 'model', 'gsl')[0] Ca = moose.element('/model/kinetics/Ca') output = moose.element('/model/kinetics/synAMPAR') iplot = moose.element('/model/graphs/conc1/Ca.Co') oplot = moose.element('/model/graphs/conc2/synAMPAR.Co') moose.setClock(iplot.tick, plotDt) for i in range(10, 20): moose.setClock(i, plotDt) Ca.concInit = 0.08e-3 moose.reinit() moose.start(tsettle) moose.start(tpre) Ca.concInit = ampl moose.start(tstim) Ca.concInit = 0.08e-3 moose.start(tpost) ivec = iplot.vector ovec = oplot.vector ivec = ivec[int(tsettle / plotDt):] ovec = ovec[int(tsettle / plotDt):] x = np.array(range(len(ivec))) * plotDt ax = plotBoilerplate(char[plotPos], plotPos, title, xlabel="Time (s)", ylabel="[synAMPAR] ($\mu$M)") #ax.plot( x , 1000 * ivec, label = "input" ) ax.plot(x, 1000 * ovec, label="output") moose.delete('/model') jsonDict = hillTau.loadHillTau(ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) model.dt = plotDt inputMolIndex = model.molInfo.get("Ca").index outputMolIndex = model.molInfo.get("synAMPAR").index model.advance(tpre + tsettle) model.conc[inputMolIndex] = ampl model.advance(tstim) model.conc[inputMolIndex] = 0.08e-3 model.advance(tpost) plotvec = np.transpose(np.array(model.plotvec)) x = np.array(range(plotvec.shape[1] - int(tsettle / plotDt))) * plotDt reacn = "this is ht" #ax = plotBoilerplate( "B", plotPos+1, reacn, xlabel = "Time (s)" ) #ax.plot( x , 1000 * plotvec[inputMolIndex], label = "input" ) ax.plot(x, 1000 * plotvec[outputMolIndex][int(tsettle / plotDt):], label="output")
def runSim(chem, ht, cps, runtime): print("-------------------------------------> RUNNING: ", chem) modelId = moose.loadModel("KKIT_MODELS/" + chem, 'model', 'gsl') for i in range(0, 20): moose.setClock(i, plotDt) moose.reinit() mooseTime = time.time() moose.start(runtime) mooseTime = time.time() - mooseTime moose.delete('/model') jsonDict = hillTau.loadHillTau("HT_MODELS/" + ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) model.dt = plotDt htTime = 0.0 for i in range(htReps): model.reinit() t0 = time.time() model.advance(runtime) htTime += time.time() - t0 # Now run it again, but in steady-state mode for HillTau htTime2 = 0.0 for i in range(htReps): model.reinit() t0 = time.time() model.advance(runtime, settle=True) htTime2 += time.time() - t0 # Now do the COPASI thing. shutil.copy("SBML_MODELS/" + cps, "temp.sbml") # It croaks if you just give the file name. Have to use ./temp.sbml smodel = pycotools3.model.ImportSBML("./temp.sbml") m = smodel.load_model() #TC = pycotools3.tasks.TimeCourse( m, end = runtime, step_size = plotDt, run = True, save = False ) TC = pycotools3.tasks.TimeCourse(m, end=runtime, step_size=plotDt, run=False, save=False) TC.run = True TC.set_timecourse() TC.set_report() t0 = time.time() TC.simulate() ctime = time.time() - t0 return [chem, mooseTime, htTime, htTime2, ctime, runtime]
def runSim(dtList, ht, plotPos, stim="input", output="output", runtime=t3): ht = "HT_MODELS/" + ht ax = plotBoilerplate(plotPos, "fb_inhib", xlabel="Time (s)") jsonDict = hillTau.loadHillTau(ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) printMinTau(model, ht) ovec = [] for idx, dt in enumerate(dtList): model.dt = dt model.reinit() model.internalDt = dt # We override internalDt to do accuracy check #print( "Timesteps = ", dt, model.internalDt ) inputMolIndex = model.molInfo.get(stim).index outputMolIndex = model.molInfo.get(output).index model.advance(t1) model.conc[inputMolIndex] = i1 if (runtime < t3): model.advance(runtime - t1) else: model.advance(t2 - t1) model.conc[inputMolIndex] = 0 model.advance(t3 - t2) plotvec = np.transpose(np.array(model.plotvec)) x = np.array(range(plotvec.shape[1])) * dt reacn = "this is ht" htvec = np.array(plotvec[outputMolIndex]) ax.plot(x, 1000 * htvec, label="dt= " + str(dt), color=color[idx], linestyle=":") #print( len( htvec ), round(dtList[-1]/dt) ) ovec.append(htvec[::round(dtList[-1] / dt)]) ax.plot(x[::round(dtList[-1] / dt)], 1000 * ovec[-1], marker=".", color=color[idx], linestyle="none") ax.legend() for v, dt in zip(ovec, dtList): ml = min(len(v), len(ovec[0])) #print( "ML = ", ml, v ) dy = v[:ml] - ovec[0][:ml] print("{} @ dt= {}: normalized rms diff ={:.4f}".format( ht, dt, np.sqrt(np.mean(dy * dy)) / np.max(ovec[0])))
def runSim(chem, ht, plotPos): modelId = moose.loadModel(chem, 'model', 'gsl') stim = moose.element('/model/kinetics/input') output = moose.element('/model/kinetics/input') iplot = moose.element('/model/graphs/conc1/input.Co') oplot = moose.element('/model/graphs/conc1/output.Co') moose.setClock(iplot.tick, plotDt) for i in range(10, 20): moose.setClock(i, plotDt) stim.concInit = 0 moose.reinit() moose.start(t1) stim.concInit = i1 moose.start(t2 - t1) stim.concInit = 0 moose.start(t3 - t2) ovec = oplot.vector x = np.array(range(len(ovec))) * plotDt ax = plotBoilerplate("C", plotPos, "This is mass-action", xlabel="Time (s)") ax.plot(x, 1000 * ovec, label="Mass-action") moose.delete('/model') jsonDict = hillTau.loadHillTau(ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) model.dt = plotDt inputMolIndex = model.molInfo.get("input").index outputMolIndex = model.molInfo.get("output").index model.advance(t1) model.conc[inputMolIndex] = i1 model.advance(t2 - t1) model.conc[inputMolIndex] = 0 model.advance(t3 - t2) plotvec = np.transpose(np.array(model.plotvec)) x = np.array(range(plotvec.shape[1])) * plotDt reacn = "this is ht" htvec = np.array(plotvec[outputMolIndex]) #ax = plotBoilerplate( "D", plotPos+1, reacn, xlabel = "Time (s)" ) ax.plot(x, 1000 * htvec, label="HillTau") ax.plot(x, 1000 * plotvec[inputMolIndex], label="input") dy = htvec - ovec print("Panel C normalized rms diff =", np.sqrt(np.mean(dy * dy)) / np.max(htvec))
def runHT( ht, stimMol, outputMol, events ): jsonDict = hillTau.loadHillTau( "HT_MODELS/"+ht ) qs = hillTau.getQuantityScale( jsonDict ) hillTau.scaleDict( jsonDict, qs ) model = hillTau.parseModel( jsonDict ) model.dt = plotDt htTime = 0.0 stimIndex = model.molInfo.get( stimMol ).index model.reinit() currTime = 0.0 for e in events: t0 = time.time() model.advance( e[0] - currTime ) model.conc[stimIndex] = e[1] / qs currTime = e[0] htTime += time.time() - t0 outIndex = model.molInfo.get( outputMol ).index return model.getConcVec( outIndex ) * 1e3
def runSim(fname, plotPos, text): ht = "HT_MODELS/" + fname + ".json" chem = "KKIT_MODELS/" + fname + ".g" ax = plotBoilerplate(plotPos, text) modelId = moose.loadModel(chem, 'model', 'gsl') #moose.le( '/model/kinetics') for i in range(10, 20): moose.setClock(i, plotDt) iplot = moose.element('/model/graphs/conc1/input.Co') oplot = moose.element('/model/graphs/conc1/output.Co') L = moose.element('/model/kinetics/input') if moose.exists('/model/kinetics/mol'): R = moose.element('/model/kinetics/mol') R.concInit = 1e-3 L.concInit = 0 moose.reinit() moose.start(pre) L.concInit = stimulusAmpl moose.start(stim) L.concInit = 0 moose.start(post) ivec = iplot.vector ovec = oplot.vector xvec = np.array(range(len(ivec))) * plotDt moose.delete('/model') jsonDict = hillTau.loadHillTau(ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) inputMolIndex = model.molInfo.get("input").index outputMolIndex = model.molInfo.get("output").index model.dt = plotDt model.reinit() model.advance(pre) model.conc[inputMolIndex] = stimulusAmpl model.advance(stim) model.conc[inputMolIndex] = 0 model.advance(post) plotvec = np.transpose(np.array(model.plotvec)) x = np.array(range(plotvec.shape[1])) * model.dt ax.plot(xvec, 1000 * ivec, label="input") ax.plot(x, 1000 * plotvec[outputMolIndex], label="output") ax.plot(xvec, 1000 * ovec, "k:", label="output")
def runOsc(dtList, ht, plotPos): ht = "HT_MODELS/" + ht runtime = 3000 ax = plotBoilerplate(plotPos, "Oscillator", xlabel="Time (s)") jsonDict = hillTau.loadHillTau(ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) printMinTau(model, ht) ovec = [] for idx, dt in enumerate(dtList): model.dt = dt model.reinit() model.internalDt = dt #print( "Timesteps = ", dt, model.internalDt ) outputMolIndex = model.molInfo.get("output").index model.advance(runtime) plotvec = np.transpose(np.array(model.plotvec)) x = np.array(range(plotvec.shape[1])) * dt reacn = "this is ht" #ax = plotBoilerplate( "H", plotPos+4, reacn, xlabel = "Time (s)" ) #ax.set_ylim( 0, 0.4 ) htvec = np.array(plotvec[outputMolIndex]) ax.plot(x, 1000 * htvec, label="dt= " + str(dt), color=color[idx], linestyle=":") #print( len( htvec ), round(dtList[-1]/dt) ) ovec.append(htvec[::round(dtList[-1] / dt)]) ax.plot(x[::round(dtList[-1] / dt)], 1000 * ovec[-1], marker=".", color=color[idx], linestyle="none") ax.legend() for v, dt in zip(ovec, dtList): dy = v - ovec[0] print("{} @ dt= {}: normalized rms diff ={:.4f}".format( ht, dt, np.sqrt(np.mean(dy * dy)) / np.max(ovec[0])))
def runSim(fname, panelTitle, reacn, plotPos): jsonDict = hillTau.loadHillTau(fname) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) model.dt = 0.01 inputMolIndex = model.molInfo.get("input").index outputMolIndex = model.molInfo.get("output").index model.advance(2) model.conc[inputMolIndex] = 1e-3 model.advance(2) model.conc[inputMolIndex] = 0.2e-3 model.advance(6) plotvec = np.transpose(np.array(model.plotvec)) x = np.array(range(plotvec.shape[1])) * model.dt ax = plotBoilerplate(panelTitle, plotPos, reacn, xlabel="Time (s)") ax.plot(x, 1e3 * plotvec[inputMolIndex], label="input") ax.plot(x, 1e3 * plotvec[outputMolIndex], label="output")
def runOsc(chem, ht, plotPos): runtime = 6000 modelId = moose.loadModel(chem, 'model', 'gsl') #output = moose.element( '/model/kinetics/MAPK/MAPK_PP' ) oplot = moose.element('/model/graphs/conc1/MAPK_PP.Co') moose.setClock(oplot.tick, plotDt) for i in range(10, 20): moose.setClock(i, plotDt) moose.reinit() moose.start(runtime) ovec = oplot.vector x = np.array(range(len(ovec))) * plotDt ax = plotBoilerplate("D", plotPos, "This is mass-action", xlabel="Time (s)") ax.plot(x, 1000 * ovec, label="Mass action") ax.set_ylim(0, 0.4) moose.delete('/model') jsonDict = hillTau.loadHillTau(ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) model.dt = plotDt model.reinit() outputMolIndex = model.molInfo.get("output").index model.advance(runtime) plotvec = np.transpose(np.array(model.plotvec)) x = np.array(range(plotvec.shape[1])) * plotDt reacn = "this is ht" #ax = plotBoilerplate( "H", plotPos+4, reacn, xlabel = "Time (s)" ) #ax.set_ylim( 0, 0.4 ) htvec = np.array(plotvec[outputMolIndex]) ax.plot(x, 1000 * htvec, label="HillTau") dy = htvec - ovec print("Panel D normalized rms diff =", np.sqrt(np.mean(dy * dy)) / np.max(htvec))
def dumpScaledFile(self, x, fname): # This is significantly more complicated because the values may # be specified in the Constants section of the file. jd = self.jsonDict consts = jd.get("Constants", {}) # Scale back to original units hillTau.scaleDict(jd, 1.0 / hillTau.getQuantityScale(jd)) # Scale each of the parameters for i, scaleFactor in zip(self.params, x): spl = i.rsplit('.', 1) assert (len(spl) == 2) obj, field = spl if field == "concInit": mi = self.model.molInfo[obj] concInit = jd["Groups"][mi.grp]["Species"][mi.name] if isinstance(concInit, str): if not concInit in consts: raise (ValueError( "Error: Constant {} not found".format(concInit))) else: consts[concInit] *= scaleFactor else: jd["Groups"][mi.grp]["Species"][ mi.name] = concInit * scaleFactor else: ri = self.model.reacInfo[obj] orig = jd["Groups"][ri.grp]["Reacs"][obj][field] if isinstance(orig, str): if not orig in consts: raise (ValueError( "Error: Constant {} not found".format(concInit))) else: consts[orig] *= scaleFactor else: jd["Groups"][ ri.grp]["Reacs"][obj][field] = orig * scaleFactor with open(fname, 'w') as f: json.dump(jd, f, indent=4)
def runDoser(kkit, ht, plotPos, title=""): ax = plotBoilerplate( char[plotPos], plotPos, title, xlabel= "[Ca] ($\mu$M) ", ylabel="[synAMPAR] ($\mu$M)") ax.set_xscale("log") ax.set_xlim(0.01, 10) ax.set_ylim(0, 0.6) modelId = moose.loadModel(kkit, 'model', 'gsl')[0] x, y = doseRespMoose() ax.plot(x, y, label="Syn_vs_Ca_moose") jsonDict = hillTau.loadHillTau(ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) outputMolIndex = model.molInfo.get("synAMPAR").index CaMolIndex = model.molInfo.get("Ca").index x, y = doseResp(model, CaMolIndex, outputMolIndex) ax.plot(x, y, label="Syn_vs_Ca")
def non_trained_ts(chem, ht, plotPos, title=''): tsettle = 6000 # This turns out to be needed for both models. tpre = 600 # Run for a bit to include baseline in plots. tpost = 1200 Ca_rest = 0.08e-3 Ca_ampl = 10e-3 BDNF_rest = 0.05e-6 BDNF_ampl = 3.7e-6 modelId = moose.loadModel(chem, 'model', 'gsl') Ca = moose.element('/model/kinetics/Ca') BDNF = moose.element('/model/kinetics/BDNF') output = moose.element('/model/kinetics/protein') #iplot = moose.element( '/model/graphs/conc1/Ca.Co' ) oplot = moose.element('/model/graphs/conc1/protein.Co') moose.setClock(oplot.tick, plotDt) for i in range(10, 20): moose.setClock(i, plotDt) Ca.concInit = Ca_rest BDNF.concInit = BDNF_rest tmoose = time.time() BDNF.concInit = BDNF_ampl moose.reinit() moose.start(tsettle + tpre) for tInter in [300, 120, 60, 10]: advTime = 0 while advTime < 1200: Ca.concInit = Ca_ampl moose.start(1) Ca.concInit = Ca_rest moose.start(tInter - 1) advTime += tInter moose.start(tpost) tmoose = time.time() - tmoose ovec = oplot.vector ovec = ovec[int(tsettle / plotDt):] x = np.array(range(len(ovec))) * plotDt ax = plotBoilerplate(char[plotPos], plotPos, title, xlabel="Time (s)", ylabel="[protein] (nM/s)") ax.plot(x, 1e6 * ovec, label="output") moose.delete('/model') jsonDict = hillTau.loadHillTau(ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) model.dt = plotDt CaMolIndex = model.molInfo.get("Ca").index BDNFMolIndex = model.molInfo.get("BDNF").index model.conc[BDNFMolIndex] = BDNF_ampl outputMolIndex = model.molInfo.get("protein").index tht = time.time() model.advance(tsettle + tpre) for tInter in [300, 120, 60, 10]: advTime = 0 while advTime < 1200: model.conc[CaMolIndex] = Ca_ampl model.advance(1) model.conc[CaMolIndex] = Ca_rest model.advance(tInter - 1) advTime += tInter model.advance(tpost) tht = time.time() - tht plotvec = np.transpose(np.array(model.plotvec)) x = np.array(range(plotvec.shape[1] - int(tsettle / plotDt))) * plotDt reacn = "this is ht" #ax = plotBoilerplate( "B", plotPos+1, reacn, xlabel = "Time (s)" ) #ax.plot( x , 1000 * plotvec[inputMolIndex], label = "input" ) htvec = np.array(plotvec[outputMolIndex][int(tsettle / plotDt):]) #ax.plot( x , 1e6 * plotvec[outputMolIndex][int(tsettle/plotDt):], label = "output" ) ax.plot(x, 1e6 * htvec, label="output") ax.set_ylim(0.0, 0.02) print( "non-trained timeseries runtimes: t Moose = {:.2f}; t HillTau = {:.4f}: " .format(tmoose, tht)) dy = htvec - ovec print(char[plotPos], ": timeseries normalized rms diff =", np.sqrt(np.mean(dy * dy)) / np.max(htvec))
def runBis(ht, plotPos): reacn = "fb vs output" ax = plotBoilerplate("H", plotPos, reacn, xlabel="fb (mM)", ylabel="output (mM)") ax.set_yscale("log") ax.set_xscale("log") ax.set_xlim(0.0005, 10) ax.set_ylim(0.001, 5) jsonDict = hillTau.loadHillTau(ht) reacs = jsonDict["Groups"]["output_g"]["Reacs"] del reacs["output"] hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) outputMolIndex = model.molInfo.get("output").index fbMolIndex = model.molInfo.get("fb").index x, y = doseResp(model, outputMolIndex, fbMolIndex) ax.plot(y, x, label="fb_vs_output", color="C6") jsonDict = hillTau.loadHillTau(ht) reacs = jsonDict["Groups"]["output_g"]["Reacs"] del reacs["fb"] hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) outputMolIndex = model.molInfo.get("output").index fbMolIndex = model.molInfo.get("fb").index x, y = doseResp(model, fbMolIndex, outputMolIndex) ax.plot(x, y, label="output_vs_fb", color="C5") jsonDict = hillTau.loadHillTau(ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) model.dt = 0.1 fbMolIndex = model.molInfo.get("fb").index stimMolIndex = model.molInfo.get("stim").index outputMolIndex = model.molInfo.get("output").index baseline = model.concInit[stimMolIndex] moose.reinit() t = 0 t = adv(model, stimMolIndex, t, 20, baseline) t = adv(model, stimMolIndex, t, 1, baseline * 100) t = adv(model, stimMolIndex, t, 19, baseline) t = adv(model, stimMolIndex, t, 5, baseline * 100) t = adv(model, stimMolIndex, t, 15, baseline) t = adv(model, stimMolIndex, t, 1, baseline * 0.01) t = adv(model, stimMolIndex, t, 19, baseline) t = adv(model, stimMolIndex, t, 5, baseline * 0.01) t = adv(model, stimMolIndex, t, 15, baseline) plotvec = np.transpose(np.array(model.plotvec)) x = np.array(range(plotvec.shape[1])) * model.dt reacn = "State switching" ax = plotBoilerplate("I", plotPos + 1, reacn, xlabel="Time (s)", ylabel="output (mM)") ax.set_yscale("log") ax.set_ylim(0.001, 5) ax.plot(x, plotvec[fbMolIndex], label="stimulus", color="C6") ax.plot(x, plotvec[outputMolIndex], label="output", color="C5")
def ts(chem, ht, ampl, plotPos, title='', is_LTP=False): tsettle = 500 # This turns out to be needed for both models. tpre = 500 # Run for a bit to include baseline in plots. tpost = 500 Ca_rest = 0.08e-3 BDNF_rest = 0.05e-6 BDNF_ampl = 3.7e-6 modelId = moose.loadModel(chem, 'model', 'gsl') Ca = moose.element('/model/kinetics/Ca') BDNF = moose.element('/model/kinetics/BDNF') output = moose.element('/model/kinetics/CaMKIII_CaM_Ca4') #iplot = moose.element( '/model/graphs/conc1/Ca.Co' ) #oplot = moose.element( '/model/graphs/conc1/CaMKIII_CaM_Ca4.Co' ) # I have to add in the plot because it isn't there in the kkit model. oplot = moose.Table2('/model/graphs/conc1/CaMKIII_CaM_Ca4.Co') moose.connect(oplot, "requestOut", output, "getConc") moose.setClock(oplot.tick, plotDt) for i in range(10, 20): moose.setClock(i, plotDt) Ca.concInit = Ca_rest BDNF.concInit = BDNF_rest tmoose = time.time() moose.reinit() moose.start(tsettle + tpre) if is_LTP: tInter = 295 for i in range(3): Ca.concInit = ampl BDNF.concInit = BDNF_ampl moose.start(1) Ca.concInit = Ca_rest moose.start(4) BDNF.concInit = BDNF_rest moose.start(tInter) moose.start(tpost) else: tstim = 900 Ca.concInit = ampl BDNF.concInit = BDNF_ampl moose.start(tstim) Ca.concInit = Ca_rest BDNF.concInit = BDNF_rest moose.start(tpost) tmoose = time.time() - tmoose ovec = oplot.vector ovec = ovec[int(tsettle / plotDt):] x = np.array(range(len(ovec))) * plotDt ax = plotBoilerplate(char[plotPos], plotPos, title, xlabel="Time (s)", ylabel="aCaMKIII ($\mu$M)") ax.plot(x, 1e3 * ovec, label="output") moose.delete('/model') jsonDict = hillTau.loadHillTau(ht) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) model = hillTau.parseModel(jsonDict) model.dt = plotDt CaMolIndex = model.molInfo.get("Ca").index BDNFMolIndex = model.molInfo.get("BDNF").index outputMolIndex = model.molInfo.get("aCaMKIII").index tht = time.time() model.advance(tsettle + tpre) if is_LTP: tInter = 295 for i in range(3): model.conc[CaMolIndex] = ampl model.conc[BDNFMolIndex] = BDNF_ampl model.advance(1) model.conc[CaMolIndex] = Ca_rest model.advance(4) model.conc[BDNFMolIndex] = BDNF_rest model.advance(tInter) model.advance(tpost) else: model.conc[CaMolIndex] = ampl model.conc[BDNFMolIndex] = BDNF_ampl model.advance(tstim) model.conc[CaMolIndex] = Ca_rest model.conc[BDNFMolIndex] = BDNF_rest model.advance(tpost) tht = time.time() - tht plotvec = np.transpose(np.array(model.plotvec)) x = np.array(range(plotvec.shape[1] - int(tsettle / plotDt))) * plotDt reacn = "this is ht" #ax = plotBoilerplate( "B", plotPos+1, reacn, xlabel = "Time (s)" ) #ax.plot( x , 1000 * plotvec[inputMolIndex], label = "input" ) ax.plot(x, 1e3 * plotvec[outputMolIndex][int(tsettle / plotDt):], label="output") htvec = np.array(plotvec[outputMolIndex][int(tsettle / plotDt):]) if is_LTP: ax.set_ylim(0.0, 0.035) else: ax.set_ylim(0.0, 0.07) print("timeseries runtimes: t Moose = {:.2f}; t HillTau = {:.4f}: ". format(tmoose, tht)) dy = htvec - ovec print(char[plotPos], ": timeseries normalized rms diff =", np.sqrt(np.mean(dy * dy)) / np.max((htvec + ovec) / 2.0))
def conv2sbml(htfile, sbmlfile, stimMol="", events=[]): # Events are a list of [time, conc] assignments. jsonDict = hillTau.loadHillTau(htfile) hillTau.scaleDict(jsonDict, hillTau.getQuantityScale(jsonDict)) htmodel = hillTau.parseModel(jsonDict) # All 'reactants' are handled as modifiers in HillTau: they affect # product formation but are themselves not changed. Unfortunately # simplesbml does not currently support them. So I'm doing a # simple hack: replace all references to listOfReactants to # listOfModifiers in the generated SBML. #smodel = simplesbml.SbmlModel( sub_units = 'millimole', extent_units = 'mole' ) # SimpleSBML doesn't let me set units to anything but mole.. # So I'll just rescale the concs. smodel = simplesbml.SbmlModel() smodel.addCompartment(1e-15, comp_id='comp') for name, mol in htmodel.molInfo.items(): smodel.addSpecies("[" + name + "]", mol.concInit * 1e-3, comp='comp') for name, reac in htmodel.reacInfo.items(): s = reac.subs expr = "1.0" local_params = { 'KA': reac.KA * 1e-3, 'tau': (reac.tau + reac.tau2) / 2.0, 'n': reac.HillCoeff, 'gain': reac.gain, 'Vcomp': 1e-15 } if len(s) == 1: # a --> p reactants = s expr = "Vcomp * (({0}*gain/KA)-{1})/tau".format(s[0], name) elif s[0] == s[-1]: # n*a --> p reactants = [str(len(s)) + " " + s[0]] expr = "Vcomp * (({0}^n * gain/KA)-{1})/tau".format(s[0], name) elif len(s) == 2: # a + b --> p reactants = [s[0], s[1]] if reac.inhibit: expr = "Vcomp * ((gain * {0} * (1-{1}/(KA + {1})))-{2})/tau".format( s[0], s[1], name) else: expr = "Vcomp * ((gain * {0} * {1}/(KA + {1}))-{2})/tau".format( s[0], s[1], name) elif s[1] == s[-1]: # a + nb --> p reactants = [s[0], str(len(s) - 1) + " " + s[1]] if reac.inhibit: expr = "Vcomp * ((gain * {0} * (1-{1}^n/(KA^n + {1}^n)))-{2})/tau".format( s[0], s[1], name) else: expr = "Vcomp * ((gain * {0} * {1}^n/(KA^n + {1}^n))-{2})/tau".format( s[0], s[1], name) else: # a + mod + nb --> p reactants = [s[0], s[1], str(len(s) - 2) + " " + s[2]] local_params['Kmod'] = reac.Kmod local_params['Amod'] = reac.Amod local_params['Nmod'] = reac.Nmod if reac.inhibit: expr = "Vcomp * ((gain * {0} * (1-{2}^n/((KA^n*(1+({1}/Kmod)^Nmod)/(1+Amod*(({1}/Kmod)^Nmod))) + {2}^n)))-{3})/tau".format( s[0], s[1], s[-1], name) else: expr = "Vcomp * ((gain * {0} * {2}^n/((KA^n*(1+({1}/Kmod)^Nmod)/(1+Amod*(({1}/Kmod)^Nmod))) + {2}^n))-{3})/tau".format( s[0], s[1], s[-1], name) smodel.addReaction(reactants, [name], expr, local_params=local_params, rxn_id="r__" + name) for e in events: smodel.addEvent(trigger='time>' + str(e[0]), assignments={stimMol: str(e[1])}) with open(sbmlfile, 'w') as fd: # Here we get into a series of hacks to convert the rectants # into modifiers. # Hack to put in modifiers instead of reactants: sbmlstr = smodel.toSBML().replace("listOfReactants", "listOfModifiers") # Remove stoichometry and constant terms from modifier species refs sbmlstr = sbmlstr.replace("speciesReference", "modifierSpeciesReference") sbmlstr = sbmlstr.replace(" stoichiometry=\"1\" constant=\"true\"", "") sbmlstr = sbmlstr.replace(" stoichiometry=\"2\" constant=\"true\"", "") sbmlstr = sbmlstr.replace(" stoichiometry=\"3\" constant=\"true\"", "") sbmlstr = sbmlstr.replace(" stoichiometry=\"4\" constant=\"true\"", "") sbmllist = sbmlstr.split("listOfProducts>") newstr = "" for s in sbmllist: if s[-1] == '/': # closing line for listOfProducts s = s.replace('/>', ' stoichiometry="1" constant="true"/>') s = s.replace('modifierSpeciesReference', 'speciesReference') newstr += s + "listOfProducts>" fd.write(newstr[:-len("listOfProducts>")]) '''