<commit_before>#!/usr/bin/env python2.7
#
# This file is part of CONCUSS, https://github.com/theoryinpractice/concuss/, and is
# Copyright (C) North Carolina State University, 2015. It is licensed under
# the three-clause BSD license; see LICENSE.
#


import sys
import os
import argparse
#import ConfigParser
from lib.util.parse_config_safe import parse_config_safe
from lib.coloring.generate_coloring import ccalgorithm_factory, \
     import_colmodules, save_file
from lib.graph.graphformats import load_graph as load_graph
from lib.pattern_counting.pattern_counter import PatternCounter
import cProfile
import pstats
from lib.graph.graph import Coloring
import lib.graph.pattern_generator as pattern_gen
from lib.graph.treedepth import treedepth

def import_modules(name):
    """
    Return namespace of module

    Arguments:
        name:  name of module to import
    """
    if not name:
        return None
    funcname = name.split('.')[-1]
    modname = '.'.join(name.split('.')[:-1])
    module = __import__(modname, fromlist=[funcname])
    return getattr(module, funcname)


def coloring_from_file(filename, graph, td, cfgfile, verbose, verify=False):
    """Read a coloring from a file"""
    # Load the coloring from the file
    coloring = Coloring()
    with open(filename, 'r') as f:
        f.readline()
        for line in f:
            vertex, color = line.split(": ")
            coloring[int(vertex)] = int(color)

    if verify:
        # Verify that the coloring is correct
        if verbose:
            print "Verifying coloring..."

        Config = parse_config_safe(cfgfile)
        ldo = import_colmodules(Config.get('color',
                                           'low_degree_orientation'))
        ctd = import_colmodules(Config.get('color',
                                           'check_tree_depth'))
        orig, _ = graph.normalize()

        correct = True
        try:
            correct, _ = ctd(orig, ldo(orig), coloring, td)
        except TypeError:
            correct = False
        assert correct, \
            "Coloring is not a valid p-centered coloring for host graph"

        if verbose:
            print "Coloring is correct"

    # Return the coloring
    return coloring


def p_centered_coloring(graph, td, cfgfile, verbose):
    """Start running the p-centered coloring"""
    m = ccalgorithm_factory(cfgfile, not verbose)
    col = m.start(graph, td)
    return col


def runPipeline(graph, pattern, cfgFile, colorFile, color_no_verify, output,
                verbose, profile):
    """Basic running of the pipeline"""

    if profile:  # time profiling
        readProfile = cProfile.Profile()
        readProfile.enable()

    # If the user specifies the pattern name for ex. as -> clique 4
    if len(pattern) == 2:
        # Get pattern type and number of vertices
        pattern_type = pattern[0]
        pattern_num_vertices = int(pattern[1])
        # Get the generator for that type of pattern
        generator = pattern_gen.get_generator(pattern_type)
        # Generate the pattern
        H = generator(pattern_num_vertices)
        # Compute lower bound given the pattern
        td_lower = treedepth(H, pattern_type)
    else:
        # Load pattern from file
        H = load_graph(pattern[0])
        # Computer lower bound
        td_lower = treedepth(H)

    # Read graphs from file
    G = load_graph(graph)
    td = len(H)

    G_path, G_local_name = os.path.split(graph)
    G_name, G_extension = os.path.splitext(G_local_name)

    if verbose:
        print "G has {0} vertices and {1} edges".format(len(G), G.num_edges())

    if profile:  # time profiling
        readProfile.disable()
        printProfileStats("reading graphs", readProfile)
        colorProfile = cProfile.Profile()
        colorProfile.enable()
<commit_msg>More error checking added<commit_after>#!/usr/bin/env python2.7
#
# This file is part of CONCUSS, https://github.com/theoryinpractice/concuss/, and is
# Copyright (C) North Carolina State University, 2015. It is licensed under
# the three-clause BSD license; see LICENSE.
#


import sys
import os
import argparse
#import ConfigParser
from lib.util.parse_config_safe import parse_config_safe
from lib.coloring.generate_coloring import ccalgorithm_factory, \
     import_colmodules, save_file
from lib.graph.graphformats import load_graph as load_graph
from lib.pattern_counting.pattern_counter import PatternCounter
import cProfile
import pstats
from lib.graph.graph import Coloring
import lib.graph.pattern_generator as pattern_gen
from lib.graph.treedepth import treedepth

def import_modules(name):
    """
    Return namespace of module

    Arguments:
        name:  name of module to import
    """
    if not name:
        return None
    funcname = name.split('.')[-1]
    modname = '.'.join(name.split('.')[:-1])
    module = __import__(modname, fromlist=[funcname])
    return getattr(module, funcname)


def coloring_from_file(filename, graph, td, cfgfile, verbose, verify=False):
    """Read a coloring from a file"""
    # Load the coloring from the file
    coloring = Coloring()
    with open(filename, 'r') as f:
        f.readline()
        for line in f:
            vertex, color = line.split(": ")
            coloring[int(vertex)] = int(color)

    if verify:
        # Verify that the coloring is correct
        if verbose:
            print "Verifying coloring..."

        Config = parse_config_safe(cfgfile)
        ldo = import_colmodules(Config.get('color',
                                           'low_degree_orientation'))
        ctd = import_colmodules(Config.get('color',
                                           'check_tree_depth'))
        orig, _ = graph.normalize()

        correct = True
        try:
            correct, _ = ctd(orig, ldo(orig), coloring, td)
        except TypeError:
            correct = False
        assert correct, \
            "Coloring is not a valid p-centered coloring for host graph"

        if verbose:
            print "Coloring is correct"

    # Return the coloring
    return coloring


def p_centered_coloring(graph, td, cfgfile, verbose):
    """Start running the p-centered coloring"""
    m = ccalgorithm_factory(cfgfile, not verbose)
    col = m.start(graph, td)
    return col


def runPipeline(graph, pattern, cfgFile, colorFile, color_no_verify, output,
                verbose, profile):
    """Basic running of the pipeline"""

    if profile:  # time profiling
        readProfile = cProfile.Profile()
        readProfile.enable()

    # If the user specifies the pattern name for ex. as -> clique 4
    if len(pattern) == 2:
        # Get pattern type and number of vertices
        pattern_type = pattern[0]
        try:
            pattern_num_vertices = int(pattern[1])
        except ValueError:
            print "\nPlease provide an integer as the second argument " \
                  "to the -m flag\n"
            sys.exit(1)
        # Get the generator for that type of pattern
        generator = pattern_gen.get_generator(pattern_type)
        # Generate the pattern
        H = generator(pattern_num_vertices)
        # Compute lower bound given the pattern
        td_lower = treedepth(H, pattern_type)
    else:
        # Load pattern from file
        H = load_graph(pattern[0])
        # Computer lower bound
        td_lower = treedepth(H)

    # Read graphs from file
    G = load_graph(graph)
    td = len(H)

    G_path, G_local_name = os.path.split(graph)
    G_name, G_extension = os.path.splitext(G_local_name)

    if verbose:
        print "G has {0} vertices and {1} edges".format(len(G), G.num_edges())

    if profile:  # time profiling
        readProfile.disable()
        printProfileStats("reading graphs", readProfile)
        colorProfile = cProfile.Profile()
        colorProfile.enable()

{'fraction_non_alphanumeric': 0.08912405967104424, 'fraction_numerical': 0.003952569169960474, 'mean_word_length': 2.511190689346464, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3994521', 'n_tokens_mistral': 2286, 'n_tokens_neox': 2179, 'n_words': 684}