| Conditions | 13 | 
| Total Lines | 58 | 
| Code Lines | 41 | 
| 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 savu.plugins.simulation.tomo_phantom_artifacts.TomoPhantomArtifacts.process_frames() 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 | # Copyright 2014 Diamond Light Source Ltd.  | 
            ||
| 43 | def process_frames(self, data):  | 
            ||
| 44 | proj_data = data[0]  | 
            ||
| 45 | |||
| 46 | if self.parameters['pattern'] == 'PROJECTION':  | 
            ||
| 47 | proj_data = np.expand_dims(proj_data, axis=1)  | 
            ||
| 48 | |||
| 49 | # apply a variety of artifacts to the generated data:  | 
            ||
| 50 |         _noise_ = {} | 
            ||
| 51 | if self.parameters['artifacts_noise_type'] is not None:  | 
            ||
| 52 |             _noise_ = {'noise_type': self.parameters['artifacts_noise_type'], | 
            ||
| 53 | 'noise_amplitude': self.parameters['artifacts_noise_amplitude'],  | 
            ||
| 54 | 'noise_seed': 0,  | 
            ||
| 55 | 'verbose': False}  | 
            ||
| 56 | |||
| 57 | # misalignment dictionary  | 
            ||
| 58 |         _datashifts_ = {} | 
            ||
| 59 | if self.parameters['datashifts_maxamplitude_pixel'] is not None:  | 
            ||
| 60 |             _datashifts_ = {'datashifts_maxamplitude_pixel': self.parameters['datashifts_maxamplitude_pixel']} | 
            ||
| 61 | if self.parameters['datashifts_maxamplitude_subpixel'] is not None:  | 
            ||
| 62 |             _datashifts_ = {'datashifts_maxamplitude_subpixel': self.parameters['datashifts_maxamplitude_subpixel']} | 
            ||
| 63 | |||
| 64 | # adding zingers  | 
            ||
| 65 |         _zingers_ = {} | 
            ||
| 66 | if self.parameters['artifacts_zingers_percentage'] is not None:  | 
            ||
| 67 |             _zingers_ = {'zingers_percentage': self.parameters['artifacts_zingers_percentage'], | 
            ||
| 68 | 'zingers_modulus': self.parameters['artifacts_zingers_modulus']}  | 
            ||
| 69 |         _stripes_ = {} | 
            ||
| 70 | |||
| 71 | # adding stripes  | 
            ||
| 72 | if self.parameters['pattern'] == 'SINOGRAM':  | 
            ||
| 73 | if self.parameters['artifacts_stripes_percentage'] is not None:  | 
            ||
| 74 |                 _stripes_ = {'stripes_percentage': self.parameters['artifacts_stripes_percentage'], | 
            ||
| 75 | 'stripes_maxthickness': self.parameters['artifacts_stripes_maxthickness'],  | 
            ||
| 76 | 'stripes_intensity': self.parameters['artifacts_stripes_intensity'],  | 
            ||
| 77 | 'stripes_type': self.parameters['artifacts_stripes_type'],  | 
            ||
| 78 | 'stripes_variability': self.parameters['artifacts_stripes_variability']}  | 
            ||
| 79 | |||
| 80 | # partial volume effect dictionary  | 
            ||
| 81 |         _pve_ = {} | 
            ||
| 82 | if self.parameters['artifacts_pve'] is not None:  | 
            ||
| 83 |             _pve_ = {'pve_strength': self.parameters['artifacts_pve']} | 
            ||
| 84 | |||
| 85 | # fresnel propagator  | 
            ||
| 86 |         _fresnel_propagator_ = {} | 
            ||
| 87 | if self.parameters['artifacts_fresnel_distance'] is not None:  | 
            ||
| 88 |             _fresnel_propagator_ = {'fresnel_dist_observation': self.parameters['artifacts_fresnel_distance'], | 
            ||
| 89 | 'fresnel_scale_factor': self.parameters['artifacts_fresnel_scale_factor'],  | 
            ||
| 90 | 'fresnel_wavelenght': self.parameters['artifacts_fresnel_wavelenght']}  | 
            ||
| 91 | |||
| 92 | if (self.parameters['datashifts_maxamplitude_pixel']) or (self.parameters['datashifts_maxamplitude_subpixel']) is not None:  | 
            ||
| 93 | [data_artifacts, shifts] = _Artifacts_(proj_data.copy(), **_noise_, **_zingers_, **_stripes_, **_datashifts_, **_pve_, **_fresnel_propagator_)  | 
            ||
| 94 | else:  | 
            ||
| 95 | data_artifacts = _Artifacts_(proj_data.copy(), **_noise_, **_zingers_, **_stripes_, **_datashifts_, **_pve_, **_fresnel_propagator_)  | 
            ||
| 96 | |||
| 97 | if self.parameters['pattern'] == 'PROJECTION':  | 
            ||
| 98 | data_artifacts = data_artifacts[:, 0, :]  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 99 | |||
| 100 | return data_artifacts  | 
            ||
| 101 | |||
| 110 |