Skip to content

Commit

Permalink
subgraph: Cope with missing information in corner cases
Browse files Browse the repository at this point in the history
  • Loading branch information
thvitt committed May 6, 2019
1 parent 73ee5c1 commit da13e51
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/macrogen/graph.py
Expand Up @@ -422,11 +422,11 @@ def add_path(self, graph: nx.MultiDiGraph, source: Node, target: Node, weight='i
if edges_from is None:
edges_from = self.dag
path = nx.shortest_path(edges_from, source, target, weight, method)
logger.info('Shortest path from %s to %s: %s', source, target, " → ".join(map(str, path)))
logger.debug('Shortest path from %s to %s: %s', source, target, " → ".join(map(str, path)))
edges = expand_edges(edges_from, nx.utils.pairwise(path))
graph.add_edges_from(edges)
return path
except nx.NetworkXNoPath as e:
except nx.NetworkXException as e:
if must_exist:
raise e

Expand Down Expand Up @@ -479,12 +479,15 @@ def subgraph(self, *nodes: Node, context: bool = True, path_to: Iterable[Node] =

for node in central_nodes:
if abs_dates:
prev = max((d for d in self.closure.pred[node] if isinstance(d, date)), default=None)
if prev is not None and prev not in central_nodes:
self.add_path(subgraph, prev, node, edges_from=path_base)
next_ = min((d for d in self.closure.succ[node] if isinstance(d, date)), default=None)
if next_ is not None and next not in central_nodes:
self.add_path(subgraph, node, next_, edges_from=path_base)
try:
prev = max((d for d in self.closure.pred[node] if isinstance(d, date)), default=None)
if prev is not None and prev not in central_nodes:
self.add_path(subgraph, prev, node, edges_from=path_base)
next_ = min((d for d in self.closure.succ[node] if isinstance(d, date)), default=None)
if next_ is not None and next not in central_nodes:
self.add_path(subgraph, node, next_, edges_from=path_base)
except KeyError as e:
logger.warning('%s is not in closure, so no date justification', node)

for source in sources:
self.add_path(subgraph, source, node, edges_from=path_base)
Expand Down

0 comments on commit da13e51

Please sign in to comment.