| Conditions | 21 |
| Total Lines | 104 |
| Code Lines | 50 |
| 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.transforms.transform.Transform.parse_range() 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 | import copy |
||
| 137 | @staticmethod |
||
| 138 | def parse_range( |
||
| 139 | nums_range: Union[TypeNumber, Tuple[TypeNumber, TypeNumber]], |
||
| 140 | name: str, |
||
| 141 | min_constraint: TypeNumber = None, |
||
| 142 | max_constraint: TypeNumber = None, |
||
| 143 | type_constraint: type = None, |
||
| 144 | ) -> Tuple[TypeNumber, TypeNumber]: |
||
| 145 | r"""Adapted from ``torchvision.transforms.RandomRotation``. |
||
| 146 | |||
| 147 | Args: |
||
| 148 | nums_range: Tuple of two numbers :math:`(n_{min}, n_{max})`, |
||
| 149 | where :math:`n_{min} \leq n_{max}`. |
||
| 150 | If a single positive number :math:`n` is provided, |
||
| 151 | :math:`n_{min} = -n` and :math:`n_{max} = n`. |
||
| 152 | name: Name of the parameter, so that an informative error message |
||
| 153 | can be printed. |
||
| 154 | min_constraint: Minimal value that :math:`n_{min}` can take, |
||
| 155 | default is None, i.e. there is no minimal value. |
||
| 156 | max_constraint: Maximal value that :math:`n_{max}` can take, |
||
| 157 | default is None, i.e. there is no maximal value. |
||
| 158 | type_constraint: Precise type that :math:`n_{max}` and |
||
| 159 | :math:`n_{min}` must take. |
||
| 160 | |||
| 161 | Returns: |
||
| 162 | A tuple of two numbers :math:`(n_{min}, n_{max})`. |
||
| 163 | |||
| 164 | Raises: |
||
| 165 | ValueError: if :attr:`nums_range` is negative |
||
| 166 | ValueError: if :math:`n_{max}` or :math:`n_{min}` is not a number |
||
| 167 | ValueError: if :math:`n_{max} \lt n_{min}` |
||
| 168 | ValueError: if :attr:`min_constraint` is not None and |
||
| 169 | :math:`n_{min}` is smaller than :attr:`min_constraint` |
||
| 170 | ValueError: if :attr:`max_constraint` is not None and |
||
| 171 | :math:`n_{max}` is greater than :attr:`max_constraint` |
||
| 172 | ValueError: if :attr:`type_constraint` is not None and |
||
| 173 | :math:`n_{max}` and :math:`n_{max}` are not of type |
||
| 174 | :attr:`type_constraint`. |
||
| 175 | """ |
||
| 176 | if isinstance(nums_range, numbers.Number): # single number given |
||
| 177 | if nums_range < 0: |
||
| 178 | raise ValueError( |
||
| 179 | f'If {name} is a single number,' |
||
| 180 | f' it must be positive, not {nums_range}') |
||
| 181 | if min_constraint is not None and nums_range < min_constraint: |
||
| 182 | raise ValueError( |
||
| 183 | f'If {name} is a single number, it must be greater' |
||
| 184 | f' than {min_constraint}, not {nums_range}' |
||
| 185 | ) |
||
| 186 | if max_constraint is not None and nums_range > max_constraint: |
||
| 187 | raise ValueError( |
||
| 188 | f'If {name} is a single number, it must be smaller' |
||
| 189 | f' than {max_constraint}, not {nums_range}' |
||
| 190 | ) |
||
| 191 | if type_constraint is not None: |
||
| 192 | if not isinstance(nums_range, type_constraint): |
||
| 193 | raise ValueError( |
||
| 194 | f'If {name} is a single number, it must be of' |
||
| 195 | f' type {type_constraint}, not {nums_range}' |
||
| 196 | ) |
||
| 197 | min_range = -nums_range if min_constraint is None else nums_range |
||
| 198 | return (min_range, nums_range) |
||
| 199 | |||
| 200 | try: |
||
| 201 | min_value, max_value = nums_range |
||
| 202 | except (TypeError, ValueError): |
||
| 203 | raise ValueError( |
||
| 204 | f'If {name} is not a single number, it must be' |
||
| 205 | f' a sequence of len 2, not {nums_range}' |
||
| 206 | ) |
||
| 207 | |||
| 208 | min_is_number = isinstance(min_value, numbers.Number) |
||
| 209 | max_is_number = isinstance(max_value, numbers.Number) |
||
| 210 | if not min_is_number or not max_is_number: |
||
| 211 | message = ( |
||
| 212 | f'{name} values must be numbers, not {nums_range}') |
||
| 213 | raise ValueError(message) |
||
| 214 | |||
| 215 | if min_value > max_value: |
||
| 216 | raise ValueError( |
||
| 217 | f'If {name} is a sequence, the second value must be' |
||
| 218 | f' equal or greater than the first, but it is {nums_range}') |
||
| 219 | |||
| 220 | if min_constraint is not None and min_value < min_constraint: |
||
| 221 | raise ValueError( |
||
| 222 | f'If {name} is a sequence, the first value must be greater' |
||
| 223 | f' than {min_constraint}, but it is {min_value}' |
||
| 224 | ) |
||
| 225 | |||
| 226 | if max_constraint is not None and max_value > max_constraint: |
||
| 227 | raise ValueError( |
||
| 228 | f'If {name} is a sequence, the second value must be smaller' |
||
| 229 | f' than {max_constraint}, but it is {max_value}' |
||
| 230 | ) |
||
| 231 | |||
| 232 | if type_constraint is not None: |
||
| 233 | min_type_ok = isinstance(min_value, type_constraint) |
||
| 234 | max_type_ok = isinstance(max_value, type_constraint) |
||
| 235 | if not min_type_ok or not max_type_ok: |
||
| 236 | raise ValueError( |
||
| 237 | f'If "{name}" is a sequence, its values must be of' |
||
| 238 | f' type "{type_constraint}", not "{type(nums_range)}"' |
||
| 239 | ) |
||
| 240 | return nums_range |
||
| 241 | |||
| 307 |