from PyQt4.QtCore import QObject, pyqtSignal, Qt from PyQt4.QtGui import QColor import numpy as N import PeakWidget import conductivityWidget import pyqtgraph as pg from PyQt4.QtCore import * from mathlib import id_to_color, hn, FitFunctionCreator 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.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=(255,127,127)) 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 def set_fit(self, param, funcs): self.fit_funcs = funcs self.fit_param = param fit_real, fit_imag = FitFunctionCreator().fitfcn(param, self.frequency, *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 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 Conductivity(QObject): changedData = pyqtSignal() def __init__(self, mpl=None, limits=None): QObject.__init__(self) super(Conductivity, self) self.widget = conductivityWidget.ConductivityWidget() self.widget.changedTable.connect(self.updateData) myPen = pg.mkPen( style=Qt.DashLine, width=1) self.color=QColor("black") self.mpl_line = pg.PlotDataItem(x=N.array([N.nan]), y=N.array([N.nan]), pen=myPen) self.mpl_line_static = pg.PlotDataItem(x=N.array([N.nan]), y=N.array([N.nan]), pen=myPen) self.mpl = mpl self.mpl.addItem(self.mpl_line) self.mpl.addItem(self.mpl_line_static) self.limits = limits self.frequency = None self.conductivity = None self.epsilon_static = None def getParameter(self): p = self.widget.getTable() return p def getFixed(self): p = self.widget.fixedParameter() return p def setParameter(self, eps_static=None, sigma=None, sigma_N=None): self.widget.updateTable(eps_static, sigma, sigma_N) self.updateData() def updateData(self): # get current axis limits x_min, x_max, y_min, y_max = self.limits nu = N.logspace(N.log10(x_min), N.log10(x_max), 1024) eps_static, sigma, sigma_N = self.getParameter() y = conductivity([sigma, sigma_N], nu) y_static = N.ones(len(nu)) * eps_static # clip data to axes limits #mask_static = (y_static < y_max) & (y_static > y_min) # clip data to axes limits #mask = (y < y_max) & (y > y_min) self.frequency = nu self.conductivity = y self.epsilon_static = y_static self.mpl_line.setData(x=nu, y=y, label="Cond.") self.mpl_line_static.setData(x=nu, y=y_static) self.changedData.emit() def get_color(self): return self.color def get_conductivity(self): return self.frequency, self.conductivity def get_epsilon_static(self): return self.frequency, self.epsilon_static def conductivity(p, nu): c = p[0] / (2 * N.pi * nu) ** p[1] return c class Peak(QObject): changedData = pyqtSignal() def __init__(self, id=None, mpl=None, limits=None): QObject.__init__(self) super(Peak, self).__init__() self.widget = PeakWidget.PeakWidget() self.widget.setId(id) self.color = id_to_color(id) self.widget.setColor(self.color) self.widget.changedTable.connect(self.updatePeak) self.mpl = mpl self.limits = limits myPen = pg.mkPen( style=Qt.DashLine, width=2, color=self.color) self.mpl_line = pg.PlotDataItem(x=N.array([N.nan]),y=N.array([N.nan]), pen=myPen) self.mpl.addItem(self.mpl_line) self.frequency = None self.epsilon = None def getParameter(self): p = self.widget.peakParameter() return p def getFixed(self): p = self.widget.fixedParameter() return p def setParameter(self, delta_eps=None, tau=None, a=None, b=None): self.widget.updateTable(delta_eps, tau, a, b) self.updatePeak() def updatePeak(self): # get current axis limits # x_min, x_max = self.mpl.canvas.axes.get_xlim() # y_min, y_max = self.mpl.canvas.axes.get_ylim() x_min,x_max, y_min, y_max = self.limits nu = N.logspace(N.log10(x_min), N.log10(x_max), 2048) y = hn(self.getParameter(), nu) # clip data to axes limits #mask = (y < y_max) & (y > y_min) #y = y[mask] #nu = nu[mask] self.frequency = nu[:] self.epsilon = y[:] self.mpl_line.setData(x=nu, y=y) self.changedData.emit() def get_color(self): return self.color def get_data(self): return self.frequency,self.epsilon