1from toolz import curry
2from toolz import curried as tlz
3from operator import attrgetter
4
5from .lib import (
6 debug,
7 debug_plot,
8 DEBUG_PLOT,
9 find_vertex_by_name_or_none,
10 is_None,
11 subcomponent_multi
12)
13
14
15call_counts = {
16 "in": 0,
17 "out": 0
18}
19
20
21@curry
22def subcomponent(mode, paths, graph):
23 if DEBUG_PLOT:
24 global call_counts
25 graph_name_prefix = f"subcomponent_{mode}_{call_counts[mode]}_"
26 call_counts[mode] += 1
27
28 layout = graph.layout('tree')
29 debug_plot(graph, f"{graph_name_prefix}input", layout=layout)
30
31 path_indices = tlz.compose(
32 tlz.map(attrgetter('index')),
33 tlz.remove(is_None),
34 tlz.map(find_vertex_by_name_or_none(graph))
35 )(paths)
36
37 debug("path_indices", path_indices)
38
39 main_indices = list(subcomponent_multi(graph, path_indices, mode))
40
41 debug('main_indices', main_indices)
42
43 if DEBUG_PLOT:
44 def choose_color(index):
45 if (index in main_indices):
46 return "green"
47 else:
48 return "red"
49
50 vertex_color = [choose_color(v.index) for v in graph.vs]
51
52 debug_plot(
53 graph,
54 f"{graph_name_prefix}result",
55 layout=layout,
56 vertex_color=vertex_color
57 )
58
59 return {
60 "main": graph.induced_subgraph(main_indices),
61 "rest": graph - main_indices
62 }
63
64
65subcomponent_in = subcomponent("in")
66
67subcomponent_out = subcomponent("out")