44import pytorch_lightning as pl
55import torch
66from torch .utils .data import Dataset , DataLoader , WeightedRandomSampler
7- from typing import Optional
7+ from typing import Optional , TYPE_CHECKING
88
99from .utils import align_dataset
1010from .ontologies import (
1313 find_most_viable_parent ,
1414)
1515
16+ if TYPE_CHECKING :
17+ import numpy
18+ from numpy .typing import NDArray
19+ from typing import Any
1620
17- class scDataset (Dataset ):
21+ Index = (
22+ NDArray [numpy .integer [Any ]]
23+ | NDArray [numpy .bool_ ]
24+ | tuple [NDArray [numpy .integer [Any ]] | NDArray [numpy .bool_ ], ...]
25+ )
26+
27+
28+ class scDataset (
29+ Dataset [tuple ["numpy.ndarray" , "numpy.ndarray" , Optional ["numpy.ndarray" ]]]
30+ ):
1831 """A class that represents a single cell dataset.
1932
2033 Parameters
@@ -27,17 +40,28 @@ class scDataset(Dataset):
2740 The study identifier for every cell.
2841 """
2942
30- def __init__ (self , X , Y , study = None ):
43+ def __init__ (
44+ self ,
45+ X : "numpy.ndarray" ,
46+ Y : "numpy.ndarray" ,
47+ study : Optional ["numpy.ndarray" ] = None ,
48+ ):
3149 self .X = X
3250 self .Y = Y
3351 self .study = study
3452
35- def __len__ (self ):
53+ def __len__ (self ) -> int :
3654 return len (self .Y )
3755
38- def __getitem__ (self , idx ):
56+ def __getitem__ (
57+ self , idx : "Index"
58+ ) -> tuple ["numpy.ndarray" , "numpy.ndarray" , Optional ["numpy.ndarray" ]]:
3959 # data, label, study
40- return self .X [idx ].toarray ().flatten (), self .Y [idx ], self .study [idx ]
60+ return (
61+ self .X [idx ].toarray ().flatten (),
62+ self .Y [idx ],
63+ self .study [idx ] if self .study is not None else None ,
64+ )
4165
4266
4367class scCollator :
@@ -51,11 +75,7 @@ class scCollator:
5175 Use sparse matrices.
5276 """
5377
54- def __init__ (
55- self ,
56- label2int : dict ,
57- sparse : bool = False ,
58- ):
78+ def __init__ (self , label2int : dict , sparse : bool = False ):
5979 self .label2int = label2int
6080 self .sparse = sparse
6181
@@ -65,11 +85,8 @@ def __call__(self, batch):
6585 X = torch .squeeze (torch .Tensor (np .vstack (profiles )))
6686 if self .sparse :
6787 X = X .to_sparse ()
68- return (
69- X ,
70- torch .Tensor ([self .label2int [l ] for l in labels ]),
71- np .array (studies ),
72- )
88+
89+ return (X , torch .Tensor ([self .label2int [l ] for l in labels ]), np .array (studies ))
7390
7491
7592class MetricLearningDataModule (pl .LightningDataModule ):
0 commit comments