/
data.py
251 lines (195 loc) · 7.41 KB
/
data.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
from torch.utils.data import Dataset
import numpy as np
import pandas as pd
import scanpy as sc
import os
from PIL import Image
from os.path import join as pjoin
from utils import load_idx
import torch
import matplotlib.pyplot as plt
def load_aml():
DATA_DIR = "./data/bmmc/"
pretransplant1 = pd.read_csv(
pjoin(DATA_DIR, "clean", "pretransplant1.csv"), index_col=0
)
pretransplant2 = pd.read_csv(
pjoin(DATA_DIR, "clean", "pretransplant2.csv"), index_col=0
)
posttransplant1 = pd.read_csv(
pjoin(DATA_DIR, "clean", "posttransplant1.csv"), index_col=0
)
posttransplant2 = pd.read_csv(
pjoin(DATA_DIR, "clean", "posttransplant2.csv"), index_col=0
)
healthy1 = pd.read_csv(pjoin(DATA_DIR, "clean", "healthy1.csv"), index_col=0)
healthy2 = pd.read_csv(pjoin(DATA_DIR, "clean", "healthy2.csv"), index_col=0)
data1 = np.concatenate([healthy1.values, healthy2.values]).astype('float32')
data2 = np.concatenate([pretransplant1.values, pretransplant2.values]).astype('float32')
data3 = np.concatenate([posttransplant1.values, posttransplant2.values]).astype('float32')
return (data1, np.zeros(data1.shape[0])), (data2, np.ones(data2.shape[0])), (data3, np.ones(data3.shape[0]) * 2)
def load_epithel():
data1 = pd.read_csv("./data/epithel/data/Control.csv", index_col=0).T.values
labels1 = np.zeros(data1.shape[0])
data2 = pd.read_csv("./data/epithel/data/Salmonella.csv", index_col=0).T.values
labels2 = np.ones(data2.shape[0])
data3 = pd.read_csv("./data/epithel/data/Hpoly.Day10.csv", index_col=0).T.values
labels3 = np.ones(data3.shape[0]) * 2 # Array of 2's
return (data1.astype('float32'), labels1), (data2.astype('float32'), labels2), (data3.astype('float32'), labels3)
class SimpleDataset(Dataset):
def __init__(self, X: np.ndarray, y: np.ndarray):
self.X = X.copy()
self.y = y.copy()
def __len__(self):
return self.X.shape[0]
def __getitem__(self, index):
return self.X[index], self.y[index]
class CelebADataset(Dataset):
def __init__(self, image_files, labels=None, transform=None):
"""
Args:
root_dir (string): Directory with all the images
transform (callable, optional): transform to be applied to each image sample
"""
# Read names of images in the root directory
self.transform = transform
self.image_names = image_files
self.labels = labels
def __len__(self):
return len(self.image_names)
def __getitem__(self, idx):
#print('idx = ', idx)
# Get the path to the image
img_path = os.path.join(self.image_names[idx])
# Load image and convert it to RGB
image = Image.open(img_path).convert('RGB')
face_width = face_height = 108
j = (image.size[0] - face_width) // 2
i = (image.size[1] - face_height) // 2
image = image.crop([j, i, j + face_width, i + face_height])
image = image.resize([64, 64], Image.BILINEAR)
# Apply transformations to the image
#image.save(str(idx) + "testPIL.jpg")
image = np.array(image.convert('RGB')) / 255
#plt.imsave(str(idx)+ 'test.png',image)
if self.labels is not None:
return image.reshape(3, 64, 64).astype('float32'), self.labels[idx]
else:
return image.reshape(3, 64, 64).astype('float32')
class CelebADataset2(Dataset):
def __init__(self, image_files, labels=None, transform=None):
"""
Args:
root_dir (string): Directory with all the images
transform (callable, optional): transform to be applied to each image sample
"""
# Read names of images in the root directory
self.transform = transform
self.image_names = image_files
self.labels = labels
def __len__(self):
return len(self.image_names)
def __getitem__(self, idx):
#print('idx = ', idx)
# Get the path to the image
img_path = os.path.join(self.image_names[idx])
# Load image and convert it to RGB
image = Image.open(img_path).convert('RGB')
face_width = face_height = 108
j = (image.size[0] - face_width) // 2
i = (image.size[1] - face_height) // 2
image = image.crop([j, i, j + face_width, i + face_height])
image = image.resize([64, 64], Image.BILINEAR)
# Apply transformations to the image
#image.save(str(idx) + "testPIL.jpg")
image = np.array(image.convert('RGB')) / 255
image = image.astype('float32')
#plt.imsave(str(idx)+ 'test.png',image)
if self.transform is not None:
image = self.transform(image)
else:
image = image.reshape(3, 64, 64)
if self.labels is not None:
return image , self.labels[idx]
else:
return image
class GridMnistDspriteDataset(Dataset):
def __init__(self, images, labels=None, transform=None, in_channels=1):
"""
Args:
root_dir (string): Directory with all the images
transform (callable, optional): transform to be applied to each image sample
"""
# Read names of images in the root directory
self.transform = transform
self.images = images
self.labels = labels
self.in_channels = in_channels
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
image = self.images[idx]
if self.transform is not None:
image = self.transform(image)
else:
image = image.reshape(self.in_channels, 64, 64)
if self.labels is not None:
return image , self.labels[idx]
else:
return image
class BratsDataset(Dataset):
def __init__(self, images, labels=None, transform=None, in_channels=1):
"""
Args:
root_dir (string): Directory with all the images
transform (callable, optional): transform to be applied to each image sample
"""
# Read names of images in the root directory
self.transform = transform
self.images = images
self.labels = labels
self.in_channels = in_channels
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
#image = torch.from_numpy(np.transpose(np.load(self.images[idx]))).type(torch.FloatTensor).unsqueeze(0)
image = np.transpose(np.load(self.images[idx])).astype('float32')
# print(self.images[idx])
# print(image.shape)
if self.transform is not None:
image = self.transform(image)
else:
image = image.reshape(self.in_channels, 64, 64)
if self.labels is not None:
return image , self.labels[idx]
else:
return image
class CifarMnistDataset(Dataset):
def __init__(self, images, labels=None, transform=None, in_channels=1, label_mnist=None, label_cifar=None):
"""
Args:
root_dir (string): Directory with all the images
transform (callable, optional): transform to be applied to each image sample
"""
# Read names of images in the root directory
self.transform = transform
self.images = images
self.labels = labels
self.in_channels = in_channels
self.label_mnist = label_mnist
self.label_cifar = label_cifar
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
image = self.images[idx]
if self.transform is not None:
image = self.transform(image)
else:
image = image.reshape(self.in_channels, 64, 64)
if self.labels is not None:
if self.label_mnist is not None and self.label_cifar is not None :
return image, self.labels[idx], self.label_mnist[idx], self.label_cifar[idx]
else:
return image , self.labels[idx]
else:
return image