-
Notifications
You must be signed in to change notification settings - Fork 1
/
imaging.py
511 lines (429 loc) · 18.1 KB
/
imaging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
"""Data analytics functions for processing neuroimaging data
Functions:
parse_volumes -> dataframe
find_largest_volume -> tuple
load_scan(path) -> array
get_pixels_hu -> array
show_scan
extract_pixels -> array
flatten -> array
flat_wrapper -> array
show_dist
show_cluster_dist
cluster -> dataframe
cluster_wrapper -> dataframe
cluster_modes -> dataframe
find_middle_cluster -> integer
filter_by_cluster -> dataframe
get_HUrange -> tuple
compare_scans
mask -> array
mask_wrapper -> array
binary_mask -> array
remove_islands -> array
render_volume -> figure
"""
import os
from dotenv import dotenv_values
import pydicom
from math import *
import numpy as np
import pandas as pd
import pickle
import copy
import matplotlib.pyplot as plt
from itertools import chain
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px
from sklearn.cluster import KMeans
import cv2
def parse_volumes(dicom_path=None, debug=True):
"""Extract all volumes from the selected DICOM directory and return the file paths
Keyword arguments:
path -- the directory where *ALL* the DICOM (.dcm) images are located
Returns: Dataframe of metadata about the volumes
"""
if (dicom_path==None):
config = dotenv_values(".env")
dicom_path = config['DICOM_SAVE']
volumes = {}
for path, subdirs, files in os.walk(dicom_path):
for name in files:
file_path = os.path.join(path,name)
splitfile = os.path.splitext(name)
vol = ('/').join(file_path.split('/')[:-1])
if splitfile[1]=='.dcm':
if vol not in volumes:
volumes[vol]=[]
else:
volumes[vol].append(name)
df = pd.DataFrame()
df['path']=list(volumes.keys())
df['files']=list(volumes.values())
df.index = [(path.split('/'))[-1] for path in df['path']]
df['count']=[len(files) for files in df['files']]
if debug:
print(df.drop(columns=['files']))
print(f'\nThe volume with the highest slice count can be found at: \n {df[df["count"] == df["count"].max()]["path"][0]}')
return df
def find_largest_volume(dicom_path=None, debug=True):
"""Find the volume with the greatest number of slices (for demonstration)
Keyword arguments:
dicom_path -- the directory where *ALL* the DICOM (.dcm) images are located
Returns: Tuple of (path, name) of first largest volume in the dicom path
"""
volumes = parse_volumes(dicom_path=dicom_path, debug=debug)
path = volumes[volumes["count"] == volumes["count"].max()]["path"][0]
name = list(volumes[volumes["count"] == volumes["count"].max()].index)[0]
return path, name
def load_scan(path):
"""Load DICOM data from a local directory.
Keyword arguments:
path -- the directory where the target volume's DICOM (.dcm) images are located
Returns: 3D pixel array of DICOM slices
"""
slices = [pydicom.dcmread(os.path.join(path,s)) for s in
os.listdir(path)]
slices = [s for s in slices if 'SliceLocation' in s]
slices.sort(key = lambda x: int(x.InstanceNumber))
return slices
def get_pixels_hu(slices):
"""Extract an array of Hounsfield Unit values from a stack of scans
Source: https://hengloose.medium.com/a-comprehensive-starter-guide-to-visualizing-and-analyzing-dicom-images-in-python-7a8430fcb7ed
Author: Franklin Heng
Keyword arguments:
slices -- an array of images
Returns: 3D numpy array of HU values
"""
image = np.stack([s.pixel_array for s in slices])
image = image.astype(np.int16)
image[image == -2000] = 0 # Set outside-of-scan pixels to 0
intercept = slices[0].RescaleIntercept if 'RescaleIntercept' in slices[0] else -1024 # Convert to Hounsfield units (HU)
slope = slices[0].RescaleSlope if 'RescaleSlope' in slices[0] else 1
if slope != 1:
image = slope * image.astype(np.float64)
image = image.astype(np.int16)
image += np.int16(intercept)
return np.array(image, dtype=np.int16)
def show_scan(pixels, viewer='plotly', output=False):
"""Render an image of the scan by slicing through the middle of the 3D array
Keyword arguments:
pixels -- a 3D numpy array of pixels for each slice of the DICOM image
output -- (optional) default: None; path of a location to save the image
Returns: Nothing; shows image
"""
midbrain = int(np.floor(len(pixels)/2))
fig = go.Figure(
data=go.Heatmap(z=pixels[midbrain],
colorscale = 'gray'),
)
if output:
head, tail = os.path.split(output)
htmlsavepath = os.path.join(head, tail+'.html')
fig.write_html(htmlsavepath)
pngsavepath = os.path.join(head, tail+'.png')
plt.imsave(pngsavepath, pixels[midbrain], cmap=plt.cm.binary)
if viewer.lower() =='plotly':
fig.show()
plt.ioff()
plt.close()
else:
plt.imshow(pixels[midbrain], cmap=plt.cm.binary)
plt.show()
def extract_pixels(path, debug=True):
"""Extract an array of pixels from a DICOM directory
Keyword arguments:
path -- the directory where the target volume's DICOM (.dcm) images are located
Returns: 3D numpy array of HU values
"""
dicom = load_scan(path)
pixels = get_pixels_hu(dicom)
if debug==True: # If debugging, show the image
show_scan(pixels, output=path)
return pixels
def flatten(pixels, output=False, min_filter=-1024):
"""Flattens a 3D numpy array of pixels for each slice of the DICOM image into a single 1D array,
excluding any values below the min_filter, and optionally saves the array as a pickle file
Keyword arguments:
pixels -- a 3D numpy array of pixels for each slice of the DICOM image
output -- (optional) default: None; path of a location to save the pickle
Returns: flattened 1D array of HU values greater than the min_filter value
"""
flatten_list = list(chain.from_iterable(chain.from_iterable(pixels)))
result = [x for x in flatten_list if x > min_filter]
if output:
try: os.makedirs(output)
except: pass
with open(os.path.join(output, f'flattened.pkl'), 'wb') as f:
pickle.dump(result, f)
return result
def flat_wrapper(pixels, output=False):
"""Wraps the flatten() function so as to check if the data has already been flattened
and not repeat the process if it is not necessary.
Keyword arguments:
pixels -- a 3D numpy array of pixels for each slice of the DICOM image
output -- (optional) default: None; the possible pre-existing save location to check
Returns: flattened 1D array of HU values
"""
if output:
try:
flat = pd.read_pickle(os.path.join(output, f'flattened.pkl'))
return flat
except:
pass
flat = flatten(pixels, output=output)
return flat
def show_dist(flat, viewer='plotly', output=False):
"""Display the distribution of values in a 1D array
Keyword arguments:
flat -- flattened 1D array of values
viewer -- 'plotly' (case-insensitive) for interactive ; anything else (default: 'mpl') returns a static matplotlib histogram
output -- (optional) default: None; (optionally, path and) filename *without extension* where to save the histogram
Returns: Nothing; shows image
"""
fig = go.Figure(data=[go.Histogram(x=flat)])
plt.clf()
plt.hist(flat, 100, facecolor='blue', alpha=0.5)
if output:
try: os.makedirs(output)
except: pass
fig.write_html(os.path.join(output,'pixel_distribution.html'))
plt.savefig(os.path.join(output,'pixel_distribution.png'))
if viewer.lower() =='plotly':
fig.show()
plt.ioff()
plt.close()
else: plt.show()
def show_cluster_dist(df, viewer='plotly', output=False):
"""Display the distribution of values by cluster
Keyword arguments:
df -- the pandas dataframe that stores the values, where (df.x is value) and (df.y is cluster_id)
viewer -- 'plotly' (case-insensitive) for interactive ; anything else returns a static matplotlib histogram
output -- (optional) default: None; (optionally, path and) filename *without extension* where to save the histogram
Returns: Nothing; shows image
"""
fig = px.histogram(df, x="x", color="y")
dfArr = []
for l in list(set(df['y'])):
dfArr.append(df[df['y']==l])
colors = ['red', 'green', 'blue','orange','purple','yellow','brown','gray']
i = 0
plt.clf()
for c in dfArr:
plt.hist(c['x'], 100, facecolor=colors[i], alpha=0.5, label=i)
i+=1
if output:
try: os.makedirs(output)
except: pass
fig.write_html(os.path.join(output,'cluster_distribution.html'))
plt.savefig(os.path.join(output,'cluster_distribution.png'))
if viewer.lower()=='plotly':
fig.show()
plt.ioff()
plt.close()
else: plt.show()
def cluster(flat, k=3, output=None):
"""Run k-means pixel clustering on a 1D array and (optionally) save as CSV
Keyword arguments:
flat -- 1D array of values
k -- number of clusters (default: 3)
output -- (optional) default: None; location to save the CSV
Returns: Dataframe of metadata about the cluster index for each pixel
"""
km = KMeans(n_clusters=k)
npArr = np.array(flat).reshape(-1,1)
km.fit(npArr)
label = km.fit_predict(npArr)
df = pd.DataFrame(data={'x':flat, 'y':label})
if output:
try: os.makedirs(output)
except: pass
df.to_csv(os.path.join(output,f'cluster_k{k}.csv'), index=False)
show_cluster_dist(df, output=output)
return df
def cluster_wrapper(pixels=False, flat=None, k=3, output=False):
"""Wraps the flatten() and cluster() functions
Keyword arguments:
pixels -- (optional) a 3D numpy array of pixels for each slice of the DICOM image
flat -- (optional; required if 'pixels' not provided) 1D array of values
k -- number of clusters (default: 3)
output -- (optional) default: None; location to save the CSV
Returns: Dataframe of metadata about the cluster index for each pixel
"""
if flat is None:
try:
flat = flatten(pixels)
except:
print('Error! If no flattened array is provided, you must supply a pixels 3D array to flatten')
if output:
try:
clustered = pd.read_csv(os.path.join(output,f'cluster_k{k}.csv'))
return clustered
except:
pass
clustered = cluster(flat, k=k, output=output)
return clustered
def cluster_modes(df):
"""Find the most common value in each cluster
Keyword arguments:
df -- the dataframe generated by cluster(), where (df.x is value) and (df.y is cluster_id)
Returns: Dataframe of metadata about the modes for each cluster
"""
clusters = list(set(df['y']))
modes = []
for k in clusters:
modes.append(df[df['y']==k]['x'].mode()[0])
mdf = pd.DataFrame(data={'cluster':clusters, 'mode':modes})
return mdf
def find_middle_cluster(df):
"""Select the cluster with the median mode
(use the higher median instead of averaging when the number of clusters is even)
Keyword arguments:
df -- the dataframe generated by cluster(), where (df.x is value) and (df.y is cluster_id)
Returns: Index of the cluster with the modal value that is the median out of all the cluster modes
"""
mdf = cluster_modes(df)
median = mdf['mode'].median()
k = int(mdf[mdf['mode']==median]['cluster'])
return k
def filter_by_cluster(df, cluster=None):
"""Filter the dataframe to only include a single cluster;
if cluster is not specified, use the middle cluster determined by find_middle_cluster()
Keyword arguments:
df -- the dataframe generated by cluster(), where (df.x is value) and (df.y is cluster_id)
cluster -- (optional) the specific cluster for which to filter
Returns: Filtered dataframe of pixel values and their cluster index
"""
if cluster is None:
cluster = find_middle_cluster(df)
filtered = df[df['y']==cluster]['x']
return filtered
def get_HUrange(df, cluster=None):
"""Extract the Hounsfield Unit (HU) range of the cluster;
if cluster is not specified, use the middle cluster determined by find_middle_cluster()
Keyword arguments:
df -- the dataframe generated by cluster(), where (df.x is value) and (df.y is cluster_id)
cluster -- (optional) the specific cluster for which to filter
Returns: Tuple of (minHU, maxHU)
"""
if cluster is None:
cluster = find_middle_cluster(df)
minHU = df[df['y']==cluster]['x'].min()
maxHU = df[df['y']==cluster]['x'].max()
return (minHU, maxHU)
def compare_scans(baseline, compare, viewer="plotly", output=False):
"""Show a slice through the middle of two brain scans (3D numpy arrays) side-by-side
and (optionally) save the comparison image
Keyword arguments:
original -- the first 3D numpy array to compare
mask -- the second 3D numpy array to compare
viewer -- 'plotly' for interactive; anything else returns a static matplotlib histogram
output -- (optional) default: None; (optionally, path and) filename *without extension* where to save the scan comparison image
Returns: Nothing; shows image
"""
midbrain = int(np.floor(len(baseline)/2))
print(f'Midbrain: {midbrain}')
fig = make_subplots(1, 2, subplot_titles=("Baseline",'Compare'))
fig.add_trace(go.Heatmap(z=baseline[midbrain],colorscale = 'gray'), 1, 1)
fig.add_trace(go.Heatmap(z=compare[midbrain],colorscale = 'gray'), 1, 2)
fig.update_layout(height=400, width=800)
plt.figure()
f, axarr = plt.subplots(1,2)
axarr[0].imshow(baseline[midbrain], cmap=plt.cm.binary)
axarr[1].imshow(compare[midbrain], cmap=plt.cm.binary)
if output:
try: os.makedirs(output)
except: pass
fig.write_html(os.path.join(output, 'compare_scans.html'))
plt.savefig(os.path.join(output, 'compare_scans.png'))
if viewer=="plotly":
fig.show()
plt.ioff()
plt.close()
else:
plt.show()
def mask(pixels, HUrange, output=False, debug=True):
"""Mask a 3D numpy array by a specified range by pushing pixels outside of the range
1000HU below the minimum of the range
Keyword arguments:
pixels -- 3D numpy array
HUrange -- a tuple of (min, max) HUrange
output -- (optional) default: None; location + filename where to save the masked data
Returns: 3D numpy array of masked pixel values
"""
mask = copy.deepcopy(pixels)
i=0
for img in mask:
j=0
for row in img:
mask[i][j] = [HUrange[0]-1000 if ((x < HUrange[0]) or (x > HUrange[1])) else x for x in row]
j+=1
i+=1
if output:
try: os.makedirs(output)
except: pass
with open(os.path.join(output, f'mask_{HUrange[0]}-{HUrange[1]}.pkl'), 'wb') as f:
pickle.dump(mask, f)
if debug:
show_scan(mask, output=output)
return mask
def mask_wrapper(pixels, output=None, HUrange=None, df=True, debug=True):
"""Wrapper for the mask() function which extracts an HUrange from the dataframe if there is no range specified
and optionally checks to see if the mask data has already been pre-computed and saved in a specified location
Keyword arguments:
pixels -- 3D numpy array of values from the DICOM image stack
output -- (optional) default: None; location + filename where to check for (or save) the masked data
HUrange -- (optional) a custom Hounsfield Units range for masking
df -- (optional, required if HUrange is 'None') dataframe from which to extract HUrange
Returns: 3D numpy array of masked pixel values
"""
if HUrange is None:
if df is not None:
HUrange = get_HUrange(df)
else:
print('Error! Must supply HUrange OR df')
try:
masked = pd.read_pickle(os.path.join(output, f'mask_{HUrange[0]}-{HUrange[1]}.pkl'))
except:
masked = mask(pixels, HUrange, output=output, debug=debug)
return masked
def binary_mask(pixels, maskRange, output=False, debug=True):
"""Generate a binary mask from a 3D numpy array according to a specified range
Keyword arguments:
pixels -- 3D numpy array
maskRange -- tuple of (min, max) Hounsfield unit range
Returns: 3D numpy array (binary) of masked pixel values
"""
binary = copy.deepcopy(pixels)
i=0
for img in pixels:
j=0
for row in img:
binary[i][j] = [1 if (maskRange[0] < x < maskRange[1]) else 0 for x in row]
j+=1
i+=1
if output:
try: os.makedirs(output)
except: pass
with open(os.path.join(output, f'binary-mask_{maskRange[0]}-{maskRange[1]}.pkl'), 'wb') as f:
pickle.dump(mask,f)
if debug: compare_scans(pixels, binary, viewer='plotly')
return binary
def remove_islands(pixels, output=False, k=3):
"""Generate a new 3D numpy array which removes islands using OpenCV's 'opening' function
Keyword arguments:
pixels -- 3D numpy array
k -- square kernel size in pixels for opening; default: 3, which returns a kernel of size 3x3
output -- (optional) where to save the pickle of the generated array
Returns: 3D numpy array (binary) of masked pixel values
"""
kernel = np.ones((k, k), np.float32)
opening = cv2.morphologyEx(pixels, cv2.MORPH_OPEN, kernel)
compare_scans(pixels, opening, viewer='plotly')
if output:
try: os.makedirs(output)
except: pass
with open(os.path.join(output, f'remove_islands_k{k}.pkl'), 'wb') as f:
pickle.dump(opening, f)
return opening