_ckdtree.pyi 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import sys
  2. from typing import (
  3. Any,
  4. Dict,
  5. Generic,
  6. List,
  7. Optional,
  8. overload,
  9. Set,
  10. Tuple,
  11. TypeVar,
  12. Union,
  13. )
  14. import numpy as np
  15. import numpy.typing as npt
  16. from scipy.sparse import coo_matrix, dok_matrix
  17. from typing import Literal
  18. # TODO: Replace `ndarray` with a 1D float64 array when possible
  19. _BoxType = TypeVar("_BoxType", None, npt.NDArray[np.float64])
  20. # Copied from `numpy.typing._scalar_like._ScalarLike`
  21. # TODO: Expand with 0D arrays once we have shape support
  22. _ArrayLike0D = Union[
  23. bool,
  24. int,
  25. float,
  26. complex,
  27. str,
  28. bytes,
  29. np.generic,
  30. ]
  31. _WeightType = Union[
  32. npt.ArrayLike,
  33. Tuple[Optional[npt.ArrayLike], Optional[npt.ArrayLike]],
  34. ]
  35. class cKDTreeNode:
  36. @property
  37. def data_points(self) -> npt.NDArray[np.float64]: ...
  38. @property
  39. def indices(self) -> npt.NDArray[np.intp]: ...
  40. # These are read-only attributes in cython, which behave like properties
  41. @property
  42. def level(self) -> int: ...
  43. @property
  44. def split_dim(self) -> int: ...
  45. @property
  46. def children(self) -> int: ...
  47. @property
  48. def start_idx(self) -> int: ...
  49. @property
  50. def end_idx(self) -> int: ...
  51. @property
  52. def split(self) -> float: ...
  53. @property
  54. def lesser(self) -> Optional[cKDTreeNode]: ...
  55. @property
  56. def greater(self) -> Optional[cKDTreeNode]: ...
  57. class cKDTree(Generic[_BoxType]):
  58. @property
  59. def n(self) -> int: ...
  60. @property
  61. def m(self) -> int: ...
  62. @property
  63. def leafsize(self) -> int: ...
  64. @property
  65. def size(self) -> int: ...
  66. @property
  67. def tree(self) -> cKDTreeNode: ...
  68. # These are read-only attributes in cython, which behave like properties
  69. @property
  70. def data(self) -> npt.NDArray[np.float64]: ...
  71. @property
  72. def maxes(self) -> npt.NDArray[np.float64]: ...
  73. @property
  74. def mins(self) -> npt.NDArray[np.float64]: ...
  75. @property
  76. def indices(self) -> npt.NDArray[np.float64]: ...
  77. @property
  78. def boxsize(self) -> _BoxType: ...
  79. # NOTE: In practice `__init__` is used as constructor, not `__new__`.
  80. # The latter gives us more flexibility in setting the generic parameter
  81. # though.
  82. @overload
  83. def __new__( # type: ignore[misc]
  84. cls,
  85. data: npt.ArrayLike,
  86. leafsize: int = ...,
  87. compact_nodes: bool = ...,
  88. copy_data: bool = ...,
  89. balanced_tree: bool = ...,
  90. boxsize: None = ...,
  91. ) -> cKDTree[None]: ...
  92. @overload
  93. def __new__(
  94. cls,
  95. data: npt.ArrayLike,
  96. leafsize: int = ...,
  97. compact_nodes: bool = ...,
  98. copy_data: bool = ...,
  99. balanced_tree: bool = ...,
  100. boxsize: npt.ArrayLike = ...,
  101. ) -> cKDTree[npt.NDArray[np.float64]]: ...
  102. # TODO: returns a 2-tuple of scalars if `x.ndim == 1` and `k == 1`,
  103. # returns a 2-tuple of arrays otherwise
  104. def query(
  105. self,
  106. x: npt.ArrayLike,
  107. k: npt.ArrayLike = ...,
  108. eps: float = ...,
  109. p: float = ...,
  110. distance_upper_bound: float = ...,
  111. workers: Optional[int] = ...,
  112. ) -> Tuple[Any, Any]: ...
  113. # TODO: returns a list scalars if `x.ndim <= 1`,
  114. # returns an object array of lists otherwise
  115. def query_ball_point(
  116. self,
  117. x: npt.ArrayLike,
  118. r: npt.ArrayLike,
  119. p: float,
  120. eps: float = ...,
  121. workers: Optional[int] = ...,
  122. return_sorted: Optional[bool] = ...,
  123. return_length: bool = ...
  124. ) -> Any: ...
  125. def query_ball_tree(
  126. self,
  127. other: cKDTree,
  128. r: float,
  129. p: float,
  130. eps: float = ...,
  131. ) -> List[List[int]]: ...
  132. @overload
  133. def query_pairs( # type: ignore[misc]
  134. self,
  135. r: float,
  136. p: float = ...,
  137. eps: float = ...,
  138. output_type: Literal["set"] = ...,
  139. ) -> Set[Tuple[int, int]]: ...
  140. @overload
  141. def query_pairs(
  142. self,
  143. r: float,
  144. p: float = ...,
  145. eps: float = ...,
  146. output_type: Literal["ndarray"] = ...,
  147. ) -> npt.NDArray[np.intp]: ...
  148. @overload
  149. def count_neighbors( # type: ignore[misc]
  150. self,
  151. other: cKDTree,
  152. r: _ArrayLike0D,
  153. p: float = ...,
  154. weights: None | Tuple[None, None] = ...,
  155. cumulative: bool = ...,
  156. ) -> int: ...
  157. @overload
  158. def count_neighbors( # type: ignore[misc]
  159. self,
  160. other: cKDTree,
  161. r: _ArrayLike0D,
  162. p: float = ...,
  163. weights: _WeightType = ...,
  164. cumulative: bool = ...,
  165. ) -> np.float64: ...
  166. @overload
  167. def count_neighbors( # type: ignore[misc]
  168. self,
  169. other: cKDTree,
  170. r: npt.ArrayLike,
  171. p: float = ...,
  172. weights: None | Tuple[None, None] = ...,
  173. cumulative: bool = ...,
  174. ) -> npt.NDArray[np.intp]: ...
  175. @overload
  176. def count_neighbors(
  177. self,
  178. other: cKDTree,
  179. r: npt.ArrayLike,
  180. p: float = ...,
  181. weights: _WeightType = ...,
  182. cumulative: bool = ...,
  183. ) -> npt.NDArray[np.float64]: ...
  184. @overload
  185. def sparse_distance_matrix( # type: ignore[misc]
  186. self,
  187. other: cKDTree,
  188. max_distance: float,
  189. p: float = ...,
  190. output_type: Literal["dok_matrix"] = ...,
  191. ) -> dok_matrix: ...
  192. @overload
  193. def sparse_distance_matrix( # type: ignore[misc]
  194. self,
  195. other: cKDTree,
  196. max_distance: float,
  197. p: float = ...,
  198. output_type: Literal["coo_matrix"] = ...,
  199. ) -> coo_matrix: ...
  200. @overload
  201. def sparse_distance_matrix( # type: ignore[misc]
  202. self,
  203. other: cKDTree,
  204. max_distance: float,
  205. p: float = ...,
  206. output_type: Literal["dict"] = ...,
  207. ) -> Dict[Tuple[int, int], float]: ...
  208. @overload
  209. def sparse_distance_matrix(
  210. self,
  211. other: cKDTree,
  212. max_distance: float,
  213. p: float = ...,
  214. output_type: Literal["ndarray"] = ...,
  215. ) -> npt.NDArray[np.void]: ...