Пример #1
0
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
Пример #2
0
def ccalgorithm_factory(cfgfile, silent):
    Config = parse_config_safe(cfgfile)

    func_ldo = import_colmodules(Config.get('color',
                                            'low_degree_orientation'))
    func_step = import_colmodules(Config.get('color', 'step'))
    func_col = import_colmodules(Config.get('color', 'coloring'))
    func_ctd = import_colmodules(Config.get('color', 'check_tree_depth'))
    if Config.has_option('color', 'optimization'):
        func_opt = import_colmodules(Config.get('color', 'optimization'))
    else:
        func_opt = None

    if Config.has_option('color', 'preprocess'):
        func_preprocess = import_colmodules(Config.get('color',
                                                       'preprocess'))
    else:
        func_preprocess = None

    return CCAlgorithm(
            preprocess=func_preprocess,
            ldo=func_ldo,
            step=func_step,
            col=func_col,
            ctd=func_ctd,
            opt=func_opt,
            silent=silent)
Пример #3
0
def ccalgorithm_factory(cfgfile, silent, execdata):
    Config = parse_config_safe(cfgfile)

    func_ldo = import_colmodules(Config.get('color', 'low_degree_orientation'))
    func_step = import_colmodules(Config.get('color', 'step'))
    func_col = import_colmodules(Config.get('color', 'coloring'))
    func_ctd = import_colmodules(Config.get('color', 'check_tree_depth'))
    if Config.has_option('color', 'optimization'):
        func_opt = import_colmodules(Config.get('color', 'optimization'))
    else:
        func_opt = None

    if Config.has_option('color', 'preprocess'):
        func_preprocess = import_colmodules(Config.get('color', 'preprocess'))
    else:
        func_preprocess = None

    return CCAlgorithm(preprocess=func_preprocess,
                       ldo=func_ldo,
                       step=func_step,
                       col=func_col,
                       ctd=func_ctd,
                       opt=func_opt,
                       silent=silent,
                       execdata=execdata)
Пример #4
0
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, mapping = graph.normalize()
        norm_col = Coloring()
        for v in orig:
            norm_col[v] = coloring[mapping[v]]
        correct = True
        try:
            correct, _ = ctd(orig, ldo(orig), norm_col, 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
Пример #5
0
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()

    H, td_lower = parse_pattern_argument(pattern)

    # 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()

    # Find p-centered coloring
    if colorFile is None:
        coloring = p_centered_coloring(G, td, cfgFile, verbose)
        save_file(coloring, 'colorings/' + G_name + str(td), False, verbose)
    else:
        coloring = coloring_from_file(colorFile, G, td, cfgFile, verbose,
                                      not color_no_verify)

    if profile:  # time profiling
        colorProfile.disable()
        printProfileStats("coloring", colorProfile)
        patternProfile = cProfile.Profile()
        patternProfile.enable()

    # Get configuration settings for dynamic programming
    cfgParser = parse_config_safe(cfgFile)
    kpat_name = cfgParser.get('compute', 'k_pattern')
    # tab_name  = cfgParser.get('dp', 'tab')
    table_hints = {
        'forward': cfgParser.getboolean('compute', 'table_forward'),
        'reuse': cfgParser.getboolean('compute', 'table_reuse')
    }
    count_name = cfgParser.get('combine', 'count')
    sweep_name = cfgParser.get('decompose', 'sweep')

    patternClass = import_modules("lib.pattern_counting.dp."+kpat_name)
    count_class = import_modules('lib.pattern_counting.double_count.' +
                                       count_name)
    sweep_class = import_modules('lib.decomposition.' + sweep_name)

    # Count patterns
    pattern_counter = PatternCounter(G, H, td_lower, coloring,
                                     pattern_class=patternClass,
                                     table_hints=table_hints,
                                     decomp_class=sweep_class,
                                     combiner_class=count_class,
                                     verbose=verbose)
    patternCount = pattern_counter.count_patterns()
    print "Number of occurrences of H in G: {0}".format(patternCount)

    if profile:  # time profiling
        patternProfile.disable()
        printProfileStats("pattern counting", patternProfile)
Пример #6
0
def runPipeline(graph, pattern, cfgFile, colorFile, color_no_verify, output,
                verbose, profile, multifile, execution_data):
    """Basic running of the pipeline"""

    # Check if execution_data flag is set
    execdata = execution_data is not None

    if execdata and pattern == "multi":
        print "CONCUSS does not support outputting execution data while using the multi-motif flag"
        sys.exit(1)

    if execdata:
        # Check if directory exists already
        if os.path.isdir("./execdata"):
            # delete it, if it does exist
            shutil.rmtree("./execdata")
        # make new directory called "execdata"
        os.mkdir("./execdata")

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

    # Parse the multifile if counting multiple patterns
    if pattern == 'multi':
        multi, td_list = parse_multifile(multifile)
    # Parse pattern argument
    else:
        basic_pattern = is_basic_pattern(pattern)
        if basic_pattern:
            H, td_lower = get_pattern_from_generator(pattern)
        else:
            H, td_lower = get_pattern_from_file(pattern)
        multi = [H]
        td_list = [td_lower]

    # Read graphs from file
    G = load_graph(graph)
    td = len(max(multi, key=len))

    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()

    # Find p-centered coloring
    if colorFile is None:
        coloring = p_centered_coloring(G, td, cfgFile, verbose, execdata)
        save_file(coloring, 'colorings/' + G_name + str(td), False, verbose)
    else:
        coloring = coloring_from_file(colorFile, G, td, cfgFile, verbose,
                                      not color_no_verify)

    if profile:  # time profiling
        colorProfile.disable()
        printProfileStats("coloring", colorProfile)
        patternProfile = cProfile.Profile()
        patternProfile.enable()

    # Get configuration settings for dynamic programming
    cfgParser = parse_config_safe(cfgFile)
    kpat_name = cfgParser.get('compute', 'k_pattern')
    # tab_name  = cfgParser.get('dp', 'tab')
    table_hints = {
        'forward': cfgParser.getboolean('compute', 'table_forward'),
        'reuse': cfgParser.getboolean('compute', 'table_reuse')
    }
    count_name = cfgParser.get('combine', 'count')
    sweep_name = cfgParser.get('decompose', 'sweep')

    patternClass = import_modules("lib.pattern_counting.dp." + kpat_name)
    count_class = import_modules('lib.pattern_counting.double_count.' +
                                 count_name)
    sweep_class = import_modules('lib.decomposition.' + sweep_name)

    # Output for Count stage
    if execdata:
        count_path = 'execdata/count/'
        if not os.path.exists(count_path):
            os.makedirs(count_path)
        big_component_file = open(count_path+'big_component.txt', 'w')
        tdd_file = open(count_path+'tdd.txt', 'w')
        dp_table_file = open(count_path+'dp_table.txt', 'w')
    else:
        big_component_file = None
        tdd_file = None
        dp_table_file = None

    # Output for Combine stage
    if execdata:
        combine_path = 'execdata/combine/'
        if not os.path.exists(combine_path):
            os.makedirs(combine_path)
        # Open the file that needs to be passed to the count combiner
        colset_count_file = open('execdata/combine/counts_per_colorset.txt',
                                 'w')
    else:
        # If execution data is not requested, we don't need to open a file
        colset_count_file = None

    if count_name != "InclusionExclusion" and execdata:
        print "CONCUSS can only output execution data using the",
        print "InclusionExclusion combiner class."

        # Check if there is incomplete execution data written
        if os.path.isdir("./execdata"):
            # delete it, if it does exist
            shutil.rmtree("./execdata")

        # Exit the program with an error code of 1
        sys.exit(1)

    pattern_counter = PatternCounter(G, multi, td_list, coloring,
                                     pattern_class=patternClass,
                                     table_hints=table_hints,
                                     decomp_class=sweep_class,
                                     combiner_class=count_class,
                                     verbose=verbose,
                                     big_component_file=big_component_file,
                                     tdd_file=tdd_file,
                                     dp_table_file=dp_table_file,
                                     colset_count_file=colset_count_file)

    pattern_count = pattern_counter.count_patterns()

    # Patterns have been counted, print output
    if pattern == "multi":
        with open(multifile[0], 'r') as pattern_file:
            pattern_names = [pat[:-1] for pat in pattern_file]
    else:
        pattern_names = [pattern]
    for i in range(len(pattern_names)):
        print "Number of occurrences of {0} in G: {1}".format(pattern_names[i], pattern_count[i])

    if execdata:
        # Close count stage files
        big_component_file.close()
        tdd_file.close()
        dp_table_file.close()
        # Close the color set file
        colset_count_file.close()

        # if execution data flag is set
        # make and write to visinfo.cfg
        with open('execdata/visinfo.cfg', 'w') as visinfo:
            write_visinfo(visinfo, graph, pattern)

        # write execution data to zip
        with ZipFile(execution_data, 'w') as exec_zip:
            # exec_zip.write("execdata/visinfo.cfg", "visinfo.cfg")

            # Write data from 'execdata' directory to the zip:
            rootDir = './execdata/'
            for dir_name, _, file_list in os.walk(rootDir):
                for f_name in file_list:
                    full_path = dir_name + '/' + f_name
                    exec_zip.write(
                        full_path,
                        '/'.join(full_path.split('/')[2:]))

            exec_zip.write(cfgFile, os.path.split(cfgFile)[1])
            exec_zip.write(graph, os.path.split(graph)[1])

            # Check to see if the user specified a basic pattern
            if basic_pattern:
                from lib.graph.graphformats import write_edgelist
                # Write the pattern graph object to a file as an edgelist
                with open(pattern + '.txt', 'w') as pattern_file:
                    write_edgelist(H, pattern_file)
                # Write the file to the zip
                exec_zip.write(
                    pattern + '.txt',
                    os.path.split(pattern + '.txt')[1])
                # File written to zip, delete it
                os.remove(pattern + '.txt')
            else:
                exec_zip.write(pattern, os.path.split(pattern)[1])

        # delete execution data stored in folder
        shutil.rmtree('./execdata')

    if profile:  # time profiling
        patternProfile.disable()
        printProfileStats("pattern counting", patternProfile)
Пример #7
0
def runPipeline(
    graph, pattern, cfgFile, colorFile, color_no_verify, output, verbose, profile, multifile, execution_data
):
    """Basic running of the pipeline"""

    # Check if execution_data flag is set
    execdata = execution_data is not None

    if execdata and pattern == "multi":
        print "CONCUSS does not support outputting execution data while using the multi-motif flag"
        sys.exit(1)

    if execdata:
        # Check if directory exists already
        if os.path.isdir("./execdata"):
            # delete it, if it does exist
            shutil.rmtree("./execdata")
        # make new directory called "execdata"
        os.mkdir("./execdata")

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

    # Parse the multifile if counting multiple patterns
    if pattern == "multi":
        multi, td_list = parse_multifile(multifile)
    # Parse pattern argument
    else:
        basic_pattern = is_basic_pattern(pattern)
        if basic_pattern:
            H, td_lower = get_pattern_from_generator(pattern)
        else:
            H, td_lower = get_pattern_from_file(pattern)
        multi = [H]
        td_list = [td_lower]

    # Read graphs from file
    G = load_graph(graph)
    td = len(max(multi, key=len))

    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()

    # Find p-centered coloring
    if colorFile is None:
        coloring = p_centered_coloring(G, td, cfgFile, verbose, execdata)
        save_file(coloring, "colorings/" + G_name + str(td), False, verbose)
    else:
        coloring = coloring_from_file(colorFile, G, td, cfgFile, verbose, not color_no_verify)

    if profile:  # time profiling
        colorProfile.disable()
        printProfileStats("coloring", colorProfile)
        patternProfile = cProfile.Profile()
        patternProfile.enable()

    # Get configuration settings for dynamic programming
    cfgParser = parse_config_safe(cfgFile)
    kpat_name = cfgParser.get("compute", "k_pattern")
    # tab_name  = cfgParser.get('dp', 'tab')
    table_hints = {
        "forward": cfgParser.getboolean("compute", "table_forward"),
        "reuse": cfgParser.getboolean("compute", "table_reuse"),
    }
    count_name = cfgParser.get("combine", "count")
    sweep_name = cfgParser.get("decompose", "sweep")

    patternClass = import_modules("lib.pattern_counting.dp." + kpat_name)
    count_class = import_modules("lib.pattern_counting.double_count." + count_name)
    sweep_class = import_modules("lib.decomposition." + sweep_name)

    # Output for Count stage
    if execdata:
        count_path = "execdata/count/"
        if not os.path.exists(count_path):
            os.makedirs(count_path)
        big_component_file = open(count_path + "big_component.txt", "w")
        tdd_file = open(count_path + "tdd.txt", "w")
        dp_table_file = open(count_path + "dp_table.txt", "w")
    else:
        big_component_file = None
        tdd_file = None
        dp_table_file = None

    # Output for Combine stage
    if execdata:
        combine_path = "execdata/combine/"
        if not os.path.exists(combine_path):
            os.makedirs(combine_path)
        # Open the file that needs to be passed to the count combiner
        colset_count_file = open("execdata/combine/counts_per_colorset.txt", "w")
    else:
        # If execution data is not requested, we don't need to open a file
        colset_count_file = None

    if count_name != "InclusionExclusion" and execdata:
        print "CONCUSS can only output execution data using the",
        print "InclusionExclusion combiner class."

        # Check if there is incomplete execution data written
        if os.path.isdir("./execdata"):
            # delete it, if it does exist
            shutil.rmtree("./execdata")

        # Exit the program with an error code of 1
        sys.exit(1)

    pattern_counter = PatternCounter(
        G,
        multi,
        td_list,
        coloring,
        pattern_class=patternClass,
        table_hints=table_hints,
        decomp_class=sweep_class,
        combiner_class=count_class,
        verbose=verbose,
        big_component_file=big_component_file,
        tdd_file=tdd_file,
        dp_table_file=dp_table_file,
        colset_count_file=colset_count_file,
    )

    pattern_count = pattern_counter.count_patterns()

    # Patterns have been counted, print output
    if pattern == "multi":
        with open(multifile[0], "r") as pattern_file:
            pattern_names = [pat[:-1] for pat in pattern_file]
    else:
        pattern_names = [pattern]
    for i in range(len(pattern_names)):
        print "Number of occurrences of {0} in G: {1}".format(pattern_names[i], pattern_count[i])

    if execdata:
        # Close count stage files
        big_component_file.close()
        tdd_file.close()
        dp_table_file.close()
        # Close the color set file
        colset_count_file.close()

        # if execution data flag is set
        # make and write to visinfo.cfg
        with open("execdata/visinfo.cfg", "w") as visinfo:
            write_visinfo(visinfo, graph, pattern)

        # write execution data to zip
        with ZipFile(execution_data, "w") as exec_zip:
            # exec_zip.write("execdata/visinfo.cfg", "visinfo.cfg")

            # Write data from 'execdata' directory to the zip:
            rootDir = "./execdata/"
            for dir_name, _, file_list in os.walk(rootDir):
                for f_name in file_list:
                    full_path = dir_name + "/" + f_name
                    exec_zip.write(full_path, "/".join(full_path.split("/")[2:]))

            exec_zip.write(cfgFile, os.path.split(cfgFile)[1])
            exec_zip.write(graph, os.path.split(graph)[1])

            # Check to see if the user specified a basic pattern
            if basic_pattern:
                from lib.graph.graphformats import write_edgelist

                # Write the pattern graph object to a file as an edgelist
                with open(pattern + ".txt", "w") as pattern_file:
                    write_edgelist(H, pattern_file)
                # Write the file to the zip
                exec_zip.write(pattern + ".txt", os.path.split(pattern + ".txt")[1])
                # File written to zip, delete it
                os.remove(pattern + ".txt")
            else:
                exec_zip.write(pattern, os.path.split(pattern)[1])

        # delete execution data stored in folder
        shutil.rmtree("./execdata")

    if profile:  # time profiling
        patternProfile.disable()
        printProfileStats("pattern counting", patternProfile)