<commit_before>from __future__ import absolute_import, print_function, unicode_literals
from builtins import dict, str
import numpy
import networkx
import logging

try:
    from indra.sources.reach.processor import determine_reach_subtype
    use_reach_subtypes = True
except ImportError:
    use_reach_subtypes = False

logger = logging.getLogger("belief")

default_probs = {
    'rand': {
        'biopax': 0.2,
        'bel': 0.1,
        'trips': 0.3,
        'reach': 0.3,
        'isi': 0.3,
        'eidos': 0.3,
        'hume': 0.3,
        'cwms': 0.3,
        'sofia': 0.3,
        'biogrid': 0.01,
        'sparser': 0.3,
        'r3': 0.1,
        'phosphosite': 0.01,
        'ndex': 0.049,
        'signor': 0.049,
        'assertion': 0.0,
        },
    'syst': {
        'biopax': 0.01,
        'bel': 0.01,
        'trips': 0.05,
        'reach': 0.05,
        'isi': 0.05,
        'eidos': 0.05,
        'hume': 0.05,
        'cwms': 0.05,
        'sofia': 0.05,
        'biogrid': 0.01,
        'sparser': 0.05,
        'r3': 0.05,
        'phosphosite': 0.01,
        'ndex': 0.01,
        'signor': 0.01,
        'assertion': 0.0,
        }
    }


class BeliefScorer(object):
    """Base class for a belief engine scorer, which computes the
    prior probability of a statement given its type and evidence.

    To use with the belief engine, make a subclass with methods implemented.
    """
    def score_statement(self, st):
        """Computes the prior belief probability for an INDRA Statement.

        The Statement is assumed to be de-duplicated. In other words,
        the Statement is assumed to have
        a list of Evidence objects that supports it. The prior probability of
        the Statement is calculated based on the number of Evidences it has
        and their sources.

        Parameters
        ----------
        statement : indra.statements.Statement
            An INDRA Statements whose belief scores are to
            be calculated.

        Returns
        -------
        belief_score : float
            The computed prior probability for the statement
        """
        raise NotImplementedError('Need to subclass BeliefScorer and '
                                  'implement methods.')

    def check_prior_probs(self, statements):
        """Make sure the scorer has all the information needed to compute
        belief scores of each statement in the provided list, and raises an
        exception otherwise.

        Parameters
        ----------
        statements : list<indra.statements.Statement>
            List of statements to check
        """
        raise NotImplementedError('Need to subclass BeliefScorer and '
                                  'implement methods.')


class SimpleScorer(BeliefScorer):
    """Computes the prior probability of a statement given its type and
    evidence.

    Parameters
    ----------
    prior_probs : dict[dict]
        A dictionary of prior probabilities used to override/extend the
        default ones. There are two types of prior probabilities: rand and syst
        corresponding to random error and systematic error rate for each
        knowledge source. The prior_probs dictionary has the general structure
        {'rand': {'s1': pr1, ..., 'sn': prn},
        'syst': {'s1': ps1, ..., 'sn': psn}}
        where 's1' ... 'sn' are names of input sources and pr1 ... prn and
        ps1 ... psn are error probabilities.
        Examples: {'rand': {'some_source': 0.1}} sets the random error rate
        for some_source to 0.1; {'rand': {''}}
    subtype_probs : dict[dict]
        A dictionary of random error probabilities for knowledge sources.
        When a subtype random error probability is not specified, will just
        use the overall type prior in prior_probs. If None, will
        only use the priors for each rule.
    """
    def __init__(self, prior_probs, subtype_probs=None):
        self.prior_probs = default_probs
        if prior_probs:
            self.prior_probs.update(prior_probs)
        for err_type, source_dict in self.prior_probs.items():
            logger.info("Prior probabilities for %s errors: %s"
                        % (err_type, source_dict))
        self.subtype_probs = subtype_probs
        return

    def score_statement(self, st):
        """Computes the prior belief probability for an INDRA Statement.

        The Statement is assumed to be de-duplicated. In other words,
        the Statement is assumed to have
        a list of Evidence objects that supports it. The prior probability of
        the Statement is calculated based on the number of Evidences it has
        and their sources.

        Parameters
        ----------
        statement : indra.statements.Statement
            An INDRA Statements whose belief scores are to
            be calculated.

        Returns
        -------
        belief_score : float
            The computed prior probability for the statement
        """

        sources = [ev.source_api for ev in st.evidence]
        uniq_sources = numpy.unique(sources)<commit_msg>Fix setting initial default belief as copy<commit_after>from __future__ import absolute_import, print_function, unicode_literals
from builtins import dict, str
import copy
import numpy
import networkx
import logging

try:
    from indra.sources.reach.processor import determine_reach_subtype
    use_reach_subtypes = True
except ImportError:
    use_reach_subtypes = False

logger = logging.getLogger("belief")

default_probs = {
    'rand': {
        'biopax': 0.2,
        'bel': 0.1,
        'trips': 0.3,
        'reach': 0.3,
        'isi': 0.3,
        'eidos': 0.3,
        'hume': 0.3,
        'cwms': 0.3,
        'sofia': 0.3,
        'biogrid': 0.01,
        'sparser': 0.3,
        'r3': 0.1,
        'phosphosite': 0.01,
        'ndex': 0.049,
        'signor': 0.049,
        'assertion': 0.0,
        },
    'syst': {
        'biopax': 0.01,
        'bel': 0.01,
        'trips': 0.05,
        'reach': 0.05,
        'isi': 0.05,
        'eidos': 0.05,
        'hume': 0.05,
        'cwms': 0.05,
        'sofia': 0.05,
        'biogrid': 0.01,
        'sparser': 0.05,
        'r3': 0.05,
        'phosphosite': 0.01,
        'ndex': 0.01,
        'signor': 0.01,
        'assertion': 0.0,
        }
    }


class BeliefScorer(object):
    """Base class for a belief engine scorer, which computes the
    prior probability of a statement given its type and evidence.

    To use with the belief engine, make a subclass with methods implemented.
    """
    def score_statement(self, st):
        """Computes the prior belief probability for an INDRA Statement.

        The Statement is assumed to be de-duplicated. In other words,
        the Statement is assumed to have
        a list of Evidence objects that supports it. The prior probability of
        the Statement is calculated based on the number of Evidences it has
        and their sources.

        Parameters
        ----------
        statement : indra.statements.Statement
            An INDRA Statements whose belief scores are to
            be calculated.

        Returns
        -------
        belief_score : float
            The computed prior probability for the statement
        """
        raise NotImplementedError('Need to subclass BeliefScorer and '
                                  'implement methods.')

    def check_prior_probs(self, statements):
        """Make sure the scorer has all the information needed to compute
        belief scores of each statement in the provided list, and raises an
        exception otherwise.

        Parameters
        ----------
        statements : list<indra.statements.Statement>
            List of statements to check
        """
        raise NotImplementedError('Need to subclass BeliefScorer and '
                                  'implement methods.')


class SimpleScorer(BeliefScorer):
    """Computes the prior probability of a statement given its type and
    evidence.

    Parameters
    ----------
    prior_probs : dict[dict]
        A dictionary of prior probabilities used to override/extend the
        default ones. There are two types of prior probabilities: rand and syst
        corresponding to random error and systematic error rate for each
        knowledge source. The prior_probs dictionary has the general structure
        {'rand': {'s1': pr1, ..., 'sn': prn},
        'syst': {'s1': ps1, ..., 'sn': psn}}
        where 's1' ... 'sn' are names of input sources and pr1 ... prn and
        ps1 ... psn are error probabilities.
        Examples: {'rand': {'some_source': 0.1}} sets the random error rate
        for some_source to 0.1; {'rand': {''}}
    subtype_probs : dict[dict]
        A dictionary of random error probabilities for knowledge sources.
        When a subtype random error probability is not specified, will just
        use the overall type prior in prior_probs. If None, will
        only use the priors for each rule.
    """
    def __init__(self, prior_probs=None, subtype_probs=None):
        self.prior_probs = copy.copy(default_probs)
        if prior_probs:
            self.prior_probs.update(prior_probs)
        for err_type, source_dict in self.prior_probs.items():
            logger.info("Prior probabilities for %s errors: %s"
                        % (err_type, source_dict))
        self.subtype_probs = subtype_probs
        return

    def score_statement(self, st):
        """Computes the prior belief probability for an INDRA Statement.

        The Statement is assumed to be de-duplicated. In other words,
        the Statement is assumed to have
        a list of Evidence objects that supports it. The prior probability of
        the Statement is calculated based on the number of Evidences it has
        and their sources.

        Parameters
        ----------
        statement : indra.statements.Statement
            An INDRA Statements whose belief scores are to
            be calculated.

        Returns
        -------
        belief_score : float
            The computed prior probability for the statement
        """

        sources = [ev.source_api for ev in st.evidence]
        uniq_sources = numpy.unique(sources)
{'fraction_non_alphanumeric': 0.09253294289897511, 'fraction_numerical': 0.01913128355295266, 'mean_word_length': 1.969855072463768, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7426893', 'n_tokens_mistral': 2969, 'n_tokens_neox': 2743, 'n_words': 1025}