1
|
|
|
# Licensed under a 3-clause BSD style license - see LICENSE |
2
|
|
|
"""Analysis of correlation of light curves.""" |
3
|
|
|
|
4
|
|
|
import logging |
5
|
|
|
|
6
|
|
|
import matplotlib.pyplot as plt |
|
|
|
|
7
|
|
|
import numpy as np |
|
|
|
|
8
|
|
|
|
9
|
|
|
from mutis.lib.correlation import * |
|
|
|
|
10
|
|
|
|
11
|
|
|
__all__ = ["Correlation"] |
12
|
|
|
|
13
|
|
|
log = logging.getLogger(__name__) |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class Correlation: |
|
|
|
|
17
|
|
|
"""Analysis of the correlation of two signals. |
18
|
|
|
|
19
|
|
|
Parameters |
20
|
|
|
---------- |
21
|
|
|
signal1 : :class:`~mutis.signal.Signal` |
22
|
|
|
Values of the time axis. |
23
|
|
|
signal2 : :class:`~mutis.signal.Signal` |
24
|
|
|
Values of the signal axis. |
25
|
|
|
fcorr : :py:class:`~str` |
26
|
|
|
Method used to correlate the signals. |
27
|
|
|
""" |
28
|
|
|
|
29
|
|
|
def __init__(self, signal1, signal2, fcorr): |
30
|
|
|
self.signal1 = signal1 |
31
|
|
|
self.signal2 = signal2 |
32
|
|
|
self.fcorr = fcorr |
33
|
|
|
|
34
|
|
|
self.times = np.array([]) |
35
|
|
|
self.dts = np.array([]) |
36
|
|
|
self.nb = np.array([]) |
|
|
|
|
37
|
|
|
|
38
|
|
|
self.values = None |
39
|
|
|
|
|
|
|
|
40
|
|
|
# TODO: have a much smaller set of attributes |
|
|
|
|
41
|
|
|
self.samples = None |
42
|
|
|
# storage of the significance limits of the correlation |
43
|
|
|
self.l1s = None |
44
|
|
|
self.l2s = None |
45
|
|
|
self.l3s = None |
46
|
|
|
# storage of the uncertainties of the correlation |
47
|
|
|
self.s1s = None |
48
|
|
|
self.s2s = None |
49
|
|
|
self.s3s = None |
50
|
|
|
|
51
|
|
|
# attributes indicating the ranges where the correlations are defined |
52
|
|
|
t1, t2 = self.signal1.times, self.signal2.times |
|
|
|
|
53
|
|
|
self.tmin_full = t2.min() - t1.max() |
54
|
|
|
self.tmax_full = t2.max() - t1.min() |
55
|
|
|
self.t0_full = (self.tmax_full + self.tmin_full) / 2 |
56
|
|
|
self.tmin_same = -(np.max([t1.max() - t1.min(), t2.max() - t2.min()])) / 2 + self.t0_full |
57
|
|
|
self.tmax_same = (np.max([t1.max() - t1.min(), t2.max() - t2.min()])) / 2 + self.t0_full |
58
|
|
|
self.tmin_valid = ( |
59
|
|
|
-(np.max([t1.max() - t1.min(), t2.max() - t2.min()]) - np.min([t1.max() - t1.min(), t2.max() - t2.min()])) |
|
|
|
|
60
|
|
|
/ 2 |
61
|
|
|
+ self.t0_full |
62
|
|
|
) |
63
|
|
|
self.tmax_valid = ( |
64
|
|
|
+(np.max([t1.max() - t1.min(), t2.max() - t2.min()]) - np.min([t1.max() - t1.min(), t2.max() - t2.min()])) |
|
|
|
|
65
|
|
|
/ 2 |
66
|
|
|
+ self.t0_full |
67
|
|
|
) |
68
|
|
|
|
69
|
|
|
def gen_synth(self, samples): |
70
|
|
|
"""Generates the synthetic light curves. |
71
|
|
|
|
72
|
|
|
Generates the specified number `samples` of synthetic light |
73
|
|
|
curves for each signal, to be used to compute the significance |
74
|
|
|
the correlation. |
75
|
|
|
|
76
|
|
|
Parameters |
77
|
|
|
---------- |
78
|
|
|
samples : :py:class:`~int` |
79
|
|
|
Number of synthetic light curves to be generated for each signal. |
80
|
|
|
""" |
81
|
|
|
|
82
|
|
|
self.samples = samples |
83
|
|
|
self.signal1.gen_synth(samples) |
84
|
|
|
self.signal2.gen_synth(samples) |
85
|
|
|
|
86
|
|
|
def gen_corr(self, uncert=True, dsamples=500): |
87
|
|
|
"""Generates the correlation of the signals. |
88
|
|
|
|
89
|
|
|
Generates the correlation of the signals, and computes their |
90
|
|
|
confidence level from the synthetic light curves, which must |
91
|
|
|
have been generated before. |
92
|
|
|
""" |
93
|
|
|
|
94
|
|
|
if uncert and self.signal1.dvalues is None: |
95
|
|
|
log.error("uncert is True but no uncertainties for Signal 1 were specified, setting uncert to False") |
|
|
|
|
96
|
|
|
uncert = False |
97
|
|
|
if uncert and self.signal2.dvalues is None: |
98
|
|
|
log.error("uncert is True but no uncertainties for Signal 2 were specified, setting uncert to False") |
|
|
|
|
99
|
|
|
uncert = False |
100
|
|
|
|
101
|
|
|
if len(self.times) == 0 or len(self.dts) == 0: |
102
|
|
|
raise Exception( |
103
|
|
|
"You need to define the times on which to calculate the correlation." |
104
|
|
|
"Please use gen_times() or manually set them." |
105
|
|
|
) |
106
|
|
|
|
107
|
|
|
# TODO: refactor if/elif with a helper function |
|
|
|
|
108
|
|
|
mc_corr = np.empty((self.samples, self.times.size)) |
109
|
|
|
if uncert: |
110
|
|
|
mc_sig = np.empty((dsamples, self.times.size)) |
111
|
|
|
|
112
|
|
|
if self.fcorr == "welsh_ab": |
113
|
|
|
for n in range(self.samples): |
|
|
|
|
114
|
|
|
mc_corr[n] = welsh_ab( |
115
|
|
|
self.signal1.times, |
116
|
|
|
self.signal1.synth[n], |
117
|
|
|
self.signal2.times, |
118
|
|
|
self.signal2.synth[n], |
119
|
|
|
self.times, |
120
|
|
|
self.dts, |
121
|
|
|
) |
122
|
|
|
if uncert: |
123
|
|
|
for n in range(dsamples): |
|
|
|
|
124
|
|
|
mc_sig[n] = welsh_ab( |
125
|
|
|
self.signal1.times, |
126
|
|
|
self.signal1.values + self.signal1.dvalues * np.random.randn(self.signal1.values.size), |
|
|
|
|
127
|
|
|
self.signal2.times, |
128
|
|
|
self.signal2.values + self.signal2.dvalues * np.random.randn(self.signal2.values.size), |
|
|
|
|
129
|
|
|
self.times, |
130
|
|
|
self.dts, |
131
|
|
|
) |
132
|
|
|
self.values = welsh_ab( |
133
|
|
|
self.signal1.times, |
134
|
|
|
self.signal1.values, |
135
|
|
|
self.signal2.times, |
136
|
|
|
self.signal2.values, |
137
|
|
|
self.times, |
138
|
|
|
self.dts, |
139
|
|
|
) |
140
|
|
|
elif self.fcorr == "kroedel_ab": |
141
|
|
|
for n in range(self.samples): |
|
|
|
|
142
|
|
|
mc_corr[n] = kroedel_ab( |
143
|
|
|
self.signal1.times, |
144
|
|
|
self.signal1.synth[n], |
145
|
|
|
self.signal2.times, |
146
|
|
|
self.signal2.synth[n], |
147
|
|
|
self.times, |
148
|
|
|
self.dts, |
149
|
|
|
) |
150
|
|
|
if uncert: |
151
|
|
|
for n in range(dsamples): |
|
|
|
|
152
|
|
|
mc_sig[n] = kroedel_ab( |
153
|
|
|
self.signal1.times, |
154
|
|
|
self.signal1.values + self.signal1.dvalues * np.random.randn(self.signal1.values.size), |
|
|
|
|
155
|
|
|
self.signal2.times, |
156
|
|
|
self.signal2.values + self.signal2.dvalues * np.random.randn(self.signal2.values.size), |
|
|
|
|
157
|
|
|
self.times, |
158
|
|
|
self.dts, |
159
|
|
|
) |
160
|
|
|
self.values = kroedel_ab( |
161
|
|
|
self.signal1.times, |
162
|
|
|
self.signal1.values, |
163
|
|
|
self.signal2.times, |
164
|
|
|
self.signal2.values, |
165
|
|
|
self.times, |
166
|
|
|
self.dts, |
167
|
|
|
) |
168
|
|
|
elif self.fcorr == "numpy": |
169
|
|
|
for n in range(self.samples): |
|
|
|
|
170
|
|
|
mc_corr[n] = nindcf( |
171
|
|
|
self.signal1.times, |
172
|
|
|
self.signal1.synth[n], |
173
|
|
|
self.signal2.times, |
174
|
|
|
self.signal2.synth[n], |
175
|
|
|
) |
176
|
|
|
if uncert: |
177
|
|
|
for n in range(dsamples): |
|
|
|
|
178
|
|
|
mc_sig[n] = nindcf( |
179
|
|
|
self.signal1.times, |
180
|
|
|
self.signal1.values + self.signal1.dvalues * np.random.randn(self.signal1.values.size), |
|
|
|
|
181
|
|
|
self.signal2.times, |
182
|
|
|
self.signal2.values + self.signal2.dvalues * np.random.randn(self.signal2.values.size), |
|
|
|
|
183
|
|
|
) |
184
|
|
|
self.values = nindcf( |
185
|
|
|
self.signal1.times, |
186
|
|
|
self.signal1.values, |
187
|
|
|
self.signal2.times, |
188
|
|
|
self.signal2.values, |
189
|
|
|
) |
190
|
|
|
else: |
191
|
|
|
raise Exception("Unknown method " + self.fcorr + " for correlation.") |
192
|
|
|
|
193
|
|
|
self.l3s = np.percentile(mc_corr, [0.135, 99.865], axis=0) |
194
|
|
|
self.l2s = np.percentile(mc_corr, [2.28, 97.73], axis=0) |
195
|
|
|
self.l1s = np.percentile(mc_corr, [15.865, 84.135], axis=0) |
196
|
|
|
|
197
|
|
|
if uncert: |
198
|
|
|
self.s3s = np.percentile(mc_sig, [0.135, 99.865], axis=0) |
|
|
|
|
199
|
|
|
self.s2s = np.percentile(mc_sig, [2.28, 97.73], axis=0) |
200
|
|
|
self.s1s = np.percentile(mc_sig, [15.865, 84.135], axis=0) |
201
|
|
|
|
202
|
|
|
def gen_times(self, ftimes="canopy", *args, **kwargs): |
|
|
|
|
203
|
|
|
"""Sets times and bins using the method defined by ftimes parameter. |
204
|
|
|
|
205
|
|
|
Parameters |
206
|
|
|
---------- |
207
|
|
|
ftimes : :py:class:`~str` |
208
|
|
|
Method used to bin the time interval of the correlation. |
209
|
|
|
Possible values are: |
210
|
|
|
- "canopy": Computes a binning as dense as possible, with |
211
|
|
|
variable bin width and (with a minimum and a maximum |
212
|
|
|
resolution) and a minimum statistic. |
213
|
|
|
- "rawab": Computes a binning with variable bin width, |
214
|
|
|
a given step, maximum bin size and a minimum statistic. |
215
|
|
|
- "uniform": Computes a binning with uniform bin width |
216
|
|
|
and a minimum statistic. |
217
|
|
|
- "numpy": Computes a binning suitable for method='numpy'. |
218
|
|
|
""" |
219
|
|
|
if ftimes == "canopy": |
220
|
|
|
self.times, self.dts, self.nb = gen_times_canopy(self.signal1.times, self.signal2.times, *args, **kwargs) |
|
|
|
|
221
|
|
|
elif ftimes == "rawab": |
222
|
|
|
self.times, self.dts, self.nb = gen_times_rawab(self.signal1.times, self.signal2.times, *args, **kwargs) |
|
|
|
|
223
|
|
|
elif ftimes == "uniform": |
224
|
|
|
self.times, self.dts, self.nb = gen_times_uniform(self.signal1.times, self.signal2.times, *args, **kwargs) |
|
|
|
|
225
|
|
|
elif ftimes == "numpy": |
226
|
|
|
t1, t2 = self.signal1.times, self.signal1.times |
|
|
|
|
227
|
|
|
dt = np.max([(t1.max() - t1.min()) / t1.size, (t2.max() - t2.min()) / t2.size]) |
|
|
|
|
228
|
|
|
n1 = int(np.ptp(t1) / dt * 10.0) |
|
|
|
|
229
|
|
|
n2 = int(np.ptp(t1) / dt * 10.0) |
|
|
|
|
230
|
|
|
self.times = np.linspace(self.tmin_full, self.tmax_full, n1 + n2 - 1) |
231
|
|
|
self.dts = np.full(self.times.size, (self.tmax_full - self.tmin_full) / (n1 + n2)) |
232
|
|
|
else: |
233
|
|
|
raise Exception("Unknown method " + ftimes + ", please indicate how to generate times.") |
234
|
|
|
|
235
|
|
|
def plot_corr(self, uncert=True, ax=None, legend=False): |
|
|
|
|
236
|
|
|
"""Plots the correlation of the signals. |
237
|
|
|
|
238
|
|
|
Plots the correlation of the signal, and the confidence limits |
239
|
|
|
computed from the synthetic curves. |
240
|
|
|
|
241
|
|
|
Parameters |
242
|
|
|
---------- |
243
|
|
|
ax : :class:`matplotlib.axes.Axes` |
244
|
|
|
Axes to be used (default None, it creates a new axes). |
245
|
|
|
legend : :py:class:`~bool` |
246
|
|
|
Whether to add a legend indicating the confidence levels. |
247
|
|
|
""" |
248
|
|
|
|
249
|
|
|
# TODO: develop a plotting object for plots |
|
|
|
|
250
|
|
|
# this will considerably shorten the |
251
|
|
|
# number of attributes of this class |
252
|
|
|
|
253
|
|
|
if uncert and self.signal1.dvalues is None: |
254
|
|
|
log.error("uncert is True but no uncertainties for Signal 1 were specified, setting uncert to False") |
|
|
|
|
255
|
|
|
uncert = False |
256
|
|
|
if uncert and self.signal2.dvalues is None: |
257
|
|
|
log.error("uncert is True but no uncertainties for Signal 2 were specified, setting uncert to False") |
|
|
|
|
258
|
|
|
uncert = False |
259
|
|
|
|
|
|
|
|
260
|
|
|
# plt.figure() |
261
|
|
|
if ax is None: |
262
|
|
|
ax = plt.gca() |
263
|
|
|
|
264
|
|
|
ax.plot(self.times, self.l1s[0], "c-.") |
265
|
|
|
ax.plot(self.times, self.l1s[1], "c-.", label=r"$1\sigma$") |
266
|
|
|
ax.plot(self.times, self.l2s[0], "k--") |
267
|
|
|
ax.plot(self.times, self.l2s[1], "k--", label=r"$2\sigma$") |
268
|
|
|
ax.plot(self.times, self.l3s[0], "r-") |
269
|
|
|
ax.plot(self.times, self.l3s[1], "r-", label=r"$3\sigma$") |
270
|
|
|
ax.plot(self.times, self.values, "b.--", lw=1) |
271
|
|
|
|
272
|
|
|
# full limit |
273
|
|
|
ax.axvline(x=self.tmin_full, ymin=-1, ymax=+1, color="red", linewidth=4, alpha=0.5) |
274
|
|
|
ax.axvline(x=self.tmax_full, ymin=-1, ymax=+1, color="red", linewidth=4, alpha=0.5) |
275
|
|
|
# same limit |
276
|
|
|
ax.axvline(x=self.tmin_same, ymin=-1, ymax=+1, color="black", linewidth=2, alpha=0.5) |
277
|
|
|
ax.axvline(x=self.tmax_same, ymin=-1, ymax=+1, color="black", linewidth=2, alpha=0.5) |
278
|
|
|
# valid limit |
279
|
|
|
ax.axvline(x=self.tmin_valid, ymin=-1, ymax=+1, color="cyan", linewidth=1, alpha=0.5) |
280
|
|
|
ax.axvline(x=self.tmax_valid, ymin=-1, ymax=+1, color="cyan", linewidth=1, alpha=0.5) |
281
|
|
|
|
282
|
|
|
|
283
|
|
|
if uncert: |
284
|
|
|
ax.fill_between(x=self.times, y1=self.s1s[0], y2=self.s1s[1], color="b", alpha=0.5) |
285
|
|
|
ax.fill_between(x=self.times, y1=self.s2s[0], y2=self.s2s[1], color="b", alpha=0.3) |
286
|
|
|
ax.fill_between(x=self.times, y1=self.s3s[0], y2=self.s3s[1], color="b", alpha=0.1) |
287
|
|
|
|
288
|
|
|
if legend: |
289
|
|
|
ax.legend() |
290
|
|
|
|
291
|
|
|
# plt.show() |
292
|
|
|
return ax |
293
|
|
|
|
294
|
|
|
def plot_times(self, rug=False): |
295
|
|
|
"""Plots the time binning generated previously. |
296
|
|
|
|
297
|
|
|
Plots the number of total bins, their distribution and the |
298
|
|
|
number of points in each bin for the generated time binning, |
299
|
|
|
previously generated with Correlation().gen_times(...). |
300
|
|
|
|
301
|
|
|
Parameters |
302
|
|
|
---------- |
303
|
|
|
rug : :py:class:`~bool` |
304
|
|
|
Whether to make a rug plot just below the binning, to make |
305
|
|
|
it easier to visually understand the density and distribution |
306
|
|
|
of the generated bins. |
307
|
|
|
|
308
|
|
|
""" |
309
|
|
|
|
310
|
|
|
# TODO: develop a plotting object for plots |
|
|
|
|
311
|
|
|
# this will considerably shorten the |
312
|
|
|
# number of attributes of this class |
313
|
|
|
|
314
|
|
|
fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True) |
|
|
|
|
315
|
|
|
tab, dtab, nab = self.times, self.dts, self.nb |
316
|
|
|
|
317
|
|
|
fig.suptitle("Total bins: {:d}".format(self.times.size)) |
318
|
|
|
ax[0].plot(tab, nab, "b.") |
319
|
|
|
ax[0].errorbar(x=tab, y=nab, xerr=dtab / 2, fmt="none") |
320
|
|
|
ax[0].set_ylabel("$n_i$") |
321
|
|
|
ax[0].grid() |
322
|
|
|
ax[0].axvline(x=self.tmin_full, ymin=-1, ymax=+1, color="red", linewidth=4, alpha=0.5) |
323
|
|
|
ax[0].axvline(x=self.tmax_full, ymin=-1, ymax=+1, color="red", linewidth=4, alpha=0.5) |
324
|
|
|
ax[0].axvline(x=self.tmin_same, ymin=-1, ymax=+1, color="black", linewidth=2, alpha=0.5) |
325
|
|
|
ax[0].axvline(x=self.tmax_same, ymin=-1, ymax=+1, color="black", linewidth=2, alpha=0.5) |
326
|
|
|
ax[0].axvline(x=self.tmin_valid, ymin=-1, ymax=+1, color="cyan", linewidth=1, alpha=0.5) |
327
|
|
|
ax[0].axvline(x=self.tmax_valid, ymin=-1, ymax=+1, color="cyan", linewidth=1, alpha=0.5) |
328
|
|
|
ax[1].plot(tab, dtab, "b.") |
329
|
|
|
ax[1].set_ylabel("$dt_i$") |
330
|
|
|
# ax[1].grid() |
331
|
|
|
ax[1].axvline(x=self.tmin_full, ymin=-1, ymax=+1, color="red", linewidth=4, alpha=0.5) |
332
|
|
|
ax[1].axvline(x=self.tmax_full, ymin=-1, ymax=+1, color="red", linewidth=4, alpha=0.5) |
333
|
|
|
ax[1].axvline(x=self.tmin_same, ymin=-1, ymax=+1, color="black", linewidth=2, alpha=0.5) |
334
|
|
|
ax[1].axvline(x=self.tmax_same, ymin=-1, ymax=+1, color="black", linewidth=2, alpha=0.5) |
335
|
|
|
ax[1].axvline(x=self.tmin_valid, ymin=-1, ymax=+1, color="cyan", linewidth=1, alpha=0.5) |
336
|
|
|
ax[1].axvline(x=self.tmax_valid, ymin=-1, ymax=+1, color="cyan", linewidth=1, alpha=0.5) |
337
|
|
|
|
338
|
|
|
if rug is True: |
339
|
|
|
for t in self.times: |
|
|
|
|
340
|
|
|
ax[1].axvline(x=t, ymin=0, ymax=0.2, color="black", linewidth=0.8, alpha=1.0) |
341
|
|
|
# ax[1].plot(self.t, ax[1].get_ylim()[0]+np.zeros(self.t.size), 'k|', alpha=0.8, lw=1) |
342
|
|
|
|
343
|
|
|
ax[1].grid() |
344
|
|
|
# fig.show() |
345
|
|
|
|
346
|
|
|
def plot_signals(self, ax=None): |
|
|
|
|
347
|
|
|
"""Plots the signals involved in this correlation. |
348
|
|
|
|
349
|
|
|
Plots the signals involved in this correlation, in the same window |
350
|
|
|
but with different twin y-axes and different colors. |
351
|
|
|
|
352
|
|
|
Parameters |
353
|
|
|
---------- |
354
|
|
|
ax : :py:class:`~matplotlib.axes.Axes` |
355
|
|
|
Axes to be used for plotting. |
356
|
|
|
""" |
357
|
|
|
|
358
|
|
|
# TODO: develop a plotting object for plots |
|
|
|
|
359
|
|
|
# this will considerably shorten the |
360
|
|
|
# number of attributes of this class |
361
|
|
|
|
362
|
|
|
if ax is None: |
363
|
|
|
ax = plt.gca() |
364
|
|
|
|
365
|
|
|
ax.plot(self.signal1.times, self.signal1.values, "b.-", lw=1, alpha=0.4) |
366
|
|
|
ax.tick_params(axis="y", labelcolor="b") |
367
|
|
|
ax.set_ylabel("sig 1", color="b") |
368
|
|
|
|
369
|
|
|
ax2 = ax.twinx() |
370
|
|
|
ax2.plot(self.signal2.times, self.signal2.values, "r.-", lw=1, alpha=0.4) |
371
|
|
|
ax2.tick_params(axis="y", labelcolor="r") |
372
|
|
|
ax2.set_ylabel("sig 2", color="r") |
373
|
|
|
|