| 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 |
||
| 180 | @staticmethod |
||
| 181 | def parse_range( |
||
| 182 | nums_range: Union[TypeNumber, Tuple[TypeNumber, TypeNumber]], |
||
| 183 | name: str, |
||
| 184 | min_constraint: TypeNumber = None, |
||
| 185 | max_constraint: TypeNumber = None, |
||
| 186 | type_constraint: type = None, |
||
| 187 | ) -> Tuple[TypeNumber, TypeNumber]: |
||
| 188 | r"""Adapted from ``torchvision.transforms.RandomRotation``. |
||
| 189 | |||
| 190 | Args: |
||
| 191 | nums_range: Tuple of two numbers :math:`(n_{min}, n_{max})`, |
||
| 192 | where :math:`n_{min} \leq n_{max}`. |
||
| 193 | If a single positive number :math:`n` is provided, |
||
| 194 | :math:`n_{min} = -n` and :math:`n_{max} = n`. |
||
| 195 | name: Name of the parameter, so that an informative error message |
||
| 196 | can be printed. |
||
| 197 | min_constraint: Minimal value that :math:`n_{min}` can take, |
||
| 198 | default is None, i.e. there is no minimal value. |
||
| 199 | max_constraint: Maximal value that :math:`n_{max}` can take, |
||
| 200 | default is None, i.e. there is no maximal value. |
||
| 201 | type_constraint: Precise type that :math:`n_{max}` and |
||
| 202 | :math:`n_{min}` must take. |
||
| 203 | |||
| 204 | Returns: |
||
| 205 | A tuple of two numbers :math:`(n_{min}, n_{max})`. |
||
| 206 | |||
| 207 | Raises: |
||
| 208 | ValueError: if :attr:`nums_range` is negative |
||
| 209 | ValueError: if :math:`n_{max}` or :math:`n_{min}` is not a number |
||
| 210 | ValueError: if :math:`n_{max} \lt n_{min}` |
||
| 211 | ValueError: if :attr:`min_constraint` is not None and |
||
| 212 | :math:`n_{min}` is smaller than :attr:`min_constraint` |
||
| 213 | ValueError: if :attr:`max_constraint` is not None and |
||
| 214 | :math:`n_{max}` is greater than :attr:`max_constraint` |
||
| 215 | ValueError: if :attr:`type_constraint` is not None and |
||
| 216 | :math:`n_{max}` and :math:`n_{max}` are not of type |
||
| 217 | :attr:`type_constraint`. |
||
| 218 | """ |
||
| 219 | if isinstance(nums_range, numbers.Number): # single number given |
||
| 220 | if nums_range < 0: |
||
| 221 | raise ValueError( |
||
| 222 | f'If {name} is a single number,' |
||
| 223 | f' it must be positive, not {nums_range}') |
||
| 224 | if min_constraint is not None and nums_range < min_constraint: |
||
| 225 | raise ValueError( |
||
| 226 | f'If {name} is a single number, it must be greater' |
||
| 227 | f' than {min_constraint}, not {nums_range}' |
||
| 228 | ) |
||
| 229 | if max_constraint is not None and nums_range > max_constraint: |
||
| 230 | raise ValueError( |
||
| 231 | f'If {name} is a single number, it must be smaller' |
||
| 232 | f' than {max_constraint}, not {nums_range}' |
||
| 233 | ) |
||
| 234 | if type_constraint is not None: |
||
| 235 | if not isinstance(nums_range, type_constraint): |
||
| 236 | raise ValueError( |
||
| 237 | f'If {name} is a single number, it must be of' |
||
| 238 | f' type {type_constraint}, not {nums_range}' |
||
| 239 | ) |
||
| 240 | min_range = -nums_range if min_constraint is None else nums_range |
||
| 241 | return (min_range, nums_range) |
||
| 242 | |||
| 243 | try: |
||
| 244 | min_value, max_value = nums_range |
||
| 245 | except (TypeError, ValueError): |
||
| 246 | raise ValueError( |
||
| 247 | f'If {name} is not a single number, it must be' |
||
| 248 | f' a sequence of len 2, not {nums_range}' |
||
| 249 | ) |
||
| 250 | |||
| 251 | min_is_number = isinstance(min_value, numbers.Number) |
||
| 252 | max_is_number = isinstance(max_value, numbers.Number) |
||
| 253 | if not min_is_number or not max_is_number: |
||
| 254 | message = ( |
||
| 255 | f'{name} values must be numbers, not {nums_range}') |
||
| 256 | raise ValueError(message) |
||
| 257 | |||
| 258 | if min_value > max_value: |
||
| 259 | raise ValueError( |
||
| 260 | f'If {name} is a sequence, the second value must be' |
||
| 261 | f' equal or greater than the first, but it is {nums_range}') |
||
| 262 | |||
| 263 | if min_constraint is not None and min_value < min_constraint: |
||
| 264 | raise ValueError( |
||
| 265 | f'If {name} is a sequence, the first value must be greater' |
||
| 266 | f' than {min_constraint}, but it is {min_value}' |
||
| 267 | ) |
||
| 268 | |||
| 269 | if max_constraint is not None and max_value > max_constraint: |
||
| 270 | raise ValueError( |
||
| 271 | f'If {name} is a sequence, the second value must be smaller' |
||
| 272 | f' than {max_constraint}, but it is {max_value}' |
||
| 273 | ) |
||
| 274 | |||
| 275 | if type_constraint is not None: |
||
| 276 | min_type_ok = isinstance(min_value, type_constraint) |
||
| 277 | max_type_ok = isinstance(max_value, type_constraint) |
||
| 278 | if not min_type_ok or not max_type_ok: |
||
| 279 | raise ValueError( |
||
| 280 | f'If "{name}" is a sequence, its values must be of' |
||
| 281 | f' type "{type_constraint}", not "{type(nums_range)}"' |
||
| 282 | ) |
||
| 283 | return nums_range |
||
| 284 | |||
| 387 |