Conditions | 12 |
Total Lines | 51 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Tests | 18 |
CRAP Score | 13.4795 |
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:
Complex classes like sciapy.regress._gpkernels.setup_celerite_terms() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
136 | 1 | def setup_celerite_terms(termnames, fit_bias=False, fit_white=False): |
|
137 | """Setup the Gaussian Process terms for celerite |
||
138 | |||
139 | Parameters |
||
140 | ---------- |
||
141 | termnames : list of str |
||
142 | List of abbreviated names for the `celerite.terms`, choices: |
||
143 | 'N' for an empty `Term`, 'B' for a constant term (bias), |
||
144 | 'W' for a `JitterTerm` (white noise), 'Mat32' for a `Matern32Term`, |
||
145 | 'SHO0'...'SHO3' for `SHOTerm`s (harmonic oscillators) with different |
||
146 | frozen parameters. |
||
147 | fit_bias : bool, optional |
||
148 | Adds a constant term (`RealTerm` with log_c fixed to -np.inf) if the |
||
149 | terms do not already contain one. |
||
150 | fit_white : bool, optional |
||
151 | Adds a `JitterTerm` if not already included. |
||
152 | |||
153 | Returns |
||
154 | ------- |
||
155 | name : The term names concatenated by an underscore and prepended by '_cel' |
||
156 | terms : The covariance terms for use with celerite.GP |
||
157 | """ |
||
158 | # celerite terms |
||
159 | 1 | cel_terms = None |
|
160 | 1 | cel_name = "_cel" |
|
161 | 1 | for tn in termnames: |
|
162 | 1 | trm = celerite_terms.get(tn, None) |
|
163 | 1 | if trm is None: |
|
164 | # not found in the list of available terms |
||
165 | 1 | continue |
|
166 | 1 | for freeze_p in _celerite_terms_freeze_params[tn]: |
|
167 | trm.freeze_parameter(freeze_p) |
||
168 | 1 | cel_terms = cel_terms + trm if cel_terms is not None else trm |
|
169 | 1 | cel_name += "_" + tn |
|
170 | |||
171 | # avoid double initialisation of White and Constant kernels |
||
172 | # if already set above |
||
173 | 1 | if fit_white and "W" not in termnames: |
|
174 | 1 | trm = celerite_terms["W"] |
|
175 | 1 | cel_terms = cel_terms + trm if cel_terms is not None else trm |
|
176 | 1 | cel_name += "_w" |
|
177 | 1 | if fit_bias and "B" not in termnames: |
|
178 | trm = celerite_terms["B"] |
||
179 | trm.freeze_parameter("log_c") |
||
180 | cel_terms = cel_terms + trm if cel_terms is not None else trm |
||
181 | cel_name += "_b" |
||
182 | |||
183 | 1 | if cel_terms is None: |
|
184 | 1 | cel_terms = terms.Term() |
|
185 | |||
186 | return cel_name, cel_terms |
||
187 |