/
mark_flares.py
279 lines (224 loc) · 10.1 KB
/
mark_flares.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
import numpy as np
from tqdm import tqdm
import more_itertools as mit
from astropy import units as u
from astropy.table import Table
from scipy.signal import medfilt
from scipy.signal import find_peaks
from scipy.optimize import minimize
from scipy.interpolate import interp1d
from .utils import *
__all__ = ['FitFlares']
class FitFlares(object):
"""
Uses the predictions from the neural network
and identifies flaring events based on consecutive
points. Users define a given probability threshold
for accpeting a flare event as real.
"""
def __init__(self, id, time, flux, flux_err, predictions):
"""
Uses the times, fluxes, and predictions defined
in stella.ConvNN to identify and fit flares, as
well as do injection-recovery for completeness.
Parameters
----------
time : np.array
Array of times to find flares on.
flux : np.array
Array of light curves.
flux_err : np.array
Array of errors on light curves.
predictions : np.array
Array of predictions for each light curve
passed in.
Attributes
----------
ids : np.array
time : np.ndarray
flux : np.ndarray
flux_err : np.ndarray
predictions : np.ndarray
"""
self.IDs = id
self.time = time
self.flux = flux
self.flux_err = flux_err
self.predictions = predictions
def group_inds(self, values):
"""
Groups regions marked as flares (> prob_threshold) for
flare fitting. Indices within 4 of each other are grouped
as one flare.
Returns
-------
results: np.ndarray
An array of arrays, which are groups of indices
supposedly attributed with a single flare.
"""
results = []
for i, v in enumerate(values):
if i == 0:
mini = maxi = v
temp = [v]
else:
# SETS 4 CADENCE LIMIT
if (np.abs(v-maxi) <= 3):
temp.append(v)
if v > maxi:
maxi = v
if v < mini:
mini = v
else:
results.append(temp)
mini = maxi = v
temp = [v]
# GETS THE LAST GROUP
if i == len(values)-1:
results.append(temp)
return np.array(results)
def get_init_guesses(self, groupings, time, flux, err, prob,
maskregion, region):
"""
Guesses at the initial t0 and amplitude based on
probability groups.
Parameters
----------
groupings : np.ndarray
Group of indices for a single flare event.
time : np.array
flux : np.array
err : np.array
prob : np.array
Returns
-------
tpeaks : np.ndarray
Array of tpeaks for each flare group.
amps : np.ndarray
Array of amplitudes at each tpeak.
"""
tpeaks = np.array([])
ampls = np.array([])
if len(groupings) > 0:
for g in groupings:
if g[0]-region < 0:
subreg = np.arange(0, g[-1]+region, 1, dtype=int)
elif g[-1]+region > len(time):
subreg = np.arange(len(time)-region, len(time), 1, dtype=int)
else:
subreg = np.arange(g[0]-region, g[-1]+region, 1, dtype=int)
# LOOKS AT REGION AROUND FLARE
subt = time[subreg]+0.0
subf = flux[subreg]+0.0
sube = err[subreg]+0.0
subp = prob[subreg]+0.0
doubcheck = np.where(subp>=self.threshold)[0]
# FINDS HIGHEST "PROBABILITY" IN FLARE
if len(doubcheck) > 1:
peak = np.argmax(subf[doubcheck])
t0 = subt[doubcheck[peak]]
amp = subf[doubcheck[peak]]
else:
t0 = subt[doubcheck]
amp = subf[doubcheck]
tpeaks = np.append(tpeaks, t0)
ampls = np.append(ampls, amp)
return tpeaks, ampls
def identify_flare_peaks(self, threshold=0.5):
"""
Finds where the predicted value is above the threshold
as a flare candidate. Groups consecutive indices as one
flaring event.
Parameters
----------
threshold : float, optional
The probability threshold for believing an event
is a flare. Default is 0.5.
Attributes
----------
treshold : float
flare_table : astropy.table.Table
A table of flare times, amplitudes, and equivalent
durations. Equivalent duration given in units of days.
"""
self.threshold = threshold
def chiSquare(var, x, y, yerr, t0_ind):
""" Chi-square fit for flare parameters. """
amp, rise, decay = var
m, p = flare_lightcurve(x, t0_ind, amp, rise, decay)
return np.sum( (y-m)**2.0 / yerr**2.0 )
table = Table(names=['Target_ID', 'tpeak', 'amp', 'ed_s',
'rise', 'fall', 'prob'])
kernel_size = 15
kernel_size1 = 21
for i in tqdm(range(len(self.IDs)), desc='Finding & Fitting Flares'):
time = self.time[i]+0.0
flux = self.flux[i]+0.0
err = self.flux_err[i]+0.0
prob = self.predictions[i]+0.0
where_prob_higher = np.where(prob >= threshold)[0]
groupings = self.group_inds(where_prob_higher)
tpeaks, amps = self.get_init_guesses(groupings, time, flux,
err, prob, 2, 50)
# FITS PARAMETERS TO FLARE
for tp, amp in zip(tpeaks,amps):
# CASES FOR HANDLING BIG FLARES
if amp > 1.3:
region = 400
maskregion = 150
else:
region = 40
maskregion = 10
where = np.where(time >= tp)[0][0]
subt = time[where-region:where+region]
subf = flux[where-region:where+region]
sube = err[ where-region:where+region]
subp = prob[where-region:where+region]
amp_ind = int(len(subf)/2)
mask = np.zeros(len(subt))
mask[int(amp_ind-maskregion/2.):int(amp_ind+maskregion)] = 1
m = mask == 0
if len(mask) > 10:
func = interp1d(subt[m], medfilt(subf[m], kernel_size=kernel_size))
func1 = interp1d(subt, medfilt(subf, kernel_size=kernel_size1))
# REMOVES LOCAL STELLAR VARIABILITY TO FIT FLARE
detrended = subf/func(subt)
std = np.nanstd(detrended[m])
med = np.nanmedian(detrended[m])
detrend_with_flare = subf/func1(subt)
std1 = np.nanstd(detrend_with_flare)
med1 = np.nanmedian(detrend_with_flare)
amp = subf[amp_ind]
amp1 = detrended[amp_ind]
if amp > 1.5:
decay_guess = 0.008
rise_guess = 0.003
else:
decay_guess = 0.001
rise_guess = 0.0001
# Checks if amplitude of flare is 1.5sig, and the next 2 consecutive points < amp
if ( (amp1 > (med+1.5*std) ) and (subf[amp_ind+1] <= amp) and (subf[amp_ind+2] <= amp) and
(subf[amp_ind-1] <= amp)):
# Checks if next 2 consecutive points are > 1sig above
if (detrended[amp_ind+1] >= (med1+std1)):# and (detrended[amp_ind+2] >= (med1+std1)):
# Checks if point before amp < amp and that it isn't catching noise
if (subf[amp_ind-1] < amp) and ((amp-subf[-1]) < 2):
amp1 -= med
x = minimize(chiSquare, x0=[amp1, rise_guess, decay_guess],
bounds=((amp1-0.1,amp1+0.1), (0.0001,0.01),
(0.0005, 0.01)),
args=(subt[int(len(subt)/2-maskregion):int(len(subt)/2+maskregion)],
detrended[int(len(detrended)/2-maskregion):int(len(detrended)/2+maskregion)],
sube[int(len(sube)/2-maskregion):int(len(sube)/2+maskregion)],
int(len(subt[int(len(subt)/2-maskregion):int(len(subt)/2+maskregion)])/2)),
method='L-BFGS-B')
if x.x[0] > 1.5 or (x.x[0]<1.5 and x.x[2]<0.4):
fm, params = flare_lightcurve(subt, amp_ind, np.nanmedian([amp1, x.x[0]]),
x.x[1], x.x[2])
dur = np.trapz(fm-1, subt) * u.day
params[1] = detrended[amp_ind]
params[2] = dur.to(u.s).value
params = np.append(params, subp[amp_ind])
params = np.append(np.array([self.IDs[i]]), params)
table.add_row(params)
self.flare_table = table[table['amp'] > 1.002]