|
a |
|
b/Preprocessing Medical Data Pipeline/preparingTestInstance.py |
|
|
1 |
# from preprocessing import ReadIn_MRIScans_Masks |
|
|
2 |
# from writeout_dataset import Export2CompressedNifiti |
|
|
3 |
import numpy as np |
|
|
4 |
import os |
|
|
5 |
import pydicom |
|
|
6 |
import pandas as pd |
|
|
7 |
import nibabel as nib |
|
|
8 |
|
|
|
9 |
# cutoffSlice = 400 |
|
|
10 |
# seg_scan_dir = 'D:/MRI - Tairawhiti (User POV)' |
|
|
11 |
# folders = ['10_AutoBindWATER_450_13A'] |
|
|
12 |
# output_seg_scan_dir = ('{}/Patient Segmentation Tasks (Google Colab)').format(seg_scan_dir) |
|
|
13 |
|
|
|
14 |
def preprocessTestScans(cutoffSlice, seg_scan_dir, folders): |
|
|
15 |
output_seg_scan_dir = ('{}/Patient Segmentation Tasks (Google Colab)').format(seg_scan_dir) |
|
|
16 |
def read_dicom_files(directory): |
|
|
17 |
dicom_files = [] |
|
|
18 |
files = os.listdir(directory) |
|
|
19 |
sorted_files = sorted(files) |
|
|
20 |
for filename in sorted_files: |
|
|
21 |
filepath = os.path.join(directory, filename) |
|
|
22 |
if os.path.isfile(filepath) and filename.endswith('.dcm'): |
|
|
23 |
try: |
|
|
24 |
dicom_file = pydicom.dcmread(filepath) |
|
|
25 |
dicom_files.append(dicom_file) |
|
|
26 |
except pydicom.errors.InvalidDicomError: |
|
|
27 |
print(f"Skipping file: {filename}. It is not a valid DICOM file.") |
|
|
28 |
return dicom_files |
|
|
29 |
|
|
|
30 |
def flatten_2d_array(arr): |
|
|
31 |
flattened = [] |
|
|
32 |
for row in arr: |
|
|
33 |
flattened.extend(row) |
|
|
34 |
return flattened |
|
|
35 |
|
|
|
36 |
def ReadIn_MRIScans_Masks(scans_path, folders): |
|
|
37 |
print('Patient Scan Data: ', folders) |
|
|
38 |
scan_pixel_data = [] |
|
|
39 |
scan_coordinate_data = [] |
|
|
40 |
single_scan_pixel_data = [] |
|
|
41 |
scan_coordinate_data = [] |
|
|
42 |
scan_orientation_data = [] |
|
|
43 |
scan_pixelspacing_data = [] |
|
|
44 |
|
|
|
45 |
|
|
|
46 |
single_paitent_scans_path = scans_path + ('/Raw DICOM MRI Scans/{}').format(folders) |
|
|
47 |
dicom_files = read_dicom_files(single_paitent_scans_path) |
|
|
48 |
|
|
|
49 |
# Extracting pixel data |
|
|
50 |
for i in range (len(dicom_files)): |
|
|
51 |
single_scan_pixel_data.append(dicom_files[i].pixel_array) |
|
|
52 |
scan_pixel_data.append(single_scan_pixel_data) |
|
|
53 |
|
|
|
54 |
training_scans = flatten_2d_array(scan_pixel_data) |
|
|
55 |
training_scans = np.array(training_scans) |
|
|
56 |
|
|
|
57 |
# Coordinate Data |
|
|
58 |
single_paitent_scans_path = scans_path + '/{}'.format(folders) |
|
|
59 |
for i in range (len(dicom_files)): |
|
|
60 |
scan_coordinate_data.append(dicom_files[i].ImagePositionPatient) |
|
|
61 |
scan_orientation_data.append(dicom_files[i].ImageOrientationPatient) |
|
|
62 |
scan_pixelspacing_data.append(dicom_files[i].PixelSpacing) |
|
|
63 |
|
|
|
64 |
coord_data = pd.DataFrame(scan_coordinate_data, columns=["x", "y", "z"]) |
|
|
65 |
return training_scans |
|
|
66 |
|
|
|
67 |
def Export2CompressedNifiti(imgs_train, scans_path, colab_fname): |
|
|
68 |
nii_img_train = nib.Nifti1Image(imgs_train, affine=np.eye(4)) |
|
|
69 |
output_file_path = ('{}/msk_{}.nii.gz').format(scans_path, colab_fname) |
|
|
70 |
nib.save(nii_img_train, output_file_path) |
|
|
71 |
|
|
|
72 |
print("Conversion to NIfTI complete with shape: ", nii_img_train.shape) |
|
|
73 |
print(('Successfully Exported Preprocessed Data! -> {}').format(output_file_path)) |
|
|
74 |
print('\n') |
|
|
75 |
|
|
|
76 |
|
|
|
77 |
for patient in folders: |
|
|
78 |
patient_scan_data = ReadIn_MRIScans_Masks(seg_scan_dir, patient) |
|
|
79 |
|
|
|
80 |
patient_scan_data = patient_scan_data[(int(cutoffSlice)):-1] |
|
|
81 |
|
|
|
82 |
patient_scan_data = np.array(patient_scan_data) |
|
|
83 |
patient_scan_data = patient_scan_data.astype('float32') |
|
|
84 |
patient_scan_data /= 255. # scale scans to [0, 1] |
|
|
85 |
|
|
|
86 |
patient_scan_data = np.expand_dims(patient_scan_data, axis=-1) |
|
|
87 |
print('Prediction Input Scan Shape: ', patient_scan_data.shape) |
|
|
88 |
|
|
|
89 |
|
|
|
90 |
scan_idx_fnamecolab = (patient.split('_'))[0] |
|
|
91 |
scan_idx_fnamecolab = '{:03d}'.format(int(scan_idx_fnamecolab)) |
|
|
92 |
Export2CompressedNifiti(patient_scan_data, output_seg_scan_dir, scan_idx_fnamecolab) |