numpy 2.4.1__pp311-pypy311_pp73-macosx_14_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1039) hide show
  1. numpy/__config__.py +170 -0
  2. numpy/__config__.pyi +108 -0
  3. numpy/__init__.cython-30.pxd +1242 -0
  4. numpy/__init__.pxd +1155 -0
  5. numpy/__init__.py +942 -0
  6. numpy/__init__.pyi +6202 -0
  7. numpy/_array_api_info.py +346 -0
  8. numpy/_array_api_info.pyi +206 -0
  9. numpy/_configtool.py +39 -0
  10. numpy/_configtool.pyi +1 -0
  11. numpy/_core/__init__.py +201 -0
  12. numpy/_core/__init__.pyi +666 -0
  13. numpy/_core/_add_newdocs.py +7151 -0
  14. numpy/_core/_add_newdocs.pyi +2 -0
  15. numpy/_core/_add_newdocs_scalars.py +381 -0
  16. numpy/_core/_add_newdocs_scalars.pyi +16 -0
  17. numpy/_core/_asarray.py +130 -0
  18. numpy/_core/_asarray.pyi +43 -0
  19. numpy/_core/_dtype.py +366 -0
  20. numpy/_core/_dtype.pyi +56 -0
  21. numpy/_core/_dtype_ctypes.py +120 -0
  22. numpy/_core/_dtype_ctypes.pyi +83 -0
  23. numpy/_core/_exceptions.py +162 -0
  24. numpy/_core/_exceptions.pyi +54 -0
  25. numpy/_core/_internal.py +968 -0
  26. numpy/_core/_internal.pyi +61 -0
  27. numpy/_core/_methods.py +252 -0
  28. numpy/_core/_methods.pyi +22 -0
  29. numpy/_core/_multiarray_tests.pypy311-pp73-darwin.so +0 -0
  30. numpy/_core/_multiarray_umath.pypy311-pp73-darwin.so +0 -0
  31. numpy/_core/_operand_flag_tests.pypy311-pp73-darwin.so +0 -0
  32. numpy/_core/_rational_tests.pypy311-pp73-darwin.so +0 -0
  33. numpy/_core/_simd.pyi +35 -0
  34. numpy/_core/_simd.pypy311-pp73-darwin.so +0 -0
  35. numpy/_core/_string_helpers.py +100 -0
  36. numpy/_core/_string_helpers.pyi +12 -0
  37. numpy/_core/_struct_ufunc_tests.pypy311-pp73-darwin.so +0 -0
  38. numpy/_core/_type_aliases.py +131 -0
  39. numpy/_core/_type_aliases.pyi +86 -0
  40. numpy/_core/_ufunc_config.py +515 -0
  41. numpy/_core/_ufunc_config.pyi +69 -0
  42. numpy/_core/_umath_tests.pyi +47 -0
  43. numpy/_core/_umath_tests.pypy311-pp73-darwin.so +0 -0
  44. numpy/_core/arrayprint.py +1779 -0
  45. numpy/_core/arrayprint.pyi +158 -0
  46. numpy/_core/cversions.py +13 -0
  47. numpy/_core/defchararray.py +1414 -0
  48. numpy/_core/defchararray.pyi +1150 -0
  49. numpy/_core/einsumfunc.py +1650 -0
  50. numpy/_core/einsumfunc.pyi +184 -0
  51. numpy/_core/fromnumeric.py +4233 -0
  52. numpy/_core/fromnumeric.pyi +1735 -0
  53. numpy/_core/function_base.py +547 -0
  54. numpy/_core/function_base.pyi +276 -0
  55. numpy/_core/getlimits.py +462 -0
  56. numpy/_core/getlimits.pyi +124 -0
  57. numpy/_core/include/numpy/__multiarray_api.c +376 -0
  58. numpy/_core/include/numpy/__multiarray_api.h +1628 -0
  59. numpy/_core/include/numpy/__ufunc_api.c +55 -0
  60. numpy/_core/include/numpy/__ufunc_api.h +349 -0
  61. numpy/_core/include/numpy/_neighborhood_iterator_imp.h +90 -0
  62. numpy/_core/include/numpy/_numpyconfig.h +33 -0
  63. numpy/_core/include/numpy/_public_dtype_api_table.h +86 -0
  64. numpy/_core/include/numpy/arrayobject.h +7 -0
  65. numpy/_core/include/numpy/arrayscalars.h +198 -0
  66. numpy/_core/include/numpy/dtype_api.h +547 -0
  67. numpy/_core/include/numpy/halffloat.h +70 -0
  68. numpy/_core/include/numpy/ndarrayobject.h +304 -0
  69. numpy/_core/include/numpy/ndarraytypes.h +1982 -0
  70. numpy/_core/include/numpy/npy_2_compat.h +249 -0
  71. numpy/_core/include/numpy/npy_2_complexcompat.h +28 -0
  72. numpy/_core/include/numpy/npy_3kcompat.h +374 -0
  73. numpy/_core/include/numpy/npy_common.h +989 -0
  74. numpy/_core/include/numpy/npy_cpu.h +126 -0
  75. numpy/_core/include/numpy/npy_endian.h +79 -0
  76. numpy/_core/include/numpy/npy_math.h +602 -0
  77. numpy/_core/include/numpy/npy_no_deprecated_api.h +20 -0
  78. numpy/_core/include/numpy/npy_os.h +42 -0
  79. numpy/_core/include/numpy/numpyconfig.h +185 -0
  80. numpy/_core/include/numpy/random/LICENSE.txt +21 -0
  81. numpy/_core/include/numpy/random/bitgen.h +20 -0
  82. numpy/_core/include/numpy/random/distributions.h +209 -0
  83. numpy/_core/include/numpy/random/libdivide.h +2079 -0
  84. numpy/_core/include/numpy/ufuncobject.h +343 -0
  85. numpy/_core/include/numpy/utils.h +37 -0
  86. numpy/_core/lib/libnpymath.a +0 -0
  87. numpy/_core/lib/npy-pkg-config/mlib.ini +12 -0
  88. numpy/_core/lib/npy-pkg-config/npymath.ini +20 -0
  89. numpy/_core/lib/pkgconfig/numpy.pc +7 -0
  90. numpy/_core/memmap.py +363 -0
  91. numpy/_core/memmap.pyi +3 -0
  92. numpy/_core/multiarray.py +1740 -0
  93. numpy/_core/multiarray.pyi +1316 -0
  94. numpy/_core/numeric.py +2758 -0
  95. numpy/_core/numeric.pyi +1276 -0
  96. numpy/_core/numerictypes.py +633 -0
  97. numpy/_core/numerictypes.pyi +196 -0
  98. numpy/_core/overrides.py +188 -0
  99. numpy/_core/overrides.pyi +47 -0
  100. numpy/_core/printoptions.py +32 -0
  101. numpy/_core/printoptions.pyi +28 -0
  102. numpy/_core/records.py +1088 -0
  103. numpy/_core/records.pyi +340 -0
  104. numpy/_core/shape_base.py +996 -0
  105. numpy/_core/shape_base.pyi +182 -0
  106. numpy/_core/strings.py +1813 -0
  107. numpy/_core/strings.pyi +536 -0
  108. numpy/_core/tests/_locales.py +72 -0
  109. numpy/_core/tests/_natype.py +144 -0
  110. numpy/_core/tests/data/astype_copy.pkl +0 -0
  111. numpy/_core/tests/data/generate_umath_validation_data.cpp +170 -0
  112. numpy/_core/tests/data/recarray_from_file.fits +0 -0
  113. numpy/_core/tests/data/umath-validation-set-README.txt +15 -0
  114. numpy/_core/tests/data/umath-validation-set-arccos.csv +1429 -0
  115. numpy/_core/tests/data/umath-validation-set-arccosh.csv +1429 -0
  116. numpy/_core/tests/data/umath-validation-set-arcsin.csv +1429 -0
  117. numpy/_core/tests/data/umath-validation-set-arcsinh.csv +1429 -0
  118. numpy/_core/tests/data/umath-validation-set-arctan.csv +1429 -0
  119. numpy/_core/tests/data/umath-validation-set-arctanh.csv +1429 -0
  120. numpy/_core/tests/data/umath-validation-set-cbrt.csv +1429 -0
  121. numpy/_core/tests/data/umath-validation-set-cos.csv +1375 -0
  122. numpy/_core/tests/data/umath-validation-set-cosh.csv +1429 -0
  123. numpy/_core/tests/data/umath-validation-set-exp.csv +412 -0
  124. numpy/_core/tests/data/umath-validation-set-exp2.csv +1429 -0
  125. numpy/_core/tests/data/umath-validation-set-expm1.csv +1429 -0
  126. numpy/_core/tests/data/umath-validation-set-log.csv +271 -0
  127. numpy/_core/tests/data/umath-validation-set-log10.csv +1629 -0
  128. numpy/_core/tests/data/umath-validation-set-log1p.csv +1429 -0
  129. numpy/_core/tests/data/umath-validation-set-log2.csv +1629 -0
  130. numpy/_core/tests/data/umath-validation-set-sin.csv +1370 -0
  131. numpy/_core/tests/data/umath-validation-set-sinh.csv +1429 -0
  132. numpy/_core/tests/data/umath-validation-set-tan.csv +1429 -0
  133. numpy/_core/tests/data/umath-validation-set-tanh.csv +1429 -0
  134. numpy/_core/tests/examples/cython/checks.pyx +373 -0
  135. numpy/_core/tests/examples/cython/meson.build +43 -0
  136. numpy/_core/tests/examples/cython/setup.py +39 -0
  137. numpy/_core/tests/examples/limited_api/limited_api1.c +17 -0
  138. numpy/_core/tests/examples/limited_api/limited_api2.pyx +11 -0
  139. numpy/_core/tests/examples/limited_api/limited_api_latest.c +19 -0
  140. numpy/_core/tests/examples/limited_api/meson.build +59 -0
  141. numpy/_core/tests/examples/limited_api/setup.py +24 -0
  142. numpy/_core/tests/test__exceptions.py +90 -0
  143. numpy/_core/tests/test_abc.py +54 -0
  144. numpy/_core/tests/test_api.py +655 -0
  145. numpy/_core/tests/test_argparse.py +90 -0
  146. numpy/_core/tests/test_array_api_info.py +113 -0
  147. numpy/_core/tests/test_array_coercion.py +928 -0
  148. numpy/_core/tests/test_array_interface.py +222 -0
  149. numpy/_core/tests/test_arraymethod.py +84 -0
  150. numpy/_core/tests/test_arrayobject.py +75 -0
  151. numpy/_core/tests/test_arrayprint.py +1324 -0
  152. numpy/_core/tests/test_casting_floatingpoint_errors.py +154 -0
  153. numpy/_core/tests/test_casting_unittests.py +955 -0
  154. numpy/_core/tests/test_conversion_utils.py +209 -0
  155. numpy/_core/tests/test_cpu_dispatcher.py +48 -0
  156. numpy/_core/tests/test_cpu_features.py +450 -0
  157. numpy/_core/tests/test_custom_dtypes.py +393 -0
  158. numpy/_core/tests/test_cython.py +352 -0
  159. numpy/_core/tests/test_datetime.py +2792 -0
  160. numpy/_core/tests/test_defchararray.py +858 -0
  161. numpy/_core/tests/test_deprecations.py +460 -0
  162. numpy/_core/tests/test_dlpack.py +190 -0
  163. numpy/_core/tests/test_dtype.py +2110 -0
  164. numpy/_core/tests/test_einsum.py +1351 -0
  165. numpy/_core/tests/test_errstate.py +131 -0
  166. numpy/_core/tests/test_extint128.py +217 -0
  167. numpy/_core/tests/test_finfo.py +86 -0
  168. numpy/_core/tests/test_function_base.py +504 -0
  169. numpy/_core/tests/test_getlimits.py +171 -0
  170. numpy/_core/tests/test_half.py +593 -0
  171. numpy/_core/tests/test_hashtable.py +36 -0
  172. numpy/_core/tests/test_indexerrors.py +122 -0
  173. numpy/_core/tests/test_indexing.py +1692 -0
  174. numpy/_core/tests/test_item_selection.py +167 -0
  175. numpy/_core/tests/test_limited_api.py +102 -0
  176. numpy/_core/tests/test_longdouble.py +370 -0
  177. numpy/_core/tests/test_mem_overlap.py +933 -0
  178. numpy/_core/tests/test_mem_policy.py +453 -0
  179. numpy/_core/tests/test_memmap.py +248 -0
  180. numpy/_core/tests/test_multiarray.py +11008 -0
  181. numpy/_core/tests/test_multiprocessing.py +55 -0
  182. numpy/_core/tests/test_multithreading.py +377 -0
  183. numpy/_core/tests/test_nditer.py +3533 -0
  184. numpy/_core/tests/test_nep50_promotions.py +287 -0
  185. numpy/_core/tests/test_numeric.py +4295 -0
  186. numpy/_core/tests/test_numerictypes.py +650 -0
  187. numpy/_core/tests/test_overrides.py +800 -0
  188. numpy/_core/tests/test_print.py +202 -0
  189. numpy/_core/tests/test_protocols.py +46 -0
  190. numpy/_core/tests/test_records.py +544 -0
  191. numpy/_core/tests/test_regression.py +2677 -0
  192. numpy/_core/tests/test_scalar_ctors.py +203 -0
  193. numpy/_core/tests/test_scalar_methods.py +328 -0
  194. numpy/_core/tests/test_scalarbuffer.py +153 -0
  195. numpy/_core/tests/test_scalarinherit.py +105 -0
  196. numpy/_core/tests/test_scalarmath.py +1168 -0
  197. numpy/_core/tests/test_scalarprint.py +403 -0
  198. numpy/_core/tests/test_shape_base.py +904 -0
  199. numpy/_core/tests/test_simd.py +1345 -0
  200. numpy/_core/tests/test_simd_module.py +105 -0
  201. numpy/_core/tests/test_stringdtype.py +1855 -0
  202. numpy/_core/tests/test_strings.py +1523 -0
  203. numpy/_core/tests/test_ufunc.py +3405 -0
  204. numpy/_core/tests/test_umath.py +4962 -0
  205. numpy/_core/tests/test_umath_accuracy.py +132 -0
  206. numpy/_core/tests/test_umath_complex.py +631 -0
  207. numpy/_core/tests/test_unicode.py +369 -0
  208. numpy/_core/umath.py +60 -0
  209. numpy/_core/umath.pyi +232 -0
  210. numpy/_distributor_init.py +15 -0
  211. numpy/_distributor_init.pyi +1 -0
  212. numpy/_expired_attrs_2_0.py +78 -0
  213. numpy/_expired_attrs_2_0.pyi +61 -0
  214. numpy/_globals.py +121 -0
  215. numpy/_globals.pyi +17 -0
  216. numpy/_pyinstaller/__init__.py +0 -0
  217. numpy/_pyinstaller/__init__.pyi +0 -0
  218. numpy/_pyinstaller/hook-numpy.py +36 -0
  219. numpy/_pyinstaller/hook-numpy.pyi +6 -0
  220. numpy/_pyinstaller/tests/__init__.py +16 -0
  221. numpy/_pyinstaller/tests/pyinstaller-smoke.py +32 -0
  222. numpy/_pyinstaller/tests/test_pyinstaller.py +35 -0
  223. numpy/_pytesttester.py +201 -0
  224. numpy/_pytesttester.pyi +18 -0
  225. numpy/_typing/__init__.py +173 -0
  226. numpy/_typing/_add_docstring.py +153 -0
  227. numpy/_typing/_array_like.py +106 -0
  228. numpy/_typing/_char_codes.py +213 -0
  229. numpy/_typing/_dtype_like.py +114 -0
  230. numpy/_typing/_extended_precision.py +15 -0
  231. numpy/_typing/_nbit.py +19 -0
  232. numpy/_typing/_nbit_base.py +94 -0
  233. numpy/_typing/_nbit_base.pyi +39 -0
  234. numpy/_typing/_nested_sequence.py +79 -0
  235. numpy/_typing/_scalars.py +20 -0
  236. numpy/_typing/_shape.py +8 -0
  237. numpy/_typing/_ufunc.py +7 -0
  238. numpy/_typing/_ufunc.pyi +975 -0
  239. numpy/_utils/__init__.py +95 -0
  240. numpy/_utils/__init__.pyi +28 -0
  241. numpy/_utils/_convertions.py +18 -0
  242. numpy/_utils/_convertions.pyi +4 -0
  243. numpy/_utils/_inspect.py +192 -0
  244. numpy/_utils/_inspect.pyi +70 -0
  245. numpy/_utils/_pep440.py +486 -0
  246. numpy/_utils/_pep440.pyi +118 -0
  247. numpy/char/__init__.py +2 -0
  248. numpy/char/__init__.pyi +111 -0
  249. numpy/conftest.py +248 -0
  250. numpy/core/__init__.py +33 -0
  251. numpy/core/__init__.pyi +0 -0
  252. numpy/core/_dtype.py +10 -0
  253. numpy/core/_dtype.pyi +0 -0
  254. numpy/core/_dtype_ctypes.py +10 -0
  255. numpy/core/_dtype_ctypes.pyi +0 -0
  256. numpy/core/_internal.py +27 -0
  257. numpy/core/_multiarray_umath.py +57 -0
  258. numpy/core/_utils.py +21 -0
  259. numpy/core/arrayprint.py +10 -0
  260. numpy/core/defchararray.py +10 -0
  261. numpy/core/einsumfunc.py +10 -0
  262. numpy/core/fromnumeric.py +10 -0
  263. numpy/core/function_base.py +10 -0
  264. numpy/core/getlimits.py +10 -0
  265. numpy/core/multiarray.py +25 -0
  266. numpy/core/numeric.py +12 -0
  267. numpy/core/numerictypes.py +10 -0
  268. numpy/core/overrides.py +10 -0
  269. numpy/core/overrides.pyi +7 -0
  270. numpy/core/records.py +10 -0
  271. numpy/core/shape_base.py +10 -0
  272. numpy/core/umath.py +10 -0
  273. numpy/ctypeslib/__init__.py +13 -0
  274. numpy/ctypeslib/__init__.pyi +15 -0
  275. numpy/ctypeslib/_ctypeslib.py +603 -0
  276. numpy/ctypeslib/_ctypeslib.pyi +236 -0
  277. numpy/distutils/__init__.py +64 -0
  278. numpy/distutils/__init__.pyi +4 -0
  279. numpy/distutils/__pycache__/conv_template.pypy311.pyc +0 -0
  280. numpy/distutils/_shell_utils.py +87 -0
  281. numpy/distutils/armccompiler.py +26 -0
  282. numpy/distutils/ccompiler.py +826 -0
  283. numpy/distutils/ccompiler_opt.py +2668 -0
  284. numpy/distutils/checks/cpu_asimd.c +27 -0
  285. numpy/distutils/checks/cpu_asimddp.c +16 -0
  286. numpy/distutils/checks/cpu_asimdfhm.c +19 -0
  287. numpy/distutils/checks/cpu_asimdhp.c +15 -0
  288. numpy/distutils/checks/cpu_avx.c +20 -0
  289. numpy/distutils/checks/cpu_avx2.c +20 -0
  290. numpy/distutils/checks/cpu_avx512_clx.c +22 -0
  291. numpy/distutils/checks/cpu_avx512_cnl.c +24 -0
  292. numpy/distutils/checks/cpu_avx512_icl.c +26 -0
  293. numpy/distutils/checks/cpu_avx512_knl.c +25 -0
  294. numpy/distutils/checks/cpu_avx512_knm.c +30 -0
  295. numpy/distutils/checks/cpu_avx512_skx.c +26 -0
  296. numpy/distutils/checks/cpu_avx512_spr.c +26 -0
  297. numpy/distutils/checks/cpu_avx512cd.c +20 -0
  298. numpy/distutils/checks/cpu_avx512f.c +20 -0
  299. numpy/distutils/checks/cpu_f16c.c +22 -0
  300. numpy/distutils/checks/cpu_fma3.c +22 -0
  301. numpy/distutils/checks/cpu_fma4.c +13 -0
  302. numpy/distutils/checks/cpu_lsx.c +11 -0
  303. numpy/distutils/checks/cpu_neon.c +19 -0
  304. numpy/distutils/checks/cpu_neon_fp16.c +11 -0
  305. numpy/distutils/checks/cpu_neon_vfpv4.c +21 -0
  306. numpy/distutils/checks/cpu_popcnt.c +32 -0
  307. numpy/distutils/checks/cpu_rvv.c +13 -0
  308. numpy/distutils/checks/cpu_sse.c +20 -0
  309. numpy/distutils/checks/cpu_sse2.c +20 -0
  310. numpy/distutils/checks/cpu_sse3.c +20 -0
  311. numpy/distutils/checks/cpu_sse41.c +20 -0
  312. numpy/distutils/checks/cpu_sse42.c +20 -0
  313. numpy/distutils/checks/cpu_ssse3.c +20 -0
  314. numpy/distutils/checks/cpu_sve.c +14 -0
  315. numpy/distutils/checks/cpu_vsx.c +21 -0
  316. numpy/distutils/checks/cpu_vsx2.c +13 -0
  317. numpy/distutils/checks/cpu_vsx3.c +13 -0
  318. numpy/distutils/checks/cpu_vsx4.c +14 -0
  319. numpy/distutils/checks/cpu_vx.c +16 -0
  320. numpy/distutils/checks/cpu_vxe.c +25 -0
  321. numpy/distutils/checks/cpu_vxe2.c +21 -0
  322. numpy/distutils/checks/cpu_xop.c +12 -0
  323. numpy/distutils/checks/extra_avx512bw_mask.c +18 -0
  324. numpy/distutils/checks/extra_avx512dq_mask.c +16 -0
  325. numpy/distutils/checks/extra_avx512f_reduce.c +41 -0
  326. numpy/distutils/checks/extra_vsx3_half_double.c +12 -0
  327. numpy/distutils/checks/extra_vsx4_mma.c +21 -0
  328. numpy/distutils/checks/extra_vsx_asm.c +36 -0
  329. numpy/distutils/checks/test_flags.c +1 -0
  330. numpy/distutils/command/__init__.py +41 -0
  331. numpy/distutils/command/autodist.py +148 -0
  332. numpy/distutils/command/bdist_rpm.py +22 -0
  333. numpy/distutils/command/build.py +62 -0
  334. numpy/distutils/command/build_clib.py +469 -0
  335. numpy/distutils/command/build_ext.py +752 -0
  336. numpy/distutils/command/build_py.py +31 -0
  337. numpy/distutils/command/build_scripts.py +49 -0
  338. numpy/distutils/command/build_src.py +773 -0
  339. numpy/distutils/command/config.py +516 -0
  340. numpy/distutils/command/config_compiler.py +126 -0
  341. numpy/distutils/command/develop.py +15 -0
  342. numpy/distutils/command/egg_info.py +25 -0
  343. numpy/distutils/command/install.py +79 -0
  344. numpy/distutils/command/install_clib.py +40 -0
  345. numpy/distutils/command/install_data.py +24 -0
  346. numpy/distutils/command/install_headers.py +25 -0
  347. numpy/distutils/command/sdist.py +27 -0
  348. numpy/distutils/conv_template.py +329 -0
  349. numpy/distutils/core.py +215 -0
  350. numpy/distutils/cpuinfo.py +683 -0
  351. numpy/distutils/exec_command.py +315 -0
  352. numpy/distutils/extension.py +101 -0
  353. numpy/distutils/fcompiler/__init__.py +1035 -0
  354. numpy/distutils/fcompiler/absoft.py +158 -0
  355. numpy/distutils/fcompiler/arm.py +71 -0
  356. numpy/distutils/fcompiler/compaq.py +120 -0
  357. numpy/distutils/fcompiler/environment.py +88 -0
  358. numpy/distutils/fcompiler/fujitsu.py +46 -0
  359. numpy/distutils/fcompiler/g95.py +42 -0
  360. numpy/distutils/fcompiler/gnu.py +555 -0
  361. numpy/distutils/fcompiler/hpux.py +41 -0
  362. numpy/distutils/fcompiler/ibm.py +97 -0
  363. numpy/distutils/fcompiler/intel.py +211 -0
  364. numpy/distutils/fcompiler/lahey.py +45 -0
  365. numpy/distutils/fcompiler/mips.py +54 -0
  366. numpy/distutils/fcompiler/nag.py +87 -0
  367. numpy/distutils/fcompiler/none.py +28 -0
  368. numpy/distutils/fcompiler/nv.py +53 -0
  369. numpy/distutils/fcompiler/pathf95.py +33 -0
  370. numpy/distutils/fcompiler/pg.py +128 -0
  371. numpy/distutils/fcompiler/sun.py +51 -0
  372. numpy/distutils/fcompiler/vast.py +52 -0
  373. numpy/distutils/from_template.py +261 -0
  374. numpy/distutils/fujitsuccompiler.py +28 -0
  375. numpy/distutils/intelccompiler.py +106 -0
  376. numpy/distutils/lib2def.py +116 -0
  377. numpy/distutils/line_endings.py +77 -0
  378. numpy/distutils/log.py +111 -0
  379. numpy/distutils/mingw/gfortran_vs2003_hack.c +6 -0
  380. numpy/distutils/mingw32ccompiler.py +620 -0
  381. numpy/distutils/misc_util.py +2484 -0
  382. numpy/distutils/msvc9compiler.py +63 -0
  383. numpy/distutils/msvccompiler.py +76 -0
  384. numpy/distutils/npy_pkg_config.py +441 -0
  385. numpy/distutils/numpy_distribution.py +17 -0
  386. numpy/distutils/pathccompiler.py +21 -0
  387. numpy/distutils/system_info.py +3267 -0
  388. numpy/distutils/tests/__init__.py +0 -0
  389. numpy/distutils/tests/test_build_ext.py +74 -0
  390. numpy/distutils/tests/test_ccompiler_opt.py +808 -0
  391. numpy/distutils/tests/test_ccompiler_opt_conf.py +176 -0
  392. numpy/distutils/tests/test_exec_command.py +217 -0
  393. numpy/distutils/tests/test_fcompiler.py +43 -0
  394. numpy/distutils/tests/test_fcompiler_gnu.py +55 -0
  395. numpy/distutils/tests/test_fcompiler_intel.py +30 -0
  396. numpy/distutils/tests/test_fcompiler_nagfor.py +22 -0
  397. numpy/distutils/tests/test_from_template.py +44 -0
  398. numpy/distutils/tests/test_log.py +34 -0
  399. numpy/distutils/tests/test_mingw32ccompiler.py +47 -0
  400. numpy/distutils/tests/test_misc_util.py +88 -0
  401. numpy/distutils/tests/test_npy_pkg_config.py +84 -0
  402. numpy/distutils/tests/test_shell_utils.py +79 -0
  403. numpy/distutils/tests/test_system_info.py +334 -0
  404. numpy/distutils/tests/utilities.py +90 -0
  405. numpy/distutils/unixccompiler.py +141 -0
  406. numpy/doc/ufuncs.py +138 -0
  407. numpy/dtypes.py +41 -0
  408. numpy/dtypes.pyi +630 -0
  409. numpy/exceptions.py +246 -0
  410. numpy/exceptions.pyi +27 -0
  411. numpy/f2py/__init__.py +86 -0
  412. numpy/f2py/__init__.pyi +5 -0
  413. numpy/f2py/__main__.py +5 -0
  414. numpy/f2py/__version__.py +1 -0
  415. numpy/f2py/__version__.pyi +1 -0
  416. numpy/f2py/_backends/__init__.py +9 -0
  417. numpy/f2py/_backends/__init__.pyi +5 -0
  418. numpy/f2py/_backends/_backend.py +44 -0
  419. numpy/f2py/_backends/_backend.pyi +46 -0
  420. numpy/f2py/_backends/_distutils.py +76 -0
  421. numpy/f2py/_backends/_distutils.pyi +13 -0
  422. numpy/f2py/_backends/_meson.py +244 -0
  423. numpy/f2py/_backends/_meson.pyi +62 -0
  424. numpy/f2py/_backends/meson.build.template +58 -0
  425. numpy/f2py/_isocbind.py +62 -0
  426. numpy/f2py/_isocbind.pyi +13 -0
  427. numpy/f2py/_src_pyf.py +247 -0
  428. numpy/f2py/_src_pyf.pyi +28 -0
  429. numpy/f2py/auxfuncs.py +1004 -0
  430. numpy/f2py/auxfuncs.pyi +262 -0
  431. numpy/f2py/capi_maps.py +811 -0
  432. numpy/f2py/capi_maps.pyi +33 -0
  433. numpy/f2py/cb_rules.py +665 -0
  434. numpy/f2py/cb_rules.pyi +17 -0
  435. numpy/f2py/cfuncs.py +1563 -0
  436. numpy/f2py/cfuncs.pyi +31 -0
  437. numpy/f2py/common_rules.py +143 -0
  438. numpy/f2py/common_rules.pyi +9 -0
  439. numpy/f2py/crackfortran.py +3725 -0
  440. numpy/f2py/crackfortran.pyi +266 -0
  441. numpy/f2py/diagnose.py +149 -0
  442. numpy/f2py/diagnose.pyi +1 -0
  443. numpy/f2py/f2py2e.py +788 -0
  444. numpy/f2py/f2py2e.pyi +74 -0
  445. numpy/f2py/f90mod_rules.py +269 -0
  446. numpy/f2py/f90mod_rules.pyi +16 -0
  447. numpy/f2py/func2subr.py +329 -0
  448. numpy/f2py/func2subr.pyi +7 -0
  449. numpy/f2py/rules.py +1629 -0
  450. numpy/f2py/rules.pyi +41 -0
  451. numpy/f2py/setup.cfg +3 -0
  452. numpy/f2py/src/fortranobject.c +1436 -0
  453. numpy/f2py/src/fortranobject.h +173 -0
  454. numpy/f2py/symbolic.py +1518 -0
  455. numpy/f2py/symbolic.pyi +219 -0
  456. numpy/f2py/tests/__init__.py +16 -0
  457. numpy/f2py/tests/src/abstract_interface/foo.f90 +34 -0
  458. numpy/f2py/tests/src/abstract_interface/gh18403_mod.f90 +6 -0
  459. numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c +235 -0
  460. numpy/f2py/tests/src/assumed_shape/.f2py_f2cmap +1 -0
  461. numpy/f2py/tests/src/assumed_shape/foo_free.f90 +34 -0
  462. numpy/f2py/tests/src/assumed_shape/foo_mod.f90 +41 -0
  463. numpy/f2py/tests/src/assumed_shape/foo_use.f90 +19 -0
  464. numpy/f2py/tests/src/assumed_shape/precision.f90 +4 -0
  465. numpy/f2py/tests/src/block_docstring/foo.f +6 -0
  466. numpy/f2py/tests/src/callback/foo.f +62 -0
  467. numpy/f2py/tests/src/callback/gh17797.f90 +7 -0
  468. numpy/f2py/tests/src/callback/gh18335.f90 +17 -0
  469. numpy/f2py/tests/src/callback/gh25211.f +10 -0
  470. numpy/f2py/tests/src/callback/gh25211.pyf +18 -0
  471. numpy/f2py/tests/src/callback/gh26681.f90 +18 -0
  472. numpy/f2py/tests/src/cli/gh_22819.pyf +6 -0
  473. numpy/f2py/tests/src/cli/hi77.f +3 -0
  474. numpy/f2py/tests/src/cli/hiworld.f90 +3 -0
  475. numpy/f2py/tests/src/common/block.f +11 -0
  476. numpy/f2py/tests/src/common/gh19161.f90 +10 -0
  477. numpy/f2py/tests/src/crackfortran/accesstype.f90 +13 -0
  478. numpy/f2py/tests/src/crackfortran/common_with_division.f +17 -0
  479. numpy/f2py/tests/src/crackfortran/data_common.f +8 -0
  480. numpy/f2py/tests/src/crackfortran/data_multiplier.f +5 -0
  481. numpy/f2py/tests/src/crackfortran/data_stmts.f90 +20 -0
  482. numpy/f2py/tests/src/crackfortran/data_with_comments.f +8 -0
  483. numpy/f2py/tests/src/crackfortran/foo_deps.f90 +6 -0
  484. numpy/f2py/tests/src/crackfortran/gh15035.f +16 -0
  485. numpy/f2py/tests/src/crackfortran/gh17859.f +12 -0
  486. numpy/f2py/tests/src/crackfortran/gh22648.pyf +7 -0
  487. numpy/f2py/tests/src/crackfortran/gh23533.f +5 -0
  488. numpy/f2py/tests/src/crackfortran/gh23598.f90 +4 -0
  489. numpy/f2py/tests/src/crackfortran/gh23598Warn.f90 +11 -0
  490. numpy/f2py/tests/src/crackfortran/gh23879.f90 +20 -0
  491. numpy/f2py/tests/src/crackfortran/gh27697.f90 +12 -0
  492. numpy/f2py/tests/src/crackfortran/gh2848.f90 +13 -0
  493. numpy/f2py/tests/src/crackfortran/operators.f90 +49 -0
  494. numpy/f2py/tests/src/crackfortran/privatemod.f90 +11 -0
  495. numpy/f2py/tests/src/crackfortran/publicmod.f90 +10 -0
  496. numpy/f2py/tests/src/crackfortran/pubprivmod.f90 +10 -0
  497. numpy/f2py/tests/src/crackfortran/unicode_comment.f90 +4 -0
  498. numpy/f2py/tests/src/f2cmap/.f2py_f2cmap +1 -0
  499. numpy/f2py/tests/src/f2cmap/isoFortranEnvMap.f90 +9 -0
  500. numpy/f2py/tests/src/isocintrin/isoCtests.f90 +34 -0
  501. numpy/f2py/tests/src/kind/foo.f90 +20 -0
  502. numpy/f2py/tests/src/mixed/foo.f +5 -0
  503. numpy/f2py/tests/src/mixed/foo_fixed.f90 +8 -0
  504. numpy/f2py/tests/src/mixed/foo_free.f90 +8 -0
  505. numpy/f2py/tests/src/modules/gh25337/data.f90 +8 -0
  506. numpy/f2py/tests/src/modules/gh25337/use_data.f90 +6 -0
  507. numpy/f2py/tests/src/modules/gh26920/two_mods_with_no_public_entities.f90 +21 -0
  508. numpy/f2py/tests/src/modules/gh26920/two_mods_with_one_public_routine.f90 +21 -0
  509. numpy/f2py/tests/src/modules/module_data_docstring.f90 +12 -0
  510. numpy/f2py/tests/src/modules/use_modules.f90 +20 -0
  511. numpy/f2py/tests/src/negative_bounds/issue_20853.f90 +7 -0
  512. numpy/f2py/tests/src/parameter/constant_array.f90 +45 -0
  513. numpy/f2py/tests/src/parameter/constant_both.f90 +57 -0
  514. numpy/f2py/tests/src/parameter/constant_compound.f90 +15 -0
  515. numpy/f2py/tests/src/parameter/constant_integer.f90 +22 -0
  516. numpy/f2py/tests/src/parameter/constant_non_compound.f90 +23 -0
  517. numpy/f2py/tests/src/parameter/constant_real.f90 +23 -0
  518. numpy/f2py/tests/src/quoted_character/foo.f +14 -0
  519. numpy/f2py/tests/src/regression/AB.inc +1 -0
  520. numpy/f2py/tests/src/regression/assignOnlyModule.f90 +25 -0
  521. numpy/f2py/tests/src/regression/datonly.f90 +17 -0
  522. numpy/f2py/tests/src/regression/f77comments.f +26 -0
  523. numpy/f2py/tests/src/regression/f77fixedform.f95 +5 -0
  524. numpy/f2py/tests/src/regression/f90continuation.f90 +9 -0
  525. numpy/f2py/tests/src/regression/incfile.f90 +5 -0
  526. numpy/f2py/tests/src/regression/inout.f90 +9 -0
  527. numpy/f2py/tests/src/regression/lower_f2py_fortran.f90 +5 -0
  528. numpy/f2py/tests/src/regression/mod_derived_types.f90 +23 -0
  529. numpy/f2py/tests/src/return_character/foo77.f +45 -0
  530. numpy/f2py/tests/src/return_character/foo90.f90 +48 -0
  531. numpy/f2py/tests/src/return_complex/foo77.f +45 -0
  532. numpy/f2py/tests/src/return_complex/foo90.f90 +48 -0
  533. numpy/f2py/tests/src/return_integer/foo77.f +56 -0
  534. numpy/f2py/tests/src/return_integer/foo90.f90 +59 -0
  535. numpy/f2py/tests/src/return_logical/foo77.f +56 -0
  536. numpy/f2py/tests/src/return_logical/foo90.f90 +59 -0
  537. numpy/f2py/tests/src/return_real/foo77.f +45 -0
  538. numpy/f2py/tests/src/return_real/foo90.f90 +48 -0
  539. numpy/f2py/tests/src/routines/funcfortranname.f +5 -0
  540. numpy/f2py/tests/src/routines/funcfortranname.pyf +11 -0
  541. numpy/f2py/tests/src/routines/subrout.f +4 -0
  542. numpy/f2py/tests/src/routines/subrout.pyf +10 -0
  543. numpy/f2py/tests/src/size/foo.f90 +44 -0
  544. numpy/f2py/tests/src/string/char.f90 +29 -0
  545. numpy/f2py/tests/src/string/fixed_string.f90 +34 -0
  546. numpy/f2py/tests/src/string/gh24008.f +8 -0
  547. numpy/f2py/tests/src/string/gh24662.f90 +7 -0
  548. numpy/f2py/tests/src/string/gh25286.f90 +14 -0
  549. numpy/f2py/tests/src/string/gh25286.pyf +12 -0
  550. numpy/f2py/tests/src/string/gh25286_bc.pyf +12 -0
  551. numpy/f2py/tests/src/string/scalar_string.f90 +9 -0
  552. numpy/f2py/tests/src/string/string.f +12 -0
  553. numpy/f2py/tests/src/value_attrspec/gh21665.f90 +9 -0
  554. numpy/f2py/tests/test_abstract_interface.py +26 -0
  555. numpy/f2py/tests/test_array_from_pyobj.py +678 -0
  556. numpy/f2py/tests/test_assumed_shape.py +50 -0
  557. numpy/f2py/tests/test_block_docstring.py +20 -0
  558. numpy/f2py/tests/test_callback.py +263 -0
  559. numpy/f2py/tests/test_character.py +641 -0
  560. numpy/f2py/tests/test_common.py +23 -0
  561. numpy/f2py/tests/test_crackfortran.py +421 -0
  562. numpy/f2py/tests/test_data.py +71 -0
  563. numpy/f2py/tests/test_docs.py +66 -0
  564. numpy/f2py/tests/test_f2cmap.py +17 -0
  565. numpy/f2py/tests/test_f2py2e.py +983 -0
  566. numpy/f2py/tests/test_isoc.py +56 -0
  567. numpy/f2py/tests/test_kind.py +52 -0
  568. numpy/f2py/tests/test_mixed.py +35 -0
  569. numpy/f2py/tests/test_modules.py +83 -0
  570. numpy/f2py/tests/test_parameter.py +129 -0
  571. numpy/f2py/tests/test_pyf_src.py +43 -0
  572. numpy/f2py/tests/test_quoted_character.py +18 -0
  573. numpy/f2py/tests/test_regression.py +187 -0
  574. numpy/f2py/tests/test_return_character.py +48 -0
  575. numpy/f2py/tests/test_return_complex.py +67 -0
  576. numpy/f2py/tests/test_return_integer.py +55 -0
  577. numpy/f2py/tests/test_return_logical.py +65 -0
  578. numpy/f2py/tests/test_return_real.py +109 -0
  579. numpy/f2py/tests/test_routines.py +29 -0
  580. numpy/f2py/tests/test_semicolon_split.py +75 -0
  581. numpy/f2py/tests/test_size.py +45 -0
  582. numpy/f2py/tests/test_string.py +100 -0
  583. numpy/f2py/tests/test_symbolic.py +500 -0
  584. numpy/f2py/tests/test_value_attrspec.py +15 -0
  585. numpy/f2py/tests/util.py +442 -0
  586. numpy/f2py/use_rules.py +99 -0
  587. numpy/f2py/use_rules.pyi +9 -0
  588. numpy/fft/__init__.py +213 -0
  589. numpy/fft/__init__.pyi +38 -0
  590. numpy/fft/_helper.py +235 -0
  591. numpy/fft/_helper.pyi +44 -0
  592. numpy/fft/_pocketfft.py +1693 -0
  593. numpy/fft/_pocketfft.pyi +137 -0
  594. numpy/fft/_pocketfft_umath.pypy311-pp73-darwin.so +0 -0
  595. numpy/fft/tests/__init__.py +0 -0
  596. numpy/fft/tests/test_helper.py +167 -0
  597. numpy/fft/tests/test_pocketfft.py +589 -0
  598. numpy/lib/__init__.py +97 -0
  599. numpy/lib/__init__.pyi +52 -0
  600. numpy/lib/_array_utils_impl.py +62 -0
  601. numpy/lib/_array_utils_impl.pyi +10 -0
  602. numpy/lib/_arraypad_impl.py +926 -0
  603. numpy/lib/_arraypad_impl.pyi +88 -0
  604. numpy/lib/_arraysetops_impl.py +1158 -0
  605. numpy/lib/_arraysetops_impl.pyi +462 -0
  606. numpy/lib/_arrayterator_impl.py +224 -0
  607. numpy/lib/_arrayterator_impl.pyi +45 -0
  608. numpy/lib/_datasource.py +700 -0
  609. numpy/lib/_datasource.pyi +30 -0
  610. numpy/lib/_format_impl.py +1036 -0
  611. numpy/lib/_format_impl.pyi +56 -0
  612. numpy/lib/_function_base_impl.py +5760 -0
  613. numpy/lib/_function_base_impl.pyi +2324 -0
  614. numpy/lib/_histograms_impl.py +1085 -0
  615. numpy/lib/_histograms_impl.pyi +40 -0
  616. numpy/lib/_index_tricks_impl.py +1048 -0
  617. numpy/lib/_index_tricks_impl.pyi +267 -0
  618. numpy/lib/_iotools.py +900 -0
  619. numpy/lib/_iotools.pyi +116 -0
  620. numpy/lib/_nanfunctions_impl.py +2006 -0
  621. numpy/lib/_nanfunctions_impl.pyi +48 -0
  622. numpy/lib/_npyio_impl.py +2583 -0
  623. numpy/lib/_npyio_impl.pyi +299 -0
  624. numpy/lib/_polynomial_impl.py +1465 -0
  625. numpy/lib/_polynomial_impl.pyi +338 -0
  626. numpy/lib/_scimath_impl.py +642 -0
  627. numpy/lib/_scimath_impl.pyi +93 -0
  628. numpy/lib/_shape_base_impl.py +1289 -0
  629. numpy/lib/_shape_base_impl.pyi +236 -0
  630. numpy/lib/_stride_tricks_impl.py +582 -0
  631. numpy/lib/_stride_tricks_impl.pyi +73 -0
  632. numpy/lib/_twodim_base_impl.py +1201 -0
  633. numpy/lib/_twodim_base_impl.pyi +408 -0
  634. numpy/lib/_type_check_impl.py +710 -0
  635. numpy/lib/_type_check_impl.pyi +348 -0
  636. numpy/lib/_ufunclike_impl.py +199 -0
  637. numpy/lib/_ufunclike_impl.pyi +60 -0
  638. numpy/lib/_user_array_impl.py +310 -0
  639. numpy/lib/_user_array_impl.pyi +226 -0
  640. numpy/lib/_utils_impl.py +784 -0
  641. numpy/lib/_utils_impl.pyi +22 -0
  642. numpy/lib/_version.py +153 -0
  643. numpy/lib/_version.pyi +17 -0
  644. numpy/lib/array_utils.py +7 -0
  645. numpy/lib/array_utils.pyi +6 -0
  646. numpy/lib/format.py +24 -0
  647. numpy/lib/format.pyi +24 -0
  648. numpy/lib/introspect.py +94 -0
  649. numpy/lib/introspect.pyi +3 -0
  650. numpy/lib/mixins.py +180 -0
  651. numpy/lib/mixins.pyi +78 -0
  652. numpy/lib/npyio.py +1 -0
  653. numpy/lib/npyio.pyi +5 -0
  654. numpy/lib/recfunctions.py +1681 -0
  655. numpy/lib/recfunctions.pyi +444 -0
  656. numpy/lib/scimath.py +13 -0
  657. numpy/lib/scimath.pyi +12 -0
  658. numpy/lib/stride_tricks.py +1 -0
  659. numpy/lib/stride_tricks.pyi +4 -0
  660. numpy/lib/tests/__init__.py +0 -0
  661. numpy/lib/tests/data/py2-np0-objarr.npy +0 -0
  662. numpy/lib/tests/data/py2-objarr.npy +0 -0
  663. numpy/lib/tests/data/py2-objarr.npz +0 -0
  664. numpy/lib/tests/data/py3-objarr.npy +0 -0
  665. numpy/lib/tests/data/py3-objarr.npz +0 -0
  666. numpy/lib/tests/data/python3.npy +0 -0
  667. numpy/lib/tests/data/win64python2.npy +0 -0
  668. numpy/lib/tests/test__datasource.py +328 -0
  669. numpy/lib/tests/test__iotools.py +358 -0
  670. numpy/lib/tests/test__version.py +64 -0
  671. numpy/lib/tests/test_array_utils.py +32 -0
  672. numpy/lib/tests/test_arraypad.py +1427 -0
  673. numpy/lib/tests/test_arraysetops.py +1302 -0
  674. numpy/lib/tests/test_arrayterator.py +45 -0
  675. numpy/lib/tests/test_format.py +1054 -0
  676. numpy/lib/tests/test_function_base.py +4750 -0
  677. numpy/lib/tests/test_histograms.py +855 -0
  678. numpy/lib/tests/test_index_tricks.py +693 -0
  679. numpy/lib/tests/test_io.py +2857 -0
  680. numpy/lib/tests/test_loadtxt.py +1099 -0
  681. numpy/lib/tests/test_mixins.py +215 -0
  682. numpy/lib/tests/test_nanfunctions.py +1438 -0
  683. numpy/lib/tests/test_packbits.py +376 -0
  684. numpy/lib/tests/test_polynomial.py +325 -0
  685. numpy/lib/tests/test_recfunctions.py +1042 -0
  686. numpy/lib/tests/test_regression.py +231 -0
  687. numpy/lib/tests/test_shape_base.py +813 -0
  688. numpy/lib/tests/test_stride_tricks.py +655 -0
  689. numpy/lib/tests/test_twodim_base.py +559 -0
  690. numpy/lib/tests/test_type_check.py +473 -0
  691. numpy/lib/tests/test_ufunclike.py +97 -0
  692. numpy/lib/tests/test_utils.py +80 -0
  693. numpy/lib/user_array.py +1 -0
  694. numpy/lib/user_array.pyi +1 -0
  695. numpy/linalg/__init__.py +95 -0
  696. numpy/linalg/__init__.pyi +71 -0
  697. numpy/linalg/_linalg.py +3657 -0
  698. numpy/linalg/_linalg.pyi +548 -0
  699. numpy/linalg/_umath_linalg.pyi +60 -0
  700. numpy/linalg/_umath_linalg.pypy311-pp73-darwin.so +0 -0
  701. numpy/linalg/lapack_lite.pyi +143 -0
  702. numpy/linalg/lapack_lite.pypy311-pp73-darwin.so +0 -0
  703. numpy/linalg/tests/__init__.py +0 -0
  704. numpy/linalg/tests/test_deprecations.py +21 -0
  705. numpy/linalg/tests/test_linalg.py +2442 -0
  706. numpy/linalg/tests/test_regression.py +182 -0
  707. numpy/ma/API_CHANGES.txt +135 -0
  708. numpy/ma/LICENSE +24 -0
  709. numpy/ma/README.rst +236 -0
  710. numpy/ma/__init__.py +53 -0
  711. numpy/ma/__init__.pyi +458 -0
  712. numpy/ma/core.py +8929 -0
  713. numpy/ma/core.pyi +3720 -0
  714. numpy/ma/extras.py +2266 -0
  715. numpy/ma/extras.pyi +297 -0
  716. numpy/ma/mrecords.py +762 -0
  717. numpy/ma/mrecords.pyi +96 -0
  718. numpy/ma/tests/__init__.py +0 -0
  719. numpy/ma/tests/test_arrayobject.py +40 -0
  720. numpy/ma/tests/test_core.py +6008 -0
  721. numpy/ma/tests/test_deprecations.py +65 -0
  722. numpy/ma/tests/test_extras.py +1945 -0
  723. numpy/ma/tests/test_mrecords.py +495 -0
  724. numpy/ma/tests/test_old_ma.py +939 -0
  725. numpy/ma/tests/test_regression.py +83 -0
  726. numpy/ma/tests/test_subclassing.py +469 -0
  727. numpy/ma/testutils.py +294 -0
  728. numpy/ma/testutils.pyi +69 -0
  729. numpy/matlib.py +380 -0
  730. numpy/matlib.pyi +580 -0
  731. numpy/matrixlib/__init__.py +12 -0
  732. numpy/matrixlib/__init__.pyi +3 -0
  733. numpy/matrixlib/defmatrix.py +1119 -0
  734. numpy/matrixlib/defmatrix.pyi +218 -0
  735. numpy/matrixlib/tests/__init__.py +0 -0
  736. numpy/matrixlib/tests/test_defmatrix.py +455 -0
  737. numpy/matrixlib/tests/test_interaction.py +360 -0
  738. numpy/matrixlib/tests/test_masked_matrix.py +240 -0
  739. numpy/matrixlib/tests/test_matrix_linalg.py +110 -0
  740. numpy/matrixlib/tests/test_multiarray.py +17 -0
  741. numpy/matrixlib/tests/test_numeric.py +18 -0
  742. numpy/matrixlib/tests/test_regression.py +31 -0
  743. numpy/polynomial/__init__.py +187 -0
  744. numpy/polynomial/__init__.pyi +31 -0
  745. numpy/polynomial/_polybase.py +1191 -0
  746. numpy/polynomial/_polybase.pyi +262 -0
  747. numpy/polynomial/_polytypes.pyi +501 -0
  748. numpy/polynomial/chebyshev.py +2001 -0
  749. numpy/polynomial/chebyshev.pyi +180 -0
  750. numpy/polynomial/hermite.py +1738 -0
  751. numpy/polynomial/hermite.pyi +106 -0
  752. numpy/polynomial/hermite_e.py +1640 -0
  753. numpy/polynomial/hermite_e.pyi +106 -0
  754. numpy/polynomial/laguerre.py +1673 -0
  755. numpy/polynomial/laguerre.pyi +100 -0
  756. numpy/polynomial/legendre.py +1603 -0
  757. numpy/polynomial/legendre.pyi +100 -0
  758. numpy/polynomial/polynomial.py +1625 -0
  759. numpy/polynomial/polynomial.pyi +109 -0
  760. numpy/polynomial/polyutils.py +759 -0
  761. numpy/polynomial/polyutils.pyi +307 -0
  762. numpy/polynomial/tests/__init__.py +0 -0
  763. numpy/polynomial/tests/test_chebyshev.py +618 -0
  764. numpy/polynomial/tests/test_classes.py +613 -0
  765. numpy/polynomial/tests/test_hermite.py +553 -0
  766. numpy/polynomial/tests/test_hermite_e.py +554 -0
  767. numpy/polynomial/tests/test_laguerre.py +535 -0
  768. numpy/polynomial/tests/test_legendre.py +566 -0
  769. numpy/polynomial/tests/test_polynomial.py +691 -0
  770. numpy/polynomial/tests/test_polyutils.py +123 -0
  771. numpy/polynomial/tests/test_printing.py +557 -0
  772. numpy/polynomial/tests/test_symbol.py +217 -0
  773. numpy/py.typed +0 -0
  774. numpy/random/LICENSE.md +71 -0
  775. numpy/random/__init__.pxd +14 -0
  776. numpy/random/__init__.py +213 -0
  777. numpy/random/__init__.pyi +124 -0
  778. numpy/random/_bounded_integers.pxd +29 -0
  779. numpy/random/_bounded_integers.pyi +1 -0
  780. numpy/random/_bounded_integers.pypy311-pp73-darwin.so +0 -0
  781. numpy/random/_common.pxd +110 -0
  782. numpy/random/_common.pyi +16 -0
  783. numpy/random/_common.pypy311-pp73-darwin.so +0 -0
  784. numpy/random/_examples/cffi/extending.py +44 -0
  785. numpy/random/_examples/cffi/parse.py +53 -0
  786. numpy/random/_examples/cython/extending.pyx +77 -0
  787. numpy/random/_examples/cython/extending_distributions.pyx +117 -0
  788. numpy/random/_examples/cython/meson.build +53 -0
  789. numpy/random/_examples/numba/extending.py +86 -0
  790. numpy/random/_examples/numba/extending_distributions.py +67 -0
  791. numpy/random/_generator.pyi +862 -0
  792. numpy/random/_generator.pypy311-pp73-darwin.so +0 -0
  793. numpy/random/_mt19937.pyi +27 -0
  794. numpy/random/_mt19937.pypy311-pp73-darwin.so +0 -0
  795. numpy/random/_pcg64.pyi +41 -0
  796. numpy/random/_pcg64.pypy311-pp73-darwin.so +0 -0
  797. numpy/random/_philox.pyi +36 -0
  798. numpy/random/_philox.pypy311-pp73-darwin.so +0 -0
  799. numpy/random/_pickle.py +88 -0
  800. numpy/random/_pickle.pyi +43 -0
  801. numpy/random/_sfc64.pyi +25 -0
  802. numpy/random/_sfc64.pypy311-pp73-darwin.so +0 -0
  803. numpy/random/bit_generator.pxd +40 -0
  804. numpy/random/bit_generator.pyi +123 -0
  805. numpy/random/bit_generator.pypy311-pp73-darwin.so +0 -0
  806. numpy/random/c_distributions.pxd +119 -0
  807. numpy/random/lib/libnpyrandom.a +0 -0
  808. numpy/random/mtrand.pyi +759 -0
  809. numpy/random/mtrand.pypy311-pp73-darwin.so +0 -0
  810. numpy/random/tests/__init__.py +0 -0
  811. numpy/random/tests/data/__init__.py +0 -0
  812. numpy/random/tests/data/generator_pcg64_np121.pkl.gz +0 -0
  813. numpy/random/tests/data/generator_pcg64_np126.pkl.gz +0 -0
  814. numpy/random/tests/data/mt19937-testset-1.csv +1001 -0
  815. numpy/random/tests/data/mt19937-testset-2.csv +1001 -0
  816. numpy/random/tests/data/pcg64-testset-1.csv +1001 -0
  817. numpy/random/tests/data/pcg64-testset-2.csv +1001 -0
  818. numpy/random/tests/data/pcg64dxsm-testset-1.csv +1001 -0
  819. numpy/random/tests/data/pcg64dxsm-testset-2.csv +1001 -0
  820. numpy/random/tests/data/philox-testset-1.csv +1001 -0
  821. numpy/random/tests/data/philox-testset-2.csv +1001 -0
  822. numpy/random/tests/data/sfc64-testset-1.csv +1001 -0
  823. numpy/random/tests/data/sfc64-testset-2.csv +1001 -0
  824. numpy/random/tests/data/sfc64_np126.pkl.gz +0 -0
  825. numpy/random/tests/test_direct.py +595 -0
  826. numpy/random/tests/test_extending.py +131 -0
  827. numpy/random/tests/test_generator_mt19937.py +2825 -0
  828. numpy/random/tests/test_generator_mt19937_regressions.py +221 -0
  829. numpy/random/tests/test_random.py +1724 -0
  830. numpy/random/tests/test_randomstate.py +2099 -0
  831. numpy/random/tests/test_randomstate_regression.py +213 -0
  832. numpy/random/tests/test_regression.py +175 -0
  833. numpy/random/tests/test_seed_sequence.py +79 -0
  834. numpy/random/tests/test_smoke.py +882 -0
  835. numpy/rec/__init__.py +2 -0
  836. numpy/rec/__init__.pyi +23 -0
  837. numpy/strings/__init__.py +2 -0
  838. numpy/strings/__init__.pyi +97 -0
  839. numpy/testing/__init__.py +22 -0
  840. numpy/testing/__init__.pyi +107 -0
  841. numpy/testing/_private/__init__.py +0 -0
  842. numpy/testing/_private/__init__.pyi +0 -0
  843. numpy/testing/_private/extbuild.py +250 -0
  844. numpy/testing/_private/extbuild.pyi +25 -0
  845. numpy/testing/_private/utils.py +2830 -0
  846. numpy/testing/_private/utils.pyi +505 -0
  847. numpy/testing/overrides.py +84 -0
  848. numpy/testing/overrides.pyi +10 -0
  849. numpy/testing/print_coercion_tables.py +207 -0
  850. numpy/testing/print_coercion_tables.pyi +26 -0
  851. numpy/testing/tests/__init__.py +0 -0
  852. numpy/testing/tests/test_utils.py +2123 -0
  853. numpy/tests/__init__.py +0 -0
  854. numpy/tests/test__all__.py +10 -0
  855. numpy/tests/test_configtool.py +51 -0
  856. numpy/tests/test_ctypeslib.py +383 -0
  857. numpy/tests/test_lazyloading.py +42 -0
  858. numpy/tests/test_matlib.py +59 -0
  859. numpy/tests/test_numpy_config.py +47 -0
  860. numpy/tests/test_numpy_version.py +54 -0
  861. numpy/tests/test_public_api.py +807 -0
  862. numpy/tests/test_reloading.py +76 -0
  863. numpy/tests/test_scripts.py +48 -0
  864. numpy/tests/test_warnings.py +79 -0
  865. numpy/typing/__init__.py +233 -0
  866. numpy/typing/__init__.pyi +3 -0
  867. numpy/typing/mypy_plugin.py +200 -0
  868. numpy/typing/tests/__init__.py +0 -0
  869. numpy/typing/tests/data/fail/arithmetic.pyi +126 -0
  870. numpy/typing/tests/data/fail/array_constructors.pyi +34 -0
  871. numpy/typing/tests/data/fail/array_like.pyi +15 -0
  872. numpy/typing/tests/data/fail/array_pad.pyi +6 -0
  873. numpy/typing/tests/data/fail/arrayprint.pyi +15 -0
  874. numpy/typing/tests/data/fail/arrayterator.pyi +14 -0
  875. numpy/typing/tests/data/fail/bitwise_ops.pyi +17 -0
  876. numpy/typing/tests/data/fail/char.pyi +63 -0
  877. numpy/typing/tests/data/fail/chararray.pyi +61 -0
  878. numpy/typing/tests/data/fail/comparisons.pyi +27 -0
  879. numpy/typing/tests/data/fail/constants.pyi +3 -0
  880. numpy/typing/tests/data/fail/datasource.pyi +16 -0
  881. numpy/typing/tests/data/fail/dtype.pyi +17 -0
  882. numpy/typing/tests/data/fail/einsumfunc.pyi +12 -0
  883. numpy/typing/tests/data/fail/flatiter.pyi +38 -0
  884. numpy/typing/tests/data/fail/fromnumeric.pyi +148 -0
  885. numpy/typing/tests/data/fail/histograms.pyi +12 -0
  886. numpy/typing/tests/data/fail/index_tricks.pyi +14 -0
  887. numpy/typing/tests/data/fail/lib_function_base.pyi +60 -0
  888. numpy/typing/tests/data/fail/lib_polynomial.pyi +29 -0
  889. numpy/typing/tests/data/fail/lib_utils.pyi +3 -0
  890. numpy/typing/tests/data/fail/lib_version.pyi +6 -0
  891. numpy/typing/tests/data/fail/linalg.pyi +52 -0
  892. numpy/typing/tests/data/fail/ma.pyi +155 -0
  893. numpy/typing/tests/data/fail/memmap.pyi +5 -0
  894. numpy/typing/tests/data/fail/modules.pyi +17 -0
  895. numpy/typing/tests/data/fail/multiarray.pyi +52 -0
  896. numpy/typing/tests/data/fail/ndarray.pyi +11 -0
  897. numpy/typing/tests/data/fail/ndarray_misc.pyi +49 -0
  898. numpy/typing/tests/data/fail/nditer.pyi +8 -0
  899. numpy/typing/tests/data/fail/nested_sequence.pyi +17 -0
  900. numpy/typing/tests/data/fail/npyio.pyi +24 -0
  901. numpy/typing/tests/data/fail/numerictypes.pyi +5 -0
  902. numpy/typing/tests/data/fail/random.pyi +62 -0
  903. numpy/typing/tests/data/fail/rec.pyi +17 -0
  904. numpy/typing/tests/data/fail/scalars.pyi +86 -0
  905. numpy/typing/tests/data/fail/shape.pyi +7 -0
  906. numpy/typing/tests/data/fail/shape_base.pyi +8 -0
  907. numpy/typing/tests/data/fail/stride_tricks.pyi +9 -0
  908. numpy/typing/tests/data/fail/strings.pyi +52 -0
  909. numpy/typing/tests/data/fail/testing.pyi +28 -0
  910. numpy/typing/tests/data/fail/twodim_base.pyi +39 -0
  911. numpy/typing/tests/data/fail/type_check.pyi +12 -0
  912. numpy/typing/tests/data/fail/ufunc_config.pyi +21 -0
  913. numpy/typing/tests/data/fail/ufunclike.pyi +21 -0
  914. numpy/typing/tests/data/fail/ufuncs.pyi +17 -0
  915. numpy/typing/tests/data/fail/warnings_and_errors.pyi +5 -0
  916. numpy/typing/tests/data/misc/extended_precision.pyi +9 -0
  917. numpy/typing/tests/data/mypy.ini +8 -0
  918. numpy/typing/tests/data/pass/arithmetic.py +614 -0
  919. numpy/typing/tests/data/pass/array_constructors.py +138 -0
  920. numpy/typing/tests/data/pass/array_like.py +43 -0
  921. numpy/typing/tests/data/pass/arrayprint.py +37 -0
  922. numpy/typing/tests/data/pass/arrayterator.py +28 -0
  923. numpy/typing/tests/data/pass/bitwise_ops.py +131 -0
  924. numpy/typing/tests/data/pass/comparisons.py +316 -0
  925. numpy/typing/tests/data/pass/dtype.py +57 -0
  926. numpy/typing/tests/data/pass/einsumfunc.py +36 -0
  927. numpy/typing/tests/data/pass/flatiter.py +26 -0
  928. numpy/typing/tests/data/pass/fromnumeric.py +272 -0
  929. numpy/typing/tests/data/pass/index_tricks.py +62 -0
  930. numpy/typing/tests/data/pass/lib_user_array.py +22 -0
  931. numpy/typing/tests/data/pass/lib_utils.py +19 -0
  932. numpy/typing/tests/data/pass/lib_version.py +18 -0
  933. numpy/typing/tests/data/pass/literal.py +52 -0
  934. numpy/typing/tests/data/pass/ma.py +199 -0
  935. numpy/typing/tests/data/pass/mod.py +149 -0
  936. numpy/typing/tests/data/pass/modules.py +45 -0
  937. numpy/typing/tests/data/pass/multiarray.py +77 -0
  938. numpy/typing/tests/data/pass/ndarray_conversion.py +81 -0
  939. numpy/typing/tests/data/pass/ndarray_misc.py +199 -0
  940. numpy/typing/tests/data/pass/ndarray_shape_manipulation.py +47 -0
  941. numpy/typing/tests/data/pass/nditer.py +4 -0
  942. numpy/typing/tests/data/pass/numeric.py +90 -0
  943. numpy/typing/tests/data/pass/numerictypes.py +17 -0
  944. numpy/typing/tests/data/pass/random.py +1498 -0
  945. numpy/typing/tests/data/pass/recfunctions.py +164 -0
  946. numpy/typing/tests/data/pass/scalars.py +249 -0
  947. numpy/typing/tests/data/pass/shape.py +19 -0
  948. numpy/typing/tests/data/pass/simple.py +170 -0
  949. numpy/typing/tests/data/pass/ufunc_config.py +64 -0
  950. numpy/typing/tests/data/pass/ufunclike.py +52 -0
  951. numpy/typing/tests/data/pass/ufuncs.py +16 -0
  952. numpy/typing/tests/data/pass/warnings_and_errors.py +6 -0
  953. numpy/typing/tests/data/reveal/arithmetic.pyi +719 -0
  954. numpy/typing/tests/data/reveal/array_api_info.pyi +70 -0
  955. numpy/typing/tests/data/reveal/array_constructors.pyi +277 -0
  956. numpy/typing/tests/data/reveal/arraypad.pyi +27 -0
  957. numpy/typing/tests/data/reveal/arrayprint.pyi +25 -0
  958. numpy/typing/tests/data/reveal/arraysetops.pyi +74 -0
  959. numpy/typing/tests/data/reveal/arrayterator.pyi +27 -0
  960. numpy/typing/tests/data/reveal/bitwise_ops.pyi +166 -0
  961. numpy/typing/tests/data/reveal/char.pyi +225 -0
  962. numpy/typing/tests/data/reveal/chararray.pyi +138 -0
  963. numpy/typing/tests/data/reveal/comparisons.pyi +264 -0
  964. numpy/typing/tests/data/reveal/constants.pyi +14 -0
  965. numpy/typing/tests/data/reveal/ctypeslib.pyi +81 -0
  966. numpy/typing/tests/data/reveal/datasource.pyi +23 -0
  967. numpy/typing/tests/data/reveal/dtype.pyi +132 -0
  968. numpy/typing/tests/data/reveal/einsumfunc.pyi +39 -0
  969. numpy/typing/tests/data/reveal/emath.pyi +54 -0
  970. numpy/typing/tests/data/reveal/fft.pyi +37 -0
  971. numpy/typing/tests/data/reveal/flatiter.pyi +86 -0
  972. numpy/typing/tests/data/reveal/fromnumeric.pyi +347 -0
  973. numpy/typing/tests/data/reveal/getlimits.pyi +53 -0
  974. numpy/typing/tests/data/reveal/histograms.pyi +25 -0
  975. numpy/typing/tests/data/reveal/index_tricks.pyi +70 -0
  976. numpy/typing/tests/data/reveal/lib_function_base.pyi +409 -0
  977. numpy/typing/tests/data/reveal/lib_polynomial.pyi +147 -0
  978. numpy/typing/tests/data/reveal/lib_utils.pyi +17 -0
  979. numpy/typing/tests/data/reveal/lib_version.pyi +20 -0
  980. numpy/typing/tests/data/reveal/linalg.pyi +154 -0
  981. numpy/typing/tests/data/reveal/ma.pyi +1098 -0
  982. numpy/typing/tests/data/reveal/matrix.pyi +73 -0
  983. numpy/typing/tests/data/reveal/memmap.pyi +19 -0
  984. numpy/typing/tests/data/reveal/mod.pyi +178 -0
  985. numpy/typing/tests/data/reveal/modules.pyi +51 -0
  986. numpy/typing/tests/data/reveal/multiarray.pyi +197 -0
  987. numpy/typing/tests/data/reveal/nbit_base_example.pyi +20 -0
  988. numpy/typing/tests/data/reveal/ndarray_assignability.pyi +82 -0
  989. numpy/typing/tests/data/reveal/ndarray_conversion.pyi +83 -0
  990. numpy/typing/tests/data/reveal/ndarray_misc.pyi +246 -0
  991. numpy/typing/tests/data/reveal/ndarray_shape_manipulation.pyi +47 -0
  992. numpy/typing/tests/data/reveal/nditer.pyi +49 -0
  993. numpy/typing/tests/data/reveal/nested_sequence.pyi +25 -0
  994. numpy/typing/tests/data/reveal/npyio.pyi +83 -0
  995. numpy/typing/tests/data/reveal/numeric.pyi +170 -0
  996. numpy/typing/tests/data/reveal/numerictypes.pyi +16 -0
  997. numpy/typing/tests/data/reveal/polynomial_polybase.pyi +217 -0
  998. numpy/typing/tests/data/reveal/polynomial_polyutils.pyi +218 -0
  999. numpy/typing/tests/data/reveal/polynomial_series.pyi +138 -0
  1000. numpy/typing/tests/data/reveal/random.pyi +1546 -0
  1001. numpy/typing/tests/data/reveal/rec.pyi +171 -0
  1002. numpy/typing/tests/data/reveal/scalars.pyi +191 -0
  1003. numpy/typing/tests/data/reveal/shape.pyi +13 -0
  1004. numpy/typing/tests/data/reveal/shape_base.pyi +52 -0
  1005. numpy/typing/tests/data/reveal/stride_tricks.pyi +27 -0
  1006. numpy/typing/tests/data/reveal/strings.pyi +196 -0
  1007. numpy/typing/tests/data/reveal/testing.pyi +198 -0
  1008. numpy/typing/tests/data/reveal/twodim_base.pyi +225 -0
  1009. numpy/typing/tests/data/reveal/type_check.pyi +67 -0
  1010. numpy/typing/tests/data/reveal/ufunc_config.pyi +29 -0
  1011. numpy/typing/tests/data/reveal/ufunclike.pyi +31 -0
  1012. numpy/typing/tests/data/reveal/ufuncs.pyi +142 -0
  1013. numpy/typing/tests/data/reveal/warnings_and_errors.pyi +11 -0
  1014. numpy/typing/tests/test_isfile.py +38 -0
  1015. numpy/typing/tests/test_runtime.py +110 -0
  1016. numpy/typing/tests/test_typing.py +205 -0
  1017. numpy/version.py +11 -0
  1018. numpy/version.pyi +9 -0
  1019. numpy-2.4.1.dist-info/METADATA +139 -0
  1020. numpy-2.4.1.dist-info/RECORD +1039 -0
  1021. numpy-2.4.1.dist-info/WHEEL +6 -0
  1022. numpy-2.4.1.dist-info/entry_points.txt +13 -0
  1023. numpy-2.4.1.dist-info/licenses/LICENSE.txt +935 -0
  1024. numpy-2.4.1.dist-info/licenses/numpy/_core/include/numpy/libdivide/LICENSE.txt +21 -0
  1025. numpy-2.4.1.dist-info/licenses/numpy/_core/src/common/pythoncapi-compat/COPYING +14 -0
  1026. numpy-2.4.1.dist-info/licenses/numpy/_core/src/highway/LICENSE +371 -0
  1027. numpy-2.4.1.dist-info/licenses/numpy/_core/src/multiarray/dragon4_LICENSE.txt +27 -0
  1028. numpy-2.4.1.dist-info/licenses/numpy/_core/src/npysort/x86-simd-sort/LICENSE.md +28 -0
  1029. numpy-2.4.1.dist-info/licenses/numpy/_core/src/umath/svml/LICENSE +30 -0
  1030. numpy-2.4.1.dist-info/licenses/numpy/fft/pocketfft/LICENSE.md +25 -0
  1031. numpy-2.4.1.dist-info/licenses/numpy/linalg/lapack_lite/LICENSE.txt +48 -0
  1032. numpy-2.4.1.dist-info/licenses/numpy/ma/LICENSE +24 -0
  1033. numpy-2.4.1.dist-info/licenses/numpy/random/LICENSE.md +71 -0
  1034. numpy-2.4.1.dist-info/licenses/numpy/random/src/distributions/LICENSE.md +61 -0
  1035. numpy-2.4.1.dist-info/licenses/numpy/random/src/mt19937/LICENSE.md +61 -0
  1036. numpy-2.4.1.dist-info/licenses/numpy/random/src/pcg64/LICENSE.md +22 -0
  1037. numpy-2.4.1.dist-info/licenses/numpy/random/src/philox/LICENSE.md +31 -0
  1038. numpy-2.4.1.dist-info/licenses/numpy/random/src/sfc64/LICENSE.md +27 -0
  1039. numpy-2.4.1.dist-info/licenses/numpy/random/src/splitmix64/LICENSE.md +9 -0
@@ -0,0 +1,2857 @@
1
+ import gc
2
+ import gzip
3
+ import locale
4
+ import os
5
+ import re
6
+ import sys
7
+ import threading
8
+ import time
9
+ import warnings
10
+ import zipfile
11
+ from ctypes import c_bool
12
+ from datetime import datetime
13
+ from io import BytesIO, StringIO
14
+ from multiprocessing import Value, get_context
15
+ from pathlib import Path
16
+ from tempfile import NamedTemporaryFile
17
+
18
+ import pytest
19
+
20
+ import numpy as np
21
+ import numpy.ma as ma
22
+ from numpy._utils import asbytes
23
+ from numpy.exceptions import VisibleDeprecationWarning
24
+ from numpy.lib import _npyio_impl
25
+ from numpy.lib._iotools import ConversionWarning, ConverterError
26
+ from numpy.lib._npyio_impl import recfromcsv, recfromtxt
27
+ from numpy.ma.testutils import assert_equal
28
+ from numpy.testing import (
29
+ HAS_REFCOUNT,
30
+ IS_PYPY,
31
+ IS_WASM,
32
+ assert_,
33
+ assert_allclose,
34
+ assert_array_equal,
35
+ assert_no_gc_cycles,
36
+ assert_no_warnings,
37
+ assert_raises,
38
+ assert_raises_regex,
39
+ break_cycles,
40
+ tempdir,
41
+ temppath,
42
+ )
43
+ from numpy.testing._private.utils import requires_memory
44
+
45
+
46
+ class TextIO(BytesIO):
47
+ """Helper IO class.
48
+
49
+ Writes encode strings to bytes if needed, reads return bytes.
50
+ This makes it easier to emulate files opened in binary mode
51
+ without needing to explicitly convert strings to bytes in
52
+ setting up the test data.
53
+
54
+ """
55
+ def __init__(self, s=""):
56
+ BytesIO.__init__(self, asbytes(s))
57
+
58
+ def write(self, s):
59
+ BytesIO.write(self, asbytes(s))
60
+
61
+ def writelines(self, lines):
62
+ BytesIO.writelines(self, [asbytes(s) for s in lines])
63
+
64
+
65
+ IS_64BIT = sys.maxsize > 2**32
66
+ try:
67
+ import bz2
68
+ HAS_BZ2 = True
69
+ except ImportError:
70
+ HAS_BZ2 = False
71
+ try:
72
+ import lzma
73
+ HAS_LZMA = True
74
+ except ImportError:
75
+ HAS_LZMA = False
76
+
77
+
78
+ def strptime(s, fmt=None):
79
+ """
80
+ This function is available in the datetime module only from Python >=
81
+ 2.5.
82
+
83
+ """
84
+ if isinstance(s, bytes):
85
+ s = s.decode("latin1")
86
+ return datetime(*time.strptime(s, fmt)[:3])
87
+
88
+
89
+ class RoundtripTest:
90
+ def roundtrip(self, save_func, *args, **kwargs):
91
+ """
92
+ save_func : callable
93
+ Function used to save arrays to file.
94
+ file_on_disk : bool
95
+ If true, store the file on disk, instead of in a
96
+ string buffer.
97
+ save_kwds : dict
98
+ Parameters passed to `save_func`.
99
+ load_kwds : dict
100
+ Parameters passed to `numpy.load`.
101
+ args : tuple of arrays
102
+ Arrays stored to file.
103
+
104
+ """
105
+ save_kwds = kwargs.get('save_kwds', {})
106
+ load_kwds = kwargs.get('load_kwds', {"allow_pickle": True})
107
+ file_on_disk = kwargs.get('file_on_disk', False)
108
+
109
+ if file_on_disk:
110
+ target_file = NamedTemporaryFile(delete=False)
111
+ load_file = target_file.name
112
+ else:
113
+ target_file = BytesIO()
114
+ load_file = target_file
115
+
116
+ try:
117
+ arr = args
118
+
119
+ save_func(target_file, *arr, **save_kwds)
120
+ target_file.flush()
121
+ target_file.seek(0)
122
+
123
+ if sys.platform == 'win32' and not isinstance(target_file, BytesIO):
124
+ target_file.close()
125
+
126
+ arr_reloaded = np.load(load_file, **load_kwds)
127
+
128
+ finally:
129
+ if not isinstance(target_file, BytesIO):
130
+ target_file.close()
131
+ # holds an open file descriptor so it can't be deleted on win
132
+ if 'arr_reloaded' in locals():
133
+ if not isinstance(arr_reloaded, np.lib.npyio.NpzFile):
134
+ os.remove(target_file.name)
135
+
136
+ return arr, arr_reloaded
137
+
138
+ def check_roundtrips(self, a):
139
+ self.roundtrip(a)
140
+ self.roundtrip(a, file_on_disk=True)
141
+ self.roundtrip(np.asfortranarray(a))
142
+ self.roundtrip(np.asfortranarray(a), file_on_disk=True)
143
+ if a.shape[0] > 1:
144
+ # neither C nor Fortran contiguous for 2D arrays or more
145
+ self.roundtrip(np.asfortranarray(a)[1:])
146
+ self.roundtrip(np.asfortranarray(a)[1:], file_on_disk=True)
147
+
148
+ def test_array(self):
149
+ a = np.array([], float)
150
+ self.check_roundtrips(a)
151
+
152
+ a = np.array([[1, 2], [3, 4]], float)
153
+ self.check_roundtrips(a)
154
+
155
+ a = np.array([[1, 2], [3, 4]], int)
156
+ self.check_roundtrips(a)
157
+
158
+ a = np.array([[1 + 5j, 2 + 6j], [3 + 7j, 4 + 8j]], dtype=np.csingle)
159
+ self.check_roundtrips(a)
160
+
161
+ a = np.array([[1 + 5j, 2 + 6j], [3 + 7j, 4 + 8j]], dtype=np.cdouble)
162
+ self.check_roundtrips(a)
163
+
164
+ def test_array_object(self):
165
+ a = np.array([], object)
166
+ self.check_roundtrips(a)
167
+
168
+ a = np.array([[1, 2], [3, 4]], object)
169
+ self.check_roundtrips(a)
170
+
171
+ def test_1D(self):
172
+ a = np.array([1, 2, 3, 4], int)
173
+ self.roundtrip(a)
174
+
175
+ @pytest.mark.skipif(sys.platform == 'win32', reason="Fails on Win32")
176
+ def test_mmap(self):
177
+ a = np.array([[1, 2.5], [4, 7.3]])
178
+ self.roundtrip(a, file_on_disk=True, load_kwds={'mmap_mode': 'r'})
179
+
180
+ a = np.asfortranarray([[1, 2.5], [4, 7.3]])
181
+ self.roundtrip(a, file_on_disk=True, load_kwds={'mmap_mode': 'r'})
182
+
183
+ def test_record(self):
184
+ a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
185
+ self.check_roundtrips(a)
186
+
187
+ @pytest.mark.slow
188
+ def test_format_2_0(self):
189
+ dt = [(("%d" % i) * 100, float) for i in range(500)]
190
+ a = np.ones(1000, dtype=dt)
191
+ with warnings.catch_warnings(record=True):
192
+ warnings.filterwarnings('always', '', UserWarning)
193
+ self.check_roundtrips(a)
194
+
195
+
196
+ class TestSaveLoad(RoundtripTest):
197
+ def roundtrip(self, *args, **kwargs):
198
+ arr, arr_reloaded = RoundtripTest.roundtrip(self, np.save, *args, **kwargs)
199
+ assert_equal(arr[0], arr_reloaded)
200
+ assert_equal(arr[0].dtype, arr_reloaded.dtype)
201
+ assert_equal(arr[0].flags.fnc, arr_reloaded.flags.fnc)
202
+
203
+
204
+ class TestSavezLoad(RoundtripTest):
205
+ def roundtrip(self, *args, **kwargs):
206
+ arr, arr_reloaded = RoundtripTest.roundtrip(self, np.savez, *args, **kwargs)
207
+ try:
208
+ for n, a in enumerate(arr):
209
+ reloaded = arr_reloaded['arr_%d' % n]
210
+ assert_equal(a, reloaded)
211
+ assert_equal(a.dtype, reloaded.dtype)
212
+ assert_equal(a.flags.fnc, reloaded.flags.fnc)
213
+ finally:
214
+ # delete tempfile, must be done here on windows
215
+ if arr_reloaded.fid:
216
+ arr_reloaded.fid.close()
217
+ os.remove(arr_reloaded.fid.name)
218
+
219
+ def test_load_non_npy(self):
220
+ """Test loading non-.npy files and name mapping in .npz."""
221
+ with temppath(prefix="numpy_test_npz_load_non_npy_", suffix=".npz") as tmp:
222
+ with zipfile.ZipFile(tmp, "w") as npz:
223
+ with npz.open("test1.npy", "w") as out_file:
224
+ np.save(out_file, np.arange(10))
225
+ with npz.open("test2", "w") as out_file:
226
+ np.save(out_file, np.arange(10))
227
+ with npz.open("metadata", "w") as out_file:
228
+ out_file.write(b"Name: Test")
229
+ with np.load(tmp) as npz:
230
+ assert len(npz["test1"]) == 10
231
+ assert len(npz["test1.npy"]) == 10
232
+ assert len(npz["test2"]) == 10
233
+ assert npz["metadata"] == b"Name: Test"
234
+
235
+ @pytest.mark.skipif(IS_PYPY, reason="Hangs on PyPy")
236
+ @pytest.mark.skipif(not IS_64BIT, reason="Needs 64bit platform")
237
+ @pytest.mark.slow
238
+ @pytest.mark.thread_unsafe(reason="crashes with low memory")
239
+ def test_big_arrays(self):
240
+ L = (1 << 31) + 100000
241
+ a = np.empty(L, dtype=np.uint8)
242
+ with temppath(prefix="numpy_test_big_arrays_", suffix=".npz") as tmp:
243
+ np.savez(tmp, a=a)
244
+ del a
245
+ npfile = np.load(tmp)
246
+ a = npfile['a'] # Should succeed
247
+ npfile.close()
248
+
249
+ def test_multiple_arrays(self):
250
+ a = np.array([[1, 2], [3, 4]], float)
251
+ b = np.array([[1 + 2j, 2 + 7j], [3 - 6j, 4 + 12j]], complex)
252
+ self.roundtrip(a, b)
253
+
254
+ def test_named_arrays(self):
255
+ a = np.array([[1, 2], [3, 4]], float)
256
+ b = np.array([[1 + 2j, 2 + 7j], [3 - 6j, 4 + 12j]], complex)
257
+ c = BytesIO()
258
+ np.savez(c, file_a=a, file_b=b)
259
+ c.seek(0)
260
+ l = np.load(c)
261
+ assert_equal(a, l['file_a'])
262
+ assert_equal(b, l['file_b'])
263
+
264
+ def test_tuple_getitem_raises(self):
265
+ # gh-23748
266
+ a = np.array([1, 2, 3])
267
+ f = BytesIO()
268
+ np.savez(f, a=a)
269
+ f.seek(0)
270
+ l = np.load(f)
271
+ with pytest.raises(KeyError, match="(1, 2)"):
272
+ l[1, 2]
273
+
274
+ def test_BagObj(self):
275
+ a = np.array([[1, 2], [3, 4]], float)
276
+ b = np.array([[1 + 2j, 2 + 7j], [3 - 6j, 4 + 12j]], complex)
277
+ c = BytesIO()
278
+ np.savez(c, file_a=a, file_b=b)
279
+ c.seek(0)
280
+ l = np.load(c)
281
+ assert_equal(sorted(dir(l.f)), ['file_a', 'file_b'])
282
+ assert_equal(a, l.f.file_a)
283
+ assert_equal(b, l.f.file_b)
284
+
285
+ @pytest.mark.skipif(IS_WASM, reason="Cannot start thread")
286
+ def test_savez_filename_clashes(self):
287
+ # Test that issue #852 is fixed
288
+ # and savez functions in multithreaded environment
289
+
290
+ def writer(error_list):
291
+ with temppath(suffix='.npz') as tmp:
292
+ arr = np.random.randn(500, 500)
293
+ try:
294
+ np.savez(tmp, arr=arr)
295
+ except OSError as err:
296
+ error_list.append(err)
297
+
298
+ errors = []
299
+ threads = [threading.Thread(target=writer, args=(errors,))
300
+ for j in range(3)]
301
+ for t in threads:
302
+ t.start()
303
+ for t in threads:
304
+ t.join()
305
+
306
+ if errors:
307
+ raise AssertionError(errors)
308
+
309
+ def test_not_closing_opened_fid(self):
310
+ # Test that issue #2178 is fixed:
311
+ # verify could seek on 'loaded' file
312
+ with temppath(suffix='.npz') as tmp:
313
+ with open(tmp, 'wb') as fp:
314
+ np.savez(fp, data='LOVELY LOAD')
315
+ with open(tmp, 'rb', 10000) as fp:
316
+ fp.seek(0)
317
+ assert_(not fp.closed)
318
+ np.load(fp)['data']
319
+ # fp must not get closed by .load
320
+ assert_(not fp.closed)
321
+ fp.seek(0)
322
+ assert_(not fp.closed)
323
+
324
+ @pytest.mark.slow_pypy
325
+ def test_closing_fid(self):
326
+ # Test that issue #1517 (too many opened files) remains closed
327
+ # It might be a "weak" test since failed to get triggered on
328
+ # e.g. Debian sid of 2012 Jul 05 but was reported to
329
+ # trigger the failure on Ubuntu 10.04:
330
+ # http://projects.scipy.org/numpy/ticket/1517#comment:2
331
+ with temppath(suffix='.npz') as tmp:
332
+ np.savez(tmp, data='LOVELY LOAD')
333
+ # We need to check if the garbage collector can properly close
334
+ # numpy npz file returned by np.load when their reference count
335
+ # goes to zero. Python running in debug mode raises a
336
+ # ResourceWarning when file closing is left to the garbage
337
+ # collector, so we catch the warnings.
338
+ with warnings.catch_warnings():
339
+ # TODO: specify exact message
340
+ warnings.simplefilter('ignore', ResourceWarning)
341
+ for i in range(1, 1025):
342
+ try:
343
+ np.load(tmp)["data"]
344
+ except Exception as e:
345
+ msg = f"Failed to load data from a file: {e}"
346
+ raise AssertionError(msg)
347
+ finally:
348
+ if IS_PYPY:
349
+ gc.collect()
350
+
351
+ def test_closing_zipfile_after_load(self):
352
+ # Check that zipfile owns file and can close it. This needs to
353
+ # pass a file name to load for the test. On windows failure will
354
+ # cause a second error will be raised when the attempt to remove
355
+ # the open file is made.
356
+ prefix = 'numpy_test_closing_zipfile_after_load_'
357
+ with temppath(suffix='.npz', prefix=prefix) as tmp:
358
+ np.savez(tmp, lab='place holder')
359
+ data = np.load(tmp)
360
+ fp = data.zip.fp
361
+ data.close()
362
+ assert_(fp.closed)
363
+
364
+ @pytest.mark.parametrize("count, expected_repr", [
365
+ (1, "NpzFile {fname!r} with keys: arr_0"),
366
+ (5, "NpzFile {fname!r} with keys: arr_0, arr_1, arr_2, arr_3, arr_4"),
367
+ # _MAX_REPR_ARRAY_COUNT is 5, so files with more than 5 keys are
368
+ # expected to end in '...'
369
+ (6, "NpzFile {fname!r} with keys: arr_0, arr_1, arr_2, arr_3, arr_4..."),
370
+ ])
371
+ def test_repr_lists_keys(self, count, expected_repr):
372
+ a = np.array([[1, 2], [3, 4]], float)
373
+ with temppath(suffix='.npz') as tmp:
374
+ np.savez(tmp, *[a] * count)
375
+ l = np.load(tmp)
376
+ assert repr(l) == expected_repr.format(fname=tmp)
377
+ l.close()
378
+
379
+
380
+ class TestSaveTxt:
381
+ def test_array(self):
382
+ a = np.array([[1, 2], [3, 4]], float)
383
+ fmt = "%.18e"
384
+ c = BytesIO()
385
+ np.savetxt(c, a, fmt=fmt)
386
+ c.seek(0)
387
+ assert_equal(c.readlines(),
388
+ [asbytes((fmt + ' ' + fmt + '\n') % (1, 2)),
389
+ asbytes((fmt + ' ' + fmt + '\n') % (3, 4))])
390
+
391
+ a = np.array([[1, 2], [3, 4]], int)
392
+ c = BytesIO()
393
+ np.savetxt(c, a, fmt='%d')
394
+ c.seek(0)
395
+ assert_equal(c.readlines(), [b'1 2\n', b'3 4\n'])
396
+
397
+ def test_1D(self):
398
+ a = np.array([1, 2, 3, 4], int)
399
+ c = BytesIO()
400
+ np.savetxt(c, a, fmt='%d')
401
+ c.seek(0)
402
+ lines = c.readlines()
403
+ assert_equal(lines, [b'1\n', b'2\n', b'3\n', b'4\n'])
404
+
405
+ def test_0D_3D(self):
406
+ c = BytesIO()
407
+ assert_raises(ValueError, np.savetxt, c, np.array(1))
408
+ assert_raises(ValueError, np.savetxt, c, np.array([[[1], [2]]]))
409
+
410
+ def test_structured(self):
411
+ a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
412
+ c = BytesIO()
413
+ np.savetxt(c, a, fmt='%d')
414
+ c.seek(0)
415
+ assert_equal(c.readlines(), [b'1 2\n', b'3 4\n'])
416
+
417
+ def test_structured_padded(self):
418
+ # gh-13297
419
+ a = np.array([(1, 2, 3), (4, 5, 6)], dtype=[
420
+ ('foo', 'i4'), ('bar', 'i4'), ('baz', 'i4')
421
+ ])
422
+ c = BytesIO()
423
+ np.savetxt(c, a[['foo', 'baz']], fmt='%d')
424
+ c.seek(0)
425
+ assert_equal(c.readlines(), [b'1 3\n', b'4 6\n'])
426
+
427
+ def test_multifield_view(self):
428
+ a = np.ones(1, dtype=[('x', 'i4'), ('y', 'i4'), ('z', 'f4')])
429
+ v = a[['x', 'z']]
430
+ with temppath(suffix='.npy') as path:
431
+ path = Path(path)
432
+ np.save(path, v)
433
+ data = np.load(path)
434
+ assert_array_equal(data, v)
435
+
436
+ def test_delimiter(self):
437
+ a = np.array([[1., 2.], [3., 4.]])
438
+ c = BytesIO()
439
+ np.savetxt(c, a, delimiter=',', fmt='%d')
440
+ c.seek(0)
441
+ assert_equal(c.readlines(), [b'1,2\n', b'3,4\n'])
442
+
443
+ def test_format(self):
444
+ a = np.array([(1, 2), (3, 4)])
445
+ c = BytesIO()
446
+ # Sequence of formats
447
+ np.savetxt(c, a, fmt=['%02d', '%3.1f'])
448
+ c.seek(0)
449
+ assert_equal(c.readlines(), [b'01 2.0\n', b'03 4.0\n'])
450
+
451
+ # A single multiformat string
452
+ c = BytesIO()
453
+ np.savetxt(c, a, fmt='%02d : %3.1f')
454
+ c.seek(0)
455
+ lines = c.readlines()
456
+ assert_equal(lines, [b'01 : 2.0\n', b'03 : 4.0\n'])
457
+
458
+ # Specify delimiter, should be overridden
459
+ c = BytesIO()
460
+ np.savetxt(c, a, fmt='%02d : %3.1f', delimiter=',')
461
+ c.seek(0)
462
+ lines = c.readlines()
463
+ assert_equal(lines, [b'01 : 2.0\n', b'03 : 4.0\n'])
464
+
465
+ # Bad fmt, should raise a ValueError
466
+ c = BytesIO()
467
+ assert_raises(ValueError, np.savetxt, c, a, fmt=99)
468
+
469
+ def test_header_footer(self):
470
+ # Test the functionality of the header and footer keyword argument.
471
+
472
+ c = BytesIO()
473
+ a = np.array([(1, 2), (3, 4)], dtype=int)
474
+ test_header_footer = 'Test header / footer'
475
+ # Test the header keyword argument
476
+ np.savetxt(c, a, fmt='%1d', header=test_header_footer)
477
+ c.seek(0)
478
+ assert_equal(c.read(),
479
+ asbytes('# ' + test_header_footer + '\n1 2\n3 4\n'))
480
+ # Test the footer keyword argument
481
+ c = BytesIO()
482
+ np.savetxt(c, a, fmt='%1d', footer=test_header_footer)
483
+ c.seek(0)
484
+ assert_equal(c.read(),
485
+ asbytes('1 2\n3 4\n# ' + test_header_footer + '\n'))
486
+ # Test the commentstr keyword argument used on the header
487
+ c = BytesIO()
488
+ commentstr = '% '
489
+ np.savetxt(c, a, fmt='%1d',
490
+ header=test_header_footer, comments=commentstr)
491
+ c.seek(0)
492
+ assert_equal(c.read(),
493
+ asbytes(commentstr + test_header_footer + '\n' + '1 2\n3 4\n'))
494
+ # Test the commentstr keyword argument used on the footer
495
+ c = BytesIO()
496
+ commentstr = '% '
497
+ np.savetxt(c, a, fmt='%1d',
498
+ footer=test_header_footer, comments=commentstr)
499
+ c.seek(0)
500
+ assert_equal(c.read(),
501
+ asbytes('1 2\n3 4\n' + commentstr + test_header_footer + '\n'))
502
+
503
+ @pytest.mark.parametrize("filename_type", [Path, str])
504
+ def test_file_roundtrip(self, filename_type):
505
+ with temppath() as name:
506
+ a = np.array([(1, 2), (3, 4)])
507
+ np.savetxt(filename_type(name), a)
508
+ b = np.loadtxt(filename_type(name))
509
+ assert_array_equal(a, b)
510
+
511
+ def test_complex_arrays(self):
512
+ ncols = 2
513
+ nrows = 2
514
+ a = np.zeros((ncols, nrows), dtype=np.complex128)
515
+ re = np.pi
516
+ im = np.e
517
+ a[:] = re + 1.0j * im
518
+
519
+ # One format only
520
+ c = BytesIO()
521
+ np.savetxt(c, a, fmt=' %+.3e')
522
+ c.seek(0)
523
+ lines = c.readlines()
524
+ assert_equal(
525
+ lines,
526
+ [b' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n',
527
+ b' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n'])
528
+
529
+ # One format for each real and imaginary part
530
+ c = BytesIO()
531
+ np.savetxt(c, a, fmt=' %+.3e' * 2 * ncols)
532
+ c.seek(0)
533
+ lines = c.readlines()
534
+ assert_equal(
535
+ lines,
536
+ [b' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n',
537
+ b' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n'])
538
+
539
+ # One format for each complex number
540
+ c = BytesIO()
541
+ np.savetxt(c, a, fmt=['(%.3e%+.3ej)'] * ncols)
542
+ c.seek(0)
543
+ lines = c.readlines()
544
+ assert_equal(
545
+ lines,
546
+ [b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n',
547
+ b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n'])
548
+
549
+ def test_complex_negative_exponent(self):
550
+ # Previous to 1.15, some formats generated x+-yj, gh 7895
551
+ ncols = 2
552
+ nrows = 2
553
+ a = np.zeros((ncols, nrows), dtype=np.complex128)
554
+ re = np.pi
555
+ im = np.e
556
+ a[:] = re - 1.0j * im
557
+ c = BytesIO()
558
+ np.savetxt(c, a, fmt='%.3e')
559
+ c.seek(0)
560
+ lines = c.readlines()
561
+ assert_equal(
562
+ lines,
563
+ [b' (3.142e+00-2.718e+00j) (3.142e+00-2.718e+00j)\n',
564
+ b' (3.142e+00-2.718e+00j) (3.142e+00-2.718e+00j)\n'])
565
+
566
+ def test_custom_writer(self):
567
+
568
+ class CustomWriter(list):
569
+ def write(self, text):
570
+ self.extend(text.split(b'\n'))
571
+
572
+ w = CustomWriter()
573
+ a = np.array([(1, 2), (3, 4)])
574
+ np.savetxt(w, a)
575
+ b = np.loadtxt(w)
576
+ assert_array_equal(a, b)
577
+
578
+ def test_unicode(self):
579
+ utf8 = b'\xcf\x96'.decode('UTF-8')
580
+ a = np.array([utf8], dtype=np.str_)
581
+ with tempdir() as tmpdir:
582
+ # set encoding as on windows it may not be unicode even on py3
583
+ np.savetxt(os.path.join(tmpdir, 'test.csv'), a, fmt=['%s'],
584
+ encoding='UTF-8')
585
+
586
+ def test_unicode_roundtrip(self):
587
+ utf8 = b'\xcf\x96'.decode('UTF-8')
588
+ a = np.array([utf8], dtype=np.str_)
589
+ # our gz wrapper support encoding
590
+ suffixes = ['', '.gz']
591
+ if HAS_BZ2:
592
+ suffixes.append('.bz2')
593
+ if HAS_LZMA:
594
+ suffixes.extend(['.xz', '.lzma'])
595
+ with tempdir() as tmpdir:
596
+ for suffix in suffixes:
597
+ np.savetxt(os.path.join(tmpdir, 'test.csv' + suffix), a,
598
+ fmt=['%s'], encoding='UTF-16-LE')
599
+ b = np.loadtxt(os.path.join(tmpdir, 'test.csv' + suffix),
600
+ encoding='UTF-16-LE', dtype=np.str_)
601
+ assert_array_equal(a, b)
602
+
603
+ def test_unicode_bytestream(self):
604
+ utf8 = b'\xcf\x96'.decode('UTF-8')
605
+ a = np.array([utf8], dtype=np.str_)
606
+ s = BytesIO()
607
+ np.savetxt(s, a, fmt=['%s'], encoding='UTF-8')
608
+ s.seek(0)
609
+ assert_equal(s.read().decode('UTF-8'), utf8 + '\n')
610
+
611
+ def test_unicode_stringstream(self):
612
+ utf8 = b'\xcf\x96'.decode('UTF-8')
613
+ a = np.array([utf8], dtype=np.str_)
614
+ s = StringIO()
615
+ np.savetxt(s, a, fmt=['%s'], encoding='UTF-8')
616
+ s.seek(0)
617
+ assert_equal(s.read(), utf8 + '\n')
618
+
619
+ @pytest.mark.parametrize("iotype", [StringIO, BytesIO])
620
+ def test_unicode_and_bytes_fmt(self, iotype):
621
+ # string type of fmt should not matter, see also gh-4053
622
+ a = np.array([1.])
623
+ s = iotype()
624
+ np.savetxt(s, a, fmt="%f")
625
+ s.seek(0)
626
+ if iotype is StringIO:
627
+ assert_equal(s.read(), "%f\n" % 1.)
628
+ else:
629
+ assert_equal(s.read(), b"%f\n" % 1.)
630
+
631
+ @pytest.mark.skipif(sys.platform == 'win32', reason="files>4GB may not work")
632
+ @pytest.mark.slow
633
+ @requires_memory(free_bytes=7e9)
634
+ @pytest.mark.thread_unsafe(reason="crashes with low memory")
635
+ def test_large_zip(self):
636
+ def check_large_zip(memoryerror_raised):
637
+ memoryerror_raised.value = False
638
+ try:
639
+ # The test takes at least 6GB of memory, writes a file larger
640
+ # than 4GB. This tests the ``allowZip64`` kwarg to ``zipfile``
641
+ test_data = np.asarray([np.random.rand(
642
+ np.random.randint(50, 100), 4)
643
+ for i in range(800000)], dtype=object)
644
+ with tempdir() as tmpdir:
645
+ np.savez(os.path.join(tmpdir, 'test.npz'),
646
+ test_data=test_data)
647
+ except MemoryError:
648
+ memoryerror_raised.value = True
649
+ raise
650
+ # run in a subprocess to ensure memory is released on PyPy, see gh-15775
651
+ # Use an object in shared memory to re-raise the MemoryError exception
652
+ # in our process if needed, see gh-16889
653
+ memoryerror_raised = Value(c_bool)
654
+
655
+ # Since Python 3.8, the default start method for multiprocessing has
656
+ # been changed from 'fork' to 'spawn' on macOS, causing inconsistency
657
+ # on memory sharing model, leading to failed test for check_large_zip
658
+ ctx = get_context('fork')
659
+ p = ctx.Process(target=check_large_zip, args=(memoryerror_raised,))
660
+ p.start()
661
+ p.join()
662
+ if memoryerror_raised.value:
663
+ raise MemoryError("Child process raised a MemoryError exception")
664
+ # -9 indicates a SIGKILL, probably an OOM.
665
+ if p.exitcode == -9:
666
+ msg = "subprocess got a SIGKILL, apparently free memory was not sufficient"
667
+ pytest.xfail(msg)
668
+ assert p.exitcode == 0
669
+
670
+ class LoadTxtBase:
671
+ def check_compressed(self, fopen, suffixes):
672
+ # Test that we can load data from a compressed file
673
+ wanted = np.arange(6).reshape((2, 3))
674
+ linesep = ('\n', '\r\n', '\r')
675
+ for sep in linesep:
676
+ data = '0 1 2' + sep + '3 4 5'
677
+ for suffix in suffixes:
678
+ with temppath(suffix=suffix) as name:
679
+ with fopen(name, mode='wt', encoding='UTF-32-LE') as f:
680
+ f.write(data)
681
+ res = self.loadfunc(name, encoding='UTF-32-LE')
682
+ assert_array_equal(res, wanted)
683
+ with fopen(name, "rt", encoding='UTF-32-LE') as f:
684
+ res = self.loadfunc(f)
685
+ assert_array_equal(res, wanted)
686
+
687
+ def test_compressed_gzip(self):
688
+ self.check_compressed(gzip.open, ('.gz',))
689
+
690
+ @pytest.mark.skipif(not HAS_BZ2, reason="Needs bz2")
691
+ def test_compressed_bz2(self):
692
+ self.check_compressed(bz2.open, ('.bz2',))
693
+
694
+ @pytest.mark.skipif(not HAS_LZMA, reason="Needs lzma")
695
+ def test_compressed_lzma(self):
696
+ self.check_compressed(lzma.open, ('.xz', '.lzma'))
697
+
698
+ def test_encoding(self):
699
+ with temppath() as path:
700
+ with open(path, "wb") as f:
701
+ f.write('0.\n1.\n2.'.encode("UTF-16"))
702
+ x = self.loadfunc(path, encoding="UTF-16")
703
+ assert_array_equal(x, [0., 1., 2.])
704
+
705
+ def test_stringload(self):
706
+ # umlaute
707
+ nonascii = b'\xc3\xb6\xc3\xbc\xc3\xb6'.decode("UTF-8")
708
+ with temppath() as path:
709
+ with open(path, "wb") as f:
710
+ f.write(nonascii.encode("UTF-16"))
711
+ x = self.loadfunc(path, encoding="UTF-16", dtype=np.str_)
712
+ assert_array_equal(x, nonascii)
713
+
714
+ def test_binary_decode(self):
715
+ utf16 = b'\xff\xfeh\x04 \x00i\x04 \x00j\x04'
716
+ v = self.loadfunc(BytesIO(utf16), dtype=np.str_, encoding='UTF-16')
717
+ assert_array_equal(v, np.array(utf16.decode('UTF-16').split()))
718
+
719
+ def test_converters_decode(self):
720
+ # test converters that decode strings
721
+ c = TextIO()
722
+ c.write(b'\xcf\x96')
723
+ c.seek(0)
724
+ x = self.loadfunc(c, dtype=np.str_, encoding="bytes",
725
+ converters={0: lambda x: x.decode('UTF-8')})
726
+ a = np.array([b'\xcf\x96'.decode('UTF-8')])
727
+ assert_array_equal(x, a)
728
+
729
+ def test_converters_nodecode(self):
730
+ # test native string converters enabled by setting an encoding
731
+ utf8 = b'\xcf\x96'.decode('UTF-8')
732
+ with temppath() as path:
733
+ with open(path, 'wt', encoding='UTF-8') as f:
734
+ f.write(utf8)
735
+ x = self.loadfunc(path, dtype=np.str_,
736
+ converters={0: lambda x: x + 't'},
737
+ encoding='UTF-8')
738
+ a = np.array([utf8 + 't'])
739
+ assert_array_equal(x, a)
740
+
741
+
742
+ class TestLoadTxt(LoadTxtBase):
743
+ loadfunc = staticmethod(np.loadtxt)
744
+
745
+ def setup_method(self):
746
+ # lower chunksize for testing
747
+ self.orig_chunk = _npyio_impl._loadtxt_chunksize
748
+ _npyio_impl._loadtxt_chunksize = 1
749
+
750
+ def teardown_method(self):
751
+ _npyio_impl._loadtxt_chunksize = self.orig_chunk
752
+
753
+ def test_record(self):
754
+ c = TextIO()
755
+ c.write('1 2\n3 4')
756
+ c.seek(0)
757
+ x = np.loadtxt(c, dtype=[('x', np.int32), ('y', np.int32)])
758
+ a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
759
+ assert_array_equal(x, a)
760
+
761
+ d = TextIO()
762
+ d.write('M 64 75.0\nF 25 60.0')
763
+ d.seek(0)
764
+ mydescriptor = {'names': ('gender', 'age', 'weight'),
765
+ 'formats': ('S1', 'i4', 'f4')}
766
+ b = np.array([('M', 64.0, 75.0),
767
+ ('F', 25.0, 60.0)], dtype=mydescriptor)
768
+ y = np.loadtxt(d, dtype=mydescriptor)
769
+ assert_array_equal(y, b)
770
+
771
+ def test_array(self):
772
+ c = TextIO()
773
+ c.write('1 2\n3 4')
774
+
775
+ c.seek(0)
776
+ x = np.loadtxt(c, dtype=int)
777
+ a = np.array([[1, 2], [3, 4]], int)
778
+ assert_array_equal(x, a)
779
+
780
+ c.seek(0)
781
+ x = np.loadtxt(c, dtype=float)
782
+ a = np.array([[1, 2], [3, 4]], float)
783
+ assert_array_equal(x, a)
784
+
785
+ def test_1D(self):
786
+ c = TextIO()
787
+ c.write('1\n2\n3\n4\n')
788
+ c.seek(0)
789
+ x = np.loadtxt(c, dtype=int)
790
+ a = np.array([1, 2, 3, 4], int)
791
+ assert_array_equal(x, a)
792
+
793
+ c = TextIO()
794
+ c.write('1,2,3,4\n')
795
+ c.seek(0)
796
+ x = np.loadtxt(c, dtype=int, delimiter=',')
797
+ a = np.array([1, 2, 3, 4], int)
798
+ assert_array_equal(x, a)
799
+
800
+ def test_missing(self):
801
+ c = TextIO()
802
+ c.write('1,2,3,,5\n')
803
+ c.seek(0)
804
+ x = np.loadtxt(c, dtype=int, delimiter=',',
805
+ converters={3: lambda s: int(s or - 999)})
806
+ a = np.array([1, 2, 3, -999, 5], int)
807
+ assert_array_equal(x, a)
808
+
809
+ def test_converters_with_usecols(self):
810
+ c = TextIO()
811
+ c.write('1,2,3,,5\n6,7,8,9,10\n')
812
+ c.seek(0)
813
+ x = np.loadtxt(c, dtype=int, delimiter=',',
814
+ converters={3: lambda s: int(s or - 999)},
815
+ usecols=(1, 3,))
816
+ a = np.array([[2, -999], [7, 9]], int)
817
+ assert_array_equal(x, a)
818
+
819
+ def test_comments_unicode(self):
820
+ c = TextIO()
821
+ c.write('# comment\n1,2,3,5\n')
822
+ c.seek(0)
823
+ x = np.loadtxt(c, dtype=int, delimiter=',',
824
+ comments='#')
825
+ a = np.array([1, 2, 3, 5], int)
826
+ assert_array_equal(x, a)
827
+
828
+ def test_comments_byte(self):
829
+ c = TextIO()
830
+ c.write('# comment\n1,2,3,5\n')
831
+ c.seek(0)
832
+ x = np.loadtxt(c, dtype=int, delimiter=',',
833
+ comments=b'#')
834
+ a = np.array([1, 2, 3, 5], int)
835
+ assert_array_equal(x, a)
836
+
837
+ def test_comments_multiple(self):
838
+ c = TextIO()
839
+ c.write('# comment\n1,2,3\n@ comment2\n4,5,6 // comment3')
840
+ c.seek(0)
841
+ x = np.loadtxt(c, dtype=int, delimiter=',',
842
+ comments=['#', '@', '//'])
843
+ a = np.array([[1, 2, 3], [4, 5, 6]], int)
844
+ assert_array_equal(x, a)
845
+
846
+ @pytest.mark.skipif(IS_PYPY and sys.implementation.version <= (7, 3, 8),
847
+ reason="PyPy bug in error formatting")
848
+ def test_comments_multi_chars(self):
849
+ c = TextIO()
850
+ c.write('/* comment\n1,2,3,5\n')
851
+ c.seek(0)
852
+ x = np.loadtxt(c, dtype=int, delimiter=',',
853
+ comments='/*')
854
+ a = np.array([1, 2, 3, 5], int)
855
+ assert_array_equal(x, a)
856
+
857
+ # Check that '/*' is not transformed to ['/', '*']
858
+ c = TextIO()
859
+ c.write('*/ comment\n1,2,3,5\n')
860
+ c.seek(0)
861
+ assert_raises(ValueError, np.loadtxt, c, dtype=int, delimiter=',',
862
+ comments='/*')
863
+
864
+ def test_skiprows(self):
865
+ c = TextIO()
866
+ c.write('comment\n1,2,3,5\n')
867
+ c.seek(0)
868
+ x = np.loadtxt(c, dtype=int, delimiter=',',
869
+ skiprows=1)
870
+ a = np.array([1, 2, 3, 5], int)
871
+ assert_array_equal(x, a)
872
+
873
+ c = TextIO()
874
+ c.write('# comment\n1,2,3,5\n')
875
+ c.seek(0)
876
+ x = np.loadtxt(c, dtype=int, delimiter=',',
877
+ skiprows=1)
878
+ a = np.array([1, 2, 3, 5], int)
879
+ assert_array_equal(x, a)
880
+
881
+ def test_usecols(self):
882
+ a = np.array([[1, 2], [3, 4]], float)
883
+ c = BytesIO()
884
+ np.savetxt(c, a)
885
+ c.seek(0)
886
+ x = np.loadtxt(c, dtype=float, usecols=(1,))
887
+ assert_array_equal(x, a[:, 1])
888
+
889
+ a = np.array([[1, 2, 3], [3, 4, 5]], float)
890
+ c = BytesIO()
891
+ np.savetxt(c, a)
892
+ c.seek(0)
893
+ x = np.loadtxt(c, dtype=float, usecols=(1, 2))
894
+ assert_array_equal(x, a[:, 1:])
895
+
896
+ # Testing with arrays instead of tuples.
897
+ c.seek(0)
898
+ x = np.loadtxt(c, dtype=float, usecols=np.array([1, 2]))
899
+ assert_array_equal(x, a[:, 1:])
900
+
901
+ # Testing with an integer instead of a sequence
902
+ for int_type in [int, np.int8, np.int16,
903
+ np.int32, np.int64, np.uint8, np.uint16,
904
+ np.uint32, np.uint64]:
905
+ to_read = int_type(1)
906
+ c.seek(0)
907
+ x = np.loadtxt(c, dtype=float, usecols=to_read)
908
+ assert_array_equal(x, a[:, 1])
909
+
910
+ # Testing with some crazy custom integer type
911
+ class CrazyInt:
912
+ def __index__(self):
913
+ return 1
914
+
915
+ crazy_int = CrazyInt()
916
+ c.seek(0)
917
+ x = np.loadtxt(c, dtype=float, usecols=crazy_int)
918
+ assert_array_equal(x, a[:, 1])
919
+
920
+ c.seek(0)
921
+ x = np.loadtxt(c, dtype=float, usecols=(crazy_int,))
922
+ assert_array_equal(x, a[:, 1])
923
+
924
+ # Checking with dtypes defined converters.
925
+ data = '''JOE 70.1 25.3
926
+ BOB 60.5 27.9
927
+ '''
928
+ c = TextIO(data)
929
+ names = ['stid', 'temp']
930
+ dtypes = ['S4', 'f8']
931
+ arr = np.loadtxt(c, usecols=(0, 2), dtype=list(zip(names, dtypes)))
932
+ assert_equal(arr['stid'], [b"JOE", b"BOB"])
933
+ assert_equal(arr['temp'], [25.3, 27.9])
934
+
935
+ # Testing non-ints in usecols
936
+ c.seek(0)
937
+ bogus_idx = 1.5
938
+ assert_raises_regex(
939
+ TypeError,
940
+ f'^usecols must be.*{type(bogus_idx).__name__}',
941
+ np.loadtxt, c, usecols=bogus_idx
942
+ )
943
+
944
+ assert_raises_regex(
945
+ TypeError,
946
+ f'^usecols must be.*{type(bogus_idx).__name__}',
947
+ np.loadtxt, c, usecols=[0, bogus_idx, 0]
948
+ )
949
+
950
+ def test_bad_usecols(self):
951
+ with pytest.raises(OverflowError):
952
+ np.loadtxt(["1\n"], usecols=[2**64], delimiter=",")
953
+ with pytest.raises((ValueError, OverflowError)):
954
+ # Overflow error on 32bit platforms
955
+ np.loadtxt(["1\n"], usecols=[2**62], delimiter=",")
956
+ with pytest.raises(TypeError,
957
+ match="If a structured dtype .*. But 1 usecols were given and "
958
+ "the number of fields is 3."):
959
+ np.loadtxt(["1,1\n"], dtype="i,2i", usecols=[0], delimiter=",")
960
+
961
+ def test_fancy_dtype(self):
962
+ c = TextIO()
963
+ c.write('1,2,3.0\n4,5,6.0\n')
964
+ c.seek(0)
965
+ dt = np.dtype([('x', int), ('y', [('t', int), ('s', float)])])
966
+ x = np.loadtxt(c, dtype=dt, delimiter=',')
967
+ a = np.array([(1, (2, 3.0)), (4, (5, 6.0))], dt)
968
+ assert_array_equal(x, a)
969
+
970
+ def test_shaped_dtype(self):
971
+ c = TextIO("aaaa 1.0 8.0 1 2 3 4 5 6")
972
+ dt = np.dtype([('name', 'S4'), ('x', float), ('y', float),
973
+ ('block', int, (2, 3))])
974
+ x = np.loadtxt(c, dtype=dt)
975
+ a = np.array([('aaaa', 1.0, 8.0, [[1, 2, 3], [4, 5, 6]])],
976
+ dtype=dt)
977
+ assert_array_equal(x, a)
978
+
979
+ def test_3d_shaped_dtype(self):
980
+ c = TextIO("aaaa 1.0 8.0 1 2 3 4 5 6 7 8 9 10 11 12")
981
+ dt = np.dtype([('name', 'S4'), ('x', float), ('y', float),
982
+ ('block', int, (2, 2, 3))])
983
+ x = np.loadtxt(c, dtype=dt)
984
+ a = np.array([('aaaa', 1.0, 8.0,
985
+ [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])],
986
+ dtype=dt)
987
+ assert_array_equal(x, a)
988
+
989
+ def test_str_dtype(self):
990
+ # see gh-8033
991
+ c = ["str1", "str2"]
992
+
993
+ for dt in (str, np.bytes_):
994
+ a = np.array(["str1", "str2"], dtype=dt)
995
+ x = np.loadtxt(c, dtype=dt)
996
+ assert_array_equal(x, a)
997
+
998
+ def test_empty_file(self):
999
+ with pytest.warns(UserWarning, match="input contained no data"):
1000
+ c = TextIO()
1001
+ x = np.loadtxt(c)
1002
+ assert_equal(x.shape, (0,))
1003
+ x = np.loadtxt(c, dtype=np.int64)
1004
+ assert_equal(x.shape, (0,))
1005
+ assert_(x.dtype == np.int64)
1006
+
1007
+ def test_unused_converter(self):
1008
+ c = TextIO()
1009
+ c.writelines(['1 21\n', '3 42\n'])
1010
+ c.seek(0)
1011
+ data = np.loadtxt(c, usecols=(1,),
1012
+ converters={0: lambda s: int(s, 16)})
1013
+ assert_array_equal(data, [21, 42])
1014
+
1015
+ c.seek(0)
1016
+ data = np.loadtxt(c, usecols=(1,),
1017
+ converters={1: lambda s: int(s, 16)})
1018
+ assert_array_equal(data, [33, 66])
1019
+
1020
+ def test_dtype_with_object(self):
1021
+ # Test using an explicit dtype with an object
1022
+ data = """ 1; 2001-01-01
1023
+ 2; 2002-01-31 """
1024
+ ndtype = [('idx', int), ('code', object)]
1025
+ func = lambda s: strptime(s.strip(), "%Y-%m-%d")
1026
+ converters = {1: func}
1027
+ test = np.loadtxt(TextIO(data), delimiter=";", dtype=ndtype,
1028
+ converters=converters)
1029
+ control = np.array(
1030
+ [(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))],
1031
+ dtype=ndtype)
1032
+ assert_equal(test, control)
1033
+
1034
+ def test_uint64_type(self):
1035
+ tgt = (9223372043271415339, 9223372043271415853)
1036
+ c = TextIO()
1037
+ c.write("%s %s" % tgt)
1038
+ c.seek(0)
1039
+ res = np.loadtxt(c, dtype=np.uint64)
1040
+ assert_equal(res, tgt)
1041
+
1042
+ def test_int64_type(self):
1043
+ tgt = (-9223372036854775807, 9223372036854775807)
1044
+ c = TextIO()
1045
+ c.write("%s %s" % tgt)
1046
+ c.seek(0)
1047
+ res = np.loadtxt(c, dtype=np.int64)
1048
+ assert_equal(res, tgt)
1049
+
1050
+ def test_from_float_hex(self):
1051
+ # IEEE doubles and floats only, otherwise the float32
1052
+ # conversion may fail.
1053
+ tgt = np.logspace(-10, 10, 5).astype(np.float32)
1054
+ tgt = np.hstack((tgt, -tgt)).astype(float)
1055
+ inp = '\n'.join(map(float.hex, tgt))
1056
+ c = TextIO()
1057
+ c.write(inp)
1058
+ for dt in [float, np.float32]:
1059
+ c.seek(0)
1060
+ res = np.loadtxt(
1061
+ c, dtype=dt, converters=float.fromhex, encoding="latin1")
1062
+ assert_equal(res, tgt, err_msg=f"{dt}")
1063
+
1064
+ @pytest.mark.skipif(IS_PYPY and sys.implementation.version <= (7, 3, 8),
1065
+ reason="PyPy bug in error formatting")
1066
+ def test_default_float_converter_no_default_hex_conversion(self):
1067
+ """
1068
+ Ensure that fromhex is only used for values with the correct prefix and
1069
+ is not called by default. Regression test related to gh-19598.
1070
+ """
1071
+ c = TextIO("a b c")
1072
+ with pytest.raises(ValueError,
1073
+ match=".*convert string 'a' to float64 at row 0, column 1"):
1074
+ np.loadtxt(c)
1075
+
1076
+ @pytest.mark.skipif(IS_PYPY and sys.implementation.version <= (7, 3, 8),
1077
+ reason="PyPy bug in error formatting")
1078
+ def test_default_float_converter_exception(self):
1079
+ """
1080
+ Ensure that the exception message raised during failed floating point
1081
+ conversion is correct. Regression test related to gh-19598.
1082
+ """
1083
+ c = TextIO("qrs tuv") # Invalid values for default float converter
1084
+ with pytest.raises(ValueError,
1085
+ match="could not convert string 'qrs' to float64"):
1086
+ np.loadtxt(c)
1087
+
1088
+ def test_from_complex(self):
1089
+ tgt = (complex(1, 1), complex(1, -1))
1090
+ c = TextIO()
1091
+ c.write("%s %s" % tgt)
1092
+ c.seek(0)
1093
+ res = np.loadtxt(c, dtype=complex)
1094
+ assert_equal(res, tgt)
1095
+
1096
+ def test_complex_misformatted(self):
1097
+ # test for backward compatibility
1098
+ # some complex formats used to generate x+-yj
1099
+ a = np.zeros((2, 2), dtype=np.complex128)
1100
+ re = np.pi
1101
+ im = np.e
1102
+ a[:] = re - 1.0j * im
1103
+ c = BytesIO()
1104
+ np.savetxt(c, a, fmt='%.16e')
1105
+ c.seek(0)
1106
+ txt = c.read()
1107
+ c.seek(0)
1108
+ # misformat the sign on the imaginary part, gh 7895
1109
+ txt_bad = txt.replace(b'e+00-', b'e00+-')
1110
+ assert_(txt_bad != txt)
1111
+ c.write(txt_bad)
1112
+ c.seek(0)
1113
+ res = np.loadtxt(c, dtype=complex)
1114
+ assert_equal(res, a)
1115
+
1116
+ def test_universal_newline(self):
1117
+ with temppath() as name:
1118
+ with open(name, 'w') as f:
1119
+ f.write('1 21\r3 42\r')
1120
+ data = np.loadtxt(name)
1121
+ assert_array_equal(data, [[1, 21], [3, 42]])
1122
+
1123
+ def test_empty_field_after_tab(self):
1124
+ c = TextIO()
1125
+ c.write('1 \t2 \t3\tstart \n4\t5\t6\t \n7\t8\t9.5\t')
1126
+ c.seek(0)
1127
+ dt = {'names': ('x', 'y', 'z', 'comment'),
1128
+ 'formats': ('<i4', '<i4', '<f4', '|S8')}
1129
+ x = np.loadtxt(c, dtype=dt, delimiter='\t')
1130
+ a = np.array([b'start ', b' ', b''])
1131
+ assert_array_equal(x['comment'], a)
1132
+
1133
+ def test_unpack_structured(self):
1134
+ txt = TextIO("M 21 72\nF 35 58")
1135
+ dt = {'names': ('a', 'b', 'c'), 'formats': ('|S1', '<i4', '<f4')}
1136
+ a, b, c = np.loadtxt(txt, dtype=dt, unpack=True)
1137
+ assert_(a.dtype.str == '|S1')
1138
+ assert_(b.dtype.str == '<i4')
1139
+ assert_(c.dtype.str == '<f4')
1140
+ assert_array_equal(a, np.array([b'M', b'F']))
1141
+ assert_array_equal(b, np.array([21, 35]))
1142
+ assert_array_equal(c, np.array([72., 58.]))
1143
+
1144
+ def test_ndmin_keyword(self):
1145
+ c = TextIO()
1146
+ c.write('1,2,3\n4,5,6')
1147
+ c.seek(0)
1148
+ assert_raises(ValueError, np.loadtxt, c, ndmin=3)
1149
+ c.seek(0)
1150
+ assert_raises(ValueError, np.loadtxt, c, ndmin=1.5)
1151
+ c.seek(0)
1152
+ x = np.loadtxt(c, dtype=int, delimiter=',', ndmin=1)
1153
+ a = np.array([[1, 2, 3], [4, 5, 6]])
1154
+ assert_array_equal(x, a)
1155
+
1156
+ d = TextIO()
1157
+ d.write('0,1,2')
1158
+ d.seek(0)
1159
+ x = np.loadtxt(d, dtype=int, delimiter=',', ndmin=2)
1160
+ assert_(x.shape == (1, 3))
1161
+ d.seek(0)
1162
+ x = np.loadtxt(d, dtype=int, delimiter=',', ndmin=1)
1163
+ assert_(x.shape == (3,))
1164
+ d.seek(0)
1165
+ x = np.loadtxt(d, dtype=int, delimiter=',', ndmin=0)
1166
+ assert_(x.shape == (3,))
1167
+
1168
+ e = TextIO()
1169
+ e.write('0\n1\n2')
1170
+ e.seek(0)
1171
+ x = np.loadtxt(e, dtype=int, delimiter=',', ndmin=2)
1172
+ assert_(x.shape == (3, 1))
1173
+ e.seek(0)
1174
+ x = np.loadtxt(e, dtype=int, delimiter=',', ndmin=1)
1175
+ assert_(x.shape == (3,))
1176
+ e.seek(0)
1177
+ x = np.loadtxt(e, dtype=int, delimiter=',', ndmin=0)
1178
+ assert_(x.shape == (3,))
1179
+
1180
+ # Test ndmin kw with empty file.
1181
+ with pytest.warns(UserWarning, match="input contained no data"):
1182
+ f = TextIO()
1183
+ assert_(np.loadtxt(f, ndmin=2).shape == (0, 1,))
1184
+ assert_(np.loadtxt(f, ndmin=1).shape == (0,))
1185
+
1186
+ def test_generator_source(self):
1187
+ def count():
1188
+ for i in range(10):
1189
+ yield "%d" % i
1190
+
1191
+ res = np.loadtxt(count())
1192
+ assert_array_equal(res, np.arange(10))
1193
+
1194
+ def test_bad_line(self):
1195
+ c = TextIO()
1196
+ c.write('1 2 3\n4 5 6\n2 3')
1197
+ c.seek(0)
1198
+
1199
+ # Check for exception and that exception contains line number
1200
+ assert_raises_regex(ValueError, "3", np.loadtxt, c)
1201
+
1202
+ def test_none_as_string(self):
1203
+ # gh-5155, None should work as string when format demands it
1204
+ c = TextIO()
1205
+ c.write('100,foo,200\n300,None,400')
1206
+ c.seek(0)
1207
+ dt = np.dtype([('x', int), ('a', 'S10'), ('y', int)])
1208
+ np.loadtxt(c, delimiter=',', dtype=dt, comments=None) # Should succeed
1209
+
1210
+ @pytest.mark.skipif(locale.getpreferredencoding() == 'ANSI_X3.4-1968',
1211
+ reason="Wrong preferred encoding")
1212
+ def test_binary_load(self):
1213
+ butf8 = b"5,6,7,\xc3\x95scarscar\r\n15,2,3,hello\r\n"\
1214
+ b"20,2,3,\xc3\x95scar\r\n"
1215
+ sutf8 = butf8.decode("UTF-8").replace("\r", "").splitlines()
1216
+ with temppath() as path:
1217
+ with open(path, "wb") as f:
1218
+ f.write(butf8)
1219
+ with open(path, "rb") as f:
1220
+ x = np.loadtxt(f, encoding="UTF-8", dtype=np.str_)
1221
+ assert_array_equal(x, sutf8)
1222
+ # test broken latin1 conversion people now rely on
1223
+ with open(path, "rb") as f:
1224
+ x = np.loadtxt(f, encoding="UTF-8", dtype="S")
1225
+ x = [b'5,6,7,\xc3\x95scarscar', b'15,2,3,hello', b'20,2,3,\xc3\x95scar']
1226
+ assert_array_equal(x, np.array(x, dtype="S"))
1227
+
1228
+ def test_max_rows(self):
1229
+ c = TextIO()
1230
+ c.write('1,2,3,5\n4,5,7,8\n2,1,4,5')
1231
+ c.seek(0)
1232
+ x = np.loadtxt(c, dtype=int, delimiter=',',
1233
+ max_rows=1)
1234
+ a = np.array([1, 2, 3, 5], int)
1235
+ assert_array_equal(x, a)
1236
+
1237
+ def test_max_rows_with_skiprows(self):
1238
+ c = TextIO()
1239
+ c.write('comments\n1,2,3,5\n4,5,7,8\n2,1,4,5')
1240
+ c.seek(0)
1241
+ x = np.loadtxt(c, dtype=int, delimiter=',',
1242
+ skiprows=1, max_rows=1)
1243
+ a = np.array([1, 2, 3, 5], int)
1244
+ assert_array_equal(x, a)
1245
+
1246
+ c = TextIO()
1247
+ c.write('comment\n1,2,3,5\n4,5,7,8\n2,1,4,5')
1248
+ c.seek(0)
1249
+ x = np.loadtxt(c, dtype=int, delimiter=',',
1250
+ skiprows=1, max_rows=2)
1251
+ a = np.array([[1, 2, 3, 5], [4, 5, 7, 8]], int)
1252
+ assert_array_equal(x, a)
1253
+
1254
+ def test_max_rows_with_read_continuation(self):
1255
+ c = TextIO()
1256
+ c.write('1,2,3,5\n4,5,7,8\n2,1,4,5')
1257
+ c.seek(0)
1258
+ x = np.loadtxt(c, dtype=int, delimiter=',',
1259
+ max_rows=2)
1260
+ a = np.array([[1, 2, 3, 5], [4, 5, 7, 8]], int)
1261
+ assert_array_equal(x, a)
1262
+ # test continuation
1263
+ x = np.loadtxt(c, dtype=int, delimiter=',')
1264
+ a = np.array([2, 1, 4, 5], int)
1265
+ assert_array_equal(x, a)
1266
+
1267
+ def test_max_rows_larger(self):
1268
+ #test max_rows > num rows
1269
+ c = TextIO()
1270
+ c.write('comment\n1,2,3,5\n4,5,7,8\n2,1,4,5')
1271
+ c.seek(0)
1272
+ x = np.loadtxt(c, dtype=int, delimiter=',',
1273
+ skiprows=1, max_rows=6)
1274
+ a = np.array([[1, 2, 3, 5], [4, 5, 7, 8], [2, 1, 4, 5]], int)
1275
+ assert_array_equal(x, a)
1276
+
1277
+ @pytest.mark.parametrize(["skip", "data"], [
1278
+ (1, ["ignored\n", "1,2\n", "\n", "3,4\n"]),
1279
+ # "Bad" lines that do not end in newlines:
1280
+ (1, ["ignored", "1,2", "", "3,4"]),
1281
+ (1, lambda: StringIO("ignored\n1,2\n\n3,4")),
1282
+ # Same as above, but do not skip any lines:
1283
+ (0, ["-1,0\n", "1,2\n", "\n", "3,4\n"]),
1284
+ (0, ["-1,0", "1,2", "", "3,4"]),
1285
+ (0, lambda: StringIO("-1,0\n1,2\n\n3,4"))])
1286
+ def test_max_rows_empty_lines(self, skip, data):
1287
+ # gh-26718 re-instantiate StringIO objects each time
1288
+ if callable(data):
1289
+ data = data()
1290
+
1291
+ with pytest.warns(UserWarning,
1292
+ match=f"Input line 3.*max_rows={3 - skip}"):
1293
+ res = np.loadtxt(data, dtype=int, skiprows=skip, delimiter=",",
1294
+ max_rows=3 - skip)
1295
+ assert_array_equal(res, [[-1, 0], [1, 2], [3, 4]][skip:])
1296
+
1297
+ if isinstance(data, StringIO):
1298
+ data.seek(0)
1299
+
1300
+ with warnings.catch_warnings():
1301
+ warnings.simplefilter("error", UserWarning)
1302
+ with pytest.raises(UserWarning):
1303
+ np.loadtxt(data, dtype=int, skiprows=skip, delimiter=",",
1304
+ max_rows=3 - skip)
1305
+
1306
+ class Testfromregex:
1307
+ def test_record(self):
1308
+ c = TextIO()
1309
+ c.write('1.312 foo\n1.534 bar\n4.444 qux')
1310
+ c.seek(0)
1311
+
1312
+ dt = [('num', np.float64), ('val', 'S3')]
1313
+ x = np.fromregex(c, r"([0-9.]+)\s+(...)", dt)
1314
+ a = np.array([(1.312, 'foo'), (1.534, 'bar'), (4.444, 'qux')],
1315
+ dtype=dt)
1316
+ assert_array_equal(x, a)
1317
+
1318
+ def test_record_2(self):
1319
+ c = TextIO()
1320
+ c.write('1312 foo\n1534 bar\n4444 qux')
1321
+ c.seek(0)
1322
+
1323
+ dt = [('num', np.int32), ('val', 'S3')]
1324
+ x = np.fromregex(c, r"(\d+)\s+(...)", dt)
1325
+ a = np.array([(1312, 'foo'), (1534, 'bar'), (4444, 'qux')],
1326
+ dtype=dt)
1327
+ assert_array_equal(x, a)
1328
+
1329
+ def test_record_3(self):
1330
+ c = TextIO()
1331
+ c.write('1312 foo\n1534 bar\n4444 qux')
1332
+ c.seek(0)
1333
+
1334
+ dt = [('num', np.float64)]
1335
+ x = np.fromregex(c, r"(\d+)\s+...", dt)
1336
+ a = np.array([(1312,), (1534,), (4444,)], dtype=dt)
1337
+ assert_array_equal(x, a)
1338
+
1339
+ @pytest.mark.parametrize("path_type", [str, Path])
1340
+ def test_record_unicode(self, path_type):
1341
+ utf8 = b'\xcf\x96'
1342
+ with temppath() as str_path:
1343
+ path = path_type(str_path)
1344
+ with open(path, 'wb') as f:
1345
+ f.write(b'1.312 foo' + utf8 + b' \n1.534 bar\n4.444 qux')
1346
+
1347
+ dt = [('num', np.float64), ('val', 'U4')]
1348
+ x = np.fromregex(path, r"(?u)([0-9.]+)\s+(\w+)", dt, encoding='UTF-8')
1349
+ a = np.array([(1.312, 'foo' + utf8.decode('UTF-8')), (1.534, 'bar'),
1350
+ (4.444, 'qux')], dtype=dt)
1351
+ assert_array_equal(x, a)
1352
+
1353
+ regexp = re.compile(r"([0-9.]+)\s+(\w+)", re.UNICODE)
1354
+ x = np.fromregex(path, regexp, dt, encoding='UTF-8')
1355
+ assert_array_equal(x, a)
1356
+
1357
+ def test_compiled_bytes(self):
1358
+ regexp = re.compile(br'(\d)')
1359
+ c = BytesIO(b'123')
1360
+ dt = [('num', np.float64)]
1361
+ a = np.array([1, 2, 3], dtype=dt)
1362
+ x = np.fromregex(c, regexp, dt)
1363
+ assert_array_equal(x, a)
1364
+
1365
+ def test_bad_dtype_not_structured(self):
1366
+ regexp = re.compile(br'(\d)')
1367
+ c = BytesIO(b'123')
1368
+ with pytest.raises(TypeError, match='structured datatype'):
1369
+ np.fromregex(c, regexp, dtype=np.float64)
1370
+
1371
+
1372
+ #####--------------------------------------------------------------------------
1373
+
1374
+
1375
+ class TestFromTxt(LoadTxtBase):
1376
+ loadfunc = staticmethod(np.genfromtxt)
1377
+
1378
+ def test_record(self):
1379
+ # Test w/ explicit dtype
1380
+ data = TextIO('1 2\n3 4')
1381
+ test = np.genfromtxt(data, dtype=[('x', np.int32), ('y', np.int32)])
1382
+ control = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
1383
+ assert_equal(test, control)
1384
+ #
1385
+ data = TextIO('M 64.0 75.0\nF 25.0 60.0')
1386
+ descriptor = {'names': ('gender', 'age', 'weight'),
1387
+ 'formats': ('S1', 'i4', 'f4')}
1388
+ control = np.array([('M', 64.0, 75.0), ('F', 25.0, 60.0)],
1389
+ dtype=descriptor)
1390
+ test = np.genfromtxt(data, dtype=descriptor)
1391
+ assert_equal(test, control)
1392
+
1393
+ def test_array(self):
1394
+ # Test outputting a standard ndarray
1395
+ data = TextIO('1 2\n3 4')
1396
+ control = np.array([[1, 2], [3, 4]], dtype=int)
1397
+ test = np.genfromtxt(data, dtype=int)
1398
+ assert_array_equal(test, control)
1399
+ #
1400
+ data.seek(0)
1401
+ control = np.array([[1, 2], [3, 4]], dtype=float)
1402
+ test = np.loadtxt(data, dtype=float)
1403
+ assert_array_equal(test, control)
1404
+
1405
+ def test_1D(self):
1406
+ # Test squeezing to 1D
1407
+ control = np.array([1, 2, 3, 4], int)
1408
+ #
1409
+ data = TextIO('1\n2\n3\n4\n')
1410
+ test = np.genfromtxt(data, dtype=int)
1411
+ assert_array_equal(test, control)
1412
+ #
1413
+ data = TextIO('1,2,3,4\n')
1414
+ test = np.genfromtxt(data, dtype=int, delimiter=',')
1415
+ assert_array_equal(test, control)
1416
+
1417
+ def test_comments(self):
1418
+ # Test the stripping of comments
1419
+ control = np.array([1, 2, 3, 5], int)
1420
+ # Comment on its own line
1421
+ data = TextIO('# comment\n1,2,3,5\n')
1422
+ test = np.genfromtxt(data, dtype=int, delimiter=',', comments='#')
1423
+ assert_equal(test, control)
1424
+ # Comment at the end of a line
1425
+ data = TextIO('1,2,3,5# comment\n')
1426
+ test = np.genfromtxt(data, dtype=int, delimiter=',', comments='#')
1427
+ assert_equal(test, control)
1428
+
1429
+ def test_skiprows(self):
1430
+ # Test row skipping
1431
+ control = np.array([1, 2, 3, 5], int)
1432
+ kwargs = {"dtype": int, "delimiter": ','}
1433
+ #
1434
+ data = TextIO('comment\n1,2,3,5\n')
1435
+ test = np.genfromtxt(data, skip_header=1, **kwargs)
1436
+ assert_equal(test, control)
1437
+ #
1438
+ data = TextIO('# comment\n1,2,3,5\n')
1439
+ test = np.loadtxt(data, skiprows=1, **kwargs)
1440
+ assert_equal(test, control)
1441
+
1442
+ def test_skip_footer(self):
1443
+ data = [f"# {i}" for i in range(1, 6)]
1444
+ data.append("A, B, C")
1445
+ data.extend([f"{i},{i:3.1f},{i:03d}" for i in range(51)])
1446
+ data[-1] = "99,99"
1447
+ kwargs = {"delimiter": ",", "names": True, "skip_header": 5, "skip_footer": 10}
1448
+ test = np.genfromtxt(TextIO("\n".join(data)), **kwargs)
1449
+ ctrl = np.array([(f"{i:f}", f"{i:f}", f"{i:f}") for i in range(41)],
1450
+ dtype=[(_, float) for _ in "ABC"])
1451
+ assert_equal(test, ctrl)
1452
+
1453
+ def test_skip_footer_with_invalid(self):
1454
+ with warnings.catch_warnings():
1455
+ warnings.simplefilter('ignore', ConversionWarning)
1456
+ basestr = '1 1\n2 2\n3 3\n4 4\n5 \n6 \n7 \n'
1457
+ # Footer too small to get rid of all invalid values
1458
+ assert_raises(ValueError, np.genfromtxt,
1459
+ TextIO(basestr), skip_footer=1)
1460
+ # except ValueError:
1461
+ # pass
1462
+ a = np.genfromtxt(
1463
+ TextIO(basestr), skip_footer=1, invalid_raise=False)
1464
+ assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
1465
+ #
1466
+ a = np.genfromtxt(TextIO(basestr), skip_footer=3)
1467
+ assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
1468
+ #
1469
+ basestr = '1 1\n2 \n3 3\n4 4\n5 \n6 6\n7 7\n'
1470
+ a = np.genfromtxt(
1471
+ TextIO(basestr), skip_footer=1, invalid_raise=False)
1472
+ assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.], [6., 6.]]))
1473
+ a = np.genfromtxt(
1474
+ TextIO(basestr), skip_footer=3, invalid_raise=False)
1475
+ assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.]]))
1476
+
1477
+ def test_header(self):
1478
+ # Test retrieving a header
1479
+ data = TextIO('gender age weight\nM 64.0 75.0\nF 25.0 60.0')
1480
+ with warnings.catch_warnings(record=True) as w:
1481
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
1482
+ test = np.genfromtxt(data, dtype=None, names=True,
1483
+ encoding='bytes')
1484
+ assert_(w[0].category is VisibleDeprecationWarning)
1485
+ control = {'gender': np.array([b'M', b'F']),
1486
+ 'age': np.array([64.0, 25.0]),
1487
+ 'weight': np.array([75.0, 60.0])}
1488
+ assert_equal(test['gender'], control['gender'])
1489
+ assert_equal(test['age'], control['age'])
1490
+ assert_equal(test['weight'], control['weight'])
1491
+
1492
+ def test_auto_dtype(self):
1493
+ # Test the automatic definition of the output dtype
1494
+ data = TextIO('A 64 75.0 3+4j True\nBCD 25 60.0 5+6j False')
1495
+ with warnings.catch_warnings(record=True) as w:
1496
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
1497
+ test = np.genfromtxt(data, dtype=None, encoding='bytes')
1498
+ assert_(w[0].category is VisibleDeprecationWarning)
1499
+ control = [np.array([b'A', b'BCD']),
1500
+ np.array([64, 25]),
1501
+ np.array([75.0, 60.0]),
1502
+ np.array([3 + 4j, 5 + 6j]),
1503
+ np.array([True, False]), ]
1504
+ assert_equal(test.dtype.names, ['f0', 'f1', 'f2', 'f3', 'f4'])
1505
+ for (i, ctrl) in enumerate(control):
1506
+ assert_equal(test[f'f{i}'], ctrl)
1507
+
1508
+ def test_auto_dtype_uniform(self):
1509
+ # Tests whether the output dtype can be uniformized
1510
+ data = TextIO('1 2 3 4\n5 6 7 8\n')
1511
+ test = np.genfromtxt(data, dtype=None)
1512
+ control = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
1513
+ assert_equal(test, control)
1514
+
1515
+ def test_fancy_dtype(self):
1516
+ # Check that a nested dtype isn't MIA
1517
+ data = TextIO('1,2,3.0\n4,5,6.0\n')
1518
+ fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])])
1519
+ test = np.genfromtxt(data, dtype=fancydtype, delimiter=',')
1520
+ control = np.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype)
1521
+ assert_equal(test, control)
1522
+
1523
+ def test_names_overwrite(self):
1524
+ # Test overwriting the names of the dtype
1525
+ descriptor = {'names': ('g', 'a', 'w'),
1526
+ 'formats': ('S1', 'i4', 'f4')}
1527
+ data = TextIO(b'M 64.0 75.0\nF 25.0 60.0')
1528
+ names = ('gender', 'age', 'weight')
1529
+ test = np.genfromtxt(data, dtype=descriptor, names=names)
1530
+ descriptor['names'] = names
1531
+ control = np.array([('M', 64.0, 75.0),
1532
+ ('F', 25.0, 60.0)], dtype=descriptor)
1533
+ assert_equal(test, control)
1534
+
1535
+ def test_bad_fname(self):
1536
+ with pytest.raises(TypeError, match='fname must be a string,'):
1537
+ np.genfromtxt(123)
1538
+
1539
+ def test_commented_header(self):
1540
+ # Check that names can be retrieved even if the line is commented out.
1541
+ data = TextIO("""
1542
+ #gender age weight
1543
+ M 21 72.100000
1544
+ F 35 58.330000
1545
+ M 33 21.99
1546
+ """)
1547
+ # The # is part of the first name and should be deleted automatically.
1548
+ with warnings.catch_warnings(record=True) as w:
1549
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
1550
+ test = np.genfromtxt(data, names=True, dtype=None,
1551
+ encoding="bytes")
1552
+ assert_(w[0].category is VisibleDeprecationWarning)
1553
+ ctrl = np.array([('M', 21, 72.1), ('F', 35, 58.33), ('M', 33, 21.99)],
1554
+ dtype=[('gender', '|S1'), ('age', int), ('weight', float)])
1555
+ assert_equal(test, ctrl)
1556
+ # Ditto, but we should get rid of the first element
1557
+ data = TextIO(b"""
1558
+ # gender age weight
1559
+ M 21 72.100000
1560
+ F 35 58.330000
1561
+ M 33 21.99
1562
+ """)
1563
+ with warnings.catch_warnings(record=True) as w:
1564
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
1565
+ test = np.genfromtxt(data, names=True, dtype=None,
1566
+ encoding="bytes")
1567
+ assert_(w[0].category is VisibleDeprecationWarning)
1568
+ assert_equal(test, ctrl)
1569
+
1570
+ def test_names_and_comments_none(self):
1571
+ # Tests case when names is true but comments is None (gh-10780)
1572
+ data = TextIO('col1 col2\n 1 2\n 3 4')
1573
+ test = np.genfromtxt(data, dtype=(int, int), comments=None, names=True)
1574
+ control = np.array([(1, 2), (3, 4)], dtype=[('col1', int), ('col2', int)])
1575
+ assert_equal(test, control)
1576
+
1577
+ def test_file_is_closed_on_error(self):
1578
+ # gh-13200
1579
+ with tempdir() as tmpdir:
1580
+ fpath = os.path.join(tmpdir, "test.csv")
1581
+ with open(fpath, "wb") as f:
1582
+ f.write('\N{GREEK PI SYMBOL}'.encode())
1583
+
1584
+ # ResourceWarnings are emitted from a destructor, so won't be
1585
+ # detected by regular propagation to errors.
1586
+ with assert_no_warnings():
1587
+ with pytest.raises(UnicodeDecodeError):
1588
+ np.genfromtxt(fpath, encoding="ascii")
1589
+
1590
+ def test_autonames_and_usecols(self):
1591
+ # Tests names and usecols
1592
+ data = TextIO('A B C D\n aaaa 121 45 9.1')
1593
+ with warnings.catch_warnings(record=True) as w:
1594
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
1595
+ test = np.genfromtxt(data, usecols=('A', 'C', 'D'),
1596
+ names=True, dtype=None, encoding="bytes")
1597
+ assert_(w[0].category is VisibleDeprecationWarning)
1598
+ control = np.array(('aaaa', 45, 9.1),
1599
+ dtype=[('A', '|S4'), ('C', int), ('D', float)])
1600
+ assert_equal(test, control)
1601
+
1602
+ def test_converters_with_usecols(self):
1603
+ # Test the combination user-defined converters and usecol
1604
+ data = TextIO('1,2,3,,5\n6,7,8,9,10\n')
1605
+ test = np.genfromtxt(data, dtype=int, delimiter=',',
1606
+ converters={3: lambda s: int(s or - 999)},
1607
+ usecols=(1, 3,))
1608
+ control = np.array([[2, -999], [7, 9]], int)
1609
+ assert_equal(test, control)
1610
+
1611
+ def test_converters_with_usecols_and_names(self):
1612
+ # Tests names and usecols
1613
+ data = TextIO('A B C D\n aaaa 121 45 9.1')
1614
+ with warnings.catch_warnings(record=True) as w:
1615
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
1616
+ test = np.genfromtxt(data, usecols=('A', 'C', 'D'), names=True,
1617
+ dtype=None, encoding="bytes",
1618
+ converters={'C': lambda s: 2 * int(s)})
1619
+ assert_(w[0].category is VisibleDeprecationWarning)
1620
+ control = np.array(('aaaa', 90, 9.1),
1621
+ dtype=[('A', '|S4'), ('C', int), ('D', float)])
1622
+ assert_equal(test, control)
1623
+
1624
+ def test_converters_cornercases(self):
1625
+ # Test the conversion to datetime.
1626
+ converter = {
1627
+ 'date': lambda s: strptime(s, '%Y-%m-%d %H:%M:%SZ')}
1628
+ data = TextIO('2009-02-03 12:00:00Z, 72214.0')
1629
+ test = np.genfromtxt(data, delimiter=',', dtype=None,
1630
+ names=['date', 'stid'], converters=converter)
1631
+ control = np.array((datetime(2009, 2, 3), 72214.),
1632
+ dtype=[('date', np.object_), ('stid', float)])
1633
+ assert_equal(test, control)
1634
+
1635
+ def test_converters_cornercases2(self):
1636
+ # Test the conversion to datetime64.
1637
+ converter = {
1638
+ 'date': lambda s: np.datetime64(strptime(s, '%Y-%m-%d %H:%M:%SZ'))}
1639
+ data = TextIO('2009-02-03 12:00:00Z, 72214.0')
1640
+ test = np.genfromtxt(data, delimiter=',', dtype=None,
1641
+ names=['date', 'stid'], converters=converter)
1642
+ control = np.array((datetime(2009, 2, 3), 72214.),
1643
+ dtype=[('date', 'datetime64[us]'), ('stid', float)])
1644
+ assert_equal(test, control)
1645
+
1646
+ def test_unused_converter(self):
1647
+ # Test whether unused converters are forgotten
1648
+ data = TextIO("1 21\n 3 42\n")
1649
+ test = np.genfromtxt(data, usecols=(1,),
1650
+ converters={0: lambda s: int(s, 16)})
1651
+ assert_equal(test, [21, 42])
1652
+ #
1653
+ data.seek(0)
1654
+ test = np.genfromtxt(data, usecols=(1,),
1655
+ converters={1: lambda s: int(s, 16)})
1656
+ assert_equal(test, [33, 66])
1657
+
1658
+ def test_invalid_converter(self):
1659
+ strip_rand = lambda x: float((b'r' in x.lower() and x.split()[-1]) or
1660
+ ((b'r' not in x.lower() and x.strip()) or 0.0))
1661
+ strip_per = lambda x: float((b'%' in x.lower() and x.split()[0]) or
1662
+ ((b'%' not in x.lower() and x.strip()) or 0.0))
1663
+ s = TextIO("D01N01,10/1/2003 ,1 %,R 75,400,600\r\n"
1664
+ "L24U05,12/5/2003, 2 %,1,300, 150.5\r\n"
1665
+ "D02N03,10/10/2004,R 1,,7,145.55")
1666
+ kwargs = {
1667
+ "converters": {2: strip_per, 3: strip_rand}, "delimiter": ",",
1668
+ "dtype": None, "encoding": "bytes"}
1669
+ assert_raises(ConverterError, np.genfromtxt, s, **kwargs)
1670
+
1671
+ def test_tricky_converter_bug1666(self):
1672
+ # Test some corner cases
1673
+ s = TextIO('q1,2\nq3,4')
1674
+ cnv = lambda s: float(s[1:])
1675
+ test = np.genfromtxt(s, delimiter=',', converters={0: cnv})
1676
+ control = np.array([[1., 2.], [3., 4.]])
1677
+ assert_equal(test, control)
1678
+
1679
+ def test_dtype_with_converters(self):
1680
+ dstr = "2009; 23; 46"
1681
+ test = np.genfromtxt(TextIO(dstr,),
1682
+ delimiter=";", dtype=float, converters={0: bytes})
1683
+ control = np.array([('2009', 23., 46)],
1684
+ dtype=[('f0', '|S4'), ('f1', float), ('f2', float)])
1685
+ assert_equal(test, control)
1686
+ test = np.genfromtxt(TextIO(dstr,),
1687
+ delimiter=";", dtype=float, converters={0: float})
1688
+ control = np.array([2009., 23., 46],)
1689
+ assert_equal(test, control)
1690
+
1691
+ @pytest.mark.filterwarnings("ignore:.*recfromcsv.*:DeprecationWarning")
1692
+ def test_dtype_with_converters_and_usecols(self):
1693
+ dstr = "1,5,-1,1:1\n2,8,-1,1:n\n3,3,-2,m:n\n"
1694
+ dmap = {'1:1': 0, '1:n': 1, 'm:1': 2, 'm:n': 3}
1695
+ dtyp = [('e1', 'i4'), ('e2', 'i4'), ('e3', 'i2'), ('n', 'i1')]
1696
+ conv = {0: int, 1: int, 2: int, 3: lambda r: dmap[r.decode()]}
1697
+ test = recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
1698
+ names=None, converters=conv, encoding="bytes")
1699
+ control = np.rec.array([(1, 5, -1, 0), (2, 8, -1, 1), (3, 3, -2, 3)],
1700
+ dtype=dtyp)
1701
+ assert_equal(test, control)
1702
+ dtyp = [('e1', 'i4'), ('e2', 'i4'), ('n', 'i1')]
1703
+ test = recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
1704
+ usecols=(0, 1, 3), names=None, converters=conv,
1705
+ encoding="bytes")
1706
+ control = np.rec.array([(1, 5, 0), (2, 8, 1), (3, 3, 3)], dtype=dtyp)
1707
+ assert_equal(test, control)
1708
+
1709
+ def test_dtype_with_object(self):
1710
+ # Test using an explicit dtype with an object
1711
+ data = """ 1; 2001-01-01
1712
+ 2; 2002-01-31 """
1713
+ ndtype = [('idx', int), ('code', object)]
1714
+ func = lambda s: strptime(s.strip(), "%Y-%m-%d")
1715
+ converters = {1: func}
1716
+ test = np.genfromtxt(TextIO(data), delimiter=";", dtype=ndtype,
1717
+ converters=converters)
1718
+ control = np.array(
1719
+ [(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))],
1720
+ dtype=ndtype)
1721
+ assert_equal(test, control)
1722
+
1723
+ ndtype = [('nest', [('idx', int), ('code', object)])]
1724
+ with assert_raises_regex(NotImplementedError,
1725
+ 'Nested fields.* not supported.*'):
1726
+ test = np.genfromtxt(TextIO(data), delimiter=";",
1727
+ dtype=ndtype, converters=converters)
1728
+
1729
+ # nested but empty fields also aren't supported
1730
+ ndtype = [('idx', int), ('code', object), ('nest', [])]
1731
+ with assert_raises_regex(NotImplementedError,
1732
+ 'Nested fields.* not supported.*'):
1733
+ test = np.genfromtxt(TextIO(data), delimiter=";",
1734
+ dtype=ndtype, converters=converters)
1735
+
1736
+ def test_dtype_with_object_no_converter(self):
1737
+ # Object without a converter uses bytes:
1738
+ parsed = np.genfromtxt(TextIO("1"), dtype=object)
1739
+ assert parsed[()] == b"1"
1740
+ parsed = np.genfromtxt(TextIO("string"), dtype=object)
1741
+ assert parsed[()] == b"string"
1742
+
1743
+ def test_userconverters_with_explicit_dtype(self):
1744
+ # Test user_converters w/ explicit (standard) dtype
1745
+ data = TextIO('skip,skip,2001-01-01,1.0,skip')
1746
+ test = np.genfromtxt(data, delimiter=",", names=None, dtype=float,
1747
+ usecols=(2, 3), converters={2: bytes})
1748
+ control = np.array([('2001-01-01', 1.)],
1749
+ dtype=[('', '|S10'), ('', float)])
1750
+ assert_equal(test, control)
1751
+
1752
+ def test_utf8_userconverters_with_explicit_dtype(self):
1753
+ utf8 = b'\xcf\x96'
1754
+ with temppath() as path:
1755
+ with open(path, 'wb') as f:
1756
+ f.write(b'skip,skip,2001-01-01' + utf8 + b',1.0,skip')
1757
+ test = np.genfromtxt(path, delimiter=",", names=None, dtype=float,
1758
+ usecols=(2, 3), converters={2: str},
1759
+ encoding='UTF-8')
1760
+ control = np.array([('2001-01-01' + utf8.decode('UTF-8'), 1.)],
1761
+ dtype=[('', '|U11'), ('', float)])
1762
+ assert_equal(test, control)
1763
+
1764
+ def test_spacedelimiter(self):
1765
+ # Test space delimiter
1766
+ data = TextIO("1 2 3 4 5\n6 7 8 9 10")
1767
+ test = np.genfromtxt(data)
1768
+ control = np.array([[1., 2., 3., 4., 5.],
1769
+ [6., 7., 8., 9., 10.]])
1770
+ assert_equal(test, control)
1771
+
1772
+ def test_integer_delimiter(self):
1773
+ # Test using an integer for delimiter
1774
+ data = " 1 2 3\n 4 5 67\n890123 4"
1775
+ test = np.genfromtxt(TextIO(data), delimiter=3)
1776
+ control = np.array([[1, 2, 3], [4, 5, 67], [890, 123, 4]])
1777
+ assert_equal(test, control)
1778
+
1779
+ def test_missing(self):
1780
+ data = TextIO('1,2,3,,5\n')
1781
+ test = np.genfromtxt(data, dtype=int, delimiter=',',
1782
+ converters={3: lambda s: int(s or - 999)})
1783
+ control = np.array([1, 2, 3, -999, 5], int)
1784
+ assert_equal(test, control)
1785
+
1786
+ def test_missing_with_tabs(self):
1787
+ # Test w/ a delimiter tab
1788
+ txt = "1\t2\t3\n\t2\t\n1\t\t3"
1789
+ test = np.genfromtxt(TextIO(txt), delimiter="\t",
1790
+ usemask=True,)
1791
+ ctrl_d = np.array([(1, 2, 3), (np.nan, 2, np.nan), (1, np.nan, 3)],)
1792
+ ctrl_m = np.array([(0, 0, 0), (1, 0, 1), (0, 1, 0)], dtype=bool)
1793
+ assert_equal(test.data, ctrl_d)
1794
+ assert_equal(test.mask, ctrl_m)
1795
+
1796
+ def test_usecols(self):
1797
+ # Test the selection of columns
1798
+ # Select 1 column
1799
+ control = np.array([[1, 2], [3, 4]], float)
1800
+ data = TextIO()
1801
+ np.savetxt(data, control)
1802
+ data.seek(0)
1803
+ test = np.genfromtxt(data, dtype=float, usecols=(1,))
1804
+ assert_equal(test, control[:, 1])
1805
+ #
1806
+ control = np.array([[1, 2, 3], [3, 4, 5]], float)
1807
+ data = TextIO()
1808
+ np.savetxt(data, control)
1809
+ data.seek(0)
1810
+ test = np.genfromtxt(data, dtype=float, usecols=(1, 2))
1811
+ assert_equal(test, control[:, 1:])
1812
+ # Testing with arrays instead of tuples.
1813
+ data.seek(0)
1814
+ test = np.genfromtxt(data, dtype=float, usecols=np.array([1, 2]))
1815
+ assert_equal(test, control[:, 1:])
1816
+
1817
+ def test_usecols_as_css(self):
1818
+ # Test giving usecols with a comma-separated string
1819
+ data = "1 2 3\n4 5 6"
1820
+ test = np.genfromtxt(TextIO(data),
1821
+ names="a, b, c", usecols="a, c")
1822
+ ctrl = np.array([(1, 3), (4, 6)], dtype=[(_, float) for _ in "ac"])
1823
+ assert_equal(test, ctrl)
1824
+
1825
+ def test_usecols_with_structured_dtype(self):
1826
+ # Test usecols with an explicit structured dtype
1827
+ data = TextIO("JOE 70.1 25.3\nBOB 60.5 27.9")
1828
+ names = ['stid', 'temp']
1829
+ dtypes = ['S4', 'f8']
1830
+ test = np.genfromtxt(
1831
+ data, usecols=(0, 2), dtype=list(zip(names, dtypes)))
1832
+ assert_equal(test['stid'], [b"JOE", b"BOB"])
1833
+ assert_equal(test['temp'], [25.3, 27.9])
1834
+
1835
+ def test_usecols_with_integer(self):
1836
+ # Test usecols with an integer
1837
+ test = np.genfromtxt(TextIO(b"1 2 3\n4 5 6"), usecols=0)
1838
+ assert_equal(test, np.array([1., 4.]))
1839
+
1840
+ def test_usecols_with_named_columns(self):
1841
+ # Test usecols with named columns
1842
+ ctrl = np.array([(1, 3), (4, 6)], dtype=[('a', float), ('c', float)])
1843
+ data = "1 2 3\n4 5 6"
1844
+ kwargs = {"names": "a, b, c"}
1845
+ test = np.genfromtxt(TextIO(data), usecols=(0, -1), **kwargs)
1846
+ assert_equal(test, ctrl)
1847
+ test = np.genfromtxt(TextIO(data),
1848
+ usecols=('a', 'c'), **kwargs)
1849
+ assert_equal(test, ctrl)
1850
+
1851
+ def test_empty_file(self):
1852
+ # Test that an empty file raises the proper warning.
1853
+ with warnings.catch_warnings():
1854
+ warnings.filterwarnings('ignore', message="genfromtxt: Empty input file:")
1855
+ data = TextIO()
1856
+ test = np.genfromtxt(data)
1857
+ assert_equal(test, np.array([]))
1858
+
1859
+ # when skip_header > 0
1860
+ test = np.genfromtxt(data, skip_header=1)
1861
+ assert_equal(test, np.array([]))
1862
+
1863
+ def test_fancy_dtype_alt(self):
1864
+ # Check that a nested dtype isn't MIA
1865
+ data = TextIO('1,2,3.0\n4,5,6.0\n')
1866
+ fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])])
1867
+ test = np.genfromtxt(data, dtype=fancydtype, delimiter=',', usemask=True)
1868
+ control = ma.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype)
1869
+ assert_equal(test, control)
1870
+
1871
+ def test_shaped_dtype(self):
1872
+ c = TextIO("aaaa 1.0 8.0 1 2 3 4 5 6")
1873
+ dt = np.dtype([('name', 'S4'), ('x', float), ('y', float),
1874
+ ('block', int, (2, 3))])
1875
+ x = np.genfromtxt(c, dtype=dt)
1876
+ a = np.array([('aaaa', 1.0, 8.0, [[1, 2, 3], [4, 5, 6]])],
1877
+ dtype=dt)
1878
+ assert_array_equal(x, a)
1879
+
1880
+ def test_withmissing(self):
1881
+ data = TextIO('A,B\n0,1\n2,N/A')
1882
+ kwargs = {"delimiter": ",", "missing_values": "N/A", "names": True}
1883
+ test = np.genfromtxt(data, dtype=None, usemask=True, **kwargs)
1884
+ control = ma.array([(0, 1), (2, -1)],
1885
+ mask=[(False, False), (False, True)],
1886
+ dtype=[('A', int), ('B', int)])
1887
+ assert_equal(test, control)
1888
+ assert_equal(test.mask, control.mask)
1889
+ #
1890
+ data.seek(0)
1891
+ test = np.genfromtxt(data, usemask=True, **kwargs)
1892
+ control = ma.array([(0, 1), (2, -1)],
1893
+ mask=[(False, False), (False, True)],
1894
+ dtype=[('A', float), ('B', float)])
1895
+ assert_equal(test, control)
1896
+ assert_equal(test.mask, control.mask)
1897
+
1898
+ def test_user_missing_values(self):
1899
+ data = "A, B, C\n0, 0., 0j\n1, N/A, 1j\n-9, 2.2, N/A\n3, -99, 3j"
1900
+ basekwargs = {"dtype": None, "delimiter": ",", "names": True}
1901
+ mdtype = [('A', int), ('B', float), ('C', complex)]
1902
+ #
1903
+ test = np.genfromtxt(TextIO(data), missing_values="N/A",
1904
+ **basekwargs)
1905
+ control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
1906
+ (-9, 2.2, -999j), (3, -99, 3j)],
1907
+ mask=[(0, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)],
1908
+ dtype=mdtype)
1909
+ assert_equal(test, control)
1910
+ #
1911
+ basekwargs['dtype'] = mdtype
1912
+ test = np.genfromtxt(TextIO(data),
1913
+ missing_values={0: -9, 1: -99, 2: -999j},
1914
+ usemask=True, **basekwargs)
1915
+ control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
1916
+ (-9, 2.2, -999j), (3, -99, 3j)],
1917
+ mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
1918
+ dtype=mdtype)
1919
+ assert_equal(test, control)
1920
+ #
1921
+ test = np.genfromtxt(TextIO(data),
1922
+ missing_values={0: -9, 'B': -99, 'C': -999j},
1923
+ usemask=True,
1924
+ **basekwargs)
1925
+ control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
1926
+ (-9, 2.2, -999j), (3, -99, 3j)],
1927
+ mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
1928
+ dtype=mdtype)
1929
+ assert_equal(test, control)
1930
+
1931
+ def test_user_filling_values(self):
1932
+ # Test with missing and filling values
1933
+ ctrl = np.array([(0, 3), (4, -999)], dtype=[('a', int), ('b', int)])
1934
+ data = "N/A, 2, 3\n4, ,???"
1935
+ kwargs = {"delimiter": ",",
1936
+ "dtype": int,
1937
+ "names": "a,b,c",
1938
+ "missing_values": {0: "N/A", 'b': " ", 2: "???"},
1939
+ "filling_values": {0: 0, 'b': 0, 2: -999}}
1940
+ test = np.genfromtxt(TextIO(data), **kwargs)
1941
+ ctrl = np.array([(0, 2, 3), (4, 0, -999)],
1942
+ dtype=[(_, int) for _ in "abc"])
1943
+ assert_equal(test, ctrl)
1944
+ #
1945
+ test = np.genfromtxt(TextIO(data), usecols=(0, -1), **kwargs)
1946
+ ctrl = np.array([(0, 3), (4, -999)], dtype=[(_, int) for _ in "ac"])
1947
+ assert_equal(test, ctrl)
1948
+
1949
+ data2 = "1,2,*,4\n5,*,7,8\n"
1950
+ test = np.genfromtxt(TextIO(data2), delimiter=',', dtype=int,
1951
+ missing_values="*", filling_values=0)
1952
+ ctrl = np.array([[1, 2, 0, 4], [5, 0, 7, 8]])
1953
+ assert_equal(test, ctrl)
1954
+ test = np.genfromtxt(TextIO(data2), delimiter=',', dtype=int,
1955
+ missing_values="*", filling_values=-1)
1956
+ ctrl = np.array([[1, 2, -1, 4], [5, -1, 7, 8]])
1957
+ assert_equal(test, ctrl)
1958
+
1959
+ def test_withmissing_float(self):
1960
+ data = TextIO('A,B\n0,1.5\n2,-999.00')
1961
+ test = np.genfromtxt(data, dtype=None, delimiter=',',
1962
+ missing_values='-999.0', names=True, usemask=True)
1963
+ control = ma.array([(0, 1.5), (2, -1.)],
1964
+ mask=[(False, False), (False, True)],
1965
+ dtype=[('A', int), ('B', float)])
1966
+ assert_equal(test, control)
1967
+ assert_equal(test.mask, control.mask)
1968
+
1969
+ def test_with_masked_column_uniform(self):
1970
+ # Test masked column
1971
+ data = TextIO('1 2 3\n4 5 6\n')
1972
+ test = np.genfromtxt(data, dtype=None,
1973
+ missing_values='2,5', usemask=True)
1974
+ control = ma.array([[1, 2, 3], [4, 5, 6]], mask=[[0, 1, 0], [0, 1, 0]])
1975
+ assert_equal(test, control)
1976
+
1977
+ def test_with_masked_column_various(self):
1978
+ # Test masked column
1979
+ data = TextIO('True 2 3\nFalse 5 6\n')
1980
+ test = np.genfromtxt(data, dtype=None,
1981
+ missing_values='2,5', usemask=True)
1982
+ control = ma.array([(1, 2, 3), (0, 5, 6)],
1983
+ mask=[(0, 1, 0), (0, 1, 0)],
1984
+ dtype=[('f0', bool), ('f1', bool), ('f2', int)])
1985
+ assert_equal(test, control)
1986
+
1987
+ def test_invalid_raise(self):
1988
+ # Test invalid raise
1989
+ data = ["1, 1, 1, 1, 1"] * 50
1990
+ for i in range(5):
1991
+ data[10 * i] = "2, 2, 2, 2 2"
1992
+ data.insert(0, "a, b, c, d, e")
1993
+ mdata = TextIO("\n".join(data))
1994
+
1995
+ kwargs = {"delimiter": ",", "dtype": None, "names": True}
1996
+
1997
+ def f():
1998
+ return np.genfromtxt(mdata, invalid_raise=False, **kwargs)
1999
+ mtest = pytest.warns(ConversionWarning, f)
2000
+ assert_equal(len(mtest), 45)
2001
+ assert_equal(mtest, np.ones(45, dtype=[(_, int) for _ in 'abcde']))
2002
+ #
2003
+ mdata.seek(0)
2004
+ assert_raises(ValueError, np.genfromtxt, mdata,
2005
+ delimiter=",", names=True)
2006
+
2007
+ def test_invalid_raise_with_usecols(self):
2008
+ # Test invalid_raise with usecols
2009
+ data = ["1, 1, 1, 1, 1"] * 50
2010
+ for i in range(5):
2011
+ data[10 * i] = "2, 2, 2, 2 2"
2012
+ data.insert(0, "a, b, c, d, e")
2013
+ mdata = TextIO("\n".join(data))
2014
+
2015
+ kwargs = {"delimiter": ",", "dtype": None, "names": True,
2016
+ "invalid_raise": False}
2017
+
2018
+ def f():
2019
+ return np.genfromtxt(mdata, usecols=(0, 4), **kwargs)
2020
+ mtest = pytest.warns(ConversionWarning, f)
2021
+ assert_equal(len(mtest), 45)
2022
+ assert_equal(mtest, np.ones(45, dtype=[(_, int) for _ in 'ae']))
2023
+ #
2024
+ mdata.seek(0)
2025
+ mtest = np.genfromtxt(mdata, usecols=(0, 1), **kwargs)
2026
+ assert_equal(len(mtest), 50)
2027
+ control = np.ones(50, dtype=[(_, int) for _ in 'ab'])
2028
+ control[[10 * _ for _ in range(5)]] = (2, 2)
2029
+ assert_equal(mtest, control)
2030
+
2031
+ def test_inconsistent_dtype(self):
2032
+ # Test inconsistent dtype
2033
+ data = ["1, 1, 1, 1, -1.1"] * 50
2034
+ mdata = TextIO("\n".join(data))
2035
+
2036
+ converters = {4: lambda x: f"({x.decode()})"}
2037
+ kwargs = {"delimiter": ",", "converters": converters,
2038
+ "dtype": [(_, int) for _ in 'abcde'], "encoding": "bytes"}
2039
+ assert_raises(ValueError, np.genfromtxt, mdata, **kwargs)
2040
+
2041
+ def test_default_field_format(self):
2042
+ # Test default format
2043
+ data = "0, 1, 2.3\n4, 5, 6.7"
2044
+ mtest = np.genfromtxt(TextIO(data),
2045
+ delimiter=",", dtype=None, defaultfmt="f%02i")
2046
+ ctrl = np.array([(0, 1, 2.3), (4, 5, 6.7)],
2047
+ dtype=[("f00", int), ("f01", int), ("f02", float)])
2048
+ assert_equal(mtest, ctrl)
2049
+
2050
+ def test_single_dtype_wo_names(self):
2051
+ # Test single dtype w/o names
2052
+ data = "0, 1, 2.3\n4, 5, 6.7"
2053
+ mtest = np.genfromtxt(TextIO(data),
2054
+ delimiter=",", dtype=float, defaultfmt="f%02i")
2055
+ ctrl = np.array([[0., 1., 2.3], [4., 5., 6.7]], dtype=float)
2056
+ assert_equal(mtest, ctrl)
2057
+
2058
+ def test_single_dtype_w_explicit_names(self):
2059
+ # Test single dtype w explicit names
2060
+ data = "0, 1, 2.3\n4, 5, 6.7"
2061
+ mtest = np.genfromtxt(TextIO(data),
2062
+ delimiter=",", dtype=float, names="a, b, c")
2063
+ ctrl = np.array([(0., 1., 2.3), (4., 5., 6.7)],
2064
+ dtype=[(_, float) for _ in "abc"])
2065
+ assert_equal(mtest, ctrl)
2066
+
2067
+ def test_single_dtype_w_implicit_names(self):
2068
+ # Test single dtype w implicit names
2069
+ data = "a, b, c\n0, 1, 2.3\n4, 5, 6.7"
2070
+ mtest = np.genfromtxt(TextIO(data),
2071
+ delimiter=",", dtype=float, names=True)
2072
+ ctrl = np.array([(0., 1., 2.3), (4., 5., 6.7)],
2073
+ dtype=[(_, float) for _ in "abc"])
2074
+ assert_equal(mtest, ctrl)
2075
+
2076
+ def test_easy_structured_dtype(self):
2077
+ # Test easy structured dtype
2078
+ data = "0, 1, 2.3\n4, 5, 6.7"
2079
+ mtest = np.genfromtxt(TextIO(data), delimiter=",",
2080
+ dtype=(int, float, float), defaultfmt="f_%02i")
2081
+ ctrl = np.array([(0, 1., 2.3), (4, 5., 6.7)],
2082
+ dtype=[("f_00", int), ("f_01", float), ("f_02", float)])
2083
+ assert_equal(mtest, ctrl)
2084
+
2085
+ def test_autostrip(self):
2086
+ # Test autostrip
2087
+ data = "01/01/2003 , 1.3, abcde"
2088
+ kwargs = {"delimiter": ",", "dtype": None, "encoding": "bytes"}
2089
+ with warnings.catch_warnings(record=True) as w:
2090
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
2091
+ mtest = np.genfromtxt(TextIO(data), **kwargs)
2092
+ assert_(w[0].category is VisibleDeprecationWarning)
2093
+ ctrl = np.array([('01/01/2003 ', 1.3, ' abcde')],
2094
+ dtype=[('f0', '|S12'), ('f1', float), ('f2', '|S8')])
2095
+ assert_equal(mtest, ctrl)
2096
+ with warnings.catch_warnings(record=True) as w:
2097
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
2098
+ mtest = np.genfromtxt(TextIO(data), autostrip=True, **kwargs)
2099
+ assert_(w[0].category is VisibleDeprecationWarning)
2100
+ ctrl = np.array([('01/01/2003', 1.3, 'abcde')],
2101
+ dtype=[('f0', '|S10'), ('f1', float), ('f2', '|S5')])
2102
+ assert_equal(mtest, ctrl)
2103
+
2104
+ def test_replace_space(self):
2105
+ # Test the 'replace_space' option
2106
+ txt = "A.A, B (B), C:C\n1, 2, 3.14"
2107
+ # Test default: replace ' ' by '_' and delete non-alphanum chars
2108
+ test = np.genfromtxt(TextIO(txt),
2109
+ delimiter=",", names=True, dtype=None)
2110
+ ctrl_dtype = [("AA", int), ("B_B", int), ("CC", float)]
2111
+ ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
2112
+ assert_equal(test, ctrl)
2113
+ # Test: no replace, no delete
2114
+ test = np.genfromtxt(TextIO(txt),
2115
+ delimiter=",", names=True, dtype=None,
2116
+ replace_space='', deletechars='')
2117
+ ctrl_dtype = [("A.A", int), ("B (B)", int), ("C:C", float)]
2118
+ ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
2119
+ assert_equal(test, ctrl)
2120
+ # Test: no delete (spaces are replaced by _)
2121
+ test = np.genfromtxt(TextIO(txt),
2122
+ delimiter=",", names=True, dtype=None,
2123
+ deletechars='')
2124
+ ctrl_dtype = [("A.A", int), ("B_(B)", int), ("C:C", float)]
2125
+ ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
2126
+ assert_equal(test, ctrl)
2127
+
2128
+ def test_replace_space_known_dtype(self):
2129
+ # Test the 'replace_space' (and related) options when dtype != None
2130
+ txt = "A.A, B (B), C:C\n1, 2, 3"
2131
+ # Test default: replace ' ' by '_' and delete non-alphanum chars
2132
+ test = np.genfromtxt(TextIO(txt),
2133
+ delimiter=",", names=True, dtype=int)
2134
+ ctrl_dtype = [("AA", int), ("B_B", int), ("CC", int)]
2135
+ ctrl = np.array((1, 2, 3), dtype=ctrl_dtype)
2136
+ assert_equal(test, ctrl)
2137
+ # Test: no replace, no delete
2138
+ test = np.genfromtxt(TextIO(txt),
2139
+ delimiter=",", names=True, dtype=int,
2140
+ replace_space='', deletechars='')
2141
+ ctrl_dtype = [("A.A", int), ("B (B)", int), ("C:C", int)]
2142
+ ctrl = np.array((1, 2, 3), dtype=ctrl_dtype)
2143
+ assert_equal(test, ctrl)
2144
+ # Test: no delete (spaces are replaced by _)
2145
+ test = np.genfromtxt(TextIO(txt),
2146
+ delimiter=",", names=True, dtype=int,
2147
+ deletechars='')
2148
+ ctrl_dtype = [("A.A", int), ("B_(B)", int), ("C:C", int)]
2149
+ ctrl = np.array((1, 2, 3), dtype=ctrl_dtype)
2150
+ assert_equal(test, ctrl)
2151
+
2152
+ def test_incomplete_names(self):
2153
+ # Test w/ incomplete names
2154
+ data = "A,,C\n0,1,2\n3,4,5"
2155
+ kwargs = {"delimiter": ",", "names": True}
2156
+ # w/ dtype=None
2157
+ ctrl = np.array([(0, 1, 2), (3, 4, 5)],
2158
+ dtype=[(_, int) for _ in ('A', 'f0', 'C')])
2159
+ test = np.genfromtxt(TextIO(data), dtype=None, **kwargs)
2160
+ assert_equal(test, ctrl)
2161
+ # w/ default dtype
2162
+ ctrl = np.array([(0, 1, 2), (3, 4, 5)],
2163
+ dtype=[(_, float) for _ in ('A', 'f0', 'C')])
2164
+ test = np.genfromtxt(TextIO(data), **kwargs)
2165
+
2166
+ def test_names_auto_completion(self):
2167
+ # Make sure that names are properly completed
2168
+ data = "1 2 3\n 4 5 6"
2169
+ test = np.genfromtxt(TextIO(data),
2170
+ dtype=(int, float, int), names="a")
2171
+ ctrl = np.array([(1, 2, 3), (4, 5, 6)],
2172
+ dtype=[('a', int), ('f0', float), ('f1', int)])
2173
+ assert_equal(test, ctrl)
2174
+
2175
+ def test_names_with_usecols_bug1636(self):
2176
+ # Make sure we pick up the right names w/ usecols
2177
+ data = "A,B,C,D,E\n0,1,2,3,4\n0,1,2,3,4\n0,1,2,3,4"
2178
+ ctrl_names = ("A", "C", "E")
2179
+ test = np.genfromtxt(TextIO(data),
2180
+ dtype=(int, int, int), delimiter=",",
2181
+ usecols=(0, 2, 4), names=True)
2182
+ assert_equal(test.dtype.names, ctrl_names)
2183
+ #
2184
+ test = np.genfromtxt(TextIO(data),
2185
+ dtype=(int, int, int), delimiter=",",
2186
+ usecols=("A", "C", "E"), names=True)
2187
+ assert_equal(test.dtype.names, ctrl_names)
2188
+ #
2189
+ test = np.genfromtxt(TextIO(data),
2190
+ dtype=int, delimiter=",",
2191
+ usecols=("A", "C", "E"), names=True)
2192
+ assert_equal(test.dtype.names, ctrl_names)
2193
+
2194
+ def test_fixed_width_names(self):
2195
+ # Test fix-width w/ names
2196
+ data = " A B C\n 0 1 2.3\n 45 67 9."
2197
+ kwargs = {"delimiter": (5, 5, 4), "names": True, "dtype": None}
2198
+ ctrl = np.array([(0, 1, 2.3), (45, 67, 9.)],
2199
+ dtype=[('A', int), ('B', int), ('C', float)])
2200
+ test = np.genfromtxt(TextIO(data), **kwargs)
2201
+ assert_equal(test, ctrl)
2202
+ #
2203
+ kwargs = {"delimiter": 5, "names": True, "dtype": None}
2204
+ ctrl = np.array([(0, 1, 2.3), (45, 67, 9.)],
2205
+ dtype=[('A', int), ('B', int), ('C', float)])
2206
+ test = np.genfromtxt(TextIO(data), **kwargs)
2207
+ assert_equal(test, ctrl)
2208
+
2209
+ def test_filling_values(self):
2210
+ # Test missing values
2211
+ data = b"1, 2, 3\n1, , 5\n0, 6, \n"
2212
+ kwargs = {"delimiter": ",", "dtype": None, "filling_values": -999}
2213
+ ctrl = np.array([[1, 2, 3], [1, -999, 5], [0, 6, -999]], dtype=int)
2214
+ test = np.genfromtxt(TextIO(data), **kwargs)
2215
+ assert_equal(test, ctrl)
2216
+
2217
+ def test_comments_is_none(self):
2218
+ # Github issue 329 (None was previously being converted to 'None').
2219
+ with warnings.catch_warnings(record=True) as w:
2220
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
2221
+ test = np.genfromtxt(TextIO("test1,testNonetherestofthedata"),
2222
+ dtype=None, comments=None, delimiter=',',
2223
+ encoding="bytes")
2224
+ assert_(w[0].category is VisibleDeprecationWarning)
2225
+ assert_equal(test[1], b'testNonetherestofthedata')
2226
+ with warnings.catch_warnings(record=True) as w:
2227
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
2228
+ test = np.genfromtxt(TextIO("test1, testNonetherestofthedata"),
2229
+ dtype=None, comments=None, delimiter=',',
2230
+ encoding="bytes")
2231
+ assert_(w[0].category is VisibleDeprecationWarning)
2232
+ assert_equal(test[1], b' testNonetherestofthedata')
2233
+
2234
+ def test_latin1(self):
2235
+ latin1 = b'\xf6\xfc\xf6'
2236
+ norm = b"norm1,norm2,norm3\n"
2237
+ enc = b"test1,testNonethe" + latin1 + b",test3\n"
2238
+ s = norm + enc + norm
2239
+ with warnings.catch_warnings(record=True) as w:
2240
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
2241
+ test = np.genfromtxt(TextIO(s),
2242
+ dtype=None, comments=None, delimiter=',',
2243
+ encoding="bytes")
2244
+ assert_(w[0].category is VisibleDeprecationWarning)
2245
+ assert_equal(test[1, 0], b"test1")
2246
+ assert_equal(test[1, 1], b"testNonethe" + latin1)
2247
+ assert_equal(test[1, 2], b"test3")
2248
+ test = np.genfromtxt(TextIO(s),
2249
+ dtype=None, comments=None, delimiter=',',
2250
+ encoding='latin1')
2251
+ assert_equal(test[1, 0], "test1")
2252
+ assert_equal(test[1, 1], "testNonethe" + latin1.decode('latin1'))
2253
+ assert_equal(test[1, 2], "test3")
2254
+
2255
+ with warnings.catch_warnings(record=True) as w:
2256
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
2257
+ test = np.genfromtxt(TextIO(b"0,testNonethe" + latin1),
2258
+ dtype=None, comments=None, delimiter=',',
2259
+ encoding="bytes")
2260
+ assert_(w[0].category is VisibleDeprecationWarning)
2261
+ assert_equal(test['f0'], 0)
2262
+ assert_equal(test['f1'], b"testNonethe" + latin1)
2263
+
2264
+ def test_binary_decode_autodtype(self):
2265
+ utf16 = b'\xff\xfeh\x04 \x00i\x04 \x00j\x04'
2266
+ v = self.loadfunc(BytesIO(utf16), dtype=None, encoding='UTF-16')
2267
+ assert_array_equal(v, np.array(utf16.decode('UTF-16').split()))
2268
+
2269
+ def test_utf8_byte_encoding(self):
2270
+ utf8 = b"\xcf\x96"
2271
+ norm = b"norm1,norm2,norm3\n"
2272
+ enc = b"test1,testNonethe" + utf8 + b",test3\n"
2273
+ s = norm + enc + norm
2274
+ with warnings.catch_warnings(record=True) as w:
2275
+ warnings.filterwarnings('always', '', VisibleDeprecationWarning)
2276
+ test = np.genfromtxt(TextIO(s),
2277
+ dtype=None, comments=None, delimiter=',',
2278
+ encoding="bytes")
2279
+ assert_(w[0].category is VisibleDeprecationWarning)
2280
+ ctl = np.array([
2281
+ [b'norm1', b'norm2', b'norm3'],
2282
+ [b'test1', b'testNonethe' + utf8, b'test3'],
2283
+ [b'norm1', b'norm2', b'norm3']])
2284
+ assert_array_equal(test, ctl)
2285
+
2286
+ def test_utf8_file(self):
2287
+ utf8 = b"\xcf\x96"
2288
+ with temppath() as path:
2289
+ with open(path, "wb") as f:
2290
+ f.write((b"test1,testNonethe" + utf8 + b",test3\n") * 2)
2291
+ test = np.genfromtxt(path, dtype=None, comments=None,
2292
+ delimiter=',', encoding="UTF-8")
2293
+ ctl = np.array([
2294
+ ["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"],
2295
+ ["test1", "testNonethe" + utf8.decode("UTF-8"), "test3"]],
2296
+ dtype=np.str_)
2297
+ assert_array_equal(test, ctl)
2298
+
2299
+ # test a mixed dtype
2300
+ with open(path, "wb") as f:
2301
+ f.write(b"0,testNonethe" + utf8)
2302
+ test = np.genfromtxt(path, dtype=None, comments=None,
2303
+ delimiter=',', encoding="UTF-8")
2304
+ assert_equal(test['f0'], 0)
2305
+ assert_equal(test['f1'], "testNonethe" + utf8.decode("UTF-8"))
2306
+
2307
+ def test_utf8_file_nodtype_unicode(self):
2308
+ # bytes encoding with non-latin1 -> unicode upcast
2309
+ utf8 = '\u03d6'
2310
+ latin1 = '\xf6\xfc\xf6'
2311
+
2312
+ # skip test if cannot encode utf8 test string with preferred
2313
+ # encoding. The preferred encoding is assumed to be the default
2314
+ # encoding of open. Will need to change this for PyTest, maybe
2315
+ # using pytest.mark.xfail(raises=***).
2316
+ try:
2317
+ encoding = locale.getpreferredencoding()
2318
+ utf8.encode(encoding)
2319
+ except (UnicodeError, ImportError):
2320
+ pytest.skip('Skipping test_utf8_file_nodtype_unicode, '
2321
+ 'unable to encode utf8 in preferred encoding')
2322
+
2323
+ with temppath() as path:
2324
+ with open(path, "wt") as f:
2325
+ f.write("norm1,norm2,norm3\n")
2326
+ f.write("norm1," + latin1 + ",norm3\n")
2327
+ f.write("test1,testNonethe" + utf8 + ",test3\n")
2328
+ with warnings.catch_warnings(record=True) as w:
2329
+ warnings.filterwarnings('always', '',
2330
+ VisibleDeprecationWarning)
2331
+ test = np.genfromtxt(path, dtype=None, comments=None,
2332
+ delimiter=',', encoding="bytes")
2333
+ # Check for warning when encoding not specified.
2334
+ assert_(w[0].category is VisibleDeprecationWarning)
2335
+ ctl = np.array([
2336
+ ["norm1", "norm2", "norm3"],
2337
+ ["norm1", latin1, "norm3"],
2338
+ ["test1", "testNonethe" + utf8, "test3"]],
2339
+ dtype=np.str_)
2340
+ assert_array_equal(test, ctl)
2341
+
2342
+ @pytest.mark.filterwarnings("ignore:.*recfromtxt.*:DeprecationWarning")
2343
+ def test_recfromtxt(self):
2344
+ #
2345
+ data = TextIO('A,B\n0,1\n2,3')
2346
+ kwargs = {"delimiter": ",", "missing_values": "N/A", "names": True}
2347
+ test = recfromtxt(data, **kwargs)
2348
+ control = np.array([(0, 1), (2, 3)],
2349
+ dtype=[('A', int), ('B', int)])
2350
+ assert_(isinstance(test, np.recarray))
2351
+ assert_equal(test, control)
2352
+ #
2353
+ data = TextIO('A,B\n0,1\n2,N/A')
2354
+ test = recfromtxt(data, dtype=None, usemask=True, **kwargs)
2355
+ control = ma.array([(0, 1), (2, -1)],
2356
+ mask=[(False, False), (False, True)],
2357
+ dtype=[('A', int), ('B', int)])
2358
+ assert_equal(test, control)
2359
+ assert_equal(test.mask, control.mask)
2360
+ assert_equal(test.A, [0, 2])
2361
+
2362
+ @pytest.mark.filterwarnings("ignore:.*recfromcsv.*:DeprecationWarning")
2363
+ def test_recfromcsv(self):
2364
+ #
2365
+ data = TextIO('A,B\n0,1\n2,3')
2366
+ kwargs = {"missing_values": "N/A", "names": True, "case_sensitive": True,
2367
+ "encoding": "bytes"}
2368
+ test = recfromcsv(data, dtype=None, **kwargs)
2369
+ control = np.array([(0, 1), (2, 3)],
2370
+ dtype=[('A', int), ('B', int)])
2371
+ assert_(isinstance(test, np.recarray))
2372
+ assert_equal(test, control)
2373
+ #
2374
+ data = TextIO('A,B\n0,1\n2,N/A')
2375
+ test = recfromcsv(data, dtype=None, usemask=True, **kwargs)
2376
+ control = ma.array([(0, 1), (2, -1)],
2377
+ mask=[(False, False), (False, True)],
2378
+ dtype=[('A', int), ('B', int)])
2379
+ assert_equal(test, control)
2380
+ assert_equal(test.mask, control.mask)
2381
+ assert_equal(test.A, [0, 2])
2382
+ #
2383
+ data = TextIO('A,B\n0,1\n2,3')
2384
+ test = recfromcsv(data, missing_values='N/A',)
2385
+ control = np.array([(0, 1), (2, 3)],
2386
+ dtype=[('a', int), ('b', int)])
2387
+ assert_(isinstance(test, np.recarray))
2388
+ assert_equal(test, control)
2389
+ #
2390
+ data = TextIO('A,B\n0,1\n2,3')
2391
+ dtype = [('a', int), ('b', float)]
2392
+ test = recfromcsv(data, missing_values='N/A', dtype=dtype)
2393
+ control = np.array([(0, 1), (2, 3)],
2394
+ dtype=dtype)
2395
+ assert_(isinstance(test, np.recarray))
2396
+ assert_equal(test, control)
2397
+
2398
+ # gh-10394
2399
+ data = TextIO('color\n"red"\n"blue"')
2400
+ test = recfromcsv(data, converters={0: lambda x: x.strip('\"')})
2401
+ control = np.array([('red',), ('blue',)], dtype=[('color', (str, 4))])
2402
+ assert_equal(test.dtype, control.dtype)
2403
+ assert_equal(test, control)
2404
+
2405
+ def test_max_rows(self):
2406
+ # Test the `max_rows` keyword argument.
2407
+ data = '1 2\n3 4\n5 6\n7 8\n9 10\n'
2408
+ txt = TextIO(data)
2409
+ a1 = np.genfromtxt(txt, max_rows=3)
2410
+ a2 = np.genfromtxt(txt)
2411
+ assert_equal(a1, [[1, 2], [3, 4], [5, 6]])
2412
+ assert_equal(a2, [[7, 8], [9, 10]])
2413
+
2414
+ # max_rows must be at least 1.
2415
+ assert_raises(ValueError, np.genfromtxt, TextIO(data), max_rows=0)
2416
+
2417
+ # An input with several invalid rows.
2418
+ data = '1 1\n2 2\n0 \n3 3\n4 4\n5 \n6 \n7 \n'
2419
+
2420
+ test = np.genfromtxt(TextIO(data), max_rows=2)
2421
+ control = np.array([[1., 1.], [2., 2.]])
2422
+ assert_equal(test, control)
2423
+
2424
+ # Test keywords conflict
2425
+ assert_raises(ValueError, np.genfromtxt, TextIO(data), skip_footer=1,
2426
+ max_rows=4)
2427
+
2428
+ # Test with invalid value
2429
+ assert_raises(ValueError, np.genfromtxt, TextIO(data), max_rows=4)
2430
+
2431
+ # Test with invalid not raise
2432
+ with warnings.catch_warnings():
2433
+ warnings.simplefilter('ignore', ConversionWarning)
2434
+
2435
+ test = np.genfromtxt(TextIO(data), max_rows=4, invalid_raise=False)
2436
+ control = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])
2437
+ assert_equal(test, control)
2438
+
2439
+ test = np.genfromtxt(TextIO(data), max_rows=5, invalid_raise=False)
2440
+ control = np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]])
2441
+ assert_equal(test, control)
2442
+
2443
+ # Structured array with field names.
2444
+ data = 'a b\n#c d\n1 1\n2 2\n#0 \n3 3\n4 4\n5 5\n'
2445
+
2446
+ # Test with header, names and comments
2447
+ txt = TextIO(data)
2448
+ test = np.genfromtxt(txt, skip_header=1, max_rows=3, names=True)
2449
+ control = np.array([(1.0, 1.0), (2.0, 2.0), (3.0, 3.0)],
2450
+ dtype=[('c', '<f8'), ('d', '<f8')])
2451
+ assert_equal(test, control)
2452
+ # To continue reading the same "file", don't use skip_header or
2453
+ # names, and use the previously determined dtype.
2454
+ test = np.genfromtxt(txt, max_rows=None, dtype=test.dtype)
2455
+ control = np.array([(4.0, 4.0), (5.0, 5.0)],
2456
+ dtype=[('c', '<f8'), ('d', '<f8')])
2457
+ assert_equal(test, control)
2458
+
2459
+ def test_gft_using_filename(self):
2460
+ # Test that we can load data from a filename as well as a file
2461
+ # object
2462
+ tgt = np.arange(6).reshape((2, 3))
2463
+ linesep = ('\n', '\r\n', '\r')
2464
+
2465
+ for sep in linesep:
2466
+ data = '0 1 2' + sep + '3 4 5'
2467
+ with temppath() as name:
2468
+ with open(name, 'w') as f:
2469
+ f.write(data)
2470
+ res = np.genfromtxt(name)
2471
+ assert_array_equal(res, tgt)
2472
+
2473
+ def test_gft_from_gzip(self):
2474
+ # Test that we can load data from a gzipped file
2475
+ wanted = np.arange(6).reshape((2, 3))
2476
+ linesep = ('\n', '\r\n', '\r')
2477
+
2478
+ for sep in linesep:
2479
+ data = '0 1 2' + sep + '3 4 5'
2480
+ s = BytesIO()
2481
+ with gzip.GzipFile(fileobj=s, mode='w') as g:
2482
+ g.write(asbytes(data))
2483
+
2484
+ with temppath(suffix='.gz2') as name:
2485
+ with open(name, 'w') as f:
2486
+ f.write(data)
2487
+ assert_array_equal(np.genfromtxt(name), wanted)
2488
+
2489
+ def test_gft_using_generator(self):
2490
+ # gft doesn't work with unicode.
2491
+ def count():
2492
+ for i in range(10):
2493
+ yield asbytes("%d" % i)
2494
+
2495
+ res = np.genfromtxt(count())
2496
+ assert_array_equal(res, np.arange(10))
2497
+
2498
+ def test_auto_dtype_largeint(self):
2499
+ # Regression test for numpy/numpy#5635 whereby large integers could
2500
+ # cause OverflowErrors.
2501
+
2502
+ # Test the automatic definition of the output dtype
2503
+ #
2504
+ # 2**66 = 73786976294838206464 => should convert to float
2505
+ # 2**34 = 17179869184 => should convert to int64
2506
+ # 2**10 = 1024 => should convert to int (int32 on 32-bit systems,
2507
+ # int64 on 64-bit systems)
2508
+
2509
+ data = TextIO('73786976294838206464 17179869184 1024')
2510
+
2511
+ test = np.genfromtxt(data, dtype=None)
2512
+
2513
+ assert_equal(test.dtype.names, ['f0', 'f1', 'f2'])
2514
+
2515
+ assert_(test.dtype['f0'] == float)
2516
+ assert_(test.dtype['f1'] == np.int64)
2517
+ assert_(test.dtype['f2'] == np.int_)
2518
+
2519
+ assert_allclose(test['f0'], 73786976294838206464.)
2520
+ assert_equal(test['f1'], 17179869184)
2521
+ assert_equal(test['f2'], 1024)
2522
+
2523
+ def test_unpack_float_data(self):
2524
+ txt = TextIO("1,2,3\n4,5,6\n7,8,9\n0.0,1.0,2.0")
2525
+ a, b, c = np.loadtxt(txt, delimiter=",", unpack=True)
2526
+ assert_array_equal(a, np.array([1.0, 4.0, 7.0, 0.0]))
2527
+ assert_array_equal(b, np.array([2.0, 5.0, 8.0, 1.0]))
2528
+ assert_array_equal(c, np.array([3.0, 6.0, 9.0, 2.0]))
2529
+
2530
+ def test_unpack_structured(self):
2531
+ # Regression test for gh-4341
2532
+ # Unpacking should work on structured arrays
2533
+ txt = TextIO("M 21 72\nF 35 58")
2534
+ dt = {'names': ('a', 'b', 'c'), 'formats': ('S1', 'i4', 'f4')}
2535
+ a, b, c = np.genfromtxt(txt, dtype=dt, unpack=True)
2536
+ assert_equal(a.dtype, np.dtype('S1'))
2537
+ assert_equal(b.dtype, np.dtype('i4'))
2538
+ assert_equal(c.dtype, np.dtype('f4'))
2539
+ assert_array_equal(a, np.array([b'M', b'F']))
2540
+ assert_array_equal(b, np.array([21, 35]))
2541
+ assert_array_equal(c, np.array([72., 58.]))
2542
+
2543
+ def test_unpack_auto_dtype(self):
2544
+ # Regression test for gh-4341
2545
+ # Unpacking should work when dtype=None
2546
+ txt = TextIO("M 21 72.\nF 35 58.")
2547
+ expected = (np.array(["M", "F"]), np.array([21, 35]), np.array([72., 58.]))
2548
+ test = np.genfromtxt(txt, dtype=None, unpack=True, encoding="utf-8")
2549
+ for arr, result in zip(expected, test):
2550
+ assert_array_equal(arr, result)
2551
+ assert_equal(arr.dtype, result.dtype)
2552
+
2553
+ def test_unpack_single_name(self):
2554
+ # Regression test for gh-4341
2555
+ # Unpacking should work when structured dtype has only one field
2556
+ txt = TextIO("21\n35")
2557
+ dt = {'names': ('a',), 'formats': ('i4',)}
2558
+ expected = np.array([21, 35], dtype=np.int32)
2559
+ test = np.genfromtxt(txt, dtype=dt, unpack=True)
2560
+ assert_array_equal(expected, test)
2561
+ assert_equal(expected.dtype, test.dtype)
2562
+
2563
+ def test_squeeze_scalar(self):
2564
+ # Regression test for gh-4341
2565
+ # Unpacking a scalar should give zero-dim output,
2566
+ # even if dtype is structured
2567
+ txt = TextIO("1")
2568
+ dt = {'names': ('a',), 'formats': ('i4',)}
2569
+ expected = np.array((1,), dtype=np.int32)
2570
+ test = np.genfromtxt(txt, dtype=dt, unpack=True)
2571
+ assert_array_equal(expected, test)
2572
+ assert_equal((), test.shape)
2573
+ assert_equal(expected.dtype, test.dtype)
2574
+
2575
+ @pytest.mark.parametrize("ndim", [0, 1, 2])
2576
+ def test_ndmin_keyword(self, ndim: int):
2577
+ # let's have the same behaviour of ndmin as loadtxt
2578
+ # as they should be the same for non-missing values
2579
+ txt = "42"
2580
+
2581
+ a = np.loadtxt(StringIO(txt), ndmin=ndim)
2582
+ b = np.genfromtxt(StringIO(txt), ndmin=ndim)
2583
+
2584
+ assert_array_equal(a, b)
2585
+
2586
+
2587
+ class TestPathUsage:
2588
+ # Test that pathlib.Path can be used
2589
+ def test_loadtxt(self):
2590
+ with temppath(suffix='.txt') as path:
2591
+ path = Path(path)
2592
+ a = np.array([[1.1, 2], [3, 4]])
2593
+ np.savetxt(path, a)
2594
+ x = np.loadtxt(path)
2595
+ assert_array_equal(x, a)
2596
+
2597
+ def test_save_load(self):
2598
+ # Test that pathlib.Path instances can be used with save.
2599
+ with temppath(suffix='.npy') as path:
2600
+ path = Path(path)
2601
+ a = np.array([[1, 2], [3, 4]], int)
2602
+ np.save(path, a)
2603
+ data = np.load(path)
2604
+ assert_array_equal(data, a)
2605
+
2606
+ def test_save_load_memmap(self):
2607
+ # Test that pathlib.Path instances can be loaded mem-mapped.
2608
+ with temppath(suffix='.npy') as path:
2609
+ path = Path(path)
2610
+ a = np.array([[1, 2], [3, 4]], int)
2611
+ np.save(path, a)
2612
+ data = np.load(path, mmap_mode='r')
2613
+ assert_array_equal(data, a)
2614
+ # close the mem-mapped file
2615
+ del data
2616
+ if IS_PYPY:
2617
+ break_cycles()
2618
+ break_cycles()
2619
+
2620
+ @pytest.mark.xfail(IS_WASM, reason="memmap doesn't work correctly")
2621
+ @pytest.mark.parametrize("filename_type", [Path, str])
2622
+ def test_save_load_memmap_readwrite(self, filename_type):
2623
+ with temppath(suffix='.npy') as path:
2624
+ path = filename_type(path)
2625
+ a = np.array([[1, 2], [3, 4]], int)
2626
+ np.save(path, a)
2627
+ b = np.load(path, mmap_mode='r+')
2628
+ a[0][0] = 5
2629
+ b[0][0] = 5
2630
+ del b # closes the file
2631
+ if IS_PYPY:
2632
+ break_cycles()
2633
+ break_cycles()
2634
+ data = np.load(path)
2635
+ assert_array_equal(data, a)
2636
+
2637
+ @pytest.mark.parametrize("filename_type", [Path, str])
2638
+ def test_savez_load(self, filename_type):
2639
+ with temppath(suffix='.npz') as path:
2640
+ path = filename_type(path)
2641
+ np.savez(path, lab='place holder')
2642
+ with np.load(path) as data:
2643
+ assert_array_equal(data['lab'], 'place holder')
2644
+
2645
+ @pytest.mark.parametrize("filename_type", [Path, str])
2646
+ def test_savez_compressed_load(self, filename_type):
2647
+ with temppath(suffix='.npz') as path:
2648
+ path = filename_type(path)
2649
+ np.savez_compressed(path, lab='place holder')
2650
+ data = np.load(path)
2651
+ assert_array_equal(data['lab'], 'place holder')
2652
+ data.close()
2653
+
2654
+ @pytest.mark.parametrize("filename_type", [Path, str])
2655
+ def test_genfromtxt(self, filename_type):
2656
+ with temppath(suffix='.txt') as path:
2657
+ path = filename_type(path)
2658
+ a = np.array([(1, 2), (3, 4)])
2659
+ np.savetxt(path, a)
2660
+ data = np.genfromtxt(path)
2661
+ assert_array_equal(a, data)
2662
+
2663
+ @pytest.mark.parametrize("filename_type", [Path, str])
2664
+ @pytest.mark.filterwarnings("ignore:.*recfromtxt.*:DeprecationWarning")
2665
+ def test_recfromtxt(self, filename_type):
2666
+ with temppath(suffix='.txt') as path:
2667
+ path = filename_type(path)
2668
+ with open(path, 'w') as f:
2669
+ f.write('A,B\n0,1\n2,3')
2670
+
2671
+ kwargs = {"delimiter": ",", "missing_values": "N/A", "names": True}
2672
+ test = recfromtxt(path, **kwargs)
2673
+ control = np.array([(0, 1), (2, 3)],
2674
+ dtype=[('A', int), ('B', int)])
2675
+ assert_(isinstance(test, np.recarray))
2676
+ assert_equal(test, control)
2677
+
2678
+ @pytest.mark.parametrize("filename_type", [Path, str])
2679
+ @pytest.mark.filterwarnings("ignore:.*recfromcsv.*:DeprecationWarning")
2680
+ def test_recfromcsv(self, filename_type):
2681
+ with temppath(suffix='.txt') as path:
2682
+ path = filename_type(path)
2683
+ with open(path, 'w') as f:
2684
+ f.write('A,B\n0,1\n2,3')
2685
+
2686
+ kwargs = {
2687
+ "missing_values": "N/A", "names": True, "case_sensitive": True
2688
+ }
2689
+ test = recfromcsv(path, dtype=None, **kwargs)
2690
+ control = np.array([(0, 1), (2, 3)],
2691
+ dtype=[('A', int), ('B', int)])
2692
+ assert_(isinstance(test, np.recarray))
2693
+ assert_equal(test, control)
2694
+
2695
+
2696
+ def test_gzip_load():
2697
+ a = np.random.random((5, 5))
2698
+
2699
+ s = BytesIO()
2700
+ f = gzip.GzipFile(fileobj=s, mode="w")
2701
+
2702
+ np.save(f, a)
2703
+ f.close()
2704
+ s.seek(0)
2705
+
2706
+ f = gzip.GzipFile(fileobj=s, mode="r")
2707
+ assert_array_equal(np.load(f), a)
2708
+
2709
+
2710
+ # These next two classes encode the minimal API needed to save()/load() arrays.
2711
+ # The `test_ducktyping` ensures they work correctly
2712
+ class JustWriter:
2713
+ def __init__(self, base):
2714
+ self.base = base
2715
+
2716
+ def write(self, s):
2717
+ return self.base.write(s)
2718
+
2719
+ def flush(self):
2720
+ return self.base.flush()
2721
+
2722
+ class JustReader:
2723
+ def __init__(self, base):
2724
+ self.base = base
2725
+
2726
+ def read(self, n):
2727
+ return self.base.read(n)
2728
+
2729
+ def seek(self, off, whence=0):
2730
+ return self.base.seek(off, whence)
2731
+
2732
+
2733
+ def test_ducktyping():
2734
+ a = np.random.random((5, 5))
2735
+
2736
+ s = BytesIO()
2737
+ f = JustWriter(s)
2738
+
2739
+ np.save(f, a)
2740
+ f.flush()
2741
+ s.seek(0)
2742
+
2743
+ f = JustReader(s)
2744
+ assert_array_equal(np.load(f), a)
2745
+
2746
+
2747
+ def test_gzip_loadtxt():
2748
+ # Thanks to another windows brokenness, we can't use
2749
+ # NamedTemporaryFile: a file created from this function cannot be
2750
+ # reopened by another open call. So we first put the gzipped string
2751
+ # of the test reference array, write it to a securely opened file,
2752
+ # which is then read from by the loadtxt function
2753
+ s = BytesIO()
2754
+ g = gzip.GzipFile(fileobj=s, mode='w')
2755
+ g.write(b'1 2 3\n')
2756
+ g.close()
2757
+
2758
+ s.seek(0)
2759
+ with temppath(suffix='.gz') as name:
2760
+ with open(name, 'wb') as f:
2761
+ f.write(s.read())
2762
+ res = np.loadtxt(name)
2763
+ s.close()
2764
+
2765
+ assert_array_equal(res, [1, 2, 3])
2766
+
2767
+
2768
+ def test_gzip_loadtxt_from_string():
2769
+ s = BytesIO()
2770
+ f = gzip.GzipFile(fileobj=s, mode="w")
2771
+ f.write(b'1 2 3\n')
2772
+ f.close()
2773
+ s.seek(0)
2774
+
2775
+ f = gzip.GzipFile(fileobj=s, mode="r")
2776
+ assert_array_equal(np.loadtxt(f), [1, 2, 3])
2777
+
2778
+
2779
+ def test_npzfile_dict():
2780
+ s = BytesIO()
2781
+ x = np.zeros((3, 3))
2782
+ y = np.zeros((3, 3))
2783
+
2784
+ np.savez(s, x=x, y=y)
2785
+ s.seek(0)
2786
+
2787
+ z = np.load(s)
2788
+
2789
+ assert_('x' in z)
2790
+ assert_('y' in z)
2791
+ assert_('x' in z.keys())
2792
+ assert_('y' in z.keys())
2793
+
2794
+ for f, a in z.items():
2795
+ assert_(f in ['x', 'y'])
2796
+ assert_equal(a.shape, (3, 3))
2797
+
2798
+ for a in z.values():
2799
+ assert_equal(a.shape, (3, 3))
2800
+
2801
+ assert_(len(z.items()) == 2)
2802
+
2803
+ for f in z:
2804
+ assert_(f in ['x', 'y'])
2805
+
2806
+ assert_('x' in z.keys())
2807
+ assert (z.get('x') == z['x']).all()
2808
+
2809
+
2810
+ @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
2811
+ @pytest.mark.thread_unsafe(reason="garbage collector is global state")
2812
+ def test_load_refcount():
2813
+ # Check that objects returned by np.load are directly freed based on
2814
+ # their refcount, rather than needing the gc to collect them.
2815
+
2816
+ f = BytesIO()
2817
+ np.savez(f, [1, 2, 3])
2818
+ f.seek(0)
2819
+
2820
+ with assert_no_gc_cycles():
2821
+ np.load(f)
2822
+
2823
+ f.seek(0)
2824
+ dt = [("a", 'u1', 2), ("b", 'u1', 2)]
2825
+ with assert_no_gc_cycles():
2826
+ x = np.loadtxt(TextIO("0 1 2 3"), dtype=dt)
2827
+ assert_equal(x, np.array([((0, 1), (2, 3))], dtype=dt))
2828
+
2829
+
2830
+ def test_load_multiple_arrays_until_eof():
2831
+ f = BytesIO()
2832
+ np.save(f, 1)
2833
+ np.save(f, 2)
2834
+ f.seek(0)
2835
+ out1 = np.load(f)
2836
+ assert out1 == 1
2837
+ out2 = np.load(f)
2838
+ assert out2 == 2
2839
+ with pytest.raises(EOFError):
2840
+ np.load(f)
2841
+
2842
+
2843
+ def test_savez_nopickle():
2844
+ obj_array = np.array([1, 'hello'], dtype=object)
2845
+ with temppath(suffix='.npz') as tmp:
2846
+ np.savez(tmp, obj_array)
2847
+
2848
+ with temppath(suffix='.npz') as tmp:
2849
+ with pytest.raises(ValueError, match="Object arrays cannot be saved when.*"):
2850
+ np.savez(tmp, obj_array, allow_pickle=False)
2851
+
2852
+ with temppath(suffix='.npz') as tmp:
2853
+ np.savez_compressed(tmp, obj_array)
2854
+
2855
+ with temppath(suffix='.npz') as tmp:
2856
+ with pytest.raises(ValueError, match="Object arrays cannot be saved when.*"):
2857
+ np.savez_compressed(tmp, obj_array, allow_pickle=False)