-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube_coalign_dkist.py
300 lines (236 loc) · 10.5 KB
/
cube_coalign_dkist.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
# still in development, not ready for use
import sunpy
import sunpy.map
from sunpy.map import Map, MapSequence
from sunpy.util.exceptions import SunpyUserWarning
import numpy as np
import warnings
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox, Button, CheckButtons
import matplotlib.patches as patches
import matplotlib.animation as animation
from skimage.feature import match_template
from scipy.ndimage import shift
import astropy.units as u
from astropy.visualization import (ImageNormalize, AsinhStretch)
from astropy.time import Time
from datetime import datetime
import dkist
from ndcube import NDCube
from dask.diagnostics import ProgressBar
from glob import glob
from copy import deepcopy
import os
import h5py
import argparse
from IPython.display import HTML, display
class DatasetCoalign(dkist.Dataset):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@classmethod
def from_dkist_ds(cls, parent):
return cls(parent.data, parent.wcs, parent.uncertainty,
parent.mask, parent.meta, parent.unit)
def flicker(self, index=0, fps = 5):
date_avg = Time(self.headers['DATE-AVG'])
if isinstance(index, int):
pass
elif isinstance(index, str):
time_select = Time(index)
index = np.argmin(np.abs(date_avg - time_select))
elif isinstance(index, Time):
index = np.argmin(np.abs(date_avg - index))
elif isinstance(index, datetime):
index = np.argmin(np.abs(date_avg - Time(index)))
if index < 0 or index >= self.dimensions[0].value - 1:
raise ValueError('Index out of bounds')
data_1 = self[index].data.compute()
data_2 = self[index+1].data.compute()
norm_1 = ImageNormalize(vmin=np.nanpercentile(data_1, 0.1),
vmax=np.nanpercentile(data_1, 99.9),
stretch=AsinhStretch())
norm_2 = ImageNormalize(vmin=np.nanpercentile(data_2, 0.1),
vmax=np.nanpercentile(data_2, 99.9),
stretch=AsinhStretch())
fig, ax = plt.subplots(figsize=(10,10),layout='constrained')
im = ax.imshow(data_1, norm=norm_1, cmap='gray', origin='lower')
plt.axis('off')
def init_plot():
return im,
def update_plot(ii):
if ii == 0:
im.set_array(data_1)
im.set_norm(norm_1)
else:
im.set_array(data_2)
im.set_norm(norm_2)
return im,
anim = animation.FuncAnimation(fig, update_plot, interval=1000/fps, blit=True,frames=2,
repeat=True, init_func=init_plot)
if matplotlib.get_backend().lower() == 'module://matplotlib_inline.backend_inline':
anim_html = HTML(anim.to_jshtml())
fig.clf()
plt.close()
display(anim_html)
else:
plt.show()
def self_coalign_single(self, single_threaded=True):
self.data_rechunk = self.data[:,100:-100,100:-100].rechunk((2, self.data.chunksize[1], self.data.chunksize[2]))
if single_threaded:
with ProgressBar():
self.shifts = self.data_rechunk.map_overlap(_self_calculate_shift,
depth=(1,0,0), # We only want to cross-correlate in the time dimension
boundary='reflect',
chunks=(3,1), # To match depth = 1, so the overlap chunksize is chuncksize + 2*depth
drop_axis=(1,2), # Drop the spatial dimensions
new_axis=1, # Add a new axis to store the y- and x-shifts
dtype='float64').compute(scheduler='single-threaded')
else:
self.shifts = self.data_rechunk.map_overlap(_self_calculate_shift,
depth=(1,0,0), # We only want to cross-correlate in the time dimension
boundary='reflect',
chunks=(3,1), # To match depth = 1, so the overlap chunksize is chuncksize + 2*depth
drop_axis=(1,2), # Drop the spatial dimensions
new_axis=1, # Add a new axis to store the y- and x-shifts
dtype='float64').compute()
def _self_calculate_shift(x):
template = x[0,974:2922,974:2922]
this_layer = x[1,:,:]
yshift_pixel, xshift_pixel = _calculate_shift(this_layer, template)
#We need to deceive the map_overlap function to let it return the x and y shifts
#at the correct position in the array
return np.array([[0,yshift_pixel.value,0], [0,xshift_pixel.value,0]]).T
# Functions copied from sunpy.image.coalignment version 0.5.1
def _calculate_shift(this_layer, template):
"""
Calculates the pixel shift required to put the template in the "best"
position on a layer.
Parameters
----------
this_layer : `numpy.ndarray`
A numpy array of size ``(ny, nx)``, where the first two dimensions are
spatial dimensions.
template : `numpy.ndarray`
A numpy array of size ``(N, M)`` where ``N < ny`` and ``M < nx``.
Returns
-------
`tuple`
Pixel shifts ``(yshift, xshift)`` relative to the offset of the template
to the input array.
"""
# Warn user if any NANs, Infs, etc are present in the layer or the template
_check_for_nonfinite_entries(this_layer, template)
# Calculate the correlation array matching the template to this layer
corr = match_template(this_layer, template)
# Calculate the y and x shifts in pixels
return _find_best_match_location(corr)
def _find_best_match_location(corr):
"""
Calculate an estimate of the location of the peak of the correlation result
in image pixels.
Parameters
----------
corr : `numpy.ndarray`
A 2D correlation array.
Returns
-------
`~astropy.units.Quantity`
The shift amounts ``(y, x)`` in image pixels. Subpixel values are
possible.
"""
# Get the index of the maximum in the correlation function
ij = np.unravel_index(np.argmax(corr), corr.shape)
cor_max_x, cor_max_y = ij[::-1]
# Get the correlation function around the maximum
array_maximum = corr[
np.max([0, cor_max_y - 1]) : np.min([cor_max_y + 2, corr.shape[0] - 1]),
np.max([0, cor_max_x - 1]) : np.min([cor_max_x + 2, corr.shape[1] - 1]),
]
y_shift_maximum, x_shift_maximum = _get_correlation_shifts(array_maximum)
# Get shift relative to correlation array
y_shift_correlation_array = y_shift_maximum + cor_max_y * u.pix
x_shift_correlation_array = x_shift_maximum + cor_max_x * u.pix
return y_shift_correlation_array, x_shift_correlation_array
def _get_correlation_shifts(array):
"""
Estimate the location of the maximum of a fit to the input array. The
estimation in the "x" and "y" directions are done separately. The location
estimates can be used to implement subpixel shifts between two different
images.
Parameters
----------
array : `numpy.ndarray`
An array with at least one dimension that has three elements. The
input array is at most a 3x3 array of correlation values calculated
by matching a template to an image.
Returns
-------
`~astropy.units.Quantity`
The ``(y, x)`` location of the peak of a parabolic fit, in image pixels.
"""
# Check input shape
ny = array.shape[0]
nx = array.shape[1]
if nx > 3 or ny > 3:
msg = "Input array dimension should not be greater than 3 in any dimension."
raise ValueError(msg)
# Find where the maximum of the input array is
ij = np.unravel_index(np.argmax(array), array.shape)
x_max_location, y_max_location = ij[::-1]
# Estimate the location of the parabolic peak if there is enough data.
# Otherwise, just return the location of the maximum in a particular
# direction.
y_location = _parabolic_turning_point(array[:, x_max_location]) if ny == 3 else 1.0 * y_max_location
x_location = _parabolic_turning_point(array[y_max_location, :]) if nx == 3 else 1.0 * x_max_location
return y_location * u.pix, x_location * u.pix
def _parabolic_turning_point(y):
"""
Find the location of the turning point for a parabola ``y(x) = ax^2 + bx +
c``, given input values ``y(-1), y(0), y(1)``. The maximum is located at
``x0 = -b / 2a``. Assumes that the input array represents an equally spaced
sampling at the locations ``y(-1), y(0) and y(1)``.
Parameters
----------
y : `numpy.ndarray`
A one dimensional numpy array of shape "3" with entries that sample the
parabola at "-1", "0", and "1".
Returns
-------
`float`
A float, the location of the parabola maximum.
"""
numerator = -0.5 * y.dot([-1, 0, 1])
denominator = y.dot([1, -2, 1])
return numerator / denominator
def _check_for_nonfinite_entries(layer_image, template_image):
"""
Issue a warning if there is any nonfinite entry in the layer or template
images.
Parameters
----------
layer_image : `numpy.ndarray`
A two-dimensional `numpy.ndarray`.
template_image : `numpy.ndarray`
A two-dimensional `numpy.ndarray`.
"""
if not np.all(np.isfinite(layer_image)):
warnings.warn(
"The layer image has nonfinite entries. "
"This could cause errors when calculating shift between two "
"images. Please make sure there are no infinity or "
"Not a Number values. For instance, replacing them with a "
"local mean.",
SunpyUserWarning,
stacklevel=3,
)
if not np.all(np.isfinite(template_image)):
warnings.warn(
"The template image has nonfinite entries. "
"This could cause errors when calculating shift between two "
"images. Please make sure there are no infinity or "
"Not a Number values. For instance, replacing them with a "
"local mean.",
SunpyUserWarning,
stacklevel=3,
)