loader
Data loading utilities for scDesigner models.
The main function is :func:adata_loader, which builds a
:class:~torch.utils.data.DataLoader` that returns minibatches of,
- X: expression matrix rows (cells × genes) as a float tensor
- obs: a dict with formula keys and design matrices from a fitted
:class:
~scdesigner.data.design.Design
We support both in-memory and backed :class:~anndata.AnnData objects. For
backed anndata, we read chunk_size rows at a time. See :func:adata_loader.
AnnDataDataset
Bases: Dataset
PyTorch dataset over AnnData rows, reading one chunk of rows at a time.
Returns (X_row, obs_dict) as items, where obs_dict has one
design-matrix row per key in the design. Responses Y come from
:func:~scdesigner.data.response.read_response_rows and designs run
design to create X on the corresponding rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
In memory or backed. |
required |
design
|
Design
|
Fitted on the complete observation table. |
required |
chunk_size
|
int
|
Number of contiguous rows to cache at once. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
predictor_names |
dict[str, list[str]]
|
Names of the design-matrix columns per key. |
device |
device
|
Device used for caching tensors. |
Source code in scdesigner/src/scdesigner/data/loader.py
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 | |
__getitem__(idx)
Returns (X_row, obs_dict) for the given index.
Source code in scdesigner/src/scdesigner/data/loader.py
129 130 131 132 133 134 | |
__getitems__(indices)
Fetch a minibatch from an existing chunk.
Source code in scdesigner/src/scdesigner/data/loader.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
to(device)
Move cached chunk data and use device for future chunks.
Source code in scdesigner/src/scdesigner/data/loader.py
174 175 176 177 178 179 180 181 182 | |
PreloadedDataset
Bases: Dataset
Dataset wrapper where both response and predictors are preloaded.
This dataset is used for in-memory AnnData objects where we can materialize:
y: the full expression matrix on the target devicex: a dict of design matrices (one per formula key) on the same device
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_tensor
|
Tensor
|
Expression tensor of shape (n_obs, n_features). |
required |
x_tensors
|
dict[str, Tensor]
|
Mapping from formula key to design matrix tensor of shape (n_obs, p_k). |
required |
predictor_names
|
dict[str, list[str]]
|
Mapping from formula key to the names of the design matrix columns. |
required |
Source code in scdesigner/src/scdesigner/data/loader.py
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 | |
__getitems__(indices)
Faster alternative to getitem
On MPS, using ordinary getitem is slow, because it requires us to
read batch_size different slices and then stack. We can bypass that
expensive read/concat by using getitems. See
examples/validation/loader_benchmark.ipynb for a precise comparison.
Source code in scdesigner/src/scdesigner/data/loader.py
70 71 72 73 74 75 76 77 78 79 | |
to(device)
Move preloaded responses and design matrices to device.
Source code in scdesigner/src/scdesigner/data/loader.py
81 82 83 84 85 86 | |
adata_loader(adata, formula, chunk_size=None, batch_size=1024, shuffle=False, num_workers=0, device=None, partition_keys=None, **kwargs)
Create a :class:~torch.utils.data.DataLoader over an AnnData dataset.
The resulting loader yields tuples (X_batch, obs_batch) where:
X_batchis a float tensor of shape (batch_size, n_genes)obs_batchis a dict mapping each formula key to a float tensor of shape (batch_size, n_covariates) for that key's design matrix
For backed AnnData objects, the underlying dataset uses chunk caching to limit memory usage. Chunks are read in the order stored on disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
Input AnnData object. |
required |
formula
|
dict[str, str] or Design
|
A fitted :class: |
required |
chunk_size
|
int
|
Rows per chunked read. |
None
|
batch_size
|
int
|
Mini-batch size returned by the loader. |
1024
|
shuffle
|
bool
|
Whether to shuffle observations each epoch. Not allowed for backed reads. |
False
|
num_workers
|
int
|
Number of DataLoader workers. |
0
|
partition_keys
|
set of str
|
Formula used to partition observations, usually used for copula
covariance groups. Ignored when |
None
|
**kwargs
|
Additional keyword arguments filtered via |
{}
|
Returns:
| Type | Description |
|---|---|
DataLoader
|
DataLoader over the dataset. |
Source code in scdesigner/src/scdesigner/data/loader.py
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 | |
dict_collate_fn(batch)
Custom collate function for handling dictionary obs tensors.
Source code in scdesigner/src/scdesigner/data/loader.py
320 321 322 323 324 325 326 327 328 329 330 331 332 | |
get_device(device=None)
Detect and return the best available device (MPS, CUDA, or CPU).
Source code in scdesigner/src/scdesigner/data/loader.py
25 26 27 28 29 30 31 32 33 34 35 | |
obs_loader(obs, formula, **kwargs)
Create a loader that yields design matrices for an observation table.
This is a convenience wrapper for prediction-time batching: it creates a
dummy AnnData with a placeholder X and uses :func:adata_loader to
construct batches of covariates derived from obs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs
|
DataFrame
|
Observation metadata used to build design matrices. |
required |
formula
|
dict[str, str] or Design
|
See :func: |
required |
**kwargs
|
Forwarded to :func: |
{}
|
Returns:
| Type | Description |
|---|---|
DataLoader
|
Loader yielding |
Source code in scdesigner/src/scdesigner/data/loader.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
resolve_chunk_size(adata, chunk_size)
How many rows per chunk? Returns None for in-memory data.
Source code in scdesigner/src/scdesigner/data/loader.py
185 186 187 188 189 | |
to_tensor(X)
Convert X to a float tensor with conservative squeezing.
Source code in scdesigner/src/scdesigner/data/loader.py
334 335 336 337 338 339 340 341 342 343 344 | |