-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurvefitter.py
166 lines (126 loc) · 4.51 KB
/
curvefitter.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
from scipy.optimize import curve_fit
import re
class CurveFitter:
def __init__(self):
self.xdata = None
self.ydata = None
self.func = None
self.nparam = None
self.varnames = None
self.p0 = None
def set_data(self, xdata, ydata):
if len(xdata) != len(ydata):
raise ValueError(" ")
self.xdata = xdata
self.ydata = ydata
def set_func(self, func):
if not hasattr(func, '__call__'):
raise TypeError('Should be a function')
self.nparam = func.func_code.co_argcount - 1
if self.nparam < 1:
raise ValueError('The function should have at least one parameter to fit')
self.func = func
# variable names, we do not care about the first one (x-variable)
self.varnames = list(func.func_code.co_varnames)[1:]
def set_p0(self, p0):
if self.func is None:
raise AttributeError('The function should first be set')
if len(p0) != self.nparam:
raise ValueError('The number of initial values is not equal to the number of parameters')
self.p0 = p0
def fit(self):
if None in (self.xdata, self.ydata, self.func):
raise ValueError('Not all requirements are set')
popt, pcov = curve_fit(self.func, self.xdata, self.ydata, self.p0)
popt_dict = dict(zip(self.varnames, popt))
return popt_dict
def parse(equation):
from pyparsing import Word, Literal, CaselessLiteral, Combine
from pyparsing import Optional, Forward, ZeroOrMore, StringEnd
from pyparsing import nums, alphas, alphanums
# grammar of equation
point = Literal('.')
e = CaselessLiteral('E')
plusorminus = Literal('+') | Literal('-')
number = Word(nums)
integer = Combine(Optional(plusorminus) + number)
floatnumber = Combine(integer + Optional(point + Optional(number)) +
Optional(e + integer))
ident = Word(alphas, alphanums + '_')
plus = Literal('+')
minus = Literal('-')
mult = Literal('*')
div = Literal('/')
expop = Literal('^')
lpar = Literal('(').suppress()
rpar = Literal(')').suppress()
addop = plus | minus
multop = mult | div
# functions
sin = Literal('sin')
cos = Literal('cos')
exp = Literal('exp')
ln = Literal('ln')
log = Literal('log')
functions = sin | cos | exp | ln | log
expr = Forward()
atom = (functions + lpar + expr + rpar)|\
(e | floatnumber | integer | ident) |\
(lpar + expr + rpar)
factor = Forward()
factor << atom + ZeroOrMore(expop + factor)
term = factor + ZeroOrMore(multop + factor)
expr << term + ZeroOrMore(addop + term)
pattern = expr + StringEnd()
variables = []
l = pattern.parseString(equation)
functions = ('sin', 'cos', 'exp', 'ln', 'log')
for op in l:
if re.search('^[a-zA-Z][a-zA-Z0-9_]*$', op):
if op not in functions:
variables.append(op)
variables = set(variables)
from keyword import iskeyword
for var in variables:
if iskeyword(var):
raise ValueError('You are using one of the forbidden words, ASSHOLE')
return variables
def string_to_func(expression, parameters, xvariable):
expression = expression.replace('^', '**')
# make a proper python function definition
func_string = "def myfunc(" + xvariable
for parameter in parameters:
func_string += ', '
func_string += parameter
func_string += '):\n'
func_string += ' from numpy import log as ln, log10 as log, sin, cos, exp\n'
func_string += ' return ' + expression
# build the code object and execute it
exec(compile(func_string, 'myfunc.py', 'exec'))
return myfunc
def main():
# input data
xdata = [1, 2, 3, 4, 5,]
ydata = [1.9, 8.2, 18.9, 32.5, 50]
expression = 'a*x^2'
xvariable = 'x'
#=============================
parameters = parse(expression)
if xvariable not in parameters:
raise ValueError('There is no x-variable in your expression')
else:
parameters.remove(xvariable)
my_func = string_to_func(expression, parameters, xvariable)
cf = CurveFitter()
cf.set_data(xdata, ydata)
cf.set_func(my_func)
popt = cf.fit()
print popt
#y_fit_data = [my_func(x, popt[0]) for x in xdata]
#fig = plt.figure()
#axes = fig.add_subplot(111)
#axes.scatter(xdata, ydata)
#axes.plot(xdata, y_fit_data)
#plt.show()
if __name__=='__main__':
main()