1
0
Fork 0
mirror of https://github.com/Athemis/PyDSF.git synced 2025-04-05 14:46:03 +00:00

Coding style; General clean-up; Move calculations to dedicated thread

This commit is contained in:
Alexander Minges 2015-07-07 15:09:49 +02:00
parent e1f34c094a
commit 9b1c32effe

View file

@ -3,22 +3,98 @@
""" """
Module implementing MainWindow. Module implementing MainWindow.
""" """
from PyQt5.QtCore import (pyqtSlot,
VERSION = "1.0" QObject,
pyqtSignal,
from PyQt5.QtCore import pyqtSlot, QObject, pyqtSignal QThreadPool,
from PyQt5.QtWidgets import * QRunnable,)
from PyQt5.QtWidgets import (QMainWindow,
QProgressBar,
QDialogButtonBox,
QFileDialog,
QMessageBox,
QApplication)
from .Ui_mainwindow import Ui_MainWindow from .Ui_mainwindow import Ui_MainWindow
from .mplwidget import MplWidget from .mplwidget import MplWidget
from pydsf import * from pydsf import Experiment, PlotResults
VERSION = "1.0"
class WorkerSignals(QObject):
class ProcessData(QObject):
finished = pyqtSignal() finished = pyqtSignal()
def process(self):
raise NotImplementedError
class Worker(QRunnable):
finished = pyqtSignal(int)
def __init__(self, owner):
super(Worker, self).__init__()
self.exp = None
self.owner = owner
self.signals = WorkerSignals()
def run(self):
c_lower = None
c_upper = None
cbar_range = None
signal_threshold = None
instrument_type = self.owner.comboBox_instrument.currentText()
if self.owner.groupBox_cutoff.isChecked():
c_lower = self.owner.doubleSpinBox_lower.value()
c_upper = self.owner.doubleSpinBox_upper.value()
if self.owner.groupBox_cbar.isChecked():
cbar_range = (self.owner.doubleSpinBox_cbar_start, self.owner.doubleSpinBox_cbar_end)
if self.owner.groupBox_signal_threshold.isChecked():
signal_threshold = self.owner.spinBox_signal_threshold.value()
items = (self.owner.listWidget_data.item(i) for i in range(self.owner.listWidget_data.count()))
files = []
for item in items:
files.append(item.text())
self.exp = Experiment(exp_type=instrument_type, files=files, t1=self.owner.doubleSpinBox_tmin.value(), t2=self.owner.doubleSpinBox_tmax.value(),
dt=self.owner.doubleSpinBox_dt.value(), cols=12, rows=8, cutoff_low=c_lower, cutoff_high=c_upper,
signal_threshold=signal_threshold, color_range=cbar_range)
print("Start processing of data... ")
self.exp.analyze()
print("done!")
self.signals.finished.emit()
class TaskSignals(QObject):
finished = pyqtSignal(list)
class Tasks(QObject):
def __init__(self):
super(Tasks, self).__init__()
self.pool = QThreadPool()
self.pool.setMaxThreadCount(1)
self.tasks = []
self.data = []
self.signals = TaskSignals()
def add_task(self, task):
self.tasks.append(task)
def get_data(self):
self.pool.waitForDone()
return self.data
def start(self):
for task in self.tasks:
self.pool.start(task)
self.pool.waitForDone()
for task in self.tasks:
self.data.append(task.exp)
print(self.data)
self.signals.finished.emit(self.data)
class MainWindow(QMainWindow, Ui_MainWindow): class MainWindow(QMainWindow, Ui_MainWindow):
@ -43,8 +119,6 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.buttonBox_process.addButton("&Start Processing", QDialogButtonBox.AcceptRole) self.buttonBox_process.addButton("&Start Processing", QDialogButtonBox.AcceptRole)
@pyqtSlot("QAbstractButton*") @pyqtSlot("QAbstractButton*")
def on_buttonBox_open_reset_clicked(self, button): def on_buttonBox_open_reset_clicked(self, button):
""" """
@ -62,7 +136,6 @@ class MainWindow(QMainWindow, Ui_MainWindow):
# self.radioButton_rep_rows.setEnabled(False) # self.radioButton_rep_rows.setEnabled(False)
# self.radioButton_rep_columns.setEnabled(False) # self.radioButton_rep_columns.setEnabled(False)
@pyqtSlot("QString") @pyqtSlot("QString")
def on_comboBox_instrument_currentIndexChanged(self, p0): def on_comboBox_instrument_currentIndexChanged(self, p0):
""" """
@ -94,6 +167,10 @@ class MainWindow(QMainWindow, Ui_MainWindow):
tab = self.generate_plot_tab("tab_raw_{}".format(id)) tab = self.generate_plot_tab("tab_raw_{}".format(id))
self.tabWidget.addTab(tab, "Raw Data #{}".format(plate.id)) self.tabWidget.addTab(tab, "Raw Data #{}".format(plate.id))
plotter.plot_raw(plate, tab) plotter.plot_raw(plate, tab)
tab = self.generate_plot_tab("tab_derivative_{}".format(id))
self.tabWidget.addTab(tab, "Derivatives #{}".format(plate.id))
plotter.plot_derivative(plate, tab)
else: else:
tab = self.generate_plot_tab("tab_heatmap_{}".format(id)) tab = self.generate_plot_tab("tab_heatmap_{}".format(id))
self.tabWidget.addTab(tab, "Heatmap ({})".format(plate.id)) self.tabWidget.addTab(tab, "Heatmap ({})".format(plate.id))
@ -115,32 +192,17 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.progressBar.setEnabled(True) self.progressBar.setEnabled(True)
self.statusBar.showMessage("Processing...") self.statusBar.showMessage("Processing...")
c_lower = None self.tasks = Tasks()
c_upper = None self.tasks.signals.finished.connect(self.on_processing_finished)
cbar_range = None self.worker = Worker(self)
signal_threshold = None self.tasks.add_task(self.worker)
type = self.comboBox_instrument.currentText() self.tasks.start()
if self.groupBox_cutoff.isChecked():
c_lower = self.doubleSpinBox_lower.value()
c_upper = self.doubleSpinBox_upper.value()
if self.groupBox_cbar.isChecked():
cbar_range = (self.doubleSpinBox_cbar_start, self.doubleSpinBox_cbar_end)
if self.groupBox_signal_threshold.isChecked():
signal_threshold = self.spinBox_signal_threshold.value()
items = (self.listWidget_data.item(i) for i in range(self.listWidget_data.count())) @pyqtSlot()
def on_processing_finished(self):
# For now, only read the first entry
exp = self.tasks.data[0]
files = []
for item in items:
files.append(item.text())
exp = Experiment(type=type, files=files, t1=self.doubleSpinBox_tmin.value(), t2=self.doubleSpinBox_tmax.value(),
dt=self.doubleSpinBox_dt.value(), cols=12, rows=8, cutoff_low=c_lower, cutoff_high=c_upper,
signal_threshold=signal_threshold, color_range=cbar_range)
exp.analyze()
# plate = Plate(type=type, filename=self.lineEdit_data_file.text(), t1=self.doubleSpinBox_tmin.value(), t2=self.doubleSpinBox_tmax.value(), dt=self.doubleSpinBox_dt.value(), cols=12, rows=8, cutoff_low=c_lower, cutoff_high=c_upper, signal_threshold=signal_threshold, color_range=cbar_range)
# self.statusBar.addWidget(self.pb, 100)
# plate.analyze(gui=self)
save_data = QMessageBox.question(self, 'Save data', "Calculations are finished. Save results?", save_data = QMessageBox.question(self, 'Save data', "Calculations are finished. Save results?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if save_data == QMessageBox.Yes: if save_data == QMessageBox.Yes:
@ -154,10 +216,9 @@ class MainWindow(QMainWindow, Ui_MainWindow):
plate.write_raw_table('{}/plate_{}_01_raw_data.csv'.format(folder, str(plate.id))) plate.write_raw_table('{}/plate_{}_01_raw_data.csv'.format(folder, str(plate.id)))
if exp.avg_plate: if exp.avg_plate:
exp.avg_plate.write_avg_tm_table('{}/plate_{}_05_tm_avg.csv'.format(folder, str(exp.avg_plate.id))) exp.avg_plate.write_avg_tm_table('{}/plate_{}_05_tm_avg.csv'.format(folder, str(self.worker.exp.avg_plate.id)))
#plot(plate, self)
for i in range(len(exp.plates)): for i in range(len(self.worker.exp.plates)):
plate = exp.plates[i] plate = exp.plates[i]
self.generate_plate_tabs(plate) self.generate_plate_tabs(plate)
@ -167,37 +228,9 @@ class MainWindow(QMainWindow, Ui_MainWindow):
plate = exp.avg_plate plate = exp.avg_plate
self.generate_plate_tabs(plate) self.generate_plate_tabs(plate)
#for i in range(self.tabWidget.count()):
# for plate in exp.plates:
# if i == 0:
# plotter.plot_raw(plate, self.tabWidget.widget(i))
# self.tabWidget.setTabText(i, "Raw Data #{}".format(plate.id))
#elif i == 1:
# plotter.plot_derivative(plate, self.tabWidget.widget(i))
#elif i == 2:
# plotter.plot_tm_heatmap_single(plate, self.tabWidget.widget(i))
#elif exp.avg_plate and i == 3:
# plotter.plot_tm_heatmap_single(exp.avg_plate, self.tabWidget.widget(i))
# self.tabWidget.setTabEnabled(i, True)
#else:
# self.tabWidget.setTabEnabled(i, False)
#fig, ax = figures[0]
#self.tabWidget.widget(0).canvas.fig = fig
#self.tabWidget.widget(0).canvas.ax = ax
#self.tabWidget.widget(0).canvas.draw()
self.progressBar.setEnabled(False) self.progressBar.setEnabled(False)
self.statusBar.showMessage("Finished!") self.statusBar.showMessage("Finished!")
@pyqtSlot() @pyqtSlot()
def on_buttonBox_process_rejected(self): def on_buttonBox_process_rejected(self):
""" """
@ -235,4 +268,3 @@ class MainWindow(QMainWindow, Ui_MainWindow):
Slot documentation goes here. Slot documentation goes here.
""" """
QApplication.aboutQt() QApplication.aboutQt()