<commit_before>#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Copyright (c) 2011 <NAME>
#
# This software is released under the MIT License. Please see
# LICENSE.txt for details.


"""A program to detect Process Linkage Networks using a Markov chain
Monte Carlo technique.

"""


import collections
import datetime
import itertools
import sys

from convutils import convutils
import networkx

import bpn.cli
import bpn.structures
from defaults import (
        SUPERDEBUG,
        SUPERDEBUG_MODE,
        LINKS_FIELDNAMES,
        PARAMETERS_FIELDNAMES,
        TRANSITIONS_FIELDNAMES,
        DETAILED_TRANSITIONS_FIELDNAMES,
        TERMS_BASED_TRANSITIONS_FIELDNAMES,
        TERMS_FIELDNAMES,
        INDEPENDENT_TERMS_BASED_TRANSITIONS_FIELDNAMES,
        GENES_BASED_TRANSITIONS_FIELDNAMES
)

# Configure all the logging stuff
import logging
logger = logging.getLogger('bpn.mcmcbpn')

if SUPERDEBUG_MODE:
    # A logging level below logging.DEBUG
    logging.addLevelName(SUPERDEBUG, 'SUPERDEBUG')
    logger.setLevel(SUPERDEBUG)
    #stream_handler.setLevel(SUPERDEBUG)

import chains
import states
import recorders


def check_link_components(annotated_interactions):
    logger.info("Checking components and sizes of potential BPN.")
    num_terms = annotated_interactions.calc_num_terms()
    num_links = annotated_interactions.calc_num_links()
    potential_links_graph = networkx.Graph()
    potential_links_graph.add_edges_from(annotated_interactions.get_all_links())
    components = networkx.algorithms.components.connected_component_subgraphs(
            potential_links_graph)
    num_components = len(components)
    component_sizes = [(c.number_of_nodes(), c.number_of_edges()) for c in
            components]
    logger.info("{0} co-annotating pairs from {1} terms.".format(
            num_links, num_terms))
    logger.info(("Co-annotation network forms {0} connected "
        "component(s)").format(num_components))
    logger.info("Component sizes:\nNodes\tEdges\n{0}".format(
            '\n'.join('{0}\t{1}'.format(*sizes) for sizes in
                component_sizes))
    )
    if num_components > 1:
        logger.warning("WARNING! Potential links do not form a single"
                "connected component; algorithm may be unsuitable!")


def main(argv=None):
    starting_time = datetime.datetime.now()
    cli_parser = bpn.cli.McmcCli()
    input_data = cli_parser.parse_args(argv)

    logger.info("Constructing supporting data structures; this may "
            "take a while...")
    if input_data.terms_based:
        annotated_interactions = (
                bpn.structures.AnnotatedInteractions2dArray(
                        input_data.interactions_graph,
                        input_data.annotations_dict,
                        stringent_coannotations=input_data.stringent_coannotations
                )
        )
    else:
        annotated_interactions = (
                bpn.structures.AnnotatedInteractionsArray(
                        input_data.interactions_graph,
                        input_data.annotations_dict,
                        stringent_coannotations=input_data.stringent_coannotations
                )
        )
    # Check to see whether the potential links form a single connected
    # component.
    check_link_components(annotated_interactions)

    logger.info("Constructing the Markov chain.")
    # Prepare the CSV writers for the state recorder.<commit_msg>Report the seed value used for the run.<commit_after>#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Copyright (c) 2011 <NAME>
#
# This software is released under the MIT License. Please see
# LICENSE.txt for details.


"""A program to detect Process Linkage Networks using a Markov chain
Monte Carlo technique.

"""


import collections
import datetime
import itertools
import random
import sys

from convutils import convutils
import networkx

import bpn.cli
import bpn.structures
from defaults import (
        SUPERDEBUG,
        SUPERDEBUG_MODE,
        LINKS_FIELDNAMES,
        PARAMETERS_FIELDNAMES,
        TRANSITIONS_FIELDNAMES,
        DETAILED_TRANSITIONS_FIELDNAMES,
        TERMS_BASED_TRANSITIONS_FIELDNAMES,
        TERMS_FIELDNAMES,
        INDEPENDENT_TERMS_BASED_TRANSITIONS_FIELDNAMES,
        GENES_BASED_TRANSITIONS_FIELDNAMES
)

# Configure all the logging stuff
import logging
logger = logging.getLogger('bpn.mcmcbpn')

if SUPERDEBUG_MODE:
    # A logging level below logging.DEBUG
    logging.addLevelName(SUPERDEBUG, 'SUPERDEBUG')
    logger.setLevel(SUPERDEBUG)
    #stream_handler.setLevel(SUPERDEBUG)

import chains
import states
import recorders


def create_seed_value():
    """Creates a seed value for the `random` module, should one not be
    provided by the user.

    This code is simply a reproduction of the code from random.py in the
    standard library.

    Returns a seed value.

    """
    from binascii import hexlify as _hexlify
    from os import urandom as _urandom
    try:
        seed = long(_hexlify(_urandom(16)), 16)
    except NotImplementedError:
        import time
        seed = long(time.time() * 256) # use fractional seconds
    return seed


def check_link_components(annotated_interactions):
    logger.info("Checking components and sizes of potential BPN.")
    num_terms = annotated_interactions.calc_num_terms()
    num_links = annotated_interactions.calc_num_links()
    potential_links_graph = networkx.Graph()
    potential_links_graph.add_edges_from(annotated_interactions.get_all_links())
    components = networkx.algorithms.components.connected_component_subgraphs(
            potential_links_graph)
    num_components = len(components)
    component_sizes = [(c.number_of_nodes(), c.number_of_edges()) for c in
            components]
    logger.info("{0} co-annotating pairs from {1} terms.".format(
            num_links, num_terms))
    logger.info(("Co-annotation network forms {0} connected "
        "component(s)").format(num_components))
    logger.info("Component sizes:\nNodes\tEdges\n{0}".format(
            '\n'.join('{0}\t{1}'.format(*sizes) for sizes in
                component_sizes))
    )
    if num_components > 1:
        logger.warning("WARNING! Potential links do not form a single"
                "connected component; algorithm may be unsuitable!")


def main(argv=None):
    starting_time = datetime.datetime.now()
    cli_parser = bpn.cli.McmcCli()
    input_data = cli_parser.parse_args(argv)

    logger.info("Constructing supporting data structures; this may "
            "take a while...")
    if input_data.terms_based:
        annotated_interactions = (
                bpn.structures.AnnotatedInteractions2dArray(
                        input_data.interactions_graph,
                        input_data.annotations_dict,
                        stringent_coannotations=input_data.stringent_coannotations
                )
        )
    else:
        annotated_interactions = (
                bpn.structures.AnnotatedInteractionsArray(
                        input_data.interactions_graph,
                        input_data.annotations_dict,
                        stringent_coannotations=input_data.stringent_coannotations
                )
        )
    # Check to see whether the potential links form a single connected
    # component.
    check_link_components(annotated_interactions)

    # TODO: check a command line option to see if the user input a seed;
    # for now, we'll just generate one all the time and report it.
    random_seed = create_seed_value()
    logger.info("The random seed value for this run is {0}.".format(
            random_seed))
    random.seed(random_seed)

    logger.info("Constructing the Markov chain.")
    # Prepare the CSV writers for the state recorder.
{'fraction_non_alphanumeric': 0.07612055641421947, 'fraction_numerical': 0.004379185986604843, 'mean_word_length': 2.888332498748122, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '765186', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2209, 'n_words': 538}