from PyQt4.QtCore import QObject, pyqtSignal, Qt from PyQt4.QtGui import QColor import numpy as N import CustomWidgets import pyqtgraph as pg from PyQt4.QtCore import * from mathlib import id_to_color, hn, FitFunctionCreator, Functions class Data: def __init__(self, frequency=N.zeros(1), die_real=N.zeros(1), die_imag=N.zeros(1)): self.frequency = frequency self.epsilon = die_real + 1j * die_imag self.frequency_fit = frequency self.epsilon_fit = die_real*0 + 1j * die_imag*0 myPen_imag = pg.mkPen(width=3, color=(255,255,127)) myPen_real = pg.mkPen(width=3, color=(51,255,127)) #33FF00 self.data_curve_imag = pg.PlotDataItem(x=[N.nan], y=[N.nan],pen=QColor(0,0,0,0), symbol='o', symbolBrush=(255,127,0,127)) self.data_curve_real = pg.PlotDataItem(x=[N.nan], y=[N.nan],pen=QColor(0,0,0,0), symbol='s', symbolBrush=(119,202,92,127)) self.fitted_curve_imag = pg.PlotDataItem(N.array([N.nan]), N.array([N.nan]), pen=myPen_imag) self.fitted_curve_real = pg.PlotDataItem(N.array([N.nan]), N.array([N.nan]), pen=myPen_real) self.length = len(frequency) self.meta = dict() self.fit_limits = [frequency.min(), frequency.max(), die_imag.min(), die_imag.max()] self.fit_param = None self.fit_funcs = None # list of fit functions self.hide_funcs = None # remove these func from the data def set_fit(self, param, funcs): self.fit_funcs = funcs self.hide_funcs = [] self.fit_param = param fit_real, fit_imag = FitFunctionCreator().fitfcn(param, self.frequency_fit, *funcs) self.epsilon_fit = fit_real + 1j*fit_imag def __del__(self): #self.remove_curves() pass #def set_fit_limits(self, limits=(None,None,None,None)): def set_data(self,f,e_real,e_imag): self.frequency = f self.epsilon = e_real + 1j*e_imag self.epsilon_fit = 0*e_real + 1j*e_imag*0 self.fit_limits = [f.min(), f.max(), e_imag.min(), e_imag.max()] self.data_curve_imag.setData(f,e_imag) self.data_curve_real.setData(f,e_real) def set_fit_xlimits(self, xmin, xmax): self.fit_limits[0] = xmin self.fit_limits[1] = xmax self.frequency_fit = self.frequency[(self.frequency <= xmax) & (self.frequency >= xmin)] def set_fit_ylimits(self, ymin, ymax): self.fit_limits[2] = ymin self.fit_limits[3] = ymax def get_data(self): """ """ mask = N.ones(len(self.frequency), dtype='bool') mask = (self.frequency > self.fit_limits[0]) & (self.frequency < self.fit_limits[1]) #mask &= (self.epsilon.imag > self.fit_limits[2]) & (self.epsilon.imag < self.fit_limits[3]) #mask &= (self.epsilon.real > self.fit_limits[2]) & (self.epsilon.real < self.fit_limits[3]) return self.frequency[mask], self.epsilon[mask] def remove_curves(self): print "remove data_curve" #if self.data_curve is not None: self.data_curve.remove() print "remove fitted_curve" #if self.fitted_curve is not None: self.fitted_curve.remove() class BaseObject(QObject): changedData = pyqtSignal() removeObj = pyqtSignal(QObject) def __init__(self, plt_real=None, plt_imag=None, limits=None): super(BaseObject, self).__init__() myPen = pg.mkPen( style=Qt.DotLine, width=2.5) self.data_curve_real = pg.PlotDataItem(x=N.array([N.nan]),y=N.array([N.nan]), pen=myPen) self.plt_real = plt_real self.plt_real.addItem(self.data_curve_real) self.data_curve_imag = pg.PlotDataItem(x=N.array([N.nan]),y=N.array([N.nan]), pen=myPen) self.plt_imag = plt_imag self.plt_imag.addItem(self.data_curve_imag) self.limits = limits self.f=Functions() self._color = QColor("white") @property def color(self): return self._color @color.setter def color(self, c): self._color = c self.data_curve_real.setPen(c) self.data_curve_imag.setPen(c) def getParameter(self): p = self.widget.getTable() return p def getFixed(self): p = self.widget.fixedParameter() return p def setParameter(self, beta, sd_beta=None): self.widget.updateTable(beta, sd_beta) self.updateData() def get_data(self): return self.frequency, self.conductivity def get_epsilon_static(self): return self.frequency, self.epsilon_static def removeMe(self): self.plt_imag.removeItem(self.data_curve_imag) self.plt_real.removeItem(self.data_curve_real) self.removeObj.emit(self) self.changedData.emit() class Conductivity(BaseObject): 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) self.widget = CustomWidgets.ConductivityWidget() self.widget.changedTable.connect(self.updateData) self.widget.removeMe.connect(self.removeMe) self.color = QColor("blue") self.frequency = None self.conductivity = None self.id_string = "conductivity" def updateData(self): # get current axis limits x_min, x_max, y_min, y_max = self.limits self.frequency = N.logspace(N.log10(x_min), N.log10(x_max), 254) p = self.getParameter() self.conductivity = self.f.cond_cmplx(p, self.frequency) self.data_curve_real.setData(x=self.frequency, y=self.conductivity[0], label="Cond.") self.data_curve_imag.setData(x=self.frequency, y=self.conductivity[1], label="Cond.") self.changedData.emit() class PowerComplex(BaseObject): 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) self.widget = CustomWidgets.PowerLawWidget() self.widget.changedTable.connect(self.updateData) self.widget.removeMe.connect(self.removeMe) self.frequency = None self.powerlaw = None self.color = QColor("#ff44c4") self.id_string = 'power' def updateData(self): # get current axis limits x_min, x_max, y_min, y_max = self.limits p = self.getParameter() self.frequency = N.logspace(N.log10(x_min), N.log10(x_max), 1024) self.powerlaw = self.f.power_cmplx(p, self.frequency) # imaginary part self.data_curve_real.setData(x=self.frequency, y=self.powerlaw[0], label="Power Law") self.data_curve_imag.setData(x=self.frequency, y=self.powerlaw[1], label="Power Law") self.changedData.emit() class Static(BaseObject): 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) self.widget = CustomWidgets.StaticWidget() self.widget.changedTable.connect(self.updateData) self.widget.removeMe.connect(self.removeMe) self.frequency = None self.static = None self.color = QColor('#FF0F13') self.id_string = 'static' def updateData(self): # get current axis limits x_min, x_max, y_min, y_max = self.limits p = self.getParameter() self.frequency = N.logspace(N.log10(x_min), N.log10(x_max), 1024) self.static = self.f.static_cmplx(p, self.frequency) [0] # real part self.data_curve_real.setData(x=self.frequency, y=self.static, label="Power Law") self.changedData.emit() class Peak(BaseObject): 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) self.widget = CustomWidgets.PeakWidget() self.widget.setId(id_num) self.color = id_to_color(id_num) self.id_num = id_num self.widget.setColor(self.color) self.widget.changedTable.connect(self.updateData) self.widget.removeMe.connect(self.removeMe) self.frequency = None self.epsilon = None self.id_string = "hn" def updateData(self): x_min,x_max, y_min, y_max = self.limits self.frequency = N.logspace(N.log10(x_min), N.log10(x_max), 1024) self.epsilon = self.f.hn_cmplx(self.getParameter(), self.frequency) self.data_curve_imag.setData(x=self.frequency, y=self.epsilon[1]) self.data_curve_real.setData(x=self.frequency, y=self.epsilon[0]) self.changedData.emit() class YAFF(BaseObject): 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) self.widget = CustomWidgets.YaffWidget() self.widget.changedTable.connect(self.updatePeak) self.widget.removeMe.connect(self.removeMe) self.frequency = None self.epsilon = None self.id_string = "yaff" def updatePeak(self): x_min,x_max, y_min, y_max = self.limits self.frequency = N.logspace(N.log10(x_min), N.log10(x_max), 1024) self.epsilon = self.f.hn_cmplx(self.getParameter()[:4], self.frequency) self.data_curve_imag.setData(x=self.frequency, y=self.epsilon[1]) self.data_curve_real.setData(x=self.frequency, y=self.epsilon[0]) self.changedData.emit()