| 1 |  |  | # Licensed under a 3-clause BSD style license - see LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | """Methods for synthetic generation of light curves.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | import nfft | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import numpy as np | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import scipy.signal as scipy_signal | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | __all__ = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     "lc_gen_samp", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     "lc_gen_psd_nft", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     "lc_gen_ou", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     "lc_gen_psd_lombscargle", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     "lc_gen_psd_fft", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | log = logging.getLogger(__name__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | def lc_gen_samp(signs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     """Generation by sampling np.random.choice with same mean and std""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     return np.random.choice(signs, signs.size) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | def lc_gen_ou(theta, mu, sigma, times, scale=None, loc=None): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     """Generation from an OU process integrating the stochastic differential equation.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     width = 100 * times.size | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     dt = (max(times) - min(times)) / width | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     s2 = np.empty(times.size) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     s2[0] = mu  # should get it from OU.rvs()!!!! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     for i in range(1, times.size): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         ti = times[i - 1] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         y = s2[i - 1] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         while ti < times[i]: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             y = y + dt * (theta * (mu - y) + sigma * y * np.random.randn() / np.sqrt(dt)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             ti = ti + dt | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         s2[i] = y | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     if scale is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         s2 = scale * s2 / np.std(s2) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     if loc is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         s2 = s2 - np.mean(s2) + loc | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     return s2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | def lc_gen_psd_c(ts, values, times): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     """Generation using interpolated PSD for light curves with similar PSD, mean and std.""" | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     f, p = scipy_signal.welch(values, nperseg=ts.size / 2) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     fp = np.linspace(min(f), max(f), times.size // 2 + 1) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     pp = np.interp(fp, f, p) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     fft = np.sqrt(2 * pp * pp.size) * np.exp(1j * 2 * np.pi * np.random.random(pp.size)) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     s2 = np.fft.irfft(fft, n=values.size) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     a = values.std() / s2.std() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     b = values.mean() - a * s2.mean() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     s2 = a * s2 + b | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     return s2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | def lc_gen_psd_fft(values): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     """Generation using welch and fft with similar PSD, mean and std.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     # this is not valid for non-uniform times (see PSD tests for a comparison) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     f, pxx = scipy_signal.welch(values) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     # fft2 = np.sqrt(2*Pxx*Pxx.size)*np.exp(1j*2*pi*np.random.randn(Pxx.size)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     fft2 = np.sqrt(2 * pxx * pxx.size) * np.exp(1j * 2 * np.pi * np.random.random(pxx.size)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     s2 = np.fft.irfft(fft2, n=values.size) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     a = values.std() / s2.std() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     b = values.mean() - a * s2.mean() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     s2 = a * s2 + b | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     return s2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | def lc_gen_psd_lombscargle(times, values): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     """Description goes here.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     if values.size % 2 != 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         sigp = values[:-1] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         tp = times[:-1] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         sigp = values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         tp = times | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     N = values.size | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     # k = np.arange(-N/2, N/2) no bc scipy_signal.lombscargle does not support freq zero | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     k = np.linspace(-N / 2, N / 2 - 1 + 1e-6, N) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     freqs = k / 2 / np.pi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     pxx = scipy_signal.lombscargle(tp, sigp, freqs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     # build random phase to get real signal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     phase = np.random.random(pxx.size // 2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     phase = np.concatenate((-np.flip(phase), [0], phase[:-1])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     fft2 = np.sqrt(2 * pxx * pxx.size) * np.exp(1j * 2 * np.pi * phase) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     s2 = nfft.nfft((times - (times.max() + times.min()) / 2) / np.ptp(times), fft2, N, use_fft=True) / N | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     # fix small deviations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     a = values.std() / s2.std() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     b = values.mean() - a * s2.mean() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     s2 = a * s2 + b | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     return s2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | def lc_gen_psd_nft(times, values): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     k = np.arange(-times.size // 2, times.size / 2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     N = k.size | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     nft = nfft.nfft_adjoint((times - (times.max() + times.min()) / 2) / np.ptp(times), values, N, use_fft=True) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     # build random phase to get real signal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     phase = np.random.random(N // 2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     phase = np.concatenate((-np.flip(phase), [0], phase[:-1])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     fft2 = np.abs(nft) * np.exp(1j * 2 * np.pi * phase) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     s2 = nfft.nfft((times - (times.max() + times.min()) / 2) / np.ptp(times), fft2, use_fft=True) / N | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     s2 = np.real(s2)  # np.real to fix small imaginary part from numerical error | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     # fix small mean, std difference from numerical error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     a = values.std() / s2.std() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     b = values.mean() - a * s2.mean() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     s2 = a * s2 + b | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  |     return s2 | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |  |