Conditions | 15 |
Total Lines | 78 |
Code Lines | 69 |
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 torchio.visualization.plot_volume() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | import warnings |
||
30 | def plot_volume( |
||
31 | image: Image, |
||
32 | radiological=True, |
||
33 | channel=-1, # default to foreground for binary maps |
||
34 | axes=None, |
||
35 | cmap=None, |
||
36 | output_path=None, |
||
37 | show=True, |
||
38 | xlabels=True, |
||
39 | percentiles=(0.5, 99.5), |
||
40 | figsize=None, |
||
41 | reorient=True, |
||
42 | indices=None, |
||
43 | ): |
||
44 | _, plt = import_mpl_plt() |
||
45 | fig = None |
||
46 | if axes is None: |
||
47 | fig, axes = plt.subplots(1, 3, figsize=figsize) |
||
48 | sag_axis, cor_axis, axi_axis = axes |
||
49 | |||
50 | if reorient: |
||
51 | image = ToCanonical()(image) |
||
52 | data = image.data[channel] |
||
53 | if indices is None: |
||
54 | indices = np.array(data.shape) // 2 |
||
55 | i, j, k = indices |
||
56 | slice_x = rotate(data[i, :, :], radiological=radiological) |
||
57 | slice_y = rotate(data[:, j, :], radiological=radiological) |
||
58 | slice_z = rotate(data[:, :, k], radiological=radiological) |
||
59 | kwargs = {} |
||
60 | is_label = isinstance(image, LabelMap) |
||
61 | if isinstance(cmap, dict): |
||
62 | slices = slice_x, slice_y, slice_z |
||
63 | slice_x, slice_y, slice_z = color_labels(slices, cmap) |
||
64 | else: |
||
65 | if cmap is None: |
||
66 | cmap = 'cubehelix' if is_label else 'gray' |
||
67 | kwargs['cmap'] = cmap |
||
68 | if is_label: |
||
69 | kwargs['interpolation'] = 'none' |
||
70 | |||
71 | sr, sa, ss = image.spacing |
||
72 | kwargs['origin'] = 'lower' |
||
73 | |||
74 | if percentiles is not None: |
||
75 | p1, p2 = np.percentile(data, percentiles) |
||
76 | kwargs['vmin'] = p1 |
||
77 | kwargs['vmax'] = p2 |
||
78 | |||
79 | sag_aspect = ss / sa |
||
80 | sag_axis.imshow(slice_x, aspect=sag_aspect, **kwargs) |
||
81 | if xlabels: |
||
82 | sag_axis.set_xlabel('A') |
||
83 | sag_axis.set_ylabel('S') |
||
84 | sag_axis.invert_xaxis() |
||
85 | sag_axis.set_title('Sagittal') |
||
86 | |||
87 | cor_aspect = ss / sr |
||
88 | cor_axis.imshow(slice_y, aspect=cor_aspect, **kwargs) |
||
89 | if xlabels: |
||
90 | cor_axis.set_xlabel('R') |
||
91 | cor_axis.set_ylabel('S') |
||
92 | cor_axis.invert_xaxis() |
||
93 | cor_axis.set_title('Coronal') |
||
94 | |||
95 | axi_aspect = sa / sr |
||
96 | axi_axis.imshow(slice_z, aspect=axi_aspect, **kwargs) |
||
97 | if xlabels: |
||
98 | axi_axis.set_xlabel('R') |
||
99 | axi_axis.set_ylabel('A') |
||
100 | axi_axis.invert_xaxis() |
||
101 | axi_axis.set_title('Axial') |
||
102 | |||
103 | plt.tight_layout() |
||
104 | if output_path is not None and fig is not None: |
||
105 | fig.savefig(output_path) |
||
106 | if show: |
||
107 | plt.show() |
||
108 | |||
231 |