Conditions | 6 |
Total Lines | 58 |
Lines | 0 |
Ratio | 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 | #!/usr/bin/env python |
||
17 | def chebyshev(dispersion, fluxes, flux_uncertainties, continuum_mask, |
||
18 | degree=2, regions=None): |
||
19 | """ |
||
20 | Fit Chebyshev polynomials to the flux values in the continuum mask. Fluxes |
||
21 | from multiple stars can be given, and the resulting continuum will have the |
||
22 | same shape as `fluxes`. |
||
23 | |||
24 | :param dispersion: |
||
25 | The dispersion values for each of the flux values. |
||
26 | |||
27 | :param fluxes: |
||
28 | The flux values. |
||
29 | |||
30 | :param flux_uncertainties: |
||
31 | The observational uncertainties associated with the fluxes. |
||
32 | |||
33 | :param continuum_mask: |
||
34 | A mask for continuum pixels to use. |
||
35 | |||
36 | :param degree: [optional] |
||
37 | The degree of Chebyshev polynomial to use in each region. |
||
38 | |||
39 | :param regions: [optional] |
||
40 | Split up the continuum fitting into different wavelength regions. For |
||
41 | example, APOGEE spectra could be splitted into three chunks: |
||
42 | `[(15150, 15800), (15890, 16430), (16490, 16950)]` |
||
43 | """ |
||
44 | |||
45 | if regions is None: |
||
46 | region_masks = [np.ones_like(dispersion, dtype=bool)] |
||
47 | else: |
||
48 | region_masks = \ |
||
49 | [(e >= dispersion) * (dispersion >= s) for s, e in regions] |
||
50 | |||
51 | dispersion = np.array(dispersion).flatten() |
||
52 | fluxes = np.atleast_2d(fluxes) |
||
53 | flux_uncertainties = np.atleast_2d(flux_uncertainties) |
||
54 | |||
55 | N_stars = fluxes.shape[0] |
||
56 | assert fluxes.shape[1] == dispersion.size |
||
57 | |||
58 | i_variances = 1.0/flux_uncertainties**2 |
||
59 | |||
60 | # Use only continuum pixels. |
||
61 | i_variances[:, ~continuum_mask] += 200**2 |
||
62 | #i_variances[~np.isfinite(i_variances)] = 0 |
||
63 | |||
64 | continuum = np.ones_like(fluxes, dtype=float) |
||
65 | for i, (flux, i_var) in enumerate(zip(fluxes, i_variances)): |
||
66 | for region_mask in region_masks: |
||
67 | fitted_mask = region_mask * continuum_mask |
||
68 | f = np.polynomial.chebyshev.Chebyshev.fit( |
||
69 | x=dispersion[fitted_mask], y=flux[fitted_mask], |
||
70 | w=i_var[fitted_mask], deg=degree) |
||
71 | |||
72 | continuum[i, region_mask] *= f(dispersion[region_mask]) |
||
73 | |||
74 | return continuum |
||
75 | |||
76 |