Repository navigation
Expand file tree
/
Copy pathread_data.py
More file actions
60 lines (48 loc) · 2.07 KB
/
Copy pathread_data.py
File metadata and controls
60 lines (48 loc) · 2.07 KB
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
import os
import h5py
import torch
import pandas as pd
from torch.utils.data import Dataset
import numpy as np
import pdb
class SuperTileRNADataset(Dataset):
def __init__(self, df, feature_path):
self.df = df
self.feature_path = feature_path
# Infer number of genes (columns that start with 'rna_')
self.rna_cols = [col for col in df.columns if col.startswith('rna_')]
self.num_genes = len(self.rna_cols)
# Infer feature dimension from the first sample
first_file = df.iloc[0]['wsi_file_name']
self.feature_dim = self._infer_feature_dim(first_file)
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
try:
row = self.df.iloc[idx]
wsi_file = row['wsi_file_name']
wsi_base = os.path.splitext(os.path.basename(wsi_file))[0]
feature_file = os.path.join(self.feature_path, wsi_base + '.h5')
if not os.path.exists(feature_file):
print(f"Feature file not found: {feature_file}")
return None
with h5py.File(feature_file, 'r') as f:
if 'cluster_features' not in f:
print(f"No cluster_features found in: {feature_file}")
return None
feats = f['cluster_features'][:] # Shape (K=500, 2048)
feats = torch.tensor(feats, dtype=torch.float32)
rna = torch.tensor(row[self.rna_cols].values.astype(np.float32))
return feats, rna, row['wsi_file_name'], row['tcga_project']
except Exception as e:
print(f"Error loading data for index {idx}: {e}")
return None
def _infer_feature_dim(self, wsi_file):
try:
wsi_base = os.path.splitext(os.path.basename(wsi_file))[0]
path = os.path.join(self.feature_path, wsi_base + '.h5')
with h5py.File(path, 'r') as f:
return f['cluster_features'].shape[1]
except Exception as e:
print(f"Feature dim inference failed: {e}")
return 2048 # fallback