Conditions | 23 |
Total Lines | 70 |
Code Lines | 55 |
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.segmentation.thresholding.otsu_thresh.OtsuThresh._crop() 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 2019 Diamond Light Source Ltd. |
||
100 | def _crop(self, binary_slice, directions, buffer=0): |
||
101 | # For 3 dimensional volume and 2 dimensional slices |
||
102 | |||
103 | total_crop = {"left": 0, "above": 0, "right": self.fully_right, "below": self.fully_below} |
||
104 | total_counter = 0 |
||
105 | reset_counter = 0 |
||
106 | dir_cycle = itertools.cycle(directions) |
||
107 | while reset_counter < 4: |
||
108 | direction = next(dir_cycle) |
||
109 | crops = {"left": 0, "above": 0, "right": binary_slice.shape[1], "below": binary_slice.shape[0]} |
||
110 | if direction in ["above", "below"]: |
||
111 | axis = 1 |
||
112 | elif direction in ["left", "right"]: |
||
113 | axis = 0 |
||
114 | |||
115 | non_zeros_list = np.count_nonzero(binary_slice, axis=axis) |
||
|
|||
116 | previous = None |
||
117 | fills = [] |
||
118 | gaps = [] |
||
119 | for i, non_zeros in enumerate(non_zeros_list): |
||
120 | if non_zeros + buffer < binary_slice.shape[axis]: # check if some zeros (indicating sample) |
||
121 | if previous != "fill": |
||
122 | fills.append([]) |
||
123 | fills[-1].append(i) |
||
124 | previous = "fill" |
||
125 | else: |
||
126 | if previous != "gap": |
||
127 | gaps.append([]) |
||
128 | gaps[-1].append(i) |
||
129 | previous = "gap" |
||
130 | |||
131 | # find largest area of zeros (assume this is where sample is) |
||
132 | largest_fill = [] |
||
133 | for fill in fills: |
||
134 | if len(fill) > len(largest_fill): |
||
135 | largest_fill = fill |
||
136 | |||
137 | # find a reasonably sized gap closest to the sample |
||
138 | if direction in ["left", "above"]: |
||
139 | for gap in gaps: |
||
140 | if gap[-1] < largest_fill[0]: |
||
141 | if len(gap) > self.gap_size: |
||
142 | crops[direction] = gap[-1] |
||
143 | reset_counter = 0 |
||
144 | total_crop[direction] += crops[direction] |
||
145 | |||
146 | elif direction in ["right", "below"]: |
||
147 | for gap in gaps[::-1]: |
||
148 | if gap[0] > largest_fill[-1]: |
||
149 | if len(gap) > self.gap_size: |
||
150 | crops[direction] = gap[0] + 1 |
||
151 | reset_counter = 0 |
||
152 | total_crop[direction] -= (binary_slice.shape[1 - axis] - crops[direction]) |
||
153 | |||
154 | #if self.pcount == 0: |
||
155 | # self.__save_image(binary_slice, f"{total_counter}-{reset_counter}-{direction}") |
||
156 | |||
157 | reset_counter += 1 |
||
158 | total_counter += 1 |
||
159 | |||
160 | binary_slice = binary_slice[crops["above"]: crops["below"], crops["left"]: crops["right"]] |
||
161 | |||
162 | for direction in self.directions: |
||
163 | if direction in ["left", "above"]: |
||
164 | if total_crop[direction] < self.volume_crop[direction]: |
||
165 | self.volume_crop[direction] = total_crop[direction] |
||
166 | if direction in ["right", "below"]: |
||
167 | if total_crop[direction] > self.volume_crop[direction]: |
||
168 | self.volume_crop[direction] = total_crop[direction] |
||
169 | return binary_slice |
||
170 | |||
205 | im.save(f"/scratch/Savu/images/{name}.jpeg") |