-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasets.py
More file actions
459 lines (366 loc) · 18.2 KB
/
datasets.py
File metadata and controls
459 lines (366 loc) · 18.2 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
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
from torch.utils.data import Dataset
import torch
import numpy as np
from einops import rearrange
import scipy.sparse as sp
from sklearn.decomposition import PCA
from utils import plot_ST, read_anndata
class ST2D(Dataset):
def __init__(self, data_file, require_coordnorm=True, keep_ratio=True, **kwargs):
super().__init__()
adata = read_anndata(data_file)
self.coordinates = adata.obsm["spatial"].astype(float)
if sp.issparse(adata.X):
self.raw_representations = adata.X.toarray()
else:
self.raw_representations = adata.X
# take statistics
self.n_cell = self.raw_representations.shape[0]
self.n_gene = self.raw_representations.shape[1]
self.n_channels = self.n_gene
if "embeddings" in adata.obsm:
self.embeddings = adata.obsm["embeddings"]
assert self.raw_representations.shape[0] == self.embeddings.shape[0]
self.n_embd = self.embeddings.shape[1]
else:
self.embeddings = None
if require_coordnorm:
self._normalize_coordinates(keep_ratio=keep_ratio)
self.raw_pca = PCA(n_components=3, random_state=0) # map raw representation dimension to 3 for visualization
self.raw_pca.fit(self.raw_representations)
if self.has_embeddings():
self.embd_pca = PCA(n_components=3, random_state=0) # map embedding dimension to 3 for visualization
self.embd_pca.fit(self.embeddings)
def has_embeddings(self):
return False if self.embeddings is None else True
def plot_raw_representations(self, spot_size=2, train_indices=None, val_indices=None):
if train_indices and val_indices:
train_fig = plot_ST(self.coordinates[train_indices,:], self.raw_pca.transform(self.raw_representations[train_indices,:]), spot_size)
val_fig = plot_ST(self.coordinates[val_indices,:], self.raw_pca.transform(self.raw_representations[val_indices,:]), spot_size)
return train_fig, val_fig
else:
fig = plot_ST(self.coordinates, self.raw_pca.transform(self.raw_representations), spot_size)
return fig
def plot_embeddings(self, spot_size=2, train_indices=None, val_indices=None):
assert self.has_embeddings(), "The current adata file has NO embeddings!"
if train_indices and val_indices:
train_fig = plot_ST(self.coordinates[train_indices,:], self.embd_pca.transform(self.embeddings[train_indices,:]), spot_size)
val_fig = plot_ST(self.coordinates[val_indices,:], self.embd_pca.transform(self.embeddings[val_indices,:]), spot_size)
return train_fig, val_fig
else:
fig = plot_ST(self.coordinates, self.embd_pca.transform(self.embeddings), spot_size)
return fig
# normalize coordinates to [-1.0, +1.0]
def _normalize_coordinates(self, keep_ratio):
x_min, y_min = list(self.coordinates.min(axis=0))
x_max, y_max = list(self.coordinates.max(axis=0))
x_range, y_range = x_max - x_min, y_max - y_min
self.coordinates[:,0] = (self.coordinates[:,0] - x_min) / x_range
self.coordinates[:,1] = (self.coordinates[:,1] - y_min) / y_range
self.coordinates -= 0.5
self.coordinates *= 2.0
if keep_ratio: # may cause waste of space in the short side
max_range = max(x_range, y_range)
scale_x, scale_y = x_range / max_range, y_range / max_range
self.coordinates[:,0] *= scale_x
self.coordinates[:,1] *= scale_y
def __len__(self):
return self.n_cell
def get_raw_dim(self):
return self.n_gene
def get_embd_dim(self):
if self.has_embeddings():
return self.n_embd
else:
return None
def __getitem__(self, idx):
if self.has_embeddings():
return {
"idx": idx,
"coordinates": torch.Tensor(self.coordinates[idx,:].copy()).float(),
"embeddings": torch.Tensor(self.embeddings[idx,:].copy()).float(),
"raw_representations": torch.Tensor(self.raw_representations[idx,:].copy()).float()
}
else:
return {
"idx": idx,
"coordinates": torch.Tensor(self.coordinates[idx,:].copy()).float(),
"raw_representations": torch.Tensor(self.raw_representations[idx,:].copy()).float()
}
# for eval only
class Full2D(Dataset):
def __init__(self, side_length=200):
self.side_length = side_length
linspace = torch.linspace(-1, 1, side_length)
self.coordinates = torch.cartesian_prod(linspace, linspace)
def __len__(self):
return self.side_length ** 2
def __getitem__(self, idx):
return {
"idx": idx,
"coordinates": self.coordinates[idx,:].float(),
}
class ST3D(Dataset):
def __init__(self, data_file, require_coordnorm=True, keep_ratio=True, **kwargs):
super().__init__()
adata = read_anndata(data_file)
self.coordinates = adata.obsm["spatial"]
if sp.issparse(adata.X):
self.raw_representations = adata.X.toarray()
else:
self.raw_representations = adata.X
# take statistics
self.n_cell = self.raw_representations.shape[0]
self.n_gene = self.raw_representations.shape[1]
self.n_channels = self.n_gene
if "embeddings" in adata.obsm:
self.embeddings = adata.obsm["embeddings"]
assert self.raw_representations.shape[0] == self.embeddings.shape[0]
self.n_embd = self.embeddings.shape[1]
else:
self.embeddings = None
if require_coordnorm:
self._normalize_coordinates(keep_ratio=keep_ratio)
# self.raw_pca = PCA(n_components=3, random_state=0) # map raw representation dimension to 3 for visualization
# self.raw_pca.fit(self.raw_representations)
if self.has_embeddings():
self.embd_pca = PCA(n_components=3, random_state=0) # map embedding dimension to 3 for visualization
self.embd_pca.fit(self.embeddings)
def has_embeddings(self):
return False if self.embeddings is None else True
def plot_raw_representations(self, spot_size=2, train_indices=None, val_indices=None):
if train_indices and val_indices:
train_fig = plot_ST(self.coordinates[train_indices,:], self.raw_pca.transform(self.raw_representations[train_indices,:]), spot_size)
val_fig = plot_ST(self.coordinates[val_indices,:], self.raw_pca.transform(self.raw_representations[val_indices,:]), spot_size)
return train_fig, val_fig
else:
fig = plot_ST(self.coordinates, self.raw_pca.transform(self.raw_representations), spot_size)
return fig
def plot_embeddings(self, spot_size=2, train_indices=None, val_indices=None):
assert self.has_embeddings(), "The current adata file has NO embeddings!"
if train_indices and val_indices:
train_fig = plot_ST(self.coordinates[train_indices,:], self.embd_pca.transform(self.embeddings[train_indices,:]), spot_size)
val_fig = plot_ST(self.coordinates[val_indices,:], self.embd_pca.transform(self.embeddings[val_indices,:]), spot_size)
return train_fig, val_fig
else:
fig = plot_ST(self.coordinates, self.embd_pca.transform(self.embeddings), spot_size)
return fig
# normalize coordinates to [-1.0, +1.0]
def _normalize_coordinates(self, keep_ratio):
x_min, y_min, z_min = list(self.coordinates.min(axis=0))
x_max, y_max, z_max = list(self.coordinates.max(axis=0))
x_range, y_range, z_range = x_max - x_min, y_max - y_min, z_max - z_min
self.coordinates[:,0] = (self.coordinates[:,0] - x_min) / x_range
self.coordinates[:,1] = (self.coordinates[:,1] - y_min) / y_range
self.coordinates[:,2] = (self.coordinates[:,2] - z_min) / z_range
self.coordinates -= 0.5
self.coordinates *= 2.0
if keep_ratio: # may cause waste of space in the short side
max_range = max(x_range, y_range, z_range)
scale_x, scale_y, scale_z = x_range / max_range, y_range / max_range, z_range / max_range
self.coordinates[:,0] *= scale_x
self.coordinates[:,1] *= scale_y
self.coordinates[:,2] *= scale_z
def __len__(self):
return self.n_cell
def get_raw_dim(self):
return self.n_gene
def get_embd_dim(self):
if self.has_embeddings():
return self.n_embd
else:
return None
def __getitem__(self, idx):
if self.has_embeddings():
return {
"idx": idx,
"coordinates": torch.Tensor(self.coordinates[idx,:].copy()).float(),
"embeddings": torch.Tensor(self.embeddings[idx,:].copy()).float(),
"raw_representations": torch.Tensor(self.raw_representations[idx,:].copy()).float()
}
else:
return {
"idx": idx,
"coordinates": torch.Tensor(self.coordinates[idx,:].copy()).float(),
"raw_representations": torch.Tensor(self.raw_representations[idx,:].copy()).float()
}
class HRSS(Dataset):
def __init__(self, data_file, **kwargs):
super().__init__()
self.data = np.load(data_file)
if "embeddings" in self.data.keys():
self.embeddings = self.data["embeddings"]
self.raw = self.data["raw"]
self.coordinates = self.data["spatial"]
self.dataset_length = self.coordinates.shape[0]
self.n_channels = self.raw.shape[-1]
self.n_embd = self.embeddings.shape[-1]
self.embd_pca = PCA(n_components=3, random_state=0)
self.embd_pca.fit(self.embeddings)
else:
self.raw = self.data["raw"]
# switch normalization strategy here
#self._uint16_normalize()
self._minmax_normalize()
# custom slicing of self.raw
self.raw = self.raw[:,:,::10] # every 10 channels
self.height, self.width, self.n_channels = self.raw.shape
self.dataset_length = self.height * self.width
h_linspace = torch.linspace(-1, 1, self.height) # rows
w_linspace = torch.linspace(-1, 1, self.width) # columns
self.coordinates = torch.cartesian_prod(h_linspace, w_linspace)
self.embeddings = None
self.raw = rearrange(self.raw, "H W C -> (H W) C")
self.raw_pca = PCA(n_components=3, random_state=0)
self.raw_pca.fit(self.raw)
def has_embeddings(self):
return False if self.embeddings is None else True
def _uint16_normalize(self):
self.raw = self.raw / 65535
def _minmax_normalize(self):
normalized_image = np.zeros_like(self.raw, dtype=float)
for i in range(self.raw.shape[2]):
channel = self.raw[:, :, i]
min_val, max_val = np.min(channel), np.max(channel)
if max_val != min_val:
# normalized_image[:, :, i] = 2 * (channel - min_val) / (max_val - min_val) - 1
normalized_image[:, :, i] = (channel - min_val) / (max_val - min_val)
else:
normalized_image[:, :, i] = 0
self.raw = normalized_image
def plot_raw_representations(self, train_indices=None, val_indices=None):
rgb = self.raw_pca.transform(self.raw)
if train_indices and val_indices:
train_fig = plot_ST(self.coordinates[train_indices,:], rgb[train_indices,:], spot_size=1)
val_fig = plot_ST(self.coordinates[val_indices,:], rgb[val_indices,:], spot_size=1)
return train_fig, val_fig
else:
fig = plot_ST(self.coordinates, rgb)
return fig
def plot_embeddings(self, spot_size=2, train_indices=None, val_indices=None):
assert self.has_embeddings(), "The current adata file has NO embeddings!"
if train_indices and val_indices:
train_fig = plot_ST(self.coordinates[train_indices,:], self.embd_pca.transform(self.embeddings[train_indices,:]), spot_size)
val_fig = plot_ST(self.coordinates[val_indices,:], self.embd_pca.transform(self.embeddings[val_indices,:]), spot_size)
return train_fig, val_fig
else:
fig = plot_ST(self.coordinates, self.embd_pca.transform(self.embeddings), spot_size)
return fig
def __len__(self):
return self.dataset_length
def get_raw_dim(self):
return self.n_channels
def get_embd_dim(self):
if self.has_embeddings():
return self.n_embd
else:
return None
def __getitem__(self, idx):
if self.has_embeddings():
return {
"idx": idx,
"coordinates": torch.Tensor(self.coordinates[idx,:].copy()).float(),
"embeddings": torch.Tensor(self.embeddings[idx,:].copy()).float(),
"raw_representations": torch.Tensor(self.raw[idx,:].copy()).float()
}
else:
return {
"idx": idx,
"coordinates": torch.Tensor(self.coordinates[idx,:]).float(),
"raw_representations": torch.Tensor(self.raw[idx,:]).float()
}
class GraphST2D(Dataset):
def __init__(self, h5ad_file,neighbors,keep_ratio=True, **kwargs):
super().__init__()
#if h5ad_file is not str, adata = h5ad_file
if type(h5ad_file) == str:
adata = read_anndata(h5ad_file)
else:
adata = h5ad_file
self.sb = True
self.coordinates = adata.obsm["spatial"]
self.neighbors = neighbors
if sp.issparse(adata.X):
self.raw_representations = adata.X.toarray()
else:
self.raw_representations = adata.X
# take statistics
self.n_cell = self.raw_representations.shape[0]
self.n_gene = self.raw_representations.shape[1]
if "embeddings" in adata.obsm:
self.embeddings = adata.obsm["embeddings"]
assert self.raw_representations.shape[0] == self.embeddings.shape[0]
self.n_embd = self.embeddings.shape[1]
else:
self.embeddings = None
self._normalize_coordinates(keep_ratio=keep_ratio)
self.raw_pca = PCA(n_components=3, random_state=0) # map raw representation dimension to 3 for visualization
self.raw_pca.fit(self.raw_representations)
if self.has_embeddings():
self.embd_pca = PCA(n_components=3, random_state=0) # map embedding dimension to 3 for visualization
self.embd_pca.fit(self.embeddings)
def has_embeddings(self):
return False if self.embeddings is None else True
def plot_raw_representations(self, spot_size=2, train_indices=None, val_indices=None):
if train_indices and val_indices:
train_fig = plot_ST(self.coordinates[train_indices,:], self.raw_pca.transform(self.raw_representations[train_indices,:]), spot_size)
val_fig = plot_ST(self.coordinates[val_indices,:], self.raw_pca.transform(self.raw_representations[val_indices,:]), spot_size)
return train_fig, val_fig
else:
fig = plot_ST(self.coordinates, self.raw_pca.transform(self.raw_representations), spot_size)
return fig
def plot_embeddings(self, spot_size=2, train_indices=None, val_indices=None):
assert self.has_embeddings(), "The current adata file has NO embeddings!"
if train_indices and val_indices:
train_fig = plot_ST(self.coordinates[train_indices,:], self.embd_pca.transform(self.embeddings[train_indices,:]), spot_size)
val_fig = plot_ST(self.coordinates[val_indices,:], self.embd_pca.transform(self.embeddings[val_indices,:]), spot_size)
return train_fig, val_fig
else:
fig = plot_ST(self.coordinates, self.embd_pca.transform(self.embeddings), spot_size)
return fig
# normalize coordinates to [-1.0, +1.0]
def _normalize_coordinates(self, keep_ratio):
x_min, y_min = list(self.coordinates.min(axis=0))
x_max, y_max = list(self.coordinates.max(axis=0))
self.coordinates = self.coordinates.astype(np.float64)
x_range, y_range = x_max - x_min, y_max - y_min
self.coordinates[:,0] = (self.coordinates[:,0] - x_min) / x_range
self.coordinates[:,1] = (self.coordinates[:,1] - y_min) / y_range
self.coordinates -= 0.5
self.coordinates *= 2.0
if keep_ratio: # may cause waste of space in the short side
max_range = max(x_range, y_range)
scale_x, scale_y = x_range / max_range, y_range / max_range
self.coordinates[:,0] *= scale_x
self.coordinates[:,1] *= scale_y
def get_raw_dim(self):
return self.n_gene
def get_embd_dim(self):
if self.has_embeddings():
return self.n_embd
else:
return None
def __len__(self):
return self.n_cell
def __getitem__(self, idx):
if self.has_embeddings():
return {
"idx": idx,
"coordinates": torch.Tensor(self.coordinates[idx,:].copy()).float(),
"embeddings": torch.Tensor(self.embeddings[idx,:].copy()).float(),
"raw_representations": torch.Tensor(self.raw_representations[idx,:].copy()).float(),
"neighbors": self.neighbors[idx]
}
else:
return {
"idx": idx,
"coordinates": torch.Tensor(self.coordinates[idx,:].copy()).float(),
"raw_representations": torch.Tensor(self.raw_representations[idx,:].copy()).float(),
"neighbors": self.neighbors[idx]
}
if __name__ == "__main__":
ds = ST2D("./data/preprocessed_data/E11.5.h5ad", True, True)
raw = ds.raw_representations
print(raw.max(), raw.min())
sparsity = (raw==0).sum() / (raw>=0).sum()
print(sparsity)