_npyio_impl.pyi 9.1 KB

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