-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
658 lines (499 loc) · 23.1 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
"""Main program module. Builds GUI and run program.
"""
# TODO:
# 1. Fix checkers messages
import itertools
import json
import os
import sys
from math import ceil, inf, pi
from pathlib import Path
from typing import Tuple
import numpy as np
import pyqtgraph as pg
import pyqtgraph.opengl as gl
from pyqtgraph.parametertree import Parameter, ParameterTree
from pyqtgraph.Qt.QtGui import QAction
from pyqtgraph.Qt.QtWidgets import (QFileDialog, QHBoxLayout, QMainWindow,
QPushButton, QSplitter, QTabWidget,
QVBoxLayout, QWidget)
from seval import safe_eval
from exporter import Exporter2D
from iterate import Worker
from iterate_3d import Worker3D
from point import Point3, nPoint
def parse_m(data: str) -> Point3:
"""Parse point data from text box in format (x:y:z).
Args:
data (str): data from the text box
Returns:
Point3: parsed point
"""
if not data:
return Point3(inf, inf, inf)
data = data.strip().replace(' ', '')[1:-1]
listed = list(map(float, data.split(':')))
return Point3(*listed)
def parse_vertices(data: str) -> list:
"""Parse vertices data from text box in format
(x1:y1:z1)
(x2:y2:z2)
.
.
.
(xn:yn:zn)
Args:
data (str): data from the text box
Returns:
list: list of parsed vertices of type Point3 or nPoint
"""
improved_data = data.strip().replace(' ', '').replace(',', '.').split('\n')
# remove ( and )
listed = [i[1:-1] for i in improved_data]
# make triples (x, y, z)
paired = [tuple(map(float, i.split(':'))) for i in listed]
if len(paired[0]) == 3:
return [Point3(x, y, z) for (x, y, z) in paired]
return [nPoint(x, y, z, w) for (x, y, z, w) in paired]
def parse_colors(data: str) -> list:
"""Parse colors data from text box in format #HEX1, #HEX2, ...
Args:
data (str): data from the text box
Returns:
list: list of strings in format ['#HEX1', '#HEX2', ...]
"""
if not data:
return []
# remove spaces, etc
improved_data = data.strip().replace(' ', '').split(',')
return [f'{i}' for i in improved_data]
def parse_limits(data: str) -> Tuple[float, float, float, float]:
"""Parse limits data from text box in format "xmin, xmax, ymin, ymax"."""
if not data:
return (inf, inf, inf, inf)
improved_data = data.strip().replace(' ', '').split(',')
return tuple(float(i) for i in improved_data)
class Application:
"""Main class that build GUI.
"""
def __init__(self):
pg.setConfigOptions(antialias=True)
self.app = pg.mkQApp('pyv')
self.main_window = QMainWindow()
self.main_window.setWindowTitle('pyv')
children = [
Parameter.create(name='Вершины',
type='text',
value='(2:0:1)\n(4:2:1)\n(4:-2:1)'),
{'name': 'Стартовая точка', 'type': 'str', 'value': '(3:0:1)'},
{'name': 'Цвета точек',
'type': 'str',
'value': '#00406b, #008000, #781f19'},
{'name': 'Случайные цвета', 'type': 'bool', 'value': False},
{'name': 'lambda', 'type': 'str', 'value': '1.0'},
{'name': 'Рисовать вторую середину',
'type': 'bool',
'value': False},
{'name': 'Пределы', 'type': 'str', 'value': ''},
{'name': 'Угадывать пределы', 'type': 'bool', 'value': False},
{'name': 'Угадывать пределы (включить абсолют)',
'type': 'bool',
'value': True},
{'name': 'Рисовать границы', 'type': 'bool', 'value': True},
{'name': 'Ширина границ', 'type': 'float', 'value': 1.0},
{'name': 'Рисовать абсолют', 'type': 'bool', 'value': True},
{'name': 'Цвет абсолюта', 'type': 'str', 'value': '#000000'},
{'name': 'Количество итераций', 'type': 'str', 'value': '2**13'},
# {'name': 'Количество итераций', 'type': 'str', 'value': '2'},
{'name': 'Размер точки', 'type': 'float', 'value': 1.0},
{'name': 'Тип репера', 'type': 'int', 'value': 1},
Parameter.create(name='Built-in checker', type='list'),
Parameter.create(name='Checker', type='file'),
{'name': 'Стратегия',
'type': 'str',
'value': f'{os.getcwd()}/strategies/default.py'}
]
children_exp = [
{'name': 'dpi', 'type': 'int', 'value': 500},
{'name': 'Директория по умолчанию',
'type': 'str',
'value': os.getcwd()},
{'name': 'Расширение по умолчанию', 'type': 'str', 'value': 'eps'},
{'name': 'Ширина линий абсолюта', 'type': 'float', 'value': 0.25},
{'name': 'Ширина границ', 'type': 'float', 'value': 0.25},
{'name': 'Размер точки', 'type': 'float', 'value': 0.25},
{'name': 'Рисовать оси', 'type': 'bool', 'value': True},
{'name': 'Растеризовать', 'type': 'bool', 'value': True},
{'name': 'Имя файла',
'type': 'str',
'value': '$color$_$lambda$_$verticies$.eps'},
]
self.params = Parameter.create(name='Параметры',
type='group',
children=children)
self.params_exp = Parameter.create(name='Экспорт',
type='group',
children=children_exp)
self.params.child('Built-in checker').setLimits(['shapely & polygon', 'shapely', 'polygon'])
# self.params.child('Built-in checker').setLimits(['shapely', 'shapely & polygon', 'polygon'])
param_tree = ParameterTree(showHeader=False)
param_tree.addParameters(self.params)
param_tree.addParameters(self.params_exp)
self.graphics_widget_2d = pg.PlotWidget(background='w')
self.canvas_2d = self.graphics_widget_2d.getPlotItem()
self.canvas_2d.setAspectLocked(True, 1.0)
# self.graphics_widget_3d =\
# gl.GLViewWidget(rotationMethod='quaternion')
self.graphics_widget_3d = gl.GLViewWidget()
self.graphics_widget_3d.setBackgroundColor('w')
self.scatter_2d = pg.ScatterPlotItem()
self.scatter_3d = gl.GLScatterPlotItem()
self.scatter_3d.setGLOptions('translucent')
self.canvas_2d.addItem(self.scatter_2d)
self.graphics_widget_3d.addItem(self.scatter_3d)
action_export = QAction(self.main_window)
action_export.setObjectName('actionExport')
action_export.setText('Экспортировать')
action_export.triggered.connect(self.export_conf)
action_import = QAction(self.main_window)
action_import.setObjectName('actionImport')
action_import.setText('Импортировать')
action_import.triggered.connect(self.import_conf)
menu = self.main_window.menuBar()
conf = menu.addMenu('Конфигурация')
conf.addAction(action_export)
conf.addAction(action_import)
btn_plot = QPushButton('Plot')
btn_plot.setShortcut('Ctrl+Return')
btn_export = QPushButton('Export')
btn_plot.clicked.connect(self.plot_2d)
btn_export.clicked.connect(self.export_2d)
button_layout = QHBoxLayout()
button_layout.addWidget(btn_plot)
button_layout.addWidget(btn_export)
main_layout = QVBoxLayout()
main_layout.addWidget(param_tree)
main_layout.addLayout(button_layout)
params_and_buttons_widget = QWidget()
params_and_buttons_widget.setLayout(main_layout)
# 2d, 3d tab on the bottom
tab_widget = QTabWidget()
tab_widget.addTab(self.graphics_widget_2d, '2d')
tab_widget.addTab(self.graphics_widget_3d, '3d')
tab_widget.setTabPosition(QTabWidget.TabPosition.South)
tab_widget.currentChanged.connect(lambda: self.tab_changed(tab_widget,
btn_plot,
btn_export))
splitter = QSplitter()
splitter.addWidget(tab_widget)
splitter.addWidget(params_and_buttons_widget)
self.main_window.setCentralWidget(splitter)
self.main_window.showMaximized()
self.worker = Worker()
self.worker_3d = Worker3D()
def read_config(self):
"""Read GUI settings and write them to variables.
"""
self.worker.vertices =\
parse_vertices(self.params.child('Вершины').value())
self.worker.start_point =\
parse_m(self.params.child('Стартовая точка').value())
if not self.params.child('Стартовая точка').value():
self.worker.start_point = self.worker.gen_start_point()
self.worker.coloring = False
if val := self.params.child('Цвета точек').value():
self.worker.vertices_colors = parse_colors(val)
if not self.params.child('Цвета точек').value():
self.worker.vertices_colors =\
['#000000'] * len(self.worker.vertices)
if self.params.child('Случайные цвета').value():
self.worker.vertices_colors = self.worker.gen_random_colors()
self.worker.double_mid =\
self.params.child('Рисовать вторую середину').value()
xmin, xmax, ymin, ymax =\
parse_limits(self.params.child('Пределы').value())
if not self.params.child('Пределы').value():
xmin, xmax, ymin, ymax = self.worker.guess_limits()
if self.params.child('Угадывать пределы (включить абсолют)').value():
xmin, xmax, ymin, ymax =\
self.worker.guess_limits(contains_absolute=True)
self.worker.xmin, self.worker.xmax = xmin, xmax
self.worker.ymin, self.worker.ymax = ymin, ymax
self.canvas_2d.setXRange(xmin, xmax)
self.canvas_2d.setYRange(ymin, ymax)
if val := self.params.child('Checker').value():
sys.path.append(os.path.dirname(val))
module = __import__(Path(val).stem)
self.worker.checker = module.checker
if val := self.params.child('Стратегия').value():
sys.path.append(os.path.dirname(val))
module = __import__(Path(val).stem)
self.worker.strategy = module.strategy
if self.params.child('Рисовать границы').value():
width = 3.0
if val := self.params.child('Ширина границ').value():
width = val
vertices = self.worker.vertices + [self.worker.vertices[0]]
for_pairing = [i.to_point2() for i in vertices]
for cur, nex in itertools.pairwise(for_pairing):
self.canvas_2d.plot([cur.x, nex.x],
[cur.y, nex.y],
pen=pg.mkPen('#000000',
width=width))
if val := self.params.child('Тип репера').value():
self.worker.frame_type = val
if self.params.child('Рисовать абсолют').value():
color = '#ff0000'
if val := self.params.child('Цвет абсолюта').value():
color = val
if self.worker.frame_type == 1:
# Absolute is a circle
# Абсолют — окружность
points = np.linspace(0, 2 * pi, num=100)
circle = self.canvas_2d.plot()
circle.setData(np.cos(points),
np.sin(points),
pen=pg.mkPen(color),
skipFiniteCheck=True)
if self.worker.frame_type == 2:
# Absolute is a hyperbola yx-1=0
left = xmin - 2
right = xmax + 2
cnt = ceil(abs(right - left) / 0.01)
if left * right > 0:
x_coords = np.linspace(left, right, cnt)
else:
# xmin * xmax < 0, значит, ноль содержится
x_coords = np.linspace(0.1, right, cnt)
y_coords = list(map(lambda x: 1 / x, x_coords))
hyperbole = pg.PlotCurveItem(x_coords,
y_coords,
pen=pg.mkPen(color),
skipFiniteCheck=True)
self.canvas_2d.addItem(hyperbole)
x_coords = np.linspace(left, -0.1, cnt)
y_coords = list(map(lambda x: 1 / x, x_coords))
hyperbole = pg.PlotCurveItem(x_coords,
y_coords,
pen=pg.mkPen(color),
skipFiniteCheck=True)
self.canvas_2d.addItem(hyperbole)
d = {'shapely & polygon': self.worker.checker,
'shapely': self.worker.shapely_default,
'polygon': self.worker.polygon_default}
self.worker.checker = d[self.params.child('Built-in checker').value()]
def plot_2d(self, rel=None, export_function=None):
"""Run chaos game and plot with ScatterPlot.
Args:
rel (_type_, optional): relation for segment division. Defaults to None.
export_function (_type_, optional): export right after the plot. Defaults to None.
"""
self.main_window.setWindowTitle('pyv PLOTTING')
self.canvas_2d.clear()
self.canvas_2d.addItem(self.scatter_2d)
# To avoid
# RuntimeError: wrapped C/C++ object of type Worker has been deleted
self.worker = Worker()
self.read_config()
if not rel:
rel = float(list(self.params.child('lambda').value().replace(' ', '').split(','))[0])
cnt = safe_eval(self.params.child("Количество итераций").value())
size = 1.0
if val := self.params.child('Размер точки').value():
size = val
def work_finished(x, y, colors):
x, y, colors = self.worker.clean(x, y, colors)
self.scatter_2d.setData(x=x,
y=y,
size=size,
brush=colors)
self.main_window.setWindowTitle('pyv DONE')
if export_function:
export_function(x, y, colors, rel)
# Run in separate thread
self.worker.args = (cnt,)
self.worker.kwargs = {'rel': rel}
self.worker.signals.result.connect(work_finished)
self.worker.threadpool.start(self.worker)
def plot_3d(self):
"""Run chaos game and plot with GLScatterPlot.
"""
self.main_window.setWindowTitle('pyv PLOTTING')
self.graphics_widget_3d.clear()
self.graphics_widget_3d.addItem(self.scatter_3d)
self.worker_3d = Worker3D()
self.worker_3d.vertices =\
parse_vertices(self.params.child('Вершины').value())
self.worker_3d.start_point = nPoint(1, 1, 1)
self.worker_3d.start_point = self.worker_3d.gen_start_point()
self.worker_3d.vertices_colors = self.worker_3d.gen_random_colors()
# draw boundary
lower = [i.to_lower_dimension().to_tuple()
for i in self.worker_3d.vertices]
# lower += [lower[-1]]
# for cur, nex in zip(lower, lower[1:]):
# # print(np.shape(np.array([cur, nex])))
# line = gl.GLLinePlotItem(pos=np.array([cur, nex]),
# color=pg.glColor('k'),
# width=5,
# antialias=True)
# self.graphics_widget_3d.addItem(line)
lower_first = lower[:4]
lower_first += [lower_first[0]]
for cur, nex in zip(lower_first, lower_first[1:]):
line = gl.GLLinePlotItem(pos=np.array([cur, nex]),
color=pg.glColor('k'),
width=5,
antialias=True)
self.graphics_widget_3d.addItem(line)
lower_second = lower[4:]
lower_second += [lower_second[0]]
for cur, nex in zip(lower_second, lower_second[1:]):
line = gl.GLLinePlotItem(pos=np.array([cur, nex]),
color=pg.glColor('k'),
width=5,
antialias=True)
self.graphics_widget_3d.addItem(line)
for cur, nex in zip(lower[:4], lower[4:]):
line = gl.GLLinePlotItem(pos=np.array([cur, nex]),
color=pg.glColor('k'),
width=5,
antialias=True)
self.graphics_widget_3d.addItem(line)
# draw absolute
mesh_data = gl.MeshData.sphere(rows=128, cols=128)
sphere = gl.GLMeshItem(meshdata=mesh_data,
smooth=True,
color=pg.glColor('r'))
self.graphics_widget_3d.addItem(sphere)
# create three grids, add each to the view
xgrid = gl.GLGridItem(color=(0, 0, 0, 255))
ygrid = gl.GLGridItem(color=(0, 0, 0, 255))
zgrid = gl.GLGridItem(color=(0, 0, 0, 255))
self.graphics_widget_3d.addItem(xgrid)
self.graphics_widget_3d.addItem(ygrid)
self.graphics_widget_3d.addItem(zgrid)
# rotate x and y grids to face the correct direction
xgrid.rotate(90, 0, 1, 0)
ygrid.rotate(90, 1, 0, 0)
relation = self.params.child('lambda').value()
cnt = safe_eval(self.params.child("Количество точек").value())
size = 1.0
if val := self.params.child('Размер точки').value():
size = val
def work_finished(x, y, z, colors):
if len(x) < 1000:
self.plot_3d()
return
colors = np.array(list(map(pg.glColor, colors)))
# colors = np.array([pg.glColor(i) for i in colors])
x = x[:, np.newaxis]
y = y[:, np.newaxis]
z = z[:, np.newaxis]
data = np.hstack((x, y, z))
print(np.shape(data))
print(np.shape(colors))
print('\n')
self.scatter_3d.setData(pos=data,
color=colors,
size=5 * size,
pxMode=True)
self.main_window.setWindowTitle('pyv DONE')
# run in separate thread
self.worker_3d.args = (cnt,)
self.worker_3d.kwargs = {'rel': relation}
self.worker_3d.signals.result.connect(work_finished)
self.worker_3d.threadpool.start(self.worker_3d)
def export_2d(self):
"""Export image to file with matplotlib.
"""
self.main_window.setWindowTitle('pyv EXPORTING')
def plot_finished(x, y, colors, rel):
self.main_window.setWindowTitle('pyv EXPORTING')
exporter = Exporter2D()
exporter.args = (self.worker, self.params, self.params_exp,
x, y, colors, rel)
exporter.kwargs = {}
exporter.signals.finished.connect(work_finished)
exporter.threadpool.start(exporter)
def work_finished():
self.main_window.setWindowTitle('pyv DONE')
if relations:
self.plot_2d(rel=relations.pop(),
export_function=plot_finished)
relations = set(map(float,
self.params.child('lambda').value().replace(' ', '').split(',')))
self.plot_2d(rel=relations.pop(), export_function=plot_finished)
def export_3d(self):
"""TODO.
"""
filename = 'test.png'
# №1
# d = self.graphics_widget_3d.renderToArray((1000, 1000))
# pg.makeQImage(d).save(filename, quality=100)
# № 2
# grabs current
self.graphics_widget_3d.grabFramebuffer().save(filename)
# № 3
# matplotlib?
def export_conf(self):
"""Write current configuration to the JSON file.
"""
default_name = ''
for i in self.worker.vertices:
default_name += f'_({i.x:.1f}:{i.y:.1f}:{i.z:.1f})'
if default_name[0] == '_':
default_name = default_name[1:]
default_name += '.json'
filt = 'Json File (*.json)'
path, _ = QFileDialog.getSaveFileName(
parent=self.main_window,
caption='Choose file',
directory=os.getcwd() + f'/{default_name}',
filter=filt,
initialFilter=filt
)
if not path:
return
if path.rfind('.json') == -1:
path += '.json'
with open(path, 'w', encoding='utf-8') as file:
json_data = {'params': self.params.saveState(),
'params_exp': self.params_exp.saveState()}
json.dump(json_data, file, indent=4)
def import_conf(self):
"""Read parameters trees from the JSON file.
"""
filt = 'Json File (*.json)'
path, _ = QFileDialog.getOpenFileName(
parent=self.main_window,
caption='Choose file',
directory=os.getcwd(),
filter=filt,
initialFilter=filt
)
if not path:
return
with open(path, 'r', encoding='utf-8') as file:
json_data = json.load(file)
self.params.restoreState(json_data['params'])
self.params_exp.restoreState(json_data['params_exp'])
def tab_changed(self, tab_widget, button_plot, button_export):
"""Switch plot and export signals, when switch between tabs.
Args:
tab_widget (_type_): tab we are actually on
button_plot (_type_): plot button
button_export (_type_): export button
"""
button_plot.disconnect()
button_export.disconnect()
# idx == 0 — 2d
# idx == 1 — 3d
choice = [{'plot': self.plot_2d, 'export': self.export_2d},
{'plot': self.plot_3d, 'export': self.export_3d}]
idx = tab_widget.currentIndex()
button_plot.clicked.connect(choice[idx]['plot'])
button_export.clicked.connect(choice[idx]['export'])
if __name__ == '__main__':
app = Application()
pg.exec()