function registry with fixed container order

This commit is contained in:
Markus Rosenstihl 2014-09-25 17:14:50 +02:00
parent 582019d7cb
commit 1a1066a07e
4 changed files with 61 additions and 69 deletions

View File

@ -1,75 +1,78 @@
# -*- encoding: utf8 -*- # -*- encoding: utf8 -*-
from libmath import yafflib
from PyQt4.QtGui import QColor from PyQt4.QtGui import QColor
import numpy as np import numpy as np
from libmath import yafflib
from libmath.BDSlib import id_to_color from libmath.BDSlib import id_to_color
from container_base import BaseObject from container_base import BaseContainer
from gui import ContainerWidgets
import gui.ContainerWidgets
__author__ = 'markusro' __author__ = 'markusro'
class Conductivity(BaseObject): class Conductivity(BaseContainer):
def __init__( self, plt_imag=None, plt_real=None, limits=None ): def __init__( self, plt_imag=None, plt_real=None, limits=None ):
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 = ContainerWidgets.ConductivityWidget() self.widget = gui.ContainerWidgets.ConductivityWidget()
self.color = QColor("blue") self.color = QColor("blue")
self.id_label = "Cond." self.id_label = "Cond."
self.id_string = "cond" self.id_string = "cond"
self.param_number = 3 self.param_number = 3
def function(self, p, x ): def function( self, p, x ):
om = 2*np.pi*x om = 2*np.pi*x
sgma, isgma, n = p sgma, isgma, n = p
cond = sgma/(om**n) + isgma/(1j*om**n) # Jonscher (Universal Dielectric Response: e",e' prop sigma/omega**n cond = sgma/(om**n)+isgma/(1j*om**n) # Jonscher (Universal Dielectric Response: e",e' prop sigma/omega**n
cplx = np.array([cond.real, -cond.imag]) cplx = np.array([cond.real, -cond.imag])
return cplx return cplx
class PowerComplex(BaseObject): class PowerComplex(BaseContainer):
def __init__( self, plt_real=None, plt_imag=None, limits=None ): def __init__( self, plt_real=None, plt_imag=None, limits=None ):
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 = ContainerWidgets.PowerLawWidget() self.widget = gui.ContainerWidgets.PowerLawWidget()
self.color = QColor("#ff44c4") self.color = QColor("#ff44c4")
self.id_label = 'Power Law' self.id_label = 'Power Law'
self.id_string = "pwr" self.id_string = "pwr"
self.param_number = 2 self.param_number = 2
def function( self, p, x ): def function( self, p, x ):
BaseObject.function(self,p,x) BaseContainer.function(self, p, x)
om = 2*np.pi*x om = 2*np.pi*x
sgma,n = p sgma, n = p
power = sgma/(om*1j)**n power = sgma/(om*1j)**n
cplx = np.array([power.real, -power.imag]) cplx = np.array([power.real, -power.imag])
return cplx return cplx
class Static(BaseObject): class Static(BaseContainer):
def __init__( self, plt_real=None, plt_imag=None, limits=None ): def __init__( self, plt_real=None, plt_imag=None, limits=None ):
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 = ContainerWidgets.StaticWidget() self.widget = gui.ContainerWidgets.StaticWidget()
self.color = QColor('#FF0F13') self.color = QColor('#FF0F13')
self.id_label = u'ε(∞)' self.id_label = u'ε(∞)'
self.id_string = "eps_infty" self.id_string = "eps_infty"
self.param_number = 1 self.param_number = 1
def function( self, p, x ): def function( self, p, x ):
BaseObject.function(self,p,x) BaseContainer.function(self, p, x)
eps_inf = p[0] eps_inf = p[0]
static = np.ones( (2,x.size) )*eps_inf static = np.ones((2, x.size))*eps_inf
static[1,:] *= 0 # set imag part zero static[1, :] *= 0 # set imag part zero
return static return static
class Peak(BaseObject): class Peak(BaseContainer):
def __init__( self, id_num=None, plt_real=None, plt_imag=None, limits=None ): def __init__( self, id_num=None, plt_real=None, plt_imag=None, limits=None ):
super(Peak, self).__init__(plt_real=plt_real, plt_imag=plt_imag, limits=limits) super(Peak, self).__init__(plt_real=plt_real, plt_imag=plt_imag, limits=limits)
self.widget = ContainerWidgets.PeakWidget() self.widget = gui.ContainerWidgets.PeakWidget()
self.widget.setId(id_num) self.widget.setId(id_num)
self.color = id_to_color(id_num) self.color = id_to_color(id_num)
self.widget.setColor(self.color) self.widget.setColor(self.color)
@ -79,18 +82,18 @@ class Peak(BaseObject):
self.param_number = 4 self.param_number = 4
def function( self, p, x ): def function( self, p, x ):
BaseObject.function(self,p,x) BaseContainer.function(self, p, x)
eps,t,a,b = p eps, t, a, b = p
om = 2*np.pi*x om = 2*np.pi*x
hn = eps/(1+(1j*om*t)**a)**b hn = eps/(1+(1j*om*t)**a)**b
cplx = np.array([hn.real, -hn.imag]) cplx = np.array([hn.real, -hn.imag])
return cplx return cplx
class YAFF(BaseObject): class YAFF(BaseContainer):
def __init__( self, plt_real=None, plt_imag=None, limits=None ): def __init__( self, plt_real=None, plt_imag=None, limits=None ):
super(YAFF, self).__init__(plt_real=plt_real, plt_imag=plt_imag, limits=limits) super(YAFF, self).__init__(plt_real=plt_real, plt_imag=plt_imag, limits=limits)
self.widget = ContainerWidgets.YaffWidget() self.widget = gui.ContainerWidgets.YaffWidget()
self.widget.on_model_changed.connect(self.change_model) self.widget.on_model_changed.connect(self.change_model)
self.widget.configuration_changed.connect(self.change_configuration) self.widget.configuration_changed.connect(self.change_configuration)
self.color = QColor(32, 120, 29, int(255*0.82)) self.color = QColor(32, 120, 29, int(255*0.82))
@ -101,19 +104,19 @@ class YAFF(BaseObject):
self._selector_mask = self.widget.selector_mask self._selector_mask = self.widget.selector_mask
@property @property
def param_number(self): def param_number( self ):
return self._param_number return self._param_number
@param_number.setter @param_number.setter
def param_number(self, num=None): def param_number( self, num=None ):
self._param_number = self._libyaff.params self._param_number = self._libyaff.params
def change_configuration(self, t_list, tau_list): def change_configuration( self, t_list, tau_list ):
self._libyaff.dist_tau = tau_list self._libyaff.dist_tau = tau_list
self._libyaff.time_points = t_list self._libyaff.time_points = t_list
self.updateData() self.updateData()
def change_model(self): def change_model( self ):
self._libyaff = yafflib.Yaff(self.widget.getYaffType()) self._libyaff = yafflib.Yaff(self.widget.getYaffType())
self._selector_mask = self.widget.selector_mask self._selector_mask = self.widget.selector_mask
self.id_label = self._libyaff.label self.id_label = self._libyaff.label
@ -121,7 +124,7 @@ class YAFF(BaseObject):
self.updateData() self.updateData()
def function( self, p, x ): def function( self, p, x ):
BaseObject.function(self,p,x) BaseContainer.function(self, p, x)
ya = self._libyaff.loss( p, x) ya = self._libyaff.loss(p, x)
cplx = np.array([ya.imag, ya.real]) cplx = np.array([ya.imag, ya.real])
return cplx return cplx

View File

@ -6,12 +6,12 @@ import pyqtgraph as pg
__author__ = 'markusro' __author__ = 'markusro'
class BaseObject(QObject): class BaseContainer(QObject):
changedData = pyqtSignal() changedData = pyqtSignal()
removeObj = pyqtSignal(QObject) removeObj = pyqtSignal(QObject)
def __init__(self, plt_real=None, plt_imag=None, limits=None): def __init__(self, plt_real=None, plt_imag=None, limits=None):
super(BaseObject, self).__init__() super(BaseContainer, self).__init__()
myPen = pg.mkPen( style=Qt.DotLine, myPen = pg.mkPen( style=Qt.DotLine,
width=2.5) width=2.5)
@ -84,7 +84,6 @@ class BaseObject(QObject):
@color.setter @color.setter
def color(self, c): def color(self, c):
self._color = c self._color = c
print c
self.data_curve_real.setPen(color=c, style=Qt.DotLine, width=2.5) self.data_curve_real.setPen(color=c, style=Qt.DotLine, width=2.5)
self.data_curve_imag.setPen(color=c, style=Qt.DotLine, width=2.5) self.data_curve_imag.setPen(color=c, style=Qt.DotLine, width=2.5)

View File

@ -159,7 +159,32 @@ class FunctionRegister:
#print "FR: ",self.registry #print "FR: ",self.registry
def get_registered_functions( self ): def get_registered_functions( self ):
return self.registry # returns functions, peaks sorted by tau
sorted_functions = list()
tau_comp = 0
for i_fcn, fcn in enumerate(self.registry):
if fcn.id_string == "hn":
for i, varname in enumerate(fcn.widget.names):
if varname == "tau":
if fcn._beta[i] <= tau_comp:
sorted_functions.append(fcn)
else:
sorted_functions.insert(0, fcn)
tau_comp = fcn._beta[i]
# eps_infty to the front
elif fcn.id_string == "eps_infty":
sorted_functions.insert(0, fcn)
# sort the rest lexigraphically
elif fcn.id_string == "pwr":
sorted_functions.append(fcn)
elif fcn.id_string == "cond":
sorted_functions.append(fcn)
elif fcn.id_string == "yaff":
sorted_functions.append(fcn)
else:
sorted_functions.append(fcn)
return sorted_functions
# ############# deprecated ##################### # ############# deprecated #####################

View File

@ -252,49 +252,15 @@ class AppWindow(QMainWindow):
pre_header += "# Time: {time}\n# SessionID={id}\n".format(time=time.strftime("%H:%M:%S"), id=self.session_id) pre_header += "# Time: {time}\n# SessionID={id}\n".format(time=time.strftime("%H:%M:%S"), id=self.session_id)
pars = [] pars = []
base_filename, file_ext = os.path.splitext(self.filepath) base_filename, file_ext = os.path.splitext(self.filepath)
# print "Registered Functions (saveFitResult): ",self.function_registry.get_registered_functions()
header = "{n1:13}{n2:13}".format(n1="# 0:T", n2="1:invT") header = "{n1:13}{n2:13}".format(n1="# 0:T", n2="1:invT")
varnum = 2 # T, invT are the first two columns varnum = 2 # T, invT are the first two columns
# sort peaks by time constant tau
sorted_functions = list()
tau_comp = 0
for i_fcn, fcn in enumerate(self.function_registry.get_registered_functions()): for i_fcn, fcn in enumerate(self.function_registry.get_registered_functions()):
if fcn.id_string == "hn":
for i, varname in enumerate(fcn.widget.names):
if varname == "tau":
if fcn._beta[i] <= tau_comp:
sorted_functions.append(fcn)
else:
sorted_functions.insert(0, fcn)
tau_comp = fcn._beta[i]
# eps_infty to the front
for a in self.function_registry.get_registered_functions():
print a.id_string
if a.id_string == "eps_infty":
sorted_functions.insert(0, a)
print a
# sort the rest lexigraphically
for a in self.function_registry.get_registered_functions():
if a.id_string == "pwr":
sorted_functions.append(a)
print a
for a in self.function_registry.get_registered_functions():
if a.id_string == "cond":
sorted_functions.append(a)
print a
for a in self.function_registry.get_registered_functions():
if a.id_string == "yaff":
print a
sorted_functions.append(a)
print sorted_functions
for i_fcn, fcn in enumerate(sorted_functions):
fit_function_name = fcn.id_string fit_function_name = fcn.id_string
for i, name in enumerate(fcn.widget.names): # get variable names for i, name in enumerate(fcn.widget.names): # get variable names
header += "{n:13}{n_sd:13}".format(n="%i:%s"%(varnum, name), n_sd="%i:%s_sd"%(varnum+1, name)) header += "{n:13}{n_sd:13}".format(n="%i:%s"%(varnum, name), n_sd="%i:%s_sd"%(varnum+1, name))
varnum += 2 varnum += 2
pre_header += "# %s\n"%fit_function_name
# write for each function an extra file # write for each function an extra file
fit_filename = "%s_%i.fit"%(base_filename, i_fcn) fit_filename = "%s_%i.fit"%(base_filename, i_fcn)
f_fcn = open(fit_filename, 'w') f_fcn = open(fit_filename, 'w')
@ -442,7 +408,6 @@ class AppWindow(QMainWindow):
self.function_registry.get_registered_functions() self.function_registry.get_registered_functions()
if key.id_string == 'hn'] if key.id_string == 'hn']
self.peakId = 1 self.peakId = 1
print id_list
while self.peakId in id_list: while self.peakId in id_list:
self.peakId += 1 self.peakId += 1
_peak = Peak(id_num=self.peakId, _peak = Peak(id_num=self.peakId,