def create_module(sbml_model_file, model_name, model_output_dir): """Create Python module from SBML model Arguments: sbml_model_file: SBML file model_name: Name of the model model_output_dir: Directory for model code Returns: list of parameters, dictionary with observables (id: formula) """ sbml_importer = amici.SbmlImporter(sbml_model_file) sbml_model = sbml_importer.sbml observables = petab.get_observables(sbml_model, remove=True) sigmas = petab.get_sigmas(sbml_model, remove=True) fixed_parameters = ['k0'] sbml_importer.sbml2amici(modelName=model_name, output_dir=model_output_dir, observables=observables, constantParameters=fixed_parameters, sigmas=sigmas) return fixed_parameters, observables
def simulate_AMICI(sbml_name: str): """ Compiles, simulates and plots the SBML in AMICI. :param yaml_name: :return: """ model_name = os.path.splitext(sbml_name)[0] model_output_dir = 'AMICI_models' sbml_importer = amici.SbmlImporter(sbml_name) sbml_importer.sbml2amici(model_name, model_output_dir) # import model sys.path.insert(0, os.path.abspath(model_output_dir)) model_module = importlib.import_module(model_name) # create model + solver instance amici_model = model_module.getModel() solver = amici_model.getSolver() # Define time points ans run simulation using default model parameters/solver options amici_model.setTimepoints(np.linspace(0, 5, 101)) rdata = amici.runAmiciSimulation(amici_model, solver) return amici_model, rdata
def load_model_objective(example_name): # name of the model that will also be the name of the python module model_name = 'model_' + example_name # sbml file sbml_file = os.path.join('doc', 'example', example_name, model_name + '.xml') # directory to which the generated model code is written model_output_dir = os.path.join('doc', 'example', 'tmp', model_name) # import sbml model, compile and generate amici module sbml_importer = amici.SbmlImporter(sbml_file) sbml_importer.sbml2amici(model_name, model_output_dir, verbose=False) # load amici module (the usual starting point later for the analysis) sys.path.insert(0, os.path.abspath(model_output_dir)) model_module = importlib.import_module(model_name) model = model_module.getModel() model.requireSensitivitiesForAllParameters() model.setTimepoints(np.linspace(0, 10, 11)) model.setParameterScale(amici.ParameterScaling_log10) model.setParameters([-0.3, -0.7]) solver = model.getSolver() solver.setSensitivityMethod(amici.SensitivityMethod_forward) solver.setSensitivityOrder(amici.SensitivityOrder_first) # generate experimental data rdata = amici.runAmiciSimulation(model, solver, None) edata = amici.ExpData(rdata, 0.05, 0.0) return (pypesto.AmiciObjective(model, solver, [edata], 2), model)
def sbml_example_presimulation_module(): """SBML example_presimulation model module fixture""" sbml_file = os.path.join(os.path.dirname(__file__), '..', 'examples', 'example_presimulation', 'model_presimulation.xml') sbml_importer = amici.SbmlImporter(sbml_file) constant_parameters = ['DRUG_0', 'KIN_0'] observables = amici.assignmentRules2observables( sbml_importer.sbml, # the libsbml model object filter_function=lambda variable: variable.getName() == 'pPROT_obs' ) outdir = 'test_model_presimulation' module_name = 'test_model_presimulation' sbml_importer.sbml2amici( model_name=module_name, output_dir=outdir, verbose=False, observables=observables, constant_parameters=constant_parameters) model_module = amici.import_model_module(module_name=module_name, module_path=outdir) yield model_module shutil.rmtree(outdir, ignore_errors=True)
def setUp(self): self.default_path = copy.copy(sys.path) self.resetdir = os.getcwd() if os.path.dirname(__file__) != '': os.chdir(os.path.dirname(__file__)) sbmlFile = os.path.join(os.path.dirname(__file__), '..', 'python', 'examples', 'example_presimulation', 'model_presimulation.xml') sbmlImporter = amici.SbmlImporter(sbmlFile) constantParameters = ['DRUG_0', 'KIN_0'] observables = amici.assignmentRules2observables( sbmlImporter.sbml, # the libsbml model object filter_function=lambda variable: variable.getName() == 'pPROT' ) outdir = 'test_model_presimulation' sbmlImporter.sbml2amici('test_model_presimulation', outdir, verbose=False, observables=observables, constantParameters=constantParameters) sys.path.insert(0, outdir) import test_model_presimulation as modelModule self.model = modelModule.getModel() self.model.setTimepoints(np.linspace(0, 60, 61)) self.solver = self.model.getSolver() rdata = amici.runAmiciSimulation(self.model, self.solver) self.edata = [amici.ExpData(rdata, 0.01, 0)] # test copy constructor self.edata_copy = amici.ExpData(self.edata[0])
def test_presimulation(self): sbmlFile = os.path.join(os.path.dirname(__file__), '..', 'python', 'examples', 'example_presimulation', 'model_presimulation.xml') sbmlImporter = amici.SbmlImporter(sbmlFile) constantParameters = ['DRUG_0', 'KIN_0'] observables = amici.assignmentRules2observables( sbmlImporter.sbml, # the libsbml model object filter_function=lambda variable: variable.getName() == 'pPROT') outdir = 'test_model_presimulation' sbmlImporter.sbml2amici('test_model_presimulation', outdir, verbose=False, observables=observables, constantParameters=constantParameters) sys.path.insert(0, outdir) import test_model_presimulation as modelModule model = modelModule.getModel() solver = model.getSolver() model.setTimepoints(amici.DoubleVector(np.linspace(0, 60, 61))) model.setReinitializeFixedParameterInitialStates(True) rdata = amici.runAmiciSimulation(model, solver) edata = amici.ExpData(rdata, 0.1, 0.0) edata.fixedParameters = amici.DoubleVector([10, 2]) edata.fixedParametersPresimulation = amici.DoubleVector([10, 2]) edata.fixedParametersPreequilibration = amici.DoubleVector([3, 0]) self.assertIsInstance(amici.runAmiciSimulation(model, solver, edata), dict)
def model_steadystate_module(): sbml_file = os.path.join(os.path.dirname(__file__), '..', 'examples', 'example_steadystate', 'model_steadystate_scaled.xml') sbml_importer = amici.SbmlImporter(sbml_file) observables = amici.assignmentRules2observables( sbml_importer.sbml, filter_function=lambda variable: variable.getId().startswith('observable_') and not variable.getId().endswith('_sigma') ) outdir = 'test_model_steadystate_scaled' module_name = 'test_model_steadystate_scaled' sbml_importer.sbml2amici( model_name=module_name, output_dir=outdir, observables=observables, constant_parameters=['k0'], sigmas={'observable_x1withsigma': 'observable_x1withsigma_sigma'}) model_module = amici.import_model_module(module_name=module_name, module_path=outdir) yield model_module shutil.rmtree(outdir, ignore_errors=True)
def model_units_module(): sbml_file = os.path.join(os.path.dirname(__file__), '..', 'examples', 'example_units', 'model_units.xml') sbml_importer = amici.SbmlImporter(sbml_file) outdir = 'test_model_units' module_name = 'test_model_units' sbml_importer.sbml2amici(model_name=module_name, output_dir=outdir) model_module = amici.import_model_module(module_name=module_name, module_path=outdir) yield model_module shutil.rmtree(outdir, ignore_errors=True)
def model_test_likelihoods(): sbml_file = os.path.join(os.path.dirname(__file__), '..', 'examples', 'example_steadystate', 'model_steadystate_scaled.xml') sbml_importer = amici.SbmlImporter(sbml_file) observables = amici.assignmentRules2observables( sbml_importer.sbml, filter_function=lambda variable: variable.getId().startswith('observable_') and not variable.getId().endswith('_sigma') ) # assign different noise models obs_keys = list(observables.keys()) # exponentiate observable formulas obs1 = observables[obs_keys[1]] obs3 = observables[obs_keys[3]] obs1['formula'] = '10^(' + obs1['formula'] + ')' obs3['formula'] = 'exp(' + obs3['formula'] + ')' # customize noise distributions noise_distributions = { obs_keys[0]: 'normal', obs_keys[1]: 'log-normal', obs_keys[2]: 'laplace', obs_keys[3]: 'log10-laplace', } module_name = 'test_likelihoods' outdir = 'test_likelihoods' sbml_importer.sbml2amici( model_name=module_name, output_dir=outdir, observables=observables, constant_parameters=['k0'], sigmas={'observable_x1withsigma': 'observable_x1withsigma_sigma'}, noise_distributions=noise_distributions ) yield amici.import_model_module(module_name=module_name, module_path=outdir) shutil.rmtree(outdir, ignore_errors=True)
def generate_models(): # SBML model we want to import sbml_file = os.path.join(os.path.dirname(__file__), '..', 'examples', 'example_constant_species', 'model_constant_species.xml') sbml_importer = amici.SbmlImporter(sbml_file) # Name of the model that will also be the name of the python module model_name = model_output_dir = 'model_constant_species' model_name_cl = model_output_dir_cl = 'model_constant_species_cl' # Define constants, observables, sigmas constant_parameters = ['synthesis_substrate', 'init_enzyme'] observables = { 'observable_product': { 'name': '', 'formula': 'product' }, 'observable_substrate': { 'name': '', 'formula': 'substrate' }, } sigmas = {'observable_product': 1.0, 'observable_substrate': 1.0} # wrap models with and without conservations laws sbml_importer.sbml2amici(model_name_cl, model_output_dir_cl, observables=observables, constant_parameters=constant_parameters, sigmas=sigmas) sbml_importer.sbml2amici(model_name, model_output_dir, observables=observables, constant_parameters=constant_parameters, sigmas=sigmas, compute_conservation_laws=False) # load both models model_without_cl_module = amici.import_model_module( model_name, module_path=os.path.abspath(model_name)) model_with_cl_module = amici.import_model_module( model_name_cl, module_path=os.path.abspath(model_name_cl)) # get the models and return model_without_cl = model_without_cl_module.getModel() model_with_cl = model_with_cl_module.getModel() return model_with_cl, model_without_cl
def test_presimulation(self): def assert_fun(x): return self.assertTrue(x) sbmlFile = os.path.join(os.path.dirname(__file__), '..', 'python', 'examples', 'example_presimulation', 'model_presimulation.xml') sbmlImporter = amici.SbmlImporter(sbmlFile) constantParameters = ['DRUG_0', 'KIN_0'] observables = amici.assignmentRules2observables( sbmlImporter.sbml, # the libsbml model object filter_function=lambda variable: variable.getName() == 'pPROT_obs' ) outdir = 'test_model_presimulation' sbmlImporter.sbml2amici('test_model_presimulation', outdir, verbose=False, observables=observables, constantParameters=constantParameters) sys.path.insert(0, outdir) import test_model_presimulation as modelModule model = modelModule.getModel() solver = model.getSolver() solver.setNewtonMaxSteps(0) model.setTimepoints(np.linspace(0, 60, 61)) model.setSteadyStateSensitivityMode( amici.SteadyStateSensitivityMode_simulationFSA ) solver.setSensitivityOrder(amici.SensitivityOrder_first) model.setReinitializeFixedParameterInitialStates(True) rdata = amici.runAmiciSimulation(model, solver) edata = amici.ExpData(rdata, 0.1, 0.0) edata.fixedParameters = [10, 2] edata.fixedParametersPresimulation = [10, 2] edata.fixedParametersPreequilibration = [3, 0] self.assertIsInstance( amici.runAmiciSimulation(model, solver, edata), amici.ReturnDataView) solver.setRelativeTolerance(1e-12) solver.setAbsoluteTolerance(1e-12) check_derivatives(model, solver, edata, assert_fun, epsilon=1e-4)
def compile_model(self): """ Compile the model. If the output folder exists already, it is first deleted. """ # check prerequisites if not petab.condition_table_is_parameter_free( self.petab_problem.condition_df): raise AssertionError( "Parameter dependent conditions in the condition file " "are not yet supported.") # delete output directory if os.path.exists(self.output_folder): shutil.rmtree(self.output_folder) # constant parameters condition_columns = self.petab_problem.condition_df.columns.values constant_parameter_ids = list( set(condition_columns) - {'conditionId', 'conditionName'}) # observables observables = self.petab_problem.get_observables() # sigmas sigmas = self.petab_problem.get_sigmas(remove=True) # noise distributions noise_distrs = _to_amici_noise_distributions( self.petab_problem.get_noise_distributions()) # model to string sbml_string = libsbml.SBMLWriter().writeSBMLToString( self.petab_problem.sbml_document) # init sbml importer sbml_importer = amici.SbmlImporter(sbml_string, from_file=False) # convert sbml_importer.sbml2amici(modelName=self.model_name, output_dir=self.output_folder, observables=observables, constantParameters=constant_parameter_ids, sigmas=sigmas, noise_distributions=noise_distrs)
def test_sympy_exp_monkeypatch(): """ This model contains a removeable discontinuity at t=0 that requires monkeypatching sympy.Pow._eval_derivative in order to be able to compute non-nan sensitivities """ url = 'https://www.ebi.ac.uk/biomodels/model/download/BIOMD0000000529.2?' \ 'filename=BIOMD0000000529_url.xml' importer = amici.SbmlImporter(urlopen(url).read().decode('utf-8'), from_file=False) module_name = 'BIOMD0000000529' outdir = 'BIOMD0000000529' importer.sbml2amici(module_name, outdir) model_module = amici.import_model_module(module_name=module_name, module_path=outdir) model = model_module.getModel() model.setTimepoints(np.linspace(0, 8, 250)) model.requireSensitivitiesForAllParameters() model.setAlwaysCheckFinite(True) model.setParameterScale( amici.parameterScalingFromIntVector([ amici.ParameterScaling.none if re.match(r'n[0-9]+$', par_id) else amici.ParameterScaling.log10 for par_id in model.getParameterIds() ])) solver = model.getSolver() solver.setSensitivityMethod(amici.SensitivityMethod.forward) solver.setSensitivityOrder(amici.SensitivityOrder.first) rdata = amici.runAmiciSimulation(model, solver) # print sensitivity-related results assert rdata['status'] == amici.AMICI_SUCCESS check_derivatives(model, solver, None, assert_fun, atol=1e-2, rtol=1e-2, epsilon=1e-3)
def compile_model(path, test_id, model_dir): """Import the given test model to AMICI""" sbml_file = find_model_file(path, test_id) wrapper = amici.SbmlImporter(sbml_file) if not os.path.exists(model_dir): os.makedirs(model_dir) model_name = 'SBMLTest' + test_id wrapper.sbml2amici(model_name, output_dir=model_dir, generate_sensitivity_code=False) # settings model_module = amici.import_model_module(model_name, model_dir) model = model_module.getModel() solver = model.getSolver() return model, solver, wrapper
def model_test_likelihoods(): """Test model for various likelihood functions.""" # load sbml model sbml_file = os.path.join(os.path.dirname(__file__), '..', 'examples', 'example_steadystate', 'model_steadystate_scaled.xml') sbml_importer = amici.SbmlImporter(sbml_file) # define observables observables = { 'o1': {'formula': 'x1'}, 'o2': {'formula': '10^x1'}, 'o3': {'formula': '10^x1'}, 'o4': {'formula': 'x1'}, 'o5': {'formula': '10^x1'}, 'o6': {'formula': '10^x1'}, 'o7': {'formula': 'x1'} } # define different noise models noise_distributions = { 'o1': 'normal', 'o2': 'log-normal', 'o3': 'log10-normal', 'o4': 'laplace', 'o5': 'log-laplace', 'o6': 'log10-laplace', 'o7': lambda str_symbol: f'Abs({str_symbol} - m{str_symbol}) ' f'/ sigma{str_symbol}', } module_name = 'test_likelihoods' outdir = 'test_likelihoods' sbml_importer.sbml2amici( model_name=module_name, output_dir=outdir, observables=observables, constant_parameters=['k0'], noise_distributions=noise_distributions, ) yield amici.import_model_module(module_name=module_name, module_path=outdir) shutil.rmtree(outdir, ignore_errors=True)
def compile_model(path, test_id): """Import the given test model to AMICI""" sbml_file = find_model_file(path, test_id) wrapper = amici.SbmlImporter(sbml_file) model_dir = os.path.join(os.path.dirname(__file__), 'SBMLTestModels', test_id) if not os.path.exists(model_dir): os.makedirs(model_dir) model_name = 'SBMLTest' + test_id wrapper.sbml2amici(model_name, output_dir=model_dir) # settings sys.path.insert(0, model_dir) model_module = importlib.import_module(model_name) model = model_module.getModel() solver = model.getSolver() return model, solver, wrapper
def model_special_likelihoods(): """Test model for special likelihood functions.""" # load sbml model sbml_file = os.path.join(os.path.dirname(__file__), '..', 'examples', 'example_steadystate', 'model_steadystate_scaled.xml') sbml_importer = amici.SbmlImporter(sbml_file) # define observables observables = { 'o1': { 'formula': '100*10^x1' }, 'o2': { 'formula': '100*10^x1' }, } # define different noise models noise_distributions = { 'o1': 'binomial', 'o2': 'negative-binomial', } module_name = 'test_special_likelihoods' outdir = 'test_special_likelihoods' sbml_importer.sbml2amici( model_name=module_name, output_dir=outdir, observables=observables, constant_parameters=['k0'], noise_distributions=noise_distributions, ) yield amici.import_model_module(module_name=module_name, module_path=outdir) shutil.rmtree(outdir, ignore_errors=True)
def test_likelihoods_error(): """Test whether wrong inputs lead to expected errors.""" sbml_file = os.path.join(os.path.dirname(__file__), '..', 'examples', 'example_steadystate', 'model_steadystate_scaled.xml') sbml_importer = amici.SbmlImporter(sbml_file) # define observables observables = {'o1': {'formula': 'x1'}} # define different noise models noise_distributions = {'o1': 'nörmal'} module_name = 'test_likelihoods' outdir = 'test_likelihoods' with pytest.raises(ValueError): sbml_importer.sbml2amici( model_name=module_name, output_dir=outdir, observables=observables, constant_parameters=['k0'], noise_distributions=noise_distributions, )
def compile_model(self): """ Compile the model. If the output folder exists already, it is first deleted. """ # check prerequisites if not petab.condition_table_is_parameter_free( self.petab_problem.condition_df): raise AssertionError( "Parameter dependent conditions in the condition file " "are not yet supported.") # delete output directory if os.path.exists(self.output_folder): shutil.rmtree(self.output_folder) # init sbml importer sbml_importer = amici.SbmlImporter(self.petab_problem.sbml_file) # constant parameters condition_columns = self.petab_problem.condition_df.columns.values constant_parameter_ids = list( set(condition_columns) - {'conditionId', 'conditionName'}) # observables observables = petab.get_observables(sbml_importer.sbml) # sigmas sigmas = petab.get_sigmas(sbml_importer.sbml) # convert sbml_importer.sbml2amici(modelName=self.petab_problem.model_name, output_dir=self.output_folder, observables=observables, constantParameters=constant_parameter_ids, sigmas=sigmas)
def test_likelihoods(self): """ Test the custom noise distributions used to define cost functions. """ def assert_fun(x): return self.assertTrue(x) sbmlFile = os.path.join(os.path.dirname(__file__), '..', 'python', 'examples', 'example_steadystate', 'model_steadystate_scaled.xml') sbmlImporter = amici.SbmlImporter(sbmlFile) observables = amici.assignmentRules2observables( sbmlImporter.sbml, filter_function=lambda variable: variable.getId().startswith('observable_') and not variable.getId().endswith('_sigma') ) # assign different noise models obs_keys = list(observables.keys()) # exponentiate observable formulas obs1 = observables[obs_keys[1]] obs3 = observables[obs_keys[3]] obs1['formula'] = '10^(' + obs1['formula'] + ')' obs3['formula'] = 'exp(' + obs3['formula'] + ')' # customize noise distributions noise_distributions = { obs_keys[0]: 'normal', obs_keys[1]: 'log-normal', obs_keys[2]: 'laplace', obs_keys[3]: 'log10-laplace', } outdir = 'test_likelihoods' sbmlImporter.sbml2amici('test_likelihoods', outdir, observables=observables, constantParameters=['k0'], sigmas={'observable_x1withsigma': 'observable_x1withsigma_sigma'}, noise_distributions=noise_distributions ) sys.path.insert(0, outdir) import test_likelihoods as modelModule model = modelModule.getModel() model.setTimepoints(np.linspace(0, 60, 60)) solver = model.getSolver() solver.setSensitivityOrder(amici.SensitivityOrder_first) # run model once to create an edata rdata = amici.runAmiciSimulation(model, solver) edata = [amici.ExpData(rdata, 1, 0)] # just make all observables positive since some are logarithmic for ed in edata: y = ed.getObservedData() y = tuple([max(val, 1e-4) for val in y]) ed.setObservedData(y) # and now run for real and also compute likelihood values rdata = amici.runAmiciSimulations(model, solver, edata)[0] # output for easy debugging for key in ['llh', 'sllh']: print(key, rdata[key]) # it would be good to compute the expected llh+sllh by hand, # here, we only check if they make overall sense self.assertTrue(np.isfinite(rdata['llh'])) self.assertTrue(np.all(np.isfinite(rdata['sllh']))) self.assertTrue(np.any(rdata['sllh']))
# sbml_model.setName(model_name+'_Annot') # sbml_model.setId(model_name+'_Annot') # sbml_filewAnnot = model_name+'_Annot.xml' # writer = libsbml.SBMLWriter() # writer.writeSBML(sbml_doc, sbml_filewAnnot) # sbml_file = sbml_filewAnnot # Comment-out to use the file name w/t annotations model_name = sbml_file[0:-4] model_output_dir = model_name sbml_reader = libsbml.SBMLReader() sbml_doc = sbml_reader.readSBML(sbml_file) sbml_model = sbml_doc.getModel() # Create an SbmlImporter instance for our SBML model sbml_importer = amici.SbmlImporter(sbml_file) constantParameters = [ params.getId() for params in sbml_model.getListOfParameters() ] observables = { 'actp53': { 'formula': 'p53ac' }, 'phERK': { 'formula': 'ppERK' }, 'egfLR': { 'formula': 'EE1' }
def test_steadystate_scaled(self): ''' Test SBML import and simulation from AMICI python interface ''' sbmlFile = os.path.join(os.path.dirname(__file__), '..', 'python', 'examples', 'example_steadystate', 'model_steadystate_scaled.xml') sbmlImporter = amici.SbmlImporter(sbmlFile) observables = amici.assignmentRules2observables( sbmlImporter.sbml, filter_function=lambda variable: variable.getId().startswith( 'observable_') and not variable.getId().endswith('_sigma')) outdir = 'test_model_steadystate_scaled' sbmlImporter.sbml2amici( 'test_model_steadystate_scaled', outdir, observables=observables, constantParameters=['k0'], sigmas={'observable_x1withsigma': 'observable_x1withsigma_sigma'}) sys.path.insert(0, outdir) import test_model_steadystate_scaled as modelModule model = modelModule.getModel() model.setTimepoints(amici.DoubleVector(np.linspace(0, 60, 60))) solver = model.getSolver() rdata = amici.runAmiciSimulation(model, solver) edata = [amici.ExpData(rdata, 0.01, 0)] rdata = amici.runAmiciSimulations(model, solver, edata) # check roundtripping of DataFrame conversion df_edata = amici.getDataObservablesAsDataFrame(model, edata) edata_reconstructed = amici.getEdataFromDataFrame(model, df_edata) self.assertTrue( np.isclose( amici.edataToNumPyArrays(edata[0])['observedData'], amici.edataToNumPyArrays( edata_reconstructed[0])['observedData'], ).all()) self.assertTrue( np.isclose( amici.edataToNumPyArrays(edata[0])['observedDataStdDev'], amici.edataToNumPyArrays( edata_reconstructed[0])['observedDataStdDev'], ).all()) if edata[0].fixedParameters.size(): self.assertListEqual( list(edata[0].fixedParameters), list(edata_reconstructed[0].fixedParameters), ) else: self.assertListEqual( list(model.getFixedParameters()), list(edata_reconstructed[0].fixedParameters), ) self.assertListEqual( list(edata[0].fixedParametersPreequilibration), list(edata_reconstructed[0].fixedParametersPreequilibration), ) df_state = amici.getSimulationStatesAsDataFrame(model, edata, rdata) self.assertTrue( np.isclose(rdata[0]['x'], df_state[list(model.getStateIds())].values).all()) df_obs = amici.getSimulationObservablesAsDataFrame(model, edata, rdata) self.assertTrue( np.isclose(rdata[0]['y'], df_obs[list(model.getObservableIds())].values).all()) amici.getResidualsAsDataFrame(model, edata, rdata)
def import_model(sbml_model: Union[str, 'libsbml.Model'], condition_table: Optional[Union[str, pd.DataFrame]] = None, observable_table: Optional[Union[str, pd.DataFrame]] = None, measurement_table: Optional[Union[str, pd.DataFrame]] = None, model_name: Optional[str] = None, model_output_dir: Optional[str] = None, verbose: Optional[Union[bool, int]] = True, allow_reinit_fixpar_initcond: bool = True, **kwargs) -> None: """ Create AMICI model from PEtab problem :param sbml_model: PEtab SBML model or SBML file name. :param condition_table: PEtab condition table. If provided, parameters from there will be turned into AMICI constant parameters (i.e. parameters w.r.t. which no sensitivities will be computed). :param observable_table: PEtab observable table. :param measurement_table: PEtab measurement table. :param model_name: Name of the generated model. If model file name was provided, this defaults to the file name without extension, otherwise the SBML model ID will be used. :param model_output_dir: Directory to write the model code to. Will be created if doesn't exist. Defaults to current directory. :param verbose: Print/log extra information. :param allow_reinit_fixpar_initcond: See :class:`amici.ode_export.ODEExporter`. Must be enabled if initial states are to be reset after preequilibration. :param kwargs: Additional keyword arguments to be passed to :meth:`amici.sbml_import.SbmlImporter.sbml2amici`. """ set_log_level(logger, verbose) logger.info(f"Importing model ...") # Get PEtab tables observable_df = petab.get_observable_df(observable_table) # to determine fixed parameters condition_df = petab.get_condition_df(condition_table) if observable_df is None: raise NotImplementedError("PEtab import without observables table " "is currently not supported.") # Model name from SBML ID or filename if model_name is None: if isinstance(sbml_model, libsbml.Model): model_name = sbml_model.getId() else: model_name = os.path.splitext(os.path.split(sbml_model)[-1])[0] if model_output_dir is None: model_output_dir = os.path.join(os.getcwd(), model_name) logger.info(f"Model name is '{model_name}'. " f"Writing model code to '{model_output_dir}'.") # Load model if isinstance(sbml_model, str): # from file sbml_reader = libsbml.SBMLReader() sbml_doc = sbml_reader.readSBMLFromFile(sbml_model) sbml_model = sbml_doc.getModel() else: # Create a copy, because it will be modified by SbmlImporter sbml_doc = sbml_model.getSBMLDocument().clone() sbml_model = sbml_doc.getModel() show_model_info(sbml_model) sbml_importer = amici.SbmlImporter(sbml_model) sbml_model = sbml_importer.sbml if observable_df is not None: observables, noise_distrs, sigmas = \ get_observation_model(observable_df) logger.info(f'Observables: {len(observables)}') logger.info(f'Sigmas: {len(sigmas)}') if not len(sigmas) == len(observables): raise AssertionError( f'Number of provided observables ({len(observables)}) and sigmas ' f'({len(sigmas)}) do not match.') # TODO: adding extra output parameters is currently not supported, # so we add any output parameters to the SBML model. # this should be changed to something more elegant # <BeginWorkAround> formulas = chain((val['formula'] for val in observables.values()), sigmas.values()) output_parameters = OrderedDict() for formula in formulas: # we want reproducible parameter ordering upon repeated import free_syms = sorted(sp.sympify(formula).free_symbols, key=lambda symbol: symbol.name) for free_sym in free_syms: sym = str(free_sym) if sbml_model.getElementBySId(sym) is None: output_parameters[sym] = None logger.debug(f"Adding output parameters to model: {output_parameters}") for par in output_parameters.keys(): petab.add_global_parameter(sbml_model, par) # <EndWorkAround> # TODO: to parameterize initial states or compartment sizes, we currently # need initial assignments. if they occur in the condition table, we # create a new parameter initial_${startOrCompartmentID}. # feels dirty and should be changed (see also #924) # <BeginWorkAround> initial_states = [ col for col in condition_df if sbml_model.getSpecies(col) is not None ] initial_sizes = [ col for col in condition_df if sbml_model.getCompartment(col) is not None ] fixed_parameters = [] if len(initial_states) or len(initial_sizes): # add preequilibration indicator variable # NOTE: would only be required if we actually have preequilibration # adding it anyways. can be optimized-out later if sbml_model.getParameter(PREEQ_INDICATOR_ID) is not None: raise AssertionError("Model already has a parameter with ID " f"{PREEQ_INDICATOR_ID}. Cannot handle " "species and compartments in condition table " "then.") indicator = sbml_model.createParameter() indicator.setId(PREEQ_INDICATOR_ID) indicator.setName(PREEQ_INDICATOR_ID) # Can only reset parameters after preequilibration if they are fixed. fixed_parameters.append(PREEQ_INDICATOR_ID) for assignee_id in initial_sizes + initial_states: init_par_id_preeq = f"initial_{assignee_id}_preeq" init_par_id_sim = f"initial_{assignee_id}_sim" for init_par_id in [init_par_id_preeq, init_par_id_sim]: if sbml_model.getElementBySId(init_par_id) is not None: raise ValueError( "Cannot create parameter for initial assignment " f"for {assignee_id} because an entity named " f"{init_par_id} exists already in the model.") init_par = sbml_model.createParameter() init_par.setId(init_par_id) init_par.setName(init_par_id) assignment = sbml_model.createInitialAssignment() assignment.setSymbol(assignee_id) formula = f'{PREEQ_INDICATOR_ID} * {init_par_id_preeq} '\ f'+ (1 - {PREEQ_INDICATOR_ID}) * {init_par_id_sim}' math_ast = libsbml.parseL3Formula(formula) assignment.setMath(math_ast) # <EndWorkAround> fixed_parameters.extend( get_fixed_parameters(sbml_model=sbml_model, condition_df=condition_df)) logger.debug(f"Fixed parameters are {fixed_parameters}") logger.info(f"Overall fixed parameters: {len(fixed_parameters)}") logger.info( "Variable parameters: " + str(len(sbml_model.getListOfParameters()) - len(fixed_parameters))) # Create Python module from SBML model sbml_importer.sbml2amici( model_name=model_name, output_dir=model_output_dir, observables=observables, constant_parameters=fixed_parameters, sigmas=sigmas, allow_reinit_fixpar_initcond=allow_reinit_fixpar_initcond, noise_distributions=noise_distrs, verbose=verbose, **kwargs)
def runTest(testId, logfile): try: current_test_path = os.path.join(test_path, testId) # results resultsFile = os.path.join(current_test_path, testId + '-results.csv') results = np.genfromtxt(resultsFile, delimiter=',') # model sbmlFile = findModelFile(current_test_path, testId) wrapper = amici.SbmlImporter(sbmlFile) modelDir = os.path.join(os.path.dirname(__file__), 'SBMLTestModels', testId) if not os.path.exists(modelDir): os.makedirs(modelDir) wrapper.sbml2amici('SBMLTest' + testId, output_dir=modelDir) # settings settings = readSettingsFile(current_test_path, testId) ts = np.linspace( float(settings['start']), float(settings['start']) + float(settings['duration']), int(settings['steps']) + 1) atol = float(settings['absolute']) rtol = float(settings['relative']) sys.path.insert(0, wrapper.modelPath) mod = importlib.import_module(wrapper.modelName) model = mod.getModel() model.setTimepoints(ts) solver = model.getSolver() solver.setMaxSteps(int(1e6)) solver.setRelativeTolerance(rtol / 1000.0) solver.setAbsoluteTolerance(atol / 1000.0) rdata = amici.runAmiciSimulation(model, solver) amountSpecies = settings['amount'].replace(' ', '').replace('\n', '').split(',') simulated_x = rdata['x'] test_x = results[1:, [ 1 + wrapper.speciesIndex[variable] for variable in settings['variables'].replace(' ', ''). replace('\n', '').split(',') if variable in wrapper.speciesIndex.keys() ]] for species in amountSpecies: if not species == '': volume = wrapper.speciesCompartment[ wrapper.speciesIndex[species]].subs( wrapper.compartmentSymbols, wrapper.compartmentVolume) simulated_x[:, wrapper.speciesIndex[ species]] = simulated_x[:, wrapper. speciesIndex[species]] * volume adev = abs(simulated_x - test_x) adev[np.isnan(adev)] = True rdev = abs((simulated_x - test_x) / test_x) rdev[np.isnan(rdev)] = True if not np.all(np.logical_or(adev < atol, rdev < rtol)): if (not np.all(adev < atol)): raise Exception('Absolute tolerance violated') if (not np.all(rdev < rtol)): raise Exception('Relative tolerance violated') except amici.SBMLException as err: print("Did not run test " + testId + ": {0}".format(err)) except Exception as err: str = "Failed test " + testId + ": {0}".format(err) traceback.print_exc(10) logfile.write(str + '\n') return
def conversion_reaction_model(): # read in sbml file model_name = 'conversion_reaction' example_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'doc', 'example') sbml_file = os.path.join(example_dir, model_name, f'model_{model_name}.xml') model_output_dir = os.path.join(example_dir, 'tmp', f'{model_name}_enhanced') # try to import the exisiting model, if possible try: sys.path.insert(0, os.path.abspath(model_output_dir)) model_module = amici.import_model_module(model_name, model_output_dir) model = model_module.getModel() except ValueError: # read in and adapt the sbml slightly if os.path.abspath(model_output_dir) in sys.path: sys.path.remove(os.path.abspath(model_output_dir)) sbml_importer = amici.SbmlImporter(sbml_file) # add observables to sbml model def create_observable(sbml_model, obs_id): # create a parameter, which will get a rule assignmed as observable parameter = sbml_model.createParameter() parameter.setId(f'observable_{obs_id}') parameter.setName(f'observable_{obs_id}') parameter.constant = True rule = sbml_importer.sbml.createAssignmentRule() rule.setId(f'observable_{obs_id}') rule.setName(f'observable_{obs_id}') rule.setVariable(f'observable_{obs_id}') rule.setFormula(obs_id) # add initial assignments to sbml model def create_intial_assignment(sbml_model, spec_id): # create a parameter, which will get a rule assignmed as observable parameter = sbml_model.createParameter() parameter.setId(f'{spec_id}0') parameter.setName(f'{spec_id}0') parameter.constant = True assignment = sbml_importer.sbml.createInitialAssignment() assignment.setSymbol(f'{spec_id}') math = ('<math xmlns="http://www.w3.org/1998/Math/MathML"><ci>' f'{spec_id}0</ci></math>') assignment.setMath(libsbml.readMathMLFromString(math)) for spec in ('A', 'B'): create_observable(sbml_importer.sbml, spec) create_intial_assignment(sbml_importer.sbml, spec) # add constant parameters and observables to AMICI model constant_parameters = ['A0', 'B0'] observables = amici.assignmentRules2observables( sbml_importer.sbml, # the libsbml model object filter_function=lambda variable: variable.getId().startswith( 'observable_'), ) # generate the python module for the model. sbml_importer.sbml2amici( model_name, model_output_dir, verbose=False, observables=observables, constant_parameters=constant_parameters, ) # Importing the module and loading the model sys.path.insert(0, os.path.abspath(model_output_dir)) model_module = amici.import_model_module(model_name, model_output_dir) model = model_module.getModel() except RuntimeError as err: print('pyPESTO unit test ran into an error importing the conversion ' 'reaction enhanced model. This may happen due to an old version ' 'of this model being present in your python path (e.g., ' 'incorrect AMICI version comparing to the installed one). ' 'Delete the conversion_reaction_enhanced model from your python ' 'path and retry. Your python path is currently:') print(sys.path) print('Original error message:') raise err return model
def import_model(sbml_file: str, condition_file: str, measurement_file: str = None, model_name: str = None, model_output_dir: str = None, verbose: bool = True, allow_reinit_fixpar_initcond: bool = False, **kwargs): """Import AMICI model""" if model_name is None: model_name = os.path.splitext(os.path.split(sbml_file)[-1])[0] if model_output_dir is None: model_output_dir = os.path.join(os.getcwd(), model_name) if verbose: print(f"{Fore.GREEN}Importing model '{sbml_file}' using fixed " f"parameters file '{condition_file}'") print(f"{Fore.GREEN}Model name is '{model_name}' Writing model code " f"to '{model_output_dir}'") sbml_importer = amici.SbmlImporter(sbml_file) sbml_model = sbml_importer.sbml show_model_info(sbml_model) observables = petab.get_observables(sbml_importer.sbml, remove=True) sigmas = petab.get_sigmas(sbml_importer.sbml, remove=True) measurement_df = petab.get_measurement_df(measurement_file) noise_distrs = petab_noise_distributions_to_amici( petab.get_noise_distributions(measurement_df)) # Replace observables in assignment import sympy as sp for observable_id, formula in sigmas.items(): repl = sp.sympify(formula).subs(observable_id, observables[observable_id]['formula']) sigmas[observable_id] = str(repl) if verbose: print('Observables', len(observables)) print('Sigmas', len(sigmas)) if not len(sigmas) == len(observables): raise AssertionError( f'Number of provided observables ({len(observables)}) and sigmas ' f'({len(sigmas)}) do not match.') fixed_parameters = get_fixed_parameters(condition_file, sbml_model) if verbose: print("Overall fixed parameters", len(fixed_parameters)) print("Non-constant global parameters", len(sbml_model.getListOfParameters()) - len(fixed_parameters)) # Create Python module from SBML model start = time.time() sbml_importer.sbml2amici( model_name, output_dir=model_output_dir, observables=observables, constantParameters=fixed_parameters, sigmas=sigmas, allow_reinit_fixpar_initcond=allow_reinit_fixpar_initcond, noise_distributions=noise_distrs, **kwargs) end = time.time() if verbose: print(f"{Fore.GREEN}Model imported successfully in " f"{round(end - start, 2)}s")
def test_steadystate_scaled(self): """ Test SBML import and simulation from AMICI python interface """ def assert_fun(x): return self.assertTrue(x) sbmlFile = os.path.join(os.path.dirname(__file__), '..', 'python', 'examples', 'example_steadystate', 'model_steadystate_scaled.xml') sbmlImporter = amici.SbmlImporter(sbmlFile) observables = amici.assignmentRules2observables( sbmlImporter.sbml, filter_function=lambda variable: variable.getId().startswith('observable_') and not variable.getId().endswith('_sigma') ) outdir = 'test_model_steadystate_scaled' sbmlImporter.sbml2amici('test_model_steadystate_scaled', outdir, observables=observables, constantParameters=['k0'], sigmas={'observable_x1withsigma': 'observable_x1withsigma_sigma'}) sys.path.insert(0, outdir) import test_model_steadystate_scaled as modelModule model = modelModule.getModel() model.setTimepoints(np.linspace(0, 60, 60)) solver = model.getSolver() solver.setSensitivityOrder(amici.SensitivityOrder_first) rdata = amici.runAmiciSimulation(model, solver) edata = [amici.ExpData(rdata, 1, 0)] rdata = amici.runAmiciSimulations(model, solver, edata) # check roundtripping of DataFrame conversion df_edata = amici.getDataObservablesAsDataFrame(model, edata) edata_reconstructed = amici.getEdataFromDataFrame(model, df_edata) self.assertTrue( np.isclose( amici.ExpDataView(edata[0]) ['observedData'], amici.ExpDataView(edata_reconstructed[0]) ['observedData'], ).all() ) self.assertTrue( np.isclose( amici.ExpDataView(edata[0]) ['observedDataStdDev'], amici.ExpDataView(edata_reconstructed[0]) ['observedDataStdDev'], ).all() ) if len(edata[0].fixedParameters): self.assertListEqual( list(edata[0].fixedParameters), list(edata_reconstructed[0].fixedParameters), ) else: self.assertListEqual( list(model.getFixedParameters()), list(edata_reconstructed[0].fixedParameters), ) self.assertListEqual( list(edata[0].fixedParametersPreequilibration), list(edata_reconstructed[0].fixedParametersPreequilibration), ) df_state = amici.getSimulationStatesAsDataFrame(model, edata, rdata) self.assertTrue( np.isclose( rdata[0]['x'], df_state[list(model.getStateIds())].values ).all() ) df_obs = amici.getSimulationObservablesAsDataFrame(model, edata, rdata) self.assertTrue( np.isclose( rdata[0]['y'], df_obs[list(model.getObservableIds())].values ).all() ) amici.getResidualsAsDataFrame(model, edata, rdata) solver.setRelativeTolerance(1e-12) solver.setAbsoluteTolerance(1e-12) check_derivatives(model, solver, edata[0], assert_fun, atol=1e-3, rtol=1e-3, epsilon=1e-4)
def test_compare_to_sbml_import(self): constant_parameters = ['DRUG_0', 'KIN_0'] # -------------- PYSB ----------------- pysb.SelfExporter.cleanup() # reset pysb pysb.SelfExporter.do_export = True sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'python', 'examples', 'example_presimulation')) if 'createModelPresimulation' in sys.modules: importlib.reload(sys.modules['createModelPresimulation']) model_module = sys.modules['createModelPresimulation'] else: model_module = importlib.import_module('createModelPresimulation') model = copy.deepcopy(model_module.model) model.name = 'test_model_presimulation_pysb' amici.pysb2amici(model, model.name, verbose=False, observables=['pPROT_obs'], constant_parameters=constant_parameters) sys.path.insert(0, model.name) import test_model_presimulation_pysb as modelModulePYSB model_pysb = modelModulePYSB.getModel() edata = get_data(model_pysb) rdata_pysb = get_results(model_pysb, edata) # -------------- SBML ----------------- sbmlFile = os.path.join(os.path.dirname(__file__), '..', 'python', 'examples', 'example_presimulation', 'model_presimulation.xml') sbmlImporter = amici.SbmlImporter(sbmlFile) observables = amici.assignmentRules2observables( sbmlImporter.sbml, # the libsbml model object filter_function=lambda variable: variable.getName() == 'pPROT_obs' ) outdir = 'test_model_presimulation_sbml' sbmlImporter.sbml2amici('test_model_presimulation_sbml', outdir, verbose=False, observables=observables, constantParameters=constant_parameters) sys.path.insert(0, outdir) import test_model_presimulation_sbml as modelModuleSBML model_sbml = modelModuleSBML.getModel() rdata_sbml = get_results(model_sbml, edata) for field in rdata_pysb: if field not in ['ptr', 't_steadystate', 'numsteps', 'newton_numsteps', 'numrhsevals', 'numerrtestfails', 'order', 'J', 'xdot', 'wrms_steadystate', ]: with self.subTest(field=field): if rdata_pysb[field] is None: self.assertIsNone( rdata_sbml[field], ) elif rdata_sbml[field] is None: self.assertIsNone( rdata_pysb[field], ) else: self.assertTrue(np.isclose( rdata_sbml[field], rdata_pysb[field], atol=1e-6, rtol=1e-6 ).all())
""" Testing basic functionality of amici. """ import os import amici if __name__ == "__main__": model_path = os.path.abspath( './examples/example_steadystate/model_steadystate_scaled.xml') if not os.path.exists(model_path): raise IOError(f"path does not exist: {model_path}") # SBML import sbml_importer = amici.SbmlImporter(model_path) # Constants # parameters that should be considered constants can be specified in a list of strings specifying the respective SbmlId of a parameter. constantParameters = ['k0'] observables = { 'observable_x1': { 'name': '', 'formula': 'x1' }, 'observable_x2': { 'name': '', 'formula': 'x2' }, 'observable_x3': { 'name': '', 'formula': 'x3'
def plotObservables(iModel, iFile): # call function 'getObservables()' #getAllObservables(iModel,iFile) # create folder for models with observables if not os.path.exists('../sbml2amici/amici_models_with_observables'): os.makedirs('../sbml2amici/amici_models_with_observables') # loop over all models #list_directory_sedml = os.listdir('./sedml_models') #for iModel in list_directory_sedml: # list_directory_sbml = os.listdir('./sedml_models' + iModel + '/sbml_models') # for iFile in list_directory_sbml: if os.path.exists( './BioModelsDatabase_models/' + iModel ): # no comparison for observables available, so no simulation needed # important paths model_output_dir = '../sbml2amici/amici_models_with_observables/' + iModel + '/' + iFile model_path = './sedml_models/' + iModel + '/sbml_models_with_observables/' + iFile + '_with_observabels.xml' ##### needs automatisation # sbml2amici import sbml_importer = amici.SbmlImporter(model_path) sbml_importer.sbml2amici(iModel, model_output_dir, verbose=False) else: # important paths model_output_dir = '../sbml2amici/amici_models_with_observables/' + iModel + '/' + iFile model_path = './sedml_models/' + iModel + '/sbml_models_with_observables/' + iFile + '_with_observabels.xml' sedml_path = './sedml_models/' + iModel + '/' + iModel + '.sedml' # get observables sbml_doc = libsbml.readSBML(model_path) model = sbml_doc.getModel() observables = get_observables(model, False) # sbml2amici import sbml_importer = amici.SbmlImporter('./sedml_models/' + iModel + '/sbml_models_with_observables/' + iFile + '_with_observabels.xml') sbml_importer.sbml2amici(iModel, model_output_dir, observables=observables, verbose=False) # load specific model sys.path.insert(0, os.path.abspath(model_output_dir)) model_module = importlib.import_module(iModel) model = model_module.getModel() # set time points sedml_file = libsedml.readSedML(sedml_path) for iTask in range(0, sedml_file.getNumTasks()): all_tasks = sedml_file.getTask(iTask) tsk_Id = all_tasks.getId() task_name = all_tasks.getName() task_modRef = all_tasks.getModelReference() task_simReference = all_tasks.getSimulationReference() # time courses try: all_simulations = sedml_file.getSimulation(iTask) sim_Id = all_simulations.getId() except: # need 'except' clause if more models have same time period if all_simulations == None: all_simulations = sedml_file.getSimulation(0) sim_Id = all_simulations.getId() try: while task_simReference != sim_Id: iTask = iTask + 1 all_simulations = sedml_file.getSimulation(iTask) sim_Id = all_simulations.getId() except: iTask = 0 while task_simReference != sim_Id: all_simulations = sedml_file.getSimulation(iTask) sim_Id = all_simulations.getId() iTask = iTask + 1 sim_start_time = all_simulations.getOutputStartTime() sim_end_time = all_simulations.getOutputEndTime() sim_num_time_points = all_simulations.getNumberOfPoints() model.setTimepoints( np.linspace(sim_start_time, sim_end_time, sim_num_time_points)) # print observables #print("Model observables: ", model.getObservableIds()) # get solver instance #solver = model.getSolver() # simulate model once #sim_data = amici.runAmiciSimulation(model,solver) # np.set_printoptions(threshold=8, edgeitems=2) #for key, value in sim_data.items(): # print('%12s: ' % key, value) # plot observable trajectories #amici.plotting.plotObservableTrajectories(sim_data) # amici.plotting.plotStateTrajectories(sim_data) # show plot #plt.show() return model