예제 #1
0
    print "{0} nodes with absolute datings.".format(len(absolutely_dated_notes_sorted))
    num_equal_absolute_datings = macrogenesis.insert_minimal_edges_from_absolute_datings(graph)
    print "{0} equal absolute datings.".format(num_equal_absolute_datings)

    print "{0} strongly connected components (conflicts).".format(networkx.number_strongly_connected_components(graph))

    transitive_closure = networkx.transitive_closure(graph)
    print "transitive closure"
    print "{0} nodes, {1} edges in transtive closure.".format(transitive_closure.number_of_nodes(),
                                                                     transitive_closure.number_of_edges())



def order_inscriptions(graph):

    transitive_closure = networkx.transitive_closure(graph)
    logging.info("{0} nodes, {1} edges in transtive closure.".format(transitive_closure.number_of_nodes(),
                                                                     transitive_closure.number_of_edges()))
    num_predecessors_nodes = [(len(transitive_closure.predecessors(node)), node) for node in transitive_closure]
    for n in sorted(num_predecessors_nodes, key=lambda tup: tup[0]):
        print '%i %s' % (n[0], n[1])



if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    graph = macrogenesis.import_graph()
    macrogenesis.insert_minimal_edges_from_absolute_datings(graph)

    analyse_graph(graph)
    #order_inscriptions(graph)
예제 #2
0
def main():
    output_dir = faust.config.get("macrogenesis", "output-dir")

    # copy resources
    try:
        shutil.copytree('macrogenesis/resources/js', os.path.join(output_dir, 'js'))
    except OSError as e:
        logging.warn(e)


    #collect hyperlinks to selected graphs for the TOC as [(link_text_1, relative_link_to_file_1), ...]
    links = []

    # draw raw input data
    graph_imported = macrogenesis.import_graph()
    logging.info("Generating raw data graph.")
    #UUU write_agraph_layout(agraph_from(graph_imported), output_dir, '00_raw_data')

    # highlight a single node and its neighbors
    # highlighted_node = 'faust://document/wa/2_I_H.17'
    for highlighted_node in graph_imported:
        highlighted_bunch = list(networkx.all_neighbors(graph_imported, highlighted_node))
        highlighted_bunch.append(highlighted_node)
        graph_highlighted_subgraph = graph_imported.subgraph(nbunch=highlighted_bunch).copy()
        graph_highlighted_subgraph.node[highlighted_node][KEY_HIGHLIGHT]= VALUE_TRUE
        macrogenesis.insert_minimal_edges_from_absolute_datings(graph_highlighted_subgraph)
        #, edge_labels=True)
        #agraph_highlighted_subgraph.node_attr[highlighted_node]['color'] = 'red'
        write_agraph_layout(agraph_from(graph_highlighted_subgraph), output_dir,
                           highlighted_base_filename(highlighted_node))


    # add relationships implicit in absolute datings
    logging.info("Generating graph with implicit absolute date relationships.")
    graph_absolute_edges = graph_imported
    macrogenesis.insert_minimal_edges_from_absolute_datings(graph_absolute_edges)
    del graph_imported
    base_filename_absolute_edges = '10_absolute_edges'
    write_agraph_layout(agraph_from(graph_absolute_edges), output_dir, base_filename_absolute_edges)
    links.append(('Raw datings (relative and absolute)', base_filename_absolute_edges))
    # again with edge labels
    # TODO this breaks graphviz
    # agraph_absolute_edges_edge_labels = agraph_from(graph_absolute_edges, edge_labels=True)
    # write_agraph_layout(agraph_absolute_edges_edge_labels, output_dir, '15_absolute_edges_edge_labels')

    logging.info("Generating condensation.")
    strongly_connected_components = list(networkx.strongly_connected_components(graph_absolute_edges))

    #condensation
    graph_condensation = networkx.condensation(graph_absolute_edges, scc=strongly_connected_components)
    for node in graph_condensation:
        label = ', '.join([label_from_uri(uri) for uri in graph_condensation.node[node]['members']])
        label_width = int(2 * math.sqrt(len(label)))
        graph_condensation.node[node]['label'] = textwrap.fill(label, label_width, break_long_words=False).replace('\n','\\n')
        component_filename_pattern = '16_strongly_connected_component_%i'
        # make a hyperlink to subgraph of the component
        if len(graph_condensation.node[node]['members']) > 1:
            set_node_url(graph_condensation.node[node], component_filename_pattern % node)
        else:
            # TODO just link to normal neighborhood subgraph for single nodes
            pass

    base_filename_condensation = '15_condensation'
    write_agraph_layout(agraph_from(graph_condensation), output_dir, base_filename_condensation)
    links.append(('Condensation', base_filename_condensation))

    for (component_index, component) in enumerate(strongly_connected_components):
        # don't generate subgraphs consisting of a single node
        if len(component) > 1:
            graph_component = graph_absolute_edges.subgraph(nbunch=component).copy()
            #macrogenesis.insert_minimal_edges_from_absolute_datings(graph_component)
            # , edge_labels=True)
            write_agraph_layout(agraph_from(graph_component), output_dir,
                                component_filename_pattern % (component_index))

    # transitive closure, don't draw
    logging.info("Generating transitive closure graph.")
    transitive_closure = networkx.transitive_closure(graph_absolute_edges)
    logging.info("{0} nodes, {1} edges in transtive closure.".format(transitive_closure.number_of_nodes(),
                                                                     transitive_closure.number_of_edges()))
    agraph_transitive_closure = agraph_from(transitive_closure)

    # draw transitive reduction
    logging.info("Generating transitive reduction graph.")
    agraph_transitive_reduction = agraph_transitive_closure.tred(copy=True)
    logging.info("{0} nodes, {1} edges in transtive reduction.".format(agraph_transitive_reduction.number_of_nodes(),
                                                                      agraph_transitive_reduction.number_of_edges()))
    base_filename_transitive_reduction = '30_transitive_reduction'
    write_agraph_layout(agraph_transitive_reduction, output_dir, base_filename_transitive_reduction)
    links.append(('Transitive reduction', base_filename_transitive_reduction))

    # generate index.html
    logging.info("Generating index.html")
    html_links = ['<a href="{1}.html">{0}</a>'.format(*link) for link in links]
    with open(os.path.join(output_dir, 'index.html'), mode='w') as html_file:
        html_file.write(html_template('<h1>macrogenesis graphs</h1>' + ('<br/> '.join(html_links))))