-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize.py
66 lines (50 loc) · 2.06 KB
/
optimize.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
import numpy as np
import scipy.optimize as spo
import scipy as sp
debug_psi , debug_data = 0,0
def tdoa3(rawData, rowsToKeep="ALL",**kwargs):
"""
this function estimates the origin of a range expansion.
The input is a np.array where each row corresponds to a pairwise psi.
The first two columns are x and y coordiante of the the first sample.
The 3rd and 4th columns are x and y coordiante of the the other sample.
the 5th column is the directionality index.
Returns:
the function returns the estimated parameter (x,y,v) and the mean squared error.
"""
def model(data,pars):
v,x,y = pars
ix,iy,jx,jy = data.transpose().tolist()
return 1/v * (sp.sqrt( (ix -x)**2 + (iy-y)**2 ) - \
sp.sqrt( (jx -x)**2 + (jy-y)**2 ))
def jacobian(data,pars):
v,x,y = pars
ix,iy,jx,jy = data.transpose().tolist()
dfdv= 1/v**2 * (sp.sqrt((jx-x)**2+(jy-y)**2) -
sp.sqrt((ix-x)**2+(iy-y)**2) )
dfdx= 1/v * ( (x-ix) / sp.sqrt((ix-x)**2+(iy-y)**2) + \
(jx-x) / sp.sqrt((jx-x)**2+(jy-y)**2) )
dfdy= 1/v * ( (y-iy) / sp.sqrt((ix-x)**2+(iy-y)**2) + \
(jy-y) / sp.sqrt((jx-x)**2+(jy-y)**2) )
return dfdv,dfdx,dfdy
def makeErrorFunction(psi,data):
def errorFunction(pars):
return psi-model(data,pars)
return errorFunction
def makeJacobianFunction(data):
def jacobianFunction(pars):
return jacobian(data,pars)
# rawData = np.loadtxt(path)
if rowsToKeep=="ALL":
rowsToKeep = range(len(rawData))
print rowsToKeep
if len(rowsToKeep) < 4:
raise ValueError("Too little data")
psi = rawData[:,4]
data = rawData[:,:4]
debug_psi, debug_data = psi, data
f = makeErrorFunction(psi,data)
#estimates = spo.leastsq(f,full_output=1,Dfun=makeJacobianFunction(data),**kwargs)
estimates = spo.leastsq(f,full_output=1,**kwargs)
mse=sum(estimates[2]['fvec']**2)
return estimates, mse,psi, data