|
2 | 2 | from pathlib import Path |
3 | 3 | from typing import List, Optional, Union |
4 | 4 |
|
| 5 | +from loguru import logger |
| 6 | + |
5 | 7 | from brainles_preprocessing.brain_extraction.brain_extractor import BrainExtractor |
6 | 8 | from brainles_preprocessing.constants import Atlas, PreprocessorSteps |
7 | 9 | from brainles_preprocessing.defacing import Defacer, QuickshearDefacer |
8 | 10 | from brainles_preprocessing.modality import CenterModality, Modality |
9 | 11 | from brainles_preprocessing.n4_bias_correction import N4BiasCorrector |
10 | 12 | from brainles_preprocessing.preprocessor.preprocessor import BasePreprocessor |
11 | 13 | from brainles_preprocessing.registration.registrator import Registrator |
12 | | -from brainles_preprocessing.utils.logging_utils import LoggingManager |
13 | 14 | from brainles_preprocessing.utils.zenodo import verify_or_download_atlases |
14 | 15 |
|
15 | | -logging_man = LoggingManager(name=__name__) |
16 | | -logger = logging_man.get_logger() |
17 | | - |
18 | 16 |
|
19 | 17 | class AtlasCentricPreprocessor(BasePreprocessor): |
20 | 18 | """ |
@@ -101,97 +99,102 @@ def run( |
101 | 99 | Results are saved in the specified directories, allowing for modular and configurable output storage. |
102 | 100 | """ |
103 | 101 |
|
104 | | - logging_man._set_log_file(log_file) |
105 | | - logger.info(f"{' Starting preprocessing ':=^80}") |
106 | | - logger.info(f"Logs are saved to {logging_man.log_file_handler.baseFilename}") |
107 | | - modality_names = ", ".join( |
108 | | - [modality.modality_name for modality in self.moving_modalities] |
109 | | - ) |
110 | | - logger.info( |
111 | | - f"Received center modality: {self.center_modality.modality_name} " |
112 | | - f"and moving modalities: {modality_names}" |
113 | | - ) |
114 | | - |
115 | | - # Co-register moving modalities to center modality |
116 | | - logger.info(f"{' Starting Coregistration ':-^80}") |
117 | | - self.run_coregistration( |
118 | | - save_dir_coregistration=save_dir_coregistration, |
119 | | - ) |
120 | | - logger.info( |
121 | | - f"Coregistration complete. Output saved to {save_dir_coregistration}" |
122 | | - ) |
123 | | - |
124 | | - # Register center modality to atlas |
125 | | - logger.info(f"{' Starting atlas registration ':-^80}") |
126 | | - self.run_atlas_registration( |
127 | | - save_dir_atlas_registration=save_dir_atlas_registration, |
128 | | - ) |
129 | | - logger.info( |
130 | | - f"Transformations complete. Output saved to {save_dir_atlas_registration}" |
131 | | - ) |
| 102 | + logger_id = self._add_log_file_handler(log_file) |
| 103 | + try: |
| 104 | + logger.info(f"{' Starting preprocessing ':=^80}") |
| 105 | + modality_names = ", ".join( |
| 106 | + [modality.modality_name for modality in self.moving_modalities] |
| 107 | + ) |
| 108 | + logger.info( |
| 109 | + f"Received center modality: {self.center_modality.modality_name} " |
| 110 | + f"and moving modalities: {modality_names}" |
| 111 | + ) |
132 | 112 |
|
133 | | - # Optional: additional correction in atlas space |
134 | | - logger.info(f"{' Checking optional atlas correction ':-^80}") |
135 | | - self.run_atlas_correction( |
136 | | - save_dir_atlas_correction=save_dir_atlas_correction, |
137 | | - ) |
| 113 | + # Co-register moving modalities to center modality |
| 114 | + logger.info(f"{' Starting Coregistration ':-^80}") |
| 115 | + self.run_coregistration( |
| 116 | + save_dir_coregistration=save_dir_coregistration, |
| 117 | + ) |
| 118 | + logger.info( |
| 119 | + f"Coregistration complete. Output saved to {save_dir_coregistration}" |
| 120 | + ) |
138 | 121 |
|
139 | | - # Optional: N4 bias correction |
140 | | - logger.info(f"{' Checking optional N4 bias correction ':-^80}") |
141 | | - self.run_n4_bias_correction( |
142 | | - save_dir_n4_bias_correction=save_dir_n4_bias_correction, |
143 | | - ) |
| 122 | + # Register center modality to atlas |
| 123 | + logger.info(f"{' Starting atlas registration ':-^80}") |
| 124 | + self.run_atlas_registration( |
| 125 | + save_dir_atlas_registration=save_dir_atlas_registration, |
| 126 | + ) |
| 127 | + logger.info( |
| 128 | + f"Transformations complete. Output saved to {save_dir_atlas_registration}" |
| 129 | + ) |
144 | 130 |
|
145 | | - # Now we save images that are not skullstripped (current image = atlas registered or atlas registered + corrected) |
146 | | - logger.info("Saving non skull-stripped images...") |
147 | | - for modality in self.all_modalities: |
148 | | - if modality.raw_skull_output_path: |
149 | | - modality.save_current_image( |
150 | | - modality.raw_skull_output_path, |
151 | | - normalization=False, |
152 | | - ) |
153 | | - if modality.normalized_skull_output_path: |
154 | | - modality.save_current_image( |
155 | | - modality.normalized_skull_output_path, |
156 | | - normalization=True, |
157 | | - ) |
| 131 | + # Optional: additional correction in atlas space |
| 132 | + logger.info(f"{' Checking optional atlas correction ':-^80}") |
| 133 | + self.run_atlas_correction( |
| 134 | + save_dir_atlas_correction=save_dir_atlas_correction, |
| 135 | + ) |
158 | 136 |
|
159 | | - # Optional: Brain extraction |
160 | | - logger.info(f"{' Checking optional brain extraction ':-^80}") |
161 | | - self.run_brain_extraction( |
162 | | - save_dir_brain_extraction=save_dir_brain_extraction, |
163 | | - ) |
| 137 | + # Optional: N4 bias correction |
| 138 | + logger.info(f"{' Checking optional N4 bias correction ':-^80}") |
| 139 | + self.run_n4_bias_correction( |
| 140 | + save_dir_n4_bias_correction=save_dir_n4_bias_correction, |
| 141 | + ) |
164 | 142 |
|
165 | | - # Defacing |
166 | | - logger.info(f"{' Checking optional defacing ':-^80}") |
167 | | - self.run_defacing( |
168 | | - save_dir_defacing=save_dir_defacing, |
169 | | - ) |
| 143 | + # Now we save images that are not skullstripped (current image = atlas registered or atlas registered + corrected) |
| 144 | + logger.info("Saving non skull-stripped images...") |
| 145 | + for modality in self.all_modalities: |
| 146 | + if modality.raw_skull_output_path: |
| 147 | + modality.save_current_image( |
| 148 | + modality.raw_skull_output_path, |
| 149 | + normalization=False, |
| 150 | + ) |
| 151 | + if modality.normalized_skull_output_path: |
| 152 | + modality.save_current_image( |
| 153 | + modality.normalized_skull_output_path, |
| 154 | + normalization=True, |
| 155 | + ) |
| 156 | + |
| 157 | + # Optional: Brain extraction |
| 158 | + logger.info(f"{' Checking optional brain extraction ':-^80}") |
| 159 | + self.run_brain_extraction( |
| 160 | + save_dir_brain_extraction=save_dir_brain_extraction, |
| 161 | + ) |
170 | 162 |
|
171 | | - # move to separate method |
172 | | - if save_dir_transformations: |
173 | | - save_dir_transformations = Path(save_dir_transformations) |
| 163 | + # Defacing |
| 164 | + logger.info(f"{' Checking optional defacing ':-^80}") |
| 165 | + self.run_defacing( |
| 166 | + save_dir_defacing=save_dir_defacing, |
| 167 | + ) |
174 | 168 |
|
175 | | - # Save transformation matrices |
176 | | - logger.info(f"Saving transformation matrices to {save_dir_transformations}") |
177 | | - for modality in self.all_modalities: |
| 169 | + # move to separate method |
| 170 | + if save_dir_transformations: |
| 171 | + save_dir_transformations = Path(save_dir_transformations) |
178 | 172 |
|
179 | | - modality_transformations_dir = ( |
180 | | - save_dir_transformations / modality.modality_name |
| 173 | + # Save transformation matrices |
| 174 | + logger.info( |
| 175 | + f"Saving transformation matrices to {save_dir_transformations}" |
181 | 176 | ) |
182 | | - modality_transformations_dir.mkdir(exist_ok=True, parents=True) |
183 | | - for step, path in modality.transformation_paths.items(): |
184 | | - if path is not None: |
185 | | - shutil.copyfile( |
186 | | - src=str(path.absolute()), |
187 | | - dst=str( |
188 | | - modality_transformations_dir |
189 | | - / f"{step.value}_{path.name}" |
190 | | - ), |
191 | | - ) |
192 | | - |
193 | | - # End |
194 | | - logger.info(f"{' Preprocessing complete ':=^80}") |
| 177 | + for modality in self.all_modalities: |
| 178 | + |
| 179 | + modality_transformations_dir = ( |
| 180 | + save_dir_transformations / modality.modality_name |
| 181 | + ) |
| 182 | + modality_transformations_dir.mkdir(exist_ok=True, parents=True) |
| 183 | + for step, path in modality.transformation_paths.items(): |
| 184 | + if path is not None: |
| 185 | + shutil.copyfile( |
| 186 | + src=str(path.absolute()), |
| 187 | + dst=str( |
| 188 | + modality_transformations_dir |
| 189 | + / f"{step.value}_{path.name}" |
| 190 | + ), |
| 191 | + ) |
| 192 | + |
| 193 | + # End |
| 194 | + logger.info(f"{' Preprocessing complete ':=^80}") |
| 195 | + finally: |
| 196 | + # Remove log file handler if it was added |
| 197 | + logger.remove(logger_id) |
195 | 198 |
|
196 | 199 | def run_atlas_registration( |
197 | 200 | self, save_dir_atlas_registration: Optional[Union[str, Path]] = None |
|
0 commit comments