File size: 5,231 Bytes
54660f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import torch
import torch.utils.data as data
import numpy as np
from PIL import Image, ImageFile
import random
from torchvision.transforms import ToTensor
from torchvision import transforms
import cv2
import pickle
import torch.nn.functional as F

ImageFile.LOAD_TRUNCATED_IMAGES = True

def collate_features(batch):
    img = torch.cat([item[0] for item in batch], dim = 0)
    coords = np.vstack([item[1] for item in batch])
    return [img, coords]
    
def eval_transforms(pretrained=False):
    if pretrained:
        mean = (0.485, 0.456, 0.406)
        std = (0.229, 0.224, 0.225)

    else:
        mean = (0.5,0.5,0.5)
        std = (0.5,0.5,0.5)

    trnsfrms_val = transforms.Compose(
                    [
                    transforms.Resize(224),
                    transforms.ToTensor(),
                    transforms.Normalize(mean = mean, std = std)
                    ]
                )

    return trnsfrms_val

class GraphDataset(data.Dataset):
    """input and label image dataset"""

    def __init__(self, root, ids, metadata_path, target_patch_size=-1):
        super(GraphDataset, self).__init__()
        """
        Args:

        fileDir(string):  directory with all the input images.
        transform(callable, optional): Optional transform to be applied on a sample
        """
        self.root = root
        self.ids = ids
        #self.target_patch_size = target_patch_size
        self.classdict =   pickle.load(open(os.path.join(metadata_path, 'label_map.pkl'), 'rb' )) #  {'normal': 0, 'luad': 1, 'lscc': 2}        #
        #self.classdict = {'normal': 0, 'tumor': 1}        #
        #self.classdict = {'Normal': 0, 'TCGA-LUAD': 1, 'TCGA-LUSC': 2}
        self._up_kwargs = {'mode': 'bilinear'}

    def __getitem__(self, index):
        sample = {}
        info = self.ids[index].replace('\n', '')
        #file_name, label = info.split('\t')[0].rsplit('.', 1)[0], info.split('\t')[1]
        file_name, label = info.split('\t')[0], info.split('\t')[1]


        sample['label'] = self.classdict[label]
        sample['id'] = file_name


        file_path = os.path.join(self.root, 'simclr_files')
        #feature_path = os.path.join(self.root, file_name, 'features.pt')
        feature_path = os.path.join(file_path, file_name, 'features.pt')

        if os.path.exists(feature_path):
            features = torch.load(feature_path, map_location=lambda storage, loc: storage)
        else:
            print(feature_path + ' not exists')
            features = torch.zeros(1, 512)

        #adj_s_path = os.path.join(self.root, file_name, 'adj_s.pt')
        adj_s_path = os.path.join(file_path, file_name, 'adj_s.pt')
        if os.path.exists(adj_s_path):
            adj_s = torch.load(adj_s_path, map_location=lambda storage, loc: storage)
        else:
            print(adj_s_path + ' not exists')
            adj_s = torch.ones(features.shape[0], features.shape[0])

        #features = features.unsqueeze(0)
        sample['image'] = features
        sample['adj_s'] = adj_s     #adj_s.to(torch.double)
        # return {'image': image.astype(np.float32), 'label': label.astype(np.int64)}

        return sample        


    def __len__(self):
        return len(self.ids)


'''    def __getitem__(self, index):
        sample = {}
        info = self.ids[index].replace('\n', '')
        file_name, label = info.split('\t')[0].rsplit('.', 1)[0], info.split('\t')[1]
        site, file_name = file_name.split('/')

        # if site =='CCRCC':
        #     file_path = self.root + 'CPTAC_CCRCC_features/simclr_files'
        if site =='LUAD' or site =='LSCC':
            site = 'LUNG'
        file_path = self.root + 'CPTAC_{}_features/simclr_files'.format(site)       #_pre# with # rushin

        # For NLST only
        if site =='NLST':
            file_path = self.root + 'NLST_Lung_features/simclr_files'

        # For TCGA only
        if site =='TCGA':
            file_name = info.split('\t')[0]
            _, file_name = file_name.split('/')
            file_path = self.root + 'TCGA_LUNG_features/simclr_files'       #_resnet_with

        sample['label'] = self.classdict[label]
        sample['id'] = file_name

        #feature_path = os.path.join(self.root, file_name, 'features.pt')
        feature_path = os.path.join(file_path, file_name, 'features.pt')

        if os.path.exists(feature_path):
            features = torch.load(feature_path, map_location=lambda storage, loc: storage)
        else:
            print(feature_path + ' not exists')
            features = torch.zeros(1, 512)

        #adj_s_path = os.path.join(self.root, file_name, 'adj_s.pt')
        adj_s_path = os.path.join(file_path, file_name, 'adj_s.pt')
        if os.path.exists(adj_s_path):
            adj_s = torch.load(adj_s_path, map_location=lambda storage, loc: storage)
        else:
            print(adj_s_path + ' not exists')
            adj_s = torch.ones(features.shape[0], features.shape[0])

        #features = features.unsqueeze(0)
        sample['image'] = features
        sample['adj_s'] = adj_s     #adj_s.to(torch.double)
        # return {'image': image.astype(np.float32), 'label': label.astype(np.int64)}

        return sample
'''