Documentation for Dataset
scdataloader.data.Dataset
dataclass
Bases: Dataset
PyTorch Dataset for loading single-cell data from a LaminDB Collection.
This class wraps LaminDB's MappedCollection to provide additional features: - Management of hierarchical ontology labels (cell type, tissue, disease, etc.) - Automatic encoding of categorical labels to integers - Multi-species gene handling with unified gene indexing - Optional metacell aggregation and KNN neighbor retrieval
The dataset lazily loads data from storage, making it memory-efficient for large collections spanning multiple files.
| Parameters: |
|
|---|
| Attributes: |
|
|---|
| Raises: |
|
|---|
Example
collection = ln.Collection.filter(key="my_collection").first() dataset = Dataset( ... lamin_dataset=collection, ... clss_to_predict=["organism_ontology_term_id", "cell_type_ontology_term_id"], ... hierarchical_clss=["cell_type_ontology_term_id"], ... ) sample = dataset[0] # Returns dict with "X" and encoded labels
Methods:
| Name | Description |
|---|---|
define_hierarchies |
Define hierarchical label groupings from ontology relationships. |
get_label_cats |
Get combined categorical codes for one or more label columns. |
get_unseen_mapped_dataset_elements |
Get genes marked as unseen for a specific sample. |
define_hierarchies
Define hierarchical label groupings from ontology relationships.
Uses Bionty to retrieve parent-child relationships for ontology terms, then builds groupings mapping parent terms to their descendants. Updates encoders to include parent terms and reorders labels so that leaf terms (directly predictable) come first.
| Parameters: |
|
|---|
| Raises: |
|
|---|
Note
Modifies self.labels_groupings, self.class_topred, and self.mapped_dataset.encoders in place.
Source code in scdataloader/data.py
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 | |
get_label_cats
Get combined categorical codes for one or more label columns.
Retrieves labels from the mapped dataset and combines them into a single categorical encoding. Useful for creating compound class labels for stratified sampling.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scdataloader/data.py
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 | |
get_unseen_mapped_dataset_elements
Get genes marked as unseen for a specific sample.
Retrieves the list of genes that were not observed (expression = 0 or marked as unseen) for the sample at the given index.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in scdataloader/data.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
scdataloader.data.SimpleAnnDataset
Bases: Dataset
Simple PyTorch Dataset wrapper for a single AnnData object.
Provides a lightweight interface for using AnnData with PyTorch DataLoaders, compatible with the scDataLoader collator. Useful for inference on new data that isn't stored in LaminDB.
| Parameters: |
|
|---|
| Attributes: |
|
|---|
| Raises: |
|
|---|
Example
dataset = SimpleAnnDataset( ... adata=my_adata, ... obs_to_output=["cell_type", "organism_ontology_term_id"], ... encoder={"organism_ontology_term_id": {"NCBITaxon:9606": 0}}, ... ) loader = DataLoader(dataset, batch_size=32, collate_fn=collator)
Source code in scdataloader/data.py
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 | |