-
Notifications
You must be signed in to change notification settings - Fork 0
/
bartender_fit.py
executable file
·95 lines (58 loc) · 1.53 KB
/
bartender_fit.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
#!/usr/bin/env python
import numpy as np
import scipy.optimize
def hooke(x,eq,k):
return 0.5 * k * (x-eq)**2
def simple_periodic(x,eq,k,n):
return k*(1+np.cos(n*x - eq))
def ryck_belle(x,p0,p1,p2,p3,p4,p5):
cos=np.cos
psi=x-np.pi
return p0+p1*cos(psi) + p1*(cos(psi)**2) + p3*(cos(psi)**3) + p4*(cos(psi)**4) + p4*(cos(psi)**5)
def reb(x,eq,k):
return 0.5 * k * np.power((np.cos(x)-eq), 2.0) * (1 / np.power(np.sin(x), 2))
def cosangle(x,eq,k):
return 0.5 * k * np.power((np.cos(x) - np.cos(eq)), 2)
fname="task.fit"
fin=open(fname,"r")
fittype=fin.readline().rstrip("\n")
td=[]
td.append(fin.readline().replace("]","").replace("[","")) # x
td.append(fin.readline().replace("]","").replace("[","")) # y
td.append(fin.readline()) #initial guess
fin.close()
x=[]
y=[]
param=[]
print("Y QUE WEAAAAAAAA")
xtext=td[0].split()
for i in xtext:
x.append(float(i))
ytext=td[1].split()
for i in ytext:
y.append(float(i))
paramtext=td[2].split()
for i in paramtext:
param.append(float(i))
x = np.array(x)
y = np.array(y)
f = ""
print(fittype)
if fittype=="hooke":
f=hooke
if fittype=="simple_periodic":
f=simple_periodic
if fittype=="ryck_belle":
f=ryck_belle
if fittype=="reb":
f=reb
if fittype=="cosa":
f=cosangle
popt,pcov=scipy.optimize.curve_fit(f,x,y,param)
res = (y - f(x, *popt))**2
rmsd=np.sqrt(res.mean())
foutname=fittype+".out"
fout=open(foutname,"w")
fout.write(str(popt).replace("\n","").replace("[","").replace("]","")+"\n")
fout.write(str(rmsd)+"\n")
fout.close()