Passed
Push — main ( 25fd53...ab2bda )
by Eran
02:05
created

graph_atlas.spiral_edges()   A

Complexity

Conditions 3

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 3
nop 2
1
from collections.abc import Iterable
2
3
import graphs
4
import networkx as nx
5
6
import graphinate
7
from graphinate.materializers import Materializers
8
9
10
def model(iterable: Iterable[nx.Graph]) -> graphinate.GraphModel:
11
    """
12
    Generate a graph model based on the provided iterable of graphs.
13
    The function creates a graph model named 'Graph Atlas' using the 'graphinate' library.
14
    It then combines all the graphs from the input iterable into a single disjoint union graph using NetworkX library.
15
    The function defines edges for the combined graph by iterating over all edges in the disjoint union graph and
16
    yielding dictionaries with 'source' and 'target' keys representing the edge connections.
17
    Finally, the function yields the created graph model containing the combined graph with defined edges.
18
19
    Args:
20
        iterable: An iterable containing graphs to be combined into a single graph model.
21
22
    Yields:
23
        GraphModel: A graph model containing the combined graph with defined edges.
24
    """
25
26
    graph_model = graphinate.model('Graph Atlas')
27
28
    @graph_model.edge()
29
    def edge():
30
        for e in nx.disjoint_union_all(g for _, g in iterable).edges:
31
            yield {'source': e[0], 'target': e[1]}
32
33
    return graph_model
34
35
36
if __name__ == '__main__':
37
    from gui import listbox_chooser, radiobutton_chooser
38
39
    graph_atlas = graphs.atlas()
40
41
    choices = listbox_chooser('Choose Graph', graph_atlas)
42
43
    model = model(choices)
44
45
    # or
46
    # model(graph_atlas.values())
47
48
    result = radiobutton_chooser('Choose Materializer',
49
                                 options={m.name: m.value for m in Materializers},
50
                                 default=(None, None))
51
    builder, handler = result[1]
52
53
    graphinate.materialize(model, builder=builder, builder_output_handler=handler)
54