-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExtractFeatures.py
442 lines (380 loc) · 17.1 KB
/
ExtractFeatures.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
"""
@ Date: 15 November 2022
@ DateMod: 02 December 2022
@ author: AMAL JOSEPH VARGHESE
@ email: amaljova@gmail.com
@ github: https://github.com/amaljova
# To extract radiomics features from a folder of DICOM images.
# Edit the last part of the script to inclue
- the path to tartget directory
- the ROI of interest
- the loaction where you want your results to be saved.
This Script contains snippets from https://github.com/zhenweishi/O-RAW (The Class DicomDatabase)
and https://github.com/zhenweishi/Py-rex
"""
import radiomics
from radiomics import featureextractor
import pydicom
import os
import numpy as np
from skimage import draw
import SimpleITK as sitk
import re
import glob
import pandas as pd
# -------------------------------------------------------------------------------------------
# DICOM DATABASE CLASS
# from https://github.com/zhenweishi/O-RAW
class DicomDatabase:
def __init__(self):
self.patient = dict()
#============================ MODIFIED ! ===================================
# Modified by Amal (https://github.com/amaljova)
"""
Use this when using a csv file with all needed paths
"""
def parseFiles(self, files):
for file_path in files:
if(file_path.endswith(".dcm") or file_path.endswith(".DCM")):
dcmHeader = pydicom.dcmread(file_path)
patientId = dcmHeader[0x10,0x20].value
patient = self.getOrCreatePatient(patientId)
patient.addFile(file_path, dcmHeader)
#==================================================================================
def parseFolder(self, folderPath):
for root, subdirs, files in os.walk(folderPath):
for filename in files:
file_path = os.path.join(root, filename)
if(file_path.endswith(".dcm") or file_path.endswith(".DCM")):
dcmHeader = pydicom.dcmread(file_path)
patientId = dcmHeader[0x10,0x20].value
patient = self.getOrCreatePatient(patientId)
patient.addFile(file_path, dcmHeader)
print("done")
#==================================================================================
def getOrCreatePatient(self, patientId):
if not (patientId in self.patient):
self.patient[patientId] = Patient()
return self.patient[patientId]
def countPatients(self):
return len(self.patient)
def getPatient(self, patientId):
return self.patient[patientId]
def getPatientIds(self):
return self.patient.keys()
def doesPatientExist(self, patientId):
# return self.patient.has_key(patientId)
return patientId in self.patient
class Patient:
def __init__(self):
self.ct = dict()
self.rtstruct = dict()
def addFile(self, filePath, dcmHeader):
modality = dcmHeader[0x8,0x60].value
sopInstanceUid = dcmHeader[0x8,0x18].value
seriesInstanceUid = dcmHeader[0x20,0xe].value
if(modality == "CT") or (modality == "PT") or (modality == "MR"):
if not (seriesInstanceUid in self.ct):
self.ct[seriesInstanceUid] = CT()
myCT = self.ct[seriesInstanceUid]
myCT.addCtSlice(filePath)
if(modality == "RTSTRUCT"):
struct = RTStruct(filePath)
self.rtstruct[sopInstanceUid] = struct
def countCTScans(self):
return len(self.ct)
def countRTStructs(self):
return len(self.rtstruct)
def getCTScan(self, seriesInstanceUid):
if seriesInstanceUid is not None:
if self.doesCTExist(seriesInstanceUid):
return self.ct[seriesInstanceUid]
return None
def getRTStruct(self, sopInstanceUid):
return self.rtstruct[sopInstanceUid]
def getCTScans(self):
return self.ct.keys()
def getRTStructs(self):
return self.rtstruct.keys()
def doesCTExist(self, seriesInstanceUid):
# return self.ct.has_key(seriesInstanceUid)
return seriesInstanceUid in self.ct
def doesRTStructExist(self, sopInstanceUid):
# return self.rtstruct.has_key(sopInstanceUid)
return sopInstanceUid in self.rtstruct
def getCTForRTStruct(self, rtStruct):
if rtStruct.getReferencedCTUID() is not None:
return self.getCTScan(rtStruct.getReferencedCTUID())
else:
return None
class CT:
def __init__(self):
self.filePath = list()
def addCtSlice(self, filePath):
self.filePath.append(filePath)
def getSlices(self):
return self.filePath
def getSliceCount(self):
return len(self.filePath)
def getSliceHeader(self, index):
return pydicom.dcmread(self.filePath[index])
class RTStruct:
def __init__(self, filePath):
self.filePath = filePath
def getHeader(self):
return pydicom.dcmread(self.filePath)
def getReferencedCTUID(self):
dcmHeader = self.getHeader()
if len(list(dcmHeader[0x3006,0x10])) > 0:
refFrameOfRef = (dcmHeader[0x3006,0x10])[0]
if len(list(refFrameOfRef[0x3006, 0x0012])) > 0:
rtRefStudy = (refFrameOfRef[0x3006, 0x0012])[0]
if len(list(rtRefStudy[0x3006,0x14])) > 0:
rtRefSerie = (rtRefStudy[0x3006,0x14])[0]
return rtRefSerie[0x20,0xe].value
return None
def getFileLocation(self):
return self.filePath
# ----------------------------------------------------------------------------------------------------------------
# To get the image and mask array of ROI of interest, from dicom files.
# MODIFIED PYREX from GITHUB
# from https://github.com/zhenweishi/Py-rex
# module PyrexReader:
def match_ROIid(rtstruct_path,ROI_name): # Match ROI id in RTSTURCT to a given ROI name in the parameter file
mask_vol = Read_RTSTRUCT(rtstruct_path)
M= mask_vol[0]
for i in range(len(M.StructureSetROISequence)):
if str(ROI_name)==M.StructureSetROISequence[i].ROIName:
ROI_number = M.StructureSetROISequence[i].ROINumber
break
for ROI_id in range(len(M.StructureSetROISequence)):
if ROI_number == M.ROIContourSequence[ROI_id].ReferencedROINumber:
break
return ROI_id
def Read_scan(myCT): # Read scans under the specified path
scan = [myCT.getSliceHeader(i) for i in range(myCT.getSliceCount())]
try:
scan.sort(key = lambda x: int(x.ImagePositionPatient[2])) # sort slices based on Z coordinate
except:
print('AttributeError: Cannot read scans')
return scan
def Read_RTSTRUCT(myStruct): # Read RTSTRUCT under the specified path
try:
rt = [myStruct.getHeader()]
except:
print('AttributeError: Cannot read RTSTRUCT')
return rt
def poly2mask(vertex_row_coords, vertex_col_coords, shape): # Mask interpolation
fill_row_coords, fill_col_coords = draw.polygon(vertex_row_coords, vertex_col_coords, shape)
mask = np.zeros(shape, dtype=bool)
mask[fill_row_coords, fill_col_coords] = True
return mask
def get_pixels_hu(scans): # convert to Hounsfield Unit (HU) by multiplying rescale slope and adding intercept
image = np.stack([s.pixel_array for s in scans])
image = image.astype(np.int16) #convert to int16
# the code below checks if the image has slope and intercept
# since MRI images often do not provide these
try:
intercept = scans[0].RescaleIntercept
slope = scans[0].RescaleSlope
except AttributeError:
pass
else:
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 Img_Bimask(img_path,rtstruct_path,ROI_name): # generating image array and binary mask
print('Generating binary mask based on ROI: %s ......' % ROI_name)
img_vol = Read_scan(img_path)
mask_vol=Read_RTSTRUCT(rtstruct_path)
IM=img_vol[0] # Slices usually have the same basic information including slice size, patient position, etc.
IM_P=get_pixels_hu(img_vol)
M=mask_vol[0]
num_slice=len(img_vol)
mask=np.zeros([num_slice, IM.Rows, IM.Columns],dtype=np.uint8)
xres=np.array(IM.PixelSpacing[0])
yres=np.array(IM.PixelSpacing[1])
slice_thickness=np.abs(img_vol[1].ImagePositionPatient[2]-img_vol[0].ImagePositionPatient[2])
ROI_id = match_ROIid(rtstruct_path,ROI_name)
#Check DICOM file Modality
# Modified by Amal (https://github.com/amaljova)
if IM.Modality == 'CT' or IM.Modality == 'PT':
for k in range(len(M.ROIContourSequence[ROI_id].ContourSequence)):
Cpostion_rt = M.ROIContourSequence[ROI_id].ContourSequence[k].ContourData[2]
for i in range(num_slice):
if np.int64(Cpostion_rt) == np.int64(img_vol[i].ImagePositionPatient[2]): # match the binary mask and the corresponding slice
sliceOK = i
break
x=[]
y=[]
z=[]
m=M.ROIContourSequence[ROI_id].ContourSequence[k].ContourData
for i in range(0,len(m),3):
x.append(m[i+1])
y.append(m[i+0])
z.append(m[i+2])
x=np.array(x)
y=np.array(y)
z=np.array(z)
x-= IM.ImagePositionPatient[1]
y-= IM.ImagePositionPatient[0]
z-= IM.ImagePositionPatient[2]
pts = np.zeros([len(x),3])
pts[:,0] = x
pts[:,1] = y
pts[:,2] = z
a=0
b=1
p1 = xres
p2 = yres
m=np.zeros([2,2])
m[0,0]=img_vol[sliceOK].ImageOrientationPatient[a]*p1
m[0,1]=img_vol[sliceOK].ImageOrientationPatient[a+3]*p2
m[1,0]=img_vol[sliceOK].ImageOrientationPatient[b]*p1
m[1,1]=img_vol[sliceOK].ImageOrientationPatient[b+3]*p2
# Transform points from reference frame to image coordinates
m_inv=np.linalg.inv(m)
pts = (np.matmul((m_inv),(pts[:,[a,b]]).T)).T
mask[sliceOK,:,:] = np.logical_or(mask[sliceOK,:,:],poly2mask(pts[:,0],pts[:,1],[IM_P.shape[1],IM_P.shape[2]]))
elif IM.Modality == 'MR':
slice_0 = img_vol[0]
slice_n = img_vol[-1]
# the screen coordinates, including the slice number can then be computed
# using the inverse of this matrix
transform_matrix = np.r_[slice_0.ImageOrientationPatient[3:], 0, slice_0.ImageOrientationPatient[:3], 0, 0, 0, 0, 0, 1, 1, 1, 1].reshape(4, 4).T # yeah that's ugly but I didn't have enough time to make anything nicer
T_0 = np.array(slice_0.ImagePositionPatient)
T_n = np.array(slice_n.ImagePositionPatient)
col_2 = (T_0 - T_n) / (1 - len(img_vol))
pix_s = slice_0.PixelSpacing
transform_matrix[:, -1] = np.r_[T_0, 1]
transform_matrix[:, 2] = np.r_[col_2, 0]
transform_matrix[:, 0] *= pix_s[1]
transform_matrix[:, 1] *= pix_s[0]
transform_matrix = np.linalg.inv(transform_matrix)
for s in M.ROIContourSequence[ROI_id].ContourSequence:
Cpostion_rt = np.r_[s.ContourData[:3], 1]
roi_slice_nb = int(transform_matrix.dot(Cpostion_rt)[2])
for i in range(num_slice):
print(roi_slice_nb, i)
if roi_slice_nb == i:
sliceOK = i
break
x=[]
y=[]
z=[]
m=s.ContourData
for i in range(0,len(m),3):
x.append(m[i+1])
y.append(m[i+0])
z.append(m[i+2])
x=np.array(x)
y=np.array(y)
z=np.array(z)
x-= IM.ImagePositionPatient[1]
y-= IM.ImagePositionPatient[0]
z-= IM.ImagePositionPatient[2]
pts = np.zeros([len(x),3])
pts[:,0] = x
pts[:,1] = y
pts[:,2] = z
a=0
b=1
p1 = xres
p2 = yres
m=np.zeros([2,2])
m[0,0]=img_vol[sliceOK].ImageOrientationPatient[a]*p1
m[0,1]=img_vol[sliceOK].ImageOrientationPatient[a+3]*p2
m[1,0]=img_vol[sliceOK].ImageOrientationPatient[b]*p1
m[1,1]=img_vol[sliceOK].ImageOrientationPatient[b+3]*p2
# Transform points from reference frame to image coordinates
m_inv=np.linalg.inv(m)
pts = (np.matmul((m_inv),(pts[:,[a,b]]).T)).T
mask[sliceOK,:,:] = np.logical_or(mask[sliceOK,:,:],poly2mask(pts[:,0],pts[:,1],[IM_P.shape[1],IM_P.shape[2]]))
# The pixel intensity values are normalized to range [0 255] using linear translation
IM_P=IM_P.astype(np.float32)
# IM_P = (IM_P-np.min(IM_P))*255/(np.max(IM_P)-np.min(IM_P))
Img=sitk.GetImageFromArray(IM_P) # convert image_array to image
Mask=sitk.GetImageFromArray(mask)
# try:
# origin = IM.GetOrigin()
# except:
# origin = (0.0, 0.0, 0.0)
# Set voxel spacing [[pixel spacing_x, pixel spacing_y, slice thickness]
#slice_thickness = IM.SliceThickness
Img.SetSpacing([np.float64(xres),np.float64(yres),np.float64(slice_thickness)])
Mask.SetSpacing([np.float64(xres),np.float64(yres),np.float64(slice_thickness)])
# sitk.WriteImage(Img,'./data/test_image.nrrd',True)
# sitk.WriteImage(Mask,'./data/test_label.nrrd',True)
return Img, Mask
# ---------------------------------------------------------------------------------------
# Extracting Radiomic Features
def extractFeatures(dicomDb, roi, parameter, out_name):
"""
This Fuction extracts radiomic features automatically.
"""
# create an empty dataframe to collect results
result = pd.DataFrame()
# Get Patients
for ptid in dicomDb.getPatientIds():
print("staring with Patient %s" % (ptid))
myPatient = dicomDb.getPatient(ptid)
# get RTSTRUCTs
for myStructUID in myPatient.getRTStructs():
print("Starting with RTStruct %s" % myStructUID)
myStruct = myPatient.getRTStruct(myStructUID)
# Get the related CT
myCT = myPatient.getCTForRTStruct(myStruct)
if myCT == None:
continue
# Collect all ROI names
roi_list= [i[(0x3006, 0x26)].value for i in myStruct.getHeader()[(0x3006, 0x20)].value]
# Take each ROI name
for roi_name in roi_list:
# Check if it is GTV or not.
if re.search(roi,roi_name):
# Convert DICOM images into Raw data and Mask
Img, Mask = Img_Bimask(myCT,myStruct,roi_name)
# Initialize feature extractor object using parameter file
extractor = featureextractor.RadiomicsFeatureExtractor(parameter)
# Extract and collect features, add patient ID, structure set UID and Contour name to it.
featureVector = pd.Series({"patient": ptid, "structUID": myStructUID, "contour": roi_name})
featureVector = featureVector.append(pd.Series(extractor.execute(Img, Mask)))
# Collect the results in a dataframe
result = result.append(featureVector.to_frame().T)
# simply set these variables to empty list
Img, Mask = [],[]
# Save Results in CSV file.
result.to_csv(out_name,index=False)
print(f"Created {out_name}")
def makeFolders(dir_name):
'''To create a directory/directories if it/they are basent'''
if not os.path.exists(dir_name):
os.makedirs(dir_name)
def execute(data_dir, roi, parameter_list,outDir,data_name):
# create dicom databese from the given folder
dicomDb = DicomDatabase()
print("Instantiated Dicomdatabase & Started parsing folders...")
# walk over all files in folder, and index in the database
dicomDb.parseFolder(data_dir)
print("Parsing is done!")
for param in parameter_list:
print(f"Using {param}")
out_name = os.path.join(outDir, f'{data_name}_{((param.split("/")[-1]).split(".")[0])}_Out.csv')
extractFeatures(dicomDb, roi, param, out_name)
# ---------------------------------------------------------------------------------------
# Edit here
if __name__ == "__main__":
# EDIT HERE
data_dir = "path_to_dataset"
# ROI of interest (regular expresion)
roi = '[Gg][Tt][Vv]'
# Edit list to include more parameter files (if interested.)
parameter_list = [
"Params/Pyradiomics_Params.yaml"
]
outDir = "results_folder" # where you want to save csv files with extracted features
data_name = "dataset_name" # name of the dataset, just to name output file
makeFolders(outDir)
execute(data_dir, roi, parameter_list, outDir, data_name)