More restructuring

This commit is contained in:
2014-09-24 20:46:23 +02:00
parent 977b091191
commit 79cb5b3ec4
12 changed files with 12 additions and 255 deletions

347
libmath/BDSMathlib.py Normal file
View File

@ -0,0 +1,347 @@
# -*- encoding: utf-8 -*-
from math import libyaff
__author__ = 'markusro'
from PyQt4.QtGui import QColor
from PyQt4.QtCore import QObject, pyqtSignal, pyqtSlot
import numpy as np
from scipy import optimize as opt, odr
def id_to_color( id ):
colors = [
QColor(255, 255, 255),
QColor(168, 149, 17),
QColor(45, 142, 15),
QColor(160, 16, 36),
QColor(54, 22, 115),
QColor(36, 10, 85),
QColor(118, 8, 23),
QColor(31, 105, 7),
QColor(124, 109, 8),
]
chosen_color = colors[id%len(colors)]
return chosen_color
class FitFunctionCreator(QObject):
new_data = pyqtSignal(np.ndarray, np.ndarray)
def __init__( self ):
super(FitFunctionCreator, self).__init__()
self.data = None
self.functions = Functions()
def fitfcn( self, p0, x, *funcs ):
if x.ndim == 2:
self.data = np.zeros(x.shape)
else:
self.data = np.zeros((2, x.size))
ndx = 0
for fn in funcs: # loop over functions and add the results
f, num_p = fn.function, fn.param_number
p = p0[ndx:ndx+num_p]
if x.ndim == 2:
x = x[0]
result = f(p, x)
# fn.widget.updateTable(p)
self.data += result # fit functions take only 1-dim x
ndx += num_p
self.new_data.emit(x, self.data)
return self.data
def fitfcn_imag( self, p0, x, *funcs ):
if x.ndim == 2:
self.data = np.zeros(x.shape)
else:
self.data = np.zeros((2, x.size))
ndx = 0
for fn in funcs: # loop over functions and add the results
f, num_p = fn.function, fn.param_number
p = p0[ndx:ndx+num_p]
if x.ndim == 2:
x = x[0]
result = f(p, x)
self.data += result # fit functions take only 1-dim x
ndx += num_p
self.new_data.emit(x, self.data)
return self.data[1]
class FitRoutine(QObject):
finished_fit = pyqtSignal()
data_ready = pyqtSignal(np.ndarray, np.ndarray)
def __init__( self ):
super(FitRoutine, self).__init__()
self.f = FitFunctionCreator()
self.f.new_data.connect(self.data_ready.emit)
self._fitter = self.fit_odr_cmplx
self._odr_fit = None
self._start_parameter = None
@property
def start_parameter( self ):
return self._start_parameter
@start_parameter.setter
def start_paramter( self, p0 ):
self._start_parameter = p0
@property
def fitter( self ):
return self._fitter
@fitter.setter
def fitter( self, f ):
self._fitter = f
def fit_odr_cmplx( self, x, y, p0, fixed, fcns ):
self._start_parameter = p0
if np.iscomplexobj(y) and y.ndim == 1:
weights = 1/np.abs(y)**2
we = np.resize(weights, (2, weights.size))
# we = 1/N.array([y.real**2, y.imag**2])
y = np.array([y.real, y.imag])
else:
raise NotImplementedError, "need complex input for now"
dat = odr.Data(x, y, we=we)
mod = odr.Model(self.f.fitfcn, extra_args=fcns)
self._odr_fit = odr.ODR(dat, mod, p0, ifixx=(0,), ifixb=fixed, maxit=800)
def fit_odr_imag( self, x, y, p0, fixed, fcns ):
self._start_parameter = p0
if np.iscomplexobj(y) and y.ndim == 1:
we = 1/np.imag(y)**2
else:
raise NotImplementedError, "need complex input for now"
dat = odr.Data(x, y.imag, we=we)
mod = odr.Model(self.f.fitfcn_imag, extra_args=fcns)
self._odr_fit = odr.ODR(dat, mod, p0, ifixx=(0,), ifixb=fixed, maxit=800)
@pyqtSlot()
def fit( self ):
try:
self._odr_fit.run()
except RuntimeError:
print "muh"
self.finished_fit.emit()
def result( self ):
if self._odr_fit.output is None:
self._odr_fit.output = odr.Output([self.start_parameter, None, None])
self._odr_fit.output.stopreason = ["Aborted by user"]
return self._odr_fit.output
class FunctionRegister:
def __init__( self ):
self.registry = { }
def register_function( self, obj ):
# print "FR: Registering:",obj
id_string = obj.id_label
if self.registry.has_key(obj):
raise AssertionError, "The object is already registered! This should NOT happen"
self.registry[obj] = id_string
#print "FR: ",self.registry
def unregister_function( self, obj ):
# print "FR: UnRegistering:",obj
if self.registry.has_key(obj):
self.registry.pop(obj)
else:
obj.deleteLater()
raise AssertionError, "The object is not in the registry! This should NOT happen"
#print "FR: ",self.registry
def get_registered_functions( self ):
return self.registry
# ############# deprecated #####################
def fit_odr_cmplx( x, y, p0, fixed, fcns ):
f = FitFunctionCreator()
#if x.ndim < 2:
# x = N.resize(x, (2,x.size))
if np.iscomplexobj(y) and y.ndim == 1:
weights = 1/np.abs(y)**2
we = np.resize(weights, (2, weights.size))
#we = 1/N.array([y.real**2, y.imag**2])
y = np.array([y.real, y.imag])
else:
raise NotImplementedError, "need complex input for now"
dat = odr.Data(x, y, we=we)
mod = odr.Model(f.fitfcn, extra_args=fcns)
fit = odr.ODR(dat, mod, p0, ifixx=(0,), ifixb=fixed, maxit=8000)
fit.run()
#print fit.output.pprint()
return fit.output
### define funcs here
class Functions(QObject):
def __init__( self ):
super(Functions, self).__init__()
self.list = {
# provides functions: "id_string":(function, number_of_parameters)
"hn": (self.hn_cmplx, 4),
"conductivity": (self.cond_cmplx, 3),
"power": (self.power_cmplx, 2),
"static": (self.static_cmplx, 1),
"yaff": (self.yaff, 8)
}
self.YAFF = libyaff.Yaff()
def hn_cmplx( self, p, x ):
om = 2*np.pi*x
#hn = om*1j
eps, t, a, b = p
hn = eps/(1+(1j*om*t)**a)**b
cplx = np.array([hn.real, -hn.imag])
return cplx
def cond_cmplx( self, p, x ):
om = 2*np.pi*x
sgma, isgma, n = p
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])
return cplx
def power_cmplx( self, p, x ):
om = 2*np.pi*x
sgma, n = p
power = sgma/(om*1j)**n
cplx = np.array([power.real, -power.imag])
return cplx
def static_cmplx( self, p, x ):
eps_inf = p[0]
static = np.ones((2, x.size))*eps_inf
static[1, :] *= 0 # set imag part zero
#cplx = N.array([static.real, static.imag])
return static
def yaff( self, p, x ):
ya = self.YAFF.yaff(p[:8], x)
cplx = np.array([ya.imag, ya.real])
return cplx
def get( self, name ):
return self.list[name]
def get_function( self, name ):
return self.list[name][0]
def fit_anneal( x, y, p0, fixed, funcs ):
raise NotImplementedError
bounds = [(0, 1e14), (0, 1)]
for i in xrange(len(p0[2:])/4):
bounds.append((1e-4, 1e12)) # delta_eps
bounds.append((1e-12, 1e3)) # tau
bounds.append((0.1, 1)) # a
bounds.append((0.1, 1)) # b
ret = opt.anneal(mini_func, p0,
args=(x, y),
maxeval=20000,
maxiter=30000,
lower=[b[0] for b in bounds],
upper=[b[1] for b in bounds],
dwell=100,
full_output=1)
#pmin, func_min, final_Temp, cooling_iters,accepted_tests, retval
#retval : int
#Flag indicating stopping condition::
# 0 : Points no longer changing
# 1 : Cooled to final temperature
# 2 : Maximum function evaluations
# 3 : Maximum cooling iterations reached
# 4 : Maximum accepted query locations reached
# 5 : Final point not the minimum amongst encountered points
print "Stop reason", ret
return ret[0]
def fit_lbfgsb( x, y, p0, fixed, funcs ):
raise NotImplementedError
# TODO fixed parameters…
bounds = [(0, None), (0, 1)]
for i in xrange(len(p0[3:])/4):
bounds.append((1e-4, 1e12)) # delta_eps
bounds.append((1e-12, 1e3)) # tau
bounds.append((0.1, 1)) # a
bounds.append((0.1, 1)) # b
x, f_minvalue, info_dict = opt.fmin_l_bfgs_b(mini_func, p0,
fprime=None,
args=(x, y),
approx_grad=True,
bounds=bounds,
iprint=0,
maxfun=4000)
if info_dict['warnflag'] != 0:
print info_dict["task"]
return x
# Replaced with fit_odr_cmplx
#
#def fit_odr(x, y, p0, fixed, funcs):
# dat = odr.Data(x, y, 1.0 / y**2)
# mod = odr.Model(multi_hn)
# fit = odr.ODR(dat, mod, p0, ifixx=(0,), ifixb=fixed, maxit=2000)
# fit.run()
# return fit.output.beta
def hn( p, nu ):
delta_eps, tau, a, b = p
om = 2*np.pi*nu
Phi = np.arctan((om*tau)**a*np.sin(np.pi*a/2.)/(1.+(om*tau)**a*np.cos(np.pi*a/2.)))
e_loss = delta_eps*(1+2*(om*tau)**a*np.cos(np.pi*a/2.)+(om*tau)**(2.*a) )**(
-b/2.)*np.sin(b*Phi)
e_stor = delta_eps*(1+2*(om*tau)**a*np.cos(np.pi*a/2.)+(om*tau)**(2.*a) )**(
-b/2.)*np.cos(b*Phi)
return e_loss # 2* oder nicht?
def mini_func( p, x, y ):
res = y-multi_hn(p, x)
# apply weights
res /= 1/y
return np.sqrt(np.dot(res, res))
def multi_hn( p, nu ):
conductivity = p[1]
cond_beta = p[2]
om = 2*np.pi*nu
e_loss = conductivity/om**cond_beta
e_loss += p[0]
#for key, igroup in groupby(p[3:], lambda x: x//4):
for i in xrange(len(p[3:])/4):
delta_eps, tau, a, b = p[3+i*4:3+(i+1)*4]
#delta_eps, tau, a, b = list(igroup)
#print delta_eps,tau,a,b
#a = 0.5 *(1 + N.tanh(a))
#b = 0.5 *(1 + N.tanh(b))
Phi = np.arctan((om*tau)**a*np.sin(np.pi*a/2.)/(1.+(om*tau)**a*np.cos(np.pi*a/2.)))
e_loss += 2*delta_eps*(1+2*(om*tau)**a*np.cos(np.pi*a/2.)+(om*tau)**(2.*a) )**(
-b/2.)*np.sin(b*Phi)
#e_stor = delta_eps * (1+ 2*(om*tau)**a * N.cos(N.pi*a/2.) + (om*tau)**(2.*a) )**(-b/2.)*N.cos(b*Phi)
return e_loss
def tau_peak( f, a, b ):
tau = (np.sin(np.pi*a/2./(b+1))/np.sin(np.pi*a*b/2./(b+1)))**(1/a)
tau /= 2*np.pi*f
return tau

0
libmath/__init__.py Normal file
View File

258
libmath/libyaff.py Normal file
View File

@ -0,0 +1,258 @@
__author__ = 'markusro'
from numpy import *
import multiprocessing
from PyQt4.QtCore import pyqtSignal, QObject
import scipy.special
import scipy.integrate
#define norm1(a,b) exp(gammln(b/a))/(a*pow(b/a,b/a))
#define glntau1(x,t,a,b) exp( -exp( (log(x)-log(t))*a )*b/a) * pow(x/t,b)
def filon(oms, unsorted_x,unsorted_y):
x = unsorted_x[unsorted_x.argsort()]
y = unsorted_y[unsorted_x.argsort()]
amps = zeros(oms.size, dtype='complex')
for i,om in enumerate(oms):
amps[i] = sum(diff(y)/diff(x)*(cos(om*x[1:])-cos(om*x[:-1])))/om**2
amps[i] += 1j*( - sum(diff(y)/diff(x)*(sin(om*x[1:])-sin(om*x[:-1])))/om**2)
#-y[0]/om
return amps
class PhiT:
def __init__(self, dist_x, dist_y):
self.dist_x = dist_x
self.dist_y = dist_y
def __call__(self, ts):
phis = zeros(ts.size)
for i,t in enumerate(ts):
kern = self.dist_y*exp(-t/self.dist_x)
phis[i] = scipy.integrate.simps(kern, log(self.dist_x))
return phis
class Yaff(QObject):
step_signal = pyqtSignal(list)
def __init__(self, dist_type=0):
super(Yaff,self).__init__()
self._dist_tau = logspace(-12,6,512)
self.dist_y = zeros(self.dist_tau.size)
self._time_points = logspace(-10,5,512*2)
yaff = [
(self.yaff_gg, "GG"),
(self.yaff_gge, "GGe"),
(self.yaff_gb, "Gb"),
(self.yaff, "GG + GGb"),
(self.yaff_extended, "GGe + GGb"),
]
self.loss, self.label = yaff[dist_type]
self.params = 10
@property
def time_points(self):
return self._time_points
@time_points.setter
def time_points(self, t_list):
tmin, tmax, num = t_list
tmi,tma = tmin, tmax
self._time_points = logspace(log10(tmi), log10(tma), num)
@property
def dist_tau(self):
return self._dist_tau
@dist_tau.setter
def dist_tau(self, tau_list):
tmin, tmax, num = tau_list
tmi,tma = tmin, tmax
self._dist_tau = logspace(log10(tmi), log10(tma), num)
def gg(self, p, tau):
tau0, a, b = p
"""
Generalized Gamma function
"""
NGG = a * (b/a)**(b/a) / scipy.special.gamma(b/a)
#g = exp(-b/a*exp( (log(tau)-log(tau0))*a))*(tau/tau0)**b
g = exp(-b/a*exp( (log(tau)-log(tau0))*a))*(tau/tau0)**b
return g*NGG
def ggb(self, p, tau):
tau0,a,b = p
norm_ggb = a*(1+b)/pi *b**(b/(1+b)) * sin(pi*b/(1+b))
g = (b*exp( (log(tau)-log(tau0)) *a) + exp( (log(tau)-log(tau0))*(-a*b)))**(-1)
return norm_ggb*g
def gg_hw(self, p ,tau):
tau0, a, b, s,g = p
"""
Generalized Gamma function with HF wing.
s gives the onset of the wing in units of tau0, i.e. 100 means 100*tau0 or two decades later
g is the slope of the wing, smaller than b; if not the slope will be b
"""
NGG = a * (b/a)**(b/a) / ( scipy.special.gamma(b/a) + s**(g-b)*(a/b)**((g-b)/a)* scipy.special.gamma(g/a) )
g = exp(-b/a*exp((log(tau) - log(tau0))*a))*(tau/tau0)**b * (1 + (tau*s/tau0)**(g-b))
return g*NGG
def dist_to_eps(omegas,dist_x,dist_y):
epp = zeros(len(omegas), dtype='complex')
for i,om in enumerate(omegas):
kern_imag = dist_y*om*dist_x/(1+(om*dist_x)**2)
kern_real = dist_y/(1+(om*dist_x)**2)
epp[i] = scipy.integrate.simps(kern_real,log(dist_x))
epp[i] += 1j*scipy.integrate.simps(kern_imag,log(dist_x))
return epp
# serial version
def dist_to_relax_(self, ts,dist_x,dist_y):
phi = zeros(len(ts))
for i,t in enumerate(ts):
phi[i] = self.phi_t(t,dist_x,dist_y)
return phi
# parallel version
def dist_to_relax(self, ts,dist_x,dist_y):
pool = multiprocessing.Pool()
num_cpus = multiprocessing.cpu_count()
ts_chunks = array_split(ts,num_cpus)
result = pool.map(PhiT(dist_x,dist_y), ts_chunks )
pool.close()
pool.terminate()
pool.join()
return concatenate(result)
def phi_t(self, t, dist_x, dist_y):
kern = dist_y*exp(-t/dist_x)
phi = scipy.integrate.simps(kern,log(dist_x))
return phi
def williams_watts(self, phi_1, phi_2, lambd):
return phi_1 * ( (1-lambd) + lambd*phi_2)
def yaff2(self, p, x):
"""
GGe+G b ---> GG+GB - lambda determined by p
"""
delta_eps, s_c, tau1,a1,b1, g,s, tau2, a2, b2 = p
p_ggb = tau2,a2,b2
p_gghw=tau1,a1,b1,g,s
lambd = 2*g/pi*a1*(b1/a1)**(b1/a1)
lambd /= scipy.special.gamma(b1/a1)*s**(b1-g) + (a1/b1)**((g-b1)/a1)*scipy.special.gamma(g/a1)
print "LAMBDA:",lambd,a1,b1,g,s
dist_ggb = self.ggb(p_ggb, dist_t)
phi_beta = self.dist_to_relax(ts, dist_t, dist_ggb ).real
phi_alpha = self.dist_to_relax(ts, dist_t, self.gg_hw(p_gghw, dist_t)).real
phi_tot = (1-lambd) + lambd*phi_beta
phi_tot *= phi_alpha
epp = delta_eps*2*pi*x*filon(2*pi*x, ts, phi_tot ).real
epp += s_c/(2*pi*x)
return epp
def yaff_extended(self, p,x, cb=None):
"""
GG + GB
"""
delta_eps, tau1, a1, b1, lambd, tau2, a2, b2, s, g = p
dist_ggb = self.ggb(p=[tau2,a2,b2], tau=self.dist_tau)
dist_gg = self.gg_hw (p=[tau1,a1,b1,s,g], tau=self.dist_tau)
#ts = logspace(-10,5,512*2)
self.dist_y = dist_ggb
phi_beta = self.dist_to_relax(self.time_points, self.dist_tau, self.dist_y).real
self.dist_y = dist_gg
phi_alpha = self.dist_to_relax(self.time_points, self.dist_tau, self.dist_y).real
# William-Watts-Ansatz
phi_tot = (1-lambd) + lambd*phi_beta
phi_tot *= phi_alpha
epp = delta_eps*2*pi*x*filon(2*pi*x, self.time_points, phi_tot)
self.step_signal.emit(list(p))
return epp
def yaff(self, p,x, cb=None):
"""
GG + GB
"""
delta_eps, tau1, a1, b1, lambd, tau2, a2, b2, s, g = p
dist_ggb = self.ggb(p=[tau2,a2,b2], tau=self.dist_tau)
dist_gg = self.gg (p=[tau1,a1,b1], tau=self.dist_tau)
#ts = logspace(-10,5,512*2)
self.dist_y = dist_ggb
phi_beta = self.dist_to_relax(self.time_points, self.dist_tau, self.dist_y).real
self.dist_y = dist_gg
phi_alpha = self.dist_to_relax(self.time_points, self.dist_tau, self.dist_y).real
# William-Watts-Ansatz
phi_tot = (1-lambd) + lambd*phi_beta
phi_tot *= phi_alpha
epp = delta_eps*2*pi*x*filon(2*pi*x, self.time_points, phi_tot)
self.step_signal.emit(list(p))
return epp
def yaff_gb(self, p,x, cb=None):
"""
GG
"""
delta_eps, tau1, a1, b1, lambd, tau2, a2, b2, s, g = p
#ts = logspace(-10,5,512*2)
dist_ggb = self.ggb(p=[tau2, a2, b2], tau=self.dist_tau)
self.dist_y = dist_ggb
phi_alpha = self.dist_to_relax(self.time_points, self.dist_tau, self.dist_y).real
epp = delta_eps*2*pi*x*filon(2*pi*x, self.time_points, phi_alpha )
self.step_signal.emit(list(p))
return epp
def yaff_gg(self, p,x, cb=None):
"""
GG
"""
delta_eps, tau1, a1, b1, lambd, tau2, a2, b2, s, g = p
#ts = logspace(-10,5,512*2)
dist_gg = self.gg(p=[tau1,a1,b1], tau=self.dist_tau)
self.dist_y = dist_gg
phi_alpha = self.dist_to_relax(self.time_points, self.dist_tau, self.dist_y).real
epp = delta_eps*2*pi*x*filon(2*pi*x, self.time_points, phi_alpha )
self.step_signal.emit(list(p))
return epp
def yaff_gge(self, p,x):
"""
GG
"""
delta_eps, tau1, a1, b1, lambd, tau2, a2, b2, s, g = p
#ts = logspace(-10,5,512*2)
dist_gg = self.gg_hw(p=[tau1, a1, b1, s, g], tau=self.dist_tau)
self.dist_y = dist_gg
phi_alpha = self.dist_to_relax(self.time_points, self.dist_tau, self.dist_y).real
epp = delta_eps*2*pi*x*filon(2*pi*x, self.time_points, phi_alpha )
self.step_signal.emit(list(p))
return epp
def show_correlations(cov, ax):
cor = zeros(cov.shape)
for i in range(cor.shape[0]):
for j in range(cor.shape[1]):
if cov[i,i] == 0 or cov[j,j]==0:
cor[i,j] = 0
else:
cor[i,j] = cov[i,j]/sqrt(cov[i,i]*cov[j,j])
ax.matshow(abs(cor), cmap=cm.RdYlGn)
ax.colorbar()