synchronous fit boundaries in real and imag plot

This commit is contained in:
Markus Rosenstihl 2014-04-14 14:36:38 +02:00
parent f70538567b
commit 6dd559968d

161
QDS.py
View File

@ -33,13 +33,15 @@ from ContainerWidgets import ParameterWidget, YaffWidget
USE_CROSSH=False USE_CROSSH=False
class AppWindow(QMainWindow): class AppWindow(QMainWindow):
def __init__(self, files=[], parent=None): def __init__(self, files=[], parent=None):
super(AppWindow, self).__init__(parent) super(AppWindow, self).__init__(parent)
self.ui = QDSMain.Ui_MainWindow() self.ui = QDSMain.Ui_MainWindow()
self.ui.setupUi(self) self.ui.setupUi(self)
self._file_paths = QStringList(files) self._file_paths = QStringList(files)
self._last_written_header = None self._last_written_header = None
actions = { actions = {
self.ui.actionAdd_Peak:self.addPeak, self.ui.actionAdd_Peak:self.addPeak,
self.ui.actionAdd_Cond:self.addCond, self.ui.actionAdd_Cond:self.addCond,
@ -51,6 +53,8 @@ class AppWindow(QMainWindow):
self.myActionGroup = QActionGroup(self) self.myActionGroup = QActionGroup(self)
for a in actions.keys(): self.myActionGroup.addAction(a) for a in actions.keys(): self.myActionGroup.addAction(a)
self._init_menu()
self.function_registry = FunctionRegister() self.function_registry = FunctionRegister()
self.peakId = 0 self.peakId = 0
@ -58,75 +62,11 @@ class AppWindow(QMainWindow):
self.parameterWidget = ParameterWidget() self.parameterWidget = ParameterWidget()
self.ui.dockWidget_3.setWidget(self.parameterWidget) self.ui.dockWidget_3.setWidget(self.parameterWidget)
## menubar
fileMenu = self.menuBar().addMenu("File")
openFile = QAction("&Open", self)
openFile.setShortcut(QKeySequence.Open)
openFile.triggered.connect(self.getFileNames)
fileMenu.addAction(openFile)
nextFile = QAction("Next", self)
nextFile.setShortcut(QKeySequence("Ctrl+k"))
nextFile.triggered.connect(self.nextFile)
fileMenu.addAction(nextFile)
previousFile = QAction("Previous", self)
previousFile.setShortcut(QKeySequence("Ctrl+j"))
previousFile.triggered.connect(self.previousFile)
fileMenu.addAction(previousFile)
saveFile = QAction("&Save Fit Result", self)
saveFile.setShortcut(QKeySequence.Save)
saveFile.triggered.connect(self.saveFitResult)
fileMenu.addAction(saveFile)
# fitting methods
fitMenu = self.menuBar().addMenu("Standard Fits")
# lm
fit_lmAction = QAction("&Levenberg-Marquardt", self)
fit_lmAction.setShortcut(QKeySequence("Ctrl+F"))
fitMenu.addAction(fit_lmAction)
# lbfgsb
fit_lbfgsbAction = QAction("&L-BFGS-B", self)
fitMenu.addAction(fit_lbfgsbAction)
# Simulated Annealing
fit_annealAction = QAction("&Simulated Annealing", self)
fitMenu.addAction(fit_annealAction)
# YAFF
yaffMenu = self.menuBar().addMenu("YAFF")
start_yaff = QAction("&Startparam.", self)
yaffMenu.addAction(start_yaff)
fit_yaff = QAction("&Fit", self)
yaffMenu.addAction(fit_yaff)
self._fit_thread = QThread()
# self._fit_method = FitRoutine()
# self._fit_method.moveToThread(self._fit_thread)
# self._fit_method.finished.connect(self._fit_thread.quit)
# self._fit_queue = QEventLoop()
# self._fit_timer = QTimer()
# self._fit_timer.setSingleShot(True)
# self._fit_timer.timeout.connect(self._fit_queue.quit)
# self._fit_method.finished.connect(self._fit_queue.quit)
self.signalMapper = QSignalMapper(self)
for i, fit_action in enumerate([fit_lmAction, fit_lbfgsbAction, fit_annealAction
]):
self.signalMapper.setMapping(fit_action, i)
fit_action.triggered.connect(self.signalMapper.map)
self.signalMapper.mapped.connect(self.fitData_start)
# save fitted values
#self.ui.actionSave_FitResult.triggered.connect(self.saveFitResult)# replaced by menu
self.data = Data() self.data = Data()
self.fit_boundary = pg.LinearRegionItem(brush=QColor(0,127,254,15)) self.fit_boundary_imag = pg.LinearRegionItem(brush=QColor(0,127,254,15))
self.fit_boundary_real = pg.LinearRegionItem(brush=QColor(0,127,254,15))
self.ui.pgPlotWidget_imag.addItem(self.data.data_curve_imag) self.ui.pgPlotWidget_imag.addItem(self.data.data_curve_imag)
self.ui.pgPlotWidget_real.addItem(self.data.data_curve_real) self.ui.pgPlotWidget_real.addItem(self.data.data_curve_real)
@ -143,7 +83,11 @@ class AppWindow(QMainWindow):
self.ui.pgPlotWidget_imag.addItem(self.horizontalLine) self.ui.pgPlotWidget_imag.addItem(self.horizontalLine)
self.ui.pgPlotWidget_imag.addItem(self.verticalLine) self.ui.pgPlotWidget_imag.addItem(self.verticalLine)
self.ui.pgPlotWidget_imag.addItem(self.fit_boundary) self.ui.pgPlotWidget_imag.addItem(self.fit_boundary_imag)
self.ui.pgPlotWidget_real.addItem(self.fit_boundary_real)
self.fit_boundary_imag.sigRegionChanged.connect(self._update_fit_boundary_real)
self.fit_boundary_real.sigRegionChanged.connect(self._update_fit_boundary_imag)
for pltwidgt in (self.ui.pgPlotWidget_real, self.ui.pgPlotWidget_imag): for pltwidgt in (self.ui.pgPlotWidget_real, self.ui.pgPlotWidget_imag):
pltwidgt.setLogMode(x=True, y=True) pltwidgt.setLogMode(x=True, y=True)
@ -167,32 +111,78 @@ class AppWindow(QMainWindow):
self.openFile(files[0]) self.openFile(files[0])
self._current_file_index = 0 self._current_file_index = 0
self._fit_thread = QThread()
self._fit_method = FitRoutine() self._fit_method = FitRoutine()
self._fit_method.moveToThread(self._fit_thread) self._fit_method.moveToThread(self._fit_thread)
self._fit_method.finished_fit.connect(self.fitData_update) self._fit_method.finished_fit.connect(self.fitData_update)
self._fit_method.data_ready.connect(self.updateIntermediatePlot) self._fit_method.data_ready.connect(self.updateIntermediatePlot)
self._fit_thread.started.connect(self._fit_method.fit) self._fit_thread.started.connect(self._fit_method.fit)
def _init_menu(self):
fileMenu = self.menuBar().addMenu("File")
openFile = QAction("&Open", self)
openFile.setShortcut(QKeySequence.Open)
openFile.triggered.connect(self.getFileNames)
fileMenu.addAction(openFile)
nextFile = QAction("Next", self)
nextFile.setShortcut(QKeySequence("Ctrl+k"))
nextFile.triggered.connect(self.nextFile)
fileMenu.addAction(nextFile)
previousFile = QAction("Previous", self)
previousFile.setShortcut(QKeySequence("Ctrl+j"))
previousFile.triggered.connect(self.previousFile)
fileMenu.addAction(previousFile)
saveFile = QAction("&Save Fit Result", self)
saveFile.setShortcut(QKeySequence.Save)
saveFile.triggered.connect(self.saveFitResult)
fileMenu.addAction(saveFile)
# fitting methods
fitMenu = self.menuBar().addMenu("Standard Fits")
# lm
fit_lmAction = QAction("&Levenberg-Marquardt", self)
fit_lmAction.setShortcut(QKeySequence("Ctrl+F"))
fitMenu.addAction(fit_lmAction)
# lbfgsb
fit_lbfgsbAction = QAction("&L-BFGS-B", self)
fitMenu.addAction(fit_lbfgsbAction)
# Simulated Annealing
fit_annealAction = QAction("&Simulated Annealing", self)
fitMenu.addAction(fit_annealAction)
# YAFF
yaffMenu = self.menuBar().addMenu("YAFF")
start_yaff = QAction("&Startparam.", self)
yaffMenu.addAction(start_yaff)
fit_yaff = QAction("&Fit", self)
yaffMenu.addAction(fit_yaff)
self.signalMapper = QSignalMapper(self)
for i, fit_action in enumerate([fit_lmAction, fit_lbfgsbAction, fit_annealAction
]):
self.signalMapper.setMapping(fit_action, i)
fit_action.triggered.connect(self.signalMapper.map)
self.signalMapper.mapped.connect(self.fitData_start)
def updateCrosshair(self,evt): def updateCrosshair(self,evt):
vb_real = self.ui.pgPlotWidget_real.getPlotItem().vb vb_real = self.ui.pgPlotWidget_real.getPlotItem().vb
vb_imag = self.ui.pgPlotWidget_imag.getPlotItem().vb vb_imag = self.ui.pgPlotWidget_imag.getPlotItem().vb
if self.ui.pgPlotWidget_imag.underMouse(): if self.ui.pgPlotWidget_imag.underMouse():
pos = vb_imag.mapSceneToView(evt) pos = vb_imag.mapSceneToView(evt)
elif self.ui.pgPlotWidget_real.underMouse(): elif self.ui.pgPlotWidget_real.underMouse():
pos = vb_real.mapSceneToView(evt) pos = vb_real.mapSceneToView(evt)
else: else:
pos = QPointF(0.0, 0.0) pos = QPointF(0.0, 0.0)
self.last_pos = pos self.last_pos = pos
if USE_CROSSH: if USE_CROSSH:
self.horizontalLine.setBounds([self.data.frequency.min(), self.data.frequency.max()]) self.horizontalLine.setBounds([self.data.frequency.min(), self.data.frequency.max()])
self.horizontalLine.setPos(pos.y()) self.horizontalLine.setPos(pos.y())
self.verticalLine.setPos(pos.x()) self.verticalLine.setPos(pos.x())
#self.poslabel.setText("<span style='font-size: 12pt'>f=%0.1f Hz <span style='color: red'>e=%0.1f</span>"\
# % (pos.x(),pos.y()))
def mousePress(self, evt): def mousePress(self, evt):
data_pos = self.last_pos data_pos = self.last_pos
@ -417,19 +407,12 @@ class AppWindow(QMainWindow):
fcn.clearData() fcn.clearData()
_freq, _fit = self.data.get_data() _freq, _fit = self.data.get_data()
# Prepare
if not self._fit_thread.isRunning(): if not self._fit_thread.isRunning():
# self._fit_method = FitRoutine()
# self._fit_method.moveToThread(self._fit_thread)
# self._fit_method.finished_fit.connect(self.fitData_update)
self._fit_method.fit_odr_cmplx(_freq, _fit, p0, fixed_params, funcs) self._fit_method.fit_odr_cmplx(_freq, _fit, p0, fixed_params, funcs)
# self._fit_method.data_ready.connect(self.updateIntermediatePlot)
# self._fit_thread.started.connect(self._fit_method.fit)
# self._fit_thread.finished.connect(self._fit_method.deleteLater)
self._fit_thread.start() self._fit_thread.start()
self.ui.statusbar.showMessage("Fitting ...") self.ui.statusbar.showMessage("Fitting ...")
else: else:
print "fit is still running" self.ui.statusbar.showMessage("Still fitting ...")
def fitData_update(self): def fitData_update(self):
self._fit_thread.quit() self._fit_thread.quit()
@ -449,13 +432,12 @@ class AppWindow(QMainWindow):
fcn.setParameter(beta, sd_beta) fcn.setParameter(beta, sd_beta)
def getFileNames(self): def getFileNames(self):
# TODO: multiple files, advance by button tmp = QFileDialog.getOpenFileNames(self, "Open file", "", '*.dat *.TXT')
self._file_paths = QFileDialog.getOpenFileNames(self, "Open file", "", '*.dat') if len(tmp) != 0:
self._file_paths = tmp
self._current_file_index = 0 self._current_file_index = 0
path = unicode(self._file_paths[self._current_file_index]) path = unicode(self._file_paths[self._current_file_index])
self.openFile(path) self.openFile(path)
#path = unicode(QFileDialog.getOpenFileName(self, "Open file"))
#path = "MCM42PG0_199.96K.dat"
def nextFile(self): def nextFile(self):
if len(self._file_paths) > self._current_file_index+1: # wrap around if len(self._file_paths) > self._current_file_index+1: # wrap around
@ -499,7 +481,7 @@ class AppWindow(QMainWindow):
_die_loss = data[mask, 2] _die_loss = data[mask, 2]
self.data.set_data(_freq, _die_stor, _die_loss) self.data.set_data(_freq, _die_stor, _die_loss)
self.data.meta["T"] = Temp self.data.meta["T"] = Temp
self.fit_boundary.setRegion([np.log10(_freq.min()), np.log10(_freq.max())]) self.fit_boundary_imag.setRegion([np.log10(_freq.min()), np.log10(_freq.max())])
self.ui.pgPlotWidget_imag.enableAutoRange() self.ui.pgPlotWidget_imag.enableAutoRange()
self.ui.pgPlotWidget_real.enableAutoRange() self.ui.pgPlotWidget_real.enableAutoRange()
self.updatePlot() self.updatePlot()
@ -508,7 +490,7 @@ class AppWindow(QMainWindow):
def updatePlot(self): def updatePlot(self):
log10fmin, log10fmax = self.fit_boundary.getRegion() log10fmin, log10fmax = self.fit_boundary_imag.getRegion()
self.data.set_fit_xlimits(10**log10fmin, 10**log10fmax) self.data.set_fit_xlimits(10**log10fmin, 10**log10fmax)
p0,funcs = [],[] p0,funcs = [],[]
@ -534,6 +516,11 @@ class AppWindow(QMainWindow):
self.data.fitted_curve_real.setData(freq, intermediate_data[0]) self.data.fitted_curve_real.setData(freq, intermediate_data[0])
self.data.fitted_curve_imag.setData(freq, intermediate_data[1]) self.data.fitted_curve_imag.setData(freq, intermediate_data[1])
def _update_fit_boundary_real(self):
self.fit_boundary_real.setRegion(self.fit_boundary_imag.getRegion())
def _update_fit_boundary_imag(self):
self.fit_boundary_imag.setRegion(self.fit_boundary_real.getRegion())
def sigint_handler(*args): def sigint_handler(*args):
"""Handler for the SIGINT signal (CTRL + C). """Handler for the SIGINT signal (CTRL + C).