File size: 5,762 Bytes
911c613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ------------------------------------------------------------ #
#
# file : utils/io/read.py
# author : CM
# Function to read dataset
#
# ------------------------------------------------------------ #

import os
import sys

import nibabel as nib
import numpy as np

# read nii file and load it into a numpy 3d array
def niiToNp(filename):
    data = nib.load(filename).get_data().astype('float16')
    return data/data.max()

# read a dataset and load it into a numpy 4d array
def readDataset(folder, size, size_x, size_y, size_z):
    dataset = np.empty((size, size_x, size_y, size_z), dtype='float16')
    i = 0
    files = os.listdir(folder)
    files.sort()
    for filename in files:
        if(i>=size):
            break
        print(filename)
        dataset[i, :, :, :] = niiToNp(os.path.join(folder, filename))
        i = i+1

    return dataset

# return dataset affine
def getAffine_subdir(folder):
    subdir = os.listdir(folder)
    subdir.sort()
    files = os.listdir(folder+subdir[0])
    path = folder + subdir[0]
    image = nib.load(os.path.join(path, files[0]))
    return image.affine

def getAffine(folder):
    files = os.listdir(folder)
    files.sort()
    image = nib.load(os.path.join(folder, files[0]))
    return image.affine



# reshape the dataset to match keras input shape (add channel dimension)
def reshapeDataset(d):
    return d.reshape(d.shape[0], d.shape[1], d.shape[2], d.shape[3], 1)

# read a dataset and load it into a numpy 3d array as raw data (no normalisation)
def readRawDataset(folder, size, size_x, size_y, size_z, dtype):
    files = os.listdir(folder)
    files.sort()

    if(len(files) < size):
        sys.exit(2)

    count = 0
    # astype depend on your dataset type.
    dataset = np.empty((size, size_x, size_y, size_z)).astype(dtype)

    for filename in files:
        if(count>=size):
            break
        dataset[count, :, :, :] = nib.load(os.path.join(folder, filename)).get_data()
        count += 1
        print(count, '/', size, os.path.join(folder, filename))

    return dataset

def readTrainValid(config):
    print("Loading training dataset")

    train_gd_dataset = readRawDataset(config["dataset_train_gd_path"],
                                      config["dataset_train_size"],
                                      config["image_size_x"],
                                      config["image_size_y"],
                                      config["image_size_z"],
                                      'uint16')

    print("Training ground truth dataset shape", train_gd_dataset.shape)
    print("Training ground truth dataset dtype", train_gd_dataset.dtype)

    train_in_dataset = readRawDataset(config["dataset_train_mra_path"],
                                      config["dataset_train_size"],
                                      config["image_size_x"],
                                      config["image_size_y"],
                                      config["image_size_z"],
                                      'uint16')

    print("Training input image dataset shape", train_in_dataset.shape)
    print("Training input image dataset dtype", train_in_dataset.dtype)

    print("Loading validation dataset")

    valid_gd_dataset = readRawDataset(config["dataset_valid_gd_path"],
                                      config["dataset_valid_size"],
                                      config["image_size_x"],
                                      config["image_size_y"],
                                      config["image_size_z"],
                                      'uint16')

    print("Validation ground truth dataset shape", valid_gd_dataset.shape)
    print("Validation ground truth dataset dtype", valid_gd_dataset.dtype)

    valid_in_dataset = readRawDataset(config["dataset_valid_mra_path"],
                                      config["dataset_valid_size"],
                                      config["image_size_x"],
                                      config["image_size_y"],
                                      config["image_size_z"],
                                      'uint16')

    print("Validation input image dataset shape", valid_in_dataset.shape)
    print("Validation input image dataset dtype", valid_in_dataset.dtype)
    return train_gd_dataset, train_in_dataset, valid_gd_dataset, valid_in_dataset

# read a dataset and load it into a numpy 3d without any preprocessing
def getDataset(folder, size, type=None):
    files = os.listdir(folder)
    files.sort()

    if(len(files) < size):
        sys.exit(0x2001)

    image = nib.load(os.path.join(folder, files[0]))

    if type==None:
        dtype = image.get_data_dtype()
    else:
        dtype = type

    dataset = np.empty((size, image.shape[0], image.shape[1], image.shape[2])).astype(dtype)
    del image

    count = 0
    for filename in files:
        dataset[count, :, :, :] = nib.load(os.path.join(folder, filename)).get_data()
        count += 1
        if(count>=size):
            break

    return dataset

# read a dataset and load it into a numpy 3d without any preprocessing with "start" index and number of files
def readDatasetPart(folder, start, size, type=None):
    files = os.listdir(folder)
    files.sort()

    if(len(files) < start + size):
        sys.exit("readDatasetPart : len(files) < start + size")

    image = nib.load(os.path.join(folder, files[0]))

    if type==None:
        dtype = image.get_data_dtype()
    else:
        dtype = type

    dataset = np.empty(((size), image.shape[0], image.shape[1], image.shape[2])).astype(dtype)
    del image

    count = 0
    for i in range(start, start + size):
        dataset[count, :, :, :] = nib.load(os.path.join(folder, files[i])).get_data()
        count += 1

    return dataset