예제 #1
0
    def _parse_expressions(self):
        expr_namespace = (self.model.parameters | self.model.expressions
                          | self.model.observables)
        expr_symbols = {e.name: sympy.Symbol(e.name) for e in expr_namespace}

        for e in self._x.iterfind(_ns('{0}ListOfFunctions/{0}Function')):
            for arg in e.iterfind(_ns('{0}ListOfArguments/{0}Argument')):
                tag_name = arg.get('id')
                try:
                    self.model.tags[tag_name]
                except KeyError:
                    tag = self.tag(tag_name)
                    expr_symbols[tag_name] = tag
            expr_name = e.get('id')
            expr_text = e.find(_ns('{0}Expression')).text
            expr_val = 0
            try:
                expr_val = parse_bngl_expr(expr_text, local_dict=expr_symbols)
            except Exception as ex:
                self._warn_or_except('Could not parse expression %s: '
                                     '%s\n\nError: %s' %
                                     (expr_name, expr_text, str(ex)))
            if isinstance(expr_val, numbers.Number):
                self.parameter(expr_name, expr_val)
            else:
                self.expression(expr_name, expr_val)
예제 #2
0
파일: bngl.py 프로젝트: vw-liane/pysb
    def _eval_in_model_env(self, expression):
        """
        Evaluates an expression string in the model environment
        """
        components = dict((c.name, c) for c in self.model.all_components())
        self._model_env.update(components)

        # Quick security check on the expression
        if re.match(r'^[\w\s()/+\-._*^]*$', expression):
            return parse_bngl_expr(expression, local_dict=self._model_env)
        else:
            self._warn_or_except('Security check on expression "%s" failed' %
                                 expression)
            return None
예제 #3
0
파일: bngl.py 프로젝트: vw-liane/pysb
    def _parse_expressions(self):
        expr_namespace = (self.model.parameters | self.model.expressions)
        expr_symbols = {e.name: e for e in expr_namespace}

        for e in self._x.iterfind(_ns('{0}ListOfFunctions/{0}Function')):
            for arg in e.iterfind(_ns('{0}ListOfArguments/{0}Argument')):
                tag_name = arg.get('id')
                try:
                    self.model.tags[tag_name]
                except KeyError:
                    tag = self.tag(tag_name)
                    expr_symbols[tag_name] = tag
            expr_name = e.get('id')
            expr_text = e.find(_ns('{0}Expression')).text
            expr_val = 0
            try:
                expr_val = parse_bngl_expr(expr_text, local_dict=expr_symbols)
            except Exception as ex:
                self._warn_or_except('Could not parse expression %s: '
                                     '%s\n\nError: %s' %
                                     (expr_name, expr_text, str(ex)))
            # Replace observables now, so they get expanded by .expand_expr()
            # Doing this as part of expr_symbols breaks local functions!
            observables = {
                sympy.Symbol(o.name): o
                for o in self.model.observables
            }
            # Add renamed observables
            observables.update({
                sympy.Symbol(obs_old): self.model.observables[obs_new]
                for obs_old, obs_new in self._renamed_observables.items()
            })
            expr_val = expr_val.xreplace(observables)

            if isinstance(expr_val, numbers.Number):
                self.parameter(expr_name, expr_val)
            else:
                self.expression(expr_name, expr_val)