forked from pjreddie/WRed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitting_functions.py
464 lines (353 loc) · 17.5 KB
/
fitting_functions.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import os, copy
import numpy as N
import simplejson
from collections import deque
class FunctionParamGroup(object):
def __init__(self, paramNames=None):
self.params = {}
if paramNames is None:
paramNames = []
for paramName in paramNames:
self.params.update({ paramName: FunctionParam(paramName) })
def get(self, paramName):
return self.params[paramName].value
def set(self, paramName, value):
self.params[paramName].value = value
def asArray(self):
return self.params.values()
def __repr__(self):
return str(self.asArray())
class FunctionParam(object):
def __init__(self, paramName = '', initValue=0):
self.paramName = paramName
self.value = initValue
self.error = 0
self.upperLimit = 0
self.lowerLimit = 0
def __repr__(self):
return str(self.getJSON())
def getJSON(self):
return { 'paramName': self.paramName, 'value': self.value, 'error': self.error, 'upperLimit': self.upperLimit, 'lowerLimit': self.lowerLimit }
class FunctionGroup(object):
def __init__(self):
self.functions = []
self.data = {}
def __repr__(self):
foo = "["
for function in self.functions:
foo += function.__repr__() + ", "
return foo + "]"
def foo(self):
f1 = Linear()
f2 = Linear()
f1.functionParams.params = { 'X1': FunctionParam('X1', 0), 'X2': FunctionParam('X2', 1),
'Y1': FunctionParam('Y1', 2), 'Y2': FunctionParam('Y2', 2) }
f2.functionParams.params = { 'X1': FunctionParam('X1', 0), 'X2': FunctionParam('X2', 1),
'Y1': FunctionParam('Y1', 3), 'Y2': FunctionParam('Y2', 3) }
self.functions = [f1, f2]
def getValueAtX(self, X):
functionYs = []
for function in self.functions:
value = function.function(X, None)
if value:
functionYs.append(value)
return sum(functionYs)
def function(self, Domain, Range):
functionRanges = []
for function in self.functions:
functionRanges.append(function.function(Domain, Range))
functionRange = sum(N.array(functionRanges))
return functionRange
def chisq(self, xData, yData, yErr):
yCalc = self.function(xData, yData)
yErr_temp = copy.deepcopy(yErr)
# zero_loc = N.where(yErr == 0)[0]
# if len(zero_loc) != 0:
# yErr_temp[zero_loc] = 1.0
chi = ((yData - yCalc) / yErr_temp) ** 2
return chi.sum() / (len(yData) - self.countFunctionParams())
def countFunctionParams(self):
count = 0
for function in self.functions:
count += len(function.functionParams.params)
return count
def createFunction(self, xData, yData):
functionDomain = N.arange(min(xData), max(xData), abs(max(xData) - min(xData)) / 200.)
functionDataRange = N.arange(min(yData), max(yData), abs(max(yData) - min(yData)) / 200.)
print self
functionRanges = []
functionYs = []
functionInfos = []
for function in self.functions:
functionRanges.append(function.function(functionDomain, functionDataRange))
functionYs.append(function.function(xData, yData))
functionInfos.append(function.getJSON())
functionRange = sum(N.array(functionRanges))
functionData = zip_(functionDomain, functionRange)
functionY = sum(N.array(functionYs))
functionResiduals = N.subtract(functionY, yData)
functionResidualsScaled = N.divide(functionResiduals, self.countFunctionParams())
#functionResidualData = zip_(xData, functionResiduals)
functionResidualData = zip_(xData, functionResidualsScaled)
print
print functionRange
print functionY
print
print functionData
print functionResidualData
print
print '--------'
print
functionObj = dict(fit=functionData, resid=functionResidualData, functionInfos=functionInfos)
return functionObj
def defPoint(self, request, functionIndex=-1):
xPos = float(request.POST['xPos'])
yPos = float(request.POST['yPos'])
# We need to subtract the value from rest of the functions
if len(self.functions) > 1:
yPos -= self.getValueAtX(xPos)
request.session[request.POST['xID']] = xPos
request.session[request.POST['yID']] = yPos
# Update function at specified index, or else most recent function (functionIndex is -1)
self.functions[functionIndex].setFunctionParamsFromRequest(request)
return request
def getFunctionsParamsAsArray(self):
functionsParamsArray = []
functionsParamsArraySlices = []
for function in self.functions:
functionParamsArray = function.getFunctionParamsAsArray()
functionsParamsArray.extend(functionParamsArray)
functionsParamsArraySlices.append(len(functionParamsArray))
return (functionsParamsArray, functionsParamsArraySlices)
def setFunctionsParamsFromArray(self, params, slices):
pointer = 0
counter = 0
for counter in range(len(slices)):
functionParamsArray = params[pointer : pointer + slices[counter]]
self.functions[counter].setFunctionParamsFromArray(functionParamsArray)
pointer += slices[counter]
counter += 1
def getFunctionsParamsFromArray(self, params, slices):
functionsParams = []
pointer = 0
counter = 0
for counter in range(len(slices)):
functionParamsArray = params[pointer : pointer + slices[counter]]
functionsParams.append(self.functions[counter].getFunctionParamsFromArray(functionParamsArray))
pointer += slices[counter]
counter += 1
return functionsParams
def getFitFunctionInfos(self, mpfitResult):
params, slices = self.getFunctionsParamsAsArray()
fitFunctionInfos = []
pointer = 0
counter = 0
for function in self.functions:
mpfitFunctionResult = dict(params=mpfitResult.params[pointer : pointer + slices[counter]],
perror=mpfitResult.perror[pointer : pointer + slices[counter]])
# Map sigfigs
fitFunctionParams = function.getFunctionParamsFromArray([sigfig(x, 6) for x in mpfitFunctionResult['params']])
fitFunctionParamsErr = function.getFunctionParamsFromArray([sigfig(x, 2) for x in mpfitFunctionResult['perror']])
fitFunctionParamsArray = paramsJoin(fitFunctionParams, fitFunctionParamsErr)
fitFunctionInfo = { 'fitFunctionParams': fitFunctionParams, 'fitFunctionParamsErr': fitFunctionParamsErr,
'fitFunctionParamsArray': fitFunctionParamsArray }
fitFunctionInfo.update(function.getJSON())
fitFunctionInfos.append(fitFunctionInfo)
pointer += slices[counter]
counter += 1
print fitFunctionInfos
print '*****'
print
return fitFunctionInfos
class Function(object):
def __init__(self, functionID=0):
self.functionID = functionID
self.functionName = ''
self.fitInstructions = deque()
self.functionParams = FunctionParamGroup()
def __repr__(self):
return str(self.getJSON())
def function(self, Domain, Range):
return None
def getJSON(self):
return { 'functionID': self.functionID, 'functionName': self.functionName, 'functionParams': self.functionParams.asArray() }
def setFunctionParamsFromRequest(self, request):
for (paramName, value) in self.functionParams.params.items():
if request.session.has_key(paramName):
self.functionParams.set(paramName, request.session[paramName])
def setFunctionParamsFromDict(self, functionParamsDict):
for (paramName, value) in self.functionParams.params.items():
if functionParamsDict.has_key(paramName):
self.functionParams.set(paramName, functionParamsDict[paramName])
def setFunctionParamsFromArrayOfDicts(self, functionParamsArrayOfDicts):
for functionParam in functionParamsArrayOfDicts:
if self.functionParams.params.has_key(functionParam['paramName']):
self.functionParams.set(functionParam['paramName'], functionParam['value'])
def setFunctionParamsFromArray(self, functionParamsArray):
functionParamsDict = self.getFunctionParamsFromArray(functionParamsArray)
self.setFunctionParamsFromDict(functionParamsDict)
def getFunctionParamsAsArray(self):
pass
def getFunctionParamsFromArray(self, params):
pass
def createFunction(self, xData, yData):
functionDomain = N.arange(min(xData), max(xData), abs(max(xData) - min(xData)) / 200.)
functionDataRange = N.arange(min(yData), max(yData), abs(max(yData) - min(yData)) / 200.)
functionRange = self.function(functionDomain, functionDataRange)
functionData = zip_(functionDomain, functionRange)
functionY = self.function(xData, yData)
functionResiduals = N.subtract(functionY, yData)
functionResidualData = zip_(xData, functionResiduals)
print
print functionRange
print functionY
print
print '--------'
print
functionObj = dict(fit=functionData, resid=functionResidualData,
functionID=self.functionID, functionName=self.functionName, functionParams=self.functionParams)
return functionObj
def chisq(self, xData, yData, yErr):
yCalc = self.function(xData, yData)
yErr_temp = copy.deepcopy(yErr)
# zero_loc = N.where(yErr == 0)[0]
# if len(zero_loc) != 0:
# yErr_temp[zero_loc] = 1.0
chi = ((yData - yCalc) / yErr_temp) ** 2
return chi.sum() / (len(yData) - len(self.functionParams))
# LINEAR
class Linear(Function):
def __init__(self):
super(Linear, self).__init__()
self.functionID = 1
self.functionName = 'Linear'
self.fitInstructions = deque([
{ 'dataType': 'askPoint', 'xID': 'X1', 'yID': 'Y1',
'messageTitle': 'Step 1', 'messageText': 'Please click on the first point' },
{ 'dataType': 'askPoint', 'xID': 'X2', 'yID': 'Y2',
'messageTitle': 'Step 2', 'messageText': 'Please click on the second point' }
])
self.functionParams = FunctionParamGroup([ 'slope', 'yInter' ])
def function(self, Domain, Range):
(slope, yInter) = (self.functionParams.get('slope'), self.functionParams.get('yInter'))
if slope is None or yInter is None:
pass
else:
return N.multiply(slope, Domain) + yInter
def getFunctionParamsAsArray(self):
return [ self.functionParams.get('slope'), self.functionParams.get('yInter') ]
def getFunctionParamsFromArray(self, params):
return { 'slope': params[0], 'yInter': params[1] }
def setFunctionParamsFromRequest(self, request):
super(Linear, self).setFunctionParamsFromRequest(request)
if request.session.has_key('Y2'):
self.setFunctionParamsFromTwoPoints(request.session)
def setFunctionParamsFromTwoPoints(self, twoPoints):
slope = N.divide(twoPoints['Y2'] - twoPoints['Y1'], twoPoints['X2'] - twoPoints['X1'])
yInter = twoPoints['Y1'] - twoPoints['X1'] * slope
self.functionParams.set('slope', slope)
self.functionParams.set('yInter', yInter)
class LinearDrag(Linear):
def __init__(self):
super(LinearDrag, self).__init__()
self.functionID = 2
self.functionName = 'Linear drag'
self.fitInstructions = deque([
{ 'dataType': 'askDrag', 'xIDstart': 'X1', 'yIDstart': 'Y1', 'xIDend': 'X2', 'yIDend': 'Y2',
'messageTitle': 'Step 1', 'messageText': 'Please drag from the first point to the second point' }
])
# GAUSSIAN
class Gaussian(Function):
def __init__(self):
super(Gaussian, self).__init__()
self.functionID = 11
self.functionName = 'Gaussian'
self.fitInstructions = deque([
{ 'dataType': 'askPoint', 'xID': 'peakX', 'yID': 'peakY',
'messageTitle': 'Step 1', 'messageText': 'Please click on the peak of the data' },
{ 'dataType': 'askPoint', 'xID': 'widthX', 'yID': 'widthY',
'messageTitle': 'Step 2', 'messageText': 'Please click on the width of the data' }
])
self.functionParams = FunctionParamGroup([ 'peakX', 'peakY', 'FWHM' ])
def function(self, Domain, Range):
(peakX, peakY, FWHM) = (self.functionParams.get('peakX'), self.functionParams.get('peakY'), self.functionParams.get('FWHM'))
bkgdY = 0
if peakX is None or FWHM is None:
pass
else:
stdDev = FWHM / 2 / N.sqrt(2 * N.log(2))
return bkgdY + (peakY - bkgdY) * N.exp(- N.power(N.subtract(Domain, peakX), 2) / 2 / N.power(stdDev, 2))
def getFunctionParamsAsArray(self):
return [ self.functionParams.get('peakX'), self.functionParams.get('peakY'),
self.functionParams.get('FWHM') ]
def getFunctionParamsFromArray(self, params):
return { 'peakX': params[0], 'peakY': params[1], 'FWHM': params[2] }
def setFunctionParamsFromRequest(self, request):
super(Gaussian, self).setFunctionParamsFromRequest(request)
if request.session.has_key('widthX'):
FWHM = 2 * N.abs(request.session['widthX'] - request.session['peakX'])
self.functionParams.set('FWHM', FWHM)
class GaussianDrag(Gaussian):
def __init__(self):
super(GaussianDrag, self).__init__()
self.functionID = 12
self.functionName = 'Gaussian drag'
self.fitInstructions = deque([
{ 'dataType': 'askPoint', 'xID': 'peakX', 'yID': 'peakY',
'messageTitle': 'Step 1', 'messageText': 'Please click on the peak of the data' },
{ 'dataType': 'askDrag', 'xIDstart': 'widthYst', 'yIDstart': 'widthYst', 'xIDend': 'widthX', 'yIDend': 'widthY',
'messageTitle': 'Step 2', 'messageText': 'Please drag on the width of the data' }
])
# LORENTZIAN
class Lorentzian(Function):
def __init__(self):
super(Lorentzian, self).__init__()
self.functionID = 21
self.functionName = 'Lorentzian'
self.fitInstructions = deque([
{ 'dataType': 'askPoint', 'xID': 'peakX', 'yID': 'peakY',
'messageTitle': 'Step 1', 'messageText': 'Please click on the peak of the data' },
{ 'dataType': 'askPoint', 'xID': 'widthX', 'yID': 'widthY',
'messageTitle': 'Step 2', 'messageText': 'Please click on the width of the data' }
])
self.functionParams = FunctionParamGroup([ 'peakX', 'peakY', 'FWHM' ])
def function(self, Domain, Range):
(peakX, peakY, FWHM) = (self.functionParams.get('peakX'), self.functionParams.get('peakY'), self.functionParams.get('FWHM'))
bkgdY = 0
if peakX is None or FWHM is None:
pass
else:
gamma = FWHM
return bkgdY + (peakY - bkgdY) * N.divide(N.power(gamma, 2), N.power(N.subtract(Domain, peakX), 2) + N.power(gamma, 2))
def getFunctionParamsAsArray(self):
return [ self.functionParams.get('peakX'), self.functionParams.get('peakY'), self.functionParams.get('FWHM') ]
def getFunctionParamsFromArray(self, params):
return { 'peakX': params[0], 'peakY': params[1], 'FWHM': params[2] }
def setFunctionParamsFromRequest(self, request):
super(Lorentzian, self).setFunctionParamsFromRequest(request)
if request.session.has_key('widthX'):
FWHM = N.abs(request.session['widthX'] - request.session['peakX'])
self.functionParams.set('FWHM', FWHM)
class LorentzianDrag(Lorentzian):
def __init__(self):
super(LorentzianDrag, self).__init__()
self.functionID = 22
self.functionName = 'Lorentzian drag'
self.fitInstructions = deque([
{ 'dataType': 'askPoint', 'xID': 'peakX', 'yID': 'peakY',
'messageTitle': 'Step 1', 'messageText': 'Please click on the peak of the data' },
{ 'dataType': 'askDrag', 'xIDstart': 'widthYst', 'yIDstart': 'widthYst', 'xIDend': 'widthX', 'yIDend': 'widthY',
'messageTitle': 'Step 2', 'messageText': 'Please drag on the width of the data' }
])
def zip_(l1, l2):
return [list(elem) for elem in zip(l1, l2)]
def paramsJoin(d1, d2):
"""Joins each parameter with its error and name as a dict"""
n = []
for (key, value) in d1.items():
n.append({ 'name': key, 'value': value, 'err': d2[key] })
return n
def sigfig(x, n=6):
if n < 1:
raise ValueError("number of significant digits must be >= 1")
return "%.*e" % (n - 1, x)