Switch to unified view

a b/Preprocessing Medical Data Pipeline/writeout_dataset.py
1
# Exporting Training Scan and Mask Data
2
import os 
3
import numpy as np
4
from PIL import Image
5
import matplotlib.pyplot as plt
6
import re
7
import numpy as np
8
import nibabel as nib
9
import matplotlib.pyplot as plt
10
from tqdm import tqdm
11
12
def WriteOutTextFile(data_array, save_dir, starting_slice):
13
    num_files = data_array.shape[0]
14
    
15
    # Create the save directory if it doesn't exist
16
    if not os.path.exists(save_dir):
17
        os.makedirs(save_dir)
18
    
19
    for i in range(num_files):
20
        file_name = f"data_{i + starting_slice}.txt"
21
        file_path = save_dir + '/' + file_name
22
        np.savetxt(file_path, data_array[i], delimiter=',', fmt='%.4f')
23
        print(f"Saved {file_path}")
24
25
26
def WriteOutImagePNGFiles(data_array, save_dir, starting_slice):
27
    num_files = data_array.shape[0]
28
29
    # Create the save directory if it doesn't exist
30
    if not os.path.exists(save_dir):
31
        os.makedirs(save_dir)
32
33
    for i in range(num_files):
34
        file_name = f"image_{i + starting_slice}.png"
35
        file_path = save_dir + '/' + file_name
36
            
37
        # Scale the pixel values to the range [0, 255] if necessary
38
        image_data = ((data_array[i] * 255).astype(np.uint8)).squeeze()
39
        # Create an Image object and save as PNG
40
        image = Image.fromarray(image_data)
41
42
        if True:
43
            image.save(file_path)
44
                
45
        print(f"Saved {file_path}")
46
47
48
def ReadInDatasets(folder_path):
49
    files = os.listdir(folder_path)
50
    num_files = len(files)
51
    result_array = np.empty((num_files, 512, 512))
52
53
    for i, file in enumerate(files):
54
        file_path = os.path.join(folder_path, file)
55
        with open(file_path, 'r') as f:
56
            # Read the contents of the text file
57
            content = f.read()
58
59
            # Convert the content into a 2D NumPy array of floats
60
            array_2d = np.array([list(map(float, line.split(','))) for line in content.splitlines()])
61
62
            # Store the 2D array in the result array
63
            result_array[i] = array_2d
64
65
    return result_array
66
67
def ReadInDatasetNPY(folder_path):
68
    # Get a list of all files in the folder
69
    file_list = os.listdir(folder_path)
70
71
    # Filter out only the .npy files
72
    npy_files = [file for file in file_list if file.endswith('.npy')]
73
74
    # Sort the .npy files based on the numeric order in their filenames
75
    npy_files.sort(key=lambda x: int(x.split('_')[0]))
76
77
    # Initialize an empty list to store arrays from each .npy file
78
    arrays_list = []
79
80
    # Read in each .npy file and append it to the list
81
    for file_name in npy_files:
82
        file_path = os.path.join(folder_path, file_name)
83
        array = np.load(file_path)
84
        arrays_list.append(array)
85
86
    # Concatenate the arrays into a single array
87
    concatenated_array = np.concatenate(arrays_list, axis=0)
88
89
    return concatenated_array
90
91
def Export2CompressedNifiti(imgs_train, scans_path, colab_fname, imgs_mask_train, orientation):
92
    nii_img_train = nib.Nifti1Image(imgs_train, affine=np.eye(4))
93
    scan_idx_fnamecolab = int((colab_fname[0].split('_'))[1])
94
    scan_idx_fnamecolab = '{:03d}'.format(scan_idx_fnamecolab)
95
    output_file_path = ('{}/nnUNet Data/unprocessed_scans/msk_{}.nii.gz').format(scans_path, scan_idx_fnamecolab)
96
    nib.save(nii_img_train, output_file_path)
97
98
    nii_img_mask = nib.Nifti1Image(imgs_mask_train, affine=np.eye(4))
99
    output_file_path = ('{}/nnUNet Data/masks/{}/{}_{}.nii.gz').format(scans_path, ((colab_fname[0]).split('_'))[0],colab_fname[0], orientation)
100
    nib.save(nii_img_mask, output_file_path)   
101
102
    print("Conversion to NIfTI complete with shape: ", imgs_mask_train.shape)
103
    print('\n')
104
    print('Successfully Exported Preprocessed Data!')