def checkKisaoIntegrator(self, inline_omex, kisao, name): """ Helper function for checking kisao integrator. """ omex_file = os.path.join(self.test_dir, "test.omex") te.exportInlineOmex(inline_omex, omex_file) omex.extractCombineArchive(omex_file, directory=self.test_dir, method="zip") locations = omex.getLocationsByFormat(omex_file, "sed-ml") sedml_files = [os.path.join(self.test_dir, loc) for loc in locations] sedml_file = sedml_files[0] # check the SED-ML doc = libsedml.readSedMLFromFile(sedml_file) # test_str = libsedml.writeSedMLToString(doc) # print(test_str) simulation = doc.getSimulation('sim0') algorithm = simulation.getAlgorithm() assert algorithm.getKisaoID() == kisao # check the generated code pystr = tesedml.sedmlToPython(sedml_file, workingDir=self.test_dir) # is integrator/solver set in python code if simulation.getTypeCode() is libsedml.SEDML_SIMULATION_STEADYSTATE: assert ".setSteadyStateSolver('{}')".format(name) in pystr else: assert ".setIntegrator('{}')".format(name) in pystr
def checkKisaoAlgorithmParameter(self, inline_omex, kisao, name, value): """ Helper function for checking kisao parameter. """ # check that set AlgorithmParameter set correctly in SED-ML omex_file = os.path.join(self.test_dir, "test.omex") te.exportInlineOmex(inline_omex, omex_file) omex.extractCombineArchive(omex_file, directory=self.test_dir, method="zip") locations = omex.getLocationsByFormat(omex_file, "sed-ml") sedml_files = [os.path.join(self.test_dir, loc) for loc in locations] sedml_file = sedml_files[0] doc = libsedml.readSedMLFromFile(sedml_file) simulation = doc.getSimulation('sim0') algorithm = simulation.getAlgorithm() pdict = { p.getKisaoID(): p for p in algorithm.getListOfAlgorithmParameters() } self.assertTrue(kisao in pdict) pkey = tesedml.SEDMLCodeFactory.algorithmParameterToParameterKey( pdict[kisao]) if pkey.dtype == str: self.assertEqual(pkey.value, value) else: # numerical parameter self.assertAlmostEqual(float(pkey.value), value) # check that integrator is set in python code pystr = tesedml.sedmlToPython(sedml_file, workingDir=self.test_dir) print(simulation.getElementName()) print(pystr) if simulation.getTypeCode() is libsedml.SEDML_SIMULATION_STEADYSTATE: if pkey.dtype == str: self.assertTrue( ".steadyStateSolver.setValue('{}', '{}')".format( name, value) in pystr) else: # numerical parameter self.assertTrue(".steadyStateSolver.setValue('{}', {})".format( name, value) in pystr) else: if pkey.dtype == str: self.assertTrue(".integrator.setValue('{}', '{}')".format( name, value) in pystr) else: # numerical parameter self.assertTrue(".integrator.setValue('{}', {})".format( name, value) in pystr)
def test_kisao_adams(self): p = """ model0 = model "m1" sim0 = simulate uniform(0, 10, 100) sim0.algorithm = nonstiff task0 = run sim0 on model0 plot task0.time vs task0.S1 """ inline_omex = '\n'.join([self.a1, p]) self.checkKisaoIntegrator(inline_omex, 'KISAO:0000280', 'cvode') te.executeInlineOmex(inline_omex) omex_file = os.path.join(self.test_dir, "test.omex") te.exportInlineOmex(inline_omex, omex_file) pycode_dict = tesedml.combineArchiveToPython(omex_file) pycode = six.next(six.itervalues(pycode_dict)) print(pycode) self.assertTrue("integrator.setValue('stiff', False)" in pycode)
def export_omex(self, antimony_str, phrasedml_str, **kwargs): """Generate COMBINE OMEX file. :param antimony_str: represent the SBML :type antimony_str: str :param phrasedml_str: represent the SEDML :type phrasedml_str: str :return: omex inline :rtype: str :Keyword Arguments: * *outputfile*: specify outputfile """ model = re.search("model (.*)\n", antimony_str) if model.group(1)[0] == "*": model = self.find_between(model.group(1), "*", "()") phrasedml_str = phrasedml_str.format(model) else: phrasedml_str = phrasedml_str.format(model.group(1)) inline_omex = "\n".join([antimony_str, phrasedml_str]) if "outputfile" in kwargs: filepath = kwargs["outputfile"] else: dirName = self.get_omexfilename() try: os.mkdir(dirName) print("Directory ", dirName, " Created ") except FileExistsError: print("Directory ", dirName, " already exists") workingDir = os.path.join(self.workingdir0, dirName) filepath = os.path.join(workingDir, "archive.omex") print("The output file path: ", filepath) te.exportInlineOmex(inline_omex, filepath) return inline_omex
def checkKisaoAlgorithmParameter(self, inline_omex, kisao, name, value): """ Helper function for checking kisao parameter. """ # check that set AlgorithmParameter set correctly in SED-ML omex_file = os.path.join(self.test_dir, "test.omex") te.exportInlineOmex(inline_omex, omex_file) omex.extractCombineArchive(omex_file, directory=self.test_dir, method="zip") locations = omex.getLocationsByFormat(omex_file, "sed-ml") sedml_files = [os.path.join(self.test_dir, loc) for loc in locations] sedml_file = sedml_files[0] doc = libsedml.readSedMLFromFile(sedml_file) simulation = doc.getSimulation('sim0') algorithm = simulation.getAlgorithm() pdict = {p.getKisaoID(): p for p in algorithm.getListOfAlgorithmParameters()} self.assertTrue(kisao in pdict) pkey = tesedml.SEDMLCodeFactory.algorithmParameterToParameterKey(pdict[kisao]) if pkey.dtype == str: self.assertEqual(pkey.value, value) else: # numerical parameter self.assertAlmostEqual(float(pkey.value), value) # check that integrator is set in python code pystr = tesedml.sedmlToPython(sedml_file, workingDir=self.test_dir) print(simulation.getElementName()) print(pystr) if simulation.getTypeCode() is libsedml.SEDML_SIMULATION_STEADYSTATE: if pkey.dtype == str: self.assertTrue(".steadyStateSolver.setValue('{}', '{}')".format(name, value) in pystr) else: # numerical parameter self.assertTrue(".steadyStateSolver.setValue('{}', {})".format(name, value) in pystr) else: if pkey.dtype == str: self.assertTrue(".integrator.setValue('{}', '{}')".format(name, value) in pystr) else: # numerical parameter self.assertTrue(".integrator.setValue('{}', {})".format(name, value) in pystr)
import tellurium as te # works # inline_omex = te.convertCombineArchive('out_sedml.omex') # doesn't work inline_omex = te.convertCombineArchive('out_phrasedml.omex') print('inline omex:') print(inline_omex) te.exportInlineOmex(inline_omex, 'out_tellurium.omex')
get_ipython().magic(u'matplotlib inline') antimony_str = ''' model myModel S1 -> S2; k1*S1 S1 = 10; S2 = 0 k1 = 1 end ''' phrasedml_str = ''' model1 = model "myModel" sim1 = simulate uniform(0, 5, 100) task1 = run sim1 on model1 plot "Figure 1" time vs S1, S2 ''' # create an inline OMEX (inline representation of a COMBINE archive) # from the antimony and phrasedml strings inline_omex = '\n'.join([antimony_str, phrasedml_str]) # execute the inline OMEX te.executeInlineOmex(inline_omex) # export to a COMBINE archive workingDir = tempfile.mkdtemp(suffix="_omex") te.exportInlineOmex(inline_omex, os.path.join(workingDir, 'archive.omex')) # In[2]: te.executeCombineArchive(os.path.join(workingDir, 'archive.omex'))
def Combinecreator(core_model, settings, Plot_Variable, outputpath, KISAO_algorithm): ''' Generates and outputs COMBINE OMEX file. :param core_model: core model database of model :param settings: settings database of model :param Plot_Variable: Variables to be plotted in list format :param outputpath: filepath to output OMEX file in OS path format :param KISAO_algorithm: Kinetic Simulation Algorithm Ontology number in string format :return combine_filename : OMEX file name in string format :return total_scenarios : Total number of scenarios in string format ''' #--- Extract data from settings and clean up units --- number_init = len(settings['init']) number_parameters = len(settings['parameters']) addparam = settings['parameters'] unit_model = sbmlgen.unitlookup(settings) total_scenarios = number_init * number_parameters #print(total_scenarios) scenario_name = core_model["system_type"] scenario_name = scenario_name.replace(", ", "_") modelname_file = [] antimony_final = [] #--- Generates SBML string and adds to list #based on the number of combinations with init #and parameter amts --- for j in range(number_init): #Cycle through number of init for k in range(number_parameters): #Cycle through number of parameters sbmlstr = sbmlgen.SBMLcreation(core_model, settings, unit_model, addparam, j, k) antimony_final.append(te.sbmlToAntimony(sbmlstr)) #--- Generates the scenario name for each SBML string generated --- scenario_placeholder = [] for l in range(total_scenarios): for j in range(number_init): #Cycle through number of init for k in range(number_parameters): temp_scenario_name = '_' + str(j+1)+ '_' + str(k+1) scenario_placeholder.append(temp_scenario_name) #--- Names the models in antimony string according the scenario --- inline_omex = '\n' for l in range(total_scenarios): scenario_name = scenario_name + scenario_placeholder[l] modelname_file.append(scenario_name) antimony_final[l] = antimony_final[l].replace('*doc0', modelname_file[l]) scenario_name = scenario_name.replace(scenario_placeholder[l], "") inline_omex = inline_omex + str(antimony_final[l]) #print("This is antimony_final", scenario_number,": \n", antimony_final[l]) #--- Generates phrasedml file --- phrasedml_final = gen_phrasedml(settings, modelname_file, Plot_Variable, KISAO_algorithm) #--- Generates and outputs OMEX file--- inline_omex = inline_omex + phrasedml_final combine_filename = 'COMBINE_' + scenario_name + '.omex' archive = os.path.join(outputpath, combine_filename) print('COMBINE Archive output: ', archive) te.exportInlineOmex(inline_omex, archive) return combine_filename, total_scenarios
model myModel S1 -> S2; k1*S1 S1 = 10; S2 = 0 k1 = 1 end ''' phrasedml_str = ''' model1 = model "myModel" sim1 = simulate uniform(0, 5, 100) task1 = run sim1 on model1 plot "Figure 1" time vs S1, S2 ''' # create an inline OMEX (inline representation of a COMBINE archive) # from the antimony and phrasedml strings inline_omex = '\n'.join([antimony_str, phrasedml_str]) # execute the inline OMEX te.executeInlineOmex(inline_omex) # export to a COMBINE archive workingDir = tempfile.mkdtemp(suffix="_omex") te.exportInlineOmex(inline_omex, os.path.join(workingDir, 'archive.omex')) # In[2]: te.executeCombineArchive(os.path.join(workingDir, 'archive.omex'))
def test_exportAsCombine(self): """ Test exportAsCombine. """ inline_omex = '\n'.join([self.antimony, self.phrasedml]) tmpdir = tempfile.mkdtemp() te.exportInlineOmex(inline_omex, os.path.join(tmpdir, 'archive.omex')) shutil.rmtree(tmpdir)