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