npyio.pyi 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. _MAX_REPR_ARRAY_COUNT: int
  65. # Represent `f` as a mutable property so we can access the type of `self`
  66. @property
  67. def f(self: _T) -> BagObj[_T]: ...
  68. @f.setter
  69. def f(self: _T, value: BagObj[_T]) -> None: ...
  70. def __init__(
  71. self,
  72. fid: IO[str],
  73. own_fid: bool = ...,
  74. allow_pickle: bool = ...,
  75. pickle_kwargs: None | Mapping[str, Any] = ...,
  76. ) -> None: ...
  77. def __enter__(self: _T) -> _T: ...
  78. def __exit__(
  79. self,
  80. exc_type: None | type[BaseException],
  81. exc_value: None | BaseException,
  82. traceback: None | types.TracebackType,
  83. /,
  84. ) -> None: ...
  85. def close(self) -> None: ...
  86. def __del__(self) -> None: ...
  87. def __iter__(self) -> Iterator[str]: ...
  88. def __len__(self) -> int: ...
  89. def __getitem__(self, key: str) -> NDArray[Any]: ...
  90. def __contains__(self, key: str) -> bool: ...
  91. def __repr__(self) -> str: ...
  92. # NOTE: Returns a `NpzFile` if file is a zip file;
  93. # returns an `ndarray`/`memmap` otherwise
  94. def load(
  95. file: str | bytes | os.PathLike[Any] | _SupportsReadSeek[bytes],
  96. mmap_mode: L[None, "r+", "r", "w+", "c"] = ...,
  97. allow_pickle: bool = ...,
  98. fix_imports: bool = ...,
  99. encoding: L["ASCII", "latin1", "bytes"] = ...,
  100. ) -> Any: ...
  101. def save(
  102. file: str | os.PathLike[str] | _SupportsWrite[bytes],
  103. arr: ArrayLike,
  104. allow_pickle: bool = ...,
  105. fix_imports: bool = ...,
  106. ) -> None: ...
  107. def savez(
  108. file: str | os.PathLike[str] | _SupportsWrite[bytes],
  109. *args: ArrayLike,
  110. **kwds: ArrayLike,
  111. ) -> None: ...
  112. def savez_compressed(
  113. file: str | os.PathLike[str] | _SupportsWrite[bytes],
  114. *args: ArrayLike,
  115. **kwds: ArrayLike,
  116. ) -> None: ...
  117. # File-like objects only have to implement `__iter__` and,
  118. # optionally, `encoding`
  119. @overload
  120. def loadtxt(
  121. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  122. dtype: None = ...,
  123. comments: None | str | Sequence[str] = ...,
  124. delimiter: None | str = ...,
  125. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  126. skiprows: int = ...,
  127. usecols: int | Sequence[int] = ...,
  128. unpack: bool = ...,
  129. ndmin: L[0, 1, 2] = ...,
  130. encoding: None | str = ...,
  131. max_rows: None | int = ...,
  132. *,
  133. quotechar: None | str = ...,
  134. like: None | _SupportsArrayFunc = ...
  135. ) -> NDArray[float64]: ...
  136. @overload
  137. def loadtxt(
  138. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  139. dtype: _DTypeLike[_SCT],
  140. comments: None | str | Sequence[str] = ...,
  141. delimiter: None | str = ...,
  142. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  143. skiprows: int = ...,
  144. usecols: int | Sequence[int] = ...,
  145. unpack: bool = ...,
  146. ndmin: L[0, 1, 2] = ...,
  147. encoding: None | str = ...,
  148. max_rows: None | int = ...,
  149. *,
  150. quotechar: None | str = ...,
  151. like: None | _SupportsArrayFunc = ...
  152. ) -> NDArray[_SCT]: ...
  153. @overload
  154. def loadtxt(
  155. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  156. dtype: DTypeLike,
  157. comments: None | str | Sequence[str] = ...,
  158. delimiter: None | str = ...,
  159. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  160. skiprows: int = ...,
  161. usecols: int | Sequence[int] = ...,
  162. unpack: bool = ...,
  163. ndmin: L[0, 1, 2] = ...,
  164. encoding: None | str = ...,
  165. max_rows: None | int = ...,
  166. *,
  167. quotechar: None | str = ...,
  168. like: None | _SupportsArrayFunc = ...
  169. ) -> NDArray[Any]: ...
  170. def savetxt(
  171. fname: str | os.PathLike[str] | _SupportsWrite[str] | _SupportsWrite[bytes],
  172. X: ArrayLike,
  173. fmt: str | Sequence[str] = ...,
  174. delimiter: str = ...,
  175. newline: str = ...,
  176. header: str = ...,
  177. footer: str = ...,
  178. comments: str = ...,
  179. encoding: None | str = ...,
  180. ) -> None: ...
  181. @overload
  182. def fromregex(
  183. file: str | os.PathLike[str] | _SupportsRead[str] | _SupportsRead[bytes],
  184. regexp: str | bytes | Pattern[Any],
  185. dtype: _DTypeLike[_SCT],
  186. encoding: None | str = ...
  187. ) -> NDArray[_SCT]: ...
  188. @overload
  189. def fromregex(
  190. file: str | os.PathLike[str] | _SupportsRead[str] | _SupportsRead[bytes],
  191. regexp: str | bytes | Pattern[Any],
  192. dtype: DTypeLike,
  193. encoding: None | str = ...
  194. ) -> NDArray[Any]: ...
  195. @overload
  196. def genfromtxt(
  197. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  198. dtype: None = ...,
  199. comments: str = ...,
  200. delimiter: None | str | int | Iterable[int] = ...,
  201. skip_header: int = ...,
  202. skip_footer: int = ...,
  203. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  204. missing_values: Any = ...,
  205. filling_values: Any = ...,
  206. usecols: None | Sequence[int] = ...,
  207. names: L[None, True] | str | Collection[str] = ...,
  208. excludelist: None | Sequence[str] = ...,
  209. deletechars: str = ...,
  210. replace_space: str = ...,
  211. autostrip: bool = ...,
  212. case_sensitive: bool | L['upper', 'lower'] = ...,
  213. defaultfmt: str = ...,
  214. unpack: None | bool = ...,
  215. usemask: bool = ...,
  216. loose: bool = ...,
  217. invalid_raise: bool = ...,
  218. max_rows: None | int = ...,
  219. encoding: str = ...,
  220. *,
  221. ndmin: L[0, 1, 2] = ...,
  222. like: None | _SupportsArrayFunc = ...,
  223. ) -> NDArray[Any]: ...
  224. @overload
  225. def genfromtxt(
  226. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  227. dtype: _DTypeLike[_SCT],
  228. comments: str = ...,
  229. delimiter: None | str | int | Iterable[int] = ...,
  230. skip_header: int = ...,
  231. skip_footer: int = ...,
  232. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  233. missing_values: Any = ...,
  234. filling_values: Any = ...,
  235. usecols: None | Sequence[int] = ...,
  236. names: L[None, True] | str | Collection[str] = ...,
  237. excludelist: None | Sequence[str] = ...,
  238. deletechars: str = ...,
  239. replace_space: str = ...,
  240. autostrip: bool = ...,
  241. case_sensitive: bool | L['upper', 'lower'] = ...,
  242. defaultfmt: str = ...,
  243. unpack: None | bool = ...,
  244. usemask: bool = ...,
  245. loose: bool = ...,
  246. invalid_raise: bool = ...,
  247. max_rows: None | int = ...,
  248. encoding: str = ...,
  249. *,
  250. ndmin: L[0, 1, 2] = ...,
  251. like: None | _SupportsArrayFunc = ...,
  252. ) -> NDArray[_SCT]: ...
  253. @overload
  254. def genfromtxt(
  255. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  256. dtype: DTypeLike,
  257. comments: str = ...,
  258. delimiter: None | str | int | Iterable[int] = ...,
  259. skip_header: int = ...,
  260. skip_footer: int = ...,
  261. converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
  262. missing_values: Any = ...,
  263. filling_values: Any = ...,
  264. usecols: None | Sequence[int] = ...,
  265. names: L[None, True] | str | Collection[str] = ...,
  266. excludelist: None | Sequence[str] = ...,
  267. deletechars: str = ...,
  268. replace_space: str = ...,
  269. autostrip: bool = ...,
  270. case_sensitive: bool | L['upper', 'lower'] = ...,
  271. defaultfmt: str = ...,
  272. unpack: None | bool = ...,
  273. usemask: bool = ...,
  274. loose: bool = ...,
  275. invalid_raise: bool = ...,
  276. max_rows: None | int = ...,
  277. encoding: str = ...,
  278. *,
  279. ndmin: L[0, 1, 2] = ...,
  280. like: None | _SupportsArrayFunc = ...,
  281. ) -> NDArray[Any]: ...
  282. @overload
  283. def recfromtxt(
  284. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  285. *,
  286. usemask: L[False] = ...,
  287. **kwargs: Any,
  288. ) -> recarray[Any, dtype[record]]: ...
  289. @overload
  290. def recfromtxt(
  291. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  292. *,
  293. usemask: L[True],
  294. **kwargs: Any,
  295. ) -> MaskedRecords[Any, dtype[void]]: ...
  296. @overload
  297. def recfromcsv(
  298. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  299. *,
  300. usemask: L[False] = ...,
  301. **kwargs: Any,
  302. ) -> recarray[Any, dtype[record]]: ...
  303. @overload
  304. def recfromcsv(
  305. fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
  306. *,
  307. usemask: L[True],
  308. **kwargs: Any,
  309. ) -> MaskedRecords[Any, dtype[void]]: ...