Passed
Push — main ( 2bcd5c...05fcff )
by Eran
01:27
created

math.graph_atlas   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 35
dl 0
loc 67
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A model() 0 36 3
1
import operator
2
3
import graphs
4
import networkx as nx
5
6
import graphinate
7
8
from .materializers import Materializers, materialize
9
10
11
def model(items: list[tuple[str, nx.Graph]]) -> graphinate.GraphModel:
12
    """
13
    Generate a graph model based on the provided iterable of graphs.
14
    The function creates a graph model named 'Graph Atlas' using the 'graphinate' library.
15
    It then combines all the graphs from the input iterable into a single disjoint union graph using NetworkX library.
16
    The function defines edges for the combined graph by iterating over all edges in the disjoint union graph and
17
    yielding dictionaries with 'source' and 'target' keys representing the edge connections.
18
    Finally, the function yields the created graph model containing the combined graph with defined edges.
19
20
    Args:
21
        items: A list containing graphs to be combined into a single graph model.
22
23
    Yields:
24
        GraphModel: A graph model containing the combined graph with defined edges.
25
    """
26
27
    def items_iter(recs):
28
        for name, g in recs:
29
            print(name)
30
            yield g
31
32
    g = nx.disjoint_union_all(items_iter(items)) if len(items) > 1 else items[0][1]
33
34
    graph_model = graphinate.model('Graph Atlas')
35
36
    @graph_model.node(operator.itemgetter(1),
37
                      key=operator.itemgetter(0),
38
                      value=operator.itemgetter(0))
39
    def nodes():
40
        yield from g.nodes(data='type')
41
42
    @graph_model.edge(operator.itemgetter('type'))
43
    def edge():
44
        yield from ({'source': e[0], 'target': e[1], **e[2]} for e in g.edges.data())
45
46
    return graph_model
47
48
49
if __name__ == '__main__':
50
    from gui import ListboxChooser, RadiobuttonChooser
51
52
    graph_atlas = graphs.atlas()
53
54
    listbox_chooser = ListboxChooser('Choose Graph/s', graph_atlas)
55
    choices = list(listbox_chooser.get_choices())
56
    model = model(choices)
57
58
    # or
59
    # model(graph_atlas.values())
60
61
    radiobutton_chooser = RadiobuttonChooser('Choose Materializer',
62
                                             options={m.name: m.value for m in Materializers},
63
                                             default=(None, None))
64
    result = radiobutton_chooser.get_choice()
65
    builder, handler = result[1]
66
    materialize(model, builder=builder, builder_output_handler=handler)
67