Skip to content

Commit

Permalink
Simplified simplify_timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
thvitt committed Mar 25, 2019
1 parent dfb29cc commit ff0aca8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
28 changes: 0 additions & 28 deletions src/macrogen/datings.py
Expand Up @@ -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,
Expand Down
34 changes: 33 additions & 1 deletion src/macrogen/graphutils.py
Expand Up @@ -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
Expand Down Expand Up @@ -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]]):
Expand Down Expand Up @@ -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

0 comments on commit ff0aca8

Please sign in to comment.