Issues (35)

examples/math/graph_atlas.py (1 issue)

1
import operator
2
3
import graphs
4
import networkx as nx
5
from materializers import Materializers, materialize
6
7
import graphinate
8
9
10
def model(items: list[tuple[str, 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
        items: A list 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
    def items_iter(recs):
27
        for name, g in recs:
28
            print(name)
29
            yield g
30
31
    g = nx.disjoint_union_all(items_iter(items)) if len(items) > 1 else items[0][1]
32
33
    graph_model = graphinate.model('Graph Atlas')
34
35
    @graph_model.node(operator.itemgetter(1),
36
                      key=operator.itemgetter(0),
37
                      value=operator.itemgetter(0))
38
    def nodes():
39
        yield from g.nodes(data='type')
40
41
    @graph_model.edge(operator.itemgetter('type'))
42
    def edge():
43
        yield from ({'source': e[0], 'target': e[1], **e[2]} for e in g.edges.data())
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable e does not seem to be defined.
Loading history...
44
45
    return graph_model
46
47
48
if __name__ == '__main__':
49
    from gui import ListboxChooser, RadiobuttonChooser
50
51
    graph_atlas = graphs.atlas()
52
53
    listbox_chooser = ListboxChooser('Choose Graph/s', graph_atlas)
54
    choices = list(listbox_chooser.get_choices())
55
    model = model(choices)
56
57
    # or
58
    # model(graph_atlas.values())
59
60
    radiobutton_chooser = RadiobuttonChooser('Choose Materializer',
61
                                             options={m.name: m.value for m in Materializers},
62
                                             default=(None, None))
63
    result = radiobutton_chooser.get_choice()
64
    builder, handler = result[1]
65
    materialize(model, builder=builder, builder_output_handler=handler)
66