-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebClassifier.py
419 lines (333 loc) · 17.6 KB
/
ebClassifier.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import glob
import os
import pandas as pd
import numpy as np
from scipy import stats
import exoplanet as xo
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.table import Table
from astropy.timeseries import LombScargle, BoxLeastSquares
from sklearn.metrics import confusion_matrix
import seaborn as sns
import time as timer
start = timer.time()
knownData = 0 # Boolean. Turns on/off diagnostic graphing. Prints confirmed light curves only.
def classification(blsMP, sd, factor):
resClassification = 'notEB'
# Line from SD 3, BLS 1000 to SD 15, BLS 100
# minblsMP = (-75 * sd) + min((20 * factor * sd), (75 * sd)) + 1225 - min(factor * 250, 1025)
minblsMP = startMP - dropMP * factor
# Identify significant eclipses
if blsMP >= minblsMP or sd >= 10 - 2 * factor:
resClassification = 'EB'
print(objName + ' IS CLASSIFIED AS AN ECLIPSING BINARY****')
return resClassification
def findsd(relFlux, time, name, smooth):
# Calculate z-score of all points, find outliers below the flux midpoint.
z = stats.zscore(relFlux)
potentialEclipses = z[np.where(relFlux < 1)[0]]
peTimes = time.iloc[np.where(relFlux < 1)[0]]
maxSDRangeIndex = range(max(np.argmin(potentialEclipses) - 2, 0),
min(np.argmin(potentialEclipses) + 3, potentialEclipses.size))
sdRange = np.ceil(potentialEclipses[maxSDRangeIndex]).astype(int)
maximumSD = abs(np.min(sdRange))
avgMaximumSD = np.average(sdRange).round(0).astype(int) * -1
# To ensure that data points near the max SD are indeed significant
# print("Generating z-score vs time chart.")
# plt.figure(figsize=(9, 6))
# plt.scatter(time, z, color='tab:purple', s=.1)
# plt.scatter(peTimes.iloc[maxSDRangeIndex], potentialEclipses[maxSDRangeIndex], marker='^', color='tab:cyan', s=5)
# plt.xlabel('BJD - 2457000 (days)') # BJD Julian corrected for elliptical orbit.
# plt.ylabel('RelFlux Z-Score')
# plt.title('z-Score vs. Time for ' + name + "\n" + str(smooth) + "x smoothing.")
# plt.savefig(os.path.join('sdz_' + str(j+1), name + "_" + str(smooth) + '.png'), orientation='landscape')
# plt.close()
return maximumSD, avgMaximumSD, potentialEclipses[maxSDRangeIndex] * -1, peTimes.iloc[maxSDRangeIndex], \
relFlux.iloc[maxSDRangeIndex]
def lombscargle(time, relFlux):
LS = LombScargle(time, relFlux)
frequency, power = LS.autopower(minimum_frequency=1 / 27, maximum_frequency=1 / .05)
bestPeriod = 1 / frequency[np.argmax(power)]
maxPower = np.max(power)
period = 1 / frequency
return period, power, bestPeriod, maxPower
def autocorrelationfn(time, relFlux, relFluxErr):
acf = xo.autocorr_estimator(time.values, relFlux.values, yerr=relFluxErr.values,
min_period=0.05, max_period=27, max_peaks=10)
period = acf['autocorr'][0]
power = acf['autocorr'][1]
acfPowerPd = pd.DataFrame(power)
acfLocalMaxima = acfPowerPd[(acfPowerPd.shift(1) < acfPowerPd) & (acfPowerPd.shift(-1) < acfPowerPd)]
maxPower = np.max(acfLocalMaxima).values
bestPeriod = period[np.where(power == maxPower)[0]][0]
peaks = acf['peaks'][0]['period']
if len(acf['peaks']) > 0:
window = int(peaks / np.abs(np.nanmedian(np.diff(time))) / k)
else:
window = 128
return period, power, bestPeriod, maxPower, window
def boxleastsquares(time, relFlux, relFluxErr, acfBP):
model = BoxLeastSquares(time.values, relFlux.values, dy=relFluxErr.values)
duration = [20 / 1440, 40 / 1440, 80 / 1440, .1]
periodogram = model.power(period=[.5 * acfBP, acfBP, 2 * acfBP], duration=duration,
objective='snr')
period = periodogram.period
power = periodogram.power
maxPower = np.max(periodogram.power)
bestPeriod = periodogram.period[np.argmax(periodogram.power)]
return period, power, bestPeriod, maxPower
def addtotable(table, oID, blsMP, acfMP, mSD, amSD, smooth, flg, c, rSD, f):
table = table.append(
{'Obj ID': oID, 'BLS Max Pow': blsMP, 'ACF Max Pow': acfMP, 'Max SD': mSD,
'AvgMax SD': amSD, 'Times Smoothed': smooth, 'Flag': flg, 'Classification': c,
'SD Range': rSD, 'Filename': f}, ignore_index=True)
return table
def makegraph(xaxis, yaxis, xlabels, ylabels, lbl, color, marker=None, size=None, style=None, ax=None):
if ax is None:
ax = plt.gca()
if style is None:
ax.scatter(xaxis, yaxis, color=color, marker=marker, s=size)
else:
ax.plot(xaxis, yaxis, color=color)
plt.xlabel(xlabels)
plt.ylabel(ylabels)
plt.title(lbl)
return ax
lightCurves = [] # Initialize the array holding light curves
path = "data_all" # Folder containing fits files
EBs = [] # Store the objects classified as eclipsing binaries
confirmedEBs = [line.rstrip('\n') for line in open('curves.txt')] # Confirmed by eye EBs from data
k = 5
startMP = 1500
dropMP = 650
files = glob.glob(os.path.join(path, "*.fits"))
lcTable = pd.DataFrame(
columns=['Obj ID', 'BLS Max Pow', 'ACF Max Pow', 'Max SD', 'AvgMax SD', 'Times Smoothed', 'Flag', 'Classification',
'SD Range', 'Filename'])
for file in files:
fitsTable = fits.open(file, memmap=True)
objName = fitsTable[0].header['OBJECT']
print("\nReading in " + objName)
curveTable = Table(fitsTable[1].data).to_pandas()
curveData = curveTable.loc[curveTable['QUALITY'] == 0].dropna(subset=['TIME']).dropna(
subset=['PDCSAP_FLUX']).copy()
fluxMed = np.nanmedian(curveData['PDCSAP_FLUX'])
curveData['REL_FLUX'] = curveData['PDCSAP_FLUX'].div(fluxMed)
curveData['REL_FLUX_ERR'] = curveData['PDCSAP_FLUX_ERR'].div(fluxMed)
originalFlux = curveData['REL_FLUX'].copy()
originalTime = curveData['TIME'].copy()
classif = 'notEB'
if knownData == 1:
# Check if marked as confirmed EB
if objName in confirmedEBs:
flag = 'EB'
else:
flag = 'notEB'
else:
flag = ''
# Classify based on outliers
i = 0
while classif == 'notEB' and i < 3: # Potential to be an EB.
maxSD, avgMaxSD, rangeSD, fluxRange, timeRange = findsd(curveData['REL_FLUX'], curveData['TIME'], objName, i)
if avgMaxSD < 3:
# Pre-classification
# Max SD of 0 through 2 highly unlikely to be eclipse.
if knownData == 1 and flag == 'EB':
title = 'Misclassified: ' + objName + "\n" + str(i) + "x smoothing" + "\n" + file
# Add to table
print("Adding to table.")
lcTable = addtotable(lcTable, objName, BLSmaxPower, acfMaxPower[0], maxSD, avgMaxSD, i, flag, classif,
rangeSD, file)
figName = 'missedEB_' + objName + '.png'
if i == 0:
# Graph the light curve vs. smoothed light curve
print("Generating misclassified chart.")
plt.figure(figsize=(9, 6))
makegraph(originalTime, originalFlux, 'BJD - 2457000 (days)', 'Relative Flux',
'Light Curve for ' + objName, 'tab:purple', '.', .2)
plt.suptitle(title)
plt.savefig(os.path.join('misclassified', figName), orientation='landscape')
plt.close()
else:
plt.figure(figsize=(9, 9))
# Graph the light curve vs. smoothed light curve
print("Generating misclassified chart.")
plt.subplot(211)
makegraph(originalTime, originalFlux, 'BJD - 2457000 (days)', 'Relative Flux',
'Light Curve for ' + objName, 'tab:purple', '.', .2)
plt.subplot(212)
makegraph(curveData['TIME'], curveData['REL_FLUX'], 'BJD - 2457000 (days)', 'Relative Flux',
'Smoothed ' + str(i) + 'x light curve for ' + objName, 'tab:purple', '.', .2)
plt.suptitle(title)
plt.savefig(os.path.join('misclassified', figName), orientation='landscape')
plt.close()
break
else:
# Run ACF and BLS functions for classification
try:
# Autocorrelation Function
print("Generating ACF periodogram.")
acfPeriod, acfPower, acfBestPeriod, acfMaxPower, s_window = autocorrelationfn(curveData['TIME'],
curveData['REL_FLUX'],
curveData[
'REL_FLUX_ERR'])
# Box Least Squares
print("Generating BLS periodogram.")
BLSperiod, BLSpower, BLSbestPeriod, BLSmaxPower = boxleastsquares(curveData['TIME'],
curveData['REL_FLUX'],
curveData['REL_FLUX_ERR'],
acfBestPeriod)
except:
break
# Additional pre-classification
if (i == 0 and BLSmaxPower < 100 and avgMaxSD < 7) or \
(acfMaxPower < 0.05 and avgMaxSD < 4) or BLSmaxPower < 60:
# No need to smooth attempt further, very unlikely to be obvious EBs.
if knownData == 1 and flag == 'EB':
title = 'Misclassified: ' + objName + "\n" + str(i) + "x smoothing" + "\n" + file
# Add to table
print("Adding to table.")
lcTable = addtotable(lcTable, objName, BLSmaxPower, acfMaxPower[0], maxSD, avgMaxSD, i, flag,
classif, rangeSD, file)
figName = 'missedEB_' + objName + '.png'
if i == 0:
plt.figure(figsize=(9, 9))
# Graph the light curve vs. smoothed light curve
print("Generating misclassified chart.")
makegraph(originalTime, originalFlux, 'BJD - 2457000 (days)', 'Relative Flux',
'Light Curve for ' + objName, 'tab:purple', '.', .2)
plt.suptitle(title)
plt.savefig(os.path.join('misclassified', figName), orientation='landscape')
plt.close()
else:
plt.figure(figsize=(9, 9))
# Graph the light curve vs. smoothed light curve
print("Generating misclassified chart.")
plt.subplot(211)
makegraph(originalTime, originalFlux, 'BJD - 2457000 (days)', 'Relative Flux',
'Light Curve for ' + objName, 'tab:purple', '.', .2)
plt.subplot(212)
makegraph(curveData['TIME'], curveData['REL_FLUX'], 'BJD - 2457000 (days)', 'Relative Flux',
'Smoothed ' + str(i) + 'x light curve for ' + objName, 'tab:purple', '.', .2)
plt.suptitle(title)
plt.savefig(os.path.join('misclassified', figName), orientation='landscape')
plt.close()
break
# Run classification
classif = classification(BLSmaxPower, avgMaxSD, i)
if classif == 'notEB':
# Perform Smoothing
print("Performing smoothing on " + objName)
smoothedFlux = curveData['REL_FLUX'].rolling(s_window, center=True).median()
SOK = np.isfinite(smoothedFlux)
newFlux = curveData['REL_FLUX'][SOK] - smoothedFlux[SOK]
curveData['REL_FLUX'] = newFlux.copy()
curveData = curveData.dropna(subset=['TIME']).dropna(subset=['REL_FLUX']).dropna(
subset=['REL_FLUX_ERR']).copy()
fluxMed = np.nanmedian(curveData['REL_FLUX'])
else:
EBs.append(objName) # Add to printout of EBs
if knownData == 1 and ((flag == 'notEB' and classif == 'EB') or (i == 2 and flag != classif)):
plt.figure(figsize=(16, 16))
title = 'Misclassified: ' + objName + " Avg Max SD: " + str(avgMaxSD) + "\n" + str(i) + \
"x smoothing" + "\n" + file
if flag == 'EB':
figName = 'missedEB_' + objName + '.png'
else:
figName = 'misclassified_' + objName + '.png'
# Graph the light curve vs. smoothed light curve
print("Generating misclassified chart.")
plt.subplot(221)
makegraph(originalTime, originalFlux, 'BJD - 2457000 (days)', 'Relative Flux',
'Light Curve for ' + objName, 'tab:purple', '.', .2)
plt.subplot(222)
makegraph(curveData['TIME'], curveData['REL_FLUX'], 'BJD - 2457000 (days)', 'Relative Flux',
'Smoothed ' + str(i) + 'x light curve for ' + objName, 'tab:purple', '.', .2)
plt.subplot(223)
makegraph(acfPeriod, acfPower, 'Period', 'AutoCorr Power', 'ACF for ' + objName, 'tab:purple',
style='sm')
plt.scatter(acfBestPeriod, acfMaxPower, c='C1')
plt.text(acfBestPeriod, acfMaxPower, 'Per: ' + str(acfBestPeriod))
plt.subplot(224)
makegraph(BLSperiod, BLSpower, 'Period', 'BLS Power', 'BLS for ' + objName, 'tab:purple', style='sm')
plt.scatter(BLSbestPeriod, BLSmaxPower, c='C1')
plt.text(BLSbestPeriod, BLSmaxPower, 'Per: ' + str(BLSbestPeriod))
plt.suptitle(title)
plt.savefig(os.path.join('misclassified', figName), orientation='landscape')
plt.close()
# Add to table
print("Adding to table.")
lcTable = addtotable(lcTable, objName, BLSmaxPower, acfMaxPower[0], maxSD, avgMaxSD, i, flag, classif,
rangeSD, file)
i += 1
if knownData == 0 and classif == 'EB':
figName = objName + '.png'
plt.figure(figsize=(9, 6))
# Graph the light curve vs. smoothed light curve
print("Generating misclassified chart.")
makegraph(originalTime, originalFlux, 'BJD - 2457000 (days)', 'Relative Flux', 'Light Curve for ' + objName,
'tab:purple', '.', .2)
plt.savefig(os.path.join('EB', figName), orientation='landscape')
plt.close()
print(objName + " complete.")
print('\nPlotting and classification complete.\n')
print('EBs found: ' + str(len(EBs)))
for EB in EBs:
print(EB)
# Print table to file
print("\nPrint curve table to file.\n")
lcTable.to_csv('curvesTable.csv')
# Print results table to file
print("Print results table to file.\n")
lcTable.groupby(['Obj ID']).last().to_csv('resultsTable.csv')
# Extract table data necessary for graphs
focusTable = lcTable.filter(
['Obj ID', 'BLS Max Pow', 'ACF Max Pow', 'Max SD', 'AvgMax SD', 'Times Smoothed', 'Flag', 'Filename'])
if knownData == 1:
ebFocus = focusTable.loc[focusTable['Flag'] == 'EB'].copy()
nonEBFocus = focusTable.loc[focusTable['Flag'] != 'EB'].copy()
# Generate graphs based on smoothing
i = 0
while i < 3:
print("Generating charts for " + str(i) + "x smoothing.")
filename = 'comps_' + str(i) + '.png'
ebTable = ebFocus.loc[ebFocus['Times Smoothed'] == i].copy()
otherTable = nonEBFocus.loc[nonEBFocus['Times Smoothed'] == i].copy()
plt.figure(figsize=(9, 9))
# ACF MP vs Max SD
plt.subplot(211)
makegraph(otherTable['AvgMax SD'], otherTable['ACF Max Pow'], 'AvgMax SD', 'ACF Max Power',
'ACF MP vs Max SD for ' + str(i) + 'x Smoothing', 'tab:gray', '^', 25)
makegraph(ebTable['AvgMax SD'], ebTable['ACF Max Pow'], 'AvgMax SD', 'ACF Max Power',
'ACF MP vs Max SD for ' + str(i) + 'x Smoothing', 'tab:cyan', 'o', 20)
# BLS MP vs Max SD
plt.subplot(212)
makegraph(otherTable['AvgMax SD'], otherTable['BLS Max Pow'], 'AvgMax SD', 'BLS Max Power',
'BLS MP vs Max SD for ' + str(i) + 'x Smoothing', 'tab:gray', '^', 25)
makegraph(ebTable['AvgMax SD'], ebTable['BLS Max Pow'], 'AvgMax SD', 'BLS Max Power',
'BLS MP vs Max SD for ' + str(i) + 'x Smoothing', 'tab:cyan', 'o', 20)
plt.axvline(10 - 2 * i, c='C9')
y = startMP - dropMP * i
plt.axhline(y, c='C9')
plt.savefig(os.path.join('charts', filename), orientation='landscape')
plt.close()
i += 1
# Confusion matrix
print("Generating Confusion matrix.")
confData = lcTable.groupby(['Obj ID']).last()
y_true = confData['Flag']
y_pred = confData['Classification']
labels = ['EB', 'Not EB']
cf_matrix = confusion_matrix(y_true, y_pred)
plt.ylabel('Actual')
plt.xlabel('Predicted')
heatmap = sns.heatmap(cf_matrix, annot=True, xticklabels=labels, yticklabels=labels,
cmap='Blues')
heatmap.figure.savefig(os.path.join('charts', 'confusionMatrix.png'))
print("Confusion matrix complete.\n")
print("\nProcess complete.\n")
end = timer.time()
hours, rem = divmod(end - start, 3600)
minutes, seconds = divmod(rem, 60)
print("{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), seconds))