-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
368 lines (275 loc) · 14.3 KB
/
main.py
File metadata and controls
368 lines (275 loc) · 14.3 KB
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
import sys
from threading import Thread
from PySide2.QtWidgets import (QApplication, QPushButton, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QLabel, QLineEdit, QComboBox, QSplitter, QSpacerItem, QSizePolicy, QGraphicsView, QGraphicsScene)
from PySide2.QtCore import Slot, Qt, QRectF, QRect, Signal
from PySide2.QtGui import QPen, QBrush
from utils.cell import Cell
from utils.color_manager import ColorManager
from partition.partition_utils import (EuclidianDistance, ChebyshevDistance, TaxicabDistance, sign,
MonotonicStepFunction, ConstantStepFunction, SeriesStepFunction, LeftRectangularIntergate)
from partition.partition import ( FuzzyPartitionWithFixedCentersAlgorithm, SimplePartitionWithFixedCentersAlgorithm)
WINDOW_SIZE = 500
class SettingsWidget(QWidget):
def __init__(self, boardWidget, *args, **kwargs):
super().__init__(*args, **kwargs)
self._boardWidget = boardWidget
self._freeCoefficients = dict()
self._layout = self._buildSettingsLayout()
self._layout.setAlignment(Qt.AlignTop)
self.setLayout(self._layout)
@Slot()
def _onAddCenterButtonClicked(self):
if not self._xCoordinateInput.text() or not self._yCoordinateInput.text():
return
x = round(float(self._xCoordinateInput.text()) * WINDOW_SIZE)
y = round(float(self._yCoordinateInput.text()) * WINDOW_SIZE)
wasAdded = self._boardWidget.addCenter(x, y)
if not wasAdded:
return
freeCoeficientsLabel = QLabel(text='a<sub>i</sub> ({:3.3f}, {:3.3f}): '.format(
x / WINDOW_SIZE, y / WINDOW_SIZE))
self._freeCoeficientsInput = QLineEdit()
self._freeCoeficientsInput.setPlaceholderText('0')
freeCoeficientsLayout = QHBoxLayout()
freeCoeficientsLayout.addWidget(freeCoeficientsLabel)
freeCoeficientsLayout.addWidget(self._freeCoeficientsInput)
self._layout.addLayout(freeCoeficientsLayout)
self._freeCoefficients[(x, y)] = self._freeCoeficientsInput
@Slot()
def _activateFuzzyInput(self, newText):
allControls = [
self._confidenceInput,
self._precisionInput,
self._xCoordinateInput,
self._yCoordinateInput,
self._addCenterButton,
]
activator = {
'simple partition': [self._xCoordinateInput, self._yCoordinateInput, self._addCenterButton],
'fuzzy partition': [self._confidenceInput, self._precisionInput, self._xCoordinateInput, self._yCoordinateInput, self._addCenterButton,]
}
for item in activator:
if item == newText.lower():
for control in allControls:
control.setEnabled(control in activator[item])
def _buildSettingsLayout(self):
#------------------------------------Distance options------------------------------------#
self._distanceOptions = QComboBox()
self._distanceOptions.addItems(['Euclidian Distance', 'Chebyshev Distance', 'Taxicab Distance'])
distanceLayout = QVBoxLayout()
distanceLayout.addWidget(self._distanceOptions)
#----------------------------------------------------------------------------------------#
#------------------------------------Partition options------------------------------------#
self._partitionOptions = QComboBox()
self._partitionOptions.addItems(['Simple Partition', 'Fuzzy Partition'])
partitionLayout = QVBoxLayout()
partitionLayout.addWidget(self._partitionOptions)
self._partitionOptions.currentTextChanged.connect(self._activateFuzzyInput)
#-----------------------------------------------------------------------------------------#
#------------------------------------Confidence Degree------------------------------------#
self._confidenceInput = QLineEdit(enabled=True)
self._confidenceInput.setPlaceholderText('Confidence degree')
confidenceLayout = QVBoxLayout()
confidenceLayout.addWidget(self._confidenceInput)
self._precisionInput = QLineEdit(enabled=True)
self._precisionInput.setPlaceholderText('Gradient method precision')
precisionLayout = QVBoxLayout()
precisionLayout.addWidget(self._precisionInput)
#-----------------------------------------------------------------------------------------#
#------------------------------------Add center------------------------------------#
self._xCoordinateInput = QLineEdit()
self._xCoordinateInput.setPlaceholderText('x center coordinate')
self._yCoordinateInput = QLineEdit()
self._yCoordinateInput.setPlaceholderText('y center coordinate')
self._addCenterButton = QPushButton()
self._addCenterButton.setText('Add new center')
self._addCenterButton.setMinimumWidth(400)
addCentersLayout = QVBoxLayout()
addCentersLayout.addWidget(self._xCoordinateInput)
addCentersLayout.addWidget(self._yCoordinateInput)
addCentersLayout.addWidget(self._addCenterButton)
self._addCenterButton.clicked.connect(self._onAddCenterButtonClicked)
#-----------------------------------------------------------------------------------#
spacer = QSpacerItem(40, 40, QSizePolicy.Expanding, QSizePolicy.Minimum)
settingsLayout = QVBoxLayout()
settingsLayout.addLayout(partitionLayout)
settingsLayout.addLayout(distanceLayout)
settingsLayout.addLayout(confidenceLayout)
settingsLayout.addLayout(precisionLayout)
settingsLayout.addItem(spacer)
settingsLayout.addLayout(addCentersLayout)
return settingsLayout
def distance(self):
returnDistance = EuclidianDistance
if 'euclidian' in self._distanceOptions.currentText().lower():
returnDistance = EuclidianDistance
elif 'cheb' in self._distanceOptions.currentText().lower():
returnDistance = ChebyshevDistance
elif 'taxi' in self._distanceOptions.currentText().lower():
returnDistance = TaxicabDistance
return returnDistance
def partitionAlgorithm(self):
return self._partitionOptions.currentText().lower()
def precision(self):
return float(self._precisionInput.text()) if self._precisionInput.text() else 0.01
def confidence(self):
return float(self._confidenceInput.text()) if self._confidenceInput.text() else 0.0
def freeCoefficients(self):
return { center: float(freeCoefficient.text()) * WINDOW_SIZE
if freeCoefficient.text() else 0.0 for center, freeCoefficient in self._freeCoefficients.items() }
class PartitionCentralWidget(QWidget):
def __init__(self, boardWidget, settingsWidget, *args, **kwargs):
super().__init__(*args, **kwargs)
self._boardWidget = boardWidget
self._settingsWidget = settingsWidget
self._startPartitionButton = QPushButton('Start Partition')
self._paintGrayScale = QPushButton('Paint as grayscale')
self._paintGrayScale.setEnabled(False)
self._startPartitionButton.clicked.connect(self._onStartPartitionButtonClicked)
self._paintGrayScale.clicked.connect(self._onPaintGrayScaleButtonClicked)
self._splitter = QSplitter(Qt.Horizontal)
self._splitter.addWidget(self._boardWidget)
self._splitter.addWidget(self._settingsWidget)
mainLayout = QVBoxLayout()
mainLayout.addWidget(self._splitter)
mainLayout.addWidget(self._startPartitionButton)
mainLayout.addWidget(self._paintGrayScale)
self.setLayout(mainLayout)
@Slot()
def _onStartPartitionButtonClicked(self):
self._startPartitionButton.setEnabled(False)
partitionAlgorithm = self._settingsWidget.partitionAlgorithm()
if 'simple' in partitionAlgorithm.lower():
self._boardWidget.startSimplePartition(
self._settingsWidget.distance(), self._settingsWidget.freeCoefficients())
else:
self._boardWidget.startFuzzyPartition(
self._settingsWidget.distance(),
self._settingsWidget.confidence(),
self._settingsWidget.freeCoefficients(),
self._settingsWidget.precision())
self._startPartitionButton.setEnabled(True)
self._paintGrayScale.setEnabled(True)
@Slot()
def _onPaintGrayScaleButtonClicked(self):
self._paintGrayScale.setEnabled(False)
self._startPartitionButton.setEnabled(False)
self._boardWidget.toGrayScale()
self._startPartitionButton.setEnabled(True)
class BoardWidget(QGraphicsView):
CELL_CENTER_COLOR = Qt.black
CELL_SIMPLE_COLOR = Qt.white
CELL_SIMPLE_GRAY_COLOR = Qt.gray
CELL_BOUNDS_COLOR = Qt.black
update_progress = Signal(object)
def __init__(self, size, cellSize, application, *args, **kwargs):
super().__init__(*args, **kwargs)
self._application = application
self._windowSize = size
self._cellSize = cellSize
self._board = dict()
self._colors = dict()
self._scene = QGraphicsScene()
self.setScene(self._scene)
self.setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
self.setFixedSize(self._windowSize * 1.05, self._windowSize * 1.05)
self.update_progress.connect(self.updateProgress)
for cellX in range(0, self._windowSize, self._cellSize):
for cellY in range(0, self._windowSize, self._cellSize):
self._board[(cellX, cellY)] = Cell(color=self.CELL_SIMPLE_COLOR)
self.updateColorAt((cellX, cellY))
def updateColorAt(self, point):
rect = QRectF(point[0], point[1], self._cellSize, self._cellSize)
self._scene.invalidate(rect)
self._scene.addRect(rect, QPen(Qt.black), QBrush(self._board[point].color))
def addCenter(self, x, y):
if (x, y) in self._board.keys():
cell = self._board[(x, y)]
if cell.isCenter:
return False
cell.isCenter = True
cell.color = self.CELL_CENTER_COLOR
self.updateColorAt((x, y))
return True
return False
def getExtraneousAdjecements(self, x, y):
adjecements = []
if (x < self._cellSize or x + self._cellSize >= self._windowSize):
return adjecements
if (y < self._cellSize or y + self._cellSize >= self._windowSize):
return adjecements
if (self._board[(x, y)].color == self.CELL_BOUNDS_COLOR):
return adjecements
checkedColor = self._board[(x, y)].color
for xDelta in [0, self._cellSize, -self._cellSize]:
for yDelta in [0, self._cellSize, -self._cellSize]:
candidate = self._board[(x + xDelta, y + yDelta)];
if (candidate.previousColor != checkedColor and candidate.color != self.CELL_BOUNDS_COLOR):
adjecements.append((x + xDelta, y + yDelta))
return adjecements
def forEachPoint(self, callback):
for x in range(0, self._windowSize, self._cellSize):
for y in range(0, self._windowSize, self._cellSize):
callback(x, y)
def savePrevColorAndGrayifyNeutralCells(self, x, y):
self._board[(x, y)].previousColor = self._board[(x, y)].color
if (self._board[(x, y)].color == self.CELL_SIMPLE_COLOR):
self._board[(x, y)].color = self.CELL_SIMPLE_GRAY_COLOR
self.updateColorAt((x, y))
def markBoundsAsBlack(self, x, y):
adjecements = self.getExtraneousAdjecements(x, y)
for point in adjecements:
if (self._board[point].color != self.CELL_SIMPLE_GRAY_COLOR):
self._board[point].color = self.CELL_BOUNDS_COLOR
self.updateColorAt(point)
def markRelatedPointAsWhite(self, x, y):
if (self._board[(x, y)].color != self.CELL_SIMPLE_GRAY_COLOR and self._board[(x, y)].color != self.CELL_BOUNDS_COLOR):
self._board[(x, y)].color = self.CELL_SIMPLE_COLOR
self.updateColorAt((x, y))
def toGrayScale(self):
self.forEachPoint(self.savePrevColorAndGrayifyNeutralCells)
self.forEachPoint(self.markBoundsAsBlack)
self.forEachPoint(self.markRelatedPointAsWhite)
@Slot(object)
def updateProgress(self, object):
point = object[0]
center = object[1]
if center:
self._board[point].color = self._colors[center]
self.updateColorAt(point)
def pointCalculatedCallback(self, point, center):
self.update_progress.emit((point, center))
def startSimplePartition(self, distance, freeCoefficients):
self._clearBoard()
centers = [ point for point in self._board.keys() if self._board[point].isCenter]
board = [ point for point in self._board.keys()]
partition = SimplePartitionWithFixedCentersAlgorithm(
board, centers, freeCoefficients, distance, self.pointCalculatedCallback)
self._colors = dict(zip(centers, ColorManager.GetRandomColors(len(centers))))
thread = Thread(target = partition.calculatePartition)
thread.start()
def startFuzzyPartition(self, distance, confidenceDeegre, freeCoefficients, precision):
self._clearBoard()
centers = [ point for point in self._board.keys() if self._board[point].isCenter]
board = [ point for point in self._board.keys()]
partition = FuzzyPartitionWithFixedCentersAlgorithm(
board, centers, SeriesStepFunction(25), confidenceDeegre, freeCoefficients, distance, self.pointCalculatedCallback, precision)
self._colors = dict(zip(centers, ColorManager.GetRandomColors(len(centers))))
thread = Thread(target = partition.calculatePartition)
thread.start()
def _clearBoard(self):
for point, cell in self._board.items():
if not cell.isCenter:
cell.color = self.CELL_SIMPLE_COLOR
self.updateColorAt(point)
if __name__ == '__main__':
application = QApplication(sys.argv)
mainWindow = QMainWindow()
mainWindow.setWindowTitle('Partition')
boardWidget = BoardWidget(WINDOW_SIZE, 5, application)
settingsWidget = SettingsWidget(boardWidget)
centralWidget = PartitionCentralWidget(boardWidget, settingsWidget)
mainWindow.setCentralWidget(centralWidget)
mainWindow.show()
sys.exit(application.exec_())