-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathread_data.py
498 lines (419 loc) · 18.3 KB
/
read_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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import os
import random
import pickle
import numpy as np
from torch.utils.data import Dataset
from torchvision.io import read_image
import pandas as pd
from PIL import Image
import torch
from tqdm import tqdm
from sklearn.preprocessing import StandardScaler, MinMaxScaler
import lmdb
import lz4framed
import cv2
from types_ import *
class InvalidFileException(Exception):
pass
class PatchBagRNADataset(Dataset):
def __init__(self, patch_data_path: str, rna_csv_path: str, img_size:int ,
transforms=None, max_patch_per_wsi=400, bag_size=20,
quick=None, labels=False):
self._patch_data_path = patch_data_path
self._rna_csv_path = rna_csv_path
self._img_size = img_size
self.bag_size = bag_size
self._transforms = transforms
self._max_patch_per_wsi = max_patch_per_wsi
self._quick = quick
self._labels = labels
self.data = {}
self.index = []
self._preprocess()
def _preprocess(self):
self.data, self.index = get_data_rna_bag_wsi(self._rna_csv_path,
self._patch_data_path, self._max_patch_per_wsi, self.bag_size,
self._quick, self._labels)
def __len__(self):
return len(self.index)
def __getitem__(self, idx):
(WSI, i) = self.index[idx]
imgs = []
item = self.data[WSI].copy()
for patch in item['images'][i:i + self.bag_size]:
with open(patch, 'rb') as f:
img = Image.open(f).convert('RGB')
if self._transforms is not None:
img = self._transforms(img)
imgs.append(img)
img = torch.stack(imgs,dim=0)
item['patch_bag'] = img
return item
def get_data_rna_bag_wsi(csv_path, patch_path:str , limit: int, bag_size: int, quick=None, labels=False):
dataset = {}
index = []
if type(csv_path) == str:
data = pd.read_csv(csv_path)
else:
data = csv_path
if quick is not None:
data = data.loc[data['wsi_file_name'].isin(quick)]
for _, row in tqdm(data.iterrows()):
WSI = row['wsi_file_name']
rna_data = row[[x for x in row.keys() if 'rna_' in x]].values.astype(np.float32)
rna_data = torch.tensor(rna_data, dtype=torch.float32)
label = row['Labels']
label = torch.tensor(label, dtype=torch.int64)
new_row = dict()
new_row['WSI'] = WSI
new_row['rna_data'] = rna_data
new_row['label'] = label
n_patches = sum(1 for _ in open(os.path.join(patch_path, WSI, 'loc.txt'))) - 2
images = [os.path.join(patch_path, WSI, WSI + "_patch_{}.jpeg".format(i)) for i in
range(n_patches)]
if limit is not None:
images = images[:limit]
new_row['images'] = images
new_row['n_images'] = len(images)
dataset[WSI] = {}
dataset[WSI] = {w.lower(): new_row[w] for w in new_row.keys()}
for k in range(len(images) // bag_size):
index.append((WSI, bag_size * k))
return dataset, index
class PatchBagDataset(Dataset):
def __init__(self, patch_data_path, csv_path, img_size, transforms=None, bag_size=40,
max_patches_total=300, quick=False):
self.patch_data_path = patch_data_path
self.csv_path = csv_path
self.img_size = img_size
self.transforms = transforms
self.bag_size = bag_size
self.max_patches_total = max_patches_total
self.quick = quick
self.data = {}
self.index = []
self._preprocess()
def _preprocess(self):
if type(self.csv_path) == str:
csv_file = pd.read_csv(self.csv_path)
else:
csv_file = self.csv_path
if self.quick:
csv_file = csv_file.sample(150)
for i, row in tqdm(csv_file.iterrows()):
row = row.to_dict()
WSI = row['wsi_file_name']
n_patches = sum(1 for _ in open(os.path.join(self.patch_data_path, WSI, 'loc.txt'))) - 2
n_patches = min(n_patches, self.max_patches_total)
images = [os.path.join(self.patch_data_path, WSI, WSI + '_patch_{}.jpeg'.format(i)) for i in range(n_patches)]
self.data[WSI] = {w.lower(): row[w] for w in row.keys()}
self.data[WSI].update({'WSI': WSI, 'images': images, 'n_images': len(images)})
for k in range(len(images) // self.bag_size):
self.index.append((WSI, self.bag_size * k))
def shuffle(self):
for k in self.data.keys():
wsi_row = self.data[k]
np.random.shuffle(wsi_row['images'])
def __len__(self):
return len(self.index)
def __getitem__(self, idx):
filenames = []
(WSI, i) = self.index[idx]
filenames.append(WSI)
imgs = []
row = self.data[WSI]
for patch in row['images'][i:i + self.bag_size]:
#with open(patch, 'rb') as f:
#img = Image.open(f).convert('RGB')
img = read_image(patch)
imgs.append(img)
img = torch.stack(imgs, dim=0)
return img, filenames
class PatchDataset(Dataset):
def __init__(self, patch_data_path, csv_path, img_size, transforms=None,
max_patches_total=300, quick=False, le=None):
self.patch_data_path = patch_data_path
self.csv_path = csv_path
self.img_size = img_size
self.transforms = transforms
self.max_patches_total = max_patches_total
self.quick = quick
self.keys = []
self.images = []
self.filenames = []
self.labels = []
self.lmdbs_path = []
self.le = le
self._preprocess()
def _preprocess(self):
if type(self.csv_path) == str:
csv_file = pd.read_csv(self.csv_path)
csv_file['patch_data_path'] = [self.patch_data_path] * csv_file.shape[0]
csv_file['labels'] = [0] * csv_file.shape[0]
else:
csv_file = self.csv_path
if self.quick:
csv_file = csv_file.sample(10)
for i, row in tqdm(csv_file.iterrows()):
row = row.to_dict()
WSI = row['wsi_file_name']
data_path = row['patch_data_path']
label = np.asarray(row['labels'])
if self.le is not None:
label = self.le.transform(label.reshape(-1,1))
label = torch.tensor(label, dtype=torch.float32)
#label = label.flatten()
try:
path = os.path.join(data_path, WSI, WSI.replace('.svs', '.db'))
lmdb_connection = lmdb.open(path,
subdir=False, readonly=True,
lock=False, readahead=False, meminit=False)
with lmdb_connection.begin(write=False) as lmdb_txn:
n_patches = lmdb_txn.stat()['entries'] - 1
keys = pickle.loads(lz4framed.decompress(lmdb_txn.get(b'__keys__')))
#n_patches = sum(1 for _ in open(os.path.join(data_path, WSI, 'loc.txt'))) - 2
n_selected = min(n_patches, self.max_patches_total)
n_patches= list(range(n_patches))
n_patches_index = random.sample(n_patches, n_selected)
'''
n_patches_index = []
for idx in n_patches_index_aux:
lmdb_value = lmdb_txn.get(keys[idx])
try:
img_name, img_arr, img_shape = pickle.loads(lz4framed.decompress(lmdb_value))
except:
continue
n_patches_index.append(idx)
'''
except:
print('Error with db {}'.format(os.path.join(data_path, WSI, WSI.replace('.svs', '.db'))))
continue
#self.keys.append(keys)
#self.random_index.append(n_patches_index)
for i in n_patches_index:
#self.images.append(os.path.join(data_path, WSI, WSI + '_patch_{}.png'.format(i)))
self.images.append(i)
self.filenames.append(WSI)
self.labels.append(label)
self.lmdbs_path.append(path)
self.keys.append(keys[i])
def decompress_and_deserialize(self, lmdb_value: Any):
try:
img_name, img_arr, img_shape = pickle.loads(lz4framed.decompress(lmdb_value))
except Exception as e:
print(e)
return None
image = np.frombuffer(img_arr, dtype=np.uint8).reshape(img_shape)
image = np.copy(image)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return torch.from_numpy(image).permute(2,0,1)
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
lmdb_connection = lmdb.open(self.lmdbs_path[idx],
subdir=False, readonly=True,
lock=False, readahead=False, meminit=False)
with lmdb_connection.begin(write=False) as txn:
lmdb_value = txn.get(self.keys[idx])
image = self.decompress_and_deserialize(lmdb_value)
if image == None:
print(self.lmdbs_path[idx])
#raise InvalidFileException("Invalid file found, skipping")
return None
#return image, self.labels[idx]
return self.transforms(image), self.labels[idx]
#return read_image(self.images[idx]), self.labels[idx]
class PatchRNADataset(Dataset):
def __init__(self, patch_data_path, csv_path, img_size, transforms=None,
max_patches_total=300, quick=False, le=None):
self.patch_data_path = patch_data_path
self.csv_path = csv_path
self.img_size = img_size
self.transforms = transforms
self.max_patches_total = max_patches_total
self.quick = quick
self.keys = []
self.images = []
self.filenames = []
self.labels = []
self.lmdbs_path = []
self.rna_data_arrays = []
self.le = le
self._preprocess()
def _preprocess(self):
if type(self.csv_path) == str:
csv_file = pd.read_csv(self.csv_path)
csv_file['patch_data_path'] = [self.patch_data_path] * csv_file.shape[0]
csv_file['labels'] = [0] * csv_file.shape[0]
else:
csv_file = self.csv_path
if self.quick:
csv_file = csv_file.sample(150)
for i, row in tqdm(csv_file.iterrows()):
WSI = row['wsi_file_name']
rna_data = row[[x for x in row.keys() if 'rna_' in x]].values.astype(np.float32)
rna_data = torch.tensor(rna_data, dtype=torch.float32)
data_path = row['patch_data_path']
label = np.asarray(row['labels'])
if self.le is not None:
label = self.le.transform(label.reshape(-1,1))
label = torch.tensor(label, dtype=torch.float32)
#label = label.flatten()
try:
path = os.path.join(data_path, WSI, WSI.replace('.svs', '.db'))
if path == '../../Histology/BrainCortex_Patches256x256/GTEX-1E2YA-3025.svs/GTEX-1E2YA-3025.db': continue
lmdb_connection = lmdb.open(path,
subdir=False, readonly=True,
lock=False, readahead=False, meminit=False)
with lmdb_connection.begin(write=False) as lmdb_txn:
n_patches = lmdb_txn.stat()['entries'] - 1
keys = pickle.loads(lz4framed.decompress(lmdb_txn.get(b'__keys__')))
#n_patches = sum(1 for _ in open(os.path.join(data_path, WSI, 'loc.txt'))) - 2
n_selected = min(n_patches, self.max_patches_total)
n_patches= list(range(n_patches))
n_patches_index = random.sample(n_patches, n_selected)
except:
print('Error with db {}'.format(os.path.join(data_path, WSI, WSI.replace('.svs', '.db'))))
continue
#self.keys.append(keys)
#self.random_index.append(n_patches_index)
for i in n_patches_index:
#self.images.append(os.path.join(data_path, WSI, WSI + '_patch_{}.png'.format(i)))
self.images.append(i)
self.filenames.append(WSI)
self.labels.append(label)
self.lmdbs_path.append(path)
self.keys.append(keys[i])
self.rna_data_arrays.append(rna_data)
def decompress_and_deserialize(self, lmdb_value: Any):
try:
img_name, img_arr, img_shape = pickle.loads(lz4framed.decompress(lmdb_value))
except:
return None
image = np.frombuffer(img_arr, dtype=np.uint8).reshape(img_shape)
image = np.copy(image)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return torch.from_numpy(image).permute(2,0,1)
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
lmdb_connection = lmdb.open(self.lmdbs_path[idx],
subdir=False, readonly=True,
lock=False, readahead=False, meminit=False)
with lmdb_connection.begin(write=False) as txn:
lmdb_value = txn.get(self.keys[idx])
image = self.decompress_and_deserialize(lmdb_value)
rna_data = self.rna_data_arrays[idx]
if image == None:
print(self.lmdbs_path[idx])
#raise InvalidFileException("Invalid file found, skipping")
out = {
'image': image,
'rna_data': rna_data,
'labels': self.labels[idx]
}
else:
out = {
'image': self.transforms(image),
'rna_data': rna_data,
'labels': self.labels[idx]
}
return out
#return read_image(self.images[idx]), self.labels[idx]
class RNADataset(Dataset):
def __init__(self, csv_path, quick=False):
self._csv_path = csv_path
self.data = None
self.quick = quick
self._preprocess()
def _preprocess(self):
self.data = get_data_rna(self._csv_path, self.quick)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.data[idx]
def get_data_rna(csv_paths, quick=False):
dataset = []
for csv_path in csv_paths:
if type(csv_path) == str:
print('Working with dataset {}'.format(csv_path))
data = pd.read_csv(csv_path)
else:
data = csv_path
if quick:
data = data.sample(10)
for _, row in tqdm(data.iterrows()):
rna_data = row[[x for x in row.keys() if 'rna_' in x]].values.astype(np.float32)
rna_data = torch.tensor(rna_data, dtype=torch.float32)
item = {'rna_data': rna_data}
dataset.append(item)
return dataset
'''
class PatchRNADataset(Dataset):
def __init__(self, patch_data_path, rna_csv_path, img_size, transforms=None,
max_patch_per_wsi=400):
self._patch_data_path = patch_data_path
self._rna_csv_path = rna_csv_path
self._img_size = img_size
self._transforms = transforms
self._max_patch_per_wsi = max_patch_per_wsi
self.data = None
self._preprocess()
def _preprocess(self):
self.data = get_data_rna_wsi(self._rna_csv_path, self._patch_data_path,
max_patches=self._max_patch_per_wsi)
def __len__(self): return len(self.data)
def __getitem__(self, idx):
item = self.data[idx].copy()
patch = item['patch']
img = Image.open(patch).convert('RGB')
if self._transforms is not None:
img = self.transforms(img)
item['img'] = img
return item
# TODO: function to permute the data, in order to not have
# the same image, and create labels
def get_data_rna_wsi(csv_path, patch_path, max_patches=None):
dataset = []
data = pd.read_csv(csv_path)
for _, row in tqdm(data.iterrows()):
wsi = row['wsi_file_name']
rna_data = row[[x for x in row.keys() if 'rna_' in x]].values.astype(np.float32)
rna_data = torch.tensor(rna_data, dtype=torch.float32)
new_row = dict()
new_row['patch_folder'] = wsi
new_row['rna_data'] = rna_data
n_patches = sum(1 for _ in open(os.path.join(patch_path, wsi, 'loc.txt'))) - 2
images = [os.path.join(patch_path, wsi, wsi + '_patch_{}.jpeg'.format(i)) for i in range(n_patches)]
if max_patches is not None:
images = images[:max_patches]
for i in images:
item = new_row.copy()
item['patch'] = os.path.join(patch_path, patch_path, i)
dataset.append(item)
return dataset
'''
def normalize_dfs(train_df, val_df, test_df, labels=False, norm_type='standard'):
def _get_log(x):
# trick to take into account zeros
x = np.log(x.replace(0, np.nan))
return x.replace(np.nan, 0)
# get list of columns to scale
rna_columns = [x for x in train_df.columns if 'rna_' in x]
# log transform
train_df[rna_columns] = train_df[rna_columns].apply(_get_log)
val_df[rna_columns] = val_df[rna_columns].apply(_get_log)
test_df[rna_columns] = test_df[rna_columns].apply(_get_log)
train_df = train_df[rna_columns+['wsi_file_name']]
val_df = val_df[rna_columns+['wsi_file_name']]
test_df = test_df[rna_columns+['wsi_file_name']]
rna_values = train_df[rna_columns].values
if norm_type == 'standard':
scaler = StandardScaler()
elif norm_type == 'minmax':
scaler = MinMaxScaler(feature_range=(0,1))
rna_values = scaler.fit_transform(rna_values)
train_df[rna_columns] = rna_values
test_df[rna_columns] = scaler.transform(test_df[rna_columns].values)
val_df[rna_columns] = scaler.transform(val_df[rna_columns].values)
return train_df, val_df, test_df, scaler