-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLSBoot.py~
executable file
·204 lines (183 loc) · 7.2 KB
/
LLSBoot.py~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
from FitFunctions import *
from multiprocessing import Pool
from MultiWrap import *
import numpy as np
from BootTest import BootStrap1
from scipy.optimize import leastsq
from Params import *
from FitParams import *
from MiscFuns import *
##Fitting Routines##
def LSCreate(Fun):
def LSFun(par,val):
xval = val[:-2]
if len(xval) == 1:
xval = xval[0]
yval = val[-2]
errval = val[-1]
return (Fun(xval,par)-yval)/errval
return LSFun
def LSDerCreate(FunDer):
def LSDerFun(par,val):
xval = val[:-2]
if len(xval) == 1:
xval = xval[0]
yval = val[-2]
errval = val[-1]
return np.transpose(FunDer(xval,par)/errval)
return LSDerFun
def DerOfFun(Fun):
if Fun.__name__ == 'ConstantFitFun':
return ConstFFDer
elif Fun.__name__ == 'LinearFitFun':
return LinFFDer
elif Fun.__name__ == 'C2OneStateFitFun':
return C2OSFFDer
elif Fun.__name__ == 'C3OneStateFitFun':
return C3OSFFDer
elif Fun.__name__ == 'C2TwoStateFitFun':
return C2TSFFDer
elif Fun.__name__ == 'C2TwoStateFitFunCM':
return C2TSFFCMDer
elif Fun.__name__ == 'C3MomTwoStateFitFun':
return C3MomTSFFDer
elif Fun.__name__ == 'TestTwoVarFitFun':
return TestTwoVarFFDer
elif Fun.__name__ == 'FormFactorO1':
return FormFactorO1Der
elif Fun.__name__ == 'FormFactorO2':
return FormFactorO2Der
elif Fun.__name__ == 'FormFactorO3':
return FormFactorO3Der
def GetLSFuns(fitfun,derfun,iGuess,parlen):
if iGuess == None:
iGuess = FitDefGuess(fitfun,Len=parlen)
LSfitfun = LSCreate(fitfun)
if derfun == None:
LSDerfitfun = LSDerCreate(DerOfFun(fitfun))
else:
LSDerfitfun = LSDerCreate(derfun)
# if 'C3TwoStateFitFun' not in fitfun.__name__ :
# LSDerfitfun = LSDerCreate(DerOfFun(fitfun))
# else:
# if derfun == None:
# raise AttributeError('Must Pass Derivative as derfun= for C3FitFun')
# else:
# LSDerfitfun = LSDerCreate(derfun)
return LSfitfun,LSDerfitfun,iGuess
def CreateArgs(xdata,ydata,yerr):
data = []
if isinstance(xdata[0],list) or isinstance(xdata[0], np.ndarray):
for ix in xdata:
data.append(np.array(ix))
else:
data.append(xdata)
data.append(np.array(ydata))
data.append(np.array(yerr))
return data
def LSFit(parlen,xdata,yerr,fitfun,ydata):
iGuess = None
MI = MaxIters
derfun = None
data = CreateArgs(xdata,ydata,yerr)
LSfitfun,LSDerfitfun,iGuess = GetLSFuns(fitfun,derfun,iGuess,parlen)
# print LSDerfitfun.__name__
# print LSfitfun.__name__
# print iGuess
# print data
x,covar, infodict, mesg, ier=leastsq(LSfitfun,iGuess,args=data, Dfun=LSDerfitfun, maxfev=MI, full_output=1)
if float(len(ydata)-len(x)) == 0:
chisqpdf = float('NaN')
else:
chisqpdf=sum(infodict["fvec"]*infodict["fvec"])/float(len(ydata)-len(x))
# if ier != 1:
# print x
# print "WARNING: Optimal parameters not found: " + mesg
# raise ValueError, "Optimal parameters not found: " + mesg
# print x,covar
return x,covar,chisqpdf
def FitBoots(ydatain,xdatain,FitFun,DoW='T',MI=MaxIters,parlen=1,tBooted=False):
GetBootStats(ydatain)
ydataAvg = Pullflag(ydatain,'Avg')
ydatavals = np.rollaxis(Pullflag(ydatain,'values'),1)
fitdata = []
if DoW:
ydataStd = Pullflag(ydatain,'Std')
else:
ydataStd = [1]*len(ydataAvg)
if tBooted:
[fitdataAvg,fitdataAvgErr,fitdataChi] = LSFit(parlen,xdatain[0],ydataStd,FitFun,ydataAvg)
else:
[fitdataAvg,fitdataAvgErr,fitdataChi] = LSFit(parlen,xdatain,ydataStd,FitFun,ydataAvg)
if DoMultiCore:
makeContextFunctions(LSFit)
FitPool = Pool(processes=AnaProc)
if tBooted:
inputdata = [(parlen,ix,ydataStd,FitFun,iy) for ix,iy in zip(xdatain[1:],ydatavals)]
else:
inputdata = [(parlen,xdatain,ydataStd,FitFun,iy) for iy in ydatavals]
fitdatavals = FitPool.map(LSFit.mapper,inputdata)
FitPool.close()
for iy in range(len(fitdatavals[0][0])):
fitdata.append(BootStrap1(nboot,0))
for iboot in range(nboot):
fitdata[iy].values[iboot] = fitdatavals[iboot][0][iy]
else:
fitdatavals = []
for iboot,bootdata in enumerate(ydatavals):
if tBooted:
tempboot = LSFit(parlen,xdatain[iboot+1],ydataStd,FitFun,bootdata)
else:
tempboot = LSFit(parlen,xdatain,ydataStd,FitFun,bootdata)
fitdatavals.append([])
for iy,iyd in enumerate(tempboot[0]):
fitdatavals[iboot].append(iyd)
for iy in range(len(fitdatavals[0])):
fitdata.append(BootStrap1(nboot,0))
for iboot in range(nboot):
fitdata[iy].values[iboot] = fitdatavals[iboot][iy]
GetBootStats(fitdata)
# fitdataChi = CalcChiSqrdPDF(FitFun,fitdataAvg,xdatain,ydataAvg,ydataStd)
return fitdata,fitdataAvg,[fitdataChi]*len(fitdata)
# def FitVarFunBoots(ydatain,xdatain,FunAvg,FunBootList,DoW='T',MI=MaxIters,parlen=1):
# # C3FitFun,C3FitFunDer = FunGen(Pullflag(BootPars,'Avg'),tsvar)
# # BPlist = np.rollaxis(Pullflag(BootPars,'values'),1)
# GetBootStats(ydatain)
# ydataAvg = Pullflag(ydatain,'Avg')
# ydatavals = np.rollaxis(Pullflag(ydatain,'values'),1)
# fitdata = []
# if DoW:
# ydataStd = Pullflag(ydatain,'Std')
# else:
# ydataStd = [1]*len(ydataAvg)
# [fitdataAvg,fitdataAvgErr,fitdataChi] = LSFit(parlen,xdatain,ydataStd,[FunAvg,ydataAvg])
# if DoMultiCore:
# # thisFunWrap = MakeWrap(LSFit,(parlen,xdatain,ydataStd))
# # makeContextFunctionsNo2(thisFunWrap)
# FitPool = Pool(processes=AnaProc)
# # inputargs = [[[FunBootList[iboot][0].mapper,FunBootList[iboot][1].mapper],ydatavals[iboot]] for iboot in range(nboot)]
# inputargs = [[parlen,xdatain,ydataStd,FunBootList[iboot],ydatavals[iboot]] for iboot in range(nboot)]
# # inputargs = [[FunAvg,ydatavals[iboot]] for iboot in range(nboot)]
# # fitdatavals = FitPool.map(thisFunWrap.mapper,inputargs)
# makeContextFunctions(LSFit)
# fitdatavals = FitPool.map(LSFit.mapper,inputargs)
# FitPool.close()
# for iy in range(len(fitdatavals[0][0])):
# fitdata.append(BootStrap1(nboot,0))
# for iboot in range(nboot):
# fitdata[iy].values[iboot] = fitdatavals[iboot][0][iy]
# else:
# fitdatavals = []
# for iboot,bootdata in enumerate(ydatavals):
# tempboot = LSFit(parlen,xdatain,ydataStd,[FunBootList[iboot],bootdata])
# fitdatavals.append([])
# for iy,iyd in enumerate(tempboot[0]):
# fitdatavals[iboot].append(iyd)
# for iy in range(len(fitdatavals[0])):
# fitdata.append(BootStrap1(nboot,0))
# for iboot in range(nboot):
# fitdata[iy].values[iboot] = fitdatavals[iboot][iy]
# GetBootStats(fitdata)
# # fitdataChi = CalcChiSqrdPDF(C3FitFun,fitdataAvg,xdatain,ydataAvg,ydataStd)
# return fitdata,fitdataAvg,[fitdataChi]*len(fitdata)