Conditions | 10 |
Total Lines | 77 |
Code Lines | 53 |
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.TomoPhantom.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. |
||
133 | def process_frames(self, data): |
||
134 | # print "The input data shape is", data[0].shape |
||
135 | if (self.out_shape_sino[1] == 1): |
||
136 | # create a 2D phantom |
||
137 | model = TomoP2D.Model(self.model, self.dims, self.path_library2D) |
||
138 | # create a 2D sinogram |
||
139 | projdata_clean = TomoP2D.ModelSino(self.model, self.dims, self.detectors_num, self.angles, self.path_library2D) |
||
140 | # forming dictionaries with different artifact types and noise |
||
141 | _noise_ = {'noise_type' : self.parameters['artifacts_noise_type'], |
||
142 | 'noise_sigma' : self.parameters['artifacts_noise_sigma'], |
||
143 | 'noise_seed' : 0} |
||
144 | # misalignment dictionary |
||
145 | _sinoshifts_ = {} |
||
146 | if self.parameters['artifacts_misalignment_maxamplitude'] is not None: |
||
147 | _sinoshifts_ = {'sinoshifts_maxamplitude' : self.parameters['artifacts_misalignment_maxamplitude']} |
||
148 | |||
149 | # adding zingers and stripes |
||
150 | _zingers_ = {} |
||
151 | if self.parameters['artifacts_zingers_percentage'] is not None: |
||
152 | _zingers_ = {'zingers_percentage' : self.parameters['artifacts_zingers_percentage', |
||
153 | 'zingers_modulus' : 10} |
||
|
|||
154 | _stripes_ = {} |
||
155 | if self.parameters['artifacts_stripes_percentage'] is not None: |
||
156 | _stripes_ = {'stripes_percentage' : self.parameters['artifacts_stripes_percentage'], |
||
157 | 'stripes_maxthickness' : self.parameters['artifacts_stripes_maxthickness'], |
||
158 | 'stripes_intensity' : self.parameters['artifacts_stripes_intensity'], |
||
159 | 'stripes_type' : self.parameters['artifacts_stripes_type'], |
||
160 | 'stripes_variability' : self.parameters['artifacts_stripes_variability']} |
||
161 | |||
162 | if self.parameters['artifacts_misalignment_maxamplitude'] is not None: |
||
163 | [projdata, shifts] = _Artifacts_(projdata_clean, **_noise_, **_zingers_, **_stripes_, **_sinoshifts_) |
||
164 | else: |
||
165 | projdata = _Artifacts_(projdata_clean, **_noise_, **_zingers_, **_stripes_, **_sinoshifts_) |
||
166 | else: |
||
167 | # create a 3D phantom |
||
168 | frame_idx = self.out_pData[0].get_current_frame_idx()[0] |
||
169 | model = TomoP3D.ModelSub(self.model, self.dims, (frame_idx, frame_idx+1), self.path_library3D) |
||
170 | model = np.swapaxes(model,0,1) |
||
171 | model = np.flipud(model[:,0,:]) |
||
172 | # create a 3D projection data |
||
173 | projdata_clean = TomoP3D.ModelSinoSub(self.model, self.dims, self.detectors_num, self.dims, (frame_idx, frame_idx+1), self.angles, self.path_library3D) |
||
174 | # forming dictionaries with different artifact types and noise |
||
175 | _noise_ = {'noise_type' : self.parameters['artifacts_noise_type'], |
||
176 | 'noise_sigma' : self.parameters['artifacts_noise_sigma'], |
||
177 | 'noise_seed' : 0} |
||
178 | # misalignment dictionary |
||
179 | _sinoshifts_ = {} |
||
180 | if self.parameters['artifacts_misalignment_maxamplitude'] is not None: |
||
181 | _sinoshifts_ = {'sinoshifts_maxamplitude' : self.parameters['artifacts_misalignment_maxamplitude']} |
||
182 | |||
183 | # adding zingers and stripes |
||
184 | _zingers_ = {} |
||
185 | if self.parameters['artifacts_zingers_percentage'] is not None: |
||
186 | _zingers_ = {'zingers_percentage' : self.parameters['artifacts_zingers_percentage', |
||
187 | 'zingers_modulus' : 10} |
||
188 | _stripes_ = {} |
||
189 | if self.parameters['artifacts_stripes_percentage'] is not None: |
||
190 | _stripes_ = {'stripes_percentage' : self.parameters['artifacts_stripes_percentage'], |
||
191 | 'stripes_maxthickness' : self.parameters['artifacts_stripes_maxthickness'], |
||
192 | 'stripes_intensity' : self.parameters['artifacts_stripes_intensity'], |
||
193 | 'stripes_type' : self.parameters['artifacts_stripes_type'], |
||
194 | 'stripes_variability' : self.parameters['artifacts_stripes_variability']} |
||
195 | |||
196 | if self.parameters['artifacts_misalignment_maxamplitude'] is not None: |
||
197 | [projdata, shifts] = _Artifacts_(projdata_clean, **_noise_, **_zingers_, **_stripes_, **_sinoshifts_) |
||
198 | else: |
||
199 | projdata = _Artifacts_(projdata_clean, **_noise_, **_zingers_, **_stripes_, **_sinoshifts_) |
||
200 | projdata = np.swapaxes(projdata,0,1) |
||
201 | return [projdata, model] |
||
202 | |||
203 | def get_citation_information(self): |
||
204 | cite_info1 = CitationInformation() |
||
205 | cite_info1.name = 'citation1' |
||
206 | cite_info1.description = \ |
||
207 | ("TomoPhantom is a software package to generate 2D-4D analytical phantoms and their Radon transforms for various testing purposes.") |
||
208 | cite_info1.bibtex = \ |
||
209 | ("@article{kazantsevTP2018,\n" + |
||
210 | "title={TomoPhantom, a software package to generate 2D-4D analytical phantoms for CT image reconstruction algorithm benchmarks},\n" + |
||
236 |