| 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 numbers |
||
| 79 | @staticmethod |
||
| 80 | def parse_range( |
||
| 81 | nums_range: Union[TypeNumber, Tuple[TypeNumber, TypeNumber]], |
||
| 82 | name: str, |
||
| 83 | min_constraint: TypeNumber = None, |
||
| 84 | max_constraint: TypeNumber = None, |
||
| 85 | type_constraint: type = None, |
||
| 86 | ) -> Tuple[TypeNumber, TypeNumber]: |
||
| 87 | r"""Adapted from ``torchvision.transforms.RandomRotation``. |
||
| 88 | |||
| 89 | Args: |
||
| 90 | nums_range: Tuple of two numbers :math:`(n_{min}, n_{max})`, |
||
| 91 | where :math:`n_{min} \leq n_{max}`. |
||
| 92 | If a single positive number :math:`n` is provided, |
||
| 93 | :math:`n_{min} = -n` and :math:`n_{max} = n`. |
||
| 94 | name: Name of the parameter, so that an informative error message |
||
| 95 | can be printed. |
||
| 96 | min_constraint: Minimal value that :math:`n_{min}` can take, |
||
| 97 | default is None, i.e. there is no minimal value. |
||
| 98 | max_constraint: Maximal value that :math:`n_{max}` can take, |
||
| 99 | default is None, i.e. there is no maximal value. |
||
| 100 | type_constraint: Precise type that :math:`n_{max}` and |
||
| 101 | :math:`n_{min}` must take. |
||
| 102 | |||
| 103 | Returns: |
||
| 104 | A tuple of two numbers :math:`(n_{min}, n_{max})`. |
||
| 105 | |||
| 106 | Raises: |
||
| 107 | ValueError: if :attr:`nums_range` is negative |
||
| 108 | ValueError: if :math:`n_{max}` or :math:`n_{min}` is not a number |
||
| 109 | ValueError: if :math:`n_{max} \lt n_{min}` |
||
| 110 | ValueError: if :attr:`min_constraint` is not None and |
||
| 111 | :math:`n_{min}` is smaller than :attr:`min_constraint` |
||
| 112 | ValueError: if :attr:`max_constraint` is not None and |
||
| 113 | :math:`n_{max}` is greater than :attr:`max_constraint` |
||
| 114 | ValueError: if :attr:`type_constraint` is not None and |
||
| 115 | :math:`n_{max}` and :math:`n_{max}` are not of type |
||
| 116 | :attr:`type_constraint`. |
||
| 117 | """ |
||
| 118 | if isinstance(nums_range, numbers.Number): |
||
| 119 | if nums_range < 0: |
||
| 120 | raise ValueError( |
||
| 121 | f'If {name} is a single number,' |
||
| 122 | f' it must be positive, not {nums_range}') |
||
| 123 | if min_constraint is not None and nums_range < min_constraint: |
||
| 124 | raise ValueError( |
||
| 125 | f'If {name} is a single number, it must be greater' |
||
| 126 | f'than {min_constraint}, not {nums_range}' |
||
| 127 | ) |
||
| 128 | if max_constraint is not None and nums_range > max_constraint: |
||
| 129 | raise ValueError( |
||
| 130 | f'If {name} is a single number, it must be smaller' |
||
| 131 | f'than {max_constraint}, not {nums_range}' |
||
| 132 | ) |
||
| 133 | if type_constraint is not None and \ |
||
| 134 | not isinstance(nums_range, type_constraint): |
||
| 135 | raise ValueError( |
||
| 136 | f'If {name} is a single number, it must be of' |
||
| 137 | f'type {type_constraint}, not {nums_range}' |
||
| 138 | ) |
||
| 139 | min_range = -nums_range if min_constraint is None else nums_range |
||
| 140 | return (min_range, nums_range) |
||
| 141 | |||
| 142 | try: |
||
| 143 | min_degree, max_degree = nums_range |
||
| 144 | except (TypeError, ValueError): |
||
| 145 | raise ValueError( |
||
| 146 | f'If {name} is not a single number, it muste be' |
||
| 147 | f'a sequence of len 2, not {nums_range}' |
||
| 148 | ) |
||
| 149 | |||
| 150 | if not isinstance(min_degree, numbers.Number) or \ |
||
| 151 | not isinstance(max_degree, numbers.Number): |
||
| 152 | message = ( |
||
| 153 | f'{name} values must be numbers, not {nums_range}') |
||
| 154 | raise ValueError(message) |
||
| 155 | |||
| 156 | if min_degree > max_degree: |
||
| 157 | raise ValueError( |
||
| 158 | f'If {name} is a sequence, the second value must be' |
||
| 159 | f' equal or greater than the first, not {nums_range}') |
||
| 160 | |||
| 161 | if min_constraint is not None and min_degree < min_constraint: |
||
| 162 | raise ValueError( |
||
| 163 | f'If {name} is a sequence, the first value must be greater' |
||
| 164 | f'than {min_constraint}, not {min_degree}' |
||
| 165 | ) |
||
| 166 | |||
| 167 | if max_constraint is not None and max_degree > max_constraint: |
||
| 168 | raise ValueError( |
||
| 169 | f'If {name} is a sequence, the second value must be smaller' |
||
| 170 | f'than {max_constraint}, not {max_degree}' |
||
| 171 | ) |
||
| 172 | |||
| 173 | if type_constraint is not None: |
||
| 174 | if not isinstance(min_degree, type_constraint) or \ |
||
| 175 | not isinstance(max_degree, type_constraint): |
||
| 176 | raise ValueError( |
||
| 177 | f'If {name} is a sequence, its values must be of' |
||
| 178 | f'type {type_constraint}, not {nums_range}' |
||
| 179 | ) |
||
| 180 | return nums_range |
||
| 181 | |||
| 266 |