-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataset.py
71 lines (56 loc) · 2.39 KB
/
dataset.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
import pandas as pd
import numpy as np
import torch
import scanpy as sc
import scipy.sparse as sp
from torchvision import datasets, transforms
from torch.utils.data import Dataset
import os
from graph_construction import calcADJ
class MyDatasetTrans(Dataset):
"""Operations with the datasets."""
def __init__(self, normed_data, coor_df, image, transform=None):
"""
Args:
normed_data: Normalized data extracted from original AnnData object.
coor_df: Spatial location extracted from original AnnData object.
transform (callable, optional): Optional transform to be applied on a sample.
"""
self.data = normed_data.values.T
self.coor_df = coor_df.values
self.image = image
self.transform = transform
self.coord = np.array_split(self.coor_df, np.ceil(len(self.coor_df) / 50))
self.exp = np.array_split(self.data, np.ceil(len(self.data) / 50))
self.image_feature = np.array_split(self.image, np.ceil(len(self.image) / 50))
self.adj = [calcADJ(coord=i, k=4, pruneTag='NA') for i in self.coord]
def __len__(self):
return len(self.coord)
def __getitem__(self, idx):
exp = torch.tensor(self.exp[idx])
coord = torch.tensor(self.coord[idx])
image = torch.tensor(self.image_feature[idx])
adj = self.adj[idx]
sample = (exp, coord, image)
return sample
class MyDatasetTrans2(Dataset):
"""Operations with the datasets."""
def __init__(self, coor_df, image, transform=None):
"""
Args:
normed_data: Normalized data extracted from original AnnData object.
coor_df: Spatial location extracted from original AnnData object.
transform (callable, optional): Optional transform to be applied on a sample.
"""
self.coor_df = coor_df.values
self.image = image
self.transform = transform
self.coord = np.array_split(self.coor_df, np.ceil(len(self.coor_df) / 50))
self.image_feature = np.array_split(self.image, np.ceil(len(self.image) / 50))
def __len__(self):
return len(self.coord)
def __getitem__(self, idx):
coord = torch.tensor(self.coord[idx])
image = torch.tensor(self.image_feature[idx])
sample = (coord, image)
return sample