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