|
1 | 1 | # This script is an example of how to use the ModalityCentricPreprocessor class to preprocess a set of MR images. It is only here for quick development and testing purposes. It is not intended to be used in a production environment. |
2 | | -import datetime |
3 | | - |
4 | 2 | from auxiliary.normalization.percentile_normalizer import PercentileNormalizer |
5 | 3 | from auxiliary.turbopath import turbopath |
6 | 4 | from tqdm import tqdm |
|
13 | 11 |
|
14 | 12 | def preprocess(inputDir): |
15 | 13 | inputDir = turbopath(inputDir) |
16 | | - try: |
17 | | - print("*** start ***") |
18 | | - |
19 | | - # where are the raw mr files? |
20 | | - btk_raw_dir = turbopath(inputDir) |
21 | | - |
22 | | - # is the exam already processed? |
23 | | - brainles_dir = turbopath(inputDir) + "/" + inputDir.name + "_brainles" |
24 | | - raw_bet_dir = brainles_dir / "raw_bet" |
25 | | - norm_bet_dir = brainles_dir / "normalized_bet" |
26 | | - raw_skull_dir = brainles_dir / "raw_skull" |
27 | | - norm_skull_dir = brainles_dir / "normalized_skull" |
28 | | - |
29 | | - # if not os.path.exists(prep_dir): |
30 | | - # if os.path.exists(prep_dir): |
31 | | - t1_file = btk_raw_dir.files("*t1.nii.gz") |
32 | | - t1c_file = btk_raw_dir.files("*t1c.nii.gz") |
33 | | - t2_file = btk_raw_dir.files("*t2.nii.gz") |
34 | | - flair_file = btk_raw_dir.files("*fla.nii.gz") |
35 | | - |
36 | | - if len(t1_file) == len(t1c_file) == len(t2_file) == len(flair_file) == 1: |
37 | | - # print(t1_file) |
38 | | - # print(t1c_file) |
39 | | - # print(t2_file) |
40 | | - # print(flair_file) |
41 | | - |
42 | | - t1File = t1_file[0] |
43 | | - t1cFile = t1c_file[0] |
44 | | - t2File = t2_file[0] |
45 | | - flaFile = flair_file[0] |
46 | | - |
47 | | - # normalizer |
48 | | - percentile_normalizer = PercentileNormalizer( |
49 | | - lower_percentile=0.1, |
50 | | - upper_percentile=99.9, |
51 | | - lower_limit=0, |
52 | | - upper_limit=1, |
53 | | - ) |
54 | | - |
55 | | - # define modalities |
56 | | - center = Modality( |
57 | | - modality_name="t1c", |
58 | | - input_path=t1cFile, |
59 | | - raw_bet_output_path=raw_bet_dir / inputDir.name + "_t1c_bet_raw.nii.gz", |
| 14 | + print("*** start ***") |
| 15 | + |
| 16 | + # where are the raw mr files? |
| 17 | + brainles_dir = turbopath(inputDir) + "/" + inputDir.name + "_brainles" |
| 18 | + |
| 19 | + raw_bet_dir = brainles_dir / "raw_bet" |
| 20 | + norm_bet_dir = brainles_dir / "normalized_bet" |
| 21 | + raw_skull_dir = brainles_dir / "raw_skull" |
| 22 | + norm_skull_dir = brainles_dir / "normalized_skull" |
| 23 | + |
| 24 | + t1_file = inputDir.files("*t1.nii.gz") |
| 25 | + t1c_file = inputDir.files("*t1c.nii.gz") |
| 26 | + t2_file = inputDir.files("*t2.nii.gz") |
| 27 | + flair_file = inputDir.files("*fla.nii.gz") |
| 28 | + if len(t1_file) == len(t1c_file) == len(t2_file) == len(flair_file) == 1: |
| 29 | + # print(t1_file) |
| 30 | + # print(t1c_file) |
| 31 | + # print(t2_file) |
| 32 | + # print(flair_file) |
| 33 | + t1File = t1_file[0] |
| 34 | + t1cFile = t1c_file[0] |
| 35 | + t2File = t2_file[0] |
| 36 | + flaFile = flair_file[0] |
| 37 | + # normalizer |
| 38 | + percentile_normalizer = PercentileNormalizer( |
| 39 | + lower_percentile=0.1, |
| 40 | + upper_percentile=99.9, |
| 41 | + lower_limit=0, |
| 42 | + upper_limit=1, |
| 43 | + ) |
| 44 | + # define modalities |
| 45 | + center = Modality( |
| 46 | + modality_name="t1c", |
| 47 | + input_path=t1cFile, |
| 48 | + raw_bet_output_path=raw_bet_dir / inputDir.name + "_t1c_bet_raw.nii.gz", |
| 49 | + raw_skull_output_path=raw_skull_dir / inputDir.name |
| 50 | + + "_t1c_skull_raw.nii.gz", |
| 51 | + normalized_bet_output_path=norm_bet_dir / inputDir.name |
| 52 | + + "_t1c_bet_normalized.nii.gz", |
| 53 | + normalized_skull_output_path=norm_skull_dir / inputDir.name |
| 54 | + + "_t1c_skull_normalized.nii.gz", |
| 55 | + atlas_correction=True, |
| 56 | + normalizer=percentile_normalizer, |
| 57 | + ) |
| 58 | + moving_modalities = [ |
| 59 | + Modality( |
| 60 | + modality_name="t1", |
| 61 | + input_path=t1File, |
| 62 | + raw_bet_output_path=raw_bet_dir / inputDir.name + "_t1_bet_raw.nii.gz", |
60 | 63 | raw_skull_output_path=raw_skull_dir / inputDir.name |
61 | | - + "_t1c_skull_raw.nii.gz", |
| 64 | + + "_t1_skull_raw.nii.gz", |
62 | 65 | normalized_bet_output_path=norm_bet_dir / inputDir.name |
63 | | - + "_t1c_bet_normalized.nii.gz", |
| 66 | + + "_t1_bet_normalized.nii.gz", |
64 | 67 | normalized_skull_output_path=norm_skull_dir / inputDir.name |
65 | | - + "_t1c_skull_normalized.nii.gz", |
| 68 | + + "_t1_skull_normalized.nii.gz", |
66 | 69 | atlas_correction=True, |
67 | 70 | normalizer=percentile_normalizer, |
68 | | - ) |
69 | | - |
70 | | - moving_modalities = [ |
71 | | - Modality( |
72 | | - modality_name="t1", |
73 | | - input_path=t1File, |
74 | | - raw_bet_output_path=raw_bet_dir / inputDir.name |
75 | | - + "_t1_bet_raw.nii.gz", |
76 | | - raw_skull_output_path=raw_skull_dir / inputDir.name |
77 | | - + "_t1_skull_raw.nii.gz", |
78 | | - normalized_bet_output_path=norm_bet_dir / inputDir.name |
79 | | - + "_t1_bet_normalized.nii.gz", |
80 | | - normalized_skull_output_path=norm_skull_dir / inputDir.name |
81 | | - + "_t1_skull_normalized.nii.gz", |
82 | | - atlas_correction=True, |
83 | | - normalizer=percentile_normalizer, |
84 | | - ), |
85 | | - Modality( |
86 | | - modality_name="t2", |
87 | | - input_path=t2File, |
88 | | - raw_bet_output_path=raw_bet_dir / inputDir.name |
89 | | - + "_t2_bet_raw.nii.gz", |
90 | | - raw_skull_output_path=raw_skull_dir / inputDir.name |
91 | | - + "_t2_skull_raw.nii.gz", |
92 | | - normalized_bet_output_path=norm_bet_dir / inputDir.name |
93 | | - + "_t2_bet_normalized.nii.gz", |
94 | | - normalized_skull_output_path=norm_skull_dir / inputDir.name |
95 | | - + "_t2_skull_normalized.nii.gz", |
96 | | - atlas_correction=True, |
97 | | - normalizer=percentile_normalizer, |
98 | | - ), |
99 | | - Modality( |
100 | | - modality_name="flair", |
101 | | - input_path=flaFile, |
102 | | - raw_bet_output_path=raw_bet_dir / inputDir.name |
103 | | - + "_fla_bet_raw.nii.gz", |
104 | | - raw_skull_output_path=raw_skull_dir / inputDir.name |
105 | | - + "_fla_skull_raw.nii.gz", |
106 | | - normalized_bet_output_path=norm_bet_dir / inputDir.name |
107 | | - + "_fla_bet_normalized.nii.gz", |
108 | | - normalized_skull_output_path=norm_skull_dir / inputDir.name |
109 | | - + "_fla_skull_normalized.nii.gz", |
110 | | - atlas_correction=True, |
111 | | - normalizer=percentile_normalizer, |
112 | | - ), |
113 | | - ] |
114 | | - |
115 | | - preprocessor = Preprocessor( |
116 | | - center_modality=center, |
117 | | - moving_modalities=moving_modalities, |
118 | | - registrator=NiftyRegRegistrator(), |
119 | | - brain_extractor=HDBetExtractor(), |
120 | | - temp_folder="tempo", |
121 | | - limit_cuda_visible_devices="1", |
122 | | - ) |
123 | | - |
124 | | - preprocessor.run( |
125 | | - save_dir_coregistration=brainles_dir + "/co-registration", |
126 | | - save_dir_atlas_registration=brainles_dir + "/atlas-registration", |
127 | | - save_dir_atlas_correction=brainles_dir + "/atlas-correction", |
128 | | - save_dir_brain_extraction=brainles_dir + "/brain-extraction", |
129 | | - ) |
130 | | - |
131 | | - except Exception as e: |
132 | | - print("error: " + str(e)) |
133 | | - print("conversion error for:", inputDir) |
134 | | - |
135 | | - time = str(datetime.datetime.now().time()) |
136 | | - |
137 | | - print("** finished:", inputDir.name, "at:", time) |
| 71 | + ), |
| 72 | + Modality( |
| 73 | + modality_name="t2", |
| 74 | + input_path=t2File, |
| 75 | + raw_bet_output_path=raw_bet_dir / inputDir.name + "_t2_bet_raw.nii.gz", |
| 76 | + raw_skull_output_path=raw_skull_dir / inputDir.name |
| 77 | + + "_t2_skull_raw.nii.gz", |
| 78 | + normalized_bet_output_path=norm_bet_dir / inputDir.name |
| 79 | + + "_t2_bet_normalized.nii.gz", |
| 80 | + normalized_skull_output_path=norm_skull_dir / inputDir.name |
| 81 | + + "_t2_skull_normalized.nii.gz", |
| 82 | + atlas_correction=True, |
| 83 | + normalizer=percentile_normalizer, |
| 84 | + ), |
| 85 | + Modality( |
| 86 | + modality_name="flair", |
| 87 | + input_path=flaFile, |
| 88 | + raw_bet_output_path=raw_bet_dir / inputDir.name + "_fla_bet_raw.nii.gz", |
| 89 | + raw_skull_output_path=raw_skull_dir / inputDir.name |
| 90 | + + "_fla_skull_raw.nii.gz", |
| 91 | + normalized_bet_output_path=norm_bet_dir / inputDir.name |
| 92 | + + "_fla_bet_normalized.nii.gz", |
| 93 | + normalized_skull_output_path=norm_skull_dir / inputDir.name |
| 94 | + + "_fla_skull_normalized.nii.gz", |
| 95 | + atlas_correction=True, |
| 96 | + normalizer=percentile_normalizer, |
| 97 | + ), |
| 98 | + ] |
| 99 | + |
| 100 | + preprocessor = Preprocessor( |
| 101 | + center_modality=center, |
| 102 | + moving_modalities=moving_modalities, |
| 103 | + registrator=NiftyRegRegistrator(), |
| 104 | + brain_extractor=HDBetExtractor(), |
| 105 | + temp_folder="temporary_directory", |
| 106 | + limit_cuda_visible_devices="0", |
| 107 | + ) |
| 108 | + |
| 109 | + preprocessor.run( |
| 110 | + save_dir_coregistration=brainles_dir + "/co-registration", |
| 111 | + save_dir_atlas_registration=brainles_dir + "/atlas-registration", |
| 112 | + save_dir_atlas_correction=brainles_dir + "/atlas-correction", |
| 113 | + save_dir_brain_extraction=brainles_dir + "/brain-extraction", |
| 114 | + ) |
138 | 115 |
|
139 | 116 |
|
140 | 117 | ### *** GOGOGO *** ### |
|
0 commit comments