-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunDictator.py
More file actions
140 lines (112 loc) · 5.25 KB
/
Copy pathRunDictator.py
File metadata and controls
140 lines (112 loc) · 5.25 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
# -*- coding: utf-8 -*-
# @Time : 2021/7/18 21:15
# @Author : Jinhang
# @File : RunDictator.py
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCursor, QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
from Dictator_ui import Ui_MainWindow
from glob import glob
import numpy as np
import json
from TimeLogger import TimeLogger
SAY_SOMETHING = {
0: "Soldier, do something!",
1: "Hm, still got a lot to do ~",
2: "Oh, let's pick up them ASAP.",
3: "Still in control~ Better watch out.",
4: "Good! Keep going!",
5: "Perfect! Why not try another round?"
}
class MainPanel(Ui_MainWindow, QMainWindow):
def __init__(self):
super(MainPanel, self).__init__()
self.setupUi(self)
self.logger = TimeLogger()
self.source_directory_path = "algorithms/cfop/f2l"
self.source_item_paths = []
self.chosen_item_paths = []
self.places = [self.Label1, self.Label2, self.Label3, self.Label4, self.Label5]
self.alg_edits = [self.AlgEdit1, self.AlgEdit2, self.AlgEdit3, self.AlgEdit4, self.AlgEdit5]
self.alg_results = [self.Result1, self.Result2, self.Result3, self.Result4, self.Result5]
self.alg_answers = []
self.chosen_indices = []
self.ButtonRandom.setCursor(QCursor(Qt.PointingHandCursor))
self.ButtonSubmit.setCursor(QCursor(Qt.PointingHandCursor))
self.ButtonSubmit.setEnabled(False)
self.ButtonRandom.clicked.connect(self.generateRandomSet)
self.ButtonSubmit.clicked.connect(self.submit)
self.setupSource()
def setupSource(self):
self.logger.info(msg="Start loading resources", start=True)
self.source_item_paths = [p.replace('\\', '/') for p in sorted(glob(self.source_directory_path + '/*.png'))]
with open("algorithms/cfop/solution.json", 'r') as f:
self.solutions = json.load(f)["f2l"]
for sip in self.source_item_paths:
self.logger.debug(sip)
self.logger.info(msg="Finish loading resources")
def generateRandomSet(self):
self.logger.info(msg="Start generating random set", start=True)
self.reset_values()
n_items = len(self.source_item_paths)
if n_items > 5:
indices = np.arange(n_items)
np.random.shuffle(indices)
self.chosen_indices = indices[:5]
self.chosen_item_paths = [self.source_item_paths[i] for i in self.chosen_indices]
else:
self.chosen_item_paths = self.source_item_paths
for sip, qlabel in zip(self.chosen_item_paths, self.places):
self.logger.debug(msg="Load source image from {}".format(sip))
qlabel.setPixmap(QPixmap(sip))
chosen_algs = ["{:02d}".format(i + 1) for i in self.chosen_indices]
self.logger.debug(msg="Test algorithms: {}".format(chosen_algs))
self.logger.info(msg="Start generating random set")
self.ButtonSubmit.setEnabled(True)
def updateValues(self):
self.alg_answers = [
self.AlgEdit1.text(),
self.AlgEdit2.text(),
self.AlgEdit3.text(),
self.AlgEdit4.text(),
self.AlgEdit5.text()
]
def reset_values(self):
for qlabel, alg_edit, alg_result in zip(self.places, self.alg_edits, self.alg_results):
qlabel.setStyleSheet("background-color: rgb(127, 127, 127);")
alg_edit.setText("")
alg_result.setText("NA")
alg_result.setStyleSheet("color: rgb(0, 0, 0);")
self.LabelSaySomething.setText("")
def submit(self):
self.logger.info(msg="Start analysing answerers", start=True)
self.updateValues()
chosen_algs = ["{:02d}".format(i + 1) for i in self.chosen_indices]
self.logger.debug(msg="Load test algorithms: {}".format(chosen_algs))
chosen_solutions = [self.solutions[alg] for alg in chosen_algs]
self.logger.debug(msg="Load solutions: {}".format(chosen_solutions))
n_correct = 0
for i, (asw, sol, alg_result) in enumerate(zip(self.alg_answers, chosen_solutions, self.alg_results)):
shorted_asw = asw.replace(' ', '').replace('(', '').replace(')', '')
shorted_sol = sol.replace(' ', '').replace('(', '').replace(')', '')
self.logger.debug(msg="Analyse situation {}".format(i))
self.logger.debug(msg="Remove whitespaces, '(', ')' characters")
self.logger.debug(msg="Shorted answer: {}".format(shorted_asw))
self.logger.debug(msg="Shorted solution: {}".format(shorted_sol))
if shorted_asw != shorted_sol:
self.logger.debug(msg="Situation answer: FAIL!")
alg_result.setText(sol)
alg_result.setStyleSheet("color: rgb(255, 0, 0);")
else:
self.logger.debug(msg="Situation answer: PASS!")
alg_result.setText('PASS')
alg_result.setStyleSheet("color: rgb(0, 255, 0);")
n_correct += 1
self.LabelSaySomething.setText(SAY_SOMETHING[n_correct])
self.logger.info(msg="Finish analysing answerers ({}/5)".format(n_correct))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
main_panel = MainPanel()
main_panel.show()
sys.exit(app.exec_())