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