npyio.pyi 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import os
  2. import sys
  3. import zipfile
  4. import types
  5. from re import Pattern
  6. from collections.abc import Collection, Mapping, Iterator, Sequence, Callable, Iterable
  7. from typing import (
  8. Literal as L,
  9. Any,
  10. TypeVar,
  11. Generic,
  12. IO,
  13. overload,
  14. Protocol,
  15. )
  16. from numpy import (
  17. DataSource as DataSource,
  18. ndarray,
  19. recarray,
  20. dtype,
  21. generic,
  22. float64,
  23. void,
  24. record,
  25. )
  26. from numpy.ma.mrecords import MaskedRecords
  27. from numpy._typing import (
  28. ArrayLike,
  29. DTypeLike,
  30. NDArray,
  31. _DTypeLike,
  32. _SupportsArrayFunc,
  33. )
  34. from numpy.core.multiarray import (
  35. packbits as packbits,
  36. unpackbits as unpackbits,
  37. )
  38. _T = TypeVar("_T")
  39. _T_contra = TypeVar("_T_contra", contravariant=True)
  40. _T_co = TypeVar("_T_co", covariant=True)
  41. _SCT = TypeVar("_SCT", bound=generic)
  42. _CharType_co = TypeVar("_CharType_co", str, bytes, covariant=True)
  43. _CharType_contra = TypeVar("_CharType_contra", str, bytes, contravariant=True)
  44. class _SupportsGetItem(Protocol[_T_contra, _T_co]):
  45. def __getitem__(self, key: _T_contra, /) -> _T_co: ...
  46. class _SupportsRead(Protocol[_CharType_co]):
  47. def read(self) -> _CharType_co: ...
  48. class _SupportsReadSeek(Protocol[_CharType_co]):
  49. def read(self, n: int, /) -> _CharType_co: ...
  50. def seek(self, offset: int, whence: int, /) -> object: ...
  51. class _SupportsWrite(Protocol[_CharType_contra]):
  52. def write(self, s: _CharType_contra, /) -> object: ...
  53. __all__: list[str]
  54. class BagObj(Generic[_T_co]):
  55. def __init__(self, obj: _SupportsGetItem[str, _T_co]) -> None: ...
  56. def __getattribute__(self, key: str) -> _T_co: ...
  57. def __dir__(self) -> list[str]: ...
  58. class NpzFile(Mapping[str, NDArray[Any]]):
  59. zip: zipfile.ZipFile
  60. fid: None | IO[str]
  61. files: list[str]
  62. allow_pickle: bool
  63. pickle_kwargs: None | Mapping[str, Any]
  64. # Represent `f` as a mutable property so we can access the type of `self`
  65. @property
  66. def f(self: _T) -> BagObj[_T]: ...
  67. @f.setter
  68. def f(self: _T, value: BagObj[_T]) -> None: ...
  69. def __init__(
  70. self,
  71. fid: IO[str],
  72. own_fid: bool = ...,
  73. allow_pickle: bool = ...,
  74. pickle_kwargs: None | Mapping[str, Any] = ...,
  75. ) -> None: ...
  76. def __enter__(self: _T) -> _T: ...
  77. def __exit__(
  78. self,
  79. exc_type: None | type[BaseException],
  80. exc_value: None | BaseException,
  81. traceback: None | types.TracebackType,
  82. /,
  83. ) -> None: ...
  84. def close(self) -> None: ...
  85. def __del__(self) -> None: ...
  86. def __iter__(self) -> Iterator[str]: ...
  87. def __len__(self) -> int: ...
  88. def __getitem__(self, key: str) -> NDArray[Any]: ...
  89. # NOTE: Returns a `NpzFile` if file is a zip file;
  90. # returns an `ndarray`/`memmap` otherwise
  91. def load(
  92. file: str | bytes | os.PathLike[Any] | _SupportsReadSeek[bytes],
  93. mmap_mode: L[None, "r+", "r", "w+", "c"] = ...,
  94. allow_pickle: bool = ...,
  95. fix_imports: bool = ...,
  96. encoding: L["ASCII", "latin1", "bytes"] = ...,
  97. ) -> Any: ...
  98. def save(
  99. file: str | os.PathLike[str] | _SupportsWrite[bytes],
  100. arr: ArrayLike,
  101. allow_pickle: bool = ...,
  102. fix_imports: bool = ...,
  103. ) -> None: ...
  104. def savez(
  105. file: str | os.PathLike[str] | _SupportsWrite[bytes],
  106. *args: ArrayLike,
  107. **kwds: ArrayLike,
  108. ) -> None: ...
  109. def savez_compressed(
  110. file: str | os.PathLike[str] | _SupportsWrite[bytes],
  111. *args: ArrayLike,
  112. **kwds: ArrayLike,
  113. ) -> None: ...
  114. # File-like objects only have to implement `__iter__` and,
  115. # optionally, `encoding`
  116. @overload
  117. def loadtxt(
  118. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  119. dtype: None = ...,
  120. comments: None | str | Sequence[str] = ...,
  121. delimiter: None | str = ...,
  122. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  123. skiprows: int = ...,
  124. usecols: int | Sequence[int] = ...,
  125. unpack: bool = ...,
  126. ndmin: L[0, 1, 2] = ...,
  127. encoding: None | str = ...,
  128. max_rows: None | int = ...,
  129. *,
  130. quotechar: None | str = ...,
  131. like: None | _SupportsArrayFunc = ...
  132. ) -> NDArray[float64]: ...
  133. @overload
  134. def loadtxt(
  135. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  136. dtype: _DTypeLike[_SCT],
  137. comments: None | str | Sequence[str] = ...,
  138. delimiter: None | str = ...,
  139. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  140. skiprows: int = ...,
  141. usecols: int | Sequence[int] = ...,
  142. unpack: bool = ...,
  143. ndmin: L[0, 1, 2] = ...,
  144. encoding: None | str = ...,
  145. max_rows: None | int = ...,
  146. *,
  147. quotechar: None | str = ...,
  148. like: None | _SupportsArrayFunc = ...
  149. ) -> NDArray[_SCT]: ...
  150. @overload
  151. def loadtxt(
  152. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  153. dtype: DTypeLike,
  154. comments: None | str | Sequence[str] = ...,
  155. delimiter: None | str = ...,
  156. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  157. skiprows: int = ...,
  158. usecols: int | Sequence[int] = ...,
  159. unpack: bool = ...,
  160. ndmin: L[0, 1, 2] = ...,
  161. encoding: None | str = ...,
  162. max_rows: None | int = ...,
  163. *,
  164. quotechar: None | str = ...,
  165. like: None | _SupportsArrayFunc = ...
  166. ) -> NDArray[Any]: ...
  167. def savetxt(
  168. fname: str | os.PathLike[str] | _SupportsWrite[str] | _SupportsWrite[bytes],
  169. X: ArrayLike,
  170. fmt: str | Sequence[str] = ...,
  171. delimiter: str = ...,
  172. newline: str = ...,
  173. header: str = ...,
  174. footer: str = ...,
  175. comments: str = ...,
  176. encoding: None | str = ...,
  177. ) -> None: ...
  178. @overload
  179. def fromregex(
  180. file: str | os.PathLike[str] | _SupportsRead[str] | _SupportsRead[bytes],
  181. regexp: str | bytes | Pattern[Any],
  182. dtype: _DTypeLike[_SCT],
  183. encoding: None | str = ...
  184. ) -> NDArray[_SCT]: ...
  185. @overload
  186. def fromregex(
  187. file: str | os.PathLike[str] | _SupportsRead[str] | _SupportsRead[bytes],
  188. regexp: str | bytes | Pattern[Any],
  189. dtype: DTypeLike,
  190. encoding: None | str = ...
  191. ) -> NDArray[Any]: ...
  192. @overload
  193. def genfromtxt(
  194. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  195. dtype: None = ...,
  196. comments: str = ...,
  197. delimiter: None | str | int | Iterable[int] = ...,
  198. skip_header: int = ...,
  199. skip_footer: int = ...,
  200. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  201. missing_values: Any = ...,
  202. filling_values: Any = ...,
  203. usecols: None | Sequence[int] = ...,
  204. names: L[None, True] | str | Collection[str] = ...,
  205. excludelist: None | Sequence[str] = ...,
  206. deletechars: str = ...,
  207. replace_space: str = ...,
  208. autostrip: bool = ...,
  209. case_sensitive: bool | L['upper', 'lower'] = ...,
  210. defaultfmt: str = ...,
  211. unpack: None | bool = ...,
  212. usemask: bool = ...,
  213. loose: bool = ...,
  214. invalid_raise: bool = ...,
  215. max_rows: None | int = ...,
  216. encoding: str = ...,
  217. *,
  218. ndmin: L[0, 1, 2] = ...,
  219. like: None | _SupportsArrayFunc = ...,
  220. ) -> NDArray[float64]: ...
  221. @overload
  222. def genfromtxt(
  223. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  224. dtype: _DTypeLike[_SCT],
  225. comments: str = ...,
  226. delimiter: None | str | int | Iterable[int] = ...,
  227. skip_header: int = ...,
  228. skip_footer: int = ...,
  229. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  230. missing_values: Any = ...,
  231. filling_values: Any = ...,
  232. usecols: None | Sequence[int] = ...,
  233. names: L[None, True] | str | Collection[str] = ...,
  234. excludelist: None | Sequence[str] = ...,
  235. deletechars: str = ...,
  236. replace_space: str = ...,
  237. autostrip: bool = ...,
  238. case_sensitive: bool | L['upper', 'lower'] = ...,
  239. defaultfmt: str = ...,
  240. unpack: None | bool = ...,
  241. usemask: bool = ...,
  242. loose: bool = ...,
  243. invalid_raise: bool = ...,
  244. max_rows: None | int = ...,
  245. encoding: str = ...,
  246. *,
  247. ndmin: L[0, 1, 2] = ...,
  248. like: None | _SupportsArrayFunc = ...,
  249. ) -> NDArray[_SCT]: ...
  250. @overload
  251. def genfromtxt(
  252. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  253. dtype: DTypeLike,
  254. comments: str = ...,
  255. delimiter: None | str | int | Iterable[int] = ...,
  256. skip_header: int = ...,
  257. skip_footer: int = ...,
  258. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  259. missing_values: Any = ...,
  260. filling_values: Any = ...,
  261. usecols: None | Sequence[int] = ...,
  262. names: L[None, True] | str | Collection[str] = ...,
  263. excludelist: None | Sequence[str] = ...,
  264. deletechars: str = ...,
  265. replace_space: str = ...,
  266. autostrip: bool = ...,
  267. case_sensitive: bool | L['upper', 'lower'] = ...,
  268. defaultfmt: str = ...,
  269. unpack: None | bool = ...,
  270. usemask: bool = ...,
  271. loose: bool = ...,
  272. invalid_raise: bool = ...,
  273. max_rows: None | int = ...,
  274. encoding: str = ...,
  275. *,
  276. ndmin: L[0, 1, 2] = ...,
  277. like: None | _SupportsArrayFunc = ...,
  278. ) -> NDArray[Any]: ...
  279. @overload
  280. def recfromtxt(
  281. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  282. *,
  283. usemask: L[False] = ...,
  284. **kwargs: Any,
  285. ) -> recarray[Any, dtype[record]]: ...
  286. @overload
  287. def recfromtxt(
  288. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  289. *,
  290. usemask: L[True],
  291. **kwargs: Any,
  292. ) -> MaskedRecords[Any, dtype[void]]: ...
  293. @overload
  294. def recfromcsv(
  295. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  296. *,
  297. usemask: L[False] = ...,
  298. **kwargs: Any,
  299. ) -> recarray[Any, dtype[record]]: ...
  300. @overload
  301. def recfromcsv(
  302. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  303. *,
  304. usemask: L[True],
  305. **kwargs: Any,
  306. ) -> MaskedRecords[Any, dtype[void]]: ...