-
Notifications
You must be signed in to change notification settings - Fork 4
/
solver.py
249 lines (216 loc) · 7.82 KB
/
solver.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
import numpy as np
import pdb,os
from scipy.optimize import fmin_slsqp,least_squares
import astropy.io.fits as pyfits
import scipy.interpolate as interp
import utilities.transformations as tr
def ampMeritFunction(voltages,distortion,ifuncs):
"""Simple merit function calculator.
voltages is 1D array of weights for the influence functions
distortion is 2D array of distortion map
ifuncs is 4D array of influence functions
shade is 2D array shade mask
Simply compute sum(ifuncs*voltages-distortion)**2)
"""
#Numpy way
r = np.dot(ifuncs,voltages)-distortion
res = np.mean((np.dot(ifuncs,voltages)-distortion)**2)
return res
def ampMeritFunction2(voltages,**kwargs):
"""Simple merit function calculator.
voltages is 1D array of weights for the influence functions
distortion is 2D array of distortion map
ifuncs is 4D array of influence functions
shade is 2D array shade mask
Simply compute sum(ifuncs*voltages-distortion)**2)
"""
#Numpy way
distortion = kwargs['inp'][0]
ifuncs = kwargs['inp'][1]
res = np.mean((np.dot(ifuncs,voltages)-distortion)**2)
return res, [], 0
def ampMeritDerivative(voltages,distortion,ifuncs):
"""Compute derivatives with respect to voltages of
simple RMS()**2 merit function
"""
res = np.dot(2*(np.dot(ifuncs,voltages)-distortion),ifuncs)/\
np.size(distortion)
return res
def ampMeritDerivative2(voltages,f,g,**kwargs):
"""Compute derivatives with respect to voltages of
simple RMS()**2 merit function
"""
distortion = kwargs['inp'][0]
ifuncs = kwargs['inp'][1]
res = np.dot(2*(np.dot(ifuncs,voltages)-distortion),ifuncs)/\
np.size(distortion)
return res.tolist(), [], 0
def rawOptimizer(ifs,dist,bounds=None,smin=0.,smax=5.):
"""Assumes ifs and dist are both in slope or amplitude space.
No conversion to slope will occur."""
#Create bounds list
if bounds is None:
bounds = []
for i in range(np.shape(ifs)[0]):
bounds.append((smin,smax))
#Get ifs in right format
ifs = ifs.transpose(1,2,0) #Last index is cell number
#Reshape ifs and distortion
sh = np.shape(ifs)
ifsFlat = ifs.reshape(sh[0]*sh[1],sh[2])
distFlat = dist.flatten()
#Call optimizer algoritim
optv = fmin_slsqp(ampMeritFunction,np.zeros(sh[2]),\
bounds=bounds,args=(distFlat,ifsFlat),\
iprint=2,fprime=ampMeritDerivative,iter=200,\
acc=1.e-10)
#Reconstruct solution
sol = np.dot(ifs,optv)
return sol,optv
def prepareIFs(ifs,dx=None,azweight=.015):
"""
Put IF arrays in format required by optimizer.
If dx is not None, apply derivative.
"""
#Apply derivative if necessary
#First element of result is axial derivative
if dx is not None:
ifs = np.array(np.gradient(ifs,*dx,axis=(1,2)))*180/np.pi*60.**2 / 1000.
ifs[1] = ifs[1]*azweight
ifs = ifs.transpose(1,0,2,3)
sha = np.shape(ifs)
for i in range(sha[0]):
for j in range(sha[1]):
ifs[i,j] = ifs[i,j] - np.nanmean(ifs[i,j])
ifs = ifs.reshape((sha[0],sha[1]*sha[2]*sha[3]))
else:
#ifs = ifs.transpose(1,2,0)
sha = np.shape(ifs)
for i in range(sha[0]):
ifs[i] = ifs[i] - np.nanmean(ifs[i])
ifs = ifs.reshape((sha[0],sha[1]*sha[2]))
return np.transpose(ifs)
def prepareDist(d,dx=None,azweight=.015):
"""
Put distortion array in format required by optimizer.
If dx is not None, apply derivative.
Can also be run on shademasks
"""
#Apply derivative if necessary
#First element of result is axial derivative
if dx is not None:
d = np.array(np.gradient(d,*dx))*180/np.pi*60.**2 / 1000.
d[0] = d[0] - np.nanmean(d[0])
d[1] = d[1] - np.nanmean(d[1])
d[1] = d[1]*azweight
return d.flatten()
def optimizer(distortion,ifs,shade,smin=0.,smax=5.,bounds=None,compare=False):
"""
Cleaner implementation of optimizer. ifs and distortion should
already be in whatever form (amplitude or slope) desired.
IFs should have had prepareIFs already run on them.
Units should be identical between the two.
"""
#Load in data
if type(distortion)==str:
distortion = pyfits.getdata(distortion)
if type(ifs)==str:
ifs = pyfits.getdata(ifs)
if type(shade)==str:
shade = pyfits.getdata(shade)
#Remove shademask
ifs = ifs[shade==1]
distortion = distortion[shade==1]
#Remove nans
ind = ~np.isnan(distortion)
ifs = ifs[ind]
distortion = distortion[ind]
if compare is True:
#Output arrays as fits to be compared using MATLAB
return ifs,distortion
#Handle bounds
if bounds is None:
bounds = []
for i in range(np.shape(ifs)[1]):
bounds.append((smin,smax))
#Call optimizer algorithm
optv = fmin_slsqp(ampMeritFunction,np.zeros(np.shape(ifs)[1]),\
bounds=bounds,args=(distortion,ifs),\
iprint=1,fprime=ampMeritDerivative,iter=200,\
acc=1.e-6)
return optv
def correctDistortion(dist,ifs,shade,dx=None,azweight=.015,smax=5.,\
bounds=None,compare=False):
"""
Wrapper function to apply and evaluate a correction
on distortion data.
Distortion and IFs are assumed to already be on the
same grid size.
dx should be in mm, dist and ifs should be in microns
"""
#Make sure shapes are correct
if not (np.shape(dist)==np.shape(ifs[0])==np.shape(shade)):
print 'Unequal shapes!'
return None
#Prepare arrays
distp = prepareDist(dist,dx=dx,azweight=azweight)
ifsp = prepareIFs(ifs,dx=dx,azweight=azweight)
shadep = prepareDist(shade)
#Run optimizer
res = optimizer(-distp,ifsp,shadep,smax=smax,bounds=bounds,compare=compare)
return res
def convertFEAInfluence(filename,Nx,Ny,method='cubic',\
cylcoords=True):
"""Read in Vanessa's CSV file for AXRO mirror
Mirror no longer assumed to be cylinder.
Need to regrid initial and perturbed nodes onto regular grid,
then compute radial difference.
"""
#Load FEA data
d = np.transpose(np.genfromtxt(filename,skip_header=1,delimiter=','))
if cylcoords is True:
r0 = d[1]*1e3
rm = np.mean(r0)
t0 = d[2]*np.pi/180. * rm #Convert to arc length in mm
z0 = d[3]*1e3
#r0 = np.repeat(220.497,len(t0))
r = r0 + d[4]*1e3
t = (d[2] + d[5])*np.pi/180. * rm #Convert to arc length in mm
z = z0 + d[6]*1e3
else:
x0 = d[2]*1e3
y0 = d[3]*1e3
z0 = d[4]*1e3
x = x0 + d[5]*1e3
y = y0 + d[6]*1e3
z = z0 + d[7]*1e3
#Convert to cylindrical
t0 = np.arctan2(x0,-z0)*220.497 #Convert to arc length in mm
r0 = np.sqrt(x0**2+z0**2)
z0 = y0
t = np.arctan2(x,-z)*220.497
r = np.sqrt(x**2+z**2)
z = y
#Construct regular grid
gy = np.linspace(z0.min(),z0.max(),Nx+2)
gx = np.linspace(t0.min(),t0.max(),Ny+2)
gx,gy = np.meshgrid(gx,gy)
#Run interpolation
g0 = interp.griddata((z0,t0),r0,(gy,gx),method=method)
g0[np.isnan(g0)] = 0.
g = interp.griddata((z,t),r,(gy,gx),method=method)
g[np.isnan(g)] = 0.
print filename + ' done'
return -(g0[1:-1,1:-1]-g[1:-1,1:-1]),g0[1:-1,1:-1],g[1:-1,1:-1]
def createShadePerimeter(sh,axialFraction=0.,azFraction=0.):
"""
Create a shademask where a fraction of the axial and
azimuthal perimeter is blocked.
Fraction is the fraction of blockage in each axis.
sh is shape tuple e.g. (200,200)
"""
arr = np.zeros(sh)
axIndex = int(round(sh[0]*axialFraction/2))
azIndex = int(round(sh[1]*azFraction/2))
arr[axIndex:-axIndex,azIndex:-azIndex] = 1.
return arr