<commit_before>from __future__ import print_function, unicode_literals, absolute_import
from builtins import dict, str
import logging
import numpy as np
import networkx
import itertools
from copy import deepcopy
from pysb import kappa
from pysb import Observable
from pysb.core import as_complex_pattern
from indra.statements import *
from indra.assemblers import pysb_assembler as pa

logger = logging.getLogger('model_checker')

class ModelChecker(object):
    """Check a PySB model against a set of INDRA statements."""

    def __init__(self, model, statements):
        self.model = model
        self.statements = statements
        self._im = None

    def get_im(self):
        if self._im:
            return self._im
        if not self.model:
            raise Exception("Cannot get influence map if there is no model.")
        else:
            self._im = kappa.influence_map(self.model)
            self._im.is_multigraph = lambda: False
            return self._im

    def check_model(self):
        results = []
        for stmt in self.statements:
            result = self.check_statement(stmt)
            results.append((stmt, result))
        return results

    def check_statement(self, stmt):
        if isinstance(stmt, Modification):
            return self.check_modification(stmt)
        else:
            return False

    def check_modification(self, stmt):
        # Identify the observable we're looking for in the model, which
        # may not exist!
        # The observable is the modified form of the substrate
        logger.info('Checking stmt: %s' % stmt)
        enz_mp = pa.get_monomer_pattern(self.model, stmt.enz)
        if type(stmt) in (Dephosphorylation,):
            unmodify_stmt = True
        else:
            unmodify_stmt = False
        modified_sub = _add_modification_to_agent(stmt.sub, 'phosphorylation',
                                                  stmt.residue, stmt.position)
        # Check if the site and corresponding state exists in the
        # model--if not, then it won't be observed. If we try to get a monomer
        # pattern for a monomer that doesn't have the specified site, PySB
        # throws a (generic) Exception; we catch this as an indication that
        # the site doesn't exist, and hence the modification is not observed,
        # and return False
        try:
            sub_mp = pa.get_monomer_pattern(self.model, modified_sub)
        except Exception as e:
            logger.info("Couldn't create monomer pattern: %s" % e)
            return False
        # Generate the influence map
        # Find rules in the model corresponding to the inputs and outputs
        input_rules = match_lhs(enz_mp, self.model.rules)
        logger.info('Found %s input rules matching %s' %
                    (len(input_rules), str(enz_mp)))
        # Get the production and consumption rules for the target
        target_prod_rules = find_production_rules(sub_mp, self.model.rules)
        target_cons_rules = find_consumption_rules(sub_mp, self.model.rules)
        # The final list contains a list of rules along with their "polarities"
        # (production = 1, consumption = -1)
        target_rules = (list(zip(target_prod_rules, [1]*len(target_prod_rules)))
                    + list(zip(target_cons_rules, [-1]*len(target_cons_rules))))
        logger.info('Found %s target rules matching %s' %
                    (len(target_rules), str(sub_mp)))
        if input_rules and target_rules:
            # Generate the influence map
            paths = []
            for input_rule, target_rule_info in \
                            itertools.product(input_rules, target_rules):<commit_msg>Break after finding one path<commit_after>from __future__ import print_function, unicode_literals, absolute_import
from builtins import dict, str
import logging
import numpy as np
import networkx
import itertools
from copy import deepcopy
from pysb import kappa
from pysb import Observable
from pysb.core import as_complex_pattern
from indra.statements import *
from indra.assemblers import pysb_assembler as pa

logger = logging.getLogger('model_checker')

class ModelChecker(object):
    """Check a PySB model against a set of INDRA statements."""

    def __init__(self, model, stmts_to_check=None):
        self.model = model
        if stmts_to_check:
            self.statements = stmts_to_check
        else:
            self.statements = []
        self._im = None

    def get_im(self):
        if self._im:
            return self._im
        if not self.model:
            raise Exception("Cannot get influence map if there is no model.")
        else:
            self._im = kappa.influence_map(self.model)
            self._im.is_multigraph = lambda: False
            return self._im

    def check_model(self):
        results = []
        for stmt in self.statements:
            result = self.check_statement(stmt)
            results.append((stmt, result))
        return results

    def check_statement(self, stmt):
        if isinstance(stmt, Modification):
            return self.check_modification(stmt)
        else:
            return False

    def check_modification(self, stmt):
        # Identify the observable we're looking for in the model, which
        # may not exist!
        # The observable is the modified form of the substrate
        logger.info('Checking stmt: %s' % stmt)
        enz_mp = pa.get_monomer_pattern(self.model, stmt.enz)
        if type(stmt) in (Dephosphorylation,):
            unmodify_stmt = True
        else:
            unmodify_stmt = False
        modified_sub = _add_modification_to_agent(stmt.sub, 'phosphorylation',
                                                  stmt.residue, stmt.position)
        # Check if the site and corresponding state exists in the
        # model--if not, then it won't be observed. If we try to get a monomer
        # pattern for a monomer that doesn't have the specified site, PySB
        # throws a (generic) Exception; we catch this as an indication that
        # the site doesn't exist, and hence the modification is not observed,
        # and return False
        try:
            sub_mp = pa.get_monomer_pattern(self.model, modified_sub)
        except Exception as e:
            logger.info("Couldn't create monomer pattern: %s" % e)
            return False
        # Generate the influence map
        # Find rules in the model corresponding to the inputs and outputs
        input_rules = match_lhs(enz_mp, self.model.rules)
        logger.info('Found %s input rules matching %s' %
                    (len(input_rules), str(enz_mp)))
        # Get the production and consumption rules for the target
        target_prod_rules = find_production_rules(sub_mp, self.model.rules)
        target_cons_rules = find_consumption_rules(sub_mp, self.model.rules)
        # The final list contains a list of rules along with their "polarities"
        # (production = 1, consumption = -1)
        target_rules = (list(zip(target_prod_rules, [1]*len(target_prod_rules)))
                    + list(zip(target_cons_rules, [-1]*len(target_cons_rules))))
        logger.info('Found %s target rules matching %s' %
                    (len(target_rules), str(sub_mp)))
        if input_rules and target_rules:
            # Generate the influence map
            paths = []
            # Look for any rule that is both in the input and target set--
            # indicates that constraint is satisfied by a single rule
            for input_rule, target_rule_info in \
                            itertools.product(input_rules, target_rules):
                if paths:
                    break
{'fraction_non_alphanumeric': 0.0721072497722244, 'fraction_numerical': 0.0010412599245086555, 'mean_word_length': 2.28516460025652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '94981', 'n_tokens_mistral': 2089, 'n_tokens_neox': 2027, 'n_words': 688}