-
Notifications
You must be signed in to change notification settings - Fork 14
/
ext_stetson.py
376 lines (280 loc) · 10.6 KB
/
ext_stetson.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The MIT License (MIT)
# Copyright (c) 2017 Juan Cabral
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# =============================================================================
# DOC
# =============================================================================
r"""These three features are based on the Welch/Stetson variability
index :math:`I` (Stetson, 1996) defined by the equation:
.. math::
I = \sqrt{\frac{1}{n(n-1)}} \sum_{i=1}^n {
(\frac{b_i-\hat{b}}{\sigma_{b,i}})
(\frac{v_i - \hat{v}}{\sigma_{v,i}})}
where \:math:`b_i` and :math:`v_i` are the apparent magnitudes obtained for
the candidate star in two observations closely spaced in time on some occasion
:math:`i`, :math:`\sigma_{b, i}` and :math:`\sigma_{v, i}` are the standard
errors of those magnitudes, :math:`\hat{b}` and \hat{v} are the weighted mean
magnitudes in the two filters, and :math:`n` is the number of observation
pairs.
Since a given frame pair may include data from two filters which did not have
equal numbers of observations overall, the "relative error" is calculated as
follows:
.. math::
\delta = \sqrt{\frac{n}{n-1}} \frac{v-\hat{v}}{\sigma_v}
allowing all residuals to be compared on an equal basis.
"""
# =============================================================================
# IMPORTS
# =============================================================================
import numpy as np
from .core import Extractor
from .ext_slotted_a_length import SlottedA_length
from ..utils import indent
# =============================================================================
# EXTRACTOR CLASS
# =============================================================================
class StetsonJ(Extractor):
__doc__ = (
indent(__doc__)
+ r"""
**StetsonJ**
Stetson J is a robust version of the variability index. It is calculated
based on two simultaneous light curves of a same star and is defined as:
.. math::
J = \sum_{k=1}^n sgn(P_k) \sqrt{|P_k|}
with :math:`P_k = \delta_{i_k} \delta_{j_k}`
For a Gaussian magnitude distribution, J should take a value close to zero:
.. code-block:: pycon
>>> fs = feets.FeatureSpace(only=['StetsonJ'])
>>> features, values = fs.extract(**lc_normal)
>>> dict(zip(features, values))
{'StetsonJ': 0.010765631555204736}
References
----------
.. [richards2011machine] Richards, J. W., Starr, D. L., Butler, N. R.,
Bloom, J. S., Brewer, J. M., Crellin-Quick, A., ... &
Rischard, M. (2011). On machine-learned classification of variable stars
with sparse and noisy time-series data.
The Astrophysical Journal, 733(1), 10. Doi:10.1088/0004-637X/733/1/10.
"""
)
data = [
"aligned_magnitude",
"aligned_magnitude2",
"aligned_error",
"aligned_error2",
]
features = ["StetsonJ"]
warnings = [
(
"The original FATS documentation says that the result of StetsonJ "
"must be ~0 for gausian distribution but the result is ~-0.41"
)
]
def fit(
self,
aligned_magnitude,
aligned_magnitude2,
aligned_error,
aligned_error2,
):
N = len(aligned_magnitude)
mean_mag = np.sum(
aligned_magnitude / (aligned_error * aligned_error)
) / np.sum(1.0 / (aligned_error * aligned_error))
mean_mag2 = np.sum(
aligned_magnitude2 / (aligned_error2 * aligned_error2)
) / np.sum(1.0 / (aligned_error2 * aligned_error2))
sigmap = (
np.sqrt(N * 1.0 / (N - 1))
* (aligned_magnitude[:N] - mean_mag)
/ aligned_error
)
sigmaq = (
np.sqrt(N * 1.0 / (N - 1))
* (aligned_magnitude2[:N] - mean_mag2)
/ aligned_error2
)
sigma_i = sigmap * sigmaq
J = (
1.0
/ len(sigma_i)
* np.sum(np.sign(sigma_i) * np.sqrt(np.abs(sigma_i)))
)
return {"StetsonJ": J}
class StetsonK(Extractor):
__doc__ = (
indent(__doc__)
+ r"""
**StetsonK**
Stetson K is a robust kurtosis measure:
.. math::
\frac{1/N \sum_{i=1}^N |\delta_i|}{\sqrt{1/N \sum_{i=1}^N \delta_i^2}}
where the index :math:`i` runs over all :math:`N` observations available
for the star without regard to pairing. For a Gaussian magnitude
distribution K should take a value close to :math:`\sqrt{2/\pi} = 0.798`:
.. code-block:: pycon
>>> fs = feets.FeatureSpace(only=['StetsonK'])
>>> features, values = fs.extract(**lc_normal)
>>> dict(zip(features, values))
{'StetsonK': 0.79914938521401002}
References
----------
.. [richards2011machine] Richards, J. W., Starr, D. L., Butler, N. R.,
Bloom, J. S., Brewer, J. M., Crellin-Quick, A., ... &
Rischard, M. (2011). On machine-learned classification of variable stars
with sparse and noisy time-series data.
The Astrophysical Journal, 733(1), 10. Doi:10.1088/0004-637X/733/1/10.
"""
)
data = ["magnitude", "error"]
features = ["StetsonK"]
warnings = [
(
"The original FATS documentation says that the result of StetsonK "
"must be 2/pi=0.798 for gausian distribution "
"but the result is ~0.2"
)
]
def fit(self, magnitude, error):
mean_mag = np.sum(magnitude / (error * error)) / np.sum(
1.0 / (error * error)
)
N = len(magnitude)
sigmap = np.sqrt(N * 1.0 / (N - 1)) * (magnitude - mean_mag) / error
K = (
1
/ np.sqrt(N * 1.0)
* np.sum(np.abs(sigmap))
/ np.sqrt(np.sum(sigmap ** 2))
)
return {"StetsonK": K}
class StetsonKAC(Extractor):
__doc__ = (
indent(__doc__)
+ r"""
**StetsonK_AC**
Stetson K applied to the slotted autocorrelation function of the
light-curve.
.. code-block:: pycon
>>> fs = feets.FeatureSpace(only=['SlottedA_length','StetsonK_AC'])
>>> features, values = fs.extract(**lc_normal)
>>> dict(zip(features, values))
{'SlottedA_length': 1.0, 'StetsonK_AC': 0.20917402545294403}
**Parameters**
- ``T``: tau - slot size in days (default=1).
References
----------
.. [kim2011quasi] Kim, D. W., Protopapas, P., Byun, Y. I., Alcock, C.,
Khardon, R., & Trichas, M. (2011). Quasi-stellar object selection
algorithm using time variability and machine learning: Selection of
1620 quasi-stellar object candidates from MACHO Large Magellanic Cloud
database. The Astrophysical Journal, 735(2), 68.
Doi:10.1088/0004-637X/735/2/68.
"""
)
data = ["magnitude", "time", "error"]
features = ["StetsonK_AC"]
params = {"T": 1}
def fit(self, magnitude, time, error, T):
sal = SlottedA_length(T=T)
autocor_vector = sal.start_conditions(magnitude, time, **sal.params)[
-1
]
N_autocor = len(autocor_vector)
sigmap = (
np.sqrt(N_autocor * 1.0 / (N_autocor - 1))
* (autocor_vector - np.mean(autocor_vector))
/ np.std(autocor_vector)
)
K = (
1
/ np.sqrt(N_autocor * 1.0)
* np.sum(np.abs(sigmap))
/ np.sqrt(np.sum(sigmap ** 2))
)
return {"StetsonK_AC": K}
class StetsonL(Extractor):
__doc__ = (
indent(__doc__)
+ r"""
**StetsonL**
Stetson L variability index describes the synchronous variability of
different bands and is defined as:
.. math::
L = \frac{JK}{0.798}
Again, for a Gaussian magnitude distribution, L should take a value close
to zero:
.. code-block:: pycon
>>> fs = feets.FeatureSpace(only=['SlottedL'])
>>> features, values = fs.extract(**lc_normal)
>>> dict(zip(features, values))
{'StetsonL': 0.0085957106316273714}
References
----------
.. [kim2011quasi] Kim, D. W., Protopapas, P., Byun, Y. I., Alcock, C.,
Khardon, R., & Trichas, M. (2011). Quasi-stellar object selection
algorithm using time variability and machine learning: Selection of
1620 quasi-stellar object candidates from MACHO Large Magellanic Cloud
database. The Astrophysical Journal, 735(2), 68.
Doi:10.1088/0004-637X/735/2/68.
"""
)
data = [
"aligned_magnitude",
"aligned_magnitude2",
"aligned_error",
"aligned_error2",
]
features = ["StetsonL"]
def fit(
self,
aligned_magnitude,
aligned_magnitude2,
aligned_error,
aligned_error2,
):
magnitude, magnitude2 = aligned_magnitude, aligned_magnitude2
error, error2 = aligned_error, aligned_error2
N = len(magnitude)
mean_mag = np.sum(magnitude / (error * error)) / np.sum(
1.0 / (error * error)
)
mean_mag2 = np.sum(magnitude2 / (error2 * error2)) / np.sum(
1.0 / (error2 * error2)
)
sigmap = (
np.sqrt(N * 1.0 / (N - 1)) * (magnitude[:N] - mean_mag) / error
)
sigmaq = (
np.sqrt(N * 1.0 / (N - 1)) * (magnitude2[:N] - mean_mag2) / error2
)
sigma_i = sigmap * sigmaq
J = (
1.0
/ len(sigma_i)
* np.sum(np.sign(sigma_i) * np.sqrt(np.abs(sigma_i)))
)
K = (
1
/ np.sqrt(N * 1.0)
* np.sum(np.abs(sigma_i))
/ np.sqrt(np.sum(sigma_i ** 2))
)
return {"StetsonL": J * K / 0.798}