-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy path_graph_dataloader.py
More file actions
227 lines (197 loc) · 8.4 KB
/
_graph_dataloader.py
File metadata and controls
227 lines (197 loc) · 8.4 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
"""Graph-aware dataloaders for spatial single-cell models."""
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
import torch
from torch.utils.data import default_convert
from scvi import REGISTRY_KEYS
from scvi.data import AnnTorchDataset
from scvi.dataloaders._ann_dataloader import AnnDataLoader
from scvi.dataloaders._data_splitting import DataSplitter
if TYPE_CHECKING:
from scvi.data import AnnDataManager
def _as_torch_tensor(array: np.ndarray | torch.Tensor) -> torch.Tensor:
"""Convert AnnTorchDataset output to a torch tensor without changing sparse layout."""
if isinstance(array, np.ndarray):
return torch.from_numpy(array)
return array
class _GraphBatchConverter:
"""Convert an AnnTorchDataset batch into a PyG Data object."""
def __init__(
self,
full_adata_manager: AnnDataManager,
neighbor_indices_key: str,
edge_obsm_keys: list[str],
load_sparse_neighbor_tensor: bool,
load_neighbor_expression: bool,
):
self.neighbor_indices_key = neighbor_indices_key
self.edge_obsm_keys = edge_obsm_keys
self.load_neighbor_expression = load_neighbor_expression
if load_neighbor_expression:
self._full_dataset = AnnTorchDataset(
full_adata_manager,
getitem_tensors=[REGISTRY_KEYS.X_KEY],
load_sparse_tensor=load_sparse_neighbor_tensor,
)
def __call__(self, batch: dict[str, np.ndarray | torch.Tensor]):
try:
from torch_geometric.data import Data
except ImportError as error:
raise ImportError(
"torch_geometric is required for GraphDataLoader. "
"Install it with: pip install torch_geometric"
) from error
batch = default_convert(batch)
ind_neighbors = batch[self.neighbor_indices_key].long()
n_obs, n_neighbors = ind_neighbors.shape
x = _as_torch_tensor(batch[REGISTRY_KEYS.X_KEY])
center_idx = torch.arange(n_obs, dtype=torch.long).repeat_interleave(n_neighbors)
neighbor_idx = torch.arange(n_obs * n_neighbors, dtype=torch.long)
edge_index = torch.stack([center_idx, neighbor_idx], dim=0)
edge_attrs = []
for key in self.edge_obsm_keys:
vals = batch[key].float()
edge_attrs.append(vals.reshape(n_obs * n_neighbors, -1))
edge_attr = torch.cat(edge_attrs, dim=1) if edge_attrs else None
data_kwargs = dict(batch)
data_kwargs.update(
{
"x": x,
"edge_index": edge_index,
"edge_attr": edge_attr,
"distances_n": batch.get("distance_neighbor"),
}
)
if self.load_neighbor_expression:
flat_neighbors = ind_neighbors.cpu().numpy().ravel()
data_kwargs["x_n"] = _as_torch_tensor(
self._full_dataset[flat_neighbors][REGISTRY_KEYS.X_KEY]
)
return Data(**data_kwargs)
class GraphDataLoader(AnnDataLoader):
"""DataLoader that yields mini-batches as :class:`torch_geometric.data.Data` objects.
Each batch contains center cells and their pre-fetched spatial neighbors. Neighbor
expression is looked up from ``full_adata_manager`` so neighbors outside the current
train/validation/test split are intentionally allowed, matching existing RESOLVI behavior.
Parameters
----------
adata_manager
:class:`~scvi.data.AnnDataManager` for the split being loaded.
full_adata_manager
:class:`~scvi.data.AnnDataManager` for all observations. Used for neighbor expression
lookup, including cross-split neighbors.
indices
Observation indices to load from ``adata_manager``.
neighbor_indices_key
Registry key containing neighbor indices, shape ``[N, K]``.
edge_obsm_keys
Registry keys to flatten and concatenate into ``edge_attr``. Each key must have shape
``[N, K]`` or ``[N, K, D]``. Defaults to ``["distance_neighbor"]``.
load_sparse_neighbor_tensor
If ``True``, loads sparse neighbor expression as sparse torch tensors. This avoids
densifying neighbor expression on the CPU before device transfer.
load_neighbor_expression
If ``False``, omits ``x_n`` and leaves neighbor expression gathering to the model. This is
useful when a model keeps a device-resident expression cache.
**kwargs
Forwarded to :class:`~scvi.dataloaders.AnnDataLoader`.
"""
def __init__(
self,
adata_manager: AnnDataManager,
full_adata_manager: AnnDataManager,
indices: list[int] | list[bool] | None = None,
neighbor_indices_key: str = "index_neighbor",
edge_obsm_keys: list[str] | None = None,
load_sparse_neighbor_tensor: bool = True,
load_neighbor_expression: bool = True,
**kwargs,
):
if "collate_fn" in kwargs:
raise ValueError("GraphDataLoader uses its own collate_fn to build graph batches.")
if kwargs.pop("iter_ndarray", False):
raise ValueError("GraphDataLoader does not support `iter_ndarray=True`.")
super().__init__(adata_manager, indices=indices, **kwargs)
self.neighbor_indices_key = neighbor_indices_key
self.edge_obsm_keys = (
list(edge_obsm_keys) if edge_obsm_keys is not None else ["distance_neighbor"]
)
self.load_sparse_neighbor_tensor = load_sparse_neighbor_tensor
self.load_neighbor_expression = load_neighbor_expression
self._graph_batch_converter = _GraphBatchConverter(
full_adata_manager,
neighbor_indices_key=self.neighbor_indices_key,
edge_obsm_keys=self.edge_obsm_keys,
load_sparse_neighbor_tensor=load_sparse_neighbor_tensor,
load_neighbor_expression=load_neighbor_expression,
)
self.collate_fn = self._graph_batch_converter
class GraphDataSplitter(DataSplitter):
"""DataSplitter that creates :class:`GraphDataLoader` instances.
Parameters
----------
load_sparse_neighbor_tensor
Forwarded to :class:`GraphDataLoader`.
load_neighbor_expression
Forwarded to :class:`GraphDataLoader`.
"""
def __init__(
self,
adata_manager: AnnDataManager,
neighbor_indices_key: str = "index_neighbor",
edge_obsm_keys: list[str] | None = None,
load_sparse_neighbor_tensor: bool = True,
load_neighbor_expression: bool = True,
**kwargs,
):
super().__init__(adata_manager, **kwargs)
self.neighbor_indices_key = neighbor_indices_key
self.edge_obsm_keys = (
list(edge_obsm_keys) if edge_obsm_keys is not None else ["distance_neighbor"]
)
self.load_sparse_neighbor_tensor = load_sparse_neighbor_tensor
self.load_neighbor_expression = load_neighbor_expression
def _make_graph_dataloader(
self,
indices: np.ndarray,
shuffle: bool,
drop_last: bool,
) -> GraphDataLoader:
return GraphDataLoader(
self.adata_manager,
full_adata_manager=self.adata_manager,
indices=indices,
shuffle=shuffle,
drop_last=drop_last,
load_sparse_tensor=self.load_sparse_tensor,
pin_memory=self.pin_memory,
neighbor_indices_key=self.neighbor_indices_key,
edge_obsm_keys=self.edge_obsm_keys,
load_sparse_neighbor_tensor=self.load_sparse_neighbor_tensor,
load_neighbor_expression=self.load_neighbor_expression,
**self.data_loader_kwargs,
)
def train_dataloader(self) -> GraphDataLoader:
"""Create graph train dataloader."""
return self._make_graph_dataloader(
self.train_idx,
shuffle=True,
drop_last=self.drop_last,
)
def val_dataloader(self) -> GraphDataLoader | None:
"""Create graph validation dataloader."""
if len(self.val_idx) > 0:
return self._make_graph_dataloader(
self.val_idx,
shuffle=False,
drop_last=False,
)
def test_dataloader(self) -> GraphDataLoader | None:
"""Create graph test dataloader."""
if len(self.test_idx) > 0:
return self._make_graph_dataloader(
self.test_idx,
shuffle=False,
drop_last=False,
)