| Conditions | 5 |
| Total Lines | 76 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # Licensed under a 3-clause BSD style license - see LICENSE |
||
| 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 | print(f"Original vs Synthethic:", |
||
| 193 | f"mean: {np.mean(y)} / {np.mean(y2)}", |
||
| 194 | f"std: {np.std(y)} / {np.std(y2)}", sep='\n') |
||
| 195 | |||
| 196 | if ("ax1" not in axes) or ("ax2" not in axes) or ("ax3" not in axes): |
||
| 197 | fig, (axes["ax1"], axes["ax2"], axes["ax3"]) = plt.subplots( |
||
| 198 | nrows=1, ncols=3, figsize=(20, 4) |
||
| 199 | ) |
||
| 200 | |||
| 201 | # plot the two signals |
||
| 202 | axes["ax1"].plot(t, y, "b-", label="orig", lw=0.5, alpha=0.8) |
||
| 203 | |||
| 204 | # ax1p = ax1.twinx() |
||
| 205 | axes["ax1"].plot(t, y2, "r-", label="gen", lw=0.5, alpha=0.8) |
||
| 206 | axes["ax1"].set_title("light curves") |
||
| 207 | |||
| 208 | # plot their histogram |
||
| 209 | bins = "auto" # bins = np.int(y.size**0.5/1.5) # |
||
| 210 | rang = (np.percentile(y, 0), np.percentile(y, 99)) |
||
| 211 | axes["ax2"].hist(y, density=True, color="b", alpha=0.4, bins=bins, range=rang) |
||
| 212 | |||
| 213 | # ax2p = ax2.twinx() |
||
| 214 | bins = "auto" # bins = np.int(y.size**0.5/1.5) # |
||
| 215 | rang = (np.percentile(y2, 0), np.percentile(y2, 99)) |
||
| 216 | axes["ax2"].hist(y2, density=True, color="r", alpha=0.4, bins=bins, range=rang) |
||
| 217 | |||
| 218 | axes["ax2"].set_title("pdf") |
||
| 219 | |||
| 220 | # plot their PSD |
||
| 221 | if fpsd == "lombscargle": |
||
| 222 | k = np.linspace(1e-3, self.values.size / 2, self.values.size // 2) |
||
| 223 | freqs = k / 2 / np.pi |
||
| 224 | |||
| 225 | pxx = scipy_signal.lombscargle(t, y, freqs, normalize=True) |
||
| 226 | axes["ax3"].plot(freqs, pxx, "b-", lw=1, alpha=0.5) |
||
| 227 | |||
| 228 | pxx2 = scipy_signal.lombscargle(t, y2, freqs, normalize=True) |
||
| 229 | axes["ax3"].plot(freqs, pxx2, "r-", lw=1, alpha=0.5) |
||
| 230 | |||
| 231 | axes["ax3"].set_xscale("log") |
||
| 232 | # ax3.set_yscale('log') |
||
| 233 | else: |
||
| 234 | axes["ax3"].psd(y, color="b", lw=1, alpha=0.5) |
||
| 235 | |||
| 236 | ax3p = axes["ax3"].twinx() |
||
| 237 | ax3p.psd(y2, color="r", lw=1, alpha=0.5) |
||
| 238 | |||
| 239 | axes["ax3"].set_title("PSD") |
||
| 240 | |||
| 241 | return axes |
||
| 242 | |||
| 254 |