1
1
from typing import Self
2
2
3
3
import numpy as np
4
- from numpy .typing import NDArray
5
4
from polars import DataFrame
6
5
7
- from ._typealias import _Csx , _CsxArray
6
+ from ._typealias import (
7
+ _AssignmentScoreMap ,
8
+ _Coord ,
9
+ _CosineMap ,
10
+ _Csx ,
11
+ _CsxArray ,
12
+ _Kernel ,
13
+ _SignatureArray ,
14
+ )
8
15
9
16
def sparse_kde_csx_py (
10
- counts : _Csx , kernel : NDArray [ np . float32 ] , * , threshold : float = 0
17
+ counts : _Csx , kernel : _Kernel , * , threshold : float = 0
11
18
) -> _CsxArray :
12
19
"""
13
20
Calculate the KDE for each spot with counts as uint16.
@@ -16,8 +23,11 @@ def sparse_kde_csx_py(
16
23
def kde_at_coord (
17
24
counts : GridCounts ,
18
25
genes : list [str ],
19
- kernel : NDArray [np .float32 ],
20
- coordinates : tuple [NDArray [np .int_ ], NDArray [np .int_ ]],
26
+ kernel : _Kernel ,
27
+ coordinates : tuple [
28
+ np .ndarray [tuple [int ], np .dtype [np .int_ ]],
29
+ np .ndarray [tuple [int ], np .dtype [np .int_ ]],
30
+ ],
21
31
* ,
22
32
n_threads : int | None = None ,
23
33
) -> _CsxArray :
@@ -26,29 +36,34 @@ def kde_at_coord(
26
36
"""
27
37
28
38
def categorical_coordinate (
29
- x : NDArray [np .int32 ], y : NDArray [np .int32 ], * , n_threads : int | None = None
30
- ) -> tuple [NDArray [np .int32 ], NDArray [np .int32 ]]:
39
+ x : _Coord , y : _Coord , * , n_threads : int | None = None
40
+ ) -> tuple [
41
+ np .ndarray [tuple [int ], np .dtype [np .int32 ]],
42
+ np .ndarray [tuple [int , int ], np .dtype [np .int32 ]],
43
+ ]:
31
44
"""
32
45
Get the codes and the coordinates (comparable to a pandas.Categorical)
33
46
"""
34
47
35
48
def coordinate_as_string (
36
- x : NDArray [ np . int32 ] , y : NDArray [ np . int32 ] , * , n_threads : int | None = None
37
- ) -> NDArray [ np .str_ ]:
49
+ x : _Coord , y : _Coord , * , n_threads : int | None = None
50
+ ) -> np . ndarray [ tuple [ int ], np .dtype [ np . str_ ] ]:
38
51
"""
39
52
Concatenate two int arrays elementwise into a string representation (i.e. 'x_y').
40
53
"""
41
54
42
55
def cosinef32_and_celltypei8 (
43
56
counts : GridCounts ,
44
57
genes : list [str ],
45
- signatures : NDArray [ np . float32 ] ,
46
- kernel : NDArray [ np . float32 ] ,
58
+ signatures : _SignatureArray ,
59
+ kernel : _Kernel ,
47
60
* ,
48
61
log : bool = False ,
49
62
chunk_size : tuple [int , int ] = (500 , 500 ),
50
63
n_threads : int | None = None ,
51
- ) -> tuple [NDArray [np .float32 ], NDArray [np .float32 ], NDArray [np .int8 ]]:
64
+ ) -> tuple [
65
+ _CosineMap , _AssignmentScoreMap , np .ndarray [tuple [int , int ], np .dtype [np .int8 ]]
66
+ ]:
52
67
"""
53
68
Calculate the cosine similarity given counts and signatures and assign the most
54
69
similar celltype.
@@ -57,13 +72,15 @@ def cosinef32_and_celltypei8(
57
72
def cosinef32_and_celltypei16 (
58
73
counts : GridCounts ,
59
74
genes : list [str ],
60
- signatures : NDArray [ np . float32 ] ,
61
- kernel : NDArray [ np . float32 ] ,
75
+ signatures : _SignatureArray ,
76
+ kernel : _Kernel ,
62
77
* ,
63
78
log : bool = False ,
64
79
chunk_size : tuple [int , int ] = (500 , 500 ),
65
80
n_threads : int | None = None ,
66
- ) -> tuple [NDArray [np .float32 ], NDArray [np .float32 ], NDArray [np .int16 ]]:
81
+ ) -> tuple [
82
+ _CosineMap , _AssignmentScoreMap , np .ndarray [tuple [int , int ], np .dtype [np .int16 ]]
83
+ ]:
67
84
"""
68
85
Calculate the cosine similarity given counts and signatures and assign the most
69
86
similar celltype.
@@ -186,7 +203,7 @@ class GridCounts:
186
203
Mapping from gene to number of counts.
187
204
"""
188
205
189
- def grid_counts (self ) -> NDArray [ np .uintc ]:
206
+ def grid_counts (self ) -> np . ndarray [ tuple [ int , int ], np .dtype [ np . uintc ] ]:
190
207
"""
191
208
Counts per pixel.
192
209
@@ -231,7 +248,7 @@ class GridCounts:
231
248
Range to crop as `(ymin, ymax)`
232
249
"""
233
250
234
- def filter_mask (self , mask : NDArray [ np .bool_ ]):
251
+ def filter_mask (self , mask : np . ndarray [ tuple [ int , int ], np .dtype [ np . bool_ ] ]):
235
252
"""
236
253
Filter all genes with a binary mask.
237
254
0 commit comments