Exemplo n.º 1
0
    def _add_observation_model(self):
        """Extend PySB model by observation model as defined in the PEtab
        observables table"""

        import pysb

        # add any required output parameters
        local_syms = {sp.Symbol.__str__(comp): comp for comp in
                      self.pysb_model.components if
                      isinstance(comp, sp.Symbol)}
        for formula in [*self.observable_df[OBSERVABLE_FORMULA],
                        *self.observable_df[NOISE_FORMULA]]:
            sym = sp.sympify(formula, locals=local_syms)
            for s in sym.free_symbols:
                if not isinstance(s, pysb.Component):
                    p = pysb.Parameter(str(s), 1.0)
                    local_syms[sp.Symbol.__str__(p)] = p

        # add observables and sigmas to pysb model
        for (observable_id, observable_formula, noise_formula) \
                in zip(self.observable_df.index,
                       self.observable_df[OBSERVABLE_FORMULA],
                       self.observable_df[NOISE_FORMULA]):
            # No observableTransformation so far
            if OBSERVABLE_TRANSFORMATION in self.observable_df:
                trafo = self.observable_df.loc[observable_id,
                                               OBSERVABLE_TRANSFORMATION]
                if trafo and trafo != LIN:
                    raise NotImplementedError(
                        "Observable transformation currently unsupported "
                        "for PySB models")
            obs_symbol = sp.sympify(observable_formula, locals=local_syms)
            obs_expr = pysb.Expression(observable_id, obs_symbol)
            local_syms[observable_id] = obs_expr

            sigma_id = f"{observable_id}_sigma"
            sigma_symbol = sp.sympify(
                noise_formula,
                locals=local_syms
            )
            sigma_expr = pysb.Expression(sigma_id, sigma_symbol)
            local_syms[sigma_id] = sigma_expr
Exemplo n.º 2
0
    def _add_observation_model(self):
        """Extend PySB model by observation model as defined in the PEtab
        observables table"""

        # add any required output parameters
        local_syms = {
            sp.Symbol.__str__(comp): comp
            for comp in self.pysb_model.components
            if isinstance(comp, sp.Symbol)
        }
        for formula in [
                *self.observable_df[OBSERVABLE_FORMULA],
                *self.observable_df[NOISE_FORMULA]
        ]:
            sym = sp.sympify(formula, locals=local_syms)
            for s in sym.free_symbols:
                if not isinstance(s, pysb.Component):
                    p = pysb.Parameter(str(s), 1.0, _export=False)
                    self.pysb_model.add_component(p)
                    local_syms[sp.Symbol.__str__(p)] = p

        # add observables and sigmas to pysb model
        for (observable_id, observable_formula, noise_formula) \
                in zip(self.observable_df.index,
                       self.observable_df[OBSERVABLE_FORMULA],
                       self.observable_df[NOISE_FORMULA]):
            obs_symbol = sp.sympify(observable_formula, locals=local_syms)
            if observable_id in self.pysb_model.expressions.keys():
                obs_expr = self.pysb_model.expressions[observable_id]
            else:
                obs_expr = pysb.Expression(observable_id,
                                           obs_symbol,
                                           _export=False)
                self.pysb_model.add_component(obs_expr)
            local_syms[observable_id] = obs_expr

            sigma_id = f"{observable_id}_sigma"
            sigma_symbol = sp.sympify(noise_formula, locals=local_syms)
            sigma_expr = pysb.Expression(sigma_id, sigma_symbol, _export=False)
            self.pysb_model.add_component(sigma_expr)
            local_syms[sigma_id] = sigma_expr
Exemplo n.º 3
0
def load_model(
    pathway_name: str,
    force_compile: bool = True,
    add_observables: bool = False
) -> Tuple[amici.AmiciModel, amici.AmiciSolver]:

    model = load_pathway(pathway_name)
    outdir = os.path.join(basedir, 'amici_models', model.name)

    # extend observables
    if add_observables:
        for obs in model.observables:
            if re.match(r'[p|t][A-Z0-9]+[SYT0-9_]*', obs.name):
                offset = pysb.Parameter(obs.name + '_offset', 0.0)
                scale = pysb.Parameter(obs.name + '_scale', 1.0)
                pysb.Expression(obs.name + '_obs',
                                sp.log(scale * (obs + offset)))

    if force_compile or not os.path.exists(
            os.path.join(outdir, model.name, model.name + '.py')):
        os.makedirs(outdir, exist_ok=True)
        amici.pysb_import.pysb2amici(model,
                                     outdir,
                                     verbose=logging.DEBUG,
                                     observables=[
                                         expr.name
                                         for expr in model.expressions
                                         if expr.name.endswith('_obs')
                                     ],
                                     constant_parameters=[])

    model_module = amici.import_model_module(model.name, outdir)

    amici_model = model_module.getModel()
    solver = amici_model.getSolver()

    solver.setMaxSteps(int(1e5))
    solver.setAbsoluteToleranceSteadyState(1e-2)
    solver.setRelativeToleranceSteadyState(1e-8)

    return amici_model, solver
Exemplo n.º 4
0
     parameter = pysb.Parameter(p.name, p.value, _export=False)
     model.add_component(parameter)
 for r in sbml_model.reactions:
     assert 1 <= len(r.reactants) <= 2
     assert len(r.products) == 1
     lterms = [model.monomers[s.name]() for s in r.reactants]
     lhs = sum(lterms[1:], lterms[0]())
     rhs = model.monomers[r.products[0].name]()
     kf = model.parameters[r.kf.name]
     kr = model.parameters[r.kr.name]
     if len(lterms) == 2 and r.reactants[0] == r.reactants[1]:
         expr_name = kf.name + '_symmetric'
         try:
             kf = model.expressions[expr_name]
         except KeyError:
             kf = pysb.Expression(expr_name, kf * 2, _export=False)
             model.add_component(kf)
     if kr.get_value() == 0:
         rule = pysb.Rule(r.name, lhs >> rhs, kf, _export=False)
     else:
         rule = pysb.Rule(r.name, lhs <> rhs, kf, kr, _export=False)
     model.add_component(rule)
 sbml_perb11_names = [
     'c483',
     'c136',
     'c23',
     'c7',
     'c25',
     'c88',
     'c27',
     'c89',