-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpreprocessing.py
417 lines (342 loc) · 14.3 KB
/
preprocessing.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
import pandas as pd
import numpy as np
from pathlib import Path
import pydicom
import os
import nrrd
# Images should be downloaded from Duke-Breast-Cancer-MRI Dataset on TCIA
# https://wiki.cancerimagingarchive.net/pages/viewpage.action?pageId=70226903
# Download should be done using descriptive file path. There is an associated
# file path mapping table that can be downloaded to determine which series is
# the precontrast sequence.
def clean_filepath_filename_mapping_csv(filepath_filename_csv_path):
"""
This function cleans the "Breast-Cancer-MRI-filepath_filename-mapping.csv"
file that can be downloaded from TCIA. It is originally an excel file. It
can be saved as a csv for use in this function. This returns a DataFrame
that pairs subject_ids to their precontrast dir
Parameters
----------
fpath_mapping_df: str
Path that leads to Breast-Cancer-MRI-filepath_filename-mapping.csv
Returns
-------
pd.DataFrame
Cleaned mapping DataFrame that can be used to find precontrast MRI
sequences
"""
# Read in csv as DataFrame
fpath_mapping_df = pd.read_csv(
filepath_filename_csv_path
)
# We only need the filename and the descriptive path
fpath_mapping_df = fpath_mapping_df.loc[
:, ['original_path_and_filename', 'descriptive_path']
]
# We only need the precontrast sequences
fpath_mapping_df = fpath_mapping_df.loc[
fpath_mapping_df['original_path_and_filename'].str.contains('pre')
]
# Only need the subject ID in first column
fpath_mapping_df['original_path_and_filename'] = \
fpath_mapping_df['original_path_and_filename'].str.split('/').str[1]
# For second column, only need the name of the dir containing the sequence
# Need to remove some extra slashes that are in there erroneously
fpath_mapping_df['descriptive_path'] = \
fpath_mapping_df['descriptive_path'].str.replace('//', '/')
fpath_mapping_df['descriptive_path'] = \
fpath_mapping_df['descriptive_path'].str.split('/').str[-2]
# Drop duplicates so each subject has on entry
fpath_mapping_df = fpath_mapping_df.drop_duplicates(
subset='original_path_and_filename'
)
# Rename columns for better clarity
fpath_mapping_df = fpath_mapping_df.rename(
columns={
'original_path_and_filename': 'subject_id',
'descriptive_path': 'precontrast_dir'
}
)
return fpath_mapping_df
def read_precontrast_mri(
subject_id,
tcia_data_dir,
fpath_mapping_df
):
"""
Reads in the precontrast MRI data given a subject ID.
This function also aligns the patient orientation so the patient's body
is in the lower part of image. The slices from the beginning move inferior
to superior.
Parameters
----------
subject_id: str
Subject_id (e.g. Breast_MRI_001)
tcia_data_dir: str
Path of downloaded database from TCIA
fpath_mapping_df: pd.DataFrame
Cleaned mapping DataFrame that can be used to find precontrast MRI
sequences
Returns
-------
np.Array
Raw MRI volume data read from all .dcm files
pydicom.dataset.FileDataset
Dicom data from final slice read. This is used for obtaining things
such as pixel spacing, image orientation, etc.
"""
tcia_data_dir = Path(tcia_data_dir)
# Get the sequence dir from the DataFrame
sequence_dir = fpath_mapping_df.loc[
fpath_mapping_df['subject_id'] == subject_id, 'precontrast_dir'
].iloc[0]
# There's also a subdir for every subject that contains the sequences
# There is only one of these
sub_dir = os.listdir(tcia_data_dir / subject_id)[0]
full_sequence_dir = tcia_data_dir / subject_id / sub_dir / sequence_dir
# Now we can iterate through the files in the sequence dir and reach each
# of them into a numpy array
dicom_file_list = sorted(os.listdir(full_sequence_dir))
dicom_data_list = []
# Saving the values of first two image positions
# This is used to orient inferior to superior
first_image_position = 0
second_image_position = 0
for i in range(len(dicom_file_list)):
dicom_data = pydicom.dcmread(full_sequence_dir / dicom_file_list[i])
if i == 0:
first_image_position = dicom_data[0x20, 0x32].value[-1]
elif i == 1:
second_image_position = dicom_data[0x20, 0x32].value[-1]
dicom_data_list.append(dicom_data.pixel_array)
# Stack in numpy array
image_array = np.stack(dicom_data_list, axis=-1)
# Rotate if inferior and superior are flipped
if first_image_position > second_image_position:
image_array = np.rot90(image_array, 2, (1, 2))
# For patients in a certain orentation, also need to flip in another axis
# This is the same in all dicom files so we can just use the last
# dicom file that we have from the iteration. It also needs to be rounded.
if round(dicom_data[0x20, 0x37].value[0], 0) == -1:
image_array = np.rot90(image_array, 2)
return image_array, dicom_data
def read_precontrast_mri_and_segmentation(
subject_id,
tcia_data_dir,
fpath_mapping_df,
segmentation_dir
):
"""
Reads in the precontrast MRI data given a subject ID.
This function also aligns the patient orientation so the patient's body
is in the lower part of image. The slices from the beginning move inferior
to superior.
Finally, because the information used to properly orient the MRI is used
in this function, it also properly orients the segmentation data.
Parameters
----------
subject_id: str
Subject_id (e.g. Breast_MRI_001)
tcia_data_dir: str
Path of downloaded database from TCIA
fpath_mapping_df: pd.DataFrame
Cleaned mapping DataFrame that can be used to find precontrast MRI
sequences
segmentation_dir: str
Directory containing segmentations in the following format
├── [subject_id_1] (e.g. Breast_MRI_001)
├── Segmentation_[subject_id_1]_Breast.seg.nrrd
└── Segmentation_[subject_id_1]_Dense_and_Vessels.seg.nrrd
Returns
-------
np.Array
Raw MRI volume data read from all .dcm files
pydicom.dataset.FileDataset
Dicom data from final slice read. This is used for obtaining things
such as pixel spacing, image orientation, etc.
np.Array
Segmentation data from .nrrd.seg file after proper orientation
"""
tcia_data_dir = Path(tcia_data_dir)
segmentation_dir = Path(segmentation_dir)
# Get the sequence dir from the DataFrame
sequence_dir = fpath_mapping_df.loc[
fpath_mapping_df['subject_id'] == subject_id, 'precontrast_dir'
].iloc[0]
# There's also a subdir for every subject that contains the sequences
# There is only one of these
sub_dir = os.listdir(tcia_data_dir / subject_id)[0]
full_sequence_dir = tcia_data_dir / subject_id / sub_dir / sequence_dir
# Now we can iterate through the files in the sequence dir and reach each
# of them into a numpy array
dicom_file_list = sorted(os.listdir(full_sequence_dir))
dicom_data_list = []
# Saving the values of first two image positions
# This is used to orient inferior to superior
first_image_position = 0
second_image_position = 0
for i in range(len(dicom_file_list)):
dicom_data = pydicom.dcmread(full_sequence_dir / dicom_file_list[i])
if i == 0:
first_image_position = dicom_data[0x20, 0x32].value[-1]
elif i == 1:
second_image_position = dicom_data[0x20, 0x32].value[-1]
dicom_data_list.append(dicom_data.pixel_array)
# Stack in numpy array
image_array = np.stack(dicom_data_list, axis=-1)
# Read in breast_nrrd data
nrrd_breast_data, _ = nrrd.read(
segmentation_dir / '{}/Segmentation_{}_Breast.seg.nrrd'.format(
subject_id, subject_id)
)
# Read in dense_and_vessels (abbreviated dv) data
# Header is used to properly write classes
nrrd_dv_data, nrrd_header = nrrd.read(
segmentation_dir / \
'{}/Segmentation_{}_Dense_and_Vessels.seg.nrrd'.format(
subject_id, subject_id)
)
# For dense and vessels one more step needs to be taken.
# Since the order of the dense and vessels may be incorrect,
# they could be labeled with the wrong classes. We will always have
# background = 0, vessels = 1, dense = 2.
# There are few situations where things can be mislabeled.
# One is where they simply have the wrong values.
# Another is when the volume has two layers.
if len(nrrd_dv_data.shape) == 3:
# First check to see if everything is correct
if (
nrrd_header['Segment0_Name'] == 'Vessels' and
nrrd_header['Segment1_Name'] == 'Dense' and
nrrd_header['Segment0_LabelValue'] == '1' and
nrrd_header['Segment1_LabelValue'] == '2'
):
pass
else:
if nrrd_header['Segment0_Name'] == 'Vessels':
original_vessels_label = \
int(nrrd_header['Segment0_LabelValue'])
original_dense_label = \
int(nrrd_header['Segment1_LabelValue'])
else:
original_vessels_label = \
int(nrrd_header['Segment1_LabelValue'])
original_dense_label = \
int(nrrd_header['Segment0_LabelValue'])
# Start by changing the vessel label to a high number, then
# fix all values. This is to prevent an incorrect np.where
# statement.
nrrd_dv_data = np.where(
nrrd_dv_data == original_vessels_label,
50, nrrd_dv_data
)
nrrd_dv_data = np.where(
nrrd_dv_data == original_dense_label,
2, nrrd_dv_data
)
nrrd_dv_data = np.where(
nrrd_dv_data == 50,
1, nrrd_dv_data
)
else:
# There are two layers in the data.
if nrrd_header['Segment0_Name'] == 'Vessels':
vessel_layer = int(nrrd_header['Segment0_Layer'])
vessel_label = int(nrrd_header['Segment0_LabelValue'])
dense_layer = int(nrrd_header['Segment1_Layer'])
dense_label = int(nrrd_header['Segment1_LabelValue'])
else:
vessel_layer = int(nrrd_header['Segment1_Layer'])
vessel_label = int(nrrd_header['Segment1_LabelValue'])
dense_layer = int(nrrd_header['Segment0_Layer'])
dense_label = int(nrrd_header['Segment0_LabelValue'])
vessel_array = nrrd_dv_data[vessel_layer, :, :, :]
dense_array = nrrd_dv_data[dense_layer, :, :, :]
# Change the values of them to match what we want
vessel_array = np.where(
vessel_array == vessel_label, 1, vessel_array
)
dense_array = np.where(
dense_array == dense_label, 2, dense_array
)
nrrd_dv_data = vessel_array + dense_array
# Might be some overlap; we'll change that into dense
nrrd_dv_data = np.where(
nrrd_dv_data == 3, 2, nrrd_dv_data
)
assert (np.unique(nrrd_dv_data) == np.array([0, 1, 2])).all(), \
"{} dense/vessel array has incorrect values".format(subject_id)
assert (image_array.shape == nrrd_breast_data.shape) and \
(image_array.shape == nrrd_dv_data.shape), \
""""Subject {}: Shape mismatch between arrays.
Image, breast, dv shapes: {}, {}, {}
""".format(
subject_id,
image_array.shape,
nrrd_breast_data.shape,
nrrd_dv_data.shape
)
# All nrrd data needs to be rotated/flipped
# Some require additional flipping/rotation using criteria below
nrrd_breast_data = np.flip(np.rot90(nrrd_breast_data), axis=1)
nrrd_dv_data = np.flip(np.rot90(nrrd_dv_data), axis=1)
# Rotate if inferior and superior are flipped
if first_image_position > second_image_position:
image_array = np.rot90(image_array, 2, (1, 2))
nrrd_breast_data = np.flip(nrrd_breast_data, axis=1)
nrrd_dv_data = np.flip(nrrd_dv_data, axis=1)
# For patients in a certain orentation, also need to flip in another axis
# This is the same in all dicom files so we can just use the last
# dicom file that we have from the iteration. It also needs to be rounded.
if round(dicom_data[0x20, 0x37].value[0], 0) == -1:
image_array = np.rot90(image_array, 2)
else:
nrrd_breast_data = np.rot90(nrrd_breast_data, 2)
nrrd_dv_data = np.rot90(nrrd_dv_data, 2)
return image_array, dicom_data, nrrd_breast_data, nrrd_dv_data
def normalize_image(image_array, min_cutoff = 0.001, max_cutoff = 0.001):
"""
Normalize the intensity of an image array by cutting off min and max values
to a certain percentile and set all values above/below that percentile to
the new max/min.
Parameters
----------
image_array: np.array
3D numpy array constructed from dicom files
min_cutoff: float
Minimum percentile of image to keep. (0.1% = 0.001)
max_cutoff: float
Maximum percentile of image to keep. (0.1% = 0.001)
Returns
-------
np.array
Normalized image
"""
# Sort image values
sorted_array = np.sort(image_array.flatten())
# Find %ile index and get values
min_index = int(len(sorted_array) * min_cutoff)
min_intensity = sorted_array[min_index]
max_index = int(len(sorted_array) * min_cutoff) * -1
max_intensity = sorted_array[max_index]
# Normalize image and cutoff values
image_array = (image_array - min_intensity) / \
(max_intensity - min_intensity)
image_array[image_array < 0.0] = 0.0
image_array[image_array > 1.0] = 1.0
return image_array
def zscore_image(image_array):
"""
Convert intensity values in an image to zscores:
zscore = (intensity_value - mean) / standard_deviation
Parameters
----------
image_array: np.array
3D numpy array constructed from dicom files
Returns
-------
np.array
Image with zscores for values
"""
image_array = (image_array - np.mean(image_array)) / np.std(image_array)
return image_array