| 1 |  |  | # Licensed under a 3-clause BSD style license - see LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | """Synthetic generation of light curves.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | import matplotlib.pyplot as plt | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import numpy as np | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import scipy.optimize as scipy_optimize | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | import scipy.signal as scipy_signal | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | import scipy.special as scipy_special | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | import scipy.stats as scipy_stats | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | from matplotlib.offsetbox import AnchoredText | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | from mutis.lib.signal import * | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | __all__ = ["Signal"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | log = logging.getLogger(__name__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class Signal: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     """Analysis and generation of a signal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     Description goes here. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     Parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     ---------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     times : :class:`numpy.ndarray` or :class:`pandas.Series` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         Values of the time axis. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     values : :class:`numpy.ndarray` or :class:`pandas.Series` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         Values of the signal axis. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     fgen : :class:`str` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         Method used to generate the synthetic signal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     def __init__(self, times, values, dvalues=None, fgen=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         self.times = np.array(times) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         self.values = np.array(values) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         self.fgen = fgen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         self.dvalues = np.array(dvalues) if dvalues is not None else None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         self.synth = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         # TODO make attributes below specific of OU method / not the entire class | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         self.OU_theta = None | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         self.OU_mu = None | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         self.OU_sigma = None | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     def plot(self, ax=None): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if ax is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ax = plt.gca() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return ax.plot(self.times, self.values, ".-", lw=1, alpha=0.7) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     def gen_synth(self, samples): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         """Description goes here.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         self.synth = np.empty((samples, self.times.size)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         for n in range(samples): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             self.synth[n] = fgen_wrapper(fgen=self.fgen, t=self.times, y=self.values, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                          fgen_params={'theta':self.OU_theta, 'mu':self.OU_mu, 'sigma':self.OU_sigma}) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     def OU_fit(self, bins=None, rang=None, a=1e-5, b=100): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         """Fit the signal to an OU stochastic process, using several statistical approaches. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         This function tries to fit the signal to an OU stochastic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         process using both basic curve fitting and the Maximum | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         Likelihood Estimation method, and returns some plots of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         the signals and its properties, and the estimated parameters. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         # TODO: make a generic fit method | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         res = dict() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         y = self.values | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         t = self.times | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         dy = np.diff(y) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         dt = np.diff(t) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         # estimate sigma | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             sigma_est = (np.nanmean(dy ** 2 / y[:-1] ** 2 / dt)) ** 0.5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             res["sigma_est"] = sigma_est | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         except Exception as e: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             log.error(f"Could not estimate sigma: {e}") | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         # plot histogram | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         if bins is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             bins = np.int(y.size ** 0.5 / 1.5)  # bins='auto' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if rang is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             rang = (np.percentile(y, 0), np.percentile(y, 99)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         p, x = np.histogram(y, density=True, bins=bins, range=rang)  # bins='sqrt') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         x = (x + np.roll(x, -1))[:-1] / 2.0 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         plt.subplots() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         plt.hist(y, density=True, alpha=0.75, bins=bins, range=rang) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         plt.plot(x, p, "r-", alpha=0.5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         anchored_text = AnchoredText( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             f"mean    {np.mean(y):.2g} \n " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             f"median  {np.median(y):.2g} \n " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             f"mode    {scipy_stats.mode(y)[0][0]:.2g} \n " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             f"std     {np.std(y):.2g} \n " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             f"var     {np.var(y):.2g}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             loc="upper right", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         plt.gca().add_artist(anchored_text) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         # curve_fit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             popt, pcov = scipy_optimize.curve_fit(f=self.pdf, xdata=x, ydata=p) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             # print('curve_fit: (l, mu)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             # print('popt: ') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             # print(popt) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             # print('pcov: ') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             # print(np.sqrt(np.diag(pcov))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             x_c = np.linspace(1e-5, 1.1 * np.max(x), 1000) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             plt.plot(x_c, self.pdf(x_c, *popt), "k-", label="curve_fit", alpha=0.8) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             res["curve_fit"] = (popt, np.sqrt(np.diag(pcov))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         except Exception as e: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             log.error(f"Some error fitting with curve_fit {e}") | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         # fit pdf with MLE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         # TODO: place outside as a helper class | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         #       mu here is different than self.mu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         class OU(scipy_stats.rv_continuous): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             def _pdf(self, x, l, mu): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 return ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                     (l * mu) ** (1 + l) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                     / scipy_special.gamma(1 + l) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     * np.exp(-l * mu / x) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                     / x ** (l + 2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             fit = OU(a=a, b=np.percentile(y, b)).fit(y, 1, 1, floc=0, fscale=1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             # print('MLE fit: (l, mu)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             # print(fit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             x_c = np.linspace(0, 1.1 * np.max(x), 1000) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             plt.plot(x_c, self.pdf(x_c, fit[0], fit[1]), "k-.", label="MLE", alpha=0.8) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             res["MLE_fit"] = fit[:-2] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         except Exception as e: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             log.error(f"Some error fitting with MLE {e}") | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         plt.legend(loc="lower right") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         # plt.show() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         # estimate theta (from curve_fit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             res["th_est1"] = fit[0] * sigma_est ** 2 / 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         except NameError as e: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             res["th_est1"] = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         # estimate theta (from MLE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             res["th_est2"] = popt[0] * sigma_est ** 2 / 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         except NameError as e: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             res["th_est2"] = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         return res | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |          | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |          | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     def check_gen(self, fgen=None, fgen_params=None, fpsd="lombscargle", **axes): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         """Check the generation of synthetic signals. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         This function checks the generation of a synthetic light curve. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |          | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         Parameters: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         ----------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         fgen: :str: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             A valid fgen method name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         fgen_params: :dict: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             Parameters for fgen (e.g. for fgen='lc_gen_ou', a dict containing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             values for theta, mu and sigma). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         Returns: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         -------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         axes: :tuple: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             Tuple of three axes, on which: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             - The first plot show both the original signal and the synthetic one. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             - The second plot shows the histogram of the values taken by both signals. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             - The third plot shows their PSD. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |          | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         t, y = self.times, self.values | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         y2 = fgen_wrapper(fgen=fgen, t=t, y=y, fgen_params=fgen_params) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |          | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |          | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         if ("ax1" not in axes) or ("ax2" not in axes) or ("ax3" not in axes): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |             fig, (axes["ax1"], axes["ax2"], axes["ax3"]) = plt.subplots( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |                 nrows=1, ncols=3, figsize=(20, 4) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         # plot the two signals | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         axes["ax1"].plot(t, y, "b-", label="orig", lw=0.5, alpha=0.8) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         # ax1p = ax1.twinx() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         axes["ax1"].plot(t, y2, "r-", label="gen", lw=0.5, alpha=0.8) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         axes["ax1"].set_title("light curves") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         # plot their histogram | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         bins = "auto"  # bins = np.int(y.size**0.5/1.5) # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         rang = (np.percentile(y, 0), np.percentile(y, 99)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         axes["ax2"].hist(y, density=True, color="b", alpha=0.4, bins=bins, range=rang) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         # ax2p = ax2.twinx() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         bins = "auto"  # bins = np.int(y.size**0.5/1.5) # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         rang = (np.percentile(y2, 0), np.percentile(y2, 99)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         axes["ax2"].hist(y2, density=True, color="r", alpha=0.4, bins=bins, range=rang) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |         axes["ax2"].set_title("pdf") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         # plot their PSD | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |         if fpsd == "lombscargle": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |             k = np.linspace(1e-3, self.values.size / 2, self.values.size // 2) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |             freqs = k / 2 / np.pi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |             pxx = scipy_signal.lombscargle(t, y, freqs, normalize=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |             axes["ax3"].plot(freqs, pxx, "b-", lw=1, alpha=0.5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |             pxx2 = scipy_signal.lombscargle(t, y2, freqs, normalize=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |             axes["ax3"].plot(freqs, pxx2, "r-", lw=1, alpha=0.5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |             axes["ax3"].set_xscale("log") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |             # ax3.set_yscale('log') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |             axes["ax3"].psd(y, color="b", lw=1, alpha=0.5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |             ax3p = axes["ax3"].twinx() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |             ax3p.psd(y2, color="r", lw=1, alpha=0.5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |         axes["ax3"].set_title("PSD") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |         return axes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |      | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |      | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |          | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 242 |  |  |     @staticmethod | 
            
                                                        
            
                                    
            
            
                | 243 |  |  |     def pdf(xx, ll, mu): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 244 |  |  |         """Helper func to fit pdf as a curve.""" | 
            
                                                        
            
                                    
            
            
                | 245 |  |  |         return ( | 
            
                                                        
            
                                    
            
            
                | 246 |  |  |             (ll * mu) ** (1 + ll) | 
            
                                                        
            
                                    
            
            
                | 247 |  |  |             / scipy_special.gamma(1 + ll) | 
            
                                                        
            
                                    
            
            
                | 248 |  |  |             * np.exp(-ll * mu / xx) | 
            
                                                        
            
                                    
            
            
                | 249 |  |  |             / xx ** (ll + 2) | 
            
                                                        
            
                                    
            
            
                | 250 |  |  |         ) | 
            
                                                        
            
                                    
            
            
                | 251 |  |  |  |