| Conditions | 21 |
| Total Lines | 102 |
| Code Lines | 48 |
| 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 |
||
| 115 | @staticmethod |
||
| 116 | def parse_range( |
||
| 117 | nums_range: Union[TypeNumber, Tuple[TypeNumber, TypeNumber]], |
||
| 118 | name: str, |
||
| 119 | min_constraint: TypeNumber = None, |
||
| 120 | max_constraint: TypeNumber = None, |
||
| 121 | type_constraint: type = None, |
||
| 122 | ) -> Tuple[TypeNumber, TypeNumber]: |
||
| 123 | r"""Adapted from ``torchvision.transforms.RandomRotation``. |
||
| 124 | |||
| 125 | Args: |
||
| 126 | nums_range: Tuple of two numbers :math:`(n_{min}, n_{max})`, |
||
| 127 | where :math:`n_{min} \leq n_{max}`. |
||
| 128 | If a single positive number :math:`n` is provided, |
||
| 129 | :math:`n_{min} = -n` and :math:`n_{max} = n`. |
||
| 130 | name: Name of the parameter, so that an informative error message |
||
| 131 | can be printed. |
||
| 132 | min_constraint: Minimal value that :math:`n_{min}` can take, |
||
| 133 | default is None, i.e. there is no minimal value. |
||
| 134 | max_constraint: Maximal value that :math:`n_{max}` can take, |
||
| 135 | default is None, i.e. there is no maximal value. |
||
| 136 | type_constraint: Precise type that :math:`n_{max}` and |
||
| 137 | :math:`n_{min}` must take. |
||
| 138 | |||
| 139 | Returns: |
||
| 140 | A tuple of two numbers :math:`(n_{min}, n_{max})`. |
||
| 141 | |||
| 142 | Raises: |
||
| 143 | ValueError: if :attr:`nums_range` is negative |
||
| 144 | ValueError: if :math:`n_{max}` or :math:`n_{min}` is not a number |
||
| 145 | ValueError: if :math:`n_{max} \lt n_{min}` |
||
| 146 | ValueError: if :attr:`min_constraint` is not None and |
||
| 147 | :math:`n_{min}` is smaller than :attr:`min_constraint` |
||
| 148 | ValueError: if :attr:`max_constraint` is not None and |
||
| 149 | :math:`n_{max}` is greater than :attr:`max_constraint` |
||
| 150 | ValueError: if :attr:`type_constraint` is not None and |
||
| 151 | :math:`n_{max}` and :math:`n_{max}` are not of type |
||
| 152 | :attr:`type_constraint`. |
||
| 153 | """ |
||
| 154 | if isinstance(nums_range, numbers.Number): |
||
| 155 | if nums_range < 0: |
||
| 156 | raise ValueError( |
||
| 157 | f'If {name} is a single number,' |
||
| 158 | f' it must be positive, not {nums_range}') |
||
| 159 | if min_constraint is not None and nums_range < min_constraint: |
||
| 160 | raise ValueError( |
||
| 161 | f'If {name} is a single number, it must be greater' |
||
| 162 | f'than {min_constraint}, not {nums_range}' |
||
| 163 | ) |
||
| 164 | if max_constraint is not None and nums_range > max_constraint: |
||
| 165 | raise ValueError( |
||
| 166 | f'If {name} is a single number, it must be smaller' |
||
| 167 | f'than {max_constraint}, not {nums_range}' |
||
| 168 | ) |
||
| 169 | if type_constraint is not None and \ |
||
| 170 | not isinstance(nums_range, type_constraint): |
||
| 171 | raise ValueError( |
||
| 172 | f'If {name} is a single number, it must be of' |
||
| 173 | f'type {type_constraint}, not {nums_range}' |
||
| 174 | ) |
||
| 175 | min_range = -nums_range if min_constraint is None else nums_range |
||
| 176 | return (min_range, nums_range) |
||
| 177 | |||
| 178 | try: |
||
| 179 | min_degree, max_degree = nums_range |
||
| 180 | except (TypeError, ValueError): |
||
| 181 | raise ValueError( |
||
| 182 | f'If {name} is not a single number, it muste be' |
||
| 183 | f'a sequence of len 2, not {nums_range}' |
||
| 184 | ) |
||
| 185 | |||
| 186 | if not isinstance(min_degree, numbers.Number) or \ |
||
| 187 | not isinstance(max_degree, numbers.Number): |
||
| 188 | message = ( |
||
| 189 | f'{name} values must be numbers, not {nums_range}') |
||
| 190 | raise ValueError(message) |
||
| 191 | |||
| 192 | if min_degree > max_degree: |
||
| 193 | raise ValueError( |
||
| 194 | f'If {name} is a sequence, the second value must be' |
||
| 195 | f' equal or greater than the first, not {nums_range}') |
||
| 196 | |||
| 197 | if min_constraint is not None and min_degree < min_constraint: |
||
| 198 | raise ValueError( |
||
| 199 | f'If {name} is a sequence, the first value must be greater' |
||
| 200 | f'than {min_constraint}, not {min_degree}' |
||
| 201 | ) |
||
| 202 | |||
| 203 | if max_constraint is not None and max_degree > max_constraint: |
||
| 204 | raise ValueError( |
||
| 205 | f'If {name} is a sequence, the second value must be smaller' |
||
| 206 | f'than {max_constraint}, not {max_degree}' |
||
| 207 | ) |
||
| 208 | |||
| 209 | if type_constraint is not None: |
||
| 210 | if not isinstance(min_degree, type_constraint) or \ |
||
| 211 | not isinstance(max_degree, type_constraint): |
||
| 212 | raise ValueError( |
||
| 213 | f'If {name} is a sequence, its values must be of' |
||
| 214 | f'type {type_constraint}, not {nums_range}' |
||
| 215 | ) |
||
| 216 | return nums_range |
||
| 217 | |||
| 306 |