|
a |
|
b/save_middle_slices_as_images.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "markdown", |
|
|
5 |
"metadata": {}, |
|
|
6 |
"source": [ |
|
|
7 |
"## Save middle slices of scans as images\n", |
|
|
8 |
"Most image models expect a three-channel input image. Will take the middle three grayscale slices, and dilated middle three slices centered on the middle, and save them as three-channel images." |
|
|
9 |
] |
|
|
10 |
}, |
|
|
11 |
{ |
|
|
12 |
"cell_type": "code", |
|
|
13 |
"execution_count": 1, |
|
|
14 |
"metadata": {}, |
|
|
15 |
"outputs": [], |
|
|
16 |
"source": [ |
|
|
17 |
"import numpy as np\n", |
|
|
18 |
"from PIL import Image\n", |
|
|
19 |
"import os" |
|
|
20 |
] |
|
|
21 |
}, |
|
|
22 |
{ |
|
|
23 |
"cell_type": "code", |
|
|
24 |
"execution_count": 2, |
|
|
25 |
"metadata": {}, |
|
|
26 |
"outputs": [ |
|
|
27 |
{ |
|
|
28 |
"name": "stdout", |
|
|
29 |
"output_type": "stream", |
|
|
30 |
"text": [ |
|
|
31 |
"\u001b[01;34m..\u001b[00m\n", |
|
|
32 |
"├── \u001b[01;34mdata\u001b[00m\n", |
|
|
33 |
"│ ├── \u001b[01;34m__MACOSX\u001b[00m\n", |
|
|
34 |
"│ │ └── \u001b[01;34mMRNet-v1.0\u001b[00m\n", |
|
|
35 |
"│ │ ├── \u001b[01;34mtrain\u001b[00m\n", |
|
|
36 |
"│ │ │ ├── \u001b[01;34maxial\u001b[00m\n", |
|
|
37 |
"│ │ │ ├── \u001b[01;34mcoronal\u001b[00m\n", |
|
|
38 |
"│ │ │ └── \u001b[01;34msagittal\u001b[00m\n", |
|
|
39 |
"│ │ └── \u001b[01;34mvalid\u001b[00m\n", |
|
|
40 |
"│ ├── \u001b[01;34mmid1\u001b[00m\n", |
|
|
41 |
"│ │ ├── \u001b[01;34mtrain\u001b[00m\n", |
|
|
42 |
"│ │ │ ├── \u001b[01;34maxial\u001b[00m\n", |
|
|
43 |
"│ │ │ ├── \u001b[01;34mcoronal\u001b[00m\n", |
|
|
44 |
"│ │ │ └── \u001b[01;34msagittal\u001b[00m\n", |
|
|
45 |
"│ │ └── \u001b[01;34mvalid\u001b[00m\n", |
|
|
46 |
"│ │ ├── \u001b[01;34maxial\u001b[00m\n", |
|
|
47 |
"│ │ ├── \u001b[01;34mcoronal\u001b[00m\n", |
|
|
48 |
"│ │ └── \u001b[01;34msagittal\u001b[00m\n", |
|
|
49 |
"│ ├── \u001b[01;34mMRNet-small\u001b[00m\n", |
|
|
50 |
"│ │ ├── \u001b[01;34mtrain\u001b[00m\n", |
|
|
51 |
"│ │ │ ├── \u001b[01;34maxial\u001b[00m\n", |
|
|
52 |
"│ │ │ ├── \u001b[01;34mcoronal\u001b[00m\n", |
|
|
53 |
"│ │ │ └── \u001b[01;34msagittal\u001b[00m\n", |
|
|
54 |
"│ │ └── \u001b[01;34mvalid\u001b[00m\n", |
|
|
55 |
"│ │ ├── \u001b[01;34maxial\u001b[00m\n", |
|
|
56 |
"│ │ ├── \u001b[01;34mcoronal\u001b[00m\n", |
|
|
57 |
"│ │ └── \u001b[01;34msagittal\u001b[00m\n", |
|
|
58 |
"│ └── \u001b[01;34mMRNet-v1.0\u001b[00m\n", |
|
|
59 |
"│ ├── \u001b[01;34mtrain\u001b[00m\n", |
|
|
60 |
"│ │ ├── \u001b[01;34maxial\u001b[00m\n", |
|
|
61 |
"│ │ ├── \u001b[01;34mcoronal\u001b[00m\n", |
|
|
62 |
"│ │ └── \u001b[01;34msagittal\u001b[00m\n", |
|
|
63 |
"│ └── \u001b[01;34mvalid\u001b[00m\n", |
|
|
64 |
"│ ├── \u001b[01;34maxial\u001b[00m\n", |
|
|
65 |
"│ ├── \u001b[01;34mcoronal\u001b[00m\n", |
|
|
66 |
"│ └── \u001b[01;34msagittal\u001b[00m\n", |
|
|
67 |
"└── \u001b[01;34mmrnet-fastai\u001b[00m\n", |
|
|
68 |
" └── \u001b[01;34mexp\u001b[00m\n", |
|
|
69 |
"\n", |
|
|
70 |
"37 directories\n" |
|
|
71 |
] |
|
|
72 |
} |
|
|
73 |
], |
|
|
74 |
"source": [ |
|
|
75 |
"! tree -d .." |
|
|
76 |
] |
|
|
77 |
}, |
|
|
78 |
{ |
|
|
79 |
"cell_type": "markdown", |
|
|
80 |
"metadata": {}, |
|
|
81 |
"source": [ |
|
|
82 |
"Copy data to another directory, for example `/mid3d0`, for middle 3 slices, 0 dilation.\n", |
|
|
83 |
"Do this for d in (0,1,2) and perhaps greater than 2." |
|
|
84 |
] |
|
|
85 |
}, |
|
|
86 |
{ |
|
|
87 |
"cell_type": "code", |
|
|
88 |
"execution_count": 3, |
|
|
89 |
"metadata": {}, |
|
|
90 |
"outputs": [], |
|
|
91 |
"source": [ |
|
|
92 |
"!cp -r ../data/MRNet-v1.0 ../data/mid3d0" |
|
|
93 |
] |
|
|
94 |
}, |
|
|
95 |
{ |
|
|
96 |
"cell_type": "markdown", |
|
|
97 |
"metadata": {}, |
|
|
98 |
"source": [ |
|
|
99 |
"Recurse through /train and /valid directories, load the scan file (.npy), select middle 3 slices, possibly with gaps of size dilation between them, and save as 3-channel images. Discard the copied files." |
|
|
100 |
] |
|
|
101 |
}, |
|
|
102 |
{ |
|
|
103 |
"cell_type": "code", |
|
|
104 |
"execution_count": 7, |
|
|
105 |
"metadata": {}, |
|
|
106 |
"outputs": [ |
|
|
107 |
{ |
|
|
108 |
"name": "stdout", |
|
|
109 |
"output_type": "stream", |
|
|
110 |
"text": [ |
|
|
111 |
"-----------------------\n", |
|
|
112 |
"Creating png files centered on middle slices with dilations of size 2\n", |
|
|
113 |
"Processing 1130 .npy files in ../data/mid3d2/train/axial\n", |
|
|
114 |
"===============================\n", |
|
|
115 |
"Processing 1130 .npy files in ../data/mid3d2/train/coronal\n", |
|
|
116 |
"===============================\n", |
|
|
117 |
"Processing 1130 .npy files in ../data/mid3d2/train/sagittal\n", |
|
|
118 |
"===============================\n", |
|
|
119 |
"Processing 120 .npy files in ../data/mid3d2/valid/axial\n", |
|
|
120 |
"===============================\n", |
|
|
121 |
"Processing 120 .npy files in ../data/mid3d2/valid/coronal\n", |
|
|
122 |
"===============================\n", |
|
|
123 |
"Processing 120 .npy files in ../data/mid3d2/valid/sagittal\n", |
|
|
124 |
"===============================\n" |
|
|
125 |
] |
|
|
126 |
} |
|
|
127 |
], |
|
|
128 |
"source": [ |
|
|
129 |
"dilations = [0,1,2]\n", |
|
|
130 |
"for d in dilations:\n", |
|
|
131 |
" print('-----------------------')\n", |
|
|
132 |
" print('Creating png files centered on middle slices with dilations of size {}'.format(d))\n", |
|
|
133 |
" for ds in ('train','valid'):\n", |
|
|
134 |
" for p in ('axial','coronal','sagittal'):\n", |
|
|
135 |
" dirpath = '../data/mid3d{}/{}/{}'.format(d, ds, p)\n", |
|
|
136 |
" npy_files = [f for f in os.listdir(dirpath) if f[-4:]=='.npy']\n", |
|
|
137 |
" print('Processing {} .npy files in {}'.format(len(npy_files), dirpath))\n", |
|
|
138 |
"\n", |
|
|
139 |
" for npyf in npy_files:\n", |
|
|
140 |
" npyfilepath = dirpath + '/' + npyf\n", |
|
|
141 |
" #print('Converting {}'.format(npyfilepath))\n", |
|
|
142 |
" # load the array\n", |
|
|
143 |
" scanarray = np.load(npyfilepath)\n", |
|
|
144 |
" #print('Number of slices for this scan: {}'.format(scanarray.shape[0]))\n", |
|
|
145 |
" # get the middle slice index\n", |
|
|
146 |
" midslice = scanarray.shape[0]//2\n", |
|
|
147 |
" # get indices for three slices centered on midslice, skipping dilation d images between the slices\n", |
|
|
148 |
" slice_indices = range(midslice-(1+d),midslice+(1+d+1),1+d)\n", |
|
|
149 |
" slices3 = scanarray[slice_indices,:,:]\n", |
|
|
150 |
" # put channels last for PIL\n", |
|
|
151 |
" slices3 = np.moveaxis(slices3, 0, -1)\n", |
|
|
152 |
" # convert to rgb\n", |
|
|
153 |
" im = Image.fromarray(slices3)\n", |
|
|
154 |
" # save file\n", |
|
|
155 |
" # replace .npy with .png\n", |
|
|
156 |
" pngf = npyf[:-4] + '.png'\n", |
|
|
157 |
" pngfilepath = dirpath + '/' + pngf\n", |
|
|
158 |
" im.save(pngfilepath)\n", |
|
|
159 |
" # remove corresponding .npy file\n", |
|
|
160 |
" os.remove(npyfilepath)\n", |
|
|
161 |
"\n", |
|
|
162 |
" print('===============================') " |
|
|
163 |
] |
|
|
164 |
}, |
|
|
165 |
{ |
|
|
166 |
"cell_type": "markdown", |
|
|
167 |
"metadata": {}, |
|
|
168 |
"source": [ |
|
|
169 |
"## Take just middle slice from each scan" |
|
|
170 |
] |
|
|
171 |
}, |
|
|
172 |
{ |
|
|
173 |
"cell_type": "code", |
|
|
174 |
"execution_count": 11, |
|
|
175 |
"metadata": {}, |
|
|
176 |
"outputs": [ |
|
|
177 |
{ |
|
|
178 |
"name": "stdout", |
|
|
179 |
"output_type": "stream", |
|
|
180 |
"text": [ |
|
|
181 |
"Found 1130 .npy files in ../data/mid1/train/axial\n", |
|
|
182 |
"===============================\n", |
|
|
183 |
"===============================\n", |
|
|
184 |
"Found 1130 .npy files in ../data/mid1/train/coronal\n", |
|
|
185 |
"===============================\n", |
|
|
186 |
"===============================\n", |
|
|
187 |
"Found 1130 .npy files in ../data/mid1/train/sagittal\n", |
|
|
188 |
"===============================\n", |
|
|
189 |
"===============================\n", |
|
|
190 |
"Found 120 .npy files in ../data/mid1/valid/axial\n", |
|
|
191 |
"===============================\n", |
|
|
192 |
"===============================\n", |
|
|
193 |
"Found 120 .npy files in ../data/mid1/valid/coronal\n", |
|
|
194 |
"===============================\n", |
|
|
195 |
"===============================\n", |
|
|
196 |
"Found 120 .npy files in ../data/mid1/valid/sagittal\n", |
|
|
197 |
"===============================\n", |
|
|
198 |
"===============================\n" |
|
|
199 |
] |
|
|
200 |
} |
|
|
201 |
], |
|
|
202 |
"source": [ |
|
|
203 |
"dilation = ''\n", |
|
|
204 |
"for d in ('train','valid'):\n", |
|
|
205 |
" for p in ('axial','coronal','sagittal'):\n", |
|
|
206 |
" dirpath = '../data/mid1{}/{}/{}'.format(dilation, d, p)\n", |
|
|
207 |
" npy_files = [f for f in os.listdir(dirpath) if f[-4:]=='.npy']\n", |
|
|
208 |
" print('Processing {} .npy files in {}'.format(len(npy_files), dirpath))\n", |
|
|
209 |
"\n", |
|
|
210 |
" for f in npy_files:\n", |
|
|
211 |
" filepath = dirpath + '/' + f\n", |
|
|
212 |
" #print('Converting {}'.format(filepath))\n", |
|
|
213 |
" # load the array\n", |
|
|
214 |
" scanarray = np.load(filepath)\n", |
|
|
215 |
" # calculate interpolation factor(s)\n", |
|
|
216 |
" #print('Number of slices for this scan: {}'.format(scanarray.shape[0]))\n", |
|
|
217 |
" midslice = scanarray.shape[0]//2\n", |
|
|
218 |
" im = Image.fromarray(scanarray[midslice,:,:], 'L').convert('RGB')\n", |
|
|
219 |
" # replace .npy with .png\n", |
|
|
220 |
" newf = f[:-4] + '.png'\n", |
|
|
221 |
" newfilepath = dirpath + '/' + newf\n", |
|
|
222 |
" im.save(newfilepath)\n", |
|
|
223 |
" # remove corresponding .npy file\n", |
|
|
224 |
" os.remove(filepath)\n", |
|
|
225 |
" \n", |
|
|
226 |
" print('===============================') " |
|
|
227 |
] |
|
|
228 |
}, |
|
|
229 |
{ |
|
|
230 |
"cell_type": "code", |
|
|
231 |
"execution_count": null, |
|
|
232 |
"metadata": {}, |
|
|
233 |
"outputs": [], |
|
|
234 |
"source": [] |
|
|
235 |
} |
|
|
236 |
], |
|
|
237 |
"metadata": { |
|
|
238 |
"kernelspec": { |
|
|
239 |
"display_name": "Python 3", |
|
|
240 |
"language": "python", |
|
|
241 |
"name": "python3" |
|
|
242 |
}, |
|
|
243 |
"language_info": { |
|
|
244 |
"codemirror_mode": { |
|
|
245 |
"name": "ipython", |
|
|
246 |
"version": 3 |
|
|
247 |
}, |
|
|
248 |
"file_extension": ".py", |
|
|
249 |
"mimetype": "text/x-python", |
|
|
250 |
"name": "python", |
|
|
251 |
"nbconvert_exporter": "python", |
|
|
252 |
"pygments_lexer": "ipython3", |
|
|
253 |
"version": "3.7.3" |
|
|
254 |
} |
|
|
255 |
}, |
|
|
256 |
"nbformat": 4, |
|
|
257 |
"nbformat_minor": 2 |
|
|
258 |
} |