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