-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphasefold.py
More file actions
350 lines (309 loc) · 15.3 KB
/
phasefold.py
File metadata and controls
350 lines (309 loc) · 15.3 KB
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
350
"""
Copyright (c) 2022, Yifan Tong
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
"""
#import and set up everything
import sys
sys.path.append("../PhaseFold")
import lightkurve as lk
import scipy.signal
import matplotlib.pyplot as plt
import math
from PIL import Image
import warnings
from ipywidgets.widgets import Button, Layout
from IPython.display import display
import os
warnings.filterwarnings("ignore", category=RuntimeWarning)
#before using the functions, create a folder called LightCurves
def foldperiod(lcurve, sect, givenperiod):
'''
Phase folds the given light curve using the given period and outputs 3 graphs: the original light curve, the periodogram, and the folded light curve
As well as gives the option to save the 3 graphs and the combined graph in the LightCurves folder.
Parameters:
lcurve (lightkurve.LightCurve): a lightkurve.LightCurve object
sect (int): the sector that the light curve is in
givenperiod (int): the period that the light curve will be folded using
'''
lightc = lcurve
a = lightc.scatter()
lc = lightc[lightc.quality==0]
pg = lc.normalize(unit='ppm').to_periodogram(minimum_period = 0.042, oversample_factor=300)
period = pg.period_at_max_power
pg1 = lc.normalize(unit='ppm').to_periodogram(maximum_period = 2.1*period.value, oversample_factor=100)
b = pg1.plot(view='period')
folded = lc.fold(givenperiod)
c = folded.scatter(label=fr'Period = {givenperiod:.5f} d')
#creates the save button that can be used to save the images, as well as the combined version
b_save = Button (description = 'save', layout = Layout(width='100px'))
def bsave(b_save):
a.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_LC.png')
b.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Periodogram.png')
c.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Folded.png')
images = [Image.open(x) for x in [f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_LC.png', f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Periodogram.png', f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Folded.png']]
widths, heights = zip(*(i.size for i in images))
total_height = sum(heights)
min_width = min(widths)
new_im = Image.new('RGB', (min_width, total_height))
y_offset = 0
for im in images:
new_im.paste(im, (0,y_offset))
y_offset += im.size[1]
new_im.save(f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}.png')
b_save.on_click(bsave)
display(b_save)
def fold(lcurve, sect):
'''
Phase folds the given light curve and outputs 3 graphs: the original light curve, the periodogram, and the folded light curve
As well as gives the option to save the 3 graphs and the combined graph in the LightCurves folder.
Parameters:
lcurve (lightkurve.LightCurve): a lightkurve.LightCurve object
sect (int): the sector that the light curve is in
'''
lightc = lcurve
a = lightc.scatter()
lc = lightc[lightc.quality==0]
pg = lc.normalize(unit='ppm').to_periodogram(minimum_period = 0.042, oversample_factor=300)
period = pg.period_at_max_power
pg1 = lc.normalize(unit='ppm').to_periodogram(maximum_period = 2.1*period.value, oversample_factor=100)
mini = .7*period
maxi = 1.3*period
midpt = (mini + maxi)/2
b = pg1.plot(view='period')
midpt = redef(mini, maxi, midpt, lc)
folded = lc.fold(midpt)
#cleanlightcurve = folded[folded.quality==0]
c = folded.scatter(label=fr'Period = {midpt.value:.5f} d')
#creates the save button that can be used to save the images, as well as the combined version
b_save = Button (description = 'save', layout = Layout(width='100px'))
def bsave(b_save):
if not os.path.exists(f"LightCurves/S{sect}TIC{lc.TARGETID}"):
os.makedirs(f"LightCurves/S{sect}TIC{lc.TARGETID}")
a.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_LC.png')
b.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Periodogram.png')
c.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Folded.png')
images = [Image.open(x) for x in [f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_LC.png', f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Periodogram.png', f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Folded.png']]
widths, heights = zip(*(i.size for i in images))
total_height = sum(heights)
min_width = min(widths)
new_im = Image.new('RGB', (min_width, total_height))
y_offset = 0
for im in images:
new_im.paste(im, (0,y_offset))
y_offset += im.size[1]
new_im.save(f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}.png')
b_save.on_click(bsave)
display(b_save)
# Folds and saves the original light curve, periodogram, folded light curve, and all 3 combined
# Into a folder in the LightCurves folder
def foldandsave(lcurve, sect):
'''
Folds and saves the original light curve, periodogram, folded light curve, and all 3 combined
Into a folder in the LightCurves folder
Parameters:
lcurve (lightkurve.LightCurve): a lightkurve.LightCurve object
sect (int): the sector that the light curve is in
'''
lightc = lcurve
a = lightc.scatter()
lc = lightc[lightc.quality==0]
a.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_LC.png')
pg = lc.normalize(unit='ppm').to_periodogram(minimum_period = 0.042, oversample_factor=300)
period = pg.period_at_max_power
pg1 = lc.normalize(unit='ppm').to_periodogram(maximum_period = 2.1*period.value, oversample_factor=100)
mini = .7*period
maxi = 1.3*period
midpt = (mini + maxi)/2
b = pg1.plot(view='period')
b.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Periodogram.png')
midpt = redef(mini, maxi, midpt, lc)
folded = lc.fold(midpt)
c = folded.scatter(label=fr'Period = {midpt.value:.5f} d')
plt.close('all')
c.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Folded.png')
images = [Image.open(x) for x in [f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_LC.png', f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Periodogram.png', f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Folded.png']]
widths, heights = zip(*(i.size for i in images))
total_height = sum(heights)
min_width = min(widths)
new_im = Image.new('RGB', (min_width, total_height))
y_offset = 0
for im in images:
new_im.paste(im, (0,y_offset))
y_offset += im.size[1]
new_im.save(f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}.png')
#Saves the images as well as prints it out
def foldsaveprint(lcurve, sect):
'''
Folds,outputs, and saves the original light curve, periodogram, folded light curve, and all 3 combined
Into a folder in the LightCurves folder
Parameters:
lcurve (lightkurve.LightCurve): a lightkurve.LightCurve object
sect (int): the sector that the light curve is in
'''
lightc = lcurve
a = lightc.scatter()
lc = lightc[lightc.quality==0]
a.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_LC.png')
pg = lc.normalize(unit='ppm').to_periodogram(minimum_period = 0.042, oversample_factor=300)
period = pg.period_at_max_power
pg1 = lc.normalize(unit='ppm').to_periodogram(maximum_period = 2.1*period.value, oversample_factor=100)
mini = .7*period
maxi = 1.3*period
midpt = (mini + maxi)/2
b = pg1.plot(view='period')
b.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Periodogram.png')
midpt = redef(mini, maxi, midpt, lc)
folded = lc.fold(midpt)
c = folded.scatter(label=fr'Period = {midpt.value:.5f} d')
c.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Folded.png')
images = [Image.open(x) for x in [f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_LC.png', f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Periodogram.png', f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Folded.png']]
widths, heights = zip(*(i.size for i in images))
total_height = sum(heights)
min_width = min(widths)
new_im = Image.new('RGB', (min_width, total_height))
y_offset = 0
for im in images:
new_im.paste(im, (0,y_offset))
y_offset += im.size[1]
new_im.save(f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}.png')
# Combines the seperate 3 pngs into one png
def combinefiles(lc,sect):
'''
Combines the 3 individual graphs (original, periodogram, and folded light curve) of the light curve into one png
Parameters:
lc (lightkurve.LightCurve): a lightkurve.LightCurve object
sect (int): the sector that the light curve is in
'''
images = [Image.open(x) for x in [f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_LC.png', f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Periodogram.png', f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Folded.png']]
widths, heights = zip(*(i.size for i in images))
total_height = sum(heights)
max_width = max(widths)
min_width = min(widths)
new_im = Image.new('RGB', (min_width, total_height))
y_offset = 0
for im in images:
new_im.paste(im, (0,y_offset))
y_offset += im.size[1]
new_im.save(f'./LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}.png')
# Function to output original graph, periodogram, and phasefolded graph given TIC ID and their sector
def graphfoldprint(lcurve, sect):
'''
Folds and outputs the original light curve, periodogram, and the folded light curve
Into a folder in the LightCurves folder
Parameters:
lcurve (lightkurve.LightCurve): a lightkurve.LightCurve object
sect (int): the sector that the light curve is in
'''
lightc = lcurve
a = lightc.scatter()
lc = lightc[lightc.quality==0]
pg = lc.normalize(unit='ppm').to_periodogram(minimum_period = 0.042, oversample_factor=300)
period = pg.period_at_max_power
pg1 = lc.normalize(unit='ppm').to_periodogram(maximum_period = 2.1*period.value, oversample_factor=100)
mini = .7*period
maxi = 1.3*period
midpt = (mini + maxi)/2
b = pg1.plot(view='period')
midpt = redef(mini, maxi, midpt, lc)
folded = lc.fold(midpt)
c = folded.scatter(label=fr'Period = {midpt.value:.5f} d')
# Function to save the original graph, periodogram, and phasefolded graph in the
# LightCurves folder without combining them
def graphfoldsave(lcurve, sect):
'''
Folds, prints, and saves the original light curve, periodogram, and folded light curve
Into a folder in the LightCurves folder without combining them
Parameters:
lcurve (lightkurve.LightCurve): a lightkurve.LightCurve object
sect (int): the sector that the light curve is in
'''
lightc = lcurve
a = lightc.scatter()
lc = lightc[lightc.quality==0]
a.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_LC.png')
pg = lc.normalize(unit='ppm').to_periodogram(minimum_period = 0.042, oversample_factor=300)
period = pg.period_at_max_power
pg1 = lc.normalize(unit='ppm').to_periodogram(maximum_period = 2.1*period.value, oversample_factor=100)
mini = .7*period
maxi = 1.3*period
midpt = (mini + maxi)/2
b = pg1.plot(view='period')
b.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Periodogram.png')
midpt = redef(mini, maxi, midpt, lc)
folded = lc.fold(midpt)
c = folded.scatter(label=fr'Period = {midpt.value:.5f} d')
c.figure.savefig(f'LightCurves/S{sect}TIC{lc.TARGETID}/S{sect}TIC{lc.TARGETID}_Folded.png')
#cleanlightcurve - light curve without noise
#phasecurve - phasefolded light curve
#smoothcurve - median filtered version of phasecurve
#cleanlcmod - median filtered version of cleanlightcurve
#adjusts min max and midpoint
def redef(mini, maxi, midpt, lc):
'''
Adjusts the midpt after comparing the standard deviation of the residuals to find the best period
Parameters:
mini (double): current minimum period for binary search
maxi (double): current maximum period for binary search
midpt (double): current assumed period value
lc (lightkurve.LightCurve): a lightkurve.LightCurve object
Returns:
midpt (double): the best period to phase fold on
'''
#tests if a multiple of the midpt is better than the current one
twomidptresstddev = calcresidualstddevmidpt(lc, 2*midpt)
midptresstddev = calcresidualstddevmidpt(lc, midpt)
if (twomidptresstddev) < (midptresstddev):
midpt = 2*midpt
mini = .7*midpt
maxi = 1.3*midpt
#global mini, maxi, midpt, lc
while(mini.value + 0.0001 < maxi.value):
minresstddev = calcresidualstddevmin(lc, mini)
midptresstddev = calcresidualstddevmidpt(lc, midpt)
maxresstddev = calcresidualstddevmax(lc, maxi)
if (minresstddev - midptresstddev) < (maxresstddev - midptresstddev):
maxi = midpt
midpt = (mini + maxi)/2
else:
mini = midpt
midpt = (mini + maxi)/2
return midpt
#calculates the std dev of residuals of period given by argument (num)
def calcresidualstddevmin(lc, num):
lcfolded = lc.fold(num)
cleanlightcurve = lcfolded[lcfolded.quality==0]
phasecurve = lc.fold(num)[:]
cleanlcmod = cleanlightcurve[:]
cleanlcmod.flux = scipy.signal.medfilt(cleanlightcurve.flux, kernel_size=13)
residual = cleanlcmod.flux.value - cleanlightcurve.flux.value
ressqr = 0
for x in range(len(cleanlcmod)):
ressqr = ressqr + (residual[x] ** 2)
minresstddev = math.sqrt((ressqr)/(len(cleanlcmod)-2))
return minresstddev
def calcresidualstddevmax(lc, num):
lcfolded = lc.fold(num)
cleanlightcurve = lcfolded[lcfolded.quality==0]
phasecurve = lc.fold(num)[:]
cleanlcmod = cleanlightcurve[:]
cleanlcmod.flux = scipy.signal.medfilt(cleanlightcurve.flux, kernel_size=13)
residual = cleanlcmod.flux.value - cleanlightcurve.flux.value
ressqr = 0
for x in range(len(cleanlcmod)):
ressqr = ressqr + (residual[x] ** 2)
maxresstddev = math.sqrt((ressqr)/(len(cleanlcmod)-2))
return (maxresstddev)
def calcresidualstddevmidpt(lc, num):
lcfolded = lc.fold(num)
cleanlightcurve = lcfolded[lcfolded.quality==0]
phasecurve = lc.fold(num)[:]
cleanlcmod = cleanlightcurve[:]
cleanlcmod.flux = scipy.signal.medfilt(cleanlightcurve.flux, kernel_size=13)
residual = cleanlcmod.flux.value - cleanlightcurve.flux.value
ressqr = 0
for x in range(len(cleanlcmod)):
ressqr = ressqr + (residual[x] ** 2)
midptresstddev = math.sqrt((ressqr)/(len(cleanlcmod)-2))
return (midptresstddev)