Example #1
0
def store_word_graph(word_graph, file_name=None):
    from .word_graphs import WordGraph, expand_word_graph  # For circular import
    if file_name is None:
        file_name = word_graph.file_name
    file_name = add_output_folder(file_name)
    if isinstance(word_graph, WordGraph):
        vertex_count = word_graph.vertex_count
        edge_count = word_graph.edge_count
        words = list(word_graph.directed_neighborhoods.keys())
    else:
        vertex_count = len(expand_word_graph(word_graph))
        edge_count = sum(len(neighors) for neighors in word_graph.values())
        words = list(word_graph)
    word_graph_data = ["Vertex count: " + str(vertex_count), 
                       "Edge count: " + str(edge_count) + "\n\n"]
    words.sort()
    for word in words:
        if (isinstance(word_graph, WordGraph) 
                and word_graph.directed_neighborhoods[word]):
            neighborhood = word + ": " + str(
                word_graph.directed_neighborhoods[word]).replace("\'", "")
        elif type(word_graph) == dict and word_graph[word]:
            neighborhood = word + ": " + str(word_graph[word]).replace("\'", "")
        word_graph_data.append(neighborhood)
    store_data(word_graph_data, file_name)
Example #2
0
def store_subgraph_statistics(subgraph_statistics, ascending_order, 
                              name_base="word_graph", name_suffix=""):
    file_name = name_base + "_" + name_suffix + "_subgraph_statistics.txt"
    file_name = add_output_folder(file_name)
    title = ((name_base + "_" + name_suffix).replace("_", " ").title() 
             + " Subgraph Statistics")
    if ascending_order:
        "Ascending Order " + title
    subgraph_data = ["\n" + title]
    for size in subgraph_statistics:
        subgraph_data.append("\n\n---------------- Words of size <= " 
                             + str(size) + " ----------------")
        subgraph_data.append(
            "\nVertices: " + str(subgraph_statistics[size]["vertices"]))
        subgraph_data.append(
            "Edges: " + str(subgraph_statistics[size]["edges"]))
        for subgraph_type, statistics_dict in subgraph_statistics[size].items():
            if isinstance(statistics_dict, dict):
                for subgraph_class, value in statistics_dict.items():
                    if subgraph_class == "total":
                        subgraph_data.append("\n" + subgraph_type.title() 
                                             + "s: " + str(value))
                    else:
                        subgraph_data.append(subgraph_class.title() + " " 
                                             + subgraph_type + "s: " + str(value))

    store_data(subgraph_data, file_name)
Example #3
0
def store_words(word_list, list_type="dow", extra_suffix=""):
    if list_type == "all":
        length = max_word_length(word_list)
        size = None
    else:
        size = max_word_size(word_list)
        length = None
    file_name = get_word_filename(list_type,
                                  size,
                                  length,
                                  extra_suffix=extra_suffix)
    store_data(word_list, file_name)
Example #4
0
def store_external_paths(external_paths_container, ascending_order, name_base):
    file_name = name_base + "_minimal_external_paths.txt"
    if ascending_order:
        file_name = "ao" + file_name
    file_name = add_output_folder(file_name)

    external_path_data = []
    for size, minimal_external_paths in external_paths_container.items():
        identifier = get_word_graph_filename(ascending_order, size, name_base)
        external_path_data.append("\n\n" + identifier + "\n")
        for vertex1 in paths_by_vertex in minimal_external_paths.items():
            for vertex2, paths in paths_by_vertex.items():
                external_path_data.append("\n" + vertex1 + " --> " + vertex2 + ":")
                for path in paths:
                    external_path_data.append(path)

    store_data(external_path_data, file_name)
Example #5
0
def store_homology_data(ascending_order,
                        size,
                        homology_data,
                        max_simplex_types,
                        name_base="word_graph_size",
                        name_suffix=""):
    file_name = get_word_graph_filename(ascending_order, size, name_base,
                                        name_suffix)[:-4]
    file_name += "_homology_" + "_".join(max_simplex_types) + ".txt"
    print_data = []
    for i, homology_group in enumerate(homology_data):
        print_data.append("\nBetti number " + i + " : " + homology_group[0])
        if len(homology_group) > 1:
            print_data.append("\nBoundary matrix of dimension " + i)
            print_data.append(homology_group[1])
            print_data.append("\nReduced boundary matrix of dimension " + i)
            print_data.append(homology_group[2])
        print_data.append("\n--------------------------\n")

    store_data(print_data, file_name)
Example #6
0
def store_word_subgraphs(subgraphs, subgraph_type, ascending_order, 
                         size, name_base="word_graph_size", name_suffix=""):
    sorted_ = True if type(subgraphs) == dict else False
    subgraph_count = (len(subgraphs) if type(subgraphs) == list 
                      else sum(len(subgraph_list) for subgraph_list 
                               in subgraphs.values()))
    subgraph_data = [subgraph_type.title() + " subgraph count: " 
                     + str(subgraph_count) + "\n"]
    if (subgraph_type in ["triangle", "3-path", "4-path", "square"] and not sorted_):
        subgraph_data += subgraphs
    elif sorted_:
        for subgraph_class, subgraph_list in subgraphs.items():
            subgraph_data.append(str(subgraph_class) + " " + subgraph_type 
                                 + " subgraph count: " + str(len(subgraph_list)))
        subgraph_data.append("\n\n")
        for subgraph_class, subgraph_list in subgraphs.items():
            subgraph_data.append(str(subgraph_class) + ":\n")
            subgraph_data += subgraph_list
            subgraph_data.append("\n\n")

    subgraph_file_name = add_output_folder(get_word_subgraph_filename(
        ascending_order, size, subgraph_type, 
        name_base, name_suffix, sorted_))
    store_data(subgraph_data, subgraph_file_name)