using abc module (abstract base class) to check for proper implementation of the container classes

This commit is contained in:
Markus Rosenstihl 2015-01-08 17:40:25 +01:00
parent c7a258aa1d
commit 8f538174d9
2 changed files with 39 additions and 9 deletions

View File

@ -13,7 +13,6 @@ import gui.container_widgets
__author__ = 'markusro'
# FIXME: why are the functions implemented again? Better to use function_library!!
class Conductivity(BaseContainer):
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)
@ -27,6 +26,10 @@ class Conductivity(BaseContainer):
def function(self, p ,x):
return functions.cond_cmplx(p,x)
def start_parameter(self, pos):
cond_par = [0.0, 10**(pos.y()+pos.x())*2*np.pi, 1.0]
self.set_parameter(beta=cond_par)
class PowerComplex(BaseContainer):
def __init__( self, plt_real=None, plt_imag=None, limits=None ):
@ -40,6 +43,10 @@ class PowerComplex(BaseContainer):
def function( self, p, x ):
return functions.power_cmplx(p, x)
def start_parameter(self, pos):
cond_par = [10**(pos.y()+pos.x())*2*np.pi, 1.0]
self.set_parameter(cond_par)
class Static(BaseContainer):
def __init__( self, plt_real=None, plt_imag=None, limits=None ):
@ -53,6 +60,9 @@ class Static(BaseContainer):
def function( self, p, x ):
return functions.static_cmplx(p, x)
def start_parameter(self, position):
cond_par = [10**position.y()]
self.set_parameter(beta=cond_par)
class Peak(BaseContainer):
def __init__( self, id_num=None, plt_real=None, plt_imag=None, limits=None ):
@ -69,6 +79,9 @@ class Peak(BaseContainer):
def function( self, p, x ):
return functions.hn(p, x)
def start_parameter(self, pos):
new_peak_beta0 = [2*10**pos.y(), 1/(2*np.pi*10**pos.x()), 1, 1]
self.set_parameter(beta=new_peak_beta0)
class YAFF(BaseContainer):
def __init__( self, plt_real=None, plt_imag=None, limits=None ):
@ -106,4 +119,10 @@ class YAFF(BaseContainer):
def function( self, p, x ):
ya = self._libyaff.loss(p, x)
cplx = np.array([ya.imag, ya.real])
return cplx
return cplx
def start_parameter(self, pos):
gg_y = 10**pos.y()*2
gg_x = 1/(10**pos.x()*2*np.pi)
yaff_par = [gg_y, gg_x, 20.0, 1.0, 0.5, gg_x/100, 1.0, 1.0]
self.set_parameter(yaff_par)

View File

@ -2,10 +2,15 @@ from PyQt4.QtCore import QObject, pyqtSignal, Qt, pyqtSlot
from PyQt4.QtGui import QColor
import numpy as np
import pyqtgraph as pg
import abc
__author__ = 'markusro'
class QABCMeta(abc.ABCMeta, QObject.__class__):
"""
Allows us to use ABCMeta
"""
pass
class BaseContainer(QObject):
"""
@ -14,6 +19,8 @@ class BaseContainer(QObject):
Specific containers are implemented in the container.py module.
"""
__metaclass__ = QABCMeta
# TODO generalize the base class so that we can use plugins (self-contained fit functions)
changedData = pyqtSignal()
removeObj = pyqtSignal(QObject)
@ -74,7 +81,6 @@ class BaseContainer(QObject):
# self._func = self.functions.get_function(id)
self._id_string = id
@property
def id_label( self ):
return self._id_label
@ -108,16 +114,16 @@ class BaseContainer(QObject):
p = self.widget.getTable() # TODO ugly ... should return self._beta etc ...?
return p
def get_fixed( self ):
p = self.widget.fixedParameter()
return p
def set_parameter( self, beta, sd_beta=None ):
self._beta = beta
self._sd_beta = sd_beta
self.widget.update_table(beta, sd_beta)
self.update_data()
def get_fixed( self ):
p = self.widget.fixedParameter()
return p
def get_data( self ):
return self._frequency, self._data
@ -147,9 +153,14 @@ class BaseContainer(QObject):
self.data_curve_real.setData(x=[np.nan], y=[np.nan])
self.data_curve_imag.setData(x=[np.nan], y=[np.nan])
@abc.abstractmethod
def start_parameter(self, position):
raise NotImplementedError("This needs to be implemented in your subclass")
@abc.abstractmethod
def function( self, p, x ):
if self._abort:
raise StopIteration
raise NotImplementedError, "This needs to be implemented in your subclass"
raise NotImplementedError("This needs to be implemented in your subclass")