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 test_via_sedml_string(self): """Test SED-ML from string.""" sedml_string = """<?xml version="1.0" encoding="UTF-8"?> <!-- Created by phraSED-ML version v0.5beta on 2016-01-31 22:02 with libSBML version 5.12.1. --> <sedML xmlns="http://sed-ml.org/sed-ml/level1/version2" level="1" version="2"> <listOfSimulations> <uniformTimeCourse id="sim1" initialTime="0" outputStartTime="0" outputEndTime="5" numberOfPoints="100"> <algorithm kisaoID="KISAO:0000019"/> </uniformTimeCourse> </listOfSimulations> <listOfModels> <model id="model1" language="urn:sedml:language:sbml.level-3.version-1" source="myModel"/> </listOfModels> <listOfTasks> <task id="task1" modelReference="model1" simulationReference="sim1"/> </listOfTasks> <listOfDataGenerators> <dataGenerator id="plot_0_0_0" name="time"> <listOfVariables> <variable id="time" symbol="urn:sedml:symbol:time" taskReference="task1"/> </listOfVariables> <math xmlns="http://www.w3.org/1998/Math/MathML"> <ci> time </ci> </math> </dataGenerator> <dataGenerator id="plot_0_0_1" name="S1"> <listOfVariables> <variable id="S1" target="/sbml:sbml/sbml:model/descendant::*[@id='S1']" taskReference="task1" modelReference="model1"/> </listOfVariables> <math xmlns="http://www.w3.org/1998/Math/MathML"> <ci> S1 </ci> </math> </dataGenerator> <dataGenerator id="plot_0_1_1" name="S2"> <listOfVariables> <variable id="S2" target="/sbml:sbml/sbml:model/descendant::*[@id='S2']" taskReference="task1" modelReference="model1"/> </listOfVariables> <math xmlns="http://www.w3.org/1998/Math/MathML"> <ci> S2 </ci> </math> </dataGenerator> </listOfDataGenerators> <listOfOutputs> <plot2D id="plot_0" name="Figure 1"> <listOfCurves> <curve logX="false" logY="false" xDataReference="plot_0_0_0" yDataReference="plot_0_0_1"/> <curve logX="false" logY="false" xDataReference="plot_0_0_0" yDataReference="plot_0_1_1"/> </listOfCurves> </plot2D> </listOfOutputs> </sedML> """ python_str = tesedml.sedmlToPython(sedml_string) self.assertIsNotNone(python_str)
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 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 _toPython(self, phrasedmlStr, workingDir=None): """ Create and return python script given phrasedml string. :param phrasedmlStr: Name of PhraSEDML string defined in the code :type phrasedmlStr: str :returns: python string to execute :rtype: str """ warnings.warn('Use inline_omex instead.', DeprecationWarning) if phrasedmlStr is None: phrasedmlStr = self._getDefaultPhrasedml() # Models have to be resolved from phrasedml string and set as referenced phrasedml.clearReferencedSBML() self._setReferencedSBML(phrasedmlStr) # check if conversion is possible self._phrasedmlToSEDML(phrasedmlStr) # create temporary working directory import tempfile isTmpDir = False if workingDir is None: workingDir = tempfile.mkdtemp(suffix="_sedml") isTmpDir = True # Export archive expId = self.getId() expArchive = os.path.join(workingDir, "{}.omex".format(expId)) self.exportAsCombine(expArchive) # Create python code from archive # This is the clean way to generate the code ! pycode = tesedml.sedmlToPython(expArchive) # remove the temporary directory if isTmpDir: shutil.rmtree(workingDir) return pycode
def single_check(self, f_sedml): """ Test if python code can be generated from the SED-ML file. :param f_sedml: :type f_sedml: :return: :rtype: """ directory = os.path.dirname(f_sedml) basename = os.path.basename(f_sedml) dir_results = os.path.join(directory, 'results') python_str = tesedml.sedmlToPython(f_sedml) self.assertIsNotNone(python_str) # create the python code file if not os.path.exists(dir_results): os.mkdir(dir_results) file_py = os.path.join(dir_results, basename + '.py') with open(file_py, 'w') as f_py: f_py.write(python_str)
def single_check(self, f_sedml): python_str = tesedml.sedmlToPython(f_sedml) self.assertIsNotNone(python_str) # create the python code file with open(f_sedml+'.py', 'w') as f_py: f_py.write(python_str)
def single_check(self, f_sedml): python_str = tesedml.sedmlToPython(f_sedml) self.assertIsNotNone(python_str) # create the python code file with open(f_sedml + '.py', 'w') as f_py: f_py.write(python_str)