individual fitfunctions saved upon saving

This commit is contained in:
Markus Rosenstihl 2014-04-11 18:20:16 +02:00
parent e32fa8f834
commit 4913924222
3 changed files with 76 additions and 17 deletions

58
QDS.py
View File

@ -32,11 +32,11 @@ from CustomWidgets import ParameterWidget, YaffWidget
USE_CROSSH=False USE_CROSSH=False
class AppWindow(QMainWindow): class AppWindow(QMainWindow):
def __init__(self, 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._last_written_header = None self._last_written_header = None
actions = { actions = {
@ -61,9 +61,16 @@ class AppWindow(QMainWindow):
fileMenu = self.menuBar().addMenu("File") fileMenu = self.menuBar().addMenu("File")
openFile = QAction("&Open", self) openFile = QAction("&Open", self)
openFile.setShortcut(QKeySequence.Open) openFile.setShortcut(QKeySequence.Open)
openFile.triggered.connect(self.openFile) openFile.triggered.connect(self.getFileNames)
fileMenu.addAction(openFile) fileMenu.addAction(openFile)
nextFile = QAction("&Next", self)
nextFile.setShortcut(QKeySequence.FindNext)
nextFile.triggered.connect(self.nextFile)
fileMenu.addAction(nextFile)
saveFile = QAction("&Save Fit Result", self) saveFile = QAction("&Save Fit Result", self)
saveFile.setShortcut(QKeySequence.Save) saveFile.setShortcut(QKeySequence.Save)
saveFile.triggered.connect(self.saveFitResult) saveFile.triggered.connect(self.saveFitResult)
@ -149,6 +156,10 @@ class AppWindow(QMainWindow):
sc_imag.sigMouseClicked.connect(self.mousePress) sc_imag.sigMouseClicked.connect(self.mousePress)
sc_imag.sigMouseMoved.connect(self.updateCrosshair) sc_imag.sigMouseMoved.connect(self.updateCrosshair)
# process cmd line args
if files is not None:
self.openFile(files[0])
self._current_file_index = 0
def updateCrosshair(self,evt): def updateCrosshair(self,evt):
@ -238,10 +249,18 @@ class AppWindow(QMainWindow):
# prepare header # prepare header
header="# T " header="# T "
pars = [] pars = []
for fcn in self.function_registry.get_registered_functions(): bname = os.path.splitext(self.filepath)[0]
for name in fcn.widget.names: # variable names for i_fcn, fcn in enumerate(self.function_registry.get_registered_functions()):
for name in fcn.widget.names: # get variable names
header += "{n:11}{n_sd:11}".format(n=name, n_sd=name+"_sd") header += "{n:11}{n_sd:11}".format(n=name, n_sd=name+"_sd")
# write for each function extra file
name_fcn = "%s_%i.fit"%(bname,i_fcn)
f_fcn = open(name_fcn, 'w')
f_fcn.write("# %s\n"%(fcn.id_string))
f_fcn.flush()
np.savetxt(f_fcn, fcn.resampleData(self.data.frequency))
f_fcn.close()
for i,par in enumerate(fcn._beta): # params # TODO: ughh for i,par in enumerate(fcn._beta): # params # TODO: ughh
if fcn._selector_mask is not None: if fcn._selector_mask is not None:
if fcn._selector_mask[i]: if fcn._selector_mask[i]:
@ -283,7 +302,7 @@ class AppWindow(QMainWindow):
for fcn in self.function_registry.get_registered_functions(): for fcn in self.function_registry.get_registered_functions():
f,eps = fcn.get_data() f,eps = fcn.get_data()
label = fcn.id_string label = fcn.id_label
color = hex2color(str(fcn.color.name())) color = hex2color(str(fcn.color.name()))
pyplot.loglog(f,eps[1], ls=":", color=color, lw=1, dashes=(1,1), label=label) pyplot.loglog(f,eps[1], ls=":", color=color, lw=1, dashes=(1,1), label=label)
@ -369,7 +388,7 @@ class AppWindow(QMainWindow):
def addPeak(self, pos): def addPeak(self, pos):
id_list = [ key.id_num for key in id_list = [ key.id_num for key in
self.function_registry.get_registered_functions().keys() self.function_registry.get_registered_functions().keys()
if key.id_string == 'hn'] if key.id_label == 'hn']
self.peakId = 1 self.peakId = 1
while self.peakId in id_list: while self.peakId in id_list:
@ -465,9 +484,27 @@ class AppWindow(QMainWindow):
i += num_p i += num_p
#self.updatePlot() #self.updatePlot()
def openFile(self): def getFileNames(self):
# TODO: multiple files, advance by button
self._file_paths = QFileDialog.getOpenFileNames(self, "Open file", "", '*.dat')
print "here", len(self._file_paths)
self._current_file_index = 0
path = unicode(self._file_paths[self._current_file_index])
self.openFile(path)
#path = unicode(QFileDialog.getOpenFileName(self, "Open file")) #path = unicode(QFileDialog.getOpenFileName(self, "Open file"))
path = "MCM42PG0_199.96K.dat" #path = "MCM42PG0_199.96K.dat"
def nextFile(self):
if len(self._file_paths) > self._current_file_index+1: # wrap around
self._current_file_index += 1
else:
self._current_file_index = 0
path = unicode(self._file_paths[self._current_file_index])
self.openFile(path)
def openFile(self,path):
print "opening: %s"%path
self.filepath=path self.filepath=path
# TODO analyize file (LF,MF, HF) and act accordingly # TODO analyize file (LF,MF, HF) and act accordingly
data = np.loadtxt(path, skiprows=4) data = np.loadtxt(path, skiprows=4)
@ -539,11 +576,12 @@ def sigint_handler(*args):
if __name__ == '__main__': if __name__ == '__main__':
signal.signal(signal.SIGINT, sigint_handler) signal.signal(signal.SIGINT, sigint_handler)
files = sys.argv[1:]
app = QApplication(sys.argv) app = QApplication(sys.argv)
timer = QTimer() timer = QTimer()
timer.start(1000) # Check every second for Strg-c on Cmd line timer.start(1000) # Check every second for Strg-c on Cmd line
timer.timeout.connect(lambda: None) timer.timeout.connect(lambda: None)
main = AppWindow() main = AppWindow(files=files)
main.showMaximized() main.showMaximized()
main.raise_() main.raise_()
sys.exit(app.exec_()) sys.exit(app.exec_())

33
data.py
View File

@ -93,6 +93,7 @@ class BaseObject(QObject):
self.functions = Functions() self.functions = Functions()
self._color = QColor("white") self._color = QColor("white")
self._id = None self._id = None
self._id_string = None
self._widget = None self._widget = None
self._frequency = np.logspace(np.log10(limits[0]), np.log10(limits[1]), 256) self._frequency = np.logspace(np.log10(limits[0]), np.log10(limits[1]), 256)
self._data = None self._data = None
@ -112,10 +113,20 @@ class BaseObject(QObject):
@property @property
def id_string(self): def id_string(self):
return self._id return self._id_string
@id_string.setter @id_string.setter
def id_string(self, id): def id_string(self, id):
#self._func = self.functions.get_function(id)
self._id_string = id
@property
def id_label(self):
return self._id
@id_label.setter
def id_label(self, id):
#self._func = self.functions.get_function(id) #self._func = self.functions.get_function(id)
self._func = self.function self._func = self.function
self._id = id self._id = id
@ -169,6 +180,11 @@ class BaseObject(QObject):
self.data_curve_imag.setData(x=self._frequency, y=self._data[1]) self.data_curve_imag.setData(x=self._frequency, y=self._data[1])
self.changedData.emit() self.changedData.emit()
def resampleData(self, x):
data = self._func(self.getParameter(), x)
return np.array([x,data[0],data[1]]).T
def clearData(self): def clearData(self):
self.data_curve_real.setData(x=[np.nan], y=[np.nan]) self.data_curve_real.setData(x=[np.nan], y=[np.nan])
self.data_curve_imag.setData(x=[np.nan], y=[np.nan]) self.data_curve_imag.setData(x=[np.nan], y=[np.nan])
@ -181,7 +197,8 @@ class Conductivity(BaseObject):
super(Conductivity, self).__init__(plt_real=plt_real, plt_imag=plt_imag, limits=limits) super(Conductivity, self).__init__(plt_real=plt_real, plt_imag=plt_imag, limits=limits)
self.widget = CustomWidgets.ConductivityWidget() self.widget = CustomWidgets.ConductivityWidget()
self.color = QColor("blue") self.color = QColor("blue")
self.id_string = "Cond." self.id_label = "Cond."
self.id_string = "cond"
self.param_number = 3 self.param_number = 3
@ -198,7 +215,8 @@ class PowerComplex(BaseObject):
super(PowerComplex, self).__init__(plt_real=plt_real, plt_imag=plt_imag, limits=limits) super(PowerComplex, self).__init__(plt_real=plt_real, plt_imag=plt_imag, limits=limits)
self.widget = CustomWidgets.PowerLawWidget() self.widget = CustomWidgets.PowerLawWidget()
self.color = QColor("#ff44c4") self.color = QColor("#ff44c4")
self.id_string = 'Power Law' self.id_label = 'Power Law'
self.id_string = "pwr"
self.param_number = 2 self.param_number = 2
def function( self, p, x ): def function( self, p, x ):
@ -214,7 +232,8 @@ class Static(BaseObject):
super(Static, self).__init__(plt_real=plt_real, plt_imag=plt_imag, limits=limits) super(Static, self).__init__(plt_real=plt_real, plt_imag=plt_imag, limits=limits)
self.widget = CustomWidgets.StaticWidget() self.widget = CustomWidgets.StaticWidget()
self.color = QColor('#FF0F13') self.color = QColor('#FF0F13')
self.id_string = u'ε(∞)' self.id_label = u'ε(∞)'
self.id_string = "eps_infty"
self.param_number = 1 self.param_number = 1
def function( self, p, x ): def function( self, p, x ):
@ -232,6 +251,7 @@ class Peak(BaseObject):
self.color = id_to_color(id_num) self.color = id_to_color(id_num)
self.widget.setColor(self.color) self.widget.setColor(self.color)
self.id_num = id_num self.id_num = id_num
self.id_label = "Hav-Neg"
self.id_string = "hn" self.id_string = "hn"
self.param_number = 4 self.param_number = 4
@ -249,7 +269,8 @@ class YAFF(BaseObject):
self.widget.on_model_changed.connect(self.change_model) self.widget.on_model_changed.connect(self.change_model)
self.color = QColor(32, 120, 29, int(255*0.82)) self.color = QColor(32, 120, 29, int(255*0.82))
self._libyaff = libyaff.Yaff(self.widget.getYaffType()) self._libyaff = libyaff.Yaff(self.widget.getYaffType())
self.id_string = self._libyaff.label self.id_label = self._libyaff.label
self.id_string = "yaff"
self._param_number = self._libyaff.params self._param_number = self._libyaff.params
self._selector_mask = self.widget.selector_mask self._selector_mask = self.widget.selector_mask
@ -264,7 +285,7 @@ class YAFF(BaseObject):
def change_model(self): def change_model(self):
self._libyaff = libyaff.Yaff(self.widget.getYaffType()) self._libyaff = libyaff.Yaff(self.widget.getYaffType())
self._selector_mask = self.widget.selector_mask self._selector_mask = self.widget.selector_mask
self.id_string = self._libyaff.label self.id_label = self._libyaff.label
self.param_number = self._libyaff.params self.param_number = self._libyaff.params
self.updateData() self.updateData()

View File

@ -301,7 +301,7 @@ class FunctionRegister:
def register_function(self, obj): def register_function(self, obj):
#print "FR: Registering:",obj #print "FR: Registering:",obj
id_string = obj.id_string id_string = obj.id_label
if self.registry.has_key(obj): if self.registry.has_key(obj):
raise AssertionError,"The object is already registered! This should NOT happen" raise AssertionError,"The object is already registered! This should NOT happen"
self.registry[obj]=id_string self.registry[obj]=id_string