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.
- numpy/__config__.py +170 -0
- numpy/__config__.pyi +108 -0
- numpy/__init__.cython-30.pxd +1242 -0
- numpy/__init__.pxd +1155 -0
- numpy/__init__.py +942 -0
- numpy/__init__.pyi +6202 -0
- numpy/_array_api_info.py +346 -0
- numpy/_array_api_info.pyi +206 -0
- numpy/_configtool.py +39 -0
- numpy/_configtool.pyi +1 -0
- numpy/_core/__init__.py +201 -0
- numpy/_core/__init__.pyi +666 -0
- numpy/_core/_add_newdocs.py +7151 -0
- numpy/_core/_add_newdocs.pyi +2 -0
- numpy/_core/_add_newdocs_scalars.py +381 -0
- numpy/_core/_add_newdocs_scalars.pyi +16 -0
- numpy/_core/_asarray.py +130 -0
- numpy/_core/_asarray.pyi +43 -0
- numpy/_core/_dtype.py +366 -0
- numpy/_core/_dtype.pyi +56 -0
- numpy/_core/_dtype_ctypes.py +120 -0
- numpy/_core/_dtype_ctypes.pyi +83 -0
- numpy/_core/_exceptions.py +162 -0
- numpy/_core/_exceptions.pyi +54 -0
- numpy/_core/_internal.py +968 -0
- numpy/_core/_internal.pyi +61 -0
- numpy/_core/_methods.py +252 -0
- numpy/_core/_methods.pyi +22 -0
- numpy/_core/_multiarray_tests.pypy311-pp73-darwin.so +0 -0
- numpy/_core/_multiarray_umath.pypy311-pp73-darwin.so +0 -0
- numpy/_core/_operand_flag_tests.pypy311-pp73-darwin.so +0 -0
- numpy/_core/_rational_tests.pypy311-pp73-darwin.so +0 -0
- numpy/_core/_simd.pyi +35 -0
- numpy/_core/_simd.pypy311-pp73-darwin.so +0 -0
- numpy/_core/_string_helpers.py +100 -0
- numpy/_core/_string_helpers.pyi +12 -0
- numpy/_core/_struct_ufunc_tests.pypy311-pp73-darwin.so +0 -0
- numpy/_core/_type_aliases.py +131 -0
- numpy/_core/_type_aliases.pyi +86 -0
- numpy/_core/_ufunc_config.py +515 -0
- numpy/_core/_ufunc_config.pyi +69 -0
- numpy/_core/_umath_tests.pyi +47 -0
- numpy/_core/_umath_tests.pypy311-pp73-darwin.so +0 -0
- numpy/_core/arrayprint.py +1779 -0
- numpy/_core/arrayprint.pyi +158 -0
- numpy/_core/cversions.py +13 -0
- numpy/_core/defchararray.py +1414 -0
- numpy/_core/defchararray.pyi +1150 -0
- numpy/_core/einsumfunc.py +1650 -0
- numpy/_core/einsumfunc.pyi +184 -0
- numpy/_core/fromnumeric.py +4233 -0
- numpy/_core/fromnumeric.pyi +1735 -0
- numpy/_core/function_base.py +547 -0
- numpy/_core/function_base.pyi +276 -0
- numpy/_core/getlimits.py +462 -0
- numpy/_core/getlimits.pyi +124 -0
- numpy/_core/include/numpy/__multiarray_api.c +376 -0
- numpy/_core/include/numpy/__multiarray_api.h +1628 -0
- numpy/_core/include/numpy/__ufunc_api.c +55 -0
- numpy/_core/include/numpy/__ufunc_api.h +349 -0
- numpy/_core/include/numpy/_neighborhood_iterator_imp.h +90 -0
- numpy/_core/include/numpy/_numpyconfig.h +33 -0
- numpy/_core/include/numpy/_public_dtype_api_table.h +86 -0
- numpy/_core/include/numpy/arrayobject.h +7 -0
- numpy/_core/include/numpy/arrayscalars.h +198 -0
- numpy/_core/include/numpy/dtype_api.h +547 -0
- numpy/_core/include/numpy/halffloat.h +70 -0
- numpy/_core/include/numpy/ndarrayobject.h +304 -0
- numpy/_core/include/numpy/ndarraytypes.h +1982 -0
- numpy/_core/include/numpy/npy_2_compat.h +249 -0
- numpy/_core/include/numpy/npy_2_complexcompat.h +28 -0
- numpy/_core/include/numpy/npy_3kcompat.h +374 -0
- numpy/_core/include/numpy/npy_common.h +989 -0
- numpy/_core/include/numpy/npy_cpu.h +126 -0
- numpy/_core/include/numpy/npy_endian.h +79 -0
- numpy/_core/include/numpy/npy_math.h +602 -0
- numpy/_core/include/numpy/npy_no_deprecated_api.h +20 -0
- numpy/_core/include/numpy/npy_os.h +42 -0
- numpy/_core/include/numpy/numpyconfig.h +185 -0
- numpy/_core/include/numpy/random/LICENSE.txt +21 -0
- numpy/_core/include/numpy/random/bitgen.h +20 -0
- numpy/_core/include/numpy/random/distributions.h +209 -0
- numpy/_core/include/numpy/random/libdivide.h +2079 -0
- numpy/_core/include/numpy/ufuncobject.h +343 -0
- numpy/_core/include/numpy/utils.h +37 -0
- numpy/_core/lib/libnpymath.a +0 -0
- numpy/_core/lib/npy-pkg-config/mlib.ini +12 -0
- numpy/_core/lib/npy-pkg-config/npymath.ini +20 -0
- numpy/_core/lib/pkgconfig/numpy.pc +7 -0
- numpy/_core/memmap.py +363 -0
- numpy/_core/memmap.pyi +3 -0
- numpy/_core/multiarray.py +1740 -0
- numpy/_core/multiarray.pyi +1316 -0
- numpy/_core/numeric.py +2758 -0
- numpy/_core/numeric.pyi +1276 -0
- numpy/_core/numerictypes.py +633 -0
- numpy/_core/numerictypes.pyi +196 -0
- numpy/_core/overrides.py +188 -0
- numpy/_core/overrides.pyi +47 -0
- numpy/_core/printoptions.py +32 -0
- numpy/_core/printoptions.pyi +28 -0
- numpy/_core/records.py +1088 -0
- numpy/_core/records.pyi +340 -0
- numpy/_core/shape_base.py +996 -0
- numpy/_core/shape_base.pyi +182 -0
- numpy/_core/strings.py +1813 -0
- numpy/_core/strings.pyi +536 -0
- numpy/_core/tests/_locales.py +72 -0
- numpy/_core/tests/_natype.py +144 -0
- numpy/_core/tests/data/astype_copy.pkl +0 -0
- numpy/_core/tests/data/generate_umath_validation_data.cpp +170 -0
- numpy/_core/tests/data/recarray_from_file.fits +0 -0
- numpy/_core/tests/data/umath-validation-set-README.txt +15 -0
- numpy/_core/tests/data/umath-validation-set-arccos.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-arccosh.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-arcsin.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-arcsinh.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-arctan.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-arctanh.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-cbrt.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-cos.csv +1375 -0
- numpy/_core/tests/data/umath-validation-set-cosh.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-exp.csv +412 -0
- numpy/_core/tests/data/umath-validation-set-exp2.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-expm1.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-log.csv +271 -0
- numpy/_core/tests/data/umath-validation-set-log10.csv +1629 -0
- numpy/_core/tests/data/umath-validation-set-log1p.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-log2.csv +1629 -0
- numpy/_core/tests/data/umath-validation-set-sin.csv +1370 -0
- numpy/_core/tests/data/umath-validation-set-sinh.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-tan.csv +1429 -0
- numpy/_core/tests/data/umath-validation-set-tanh.csv +1429 -0
- numpy/_core/tests/examples/cython/checks.pyx +373 -0
- numpy/_core/tests/examples/cython/meson.build +43 -0
- numpy/_core/tests/examples/cython/setup.py +39 -0
- numpy/_core/tests/examples/limited_api/limited_api1.c +17 -0
- numpy/_core/tests/examples/limited_api/limited_api2.pyx +11 -0
- numpy/_core/tests/examples/limited_api/limited_api_latest.c +19 -0
- numpy/_core/tests/examples/limited_api/meson.build +59 -0
- numpy/_core/tests/examples/limited_api/setup.py +24 -0
- numpy/_core/tests/test__exceptions.py +90 -0
- numpy/_core/tests/test_abc.py +54 -0
- numpy/_core/tests/test_api.py +655 -0
- numpy/_core/tests/test_argparse.py +90 -0
- numpy/_core/tests/test_array_api_info.py +113 -0
- numpy/_core/tests/test_array_coercion.py +928 -0
- numpy/_core/tests/test_array_interface.py +222 -0
- numpy/_core/tests/test_arraymethod.py +84 -0
- numpy/_core/tests/test_arrayobject.py +75 -0
- numpy/_core/tests/test_arrayprint.py +1324 -0
- numpy/_core/tests/test_casting_floatingpoint_errors.py +154 -0
- numpy/_core/tests/test_casting_unittests.py +955 -0
- numpy/_core/tests/test_conversion_utils.py +209 -0
- numpy/_core/tests/test_cpu_dispatcher.py +48 -0
- numpy/_core/tests/test_cpu_features.py +450 -0
- numpy/_core/tests/test_custom_dtypes.py +393 -0
- numpy/_core/tests/test_cython.py +352 -0
- numpy/_core/tests/test_datetime.py +2792 -0
- numpy/_core/tests/test_defchararray.py +858 -0
- numpy/_core/tests/test_deprecations.py +460 -0
- numpy/_core/tests/test_dlpack.py +190 -0
- numpy/_core/tests/test_dtype.py +2110 -0
- numpy/_core/tests/test_einsum.py +1351 -0
- numpy/_core/tests/test_errstate.py +131 -0
- numpy/_core/tests/test_extint128.py +217 -0
- numpy/_core/tests/test_finfo.py +86 -0
- numpy/_core/tests/test_function_base.py +504 -0
- numpy/_core/tests/test_getlimits.py +171 -0
- numpy/_core/tests/test_half.py +593 -0
- numpy/_core/tests/test_hashtable.py +36 -0
- numpy/_core/tests/test_indexerrors.py +122 -0
- numpy/_core/tests/test_indexing.py +1692 -0
- numpy/_core/tests/test_item_selection.py +167 -0
- numpy/_core/tests/test_limited_api.py +102 -0
- numpy/_core/tests/test_longdouble.py +370 -0
- numpy/_core/tests/test_mem_overlap.py +933 -0
- numpy/_core/tests/test_mem_policy.py +453 -0
- numpy/_core/tests/test_memmap.py +248 -0
- numpy/_core/tests/test_multiarray.py +11008 -0
- numpy/_core/tests/test_multiprocessing.py +55 -0
- numpy/_core/tests/test_multithreading.py +377 -0
- numpy/_core/tests/test_nditer.py +3533 -0
- numpy/_core/tests/test_nep50_promotions.py +287 -0
- numpy/_core/tests/test_numeric.py +4295 -0
- numpy/_core/tests/test_numerictypes.py +650 -0
- numpy/_core/tests/test_overrides.py +800 -0
- numpy/_core/tests/test_print.py +202 -0
- numpy/_core/tests/test_protocols.py +46 -0
- numpy/_core/tests/test_records.py +544 -0
- numpy/_core/tests/test_regression.py +2677 -0
- numpy/_core/tests/test_scalar_ctors.py +203 -0
- numpy/_core/tests/test_scalar_methods.py +328 -0
- numpy/_core/tests/test_scalarbuffer.py +153 -0
- numpy/_core/tests/test_scalarinherit.py +105 -0
- numpy/_core/tests/test_scalarmath.py +1168 -0
- numpy/_core/tests/test_scalarprint.py +403 -0
- numpy/_core/tests/test_shape_base.py +904 -0
- numpy/_core/tests/test_simd.py +1345 -0
- numpy/_core/tests/test_simd_module.py +105 -0
- numpy/_core/tests/test_stringdtype.py +1855 -0
- numpy/_core/tests/test_strings.py +1523 -0
- numpy/_core/tests/test_ufunc.py +3405 -0
- numpy/_core/tests/test_umath.py +4962 -0
- numpy/_core/tests/test_umath_accuracy.py +132 -0
- numpy/_core/tests/test_umath_complex.py +631 -0
- numpy/_core/tests/test_unicode.py +369 -0
- numpy/_core/umath.py +60 -0
- numpy/_core/umath.pyi +232 -0
- numpy/_distributor_init.py +15 -0
- numpy/_distributor_init.pyi +1 -0
- numpy/_expired_attrs_2_0.py +78 -0
- numpy/_expired_attrs_2_0.pyi +61 -0
- numpy/_globals.py +121 -0
- numpy/_globals.pyi +17 -0
- numpy/_pyinstaller/__init__.py +0 -0
- numpy/_pyinstaller/__init__.pyi +0 -0
- numpy/_pyinstaller/hook-numpy.py +36 -0
- numpy/_pyinstaller/hook-numpy.pyi +6 -0
- numpy/_pyinstaller/tests/__init__.py +16 -0
- numpy/_pyinstaller/tests/pyinstaller-smoke.py +32 -0
- numpy/_pyinstaller/tests/test_pyinstaller.py +35 -0
- numpy/_pytesttester.py +201 -0
- numpy/_pytesttester.pyi +18 -0
- numpy/_typing/__init__.py +173 -0
- numpy/_typing/_add_docstring.py +153 -0
- numpy/_typing/_array_like.py +106 -0
- numpy/_typing/_char_codes.py +213 -0
- numpy/_typing/_dtype_like.py +114 -0
- numpy/_typing/_extended_precision.py +15 -0
- numpy/_typing/_nbit.py +19 -0
- numpy/_typing/_nbit_base.py +94 -0
- numpy/_typing/_nbit_base.pyi +39 -0
- numpy/_typing/_nested_sequence.py +79 -0
- numpy/_typing/_scalars.py +20 -0
- numpy/_typing/_shape.py +8 -0
- numpy/_typing/_ufunc.py +7 -0
- numpy/_typing/_ufunc.pyi +975 -0
- numpy/_utils/__init__.py +95 -0
- numpy/_utils/__init__.pyi +28 -0
- numpy/_utils/_convertions.py +18 -0
- numpy/_utils/_convertions.pyi +4 -0
- numpy/_utils/_inspect.py +192 -0
- numpy/_utils/_inspect.pyi +70 -0
- numpy/_utils/_pep440.py +486 -0
- numpy/_utils/_pep440.pyi +118 -0
- numpy/char/__init__.py +2 -0
- numpy/char/__init__.pyi +111 -0
- numpy/conftest.py +248 -0
- numpy/core/__init__.py +33 -0
- numpy/core/__init__.pyi +0 -0
- numpy/core/_dtype.py +10 -0
- numpy/core/_dtype.pyi +0 -0
- numpy/core/_dtype_ctypes.py +10 -0
- numpy/core/_dtype_ctypes.pyi +0 -0
- numpy/core/_internal.py +27 -0
- numpy/core/_multiarray_umath.py +57 -0
- numpy/core/_utils.py +21 -0
- numpy/core/arrayprint.py +10 -0
- numpy/core/defchararray.py +10 -0
- numpy/core/einsumfunc.py +10 -0
- numpy/core/fromnumeric.py +10 -0
- numpy/core/function_base.py +10 -0
- numpy/core/getlimits.py +10 -0
- numpy/core/multiarray.py +25 -0
- numpy/core/numeric.py +12 -0
- numpy/core/numerictypes.py +10 -0
- numpy/core/overrides.py +10 -0
- numpy/core/overrides.pyi +7 -0
- numpy/core/records.py +10 -0
- numpy/core/shape_base.py +10 -0
- numpy/core/umath.py +10 -0
- numpy/ctypeslib/__init__.py +13 -0
- numpy/ctypeslib/__init__.pyi +15 -0
- numpy/ctypeslib/_ctypeslib.py +603 -0
- numpy/ctypeslib/_ctypeslib.pyi +236 -0
- numpy/distutils/__init__.py +64 -0
- numpy/distutils/__init__.pyi +4 -0
- numpy/distutils/__pycache__/conv_template.pypy311.pyc +0 -0
- numpy/distutils/_shell_utils.py +87 -0
- numpy/distutils/armccompiler.py +26 -0
- numpy/distutils/ccompiler.py +826 -0
- numpy/distutils/ccompiler_opt.py +2668 -0
- numpy/distutils/checks/cpu_asimd.c +27 -0
- numpy/distutils/checks/cpu_asimddp.c +16 -0
- numpy/distutils/checks/cpu_asimdfhm.c +19 -0
- numpy/distutils/checks/cpu_asimdhp.c +15 -0
- numpy/distutils/checks/cpu_avx.c +20 -0
- numpy/distutils/checks/cpu_avx2.c +20 -0
- numpy/distutils/checks/cpu_avx512_clx.c +22 -0
- numpy/distutils/checks/cpu_avx512_cnl.c +24 -0
- numpy/distutils/checks/cpu_avx512_icl.c +26 -0
- numpy/distutils/checks/cpu_avx512_knl.c +25 -0
- numpy/distutils/checks/cpu_avx512_knm.c +30 -0
- numpy/distutils/checks/cpu_avx512_skx.c +26 -0
- numpy/distutils/checks/cpu_avx512_spr.c +26 -0
- numpy/distutils/checks/cpu_avx512cd.c +20 -0
- numpy/distutils/checks/cpu_avx512f.c +20 -0
- numpy/distutils/checks/cpu_f16c.c +22 -0
- numpy/distutils/checks/cpu_fma3.c +22 -0
- numpy/distutils/checks/cpu_fma4.c +13 -0
- numpy/distutils/checks/cpu_lsx.c +11 -0
- numpy/distutils/checks/cpu_neon.c +19 -0
- numpy/distutils/checks/cpu_neon_fp16.c +11 -0
- numpy/distutils/checks/cpu_neon_vfpv4.c +21 -0
- numpy/distutils/checks/cpu_popcnt.c +32 -0
- numpy/distutils/checks/cpu_rvv.c +13 -0
- numpy/distutils/checks/cpu_sse.c +20 -0
- numpy/distutils/checks/cpu_sse2.c +20 -0
- numpy/distutils/checks/cpu_sse3.c +20 -0
- numpy/distutils/checks/cpu_sse41.c +20 -0
- numpy/distutils/checks/cpu_sse42.c +20 -0
- numpy/distutils/checks/cpu_ssse3.c +20 -0
- numpy/distutils/checks/cpu_sve.c +14 -0
- numpy/distutils/checks/cpu_vsx.c +21 -0
- numpy/distutils/checks/cpu_vsx2.c +13 -0
- numpy/distutils/checks/cpu_vsx3.c +13 -0
- numpy/distutils/checks/cpu_vsx4.c +14 -0
- numpy/distutils/checks/cpu_vx.c +16 -0
- numpy/distutils/checks/cpu_vxe.c +25 -0
- numpy/distutils/checks/cpu_vxe2.c +21 -0
- numpy/distutils/checks/cpu_xop.c +12 -0
- numpy/distutils/checks/extra_avx512bw_mask.c +18 -0
- numpy/distutils/checks/extra_avx512dq_mask.c +16 -0
- numpy/distutils/checks/extra_avx512f_reduce.c +41 -0
- numpy/distutils/checks/extra_vsx3_half_double.c +12 -0
- numpy/distutils/checks/extra_vsx4_mma.c +21 -0
- numpy/distutils/checks/extra_vsx_asm.c +36 -0
- numpy/distutils/checks/test_flags.c +1 -0
- numpy/distutils/command/__init__.py +41 -0
- numpy/distutils/command/autodist.py +148 -0
- numpy/distutils/command/bdist_rpm.py +22 -0
- numpy/distutils/command/build.py +62 -0
- numpy/distutils/command/build_clib.py +469 -0
- numpy/distutils/command/build_ext.py +752 -0
- numpy/distutils/command/build_py.py +31 -0
- numpy/distutils/command/build_scripts.py +49 -0
- numpy/distutils/command/build_src.py +773 -0
- numpy/distutils/command/config.py +516 -0
- numpy/distutils/command/config_compiler.py +126 -0
- numpy/distutils/command/develop.py +15 -0
- numpy/distutils/command/egg_info.py +25 -0
- numpy/distutils/command/install.py +79 -0
- numpy/distutils/command/install_clib.py +40 -0
- numpy/distutils/command/install_data.py +24 -0
- numpy/distutils/command/install_headers.py +25 -0
- numpy/distutils/command/sdist.py +27 -0
- numpy/distutils/conv_template.py +329 -0
- numpy/distutils/core.py +215 -0
- numpy/distutils/cpuinfo.py +683 -0
- numpy/distutils/exec_command.py +315 -0
- numpy/distutils/extension.py +101 -0
- numpy/distutils/fcompiler/__init__.py +1035 -0
- numpy/distutils/fcompiler/absoft.py +158 -0
- numpy/distutils/fcompiler/arm.py +71 -0
- numpy/distutils/fcompiler/compaq.py +120 -0
- numpy/distutils/fcompiler/environment.py +88 -0
- numpy/distutils/fcompiler/fujitsu.py +46 -0
- numpy/distutils/fcompiler/g95.py +42 -0
- numpy/distutils/fcompiler/gnu.py +555 -0
- numpy/distutils/fcompiler/hpux.py +41 -0
- numpy/distutils/fcompiler/ibm.py +97 -0
- numpy/distutils/fcompiler/intel.py +211 -0
- numpy/distutils/fcompiler/lahey.py +45 -0
- numpy/distutils/fcompiler/mips.py +54 -0
- numpy/distutils/fcompiler/nag.py +87 -0
- numpy/distutils/fcompiler/none.py +28 -0
- numpy/distutils/fcompiler/nv.py +53 -0
- numpy/distutils/fcompiler/pathf95.py +33 -0
- numpy/distutils/fcompiler/pg.py +128 -0
- numpy/distutils/fcompiler/sun.py +51 -0
- numpy/distutils/fcompiler/vast.py +52 -0
- numpy/distutils/from_template.py +261 -0
- numpy/distutils/fujitsuccompiler.py +28 -0
- numpy/distutils/intelccompiler.py +106 -0
- numpy/distutils/lib2def.py +116 -0
- numpy/distutils/line_endings.py +77 -0
- numpy/distutils/log.py +111 -0
- numpy/distutils/mingw/gfortran_vs2003_hack.c +6 -0
- numpy/distutils/mingw32ccompiler.py +620 -0
- numpy/distutils/misc_util.py +2484 -0
- numpy/distutils/msvc9compiler.py +63 -0
- numpy/distutils/msvccompiler.py +76 -0
- numpy/distutils/npy_pkg_config.py +441 -0
- numpy/distutils/numpy_distribution.py +17 -0
- numpy/distutils/pathccompiler.py +21 -0
- numpy/distutils/system_info.py +3267 -0
- numpy/distutils/tests/__init__.py +0 -0
- numpy/distutils/tests/test_build_ext.py +74 -0
- numpy/distutils/tests/test_ccompiler_opt.py +808 -0
- numpy/distutils/tests/test_ccompiler_opt_conf.py +176 -0
- numpy/distutils/tests/test_exec_command.py +217 -0
- numpy/distutils/tests/test_fcompiler.py +43 -0
- numpy/distutils/tests/test_fcompiler_gnu.py +55 -0
- numpy/distutils/tests/test_fcompiler_intel.py +30 -0
- numpy/distutils/tests/test_fcompiler_nagfor.py +22 -0
- numpy/distutils/tests/test_from_template.py +44 -0
- numpy/distutils/tests/test_log.py +34 -0
- numpy/distutils/tests/test_mingw32ccompiler.py +47 -0
- numpy/distutils/tests/test_misc_util.py +88 -0
- numpy/distutils/tests/test_npy_pkg_config.py +84 -0
- numpy/distutils/tests/test_shell_utils.py +79 -0
- numpy/distutils/tests/test_system_info.py +334 -0
- numpy/distutils/tests/utilities.py +90 -0
- numpy/distutils/unixccompiler.py +141 -0
- numpy/doc/ufuncs.py +138 -0
- numpy/dtypes.py +41 -0
- numpy/dtypes.pyi +630 -0
- numpy/exceptions.py +246 -0
- numpy/exceptions.pyi +27 -0
- numpy/f2py/__init__.py +86 -0
- numpy/f2py/__init__.pyi +5 -0
- numpy/f2py/__main__.py +5 -0
- numpy/f2py/__version__.py +1 -0
- numpy/f2py/__version__.pyi +1 -0
- numpy/f2py/_backends/__init__.py +9 -0
- numpy/f2py/_backends/__init__.pyi +5 -0
- numpy/f2py/_backends/_backend.py +44 -0
- numpy/f2py/_backends/_backend.pyi +46 -0
- numpy/f2py/_backends/_distutils.py +76 -0
- numpy/f2py/_backends/_distutils.pyi +13 -0
- numpy/f2py/_backends/_meson.py +244 -0
- numpy/f2py/_backends/_meson.pyi +62 -0
- numpy/f2py/_backends/meson.build.template +58 -0
- numpy/f2py/_isocbind.py +62 -0
- numpy/f2py/_isocbind.pyi +13 -0
- numpy/f2py/_src_pyf.py +247 -0
- numpy/f2py/_src_pyf.pyi +28 -0
- numpy/f2py/auxfuncs.py +1004 -0
- numpy/f2py/auxfuncs.pyi +262 -0
- numpy/f2py/capi_maps.py +811 -0
- numpy/f2py/capi_maps.pyi +33 -0
- numpy/f2py/cb_rules.py +665 -0
- numpy/f2py/cb_rules.pyi +17 -0
- numpy/f2py/cfuncs.py +1563 -0
- numpy/f2py/cfuncs.pyi +31 -0
- numpy/f2py/common_rules.py +143 -0
- numpy/f2py/common_rules.pyi +9 -0
- numpy/f2py/crackfortran.py +3725 -0
- numpy/f2py/crackfortran.pyi +266 -0
- numpy/f2py/diagnose.py +149 -0
- numpy/f2py/diagnose.pyi +1 -0
- numpy/f2py/f2py2e.py +788 -0
- numpy/f2py/f2py2e.pyi +74 -0
- numpy/f2py/f90mod_rules.py +269 -0
- numpy/f2py/f90mod_rules.pyi +16 -0
- numpy/f2py/func2subr.py +329 -0
- numpy/f2py/func2subr.pyi +7 -0
- numpy/f2py/rules.py +1629 -0
- numpy/f2py/rules.pyi +41 -0
- numpy/f2py/setup.cfg +3 -0
- numpy/f2py/src/fortranobject.c +1436 -0
- numpy/f2py/src/fortranobject.h +173 -0
- numpy/f2py/symbolic.py +1518 -0
- numpy/f2py/symbolic.pyi +219 -0
- numpy/f2py/tests/__init__.py +16 -0
- numpy/f2py/tests/src/abstract_interface/foo.f90 +34 -0
- numpy/f2py/tests/src/abstract_interface/gh18403_mod.f90 +6 -0
- numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c +235 -0
- numpy/f2py/tests/src/assumed_shape/.f2py_f2cmap +1 -0
- numpy/f2py/tests/src/assumed_shape/foo_free.f90 +34 -0
- numpy/f2py/tests/src/assumed_shape/foo_mod.f90 +41 -0
- numpy/f2py/tests/src/assumed_shape/foo_use.f90 +19 -0
- numpy/f2py/tests/src/assumed_shape/precision.f90 +4 -0
- numpy/f2py/tests/src/block_docstring/foo.f +6 -0
- numpy/f2py/tests/src/callback/foo.f +62 -0
- numpy/f2py/tests/src/callback/gh17797.f90 +7 -0
- numpy/f2py/tests/src/callback/gh18335.f90 +17 -0
- numpy/f2py/tests/src/callback/gh25211.f +10 -0
- numpy/f2py/tests/src/callback/gh25211.pyf +18 -0
- numpy/f2py/tests/src/callback/gh26681.f90 +18 -0
- numpy/f2py/tests/src/cli/gh_22819.pyf +6 -0
- numpy/f2py/tests/src/cli/hi77.f +3 -0
- numpy/f2py/tests/src/cli/hiworld.f90 +3 -0
- numpy/f2py/tests/src/common/block.f +11 -0
- numpy/f2py/tests/src/common/gh19161.f90 +10 -0
- numpy/f2py/tests/src/crackfortran/accesstype.f90 +13 -0
- numpy/f2py/tests/src/crackfortran/common_with_division.f +17 -0
- numpy/f2py/tests/src/crackfortran/data_common.f +8 -0
- numpy/f2py/tests/src/crackfortran/data_multiplier.f +5 -0
- numpy/f2py/tests/src/crackfortran/data_stmts.f90 +20 -0
- numpy/f2py/tests/src/crackfortran/data_with_comments.f +8 -0
- numpy/f2py/tests/src/crackfortran/foo_deps.f90 +6 -0
- numpy/f2py/tests/src/crackfortran/gh15035.f +16 -0
- numpy/f2py/tests/src/crackfortran/gh17859.f +12 -0
- numpy/f2py/tests/src/crackfortran/gh22648.pyf +7 -0
- numpy/f2py/tests/src/crackfortran/gh23533.f +5 -0
- numpy/f2py/tests/src/crackfortran/gh23598.f90 +4 -0
- numpy/f2py/tests/src/crackfortran/gh23598Warn.f90 +11 -0
- numpy/f2py/tests/src/crackfortran/gh23879.f90 +20 -0
- numpy/f2py/tests/src/crackfortran/gh27697.f90 +12 -0
- numpy/f2py/tests/src/crackfortran/gh2848.f90 +13 -0
- numpy/f2py/tests/src/crackfortran/operators.f90 +49 -0
- numpy/f2py/tests/src/crackfortran/privatemod.f90 +11 -0
- numpy/f2py/tests/src/crackfortran/publicmod.f90 +10 -0
- numpy/f2py/tests/src/crackfortran/pubprivmod.f90 +10 -0
- numpy/f2py/tests/src/crackfortran/unicode_comment.f90 +4 -0
- numpy/f2py/tests/src/f2cmap/.f2py_f2cmap +1 -0
- numpy/f2py/tests/src/f2cmap/isoFortranEnvMap.f90 +9 -0
- numpy/f2py/tests/src/isocintrin/isoCtests.f90 +34 -0
- numpy/f2py/tests/src/kind/foo.f90 +20 -0
- numpy/f2py/tests/src/mixed/foo.f +5 -0
- numpy/f2py/tests/src/mixed/foo_fixed.f90 +8 -0
- numpy/f2py/tests/src/mixed/foo_free.f90 +8 -0
- numpy/f2py/tests/src/modules/gh25337/data.f90 +8 -0
- numpy/f2py/tests/src/modules/gh25337/use_data.f90 +6 -0
- numpy/f2py/tests/src/modules/gh26920/two_mods_with_no_public_entities.f90 +21 -0
- numpy/f2py/tests/src/modules/gh26920/two_mods_with_one_public_routine.f90 +21 -0
- numpy/f2py/tests/src/modules/module_data_docstring.f90 +12 -0
- numpy/f2py/tests/src/modules/use_modules.f90 +20 -0
- numpy/f2py/tests/src/negative_bounds/issue_20853.f90 +7 -0
- numpy/f2py/tests/src/parameter/constant_array.f90 +45 -0
- numpy/f2py/tests/src/parameter/constant_both.f90 +57 -0
- numpy/f2py/tests/src/parameter/constant_compound.f90 +15 -0
- numpy/f2py/tests/src/parameter/constant_integer.f90 +22 -0
- numpy/f2py/tests/src/parameter/constant_non_compound.f90 +23 -0
- numpy/f2py/tests/src/parameter/constant_real.f90 +23 -0
- numpy/f2py/tests/src/quoted_character/foo.f +14 -0
- numpy/f2py/tests/src/regression/AB.inc +1 -0
- numpy/f2py/tests/src/regression/assignOnlyModule.f90 +25 -0
- numpy/f2py/tests/src/regression/datonly.f90 +17 -0
- numpy/f2py/tests/src/regression/f77comments.f +26 -0
- numpy/f2py/tests/src/regression/f77fixedform.f95 +5 -0
- numpy/f2py/tests/src/regression/f90continuation.f90 +9 -0
- numpy/f2py/tests/src/regression/incfile.f90 +5 -0
- numpy/f2py/tests/src/regression/inout.f90 +9 -0
- numpy/f2py/tests/src/regression/lower_f2py_fortran.f90 +5 -0
- numpy/f2py/tests/src/regression/mod_derived_types.f90 +23 -0
- numpy/f2py/tests/src/return_character/foo77.f +45 -0
- numpy/f2py/tests/src/return_character/foo90.f90 +48 -0
- numpy/f2py/tests/src/return_complex/foo77.f +45 -0
- numpy/f2py/tests/src/return_complex/foo90.f90 +48 -0
- numpy/f2py/tests/src/return_integer/foo77.f +56 -0
- numpy/f2py/tests/src/return_integer/foo90.f90 +59 -0
- numpy/f2py/tests/src/return_logical/foo77.f +56 -0
- numpy/f2py/tests/src/return_logical/foo90.f90 +59 -0
- numpy/f2py/tests/src/return_real/foo77.f +45 -0
- numpy/f2py/tests/src/return_real/foo90.f90 +48 -0
- numpy/f2py/tests/src/routines/funcfortranname.f +5 -0
- numpy/f2py/tests/src/routines/funcfortranname.pyf +11 -0
- numpy/f2py/tests/src/routines/subrout.f +4 -0
- numpy/f2py/tests/src/routines/subrout.pyf +10 -0
- numpy/f2py/tests/src/size/foo.f90 +44 -0
- numpy/f2py/tests/src/string/char.f90 +29 -0
- numpy/f2py/tests/src/string/fixed_string.f90 +34 -0
- numpy/f2py/tests/src/string/gh24008.f +8 -0
- numpy/f2py/tests/src/string/gh24662.f90 +7 -0
- numpy/f2py/tests/src/string/gh25286.f90 +14 -0
- numpy/f2py/tests/src/string/gh25286.pyf +12 -0
- numpy/f2py/tests/src/string/gh25286_bc.pyf +12 -0
- numpy/f2py/tests/src/string/scalar_string.f90 +9 -0
- numpy/f2py/tests/src/string/string.f +12 -0
- numpy/f2py/tests/src/value_attrspec/gh21665.f90 +9 -0
- numpy/f2py/tests/test_abstract_interface.py +26 -0
- numpy/f2py/tests/test_array_from_pyobj.py +678 -0
- numpy/f2py/tests/test_assumed_shape.py +50 -0
- numpy/f2py/tests/test_block_docstring.py +20 -0
- numpy/f2py/tests/test_callback.py +263 -0
- numpy/f2py/tests/test_character.py +641 -0
- numpy/f2py/tests/test_common.py +23 -0
- numpy/f2py/tests/test_crackfortran.py +421 -0
- numpy/f2py/tests/test_data.py +71 -0
- numpy/f2py/tests/test_docs.py +66 -0
- numpy/f2py/tests/test_f2cmap.py +17 -0
- numpy/f2py/tests/test_f2py2e.py +983 -0
- numpy/f2py/tests/test_isoc.py +56 -0
- numpy/f2py/tests/test_kind.py +52 -0
- numpy/f2py/tests/test_mixed.py +35 -0
- numpy/f2py/tests/test_modules.py +83 -0
- numpy/f2py/tests/test_parameter.py +129 -0
- numpy/f2py/tests/test_pyf_src.py +43 -0
- numpy/f2py/tests/test_quoted_character.py +18 -0
- numpy/f2py/tests/test_regression.py +187 -0
- numpy/f2py/tests/test_return_character.py +48 -0
- numpy/f2py/tests/test_return_complex.py +67 -0
- numpy/f2py/tests/test_return_integer.py +55 -0
- numpy/f2py/tests/test_return_logical.py +65 -0
- numpy/f2py/tests/test_return_real.py +109 -0
- numpy/f2py/tests/test_routines.py +29 -0
- numpy/f2py/tests/test_semicolon_split.py +75 -0
- numpy/f2py/tests/test_size.py +45 -0
- numpy/f2py/tests/test_string.py +100 -0
- numpy/f2py/tests/test_symbolic.py +500 -0
- numpy/f2py/tests/test_value_attrspec.py +15 -0
- numpy/f2py/tests/util.py +442 -0
- numpy/f2py/use_rules.py +99 -0
- numpy/f2py/use_rules.pyi +9 -0
- numpy/fft/__init__.py +213 -0
- numpy/fft/__init__.pyi +38 -0
- numpy/fft/_helper.py +235 -0
- numpy/fft/_helper.pyi +44 -0
- numpy/fft/_pocketfft.py +1693 -0
- numpy/fft/_pocketfft.pyi +137 -0
- numpy/fft/_pocketfft_umath.pypy311-pp73-darwin.so +0 -0
- numpy/fft/tests/__init__.py +0 -0
- numpy/fft/tests/test_helper.py +167 -0
- numpy/fft/tests/test_pocketfft.py +589 -0
- numpy/lib/__init__.py +97 -0
- numpy/lib/__init__.pyi +52 -0
- numpy/lib/_array_utils_impl.py +62 -0
- numpy/lib/_array_utils_impl.pyi +10 -0
- numpy/lib/_arraypad_impl.py +926 -0
- numpy/lib/_arraypad_impl.pyi +88 -0
- numpy/lib/_arraysetops_impl.py +1158 -0
- numpy/lib/_arraysetops_impl.pyi +462 -0
- numpy/lib/_arrayterator_impl.py +224 -0
- numpy/lib/_arrayterator_impl.pyi +45 -0
- numpy/lib/_datasource.py +700 -0
- numpy/lib/_datasource.pyi +30 -0
- numpy/lib/_format_impl.py +1036 -0
- numpy/lib/_format_impl.pyi +56 -0
- numpy/lib/_function_base_impl.py +5760 -0
- numpy/lib/_function_base_impl.pyi +2324 -0
- numpy/lib/_histograms_impl.py +1085 -0
- numpy/lib/_histograms_impl.pyi +40 -0
- numpy/lib/_index_tricks_impl.py +1048 -0
- numpy/lib/_index_tricks_impl.pyi +267 -0
- numpy/lib/_iotools.py +900 -0
- numpy/lib/_iotools.pyi +116 -0
- numpy/lib/_nanfunctions_impl.py +2006 -0
- numpy/lib/_nanfunctions_impl.pyi +48 -0
- numpy/lib/_npyio_impl.py +2583 -0
- numpy/lib/_npyio_impl.pyi +299 -0
- numpy/lib/_polynomial_impl.py +1465 -0
- numpy/lib/_polynomial_impl.pyi +338 -0
- numpy/lib/_scimath_impl.py +642 -0
- numpy/lib/_scimath_impl.pyi +93 -0
- numpy/lib/_shape_base_impl.py +1289 -0
- numpy/lib/_shape_base_impl.pyi +236 -0
- numpy/lib/_stride_tricks_impl.py +582 -0
- numpy/lib/_stride_tricks_impl.pyi +73 -0
- numpy/lib/_twodim_base_impl.py +1201 -0
- numpy/lib/_twodim_base_impl.pyi +408 -0
- numpy/lib/_type_check_impl.py +710 -0
- numpy/lib/_type_check_impl.pyi +348 -0
- numpy/lib/_ufunclike_impl.py +199 -0
- numpy/lib/_ufunclike_impl.pyi +60 -0
- numpy/lib/_user_array_impl.py +310 -0
- numpy/lib/_user_array_impl.pyi +226 -0
- numpy/lib/_utils_impl.py +784 -0
- numpy/lib/_utils_impl.pyi +22 -0
- numpy/lib/_version.py +153 -0
- numpy/lib/_version.pyi +17 -0
- numpy/lib/array_utils.py +7 -0
- numpy/lib/array_utils.pyi +6 -0
- numpy/lib/format.py +24 -0
- numpy/lib/format.pyi +24 -0
- numpy/lib/introspect.py +94 -0
- numpy/lib/introspect.pyi +3 -0
- numpy/lib/mixins.py +180 -0
- numpy/lib/mixins.pyi +78 -0
- numpy/lib/npyio.py +1 -0
- numpy/lib/npyio.pyi +5 -0
- numpy/lib/recfunctions.py +1681 -0
- numpy/lib/recfunctions.pyi +444 -0
- numpy/lib/scimath.py +13 -0
- numpy/lib/scimath.pyi +12 -0
- numpy/lib/stride_tricks.py +1 -0
- numpy/lib/stride_tricks.pyi +4 -0
- numpy/lib/tests/__init__.py +0 -0
- numpy/lib/tests/data/py2-np0-objarr.npy +0 -0
- numpy/lib/tests/data/py2-objarr.npy +0 -0
- numpy/lib/tests/data/py2-objarr.npz +0 -0
- numpy/lib/tests/data/py3-objarr.npy +0 -0
- numpy/lib/tests/data/py3-objarr.npz +0 -0
- numpy/lib/tests/data/python3.npy +0 -0
- numpy/lib/tests/data/win64python2.npy +0 -0
- numpy/lib/tests/test__datasource.py +328 -0
- numpy/lib/tests/test__iotools.py +358 -0
- numpy/lib/tests/test__version.py +64 -0
- numpy/lib/tests/test_array_utils.py +32 -0
- numpy/lib/tests/test_arraypad.py +1427 -0
- numpy/lib/tests/test_arraysetops.py +1302 -0
- numpy/lib/tests/test_arrayterator.py +45 -0
- numpy/lib/tests/test_format.py +1054 -0
- numpy/lib/tests/test_function_base.py +4750 -0
- numpy/lib/tests/test_histograms.py +855 -0
- numpy/lib/tests/test_index_tricks.py +693 -0
- numpy/lib/tests/test_io.py +2857 -0
- numpy/lib/tests/test_loadtxt.py +1099 -0
- numpy/lib/tests/test_mixins.py +215 -0
- numpy/lib/tests/test_nanfunctions.py +1438 -0
- numpy/lib/tests/test_packbits.py +376 -0
- numpy/lib/tests/test_polynomial.py +325 -0
- numpy/lib/tests/test_recfunctions.py +1042 -0
- numpy/lib/tests/test_regression.py +231 -0
- numpy/lib/tests/test_shape_base.py +813 -0
- numpy/lib/tests/test_stride_tricks.py +655 -0
- numpy/lib/tests/test_twodim_base.py +559 -0
- numpy/lib/tests/test_type_check.py +473 -0
- numpy/lib/tests/test_ufunclike.py +97 -0
- numpy/lib/tests/test_utils.py +80 -0
- numpy/lib/user_array.py +1 -0
- numpy/lib/user_array.pyi +1 -0
- numpy/linalg/__init__.py +95 -0
- numpy/linalg/__init__.pyi +71 -0
- numpy/linalg/_linalg.py +3657 -0
- numpy/linalg/_linalg.pyi +548 -0
- numpy/linalg/_umath_linalg.pyi +60 -0
- numpy/linalg/_umath_linalg.pypy311-pp73-darwin.so +0 -0
- numpy/linalg/lapack_lite.pyi +143 -0
- numpy/linalg/lapack_lite.pypy311-pp73-darwin.so +0 -0
- numpy/linalg/tests/__init__.py +0 -0
- numpy/linalg/tests/test_deprecations.py +21 -0
- numpy/linalg/tests/test_linalg.py +2442 -0
- numpy/linalg/tests/test_regression.py +182 -0
- numpy/ma/API_CHANGES.txt +135 -0
- numpy/ma/LICENSE +24 -0
- numpy/ma/README.rst +236 -0
- numpy/ma/__init__.py +53 -0
- numpy/ma/__init__.pyi +458 -0
- numpy/ma/core.py +8929 -0
- numpy/ma/core.pyi +3720 -0
- numpy/ma/extras.py +2266 -0
- numpy/ma/extras.pyi +297 -0
- numpy/ma/mrecords.py +762 -0
- numpy/ma/mrecords.pyi +96 -0
- numpy/ma/tests/__init__.py +0 -0
- numpy/ma/tests/test_arrayobject.py +40 -0
- numpy/ma/tests/test_core.py +6008 -0
- numpy/ma/tests/test_deprecations.py +65 -0
- numpy/ma/tests/test_extras.py +1945 -0
- numpy/ma/tests/test_mrecords.py +495 -0
- numpy/ma/tests/test_old_ma.py +939 -0
- numpy/ma/tests/test_regression.py +83 -0
- numpy/ma/tests/test_subclassing.py +469 -0
- numpy/ma/testutils.py +294 -0
- numpy/ma/testutils.pyi +69 -0
- numpy/matlib.py +380 -0
- numpy/matlib.pyi +580 -0
- numpy/matrixlib/__init__.py +12 -0
- numpy/matrixlib/__init__.pyi +3 -0
- numpy/matrixlib/defmatrix.py +1119 -0
- numpy/matrixlib/defmatrix.pyi +218 -0
- numpy/matrixlib/tests/__init__.py +0 -0
- numpy/matrixlib/tests/test_defmatrix.py +455 -0
- numpy/matrixlib/tests/test_interaction.py +360 -0
- numpy/matrixlib/tests/test_masked_matrix.py +240 -0
- numpy/matrixlib/tests/test_matrix_linalg.py +110 -0
- numpy/matrixlib/tests/test_multiarray.py +17 -0
- numpy/matrixlib/tests/test_numeric.py +18 -0
- numpy/matrixlib/tests/test_regression.py +31 -0
- numpy/polynomial/__init__.py +187 -0
- numpy/polynomial/__init__.pyi +31 -0
- numpy/polynomial/_polybase.py +1191 -0
- numpy/polynomial/_polybase.pyi +262 -0
- numpy/polynomial/_polytypes.pyi +501 -0
- numpy/polynomial/chebyshev.py +2001 -0
- numpy/polynomial/chebyshev.pyi +180 -0
- numpy/polynomial/hermite.py +1738 -0
- numpy/polynomial/hermite.pyi +106 -0
- numpy/polynomial/hermite_e.py +1640 -0
- numpy/polynomial/hermite_e.pyi +106 -0
- numpy/polynomial/laguerre.py +1673 -0
- numpy/polynomial/laguerre.pyi +100 -0
- numpy/polynomial/legendre.py +1603 -0
- numpy/polynomial/legendre.pyi +100 -0
- numpy/polynomial/polynomial.py +1625 -0
- numpy/polynomial/polynomial.pyi +109 -0
- numpy/polynomial/polyutils.py +759 -0
- numpy/polynomial/polyutils.pyi +307 -0
- numpy/polynomial/tests/__init__.py +0 -0
- numpy/polynomial/tests/test_chebyshev.py +618 -0
- numpy/polynomial/tests/test_classes.py +613 -0
- numpy/polynomial/tests/test_hermite.py +553 -0
- numpy/polynomial/tests/test_hermite_e.py +554 -0
- numpy/polynomial/tests/test_laguerre.py +535 -0
- numpy/polynomial/tests/test_legendre.py +566 -0
- numpy/polynomial/tests/test_polynomial.py +691 -0
- numpy/polynomial/tests/test_polyutils.py +123 -0
- numpy/polynomial/tests/test_printing.py +557 -0
- numpy/polynomial/tests/test_symbol.py +217 -0
- numpy/py.typed +0 -0
- numpy/random/LICENSE.md +71 -0
- numpy/random/__init__.pxd +14 -0
- numpy/random/__init__.py +213 -0
- numpy/random/__init__.pyi +124 -0
- numpy/random/_bounded_integers.pxd +29 -0
- numpy/random/_bounded_integers.pyi +1 -0
- numpy/random/_bounded_integers.pypy311-pp73-darwin.so +0 -0
- numpy/random/_common.pxd +110 -0
- numpy/random/_common.pyi +16 -0
- numpy/random/_common.pypy311-pp73-darwin.so +0 -0
- numpy/random/_examples/cffi/extending.py +44 -0
- numpy/random/_examples/cffi/parse.py +53 -0
- numpy/random/_examples/cython/extending.pyx +77 -0
- numpy/random/_examples/cython/extending_distributions.pyx +117 -0
- numpy/random/_examples/cython/meson.build +53 -0
- numpy/random/_examples/numba/extending.py +86 -0
- numpy/random/_examples/numba/extending_distributions.py +67 -0
- numpy/random/_generator.pyi +862 -0
- numpy/random/_generator.pypy311-pp73-darwin.so +0 -0
- numpy/random/_mt19937.pyi +27 -0
- numpy/random/_mt19937.pypy311-pp73-darwin.so +0 -0
- numpy/random/_pcg64.pyi +41 -0
- numpy/random/_pcg64.pypy311-pp73-darwin.so +0 -0
- numpy/random/_philox.pyi +36 -0
- numpy/random/_philox.pypy311-pp73-darwin.so +0 -0
- numpy/random/_pickle.py +88 -0
- numpy/random/_pickle.pyi +43 -0
- numpy/random/_sfc64.pyi +25 -0
- numpy/random/_sfc64.pypy311-pp73-darwin.so +0 -0
- numpy/random/bit_generator.pxd +40 -0
- numpy/random/bit_generator.pyi +123 -0
- numpy/random/bit_generator.pypy311-pp73-darwin.so +0 -0
- numpy/random/c_distributions.pxd +119 -0
- numpy/random/lib/libnpyrandom.a +0 -0
- numpy/random/mtrand.pyi +759 -0
- numpy/random/mtrand.pypy311-pp73-darwin.so +0 -0
- numpy/random/tests/__init__.py +0 -0
- numpy/random/tests/data/__init__.py +0 -0
- numpy/random/tests/data/generator_pcg64_np121.pkl.gz +0 -0
- numpy/random/tests/data/generator_pcg64_np126.pkl.gz +0 -0
- numpy/random/tests/data/mt19937-testset-1.csv +1001 -0
- numpy/random/tests/data/mt19937-testset-2.csv +1001 -0
- numpy/random/tests/data/pcg64-testset-1.csv +1001 -0
- numpy/random/tests/data/pcg64-testset-2.csv +1001 -0
- numpy/random/tests/data/pcg64dxsm-testset-1.csv +1001 -0
- numpy/random/tests/data/pcg64dxsm-testset-2.csv +1001 -0
- numpy/random/tests/data/philox-testset-1.csv +1001 -0
- numpy/random/tests/data/philox-testset-2.csv +1001 -0
- numpy/random/tests/data/sfc64-testset-1.csv +1001 -0
- numpy/random/tests/data/sfc64-testset-2.csv +1001 -0
- numpy/random/tests/data/sfc64_np126.pkl.gz +0 -0
- numpy/random/tests/test_direct.py +595 -0
- numpy/random/tests/test_extending.py +131 -0
- numpy/random/tests/test_generator_mt19937.py +2825 -0
- numpy/random/tests/test_generator_mt19937_regressions.py +221 -0
- numpy/random/tests/test_random.py +1724 -0
- numpy/random/tests/test_randomstate.py +2099 -0
- numpy/random/tests/test_randomstate_regression.py +213 -0
- numpy/random/tests/test_regression.py +175 -0
- numpy/random/tests/test_seed_sequence.py +79 -0
- numpy/random/tests/test_smoke.py +882 -0
- numpy/rec/__init__.py +2 -0
- numpy/rec/__init__.pyi +23 -0
- numpy/strings/__init__.py +2 -0
- numpy/strings/__init__.pyi +97 -0
- numpy/testing/__init__.py +22 -0
- numpy/testing/__init__.pyi +107 -0
- numpy/testing/_private/__init__.py +0 -0
- numpy/testing/_private/__init__.pyi +0 -0
- numpy/testing/_private/extbuild.py +250 -0
- numpy/testing/_private/extbuild.pyi +25 -0
- numpy/testing/_private/utils.py +2830 -0
- numpy/testing/_private/utils.pyi +505 -0
- numpy/testing/overrides.py +84 -0
- numpy/testing/overrides.pyi +10 -0
- numpy/testing/print_coercion_tables.py +207 -0
- numpy/testing/print_coercion_tables.pyi +26 -0
- numpy/testing/tests/__init__.py +0 -0
- numpy/testing/tests/test_utils.py +2123 -0
- numpy/tests/__init__.py +0 -0
- numpy/tests/test__all__.py +10 -0
- numpy/tests/test_configtool.py +51 -0
- numpy/tests/test_ctypeslib.py +383 -0
- numpy/tests/test_lazyloading.py +42 -0
- numpy/tests/test_matlib.py +59 -0
- numpy/tests/test_numpy_config.py +47 -0
- numpy/tests/test_numpy_version.py +54 -0
- numpy/tests/test_public_api.py +807 -0
- numpy/tests/test_reloading.py +76 -0
- numpy/tests/test_scripts.py +48 -0
- numpy/tests/test_warnings.py +79 -0
- numpy/typing/__init__.py +233 -0
- numpy/typing/__init__.pyi +3 -0
- numpy/typing/mypy_plugin.py +200 -0
- numpy/typing/tests/__init__.py +0 -0
- numpy/typing/tests/data/fail/arithmetic.pyi +126 -0
- numpy/typing/tests/data/fail/array_constructors.pyi +34 -0
- numpy/typing/tests/data/fail/array_like.pyi +15 -0
- numpy/typing/tests/data/fail/array_pad.pyi +6 -0
- numpy/typing/tests/data/fail/arrayprint.pyi +15 -0
- numpy/typing/tests/data/fail/arrayterator.pyi +14 -0
- numpy/typing/tests/data/fail/bitwise_ops.pyi +17 -0
- numpy/typing/tests/data/fail/char.pyi +63 -0
- numpy/typing/tests/data/fail/chararray.pyi +61 -0
- numpy/typing/tests/data/fail/comparisons.pyi +27 -0
- numpy/typing/tests/data/fail/constants.pyi +3 -0
- numpy/typing/tests/data/fail/datasource.pyi +16 -0
- numpy/typing/tests/data/fail/dtype.pyi +17 -0
- numpy/typing/tests/data/fail/einsumfunc.pyi +12 -0
- numpy/typing/tests/data/fail/flatiter.pyi +38 -0
- numpy/typing/tests/data/fail/fromnumeric.pyi +148 -0
- numpy/typing/tests/data/fail/histograms.pyi +12 -0
- numpy/typing/tests/data/fail/index_tricks.pyi +14 -0
- numpy/typing/tests/data/fail/lib_function_base.pyi +60 -0
- numpy/typing/tests/data/fail/lib_polynomial.pyi +29 -0
- numpy/typing/tests/data/fail/lib_utils.pyi +3 -0
- numpy/typing/tests/data/fail/lib_version.pyi +6 -0
- numpy/typing/tests/data/fail/linalg.pyi +52 -0
- numpy/typing/tests/data/fail/ma.pyi +155 -0
- numpy/typing/tests/data/fail/memmap.pyi +5 -0
- numpy/typing/tests/data/fail/modules.pyi +17 -0
- numpy/typing/tests/data/fail/multiarray.pyi +52 -0
- numpy/typing/tests/data/fail/ndarray.pyi +11 -0
- numpy/typing/tests/data/fail/ndarray_misc.pyi +49 -0
- numpy/typing/tests/data/fail/nditer.pyi +8 -0
- numpy/typing/tests/data/fail/nested_sequence.pyi +17 -0
- numpy/typing/tests/data/fail/npyio.pyi +24 -0
- numpy/typing/tests/data/fail/numerictypes.pyi +5 -0
- numpy/typing/tests/data/fail/random.pyi +62 -0
- numpy/typing/tests/data/fail/rec.pyi +17 -0
- numpy/typing/tests/data/fail/scalars.pyi +86 -0
- numpy/typing/tests/data/fail/shape.pyi +7 -0
- numpy/typing/tests/data/fail/shape_base.pyi +8 -0
- numpy/typing/tests/data/fail/stride_tricks.pyi +9 -0
- numpy/typing/tests/data/fail/strings.pyi +52 -0
- numpy/typing/tests/data/fail/testing.pyi +28 -0
- numpy/typing/tests/data/fail/twodim_base.pyi +39 -0
- numpy/typing/tests/data/fail/type_check.pyi +12 -0
- numpy/typing/tests/data/fail/ufunc_config.pyi +21 -0
- numpy/typing/tests/data/fail/ufunclike.pyi +21 -0
- numpy/typing/tests/data/fail/ufuncs.pyi +17 -0
- numpy/typing/tests/data/fail/warnings_and_errors.pyi +5 -0
- numpy/typing/tests/data/misc/extended_precision.pyi +9 -0
- numpy/typing/tests/data/mypy.ini +8 -0
- numpy/typing/tests/data/pass/arithmetic.py +614 -0
- numpy/typing/tests/data/pass/array_constructors.py +138 -0
- numpy/typing/tests/data/pass/array_like.py +43 -0
- numpy/typing/tests/data/pass/arrayprint.py +37 -0
- numpy/typing/tests/data/pass/arrayterator.py +28 -0
- numpy/typing/tests/data/pass/bitwise_ops.py +131 -0
- numpy/typing/tests/data/pass/comparisons.py +316 -0
- numpy/typing/tests/data/pass/dtype.py +57 -0
- numpy/typing/tests/data/pass/einsumfunc.py +36 -0
- numpy/typing/tests/data/pass/flatiter.py +26 -0
- numpy/typing/tests/data/pass/fromnumeric.py +272 -0
- numpy/typing/tests/data/pass/index_tricks.py +62 -0
- numpy/typing/tests/data/pass/lib_user_array.py +22 -0
- numpy/typing/tests/data/pass/lib_utils.py +19 -0
- numpy/typing/tests/data/pass/lib_version.py +18 -0
- numpy/typing/tests/data/pass/literal.py +52 -0
- numpy/typing/tests/data/pass/ma.py +199 -0
- numpy/typing/tests/data/pass/mod.py +149 -0
- numpy/typing/tests/data/pass/modules.py +45 -0
- numpy/typing/tests/data/pass/multiarray.py +77 -0
- numpy/typing/tests/data/pass/ndarray_conversion.py +81 -0
- numpy/typing/tests/data/pass/ndarray_misc.py +199 -0
- numpy/typing/tests/data/pass/ndarray_shape_manipulation.py +47 -0
- numpy/typing/tests/data/pass/nditer.py +4 -0
- numpy/typing/tests/data/pass/numeric.py +90 -0
- numpy/typing/tests/data/pass/numerictypes.py +17 -0
- numpy/typing/tests/data/pass/random.py +1498 -0
- numpy/typing/tests/data/pass/recfunctions.py +164 -0
- numpy/typing/tests/data/pass/scalars.py +249 -0
- numpy/typing/tests/data/pass/shape.py +19 -0
- numpy/typing/tests/data/pass/simple.py +170 -0
- numpy/typing/tests/data/pass/ufunc_config.py +64 -0
- numpy/typing/tests/data/pass/ufunclike.py +52 -0
- numpy/typing/tests/data/pass/ufuncs.py +16 -0
- numpy/typing/tests/data/pass/warnings_and_errors.py +6 -0
- numpy/typing/tests/data/reveal/arithmetic.pyi +719 -0
- numpy/typing/tests/data/reveal/array_api_info.pyi +70 -0
- numpy/typing/tests/data/reveal/array_constructors.pyi +277 -0
- numpy/typing/tests/data/reveal/arraypad.pyi +27 -0
- numpy/typing/tests/data/reveal/arrayprint.pyi +25 -0
- numpy/typing/tests/data/reveal/arraysetops.pyi +74 -0
- numpy/typing/tests/data/reveal/arrayterator.pyi +27 -0
- numpy/typing/tests/data/reveal/bitwise_ops.pyi +166 -0
- numpy/typing/tests/data/reveal/char.pyi +225 -0
- numpy/typing/tests/data/reveal/chararray.pyi +138 -0
- numpy/typing/tests/data/reveal/comparisons.pyi +264 -0
- numpy/typing/tests/data/reveal/constants.pyi +14 -0
- numpy/typing/tests/data/reveal/ctypeslib.pyi +81 -0
- numpy/typing/tests/data/reveal/datasource.pyi +23 -0
- numpy/typing/tests/data/reveal/dtype.pyi +132 -0
- numpy/typing/tests/data/reveal/einsumfunc.pyi +39 -0
- numpy/typing/tests/data/reveal/emath.pyi +54 -0
- numpy/typing/tests/data/reveal/fft.pyi +37 -0
- numpy/typing/tests/data/reveal/flatiter.pyi +86 -0
- numpy/typing/tests/data/reveal/fromnumeric.pyi +347 -0
- numpy/typing/tests/data/reveal/getlimits.pyi +53 -0
- numpy/typing/tests/data/reveal/histograms.pyi +25 -0
- numpy/typing/tests/data/reveal/index_tricks.pyi +70 -0
- numpy/typing/tests/data/reveal/lib_function_base.pyi +409 -0
- numpy/typing/tests/data/reveal/lib_polynomial.pyi +147 -0
- numpy/typing/tests/data/reveal/lib_utils.pyi +17 -0
- numpy/typing/tests/data/reveal/lib_version.pyi +20 -0
- numpy/typing/tests/data/reveal/linalg.pyi +154 -0
- numpy/typing/tests/data/reveal/ma.pyi +1098 -0
- numpy/typing/tests/data/reveal/matrix.pyi +73 -0
- numpy/typing/tests/data/reveal/memmap.pyi +19 -0
- numpy/typing/tests/data/reveal/mod.pyi +178 -0
- numpy/typing/tests/data/reveal/modules.pyi +51 -0
- numpy/typing/tests/data/reveal/multiarray.pyi +197 -0
- numpy/typing/tests/data/reveal/nbit_base_example.pyi +20 -0
- numpy/typing/tests/data/reveal/ndarray_assignability.pyi +82 -0
- numpy/typing/tests/data/reveal/ndarray_conversion.pyi +83 -0
- numpy/typing/tests/data/reveal/ndarray_misc.pyi +246 -0
- numpy/typing/tests/data/reveal/ndarray_shape_manipulation.pyi +47 -0
- numpy/typing/tests/data/reveal/nditer.pyi +49 -0
- numpy/typing/tests/data/reveal/nested_sequence.pyi +25 -0
- numpy/typing/tests/data/reveal/npyio.pyi +83 -0
- numpy/typing/tests/data/reveal/numeric.pyi +170 -0
- numpy/typing/tests/data/reveal/numerictypes.pyi +16 -0
- numpy/typing/tests/data/reveal/polynomial_polybase.pyi +217 -0
- numpy/typing/tests/data/reveal/polynomial_polyutils.pyi +218 -0
- numpy/typing/tests/data/reveal/polynomial_series.pyi +138 -0
- numpy/typing/tests/data/reveal/random.pyi +1546 -0
- numpy/typing/tests/data/reveal/rec.pyi +171 -0
- numpy/typing/tests/data/reveal/scalars.pyi +191 -0
- numpy/typing/tests/data/reveal/shape.pyi +13 -0
- numpy/typing/tests/data/reveal/shape_base.pyi +52 -0
- numpy/typing/tests/data/reveal/stride_tricks.pyi +27 -0
- numpy/typing/tests/data/reveal/strings.pyi +196 -0
- numpy/typing/tests/data/reveal/testing.pyi +198 -0
- numpy/typing/tests/data/reveal/twodim_base.pyi +225 -0
- numpy/typing/tests/data/reveal/type_check.pyi +67 -0
- numpy/typing/tests/data/reveal/ufunc_config.pyi +29 -0
- numpy/typing/tests/data/reveal/ufunclike.pyi +31 -0
- numpy/typing/tests/data/reveal/ufuncs.pyi +142 -0
- numpy/typing/tests/data/reveal/warnings_and_errors.pyi +11 -0
- numpy/typing/tests/test_isfile.py +38 -0
- numpy/typing/tests/test_runtime.py +110 -0
- numpy/typing/tests/test_typing.py +205 -0
- numpy/version.py +11 -0
- numpy/version.pyi +9 -0
- numpy-2.4.1.dist-info/METADATA +139 -0
- numpy-2.4.1.dist-info/RECORD +1039 -0
- numpy-2.4.1.dist-info/WHEEL +6 -0
- numpy-2.4.1.dist-info/entry_points.txt +13 -0
- numpy-2.4.1.dist-info/licenses/LICENSE.txt +935 -0
- numpy-2.4.1.dist-info/licenses/numpy/_core/include/numpy/libdivide/LICENSE.txt +21 -0
- numpy-2.4.1.dist-info/licenses/numpy/_core/src/common/pythoncapi-compat/COPYING +14 -0
- numpy-2.4.1.dist-info/licenses/numpy/_core/src/highway/LICENSE +371 -0
- numpy-2.4.1.dist-info/licenses/numpy/_core/src/multiarray/dragon4_LICENSE.txt +27 -0
- numpy-2.4.1.dist-info/licenses/numpy/_core/src/npysort/x86-simd-sort/LICENSE.md +28 -0
- numpy-2.4.1.dist-info/licenses/numpy/_core/src/umath/svml/LICENSE +30 -0
- numpy-2.4.1.dist-info/licenses/numpy/fft/pocketfft/LICENSE.md +25 -0
- numpy-2.4.1.dist-info/licenses/numpy/linalg/lapack_lite/LICENSE.txt +48 -0
- numpy-2.4.1.dist-info/licenses/numpy/ma/LICENSE +24 -0
- numpy-2.4.1.dist-info/licenses/numpy/random/LICENSE.md +71 -0
- numpy-2.4.1.dist-info/licenses/numpy/random/src/distributions/LICENSE.md +61 -0
- numpy-2.4.1.dist-info/licenses/numpy/random/src/mt19937/LICENSE.md +61 -0
- numpy-2.4.1.dist-info/licenses/numpy/random/src/pcg64/LICENSE.md +22 -0
- numpy-2.4.1.dist-info/licenses/numpy/random/src/philox/LICENSE.md +31 -0
- numpy-2.4.1.dist-info/licenses/numpy/random/src/sfc64/LICENSE.md +27 -0
- numpy-2.4.1.dist-info/licenses/numpy/random/src/splitmix64/LICENSE.md +9 -0
|
@@ -0,0 +1,4750 @@
|
|
|
1
|
+
import decimal
|
|
2
|
+
import math
|
|
3
|
+
import operator
|
|
4
|
+
import sys
|
|
5
|
+
import warnings
|
|
6
|
+
from fractions import Fraction
|
|
7
|
+
from functools import partial
|
|
8
|
+
|
|
9
|
+
import hypothesis
|
|
10
|
+
import hypothesis.strategies as st
|
|
11
|
+
import pytest
|
|
12
|
+
from hypothesis.extra.numpy import arrays
|
|
13
|
+
|
|
14
|
+
import numpy as np
|
|
15
|
+
import numpy.lib._function_base_impl as nfb
|
|
16
|
+
from numpy import (
|
|
17
|
+
angle,
|
|
18
|
+
average,
|
|
19
|
+
bartlett,
|
|
20
|
+
blackman,
|
|
21
|
+
corrcoef,
|
|
22
|
+
cov,
|
|
23
|
+
delete,
|
|
24
|
+
diff,
|
|
25
|
+
digitize,
|
|
26
|
+
extract,
|
|
27
|
+
flipud,
|
|
28
|
+
gradient,
|
|
29
|
+
hamming,
|
|
30
|
+
hanning,
|
|
31
|
+
i0,
|
|
32
|
+
insert,
|
|
33
|
+
interp,
|
|
34
|
+
kaiser,
|
|
35
|
+
ma,
|
|
36
|
+
meshgrid,
|
|
37
|
+
piecewise,
|
|
38
|
+
place,
|
|
39
|
+
rot90,
|
|
40
|
+
select,
|
|
41
|
+
setxor1d,
|
|
42
|
+
sinc,
|
|
43
|
+
trapezoid,
|
|
44
|
+
trim_zeros,
|
|
45
|
+
unique,
|
|
46
|
+
unwrap,
|
|
47
|
+
vectorize,
|
|
48
|
+
)
|
|
49
|
+
from numpy._core.numeric import normalize_axis_tuple
|
|
50
|
+
from numpy.exceptions import AxisError
|
|
51
|
+
from numpy.random import rand
|
|
52
|
+
from numpy.testing import (
|
|
53
|
+
HAS_REFCOUNT,
|
|
54
|
+
IS_WASM,
|
|
55
|
+
NOGIL_BUILD,
|
|
56
|
+
assert_,
|
|
57
|
+
assert_allclose,
|
|
58
|
+
assert_almost_equal,
|
|
59
|
+
assert_array_almost_equal,
|
|
60
|
+
assert_array_equal,
|
|
61
|
+
assert_equal,
|
|
62
|
+
assert_raises,
|
|
63
|
+
assert_raises_regex,
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
np_floats = [np.half, np.single, np.double, np.longdouble]
|
|
67
|
+
|
|
68
|
+
def get_mat(n):
|
|
69
|
+
data = np.arange(n)
|
|
70
|
+
data = np.add.outer(data, data)
|
|
71
|
+
return data
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _make_complex(real, imag):
|
|
75
|
+
"""
|
|
76
|
+
Like real + 1j * imag, but behaves as expected when imag contains non-finite
|
|
77
|
+
values
|
|
78
|
+
"""
|
|
79
|
+
ret = np.zeros(np.broadcast(real, imag).shape, np.complex128)
|
|
80
|
+
ret.real = real
|
|
81
|
+
ret.imag = imag
|
|
82
|
+
return ret
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class TestRot90:
|
|
86
|
+
def test_basic(self):
|
|
87
|
+
assert_raises(ValueError, rot90, np.ones(4))
|
|
88
|
+
assert_raises(ValueError, rot90, np.ones((2, 2, 2)), axes=(0, 1, 2))
|
|
89
|
+
assert_raises(ValueError, rot90, np.ones((2, 2)), axes=(0, 2))
|
|
90
|
+
assert_raises(ValueError, rot90, np.ones((2, 2)), axes=(1, 1))
|
|
91
|
+
assert_raises(ValueError, rot90, np.ones((2, 2, 2)), axes=(-2, 1))
|
|
92
|
+
|
|
93
|
+
a = [[0, 1, 2],
|
|
94
|
+
[3, 4, 5]]
|
|
95
|
+
b1 = [[2, 5],
|
|
96
|
+
[1, 4],
|
|
97
|
+
[0, 3]]
|
|
98
|
+
b2 = [[5, 4, 3],
|
|
99
|
+
[2, 1, 0]]
|
|
100
|
+
b3 = [[3, 0],
|
|
101
|
+
[4, 1],
|
|
102
|
+
[5, 2]]
|
|
103
|
+
b4 = [[0, 1, 2],
|
|
104
|
+
[3, 4, 5]]
|
|
105
|
+
|
|
106
|
+
for k in range(-3, 13, 4):
|
|
107
|
+
assert_equal(rot90(a, k=k), b1)
|
|
108
|
+
for k in range(-2, 13, 4):
|
|
109
|
+
assert_equal(rot90(a, k=k), b2)
|
|
110
|
+
for k in range(-1, 13, 4):
|
|
111
|
+
assert_equal(rot90(a, k=k), b3)
|
|
112
|
+
for k in range(0, 13, 4):
|
|
113
|
+
assert_equal(rot90(a, k=k), b4)
|
|
114
|
+
|
|
115
|
+
assert_equal(rot90(rot90(a, axes=(0, 1)), axes=(1, 0)), a)
|
|
116
|
+
assert_equal(rot90(a, k=1, axes=(1, 0)), rot90(a, k=-1, axes=(0, 1)))
|
|
117
|
+
|
|
118
|
+
def test_axes(self):
|
|
119
|
+
a = np.ones((50, 40, 3))
|
|
120
|
+
assert_equal(rot90(a).shape, (40, 50, 3))
|
|
121
|
+
assert_equal(rot90(a, axes=(0, 2)), rot90(a, axes=(0, -1)))
|
|
122
|
+
assert_equal(rot90(a, axes=(1, 2)), rot90(a, axes=(-2, -1)))
|
|
123
|
+
|
|
124
|
+
def test_rotation_axes(self):
|
|
125
|
+
a = np.arange(8).reshape((2, 2, 2))
|
|
126
|
+
|
|
127
|
+
a_rot90_01 = [[[2, 3],
|
|
128
|
+
[6, 7]],
|
|
129
|
+
[[0, 1],
|
|
130
|
+
[4, 5]]]
|
|
131
|
+
a_rot90_12 = [[[1, 3],
|
|
132
|
+
[0, 2]],
|
|
133
|
+
[[5, 7],
|
|
134
|
+
[4, 6]]]
|
|
135
|
+
a_rot90_20 = [[[4, 0],
|
|
136
|
+
[6, 2]],
|
|
137
|
+
[[5, 1],
|
|
138
|
+
[7, 3]]]
|
|
139
|
+
a_rot90_10 = [[[4, 5],
|
|
140
|
+
[0, 1]],
|
|
141
|
+
[[6, 7],
|
|
142
|
+
[2, 3]]]
|
|
143
|
+
|
|
144
|
+
assert_equal(rot90(a, axes=(0, 1)), a_rot90_01)
|
|
145
|
+
assert_equal(rot90(a, axes=(1, 0)), a_rot90_10)
|
|
146
|
+
assert_equal(rot90(a, axes=(1, 2)), a_rot90_12)
|
|
147
|
+
|
|
148
|
+
for k in range(1, 5):
|
|
149
|
+
assert_equal(rot90(a, k=k, axes=(2, 0)),
|
|
150
|
+
rot90(a_rot90_20, k=k - 1, axes=(2, 0)))
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
class TestFlip:
|
|
154
|
+
|
|
155
|
+
def test_axes(self):
|
|
156
|
+
assert_raises(AxisError, np.flip, np.ones(4), axis=1)
|
|
157
|
+
assert_raises(AxisError, np.flip, np.ones((4, 4)), axis=2)
|
|
158
|
+
assert_raises(AxisError, np.flip, np.ones((4, 4)), axis=-3)
|
|
159
|
+
assert_raises(AxisError, np.flip, np.ones((4, 4)), axis=(0, 3))
|
|
160
|
+
|
|
161
|
+
def test_basic_lr(self):
|
|
162
|
+
a = get_mat(4)
|
|
163
|
+
b = a[:, ::-1]
|
|
164
|
+
assert_equal(np.flip(a, 1), b)
|
|
165
|
+
a = [[0, 1, 2],
|
|
166
|
+
[3, 4, 5]]
|
|
167
|
+
b = [[2, 1, 0],
|
|
168
|
+
[5, 4, 3]]
|
|
169
|
+
assert_equal(np.flip(a, 1), b)
|
|
170
|
+
|
|
171
|
+
def test_basic_ud(self):
|
|
172
|
+
a = get_mat(4)
|
|
173
|
+
b = a[::-1, :]
|
|
174
|
+
assert_equal(np.flip(a, 0), b)
|
|
175
|
+
a = [[0, 1, 2],
|
|
176
|
+
[3, 4, 5]]
|
|
177
|
+
b = [[3, 4, 5],
|
|
178
|
+
[0, 1, 2]]
|
|
179
|
+
assert_equal(np.flip(a, 0), b)
|
|
180
|
+
|
|
181
|
+
def test_3d_swap_axis0(self):
|
|
182
|
+
a = np.array([[[0, 1],
|
|
183
|
+
[2, 3]],
|
|
184
|
+
[[4, 5],
|
|
185
|
+
[6, 7]]])
|
|
186
|
+
|
|
187
|
+
b = np.array([[[4, 5],
|
|
188
|
+
[6, 7]],
|
|
189
|
+
[[0, 1],
|
|
190
|
+
[2, 3]]])
|
|
191
|
+
|
|
192
|
+
assert_equal(np.flip(a, 0), b)
|
|
193
|
+
|
|
194
|
+
def test_3d_swap_axis1(self):
|
|
195
|
+
a = np.array([[[0, 1],
|
|
196
|
+
[2, 3]],
|
|
197
|
+
[[4, 5],
|
|
198
|
+
[6, 7]]])
|
|
199
|
+
|
|
200
|
+
b = np.array([[[2, 3],
|
|
201
|
+
[0, 1]],
|
|
202
|
+
[[6, 7],
|
|
203
|
+
[4, 5]]])
|
|
204
|
+
|
|
205
|
+
assert_equal(np.flip(a, 1), b)
|
|
206
|
+
|
|
207
|
+
def test_3d_swap_axis2(self):
|
|
208
|
+
a = np.array([[[0, 1],
|
|
209
|
+
[2, 3]],
|
|
210
|
+
[[4, 5],
|
|
211
|
+
[6, 7]]])
|
|
212
|
+
|
|
213
|
+
b = np.array([[[1, 0],
|
|
214
|
+
[3, 2]],
|
|
215
|
+
[[5, 4],
|
|
216
|
+
[7, 6]]])
|
|
217
|
+
|
|
218
|
+
assert_equal(np.flip(a, 2), b)
|
|
219
|
+
|
|
220
|
+
def test_4d(self):
|
|
221
|
+
a = np.arange(2 * 3 * 4 * 5).reshape(2, 3, 4, 5)
|
|
222
|
+
for i in range(a.ndim):
|
|
223
|
+
assert_equal(np.flip(a, i),
|
|
224
|
+
np.flipud(a.swapaxes(0, i)).swapaxes(i, 0))
|
|
225
|
+
|
|
226
|
+
def test_default_axis(self):
|
|
227
|
+
a = np.array([[1, 2, 3],
|
|
228
|
+
[4, 5, 6]])
|
|
229
|
+
b = np.array([[6, 5, 4],
|
|
230
|
+
[3, 2, 1]])
|
|
231
|
+
assert_equal(np.flip(a), b)
|
|
232
|
+
|
|
233
|
+
def test_multiple_axes(self):
|
|
234
|
+
a = np.array([[[0, 1],
|
|
235
|
+
[2, 3]],
|
|
236
|
+
[[4, 5],
|
|
237
|
+
[6, 7]]])
|
|
238
|
+
|
|
239
|
+
assert_equal(np.flip(a, axis=()), a)
|
|
240
|
+
|
|
241
|
+
b = np.array([[[5, 4],
|
|
242
|
+
[7, 6]],
|
|
243
|
+
[[1, 0],
|
|
244
|
+
[3, 2]]])
|
|
245
|
+
|
|
246
|
+
assert_equal(np.flip(a, axis=(0, 2)), b)
|
|
247
|
+
|
|
248
|
+
c = np.array([[[3, 2],
|
|
249
|
+
[1, 0]],
|
|
250
|
+
[[7, 6],
|
|
251
|
+
[5, 4]]])
|
|
252
|
+
|
|
253
|
+
assert_equal(np.flip(a, axis=(1, 2)), c)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
class TestAny:
|
|
257
|
+
|
|
258
|
+
def test_basic(self):
|
|
259
|
+
y1 = [0, 0, 1, 0]
|
|
260
|
+
y2 = [0, 0, 0, 0]
|
|
261
|
+
y3 = [1, 0, 1, 0]
|
|
262
|
+
assert_(np.any(y1))
|
|
263
|
+
assert_(np.any(y3))
|
|
264
|
+
assert_(not np.any(y2))
|
|
265
|
+
|
|
266
|
+
def test_nd(self):
|
|
267
|
+
y1 = [[0, 0, 0], [0, 1, 0], [1, 1, 0]]
|
|
268
|
+
assert_(np.any(y1))
|
|
269
|
+
assert_array_equal(np.any(y1, axis=0), [1, 1, 0])
|
|
270
|
+
assert_array_equal(np.any(y1, axis=1), [0, 1, 1])
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
class TestAll:
|
|
274
|
+
|
|
275
|
+
def test_basic(self):
|
|
276
|
+
y1 = [0, 1, 1, 0]
|
|
277
|
+
y2 = [0, 0, 0, 0]
|
|
278
|
+
y3 = [1, 1, 1, 1]
|
|
279
|
+
assert_(not np.all(y1))
|
|
280
|
+
assert_(np.all(y3))
|
|
281
|
+
assert_(not np.all(y2))
|
|
282
|
+
assert_(np.all(~np.array(y2)))
|
|
283
|
+
|
|
284
|
+
def test_nd(self):
|
|
285
|
+
y1 = [[0, 0, 1], [0, 1, 1], [1, 1, 1]]
|
|
286
|
+
assert_(not np.all(y1))
|
|
287
|
+
assert_array_equal(np.all(y1, axis=0), [0, 0, 1])
|
|
288
|
+
assert_array_equal(np.all(y1, axis=1), [0, 0, 1])
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
@pytest.mark.parametrize("dtype", ["i8", "U10", "object", "datetime64[ms]"])
|
|
292
|
+
def test_any_and_all_result_dtype(dtype):
|
|
293
|
+
arr = np.ones(3, dtype=dtype)
|
|
294
|
+
assert np.any(arr).dtype == np.bool
|
|
295
|
+
assert np.all(arr).dtype == np.bool
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
class TestCopy:
|
|
299
|
+
|
|
300
|
+
def test_basic(self):
|
|
301
|
+
a = np.array([[1, 2], [3, 4]])
|
|
302
|
+
a_copy = np.copy(a)
|
|
303
|
+
assert_array_equal(a, a_copy)
|
|
304
|
+
a_copy[0, 0] = 10
|
|
305
|
+
assert_equal(a[0, 0], 1)
|
|
306
|
+
assert_equal(a_copy[0, 0], 10)
|
|
307
|
+
|
|
308
|
+
def test_order(self):
|
|
309
|
+
# It turns out that people rely on np.copy() preserving order by
|
|
310
|
+
# default; changing this broke scikit-learn:
|
|
311
|
+
# github.com/scikit-learn/scikit-learn/commit/7842748
|
|
312
|
+
a = np.array([[1, 2], [3, 4]])
|
|
313
|
+
assert_(a.flags.c_contiguous)
|
|
314
|
+
assert_(not a.flags.f_contiguous)
|
|
315
|
+
a_fort = np.array([[1, 2], [3, 4]], order="F")
|
|
316
|
+
assert_(not a_fort.flags.c_contiguous)
|
|
317
|
+
assert_(a_fort.flags.f_contiguous)
|
|
318
|
+
a_copy = np.copy(a)
|
|
319
|
+
assert_(a_copy.flags.c_contiguous)
|
|
320
|
+
assert_(not a_copy.flags.f_contiguous)
|
|
321
|
+
a_fort_copy = np.copy(a_fort)
|
|
322
|
+
assert_(not a_fort_copy.flags.c_contiguous)
|
|
323
|
+
assert_(a_fort_copy.flags.f_contiguous)
|
|
324
|
+
|
|
325
|
+
def test_subok(self):
|
|
326
|
+
mx = ma.ones(5)
|
|
327
|
+
assert_(not ma.isMaskedArray(np.copy(mx, subok=False)))
|
|
328
|
+
assert_(ma.isMaskedArray(np.copy(mx, subok=True)))
|
|
329
|
+
# Default behavior
|
|
330
|
+
assert_(not ma.isMaskedArray(np.copy(mx)))
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
class TestAverage:
|
|
334
|
+
|
|
335
|
+
def test_basic(self):
|
|
336
|
+
y1 = np.array([1, 2, 3])
|
|
337
|
+
assert_(average(y1, axis=0) == 2.)
|
|
338
|
+
y2 = np.array([1., 2., 3.])
|
|
339
|
+
assert_(average(y2, axis=0) == 2.)
|
|
340
|
+
y3 = [0., 0., 0.]
|
|
341
|
+
assert_(average(y3, axis=0) == 0.)
|
|
342
|
+
|
|
343
|
+
y4 = np.ones((4, 4))
|
|
344
|
+
y4[0, 1] = 0
|
|
345
|
+
y4[1, 0] = 2
|
|
346
|
+
assert_almost_equal(y4.mean(0), average(y4, 0))
|
|
347
|
+
assert_almost_equal(y4.mean(1), average(y4, 1))
|
|
348
|
+
|
|
349
|
+
y5 = rand(5, 5)
|
|
350
|
+
assert_almost_equal(y5.mean(0), average(y5, 0))
|
|
351
|
+
assert_almost_equal(y5.mean(1), average(y5, 1))
|
|
352
|
+
|
|
353
|
+
@pytest.mark.parametrize(
|
|
354
|
+
'x, axis, expected_avg, weights, expected_wavg, expected_wsum',
|
|
355
|
+
[([1, 2, 3], None, [2.0], [3, 4, 1], [1.75], [8.0]),
|
|
356
|
+
([[1, 2, 5], [1, 6, 11]], 0, [[1.0, 4.0, 8.0]],
|
|
357
|
+
[1, 3], [[1.0, 5.0, 9.5]], [[4, 4, 4]])],
|
|
358
|
+
)
|
|
359
|
+
def test_basic_keepdims(self, x, axis, expected_avg,
|
|
360
|
+
weights, expected_wavg, expected_wsum):
|
|
361
|
+
avg = np.average(x, axis=axis, keepdims=True)
|
|
362
|
+
assert avg.shape == np.shape(expected_avg)
|
|
363
|
+
assert_array_equal(avg, expected_avg)
|
|
364
|
+
|
|
365
|
+
wavg = np.average(x, axis=axis, weights=weights, keepdims=True)
|
|
366
|
+
assert wavg.shape == np.shape(expected_wavg)
|
|
367
|
+
assert_array_equal(wavg, expected_wavg)
|
|
368
|
+
|
|
369
|
+
wavg, wsum = np.average(x, axis=axis, weights=weights, returned=True,
|
|
370
|
+
keepdims=True)
|
|
371
|
+
assert wavg.shape == np.shape(expected_wavg)
|
|
372
|
+
assert_array_equal(wavg, expected_wavg)
|
|
373
|
+
assert wsum.shape == np.shape(expected_wsum)
|
|
374
|
+
assert_array_equal(wsum, expected_wsum)
|
|
375
|
+
|
|
376
|
+
def test_weights(self):
|
|
377
|
+
y = np.arange(10)
|
|
378
|
+
w = np.arange(10)
|
|
379
|
+
actual = average(y, weights=w)
|
|
380
|
+
desired = (np.arange(10) ** 2).sum() * 1. / np.arange(10).sum()
|
|
381
|
+
assert_almost_equal(actual, desired)
|
|
382
|
+
|
|
383
|
+
y1 = np.array([[1, 2, 3], [4, 5, 6]])
|
|
384
|
+
w0 = [1, 2]
|
|
385
|
+
actual = average(y1, weights=w0, axis=0)
|
|
386
|
+
desired = np.array([3., 4., 5.])
|
|
387
|
+
assert_almost_equal(actual, desired)
|
|
388
|
+
|
|
389
|
+
w1 = [0, 0, 1]
|
|
390
|
+
actual = average(y1, weights=w1, axis=1)
|
|
391
|
+
desired = np.array([3., 6.])
|
|
392
|
+
assert_almost_equal(actual, desired)
|
|
393
|
+
|
|
394
|
+
# weights and input have different shapes but no axis is specified
|
|
395
|
+
with pytest.raises(
|
|
396
|
+
TypeError,
|
|
397
|
+
match="Axis must be specified when shapes of a "
|
|
398
|
+
"and weights differ"):
|
|
399
|
+
average(y1, weights=w1)
|
|
400
|
+
|
|
401
|
+
# 2D Case
|
|
402
|
+
w2 = [[0, 0, 1], [0, 0, 2]]
|
|
403
|
+
desired = np.array([3., 6.])
|
|
404
|
+
assert_array_equal(average(y1, weights=w2, axis=1), desired)
|
|
405
|
+
assert_equal(average(y1, weights=w2), 5.)
|
|
406
|
+
|
|
407
|
+
y3 = rand(5).astype(np.float32)
|
|
408
|
+
w3 = rand(5).astype(np.float64)
|
|
409
|
+
|
|
410
|
+
assert_(np.average(y3, weights=w3).dtype == np.result_type(y3, w3))
|
|
411
|
+
|
|
412
|
+
# test weights with `keepdims=False` and `keepdims=True`
|
|
413
|
+
x = np.array([2, 3, 4]).reshape(3, 1)
|
|
414
|
+
w = np.array([4, 5, 6]).reshape(3, 1)
|
|
415
|
+
|
|
416
|
+
actual = np.average(x, weights=w, axis=1, keepdims=False)
|
|
417
|
+
desired = np.array([2., 3., 4.])
|
|
418
|
+
assert_array_equal(actual, desired)
|
|
419
|
+
|
|
420
|
+
actual = np.average(x, weights=w, axis=1, keepdims=True)
|
|
421
|
+
desired = np.array([[2.], [3.], [4.]])
|
|
422
|
+
assert_array_equal(actual, desired)
|
|
423
|
+
|
|
424
|
+
def test_weight_and_input_dims_different(self):
|
|
425
|
+
y = np.arange(12).reshape(2, 2, 3)
|
|
426
|
+
w = np.array([0., 0., 1., .5, .5, 0., 0., .5, .5, 1., 0., 0.])\
|
|
427
|
+
.reshape(2, 2, 3)
|
|
428
|
+
|
|
429
|
+
subw0 = w[:, :, 0]
|
|
430
|
+
actual = average(y, axis=(0, 1), weights=subw0)
|
|
431
|
+
desired = np.array([7., 8., 9.])
|
|
432
|
+
assert_almost_equal(actual, desired)
|
|
433
|
+
|
|
434
|
+
subw1 = w[1, :, :]
|
|
435
|
+
actual = average(y, axis=(1, 2), weights=subw1)
|
|
436
|
+
desired = np.array([2.25, 8.25])
|
|
437
|
+
assert_almost_equal(actual, desired)
|
|
438
|
+
|
|
439
|
+
subw2 = w[:, 0, :]
|
|
440
|
+
actual = average(y, axis=(0, 2), weights=subw2)
|
|
441
|
+
desired = np.array([4.75, 7.75])
|
|
442
|
+
assert_almost_equal(actual, desired)
|
|
443
|
+
|
|
444
|
+
# here the weights have the wrong shape for the specified axes
|
|
445
|
+
with pytest.raises(
|
|
446
|
+
ValueError,
|
|
447
|
+
match="Shape of weights must be consistent with "
|
|
448
|
+
"shape of a along specified axis"):
|
|
449
|
+
average(y, axis=(0, 1, 2), weights=subw0)
|
|
450
|
+
|
|
451
|
+
with pytest.raises(
|
|
452
|
+
ValueError,
|
|
453
|
+
match="Shape of weights must be consistent with "
|
|
454
|
+
"shape of a along specified axis"):
|
|
455
|
+
average(y, axis=(0, 1), weights=subw1)
|
|
456
|
+
|
|
457
|
+
# swapping the axes should be same as transposing weights
|
|
458
|
+
actual = average(y, axis=(1, 0), weights=subw0)
|
|
459
|
+
desired = average(y, axis=(0, 1), weights=subw0.T)
|
|
460
|
+
assert_almost_equal(actual, desired)
|
|
461
|
+
|
|
462
|
+
# if average over all axes, should have float output
|
|
463
|
+
actual = average(y, axis=(0, 1, 2), weights=w)
|
|
464
|
+
assert_(actual.ndim == 0)
|
|
465
|
+
|
|
466
|
+
def test_returned(self):
|
|
467
|
+
y = np.array([[1, 2, 3], [4, 5, 6]])
|
|
468
|
+
|
|
469
|
+
# No weights
|
|
470
|
+
avg, scl = average(y, returned=True)
|
|
471
|
+
assert_equal(scl, 6.)
|
|
472
|
+
|
|
473
|
+
avg, scl = average(y, 0, returned=True)
|
|
474
|
+
assert_array_equal(scl, np.array([2., 2., 2.]))
|
|
475
|
+
|
|
476
|
+
avg, scl = average(y, 1, returned=True)
|
|
477
|
+
assert_array_equal(scl, np.array([3., 3.]))
|
|
478
|
+
|
|
479
|
+
# With weights
|
|
480
|
+
w0 = [1, 2]
|
|
481
|
+
avg, scl = average(y, weights=w0, axis=0, returned=True)
|
|
482
|
+
assert_array_equal(scl, np.array([3., 3., 3.]))
|
|
483
|
+
|
|
484
|
+
w1 = [1, 2, 3]
|
|
485
|
+
avg, scl = average(y, weights=w1, axis=1, returned=True)
|
|
486
|
+
assert_array_equal(scl, np.array([6., 6.]))
|
|
487
|
+
|
|
488
|
+
w2 = [[0, 0, 1], [1, 2, 3]]
|
|
489
|
+
avg, scl = average(y, weights=w2, axis=1, returned=True)
|
|
490
|
+
assert_array_equal(scl, np.array([1., 6.]))
|
|
491
|
+
|
|
492
|
+
def test_subclasses(self):
|
|
493
|
+
class subclass(np.ndarray):
|
|
494
|
+
pass
|
|
495
|
+
a = np.array([[1, 2], [3, 4]]).view(subclass)
|
|
496
|
+
w = np.array([[1, 2], [3, 4]]).view(subclass)
|
|
497
|
+
|
|
498
|
+
assert_equal(type(np.average(a)), subclass)
|
|
499
|
+
assert_equal(type(np.average(a, weights=w)), subclass)
|
|
500
|
+
# Ensure a possibly returned sum of weights is correct too.
|
|
501
|
+
ra, rw = np.average(a, weights=w, returned=True)
|
|
502
|
+
assert_equal(type(ra), subclass)
|
|
503
|
+
assert_equal(type(rw), subclass)
|
|
504
|
+
# Even if it needs to be broadcast.
|
|
505
|
+
ra, rw = np.average(a, weights=w[0], axis=1, returned=True)
|
|
506
|
+
assert_equal(type(ra), subclass)
|
|
507
|
+
assert_equal(type(rw), subclass)
|
|
508
|
+
|
|
509
|
+
def test_upcasting(self):
|
|
510
|
+
typs = [('i4', 'i4', 'f8'), ('i4', 'f4', 'f8'), ('f4', 'i4', 'f8'),
|
|
511
|
+
('f4', 'f4', 'f4'), ('f4', 'f8', 'f8')]
|
|
512
|
+
for at, wt, rt in typs:
|
|
513
|
+
a = np.array([[1, 2], [3, 4]], dtype=at)
|
|
514
|
+
w = np.array([[1, 2], [3, 4]], dtype=wt)
|
|
515
|
+
assert_equal(np.average(a, weights=w).dtype, np.dtype(rt))
|
|
516
|
+
|
|
517
|
+
def test_object_dtype(self):
|
|
518
|
+
a = np.array([decimal.Decimal(x) for x in range(10)])
|
|
519
|
+
w = np.array([decimal.Decimal(1) for _ in range(10)])
|
|
520
|
+
w /= w.sum()
|
|
521
|
+
assert_almost_equal(a.mean(0), average(a, weights=w))
|
|
522
|
+
|
|
523
|
+
def test_object_no_weights(self):
|
|
524
|
+
a = np.array([decimal.Decimal(x) for x in range(10)])
|
|
525
|
+
m = average(a)
|
|
526
|
+
assert m == decimal.Decimal('4.5')
|
|
527
|
+
|
|
528
|
+
def test_average_class_without_dtype(self):
|
|
529
|
+
# see gh-21988
|
|
530
|
+
a = np.array([Fraction(1, 5), Fraction(3, 5)])
|
|
531
|
+
assert_equal(np.average(a), Fraction(2, 5))
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
class TestSelect:
|
|
535
|
+
choices = [np.array([1, 2, 3]),
|
|
536
|
+
np.array([4, 5, 6]),
|
|
537
|
+
np.array([7, 8, 9])]
|
|
538
|
+
conditions = [np.array([False, False, False]),
|
|
539
|
+
np.array([False, True, False]),
|
|
540
|
+
np.array([False, False, True])]
|
|
541
|
+
|
|
542
|
+
def _select(self, cond, values, default=0):
|
|
543
|
+
output = []
|
|
544
|
+
for m in range(len(cond)):
|
|
545
|
+
output += [V[m] for V, C in zip(values, cond) if C[m]] or [default]
|
|
546
|
+
return output
|
|
547
|
+
|
|
548
|
+
def test_basic(self):
|
|
549
|
+
choices = self.choices
|
|
550
|
+
conditions = self.conditions
|
|
551
|
+
assert_array_equal(select(conditions, choices, default=15),
|
|
552
|
+
self._select(conditions, choices, default=15))
|
|
553
|
+
|
|
554
|
+
assert_equal(len(choices), 3)
|
|
555
|
+
assert_equal(len(conditions), 3)
|
|
556
|
+
|
|
557
|
+
def test_broadcasting(self):
|
|
558
|
+
conditions = [np.array(True), np.array([False, True, False])]
|
|
559
|
+
choices = [1, np.arange(12).reshape(4, 3)]
|
|
560
|
+
assert_array_equal(select(conditions, choices), np.ones((4, 3)))
|
|
561
|
+
# default can broadcast too:
|
|
562
|
+
assert_equal(select([True], [0], default=[0]).shape, (1,))
|
|
563
|
+
|
|
564
|
+
def test_return_dtype(self):
|
|
565
|
+
assert_equal(select(self.conditions, self.choices, 1j).dtype,
|
|
566
|
+
np.complex128)
|
|
567
|
+
# But the conditions need to be stronger then the scalar default
|
|
568
|
+
# if it is scalar.
|
|
569
|
+
choices = [choice.astype(np.int8) for choice in self.choices]
|
|
570
|
+
assert_equal(select(self.conditions, choices).dtype, np.int8)
|
|
571
|
+
|
|
572
|
+
d = np.array([1, 2, 3, np.nan, 5, 7])
|
|
573
|
+
m = np.isnan(d)
|
|
574
|
+
assert_equal(select([m], [d]), [0, 0, 0, np.nan, 0, 0])
|
|
575
|
+
|
|
576
|
+
def test_non_bool_deprecation(self):
|
|
577
|
+
choices = self.choices
|
|
578
|
+
conditions = self.conditions[:]
|
|
579
|
+
conditions[0] = conditions[0].astype(np.int_)
|
|
580
|
+
assert_raises(TypeError, select, conditions, choices)
|
|
581
|
+
conditions[0] = conditions[0].astype(np.uint8)
|
|
582
|
+
assert_raises(TypeError, select, conditions, choices)
|
|
583
|
+
assert_raises(TypeError, select, conditions, choices)
|
|
584
|
+
|
|
585
|
+
def test_many_arguments(self):
|
|
586
|
+
# This used to be limited by NPY_MAXARGS == 32
|
|
587
|
+
conditions = [np.array([False])] * 100
|
|
588
|
+
choices = [np.array([1])] * 100
|
|
589
|
+
select(conditions, choices)
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
class TestInsert:
|
|
593
|
+
|
|
594
|
+
def test_basic(self):
|
|
595
|
+
a = [1, 2, 3]
|
|
596
|
+
assert_equal(insert(a, 0, 1), [1, 1, 2, 3])
|
|
597
|
+
assert_equal(insert(a, 3, 1), [1, 2, 3, 1])
|
|
598
|
+
assert_equal(insert(a, [1, 1, 1], [1, 2, 3]), [1, 1, 2, 3, 2, 3])
|
|
599
|
+
assert_equal(insert(a, 1, [1, 2, 3]), [1, 1, 2, 3, 2, 3])
|
|
600
|
+
assert_equal(insert(a, [1, -1, 3], 9), [1, 9, 2, 9, 3, 9])
|
|
601
|
+
assert_equal(insert(a, slice(-1, None, -1), 9), [9, 1, 9, 2, 9, 3])
|
|
602
|
+
assert_equal(insert(a, [-1, 1, 3], [7, 8, 9]), [1, 8, 2, 7, 3, 9])
|
|
603
|
+
b = np.array([0, 1], dtype=np.float64)
|
|
604
|
+
assert_equal(insert(b, 0, b[0]), [0., 0., 1.])
|
|
605
|
+
assert_equal(insert(b, [], []), b)
|
|
606
|
+
assert_equal(insert(a, np.array([True] * 4), 9), [9, 1, 9, 2, 9, 3, 9])
|
|
607
|
+
assert_equal(insert(a, np.array([True, False, True, False]), 9),
|
|
608
|
+
[9, 1, 2, 9, 3])
|
|
609
|
+
|
|
610
|
+
def test_multidim(self):
|
|
611
|
+
a = [[1, 1, 1]]
|
|
612
|
+
r = [[2, 2, 2],
|
|
613
|
+
[1, 1, 1]]
|
|
614
|
+
assert_equal(insert(a, 0, [1]), [1, 1, 1, 1])
|
|
615
|
+
assert_equal(insert(a, 0, [2, 2, 2], axis=0), r)
|
|
616
|
+
assert_equal(insert(a, 0, 2, axis=0), r)
|
|
617
|
+
assert_equal(insert(a, 2, 2, axis=1), [[1, 1, 2, 1]])
|
|
618
|
+
|
|
619
|
+
a = np.array([[1, 1], [2, 2], [3, 3]])
|
|
620
|
+
b = np.arange(1, 4).repeat(3).reshape(3, 3)
|
|
621
|
+
c = np.concatenate(
|
|
622
|
+
(a[:, 0:1], np.arange(1, 4).repeat(3).reshape(3, 3).T,
|
|
623
|
+
a[:, 1:2]), axis=1)
|
|
624
|
+
assert_equal(insert(a, [1], [[1], [2], [3]], axis=1), b)
|
|
625
|
+
assert_equal(insert(a, [1], [1, 2, 3], axis=1), c)
|
|
626
|
+
# scalars behave differently, in this case exactly opposite:
|
|
627
|
+
assert_equal(insert(a, 1, [1, 2, 3], axis=1), b)
|
|
628
|
+
assert_equal(insert(a, 1, [[1], [2], [3]], axis=1), c)
|
|
629
|
+
|
|
630
|
+
a = np.arange(4).reshape(2, 2)
|
|
631
|
+
assert_equal(insert(a[:, :1], 1, a[:, 1], axis=1), a)
|
|
632
|
+
assert_equal(insert(a[:1, :], 1, a[1, :], axis=0), a)
|
|
633
|
+
|
|
634
|
+
# negative axis value
|
|
635
|
+
a = np.arange(24).reshape((2, 3, 4))
|
|
636
|
+
assert_equal(insert(a, 1, a[:, :, 3], axis=-1),
|
|
637
|
+
insert(a, 1, a[:, :, 3], axis=2))
|
|
638
|
+
assert_equal(insert(a, 1, a[:, 2, :], axis=-2),
|
|
639
|
+
insert(a, 1, a[:, 2, :], axis=1))
|
|
640
|
+
|
|
641
|
+
# invalid axis value
|
|
642
|
+
assert_raises(AxisError, insert, a, 1, a[:, 2, :], axis=3)
|
|
643
|
+
assert_raises(AxisError, insert, a, 1, a[:, 2, :], axis=-4)
|
|
644
|
+
|
|
645
|
+
# negative axis value
|
|
646
|
+
a = np.arange(24).reshape((2, 3, 4))
|
|
647
|
+
assert_equal(insert(a, 1, a[:, :, 3], axis=-1),
|
|
648
|
+
insert(a, 1, a[:, :, 3], axis=2))
|
|
649
|
+
assert_equal(insert(a, 1, a[:, 2, :], axis=-2),
|
|
650
|
+
insert(a, 1, a[:, 2, :], axis=1))
|
|
651
|
+
|
|
652
|
+
def test_0d(self):
|
|
653
|
+
a = np.array(1)
|
|
654
|
+
with pytest.raises(AxisError):
|
|
655
|
+
insert(a, [], 2, axis=0)
|
|
656
|
+
with pytest.raises(TypeError):
|
|
657
|
+
insert(a, [], 2, axis="nonsense")
|
|
658
|
+
|
|
659
|
+
def test_subclass(self):
|
|
660
|
+
class SubClass(np.ndarray):
|
|
661
|
+
pass
|
|
662
|
+
a = np.arange(10).view(SubClass)
|
|
663
|
+
assert_(isinstance(np.insert(a, 0, [0]), SubClass))
|
|
664
|
+
assert_(isinstance(np.insert(a, [], []), SubClass))
|
|
665
|
+
assert_(isinstance(np.insert(a, [0, 1], [1, 2]), SubClass))
|
|
666
|
+
assert_(isinstance(np.insert(a, slice(1, 2), [1, 2]), SubClass))
|
|
667
|
+
assert_(isinstance(np.insert(a, slice(1, -2, -1), []), SubClass))
|
|
668
|
+
# This is an error in the future:
|
|
669
|
+
a = np.array(1).view(SubClass)
|
|
670
|
+
assert_(isinstance(np.insert(a, 0, [0]), SubClass))
|
|
671
|
+
|
|
672
|
+
def test_index_array_copied(self):
|
|
673
|
+
x = np.array([1, 1, 1])
|
|
674
|
+
np.insert([0, 1, 2], x, [3, 4, 5])
|
|
675
|
+
assert_equal(x, np.array([1, 1, 1]))
|
|
676
|
+
|
|
677
|
+
def test_structured_array(self):
|
|
678
|
+
a = np.array([(1, 'a'), (2, 'b'), (3, 'c')],
|
|
679
|
+
dtype=[('foo', 'i'), ('bar', 'S1')])
|
|
680
|
+
val = (4, 'd')
|
|
681
|
+
b = np.insert(a, 0, val)
|
|
682
|
+
assert_array_equal(b[0], np.array(val, dtype=b.dtype))
|
|
683
|
+
val = [(4, 'd')] * 2
|
|
684
|
+
b = np.insert(a, [0, 2], val)
|
|
685
|
+
assert_array_equal(b[[0, 3]], np.array(val, dtype=b.dtype))
|
|
686
|
+
|
|
687
|
+
def test_index_floats(self):
|
|
688
|
+
with pytest.raises(IndexError):
|
|
689
|
+
np.insert([0, 1, 2], np.array([1.0, 2.0]), [10, 20])
|
|
690
|
+
with pytest.raises(IndexError):
|
|
691
|
+
np.insert([0, 1, 2], np.array([], dtype=float), [])
|
|
692
|
+
|
|
693
|
+
@pytest.mark.parametrize('idx', [4, -4])
|
|
694
|
+
def test_index_out_of_bounds(self, idx):
|
|
695
|
+
with pytest.raises(IndexError, match='out of bounds'):
|
|
696
|
+
np.insert([0, 1, 2], [idx], [3, 4])
|
|
697
|
+
|
|
698
|
+
|
|
699
|
+
class TestAmax:
|
|
700
|
+
|
|
701
|
+
def test_basic(self):
|
|
702
|
+
a = [3, 4, 5, 10, -3, -5, 6.0]
|
|
703
|
+
assert_equal(np.amax(a), 10.0)
|
|
704
|
+
b = [[3, 6.0, 9.0],
|
|
705
|
+
[4, 10.0, 5.0],
|
|
706
|
+
[8, 3.0, 2.0]]
|
|
707
|
+
assert_equal(np.amax(b, axis=0), [8.0, 10.0, 9.0])
|
|
708
|
+
assert_equal(np.amax(b, axis=1), [9.0, 10.0, 8.0])
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
class TestAmin:
|
|
712
|
+
|
|
713
|
+
def test_basic(self):
|
|
714
|
+
a = [3, 4, 5, 10, -3, -5, 6.0]
|
|
715
|
+
assert_equal(np.amin(a), -5.0)
|
|
716
|
+
b = [[3, 6.0, 9.0],
|
|
717
|
+
[4, 10.0, 5.0],
|
|
718
|
+
[8, 3.0, 2.0]]
|
|
719
|
+
assert_equal(np.amin(b, axis=0), [3.0, 3.0, 2.0])
|
|
720
|
+
assert_equal(np.amin(b, axis=1), [3.0, 4.0, 2.0])
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
class TestPtp:
|
|
724
|
+
|
|
725
|
+
def test_basic(self):
|
|
726
|
+
a = np.array([3, 4, 5, 10, -3, -5, 6.0])
|
|
727
|
+
assert_equal(np.ptp(a, axis=0), 15.0)
|
|
728
|
+
b = np.array([[3, 6.0, 9.0],
|
|
729
|
+
[4, 10.0, 5.0],
|
|
730
|
+
[8, 3.0, 2.0]])
|
|
731
|
+
assert_equal(np.ptp(b, axis=0), [5.0, 7.0, 7.0])
|
|
732
|
+
assert_equal(np.ptp(b, axis=-1), [6.0, 6.0, 6.0])
|
|
733
|
+
|
|
734
|
+
assert_equal(np.ptp(b, axis=0, keepdims=True), [[5.0, 7.0, 7.0]])
|
|
735
|
+
assert_equal(np.ptp(b, axis=(0, 1), keepdims=True), [[8.0]])
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
class TestCumsum:
|
|
739
|
+
|
|
740
|
+
@pytest.mark.parametrize("cumsum", [np.cumsum, np.cumulative_sum])
|
|
741
|
+
def test_basic(self, cumsum):
|
|
742
|
+
ba = [1, 2, 10, 11, 6, 5, 4]
|
|
743
|
+
ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]]
|
|
744
|
+
for ctype in [np.int8, np.uint8, np.int16, np.uint16, np.int32,
|
|
745
|
+
np.uint32, np.float32, np.float64, np.complex64,
|
|
746
|
+
np.complex128]:
|
|
747
|
+
a = np.array(ba, ctype)
|
|
748
|
+
a2 = np.array(ba2, ctype)
|
|
749
|
+
|
|
750
|
+
tgt = np.array([1, 3, 13, 24, 30, 35, 39], ctype)
|
|
751
|
+
assert_array_equal(cumsum(a, axis=0), tgt)
|
|
752
|
+
|
|
753
|
+
tgt = np.array(
|
|
754
|
+
[[1, 2, 3, 4], [6, 8, 10, 13], [16, 11, 14, 18]], ctype)
|
|
755
|
+
assert_array_equal(cumsum(a2, axis=0), tgt)
|
|
756
|
+
|
|
757
|
+
tgt = np.array(
|
|
758
|
+
[[1, 3, 6, 10], [5, 11, 18, 27], [10, 13, 17, 22]], ctype)
|
|
759
|
+
assert_array_equal(cumsum(a2, axis=1), tgt)
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
class TestProd:
|
|
763
|
+
|
|
764
|
+
def test_basic(self):
|
|
765
|
+
ba = [1, 2, 10, 11, 6, 5, 4]
|
|
766
|
+
ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]]
|
|
767
|
+
for ctype in [np.int16, np.uint16, np.int32, np.uint32,
|
|
768
|
+
np.float32, np.float64, np.complex64, np.complex128]:
|
|
769
|
+
a = np.array(ba, ctype)
|
|
770
|
+
a2 = np.array(ba2, ctype)
|
|
771
|
+
if ctype in ['1', 'b']:
|
|
772
|
+
assert_raises(ArithmeticError, np.prod, a)
|
|
773
|
+
assert_raises(ArithmeticError, np.prod, a2, 1)
|
|
774
|
+
else:
|
|
775
|
+
assert_equal(a.prod(axis=0), 26400)
|
|
776
|
+
assert_array_equal(a2.prod(axis=0),
|
|
777
|
+
np.array([50, 36, 84, 180], ctype))
|
|
778
|
+
assert_array_equal(a2.prod(axis=-1),
|
|
779
|
+
np.array([24, 1890, 600], ctype))
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
class TestCumprod:
|
|
783
|
+
|
|
784
|
+
@pytest.mark.parametrize("cumprod", [np.cumprod, np.cumulative_prod])
|
|
785
|
+
def test_basic(self, cumprod):
|
|
786
|
+
ba = [1, 2, 10, 11, 6, 5, 4]
|
|
787
|
+
ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]]
|
|
788
|
+
for ctype in [np.int16, np.uint16, np.int32, np.uint32,
|
|
789
|
+
np.float32, np.float64, np.complex64, np.complex128]:
|
|
790
|
+
a = np.array(ba, ctype)
|
|
791
|
+
a2 = np.array(ba2, ctype)
|
|
792
|
+
if ctype in ['1', 'b']:
|
|
793
|
+
assert_raises(ArithmeticError, cumprod, a)
|
|
794
|
+
assert_raises(ArithmeticError, cumprod, a2, 1)
|
|
795
|
+
assert_raises(ArithmeticError, cumprod, a)
|
|
796
|
+
else:
|
|
797
|
+
assert_array_equal(cumprod(a, axis=-1),
|
|
798
|
+
np.array([1, 2, 20, 220,
|
|
799
|
+
1320, 6600, 26400], ctype))
|
|
800
|
+
assert_array_equal(cumprod(a2, axis=0),
|
|
801
|
+
np.array([[1, 2, 3, 4],
|
|
802
|
+
[5, 12, 21, 36],
|
|
803
|
+
[50, 36, 84, 180]], ctype))
|
|
804
|
+
assert_array_equal(cumprod(a2, axis=-1),
|
|
805
|
+
np.array([[1, 2, 6, 24],
|
|
806
|
+
[5, 30, 210, 1890],
|
|
807
|
+
[10, 30, 120, 600]], ctype))
|
|
808
|
+
|
|
809
|
+
|
|
810
|
+
def test_cumulative_include_initial():
|
|
811
|
+
arr = np.arange(8).reshape((2, 2, 2))
|
|
812
|
+
|
|
813
|
+
expected = np.array([
|
|
814
|
+
[[0, 0], [0, 1], [2, 4]], [[0, 0], [4, 5], [10, 12]]
|
|
815
|
+
])
|
|
816
|
+
assert_array_equal(
|
|
817
|
+
np.cumulative_sum(arr, axis=1, include_initial=True), expected
|
|
818
|
+
)
|
|
819
|
+
|
|
820
|
+
expected = np.array([
|
|
821
|
+
[[1, 0, 0], [1, 2, 6]], [[1, 4, 20], [1, 6, 42]]
|
|
822
|
+
])
|
|
823
|
+
assert_array_equal(
|
|
824
|
+
np.cumulative_prod(arr, axis=2, include_initial=True), expected
|
|
825
|
+
)
|
|
826
|
+
|
|
827
|
+
out = np.zeros((3, 2), dtype=np.float64)
|
|
828
|
+
expected = np.array([[0, 0], [1, 2], [4, 6]], dtype=np.float64)
|
|
829
|
+
arr = np.arange(1, 5).reshape((2, 2))
|
|
830
|
+
np.cumulative_sum(arr, axis=0, out=out, include_initial=True)
|
|
831
|
+
assert_array_equal(out, expected)
|
|
832
|
+
|
|
833
|
+
expected = np.array([1, 2, 4])
|
|
834
|
+
assert_array_equal(
|
|
835
|
+
np.cumulative_prod(np.array([2, 2]), include_initial=True), expected
|
|
836
|
+
)
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
class TestDiff:
|
|
840
|
+
|
|
841
|
+
def test_basic(self):
|
|
842
|
+
x = [1, 4, 6, 7, 12]
|
|
843
|
+
out = np.array([3, 2, 1, 5])
|
|
844
|
+
out2 = np.array([-1, -1, 4])
|
|
845
|
+
out3 = np.array([0, 5])
|
|
846
|
+
assert_array_equal(diff(x), out)
|
|
847
|
+
assert_array_equal(diff(x, n=2), out2)
|
|
848
|
+
assert_array_equal(diff(x, n=3), out3)
|
|
849
|
+
|
|
850
|
+
x = [1.1, 2.2, 3.0, -0.2, -0.1]
|
|
851
|
+
out = np.array([1.1, 0.8, -3.2, 0.1])
|
|
852
|
+
assert_almost_equal(diff(x), out)
|
|
853
|
+
|
|
854
|
+
x = [True, True, False, False]
|
|
855
|
+
out = np.array([False, True, False])
|
|
856
|
+
out2 = np.array([True, True])
|
|
857
|
+
assert_array_equal(diff(x), out)
|
|
858
|
+
assert_array_equal(diff(x, n=2), out2)
|
|
859
|
+
|
|
860
|
+
def test_axis(self):
|
|
861
|
+
x = np.zeros((10, 20, 30))
|
|
862
|
+
x[:, 1::2, :] = 1
|
|
863
|
+
exp = np.ones((10, 19, 30))
|
|
864
|
+
exp[:, 1::2, :] = -1
|
|
865
|
+
assert_array_equal(diff(x), np.zeros((10, 20, 29)))
|
|
866
|
+
assert_array_equal(diff(x, axis=-1), np.zeros((10, 20, 29)))
|
|
867
|
+
assert_array_equal(diff(x, axis=0), np.zeros((9, 20, 30)))
|
|
868
|
+
assert_array_equal(diff(x, axis=1), exp)
|
|
869
|
+
assert_array_equal(diff(x, axis=-2), exp)
|
|
870
|
+
assert_raises(AxisError, diff, x, axis=3)
|
|
871
|
+
assert_raises(AxisError, diff, x, axis=-4)
|
|
872
|
+
|
|
873
|
+
x = np.array(1.11111111111, np.float64)
|
|
874
|
+
assert_raises(ValueError, diff, x)
|
|
875
|
+
|
|
876
|
+
def test_nd(self):
|
|
877
|
+
x = 20 * rand(10, 20, 30)
|
|
878
|
+
out1 = x[:, :, 1:] - x[:, :, :-1]
|
|
879
|
+
out2 = out1[:, :, 1:] - out1[:, :, :-1]
|
|
880
|
+
out3 = x[1:, :, :] - x[:-1, :, :]
|
|
881
|
+
out4 = out3[1:, :, :] - out3[:-1, :, :]
|
|
882
|
+
assert_array_equal(diff(x), out1)
|
|
883
|
+
assert_array_equal(diff(x, n=2), out2)
|
|
884
|
+
assert_array_equal(diff(x, axis=0), out3)
|
|
885
|
+
assert_array_equal(diff(x, n=2, axis=0), out4)
|
|
886
|
+
|
|
887
|
+
def test_n(self):
|
|
888
|
+
x = list(range(3))
|
|
889
|
+
assert_raises(ValueError, diff, x, n=-1)
|
|
890
|
+
output = [diff(x, n=n) for n in range(1, 5)]
|
|
891
|
+
expected = [[1, 1], [0], [], []]
|
|
892
|
+
assert_(diff(x, n=0) is x)
|
|
893
|
+
for n, (expected_n, output_n) in enumerate(zip(expected, output), start=1):
|
|
894
|
+
assert_(type(output_n) is np.ndarray)
|
|
895
|
+
assert_array_equal(output_n, expected_n)
|
|
896
|
+
assert_equal(output_n.dtype, np.int_)
|
|
897
|
+
assert_equal(len(output_n), max(0, len(x) - n))
|
|
898
|
+
|
|
899
|
+
def test_times(self):
|
|
900
|
+
x = np.arange('1066-10-13', '1066-10-16', dtype=np.datetime64)
|
|
901
|
+
expected = [
|
|
902
|
+
np.array([1, 1], dtype='timedelta64[D]'),
|
|
903
|
+
np.array([0], dtype='timedelta64[D]'),
|
|
904
|
+
]
|
|
905
|
+
expected.extend([np.array([], dtype='timedelta64[D]')] * 3)
|
|
906
|
+
for n, exp in enumerate(expected, start=1):
|
|
907
|
+
out = diff(x, n=n)
|
|
908
|
+
assert_array_equal(out, exp)
|
|
909
|
+
assert_equal(out.dtype, exp.dtype)
|
|
910
|
+
|
|
911
|
+
def test_subclass(self):
|
|
912
|
+
x = ma.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]],
|
|
913
|
+
mask=[[False, False], [True, False],
|
|
914
|
+
[False, True], [True, True], [False, False]])
|
|
915
|
+
out = diff(x)
|
|
916
|
+
assert_array_equal(out.data, [[1], [1], [1], [1], [1]])
|
|
917
|
+
assert_array_equal(out.mask, [[False], [True],
|
|
918
|
+
[True], [True], [False]])
|
|
919
|
+
assert_(type(out) is type(x))
|
|
920
|
+
|
|
921
|
+
out3 = diff(x, n=3)
|
|
922
|
+
assert_array_equal(out3.data, [[], [], [], [], []])
|
|
923
|
+
assert_array_equal(out3.mask, [[], [], [], [], []])
|
|
924
|
+
assert_(type(out3) is type(x))
|
|
925
|
+
|
|
926
|
+
def test_prepend(self):
|
|
927
|
+
x = np.arange(5) + 1
|
|
928
|
+
assert_array_equal(diff(x, prepend=0), np.ones(5))
|
|
929
|
+
assert_array_equal(diff(x, prepend=[0]), np.ones(5))
|
|
930
|
+
assert_array_equal(np.cumsum(np.diff(x, prepend=0)), x)
|
|
931
|
+
assert_array_equal(diff(x, prepend=[-1, 0]), np.ones(6))
|
|
932
|
+
|
|
933
|
+
x = np.arange(4).reshape(2, 2)
|
|
934
|
+
result = np.diff(x, axis=1, prepend=0)
|
|
935
|
+
expected = [[0, 1], [2, 1]]
|
|
936
|
+
assert_array_equal(result, expected)
|
|
937
|
+
result = np.diff(x, axis=1, prepend=[[0], [0]])
|
|
938
|
+
assert_array_equal(result, expected)
|
|
939
|
+
|
|
940
|
+
result = np.diff(x, axis=0, prepend=0)
|
|
941
|
+
expected = [[0, 1], [2, 2]]
|
|
942
|
+
assert_array_equal(result, expected)
|
|
943
|
+
result = np.diff(x, axis=0, prepend=[[0, 0]])
|
|
944
|
+
assert_array_equal(result, expected)
|
|
945
|
+
|
|
946
|
+
assert_raises(ValueError, np.diff, x, prepend=np.zeros((3, 3)))
|
|
947
|
+
|
|
948
|
+
assert_raises(AxisError, diff, x, prepend=0, axis=3)
|
|
949
|
+
|
|
950
|
+
def test_append(self):
|
|
951
|
+
x = np.arange(5)
|
|
952
|
+
result = diff(x, append=0)
|
|
953
|
+
expected = [1, 1, 1, 1, -4]
|
|
954
|
+
assert_array_equal(result, expected)
|
|
955
|
+
result = diff(x, append=[0])
|
|
956
|
+
assert_array_equal(result, expected)
|
|
957
|
+
result = diff(x, append=[0, 2])
|
|
958
|
+
expected = expected + [2]
|
|
959
|
+
assert_array_equal(result, expected)
|
|
960
|
+
|
|
961
|
+
x = np.arange(4).reshape(2, 2)
|
|
962
|
+
result = np.diff(x, axis=1, append=0)
|
|
963
|
+
expected = [[1, -1], [1, -3]]
|
|
964
|
+
assert_array_equal(result, expected)
|
|
965
|
+
result = np.diff(x, axis=1, append=[[0], [0]])
|
|
966
|
+
assert_array_equal(result, expected)
|
|
967
|
+
|
|
968
|
+
result = np.diff(x, axis=0, append=0)
|
|
969
|
+
expected = [[2, 2], [-2, -3]]
|
|
970
|
+
assert_array_equal(result, expected)
|
|
971
|
+
result = np.diff(x, axis=0, append=[[0, 0]])
|
|
972
|
+
assert_array_equal(result, expected)
|
|
973
|
+
|
|
974
|
+
assert_raises(ValueError, np.diff, x, append=np.zeros((3, 3)))
|
|
975
|
+
|
|
976
|
+
assert_raises(AxisError, diff, x, append=0, axis=3)
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
class TestDelete:
|
|
980
|
+
|
|
981
|
+
def _create_arrays(self):
|
|
982
|
+
a = np.arange(5)
|
|
983
|
+
nd_a = np.arange(5).repeat(2).reshape(1, 5, 2)
|
|
984
|
+
return a, nd_a
|
|
985
|
+
|
|
986
|
+
def _check_inverse_of_slicing(self, indices):
|
|
987
|
+
a, nd_a = self._create_arrays()
|
|
988
|
+
a_del = delete(a, indices)
|
|
989
|
+
nd_a_del = delete(nd_a, indices, axis=1)
|
|
990
|
+
msg = f'Delete failed for obj: {indices!r}'
|
|
991
|
+
assert_array_equal(setxor1d(a_del, a[indices, ]), a,
|
|
992
|
+
err_msg=msg)
|
|
993
|
+
xor = setxor1d(nd_a_del[0, :, 0], nd_a[0, indices, 0])
|
|
994
|
+
assert_array_equal(xor, nd_a[0, :, 0], err_msg=msg)
|
|
995
|
+
|
|
996
|
+
def test_slices(self):
|
|
997
|
+
lims = [-6, -2, 0, 1, 2, 4, 5]
|
|
998
|
+
steps = [-3, -1, 1, 3]
|
|
999
|
+
for start in lims:
|
|
1000
|
+
for stop in lims:
|
|
1001
|
+
for step in steps:
|
|
1002
|
+
s = slice(start, stop, step)
|
|
1003
|
+
self._check_inverse_of_slicing(s)
|
|
1004
|
+
|
|
1005
|
+
def test_fancy(self):
|
|
1006
|
+
a, _ = self._create_arrays()
|
|
1007
|
+
self._check_inverse_of_slicing(np.array([[0, 1], [2, 1]]))
|
|
1008
|
+
with pytest.raises(IndexError):
|
|
1009
|
+
delete(a, [100])
|
|
1010
|
+
with pytest.raises(IndexError):
|
|
1011
|
+
delete(a, [-100])
|
|
1012
|
+
|
|
1013
|
+
self._check_inverse_of_slicing([0, -1, 2, 2])
|
|
1014
|
+
|
|
1015
|
+
self._check_inverse_of_slicing([True, False, False, True, False])
|
|
1016
|
+
|
|
1017
|
+
# not legal, indexing with these would change the dimension
|
|
1018
|
+
with pytest.raises(ValueError):
|
|
1019
|
+
delete(a, True)
|
|
1020
|
+
with pytest.raises(ValueError):
|
|
1021
|
+
delete(a, False)
|
|
1022
|
+
|
|
1023
|
+
# not enough items
|
|
1024
|
+
with pytest.raises(ValueError):
|
|
1025
|
+
delete(a, [False] * 4)
|
|
1026
|
+
|
|
1027
|
+
def test_single(self):
|
|
1028
|
+
self._check_inverse_of_slicing(0)
|
|
1029
|
+
self._check_inverse_of_slicing(-4)
|
|
1030
|
+
|
|
1031
|
+
def test_0d(self):
|
|
1032
|
+
a = np.array(1)
|
|
1033
|
+
with pytest.raises(AxisError):
|
|
1034
|
+
delete(a, [], axis=0)
|
|
1035
|
+
with pytest.raises(TypeError):
|
|
1036
|
+
delete(a, [], axis="nonsense")
|
|
1037
|
+
|
|
1038
|
+
def test_subclass(self):
|
|
1039
|
+
class SubClass(np.ndarray):
|
|
1040
|
+
pass
|
|
1041
|
+
|
|
1042
|
+
a_orig, _ = self._create_arrays()
|
|
1043
|
+
a = a_orig.view(SubClass)
|
|
1044
|
+
assert_(isinstance(delete(a, 0), SubClass))
|
|
1045
|
+
assert_(isinstance(delete(a, []), SubClass))
|
|
1046
|
+
assert_(isinstance(delete(a, [0, 1]), SubClass))
|
|
1047
|
+
assert_(isinstance(delete(a, slice(1, 2)), SubClass))
|
|
1048
|
+
assert_(isinstance(delete(a, slice(1, -2)), SubClass))
|
|
1049
|
+
|
|
1050
|
+
def test_array_order_preserve(self):
|
|
1051
|
+
# See gh-7113
|
|
1052
|
+
k = np.arange(10).reshape(2, 5, order='F')
|
|
1053
|
+
m = delete(k, slice(60, None), axis=1)
|
|
1054
|
+
|
|
1055
|
+
# 'k' is Fortran ordered, and 'm' should have the
|
|
1056
|
+
# same ordering as 'k' and NOT become C ordered
|
|
1057
|
+
assert_equal(m.flags.c_contiguous, k.flags.c_contiguous)
|
|
1058
|
+
assert_equal(m.flags.f_contiguous, k.flags.f_contiguous)
|
|
1059
|
+
|
|
1060
|
+
def test_index_floats(self):
|
|
1061
|
+
with pytest.raises(IndexError):
|
|
1062
|
+
np.delete([0, 1, 2], np.array([1.0, 2.0]))
|
|
1063
|
+
with pytest.raises(IndexError):
|
|
1064
|
+
np.delete([0, 1, 2], np.array([], dtype=float))
|
|
1065
|
+
|
|
1066
|
+
@pytest.mark.parametrize("indexer", [np.array([1]), [1]])
|
|
1067
|
+
def test_single_item_array(self, indexer):
|
|
1068
|
+
a, nd_a = self._create_arrays()
|
|
1069
|
+
a_del_int = delete(a, 1)
|
|
1070
|
+
a_del = delete(a, indexer)
|
|
1071
|
+
assert_equal(a_del_int, a_del)
|
|
1072
|
+
|
|
1073
|
+
nd_a_del_int = delete(nd_a, 1, axis=1)
|
|
1074
|
+
nd_a_del = delete(nd_a, np.array([1]), axis=1)
|
|
1075
|
+
assert_equal(nd_a_del_int, nd_a_del)
|
|
1076
|
+
|
|
1077
|
+
def test_single_item_array_non_int(self):
|
|
1078
|
+
# Special handling for integer arrays must not affect non-integer ones.
|
|
1079
|
+
# If `False` was cast to `0` it would delete the element:
|
|
1080
|
+
res = delete(np.ones(1), np.array([False]))
|
|
1081
|
+
assert_array_equal(res, np.ones(1))
|
|
1082
|
+
|
|
1083
|
+
# Test the more complicated (with axis) case from gh-21840
|
|
1084
|
+
x = np.ones((3, 1))
|
|
1085
|
+
false_mask = np.array([False], dtype=bool)
|
|
1086
|
+
true_mask = np.array([True], dtype=bool)
|
|
1087
|
+
|
|
1088
|
+
res = delete(x, false_mask, axis=-1)
|
|
1089
|
+
assert_array_equal(res, x)
|
|
1090
|
+
res = delete(x, true_mask, axis=-1)
|
|
1091
|
+
assert_array_equal(res, x[:, :0])
|
|
1092
|
+
|
|
1093
|
+
# Object or e.g. timedeltas should *not* be allowed
|
|
1094
|
+
with pytest.raises(IndexError):
|
|
1095
|
+
delete(np.ones(2), np.array([0], dtype=object))
|
|
1096
|
+
|
|
1097
|
+
with pytest.raises(IndexError):
|
|
1098
|
+
# timedeltas are sometimes "integral, but clearly not allowed:
|
|
1099
|
+
delete(np.ones(2), np.array([0], dtype="m8[ns]"))
|
|
1100
|
+
|
|
1101
|
+
|
|
1102
|
+
class TestGradient:
|
|
1103
|
+
|
|
1104
|
+
def test_basic(self):
|
|
1105
|
+
v = [[1, 1], [3, 4]]
|
|
1106
|
+
x = np.array(v)
|
|
1107
|
+
dx = [np.array([[2., 3.], [2., 3.]]),
|
|
1108
|
+
np.array([[0., 0.], [1., 1.]])]
|
|
1109
|
+
assert_array_equal(gradient(x), dx)
|
|
1110
|
+
assert_array_equal(gradient(v), dx)
|
|
1111
|
+
|
|
1112
|
+
def test_args(self):
|
|
1113
|
+
dx = np.cumsum(np.ones(5))
|
|
1114
|
+
dx_uneven = [1., 2., 5., 9., 11.]
|
|
1115
|
+
f_2d = np.arange(25).reshape(5, 5)
|
|
1116
|
+
|
|
1117
|
+
# distances must be scalars or have size equal to gradient[axis]
|
|
1118
|
+
gradient(np.arange(5), 3.)
|
|
1119
|
+
gradient(np.arange(5), np.array(3.))
|
|
1120
|
+
gradient(np.arange(5), dx)
|
|
1121
|
+
# dy is set equal to dx because scalar
|
|
1122
|
+
gradient(f_2d, 1.5)
|
|
1123
|
+
gradient(f_2d, np.array(1.5))
|
|
1124
|
+
|
|
1125
|
+
gradient(f_2d, dx_uneven, dx_uneven)
|
|
1126
|
+
# mix between even and uneven spaces and
|
|
1127
|
+
# mix between scalar and vector
|
|
1128
|
+
gradient(f_2d, dx, 2)
|
|
1129
|
+
|
|
1130
|
+
# 2D but axis specified
|
|
1131
|
+
gradient(f_2d, dx, axis=1)
|
|
1132
|
+
|
|
1133
|
+
# 2d coordinate arguments are not yet allowed
|
|
1134
|
+
assert_raises_regex(ValueError, '.*scalars or 1d',
|
|
1135
|
+
gradient, f_2d, np.stack([dx] * 2, axis=-1), 1)
|
|
1136
|
+
|
|
1137
|
+
def test_badargs(self):
|
|
1138
|
+
f_2d = np.arange(25).reshape(5, 5)
|
|
1139
|
+
x = np.cumsum(np.ones(5))
|
|
1140
|
+
|
|
1141
|
+
# wrong sizes
|
|
1142
|
+
assert_raises(ValueError, gradient, f_2d, x, np.ones(2))
|
|
1143
|
+
assert_raises(ValueError, gradient, f_2d, 1, np.ones(2))
|
|
1144
|
+
assert_raises(ValueError, gradient, f_2d, np.ones(2), np.ones(2))
|
|
1145
|
+
# wrong number of arguments
|
|
1146
|
+
assert_raises(TypeError, gradient, f_2d, x)
|
|
1147
|
+
assert_raises(TypeError, gradient, f_2d, x, axis=(0, 1))
|
|
1148
|
+
assert_raises(TypeError, gradient, f_2d, x, x, x)
|
|
1149
|
+
assert_raises(TypeError, gradient, f_2d, 1, 1, 1)
|
|
1150
|
+
assert_raises(TypeError, gradient, f_2d, x, x, axis=1)
|
|
1151
|
+
assert_raises(TypeError, gradient, f_2d, 1, 1, axis=1)
|
|
1152
|
+
|
|
1153
|
+
def test_datetime64(self):
|
|
1154
|
+
# Make sure gradient() can handle special types like datetime64
|
|
1155
|
+
x = np.array(
|
|
1156
|
+
['1910-08-16', '1910-08-11', '1910-08-10', '1910-08-12',
|
|
1157
|
+
'1910-10-12', '1910-12-12', '1912-12-12'],
|
|
1158
|
+
dtype='datetime64[D]')
|
|
1159
|
+
dx = np.array(
|
|
1160
|
+
[-5, -3, 0, 31, 61, 396, 731],
|
|
1161
|
+
dtype='timedelta64[D]')
|
|
1162
|
+
assert_array_equal(gradient(x), dx)
|
|
1163
|
+
assert_(dx.dtype == np.dtype('timedelta64[D]'))
|
|
1164
|
+
|
|
1165
|
+
def test_masked(self):
|
|
1166
|
+
# Make sure that gradient supports subclasses like masked arrays
|
|
1167
|
+
x = np.ma.array([[1, 1], [3, 4]],
|
|
1168
|
+
mask=[[False, False], [False, False]])
|
|
1169
|
+
out = gradient(x)[0]
|
|
1170
|
+
assert_equal(type(out), type(x))
|
|
1171
|
+
# And make sure that the output and input don't have aliased mask
|
|
1172
|
+
# arrays
|
|
1173
|
+
assert_(x._mask is not out._mask)
|
|
1174
|
+
# Also check that edge_order=2 doesn't alter the original mask
|
|
1175
|
+
x2 = np.ma.arange(5)
|
|
1176
|
+
x2[2] = np.ma.masked
|
|
1177
|
+
np.gradient(x2, edge_order=2)
|
|
1178
|
+
assert_array_equal(x2.mask, [False, False, True, False, False])
|
|
1179
|
+
|
|
1180
|
+
def test_second_order_accurate(self):
|
|
1181
|
+
# Testing that the relative numerical error is less that 3% for
|
|
1182
|
+
# this example problem. This corresponds to second order
|
|
1183
|
+
# accurate finite differences for all interior and boundary
|
|
1184
|
+
# points.
|
|
1185
|
+
x = np.linspace(0, 1, 10)
|
|
1186
|
+
dx = x[1] - x[0]
|
|
1187
|
+
y = 2 * x ** 3 + 4 * x ** 2 + 2 * x
|
|
1188
|
+
analytical = 6 * x ** 2 + 8 * x + 2
|
|
1189
|
+
num_error = np.abs((np.gradient(y, dx, edge_order=2) / analytical) - 1)
|
|
1190
|
+
assert_(np.all(num_error < 0.03) == True)
|
|
1191
|
+
|
|
1192
|
+
# test with unevenly spaced
|
|
1193
|
+
rng = np.random.default_rng(0)
|
|
1194
|
+
x = np.sort(rng.random(10))
|
|
1195
|
+
y = 2 * x ** 3 + 4 * x ** 2 + 2 * x
|
|
1196
|
+
analytical = 6 * x ** 2 + 8 * x + 2
|
|
1197
|
+
num_error = np.abs((np.gradient(y, x, edge_order=2) / analytical) - 1)
|
|
1198
|
+
assert_(np.all(num_error < 0.03) == True)
|
|
1199
|
+
|
|
1200
|
+
def test_spacing(self):
|
|
1201
|
+
f = np.array([0, 2., 3., 4., 5., 5.])
|
|
1202
|
+
f = np.tile(f, (6, 1)) + f.reshape(-1, 1)
|
|
1203
|
+
x_uneven = np.array([0., 0.5, 1., 3., 5., 7.])
|
|
1204
|
+
x_even = np.arange(6.)
|
|
1205
|
+
|
|
1206
|
+
fdx_even_ord1 = np.tile([2., 1.5, 1., 1., 0.5, 0.], (6, 1))
|
|
1207
|
+
fdx_even_ord2 = np.tile([2.5, 1.5, 1., 1., 0.5, -0.5], (6, 1))
|
|
1208
|
+
fdx_uneven_ord1 = np.tile([4., 3., 1.7, 0.5, 0.25, 0.], (6, 1))
|
|
1209
|
+
fdx_uneven_ord2 = np.tile([5., 3., 1.7, 0.5, 0.25, -0.25], (6, 1))
|
|
1210
|
+
|
|
1211
|
+
# evenly spaced
|
|
1212
|
+
for edge_order, exp_res in [(1, fdx_even_ord1), (2, fdx_even_ord2)]:
|
|
1213
|
+
res1 = gradient(f, 1., axis=(0, 1), edge_order=edge_order)
|
|
1214
|
+
res2 = gradient(f, x_even, x_even,
|
|
1215
|
+
axis=(0, 1), edge_order=edge_order)
|
|
1216
|
+
res3 = gradient(f, x_even, x_even,
|
|
1217
|
+
axis=None, edge_order=edge_order)
|
|
1218
|
+
assert_array_equal(res1, res2)
|
|
1219
|
+
assert_array_equal(res2, res3)
|
|
1220
|
+
assert_almost_equal(res1[0], exp_res.T)
|
|
1221
|
+
assert_almost_equal(res1[1], exp_res)
|
|
1222
|
+
|
|
1223
|
+
res1 = gradient(f, 1., axis=0, edge_order=edge_order)
|
|
1224
|
+
res2 = gradient(f, x_even, axis=0, edge_order=edge_order)
|
|
1225
|
+
assert_(res1.shape == res2.shape)
|
|
1226
|
+
assert_almost_equal(res2, exp_res.T)
|
|
1227
|
+
|
|
1228
|
+
res1 = gradient(f, 1., axis=1, edge_order=edge_order)
|
|
1229
|
+
res2 = gradient(f, x_even, axis=1, edge_order=edge_order)
|
|
1230
|
+
assert_(res1.shape == res2.shape)
|
|
1231
|
+
assert_array_equal(res2, exp_res)
|
|
1232
|
+
|
|
1233
|
+
# unevenly spaced
|
|
1234
|
+
for edge_order, exp_res in [(1, fdx_uneven_ord1), (2, fdx_uneven_ord2)]:
|
|
1235
|
+
res1 = gradient(f, x_uneven, x_uneven,
|
|
1236
|
+
axis=(0, 1), edge_order=edge_order)
|
|
1237
|
+
res2 = gradient(f, x_uneven, x_uneven,
|
|
1238
|
+
axis=None, edge_order=edge_order)
|
|
1239
|
+
assert_array_equal(res1, res2)
|
|
1240
|
+
assert_almost_equal(res1[0], exp_res.T)
|
|
1241
|
+
assert_almost_equal(res1[1], exp_res)
|
|
1242
|
+
|
|
1243
|
+
res1 = gradient(f, x_uneven, axis=0, edge_order=edge_order)
|
|
1244
|
+
assert_almost_equal(res1, exp_res.T)
|
|
1245
|
+
|
|
1246
|
+
res1 = gradient(f, x_uneven, axis=1, edge_order=edge_order)
|
|
1247
|
+
assert_almost_equal(res1, exp_res)
|
|
1248
|
+
|
|
1249
|
+
# mixed
|
|
1250
|
+
res1 = gradient(f, x_even, x_uneven, axis=(0, 1), edge_order=1)
|
|
1251
|
+
res2 = gradient(f, x_uneven, x_even, axis=(1, 0), edge_order=1)
|
|
1252
|
+
assert_array_equal(res1[0], res2[1])
|
|
1253
|
+
assert_array_equal(res1[1], res2[0])
|
|
1254
|
+
assert_almost_equal(res1[0], fdx_even_ord1.T)
|
|
1255
|
+
assert_almost_equal(res1[1], fdx_uneven_ord1)
|
|
1256
|
+
|
|
1257
|
+
res1 = gradient(f, x_even, x_uneven, axis=(0, 1), edge_order=2)
|
|
1258
|
+
res2 = gradient(f, x_uneven, x_even, axis=(1, 0), edge_order=2)
|
|
1259
|
+
assert_array_equal(res1[0], res2[1])
|
|
1260
|
+
assert_array_equal(res1[1], res2[0])
|
|
1261
|
+
assert_almost_equal(res1[0], fdx_even_ord2.T)
|
|
1262
|
+
assert_almost_equal(res1[1], fdx_uneven_ord2)
|
|
1263
|
+
|
|
1264
|
+
def test_specific_axes(self):
|
|
1265
|
+
# Testing that gradient can work on a given axis only
|
|
1266
|
+
v = [[1, 1], [3, 4]]
|
|
1267
|
+
x = np.array(v)
|
|
1268
|
+
dx = [np.array([[2., 3.], [2., 3.]]),
|
|
1269
|
+
np.array([[0., 0.], [1., 1.]])]
|
|
1270
|
+
assert_array_equal(gradient(x, axis=0), dx[0])
|
|
1271
|
+
assert_array_equal(gradient(x, axis=1), dx[1])
|
|
1272
|
+
assert_array_equal(gradient(x, axis=-1), dx[1])
|
|
1273
|
+
assert_array_equal(gradient(x, axis=(1, 0)), [dx[1], dx[0]])
|
|
1274
|
+
|
|
1275
|
+
# test axis=None which means all axes
|
|
1276
|
+
assert_almost_equal(gradient(x, axis=None), [dx[0], dx[1]])
|
|
1277
|
+
# and is the same as no axis keyword given
|
|
1278
|
+
assert_almost_equal(gradient(x, axis=None), gradient(x))
|
|
1279
|
+
|
|
1280
|
+
# test vararg order
|
|
1281
|
+
assert_array_equal(gradient(x, 2, 3, axis=(1, 0)),
|
|
1282
|
+
[dx[1] / 2.0, dx[0] / 3.0])
|
|
1283
|
+
# test maximal number of varargs
|
|
1284
|
+
assert_raises(TypeError, gradient, x, 1, 2, axis=1)
|
|
1285
|
+
|
|
1286
|
+
assert_raises(AxisError, gradient, x, axis=3)
|
|
1287
|
+
assert_raises(AxisError, gradient, x, axis=-3)
|
|
1288
|
+
# assert_raises(TypeError, gradient, x, axis=[1,])
|
|
1289
|
+
|
|
1290
|
+
def test_timedelta64(self):
|
|
1291
|
+
# Make sure gradient() can handle special types like timedelta64
|
|
1292
|
+
x = np.array(
|
|
1293
|
+
[-5, -3, 10, 12, 61, 321, 300],
|
|
1294
|
+
dtype='timedelta64[D]')
|
|
1295
|
+
dx = np.array(
|
|
1296
|
+
[2, 7, 7, 25, 154, 119, -21],
|
|
1297
|
+
dtype='timedelta64[D]')
|
|
1298
|
+
assert_array_equal(gradient(x), dx)
|
|
1299
|
+
assert_(dx.dtype == np.dtype('timedelta64[D]'))
|
|
1300
|
+
|
|
1301
|
+
def test_inexact_dtypes(self):
|
|
1302
|
+
for dt in [np.float16, np.float32, np.float64]:
|
|
1303
|
+
# dtypes should not be promoted in a different way to what diff does
|
|
1304
|
+
x = np.array([1, 2, 3], dtype=dt)
|
|
1305
|
+
assert_equal(gradient(x).dtype, np.diff(x).dtype)
|
|
1306
|
+
|
|
1307
|
+
def test_values(self):
|
|
1308
|
+
# needs at least 2 points for edge_order ==1
|
|
1309
|
+
gradient(np.arange(2), edge_order=1)
|
|
1310
|
+
# needs at least 3 points for edge_order ==1
|
|
1311
|
+
gradient(np.arange(3), edge_order=2)
|
|
1312
|
+
|
|
1313
|
+
assert_raises(ValueError, gradient, np.arange(0), edge_order=1)
|
|
1314
|
+
assert_raises(ValueError, gradient, np.arange(0), edge_order=2)
|
|
1315
|
+
assert_raises(ValueError, gradient, np.arange(1), edge_order=1)
|
|
1316
|
+
assert_raises(ValueError, gradient, np.arange(1), edge_order=2)
|
|
1317
|
+
assert_raises(ValueError, gradient, np.arange(2), edge_order=2)
|
|
1318
|
+
|
|
1319
|
+
@pytest.mark.parametrize('f_dtype', [np.uint8, np.uint16,
|
|
1320
|
+
np.uint32, np.uint64])
|
|
1321
|
+
def test_f_decreasing_unsigned_int(self, f_dtype):
|
|
1322
|
+
f = np.array([5, 4, 3, 2, 1], dtype=f_dtype)
|
|
1323
|
+
g = gradient(f)
|
|
1324
|
+
assert_array_equal(g, [-1] * len(f))
|
|
1325
|
+
|
|
1326
|
+
@pytest.mark.parametrize('f_dtype', [np.int8, np.int16,
|
|
1327
|
+
np.int32, np.int64])
|
|
1328
|
+
def test_f_signed_int_big_jump(self, f_dtype):
|
|
1329
|
+
maxint = np.iinfo(f_dtype).max
|
|
1330
|
+
x = np.array([1, 3])
|
|
1331
|
+
f = np.array([-1, maxint], dtype=f_dtype)
|
|
1332
|
+
dfdx = gradient(f, x)
|
|
1333
|
+
assert_array_equal(dfdx, [(maxint + 1) // 2] * 2)
|
|
1334
|
+
|
|
1335
|
+
@pytest.mark.parametrize('x_dtype', [np.uint8, np.uint16,
|
|
1336
|
+
np.uint32, np.uint64])
|
|
1337
|
+
def test_x_decreasing_unsigned(self, x_dtype):
|
|
1338
|
+
x = np.array([3, 2, 1], dtype=x_dtype)
|
|
1339
|
+
f = np.array([0, 2, 4])
|
|
1340
|
+
dfdx = gradient(f, x)
|
|
1341
|
+
assert_array_equal(dfdx, [-2] * len(x))
|
|
1342
|
+
|
|
1343
|
+
@pytest.mark.parametrize('x_dtype', [np.int8, np.int16,
|
|
1344
|
+
np.int32, np.int64])
|
|
1345
|
+
def test_x_signed_int_big_jump(self, x_dtype):
|
|
1346
|
+
minint = np.iinfo(x_dtype).min
|
|
1347
|
+
maxint = np.iinfo(x_dtype).max
|
|
1348
|
+
x = np.array([-1, maxint], dtype=x_dtype)
|
|
1349
|
+
f = np.array([minint // 2, 0])
|
|
1350
|
+
dfdx = gradient(f, x)
|
|
1351
|
+
assert_array_equal(dfdx, [0.5, 0.5])
|
|
1352
|
+
|
|
1353
|
+
def test_return_type(self):
|
|
1354
|
+
res = np.gradient(([1, 2], [2, 3]))
|
|
1355
|
+
assert type(res) is tuple
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
class TestAngle:
|
|
1359
|
+
|
|
1360
|
+
def test_basic(self):
|
|
1361
|
+
x = [1 + 3j, np.sqrt(2) / 2.0 + 1j * np.sqrt(2) / 2,
|
|
1362
|
+
1, 1j, -1, -1j, 1 - 3j, -1 + 3j]
|
|
1363
|
+
y = angle(x)
|
|
1364
|
+
yo = [
|
|
1365
|
+
np.arctan(3.0 / 1.0),
|
|
1366
|
+
np.arctan(1.0), 0, np.pi / 2, np.pi, -np.pi / 2.0,
|
|
1367
|
+
-np.arctan(3.0 / 1.0), np.pi - np.arctan(3.0 / 1.0)]
|
|
1368
|
+
z = angle(x, deg=True)
|
|
1369
|
+
zo = np.array(yo) * 180 / np.pi
|
|
1370
|
+
assert_array_almost_equal(y, yo, 11)
|
|
1371
|
+
assert_array_almost_equal(z, zo, 11)
|
|
1372
|
+
|
|
1373
|
+
def test_subclass(self):
|
|
1374
|
+
x = np.ma.array([1 + 3j, 1, np.sqrt(2) / 2 * (1 + 1j)])
|
|
1375
|
+
x[1] = np.ma.masked
|
|
1376
|
+
expected = np.ma.array([np.arctan(3.0 / 1.0), 0, np.arctan(1.0)])
|
|
1377
|
+
expected[1] = np.ma.masked
|
|
1378
|
+
actual = angle(x)
|
|
1379
|
+
assert_equal(type(actual), type(expected))
|
|
1380
|
+
assert_equal(actual.mask, expected.mask)
|
|
1381
|
+
assert_equal(actual, expected)
|
|
1382
|
+
|
|
1383
|
+
|
|
1384
|
+
class TestTrimZeros:
|
|
1385
|
+
|
|
1386
|
+
a = np.array([0, 0, 1, 0, 2, 3, 4, 0])
|
|
1387
|
+
b = a.astype(float)
|
|
1388
|
+
c = a.astype(complex)
|
|
1389
|
+
d = a.astype(object)
|
|
1390
|
+
|
|
1391
|
+
def construct_input_output(self, rng, shape, axis, trim):
|
|
1392
|
+
"""Construct an input/output test pair for trim_zeros"""
|
|
1393
|
+
# Standardize axis to a tuple.
|
|
1394
|
+
if axis is None:
|
|
1395
|
+
axis = tuple(range(len(shape)))
|
|
1396
|
+
elif isinstance(axis, int):
|
|
1397
|
+
axis = (len(shape) + axis if axis < 0 else axis,)
|
|
1398
|
+
else:
|
|
1399
|
+
axis = tuple(len(shape) + ax if ax < 0 else ax for ax in axis)
|
|
1400
|
+
|
|
1401
|
+
# Populate a random interior slice with nonzero entries.
|
|
1402
|
+
data = np.zeros(shape)
|
|
1403
|
+
i_start = rng.integers(low=0, high=np.array(shape) - 1)
|
|
1404
|
+
i_end = rng.integers(low=i_start + 1, high=shape)
|
|
1405
|
+
inner_shape = tuple(i_end - i_start)
|
|
1406
|
+
inner_data = 1 + rng.random(inner_shape)
|
|
1407
|
+
data[tuple(slice(i, j) for i, j in zip(i_start, i_end))] = inner_data
|
|
1408
|
+
|
|
1409
|
+
# Construct the expected output of N-dimensional trim_zeros
|
|
1410
|
+
# with the given axis and trim arguments.
|
|
1411
|
+
if 'f' not in trim:
|
|
1412
|
+
i_start = np.array([None for _ in shape])
|
|
1413
|
+
if 'b' not in trim:
|
|
1414
|
+
i_end = np.array([None for _ in shape])
|
|
1415
|
+
idx = tuple(slice(i, j) if ax in axis else slice(None)
|
|
1416
|
+
for ax, (i, j) in enumerate(zip(i_start, i_end)))
|
|
1417
|
+
expected = data[idx]
|
|
1418
|
+
|
|
1419
|
+
return data, expected
|
|
1420
|
+
|
|
1421
|
+
def values(self):
|
|
1422
|
+
attr_names = ('a', 'b', 'c', 'd')
|
|
1423
|
+
return (getattr(self, name) for name in attr_names)
|
|
1424
|
+
|
|
1425
|
+
def test_basic(self):
|
|
1426
|
+
slc = np.s_[2:-1]
|
|
1427
|
+
for arr in self.values():
|
|
1428
|
+
res = trim_zeros(arr)
|
|
1429
|
+
assert_array_equal(res, arr[slc])
|
|
1430
|
+
|
|
1431
|
+
def test_leading_skip(self):
|
|
1432
|
+
slc = np.s_[:-1]
|
|
1433
|
+
for arr in self.values():
|
|
1434
|
+
res = trim_zeros(arr, trim='b')
|
|
1435
|
+
assert_array_equal(res, arr[slc])
|
|
1436
|
+
|
|
1437
|
+
def test_trailing_skip(self):
|
|
1438
|
+
slc = np.s_[2:]
|
|
1439
|
+
for arr in self.values():
|
|
1440
|
+
res = trim_zeros(arr, trim='F')
|
|
1441
|
+
assert_array_equal(res, arr[slc])
|
|
1442
|
+
|
|
1443
|
+
def test_all_zero(self):
|
|
1444
|
+
for _arr in self.values():
|
|
1445
|
+
arr = np.zeros_like(_arr, dtype=_arr.dtype)
|
|
1446
|
+
|
|
1447
|
+
res1 = trim_zeros(arr, trim='B')
|
|
1448
|
+
assert len(res1) == 0
|
|
1449
|
+
|
|
1450
|
+
res2 = trim_zeros(arr, trim='f')
|
|
1451
|
+
assert len(res2) == 0
|
|
1452
|
+
|
|
1453
|
+
def test_size_zero(self):
|
|
1454
|
+
arr = np.zeros(0)
|
|
1455
|
+
res = trim_zeros(arr)
|
|
1456
|
+
assert_array_equal(arr, res)
|
|
1457
|
+
|
|
1458
|
+
@pytest.mark.parametrize(
|
|
1459
|
+
'arr',
|
|
1460
|
+
[np.array([0, 2**62, 0]),
|
|
1461
|
+
np.array([0, 2**63, 0]),
|
|
1462
|
+
np.array([0, 2**64, 0])]
|
|
1463
|
+
)
|
|
1464
|
+
def test_overflow(self, arr):
|
|
1465
|
+
slc = np.s_[1:2]
|
|
1466
|
+
res = trim_zeros(arr)
|
|
1467
|
+
assert_array_equal(res, arr[slc])
|
|
1468
|
+
|
|
1469
|
+
def test_no_trim(self):
|
|
1470
|
+
arr = np.array([None, 1, None])
|
|
1471
|
+
res = trim_zeros(arr)
|
|
1472
|
+
assert_array_equal(arr, res)
|
|
1473
|
+
|
|
1474
|
+
def test_list_to_list(self):
|
|
1475
|
+
res = trim_zeros(self.a.tolist())
|
|
1476
|
+
assert isinstance(res, list)
|
|
1477
|
+
|
|
1478
|
+
@pytest.mark.parametrize("ndim", (0, 1, 2, 3, 10))
|
|
1479
|
+
def test_nd_basic(self, ndim):
|
|
1480
|
+
a = np.ones((2,) * ndim)
|
|
1481
|
+
b = np.pad(a, (2, 1), mode="constant", constant_values=0)
|
|
1482
|
+
res = trim_zeros(b, axis=None)
|
|
1483
|
+
assert_array_equal(a, res)
|
|
1484
|
+
|
|
1485
|
+
@pytest.mark.parametrize("ndim", (0, 1, 2, 3))
|
|
1486
|
+
def test_allzero(self, ndim):
|
|
1487
|
+
a = np.zeros((3,) * ndim)
|
|
1488
|
+
res = trim_zeros(a, axis=None)
|
|
1489
|
+
assert_array_equal(res, np.zeros((0,) * ndim))
|
|
1490
|
+
|
|
1491
|
+
def test_trim_arg(self):
|
|
1492
|
+
a = np.array([0, 1, 2, 0])
|
|
1493
|
+
|
|
1494
|
+
res = trim_zeros(a, trim='f')
|
|
1495
|
+
assert_array_equal(res, [1, 2, 0])
|
|
1496
|
+
|
|
1497
|
+
res = trim_zeros(a, trim='b')
|
|
1498
|
+
assert_array_equal(res, [0, 1, 2])
|
|
1499
|
+
|
|
1500
|
+
@pytest.mark.parametrize("trim", ("front", ""))
|
|
1501
|
+
def test_unexpected_trim_value(self, trim):
|
|
1502
|
+
arr = self.a
|
|
1503
|
+
with pytest.raises(ValueError, match=r"unexpected character\(s\) in `trim`"):
|
|
1504
|
+
trim_zeros(arr, trim=trim)
|
|
1505
|
+
|
|
1506
|
+
@pytest.mark.parametrize("shape, axis", [
|
|
1507
|
+
[(5,), None],
|
|
1508
|
+
[(5,), ()],
|
|
1509
|
+
[(5,), 0],
|
|
1510
|
+
[(5, 6), None],
|
|
1511
|
+
[(5, 6), ()],
|
|
1512
|
+
[(5, 6), 0],
|
|
1513
|
+
[(5, 6), (-1,)],
|
|
1514
|
+
[(5, 6, 7), None],
|
|
1515
|
+
[(5, 6, 7), ()],
|
|
1516
|
+
[(5, 6, 7), 1],
|
|
1517
|
+
[(5, 6, 7), (0, 2)],
|
|
1518
|
+
[(5, 6, 7, 8), None],
|
|
1519
|
+
[(5, 6, 7, 8), ()],
|
|
1520
|
+
[(5, 6, 7, 8), -2],
|
|
1521
|
+
[(5, 6, 7, 8), (0, 1, 3)],
|
|
1522
|
+
])
|
|
1523
|
+
@pytest.mark.parametrize("trim", ['fb', 'f', 'b'])
|
|
1524
|
+
def test_multiple_axes(self, shape, axis, trim):
|
|
1525
|
+
rng = np.random.default_rng(4321)
|
|
1526
|
+
data, expected = self.construct_input_output(rng, shape, axis, trim)
|
|
1527
|
+
assert_array_equal(trim_zeros(data, axis=axis, trim=trim), expected)
|
|
1528
|
+
|
|
1529
|
+
|
|
1530
|
+
class TestExtins:
|
|
1531
|
+
|
|
1532
|
+
def test_basic(self):
|
|
1533
|
+
a = np.array([1, 3, 2, 1, 2, 3, 3])
|
|
1534
|
+
b = extract(a > 1, a)
|
|
1535
|
+
assert_array_equal(b, [3, 2, 2, 3, 3])
|
|
1536
|
+
|
|
1537
|
+
def test_place(self):
|
|
1538
|
+
# Make sure that non-np.ndarray objects
|
|
1539
|
+
# raise an error instead of doing nothing
|
|
1540
|
+
assert_raises(TypeError, place, [1, 2, 3], [True, False], [0, 1])
|
|
1541
|
+
|
|
1542
|
+
a = np.array([1, 4, 3, 2, 5, 8, 7])
|
|
1543
|
+
place(a, [0, 1, 0, 1, 0, 1, 0], [2, 4, 6])
|
|
1544
|
+
assert_array_equal(a, [1, 2, 3, 4, 5, 6, 7])
|
|
1545
|
+
|
|
1546
|
+
place(a, np.zeros(7), [])
|
|
1547
|
+
assert_array_equal(a, np.arange(1, 8))
|
|
1548
|
+
|
|
1549
|
+
place(a, [1, 0, 1, 0, 1, 0, 1], [8, 9])
|
|
1550
|
+
assert_array_equal(a, [8, 2, 9, 4, 8, 6, 9])
|
|
1551
|
+
assert_raises_regex(ValueError, "Cannot insert from an empty array",
|
|
1552
|
+
lambda: place(a, [0, 0, 0, 0, 0, 1, 0], []))
|
|
1553
|
+
|
|
1554
|
+
# See Issue #6974
|
|
1555
|
+
a = np.array(['12', '34'])
|
|
1556
|
+
place(a, [0, 1], '9')
|
|
1557
|
+
assert_array_equal(a, ['12', '9'])
|
|
1558
|
+
|
|
1559
|
+
def test_both(self):
|
|
1560
|
+
a = rand(10)
|
|
1561
|
+
mask = a > 0.5
|
|
1562
|
+
ac = a.copy()
|
|
1563
|
+
c = extract(mask, a)
|
|
1564
|
+
place(a, mask, 0)
|
|
1565
|
+
place(a, mask, c)
|
|
1566
|
+
assert_array_equal(a, ac)
|
|
1567
|
+
|
|
1568
|
+
|
|
1569
|
+
# _foo1 and _foo2 are used in some tests in TestVectorize.
|
|
1570
|
+
|
|
1571
|
+
def _foo1(x, y=1.0):
|
|
1572
|
+
return y * math.floor(x)
|
|
1573
|
+
|
|
1574
|
+
|
|
1575
|
+
def _foo2(x, y=1.0, z=0.0):
|
|
1576
|
+
return y * math.floor(x) + z
|
|
1577
|
+
|
|
1578
|
+
|
|
1579
|
+
class TestVectorize:
|
|
1580
|
+
|
|
1581
|
+
def test_simple(self):
|
|
1582
|
+
def addsubtract(a, b):
|
|
1583
|
+
if a > b:
|
|
1584
|
+
return a - b
|
|
1585
|
+
else:
|
|
1586
|
+
return a + b
|
|
1587
|
+
|
|
1588
|
+
f = vectorize(addsubtract)
|
|
1589
|
+
r = f([0, 3, 6, 9], [1, 3, 5, 7])
|
|
1590
|
+
assert_array_equal(r, [1, 6, 1, 2])
|
|
1591
|
+
|
|
1592
|
+
def test_scalar(self):
|
|
1593
|
+
def addsubtract(a, b):
|
|
1594
|
+
if a > b:
|
|
1595
|
+
return a - b
|
|
1596
|
+
else:
|
|
1597
|
+
return a + b
|
|
1598
|
+
|
|
1599
|
+
f = vectorize(addsubtract)
|
|
1600
|
+
r = f([0, 3, 6, 9], 5)
|
|
1601
|
+
assert_array_equal(r, [5, 8, 1, 4])
|
|
1602
|
+
|
|
1603
|
+
def test_large(self):
|
|
1604
|
+
x = np.linspace(-3, 2, 10000)
|
|
1605
|
+
f = vectorize(lambda x: x)
|
|
1606
|
+
y = f(x)
|
|
1607
|
+
assert_array_equal(y, x)
|
|
1608
|
+
|
|
1609
|
+
def test_ufunc(self):
|
|
1610
|
+
f = vectorize(math.cos)
|
|
1611
|
+
args = np.array([0, 0.5 * np.pi, np.pi, 1.5 * np.pi, 2 * np.pi])
|
|
1612
|
+
r1 = f(args)
|
|
1613
|
+
r2 = np.cos(args)
|
|
1614
|
+
assert_array_almost_equal(r1, r2)
|
|
1615
|
+
|
|
1616
|
+
def test_keywords(self):
|
|
1617
|
+
|
|
1618
|
+
def foo(a, b=1):
|
|
1619
|
+
return a + b
|
|
1620
|
+
|
|
1621
|
+
f = vectorize(foo)
|
|
1622
|
+
args = np.array([1, 2, 3])
|
|
1623
|
+
r1 = f(args)
|
|
1624
|
+
r2 = np.array([2, 3, 4])
|
|
1625
|
+
assert_array_equal(r1, r2)
|
|
1626
|
+
r1 = f(args, 2)
|
|
1627
|
+
r2 = np.array([3, 4, 5])
|
|
1628
|
+
assert_array_equal(r1, r2)
|
|
1629
|
+
|
|
1630
|
+
def test_keywords_with_otypes_order1(self):
|
|
1631
|
+
# gh-1620: The second call of f would crash with
|
|
1632
|
+
# `ValueError: invalid number of arguments`.
|
|
1633
|
+
f = vectorize(_foo1, otypes=[float])
|
|
1634
|
+
# We're testing the caching of ufuncs by vectorize, so the order
|
|
1635
|
+
# of these function calls is an important part of the test.
|
|
1636
|
+
r1 = f(np.arange(3.0), 1.0)
|
|
1637
|
+
r2 = f(np.arange(3.0))
|
|
1638
|
+
assert_array_equal(r1, r2)
|
|
1639
|
+
|
|
1640
|
+
def test_keywords_with_otypes_order2(self):
|
|
1641
|
+
# gh-1620: The second call of f would crash with
|
|
1642
|
+
# `ValueError: non-broadcastable output operand with shape ()
|
|
1643
|
+
# doesn't match the broadcast shape (3,)`.
|
|
1644
|
+
f = vectorize(_foo1, otypes=[float])
|
|
1645
|
+
# We're testing the caching of ufuncs by vectorize, so the order
|
|
1646
|
+
# of these function calls is an important part of the test.
|
|
1647
|
+
r1 = f(np.arange(3.0))
|
|
1648
|
+
r2 = f(np.arange(3.0), 1.0)
|
|
1649
|
+
assert_array_equal(r1, r2)
|
|
1650
|
+
|
|
1651
|
+
def test_keywords_with_otypes_order3(self):
|
|
1652
|
+
# gh-1620: The third call of f would crash with
|
|
1653
|
+
# `ValueError: invalid number of arguments`.
|
|
1654
|
+
f = vectorize(_foo1, otypes=[float])
|
|
1655
|
+
# We're testing the caching of ufuncs by vectorize, so the order
|
|
1656
|
+
# of these function calls is an important part of the test.
|
|
1657
|
+
r1 = f(np.arange(3.0))
|
|
1658
|
+
r2 = f(np.arange(3.0), y=1.0)
|
|
1659
|
+
r3 = f(np.arange(3.0))
|
|
1660
|
+
assert_array_equal(r1, r2)
|
|
1661
|
+
assert_array_equal(r1, r3)
|
|
1662
|
+
|
|
1663
|
+
def test_keywords_with_otypes_several_kwd_args1(self):
|
|
1664
|
+
# gh-1620 Make sure different uses of keyword arguments
|
|
1665
|
+
# don't break the vectorized function.
|
|
1666
|
+
f = vectorize(_foo2, otypes=[float])
|
|
1667
|
+
# We're testing the caching of ufuncs by vectorize, so the order
|
|
1668
|
+
# of these function calls is an important part of the test.
|
|
1669
|
+
r1 = f(10.4, z=100)
|
|
1670
|
+
r2 = f(10.4, y=-1)
|
|
1671
|
+
r3 = f(10.4)
|
|
1672
|
+
assert_equal(r1, _foo2(10.4, z=100))
|
|
1673
|
+
assert_equal(r2, _foo2(10.4, y=-1))
|
|
1674
|
+
assert_equal(r3, _foo2(10.4))
|
|
1675
|
+
|
|
1676
|
+
def test_keywords_with_otypes_several_kwd_args2(self):
|
|
1677
|
+
# gh-1620 Make sure different uses of keyword arguments
|
|
1678
|
+
# don't break the vectorized function.
|
|
1679
|
+
f = vectorize(_foo2, otypes=[float])
|
|
1680
|
+
# We're testing the caching of ufuncs by vectorize, so the order
|
|
1681
|
+
# of these function calls is an important part of the test.
|
|
1682
|
+
r1 = f(z=100, x=10.4, y=-1)
|
|
1683
|
+
r2 = f(1, 2, 3)
|
|
1684
|
+
assert_equal(r1, _foo2(z=100, x=10.4, y=-1))
|
|
1685
|
+
assert_equal(r2, _foo2(1, 2, 3))
|
|
1686
|
+
|
|
1687
|
+
def test_keywords_no_func_code(self):
|
|
1688
|
+
# This needs to test a function that has keywords but
|
|
1689
|
+
# no func_code attribute, since otherwise vectorize will
|
|
1690
|
+
# inspect the func_code.
|
|
1691
|
+
import random
|
|
1692
|
+
try:
|
|
1693
|
+
vectorize(random.randrange) # Should succeed
|
|
1694
|
+
except Exception:
|
|
1695
|
+
raise AssertionError
|
|
1696
|
+
|
|
1697
|
+
def test_keywords2_ticket_2100(self):
|
|
1698
|
+
# Test kwarg support: enhancement ticket 2100
|
|
1699
|
+
|
|
1700
|
+
def foo(a, b=1):
|
|
1701
|
+
return a + b
|
|
1702
|
+
|
|
1703
|
+
f = vectorize(foo)
|
|
1704
|
+
args = np.array([1, 2, 3])
|
|
1705
|
+
r1 = f(a=args)
|
|
1706
|
+
r2 = np.array([2, 3, 4])
|
|
1707
|
+
assert_array_equal(r1, r2)
|
|
1708
|
+
r1 = f(b=1, a=args)
|
|
1709
|
+
assert_array_equal(r1, r2)
|
|
1710
|
+
r1 = f(args, b=2)
|
|
1711
|
+
r2 = np.array([3, 4, 5])
|
|
1712
|
+
assert_array_equal(r1, r2)
|
|
1713
|
+
|
|
1714
|
+
def test_keywords3_ticket_2100(self):
|
|
1715
|
+
# Test excluded with mixed positional and kwargs: ticket 2100
|
|
1716
|
+
def mypolyval(x, p):
|
|
1717
|
+
_p = list(p)
|
|
1718
|
+
res = _p.pop(0)
|
|
1719
|
+
while _p:
|
|
1720
|
+
res = res * x + _p.pop(0)
|
|
1721
|
+
return res
|
|
1722
|
+
|
|
1723
|
+
vpolyval = np.vectorize(mypolyval, excluded=['p', 1])
|
|
1724
|
+
ans = [3, 6]
|
|
1725
|
+
assert_array_equal(ans, vpolyval(x=[0, 1], p=[1, 2, 3]))
|
|
1726
|
+
assert_array_equal(ans, vpolyval([0, 1], p=[1, 2, 3]))
|
|
1727
|
+
assert_array_equal(ans, vpolyval([0, 1], [1, 2, 3]))
|
|
1728
|
+
|
|
1729
|
+
def test_keywords4_ticket_2100(self):
|
|
1730
|
+
# Test vectorizing function with no positional args.
|
|
1731
|
+
@vectorize
|
|
1732
|
+
def f(**kw):
|
|
1733
|
+
res = 1.0
|
|
1734
|
+
for _k in kw:
|
|
1735
|
+
res *= kw[_k]
|
|
1736
|
+
return res
|
|
1737
|
+
|
|
1738
|
+
assert_array_equal(f(a=[1, 2], b=[3, 4]), [3, 8])
|
|
1739
|
+
|
|
1740
|
+
def test_keywords5_ticket_2100(self):
|
|
1741
|
+
# Test vectorizing function with no kwargs args.
|
|
1742
|
+
@vectorize
|
|
1743
|
+
def f(*v):
|
|
1744
|
+
return np.prod(v)
|
|
1745
|
+
|
|
1746
|
+
assert_array_equal(f([1, 2], [3, 4]), [3, 8])
|
|
1747
|
+
|
|
1748
|
+
def test_coverage1_ticket_2100(self):
|
|
1749
|
+
def foo():
|
|
1750
|
+
return 1
|
|
1751
|
+
|
|
1752
|
+
f = vectorize(foo)
|
|
1753
|
+
assert_array_equal(f(), 1)
|
|
1754
|
+
|
|
1755
|
+
def test_assigning_docstring(self):
|
|
1756
|
+
def foo(x):
|
|
1757
|
+
"""Original documentation"""
|
|
1758
|
+
return x
|
|
1759
|
+
|
|
1760
|
+
f = vectorize(foo)
|
|
1761
|
+
assert_equal(f.__doc__, foo.__doc__)
|
|
1762
|
+
|
|
1763
|
+
doc = "Provided documentation"
|
|
1764
|
+
f = vectorize(foo, doc=doc)
|
|
1765
|
+
assert_equal(f.__doc__, doc)
|
|
1766
|
+
|
|
1767
|
+
def test_UnboundMethod_ticket_1156(self):
|
|
1768
|
+
# Regression test for issue 1156
|
|
1769
|
+
class Foo:
|
|
1770
|
+
b = 2
|
|
1771
|
+
|
|
1772
|
+
def bar(self, a):
|
|
1773
|
+
return a ** self.b
|
|
1774
|
+
|
|
1775
|
+
assert_array_equal(vectorize(Foo().bar)(np.arange(9)),
|
|
1776
|
+
np.arange(9) ** 2)
|
|
1777
|
+
assert_array_equal(vectorize(Foo.bar)(Foo(), np.arange(9)),
|
|
1778
|
+
np.arange(9) ** 2)
|
|
1779
|
+
|
|
1780
|
+
def test_execution_order_ticket_1487(self):
|
|
1781
|
+
# Regression test for dependence on execution order: issue 1487
|
|
1782
|
+
f1 = vectorize(lambda x: x)
|
|
1783
|
+
res1a = f1(np.arange(3))
|
|
1784
|
+
res1b = f1(np.arange(0.1, 3))
|
|
1785
|
+
f2 = vectorize(lambda x: x)
|
|
1786
|
+
res2b = f2(np.arange(0.1, 3))
|
|
1787
|
+
res2a = f2(np.arange(3))
|
|
1788
|
+
assert_equal(res1a, res2a)
|
|
1789
|
+
assert_equal(res1b, res2b)
|
|
1790
|
+
|
|
1791
|
+
def test_string_ticket_1892(self):
|
|
1792
|
+
# Test vectorization over strings: issue 1892.
|
|
1793
|
+
f = np.vectorize(lambda x: x)
|
|
1794
|
+
s = '0123456789' * 10
|
|
1795
|
+
assert_equal(s, f(s))
|
|
1796
|
+
|
|
1797
|
+
def test_dtype_promotion_gh_29189(self):
|
|
1798
|
+
# dtype should not be silently promoted (int32 -> int64)
|
|
1799
|
+
dtypes = [np.int16, np.int32, np.int64, np.float16, np.float32, np.float64]
|
|
1800
|
+
|
|
1801
|
+
for dtype in dtypes:
|
|
1802
|
+
x = np.asarray([1, 2, 3], dtype=dtype)
|
|
1803
|
+
y = np.vectorize(lambda x: x + x)(x)
|
|
1804
|
+
assert x.dtype == y.dtype
|
|
1805
|
+
|
|
1806
|
+
def test_cache(self):
|
|
1807
|
+
# Ensure that vectorized func called exactly once per argument.
|
|
1808
|
+
_calls = [0]
|
|
1809
|
+
|
|
1810
|
+
@vectorize
|
|
1811
|
+
def f(x):
|
|
1812
|
+
_calls[0] += 1
|
|
1813
|
+
return x ** 2
|
|
1814
|
+
|
|
1815
|
+
f.cache = True
|
|
1816
|
+
x = np.arange(5)
|
|
1817
|
+
assert_array_equal(f(x), x * x)
|
|
1818
|
+
assert_equal(_calls[0], len(x))
|
|
1819
|
+
|
|
1820
|
+
def test_otypes(self):
|
|
1821
|
+
f = np.vectorize(lambda x: x)
|
|
1822
|
+
f.otypes = 'i'
|
|
1823
|
+
x = np.arange(5)
|
|
1824
|
+
assert_array_equal(f(x), x)
|
|
1825
|
+
|
|
1826
|
+
def test_otypes_object_28624(self):
|
|
1827
|
+
# with object otype, the vectorized function should return y
|
|
1828
|
+
# wrapped into an object array
|
|
1829
|
+
y = np.arange(3)
|
|
1830
|
+
f = vectorize(lambda x: y, otypes=[object])
|
|
1831
|
+
|
|
1832
|
+
assert f(None).item() is y
|
|
1833
|
+
assert f([None]).item() is y
|
|
1834
|
+
|
|
1835
|
+
y = [1, 2, 3]
|
|
1836
|
+
f = vectorize(lambda x: y, otypes=[object])
|
|
1837
|
+
|
|
1838
|
+
assert f(None).item() is y
|
|
1839
|
+
assert f([None]).item() is y
|
|
1840
|
+
|
|
1841
|
+
def test_parse_gufunc_signature(self):
|
|
1842
|
+
assert_equal(nfb._parse_gufunc_signature('(x)->()'), ([('x',)], [()]))
|
|
1843
|
+
assert_equal(nfb._parse_gufunc_signature('(x,y)->()'),
|
|
1844
|
+
([('x', 'y')], [()]))
|
|
1845
|
+
assert_equal(nfb._parse_gufunc_signature('(x),(y)->()'),
|
|
1846
|
+
([('x',), ('y',)], [()]))
|
|
1847
|
+
assert_equal(nfb._parse_gufunc_signature('(x)->(y)'),
|
|
1848
|
+
([('x',)], [('y',)]))
|
|
1849
|
+
assert_equal(nfb._parse_gufunc_signature('(x)->(y),()'),
|
|
1850
|
+
([('x',)], [('y',), ()]))
|
|
1851
|
+
assert_equal(nfb._parse_gufunc_signature('(),(a,b,c),(d)->(d,e)'),
|
|
1852
|
+
([(), ('a', 'b', 'c'), ('d',)], [('d', 'e')]))
|
|
1853
|
+
|
|
1854
|
+
# Tests to check if whitespaces are ignored
|
|
1855
|
+
assert_equal(nfb._parse_gufunc_signature('(x )->()'), ([('x',)], [()]))
|
|
1856
|
+
assert_equal(nfb._parse_gufunc_signature('( x , y )->( )'),
|
|
1857
|
+
([('x', 'y')], [()]))
|
|
1858
|
+
assert_equal(nfb._parse_gufunc_signature('(x),( y) ->()'),
|
|
1859
|
+
([('x',), ('y',)], [()]))
|
|
1860
|
+
assert_equal(nfb._parse_gufunc_signature('( x)-> (y ) '),
|
|
1861
|
+
([('x',)], [('y',)]))
|
|
1862
|
+
assert_equal(nfb._parse_gufunc_signature(' (x)->( y),( )'),
|
|
1863
|
+
([('x',)], [('y',), ()]))
|
|
1864
|
+
assert_equal(nfb._parse_gufunc_signature(
|
|
1865
|
+
'( ), ( a, b,c ) ,( d) -> (d , e)'),
|
|
1866
|
+
([(), ('a', 'b', 'c'), ('d',)], [('d', 'e')]))
|
|
1867
|
+
|
|
1868
|
+
with assert_raises(ValueError):
|
|
1869
|
+
nfb._parse_gufunc_signature('(x)(y)->()')
|
|
1870
|
+
with assert_raises(ValueError):
|
|
1871
|
+
nfb._parse_gufunc_signature('(x),(y)->')
|
|
1872
|
+
with assert_raises(ValueError):
|
|
1873
|
+
nfb._parse_gufunc_signature('((x))->(x)')
|
|
1874
|
+
|
|
1875
|
+
def test_signature_simple(self):
|
|
1876
|
+
def addsubtract(a, b):
|
|
1877
|
+
if a > b:
|
|
1878
|
+
return a - b
|
|
1879
|
+
else:
|
|
1880
|
+
return a + b
|
|
1881
|
+
|
|
1882
|
+
f = vectorize(addsubtract, signature='(),()->()')
|
|
1883
|
+
r = f([0, 3, 6, 9], [1, 3, 5, 7])
|
|
1884
|
+
assert_array_equal(r, [1, 6, 1, 2])
|
|
1885
|
+
|
|
1886
|
+
def test_signature_mean_last(self):
|
|
1887
|
+
def mean(a):
|
|
1888
|
+
return a.mean()
|
|
1889
|
+
|
|
1890
|
+
f = vectorize(mean, signature='(n)->()')
|
|
1891
|
+
r = f([[1, 3], [2, 4]])
|
|
1892
|
+
assert_array_equal(r, [2, 3])
|
|
1893
|
+
|
|
1894
|
+
def test_signature_center(self):
|
|
1895
|
+
def center(a):
|
|
1896
|
+
return a - a.mean()
|
|
1897
|
+
|
|
1898
|
+
f = vectorize(center, signature='(n)->(n)')
|
|
1899
|
+
r = f([[1, 3], [2, 4]])
|
|
1900
|
+
assert_array_equal(r, [[-1, 1], [-1, 1]])
|
|
1901
|
+
|
|
1902
|
+
def test_signature_two_outputs(self):
|
|
1903
|
+
f = vectorize(lambda x: (x, x), signature='()->(),()')
|
|
1904
|
+
r = f([1, 2, 3])
|
|
1905
|
+
assert_(isinstance(r, tuple) and len(r) == 2)
|
|
1906
|
+
assert_array_equal(r[0], [1, 2, 3])
|
|
1907
|
+
assert_array_equal(r[1], [1, 2, 3])
|
|
1908
|
+
|
|
1909
|
+
def test_signature_outer(self):
|
|
1910
|
+
f = vectorize(np.outer, signature='(a),(b)->(a,b)')
|
|
1911
|
+
r = f([1, 2], [1, 2, 3])
|
|
1912
|
+
assert_array_equal(r, [[1, 2, 3], [2, 4, 6]])
|
|
1913
|
+
|
|
1914
|
+
r = f([[[1, 2]]], [1, 2, 3])
|
|
1915
|
+
assert_array_equal(r, [[[[1, 2, 3], [2, 4, 6]]]])
|
|
1916
|
+
|
|
1917
|
+
r = f([[1, 0], [2, 0]], [1, 2, 3])
|
|
1918
|
+
assert_array_equal(r, [[[1, 2, 3], [0, 0, 0]],
|
|
1919
|
+
[[2, 4, 6], [0, 0, 0]]])
|
|
1920
|
+
|
|
1921
|
+
r = f([1, 2], [[1, 2, 3], [0, 0, 0]])
|
|
1922
|
+
assert_array_equal(r, [[[1, 2, 3], [2, 4, 6]],
|
|
1923
|
+
[[0, 0, 0], [0, 0, 0]]])
|
|
1924
|
+
|
|
1925
|
+
def test_signature_computed_size(self):
|
|
1926
|
+
f = vectorize(lambda x: x[:-1], signature='(n)->(m)')
|
|
1927
|
+
r = f([1, 2, 3])
|
|
1928
|
+
assert_array_equal(r, [1, 2])
|
|
1929
|
+
|
|
1930
|
+
r = f([[1, 2, 3], [2, 3, 4]])
|
|
1931
|
+
assert_array_equal(r, [[1, 2], [2, 3]])
|
|
1932
|
+
|
|
1933
|
+
def test_signature_excluded(self):
|
|
1934
|
+
|
|
1935
|
+
def foo(a, b=1):
|
|
1936
|
+
return a + b
|
|
1937
|
+
|
|
1938
|
+
f = vectorize(foo, signature='()->()', excluded={'b'})
|
|
1939
|
+
assert_array_equal(f([1, 2, 3]), [2, 3, 4])
|
|
1940
|
+
assert_array_equal(f([1, 2, 3], b=0), [1, 2, 3])
|
|
1941
|
+
|
|
1942
|
+
def test_signature_otypes(self):
|
|
1943
|
+
f = vectorize(lambda x: x, signature='(n)->(n)', otypes=['float64'])
|
|
1944
|
+
r = f([1, 2, 3])
|
|
1945
|
+
assert_equal(r.dtype, np.dtype('float64'))
|
|
1946
|
+
assert_array_equal(r, [1, 2, 3])
|
|
1947
|
+
|
|
1948
|
+
def test_signature_invalid_inputs(self):
|
|
1949
|
+
f = vectorize(operator.add, signature='(n),(n)->(n)')
|
|
1950
|
+
with assert_raises_regex(TypeError, 'wrong number of positional'):
|
|
1951
|
+
f([1, 2])
|
|
1952
|
+
with assert_raises_regex(
|
|
1953
|
+
ValueError, 'does not have enough dimensions'):
|
|
1954
|
+
f(1, 2)
|
|
1955
|
+
with assert_raises_regex(
|
|
1956
|
+
ValueError, 'inconsistent size for core dimension'):
|
|
1957
|
+
f([1, 2], [1, 2, 3])
|
|
1958
|
+
|
|
1959
|
+
f = vectorize(operator.add, signature='()->()')
|
|
1960
|
+
with assert_raises_regex(TypeError, 'wrong number of positional'):
|
|
1961
|
+
f(1, 2)
|
|
1962
|
+
|
|
1963
|
+
def test_signature_invalid_outputs(self):
|
|
1964
|
+
|
|
1965
|
+
f = vectorize(lambda x: x[:-1], signature='(n)->(n)')
|
|
1966
|
+
with assert_raises_regex(
|
|
1967
|
+
ValueError, 'inconsistent size for core dimension'):
|
|
1968
|
+
f([1, 2, 3])
|
|
1969
|
+
|
|
1970
|
+
f = vectorize(lambda x: x, signature='()->(),()')
|
|
1971
|
+
with assert_raises_regex(ValueError, 'wrong number of outputs'):
|
|
1972
|
+
f(1)
|
|
1973
|
+
|
|
1974
|
+
f = vectorize(lambda x: (x, x), signature='()->()')
|
|
1975
|
+
with assert_raises_regex(ValueError, 'wrong number of outputs'):
|
|
1976
|
+
f([1, 2])
|
|
1977
|
+
|
|
1978
|
+
def test_size_zero_output(self):
|
|
1979
|
+
# see issue 5868
|
|
1980
|
+
f = np.vectorize(lambda x: x)
|
|
1981
|
+
x = np.zeros([0, 5], dtype=int)
|
|
1982
|
+
with assert_raises_regex(ValueError, 'otypes'):
|
|
1983
|
+
f(x)
|
|
1984
|
+
|
|
1985
|
+
f.otypes = 'i'
|
|
1986
|
+
assert_array_equal(f(x), x)
|
|
1987
|
+
|
|
1988
|
+
f = np.vectorize(lambda x: x, signature='()->()')
|
|
1989
|
+
with assert_raises_regex(ValueError, 'otypes'):
|
|
1990
|
+
f(x)
|
|
1991
|
+
|
|
1992
|
+
f = np.vectorize(lambda x: x, signature='()->()', otypes='i')
|
|
1993
|
+
assert_array_equal(f(x), x)
|
|
1994
|
+
|
|
1995
|
+
f = np.vectorize(lambda x: x, signature='(n)->(n)', otypes='i')
|
|
1996
|
+
assert_array_equal(f(x), x)
|
|
1997
|
+
|
|
1998
|
+
f = np.vectorize(lambda x: x, signature='(n)->(n)')
|
|
1999
|
+
assert_array_equal(f(x.T), x.T)
|
|
2000
|
+
|
|
2001
|
+
f = np.vectorize(lambda x: [x], signature='()->(n)', otypes='i')
|
|
2002
|
+
with assert_raises_regex(ValueError, 'new output dimensions'):
|
|
2003
|
+
f(x)
|
|
2004
|
+
|
|
2005
|
+
def test_subclasses(self):
|
|
2006
|
+
class subclass(np.ndarray):
|
|
2007
|
+
pass
|
|
2008
|
+
|
|
2009
|
+
m = np.array([[1., 0., 0.],
|
|
2010
|
+
[0., 0., 1.],
|
|
2011
|
+
[0., 1., 0.]]).view(subclass)
|
|
2012
|
+
v = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]).view(subclass)
|
|
2013
|
+
# generalized (gufunc)
|
|
2014
|
+
matvec = np.vectorize(np.matmul, signature='(m,m),(m)->(m)')
|
|
2015
|
+
r = matvec(m, v)
|
|
2016
|
+
assert_equal(type(r), subclass)
|
|
2017
|
+
assert_equal(r, [[1., 3., 2.], [4., 6., 5.], [7., 9., 8.]])
|
|
2018
|
+
|
|
2019
|
+
# element-wise (ufunc)
|
|
2020
|
+
mult = np.vectorize(lambda x, y: x * y)
|
|
2021
|
+
r = mult(m, v)
|
|
2022
|
+
assert_equal(type(r), subclass)
|
|
2023
|
+
assert_equal(r, m * v)
|
|
2024
|
+
|
|
2025
|
+
def test_name(self):
|
|
2026
|
+
# gh-23021
|
|
2027
|
+
@np.vectorize
|
|
2028
|
+
def f2(a, b):
|
|
2029
|
+
return a + b
|
|
2030
|
+
|
|
2031
|
+
assert f2.__name__ == 'f2'
|
|
2032
|
+
|
|
2033
|
+
def test_decorator(self):
|
|
2034
|
+
@vectorize
|
|
2035
|
+
def addsubtract(a, b):
|
|
2036
|
+
if a > b:
|
|
2037
|
+
return a - b
|
|
2038
|
+
else:
|
|
2039
|
+
return a + b
|
|
2040
|
+
|
|
2041
|
+
r = addsubtract([0, 3, 6, 9], [1, 3, 5, 7])
|
|
2042
|
+
assert_array_equal(r, [1, 6, 1, 2])
|
|
2043
|
+
|
|
2044
|
+
def test_docstring(self):
|
|
2045
|
+
@vectorize
|
|
2046
|
+
def f(x):
|
|
2047
|
+
"""Docstring"""
|
|
2048
|
+
return x
|
|
2049
|
+
|
|
2050
|
+
if sys.flags.optimize < 2:
|
|
2051
|
+
assert f.__doc__ == "Docstring"
|
|
2052
|
+
|
|
2053
|
+
def test_partial(self):
|
|
2054
|
+
def foo(x, y):
|
|
2055
|
+
return x + y
|
|
2056
|
+
|
|
2057
|
+
bar = partial(foo, 3)
|
|
2058
|
+
vbar = np.vectorize(bar)
|
|
2059
|
+
assert vbar(1) == 4
|
|
2060
|
+
|
|
2061
|
+
def test_signature_otypes_decorator(self):
|
|
2062
|
+
@vectorize(signature='(n)->(n)', otypes=['float64'])
|
|
2063
|
+
def f(x):
|
|
2064
|
+
return x
|
|
2065
|
+
|
|
2066
|
+
r = f([1, 2, 3])
|
|
2067
|
+
assert_equal(r.dtype, np.dtype('float64'))
|
|
2068
|
+
assert_array_equal(r, [1, 2, 3])
|
|
2069
|
+
assert f.__name__ == 'f'
|
|
2070
|
+
|
|
2071
|
+
def test_bad_input(self):
|
|
2072
|
+
with assert_raises(TypeError):
|
|
2073
|
+
A = np.vectorize(pyfunc=3)
|
|
2074
|
+
|
|
2075
|
+
def test_no_keywords(self):
|
|
2076
|
+
with assert_raises(TypeError):
|
|
2077
|
+
@np.vectorize("string")
|
|
2078
|
+
def foo():
|
|
2079
|
+
return "bar"
|
|
2080
|
+
|
|
2081
|
+
def test_positional_regression_9477(self):
|
|
2082
|
+
# This supplies the first keyword argument as a positional,
|
|
2083
|
+
# to ensure that they are still properly forwarded after the
|
|
2084
|
+
# enhancement for #9477
|
|
2085
|
+
f = vectorize((lambda x: x), ['float64'])
|
|
2086
|
+
r = f([2])
|
|
2087
|
+
assert_equal(r.dtype, np.dtype('float64'))
|
|
2088
|
+
|
|
2089
|
+
def test_datetime_conversion(self):
|
|
2090
|
+
otype = "datetime64[ns]"
|
|
2091
|
+
arr = np.array(['2024-01-01', '2024-01-02', '2024-01-03'],
|
|
2092
|
+
dtype='datetime64[ns]')
|
|
2093
|
+
assert_array_equal(np.vectorize(lambda x: x, signature="(i)->(j)",
|
|
2094
|
+
otypes=[otype])(arr), arr)
|
|
2095
|
+
|
|
2096
|
+
|
|
2097
|
+
class TestLeaks:
|
|
2098
|
+
class A:
|
|
2099
|
+
iters = 20
|
|
2100
|
+
|
|
2101
|
+
def bound(self, *args):
|
|
2102
|
+
return 0
|
|
2103
|
+
|
|
2104
|
+
@staticmethod
|
|
2105
|
+
def unbound(*args):
|
|
2106
|
+
return 0
|
|
2107
|
+
|
|
2108
|
+
@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
|
|
2109
|
+
@pytest.mark.skipif(NOGIL_BUILD,
|
|
2110
|
+
reason=("Functions are immortalized if a thread is "
|
|
2111
|
+
"launched, making this test flaky"))
|
|
2112
|
+
@pytest.mark.parametrize('name, incr', [
|
|
2113
|
+
('bound', A.iters),
|
|
2114
|
+
('unbound', 0),
|
|
2115
|
+
])
|
|
2116
|
+
@pytest.mark.thread_unsafe(
|
|
2117
|
+
reason="test result depends on the reference count of a global object"
|
|
2118
|
+
)
|
|
2119
|
+
def test_frompyfunc_leaks(self, name, incr):
|
|
2120
|
+
# exposed in gh-11867 as np.vectorized, but the problem stems from
|
|
2121
|
+
# frompyfunc.
|
|
2122
|
+
# class.attribute = np.frompyfunc(<method>) creates a
|
|
2123
|
+
# reference cycle if <method> is a bound class method.
|
|
2124
|
+
# It requires a gc collection cycle to break the cycle.
|
|
2125
|
+
import gc
|
|
2126
|
+
A_func = getattr(self.A, name)
|
|
2127
|
+
gc.disable()
|
|
2128
|
+
try:
|
|
2129
|
+
refcount = sys.getrefcount(A_func)
|
|
2130
|
+
for i in range(self.A.iters):
|
|
2131
|
+
a = self.A()
|
|
2132
|
+
a.f = np.frompyfunc(getattr(a, name), 1, 1)
|
|
2133
|
+
out = a.f(np.arange(10))
|
|
2134
|
+
a = None
|
|
2135
|
+
# A.func is part of a reference cycle if incr is non-zero
|
|
2136
|
+
assert_equal(sys.getrefcount(A_func), refcount + incr)
|
|
2137
|
+
for i in range(5):
|
|
2138
|
+
gc.collect()
|
|
2139
|
+
assert_equal(sys.getrefcount(A_func), refcount)
|
|
2140
|
+
finally:
|
|
2141
|
+
gc.enable()
|
|
2142
|
+
|
|
2143
|
+
|
|
2144
|
+
class TestDigitize:
|
|
2145
|
+
|
|
2146
|
+
def test_forward(self):
|
|
2147
|
+
x = np.arange(-6, 5)
|
|
2148
|
+
bins = np.arange(-5, 5)
|
|
2149
|
+
assert_array_equal(digitize(x, bins), np.arange(11))
|
|
2150
|
+
|
|
2151
|
+
def test_reverse(self):
|
|
2152
|
+
x = np.arange(5, -6, -1)
|
|
2153
|
+
bins = np.arange(5, -5, -1)
|
|
2154
|
+
assert_array_equal(digitize(x, bins), np.arange(11))
|
|
2155
|
+
|
|
2156
|
+
def test_random(self):
|
|
2157
|
+
x = rand(10)
|
|
2158
|
+
bin = np.linspace(x.min(), x.max(), 10)
|
|
2159
|
+
assert_(np.all(digitize(x, bin) != 0))
|
|
2160
|
+
|
|
2161
|
+
def test_right_basic(self):
|
|
2162
|
+
x = [1, 5, 4, 10, 8, 11, 0]
|
|
2163
|
+
bins = [1, 5, 10]
|
|
2164
|
+
default_answer = [1, 2, 1, 3, 2, 3, 0]
|
|
2165
|
+
assert_array_equal(digitize(x, bins), default_answer)
|
|
2166
|
+
right_answer = [0, 1, 1, 2, 2, 3, 0]
|
|
2167
|
+
assert_array_equal(digitize(x, bins, True), right_answer)
|
|
2168
|
+
|
|
2169
|
+
def test_right_open(self):
|
|
2170
|
+
x = np.arange(-6, 5)
|
|
2171
|
+
bins = np.arange(-6, 4)
|
|
2172
|
+
assert_array_equal(digitize(x, bins, True), np.arange(11))
|
|
2173
|
+
|
|
2174
|
+
def test_right_open_reverse(self):
|
|
2175
|
+
x = np.arange(5, -6, -1)
|
|
2176
|
+
bins = np.arange(4, -6, -1)
|
|
2177
|
+
assert_array_equal(digitize(x, bins, True), np.arange(11))
|
|
2178
|
+
|
|
2179
|
+
def test_right_open_random(self):
|
|
2180
|
+
x = rand(10)
|
|
2181
|
+
bins = np.linspace(x.min(), x.max(), 10)
|
|
2182
|
+
assert_(np.all(digitize(x, bins, True) != 10))
|
|
2183
|
+
|
|
2184
|
+
def test_monotonic(self):
|
|
2185
|
+
x = [-1, 0, 1, 2]
|
|
2186
|
+
bins = [0, 0, 1]
|
|
2187
|
+
assert_array_equal(digitize(x, bins, False), [0, 2, 3, 3])
|
|
2188
|
+
assert_array_equal(digitize(x, bins, True), [0, 0, 2, 3])
|
|
2189
|
+
bins = [1, 1, 0]
|
|
2190
|
+
assert_array_equal(digitize(x, bins, False), [3, 2, 0, 0])
|
|
2191
|
+
assert_array_equal(digitize(x, bins, True), [3, 3, 2, 0])
|
|
2192
|
+
bins = [1, 1, 1, 1]
|
|
2193
|
+
assert_array_equal(digitize(x, bins, False), [0, 0, 4, 4])
|
|
2194
|
+
assert_array_equal(digitize(x, bins, True), [0, 0, 0, 4])
|
|
2195
|
+
bins = [0, 0, 1, 0]
|
|
2196
|
+
assert_raises(ValueError, digitize, x, bins)
|
|
2197
|
+
bins = [1, 1, 0, 1]
|
|
2198
|
+
assert_raises(ValueError, digitize, x, bins)
|
|
2199
|
+
|
|
2200
|
+
def test_casting_error(self):
|
|
2201
|
+
x = [1, 2, 3 + 1.j]
|
|
2202
|
+
bins = [1, 2, 3]
|
|
2203
|
+
assert_raises(TypeError, digitize, x, bins)
|
|
2204
|
+
x, bins = bins, x
|
|
2205
|
+
assert_raises(TypeError, digitize, x, bins)
|
|
2206
|
+
|
|
2207
|
+
def test_return_type(self):
|
|
2208
|
+
# Functions returning indices should always return base ndarrays
|
|
2209
|
+
class A(np.ndarray):
|
|
2210
|
+
pass
|
|
2211
|
+
a = np.arange(5).view(A)
|
|
2212
|
+
b = np.arange(1, 3).view(A)
|
|
2213
|
+
assert_(not isinstance(digitize(b, a, False), A))
|
|
2214
|
+
assert_(not isinstance(digitize(b, a, True), A))
|
|
2215
|
+
|
|
2216
|
+
def test_large_integers_increasing(self):
|
|
2217
|
+
# gh-11022
|
|
2218
|
+
x = 2**54 # loses precision in a float
|
|
2219
|
+
assert_equal(np.digitize(x, [x - 1, x + 1]), 1)
|
|
2220
|
+
|
|
2221
|
+
@pytest.mark.xfail(
|
|
2222
|
+
reason="gh-11022: np._core.multiarray._monoticity loses precision")
|
|
2223
|
+
def test_large_integers_decreasing(self):
|
|
2224
|
+
# gh-11022
|
|
2225
|
+
x = 2**54 # loses precision in a float
|
|
2226
|
+
assert_equal(np.digitize(x, [x + 1, x - 1]), 1)
|
|
2227
|
+
|
|
2228
|
+
|
|
2229
|
+
class TestUnwrap:
|
|
2230
|
+
|
|
2231
|
+
def test_simple(self):
|
|
2232
|
+
# check that unwrap removes jumps greater that 2*pi
|
|
2233
|
+
assert_array_equal(unwrap([1, 1 + 2 * np.pi]), [1, 1])
|
|
2234
|
+
# check that unwrap maintains continuity
|
|
2235
|
+
assert_(np.all(diff(unwrap(rand(10) * 100)) < np.pi))
|
|
2236
|
+
|
|
2237
|
+
def test_period(self):
|
|
2238
|
+
# check that unwrap removes jumps greater that 255
|
|
2239
|
+
assert_array_equal(unwrap([1, 1 + 256], period=255), [1, 2])
|
|
2240
|
+
# check that unwrap maintains continuity
|
|
2241
|
+
assert_(np.all(diff(unwrap(rand(10) * 1000, period=255)) < 255))
|
|
2242
|
+
# check simple case
|
|
2243
|
+
simple_seq = np.array([0, 75, 150, 225, 300])
|
|
2244
|
+
wrap_seq = np.mod(simple_seq, 255)
|
|
2245
|
+
assert_array_equal(unwrap(wrap_seq, period=255), simple_seq)
|
|
2246
|
+
# check custom discont value
|
|
2247
|
+
uneven_seq = np.array([0, 75, 150, 225, 300, 430])
|
|
2248
|
+
wrap_uneven = np.mod(uneven_seq, 250)
|
|
2249
|
+
no_discont = unwrap(wrap_uneven, period=250)
|
|
2250
|
+
assert_array_equal(no_discont, [0, 75, 150, 225, 300, 180])
|
|
2251
|
+
sm_discont = unwrap(wrap_uneven, period=250, discont=140)
|
|
2252
|
+
assert_array_equal(sm_discont, [0, 75, 150, 225, 300, 430])
|
|
2253
|
+
assert sm_discont.dtype == wrap_uneven.dtype
|
|
2254
|
+
|
|
2255
|
+
|
|
2256
|
+
@pytest.mark.parametrize(
|
|
2257
|
+
"dtype", "O" + np.typecodes["AllInteger"] + np.typecodes["Float"]
|
|
2258
|
+
)
|
|
2259
|
+
@pytest.mark.parametrize("M", [0, 1, 10])
|
|
2260
|
+
class TestFilterwindows:
|
|
2261
|
+
|
|
2262
|
+
def test_hanning(self, dtype: str, M: int) -> None:
|
|
2263
|
+
scalar = np.array(M, dtype=dtype)[()]
|
|
2264
|
+
|
|
2265
|
+
w = hanning(scalar)
|
|
2266
|
+
if dtype == "O":
|
|
2267
|
+
ref_dtype = np.float64
|
|
2268
|
+
else:
|
|
2269
|
+
ref_dtype = np.result_type(scalar.dtype, np.float64)
|
|
2270
|
+
assert w.dtype == ref_dtype
|
|
2271
|
+
|
|
2272
|
+
# check symmetry
|
|
2273
|
+
assert_equal(w, flipud(w))
|
|
2274
|
+
|
|
2275
|
+
# check known value
|
|
2276
|
+
if scalar < 1:
|
|
2277
|
+
assert_array_equal(w, np.array([]))
|
|
2278
|
+
elif scalar == 1:
|
|
2279
|
+
assert_array_equal(w, np.ones(1))
|
|
2280
|
+
else:
|
|
2281
|
+
assert_almost_equal(np.sum(w, axis=0), 4.500, 4)
|
|
2282
|
+
|
|
2283
|
+
def test_hamming(self, dtype: str, M: int) -> None:
|
|
2284
|
+
scalar = np.array(M, dtype=dtype)[()]
|
|
2285
|
+
|
|
2286
|
+
w = hamming(scalar)
|
|
2287
|
+
if dtype == "O":
|
|
2288
|
+
ref_dtype = np.float64
|
|
2289
|
+
else:
|
|
2290
|
+
ref_dtype = np.result_type(scalar.dtype, np.float64)
|
|
2291
|
+
assert w.dtype == ref_dtype
|
|
2292
|
+
|
|
2293
|
+
# check symmetry
|
|
2294
|
+
assert_equal(w, flipud(w))
|
|
2295
|
+
|
|
2296
|
+
# check known value
|
|
2297
|
+
if scalar < 1:
|
|
2298
|
+
assert_array_equal(w, np.array([]))
|
|
2299
|
+
elif scalar == 1:
|
|
2300
|
+
assert_array_equal(w, np.ones(1))
|
|
2301
|
+
else:
|
|
2302
|
+
assert_almost_equal(np.sum(w, axis=0), 4.9400, 4)
|
|
2303
|
+
|
|
2304
|
+
def test_bartlett(self, dtype: str, M: int) -> None:
|
|
2305
|
+
scalar = np.array(M, dtype=dtype)[()]
|
|
2306
|
+
|
|
2307
|
+
w = bartlett(scalar)
|
|
2308
|
+
if dtype == "O":
|
|
2309
|
+
ref_dtype = np.float64
|
|
2310
|
+
else:
|
|
2311
|
+
ref_dtype = np.result_type(scalar.dtype, np.float64)
|
|
2312
|
+
assert w.dtype == ref_dtype
|
|
2313
|
+
|
|
2314
|
+
# check symmetry
|
|
2315
|
+
assert_equal(w, flipud(w))
|
|
2316
|
+
|
|
2317
|
+
# check known value
|
|
2318
|
+
if scalar < 1:
|
|
2319
|
+
assert_array_equal(w, np.array([]))
|
|
2320
|
+
elif scalar == 1:
|
|
2321
|
+
assert_array_equal(w, np.ones(1))
|
|
2322
|
+
else:
|
|
2323
|
+
assert_almost_equal(np.sum(w, axis=0), 4.4444, 4)
|
|
2324
|
+
|
|
2325
|
+
def test_blackman(self, dtype: str, M: int) -> None:
|
|
2326
|
+
scalar = np.array(M, dtype=dtype)[()]
|
|
2327
|
+
|
|
2328
|
+
w = blackman(scalar)
|
|
2329
|
+
if dtype == "O":
|
|
2330
|
+
ref_dtype = np.float64
|
|
2331
|
+
else:
|
|
2332
|
+
ref_dtype = np.result_type(scalar.dtype, np.float64)
|
|
2333
|
+
assert w.dtype == ref_dtype
|
|
2334
|
+
|
|
2335
|
+
# check symmetry
|
|
2336
|
+
assert_equal(w, flipud(w))
|
|
2337
|
+
|
|
2338
|
+
# check known value
|
|
2339
|
+
if scalar < 1:
|
|
2340
|
+
assert_array_equal(w, np.array([]))
|
|
2341
|
+
elif scalar == 1:
|
|
2342
|
+
assert_array_equal(w, np.ones(1))
|
|
2343
|
+
else:
|
|
2344
|
+
assert_almost_equal(np.sum(w, axis=0), 3.7800, 4)
|
|
2345
|
+
|
|
2346
|
+
def test_kaiser(self, dtype: str, M: int) -> None:
|
|
2347
|
+
scalar = np.array(M, dtype=dtype)[()]
|
|
2348
|
+
|
|
2349
|
+
w = kaiser(scalar, 0)
|
|
2350
|
+
if dtype == "O":
|
|
2351
|
+
ref_dtype = np.float64
|
|
2352
|
+
else:
|
|
2353
|
+
ref_dtype = np.result_type(scalar.dtype, np.float64)
|
|
2354
|
+
assert w.dtype == ref_dtype
|
|
2355
|
+
|
|
2356
|
+
# check symmetry
|
|
2357
|
+
assert_equal(w, flipud(w))
|
|
2358
|
+
|
|
2359
|
+
# check known value
|
|
2360
|
+
if scalar < 1:
|
|
2361
|
+
assert_array_equal(w, np.array([]))
|
|
2362
|
+
elif scalar == 1:
|
|
2363
|
+
assert_array_equal(w, np.ones(1))
|
|
2364
|
+
else:
|
|
2365
|
+
assert_almost_equal(np.sum(w, axis=0), 10, 15)
|
|
2366
|
+
|
|
2367
|
+
|
|
2368
|
+
class TestTrapezoid:
|
|
2369
|
+
|
|
2370
|
+
def test_simple(self):
|
|
2371
|
+
x = np.arange(-10, 10, .1)
|
|
2372
|
+
r = trapezoid(np.exp(-.5 * x ** 2) / np.sqrt(2 * np.pi), dx=0.1)
|
|
2373
|
+
# check integral of normal equals 1
|
|
2374
|
+
assert_almost_equal(r, 1, 7)
|
|
2375
|
+
|
|
2376
|
+
def test_ndim(self):
|
|
2377
|
+
x = np.linspace(0, 1, 3)
|
|
2378
|
+
y = np.linspace(0, 2, 8)
|
|
2379
|
+
z = np.linspace(0, 3, 13)
|
|
2380
|
+
|
|
2381
|
+
wx = np.ones_like(x) * (x[1] - x[0])
|
|
2382
|
+
wx[0] /= 2
|
|
2383
|
+
wx[-1] /= 2
|
|
2384
|
+
wy = np.ones_like(y) * (y[1] - y[0])
|
|
2385
|
+
wy[0] /= 2
|
|
2386
|
+
wy[-1] /= 2
|
|
2387
|
+
wz = np.ones_like(z) * (z[1] - z[0])
|
|
2388
|
+
wz[0] /= 2
|
|
2389
|
+
wz[-1] /= 2
|
|
2390
|
+
|
|
2391
|
+
q = x[:, None, None] + y[None, :, None] + z[None, None, :]
|
|
2392
|
+
|
|
2393
|
+
qx = (q * wx[:, None, None]).sum(axis=0)
|
|
2394
|
+
qy = (q * wy[None, :, None]).sum(axis=1)
|
|
2395
|
+
qz = (q * wz[None, None, :]).sum(axis=2)
|
|
2396
|
+
|
|
2397
|
+
# n-d `x`
|
|
2398
|
+
r = trapezoid(q, x=x[:, None, None], axis=0)
|
|
2399
|
+
assert_almost_equal(r, qx)
|
|
2400
|
+
r = trapezoid(q, x=y[None, :, None], axis=1)
|
|
2401
|
+
assert_almost_equal(r, qy)
|
|
2402
|
+
r = trapezoid(q, x=z[None, None, :], axis=2)
|
|
2403
|
+
assert_almost_equal(r, qz)
|
|
2404
|
+
|
|
2405
|
+
# 1-d `x`
|
|
2406
|
+
r = trapezoid(q, x=x, axis=0)
|
|
2407
|
+
assert_almost_equal(r, qx)
|
|
2408
|
+
r = trapezoid(q, x=y, axis=1)
|
|
2409
|
+
assert_almost_equal(r, qy)
|
|
2410
|
+
r = trapezoid(q, x=z, axis=2)
|
|
2411
|
+
assert_almost_equal(r, qz)
|
|
2412
|
+
|
|
2413
|
+
def test_masked(self):
|
|
2414
|
+
# Testing that masked arrays behave as if the function is 0 where
|
|
2415
|
+
# masked
|
|
2416
|
+
x = np.arange(5)
|
|
2417
|
+
y = x * x
|
|
2418
|
+
mask = x == 2
|
|
2419
|
+
ym = np.ma.array(y, mask=mask)
|
|
2420
|
+
r = 13.0 # sum(0.5 * (0 + 1) * 1.0 + 0.5 * (9 + 16))
|
|
2421
|
+
assert_almost_equal(trapezoid(ym, x), r)
|
|
2422
|
+
|
|
2423
|
+
xm = np.ma.array(x, mask=mask)
|
|
2424
|
+
assert_almost_equal(trapezoid(ym, xm), r)
|
|
2425
|
+
|
|
2426
|
+
xm = np.ma.array(x, mask=mask)
|
|
2427
|
+
assert_almost_equal(trapezoid(y, xm), r)
|
|
2428
|
+
|
|
2429
|
+
|
|
2430
|
+
class TestSinc:
|
|
2431
|
+
|
|
2432
|
+
def test_simple(self):
|
|
2433
|
+
assert_(sinc(0) == 1)
|
|
2434
|
+
w = sinc(np.linspace(-1, 1, 100))
|
|
2435
|
+
# check symmetry
|
|
2436
|
+
assert_array_almost_equal(w, flipud(w), 7)
|
|
2437
|
+
|
|
2438
|
+
def test_array_like(self):
|
|
2439
|
+
x = [0, 0.5]
|
|
2440
|
+
y1 = sinc(np.array(x))
|
|
2441
|
+
y2 = sinc(list(x))
|
|
2442
|
+
y3 = sinc(tuple(x))
|
|
2443
|
+
assert_array_equal(y1, y2)
|
|
2444
|
+
assert_array_equal(y1, y3)
|
|
2445
|
+
|
|
2446
|
+
def test_bool_dtype(self):
|
|
2447
|
+
x = (np.arange(4, dtype=np.uint8) % 2 == 1)
|
|
2448
|
+
actual = sinc(x)
|
|
2449
|
+
expected = sinc(x.astype(np.float64))
|
|
2450
|
+
assert_allclose(actual, expected)
|
|
2451
|
+
assert actual.dtype == np.float64
|
|
2452
|
+
|
|
2453
|
+
@pytest.mark.parametrize('dtype', [np.uint8, np.int16, np.uint64])
|
|
2454
|
+
def test_int_dtypes(self, dtype):
|
|
2455
|
+
x = np.arange(4, dtype=dtype)
|
|
2456
|
+
actual = sinc(x)
|
|
2457
|
+
expected = sinc(x.astype(np.float64))
|
|
2458
|
+
assert_allclose(actual, expected)
|
|
2459
|
+
assert actual.dtype == np.float64
|
|
2460
|
+
|
|
2461
|
+
@pytest.mark.parametrize(
|
|
2462
|
+
'dtype',
|
|
2463
|
+
[np.float16, np.float32, np.longdouble, np.complex64, np.complex128]
|
|
2464
|
+
)
|
|
2465
|
+
def test_float_dtypes(self, dtype):
|
|
2466
|
+
x = np.arange(4, dtype=dtype)
|
|
2467
|
+
assert sinc(x).dtype == x.dtype
|
|
2468
|
+
|
|
2469
|
+
def test_float16_underflow(self):
|
|
2470
|
+
x = np.float16(0)
|
|
2471
|
+
# before gh-27784, fill value for 0 in input would underflow float16,
|
|
2472
|
+
# resulting in nan
|
|
2473
|
+
assert_array_equal(sinc(x), np.asarray(1.0))
|
|
2474
|
+
|
|
2475
|
+
|
|
2476
|
+
class TestUnique:
|
|
2477
|
+
|
|
2478
|
+
def test_simple(self):
|
|
2479
|
+
x = np.array([4, 3, 2, 1, 1, 2, 3, 4, 0])
|
|
2480
|
+
assert_(np.all(unique(x) == [0, 1, 2, 3, 4]))
|
|
2481
|
+
assert_(unique(np.array([1, 1, 1, 1, 1])) == np.array([1]))
|
|
2482
|
+
x = ['widget', 'ham', 'foo', 'bar', 'foo', 'ham']
|
|
2483
|
+
assert_(np.all(unique(x) == ['bar', 'foo', 'ham', 'widget']))
|
|
2484
|
+
x = np.array([5 + 6j, 1 + 1j, 1 + 10j, 10, 5 + 6j])
|
|
2485
|
+
assert_(np.all(unique(x) == [1 + 1j, 1 + 10j, 5 + 6j, 10]))
|
|
2486
|
+
|
|
2487
|
+
|
|
2488
|
+
class TestCheckFinite:
|
|
2489
|
+
|
|
2490
|
+
def test_simple(self):
|
|
2491
|
+
a = [1, 2, 3]
|
|
2492
|
+
b = [1, 2, np.inf]
|
|
2493
|
+
c = [1, 2, np.nan]
|
|
2494
|
+
np.asarray_chkfinite(a)
|
|
2495
|
+
assert_raises(ValueError, np.asarray_chkfinite, b)
|
|
2496
|
+
assert_raises(ValueError, np.asarray_chkfinite, c)
|
|
2497
|
+
|
|
2498
|
+
def test_dtype_order(self):
|
|
2499
|
+
# Regression test for missing dtype and order arguments
|
|
2500
|
+
a = [1, 2, 3]
|
|
2501
|
+
a = np.asarray_chkfinite(a, order='F', dtype=np.float64)
|
|
2502
|
+
assert_(a.dtype == np.float64)
|
|
2503
|
+
|
|
2504
|
+
|
|
2505
|
+
class TestCorrCoef:
|
|
2506
|
+
A = np.array(
|
|
2507
|
+
[[0.15391142, 0.18045767, 0.14197213],
|
|
2508
|
+
[0.70461506, 0.96474128, 0.27906989],
|
|
2509
|
+
[0.9297531, 0.32296769, 0.19267156]])
|
|
2510
|
+
B = np.array(
|
|
2511
|
+
[[0.10377691, 0.5417086, 0.49807457],
|
|
2512
|
+
[0.82872117, 0.77801674, 0.39226705],
|
|
2513
|
+
[0.9314666, 0.66800209, 0.03538394]])
|
|
2514
|
+
res1 = np.array(
|
|
2515
|
+
[[1., 0.9379533, -0.04931983],
|
|
2516
|
+
[0.9379533, 1., 0.30007991],
|
|
2517
|
+
[-0.04931983, 0.30007991, 1.]])
|
|
2518
|
+
res2 = np.array(
|
|
2519
|
+
[[1., 0.9379533, -0.04931983, 0.30151751, 0.66318558, 0.51532523],
|
|
2520
|
+
[0.9379533, 1., 0.30007991, -0.04781421, 0.88157256, 0.78052386],
|
|
2521
|
+
[-0.04931983, 0.30007991, 1., -0.96717111, 0.71483595, 0.83053601],
|
|
2522
|
+
[0.30151751, -0.04781421, -0.96717111, 1., -0.51366032, -0.66173113],
|
|
2523
|
+
[0.66318558, 0.88157256, 0.71483595, -0.51366032, 1., 0.98317823],
|
|
2524
|
+
[0.51532523, 0.78052386, 0.83053601, -0.66173113, 0.98317823, 1.]])
|
|
2525
|
+
|
|
2526
|
+
def test_non_array(self):
|
|
2527
|
+
assert_almost_equal(np.corrcoef([0, 1, 0], [1, 0, 1]),
|
|
2528
|
+
[[1., -1.], [-1., 1.]])
|
|
2529
|
+
|
|
2530
|
+
def test_simple(self):
|
|
2531
|
+
tgt1 = corrcoef(self.A)
|
|
2532
|
+
assert_almost_equal(tgt1, self.res1)
|
|
2533
|
+
assert_(np.all(np.abs(tgt1) <= 1.0))
|
|
2534
|
+
|
|
2535
|
+
tgt2 = corrcoef(self.A, self.B)
|
|
2536
|
+
assert_almost_equal(tgt2, self.res2)
|
|
2537
|
+
assert_(np.all(np.abs(tgt2) <= 1.0))
|
|
2538
|
+
|
|
2539
|
+
def test_complex(self):
|
|
2540
|
+
x = np.array([[1, 2, 3], [1j, 2j, 3j]])
|
|
2541
|
+
res = corrcoef(x)
|
|
2542
|
+
tgt = np.array([[1., -1.j], [1.j, 1.]])
|
|
2543
|
+
assert_allclose(res, tgt)
|
|
2544
|
+
assert_(np.all(np.abs(res) <= 1.0))
|
|
2545
|
+
|
|
2546
|
+
def test_xy(self):
|
|
2547
|
+
x = np.array([[1, 2, 3]])
|
|
2548
|
+
y = np.array([[1j, 2j, 3j]])
|
|
2549
|
+
assert_allclose(np.corrcoef(x, y), np.array([[1., -1.j], [1.j, 1.]]))
|
|
2550
|
+
|
|
2551
|
+
def test_empty(self):
|
|
2552
|
+
with warnings.catch_warnings(record=True):
|
|
2553
|
+
warnings.simplefilter('always', RuntimeWarning)
|
|
2554
|
+
assert_array_equal(corrcoef(np.array([])), np.nan)
|
|
2555
|
+
assert_array_equal(corrcoef(np.array([]).reshape(0, 2)),
|
|
2556
|
+
np.array([]).reshape(0, 0))
|
|
2557
|
+
assert_array_equal(corrcoef(np.array([]).reshape(2, 0)),
|
|
2558
|
+
np.array([[np.nan, np.nan], [np.nan, np.nan]]))
|
|
2559
|
+
|
|
2560
|
+
def test_extreme(self):
|
|
2561
|
+
x = [[1e-100, 1e100], [1e100, 1e-100]]
|
|
2562
|
+
with np.errstate(all='raise'):
|
|
2563
|
+
c = corrcoef(x)
|
|
2564
|
+
assert_array_almost_equal(c, np.array([[1., -1.], [-1., 1.]]))
|
|
2565
|
+
assert_(np.all(np.abs(c) <= 1.0))
|
|
2566
|
+
|
|
2567
|
+
@pytest.mark.parametrize("test_type", np_floats)
|
|
2568
|
+
def test_corrcoef_dtype(self, test_type):
|
|
2569
|
+
cast_A = self.A.astype(test_type)
|
|
2570
|
+
res = corrcoef(cast_A, dtype=test_type)
|
|
2571
|
+
assert test_type == res.dtype
|
|
2572
|
+
|
|
2573
|
+
|
|
2574
|
+
class TestCov:
|
|
2575
|
+
x1 = np.array([[0, 2], [1, 1], [2, 0]]).T
|
|
2576
|
+
res1 = np.array([[1., -1.], [-1., 1.]])
|
|
2577
|
+
x2 = np.array([0.0, 1.0, 2.0], ndmin=2)
|
|
2578
|
+
frequencies = np.array([1, 4, 1])
|
|
2579
|
+
x2_repeats = np.array([[0.0], [1.0], [1.0], [1.0], [1.0], [2.0]]).T
|
|
2580
|
+
res2 = np.array([[0.4, -0.4], [-0.4, 0.4]])
|
|
2581
|
+
unit_frequencies = np.ones(3, dtype=np.int_)
|
|
2582
|
+
weights = np.array([1.0, 4.0, 1.0])
|
|
2583
|
+
res3 = np.array([[2. / 3., -2. / 3.], [-2. / 3., 2. / 3.]])
|
|
2584
|
+
unit_weights = np.ones(3)
|
|
2585
|
+
x3 = np.array([0.3942, 0.5969, 0.7730, 0.9918, 0.7964])
|
|
2586
|
+
|
|
2587
|
+
def test_basic(self):
|
|
2588
|
+
assert_allclose(cov(self.x1), self.res1)
|
|
2589
|
+
|
|
2590
|
+
def test_complex(self):
|
|
2591
|
+
x = np.array([[1, 2, 3], [1j, 2j, 3j]])
|
|
2592
|
+
res = np.array([[1., -1.j], [1.j, 1.]])
|
|
2593
|
+
assert_allclose(cov(x), res)
|
|
2594
|
+
assert_allclose(cov(x, aweights=np.ones(3)), res)
|
|
2595
|
+
|
|
2596
|
+
def test_xy(self):
|
|
2597
|
+
x = np.array([[1, 2, 3]])
|
|
2598
|
+
y = np.array([[1j, 2j, 3j]])
|
|
2599
|
+
assert_allclose(cov(x, y), np.array([[1., -1.j], [1.j, 1.]]))
|
|
2600
|
+
|
|
2601
|
+
def test_empty(self):
|
|
2602
|
+
with warnings.catch_warnings(record=True):
|
|
2603
|
+
warnings.simplefilter('always', RuntimeWarning)
|
|
2604
|
+
assert_array_equal(cov(np.array([])), np.nan)
|
|
2605
|
+
assert_array_equal(cov(np.array([]).reshape(0, 2)),
|
|
2606
|
+
np.array([]).reshape(0, 0))
|
|
2607
|
+
assert_array_equal(cov(np.array([]).reshape(2, 0)),
|
|
2608
|
+
np.array([[np.nan, np.nan], [np.nan, np.nan]]))
|
|
2609
|
+
|
|
2610
|
+
def test_wrong_ddof(self):
|
|
2611
|
+
with warnings.catch_warnings(record=True):
|
|
2612
|
+
warnings.simplefilter('always', RuntimeWarning)
|
|
2613
|
+
assert_array_equal(cov(self.x1, ddof=5),
|
|
2614
|
+
np.array([[np.inf, -np.inf],
|
|
2615
|
+
[-np.inf, np.inf]]))
|
|
2616
|
+
|
|
2617
|
+
def test_1D_rowvar(self):
|
|
2618
|
+
assert_allclose(cov(self.x3), cov(self.x3, rowvar=False))
|
|
2619
|
+
y = np.array([0.0780, 0.3107, 0.2111, 0.0334, 0.8501])
|
|
2620
|
+
assert_allclose(cov(self.x3, y), cov(self.x3, y, rowvar=False))
|
|
2621
|
+
|
|
2622
|
+
def test_1D_variance(self):
|
|
2623
|
+
assert_allclose(cov(self.x3, ddof=1), np.var(self.x3, ddof=1))
|
|
2624
|
+
|
|
2625
|
+
def test_fweights(self):
|
|
2626
|
+
assert_allclose(cov(self.x2, fweights=self.frequencies),
|
|
2627
|
+
cov(self.x2_repeats))
|
|
2628
|
+
assert_allclose(cov(self.x1, fweights=self.frequencies),
|
|
2629
|
+
self.res2)
|
|
2630
|
+
assert_allclose(cov(self.x1, fweights=self.unit_frequencies),
|
|
2631
|
+
self.res1)
|
|
2632
|
+
nonint = self.frequencies + 0.5
|
|
2633
|
+
assert_raises(TypeError, cov, self.x1, fweights=nonint)
|
|
2634
|
+
f = np.ones((2, 3), dtype=np.int_)
|
|
2635
|
+
assert_raises(RuntimeError, cov, self.x1, fweights=f)
|
|
2636
|
+
f = np.ones(2, dtype=np.int_)
|
|
2637
|
+
assert_raises(RuntimeError, cov, self.x1, fweights=f)
|
|
2638
|
+
f = -1 * np.ones(3, dtype=np.int_)
|
|
2639
|
+
assert_raises(ValueError, cov, self.x1, fweights=f)
|
|
2640
|
+
|
|
2641
|
+
def test_aweights(self):
|
|
2642
|
+
assert_allclose(cov(self.x1, aweights=self.weights), self.res3)
|
|
2643
|
+
assert_allclose(cov(self.x1, aweights=3.0 * self.weights),
|
|
2644
|
+
cov(self.x1, aweights=self.weights))
|
|
2645
|
+
assert_allclose(cov(self.x1, aweights=self.unit_weights), self.res1)
|
|
2646
|
+
w = np.ones((2, 3))
|
|
2647
|
+
assert_raises(RuntimeError, cov, self.x1, aweights=w)
|
|
2648
|
+
w = np.ones(2)
|
|
2649
|
+
assert_raises(RuntimeError, cov, self.x1, aweights=w)
|
|
2650
|
+
w = -1.0 * np.ones(3)
|
|
2651
|
+
assert_raises(ValueError, cov, self.x1, aweights=w)
|
|
2652
|
+
|
|
2653
|
+
def test_unit_fweights_and_aweights(self):
|
|
2654
|
+
assert_allclose(cov(self.x2, fweights=self.frequencies,
|
|
2655
|
+
aweights=self.unit_weights),
|
|
2656
|
+
cov(self.x2_repeats))
|
|
2657
|
+
assert_allclose(cov(self.x1, fweights=self.frequencies,
|
|
2658
|
+
aweights=self.unit_weights),
|
|
2659
|
+
self.res2)
|
|
2660
|
+
assert_allclose(cov(self.x1, fweights=self.unit_frequencies,
|
|
2661
|
+
aweights=self.unit_weights),
|
|
2662
|
+
self.res1)
|
|
2663
|
+
assert_allclose(cov(self.x1, fweights=self.unit_frequencies,
|
|
2664
|
+
aweights=self.weights),
|
|
2665
|
+
self.res3)
|
|
2666
|
+
assert_allclose(cov(self.x1, fweights=self.unit_frequencies,
|
|
2667
|
+
aweights=3.0 * self.weights),
|
|
2668
|
+
cov(self.x1, aweights=self.weights))
|
|
2669
|
+
assert_allclose(cov(self.x1, fweights=self.unit_frequencies,
|
|
2670
|
+
aweights=self.unit_weights),
|
|
2671
|
+
self.res1)
|
|
2672
|
+
|
|
2673
|
+
@pytest.mark.parametrize("test_type", np_floats)
|
|
2674
|
+
def test_cov_dtype(self, test_type):
|
|
2675
|
+
cast_x1 = self.x1.astype(test_type)
|
|
2676
|
+
res = cov(cast_x1, dtype=test_type)
|
|
2677
|
+
assert test_type == res.dtype
|
|
2678
|
+
|
|
2679
|
+
def test_gh_27658(self):
|
|
2680
|
+
x = np.ones((3, 1))
|
|
2681
|
+
expected = np.cov(x, ddof=0, rowvar=True)
|
|
2682
|
+
actual = np.cov(x.T, ddof=0, rowvar=False)
|
|
2683
|
+
assert_allclose(actual, expected, strict=True)
|
|
2684
|
+
|
|
2685
|
+
|
|
2686
|
+
class Test_I0:
|
|
2687
|
+
|
|
2688
|
+
def test_simple(self):
|
|
2689
|
+
assert_almost_equal(
|
|
2690
|
+
i0(0.5),
|
|
2691
|
+
np.array(1.0634833707413234))
|
|
2692
|
+
|
|
2693
|
+
# need at least one test above 8, as the implementation is piecewise
|
|
2694
|
+
A = np.array([0.49842636, 0.6969809, 0.22011976, 0.0155549, 10.0])
|
|
2695
|
+
expected = np.array([1.06307822, 1.12518299, 1.01214991,
|
|
2696
|
+
1.00006049, 2815.71662847])
|
|
2697
|
+
assert_almost_equal(i0(A), expected)
|
|
2698
|
+
assert_almost_equal(i0(-A), expected)
|
|
2699
|
+
|
|
2700
|
+
B = np.array([[0.827002, 0.99959078],
|
|
2701
|
+
[0.89694769, 0.39298162],
|
|
2702
|
+
[0.37954418, 0.05206293],
|
|
2703
|
+
[0.36465447, 0.72446427],
|
|
2704
|
+
[0.48164949, 0.50324519]])
|
|
2705
|
+
assert_almost_equal(
|
|
2706
|
+
i0(B),
|
|
2707
|
+
np.array([[1.17843223, 1.26583466],
|
|
2708
|
+
[1.21147086, 1.03898290],
|
|
2709
|
+
[1.03633899, 1.00067775],
|
|
2710
|
+
[1.03352052, 1.13557954],
|
|
2711
|
+
[1.05884290, 1.06432317]]))
|
|
2712
|
+
# Regression test for gh-11205
|
|
2713
|
+
i0_0 = np.i0([0.])
|
|
2714
|
+
assert_equal(i0_0.shape, (1,))
|
|
2715
|
+
assert_array_equal(np.i0([0.]), np.array([1.]))
|
|
2716
|
+
|
|
2717
|
+
def test_non_array(self):
|
|
2718
|
+
a = np.arange(4)
|
|
2719
|
+
|
|
2720
|
+
class array_like:
|
|
2721
|
+
__array_interface__ = a.__array_interface__
|
|
2722
|
+
|
|
2723
|
+
def __array_wrap__(self, arr, context, return_scalar):
|
|
2724
|
+
return self
|
|
2725
|
+
|
|
2726
|
+
# E.g. pandas series survive ufunc calls through array-wrap:
|
|
2727
|
+
assert isinstance(np.abs(array_like()), array_like)
|
|
2728
|
+
exp = np.i0(a)
|
|
2729
|
+
res = np.i0(array_like())
|
|
2730
|
+
|
|
2731
|
+
assert_array_equal(exp, res)
|
|
2732
|
+
|
|
2733
|
+
def test_complex(self):
|
|
2734
|
+
a = np.array([0, 1 + 2j])
|
|
2735
|
+
with pytest.raises(TypeError, match="i0 not supported for complex values"):
|
|
2736
|
+
res = i0(a)
|
|
2737
|
+
|
|
2738
|
+
|
|
2739
|
+
class TestKaiser:
|
|
2740
|
+
|
|
2741
|
+
def test_simple(self):
|
|
2742
|
+
assert_(np.isfinite(kaiser(1, 1.0)))
|
|
2743
|
+
assert_almost_equal(kaiser(0, 1.0),
|
|
2744
|
+
np.array([]))
|
|
2745
|
+
assert_almost_equal(kaiser(2, 1.0),
|
|
2746
|
+
np.array([0.78984831, 0.78984831]))
|
|
2747
|
+
assert_almost_equal(kaiser(5, 1.0),
|
|
2748
|
+
np.array([0.78984831, 0.94503323, 1.,
|
|
2749
|
+
0.94503323, 0.78984831]))
|
|
2750
|
+
assert_almost_equal(kaiser(5, 1.56789),
|
|
2751
|
+
np.array([0.58285404, 0.88409679, 1.,
|
|
2752
|
+
0.88409679, 0.58285404]))
|
|
2753
|
+
|
|
2754
|
+
def test_int_beta(self):
|
|
2755
|
+
kaiser(3, 4)
|
|
2756
|
+
|
|
2757
|
+
|
|
2758
|
+
class TestMeshgrid:
|
|
2759
|
+
|
|
2760
|
+
def test_simple(self):
|
|
2761
|
+
[X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7])
|
|
2762
|
+
assert_array_equal(X, np.array([[1, 2, 3],
|
|
2763
|
+
[1, 2, 3],
|
|
2764
|
+
[1, 2, 3],
|
|
2765
|
+
[1, 2, 3]]))
|
|
2766
|
+
assert_array_equal(Y, np.array([[4, 4, 4],
|
|
2767
|
+
[5, 5, 5],
|
|
2768
|
+
[6, 6, 6],
|
|
2769
|
+
[7, 7, 7]]))
|
|
2770
|
+
|
|
2771
|
+
def test_single_input(self):
|
|
2772
|
+
[X] = meshgrid([1, 2, 3, 4])
|
|
2773
|
+
assert_array_equal(X, np.array([1, 2, 3, 4]))
|
|
2774
|
+
|
|
2775
|
+
def test_no_input(self):
|
|
2776
|
+
args = []
|
|
2777
|
+
assert_array_equal([], meshgrid(*args))
|
|
2778
|
+
assert_array_equal([], meshgrid(*args, copy=False))
|
|
2779
|
+
|
|
2780
|
+
def test_indexing(self):
|
|
2781
|
+
x = [1, 2, 3]
|
|
2782
|
+
y = [4, 5, 6, 7]
|
|
2783
|
+
[X, Y] = meshgrid(x, y, indexing='ij')
|
|
2784
|
+
assert_array_equal(X, np.array([[1, 1, 1, 1],
|
|
2785
|
+
[2, 2, 2, 2],
|
|
2786
|
+
[3, 3, 3, 3]]))
|
|
2787
|
+
assert_array_equal(Y, np.array([[4, 5, 6, 7],
|
|
2788
|
+
[4, 5, 6, 7],
|
|
2789
|
+
[4, 5, 6, 7]]))
|
|
2790
|
+
|
|
2791
|
+
# Test expected shapes:
|
|
2792
|
+
z = [8, 9]
|
|
2793
|
+
assert_(meshgrid(x, y)[0].shape == (4, 3))
|
|
2794
|
+
assert_(meshgrid(x, y, indexing='ij')[0].shape == (3, 4))
|
|
2795
|
+
assert_(meshgrid(x, y, z)[0].shape == (4, 3, 2))
|
|
2796
|
+
assert_(meshgrid(x, y, z, indexing='ij')[0].shape == (3, 4, 2))
|
|
2797
|
+
|
|
2798
|
+
assert_raises(ValueError, meshgrid, x, y, indexing='notvalid')
|
|
2799
|
+
|
|
2800
|
+
def test_sparse(self):
|
|
2801
|
+
[X, Y] = meshgrid([1, 2, 3], [4, 5, 6, 7], sparse=True)
|
|
2802
|
+
assert_array_equal(X, np.array([[1, 2, 3]]))
|
|
2803
|
+
assert_array_equal(Y, np.array([[4], [5], [6], [7]]))
|
|
2804
|
+
|
|
2805
|
+
def test_invalid_arguments(self):
|
|
2806
|
+
# Test that meshgrid complains about invalid arguments
|
|
2807
|
+
# Regression test for issue #4755:
|
|
2808
|
+
# https://github.com/numpy/numpy/issues/4755
|
|
2809
|
+
assert_raises(TypeError, meshgrid,
|
|
2810
|
+
[1, 2, 3], [4, 5, 6, 7], indices='ij')
|
|
2811
|
+
|
|
2812
|
+
def test_return_type(self):
|
|
2813
|
+
# Test for appropriate dtype in returned arrays.
|
|
2814
|
+
# Regression test for issue #5297
|
|
2815
|
+
# https://github.com/numpy/numpy/issues/5297
|
|
2816
|
+
x = np.arange(0, 10, dtype=np.float32)
|
|
2817
|
+
y = np.arange(10, 20, dtype=np.float64)
|
|
2818
|
+
|
|
2819
|
+
X, Y = np.meshgrid(x, y)
|
|
2820
|
+
|
|
2821
|
+
assert_(X.dtype == x.dtype)
|
|
2822
|
+
assert_(Y.dtype == y.dtype)
|
|
2823
|
+
|
|
2824
|
+
# copy
|
|
2825
|
+
X, Y = np.meshgrid(x, y, copy=True)
|
|
2826
|
+
|
|
2827
|
+
assert_(X.dtype == x.dtype)
|
|
2828
|
+
assert_(Y.dtype == y.dtype)
|
|
2829
|
+
|
|
2830
|
+
# sparse
|
|
2831
|
+
X, Y = np.meshgrid(x, y, sparse=True)
|
|
2832
|
+
|
|
2833
|
+
assert_(X.dtype == x.dtype)
|
|
2834
|
+
assert_(Y.dtype == y.dtype)
|
|
2835
|
+
|
|
2836
|
+
def test_writeback(self):
|
|
2837
|
+
# Issue 8561
|
|
2838
|
+
X = np.array([1.1, 2.2])
|
|
2839
|
+
Y = np.array([3.3, 4.4])
|
|
2840
|
+
x, y = np.meshgrid(X, Y, sparse=False, copy=True)
|
|
2841
|
+
|
|
2842
|
+
x[0, :] = 0
|
|
2843
|
+
assert_equal(x[0, :], 0)
|
|
2844
|
+
assert_equal(x[1, :], X)
|
|
2845
|
+
|
|
2846
|
+
def test_nd_shape(self):
|
|
2847
|
+
a, b, c, d, e = np.meshgrid(*([0] * i for i in range(1, 6)))
|
|
2848
|
+
expected_shape = (2, 1, 3, 4, 5)
|
|
2849
|
+
assert_equal(a.shape, expected_shape)
|
|
2850
|
+
assert_equal(b.shape, expected_shape)
|
|
2851
|
+
assert_equal(c.shape, expected_shape)
|
|
2852
|
+
assert_equal(d.shape, expected_shape)
|
|
2853
|
+
assert_equal(e.shape, expected_shape)
|
|
2854
|
+
|
|
2855
|
+
def test_nd_values(self):
|
|
2856
|
+
a, b, c = np.meshgrid([0], [1, 2], [3, 4, 5])
|
|
2857
|
+
assert_equal(a, [[[0, 0, 0]], [[0, 0, 0]]])
|
|
2858
|
+
assert_equal(b, [[[1, 1, 1]], [[2, 2, 2]]])
|
|
2859
|
+
assert_equal(c, [[[3, 4, 5]], [[3, 4, 5]]])
|
|
2860
|
+
|
|
2861
|
+
def test_nd_indexing(self):
|
|
2862
|
+
a, b, c = np.meshgrid([0], [1, 2], [3, 4, 5], indexing='ij')
|
|
2863
|
+
assert_equal(a, [[[0, 0, 0], [0, 0, 0]]])
|
|
2864
|
+
assert_equal(b, [[[1, 1, 1], [2, 2, 2]]])
|
|
2865
|
+
assert_equal(c, [[[3, 4, 5], [3, 4, 5]]])
|
|
2866
|
+
|
|
2867
|
+
|
|
2868
|
+
class TestPiecewise:
|
|
2869
|
+
|
|
2870
|
+
def test_simple(self):
|
|
2871
|
+
# Condition is single bool list
|
|
2872
|
+
x = piecewise([0, 0], [True, False], [1])
|
|
2873
|
+
assert_array_equal(x, [1, 0])
|
|
2874
|
+
|
|
2875
|
+
# List of conditions: single bool list
|
|
2876
|
+
x = piecewise([0, 0], [[True, False]], [1])
|
|
2877
|
+
assert_array_equal(x, [1, 0])
|
|
2878
|
+
|
|
2879
|
+
# Conditions is single bool array
|
|
2880
|
+
x = piecewise([0, 0], np.array([True, False]), [1])
|
|
2881
|
+
assert_array_equal(x, [1, 0])
|
|
2882
|
+
|
|
2883
|
+
# Condition is single int array
|
|
2884
|
+
x = piecewise([0, 0], np.array([1, 0]), [1])
|
|
2885
|
+
assert_array_equal(x, [1, 0])
|
|
2886
|
+
|
|
2887
|
+
# List of conditions: int array
|
|
2888
|
+
x = piecewise([0, 0], [np.array([1, 0])], [1])
|
|
2889
|
+
assert_array_equal(x, [1, 0])
|
|
2890
|
+
|
|
2891
|
+
x = piecewise([0, 0], [[False, True]], [lambda x:-1])
|
|
2892
|
+
assert_array_equal(x, [0, -1])
|
|
2893
|
+
|
|
2894
|
+
assert_raises_regex(ValueError, '1 or 2 functions are expected',
|
|
2895
|
+
piecewise, [0, 0], [[False, True]], [])
|
|
2896
|
+
assert_raises_regex(ValueError, '1 or 2 functions are expected',
|
|
2897
|
+
piecewise, [0, 0], [[False, True]], [1, 2, 3])
|
|
2898
|
+
|
|
2899
|
+
def test_two_conditions(self):
|
|
2900
|
+
x = piecewise([1, 2], [[True, False], [False, True]], [3, 4])
|
|
2901
|
+
assert_array_equal(x, [3, 4])
|
|
2902
|
+
|
|
2903
|
+
def test_scalar_domains_three_conditions(self):
|
|
2904
|
+
x = piecewise(3, [True, False, False], [4, 2, 0])
|
|
2905
|
+
assert_equal(x, 4)
|
|
2906
|
+
|
|
2907
|
+
def test_default(self):
|
|
2908
|
+
# No value specified for x[1], should be 0
|
|
2909
|
+
x = piecewise([1, 2], [True, False], [2])
|
|
2910
|
+
assert_array_equal(x, [2, 0])
|
|
2911
|
+
|
|
2912
|
+
# Should set x[1] to 3
|
|
2913
|
+
x = piecewise([1, 2], [True, False], [2, 3])
|
|
2914
|
+
assert_array_equal(x, [2, 3])
|
|
2915
|
+
|
|
2916
|
+
def test_0d(self):
|
|
2917
|
+
x = np.array(3)
|
|
2918
|
+
y = piecewise(x, x > 3, [4, 0])
|
|
2919
|
+
assert_(y.ndim == 0)
|
|
2920
|
+
assert_(y == 0)
|
|
2921
|
+
|
|
2922
|
+
x = 5
|
|
2923
|
+
y = piecewise(x, [True, False], [1, 0])
|
|
2924
|
+
assert_(y.ndim == 0)
|
|
2925
|
+
assert_(y == 1)
|
|
2926
|
+
|
|
2927
|
+
# With 3 ranges (It was failing, before)
|
|
2928
|
+
y = piecewise(x, [False, False, True], [1, 2, 3])
|
|
2929
|
+
assert_array_equal(y, 3)
|
|
2930
|
+
|
|
2931
|
+
def test_0d_comparison(self):
|
|
2932
|
+
x = 3
|
|
2933
|
+
y = piecewise(x, [x <= 3, x > 3], [4, 0]) # Should succeed.
|
|
2934
|
+
assert_equal(y, 4)
|
|
2935
|
+
|
|
2936
|
+
# With 3 ranges (It was failing, before)
|
|
2937
|
+
x = 4
|
|
2938
|
+
y = piecewise(x, [x <= 3, (x > 3) * (x <= 5), x > 5], [1, 2, 3])
|
|
2939
|
+
assert_array_equal(y, 2)
|
|
2940
|
+
|
|
2941
|
+
assert_raises_regex(ValueError, '2 or 3 functions are expected',
|
|
2942
|
+
piecewise, x, [x <= 3, x > 3], [1])
|
|
2943
|
+
assert_raises_regex(ValueError, '2 or 3 functions are expected',
|
|
2944
|
+
piecewise, x, [x <= 3, x > 3], [1, 1, 1, 1])
|
|
2945
|
+
|
|
2946
|
+
def test_0d_0d_condition(self):
|
|
2947
|
+
x = np.array(3)
|
|
2948
|
+
c = np.array(x > 3)
|
|
2949
|
+
y = piecewise(x, [c], [1, 2])
|
|
2950
|
+
assert_equal(y, 2)
|
|
2951
|
+
|
|
2952
|
+
def test_multidimensional_extrafunc(self):
|
|
2953
|
+
x = np.array([[-2.5, -1.5, -0.5],
|
|
2954
|
+
[0.5, 1.5, 2.5]])
|
|
2955
|
+
y = piecewise(x, [x < 0, x >= 2], [-1, 1, 3])
|
|
2956
|
+
assert_array_equal(y, np.array([[-1., -1., -1.],
|
|
2957
|
+
[3., 3., 1.]]))
|
|
2958
|
+
|
|
2959
|
+
def test_subclasses(self):
|
|
2960
|
+
class subclass(np.ndarray):
|
|
2961
|
+
pass
|
|
2962
|
+
x = np.arange(5.).view(subclass)
|
|
2963
|
+
r = piecewise(x, [x < 2., x >= 4], [-1., 1., 0.])
|
|
2964
|
+
assert_equal(type(r), subclass)
|
|
2965
|
+
assert_equal(r, [-1., -1., 0., 0., 1.])
|
|
2966
|
+
|
|
2967
|
+
|
|
2968
|
+
class TestBincount:
|
|
2969
|
+
|
|
2970
|
+
def test_simple(self):
|
|
2971
|
+
y = np.bincount(np.arange(4))
|
|
2972
|
+
assert_array_equal(y, np.ones(4))
|
|
2973
|
+
|
|
2974
|
+
def test_simple2(self):
|
|
2975
|
+
y = np.bincount(np.array([1, 5, 2, 4, 1]))
|
|
2976
|
+
assert_array_equal(y, np.array([0, 2, 1, 0, 1, 1]))
|
|
2977
|
+
|
|
2978
|
+
def test_simple_weight(self):
|
|
2979
|
+
x = np.arange(4)
|
|
2980
|
+
w = np.array([0.2, 0.3, 0.5, 0.1])
|
|
2981
|
+
y = np.bincount(x, w)
|
|
2982
|
+
assert_array_equal(y, w)
|
|
2983
|
+
|
|
2984
|
+
def test_simple_weight2(self):
|
|
2985
|
+
x = np.array([1, 2, 4, 5, 2])
|
|
2986
|
+
w = np.array([0.2, 0.3, 0.5, 0.1, 0.2])
|
|
2987
|
+
y = np.bincount(x, w)
|
|
2988
|
+
assert_array_equal(y, np.array([0, 0.2, 0.5, 0, 0.5, 0.1]))
|
|
2989
|
+
|
|
2990
|
+
def test_with_minlength(self):
|
|
2991
|
+
x = np.array([0, 1, 0, 1, 1])
|
|
2992
|
+
y = np.bincount(x, minlength=3)
|
|
2993
|
+
assert_array_equal(y, np.array([2, 3, 0]))
|
|
2994
|
+
x = []
|
|
2995
|
+
y = np.bincount(x, minlength=0)
|
|
2996
|
+
assert_array_equal(y, np.array([]))
|
|
2997
|
+
|
|
2998
|
+
def test_with_minlength_smaller_than_maxvalue(self):
|
|
2999
|
+
x = np.array([0, 1, 1, 2, 2, 3, 3])
|
|
3000
|
+
y = np.bincount(x, minlength=2)
|
|
3001
|
+
assert_array_equal(y, np.array([1, 2, 2, 2]))
|
|
3002
|
+
y = np.bincount(x, minlength=0)
|
|
3003
|
+
assert_array_equal(y, np.array([1, 2, 2, 2]))
|
|
3004
|
+
|
|
3005
|
+
def test_with_minlength_and_weights(self):
|
|
3006
|
+
x = np.array([1, 2, 4, 5, 2])
|
|
3007
|
+
w = np.array([0.2, 0.3, 0.5, 0.1, 0.2])
|
|
3008
|
+
y = np.bincount(x, w, 8)
|
|
3009
|
+
assert_array_equal(y, np.array([0, 0.2, 0.5, 0, 0.5, 0.1, 0, 0]))
|
|
3010
|
+
|
|
3011
|
+
def test_empty(self):
|
|
3012
|
+
x = np.array([], dtype=int)
|
|
3013
|
+
y = np.bincount(x)
|
|
3014
|
+
assert_array_equal(x, y)
|
|
3015
|
+
|
|
3016
|
+
def test_empty_with_minlength(self):
|
|
3017
|
+
x = np.array([], dtype=int)
|
|
3018
|
+
y = np.bincount(x, minlength=5)
|
|
3019
|
+
assert_array_equal(y, np.zeros(5, dtype=int))
|
|
3020
|
+
|
|
3021
|
+
@pytest.mark.parametrize('minlength', [0, 3])
|
|
3022
|
+
def test_empty_list(self, minlength):
|
|
3023
|
+
assert_array_equal(np.bincount([], minlength=minlength),
|
|
3024
|
+
np.zeros(minlength, dtype=int))
|
|
3025
|
+
|
|
3026
|
+
def test_with_incorrect_minlength(self):
|
|
3027
|
+
x = np.array([], dtype=int)
|
|
3028
|
+
assert_raises_regex(TypeError,
|
|
3029
|
+
"'str' object cannot be interpreted",
|
|
3030
|
+
lambda: np.bincount(x, minlength="foobar"))
|
|
3031
|
+
assert_raises_regex(ValueError,
|
|
3032
|
+
"must not be negative",
|
|
3033
|
+
lambda: np.bincount(x, minlength=-1))
|
|
3034
|
+
|
|
3035
|
+
x = np.arange(5)
|
|
3036
|
+
assert_raises_regex(TypeError,
|
|
3037
|
+
"'str' object cannot be interpreted",
|
|
3038
|
+
lambda: np.bincount(x, minlength="foobar"))
|
|
3039
|
+
assert_raises_regex(ValueError,
|
|
3040
|
+
"must not be negative",
|
|
3041
|
+
lambda: np.bincount(x, minlength=-1))
|
|
3042
|
+
|
|
3043
|
+
@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
|
|
3044
|
+
def test_dtype_reference_leaks(self):
|
|
3045
|
+
# gh-6805
|
|
3046
|
+
intp_refcount = sys.getrefcount(np.dtype(np.intp))
|
|
3047
|
+
double_refcount = sys.getrefcount(np.dtype(np.double))
|
|
3048
|
+
|
|
3049
|
+
for j in range(10):
|
|
3050
|
+
np.bincount([1, 2, 3])
|
|
3051
|
+
assert_equal(sys.getrefcount(np.dtype(np.intp)), intp_refcount)
|
|
3052
|
+
assert_equal(sys.getrefcount(np.dtype(np.double)), double_refcount)
|
|
3053
|
+
|
|
3054
|
+
for j in range(10):
|
|
3055
|
+
np.bincount([1, 2, 3], [4, 5, 6])
|
|
3056
|
+
assert_equal(sys.getrefcount(np.dtype(np.intp)), intp_refcount)
|
|
3057
|
+
assert_equal(sys.getrefcount(np.dtype(np.double)), double_refcount)
|
|
3058
|
+
|
|
3059
|
+
@pytest.mark.parametrize("vals", [[[2, 2]], 2])
|
|
3060
|
+
def test_error_not_1d(self, vals):
|
|
3061
|
+
# Test that values has to be 1-D (both as array and nested list)
|
|
3062
|
+
vals_arr = np.asarray(vals)
|
|
3063
|
+
with assert_raises(ValueError):
|
|
3064
|
+
np.bincount(vals_arr)
|
|
3065
|
+
with assert_raises(ValueError):
|
|
3066
|
+
np.bincount(vals)
|
|
3067
|
+
|
|
3068
|
+
@pytest.mark.parametrize("dt", np.typecodes["AllInteger"])
|
|
3069
|
+
def test_gh_28354(self, dt):
|
|
3070
|
+
a = np.array([0, 1, 1, 3, 2, 1, 7], dtype=dt)
|
|
3071
|
+
actual = np.bincount(a)
|
|
3072
|
+
expected = [1, 3, 1, 1, 0, 0, 0, 1]
|
|
3073
|
+
assert_array_equal(actual, expected)
|
|
3074
|
+
|
|
3075
|
+
def test_contiguous_handling(self):
|
|
3076
|
+
# check for absence of hard crash
|
|
3077
|
+
np.bincount(np.arange(10000)[::2])
|
|
3078
|
+
|
|
3079
|
+
def test_gh_28354_array_like(self):
|
|
3080
|
+
class A:
|
|
3081
|
+
def __array__(self):
|
|
3082
|
+
return np.array([0, 1, 1, 3, 2, 1, 7], dtype=np.uint64)
|
|
3083
|
+
|
|
3084
|
+
a = A()
|
|
3085
|
+
actual = np.bincount(a)
|
|
3086
|
+
expected = [1, 3, 1, 1, 0, 0, 0, 1]
|
|
3087
|
+
assert_array_equal(actual, expected)
|
|
3088
|
+
|
|
3089
|
+
|
|
3090
|
+
class TestInterp:
|
|
3091
|
+
|
|
3092
|
+
def test_exceptions(self):
|
|
3093
|
+
assert_raises(ValueError, interp, 0, [], [])
|
|
3094
|
+
assert_raises(ValueError, interp, 0, [0], [1, 2])
|
|
3095
|
+
assert_raises(ValueError, interp, 0, [0, 1], [1, 2], period=0)
|
|
3096
|
+
assert_raises(ValueError, interp, 0, [], [], period=360)
|
|
3097
|
+
assert_raises(ValueError, interp, 0, [0], [1, 2], period=360)
|
|
3098
|
+
|
|
3099
|
+
def test_basic(self):
|
|
3100
|
+
x = np.linspace(0, 1, 5)
|
|
3101
|
+
y = np.linspace(0, 1, 5)
|
|
3102
|
+
x0 = np.linspace(0, 1, 50)
|
|
3103
|
+
assert_almost_equal(np.interp(x0, x, y), x0)
|
|
3104
|
+
|
|
3105
|
+
def test_right_left_behavior(self):
|
|
3106
|
+
# Needs range of sizes to test different code paths.
|
|
3107
|
+
# size ==1 is special cased, 1 < size < 5 is linear search, and
|
|
3108
|
+
# size >= 5 goes through local search and possibly binary search.
|
|
3109
|
+
for size in range(1, 10):
|
|
3110
|
+
xp = np.arange(size, dtype=np.double)
|
|
3111
|
+
yp = np.ones(size, dtype=np.double)
|
|
3112
|
+
incpts = np.array([-1, 0, size - 1, size], dtype=np.double)
|
|
3113
|
+
decpts = incpts[::-1]
|
|
3114
|
+
|
|
3115
|
+
incres = interp(incpts, xp, yp)
|
|
3116
|
+
decres = interp(decpts, xp, yp)
|
|
3117
|
+
inctgt = np.array([1, 1, 1, 1], dtype=float)
|
|
3118
|
+
dectgt = inctgt[::-1]
|
|
3119
|
+
assert_equal(incres, inctgt)
|
|
3120
|
+
assert_equal(decres, dectgt)
|
|
3121
|
+
|
|
3122
|
+
incres = interp(incpts, xp, yp, left=0)
|
|
3123
|
+
decres = interp(decpts, xp, yp, left=0)
|
|
3124
|
+
inctgt = np.array([0, 1, 1, 1], dtype=float)
|
|
3125
|
+
dectgt = inctgt[::-1]
|
|
3126
|
+
assert_equal(incres, inctgt)
|
|
3127
|
+
assert_equal(decres, dectgt)
|
|
3128
|
+
|
|
3129
|
+
incres = interp(incpts, xp, yp, right=2)
|
|
3130
|
+
decres = interp(decpts, xp, yp, right=2)
|
|
3131
|
+
inctgt = np.array([1, 1, 1, 2], dtype=float)
|
|
3132
|
+
dectgt = inctgt[::-1]
|
|
3133
|
+
assert_equal(incres, inctgt)
|
|
3134
|
+
assert_equal(decres, dectgt)
|
|
3135
|
+
|
|
3136
|
+
incres = interp(incpts, xp, yp, left=0, right=2)
|
|
3137
|
+
decres = interp(decpts, xp, yp, left=0, right=2)
|
|
3138
|
+
inctgt = np.array([0, 1, 1, 2], dtype=float)
|
|
3139
|
+
dectgt = inctgt[::-1]
|
|
3140
|
+
assert_equal(incres, inctgt)
|
|
3141
|
+
assert_equal(decres, dectgt)
|
|
3142
|
+
|
|
3143
|
+
def test_scalar_interpolation_point(self):
|
|
3144
|
+
x = np.linspace(0, 1, 5)
|
|
3145
|
+
y = np.linspace(0, 1, 5)
|
|
3146
|
+
x0 = 0
|
|
3147
|
+
assert_almost_equal(np.interp(x0, x, y), x0)
|
|
3148
|
+
x0 = .3
|
|
3149
|
+
assert_almost_equal(np.interp(x0, x, y), x0)
|
|
3150
|
+
x0 = np.float32(.3)
|
|
3151
|
+
assert_almost_equal(np.interp(x0, x, y), x0)
|
|
3152
|
+
x0 = np.float64(.3)
|
|
3153
|
+
assert_almost_equal(np.interp(x0, x, y), x0)
|
|
3154
|
+
x0 = np.nan
|
|
3155
|
+
assert_almost_equal(np.interp(x0, x, y), x0)
|
|
3156
|
+
|
|
3157
|
+
def test_non_finite_behavior_exact_x(self):
|
|
3158
|
+
x = [1, 2, 2.5, 3, 4]
|
|
3159
|
+
xp = [1, 2, 3, 4]
|
|
3160
|
+
fp = [1, 2, np.inf, 4]
|
|
3161
|
+
assert_almost_equal(np.interp(x, xp, fp), [1, 2, np.inf, np.inf, 4])
|
|
3162
|
+
fp = [1, 2, np.nan, 4]
|
|
3163
|
+
assert_almost_equal(np.interp(x, xp, fp), [1, 2, np.nan, np.nan, 4])
|
|
3164
|
+
|
|
3165
|
+
@pytest.fixture(params=[
|
|
3166
|
+
np.float64,
|
|
3167
|
+
lambda x: _make_complex(x, 0),
|
|
3168
|
+
lambda x: _make_complex(0, x),
|
|
3169
|
+
lambda x: _make_complex(x, np.multiply(x, -2))
|
|
3170
|
+
], ids=[
|
|
3171
|
+
'real',
|
|
3172
|
+
'complex-real',
|
|
3173
|
+
'complex-imag',
|
|
3174
|
+
'complex-both'
|
|
3175
|
+
])
|
|
3176
|
+
def sc(self, request):
|
|
3177
|
+
""" scale function used by the below tests """
|
|
3178
|
+
return request.param
|
|
3179
|
+
|
|
3180
|
+
def test_non_finite_any_nan(self, sc):
|
|
3181
|
+
""" test that nans are propagated """
|
|
3182
|
+
assert_equal(np.interp(0.5, [np.nan, 1], sc([ 0, 10])), sc(np.nan))
|
|
3183
|
+
assert_equal(np.interp(0.5, [ 0, np.nan], sc([ 0, 10])), sc(np.nan))
|
|
3184
|
+
assert_equal(np.interp(0.5, [ 0, 1], sc([np.nan, 10])), sc(np.nan))
|
|
3185
|
+
assert_equal(np.interp(0.5, [ 0, 1], sc([ 0, np.nan])), sc(np.nan))
|
|
3186
|
+
|
|
3187
|
+
def test_non_finite_inf(self, sc):
|
|
3188
|
+
""" Test that interp between opposite infs gives nan """
|
|
3189
|
+
inf = np.inf
|
|
3190
|
+
nan = np.nan
|
|
3191
|
+
assert_equal(np.interp(0.5, [-inf, +inf], sc([ 0, 10])), sc(nan))
|
|
3192
|
+
assert_equal(np.interp(0.5, [ 0, 1], sc([-inf, +inf])), sc(nan))
|
|
3193
|
+
assert_equal(np.interp(0.5, [ 0, 1], sc([+inf, -inf])), sc(nan))
|
|
3194
|
+
|
|
3195
|
+
# unless the y values are equal
|
|
3196
|
+
assert_equal(np.interp(0.5, [-np.inf, +np.inf], sc([ 10, 10])), sc(10))
|
|
3197
|
+
|
|
3198
|
+
def test_non_finite_half_inf_xf(self, sc):
|
|
3199
|
+
""" Test that interp where both axes have a bound at inf gives nan """
|
|
3200
|
+
inf = np.inf
|
|
3201
|
+
nan = np.nan
|
|
3202
|
+
assert_equal(np.interp(0.5, [-inf, 1], sc([-inf, 10])), sc(nan))
|
|
3203
|
+
assert_equal(np.interp(0.5, [-inf, 1], sc([+inf, 10])), sc(nan))
|
|
3204
|
+
assert_equal(np.interp(0.5, [-inf, 1], sc([ 0, -inf])), sc(nan))
|
|
3205
|
+
assert_equal(np.interp(0.5, [-inf, 1], sc([ 0, +inf])), sc(nan))
|
|
3206
|
+
assert_equal(np.interp(0.5, [ 0, +inf], sc([-inf, 10])), sc(nan))
|
|
3207
|
+
assert_equal(np.interp(0.5, [ 0, +inf], sc([+inf, 10])), sc(nan))
|
|
3208
|
+
assert_equal(np.interp(0.5, [ 0, +inf], sc([ 0, -inf])), sc(nan))
|
|
3209
|
+
assert_equal(np.interp(0.5, [ 0, +inf], sc([ 0, +inf])), sc(nan))
|
|
3210
|
+
|
|
3211
|
+
def test_non_finite_half_inf_x(self, sc):
|
|
3212
|
+
""" Test interp where the x axis has a bound at inf """
|
|
3213
|
+
assert_equal(np.interp(0.5, [-np.inf, -np.inf], sc([0, 10])), sc(10))
|
|
3214
|
+
assert_equal(np.interp(0.5, [-np.inf, 1 ], sc([0, 10])), sc(10)) # noqa: E202
|
|
3215
|
+
assert_equal(np.interp(0.5, [ 0, +np.inf], sc([0, 10])), sc(0))
|
|
3216
|
+
assert_equal(np.interp(0.5, [+np.inf, +np.inf], sc([0, 10])), sc(0))
|
|
3217
|
+
|
|
3218
|
+
def test_non_finite_half_inf_f(self, sc):
|
|
3219
|
+
""" Test interp where the f axis has a bound at inf """
|
|
3220
|
+
assert_equal(np.interp(0.5, [0, 1], sc([ 0, -np.inf])), sc(-np.inf))
|
|
3221
|
+
assert_equal(np.interp(0.5, [0, 1], sc([ 0, +np.inf])), sc(+np.inf))
|
|
3222
|
+
assert_equal(np.interp(0.5, [0, 1], sc([-np.inf, 10])), sc(-np.inf))
|
|
3223
|
+
assert_equal(np.interp(0.5, [0, 1], sc([+np.inf, 10])), sc(+np.inf))
|
|
3224
|
+
assert_equal(np.interp(0.5, [0, 1], sc([-np.inf, -np.inf])), sc(-np.inf))
|
|
3225
|
+
assert_equal(np.interp(0.5, [0, 1], sc([+np.inf, +np.inf])), sc(+np.inf))
|
|
3226
|
+
|
|
3227
|
+
def test_complex_interp(self):
|
|
3228
|
+
# test complex interpolation
|
|
3229
|
+
x = np.linspace(0, 1, 5)
|
|
3230
|
+
y = np.linspace(0, 1, 5) + (1 + np.linspace(0, 1, 5)) * 1.0j
|
|
3231
|
+
x0 = 0.3
|
|
3232
|
+
y0 = x0 + (1 + x0) * 1.0j
|
|
3233
|
+
assert_almost_equal(np.interp(x0, x, y), y0)
|
|
3234
|
+
# test complex left and right
|
|
3235
|
+
x0 = -1
|
|
3236
|
+
left = 2 + 3.0j
|
|
3237
|
+
assert_almost_equal(np.interp(x0, x, y, left=left), left)
|
|
3238
|
+
x0 = 2.0
|
|
3239
|
+
right = 2 + 3.0j
|
|
3240
|
+
assert_almost_equal(np.interp(x0, x, y, right=right), right)
|
|
3241
|
+
# test complex non finite
|
|
3242
|
+
x = [1, 2, 2.5, 3, 4]
|
|
3243
|
+
xp = [1, 2, 3, 4]
|
|
3244
|
+
fp = [1, 2 + 1j, np.inf, 4]
|
|
3245
|
+
y = [1, 2 + 1j, np.inf + 0.5j, np.inf, 4]
|
|
3246
|
+
assert_almost_equal(np.interp(x, xp, fp), y)
|
|
3247
|
+
# test complex periodic
|
|
3248
|
+
x = [-180, -170, -185, 185, -10, -5, 0, 365]
|
|
3249
|
+
xp = [190, -190, 350, -350]
|
|
3250
|
+
fp = [5 + 1.0j, 10 + 2j, 3 + 3j, 4 + 4j]
|
|
3251
|
+
y = [7.5 + 1.5j, 5. + 1.0j, 8.75 + 1.75j, 6.25 + 1.25j, 3. + 3j, 3.25 + 3.25j,
|
|
3252
|
+
3.5 + 3.5j, 3.75 + 3.75j]
|
|
3253
|
+
assert_almost_equal(np.interp(x, xp, fp, period=360), y)
|
|
3254
|
+
|
|
3255
|
+
def test_zero_dimensional_interpolation_point(self):
|
|
3256
|
+
x = np.linspace(0, 1, 5)
|
|
3257
|
+
y = np.linspace(0, 1, 5)
|
|
3258
|
+
x0 = np.array(.3)
|
|
3259
|
+
assert_almost_equal(np.interp(x0, x, y), x0)
|
|
3260
|
+
|
|
3261
|
+
xp = np.array([0, 2, 4])
|
|
3262
|
+
fp = np.array([1, -1, 1])
|
|
3263
|
+
|
|
3264
|
+
actual = np.interp(np.array(1), xp, fp)
|
|
3265
|
+
assert_equal(actual, 0)
|
|
3266
|
+
assert_(isinstance(actual, np.float64))
|
|
3267
|
+
|
|
3268
|
+
actual = np.interp(np.array(4.5), xp, fp, period=4)
|
|
3269
|
+
assert_equal(actual, 0.5)
|
|
3270
|
+
assert_(isinstance(actual, np.float64))
|
|
3271
|
+
|
|
3272
|
+
def test_if_len_x_is_small(self):
|
|
3273
|
+
xp = np.arange(0, 10, 0.0001)
|
|
3274
|
+
fp = np.sin(xp)
|
|
3275
|
+
assert_almost_equal(np.interp(np.pi, xp, fp), 0.0)
|
|
3276
|
+
|
|
3277
|
+
def test_period(self):
|
|
3278
|
+
x = [-180, -170, -185, 185, -10, -5, 0, 365]
|
|
3279
|
+
xp = [190, -190, 350, -350]
|
|
3280
|
+
fp = [5, 10, 3, 4]
|
|
3281
|
+
y = [7.5, 5., 8.75, 6.25, 3., 3.25, 3.5, 3.75]
|
|
3282
|
+
assert_almost_equal(np.interp(x, xp, fp, period=360), y)
|
|
3283
|
+
x = np.array(x, order='F').reshape(2, -1)
|
|
3284
|
+
y = np.array(y, order='C').reshape(2, -1)
|
|
3285
|
+
assert_almost_equal(np.interp(x, xp, fp, period=360), y)
|
|
3286
|
+
|
|
3287
|
+
|
|
3288
|
+
quantile_methods = [
|
|
3289
|
+
'inverted_cdf', 'averaged_inverted_cdf', 'closest_observation',
|
|
3290
|
+
'interpolated_inverted_cdf', 'hazen', 'weibull', 'linear',
|
|
3291
|
+
'median_unbiased', 'normal_unbiased', 'nearest', 'lower', 'higher',
|
|
3292
|
+
'midpoint']
|
|
3293
|
+
|
|
3294
|
+
# Note: Technically, averaged_inverted_cdf and midpoint are not interpolated.
|
|
3295
|
+
# but NumPy doesn't currently make a difference (at least w.r.t. to promotion).
|
|
3296
|
+
interpolating_quantile_methods = [
|
|
3297
|
+
'averaged_inverted_cdf', 'interpolated_inverted_cdf', 'hazen', 'weibull',
|
|
3298
|
+
'linear', 'median_unbiased', 'normal_unbiased', 'midpoint']
|
|
3299
|
+
|
|
3300
|
+
methods_supporting_weights = ["inverted_cdf"]
|
|
3301
|
+
|
|
3302
|
+
|
|
3303
|
+
class TestPercentile:
|
|
3304
|
+
|
|
3305
|
+
def test_basic(self):
|
|
3306
|
+
x = np.arange(8) * 0.5
|
|
3307
|
+
assert_equal(np.percentile(x, 0), 0.)
|
|
3308
|
+
assert_equal(np.percentile(x, 100), 3.5)
|
|
3309
|
+
assert_equal(np.percentile(x, 50), 1.75)
|
|
3310
|
+
x[1] = np.nan
|
|
3311
|
+
assert_equal(np.percentile(x, 0), np.nan)
|
|
3312
|
+
assert_equal(np.percentile(x, 0, method='nearest'), np.nan)
|
|
3313
|
+
assert_equal(np.percentile(x, 0, method='inverted_cdf'), np.nan)
|
|
3314
|
+
assert_equal(
|
|
3315
|
+
np.percentile(x, 0, method='inverted_cdf',
|
|
3316
|
+
weights=np.ones_like(x)),
|
|
3317
|
+
np.nan,
|
|
3318
|
+
)
|
|
3319
|
+
|
|
3320
|
+
def test_fraction(self):
|
|
3321
|
+
x = [Fraction(i, 2) for i in range(8)]
|
|
3322
|
+
|
|
3323
|
+
p = np.percentile(x, Fraction(0))
|
|
3324
|
+
assert_equal(p, Fraction(0))
|
|
3325
|
+
assert_equal(type(p), Fraction)
|
|
3326
|
+
|
|
3327
|
+
p = np.percentile(x, Fraction(100))
|
|
3328
|
+
assert_equal(p, Fraction(7, 2))
|
|
3329
|
+
assert_equal(type(p), Fraction)
|
|
3330
|
+
|
|
3331
|
+
p = np.percentile(x, Fraction(50))
|
|
3332
|
+
assert_equal(p, Fraction(7, 4))
|
|
3333
|
+
assert_equal(type(p), Fraction)
|
|
3334
|
+
|
|
3335
|
+
p = np.percentile(x, [Fraction(50)])
|
|
3336
|
+
assert_equal(p, np.array([Fraction(7, 4)]))
|
|
3337
|
+
assert_equal(type(p), np.ndarray)
|
|
3338
|
+
|
|
3339
|
+
def test_api(self):
|
|
3340
|
+
d = np.ones(5)
|
|
3341
|
+
np.percentile(d, 5, None, None, False)
|
|
3342
|
+
np.percentile(d, 5, None, None, False, 'linear')
|
|
3343
|
+
o = np.ones((1,))
|
|
3344
|
+
np.percentile(d, 5, None, o, False, 'linear')
|
|
3345
|
+
|
|
3346
|
+
def test_complex(self):
|
|
3347
|
+
arr_c = np.array([0.5 + 3.0j, 2.1 + 0.5j, 1.6 + 2.3j], dtype='G')
|
|
3348
|
+
assert_raises(TypeError, np.percentile, arr_c, 0.5)
|
|
3349
|
+
arr_c = np.array([0.5 + 3.0j, 2.1 + 0.5j, 1.6 + 2.3j], dtype='D')
|
|
3350
|
+
assert_raises(TypeError, np.percentile, arr_c, 0.5)
|
|
3351
|
+
arr_c = np.array([0.5 + 3.0j, 2.1 + 0.5j, 1.6 + 2.3j], dtype='F')
|
|
3352
|
+
assert_raises(TypeError, np.percentile, arr_c, 0.5)
|
|
3353
|
+
|
|
3354
|
+
def test_2D(self):
|
|
3355
|
+
x = np.array([[1, 1, 1],
|
|
3356
|
+
[1, 1, 1],
|
|
3357
|
+
[4, 4, 3],
|
|
3358
|
+
[1, 1, 1],
|
|
3359
|
+
[1, 1, 1]])
|
|
3360
|
+
assert_array_equal(np.percentile(x, 50, axis=0), [1, 1, 1])
|
|
3361
|
+
|
|
3362
|
+
@pytest.mark.parametrize("dtype", np.typecodes["Float"])
|
|
3363
|
+
def test_linear_nan_1D(self, dtype):
|
|
3364
|
+
# METHOD 1 of H&F
|
|
3365
|
+
arr = np.asarray([15.0, np.nan, 35.0, 40.0, 50.0], dtype=dtype)
|
|
3366
|
+
res = np.percentile(
|
|
3367
|
+
arr,
|
|
3368
|
+
40.0,
|
|
3369
|
+
method="linear")
|
|
3370
|
+
np.testing.assert_equal(res, np.nan)
|
|
3371
|
+
np.testing.assert_equal(res.dtype, arr.dtype)
|
|
3372
|
+
|
|
3373
|
+
H_F_TYPE_CODES = [(int_type, np.float64)
|
|
3374
|
+
for int_type in np.typecodes["AllInteger"]
|
|
3375
|
+
] + [(np.float16, np.float16),
|
|
3376
|
+
(np.float32, np.float32),
|
|
3377
|
+
(np.float64, np.float64),
|
|
3378
|
+
(np.longdouble, np.longdouble),
|
|
3379
|
+
(np.dtype("O"), np.float64)]
|
|
3380
|
+
|
|
3381
|
+
@pytest.mark.parametrize(["function", "quantile"],
|
|
3382
|
+
[(np.quantile, 0.4),
|
|
3383
|
+
(np.percentile, 40.0)])
|
|
3384
|
+
@pytest.mark.parametrize(["input_dtype", "expected_dtype"], H_F_TYPE_CODES)
|
|
3385
|
+
@pytest.mark.parametrize(["method", "weighted", "expected"],
|
|
3386
|
+
[("inverted_cdf", False, 20),
|
|
3387
|
+
("inverted_cdf", True, 20),
|
|
3388
|
+
("averaged_inverted_cdf", False, 27.5),
|
|
3389
|
+
("closest_observation", False, 20),
|
|
3390
|
+
("interpolated_inverted_cdf", False, 20),
|
|
3391
|
+
("hazen", False, 27.5),
|
|
3392
|
+
("weibull", False, 26),
|
|
3393
|
+
("linear", False, 29),
|
|
3394
|
+
("median_unbiased", False, 27),
|
|
3395
|
+
("normal_unbiased", False, 27.125),
|
|
3396
|
+
])
|
|
3397
|
+
def test_linear_interpolation(self,
|
|
3398
|
+
function,
|
|
3399
|
+
quantile,
|
|
3400
|
+
method,
|
|
3401
|
+
weighted,
|
|
3402
|
+
expected,
|
|
3403
|
+
input_dtype,
|
|
3404
|
+
expected_dtype):
|
|
3405
|
+
expected_dtype = np.dtype(expected_dtype)
|
|
3406
|
+
|
|
3407
|
+
arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=input_dtype)
|
|
3408
|
+
weights = np.ones_like(arr) if weighted else None
|
|
3409
|
+
if input_dtype is np.longdouble:
|
|
3410
|
+
if function is np.quantile:
|
|
3411
|
+
# 0.4 is not exactly representable and it matters
|
|
3412
|
+
# for "averaged_inverted_cdf", so we need to cheat.
|
|
3413
|
+
quantile = input_dtype("0.4")
|
|
3414
|
+
# We want to use nulp, but that does not work for longdouble
|
|
3415
|
+
test_function = np.testing.assert_almost_equal
|
|
3416
|
+
else:
|
|
3417
|
+
test_function = np.testing.assert_array_almost_equal_nulp
|
|
3418
|
+
|
|
3419
|
+
actual = function(arr, quantile, method=method, weights=weights)
|
|
3420
|
+
|
|
3421
|
+
test_function(actual, expected_dtype.type(expected))
|
|
3422
|
+
|
|
3423
|
+
if method in ["inverted_cdf", "closest_observation"]:
|
|
3424
|
+
if input_dtype == "O":
|
|
3425
|
+
np.testing.assert_equal(np.asarray(actual).dtype, np.float64)
|
|
3426
|
+
else:
|
|
3427
|
+
np.testing.assert_equal(np.asarray(actual).dtype,
|
|
3428
|
+
np.dtype(input_dtype))
|
|
3429
|
+
else:
|
|
3430
|
+
np.testing.assert_equal(np.asarray(actual).dtype,
|
|
3431
|
+
np.dtype(expected_dtype))
|
|
3432
|
+
|
|
3433
|
+
TYPE_CODES = np.typecodes["AllInteger"] + np.typecodes["Float"] + "O"
|
|
3434
|
+
|
|
3435
|
+
@pytest.mark.parametrize("dtype", TYPE_CODES)
|
|
3436
|
+
def test_lower_higher(self, dtype):
|
|
3437
|
+
assert_equal(np.percentile(np.arange(10, dtype=dtype), 50,
|
|
3438
|
+
method='lower'), 4)
|
|
3439
|
+
assert_equal(np.percentile(np.arange(10, dtype=dtype), 50,
|
|
3440
|
+
method='higher'), 5)
|
|
3441
|
+
|
|
3442
|
+
@pytest.mark.parametrize("dtype", TYPE_CODES)
|
|
3443
|
+
def test_midpoint(self, dtype):
|
|
3444
|
+
assert_equal(np.percentile(np.arange(10, dtype=dtype), 51,
|
|
3445
|
+
method='midpoint'), 4.5)
|
|
3446
|
+
assert_equal(np.percentile(np.arange(9, dtype=dtype) + 1, 50,
|
|
3447
|
+
method='midpoint'), 5)
|
|
3448
|
+
assert_equal(np.percentile(np.arange(11, dtype=dtype), 51,
|
|
3449
|
+
method='midpoint'), 5.5)
|
|
3450
|
+
assert_equal(np.percentile(np.arange(11, dtype=dtype), 50,
|
|
3451
|
+
method='midpoint'), 5)
|
|
3452
|
+
|
|
3453
|
+
@pytest.mark.parametrize("dtype", TYPE_CODES)
|
|
3454
|
+
def test_nearest(self, dtype):
|
|
3455
|
+
assert_equal(np.percentile(np.arange(10, dtype=dtype), 51,
|
|
3456
|
+
method='nearest'), 5)
|
|
3457
|
+
assert_equal(np.percentile(np.arange(10, dtype=dtype), 49,
|
|
3458
|
+
method='nearest'), 4)
|
|
3459
|
+
|
|
3460
|
+
def test_linear_interpolation_extrapolation(self):
|
|
3461
|
+
arr = np.random.rand(5)
|
|
3462
|
+
|
|
3463
|
+
actual = np.percentile(arr, 100)
|
|
3464
|
+
np.testing.assert_equal(actual, arr.max())
|
|
3465
|
+
|
|
3466
|
+
actual = np.percentile(arr, 0)
|
|
3467
|
+
np.testing.assert_equal(actual, arr.min())
|
|
3468
|
+
|
|
3469
|
+
def test_sequence(self):
|
|
3470
|
+
x = np.arange(8) * 0.5
|
|
3471
|
+
assert_equal(np.percentile(x, [0, 100, 50]), [0, 3.5, 1.75])
|
|
3472
|
+
|
|
3473
|
+
def test_axis(self):
|
|
3474
|
+
x = np.arange(12).reshape(3, 4)
|
|
3475
|
+
|
|
3476
|
+
assert_equal(np.percentile(x, (25, 50, 100)), [2.75, 5.5, 11.0])
|
|
3477
|
+
|
|
3478
|
+
r0 = [[2, 3, 4, 5], [4, 5, 6, 7], [8, 9, 10, 11]]
|
|
3479
|
+
assert_equal(np.percentile(x, (25, 50, 100), axis=0), r0)
|
|
3480
|
+
|
|
3481
|
+
r1 = [[0.75, 1.5, 3], [4.75, 5.5, 7], [8.75, 9.5, 11]]
|
|
3482
|
+
assert_equal(np.percentile(x, (25, 50, 100), axis=1), np.array(r1).T)
|
|
3483
|
+
|
|
3484
|
+
# ensure qth axis is always first as with np.array(old_percentile(..))
|
|
3485
|
+
x = np.arange(3 * 4 * 5 * 6).reshape(3, 4, 5, 6)
|
|
3486
|
+
assert_equal(np.percentile(x, (25, 50)).shape, (2,))
|
|
3487
|
+
assert_equal(np.percentile(x, (25, 50, 75)).shape, (3,))
|
|
3488
|
+
assert_equal(np.percentile(x, (25, 50), axis=0).shape, (2, 4, 5, 6))
|
|
3489
|
+
assert_equal(np.percentile(x, (25, 50), axis=1).shape, (2, 3, 5, 6))
|
|
3490
|
+
assert_equal(np.percentile(x, (25, 50), axis=2).shape, (2, 3, 4, 6))
|
|
3491
|
+
assert_equal(np.percentile(x, (25, 50), axis=3).shape, (2, 3, 4, 5))
|
|
3492
|
+
assert_equal(
|
|
3493
|
+
np.percentile(x, (25, 50, 75), axis=1).shape, (3, 3, 5, 6))
|
|
3494
|
+
assert_equal(np.percentile(x, (25, 50),
|
|
3495
|
+
method="higher").shape, (2,))
|
|
3496
|
+
assert_equal(np.percentile(x, (25, 50, 75),
|
|
3497
|
+
method="higher").shape, (3,))
|
|
3498
|
+
assert_equal(np.percentile(x, (25, 50), axis=0,
|
|
3499
|
+
method="higher").shape, (2, 4, 5, 6))
|
|
3500
|
+
assert_equal(np.percentile(x, (25, 50), axis=1,
|
|
3501
|
+
method="higher").shape, (2, 3, 5, 6))
|
|
3502
|
+
assert_equal(np.percentile(x, (25, 50), axis=2,
|
|
3503
|
+
method="higher").shape, (2, 3, 4, 6))
|
|
3504
|
+
assert_equal(np.percentile(x, (25, 50), axis=3,
|
|
3505
|
+
method="higher").shape, (2, 3, 4, 5))
|
|
3506
|
+
assert_equal(np.percentile(x, (25, 50, 75), axis=1,
|
|
3507
|
+
method="higher").shape, (3, 3, 5, 6))
|
|
3508
|
+
|
|
3509
|
+
def test_scalar_q(self):
|
|
3510
|
+
# test for no empty dimensions for compatibility with old percentile
|
|
3511
|
+
x = np.arange(12).reshape(3, 4)
|
|
3512
|
+
assert_equal(np.percentile(x, 50), 5.5)
|
|
3513
|
+
assert_(np.isscalar(np.percentile(x, 50)))
|
|
3514
|
+
r0 = np.array([4., 5., 6., 7.])
|
|
3515
|
+
assert_equal(np.percentile(x, 50, axis=0), r0)
|
|
3516
|
+
assert_equal(np.percentile(x, 50, axis=0).shape, r0.shape)
|
|
3517
|
+
r1 = np.array([1.5, 5.5, 9.5])
|
|
3518
|
+
assert_almost_equal(np.percentile(x, 50, axis=1), r1)
|
|
3519
|
+
assert_equal(np.percentile(x, 50, axis=1).shape, r1.shape)
|
|
3520
|
+
|
|
3521
|
+
out = np.empty(1)
|
|
3522
|
+
assert_equal(np.percentile(x, 50, out=out), 5.5)
|
|
3523
|
+
assert_equal(out, 5.5)
|
|
3524
|
+
out = np.empty(4)
|
|
3525
|
+
assert_equal(np.percentile(x, 50, axis=0, out=out), r0)
|
|
3526
|
+
assert_equal(out, r0)
|
|
3527
|
+
out = np.empty(3)
|
|
3528
|
+
assert_equal(np.percentile(x, 50, axis=1, out=out), r1)
|
|
3529
|
+
assert_equal(out, r1)
|
|
3530
|
+
|
|
3531
|
+
# test for no empty dimensions for compatibility with old percentile
|
|
3532
|
+
x = np.arange(12).reshape(3, 4)
|
|
3533
|
+
assert_equal(np.percentile(x, 50, method='lower'), 5.)
|
|
3534
|
+
assert_(np.isscalar(np.percentile(x, 50)))
|
|
3535
|
+
r0 = np.array([4., 5., 6., 7.])
|
|
3536
|
+
c0 = np.percentile(x, 50, method='lower', axis=0)
|
|
3537
|
+
assert_equal(c0, r0)
|
|
3538
|
+
assert_equal(c0.shape, r0.shape)
|
|
3539
|
+
r1 = np.array([1., 5., 9.])
|
|
3540
|
+
c1 = np.percentile(x, 50, method='lower', axis=1)
|
|
3541
|
+
assert_almost_equal(c1, r1)
|
|
3542
|
+
assert_equal(c1.shape, r1.shape)
|
|
3543
|
+
|
|
3544
|
+
out = np.empty((), dtype=x.dtype)
|
|
3545
|
+
c = np.percentile(x, 50, method='lower', out=out)
|
|
3546
|
+
assert_equal(c, 5)
|
|
3547
|
+
assert_equal(out, 5)
|
|
3548
|
+
out = np.empty(4, dtype=x.dtype)
|
|
3549
|
+
c = np.percentile(x, 50, method='lower', axis=0, out=out)
|
|
3550
|
+
assert_equal(c, r0)
|
|
3551
|
+
assert_equal(out, r0)
|
|
3552
|
+
out = np.empty(3, dtype=x.dtype)
|
|
3553
|
+
c = np.percentile(x, 50, method='lower', axis=1, out=out)
|
|
3554
|
+
assert_equal(c, r1)
|
|
3555
|
+
assert_equal(out, r1)
|
|
3556
|
+
|
|
3557
|
+
def test_exception(self):
|
|
3558
|
+
assert_raises(ValueError, np.percentile, [1, 2], 56,
|
|
3559
|
+
method='foobar')
|
|
3560
|
+
assert_raises(ValueError, np.percentile, [1], 101)
|
|
3561
|
+
assert_raises(ValueError, np.percentile, [1], -1)
|
|
3562
|
+
assert_raises(ValueError, np.percentile, [1], list(range(50)) + [101])
|
|
3563
|
+
assert_raises(ValueError, np.percentile, [1], list(range(50)) + [-0.1])
|
|
3564
|
+
|
|
3565
|
+
def test_percentile_list(self):
|
|
3566
|
+
assert_equal(np.percentile([1, 2, 3], 0), 1)
|
|
3567
|
+
|
|
3568
|
+
@pytest.mark.parametrize(
|
|
3569
|
+
"percentile, with_weights",
|
|
3570
|
+
[
|
|
3571
|
+
(np.percentile, False),
|
|
3572
|
+
(partial(np.percentile, method="inverted_cdf"), True),
|
|
3573
|
+
]
|
|
3574
|
+
)
|
|
3575
|
+
def test_percentile_out(self, percentile, with_weights):
|
|
3576
|
+
out_dtype = int if with_weights else float
|
|
3577
|
+
x = np.array([1, 2, 3])
|
|
3578
|
+
y = np.zeros((3,), dtype=out_dtype)
|
|
3579
|
+
p = (1, 2, 3)
|
|
3580
|
+
weights = np.ones_like(x) if with_weights else None
|
|
3581
|
+
r = percentile(x, p, out=y, weights=weights)
|
|
3582
|
+
assert r is y
|
|
3583
|
+
assert_equal(percentile(x, p, weights=weights), y)
|
|
3584
|
+
|
|
3585
|
+
x = np.array([[1, 2, 3],
|
|
3586
|
+
[4, 5, 6]])
|
|
3587
|
+
y = np.zeros((3, 3), dtype=out_dtype)
|
|
3588
|
+
weights = np.ones_like(x) if with_weights else None
|
|
3589
|
+
r = percentile(x, p, axis=0, out=y, weights=weights)
|
|
3590
|
+
assert r is y
|
|
3591
|
+
assert_equal(percentile(x, p, weights=weights, axis=0), y)
|
|
3592
|
+
|
|
3593
|
+
y = np.zeros((3, 2), dtype=out_dtype)
|
|
3594
|
+
percentile(x, p, axis=1, out=y, weights=weights)
|
|
3595
|
+
assert_equal(percentile(x, p, weights=weights, axis=1), y)
|
|
3596
|
+
|
|
3597
|
+
x = np.arange(12).reshape(3, 4)
|
|
3598
|
+
# q.dim > 1, float
|
|
3599
|
+
if with_weights:
|
|
3600
|
+
r0 = np.array([[0, 1, 2, 3], [4, 5, 6, 7]])
|
|
3601
|
+
else:
|
|
3602
|
+
r0 = np.array([[2., 3., 4., 5.], [4., 5., 6., 7.]])
|
|
3603
|
+
out = np.empty((2, 4), dtype=out_dtype)
|
|
3604
|
+
weights = np.ones_like(x) if with_weights else None
|
|
3605
|
+
assert_equal(
|
|
3606
|
+
percentile(x, (25, 50), axis=0, out=out, weights=weights), r0
|
|
3607
|
+
)
|
|
3608
|
+
assert_equal(out, r0)
|
|
3609
|
+
r1 = np.array([[0.75, 4.75, 8.75], [1.5, 5.5, 9.5]])
|
|
3610
|
+
out = np.empty((2, 3))
|
|
3611
|
+
assert_equal(np.percentile(x, (25, 50), axis=1, out=out), r1)
|
|
3612
|
+
assert_equal(out, r1)
|
|
3613
|
+
|
|
3614
|
+
# q.dim > 1, int
|
|
3615
|
+
r0 = np.array([[0, 1, 2, 3], [4, 5, 6, 7]])
|
|
3616
|
+
out = np.empty((2, 4), dtype=x.dtype)
|
|
3617
|
+
c = np.percentile(x, (25, 50), method='lower', axis=0, out=out)
|
|
3618
|
+
assert_equal(c, r0)
|
|
3619
|
+
assert_equal(out, r0)
|
|
3620
|
+
r1 = np.array([[0, 4, 8], [1, 5, 9]])
|
|
3621
|
+
out = np.empty((2, 3), dtype=x.dtype)
|
|
3622
|
+
c = np.percentile(x, (25, 50), method='lower', axis=1, out=out)
|
|
3623
|
+
assert_equal(c, r1)
|
|
3624
|
+
assert_equal(out, r1)
|
|
3625
|
+
|
|
3626
|
+
def test_percentile_empty_dim(self):
|
|
3627
|
+
# empty dims are preserved
|
|
3628
|
+
d = np.arange(11 * 2).reshape(11, 1, 2, 1)
|
|
3629
|
+
assert_array_equal(np.percentile(d, 50, axis=0).shape, (1, 2, 1))
|
|
3630
|
+
assert_array_equal(np.percentile(d, 50, axis=1).shape, (11, 2, 1))
|
|
3631
|
+
assert_array_equal(np.percentile(d, 50, axis=2).shape, (11, 1, 1))
|
|
3632
|
+
assert_array_equal(np.percentile(d, 50, axis=3).shape, (11, 1, 2))
|
|
3633
|
+
assert_array_equal(np.percentile(d, 50, axis=-1).shape, (11, 1, 2))
|
|
3634
|
+
assert_array_equal(np.percentile(d, 50, axis=-2).shape, (11, 1, 1))
|
|
3635
|
+
assert_array_equal(np.percentile(d, 50, axis=-3).shape, (11, 2, 1))
|
|
3636
|
+
assert_array_equal(np.percentile(d, 50, axis=-4).shape, (1, 2, 1))
|
|
3637
|
+
|
|
3638
|
+
assert_array_equal(np.percentile(d, 50, axis=2,
|
|
3639
|
+
method='midpoint').shape,
|
|
3640
|
+
(11, 1, 1))
|
|
3641
|
+
assert_array_equal(np.percentile(d, 50, axis=-2,
|
|
3642
|
+
method='midpoint').shape,
|
|
3643
|
+
(11, 1, 1))
|
|
3644
|
+
|
|
3645
|
+
assert_array_equal(np.array(np.percentile(d, [10, 50], axis=0)).shape,
|
|
3646
|
+
(2, 1, 2, 1))
|
|
3647
|
+
assert_array_equal(np.array(np.percentile(d, [10, 50], axis=1)).shape,
|
|
3648
|
+
(2, 11, 2, 1))
|
|
3649
|
+
assert_array_equal(np.array(np.percentile(d, [10, 50], axis=2)).shape,
|
|
3650
|
+
(2, 11, 1, 1))
|
|
3651
|
+
assert_array_equal(np.array(np.percentile(d, [10, 50], axis=3)).shape,
|
|
3652
|
+
(2, 11, 1, 2))
|
|
3653
|
+
|
|
3654
|
+
def test_percentile_no_overwrite(self):
|
|
3655
|
+
a = np.array([2, 3, 4, 1])
|
|
3656
|
+
np.percentile(a, [50], overwrite_input=False)
|
|
3657
|
+
assert_equal(a, np.array([2, 3, 4, 1]))
|
|
3658
|
+
|
|
3659
|
+
a = np.array([2, 3, 4, 1])
|
|
3660
|
+
np.percentile(a, [50])
|
|
3661
|
+
assert_equal(a, np.array([2, 3, 4, 1]))
|
|
3662
|
+
|
|
3663
|
+
def test_no_p_overwrite(self):
|
|
3664
|
+
p = np.linspace(0., 100., num=5)
|
|
3665
|
+
np.percentile(np.arange(100.), p, method="midpoint")
|
|
3666
|
+
assert_array_equal(p, np.linspace(0., 100., num=5))
|
|
3667
|
+
p = np.linspace(0., 100., num=5).tolist()
|
|
3668
|
+
np.percentile(np.arange(100.), p, method="midpoint")
|
|
3669
|
+
assert_array_equal(p, np.linspace(0., 100., num=5).tolist())
|
|
3670
|
+
|
|
3671
|
+
def test_percentile_overwrite(self):
|
|
3672
|
+
a = np.array([2, 3, 4, 1])
|
|
3673
|
+
b = np.percentile(a, [50], overwrite_input=True)
|
|
3674
|
+
assert_equal(b, np.array([2.5]))
|
|
3675
|
+
|
|
3676
|
+
b = np.percentile([2, 3, 4, 1], [50], overwrite_input=True)
|
|
3677
|
+
assert_equal(b, np.array([2.5]))
|
|
3678
|
+
|
|
3679
|
+
def test_extended_axis(self):
|
|
3680
|
+
o = np.random.normal(size=(71, 23))
|
|
3681
|
+
x = np.dstack([o] * 10)
|
|
3682
|
+
assert_equal(np.percentile(x, 30, axis=(0, 1)), np.percentile(o, 30))
|
|
3683
|
+
x = np.moveaxis(x, -1, 0)
|
|
3684
|
+
assert_equal(np.percentile(x, 30, axis=(-2, -1)), np.percentile(o, 30))
|
|
3685
|
+
x = x.swapaxes(0, 1).copy()
|
|
3686
|
+
assert_equal(np.percentile(x, 30, axis=(0, -1)), np.percentile(o, 30))
|
|
3687
|
+
x = x.swapaxes(0, 1).copy()
|
|
3688
|
+
|
|
3689
|
+
assert_equal(np.percentile(x, [25, 60], axis=(0, 1, 2)),
|
|
3690
|
+
np.percentile(x, [25, 60], axis=None))
|
|
3691
|
+
assert_equal(np.percentile(x, [25, 60], axis=(0,)),
|
|
3692
|
+
np.percentile(x, [25, 60], axis=0))
|
|
3693
|
+
|
|
3694
|
+
d = np.arange(3 * 5 * 7 * 11).reshape((3, 5, 7, 11))
|
|
3695
|
+
np.random.shuffle(d.ravel())
|
|
3696
|
+
assert_equal(np.percentile(d, 25, axis=(0, 1, 2))[0],
|
|
3697
|
+
np.percentile(d[:, :, :, 0].flatten(), 25))
|
|
3698
|
+
assert_equal(np.percentile(d, [10, 90], axis=(0, 1, 3))[:, 1],
|
|
3699
|
+
np.percentile(d[:, :, 1, :].flatten(), [10, 90]))
|
|
3700
|
+
assert_equal(np.percentile(d, 25, axis=(3, 1, -4))[2],
|
|
3701
|
+
np.percentile(d[:, :, 2, :].flatten(), 25))
|
|
3702
|
+
assert_equal(np.percentile(d, 25, axis=(3, 1, 2))[2],
|
|
3703
|
+
np.percentile(d[2, :, :, :].flatten(), 25))
|
|
3704
|
+
assert_equal(np.percentile(d, 25, axis=(3, 2))[2, 1],
|
|
3705
|
+
np.percentile(d[2, 1, :, :].flatten(), 25))
|
|
3706
|
+
assert_equal(np.percentile(d, 25, axis=(1, -2))[2, 1],
|
|
3707
|
+
np.percentile(d[2, :, :, 1].flatten(), 25))
|
|
3708
|
+
assert_equal(np.percentile(d, 25, axis=(1, 3))[2, 2],
|
|
3709
|
+
np.percentile(d[2, :, 2, :].flatten(), 25))
|
|
3710
|
+
|
|
3711
|
+
def test_extended_axis_invalid(self):
|
|
3712
|
+
d = np.ones((3, 5, 7, 11))
|
|
3713
|
+
assert_raises(AxisError, np.percentile, d, axis=-5, q=25)
|
|
3714
|
+
assert_raises(AxisError, np.percentile, d, axis=(0, -5), q=25)
|
|
3715
|
+
assert_raises(AxisError, np.percentile, d, axis=4, q=25)
|
|
3716
|
+
assert_raises(AxisError, np.percentile, d, axis=(0, 4), q=25)
|
|
3717
|
+
# each of these refers to the same axis twice
|
|
3718
|
+
assert_raises(ValueError, np.percentile, d, axis=(1, 1), q=25)
|
|
3719
|
+
assert_raises(ValueError, np.percentile, d, axis=(-1, -1), q=25)
|
|
3720
|
+
assert_raises(ValueError, np.percentile, d, axis=(3, -1), q=25)
|
|
3721
|
+
|
|
3722
|
+
def test_keepdims(self):
|
|
3723
|
+
d = np.ones((3, 5, 7, 11))
|
|
3724
|
+
assert_equal(np.percentile(d, 7, axis=None, keepdims=True).shape,
|
|
3725
|
+
(1, 1, 1, 1))
|
|
3726
|
+
assert_equal(np.percentile(d, 7, axis=(0, 1), keepdims=True).shape,
|
|
3727
|
+
(1, 1, 7, 11))
|
|
3728
|
+
assert_equal(np.percentile(d, 7, axis=(0, 3), keepdims=True).shape,
|
|
3729
|
+
(1, 5, 7, 1))
|
|
3730
|
+
assert_equal(np.percentile(d, 7, axis=(1,), keepdims=True).shape,
|
|
3731
|
+
(3, 1, 7, 11))
|
|
3732
|
+
assert_equal(np.percentile(d, 7, (0, 1, 2, 3), keepdims=True).shape,
|
|
3733
|
+
(1, 1, 1, 1))
|
|
3734
|
+
assert_equal(np.percentile(d, 7, axis=(0, 1, 3), keepdims=True).shape,
|
|
3735
|
+
(1, 1, 7, 1))
|
|
3736
|
+
|
|
3737
|
+
assert_equal(np.percentile(d, [1, 7], axis=(0, 1, 3),
|
|
3738
|
+
keepdims=True).shape, (2, 1, 1, 7, 1))
|
|
3739
|
+
assert_equal(np.percentile(d, [1, 7], axis=(0, 3),
|
|
3740
|
+
keepdims=True).shape, (2, 1, 5, 7, 1))
|
|
3741
|
+
|
|
3742
|
+
@pytest.mark.parametrize('q', [7, [1, 7]])
|
|
3743
|
+
@pytest.mark.parametrize(
|
|
3744
|
+
argnames='axis',
|
|
3745
|
+
argvalues=[
|
|
3746
|
+
None,
|
|
3747
|
+
1,
|
|
3748
|
+
(1,),
|
|
3749
|
+
(0, 1),
|
|
3750
|
+
(-3, -1),
|
|
3751
|
+
]
|
|
3752
|
+
)
|
|
3753
|
+
def test_keepdims_out(self, q, axis):
|
|
3754
|
+
d = np.ones((3, 5, 7, 11))
|
|
3755
|
+
if axis is None:
|
|
3756
|
+
shape_out = (1,) * d.ndim
|
|
3757
|
+
else:
|
|
3758
|
+
axis_norm = normalize_axis_tuple(axis, d.ndim)
|
|
3759
|
+
shape_out = tuple(
|
|
3760
|
+
1 if i in axis_norm else d.shape[i] for i in range(d.ndim))
|
|
3761
|
+
shape_out = np.shape(q) + shape_out
|
|
3762
|
+
|
|
3763
|
+
out = np.empty(shape_out)
|
|
3764
|
+
result = np.percentile(d, q, axis=axis, keepdims=True, out=out)
|
|
3765
|
+
assert result is out
|
|
3766
|
+
assert_equal(result.shape, shape_out)
|
|
3767
|
+
|
|
3768
|
+
def test_out(self):
|
|
3769
|
+
o = np.zeros((4,))
|
|
3770
|
+
d = np.ones((3, 4))
|
|
3771
|
+
assert_equal(np.percentile(d, 0, 0, out=o), o)
|
|
3772
|
+
assert_equal(np.percentile(d, 0, 0, method='nearest', out=o), o)
|
|
3773
|
+
o = np.zeros((3,))
|
|
3774
|
+
assert_equal(np.percentile(d, 1, 1, out=o), o)
|
|
3775
|
+
assert_equal(np.percentile(d, 1, 1, method='nearest', out=o), o)
|
|
3776
|
+
|
|
3777
|
+
o = np.zeros(())
|
|
3778
|
+
assert_equal(np.percentile(d, 2, out=o), o)
|
|
3779
|
+
assert_equal(np.percentile(d, 2, method='nearest', out=o), o)
|
|
3780
|
+
|
|
3781
|
+
@pytest.mark.parametrize("method, weighted", [
|
|
3782
|
+
("linear", False),
|
|
3783
|
+
("nearest", False),
|
|
3784
|
+
("inverted_cdf", False),
|
|
3785
|
+
("inverted_cdf", True),
|
|
3786
|
+
])
|
|
3787
|
+
def test_out_nan(self, method, weighted):
|
|
3788
|
+
if weighted:
|
|
3789
|
+
kwargs = {"weights": np.ones((3, 4)), "method": method}
|
|
3790
|
+
else:
|
|
3791
|
+
kwargs = {"method": method}
|
|
3792
|
+
with warnings.catch_warnings(record=True):
|
|
3793
|
+
warnings.filterwarnings('always', '', RuntimeWarning)
|
|
3794
|
+
o = np.zeros((4,))
|
|
3795
|
+
d = np.ones((3, 4))
|
|
3796
|
+
d[2, 1] = np.nan
|
|
3797
|
+
assert_equal(np.percentile(d, 0, 0, out=o, **kwargs), o)
|
|
3798
|
+
|
|
3799
|
+
o = np.zeros((3,))
|
|
3800
|
+
assert_equal(np.percentile(d, 1, 1, out=o, **kwargs), o)
|
|
3801
|
+
|
|
3802
|
+
o = np.zeros(())
|
|
3803
|
+
assert_equal(np.percentile(d, 1, out=o, **kwargs), o)
|
|
3804
|
+
|
|
3805
|
+
def test_nan_behavior(self):
|
|
3806
|
+
a = np.arange(24, dtype=float)
|
|
3807
|
+
a[2] = np.nan
|
|
3808
|
+
assert_equal(np.percentile(a, 0.3), np.nan)
|
|
3809
|
+
assert_equal(np.percentile(a, 0.3, axis=0), np.nan)
|
|
3810
|
+
assert_equal(np.percentile(a, [0.3, 0.6], axis=0),
|
|
3811
|
+
np.array([np.nan] * 2))
|
|
3812
|
+
|
|
3813
|
+
a = np.arange(24, dtype=float).reshape(2, 3, 4)
|
|
3814
|
+
a[1, 2, 3] = np.nan
|
|
3815
|
+
a[1, 1, 2] = np.nan
|
|
3816
|
+
|
|
3817
|
+
# no axis
|
|
3818
|
+
assert_equal(np.percentile(a, 0.3), np.nan)
|
|
3819
|
+
assert_equal(np.percentile(a, 0.3).ndim, 0)
|
|
3820
|
+
|
|
3821
|
+
# axis0 zerod
|
|
3822
|
+
b = np.percentile(np.arange(24, dtype=float).reshape(2, 3, 4), 0.3, 0)
|
|
3823
|
+
b[2, 3] = np.nan
|
|
3824
|
+
b[1, 2] = np.nan
|
|
3825
|
+
assert_equal(np.percentile(a, 0.3, 0), b)
|
|
3826
|
+
|
|
3827
|
+
# axis0 not zerod
|
|
3828
|
+
b = np.percentile(np.arange(24, dtype=float).reshape(2, 3, 4),
|
|
3829
|
+
[0.3, 0.6], 0)
|
|
3830
|
+
b[:, 2, 3] = np.nan
|
|
3831
|
+
b[:, 1, 2] = np.nan
|
|
3832
|
+
assert_equal(np.percentile(a, [0.3, 0.6], 0), b)
|
|
3833
|
+
|
|
3834
|
+
# axis1 zerod
|
|
3835
|
+
b = np.percentile(np.arange(24, dtype=float).reshape(2, 3, 4), 0.3, 1)
|
|
3836
|
+
b[1, 3] = np.nan
|
|
3837
|
+
b[1, 2] = np.nan
|
|
3838
|
+
assert_equal(np.percentile(a, 0.3, 1), b)
|
|
3839
|
+
# axis1 not zerod
|
|
3840
|
+
b = np.percentile(
|
|
3841
|
+
np.arange(24, dtype=float).reshape(2, 3, 4), [0.3, 0.6], 1)
|
|
3842
|
+
b[:, 1, 3] = np.nan
|
|
3843
|
+
b[:, 1, 2] = np.nan
|
|
3844
|
+
assert_equal(np.percentile(a, [0.3, 0.6], 1), b)
|
|
3845
|
+
|
|
3846
|
+
# axis02 zerod
|
|
3847
|
+
b = np.percentile(
|
|
3848
|
+
np.arange(24, dtype=float).reshape(2, 3, 4), 0.3, (0, 2))
|
|
3849
|
+
b[1] = np.nan
|
|
3850
|
+
b[2] = np.nan
|
|
3851
|
+
assert_equal(np.percentile(a, 0.3, (0, 2)), b)
|
|
3852
|
+
# axis02 not zerod
|
|
3853
|
+
b = np.percentile(np.arange(24, dtype=float).reshape(2, 3, 4),
|
|
3854
|
+
[0.3, 0.6], (0, 2))
|
|
3855
|
+
b[:, 1] = np.nan
|
|
3856
|
+
b[:, 2] = np.nan
|
|
3857
|
+
assert_equal(np.percentile(a, [0.3, 0.6], (0, 2)), b)
|
|
3858
|
+
# axis02 not zerod with method='nearest'
|
|
3859
|
+
b = np.percentile(np.arange(24, dtype=float).reshape(2, 3, 4),
|
|
3860
|
+
[0.3, 0.6], (0, 2), method='nearest')
|
|
3861
|
+
b[:, 1] = np.nan
|
|
3862
|
+
b[:, 2] = np.nan
|
|
3863
|
+
assert_equal(np.percentile(
|
|
3864
|
+
a, [0.3, 0.6], (0, 2), method='nearest'), b)
|
|
3865
|
+
|
|
3866
|
+
def test_nan_q(self):
|
|
3867
|
+
# GH18830
|
|
3868
|
+
with pytest.raises(ValueError, match="Percentiles must be in"):
|
|
3869
|
+
np.percentile([1, 2, 3, 4.0], np.nan)
|
|
3870
|
+
with pytest.raises(ValueError, match="Percentiles must be in"):
|
|
3871
|
+
np.percentile([1, 2, 3, 4.0], [np.nan])
|
|
3872
|
+
q = np.linspace(1.0, 99.0, 16)
|
|
3873
|
+
q[0] = np.nan
|
|
3874
|
+
with pytest.raises(ValueError, match="Percentiles must be in"):
|
|
3875
|
+
np.percentile([1, 2, 3, 4.0], q)
|
|
3876
|
+
|
|
3877
|
+
@pytest.mark.parametrize("dtype", ["m8[D]", "M8[s]"])
|
|
3878
|
+
@pytest.mark.parametrize("pos", [0, 23, 10])
|
|
3879
|
+
def test_nat_basic(self, dtype, pos):
|
|
3880
|
+
# TODO: Note that times have dubious rounding as of fixing NaTs!
|
|
3881
|
+
# NaT and NaN should behave the same, do basic tests for NaT:
|
|
3882
|
+
a = np.arange(0, 24, dtype=dtype)
|
|
3883
|
+
a[pos] = "NaT"
|
|
3884
|
+
res = np.percentile(a, 30)
|
|
3885
|
+
assert res.dtype == dtype
|
|
3886
|
+
assert np.isnat(res)
|
|
3887
|
+
res = np.percentile(a, [30, 60])
|
|
3888
|
+
assert res.dtype == dtype
|
|
3889
|
+
assert np.isnat(res).all()
|
|
3890
|
+
|
|
3891
|
+
a = np.arange(0, 24 * 3, dtype=dtype).reshape(-1, 3)
|
|
3892
|
+
a[pos, 1] = "NaT"
|
|
3893
|
+
res = np.percentile(a, 30, axis=0)
|
|
3894
|
+
assert_array_equal(np.isnat(res), [False, True, False])
|
|
3895
|
+
|
|
3896
|
+
@pytest.mark.parametrize("qtype", [np.float16, np.float32])
|
|
3897
|
+
@pytest.mark.parametrize("method", quantile_methods)
|
|
3898
|
+
def test_percentile_gh_29003(self, qtype, method):
|
|
3899
|
+
# test that with float16 or float32 input we do not get overflow
|
|
3900
|
+
zero = qtype(0)
|
|
3901
|
+
one = qtype(1)
|
|
3902
|
+
a = np.zeros(65521, qtype)
|
|
3903
|
+
a[:20_000] = one
|
|
3904
|
+
z = np.percentile(a, 50, method=method)
|
|
3905
|
+
assert z == zero
|
|
3906
|
+
assert z.dtype == a.dtype
|
|
3907
|
+
z = np.percentile(a, 99, method=method)
|
|
3908
|
+
assert z == one
|
|
3909
|
+
assert z.dtype == a.dtype
|
|
3910
|
+
|
|
3911
|
+
def test_percentile_gh_29003_Fraction(self):
|
|
3912
|
+
zero = Fraction(0)
|
|
3913
|
+
one = Fraction(1)
|
|
3914
|
+
a = np.array([zero] * 65521)
|
|
3915
|
+
a[:20_000] = one
|
|
3916
|
+
z = np.percentile(a, 50)
|
|
3917
|
+
assert z == zero
|
|
3918
|
+
z = np.percentile(a, Fraction(50))
|
|
3919
|
+
assert z == zero
|
|
3920
|
+
assert np.array(z).dtype == a.dtype
|
|
3921
|
+
|
|
3922
|
+
z = np.percentile(a, 99)
|
|
3923
|
+
assert z == one
|
|
3924
|
+
# test that with only Fraction input the return type is a Fraction
|
|
3925
|
+
z = np.percentile(a, Fraction(99))
|
|
3926
|
+
assert z == one
|
|
3927
|
+
assert np.array(z).dtype == a.dtype
|
|
3928
|
+
|
|
3929
|
+
@pytest.mark.parametrize("method", interpolating_quantile_methods)
|
|
3930
|
+
@pytest.mark.parametrize("q", [50, 10.0])
|
|
3931
|
+
def test_q_weak_promotion(self, method, q):
|
|
3932
|
+
a = np.array([1, 2, 3, 4, 5], dtype=np.float32)
|
|
3933
|
+
value = np.percentile(a, q, method=method)
|
|
3934
|
+
assert value.dtype == np.float32
|
|
3935
|
+
|
|
3936
|
+
@pytest.mark.parametrize("method", interpolating_quantile_methods)
|
|
3937
|
+
def test_q_strong_promotion(self, method):
|
|
3938
|
+
# For interpolating methods, the dtype should be float64, for
|
|
3939
|
+
# discrete ones the original int8. (technically, mid-point has no
|
|
3940
|
+
# reason to take into account `q`, but does so anyway.)
|
|
3941
|
+
a = np.array([1, 2, 3, 4, 5], dtype=np.float32)
|
|
3942
|
+
value = np.percentile(a, np.float64(50), method=method)
|
|
3943
|
+
assert value.dtype == np.float64
|
|
3944
|
+
|
|
3945
|
+
|
|
3946
|
+
class TestQuantile:
|
|
3947
|
+
# most of this is already tested by TestPercentile
|
|
3948
|
+
|
|
3949
|
+
def V(self, x, y, alpha):
|
|
3950
|
+
# Identification function used in several tests.
|
|
3951
|
+
return (x >= y) - alpha
|
|
3952
|
+
|
|
3953
|
+
def test_max_ulp(self):
|
|
3954
|
+
x = [0.0, 0.2, 0.4]
|
|
3955
|
+
a = np.quantile(x, 0.45)
|
|
3956
|
+
# The default linear method would result in 0 + 0.2 * (0.45/2) = 0.18.
|
|
3957
|
+
# 0.18 is not exactly representable and the formula leads to a 1 ULP
|
|
3958
|
+
# different result. Ensure it is this exact within 1 ULP, see gh-20331.
|
|
3959
|
+
np.testing.assert_array_max_ulp(a, 0.18, maxulp=1)
|
|
3960
|
+
|
|
3961
|
+
def test_basic(self):
|
|
3962
|
+
x = np.arange(8) * 0.5
|
|
3963
|
+
assert_equal(np.quantile(x, 0), 0.)
|
|
3964
|
+
assert_equal(np.quantile(x, 1), 3.5)
|
|
3965
|
+
assert_equal(np.quantile(x, 0.5), 1.75)
|
|
3966
|
+
|
|
3967
|
+
def test_correct_quantile_value(self):
|
|
3968
|
+
a = np.array([True])
|
|
3969
|
+
tf_quant = np.quantile(True, False)
|
|
3970
|
+
assert_equal(tf_quant, a[0])
|
|
3971
|
+
assert_equal(type(tf_quant), a.dtype)
|
|
3972
|
+
a = np.array([False, True, True])
|
|
3973
|
+
quant_res = np.quantile(a, a)
|
|
3974
|
+
assert_array_equal(quant_res, a)
|
|
3975
|
+
assert_equal(quant_res.dtype, a.dtype)
|
|
3976
|
+
|
|
3977
|
+
def test_fraction(self):
|
|
3978
|
+
# fractional input, integral quantile
|
|
3979
|
+
x = [Fraction(i, 2) for i in range(8)]
|
|
3980
|
+
q = np.quantile(x, 0)
|
|
3981
|
+
assert_equal(q, 0)
|
|
3982
|
+
assert_equal(type(q), Fraction)
|
|
3983
|
+
|
|
3984
|
+
q = np.quantile(x, 1)
|
|
3985
|
+
assert_equal(q, Fraction(7, 2))
|
|
3986
|
+
assert_equal(type(q), Fraction)
|
|
3987
|
+
|
|
3988
|
+
q = np.quantile(x, .5)
|
|
3989
|
+
assert_equal(q, 1.75)
|
|
3990
|
+
assert isinstance(q, float)
|
|
3991
|
+
|
|
3992
|
+
q = np.quantile(x, Fraction(1, 2))
|
|
3993
|
+
assert_equal(q, Fraction(7, 4))
|
|
3994
|
+
assert_equal(type(q), Fraction)
|
|
3995
|
+
|
|
3996
|
+
q = np.quantile(x, [Fraction(1, 2)])
|
|
3997
|
+
assert_equal(q, np.array([Fraction(7, 4)]))
|
|
3998
|
+
assert_equal(type(q), np.ndarray)
|
|
3999
|
+
|
|
4000
|
+
q = np.quantile(x, [[Fraction(1, 2)]])
|
|
4001
|
+
assert_equal(q, np.array([[Fraction(7, 4)]]))
|
|
4002
|
+
assert_equal(type(q), np.ndarray)
|
|
4003
|
+
|
|
4004
|
+
# repeat with integral input but fractional quantile
|
|
4005
|
+
x = np.arange(8)
|
|
4006
|
+
assert_equal(np.quantile(x, Fraction(1, 2)), Fraction(7, 2))
|
|
4007
|
+
|
|
4008
|
+
def test_complex(self):
|
|
4009
|
+
# gh-22652
|
|
4010
|
+
arr_c = np.array([0.5 + 3.0j, 2.1 + 0.5j, 1.6 + 2.3j], dtype='G')
|
|
4011
|
+
assert_raises(TypeError, np.quantile, arr_c, 0.5)
|
|
4012
|
+
arr_c = np.array([0.5 + 3.0j, 2.1 + 0.5j, 1.6 + 2.3j], dtype='D')
|
|
4013
|
+
assert_raises(TypeError, np.quantile, arr_c, 0.5)
|
|
4014
|
+
arr_c = np.array([0.5 + 3.0j, 2.1 + 0.5j, 1.6 + 2.3j], dtype='F')
|
|
4015
|
+
assert_raises(TypeError, np.quantile, arr_c, 0.5)
|
|
4016
|
+
|
|
4017
|
+
def test_no_p_overwrite(self):
|
|
4018
|
+
# this is worth retesting, because quantile does not make a copy
|
|
4019
|
+
p0 = np.array([0, 0.75, 0.25, 0.5, 1.0])
|
|
4020
|
+
p = p0.copy()
|
|
4021
|
+
np.quantile(np.arange(100.), p, method="midpoint")
|
|
4022
|
+
assert_array_equal(p, p0)
|
|
4023
|
+
|
|
4024
|
+
p0 = p0.tolist()
|
|
4025
|
+
p = p.tolist()
|
|
4026
|
+
np.quantile(np.arange(100.), p, method="midpoint")
|
|
4027
|
+
assert_array_equal(p, p0)
|
|
4028
|
+
|
|
4029
|
+
@pytest.mark.parametrize("dtype", np.typecodes["AllInteger"])
|
|
4030
|
+
def test_quantile_preserve_int_type(self, dtype):
|
|
4031
|
+
res = np.quantile(np.array([1, 2], dtype=dtype), [0.5],
|
|
4032
|
+
method="nearest")
|
|
4033
|
+
assert res.dtype == dtype
|
|
4034
|
+
|
|
4035
|
+
@pytest.mark.parametrize("method", quantile_methods)
|
|
4036
|
+
def test_q_zero_one(self, method):
|
|
4037
|
+
# gh-24710
|
|
4038
|
+
arr = [10, 11, 12]
|
|
4039
|
+
quantile = np.quantile(arr, q=[0, 1], method=method)
|
|
4040
|
+
assert_equal(quantile, np.array([10, 12]))
|
|
4041
|
+
|
|
4042
|
+
@pytest.mark.parametrize("method", quantile_methods)
|
|
4043
|
+
def test_quantile_monotonic(self, method):
|
|
4044
|
+
# GH 14685
|
|
4045
|
+
# test that the return value of quantile is monotonic if p0 is ordered
|
|
4046
|
+
# Also tests that the boundary values are not mishandled.
|
|
4047
|
+
p0 = np.linspace(0, 1, 101)
|
|
4048
|
+
quantile = np.quantile(np.array([0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 1, 1, 9, 9, 9,
|
|
4049
|
+
8, 8, 7]) * 0.1, p0, method=method)
|
|
4050
|
+
assert_equal(np.sort(quantile), quantile)
|
|
4051
|
+
|
|
4052
|
+
# Also test one where the number of data points is clearly divisible:
|
|
4053
|
+
quantile = np.quantile([0., 1., 2., 3.], p0, method=method)
|
|
4054
|
+
assert_equal(np.sort(quantile), quantile)
|
|
4055
|
+
|
|
4056
|
+
@hypothesis.given(
|
|
4057
|
+
arr=arrays(dtype=np.float64,
|
|
4058
|
+
shape=st.integers(min_value=3, max_value=1000),
|
|
4059
|
+
elements=st.floats(allow_infinity=False, allow_nan=False,
|
|
4060
|
+
min_value=-1e300, max_value=1e300)))
|
|
4061
|
+
def test_quantile_monotonic_hypo(self, arr):
|
|
4062
|
+
p0 = np.arange(0, 1, 0.01)
|
|
4063
|
+
quantile = np.quantile(arr, p0)
|
|
4064
|
+
assert_equal(np.sort(quantile), quantile)
|
|
4065
|
+
|
|
4066
|
+
def test_quantile_scalar_nan(self):
|
|
4067
|
+
a = np.array([[10., 7., 4.], [3., 2., 1.]])
|
|
4068
|
+
a[0][1] = np.nan
|
|
4069
|
+
actual = np.quantile(a, 0.5)
|
|
4070
|
+
assert np.isscalar(actual)
|
|
4071
|
+
assert_equal(np.quantile(a, 0.5), np.nan)
|
|
4072
|
+
|
|
4073
|
+
@pytest.mark.parametrize("weights", [False, True])
|
|
4074
|
+
@pytest.mark.parametrize("method", quantile_methods)
|
|
4075
|
+
@pytest.mark.parametrize("alpha", [0.2, 0.5, 0.9])
|
|
4076
|
+
def test_quantile_identification_equation(self, weights, method, alpha):
|
|
4077
|
+
# Test that the identification equation holds for the empirical
|
|
4078
|
+
# CDF:
|
|
4079
|
+
# E[V(x, Y)] = 0 <=> x is quantile
|
|
4080
|
+
# with Y the random variable for which we have observed values and
|
|
4081
|
+
# V(x, y) the canonical identification function for the quantile (at
|
|
4082
|
+
# level alpha), see
|
|
4083
|
+
# https://doi.org/10.48550/arXiv.0912.0902
|
|
4084
|
+
if weights and method not in methods_supporting_weights:
|
|
4085
|
+
pytest.skip("Weights not supported by method.")
|
|
4086
|
+
rng = np.random.default_rng(4321)
|
|
4087
|
+
# We choose n and alpha such that we cover 3 cases:
|
|
4088
|
+
# - n * alpha is an integer
|
|
4089
|
+
# - n * alpha is a float that gets rounded down
|
|
4090
|
+
# - n * alpha is a float that gest rounded up
|
|
4091
|
+
n = 102 # n * alpha = 20.4, 51. , 91.8
|
|
4092
|
+
y = rng.random(n)
|
|
4093
|
+
w = rng.integers(low=0, high=10, size=n) if weights else None
|
|
4094
|
+
x = np.quantile(y, alpha, method=method, weights=w)
|
|
4095
|
+
|
|
4096
|
+
if method in ("higher",):
|
|
4097
|
+
# These methods do not fulfill the identification equation.
|
|
4098
|
+
assert np.abs(np.mean(self.V(x, y, alpha))) > 0.1 / n
|
|
4099
|
+
elif int(n * alpha) == n * alpha and not weights:
|
|
4100
|
+
# We can expect exact results, up to machine precision.
|
|
4101
|
+
assert_allclose(
|
|
4102
|
+
np.average(self.V(x, y, alpha), weights=w), 0, atol=1e-14,
|
|
4103
|
+
)
|
|
4104
|
+
else:
|
|
4105
|
+
# V = (x >= y) - alpha cannot sum to zero exactly but within
|
|
4106
|
+
# "sample precision".
|
|
4107
|
+
assert_allclose(np.average(self.V(x, y, alpha), weights=w), 0,
|
|
4108
|
+
atol=1 / n / np.amin([alpha, 1 - alpha]))
|
|
4109
|
+
|
|
4110
|
+
@pytest.mark.parametrize("weights", [False, True])
|
|
4111
|
+
@pytest.mark.parametrize("method", quantile_methods)
|
|
4112
|
+
@pytest.mark.parametrize("alpha", [0.2, 0.5, 0.9])
|
|
4113
|
+
def test_quantile_add_and_multiply_constant(self, weights, method, alpha):
|
|
4114
|
+
# Test that
|
|
4115
|
+
# 1. quantile(c + x) = c + quantile(x)
|
|
4116
|
+
# 2. quantile(c * x) = c * quantile(x)
|
|
4117
|
+
# 3. quantile(-x) = -quantile(x, 1 - alpha)
|
|
4118
|
+
# On empirical quantiles, this equation does not hold exactly.
|
|
4119
|
+
# Koenker (2005) "Quantile Regression" Chapter 2.2.3 calls these
|
|
4120
|
+
# properties equivariance.
|
|
4121
|
+
if weights and method not in methods_supporting_weights:
|
|
4122
|
+
pytest.skip("Weights not supported by method.")
|
|
4123
|
+
rng = np.random.default_rng(4321)
|
|
4124
|
+
# We choose n and alpha such that we have cases for
|
|
4125
|
+
# - n * alpha is an integer
|
|
4126
|
+
# - n * alpha is a float that gets rounded down
|
|
4127
|
+
# - n * alpha is a float that gest rounded up
|
|
4128
|
+
n = 102 # n * alpha = 20.4, 51. , 91.8
|
|
4129
|
+
y = rng.random(n)
|
|
4130
|
+
w = rng.integers(low=0, high=10, size=n) if weights else None
|
|
4131
|
+
q = np.quantile(y, alpha, method=method, weights=w)
|
|
4132
|
+
c = 13.5
|
|
4133
|
+
|
|
4134
|
+
# 1
|
|
4135
|
+
assert_allclose(np.quantile(c + y, alpha, method=method, weights=w),
|
|
4136
|
+
c + q)
|
|
4137
|
+
# 2
|
|
4138
|
+
assert_allclose(np.quantile(c * y, alpha, method=method, weights=w),
|
|
4139
|
+
c * q)
|
|
4140
|
+
# 3
|
|
4141
|
+
if weights:
|
|
4142
|
+
# From here on, we would need more methods to support weights.
|
|
4143
|
+
return
|
|
4144
|
+
q = -np.quantile(-y, 1 - alpha, method=method)
|
|
4145
|
+
if method == "inverted_cdf":
|
|
4146
|
+
if (
|
|
4147
|
+
n * alpha == int(n * alpha)
|
|
4148
|
+
or np.round(n * alpha) == int(n * alpha) + 1
|
|
4149
|
+
):
|
|
4150
|
+
assert_allclose(q, np.quantile(y, alpha, method="higher"))
|
|
4151
|
+
else:
|
|
4152
|
+
assert_allclose(q, np.quantile(y, alpha, method="lower"))
|
|
4153
|
+
elif method == "closest_observation":
|
|
4154
|
+
if n * alpha == int(n * alpha):
|
|
4155
|
+
assert_allclose(q, np.quantile(y, alpha, method="higher"))
|
|
4156
|
+
elif np.round(n * alpha) == int(n * alpha) + 1:
|
|
4157
|
+
assert_allclose(
|
|
4158
|
+
q, np.quantile(y, alpha + 1 / n, method="higher"))
|
|
4159
|
+
else:
|
|
4160
|
+
assert_allclose(q, np.quantile(y, alpha, method="lower"))
|
|
4161
|
+
elif method == "interpolated_inverted_cdf":
|
|
4162
|
+
assert_allclose(q, np.quantile(y, alpha + 1 / n, method=method))
|
|
4163
|
+
elif method == "nearest":
|
|
4164
|
+
if n * alpha == int(n * alpha):
|
|
4165
|
+
assert_allclose(q, np.quantile(y, alpha + 1 / n, method=method))
|
|
4166
|
+
else:
|
|
4167
|
+
assert_allclose(q, np.quantile(y, alpha, method=method))
|
|
4168
|
+
elif method == "lower":
|
|
4169
|
+
assert_allclose(q, np.quantile(y, alpha, method="higher"))
|
|
4170
|
+
elif method == "higher":
|
|
4171
|
+
assert_allclose(q, np.quantile(y, alpha, method="lower"))
|
|
4172
|
+
else:
|
|
4173
|
+
# "averaged_inverted_cdf", "hazen", "weibull", "linear",
|
|
4174
|
+
# "median_unbiased", "normal_unbiased", "midpoint"
|
|
4175
|
+
assert_allclose(q, np.quantile(y, alpha, method=method))
|
|
4176
|
+
|
|
4177
|
+
@pytest.mark.parametrize("method", methods_supporting_weights)
|
|
4178
|
+
@pytest.mark.parametrize("alpha", [0.2, 0.5, 0.9])
|
|
4179
|
+
def test_quantile_constant_weights(self, method, alpha):
|
|
4180
|
+
rng = np.random.default_rng(4321)
|
|
4181
|
+
# We choose n and alpha such that we have cases for
|
|
4182
|
+
# - n * alpha is an integer
|
|
4183
|
+
# - n * alpha is a float that gets rounded down
|
|
4184
|
+
# - n * alpha is a float that gest rounded up
|
|
4185
|
+
n = 102 # n * alpha = 20.4, 51. , 91.8
|
|
4186
|
+
y = rng.random(n)
|
|
4187
|
+
q = np.quantile(y, alpha, method=method)
|
|
4188
|
+
|
|
4189
|
+
w = np.ones_like(y)
|
|
4190
|
+
qw = np.quantile(y, alpha, method=method, weights=w)
|
|
4191
|
+
assert_allclose(qw, q)
|
|
4192
|
+
|
|
4193
|
+
w = 8.125 * np.ones_like(y)
|
|
4194
|
+
qw = np.quantile(y, alpha, method=method, weights=w)
|
|
4195
|
+
assert_allclose(qw, q)
|
|
4196
|
+
|
|
4197
|
+
@pytest.mark.parametrize("method", methods_supporting_weights)
|
|
4198
|
+
@pytest.mark.parametrize("alpha", [0, 0.2, 0.5, 0.9, 1])
|
|
4199
|
+
def test_quantile_with_integer_weights(self, method, alpha):
|
|
4200
|
+
# Integer weights can be interpreted as repeated observations.
|
|
4201
|
+
rng = np.random.default_rng(4321)
|
|
4202
|
+
# We choose n and alpha such that we have cases for
|
|
4203
|
+
# - n * alpha is an integer
|
|
4204
|
+
# - n * alpha is a float that gets rounded down
|
|
4205
|
+
# - n * alpha is a float that gest rounded up
|
|
4206
|
+
n = 102 # n * alpha = 20.4, 51. , 91.8
|
|
4207
|
+
y = rng.random(n)
|
|
4208
|
+
w = rng.integers(low=0, high=10, size=n, dtype=np.int32)
|
|
4209
|
+
|
|
4210
|
+
qw = np.quantile(y, alpha, method=method, weights=w)
|
|
4211
|
+
q = np.quantile(np.repeat(y, w), alpha, method=method)
|
|
4212
|
+
assert_allclose(qw, q)
|
|
4213
|
+
|
|
4214
|
+
@pytest.mark.parametrize("method", methods_supporting_weights)
|
|
4215
|
+
def test_quantile_with_weights_and_axis(self, method):
|
|
4216
|
+
rng = np.random.default_rng(4321)
|
|
4217
|
+
|
|
4218
|
+
# 1d weight and single alpha
|
|
4219
|
+
y = rng.random((2, 10, 3))
|
|
4220
|
+
w = np.abs(rng.random(10))
|
|
4221
|
+
alpha = 0.5
|
|
4222
|
+
q = np.quantile(y, alpha, weights=w, method=method, axis=1)
|
|
4223
|
+
q_res = np.zeros(shape=(2, 3))
|
|
4224
|
+
for i in range(2):
|
|
4225
|
+
for j in range(3):
|
|
4226
|
+
q_res[i, j] = np.quantile(
|
|
4227
|
+
y[i, :, j], alpha, method=method, weights=w
|
|
4228
|
+
)
|
|
4229
|
+
assert_allclose(q, q_res)
|
|
4230
|
+
|
|
4231
|
+
# 1d weight and 1d alpha
|
|
4232
|
+
alpha = [0, 0.2, 0.4, 0.6, 0.8, 1] # shape (6,)
|
|
4233
|
+
q = np.quantile(y, alpha, weights=w, method=method, axis=1)
|
|
4234
|
+
q_res = np.zeros(shape=(6, 2, 3))
|
|
4235
|
+
for i in range(2):
|
|
4236
|
+
for j in range(3):
|
|
4237
|
+
q_res[:, i, j] = np.quantile(
|
|
4238
|
+
y[i, :, j], alpha, method=method, weights=w
|
|
4239
|
+
)
|
|
4240
|
+
assert_allclose(q, q_res)
|
|
4241
|
+
|
|
4242
|
+
# 1d weight and 2d alpha
|
|
4243
|
+
alpha = [[0, 0.2], [0.4, 0.6], [0.8, 1]] # shape (3, 2)
|
|
4244
|
+
q = np.quantile(y, alpha, weights=w, method=method, axis=1)
|
|
4245
|
+
q_res = q_res.reshape((3, 2, 2, 3))
|
|
4246
|
+
assert_allclose(q, q_res)
|
|
4247
|
+
|
|
4248
|
+
# shape of weights equals shape of y
|
|
4249
|
+
w = np.abs(rng.random((2, 10, 3)))
|
|
4250
|
+
alpha = 0.5
|
|
4251
|
+
q = np.quantile(y, alpha, weights=w, method=method, axis=1)
|
|
4252
|
+
q_res = np.zeros(shape=(2, 3))
|
|
4253
|
+
for i in range(2):
|
|
4254
|
+
for j in range(3):
|
|
4255
|
+
q_res[i, j] = np.quantile(
|
|
4256
|
+
y[i, :, j], alpha, method=method, weights=w[i, :, j]
|
|
4257
|
+
)
|
|
4258
|
+
assert_allclose(q, q_res)
|
|
4259
|
+
|
|
4260
|
+
# axis is a tuple of all axes
|
|
4261
|
+
q = np.quantile(y, alpha, weights=w, method=method, axis=(0, 1, 2))
|
|
4262
|
+
q_res = np.quantile(y, alpha, weights=w, method=method, axis=None)
|
|
4263
|
+
assert_allclose(q, q_res)
|
|
4264
|
+
|
|
4265
|
+
q = np.quantile(y, alpha, weights=w, method=method, axis=(1, 2))
|
|
4266
|
+
q_res = np.zeros(shape=(2,))
|
|
4267
|
+
for i in range(2):
|
|
4268
|
+
q_res[i] = np.quantile(y[i], alpha, weights=w[i], method=method)
|
|
4269
|
+
assert_allclose(q, q_res)
|
|
4270
|
+
|
|
4271
|
+
@pytest.mark.parametrize("method", methods_supporting_weights)
|
|
4272
|
+
def test_quantile_weights_min_max(self, method):
|
|
4273
|
+
# Test weighted quantile at 0 and 1 with leading and trailing zero
|
|
4274
|
+
# weights.
|
|
4275
|
+
w = [0, 0, 1, 2, 3, 0]
|
|
4276
|
+
y = np.arange(6)
|
|
4277
|
+
y_min = np.quantile(y, 0, weights=w, method="inverted_cdf")
|
|
4278
|
+
y_max = np.quantile(y, 1, weights=w, method="inverted_cdf")
|
|
4279
|
+
assert y_min == y[2] # == 2
|
|
4280
|
+
assert y_max == y[4] # == 4
|
|
4281
|
+
|
|
4282
|
+
def test_quantile_weights_raises_negative_weights(self):
|
|
4283
|
+
y = [1, 2]
|
|
4284
|
+
w = [-0.5, 1]
|
|
4285
|
+
with pytest.raises(ValueError, match="Weights must be non-negative"):
|
|
4286
|
+
np.quantile(y, 0.5, weights=w, method="inverted_cdf")
|
|
4287
|
+
|
|
4288
|
+
@pytest.mark.parametrize(
|
|
4289
|
+
"method",
|
|
4290
|
+
sorted(set(quantile_methods) - set(methods_supporting_weights)),
|
|
4291
|
+
)
|
|
4292
|
+
def test_quantile_weights_raises_unsupported_methods(self, method):
|
|
4293
|
+
y = [1, 2]
|
|
4294
|
+
w = [0.5, 1]
|
|
4295
|
+
msg = "Only method 'inverted_cdf' supports weights"
|
|
4296
|
+
with pytest.raises(ValueError, match=msg):
|
|
4297
|
+
np.quantile(y, 0.5, weights=w, method=method)
|
|
4298
|
+
|
|
4299
|
+
def test_weibull_fraction(self):
|
|
4300
|
+
arr = [Fraction(0, 1), Fraction(1, 10)]
|
|
4301
|
+
quantile = np.quantile(arr, [0, ], method='weibull')
|
|
4302
|
+
assert_equal(quantile, np.array(Fraction(0, 1)))
|
|
4303
|
+
quantile = np.quantile(arr, [Fraction(1, 2)], method='weibull')
|
|
4304
|
+
assert_equal(quantile, np.array(Fraction(1, 20)))
|
|
4305
|
+
|
|
4306
|
+
def test_closest_observation(self):
|
|
4307
|
+
# Round ties to nearest even order statistic (see #26656)
|
|
4308
|
+
m = 'closest_observation'
|
|
4309
|
+
q = 0.5
|
|
4310
|
+
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
4311
|
+
assert_equal(2, np.quantile(arr[0:3], q, method=m))
|
|
4312
|
+
assert_equal(2, np.quantile(arr[0:4], q, method=m))
|
|
4313
|
+
assert_equal(2, np.quantile(arr[0:5], q, method=m))
|
|
4314
|
+
assert_equal(3, np.quantile(arr[0:6], q, method=m))
|
|
4315
|
+
assert_equal(4, np.quantile(arr[0:7], q, method=m))
|
|
4316
|
+
assert_equal(4, np.quantile(arr[0:8], q, method=m))
|
|
4317
|
+
assert_equal(4, np.quantile(arr[0:9], q, method=m))
|
|
4318
|
+
assert_equal(5, np.quantile(arr, q, method=m))
|
|
4319
|
+
|
|
4320
|
+
@pytest.mark.parametrize("weights",
|
|
4321
|
+
[[1, np.inf, 1, 1], [1, np.inf, 1, np.inf], [0, 0, 0, 0],
|
|
4322
|
+
[np.finfo("float64").max] * 4])
|
|
4323
|
+
@pytest.mark.parametrize("dty", ["f8", "O"])
|
|
4324
|
+
def test_inf_zeroes_err(self, weights, dty):
|
|
4325
|
+
m = "inverted_cdf"
|
|
4326
|
+
q = 0.5
|
|
4327
|
+
arr = np.array([[1, 2, 3, 4]] * 2)
|
|
4328
|
+
# Make one entry have bad weights and another good ones.
|
|
4329
|
+
wgts = np.array([weights, [0.5] * 4], dtype=dty)
|
|
4330
|
+
with pytest.raises(ValueError,
|
|
4331
|
+
match=r"Weights included NaN, inf or were all zero"):
|
|
4332
|
+
# We (currently) don't bother to check ahead so 0/0 or
|
|
4333
|
+
# overflow to `inf` while summing weights, or `inf / inf`
|
|
4334
|
+
# will all warn before the error is raised.
|
|
4335
|
+
with np.errstate(all="ignore"):
|
|
4336
|
+
a = np.quantile(arr, q, weights=wgts, method=m, axis=1)
|
|
4337
|
+
|
|
4338
|
+
@pytest.mark.parametrize("weights",
|
|
4339
|
+
[[1, np.nan, 1, 1], [1, np.nan, np.nan, 1]])
|
|
4340
|
+
@pytest.mark.parametrize(["err", "dty"],
|
|
4341
|
+
[(ValueError, "f8"), ((RuntimeWarning, ValueError), "O")])
|
|
4342
|
+
def test_nan_err(self, err, dty, weights):
|
|
4343
|
+
m = "inverted_cdf"
|
|
4344
|
+
q = 0.5
|
|
4345
|
+
arr = np.array([[1, 2, 3, 4]] * 2)
|
|
4346
|
+
# Make one entry have bad weights and another good ones.
|
|
4347
|
+
wgts = np.array([weights, [0.5] * 4], dtype=dty)
|
|
4348
|
+
with pytest.raises(err):
|
|
4349
|
+
a = np.quantile(arr, q, weights=wgts, method=m)
|
|
4350
|
+
|
|
4351
|
+
def test_quantile_gh_29003_Fraction(self):
|
|
4352
|
+
r = np.quantile([1, 2], q=Fraction(1))
|
|
4353
|
+
assert r == Fraction(2)
|
|
4354
|
+
assert isinstance(r, Fraction)
|
|
4355
|
+
|
|
4356
|
+
r = np.quantile([1, 2], q=Fraction(.5))
|
|
4357
|
+
assert r == Fraction(3, 2)
|
|
4358
|
+
assert isinstance(r, Fraction)
|
|
4359
|
+
|
|
4360
|
+
def test_float16_gh_29003(self):
|
|
4361
|
+
a = np.arange(50_001, dtype=np.float16)
|
|
4362
|
+
q = .999
|
|
4363
|
+
value = np.quantile(a, q)
|
|
4364
|
+
assert value == q * 50_000
|
|
4365
|
+
assert value.dtype == np.float16
|
|
4366
|
+
|
|
4367
|
+
@pytest.mark.parametrize("method", interpolating_quantile_methods)
|
|
4368
|
+
@pytest.mark.parametrize("q", [0.5, 1])
|
|
4369
|
+
def test_q_weak_promotion(self, method, q):
|
|
4370
|
+
a = np.array([1, 2, 3, 4, 5], dtype=np.float32)
|
|
4371
|
+
value = np.quantile(a, q, method=method)
|
|
4372
|
+
assert value.dtype == np.float32
|
|
4373
|
+
|
|
4374
|
+
@pytest.mark.parametrize("method", interpolating_quantile_methods)
|
|
4375
|
+
def test_q_strong_promotion(self, method):
|
|
4376
|
+
# For interpolating methods, the dtype should be float64, for
|
|
4377
|
+
# discrete ones the original int8. (technically, mid-point has no
|
|
4378
|
+
# reason to take into account `q`, but does so anyway.)
|
|
4379
|
+
a = np.array([1, 2, 3, 4, 5], dtype=np.float32)
|
|
4380
|
+
value = np.quantile(a, np.float64(0.5), method=method)
|
|
4381
|
+
assert value.dtype == np.float64
|
|
4382
|
+
|
|
4383
|
+
|
|
4384
|
+
class TestLerp:
|
|
4385
|
+
@hypothesis.given(t0=st.floats(allow_nan=False, allow_infinity=False,
|
|
4386
|
+
min_value=0, max_value=1),
|
|
4387
|
+
t1=st.floats(allow_nan=False, allow_infinity=False,
|
|
4388
|
+
min_value=0, max_value=1),
|
|
4389
|
+
a=st.floats(allow_nan=False, allow_infinity=False,
|
|
4390
|
+
min_value=-1e300, max_value=1e300),
|
|
4391
|
+
b=st.floats(allow_nan=False, allow_infinity=False,
|
|
4392
|
+
min_value=-1e300, max_value=1e300))
|
|
4393
|
+
def test_linear_interpolation_formula_monotonic(self, t0, t1, a, b):
|
|
4394
|
+
l0 = nfb._lerp(a, b, t0)
|
|
4395
|
+
l1 = nfb._lerp(a, b, t1)
|
|
4396
|
+
if t0 == t1 or a == b:
|
|
4397
|
+
assert l0 == l1 # uninteresting
|
|
4398
|
+
elif (t0 < t1) == (a < b):
|
|
4399
|
+
assert l0 <= l1
|
|
4400
|
+
else:
|
|
4401
|
+
assert l0 >= l1
|
|
4402
|
+
|
|
4403
|
+
@hypothesis.given(t=st.floats(allow_nan=False, allow_infinity=False,
|
|
4404
|
+
min_value=0, max_value=1),
|
|
4405
|
+
a=st.floats(allow_nan=False, allow_infinity=False,
|
|
4406
|
+
min_value=-1e300, max_value=1e300),
|
|
4407
|
+
b=st.floats(allow_nan=False, allow_infinity=False,
|
|
4408
|
+
min_value=-1e300, max_value=1e300))
|
|
4409
|
+
def test_linear_interpolation_formula_bounded(self, t, a, b):
|
|
4410
|
+
if a <= b:
|
|
4411
|
+
assert a <= nfb._lerp(a, b, t) <= b
|
|
4412
|
+
else:
|
|
4413
|
+
assert b <= nfb._lerp(a, b, t) <= a
|
|
4414
|
+
|
|
4415
|
+
@hypothesis.given(t=st.floats(allow_nan=False, allow_infinity=False,
|
|
4416
|
+
min_value=0, max_value=1),
|
|
4417
|
+
a=st.floats(allow_nan=False, allow_infinity=False,
|
|
4418
|
+
min_value=-1e300, max_value=1e300),
|
|
4419
|
+
b=st.floats(allow_nan=False, allow_infinity=False,
|
|
4420
|
+
min_value=-1e300, max_value=1e300))
|
|
4421
|
+
def test_linear_interpolation_formula_symmetric(self, t, a, b):
|
|
4422
|
+
# double subtraction is needed to remove the extra precision of t < 0.5
|
|
4423
|
+
left = nfb._lerp(a, b, 1 - (1 - t))
|
|
4424
|
+
right = nfb._lerp(b, a, 1 - t)
|
|
4425
|
+
assert_allclose(left, right)
|
|
4426
|
+
|
|
4427
|
+
def test_linear_interpolation_formula_0d_inputs(self):
|
|
4428
|
+
a = np.array(2)
|
|
4429
|
+
b = np.array(5)
|
|
4430
|
+
t = np.array(0.2)
|
|
4431
|
+
assert nfb._lerp(a, b, t) == 2.6
|
|
4432
|
+
|
|
4433
|
+
|
|
4434
|
+
class TestMedian:
|
|
4435
|
+
|
|
4436
|
+
def test_basic(self):
|
|
4437
|
+
a0 = np.array(1)
|
|
4438
|
+
a1 = np.arange(2)
|
|
4439
|
+
a2 = np.arange(6).reshape(2, 3)
|
|
4440
|
+
assert_equal(np.median(a0), 1)
|
|
4441
|
+
assert_allclose(np.median(a1), 0.5)
|
|
4442
|
+
assert_allclose(np.median(a2), 2.5)
|
|
4443
|
+
assert_allclose(np.median(a2, axis=0), [1.5, 2.5, 3.5])
|
|
4444
|
+
assert_equal(np.median(a2, axis=1), [1, 4])
|
|
4445
|
+
assert_allclose(np.median(a2, axis=None), 2.5)
|
|
4446
|
+
|
|
4447
|
+
a = np.array([0.0444502, 0.0463301, 0.141249, 0.0606775])
|
|
4448
|
+
assert_almost_equal((a[1] + a[3]) / 2., np.median(a))
|
|
4449
|
+
a = np.array([0.0463301, 0.0444502, 0.141249])
|
|
4450
|
+
assert_equal(a[0], np.median(a))
|
|
4451
|
+
a = np.array([0.0444502, 0.141249, 0.0463301])
|
|
4452
|
+
assert_equal(a[-1], np.median(a))
|
|
4453
|
+
# check array scalar result
|
|
4454
|
+
assert_equal(np.median(a).ndim, 0)
|
|
4455
|
+
a[1] = np.nan
|
|
4456
|
+
assert_equal(np.median(a).ndim, 0)
|
|
4457
|
+
|
|
4458
|
+
def test_axis_keyword(self):
|
|
4459
|
+
a3 = np.array([[2, 3],
|
|
4460
|
+
[0, 1],
|
|
4461
|
+
[6, 7],
|
|
4462
|
+
[4, 5]])
|
|
4463
|
+
for a in [a3, np.random.randint(0, 100, size=(2, 3, 4))]:
|
|
4464
|
+
orig = a.copy()
|
|
4465
|
+
np.median(a, axis=None)
|
|
4466
|
+
for ax in range(a.ndim):
|
|
4467
|
+
np.median(a, axis=ax)
|
|
4468
|
+
assert_array_equal(a, orig)
|
|
4469
|
+
|
|
4470
|
+
assert_allclose(np.median(a3, axis=0), [3, 4])
|
|
4471
|
+
assert_allclose(np.median(a3.T, axis=1), [3, 4])
|
|
4472
|
+
assert_allclose(np.median(a3), 3.5)
|
|
4473
|
+
assert_allclose(np.median(a3, axis=None), 3.5)
|
|
4474
|
+
assert_allclose(np.median(a3.T), 3.5)
|
|
4475
|
+
|
|
4476
|
+
def test_overwrite_keyword(self):
|
|
4477
|
+
a3 = np.array([[2, 3],
|
|
4478
|
+
[0, 1],
|
|
4479
|
+
[6, 7],
|
|
4480
|
+
[4, 5]])
|
|
4481
|
+
a0 = np.array(1)
|
|
4482
|
+
a1 = np.arange(2)
|
|
4483
|
+
a2 = np.arange(6).reshape(2, 3)
|
|
4484
|
+
assert_allclose(np.median(a0.copy(), overwrite_input=True), 1)
|
|
4485
|
+
assert_allclose(np.median(a1.copy(), overwrite_input=True), 0.5)
|
|
4486
|
+
assert_allclose(np.median(a2.copy(), overwrite_input=True), 2.5)
|
|
4487
|
+
assert_allclose(
|
|
4488
|
+
np.median(a2.copy(), overwrite_input=True, axis=0), [1.5, 2.5, 3.5])
|
|
4489
|
+
assert_allclose(
|
|
4490
|
+
np.median(a2.copy(), overwrite_input=True, axis=1), [1, 4])
|
|
4491
|
+
assert_allclose(
|
|
4492
|
+
np.median(a2.copy(), overwrite_input=True, axis=None), 2.5)
|
|
4493
|
+
assert_allclose(
|
|
4494
|
+
np.median(a3.copy(), overwrite_input=True, axis=0), [3, 4])
|
|
4495
|
+
assert_allclose(
|
|
4496
|
+
np.median(a3.T.copy(), overwrite_input=True, axis=1), [3, 4])
|
|
4497
|
+
|
|
4498
|
+
a4 = np.arange(3 * 4 * 5, dtype=np.float32).reshape((3, 4, 5))
|
|
4499
|
+
np.random.shuffle(a4.ravel())
|
|
4500
|
+
assert_allclose(np.median(a4, axis=None),
|
|
4501
|
+
np.median(a4.copy(), axis=None, overwrite_input=True))
|
|
4502
|
+
assert_allclose(np.median(a4, axis=0),
|
|
4503
|
+
np.median(a4.copy(), axis=0, overwrite_input=True))
|
|
4504
|
+
assert_allclose(np.median(a4, axis=1),
|
|
4505
|
+
np.median(a4.copy(), axis=1, overwrite_input=True))
|
|
4506
|
+
assert_allclose(np.median(a4, axis=2),
|
|
4507
|
+
np.median(a4.copy(), axis=2, overwrite_input=True))
|
|
4508
|
+
|
|
4509
|
+
def test_array_like(self):
|
|
4510
|
+
x = [1, 2, 3]
|
|
4511
|
+
assert_almost_equal(np.median(x), 2)
|
|
4512
|
+
x2 = [x]
|
|
4513
|
+
assert_almost_equal(np.median(x2), 2)
|
|
4514
|
+
assert_allclose(np.median(x2, axis=0), x)
|
|
4515
|
+
|
|
4516
|
+
def test_subclass(self):
|
|
4517
|
+
# gh-3846
|
|
4518
|
+
class MySubClass(np.ndarray):
|
|
4519
|
+
|
|
4520
|
+
def __new__(cls, input_array, info=None):
|
|
4521
|
+
obj = np.asarray(input_array).view(cls)
|
|
4522
|
+
obj.info = info
|
|
4523
|
+
return obj
|
|
4524
|
+
|
|
4525
|
+
def mean(self, axis=None, dtype=None, out=None):
|
|
4526
|
+
return -7
|
|
4527
|
+
|
|
4528
|
+
a = MySubClass([1, 2, 3])
|
|
4529
|
+
assert_equal(np.median(a), -7)
|
|
4530
|
+
|
|
4531
|
+
@pytest.mark.parametrize('arr',
|
|
4532
|
+
([1., 2., 3.], [1., np.nan, 3.], np.nan, 0.))
|
|
4533
|
+
def test_subclass2(self, arr):
|
|
4534
|
+
"""Check that we return subclasses, even if a NaN scalar."""
|
|
4535
|
+
class MySubclass(np.ndarray):
|
|
4536
|
+
pass
|
|
4537
|
+
|
|
4538
|
+
m = np.median(np.array(arr).view(MySubclass))
|
|
4539
|
+
assert isinstance(m, MySubclass)
|
|
4540
|
+
|
|
4541
|
+
def test_out(self):
|
|
4542
|
+
o = np.zeros((4,))
|
|
4543
|
+
d = np.ones((3, 4))
|
|
4544
|
+
assert_equal(np.median(d, 0, out=o), o)
|
|
4545
|
+
o = np.zeros((3,))
|
|
4546
|
+
assert_equal(np.median(d, 1, out=o), o)
|
|
4547
|
+
o = np.zeros(())
|
|
4548
|
+
assert_equal(np.median(d, out=o), o)
|
|
4549
|
+
|
|
4550
|
+
def test_out_nan(self):
|
|
4551
|
+
with warnings.catch_warnings(record=True):
|
|
4552
|
+
warnings.filterwarnings('always', '', RuntimeWarning)
|
|
4553
|
+
o = np.zeros((4,))
|
|
4554
|
+
d = np.ones((3, 4))
|
|
4555
|
+
d[2, 1] = np.nan
|
|
4556
|
+
assert_equal(np.median(d, 0, out=o), o)
|
|
4557
|
+
o = np.zeros((3,))
|
|
4558
|
+
assert_equal(np.median(d, 1, out=o), o)
|
|
4559
|
+
o = np.zeros(())
|
|
4560
|
+
assert_equal(np.median(d, out=o), o)
|
|
4561
|
+
|
|
4562
|
+
def test_nan_behavior(self):
|
|
4563
|
+
a = np.arange(24, dtype=float)
|
|
4564
|
+
a[2] = np.nan
|
|
4565
|
+
assert_equal(np.median(a), np.nan)
|
|
4566
|
+
assert_equal(np.median(a, axis=0), np.nan)
|
|
4567
|
+
|
|
4568
|
+
a = np.arange(24, dtype=float).reshape(2, 3, 4)
|
|
4569
|
+
a[1, 2, 3] = np.nan
|
|
4570
|
+
a[1, 1, 2] = np.nan
|
|
4571
|
+
|
|
4572
|
+
# no axis
|
|
4573
|
+
assert_equal(np.median(a), np.nan)
|
|
4574
|
+
assert_equal(np.median(a).ndim, 0)
|
|
4575
|
+
|
|
4576
|
+
# axis0
|
|
4577
|
+
b = np.median(np.arange(24, dtype=float).reshape(2, 3, 4), 0)
|
|
4578
|
+
b[2, 3] = np.nan
|
|
4579
|
+
b[1, 2] = np.nan
|
|
4580
|
+
assert_equal(np.median(a, 0), b)
|
|
4581
|
+
|
|
4582
|
+
# axis1
|
|
4583
|
+
b = np.median(np.arange(24, dtype=float).reshape(2, 3, 4), 1)
|
|
4584
|
+
b[1, 3] = np.nan
|
|
4585
|
+
b[1, 2] = np.nan
|
|
4586
|
+
assert_equal(np.median(a, 1), b)
|
|
4587
|
+
|
|
4588
|
+
# axis02
|
|
4589
|
+
b = np.median(np.arange(24, dtype=float).reshape(2, 3, 4), (0, 2))
|
|
4590
|
+
b[1] = np.nan
|
|
4591
|
+
b[2] = np.nan
|
|
4592
|
+
assert_equal(np.median(a, (0, 2)), b)
|
|
4593
|
+
|
|
4594
|
+
@pytest.mark.skipif(IS_WASM, reason="fp errors don't work correctly")
|
|
4595
|
+
def test_empty(self):
|
|
4596
|
+
# mean(empty array) emits two warnings: empty slice and divide by 0
|
|
4597
|
+
a = np.array([], dtype=float)
|
|
4598
|
+
with warnings.catch_warnings(record=True) as w:
|
|
4599
|
+
warnings.filterwarnings('always', '', RuntimeWarning)
|
|
4600
|
+
assert_equal(np.median(a), np.nan)
|
|
4601
|
+
assert_(w[0].category is RuntimeWarning)
|
|
4602
|
+
assert_equal(len(w), 2)
|
|
4603
|
+
|
|
4604
|
+
# multiple dimensions
|
|
4605
|
+
a = np.array([], dtype=float, ndmin=3)
|
|
4606
|
+
# no axis
|
|
4607
|
+
with warnings.catch_warnings(record=True) as w:
|
|
4608
|
+
warnings.filterwarnings('always', '', RuntimeWarning)
|
|
4609
|
+
assert_equal(np.median(a), np.nan)
|
|
4610
|
+
assert_(w[0].category is RuntimeWarning)
|
|
4611
|
+
|
|
4612
|
+
# axis 0 and 1
|
|
4613
|
+
b = np.array([], dtype=float, ndmin=2)
|
|
4614
|
+
assert_equal(np.median(a, axis=0), b)
|
|
4615
|
+
assert_equal(np.median(a, axis=1), b)
|
|
4616
|
+
|
|
4617
|
+
# axis 2
|
|
4618
|
+
b = np.array(np.nan, dtype=float, ndmin=2)
|
|
4619
|
+
with warnings.catch_warnings(record=True) as w:
|
|
4620
|
+
warnings.filterwarnings('always', '', RuntimeWarning)
|
|
4621
|
+
assert_equal(np.median(a, axis=2), b)
|
|
4622
|
+
assert_(w[0].category is RuntimeWarning)
|
|
4623
|
+
|
|
4624
|
+
def test_object(self):
|
|
4625
|
+
o = np.arange(7.)
|
|
4626
|
+
assert_(type(np.median(o.astype(object))), float)
|
|
4627
|
+
o[2] = np.nan
|
|
4628
|
+
assert_(type(np.median(o.astype(object))), float)
|
|
4629
|
+
|
|
4630
|
+
def test_extended_axis(self):
|
|
4631
|
+
o = np.random.normal(size=(71, 23))
|
|
4632
|
+
x = np.dstack([o] * 10)
|
|
4633
|
+
assert_equal(np.median(x, axis=(0, 1)), np.median(o))
|
|
4634
|
+
x = np.moveaxis(x, -1, 0)
|
|
4635
|
+
assert_equal(np.median(x, axis=(-2, -1)), np.median(o))
|
|
4636
|
+
x = x.swapaxes(0, 1).copy()
|
|
4637
|
+
assert_equal(np.median(x, axis=(0, -1)), np.median(o))
|
|
4638
|
+
|
|
4639
|
+
assert_equal(np.median(x, axis=(0, 1, 2)), np.median(x, axis=None))
|
|
4640
|
+
assert_equal(np.median(x, axis=(0, )), np.median(x, axis=0))
|
|
4641
|
+
assert_equal(np.median(x, axis=(-1, )), np.median(x, axis=-1))
|
|
4642
|
+
|
|
4643
|
+
d = np.arange(3 * 5 * 7 * 11).reshape((3, 5, 7, 11))
|
|
4644
|
+
np.random.shuffle(d.ravel())
|
|
4645
|
+
assert_equal(np.median(d, axis=(0, 1, 2))[0],
|
|
4646
|
+
np.median(d[:, :, :, 0].flatten()))
|
|
4647
|
+
assert_equal(np.median(d, axis=(0, 1, 3))[1],
|
|
4648
|
+
np.median(d[:, :, 1, :].flatten()))
|
|
4649
|
+
assert_equal(np.median(d, axis=(3, 1, -4))[2],
|
|
4650
|
+
np.median(d[:, :, 2, :].flatten()))
|
|
4651
|
+
assert_equal(np.median(d, axis=(3, 1, 2))[2],
|
|
4652
|
+
np.median(d[2, :, :, :].flatten()))
|
|
4653
|
+
assert_equal(np.median(d, axis=(3, 2))[2, 1],
|
|
4654
|
+
np.median(d[2, 1, :, :].flatten()))
|
|
4655
|
+
assert_equal(np.median(d, axis=(1, -2))[2, 1],
|
|
4656
|
+
np.median(d[2, :, :, 1].flatten()))
|
|
4657
|
+
assert_equal(np.median(d, axis=(1, 3))[2, 2],
|
|
4658
|
+
np.median(d[2, :, 2, :].flatten()))
|
|
4659
|
+
|
|
4660
|
+
def test_extended_axis_invalid(self):
|
|
4661
|
+
d = np.ones((3, 5, 7, 11))
|
|
4662
|
+
assert_raises(AxisError, np.median, d, axis=-5)
|
|
4663
|
+
assert_raises(AxisError, np.median, d, axis=(0, -5))
|
|
4664
|
+
assert_raises(AxisError, np.median, d, axis=4)
|
|
4665
|
+
assert_raises(AxisError, np.median, d, axis=(0, 4))
|
|
4666
|
+
assert_raises(ValueError, np.median, d, axis=(1, 1))
|
|
4667
|
+
|
|
4668
|
+
def test_keepdims(self):
|
|
4669
|
+
d = np.ones((3, 5, 7, 11))
|
|
4670
|
+
assert_equal(np.median(d, axis=None, keepdims=True).shape,
|
|
4671
|
+
(1, 1, 1, 1))
|
|
4672
|
+
assert_equal(np.median(d, axis=(0, 1), keepdims=True).shape,
|
|
4673
|
+
(1, 1, 7, 11))
|
|
4674
|
+
assert_equal(np.median(d, axis=(0, 3), keepdims=True).shape,
|
|
4675
|
+
(1, 5, 7, 1))
|
|
4676
|
+
assert_equal(np.median(d, axis=(1,), keepdims=True).shape,
|
|
4677
|
+
(3, 1, 7, 11))
|
|
4678
|
+
assert_equal(np.median(d, axis=(0, 1, 2, 3), keepdims=True).shape,
|
|
4679
|
+
(1, 1, 1, 1))
|
|
4680
|
+
assert_equal(np.median(d, axis=(0, 1, 3), keepdims=True).shape,
|
|
4681
|
+
(1, 1, 7, 1))
|
|
4682
|
+
|
|
4683
|
+
@pytest.mark.parametrize(
|
|
4684
|
+
argnames='axis',
|
|
4685
|
+
argvalues=[
|
|
4686
|
+
None,
|
|
4687
|
+
1,
|
|
4688
|
+
(1, ),
|
|
4689
|
+
(0, 1),
|
|
4690
|
+
(-3, -1),
|
|
4691
|
+
]
|
|
4692
|
+
)
|
|
4693
|
+
def test_keepdims_out(self, axis):
|
|
4694
|
+
d = np.ones((3, 5, 7, 11))
|
|
4695
|
+
if axis is None:
|
|
4696
|
+
shape_out = (1,) * d.ndim
|
|
4697
|
+
else:
|
|
4698
|
+
axis_norm = normalize_axis_tuple(axis, d.ndim)
|
|
4699
|
+
shape_out = tuple(
|
|
4700
|
+
1 if i in axis_norm else d.shape[i] for i in range(d.ndim))
|
|
4701
|
+
out = np.empty(shape_out)
|
|
4702
|
+
result = np.median(d, axis=axis, keepdims=True, out=out)
|
|
4703
|
+
assert result is out
|
|
4704
|
+
assert_equal(result.shape, shape_out)
|
|
4705
|
+
|
|
4706
|
+
@pytest.mark.parametrize("dtype", ["m8[s]"])
|
|
4707
|
+
@pytest.mark.parametrize("pos", [0, 23, 10])
|
|
4708
|
+
def test_nat_behavior(self, dtype, pos):
|
|
4709
|
+
# TODO: Median does not support Datetime, due to `mean`.
|
|
4710
|
+
# NaT and NaN should behave the same, do basic tests for NaT.
|
|
4711
|
+
a = np.arange(0, 24, dtype=dtype)
|
|
4712
|
+
a[pos] = "NaT"
|
|
4713
|
+
res = np.median(a)
|
|
4714
|
+
assert res.dtype == dtype
|
|
4715
|
+
assert np.isnat(res)
|
|
4716
|
+
res = np.percentile(a, [30, 60])
|
|
4717
|
+
assert res.dtype == dtype
|
|
4718
|
+
assert np.isnat(res).all()
|
|
4719
|
+
|
|
4720
|
+
a = np.arange(0, 24 * 3, dtype=dtype).reshape(-1, 3)
|
|
4721
|
+
a[pos, 1] = "NaT"
|
|
4722
|
+
res = np.median(a, axis=0)
|
|
4723
|
+
assert_array_equal(np.isnat(res), [False, True, False])
|
|
4724
|
+
|
|
4725
|
+
|
|
4726
|
+
class TestSortComplex:
|
|
4727
|
+
|
|
4728
|
+
@pytest.mark.parametrize("type_in, type_out", [
|
|
4729
|
+
('l', 'D'),
|
|
4730
|
+
('h', 'F'),
|
|
4731
|
+
('H', 'F'),
|
|
4732
|
+
('b', 'F'),
|
|
4733
|
+
('B', 'F'),
|
|
4734
|
+
('g', 'G'),
|
|
4735
|
+
])
|
|
4736
|
+
def test_sort_real(self, type_in, type_out):
|
|
4737
|
+
# sort_complex() type casting for real input types
|
|
4738
|
+
a = np.array([5, 3, 6, 2, 1], dtype=type_in)
|
|
4739
|
+
actual = np.sort_complex(a)
|
|
4740
|
+
expected = np.sort(a).astype(type_out)
|
|
4741
|
+
assert_equal(actual, expected)
|
|
4742
|
+
assert_equal(actual.dtype, expected.dtype)
|
|
4743
|
+
|
|
4744
|
+
def test_sort_complex(self):
|
|
4745
|
+
# sort_complex() handling of complex input
|
|
4746
|
+
a = np.array([2 + 3j, 1 - 2j, 1 - 3j, 2 + 1j], dtype='D')
|
|
4747
|
+
expected = np.array([1 - 3j, 1 - 2j, 2 + 1j, 2 + 3j], dtype='D')
|
|
4748
|
+
actual = np.sort_complex(a)
|
|
4749
|
+
assert_equal(actual, expected)
|
|
4750
|
+
assert_equal(actual.dtype, expected.dtype)
|