| Conditions | 8 |
| Total Lines | 88 |
| Code Lines | 35 |
| 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:
| 1 | from pathlib import Path |
||
| 100 | @classmethod |
||
| 101 | def train( |
||
| 102 | cls, |
||
| 103 | images_paths: Sequence[TypePath], |
||
| 104 | cutoff: Optional[Tuple[float, float]] = None, |
||
| 105 | mask_path: Optional[TypePath] = None, |
||
| 106 | masking_function: Optional[Callable] = None, |
||
| 107 | output_path: Optional[TypePath] = None, |
||
| 108 | ) -> np.ndarray: |
||
| 109 | """Extract average histogram landmarks from images used for training. |
||
| 110 | |||
| 111 | Args: |
||
| 112 | images_paths: List of image paths used to train. |
||
| 113 | cutoff: Optional minimum and maximum quantile values, |
||
| 114 | respectively, that are used to select a range of intensity of |
||
| 115 | interest. Equivalent to :math:`pc_1` and :math:`pc_2` in |
||
| 116 | `Nyúl and Udupa's paper <http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.204.102&rep=rep1&type=pdf>`_. |
||
| 117 | mask_path: Optional path to a mask image to extract voxels used for |
||
| 118 | training. |
||
| 119 | masking_function: Optional function used to extract voxels used for |
||
| 120 | training. |
||
| 121 | output_path: Optional file path with extension ``.txt`` or |
||
| 122 | ``.npy``, where the landmarks will be saved. |
||
| 123 | |||
| 124 | Example: |
||
| 125 | |||
| 126 | >>> import torch |
||
| 127 | >>> import numpy as np |
||
| 128 | >>> from pathlib import Path |
||
| 129 | >>> from torchio.transforms import HistogramStandardization |
||
| 130 | >>> |
||
| 131 | >>> t1_paths = ['subject_a_t1.nii', 'subject_b_t1.nii.gz'] |
||
| 132 | >>> t2_paths = ['subject_a_t2.nii', 'subject_b_t2.nii.gz'] |
||
| 133 | >>> |
||
| 134 | >>> t1_landmarks_path = Path('t1_landmarks.npy') |
||
| 135 | >>> t2_landmarks_path = Path('t2_landmarks.npy') |
||
| 136 | >>> |
||
| 137 | >>> t1_landmarks = ( |
||
| 138 | ... t1_landmarks_path |
||
| 139 | ... if t1_landmarks_path.is_file() |
||
| 140 | ... else HistogramStandardization.train(t1_paths) |
||
| 141 | ... ) |
||
| 142 | >>> torch.save(t1_landmarks, t1_landmarks_path) |
||
| 143 | >>> |
||
| 144 | >>> t2_landmarks = ( |
||
| 145 | ... t2_landmarks_path |
||
| 146 | ... if t2_landmarks_path.is_file() |
||
| 147 | ... else HistogramStandardization.train(t2_paths) |
||
| 148 | ... ) |
||
| 149 | >>> torch.save(t2_landmarks, t2_landmarks_path) |
||
| 150 | >>> |
||
| 151 | >>> landmarks_dict = { |
||
| 152 | ... 't1': t1_landmarks, |
||
| 153 | ... 't2': t2_landmarks, |
||
| 154 | ... } |
||
| 155 | >>> |
||
| 156 | >>> transform = HistogramStandardization(landmarks_dict) |
||
| 157 | """ |
||
| 158 | quantiles_cutoff = DEFAULT_CUTOFF if cutoff is None else cutoff |
||
| 159 | percentiles_cutoff = 100 * np.array(quantiles_cutoff) |
||
| 160 | percentiles_database = [] |
||
| 161 | percentiles = _get_percentiles(percentiles_cutoff) |
||
| 162 | for image_file_path in tqdm(images_paths): |
||
| 163 | tensor, _ = read_image(image_file_path) |
||
| 164 | data = tensor.numpy() |
||
| 165 | if masking_function is not None: |
||
| 166 | mask = masking_function(data) |
||
| 167 | else: |
||
| 168 | if mask_path is not None: |
||
| 169 | mask, _ = read_image(mask_path) |
||
| 170 | mask = mask.numpy() > 0 |
||
| 171 | else: |
||
| 172 | mask = np.ones_like(data, dtype=np.bool) |
||
| 173 | percentile_values = np.percentile(data[mask], percentiles) |
||
| 174 | percentiles_database.append(percentile_values) |
||
| 175 | percentiles_database = np.vstack(percentiles_database) |
||
| 176 | mapping = _get_average_mapping(percentiles_database) |
||
| 177 | |||
| 178 | if output_path is not None: |
||
| 179 | output_path = Path(output_path).expanduser() |
||
| 180 | extension = output_path.suffix |
||
| 181 | if extension == '.txt': |
||
| 182 | modality = 'image' |
||
| 183 | text = f'{modality} {" ".join(map(str, mapping))}' |
||
| 184 | output_path.write_text(text) |
||
| 185 | elif extension == '.npy': |
||
| 186 | np.save(output_path, mapping) |
||
| 187 | return mapping |
||
| 188 | |||
| 287 |