From ff0aca80efabb4237dc10842a4d5922fbd7f047d Mon Sep 17 00:00:00 2001 From: Thorsten Vitt Date: Mon, 25 Mar 2019 10:40:09 +0100 Subject: [PATCH] Simplified simplify_timeline --- src/macrogen/datings.py | 28 ---------------------------- src/macrogen/graphutils.py | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/macrogen/datings.py b/src/macrogen/datings.py index 3fae7de..48e577d 100644 --- a/src/macrogen/datings.py +++ b/src/macrogen/datings.py @@ -259,34 +259,6 @@ def add_timeline_edges(graph): graph.add_edge(earlier, later, kind='timeline') -def simplify_timeline(graph: nx.MultiDiGraph): - """ - Remove superfluous date nodes (and timeline edges) from the graph. - - When creating subgraphs of the base graph, the subgraph will sometimes contain date nodes that - are not linked to references remaining in the subgraph. This function will remove those nodes - and link the remaining date nodes instead. So, it will reduce - - 1798-01-01 -> 1709-01-15 -> 1798-02-01 - `-------------> H.x ---------------^ - - to - - 1798-01-01 -> 1798-02-01 - `-----> H.x -----^ - """ - date_nodes = sorted(node for node in graph.nodes if isinstance(node, date)) - prev = None - for node in date_nodes: - if prev is not None and graph.in_degree(node) == graph.out_degree(node) == 1 and isinstance( - graph.successors(node)[0], date): - graph.remove_node(node) - else: - if prev is not None: - graph.add_edge(prev, node, kind='timeline') - prev = node - - def build_datings_graph() -> nx.MultiDiGraph: """ Builds the raw datings graph by parsing the datings from all macrogenesis files from the default directory, diff --git a/src/macrogen/graphutils.py b/src/macrogen/graphutils.py index 204412b..6417297 100644 --- a/src/macrogen/graphutils.py +++ b/src/macrogen/graphutils.py @@ -5,6 +5,7 @@ import networkx as nx +from macrogen.datings import add_timeline_edges from .uris import Witness, Reference from .bibliography import BiblSource from .datings import parse_datestr @@ -151,7 +152,7 @@ def add_iweight(graph: nx.MultiDiGraph): elif attr['weight'] > 0: attr['iweight'] = 1 / attr['weight'] else: - attr['iweight'] = 0 + attr['iweight'] = 2_000_000 def mark_edges_to_delete(graph: nx.MultiDiGraph, edges: List[Tuple[Any, Any, int, Any]]): @@ -202,3 +203,34 @@ def in_path(edge: Tuple[T, T], path: Sequence[T], cycle=False) -> bool: return False except ValueError: return False + + +def simplify_timeline(graph: nx.MultiDiGraph): + """ + Remove superfluous date nodes (and timeline edges) from the graph. + + When creating subgraphs of the base graph, the subgraph will sometimes contain date nodes that + are not linked to references remaining in the subgraph. This function will remove those nodes + and link the remaining date nodes instead. So, it will reduce + + 1798-01-01 -> 1709-01-15 -> 1798-02-01 + `-------------> H.x ---------------^ + + to + + 1798-01-01 -> 1798-02-01 + `-----> H.x -----^ + """ + graph = remove_edges(graph, lambda u, v, attr: attr.get('kind') == 'timeline').copy() + add_timeline_edges(graph) + return graph + # date_nodes = sorted(node for node in graph.nodes if isinstance(node, date)) + # prev = None + # for node in date_nodes: + # if prev is not None and graph.in_degree(node) == graph.out_degree(node) == 1 and isinstance( + # one(graph.successors(node)), date): + # graph.remove_node(node) + # else: + # if prev is not None: + # graph.add_edge(prev, node, kind='timeline') + # prev = node \ No newline at end of file