Conditions | 23 |
Total Lines | 92 |
Code Lines | 47 |
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:
Complex classes like mutis.flares.bayblocks.BayesianBlocks.get_flares() 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 | """ Flare analysis """ |
||
115 | def get_flares(self, thresh=1): |
||
116 | """ Get a list of flares following the algorithm proposed in |
||
117 | [Meyer, Scargle, Blandford (2019)] |
||
118 | (https://iopscience.iop.org/article/10.3847/1538-4357/ab1651/pdf): |
||
119 | |||
120 | ```There is no generally accepted consensus on the best way to |
||
121 | determine which data points belong to a flaring state and which |
||
122 | characterize the quiescent level. Nalewajko (2013)suggested |
||
123 | the simple definition that a flare is a continuous time interval |
||
124 | associated with a flux peak in which the flux is larger than half |
||
125 | the peak flux value. This definition is intuitive, however, and it |
||
126 | is unclear how to treat overlapping flares and identify flux |
||
127 | peaks in an objective way. Here we use a simple two-step |
||
128 | procedure tailored to the block representation: (1)identify a |
||
129 | block that is higher than both the previous and subsequent |
||
130 | blocks as a peak, and (2)proceed downward from the peak in |
||
131 | both directions as long as the blocks are successively lower.``` |
||
132 | """ |
||
133 | di_max = 5 |
||
134 | |||
135 | # get list of local maxima |
||
136 | # bad beheaviour at 0 and -1: |
||
137 | #imaxL = sp.signal.argrelextrema(self.values, np.greater)[0] |
||
138 | imaxL = list() |
||
139 | for i in range(0,len(self.values)): |
||
140 | if i == 0 and thresh*self.values[0] > self.values[1]: |
||
141 | imaxL.append(i) |
||
142 | elif i == len(self.values)-1 and self.values[i-1] < thresh*self.values[i]: |
||
143 | imaxL.append(i) |
||
144 | elif self.values[i-1] < thresh*self.values[i] > self.values[i+1]: |
||
145 | imaxL.append(i) |
||
146 | imaxL = np.array(imaxL) |
||
147 | |||
148 | |||
149 | inflareL = np.full(self.values.shape, False) |
||
150 | |||
151 | # get list of local maxima over a threshold (flare peaks) |
||
152 | for imax in imaxL: |
||
153 | inflare = True |
||
154 | |||
155 | if imax == 0: |
||
156 | pass |
||
157 | elif not (self.values[imax-1] < thresh*self.values[imax]): |
||
158 | inflare = False |
||
159 | |||
160 | |||
161 | if imax == len(self.values)-1: |
||
162 | pass |
||
163 | elif not (thresh*self.values[imax] > self.values[imax+1]): |
||
164 | inflare = False |
||
165 | |||
166 | inflareL[imax] = inflare |
||
167 | |||
168 | # extend the flare to adyacent blocks |
||
169 | for imax in np.argwhere(inflareL): |
||
170 | |||
171 | if imax == 0: |
||
172 | pass |
||
173 | else: |
||
174 | di = 1 |
||
175 | stop = False |
||
176 | while not stop and di < di_max: |
||
177 | if imax-di-1 < 0: |
||
178 | break |
||
179 | |||
180 | if self.values[imax-di-1] < thresh*self.values[imax-di]: |
||
181 | inflareL[imax-di] = True |
||
182 | else: |
||
183 | stop = True |
||
184 | |||
185 | |||
186 | di = di + 1 |
||
187 | |||
188 | if imax == len(self.values)-1: |
||
189 | pass |
||
190 | else: |
||
191 | di = 1 |
||
192 | stop = False |
||
193 | while not stop and di < di_max: |
||
194 | if imax+di+1 > len(self.values)-1: |
||
195 | break |
||
196 | |||
197 | if thresh*self.values[imax+di] > self.values[imax+di+1]: |
||
198 | inflareL[imax+di] = True |
||
199 | else: |
||
200 | stop = True |
||
201 | |||
202 | |||
203 | di = di + 1 |
||
204 | |||
205 | |||
206 | self.inflare = np.array(inflareL) |
||
207 | |||
232 | return flareL |
||