Conditions | 11 |
Total Lines | 54 |
Code Lines | 37 |
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.basic_operations.rotate_90.Rotate90.setup() 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. |
||
50 | def setup(self): |
||
51 | |||
52 | # assumes 3D data and 2D frames |
||
53 | |||
54 | if self.exp.meta_data.get("pre_run"): |
||
55 | self.stats_obj.calc_stats = False |
||
56 | |||
57 | in_dataset, out_dataset = self.get_datasets() |
||
58 | pattern = self.parameters["pattern"] |
||
59 | data_info = in_dataset[0].data_info |
||
60 | core_dims = data_info["data_patterns"][pattern]["core_dims"] |
||
61 | c0, c1 = core_dims[0], core_dims[1] # core dimensions |
||
62 | s0 = data_info["data_patterns"][pattern]["slice_dims"][0] # slice dimension |
||
63 | |||
64 | # swapping round core dimensions in the shape due to rotation |
||
65 | new_shape = list(data_info["shape"]) |
||
66 | new_shape[c0], new_shape[c1] = data_info["shape"][c1], data_info["shape"][c0] |
||
67 | if self.exp.meta_data.get("pre_run"): |
||
68 | new_shape[0] = in_dataset[0].data.image_key.shape[0] |
||
69 | new_shape = tuple(new_shape) |
||
70 | |||
71 | # swapping round core dimensions in axis labels |
||
72 | new_axis_labels = deepcopy(data_info["axis_labels"]) |
||
73 | new_axis_labels[c0], new_axis_labels[c1] = data_info["axis_labels"][c1], data_info["axis_labels"][c0] |
||
74 | |||
75 | # swapping round core dimensions in data patterns |
||
76 | new_data_patterns = deepcopy(data_info["data_patterns"]) |
||
77 | for pattern in new_data_patterns: |
||
78 | for dims in new_data_patterns[pattern]: |
||
79 | if dims != "main_dir": # not sure what main_dir is ( = slice dim?) |
||
80 | dims_list = list(new_data_patterns[pattern][dims]) |
||
81 | for i, dim in enumerate(dims_list): |
||
82 | if dim == c0: |
||
83 | dims_list[i] = c1 |
||
84 | elif dim == c1: |
||
85 | dims_list[i] = c0 |
||
86 | new_data_patterns[pattern][dims] = tuple(dims_list) |
||
87 | |||
88 | dtype = in_dataset[0].dtype |
||
89 | if dtype is None: |
||
90 | dtype = in_dataset[0].data.data.dtype |
||
91 | |||
92 | # creating output dataset with new axis, shape and data patterns to reflect rotated image |
||
93 | if dtype: |
||
94 | out_dataset[0].create_dataset(shape=new_shape, axis_labels=new_axis_labels, dtype=dtype) |
||
95 | else: |
||
96 | out_dataset[0].create_dataset(shape=new_shape, axis_labels=new_axis_labels) |
||
97 | out_dataset[0].data_info.set("data_patterns", new_data_patterns) |
||
98 | |||
99 | |||
100 | in_pData, out_pData = self.get_plugin_datasets() |
||
101 | |||
102 | in_pData[0].plugin_data_setup(self.parameters['pattern'], 'single') |
||
103 | out_pData[0].plugin_data_setup(self.parameters['pattern'], 'single') |
||
104 | |||
151 |