def main(): """ This example illustrates loading, running of an SBML model defined in XML format.\n The model 00001-sbml-l3v1.xml is taken from l3v1 SBML testcase.\n Plots are setup.\n Model is run for 20sec.\n As a general rule we created model under '/path/model' and plots under '/path/graphs'.\n """ mfile = os.path.join(script_dir, 'chem_models/00001-sbml-l3v1.xml') runtime = 20.0 # Loading the sbml file into MOOSE, models are loaded in path/model sbmlId = moose.readSBML(mfile, 'sbml') s1 = moose.element('/sbml/model/compartment/S1') s2 = moose.element('/sbml/model/compartment/S2') # Creating MOOSE Table, Table2 is for the chemical model graphs = moose.Neutral('/sbml/graphs') outputs1 = moose.Table2('/sbml/graphs/concS1') outputs2 = moose.Table2('/sbml/graphs/concS2') # connect up the tables moose.connect(outputs1, 'requestOut', s1, 'getConc') moose.connect(outputs2, 'requestOut', s2, 'getConc') # Reset and Run moose.reinit() moose.start(runtime)
def main(): """ This example illustrates loading, running of an SBML model defined in XML format.\n The model 00001-sbml-l3v1.xml is taken from l3v1 SBML testcase.\n Plots are setup.\n Model is run for 20sec.\n As a general rule we created model under '/path/model' and plots under '/path/graphs'.\n """ mfile = os.path.join( script_dir, 'chem_models/00001-sbml-l3v1.xml') runtime = 20.0 # Loading the sbml file into MOOSE, models are loaded in path/model sbmlId = moose.readSBML(mfile,'sbml') s1 = moose.element('/sbml/model/compartment/S1') s2= moose.element('/sbml/model/compartment/S2') # Creating MOOSE Table, Table2 is for the chemical model graphs = moose.Neutral( '/sbml/graphs' ) outputs1 = moose.Table2 ( '/sbml/graphs/concS1') outputs2 = moose.Table2 ( '/sbml/graphs/concS2') # connect up the tables moose.connect( outputs1,'requestOut', s1, 'getConc' ); moose.connect( outputs2,'requestOut', s2, 'getConc' ); # Reset and Run moose.reinit() moose.start(runtime)
def main(): """ This example illustrates loading, running of an SBML model defined in XML format. Default this file load's 00001-sbml-l3v1.xml which is taken from l3v1 SBML testcase. Plots are setup. Model is run for 20sec. As a general rule we created model under '/path/model' and plots under '/path/graphs'. If someone wants to load anyother file then `python loadSbmlmodel filepath runtime` """ dfilepath = "../genesis/00001-sbml-l3v1.xml" druntime = 20.0 msg = "" try: sys.argv[1] except IndexError: filepath = dfilepath else: filepath = sys.argv[1] if not os.path.exists(filepath): msg = "Filename or path does not exist",filepath,"loading default file",dfilepath filepath = dfilepath try: sys.argv[2] except : runtime = druntime else: runtime = float(sys.argv[2]) sbmlId = moose.element('/') # Loading the sbml file into MOOSE, models are loaded in path/model sbmlId = moose.readSBML(filepath,'/sbml')[0] if isinstance(sbmlId, (list, tuple)): print(sbmlId) elif sbmlId.path != '/': s1 = moose.element('/sbml/compartment/S1') s2 = moose.element('/sbml/compartment/S2') # Creating MOOSE Table, Table2 is for the chemical model graphs = moose.Neutral( '/sbml/graphs' ) outputs1 = moose.Table2 ( '/sbml/graphs/concS1') outputs2 = moose.Table2 ( '/sbml/graphs/concS2') # connect up the tables moose.connect( outputs1,'requestOut', s1, 'getConc' ); moose.connect( outputs2,'requestOut', s2, 'getConc' ); # gsl solver is added, default is ee moose.addChemSolver(sbmlId.path,"ee") # Reset and Run moose.reinit() moose.start(runtime) return sbmlId,True,msg return sbmlId,False,msg
def main(): modelname = os.path.join(modeldir, 'chem_models/mkp1_feedback_effects_acc4.xml') model = moose.readSBML(modelname, '/model') tables = moose.wildcardFind('/##[TYPE=Table2]') records = {} for t in tables: records[t.path.split('/')[-1]] = t c = moose.Clock('/clock') moose.reinit() moose.start(200) check(tables)
def test_sbml(): modelname = os.path.join(sdir_, "..", "data", "00001-sbml-l3v1.xml") model = moose.readSBML(modelname, "/sbml") if not model: print("Most likely python-libsbml is not installed.") return 0 moose.reinit() moose.start(200) # TODO: Add more tests here. p = moose.wildcardFind("/sbml/##") assert len(p) == 8, p names = ["compartment", "mesh", "S1", "info", "S2", "info", "reaction1"] for x in p: assert x.name in names
def main(): modelname = './chem_models/mkp1_feedback_effects_acc4.xml' modelType = modelname.split(".")[-1] if modelType == "xml": model = moose.readSBML(modelname, '/model') else: raise IOError("Input is not XML model. Wrong extension %s" % modelType) tables = moose.wildcardFind('/##[TYPE=Table2]') records = {} for t in tables: records[t.path.split('/')[-1]] = t c = moose.Clock('/clock') moose.reinit() moose.start(200) check(tables)
def runMoose(chem, stimVec, outMols): filename, file_extension = os.path.splitext(chem) if file_extension == ".g": modelId = moose.loadModel(chem, 'model', 'gsl') elif file_extension == ".xml": #modelId = mu.mooseReadSBML( chem, 'model', 'gsl' ) modelId = moose.readSBML(chem, 'model', 'gsl') ''' moose.le( "/model/kinetics" ) for i in moose.wildcardFind ( "/model/kinetics/##[ISA=PoolBase]" ): print( i.name, i.concInit ) for i in moose.wildcardFind ( "/model/kinetics/##[ISA=Reac]" ): print( i.name, i.Kf, i.Kb ) ''' tabs = moose.Neutral("/model/tabs") mooseMols = [getMooseName(i) for i in outMols] for i in mooseMols: el = moose.wildcardFind("/model/kinetics/" + i + ",/model/kinetics/##/" + i) if len(el) > 0: # Make an output table tab = moose.Table2("/model/tabs/" + i) moose.connect(tab, "requestOut", el[0], "getConc") for i in range(10, 20): moose.setClock(i, plotDt) moose.reinit() lastt = 0.0 for stim in stimVec: #print( "STIM = ", stim.mol, " ", stim.conc, " ", stim.time ) el = moose.wildcardFind("/model/kinetics/" + stim.mooseMol + ",/model/kinetics/##/" + stim.mooseMol) if len(el) > 0: if stim.time > lastt: moose.start(stim.time - lastt) lastt = stim.time el[0].concInit = stim.conc # assign conc even if no sim advance else: print("Warning: Stimulus molecule '{}' not found in MOOSE".format( stim.mooseMol)) vecs = {i.name: i.vector for i in moose.wildcardFind("/model/tabs/#")} return vecs
def loadFile(filename, target, merge=True): """Try to load a model from specified `filename` under the element `target`. if `merge` is True, the contents are just loaded at target. If false, everything is deleted from the parent of target unless the parent is root. Returns ------- a dict containing at least these three entries: modeltype: type of the loaded model. subtype: subtype of the loaded model, None if no specific subtype modelroot: root element of the model, None if could not be located - as is the case with Python scripts """ istext = True with open(filename, 'rb') as infile: istext = mtypes.istextfile(infile) if not istext: print 'Cannot handle any binary formats yet' return None parent, child = posixpath.split(target) p = moose.Neutral(parent) if not merge and p.path != '/': for ch in p.children: moose.delete(ch) try: modeltype = mtypes.getType(filename) subtype = mtypes.getSubtype(filename, modeltype) except KeyError: raise FileLoadError('Do not know how to handle this filetype: %s' % (filename)) pwe = moose.getCwe() if modeltype == 'genesis': if subtype == 'kkit' or subtype == 'prototype': model = moose.loadModel(filename, target,'gsl') else: print 'Only kkit and prototype files can be loaded.' elif modeltype == 'cspace': model = moose.loadModel(filename, target) elif modeltype == 'xml': if subtype == 'neuroml': popdict, projdict = neuroml.loadNeuroML_L123(filename) # Circus to get the container of populations from loaded neuroml for popinfo in popdict.values(): for cell in popinfo[1].values(): model = cell.parent break break elif subtype == 'sbml': model = moose.readSBML(filename,target) else: raise FileLoadError('Do not know how to handle this filetype: %s' % (filename)) moose.setCwe(pwe) # The MOOSE loadModel changes the current working element to newly loaded model. We revert that behaviour # TODO: check with Aditya how to specify the target for # neuroml reader return {'modeltype': modeltype, 'subtype': subtype, 'model': model}
def loadFile(filename, target, merge=True): """Try to load a model from specified `filename` under the element `target`. if `merge` is True, the contents are just loaded at target. If false, everything is deleted from the parent of target unless the parent is root. Returns ------- a dict containing at least these three entries: modeltype: type of the loaded model. subtype: subtype of the loaded model, None if no specific subtype modelroot: root element of the model, None if could not be located - as is the case with Python scripts """ istext = True with open(filename, 'rb') as infile: istext = mtypes.istextfile(infile) if not istext: print 'Cannot handle any binary formats yet' return None parent, child = posixpath.split(target) p = moose.Neutral(parent) if not merge and p.path != '/': for ch in p.children: moose.delete(ch) try: modeltype = mtypes.getType(filename) subtype = mtypes.getSubtype(filename, modeltype) except KeyError: raise FileLoadError('Do not know how to handle this filetype: %s' % (filename)) pwe = moose.getCwe() #self.statusBar.showMessage('Loading model, please wait') # app = QtGui.qApp # app.setOverrideCursor(QtGui.QCursor(Qt.Qt.BusyCursor)) #shows a hourglass - or a busy/working arrow if modeltype == 'genesis': if subtype == 'kkit' or subtype == 'prototype': model = moose.loadModel(filename, target,'gsl') #Harsha: Moving the model under /modelname/model and graphs under /model/graphs lmodel = moose.Neutral('%s/%s' %(model.path,"model")) for compt in moose.wildcardFind(model.path+'/##[ISA=ChemCompt]'): moose.move(compt.path,lmodel) if not moose.exists(model.path+'/data'): graphspath = moose.Neutral('%s/%s' %(model.path,"data")) dataPath = moose.element(model.path+'/data') i =0 nGraphs = moose.wildcardFind(model.path+'/graphs/##[TYPE=Table2]') for graphs in nGraphs: if not moose.exists(dataPath.path+'/graph_'+str(i)): graphspath = moose.Neutral('%s/%s' %(dataPath.path,"graph_"+str(i))) else: graphspath = moose.element(dataPath.path+'/graph_'+str(i)) moose.move(graphs.path,graphspath) if len(nGraphs) > 0: i = i+1 #print " i ", i,moose.wildcardFind(model.path+'/moregraphs/##[TYPE=Table2]') for moregraphs in moose.wildcardFind(model.path+'/moregraphs/##[TYPE=Table2]'): if not moose.exists(dataPath.path+'/graph_'+str(i)): graphspath = moose.Neutral('%s/%s' %(dataPath.path,"graph_"+str(i))) else: graphspath = moose.element(dataPath.path+'/graph_'+str(i)) moose.move(moregraphs.path,graphspath) moose.delete(model.path+'/graphs') moose.delete(model.path+'/moregraphs') else: print 'Only kkit and prototype files can be loaded.' elif modeltype == 'cspace': model = moose.loadModel(filename, target,'gsl') #Harsha: Moving the model under /modelname/model and graphs under /model/graphs lmodel = moose.Neutral('%s/%s' %(model.path,"model")) for compt in moose.wildcardFind(model.path+'/##[ISA=ChemCompt]'): moose.move(compt.path,lmodel) if not moose.exists(model.path+'/data'): graphspath = moose.Neutral('%s/%s' %(model.path,"data")) dataPath = moose.element(model.path+'/data') i =0 nGraphs = moose.wildcardFind(model.path+'/graphs/##[TYPE=Table2]') for graphs in nGraphs: if not moose.exists(dataPath.path+'/graph_'+str(i)): graphspath = moose.Neutral('%s/%s' %(dataPath.path,"graph_"+str(i))) else: graphspath = moose.element(dataPath.path+'/graph_'+str(i)) moose.move(graphs.path,graphspath) if len(nGraphs) > 0: i = i+1 #print " i ", i,moose.wildcardFind(model.path+'/moregraphs/##[TYPE=Table2]') for moregraphs in moose.wildcardFind(model.path+'/moregraphs/##[TYPE=Table2]'): if not moose.exists(dataPath.path+'/graph_'+str(i)): graphspath = moose.Neutral('%s/%s' %(dataPath.path,"graph_"+str(i))) else: graphspath = moose.element(dataPath.path+'/graph_'+str(i)) moose.move(moregraphs.path,graphspath) moose.delete(model.path+'/graphs') moose.delete(model.path+'/moregraphs') addSolver(target,'gsl') elif modeltype == 'xml': if subtype == 'neuroml': popdict, projdict = neuroml.loadNeuroML_L123(filename) # Circus to get the container of populations from loaded neuroml for popinfo in popdict.values(): for cell in popinfo[1].values(): solver = moose.HSolve(cell.path + "/hsolve") solver.target = cell.path # model = cell.parent # break # break # Moving model to a new location under the model name # model name is the filename without extension model = moose.Neutral("/" + splitext(basename(filename))[0]) element = moose.Neutral(model.path + "/model") if(moose.exists("/cells")) : moose.move("/cells" , element.path) if(moose.exists("/elec")) : moose.move("/elec" , model.path) if(moose.exists("/library")): moose.move("/library", model.path) # moose.move("cells/", cell.path) elif subtype == 'sbml': model = moose.readSBML(filename,target,'gsl') addSolver(target,'gsl') else: raise FileLoadError('Do not know how to handle this filetype: %s' % (filename)) moose.setCwe(pwe) # The MOOSE loadModel changes the current working element to newly loaded model. We revert that behaviour # TODO: check with Aditya how to specify the target for # neuroml reader # app.restoreOverrideCursor() return {'modeltype': modeltype, 'subtype': subtype, 'model': model}
def load_model(model_path): moose.readSBML(model_path, "/model") logging.info("Done loading model %s " % model_path)