示例#1
0
文件: testSBML.py 项目: permfl/AMICI
    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)
示例#2
0
def getReturnDataForCondition(model, solver, condition, simulationParameters,
                              sigmay):
    model.setParameters(amici.DoubleVector(simulationParameters))

    # simulate without measurements
    edata = amici.ExpData(model.get())
    edata.fixedParameters = amici.DoubleVector(condition)
    edata.my = amici.DoubleVector(
        np.full(shape=model.nt() * model.nytrue, fill_value=np.nan))
    rdata = amici.runAmiciSimulation(model, solver, edata)
    # fixedParametersPreequilibration =

    # confirm gradient is 0 for real measurements and save expected llh
    measurement = rdata['y']
    measurement = np.random.normal(loc=rdata['y'], scale=sigmay)
    measurement = np.random.normal(loc=rdata['y'], scale=sigmay[0][0])
    # print(measurement)

    edata.my = amici.DoubleVector(measurement.flatten())
    edata.sigmay = amici.DoubleVector(sigmay.flatten())
    model.requireSensitivitiesForAllParameters()
    rdata = amici.runAmiciSimulation(model, solver, edata)
    # return generated noisy measurents
    rdata['y'] = measurement
    return rdata
示例#3
0
def edata_objects(conversion_reaction_model):
    testmodel = conversion_reaction_model

    # set timepoints for which we want to simulate the model
    testmodel.setTimepoints(np.linspace(0, 4, 10))
    testmodel.setParameters(np.array([4.0, 0.4]))
    # Create solver instance
    solver = testmodel.getSolver()

    # create edatas
    rdatas = []
    edatas = []
    fixedParameters = [
        np.array([2.0, 0.0]),
        np.array([0.0, 4.0]),
        np.array([1.0, 1.0]),
    ]
    # create rdatas and edatas from those
    for fp in fixedParameters:
        testmodel.setFixedParameters(amici.DoubleVector(fp))
        rdata = amici.runAmiciSimulation(testmodel, solver)
        rdatas.append(rdata)
        edatas.append(amici.ExpData(rdata, 1.0, 0))

    return testmodel, solver, edatas
示例#4
0
    def grad(x0, symbol='llh', x0full=None, plist=[], verbose=False):
        """Gradient which is to be checked"""
        old_parameters = model.getParameters()
        old_plist = model.getParameterList()

        p = x0
        if len(plist):
            model.setParameterList(amici.IntVector(plist))
            p = x0full[:]
            p[plist] = x0
        else:
            model.requireSensitivitiesForAllParameters()
        verbose and print('g: p=%s' % p)

        model.setParameters(amici.DoubleVector(p))
        rdata = amici.runAmiciSimulation(model, solver, edata)

        model.setParameters(old_parameters)
        model.setParameterList(old_plist)

        res = rdata['s%s' % symbol]
        if not isinstance(res, float):
            if len(res.shape) == 2:
                res = np.sum(res, axis=(0, ))
            if len(res.shape) == 3:
                res = np.sum(res, axis=(0, 2))
        return res
示例#5
0
def getReturnDataForCondition(model, solver, fixed_parameters,
                              dynamic_parameters, sigmay,
                              sigma_parameter_observable_idx,
                              sigma_parameter_idx):

    model.setParameters(amici.DoubleVector(dynamic_parameters))

    # Simulate without measurements for noise-free trajectories
    edata = amici.ExpData(model.get())
    edata.fixedParameters = fixed_parameters
    edata.my = np.full(shape=model.nt() * model.nytrue, fill_value=np.nan)
    rdata = amici.runAmiciSimulation(model, solver, edata)
    # fixedParametersPreequilibration =

    # synthetic_data = rdata['y'] # noise-free

    # Add noise to simulation
    synthetic_data = np.random.normal(loc=rdata['y'], scale=sigmay)
    print("\tSigma mean per observable:", sigmay.mean(axis=0))
    # Apply correct sigma parameter
    synthetic_data[:, sigma_parameter_observable_idx] = \
        np.random.normal(loc=rdata['y'][:, sigma_parameter_observable_idx],
                         scale=dynamic_parameters[sigma_parameter_idx])
    # due to noise, there may be negative measurements. we don't want them.
    synthetic_data = np.abs(synthetic_data)
    print("\tMean abs. relative measurement error per observable:")
    print("\t",
          np.mean(np.abs((synthetic_data - rdata['y']) / rdata['y']), axis=0))

    # Use synthetic data to get expected llh
    edata.my = synthetic_data.flatten()
    edata.sigmay = sigmay.flatten()
    solver.setSensitivityMethod(amici.SensitivityMethod_forward)
    solver.setSensitivityOrder(amici.SensitivityOrder_first)
    model.requireSensitivitiesForAllParameters()
    rdata = amici.runAmiciSimulation(model, solver, edata)
    # TODO: confirm gradient is 0 for real measurements and save expected llh

    # return generated noisy measurements
    rdata._swigptr.y = amici.DoubleVector(synthetic_data.flatten())

    return rdata
示例#6
0
    def func(x0, symbol='llh', x0full=None, plist=[], verbose=False):
        """Function of which the gradient is to be checked"""
        p = x0
        if len(plist):
            p = x0full[:]
            p[plist] = x0
        verbose and print('f: p=%s' % p)

        old_sensitivity_order = solver.getSensitivityOrder()
        old_parameters = model.getParameters()

        solver.setSensitivityOrder(amici.SensitivityOrder_none)
        model.setParameters(amici.DoubleVector(p))
        rdata = amici.runAmiciSimulation(model, solver, edata)

        solver.setSensitivityOrder(old_sensitivity_order)
        model.setParameters(old_parameters)

        res = np.sum(rdata[symbol])
        return res
    '$\log_{10}$ $(\mathcal{M}^*)$': best_par_red
}
#'$\log(k+10^{-5})$ $(\mathcal{M}_{com})$': best_par_logE}#,  #,
#'aic': best_par_logE_aic,
#'aicc': best_par_logE_aicc}#,
#'bic': best_par_logE_bic}

#_______________________________________________________________________________________________________________________

par_values = list(par_dict.values())
par_names = list(par_dict.keys())

rdata = []
rdata_par = []
for i in range(len(par_dict)):
    model.setParameters(amici.DoubleVector(par_values[i]))
    rdata_tmp = amici.runAmiciSimulation(model, solver)['y']
    # print('iiiiiii', i)
    # print(rdata_tmp)

    naive = [item[0] for item in rdata_tmp]
    ee = [item[1] for item in rdata_tmp]
    le = [item[2] for item in rdata_tmp]
    memory = [item[3] for item in rdata_tmp]

    rdata.append([naive, ee, le, memory])

se = [[0, 2], [2, 9], [9, 16], [16, 21]]
color = ['green', 'red', 'gold']
k = ['A', 'B', 'C', 'D']
title = [
示例#8
0
文件: testSBML.py 项目: permfl/AMICI
    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)
示例#9
0
def main():
    script_dir = os.path.splitext(os.path.abspath(__file__))[0]
    script_dir = os.path.split(script_dir)[0]
    sbml_file = os.path.join(script_dir, 'lucarelli_12.xml')
    model_name = 'lucarelli_12'
    model_output_dir = os.path.join(os.getcwd())

    print("Importing model from", sbml_file)
    print("Generating files in", model_output_dir)

    # set observables and constants
    observables_list = [
        'observable_Ski', 'observable_Skil', 'observable_Dnmt3a',
        'observable_Sox4', 'observable_Jun', 'observable_Smad7',
        'observable_Klf10', 'observable_Bmp4', 'observable_Cxcl15',
        'observable_Dusp5', 'observable_Tgfa', 'observable_Pdk4'
    ]  #
    fixed_parameters = ['init_TGFb', 'init_S2', 'init_S3', 'init_S4']

    # wrap the model
    sbmlImporter = amici.SbmlImporter(sbml_file)

    observables = amici.assignmentRules2observables( \
        sbmlImporter.sbml,  # the libsbml model object
        filter_function=lambda variable: variable.getId() in observables_list)

    sbmlImporter.sbml2amici(model_name,
                            output_dir=model_output_dir,
                            observables=observables,
                            constantParameters=fixed_parameters)

    # load model
    sys.path.insert(0, model_output_dir)
    import lucarelli_12 as modelModule

    model = modelModule.getModel()
    solver = model.getSolver()
    rdata = amici.runAmiciSimulation(model, solver)

    expectedLlh = 0.0
    sigma_default = 0.1  # parameters are lin
    timepoints = np.linspace(0, 100, 5)

    model = modelModule.getModel()
    true_parameters = np.array(model.getParameters())

    # setup model & solver
    model = modelModule.getModel()
    model.setTimepoints(amici.DoubleVector(timepoints))
    model.setParameters(amici.DoubleVector(true_parameters))

    solver = model.getSolver()
    solver.setMaxSteps(10000)

    # generate condition-vectors
    sigma = 0.5
    mu = 1.5
    nConditions = 10
    init_TGFb = np.linspace(0, 1, 11)
    init_conditions = [10.0**(mu + sigma * np.random.randn(4))]
    init_conditions[0][0] = init_TGFb[np.random.randint(0, 11)]
    for i in range(1, nConditions):
        init_conditions.append(
            np.array(10.0**(mu + sigma * np.random.randn(4))))
        init_conditions[i][0] = init_TGFb[np.random.randint(0, 11)]

    indices = fixed_parameters
    conditionDf = createConditionDataframe(indices, init_conditions,
                                           true_parameters)

    df = pd.DataFrame(
        data={
            'observable': [],
            'condition': [],
            'conditionRef': [],
            'time': [],
            'measurement': [],
        })

    for icondition, condition in enumerate(init_conditions):

        simulationParameters = true_parameters
        sigmay = np.ones(shape=(model.nt(), model.nytrue)) * sigma_default

        # simulate condition
        rdata = getReturnDataForCondition(model, solver, condition,
                                          simulationParameters, sigmay)

        expectedLlh += rdata['llh']

        conditionName = 'condition-%d' % icondition

        # Append data
        for iy, observableName in enumerate(observables.keys()):
            scalingParameter = ['']
            sigma = sigmay[:, iy]

            df = df.append(pd.DataFrame({
                'observable': [observableName] * model.nt(),
                'condition': [conditionName] * model.nt(),
                'conditionRef': [''] * model.nt(),
                'scalingParameter':
                scalingParameter * model.nt(),
                'time':
                np.array(model.getTimepoints()),
                'measurement':
                rdata['y'][:, iy],
                'sigma':
                sigma
            }),
                           ignore_index=True)

    # write data frames to file
    measurement_file = 'example_data.tsv'
    fixed_parameter_file = 'example_data_fixed.tsv'
    hdf5File = 'example_data.h5'

    df.to_csv(measurement_file, sep='\t', index=False)
    conditionDf.to_csv(fixed_parameter_file, sep='\t', index=False)

    # convert to HDF5

    subprocess.call([
        "/bin/bash", "-c",
        "if [[ -f example_data.h5 ]]; then cp example_data.h5 example_data.h5.bak; fi"
    ])

    out = subprocess.run([
        '%s/generateHDF5DataFileFromText.py' %
        os.path.join(script_dir, '..', '..', '..', 'misc'), hdf5File,
        sbml_file, model_output_dir, measurement_file, fixed_parameter_file
    ],
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    print(out.stdout.decode("utf-8"))

    # changes some solver options in the hdf5 file
    f_hdf5 = h5py.File(hdf5File, 'r+')
    amici_options = f_hdf5['amiciOptions']
    amici_options.attrs['atol'] = 1.0e-8
    amici_options.attrs['rtol'] = 1.0e-6
    amici_options.attrs['sensi_meth'] = 1