-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
317 lines (273 loc) · 11.2 KB
/
main.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
#!/usr/bin/python3
import sys, os
import pickle
import copy
from math import pi
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QMenu
from PyQt6.QtGui import QAction,QIcon
from PyQt6.QtWidgets import QMessageBox
from PyQt6.QtWidgets import QListWidget, QListWidgetItem, QAbstractItemView
from PyQt6.QtWidgets import QApplication, QMainWindow, QMenuBar, QMenu, QTabWidget, QVBoxLayout, QWidget, QLabel, QLineEdit, QTextEdit, QPushButton, QDialog, QHBoxLayout, QFileDialog
from uncertaintytrack import TrackUncertainty # Make sure to import your TrackUncertainty module
if getattr(sys, 'frozen', None):
basedir = sys._MEIPASS
else:
basedir = os.path.dirname(__file__)
class AboutDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("About")
self.setGeometry(100, 100, 400, 200)
layout = QVBoxLayout()
text_label = QLabel("<h1>PEHelper</h1><h2>v0.3.2</h2>This is a program designed for basic physics experiment data analysis, written by happyZYM.\nVisit the repository:")
layout.addWidget(text_label)
link_label = QLabel('<a href="https://dev.zymsite.ink/Academic/PEHelper">https://dev.zymsite.ink/Academic/PEHelper</a>')
link_label.setOpenExternalLinks(True)
layout.addWidget(link_label)
self.setLayout(layout)
from intertab import *
from rawtab import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.OpeningFile=False
self.setWindowTitle("PEHelper - Physics Experiment Helper")
self.setWindowIcon(QIcon(os.path.join(basedir,"static/icon.ico")))
self.setGeometry(100, 100, 800, 600)
menubar = self.menuBar()
file_menu = menubar.addMenu("File")
new_action = file_menu.addAction("New", self.new_file) # Add New action
new_action.setShortcut("Ctrl+N") # Set shortcut for New
open_action = file_menu.addAction("Open", self.open_file)
open_action.setShortcut("Ctrl+O")
save_action = file_menu.addAction("Save", self.save_file)
save_action.setShortcut("Ctrl+S")
save_as_action = file_menu.addAction("Save As", self.save_as_file)
save_as_action.setShortcut("Ctrl+Shift+S")
about_action = menubar.addAction("About", self.show_about_dialog)
self.data = {
"independent_vars": {},
"intermediate_vars": [],
"dependent_var": {
"name": "",
"expr": ""
},
"confidence": "0.95",
"accuracy": "20"
}
self.data_backup=copy.deepcopy(self.data)
# self.data = data_demo
self.current_file_name=""
self.tab_widget = QTabWidget(self)
self.raw_variables_tab = QWidget()
self.intermediate_variables_tab = QWidget()
self.analysis_tab = QWidget()
self.setup_raw_variables_tab()
self.setup_intermediate_variables_tab()
self.setup_analysis_tab()
self.tab_widget.addTab(self.raw_variables_tab, "Raw Variables")
self.tab_widget.addTab(self.intermediate_variables_tab, "Intermediate Variables")
self.tab_widget.addTab(self.analysis_tab, "Analysis")
layout = QVBoxLayout()
layout.addWidget(self.tab_widget)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
self.update_window_title()
def new_file(self):
self.FetchInfoFromUI()
if self.data != self.data_backup:
# Prompt the user to save current work before creating a new file
reply = QMessageBox.question(
self, 'Save Work', 'Do you want to save your current work before creating a new file?',
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No | QMessageBox.StandardButton.Cancel
)
if reply == QMessageBox.StandardButton.Yes:
self.save_file()
elif reply == QMessageBox.StandardButton.Cancel:
return
# Reset data and UI to initial state
self.data = {
"independent_vars": {},
"intermediate_vars": [],
"dependent_var": {
"name": "",
"expr": ""
},
"confidence": "0.95",
"accuracy": "20"
}
self.data_backup = copy.deepcopy(self.data)
self.current_file_name = ""
# Update UI with the new data
self.update_ui_with_data(self.data)
self.update_window_title()
def setup_raw_variables_tab(self):
self.raw_variables_tab = RawVariablesTab(self, self.data)
def setup_intermediate_variables_tab(self):
self.intermediate_variables_tab = IntermediateVariablesTab(self, self.data)
def setup_analysis_tab(self):
layout = QVBoxLayout()
label_name = QLabel("Dependent Variable's Name:")
self.input_name = QLineEdit()
# Track changes in the input field
self.input_name.textChanged.connect(self.update_window_title)
layout.addWidget(label_name)
layout.addWidget(self.input_name)
label_expr = QLabel("Expression:")
self.input_expr = QLineEdit()
# Track changes in the input field
self.input_expr.textChanged.connect(self.update_window_title)
layout.addWidget(label_expr)
layout.addWidget(self.input_expr)
label_confidence = QLabel("Confidence Level:")
self.input_confidence = QLineEdit("0.95") # Set default value
# Track changes in the input field
self.input_confidence.textChanged.connect(self.update_window_title)
layout.addWidget(label_confidence)
layout.addWidget(self.input_confidence)
label_accuracy = QLabel("Accuracy:")
self.input_accuracy = QLineEdit("20") # Set default value
# Track changes in the input field
self.input_accuracy.textChanged.connect(self.update_window_title)
layout.addWidget(label_accuracy)
layout.addWidget(self.input_accuracy)
self.analysis_result_area = QTextEdit()
self.analysis_result_area.setReadOnly(True)
layout.addWidget(self.analysis_result_area)
start_analysis_button = QPushButton("Start Analysis")
start_analysis_button.clicked.connect(self.start_analysis)
layout.addWidget(start_analysis_button)
self.analysis_tab.setLayout(layout)
def FetchInfoFromUI(self):
# Retrieve user inputs in tab 3
name = self.input_name.text()
expr = self.input_expr.text()
confidence = self.input_confidence.text()
accuracy = self.input_accuracy.text()
self.data["dependent_var"] = {"name": name, "expr": expr}
self.data["confidence"] = confidence
self.data["accuracy"] = accuracy
# Retrieve user inputs in tab 2
self.intermediate_variables_tab.update_data_from_list()
# data in tab 1 is updated automatically
def update_ui_with_data(self, new_data):
# Update UI with the new data
self.data = new_data
self.input_name.setText(self.data["dependent_var"].get("name", ""))
self.input_expr.setText(self.data["dependent_var"].get("expr", ""))
self.input_confidence.setText(str(self.data.get("confidence", 0.95)))
self.input_accuracy.setText(str(self.data.get("accuracy", 20)))
# Update intermediate variables tab
self.intermediate_variables_tab.setup_list(self.data)
# clear the analysis result area
self.analysis_result_area.clear()
# Update raw variables tab
self.raw_variables_tab.flush_list(self.data)
def update_window_title(self):
print("updating window title")
if self.OpeningFile:
return
self.FetchInfoFromUI()
changed=(self.data != self.data_backup)
if changed:
flag="*"
else:
flag=""
if self.current_file_name:
self.setWindowTitle(f"PEHelper - Physics Experiment Helper - {flag}{self.current_file_name}")
else:
self.setWindowTitle(f"PEHelper - Physics Experiment Helper - {flag}[New Project]")
def start_analysis(self):
self.FetchInfoFromUI()
name=self.data['dependent_var']['name']
try:
# Perform analysis using TrackUncertainty
result = TrackUncertainty(self.data)
# Display the result in the text area
self.analysis_result_area.setPlainText(f"Analysis Result for {name}:\n\n"
f"Value: {result[name]['value']}\n"
f"Uncertainty: {result[name]['uncertainty']}\n")
# Display origin independent variables' information
self.analysis_result_area.append("\nOrigin Independent Variables:")
for var_name, var_data in result.items():
if var_name != name and var_name in self.data["independent_vars"]:
self.analysis_result_area.append(f"{var_name}: "
f"Value: {var_data['value']}, "
f"Uncertainty: {var_data['uncertainty']}, "
f"Derivative: {var_data['derivative']}")
except Exception as e:
self.analysis_result_area.setPlainText(f"An error occurred during analysis:\n\n{e}")
def open_file(self):
self.FetchInfoFromUI()
if self.data != self.data_backup:
# Prompt the user to save current work before opening a new file
reply = QMessageBox.question(self, 'Save Work', 'Do you want to save your current work before opening a new file?',
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No | QMessageBox.StandardButton.Cancel)
if reply == QMessageBox.StandardButton.Yes:
self.save_file()
elif reply == QMessageBox.StandardButton.Cancel:
return
file_dialog = QFileDialog()
file_path, _ = file_dialog.getOpenFileName(self, "Open File", "", "Pickle Files (*.pe)")
if file_path:
self.current_file_name = file_path
with open(file_path, 'rb') as file:
self.OpeningFile=True
new_data = pickle.load(file)
# Update the UI with the new data
self.update_ui_with_data(new_data)
self.data_backup=copy.deepcopy(self.data)
self.OpeningFile=False
self.update_window_title()
def save_file(self):
file_dialog = QFileDialog()
if self.current_file_name == "":
file_path, _ = file_dialog.getSaveFileName(self, "Save File", "", "Pickle Files (*.pe)")
else:
file_path=self.current_file_name
if file_path:
self.current_file_name=file_path
with open(file_path, 'wb') as file:
print("data stored is",self.data)
pickle.dump(self.data, file)
self.data_backup=copy.deepcopy(self.data)
self.update_window_title()
def save_as_file(self):
file_dialog = QFileDialog()
file_path, _ = file_dialog.getSaveFileName(self, "Save As", "", "Pickle Files (*.pe)")
if file_path:
with open(file_path, 'wb') as file:
self.current_file_name=file_path
pickle.dump(self.data, file)
self.data_backup=copy.deepcopy(self.data)
self.update_window_title()
def show_about_dialog(self):
about_dialog = AboutDialog()
about_dialog.exec()
def closeEvent(self, event):
self.FetchInfoFromUI()
if self.data != self.data_backup:
reply = QMessageBox.question(
self, 'Save Work', 'Do you want to save your current work before closing?',
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No | QMessageBox.StandardButton.Cancel
)
if reply == QMessageBox.StandardButton.Yes:
self.save_file()
elif reply == QMessageBox.StandardButton.Cancel:
event.ignore()
return
# 调用父类的 closeEvent 方法
super().closeEvent(event)
def main():
app = QApplication(sys.argv)
try:
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())
except Exception as e:
print(e)
sys.exit(1)
if __name__ == "__main__":
main()