-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
92 lines (82 loc) · 3.18 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import PySimpleGUI as sg
import networkx as nx
import matplotlib
import threading
from tui import kClique
def main():
sg.theme('TanBlue')
sg.set_options(font=("Helvetica", 40))
layout = [
[sg.Text('Choose a text file for the graph:')],
[
sg.InputText('graph.txt', key='-FILE_PATH-'),
sg.FileBrowse(file_types=(("Text Files", "*.txt"),)),
],
[sg.Text('Choose an integer k:')],
[sg.InputText('1', key='-K-')],
[sg.Button('Submit')],
[sg.Text('_' * 80)],
[sg.Text('Loading...', key='-LOADING-', visible=False)],
[sg.Multiline(key='-OUTPUT-', visible=False)],
[sg.Canvas(key='-CANVAS-', visible=False)]
]
window = sg.Window('K-Clique', layout, grab_anywhere=False, size=(2000, 2000), finalize=True)
matplotlib.use('TkAgg')
fig = matplotlib.figure.Figure(figsize=(5, 5), dpi=300)
tkcanvas = FigureCanvasTkAgg(fig, window['-CANVAS-'].TKCanvas)
tkcanvas.get_tk_widget().pack(side='top', fill='both', expand=1)
def calculate_output(filepath, k):
nodes, edges, _, pos_vars, message = kClique(filepath, k)
if pos_vars is not None:
G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)
node_color = ['lightgrey'] * len(nodes)
nodes_in_clique = []
for v in pos_vars:
v = v.split('_')
if v[0] == "n":
idx = int(v[1]) - 1
nodes_in_clique.append(nodes[idx])
node_color[idx] = '#ff6961'
edge_color = list(map(
lambda edge: '#ff6961'
if edge[0] in nodes_in_clique and edge[1] in nodes_in_clique
else 'lightgrey',
edges,
))
fig.clear()
nx.draw_networkx(G,
pos=nx.circular_layout(G),
ax=fig.gca(),
node_color=node_color,
edge_color=edge_color)
fig.canvas.draw()
if len(pos_vars) == 0:
window['-OUTPUT-'].update(message)
else:
window['-OUTPUT-'].update(message)
window['-OUTPUT-'].update(visible=pos_vars is None or len(pos_vars) == 0)
window['-CANVAS-'].update(visible=pos_vars is not None)
window.write_event_value('JOB DONE', None)
while True:
event, values = window.read()
if event == 'Submit':
window['Submit'].update(disabled=True)
window['-OUTPUT-'].update(visible=False)
window['-CANVAS-'].update(visible=False)
window['-LOADING-'].update(visible=True)
filepath = values['-FILE_PATH-']
k = values['-K-']
threading.Thread(target=calculate_output, args=(filepath, k), daemon=True).start()
elif event == 'JOB DONE':
window['Submit'].update(disabled=False)
window['-LOADING-'].update(visible=False)
elif event in ('Exit', None):
break
window.close()
if __name__ == '__main__':
main()