Conditions | 2 |
Total Lines | 56 |
Code Lines | 30 |
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:
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 | # coding=utf-8 |
||
86 | def __init__( |
||
87 | self, |
||
88 | image_size: tuple, |
||
89 | num_channel_initial: int, |
||
90 | extract_levels: Tuple[int], |
||
91 | out_kernel_initializer: str, |
||
92 | out_activation: str, |
||
93 | out_channels: int, |
||
94 | depth: Optional[int] = None, |
||
95 | use_additive_upsampling: bool = True, |
||
96 | pooling: bool = True, |
||
97 | concat_skip: bool = False, |
||
98 | name: str = "LocalNet", |
||
99 | **kwargs, |
||
100 | ): |
||
101 | """ |
||
102 | Init. |
||
103 | |||
104 | Image is encoded gradually, i from level 0 to D, |
||
105 | then it is decoded gradually, j from level D to 0. |
||
106 | Some of the decoded levels are used for generating extractions. |
||
107 | |||
108 | So, extract_levels are between [0, D]. |
||
109 | |||
110 | :param image_size: such as (dim1, dim2, dim3) |
||
111 | :param num_channel_initial: number of initial channels. |
||
112 | :param extract_levels: from which depths the output will be built. |
||
113 | :param out_kernel_initializer: initializer to use for kernels. |
||
114 | :param out_activation: activation to use at end layer. |
||
115 | :param out_channels: number of channels for the extractions |
||
116 | :param depth: depth of the encoder. |
||
117 | :param use_additive_upsampling: whether use additive up-sampling layer |
||
118 | for decoding. |
||
119 | :param pooling: for down-sampling, use non-parameterized |
||
120 | pooling if true, otherwise use conv3d |
||
121 | :param concat_skip: when up-sampling, concatenate skipped |
||
122 | tensor if true, otherwise use addition |
||
123 | :param name: name of the backbone. |
||
124 | :param kwargs: additional arguments. |
||
125 | """ |
||
126 | self._use_additive_upsampling = use_additive_upsampling |
||
127 | if depth is None: |
||
128 | depth = max(extract_levels) |
||
129 | kwargs["encode_kernel_sizes"] = [7] + [3] * depth |
||
130 | super().__init__( |
||
131 | image_size=image_size, |
||
132 | num_channel_initial=num_channel_initial, |
||
133 | depth=depth, |
||
134 | extract_levels=extract_levels, |
||
135 | out_kernel_initializer=out_kernel_initializer, |
||
136 | out_activation=out_activation, |
||
137 | out_channels=out_channels, |
||
138 | pooling=pooling, |
||
139 | concat_skip=concat_skip, |
||
140 | name=name, |
||
141 | **kwargs, |
||
142 | ) |
||
237 |