def count_patterns(self): """Count the number of occurrences of our pattern in our host graph.""" # Make a list to store counts of patterns specified final_count = [0]*len(self.multi) # For every TDD given to us by the decomposition generator for tdd in self.decomp_generator: # Remember the largest component we've seen if we're making # visualization output if self.big_component_file is not None: if self.big_component is None: self.big_component = tdd elif len(self.big_component) < len(tdd): self.big_component = tdd # Count patterns in that TDD for idx, pat in enumerate(self.multi): count = self.count_patterns_from_TDD(tdd, pat, idx) # Combine the count from the TDD self.combiners[idx].combine_count(count) # Populate the list of counts that will be returned for idx in range(len(self.multi)): final_count[idx] += self.combiners[idx].get_count() # Write the largest component to a file if self.big_component_file is not None: from lib.graph.graphformats import write_edgelist write_edgelist(self.big_component, self.big_component_file) # Write the TDD of the largest component to a file if self.tdd_file is not None: for v in self.big_component.nodes: parent = self.big_component.vertexRecords[v].parent if parent is not None: print >> self.tdd_file, v, parent # Write the DP table for the largest component to a file if self.dp_table_file is not None: # Write the table in a machine-readable format dp_table = self.dp_table.table for v_tup in sorted(dp_table.keys()): self.dp_table_file.write(str([v for v in v_tup]) + " {\n") for pattern, count in sorted(dp_table[v_tup].iteritems()): if count > 0: self.dp_table_file.write("\t" + str(count) + "; ") vString = [v for v in pattern.vertices] bString = [str(v) + ":" + str(i) for v, i in pattern.boundary.iteritems()] bString = '[' + ', '.join(bString) + ']' self.dp_table_file.write( str(vString) + "; " + str(bString) + "\n") self.dp_table_file.write("}\n") # Return the totals for the whole graph return final_count
""" sortby = 'time' restrictions = "" ps = pstats.Stats(profile).strip_dirs().sort_stats(sortby) print "Stats from {0}".format(name) ps.print_stats(restrictions, percent) if __name__ == "__main__": G_file = sys.argv[1] H_name = sys.argv[2] # Check to see if we have a basic pattern if is_basic_pattern(H_name): H, _ = get_pattern_from_generator(H_name) with open(H_name + '.txt', 'w') as pattern_file: write_edgelist(H, pattern_file) H_file = H_name + '.txt' else: H_file = H_name directed = len(sys.argv) > 3 if directed: graphType = nx.DiGraph matchType = isomorphism.DiGraphMatcher else: graphType = nx.Graph matchType = isomorphism.GraphMatcher readProfile = cProfile.Profile() readProfile.enable()
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)
sortby = 'time' restrictions = "" ps = pstats.Stats(profile).strip_dirs().sort_stats(sortby) print "Stats from {0}".format(name) ps.print_stats(restrictions, percent) if __name__ == "__main__": G_file = sys.argv[1] H_name = sys.argv[2] # Check to see if we have a basic pattern if is_basic_pattern(H_name): H, _ = get_pattern_from_generator(H_name) with open(H_name + '.txt', 'w') as pattern_file: write_edgelist(H, pattern_file) H_file = H_name + '.txt' else: H_file = H_name directed = len(sys.argv) > 3 if directed: graphType = nx.DiGraph matchType = isomorphism.DiGraphMatcher else: graphType = nx.Graph matchType = isomorphism.GraphMatcher readProfile = cProfile.Profile() readProfile.enable()
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)