-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitsfile.py
345 lines (252 loc) · 11.1 KB
/
fitsfile.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
from astropy.io import fits
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import scipy
from spectrum import Spectrum
import gaussfit
import sys
### global variable for button toggling purposes, modeled off the matplotlib
# documentation here: https://matplotlib.org/2.1.2/gallery/event_handling/keypress_demo.html.
class FitsFile:
def __init__(self, fits_file):
"""
Creates a class that opens and contains .fits image attributes.
It takes in a fits image path.
"""
self.fits_file = fits_file
hdul = fits.open(fits_file)
self.image_data = hdul[0].data
self.log_image_data = np.log(self.image_data)
self.rows = self.image_data.shape[0]
self.cols = self.image_data.shape[1]
self.spectra = []
self.num_spectra = 0
# self.__get_spectra()
def get_dimensions(self):
return (self.rows, self.cols)
def plot_spectra(self, num_to_plot=None, save=False, show=False):
"""
Plots the spectra on top of the image.
"""
# Clear the figure.
plt.clf()
if not show and not save:
raise ValueError("You must either choose to save or show the spectra.")
if num_to_plot is None:
num_to_plot = len(self.spectra)
# importing inside function to avoid circular dependency issues
import util
# Setting up plotting...
vmin, vmax = util.get_vmin_vmax(self.image_data)
plt.imshow(self.image_data, origin="lower", cmap="gray",
vmin=vmin, vmax=vmax)
image_rows = self.image_data.shape[0]
image_cols = self.image_data.shape[1]
degree = 3
spectrum_scatter_plots = []
fit_plots = []
for spectrum in self.spectra[:num_to_plot]:
# Uncomment this section if the scatter plot portion of the spectrum
# is desired.
spectrum_scatter = spectrum.plot(only_endpoints=True)
spectrum_scatter_plots.append(spectrum_scatter)
fit_plot = spectrum.plot_fit()
fit_plots.append(fit_plot)
# Plotting...
plt.plot(self.start_parab, self.parab_range)
plt.plot(self.end_parab, self.parab_range)
plt.xlabel("xpixel")
plt.ylabel("ypixel")
plt.title("Image " + self.get_file_name() + " with Spectral Continuum Fits\nSpectra " + str(num_to_plot) + "/" + str(self.num_spectra))
plt.xlim(0, self.get_dimensions()[1])
plt.ylim(0, self.get_dimensions()[0])
current_fig = plt.gcf()
if save:
directory = "completed_images/"
image_file_name = self.get_file_name() + "_fitted.svg"
print("Saving " + image_file_name + " to disk...")
current_fig.savefig(directory + image_file_name, dpi=1500)
if show:
plt.show()
def __get_intensity_array(self, xpixel=1000):
"""
At a particular xpixle, (default is 1000), this function returns
an array of intensity for each pixel in y.
"""
intensity = []
for row_num, row in enumerate(self.image_data):
intensity.append(row[xpixel])
return np.array(intensity)
def __find_peaks(self):
"""
Find peaks in the intensity array. The peaks correspond to each order of the spectrograph.
"""
import util
prime_pixel = 1000
length = self.image_data.shape[1]
xpixels = np.arange(length)
# Finds number of peaks based on reference x pixel
intensity_array = self.__get_intensity_array(xpixel=prime_pixel)
num_peaks_prime = util.find_int_peaks(intensity_array)
used_xpixels = []
# The dictionary containing peaks at each x value
xpeaks = dict()
# Obtain a dictionary of x pixels and the peaks at each x pixel.
for xpixel in xpixels:
ia = self.__get_intensity_array(xpixel=xpixel)
peaks = util.find_int_peaks(ia)
xpeaks[xpixel] = peaks
return xpeaks
def __plot_peaks(self, xpeaks):
print("Plotting peaks")
img = np.zeros((self.image_data.shape[0], self.image_data.shape[1]))
for x in xpeaks:
img[xpeaks[x], x] = 1
plt.imshow(img, origin="lower")
current_figure = plt.gcf()
plt.show()
print("Saving peak plot...")
current_figure.savefig("assets/peak_plot.svg", dpi=2000)
def get_spectra(self):
import util
xpeaks = self.__find_peaks()
image_length = self.image_data.shape[1]
start_pixel = 1000
yvalues_at_start = xpeaks[start_pixel]
self.num_spectra = len(yvalues_at_start)
xthreshold = 5
ythreshold = 2
cur_num_spectra = 0
# Going from right to left
for num, y in enumerate(yvalues_at_start):
cur_y = y
s = Spectrum([], [], self)
cur_x = start_pixel
for next_spec_x in range(start_pixel+1, image_length):
check_y = xpeaks[next_spec_x]
# Check for xpixels to see if there exists a y pixel that's less
# than some value away.
spec_indices = np.where(abs(cur_y-check_y) <= ythreshold)[0]
if len(spec_indices) > 0:
next_ind = spec_indices[0]
nexty = check_y[next_ind]
s.add_peak(next_spec_x, nexty)
cur_x = next_spec_x
cur_y = nexty
if abs(next_spec_x - cur_x) >= xthreshold:
break
cur_x = start_pixel
cur_y = y
for prev_spec_x in range(start_pixel-1, 0, -1):
check_y = xpeaks[prev_spec_x]
spec_indices = np.where(abs(cur_y-check_y) <= ythreshold)[0]
if len(spec_indices) > 0:
prev_ind = spec_indices[0]
prevy = check_y[prev_ind]
s.add_peak(prev_spec_x, prevy)
cur_x = prev_spec_x
cur_y = prevy
if abs(prev_spec_x - cur_x) >= xthreshold:
break
build_prep_success = s.build_prepare()
if build_prep_success:
cur_num_spectra += 1
self.spectra.append(s)
print("Spectrum %d/%d ready for building..." % (cur_num_spectra, self.num_spectra))
else:
self.num_spectra -= 1
self.__fit_overlap_boundary_parabola()
self.__update_spectral_boundaries()
built_spectra = []
cur_num_spectra = 0
for spectrum in self.spectra:
build_success = spectrum.build()
if build_success:
cur_num_spectra += 1
built_spectra.append(spectrum)
print("Building spectrum %d/%d" % (cur_num_spectra, self.num_spectra))
print("Min x:", spectrum.xvalues[0], "\nMax x:", spectrum.xvalues[-1])
else:
self.num_spectra -= 1
self.spectra = built_spectra
def plot_spectra_brightness(self):
for ind, spectrum in enumerate(self.spectra):
num = ind + 1
spectrum.plot_spectrum_brightness(num)
# plt.show()
def __fit_overlap_boundary_parabola(self):
"""
This function fits a parabola to the overlap boundaries after the
spectrum.remove_overlap_spectrum is run. However, since a vertical
parabola needs to be fit, the y value are the effective xvalues and
the xvalues are the effective yvalues.
"""
import util
spectrum_startx = []
spectrum_starty = []
spectrum_endx = []
spectrum_endy = []
for spectrum in self.spectra:
spectrum_startx.append(spectrum.int_xvalues[0])
spectrum_starty.append(spectrum.int_yvalues[0])
spectrum_endx.append(spectrum.int_xvalues[-1])
spectrum_endy.append(spectrum.int_yvalues[-1])
height = self.image_data.shape[0]
domain = np.arange(height)
start_parab, sp_rms = util.fit_parabola(spectrum_starty,
spectrum_startx,
domain)
end_parab, ep_rms = util.fit_parabola(spectrum_endy, spectrum_endx,
domain)
self.start_parab = start_parab
self.end_parab = end_parab
self.parab_range = domain
# StartParabola_rms and EndParabola_rms
self.sp_rms = sp_rms
self.ep_rms = ep_rms
def __update_spectral_boundaries(self):
"""
Fixes the spectral boundaries by replacing the spectral boundaries with
points that should be closer to the edges.
"""
import util
height = self.image_data.shape[0]
domain = np.arange(height)
print("spectral boundaries before...")
for spectrum in self.spectra:
# Updating the left half boundaries
startx = spectrum.int_xvalues[0]
starty = spectrum.int_yvalues[0]
parab_starty_ind = util.nearest_ind_to_val(domain, starty)
start_parabx = self.start_parab[parab_starty_ind]
# Starting and ending indices of the first and last
# values of the int_xvalues array in the spectrum.ox array
spec_start_ind = spectrum.ox.index(spectrum.int_xvalues[0])
spec_end_ind = spectrum.ox.index(spectrum.int_xvalues[-1])
# If the true yvalue is greater than 3 std. dev. away from the
# parabola, replace the y value with the x value at the parabola
if abs(start_parabx - startx) >= 3 * self.sp_rms:
# Obtain index of xvalue that is closest to the starting value
# of the parabola
print("start_parabx:", start_parabx, "startx:", startx)
spec_start_ind = util.nearest_ind_to_val(spectrum.ox, start_parabx)
# Repeat same procedure as above for the last values
endx = spectrum.int_xvalues[-1]
endy = spectrum.int_yvalues[-1]
parab_endy_ind = util.nearest_ind_to_val(domain, endy)
end_parabx = self.end_parab[parab_endy_ind]
if abs(end_parabx - endx) >= 3 * self.ep_rms:
print("end_parabx:", end_parabx, "endx:", endx)
spec_end_ind = util.nearest_ind_to_val(spectrum.ox, end_parabx)
int_xvals = spectrum.ox[spec_start_ind:spec_end_ind]
int_yvals = spectrum.oy[spec_start_ind:spec_end_ind]
# Update the spectrum int_xvals and the int_yvals
spectrum.int_xvalues = int_xvals
spectrum.int_yvalues = int_yvals
def get_file_name(self):
"""
Returns the name of the file independent of the path.
"""
return self.fits_file[self.fits_file.rfind("/")+1:]