-
Notifications
You must be signed in to change notification settings - Fork 0
/
Average.py
350 lines (321 loc) · 14.2 KB
/
Average.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
# This file is part of holoaverage.
# Copyright (c) 2018 Tore Niermann
#
# holoaverage is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# holoaverage is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with holoaverage. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import numpy as np
from math import pi, atan2
from scipy.optimize import leastsq
from Series import DataSet
from FFT import aligned_copy
from Grid import ScaleMatrix
from Defocus import calcWaveLength
__all__ = ['HoloAveraging', 'holoAverage']
class HoloAveraging(object):
# Private members
# _grid The grid object
# _rx, _ry Sampling point of real grid
# _qx, _qy Frequencies of reciprocal grid
# _qx2, _qy2 Squared frequencies of reciprocal grid
# _margin Fraction of shape used as margin in comparisons
# _series The original series
# _shift Shifts
# _tilt Tilts
# _defocus Defocus
# _factor Prefactor
# _error Per image error
# _verbose Verbosity
# Available after _prepare
# _sourceF FFT of source images
# _sourceR real space source images
# _waveF Current wavefunction (fourier space)
# _waveR Current wavefunction (real space)
# _globalNorm Ratio between series and factors
def __init__(self, series, defocus=None):
# Test param
if len(series.shape) != 2:
raise ValueError("Series must be 2D.")
# Init Object
self._series = series
self._indexShape = series.indexShape
self._indexSize = series.indexSize
self._grid = series.grid
self._initGrid()
self._initDefocus(defocus)
self._waveLength = calcWaveLength(series.attrs["voltage(kV)"])
self._error = np.zeros(series.indexShape, dtype=float)
self._shift = np.zeros(series.indexShape + (2,), dtype=float)
self._tilt = np.zeros(series.indexShape + (2,), dtype=float)
self._factor = np.ones(series.indexShape, dtype=complex)
self._iteration = 0 # Current iteratiuon
self._convergence = []
self._lock = None
# Public attributes
self.verbose = 0
self.adjustDefocus = True
self.adjustShift = True
self.adjustTilt = True
self.margin = 1.0 / 6.0
def _initGrid(self):
qy, qx = self._grid.getRcprGrid()
self._qy = qy
self._qx = qx
self._qx2 = qx ** 2
self._qy2 = qy ** 2
ry, rx = self._grid.getRealGrid()
self._ry = ry
self._rx = rx
def _initDefocus(self, defocus):
if defocus is None:
defocus = np.zeros(self._indexShape, dtype=float)
elif defocus.shape != self._indexShape:
raise ValueError("defocus array must have same size as series.")
self._defocus = defocus.copy()
def _prepare(self):
self._waveF = np.zeros(self._grid.shape, dtype=self._grid.complexType)
self._sourceR = np.empty(self._indexShape + self._grid.shape, dtype=self._grid.complexType)
self._grid.prepareFFT()
space = self._series.attrs.get("space", +1)
for n in range(self._indexSize):
index = np.unravel_index(n, self._indexShape)
# get FFT
if space >= 0:
self._sourceR[index, ...] = self._series[index].array
else:
self._sourceR[index, ...] = self._grid.backwardFFT(self._series[index].array)
self._factor[index] = np.mean(self._sourceR[index])
self._globalNorm = np.mean(abs(self._factor))
self._factor /= self._globalNorm
# print "Globalnorm:", self._globalNorm
def _averageSingle(self, index):
tmp = self._propagateSingle(self._sourceR[index], self._shift[index], self._defocus[index], self._tilt[index], self._factor[index])
self._waveF += tmp
def _evaluateSingleError(self, index):
tmp = self._backPropagateSingle(self._waveF, self._shift[index], self._defocus[index], self._tilt[index], self._factor[index])
self._error[index] = self._calcError(tmp, self._sourceR[index])
def _dumpCurrentError(self, errorSum):
if self.verbose > 1:
T = self._grid.realSampling
invT = np.linalg.inv(T)
print("Optimizing after iteration %d" % self._iteration)
print("\t[NN] sx[px] sy[px] tx[1/px] ty[1/px] def[nm] Ampl Phase Error")
for n in range(self._indexSize):
index = np.unravel_index(n, self._indexShape)
shift_px = np.dot(invT, self._shift[index + (Ellipsis,)])
tilt_px = np.dot(T, self._tilt[index + (Ellipsis,)])
a = np.sqrt(self._factor[index].real**2 + self._factor[index].imag**2)
p = atan2(self._factor[index].imag, self._factor[index].real)
print("\t[%02d] %6.3f %6.3f %8.5f %8.5f %7.3f %6.4f %+6.3f %8e" % (n, shift_px[1], shift_px[0], tilt_px[1], tilt_px[0], self._defocus[index], a, p, self._error[index]))
if self.verbose > 0:
print("Iteration %3d: total error=%e" % (self._iteration, errorSum))
def _propagateSingleFFT(self, sourceF, shift, defocus, factor):
tmp = aligned_copy(sourceF)
preD = pi * self._waveLength * defocus
preSX = -2.0 * pi * shift[1]
preSY = -2.0 * pi * shift[0]
propX = np.exp(1.0j * (self._qx * preSX + self._qx2 * preD))
propY = np.exp(1.0j * (self._qy * preSY + self._qy2 * preD))
tmp *= propX * propY / factor
return tmp
def _backPropagateSingleFFT(self, sourceF, shift, defocus, factor):
return self._propagateSingleFFT(sourceF, -shift, -defocus, 1.0 / factor)
def _propagateSingle(self, sourceR, shift, defocus, tilt, factor):
tmp = aligned_copy(sourceR)
preTX = 2.0 * pi * tilt[1]
preTY = 2.0 * pi * tilt[0]
facX = np.exp(-1.0j * (self._rx * preTX))
facY = np.exp(-1.0j * (self._ry * preTY))
tmp *= facX * facY
tmp = self._grid.forwardFFT(tmp)
return self._propagateSingleFFT(tmp, shift, defocus, factor)
def _backPropagateSingle(self, sourceF, shift, defocus, tilt, factor):
tmp = self._backPropagateSingleFFT(sourceF, shift, defocus, factor)
tmp = self._grid.backwardFFT(tmp)
preTX = 2.0 * pi * tilt[1]
preTY = 2.0 * pi * tilt[0]
facX = np.exp(+1.0j * (self._rx * preTX))
facY = np.exp(+1.0j * (self._ry * preTY))
tmp *= facX * facY
return tmp
def _calcError(self, model, data, leastsq_compatible=False):
rx = int(model.shape[1] * self.margin)
ry = int(model.shape[0] * self.margin)
sub = (slice(ry, -ry), slice(rx, -rx))
res = model[sub] - data[sub]
if leastsq_compatible:
return abs(res).ravel()
else:
return np.sum(res.real ** 2 + res.imag ** 2)
def _optimizeFunc(self, p, index, optimizeShift, optimizeTilt, optimizeDefocus):
factor = complex(p[0], p[1])
if optimizeShift:
shift = p[2:4]
pIter = 4
else:
shift = self._shift[index]
pIter = 2
if optimizeTilt:
tilt = p[pIter:pIter + 2]
pIter += 2
else:
tilt = self._tilt[index]
if optimizeDefocus:
defocus = p[pIter]
else:
defocus = self._defocus[index]
if np.allclose(tilt, 0):
model = self._backPropagateSingleFFT(self._waveF, shift, defocus, factor)
model = self._grid.backwardFFT(model)
else:
model = self._backPropagateSingle(self._waveF, shift, defocus, tilt, factor)
return self._calcError(model, self._sourceR[index], leastsq_compatible=True)
def _optimizeSingle(self, index, optimizeShift, optimizeTilt, optimizeDefocus):
p0 = (self._factor[index].real, self._factor[index].imag)
if optimizeShift:
p0 = p0 + tuple(self._shift[index])
if optimizeTilt:
p0 = p0 + tuple(self._tilt[index])
if optimizeDefocus:
p0 = p0 + (self._defocus[index],)
pX, ier = leastsq(self._optimizeFunc, p0, args=(index, optimizeShift, optimizeTilt, optimizeDefocus), ftol=1e-4, epsfcn=1e-4)
if ier not in [1, 2, 3, 4]:
raise RuntimeError("Fit did not converge: index=%d" % index)
self._factor[index] = complex(pX[0], pX[1])
if optimizeShift:
self._shift[index, ...] = pX[2:4]
pIter = 4
else:
pIter = 2
if optimizeTilt:
self._tilt[index, ...] = pX[pIter:pIter + 2]
pIter += 2
if optimizeDefocus:
self._defocus[index] = pX[pIter]
def _averageFirst(self):
self._waveF[...] = 0.0
for n in range(self._indexSize):
index = np.unravel_index(n, self._indexShape)
if n > 0:
self._optimizeSingle(index, self.adjustShift, False, False)
self._factor[index] /= abs(self._factor[index])
self._waveF *= n
self._averageSingle(index)
self._waveF /= n + 1
self._waveR = self._grid.backwardFFT(self._waveF.copy())
for n in range(self._indexSize):
index = np.unravel_index(n, self._indexShape)
self._evaluateSingleError(index)
errorSum = np.sum(self._error)
self._dumpCurrentError(errorSum)
return errorSum
def _averageCurrent(self):
self._factor /= np.mean(abs(self._factor)) # Keep average factor at amplitude=1
self._waveF[...] = 0.0
for n in range(self._indexSize):
index = np.unravel_index(n, self._indexShape)
self._averageSingle(index)
self._waveF /= self._indexSize
self._waveR = self._grid.backwardFFT(self._waveF.copy())
for n in range(self._indexSize):
index = np.unravel_index(n, self._indexShape)
self._evaluateSingleError(index)
errorSum = np.sum(self._error)
self._dumpCurrentError(errorSum)
return errorSum
def _showCurrent(self):
import matplotlib.pyplot as plt
tmp = self._waveR
plt.subplot(121)
plt.imshow(abs(tmp))
plt.subplot(122)
plt.imshow(np.arctan2(tmp.imag, tmp.real))
plt.show()
def average(self, iterations):
"""Average series for *iteration* rounds"""
# Prepare
if (self._iteration == 0):
self._prepare()
errorSum = self._averageFirst()
self._convergence.append(errorSum)
while iterations > 0:
if self.verbose > 2:
self._showCurrent()
# Optimize ?
for n in range(self._indexSize):
index = np.unravel_index(n, self._indexShape)
self._optimizeSingle(index, self.adjustShift, self.adjustTilt, (self._iteration >= 4) and self.adjustDefocus)
# Book-keeping
self._iteration += 1
iterations -= 1
errorSum = self._averageCurrent()
self._convergence.append(errorSum)
# Done
def getCurrentAsDataSet(self):
data = DataSet(self._waveR.shape, self._waveR.dtype)
data.array[...] = self._waveR
data.attrs.update(self._series.attrs)
T = ScaleMatrix(2)
T.set(self._grid.realSampling[::-1, ::-1])
data.attrs["dim_offset"] = [0.0, 0.0]
data.attrs["dim_scale"] = T.getCompact(minRank=1)
data.attrs["dim_unit"] = ["nm", "nm"]
data.attrs["space"] = +1
data.attrs["shift(nm)"] = self._shift
data.attrs["tilt(1/nm)"] = self._tilt
data.attrs["defocus(nm)"] = self._defocus
data.attrs["factor"] = self._factor
data.attrs["error"] = self._error
data.attrs["convergence"] = self._convergence
return data
def getCurrent(self):
"""The reconstructed (real space) wave."""
return self._waveR.copy()
def getVariance(self):
var = np.zeros(self._grid.shape, self._grid.floatType)
for n in range(self._indexSize):
index = np.unravel_index(n, self._indexShape)
delta = self._waveR - self._grid.backwardFFT(
self._propagateSingle(self._sourceR[index], self._shift[index], self._defocus[index], self._tilt[index], self._factor[index]))
var += delta.real ** 2 + delta.imag ** 2
return var / (self._indexSize - 1)
def getVarianceAsDataSet(self):
data = DataSet(self._grid.shape, self._grid.floatType)
data.array[...] = self.getVariance()
data.attrs.update(self._series.attrs)
T = ScaleMatrix(2)
T.set(self._grid.realSampling[::-1, ::-1])
data.attrs["dim_offset"] = [0.0, 0.0]
data.attrs["dim_scale"] = T.getCompact(minRank=1)
data.attrs["dim_unit"] = ["nm", "nm"]
data.attrs["space"] = +1
data.attrs["shift(nm)"] = self._shift
data.attrs["tilt(1/nm)"] = self._tilt
data.attrs["defocus(nm)"] = self._defocus
data.attrs["factor"] = self._factor
data.attrs["error"] = self._error
data.attrs["convergence"] = self._convergence
return data
def holoAverage(series, defocus=None, iterations=7, adjustTilt=True, adjustDefocus=True, adjustShift=True, margin=None, verbose=2, variance=False):
averager = HoloAveraging(series, defocus=defocus)
averager.verbose = verbose
averager.adjustDefocus = adjustDefocus
averager.adjustTilt = adjustTilt
averager.adjustShift = adjustShift
if margin is not None:
averager.margin = margin
averager.average(iterations)
if variance:
return averager.getCurrentAsDataSet(), averager.getVarianceAsDataSet()
else:
return averager.getCurrentAsDataSet()