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