def get_pattern_from_generator(pattern): """ Get pattern from pattern generator since basic pattern was specified :param pattern: Name of basic pattern :return: Pattern graph and its treedepth """ import re p = re.compile(r"(\d*)") # Parse out the different parts of the argument args = filter(lambda x: x != "" and x != ",", p.split(pattern)) # There are two parts if len(args) == 2 and args[0] not in pattern_gen.bipartite_patterns: try: # Get the generator for the pattern type generator = pattern_gen.get_generator(args[0]) # Get the number of vertices provided pattern_num_vertices = int(args[1]) # Generate the pattern H = generator(pattern_num_vertices) # Return the pattern along with its treedepth return H, treedepth(H, args[0], pattern_num_vertices) except KeyError: pattern_argument_error_msg(pattern) # Bipartite pattern type provided elif len(args) == 3 and args[0] in pattern_gen.bipartite_patterns: # Make sure it is a valid bipartite pattern try: generator = pattern_gen.get_generator(args[0]) # Try to get the two set sizes m = int(args[1]) n = int(args[2]) # Generate the pattern H = generator(m, n) # Return the pattern along with its treedepth return H, treedepth(H, args[0], m, n) except (KeyError, ValueError): # Invalid sizes provided pattern_argument_error_msg(pattern) else: # Number of vertices not provided in argument pattern_argument_error_msg(pattern)
def get_pattern_from_generator(pattern): """ Get pattern from pattern generator since basic pattern was specified :param pattern: Name of basic pattern :return: Pattern graph and its treedepth """ import re p = re.compile(r'(\d*)') # Parse out the different parts of the argument args = filter(lambda x: x != "" and x != ",", p.split(pattern)) # There are two parts if len(args) == 2 and args[0] not in pattern_gen.bipartite_patterns: try: # Get the generator for the pattern type generator = pattern_gen.get_generator(args[0]) # Get the number of vertices provided pattern_num_vertices = int(args[1]) # Generate the pattern H = generator(pattern_num_vertices) # Return the pattern along with its treedepth return H, treedepth(H, args[0], pattern_num_vertices) except KeyError: pattern_argument_error_msg(pattern) # Bipartite pattern type provided elif len(args) == 3 and args[0] in pattern_gen.bipartite_patterns: # Make sure it is a valid bipartite pattern try: generator = pattern_gen.get_generator(args[0]) # Try to get the two set sizes m = int(args[1]) n = int(args[2]) # Generate the pattern H = generator(m, n) # Return the pattern along with its treedepth return H, treedepth(H, args[0], m, n) except (KeyError, ValueError): # Invalid sizes provided pattern_argument_error_msg(pattern) else: # Number of vertices not provided in argument pattern_argument_error_msg(pattern)
def get_pattern_from_file(filename): """ Load pattern from a file :param filename: The name of the pattern file :return: A graph object representing the pattern along with its treedepth """ # Argument is a filename try: # Try to load the graph from file H = load_graph(filename) # Return pattern along with lower bound on its treedepth return H, treedepth(H) except Exception: # Invalid file extension pattern_argument_error_msg(filename)
def parse_pattern_argument(pattern): """ Parses the 'pattern' command line argument. Checks to see if this argument is a filename or a description of the pattern :param pattern: Filename or description of pattern :return: A tuple with the pattern graph and a lower bound on its treedepth """ import os # Get the name of the file and the file extension name, ext = os.path.splitext(pattern) # There is no extension, so argument is a description # of the pattern if ext == "": import re p = re.compile(r'(\d*)') # Parse out the different parts of the argument args = filter(lambda x: x != "" and x != ",", p.split(pattern)) # There are two parts if len(args) == 2 and args[0] not in pattern_gen.bipartite_patterns: try: # Get the generator for the pattern type generator = pattern_gen.get_generator(args[0]) # Get the number of vertices provided pattern_num_vertices = int(args[1]) # Generate the pattern H = generator(pattern_num_vertices) # Return the pattern along with its treedepth return H, treedepth(H, args[0], pattern_num_vertices) except KeyError: pattern_argument_error_msg() # Bipartite pattern type provided elif len(args) == 3 and args[0] in pattern_gen.bipartite_patterns: # Make sure it is a valid bipartite pattern try: generator = pattern_gen.get_generator(args[0]) # Try to get the two set sizes m = int(args[1]) n = int(args[2]) # Generate the pattern H = generator(m, n) # Return the pattern along with its treedepth return H, treedepth(H, args[0], m, n) except (KeyError, ValueError): # Invalid sizes provided pattern_argument_error_msg() else: # Number of vertices not provided in argument pattern_argument_error_msg() else: # Argument is a filename try: # Try to load the graph from file H = load_graph(pattern) # Return pattern along with lower bound on its treedepth return H, treedepth(H) except Exception: # Invalid file extension pattern_argument_error_msg()
def parse_pattern_argument(pattern): """ Parses the 'pattern' command line argument. Checks to see if this argument is a filename or a description of the pattern :param pattern: Filename or description of pattern :return: A tuple with the pattern graph and a lower bound on its treedepth """ # Get the name of the file and the file extension name, ext = os.path.splitext(pattern) # There is no extension, so argument is a description # of the pattern if ext == "": import re p = re.compile(r'(\d*)') # Parse out the different parts of the argument args = filter(lambda x: x != "" and x != ",", p.split(pattern)) # There are two parts if len(args) == 2 and args[0] not in pattern_gen.bipartite_patterns: try: # Get the generator for the pattern type generator = pattern_gen.get_generator(args[0]) # Get the number of vertices provided pattern_num_vertices = int(args[1]) # Generate the pattern H = generator(pattern_num_vertices) # Return the pattern along with its treedepth return H, treedepth(H, args[0], pattern_num_vertices) except KeyError: pattern_argument_error_msg(pattern) # Bipartite pattern type provided elif len(args) == 3 and args[0] in pattern_gen.bipartite_patterns: # Make sure it is a valid bipartite pattern try: generator = pattern_gen.get_generator(args[0]) # Try to get the two set sizes m = int(args[1]) n = int(args[2]) # Generate the pattern H = generator(m, n) # Return the pattern along with its treedepth return H, treedepth(H, args[0], m, n) except (KeyError, ValueError): # Invalid sizes provided pattern_argument_error_msg(pattern) else: # Number of vertices not provided in argument pattern_argument_error_msg(pattern) else: # Argument is a filename try: # Try to load the graph from file H = load_graph(pattern) # Return pattern along with lower bound on its treedepth return H, treedepth(H) except Exception: # Invalid file extension pattern_argument_error_msg(pattern)