update
This commit is contained in:
0
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__init__.py
vendored
Normal file
0
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__init__.py
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/__init__.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/__init__.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test__datasource.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test__datasource.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test__iotools.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test__iotools.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test__version.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test__version.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_array_utils.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_array_utils.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_arraypad.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_arraypad.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_arraysetops.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_arraysetops.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_arrayterator.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_arrayterator.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_format.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_format.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_function_base.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_function_base.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_histograms.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_histograms.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_index_tricks.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_index_tricks.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_io.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_io.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_loadtxt.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_loadtxt.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_mixins.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_mixins.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_nanfunctions.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_nanfunctions.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_packbits.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_packbits.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_polynomial.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_polynomial.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_recfunctions.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_recfunctions.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_regression.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_regression.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_shape_base.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_shape_base.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_stride_tricks.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_stride_tricks.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_twodim_base.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_twodim_base.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_type_check.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_type_check.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_ufunclike.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_ufunclike.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_utils.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/__pycache__/test_utils.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/py2-np0-objarr.npy
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/py2-np0-objarr.npy
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/py2-objarr.npy
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/py2-objarr.npy
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/py2-objarr.npz
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/py2-objarr.npz
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/py3-objarr.npy
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/py3-objarr.npy
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/py3-objarr.npz
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/py3-objarr.npz
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/python3.npy
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/python3.npy
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/win64python2.npy
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/data/win64python2.npy
vendored
Normal file
Binary file not shown.
350
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test__datasource.py
vendored
Normal file
350
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test__datasource.py
vendored
Normal file
@@ -0,0 +1,350 @@
|
||||
import os
|
||||
import pytest
|
||||
from tempfile import mkdtemp, mkstemp, NamedTemporaryFile
|
||||
from shutil import rmtree
|
||||
|
||||
import numpy.lib._datasource as datasource
|
||||
from numpy.testing import assert_, assert_equal, assert_raises
|
||||
|
||||
import urllib.request as urllib_request
|
||||
from urllib.parse import urlparse
|
||||
from urllib.error import URLError
|
||||
|
||||
|
||||
def urlopen_stub(url, data=None):
|
||||
'''Stub to replace urlopen for testing.'''
|
||||
if url == valid_httpurl():
|
||||
tmpfile = NamedTemporaryFile(prefix='urltmp_')
|
||||
return tmpfile
|
||||
else:
|
||||
raise URLError('Name or service not known')
|
||||
|
||||
# setup and teardown
|
||||
old_urlopen = None
|
||||
|
||||
|
||||
def setup_module():
|
||||
global old_urlopen
|
||||
|
||||
old_urlopen = urllib_request.urlopen
|
||||
urllib_request.urlopen = urlopen_stub
|
||||
|
||||
|
||||
def teardown_module():
|
||||
urllib_request.urlopen = old_urlopen
|
||||
|
||||
# A valid website for more robust testing
|
||||
http_path = 'http://www.google.com/'
|
||||
http_file = 'index.html'
|
||||
|
||||
http_fakepath = 'http://fake.abc.web/site/'
|
||||
http_fakefile = 'fake.txt'
|
||||
|
||||
malicious_files = ['/etc/shadow', '../../shadow',
|
||||
'..\\system.dat', 'c:\\windows\\system.dat']
|
||||
|
||||
magic_line = b'three is the magic number'
|
||||
|
||||
|
||||
# Utility functions used by many tests
|
||||
def valid_textfile(filedir):
|
||||
# Generate and return a valid temporary file.
|
||||
fd, path = mkstemp(suffix='.txt', prefix='dstmp_', dir=filedir, text=True)
|
||||
os.close(fd)
|
||||
return path
|
||||
|
||||
|
||||
def invalid_textfile(filedir):
|
||||
# Generate and return an invalid filename.
|
||||
fd, path = mkstemp(suffix='.txt', prefix='dstmp_', dir=filedir)
|
||||
os.close(fd)
|
||||
os.remove(path)
|
||||
return path
|
||||
|
||||
|
||||
def valid_httpurl():
|
||||
return http_path+http_file
|
||||
|
||||
|
||||
def invalid_httpurl():
|
||||
return http_fakepath+http_fakefile
|
||||
|
||||
|
||||
def valid_baseurl():
|
||||
return http_path
|
||||
|
||||
|
||||
def invalid_baseurl():
|
||||
return http_fakepath
|
||||
|
||||
|
||||
def valid_httpfile():
|
||||
return http_file
|
||||
|
||||
|
||||
def invalid_httpfile():
|
||||
return http_fakefile
|
||||
|
||||
|
||||
class TestDataSourceOpen:
|
||||
def setup_method(self):
|
||||
self.tmpdir = mkdtemp()
|
||||
self.ds = datasource.DataSource(self.tmpdir)
|
||||
|
||||
def teardown_method(self):
|
||||
rmtree(self.tmpdir)
|
||||
del self.ds
|
||||
|
||||
def test_ValidHTTP(self):
|
||||
fh = self.ds.open(valid_httpurl())
|
||||
assert_(fh)
|
||||
fh.close()
|
||||
|
||||
def test_InvalidHTTP(self):
|
||||
url = invalid_httpurl()
|
||||
assert_raises(OSError, self.ds.open, url)
|
||||
try:
|
||||
self.ds.open(url)
|
||||
except OSError as e:
|
||||
# Regression test for bug fixed in r4342.
|
||||
assert_(e.errno is None)
|
||||
|
||||
def test_InvalidHTTPCacheURLError(self):
|
||||
assert_raises(URLError, self.ds._cache, invalid_httpurl())
|
||||
|
||||
def test_ValidFile(self):
|
||||
local_file = valid_textfile(self.tmpdir)
|
||||
fh = self.ds.open(local_file)
|
||||
assert_(fh)
|
||||
fh.close()
|
||||
|
||||
def test_InvalidFile(self):
|
||||
invalid_file = invalid_textfile(self.tmpdir)
|
||||
assert_raises(OSError, self.ds.open, invalid_file)
|
||||
|
||||
def test_ValidGzipFile(self):
|
||||
try:
|
||||
import gzip
|
||||
except ImportError:
|
||||
# We don't have the gzip capabilities to test.
|
||||
pytest.skip()
|
||||
# Test datasource's internal file_opener for Gzip files.
|
||||
filepath = os.path.join(self.tmpdir, 'foobar.txt.gz')
|
||||
fp = gzip.open(filepath, 'w')
|
||||
fp.write(magic_line)
|
||||
fp.close()
|
||||
fp = self.ds.open(filepath)
|
||||
result = fp.readline()
|
||||
fp.close()
|
||||
assert_equal(magic_line, result)
|
||||
|
||||
def test_ValidBz2File(self):
|
||||
try:
|
||||
import bz2
|
||||
except ImportError:
|
||||
# We don't have the bz2 capabilities to test.
|
||||
pytest.skip()
|
||||
# Test datasource's internal file_opener for BZip2 files.
|
||||
filepath = os.path.join(self.tmpdir, 'foobar.txt.bz2')
|
||||
fp = bz2.BZ2File(filepath, 'w')
|
||||
fp.write(magic_line)
|
||||
fp.close()
|
||||
fp = self.ds.open(filepath)
|
||||
result = fp.readline()
|
||||
fp.close()
|
||||
assert_equal(magic_line, result)
|
||||
|
||||
|
||||
class TestDataSourceExists:
|
||||
def setup_method(self):
|
||||
self.tmpdir = mkdtemp()
|
||||
self.ds = datasource.DataSource(self.tmpdir)
|
||||
|
||||
def teardown_method(self):
|
||||
rmtree(self.tmpdir)
|
||||
del self.ds
|
||||
|
||||
def test_ValidHTTP(self):
|
||||
assert_(self.ds.exists(valid_httpurl()))
|
||||
|
||||
def test_InvalidHTTP(self):
|
||||
assert_equal(self.ds.exists(invalid_httpurl()), False)
|
||||
|
||||
def test_ValidFile(self):
|
||||
# Test valid file in destpath
|
||||
tmpfile = valid_textfile(self.tmpdir)
|
||||
assert_(self.ds.exists(tmpfile))
|
||||
# Test valid local file not in destpath
|
||||
localdir = mkdtemp()
|
||||
tmpfile = valid_textfile(localdir)
|
||||
assert_(self.ds.exists(tmpfile))
|
||||
rmtree(localdir)
|
||||
|
||||
def test_InvalidFile(self):
|
||||
tmpfile = invalid_textfile(self.tmpdir)
|
||||
assert_equal(self.ds.exists(tmpfile), False)
|
||||
|
||||
|
||||
class TestDataSourceAbspath:
|
||||
def setup_method(self):
|
||||
self.tmpdir = os.path.abspath(mkdtemp())
|
||||
self.ds = datasource.DataSource(self.tmpdir)
|
||||
|
||||
def teardown_method(self):
|
||||
rmtree(self.tmpdir)
|
||||
del self.ds
|
||||
|
||||
def test_ValidHTTP(self):
|
||||
scheme, netloc, upath, pms, qry, frg = urlparse(valid_httpurl())
|
||||
local_path = os.path.join(self.tmpdir, netloc,
|
||||
upath.strip(os.sep).strip('/'))
|
||||
assert_equal(local_path, self.ds.abspath(valid_httpurl()))
|
||||
|
||||
def test_ValidFile(self):
|
||||
tmpfile = valid_textfile(self.tmpdir)
|
||||
tmpfilename = os.path.split(tmpfile)[-1]
|
||||
# Test with filename only
|
||||
assert_equal(tmpfile, self.ds.abspath(tmpfilename))
|
||||
# Test filename with complete path
|
||||
assert_equal(tmpfile, self.ds.abspath(tmpfile))
|
||||
|
||||
def test_InvalidHTTP(self):
|
||||
scheme, netloc, upath, pms, qry, frg = urlparse(invalid_httpurl())
|
||||
invalidhttp = os.path.join(self.tmpdir, netloc,
|
||||
upath.strip(os.sep).strip('/'))
|
||||
assert_(invalidhttp != self.ds.abspath(valid_httpurl()))
|
||||
|
||||
def test_InvalidFile(self):
|
||||
invalidfile = valid_textfile(self.tmpdir)
|
||||
tmpfile = valid_textfile(self.tmpdir)
|
||||
tmpfilename = os.path.split(tmpfile)[-1]
|
||||
# Test with filename only
|
||||
assert_(invalidfile != self.ds.abspath(tmpfilename))
|
||||
# Test filename with complete path
|
||||
assert_(invalidfile != self.ds.abspath(tmpfile))
|
||||
|
||||
def test_sandboxing(self):
|
||||
tmpfile = valid_textfile(self.tmpdir)
|
||||
tmpfilename = os.path.split(tmpfile)[-1]
|
||||
|
||||
tmp_path = lambda x: os.path.abspath(self.ds.abspath(x))
|
||||
|
||||
assert_(tmp_path(valid_httpurl()).startswith(self.tmpdir))
|
||||
assert_(tmp_path(invalid_httpurl()).startswith(self.tmpdir))
|
||||
assert_(tmp_path(tmpfile).startswith(self.tmpdir))
|
||||
assert_(tmp_path(tmpfilename).startswith(self.tmpdir))
|
||||
for fn in malicious_files:
|
||||
assert_(tmp_path(http_path+fn).startswith(self.tmpdir))
|
||||
assert_(tmp_path(fn).startswith(self.tmpdir))
|
||||
|
||||
def test_windows_os_sep(self):
|
||||
orig_os_sep = os.sep
|
||||
try:
|
||||
os.sep = '\\'
|
||||
self.test_ValidHTTP()
|
||||
self.test_ValidFile()
|
||||
self.test_InvalidHTTP()
|
||||
self.test_InvalidFile()
|
||||
self.test_sandboxing()
|
||||
finally:
|
||||
os.sep = orig_os_sep
|
||||
|
||||
|
||||
class TestRepositoryAbspath:
|
||||
def setup_method(self):
|
||||
self.tmpdir = os.path.abspath(mkdtemp())
|
||||
self.repos = datasource.Repository(valid_baseurl(), self.tmpdir)
|
||||
|
||||
def teardown_method(self):
|
||||
rmtree(self.tmpdir)
|
||||
del self.repos
|
||||
|
||||
def test_ValidHTTP(self):
|
||||
scheme, netloc, upath, pms, qry, frg = urlparse(valid_httpurl())
|
||||
local_path = os.path.join(self.repos._destpath, netloc,
|
||||
upath.strip(os.sep).strip('/'))
|
||||
filepath = self.repos.abspath(valid_httpfile())
|
||||
assert_equal(local_path, filepath)
|
||||
|
||||
def test_sandboxing(self):
|
||||
tmp_path = lambda x: os.path.abspath(self.repos.abspath(x))
|
||||
assert_(tmp_path(valid_httpfile()).startswith(self.tmpdir))
|
||||
for fn in malicious_files:
|
||||
assert_(tmp_path(http_path+fn).startswith(self.tmpdir))
|
||||
assert_(tmp_path(fn).startswith(self.tmpdir))
|
||||
|
||||
def test_windows_os_sep(self):
|
||||
orig_os_sep = os.sep
|
||||
try:
|
||||
os.sep = '\\'
|
||||
self.test_ValidHTTP()
|
||||
self.test_sandboxing()
|
||||
finally:
|
||||
os.sep = orig_os_sep
|
||||
|
||||
|
||||
class TestRepositoryExists:
|
||||
def setup_method(self):
|
||||
self.tmpdir = mkdtemp()
|
||||
self.repos = datasource.Repository(valid_baseurl(), self.tmpdir)
|
||||
|
||||
def teardown_method(self):
|
||||
rmtree(self.tmpdir)
|
||||
del self.repos
|
||||
|
||||
def test_ValidFile(self):
|
||||
# Create local temp file
|
||||
tmpfile = valid_textfile(self.tmpdir)
|
||||
assert_(self.repos.exists(tmpfile))
|
||||
|
||||
def test_InvalidFile(self):
|
||||
tmpfile = invalid_textfile(self.tmpdir)
|
||||
assert_equal(self.repos.exists(tmpfile), False)
|
||||
|
||||
def test_RemoveHTTPFile(self):
|
||||
assert_(self.repos.exists(valid_httpurl()))
|
||||
|
||||
def test_CachedHTTPFile(self):
|
||||
localfile = valid_httpurl()
|
||||
# Create a locally cached temp file with an URL based
|
||||
# directory structure. This is similar to what Repository.open
|
||||
# would do.
|
||||
scheme, netloc, upath, pms, qry, frg = urlparse(localfile)
|
||||
local_path = os.path.join(self.repos._destpath, netloc)
|
||||
os.mkdir(local_path, 0o0700)
|
||||
tmpfile = valid_textfile(local_path)
|
||||
assert_(self.repos.exists(tmpfile))
|
||||
|
||||
|
||||
class TestOpenFunc:
|
||||
def setup_method(self):
|
||||
self.tmpdir = mkdtemp()
|
||||
|
||||
def teardown_method(self):
|
||||
rmtree(self.tmpdir)
|
||||
|
||||
def test_DataSourceOpen(self):
|
||||
local_file = valid_textfile(self.tmpdir)
|
||||
# Test case where destpath is passed in
|
||||
fp = datasource.open(local_file, destpath=self.tmpdir)
|
||||
assert_(fp)
|
||||
fp.close()
|
||||
# Test case where default destpath is used
|
||||
fp = datasource.open(local_file)
|
||||
assert_(fp)
|
||||
fp.close()
|
||||
|
||||
def test_del_attr_handling():
|
||||
# DataSource __del__ can be called
|
||||
# even if __init__ fails when the
|
||||
# Exception object is caught by the
|
||||
# caller as happens in refguide_check
|
||||
# is_deprecated() function
|
||||
|
||||
ds = datasource.DataSource()
|
||||
# simulate failed __init__ by removing key attribute
|
||||
# produced within __init__ and expected by __del__
|
||||
del ds._istmpdest
|
||||
# should not raise an AttributeError if __del__
|
||||
# gracefully handles failed __init__:
|
||||
ds.__del__()
|
||||
353
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test__iotools.py
vendored
Normal file
353
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test__iotools.py
vendored
Normal file
@@ -0,0 +1,353 @@
|
||||
import time
|
||||
from datetime import date
|
||||
|
||||
import numpy as np
|
||||
from numpy.testing import (
|
||||
assert_, assert_equal, assert_allclose, assert_raises,
|
||||
)
|
||||
from numpy.lib._iotools import (
|
||||
LineSplitter, NameValidator, StringConverter,
|
||||
has_nested_fields, easy_dtype, flatten_dtype
|
||||
)
|
||||
|
||||
|
||||
class TestLineSplitter:
|
||||
"Tests the LineSplitter class."
|
||||
|
||||
def test_no_delimiter(self):
|
||||
"Test LineSplitter w/o delimiter"
|
||||
strg = " 1 2 3 4 5 # test"
|
||||
test = LineSplitter()(strg)
|
||||
assert_equal(test, ['1', '2', '3', '4', '5'])
|
||||
test = LineSplitter('')(strg)
|
||||
assert_equal(test, ['1', '2', '3', '4', '5'])
|
||||
|
||||
def test_space_delimiter(self):
|
||||
"Test space delimiter"
|
||||
strg = " 1 2 3 4 5 # test"
|
||||
test = LineSplitter(' ')(strg)
|
||||
assert_equal(test, ['1', '2', '3', '4', '', '5'])
|
||||
test = LineSplitter(' ')(strg)
|
||||
assert_equal(test, ['1 2 3 4', '5'])
|
||||
|
||||
def test_tab_delimiter(self):
|
||||
"Test tab delimiter"
|
||||
strg = " 1\t 2\t 3\t 4\t 5 6"
|
||||
test = LineSplitter('\t')(strg)
|
||||
assert_equal(test, ['1', '2', '3', '4', '5 6'])
|
||||
strg = " 1 2\t 3 4\t 5 6"
|
||||
test = LineSplitter('\t')(strg)
|
||||
assert_equal(test, ['1 2', '3 4', '5 6'])
|
||||
|
||||
def test_other_delimiter(self):
|
||||
"Test LineSplitter on delimiter"
|
||||
strg = "1,2,3,4,,5"
|
||||
test = LineSplitter(',')(strg)
|
||||
assert_equal(test, ['1', '2', '3', '4', '', '5'])
|
||||
#
|
||||
strg = " 1,2,3,4,,5 # test"
|
||||
test = LineSplitter(',')(strg)
|
||||
assert_equal(test, ['1', '2', '3', '4', '', '5'])
|
||||
|
||||
# gh-11028 bytes comment/delimiters should get encoded
|
||||
strg = b" 1,2,3,4,,5 % test"
|
||||
test = LineSplitter(delimiter=b',', comments=b'%')(strg)
|
||||
assert_equal(test, ['1', '2', '3', '4', '', '5'])
|
||||
|
||||
def test_constant_fixed_width(self):
|
||||
"Test LineSplitter w/ fixed-width fields"
|
||||
strg = " 1 2 3 4 5 # test"
|
||||
test = LineSplitter(3)(strg)
|
||||
assert_equal(test, ['1', '2', '3', '4', '', '5', ''])
|
||||
#
|
||||
strg = " 1 3 4 5 6# test"
|
||||
test = LineSplitter(20)(strg)
|
||||
assert_equal(test, ['1 3 4 5 6'])
|
||||
#
|
||||
strg = " 1 3 4 5 6# test"
|
||||
test = LineSplitter(30)(strg)
|
||||
assert_equal(test, ['1 3 4 5 6'])
|
||||
|
||||
def test_variable_fixed_width(self):
|
||||
strg = " 1 3 4 5 6# test"
|
||||
test = LineSplitter((3, 6, 6, 3))(strg)
|
||||
assert_equal(test, ['1', '3', '4 5', '6'])
|
||||
#
|
||||
strg = " 1 3 4 5 6# test"
|
||||
test = LineSplitter((6, 6, 9))(strg)
|
||||
assert_equal(test, ['1', '3 4', '5 6'])
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestNameValidator:
|
||||
|
||||
def test_case_sensitivity(self):
|
||||
"Test case sensitivity"
|
||||
names = ['A', 'a', 'b', 'c']
|
||||
test = NameValidator().validate(names)
|
||||
assert_equal(test, ['A', 'a', 'b', 'c'])
|
||||
test = NameValidator(case_sensitive=False).validate(names)
|
||||
assert_equal(test, ['A', 'A_1', 'B', 'C'])
|
||||
test = NameValidator(case_sensitive='upper').validate(names)
|
||||
assert_equal(test, ['A', 'A_1', 'B', 'C'])
|
||||
test = NameValidator(case_sensitive='lower').validate(names)
|
||||
assert_equal(test, ['a', 'a_1', 'b', 'c'])
|
||||
|
||||
# check exceptions
|
||||
assert_raises(ValueError, NameValidator, case_sensitive='foobar')
|
||||
|
||||
def test_excludelist(self):
|
||||
"Test excludelist"
|
||||
names = ['dates', 'data', 'Other Data', 'mask']
|
||||
validator = NameValidator(excludelist=['dates', 'data', 'mask'])
|
||||
test = validator.validate(names)
|
||||
assert_equal(test, ['dates_', 'data_', 'Other_Data', 'mask_'])
|
||||
|
||||
def test_missing_names(self):
|
||||
"Test validate missing names"
|
||||
namelist = ('a', 'b', 'c')
|
||||
validator = NameValidator()
|
||||
assert_equal(validator(namelist), ['a', 'b', 'c'])
|
||||
namelist = ('', 'b', 'c')
|
||||
assert_equal(validator(namelist), ['f0', 'b', 'c'])
|
||||
namelist = ('a', 'b', '')
|
||||
assert_equal(validator(namelist), ['a', 'b', 'f0'])
|
||||
namelist = ('', 'f0', '')
|
||||
assert_equal(validator(namelist), ['f1', 'f0', 'f2'])
|
||||
|
||||
def test_validate_nb_names(self):
|
||||
"Test validate nb names"
|
||||
namelist = ('a', 'b', 'c')
|
||||
validator = NameValidator()
|
||||
assert_equal(validator(namelist, nbfields=1), ('a',))
|
||||
assert_equal(validator(namelist, nbfields=5, defaultfmt="g%i"),
|
||||
['a', 'b', 'c', 'g0', 'g1'])
|
||||
|
||||
def test_validate_wo_names(self):
|
||||
"Test validate no names"
|
||||
namelist = None
|
||||
validator = NameValidator()
|
||||
assert_(validator(namelist) is None)
|
||||
assert_equal(validator(namelist, nbfields=3), ['f0', 'f1', 'f2'])
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _bytes_to_date(s):
|
||||
return date(*time.strptime(s, "%Y-%m-%d")[:3])
|
||||
|
||||
|
||||
class TestStringConverter:
|
||||
"Test StringConverter"
|
||||
|
||||
def test_creation(self):
|
||||
"Test creation of a StringConverter"
|
||||
converter = StringConverter(int, -99999)
|
||||
assert_equal(converter._status, 1)
|
||||
assert_equal(converter.default, -99999)
|
||||
|
||||
def test_upgrade(self):
|
||||
"Tests the upgrade method."
|
||||
|
||||
converter = StringConverter()
|
||||
assert_equal(converter._status, 0)
|
||||
|
||||
# test int
|
||||
assert_equal(converter.upgrade('0'), 0)
|
||||
assert_equal(converter._status, 1)
|
||||
|
||||
# On systems where long defaults to 32-bit, the statuses will be
|
||||
# offset by one, so we check for this here.
|
||||
import numpy._core.numeric as nx
|
||||
status_offset = int(nx.dtype(nx.int_).itemsize < nx.dtype(nx.int64).itemsize)
|
||||
|
||||
# test int > 2**32
|
||||
assert_equal(converter.upgrade('17179869184'), 17179869184)
|
||||
assert_equal(converter._status, 1 + status_offset)
|
||||
|
||||
# test float
|
||||
assert_allclose(converter.upgrade('0.'), 0.0)
|
||||
assert_equal(converter._status, 2 + status_offset)
|
||||
|
||||
# test complex
|
||||
assert_equal(converter.upgrade('0j'), complex('0j'))
|
||||
assert_equal(converter._status, 3 + status_offset)
|
||||
|
||||
# test str
|
||||
# note that the longdouble type has been skipped, so the
|
||||
# _status increases by 2. Everything should succeed with
|
||||
# unicode conversion (8).
|
||||
for s in ['a', b'a']:
|
||||
res = converter.upgrade(s)
|
||||
assert_(type(res) is str)
|
||||
assert_equal(res, 'a')
|
||||
assert_equal(converter._status, 8 + status_offset)
|
||||
|
||||
def test_missing(self):
|
||||
"Tests the use of missing values."
|
||||
converter = StringConverter(missing_values=('missing',
|
||||
'missed'))
|
||||
converter.upgrade('0')
|
||||
assert_equal(converter('0'), 0)
|
||||
assert_equal(converter(''), converter.default)
|
||||
assert_equal(converter('missing'), converter.default)
|
||||
assert_equal(converter('missed'), converter.default)
|
||||
try:
|
||||
converter('miss')
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def test_upgrademapper(self):
|
||||
"Tests updatemapper"
|
||||
dateparser = _bytes_to_date
|
||||
_original_mapper = StringConverter._mapper[:]
|
||||
try:
|
||||
StringConverter.upgrade_mapper(dateparser, date(2000, 1, 1))
|
||||
convert = StringConverter(dateparser, date(2000, 1, 1))
|
||||
test = convert('2001-01-01')
|
||||
assert_equal(test, date(2001, 1, 1))
|
||||
test = convert('2009-01-01')
|
||||
assert_equal(test, date(2009, 1, 1))
|
||||
test = convert('')
|
||||
assert_equal(test, date(2000, 1, 1))
|
||||
finally:
|
||||
StringConverter._mapper = _original_mapper
|
||||
|
||||
def test_string_to_object(self):
|
||||
"Make sure that string-to-object functions are properly recognized"
|
||||
old_mapper = StringConverter._mapper[:] # copy of list
|
||||
conv = StringConverter(_bytes_to_date)
|
||||
assert_equal(conv._mapper, old_mapper)
|
||||
assert_(hasattr(conv, 'default'))
|
||||
|
||||
def test_keep_default(self):
|
||||
"Make sure we don't lose an explicit default"
|
||||
converter = StringConverter(None, missing_values='',
|
||||
default=-999)
|
||||
converter.upgrade('3.14159265')
|
||||
assert_equal(converter.default, -999)
|
||||
assert_equal(converter.type, np.dtype(float))
|
||||
#
|
||||
converter = StringConverter(
|
||||
None, missing_values='', default=0)
|
||||
converter.upgrade('3.14159265')
|
||||
assert_equal(converter.default, 0)
|
||||
assert_equal(converter.type, np.dtype(float))
|
||||
|
||||
def test_keep_default_zero(self):
|
||||
"Check that we don't lose a default of 0"
|
||||
converter = StringConverter(int, default=0,
|
||||
missing_values="N/A")
|
||||
assert_equal(converter.default, 0)
|
||||
|
||||
def test_keep_missing_values(self):
|
||||
"Check that we're not losing missing values"
|
||||
converter = StringConverter(int, default=0,
|
||||
missing_values="N/A")
|
||||
assert_equal(
|
||||
converter.missing_values, {'', 'N/A'})
|
||||
|
||||
def test_int64_dtype(self):
|
||||
"Check that int64 integer types can be specified"
|
||||
converter = StringConverter(np.int64, default=0)
|
||||
val = "-9223372036854775807"
|
||||
assert_(converter(val) == -9223372036854775807)
|
||||
val = "9223372036854775807"
|
||||
assert_(converter(val) == 9223372036854775807)
|
||||
|
||||
def test_uint64_dtype(self):
|
||||
"Check that uint64 integer types can be specified"
|
||||
converter = StringConverter(np.uint64, default=0)
|
||||
val = "9223372043271415339"
|
||||
assert_(converter(val) == 9223372043271415339)
|
||||
|
||||
|
||||
class TestMiscFunctions:
|
||||
|
||||
def test_has_nested_dtype(self):
|
||||
"Test has_nested_dtype"
|
||||
ndtype = np.dtype(float)
|
||||
assert_equal(has_nested_fields(ndtype), False)
|
||||
ndtype = np.dtype([('A', '|S3'), ('B', float)])
|
||||
assert_equal(has_nested_fields(ndtype), False)
|
||||
ndtype = np.dtype([('A', int), ('B', [('BA', float), ('BB', '|S1')])])
|
||||
assert_equal(has_nested_fields(ndtype), True)
|
||||
|
||||
def test_easy_dtype(self):
|
||||
"Test ndtype on dtypes"
|
||||
# Simple case
|
||||
ndtype = float
|
||||
assert_equal(easy_dtype(ndtype), np.dtype(float))
|
||||
# As string w/o names
|
||||
ndtype = "i4, f8"
|
||||
assert_equal(easy_dtype(ndtype),
|
||||
np.dtype([('f0', "i4"), ('f1', "f8")]))
|
||||
# As string w/o names but different default format
|
||||
assert_equal(easy_dtype(ndtype, defaultfmt="field_%03i"),
|
||||
np.dtype([('field_000', "i4"), ('field_001', "f8")]))
|
||||
# As string w/ names
|
||||
ndtype = "i4, f8"
|
||||
assert_equal(easy_dtype(ndtype, names="a, b"),
|
||||
np.dtype([('a', "i4"), ('b', "f8")]))
|
||||
# As string w/ names (too many)
|
||||
ndtype = "i4, f8"
|
||||
assert_equal(easy_dtype(ndtype, names="a, b, c"),
|
||||
np.dtype([('a', "i4"), ('b', "f8")]))
|
||||
# As string w/ names (not enough)
|
||||
ndtype = "i4, f8"
|
||||
assert_equal(easy_dtype(ndtype, names=", b"),
|
||||
np.dtype([('f0', "i4"), ('b', "f8")]))
|
||||
# ... (with different default format)
|
||||
assert_equal(easy_dtype(ndtype, names="a", defaultfmt="f%02i"),
|
||||
np.dtype([('a', "i4"), ('f00', "f8")]))
|
||||
# As list of tuples w/o names
|
||||
ndtype = [('A', int), ('B', float)]
|
||||
assert_equal(easy_dtype(ndtype), np.dtype([('A', int), ('B', float)]))
|
||||
# As list of tuples w/ names
|
||||
assert_equal(easy_dtype(ndtype, names="a,b"),
|
||||
np.dtype([('a', int), ('b', float)]))
|
||||
# As list of tuples w/ not enough names
|
||||
assert_equal(easy_dtype(ndtype, names="a"),
|
||||
np.dtype([('a', int), ('f0', float)]))
|
||||
# As list of tuples w/ too many names
|
||||
assert_equal(easy_dtype(ndtype, names="a,b,c"),
|
||||
np.dtype([('a', int), ('b', float)]))
|
||||
# As list of types w/o names
|
||||
ndtype = (int, float, float)
|
||||
assert_equal(easy_dtype(ndtype),
|
||||
np.dtype([('f0', int), ('f1', float), ('f2', float)]))
|
||||
# As list of types w names
|
||||
ndtype = (int, float, float)
|
||||
assert_equal(easy_dtype(ndtype, names="a, b, c"),
|
||||
np.dtype([('a', int), ('b', float), ('c', float)]))
|
||||
# As simple dtype w/ names
|
||||
ndtype = np.dtype(float)
|
||||
assert_equal(easy_dtype(ndtype, names="a, b, c"),
|
||||
np.dtype([(_, float) for _ in ('a', 'b', 'c')]))
|
||||
# As simple dtype w/o names (but multiple fields)
|
||||
ndtype = np.dtype(float)
|
||||
assert_equal(
|
||||
easy_dtype(ndtype, names=['', '', ''], defaultfmt="f%02i"),
|
||||
np.dtype([(_, float) for _ in ('f00', 'f01', 'f02')]))
|
||||
|
||||
def test_flatten_dtype(self):
|
||||
"Testing flatten_dtype"
|
||||
# Standard dtype
|
||||
dt = np.dtype([("a", "f8"), ("b", "f8")])
|
||||
dt_flat = flatten_dtype(dt)
|
||||
assert_equal(dt_flat, [float, float])
|
||||
# Recursive dtype
|
||||
dt = np.dtype([("a", [("aa", '|S1'), ("ab", '|S2')]), ("b", int)])
|
||||
dt_flat = flatten_dtype(dt)
|
||||
assert_equal(dt_flat, [np.dtype('|S1'), np.dtype('|S2'), int])
|
||||
# dtype with shaped fields
|
||||
dt = np.dtype([("a", (float, 2)), ("b", (int, 3))])
|
||||
dt_flat = flatten_dtype(dt)
|
||||
assert_equal(dt_flat, [float, int])
|
||||
dt_flat = flatten_dtype(dt, True)
|
||||
assert_equal(dt_flat, [float] * 2 + [int] * 3)
|
||||
# dtype w/ titles
|
||||
dt = np.dtype([(("a", "A"), "f8"), (("b", "B"), "f8")])
|
||||
dt_flat = flatten_dtype(dt)
|
||||
assert_equal(dt_flat, [float, float])
|
||||
64
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test__version.py
vendored
Normal file
64
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test__version.py
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
"""Tests for the NumpyVersion class.
|
||||
|
||||
"""
|
||||
from numpy.testing import assert_, assert_raises
|
||||
from numpy.lib import NumpyVersion
|
||||
|
||||
|
||||
def test_main_versions():
|
||||
assert_(NumpyVersion('1.8.0') == '1.8.0')
|
||||
for ver in ['1.9.0', '2.0.0', '1.8.1', '10.0.1']:
|
||||
assert_(NumpyVersion('1.8.0') < ver)
|
||||
|
||||
for ver in ['1.7.0', '1.7.1', '0.9.9']:
|
||||
assert_(NumpyVersion('1.8.0') > ver)
|
||||
|
||||
|
||||
def test_version_1_point_10():
|
||||
# regression test for gh-2998.
|
||||
assert_(NumpyVersion('1.9.0') < '1.10.0')
|
||||
assert_(NumpyVersion('1.11.0') < '1.11.1')
|
||||
assert_(NumpyVersion('1.11.0') == '1.11.0')
|
||||
assert_(NumpyVersion('1.99.11') < '1.99.12')
|
||||
|
||||
|
||||
def test_alpha_beta_rc():
|
||||
assert_(NumpyVersion('1.8.0rc1') == '1.8.0rc1')
|
||||
for ver in ['1.8.0', '1.8.0rc2']:
|
||||
assert_(NumpyVersion('1.8.0rc1') < ver)
|
||||
|
||||
for ver in ['1.8.0a2', '1.8.0b3', '1.7.2rc4']:
|
||||
assert_(NumpyVersion('1.8.0rc1') > ver)
|
||||
|
||||
assert_(NumpyVersion('1.8.0b1') > '1.8.0a2')
|
||||
|
||||
|
||||
def test_dev_version():
|
||||
assert_(NumpyVersion('1.9.0.dev-Unknown') < '1.9.0')
|
||||
for ver in ['1.9.0', '1.9.0a1', '1.9.0b2', '1.9.0b2.dev-ffffffff']:
|
||||
assert_(NumpyVersion('1.9.0.dev-f16acvda') < ver)
|
||||
|
||||
assert_(NumpyVersion('1.9.0.dev-f16acvda') == '1.9.0.dev-11111111')
|
||||
|
||||
|
||||
def test_dev_a_b_rc_mixed():
|
||||
assert_(NumpyVersion('1.9.0a2.dev-f16acvda') == '1.9.0a2.dev-11111111')
|
||||
assert_(NumpyVersion('1.9.0a2.dev-6acvda54') < '1.9.0a2')
|
||||
|
||||
|
||||
def test_dev0_version():
|
||||
assert_(NumpyVersion('1.9.0.dev0+Unknown') < '1.9.0')
|
||||
for ver in ['1.9.0', '1.9.0a1', '1.9.0b2', '1.9.0b2.dev0+ffffffff']:
|
||||
assert_(NumpyVersion('1.9.0.dev0+f16acvda') < ver)
|
||||
|
||||
assert_(NumpyVersion('1.9.0.dev0+f16acvda') == '1.9.0.dev0+11111111')
|
||||
|
||||
|
||||
def test_dev0_a_b_rc_mixed():
|
||||
assert_(NumpyVersion('1.9.0a2.dev0+f16acvda') == '1.9.0a2.dev0+11111111')
|
||||
assert_(NumpyVersion('1.9.0a2.dev0+6acvda54') < '1.9.0a2')
|
||||
|
||||
|
||||
def test_raises():
|
||||
for ver in ['1.9', '1,9.0', '1.7.x']:
|
||||
assert_raises(ValueError, NumpyVersion, ver)
|
||||
33
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_array_utils.py
vendored
Normal file
33
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_array_utils.py
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
import numpy as np
|
||||
|
||||
from numpy.lib import array_utils
|
||||
from numpy.testing import assert_equal
|
||||
|
||||
|
||||
class TestByteBounds:
|
||||
def test_byte_bounds(self):
|
||||
# pointer difference matches size * itemsize
|
||||
# due to contiguity
|
||||
a = np.arange(12).reshape(3, 4)
|
||||
low, high = array_utils.byte_bounds(a)
|
||||
assert_equal(high - low, a.size * a.itemsize)
|
||||
|
||||
def test_unusual_order_positive_stride(self):
|
||||
a = np.arange(12).reshape(3, 4)
|
||||
b = a.T
|
||||
low, high = array_utils.byte_bounds(b)
|
||||
assert_equal(high - low, b.size * b.itemsize)
|
||||
|
||||
def test_unusual_order_negative_stride(self):
|
||||
a = np.arange(12).reshape(3, 4)
|
||||
b = a.T[::-1]
|
||||
low, high = array_utils.byte_bounds(b)
|
||||
assert_equal(high - low, b.size * b.itemsize)
|
||||
|
||||
def test_strided(self):
|
||||
a = np.arange(12)
|
||||
b = a[::2]
|
||||
low, high = array_utils.byte_bounds(b)
|
||||
# the largest pointer address is lost (even numbers only in the
|
||||
# stride), and compensate addresses for striding by 2
|
||||
assert_equal(high - low, b.size * 2 * b.itemsize - b.itemsize)
|
||||
1416
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_arraypad.py
vendored
Normal file
1416
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_arraypad.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1000
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_arraysetops.py
vendored
Normal file
1000
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_arraysetops.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
46
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_arrayterator.py
vendored
Normal file
46
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_arrayterator.py
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
from operator import mul
|
||||
from functools import reduce
|
||||
|
||||
import numpy as np
|
||||
from numpy.random import randint
|
||||
from numpy.lib import Arrayterator
|
||||
from numpy.testing import assert_
|
||||
|
||||
|
||||
def test():
|
||||
np.random.seed(np.arange(10))
|
||||
|
||||
# Create a random array
|
||||
ndims = randint(5)+1
|
||||
shape = tuple(randint(10)+1 for dim in range(ndims))
|
||||
els = reduce(mul, shape)
|
||||
a = np.arange(els)
|
||||
a.shape = shape
|
||||
|
||||
buf_size = randint(2*els)
|
||||
b = Arrayterator(a, buf_size)
|
||||
|
||||
# Check that each block has at most ``buf_size`` elements
|
||||
for block in b:
|
||||
assert_(len(block.flat) <= (buf_size or els))
|
||||
|
||||
# Check that all elements are iterated correctly
|
||||
assert_(list(b.flat) == list(a.flat))
|
||||
|
||||
# Slice arrayterator
|
||||
start = [randint(dim) for dim in shape]
|
||||
stop = [randint(dim)+1 for dim in shape]
|
||||
step = [randint(dim)+1 for dim in shape]
|
||||
slice_ = tuple(slice(*t) for t in zip(start, stop, step))
|
||||
c = b[slice_]
|
||||
d = a[slice_]
|
||||
|
||||
# Check that each block has at most ``buf_size`` elements
|
||||
for block in c:
|
||||
assert_(len(block.flat) <= (buf_size or els))
|
||||
|
||||
# Check that the arrayterator is sliced correctly
|
||||
assert_(np.all(c.__array__() == d))
|
||||
|
||||
# Check that all elements are iterated correctly
|
||||
assert_(list(c.flat) == list(d.flat))
|
||||
1027
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_format.py
vendored
Normal file
1027
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_format.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
4418
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_function_base.py
vendored
Normal file
4418
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_function_base.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
837
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_histograms.py
vendored
Normal file
837
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_histograms.py
vendored
Normal file
@@ -0,0 +1,837 @@
|
||||
import numpy as np
|
||||
|
||||
from numpy import histogram, histogramdd, histogram_bin_edges
|
||||
from numpy.testing import (
|
||||
assert_, assert_equal, assert_array_equal, assert_almost_equal,
|
||||
assert_array_almost_equal, assert_raises, assert_allclose,
|
||||
assert_array_max_ulp, assert_raises_regex, suppress_warnings,
|
||||
)
|
||||
from numpy.testing._private.utils import requires_memory
|
||||
import pytest
|
||||
|
||||
|
||||
class TestHistogram:
|
||||
|
||||
def setup_method(self):
|
||||
pass
|
||||
|
||||
def teardown_method(self):
|
||||
pass
|
||||
|
||||
def test_simple(self):
|
||||
n = 100
|
||||
v = np.random.rand(n)
|
||||
(a, b) = histogram(v)
|
||||
# check if the sum of the bins equals the number of samples
|
||||
assert_equal(np.sum(a, axis=0), n)
|
||||
# check that the bin counts are evenly spaced when the data is from
|
||||
# a linear function
|
||||
(a, b) = histogram(np.linspace(0, 10, 100))
|
||||
assert_array_equal(a, 10)
|
||||
|
||||
def test_one_bin(self):
|
||||
# Ticket 632
|
||||
hist, edges = histogram([1, 2, 3, 4], [1, 2])
|
||||
assert_array_equal(hist, [2, ])
|
||||
assert_array_equal(edges, [1, 2])
|
||||
assert_raises(ValueError, histogram, [1, 2], bins=0)
|
||||
h, e = histogram([1, 2], bins=1)
|
||||
assert_equal(h, np.array([2]))
|
||||
assert_allclose(e, np.array([1., 2.]))
|
||||
|
||||
def test_density(self):
|
||||
# Check that the integral of the density equals 1.
|
||||
n = 100
|
||||
v = np.random.rand(n)
|
||||
a, b = histogram(v, density=True)
|
||||
area = np.sum(a * np.diff(b))
|
||||
assert_almost_equal(area, 1)
|
||||
|
||||
# Check with non-constant bin widths
|
||||
v = np.arange(10)
|
||||
bins = [0, 1, 3, 6, 10]
|
||||
a, b = histogram(v, bins, density=True)
|
||||
assert_array_equal(a, .1)
|
||||
assert_equal(np.sum(a * np.diff(b)), 1)
|
||||
|
||||
# Test that passing False works too
|
||||
a, b = histogram(v, bins, density=False)
|
||||
assert_array_equal(a, [1, 2, 3, 4])
|
||||
|
||||
# Variable bin widths are especially useful to deal with
|
||||
# infinities.
|
||||
v = np.arange(10)
|
||||
bins = [0, 1, 3, 6, np.inf]
|
||||
a, b = histogram(v, bins, density=True)
|
||||
assert_array_equal(a, [.1, .1, .1, 0.])
|
||||
|
||||
# Taken from a bug report from N. Becker on the numpy-discussion
|
||||
# mailing list Aug. 6, 2010.
|
||||
counts, dmy = np.histogram(
|
||||
[1, 2, 3, 4], [0.5, 1.5, np.inf], density=True)
|
||||
assert_equal(counts, [.25, 0])
|
||||
|
||||
def test_outliers(self):
|
||||
# Check that outliers are not tallied
|
||||
a = np.arange(10) + .5
|
||||
|
||||
# Lower outliers
|
||||
h, b = histogram(a, range=[0, 9])
|
||||
assert_equal(h.sum(), 9)
|
||||
|
||||
# Upper outliers
|
||||
h, b = histogram(a, range=[1, 10])
|
||||
assert_equal(h.sum(), 9)
|
||||
|
||||
# Normalization
|
||||
h, b = histogram(a, range=[1, 9], density=True)
|
||||
assert_almost_equal((h * np.diff(b)).sum(), 1, decimal=15)
|
||||
|
||||
# Weights
|
||||
w = np.arange(10) + .5
|
||||
h, b = histogram(a, range=[1, 9], weights=w, density=True)
|
||||
assert_equal((h * np.diff(b)).sum(), 1)
|
||||
|
||||
h, b = histogram(a, bins=8, range=[1, 9], weights=w)
|
||||
assert_equal(h, w[1:-1])
|
||||
|
||||
def test_arr_weights_mismatch(self):
|
||||
a = np.arange(10) + .5
|
||||
w = np.arange(11) + .5
|
||||
with assert_raises_regex(ValueError, "same shape as"):
|
||||
h, b = histogram(a, range=[1, 9], weights=w, density=True)
|
||||
|
||||
|
||||
def test_type(self):
|
||||
# Check the type of the returned histogram
|
||||
a = np.arange(10) + .5
|
||||
h, b = histogram(a)
|
||||
assert_(np.issubdtype(h.dtype, np.integer))
|
||||
|
||||
h, b = histogram(a, density=True)
|
||||
assert_(np.issubdtype(h.dtype, np.floating))
|
||||
|
||||
h, b = histogram(a, weights=np.ones(10, int))
|
||||
assert_(np.issubdtype(h.dtype, np.integer))
|
||||
|
||||
h, b = histogram(a, weights=np.ones(10, float))
|
||||
assert_(np.issubdtype(h.dtype, np.floating))
|
||||
|
||||
def test_f32_rounding(self):
|
||||
# gh-4799, check that the rounding of the edges works with float32
|
||||
x = np.array([276.318359, -69.593948, 21.329449], dtype=np.float32)
|
||||
y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32)
|
||||
counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100)
|
||||
assert_equal(counts_hist.sum(), 3.)
|
||||
|
||||
def test_bool_conversion(self):
|
||||
# gh-12107
|
||||
# Reference integer histogram
|
||||
a = np.array([1, 1, 0], dtype=np.uint8)
|
||||
int_hist, int_edges = np.histogram(a)
|
||||
|
||||
# Should raise an warning on booleans
|
||||
# Ensure that the histograms are equivalent, need to suppress
|
||||
# the warnings to get the actual outputs
|
||||
with suppress_warnings() as sup:
|
||||
rec = sup.record(RuntimeWarning, 'Converting input from .*')
|
||||
hist, edges = np.histogram([True, True, False])
|
||||
# A warning should be issued
|
||||
assert_equal(len(rec), 1)
|
||||
assert_array_equal(hist, int_hist)
|
||||
assert_array_equal(edges, int_edges)
|
||||
|
||||
def test_weights(self):
|
||||
v = np.random.rand(100)
|
||||
w = np.ones(100) * 5
|
||||
a, b = histogram(v)
|
||||
na, nb = histogram(v, density=True)
|
||||
wa, wb = histogram(v, weights=w)
|
||||
nwa, nwb = histogram(v, weights=w, density=True)
|
||||
assert_array_almost_equal(a * 5, wa)
|
||||
assert_array_almost_equal(na, nwa)
|
||||
|
||||
# Check weights are properly applied.
|
||||
v = np.linspace(0, 10, 10)
|
||||
w = np.concatenate((np.zeros(5), np.ones(5)))
|
||||
wa, wb = histogram(v, bins=np.arange(11), weights=w)
|
||||
assert_array_almost_equal(wa, w)
|
||||
|
||||
# Check with integer weights
|
||||
wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1])
|
||||
assert_array_equal(wa, [4, 5, 0, 1])
|
||||
wa, wb = histogram(
|
||||
[1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1], density=True)
|
||||
assert_array_almost_equal(wa, np.array([4, 5, 0, 1]) / 10. / 3. * 4)
|
||||
|
||||
# Check weights with non-uniform bin widths
|
||||
a, b = histogram(
|
||||
np.arange(9), [0, 1, 3, 6, 10],
|
||||
weights=[2, 1, 1, 1, 1, 1, 1, 1, 1], density=True)
|
||||
assert_almost_equal(a, [.2, .1, .1, .075])
|
||||
|
||||
def test_exotic_weights(self):
|
||||
|
||||
# Test the use of weights that are not integer or floats, but e.g.
|
||||
# complex numbers or object types.
|
||||
|
||||
# Complex weights
|
||||
values = np.array([1.3, 2.5, 2.3])
|
||||
weights = np.array([1, -1, 2]) + 1j * np.array([2, 1, 2])
|
||||
|
||||
# Check with custom bins
|
||||
wa, wb = histogram(values, bins=[0, 2, 3], weights=weights)
|
||||
assert_array_almost_equal(wa, np.array([1, 1]) + 1j * np.array([2, 3]))
|
||||
|
||||
# Check with even bins
|
||||
wa, wb = histogram(values, bins=2, range=[1, 3], weights=weights)
|
||||
assert_array_almost_equal(wa, np.array([1, 1]) + 1j * np.array([2, 3]))
|
||||
|
||||
# Decimal weights
|
||||
from decimal import Decimal
|
||||
values = np.array([1.3, 2.5, 2.3])
|
||||
weights = np.array([Decimal(1), Decimal(2), Decimal(3)])
|
||||
|
||||
# Check with custom bins
|
||||
wa, wb = histogram(values, bins=[0, 2, 3], weights=weights)
|
||||
assert_array_almost_equal(wa, [Decimal(1), Decimal(5)])
|
||||
|
||||
# Check with even bins
|
||||
wa, wb = histogram(values, bins=2, range=[1, 3], weights=weights)
|
||||
assert_array_almost_equal(wa, [Decimal(1), Decimal(5)])
|
||||
|
||||
def test_no_side_effects(self):
|
||||
# This is a regression test that ensures that values passed to
|
||||
# ``histogram`` are unchanged.
|
||||
values = np.array([1.3, 2.5, 2.3])
|
||||
np.histogram(values, range=[-10, 10], bins=100)
|
||||
assert_array_almost_equal(values, [1.3, 2.5, 2.3])
|
||||
|
||||
def test_empty(self):
|
||||
a, b = histogram([], bins=([0, 1]))
|
||||
assert_array_equal(a, np.array([0]))
|
||||
assert_array_equal(b, np.array([0, 1]))
|
||||
|
||||
def test_error_binnum_type (self):
|
||||
# Tests if right Error is raised if bins argument is float
|
||||
vals = np.linspace(0.0, 1.0, num=100)
|
||||
histogram(vals, 5)
|
||||
assert_raises(TypeError, histogram, vals, 2.4)
|
||||
|
||||
def test_finite_range(self):
|
||||
# Normal ranges should be fine
|
||||
vals = np.linspace(0.0, 1.0, num=100)
|
||||
histogram(vals, range=[0.25,0.75])
|
||||
assert_raises(ValueError, histogram, vals, range=[np.nan,0.75])
|
||||
assert_raises(ValueError, histogram, vals, range=[0.25,np.inf])
|
||||
|
||||
def test_invalid_range(self):
|
||||
# start of range must be < end of range
|
||||
vals = np.linspace(0.0, 1.0, num=100)
|
||||
with assert_raises_regex(ValueError, "max must be larger than"):
|
||||
np.histogram(vals, range=[0.1, 0.01])
|
||||
|
||||
def test_bin_edge_cases(self):
|
||||
# Ensure that floating-point computations correctly place edge cases.
|
||||
arr = np.array([337, 404, 739, 806, 1007, 1811, 2012])
|
||||
hist, edges = np.histogram(arr, bins=8296, range=(2, 2280))
|
||||
mask = hist > 0
|
||||
left_edges = edges[:-1][mask]
|
||||
right_edges = edges[1:][mask]
|
||||
for x, left, right in zip(arr, left_edges, right_edges):
|
||||
assert_(x >= left)
|
||||
assert_(x < right)
|
||||
|
||||
def test_last_bin_inclusive_range(self):
|
||||
arr = np.array([0., 0., 0., 1., 2., 3., 3., 4., 5.])
|
||||
hist, edges = np.histogram(arr, bins=30, range=(-0.5, 5))
|
||||
assert_equal(hist[-1], 1)
|
||||
|
||||
def test_bin_array_dims(self):
|
||||
# gracefully handle bins object > 1 dimension
|
||||
vals = np.linspace(0.0, 1.0, num=100)
|
||||
bins = np.array([[0, 0.5], [0.6, 1.0]])
|
||||
with assert_raises_regex(ValueError, "must be 1d"):
|
||||
np.histogram(vals, bins=bins)
|
||||
|
||||
def test_unsigned_monotonicity_check(self):
|
||||
# Ensures ValueError is raised if bins not increasing monotonically
|
||||
# when bins contain unsigned values (see #9222)
|
||||
arr = np.array([2])
|
||||
bins = np.array([1, 3, 1], dtype='uint64')
|
||||
with assert_raises(ValueError):
|
||||
hist, edges = np.histogram(arr, bins=bins)
|
||||
|
||||
def test_object_array_of_0d(self):
|
||||
# gh-7864
|
||||
assert_raises(ValueError,
|
||||
histogram, [np.array(0.4) for i in range(10)] + [-np.inf])
|
||||
assert_raises(ValueError,
|
||||
histogram, [np.array(0.4) for i in range(10)] + [np.inf])
|
||||
|
||||
# these should not crash
|
||||
np.histogram([np.array(0.5) for i in range(10)] + [.500000000000001])
|
||||
np.histogram([np.array(0.5) for i in range(10)] + [.5])
|
||||
|
||||
def test_some_nan_values(self):
|
||||
# gh-7503
|
||||
one_nan = np.array([0, 1, np.nan])
|
||||
all_nan = np.array([np.nan, np.nan])
|
||||
|
||||
# the internal comparisons with NaN give warnings
|
||||
sup = suppress_warnings()
|
||||
sup.filter(RuntimeWarning)
|
||||
with sup:
|
||||
# can't infer range with nan
|
||||
assert_raises(ValueError, histogram, one_nan, bins='auto')
|
||||
assert_raises(ValueError, histogram, all_nan, bins='auto')
|
||||
|
||||
# explicit range solves the problem
|
||||
h, b = histogram(one_nan, bins='auto', range=(0, 1))
|
||||
assert_equal(h.sum(), 2) # nan is not counted
|
||||
h, b = histogram(all_nan, bins='auto', range=(0, 1))
|
||||
assert_equal(h.sum(), 0) # nan is not counted
|
||||
|
||||
# as does an explicit set of bins
|
||||
h, b = histogram(one_nan, bins=[0, 1])
|
||||
assert_equal(h.sum(), 2) # nan is not counted
|
||||
h, b = histogram(all_nan, bins=[0, 1])
|
||||
assert_equal(h.sum(), 0) # nan is not counted
|
||||
|
||||
def test_datetime(self):
|
||||
begin = np.datetime64('2000-01-01', 'D')
|
||||
offsets = np.array([0, 0, 1, 1, 2, 3, 5, 10, 20])
|
||||
bins = np.array([0, 2, 7, 20])
|
||||
dates = begin + offsets
|
||||
date_bins = begin + bins
|
||||
|
||||
td = np.dtype('timedelta64[D]')
|
||||
|
||||
# Results should be the same for integer offsets or datetime values.
|
||||
# For now, only explicit bins are supported, since linspace does not
|
||||
# work on datetimes or timedeltas
|
||||
d_count, d_edge = histogram(dates, bins=date_bins)
|
||||
t_count, t_edge = histogram(offsets.astype(td), bins=bins.astype(td))
|
||||
i_count, i_edge = histogram(offsets, bins=bins)
|
||||
|
||||
assert_equal(d_count, i_count)
|
||||
assert_equal(t_count, i_count)
|
||||
|
||||
assert_equal((d_edge - begin).astype(int), i_edge)
|
||||
assert_equal(t_edge.astype(int), i_edge)
|
||||
|
||||
assert_equal(d_edge.dtype, dates.dtype)
|
||||
assert_equal(t_edge.dtype, td)
|
||||
|
||||
def do_signed_overflow_bounds(self, dtype):
|
||||
exponent = 8 * np.dtype(dtype).itemsize - 1
|
||||
arr = np.array([-2**exponent + 4, 2**exponent - 4], dtype=dtype)
|
||||
hist, e = histogram(arr, bins=2)
|
||||
assert_equal(e, [-2**exponent + 4, 0, 2**exponent - 4])
|
||||
assert_equal(hist, [1, 1])
|
||||
|
||||
def test_signed_overflow_bounds(self):
|
||||
self.do_signed_overflow_bounds(np.byte)
|
||||
self.do_signed_overflow_bounds(np.short)
|
||||
self.do_signed_overflow_bounds(np.intc)
|
||||
self.do_signed_overflow_bounds(np.int_)
|
||||
self.do_signed_overflow_bounds(np.longlong)
|
||||
|
||||
def do_precision_lower_bound(self, float_small, float_large):
|
||||
eps = np.finfo(float_large).eps
|
||||
|
||||
arr = np.array([1.0], float_small)
|
||||
range = np.array([1.0 + eps, 2.0], float_large)
|
||||
|
||||
# test is looking for behavior when the bounds change between dtypes
|
||||
if range.astype(float_small)[0] != 1:
|
||||
return
|
||||
|
||||
# previously crashed
|
||||
count, x_loc = np.histogram(arr, bins=1, range=range)
|
||||
assert_equal(count, [0])
|
||||
assert_equal(x_loc.dtype, float_large)
|
||||
|
||||
def do_precision_upper_bound(self, float_small, float_large):
|
||||
eps = np.finfo(float_large).eps
|
||||
|
||||
arr = np.array([1.0], float_small)
|
||||
range = np.array([0.0, 1.0 - eps], float_large)
|
||||
|
||||
# test is looking for behavior when the bounds change between dtypes
|
||||
if range.astype(float_small)[-1] != 1:
|
||||
return
|
||||
|
||||
# previously crashed
|
||||
count, x_loc = np.histogram(arr, bins=1, range=range)
|
||||
assert_equal(count, [0])
|
||||
|
||||
assert_equal(x_loc.dtype, float_large)
|
||||
|
||||
def do_precision(self, float_small, float_large):
|
||||
self.do_precision_lower_bound(float_small, float_large)
|
||||
self.do_precision_upper_bound(float_small, float_large)
|
||||
|
||||
def test_precision(self):
|
||||
# not looping results in a useful stack trace upon failure
|
||||
self.do_precision(np.half, np.single)
|
||||
self.do_precision(np.half, np.double)
|
||||
self.do_precision(np.half, np.longdouble)
|
||||
self.do_precision(np.single, np.double)
|
||||
self.do_precision(np.single, np.longdouble)
|
||||
self.do_precision(np.double, np.longdouble)
|
||||
|
||||
def test_histogram_bin_edges(self):
|
||||
hist, e = histogram([1, 2, 3, 4], [1, 2])
|
||||
edges = histogram_bin_edges([1, 2, 3, 4], [1, 2])
|
||||
assert_array_equal(edges, e)
|
||||
|
||||
arr = np.array([0., 0., 0., 1., 2., 3., 3., 4., 5.])
|
||||
hist, e = histogram(arr, bins=30, range=(-0.5, 5))
|
||||
edges = histogram_bin_edges(arr, bins=30, range=(-0.5, 5))
|
||||
assert_array_equal(edges, e)
|
||||
|
||||
hist, e = histogram(arr, bins='auto', range=(0, 1))
|
||||
edges = histogram_bin_edges(arr, bins='auto', range=(0, 1))
|
||||
assert_array_equal(edges, e)
|
||||
|
||||
# @requires_memory(free_bytes=1e10)
|
||||
# @pytest.mark.slow
|
||||
@pytest.mark.skip(reason="Bad memory reports lead to OOM in ci testing")
|
||||
def test_big_arrays(self):
|
||||
sample = np.zeros([100000000, 3])
|
||||
xbins = 400
|
||||
ybins = 400
|
||||
zbins = np.arange(16000)
|
||||
hist = np.histogramdd(sample=sample, bins=(xbins, ybins, zbins))
|
||||
assert_equal(type(hist), type((1, 2)))
|
||||
|
||||
def test_gh_23110(self):
|
||||
hist, e = np.histogram(np.array([-0.9e-308], dtype='>f8'),
|
||||
bins=2,
|
||||
range=(-1e-308, -2e-313))
|
||||
expected_hist = np.array([1, 0])
|
||||
assert_array_equal(hist, expected_hist)
|
||||
|
||||
|
||||
class TestHistogramOptimBinNums:
|
||||
"""
|
||||
Provide test coverage when using provided estimators for optimal number of
|
||||
bins
|
||||
"""
|
||||
|
||||
def test_empty(self):
|
||||
estimator_list = ['fd', 'scott', 'rice', 'sturges',
|
||||
'doane', 'sqrt', 'auto', 'stone']
|
||||
# check it can deal with empty data
|
||||
for estimator in estimator_list:
|
||||
a, b = histogram([], bins=estimator)
|
||||
assert_array_equal(a, np.array([0]))
|
||||
assert_array_equal(b, np.array([0, 1]))
|
||||
|
||||
def test_simple(self):
|
||||
"""
|
||||
Straightforward testing with a mixture of linspace data (for
|
||||
consistency). All test values have been precomputed and the values
|
||||
shouldn't change
|
||||
"""
|
||||
# Some basic sanity checking, with some fixed data.
|
||||
# Checking for the correct number of bins
|
||||
basic_test = {50: {'fd': 4, 'scott': 4, 'rice': 8, 'sturges': 7,
|
||||
'doane': 8, 'sqrt': 8, 'auto': 7, 'stone': 2},
|
||||
500: {'fd': 8, 'scott': 8, 'rice': 16, 'sturges': 10,
|
||||
'doane': 12, 'sqrt': 23, 'auto': 10, 'stone': 9},
|
||||
5000: {'fd': 17, 'scott': 17, 'rice': 35, 'sturges': 14,
|
||||
'doane': 17, 'sqrt': 71, 'auto': 17, 'stone': 20}}
|
||||
|
||||
for testlen, expectedResults in basic_test.items():
|
||||
# Create some sort of non uniform data to test with
|
||||
# (2 peak uniform mixture)
|
||||
x1 = np.linspace(-10, -1, testlen // 5 * 2)
|
||||
x2 = np.linspace(1, 10, testlen // 5 * 3)
|
||||
x = np.concatenate((x1, x2))
|
||||
for estimator, numbins in expectedResults.items():
|
||||
a, b = np.histogram(x, estimator)
|
||||
assert_equal(len(a), numbins, err_msg="For the {0} estimator "
|
||||
"with datasize of {1}".format(estimator, testlen))
|
||||
|
||||
def test_small(self):
|
||||
"""
|
||||
Smaller datasets have the potential to cause issues with the data
|
||||
adaptive methods, especially the FD method. All bin numbers have been
|
||||
precalculated.
|
||||
"""
|
||||
small_dat = {1: {'fd': 1, 'scott': 1, 'rice': 1, 'sturges': 1,
|
||||
'doane': 1, 'sqrt': 1, 'stone': 1},
|
||||
2: {'fd': 2, 'scott': 1, 'rice': 3, 'sturges': 2,
|
||||
'doane': 1, 'sqrt': 2, 'stone': 1},
|
||||
3: {'fd': 2, 'scott': 2, 'rice': 3, 'sturges': 3,
|
||||
'doane': 3, 'sqrt': 2, 'stone': 1}}
|
||||
|
||||
for testlen, expectedResults in small_dat.items():
|
||||
testdat = np.arange(testlen).astype(float)
|
||||
for estimator, expbins in expectedResults.items():
|
||||
a, b = np.histogram(testdat, estimator)
|
||||
assert_equal(len(a), expbins, err_msg="For the {0} estimator "
|
||||
"with datasize of {1}".format(estimator, testlen))
|
||||
|
||||
def test_incorrect_methods(self):
|
||||
"""
|
||||
Check a Value Error is thrown when an unknown string is passed in
|
||||
"""
|
||||
check_list = ['mad', 'freeman', 'histograms', 'IQR']
|
||||
for estimator in check_list:
|
||||
assert_raises(ValueError, histogram, [1, 2, 3], estimator)
|
||||
|
||||
def test_novariance(self):
|
||||
"""
|
||||
Check that methods handle no variance in data
|
||||
Primarily for Scott and FD as the SD and IQR are both 0 in this case
|
||||
"""
|
||||
novar_dataset = np.ones(100)
|
||||
novar_resultdict = {'fd': 1, 'scott': 1, 'rice': 1, 'sturges': 1,
|
||||
'doane': 1, 'sqrt': 1, 'auto': 1, 'stone': 1}
|
||||
|
||||
for estimator, numbins in novar_resultdict.items():
|
||||
a, b = np.histogram(novar_dataset, estimator)
|
||||
assert_equal(len(a), numbins, err_msg="{0} estimator, "
|
||||
"No Variance test".format(estimator))
|
||||
|
||||
def test_limited_variance(self):
|
||||
"""
|
||||
Check when IQR is 0, but variance exists, we return the sturges value
|
||||
and not the fd value.
|
||||
"""
|
||||
lim_var_data = np.ones(1000)
|
||||
lim_var_data[:3] = 0
|
||||
lim_var_data[-4:] = 100
|
||||
|
||||
edges_auto = histogram_bin_edges(lim_var_data, 'auto')
|
||||
assert_equal(edges_auto, np.linspace(0, 100, 12))
|
||||
|
||||
edges_fd = histogram_bin_edges(lim_var_data, 'fd')
|
||||
assert_equal(edges_fd, np.array([0, 100]))
|
||||
|
||||
edges_sturges = histogram_bin_edges(lim_var_data, 'sturges')
|
||||
assert_equal(edges_sturges, np.linspace(0, 100, 12))
|
||||
|
||||
def test_outlier(self):
|
||||
"""
|
||||
Check the FD, Scott and Doane with outliers.
|
||||
|
||||
The FD estimates a smaller binwidth since it's less affected by
|
||||
outliers. Since the range is so (artificially) large, this means more
|
||||
bins, most of which will be empty, but the data of interest usually is
|
||||
unaffected. The Scott estimator is more affected and returns fewer bins,
|
||||
despite most of the variance being in one area of the data. The Doane
|
||||
estimator lies somewhere between the other two.
|
||||
"""
|
||||
xcenter = np.linspace(-10, 10, 50)
|
||||
outlier_dataset = np.hstack((np.linspace(-110, -100, 5), xcenter))
|
||||
|
||||
outlier_resultdict = {'fd': 21, 'scott': 5, 'doane': 11, 'stone': 6}
|
||||
|
||||
for estimator, numbins in outlier_resultdict.items():
|
||||
a, b = np.histogram(outlier_dataset, estimator)
|
||||
assert_equal(len(a), numbins)
|
||||
|
||||
def test_scott_vs_stone(self):
|
||||
"""Verify that Scott's rule and Stone's rule converges for normally distributed data"""
|
||||
|
||||
def nbins_ratio(seed, size):
|
||||
rng = np.random.RandomState(seed)
|
||||
x = rng.normal(loc=0, scale=2, size=size)
|
||||
a, b = len(np.histogram(x, 'stone')[0]), len(np.histogram(x, 'scott')[0])
|
||||
return a / (a + b)
|
||||
|
||||
ll = [[nbins_ratio(seed, size) for size in np.geomspace(start=10, stop=100, num=4).round().astype(int)]
|
||||
for seed in range(10)]
|
||||
|
||||
# the average difference between the two methods decreases as the dataset size increases.
|
||||
avg = abs(np.mean(ll, axis=0) - 0.5)
|
||||
assert_almost_equal(avg, [0.15, 0.09, 0.08, 0.03], decimal=2)
|
||||
|
||||
def test_simple_range(self):
|
||||
"""
|
||||
Straightforward testing with a mixture of linspace data (for
|
||||
consistency). Adding in a 3rd mixture that will then be
|
||||
completely ignored. All test values have been precomputed and
|
||||
the shouldn't change.
|
||||
"""
|
||||
# some basic sanity checking, with some fixed data.
|
||||
# Checking for the correct number of bins
|
||||
basic_test = {
|
||||
50: {'fd': 8, 'scott': 8, 'rice': 15,
|
||||
'sturges': 14, 'auto': 14, 'stone': 8},
|
||||
500: {'fd': 15, 'scott': 16, 'rice': 32,
|
||||
'sturges': 20, 'auto': 20, 'stone': 80},
|
||||
5000: {'fd': 33, 'scott': 33, 'rice': 69,
|
||||
'sturges': 27, 'auto': 33, 'stone': 80}
|
||||
}
|
||||
|
||||
for testlen, expectedResults in basic_test.items():
|
||||
# create some sort of non uniform data to test with
|
||||
# (3 peak uniform mixture)
|
||||
x1 = np.linspace(-10, -1, testlen // 5 * 2)
|
||||
x2 = np.linspace(1, 10, testlen // 5 * 3)
|
||||
x3 = np.linspace(-100, -50, testlen)
|
||||
x = np.hstack((x1, x2, x3))
|
||||
for estimator, numbins in expectedResults.items():
|
||||
a, b = np.histogram(x, estimator, range = (-20, 20))
|
||||
msg = "For the {0} estimator".format(estimator)
|
||||
msg += " with datasize of {0}".format(testlen)
|
||||
assert_equal(len(a), numbins, err_msg=msg)
|
||||
|
||||
@pytest.mark.parametrize("bins", ['auto', 'fd', 'doane', 'scott',
|
||||
'stone', 'rice', 'sturges'])
|
||||
def test_signed_integer_data(self, bins):
|
||||
# Regression test for gh-14379.
|
||||
a = np.array([-2, 0, 127], dtype=np.int8)
|
||||
hist, edges = np.histogram(a, bins=bins)
|
||||
hist32, edges32 = np.histogram(a.astype(np.int32), bins=bins)
|
||||
assert_array_equal(hist, hist32)
|
||||
assert_array_equal(edges, edges32)
|
||||
|
||||
@pytest.mark.parametrize("bins", ['auto', 'fd', 'doane', 'scott',
|
||||
'stone', 'rice', 'sturges'])
|
||||
def test_integer(self, bins):
|
||||
"""
|
||||
Test that bin width for integer data is at least 1.
|
||||
"""
|
||||
with suppress_warnings() as sup:
|
||||
if bins == 'stone':
|
||||
sup.filter(RuntimeWarning)
|
||||
assert_equal(
|
||||
np.histogram_bin_edges(np.tile(np.arange(9), 1000), bins),
|
||||
np.arange(9))
|
||||
|
||||
def test_integer_non_auto(self):
|
||||
"""
|
||||
Test that the bin-width>=1 requirement *only* applies to auto binning.
|
||||
"""
|
||||
assert_equal(
|
||||
np.histogram_bin_edges(np.tile(np.arange(9), 1000), 16),
|
||||
np.arange(17) / 2)
|
||||
assert_equal(
|
||||
np.histogram_bin_edges(np.tile(np.arange(9), 1000), [.1, .2]),
|
||||
[.1, .2])
|
||||
|
||||
def test_simple_weighted(self):
|
||||
"""
|
||||
Check that weighted data raises a TypeError
|
||||
"""
|
||||
estimator_list = ['fd', 'scott', 'rice', 'sturges', 'auto']
|
||||
for estimator in estimator_list:
|
||||
assert_raises(TypeError, histogram, [1, 2, 3],
|
||||
estimator, weights=[1, 2, 3])
|
||||
|
||||
|
||||
class TestHistogramdd:
|
||||
|
||||
def test_simple(self):
|
||||
x = np.array([[-.5, .5, 1.5], [-.5, 1.5, 2.5], [-.5, 2.5, .5],
|
||||
[.5, .5, 1.5], [.5, 1.5, 2.5], [.5, 2.5, 2.5]])
|
||||
H, edges = histogramdd(x, (2, 3, 3),
|
||||
range=[[-1, 1], [0, 3], [0, 3]])
|
||||
answer = np.array([[[0, 1, 0], [0, 0, 1], [1, 0, 0]],
|
||||
[[0, 1, 0], [0, 0, 1], [0, 0, 1]]])
|
||||
assert_array_equal(H, answer)
|
||||
|
||||
# Check normalization
|
||||
ed = [[-2, 0, 2], [0, 1, 2, 3], [0, 1, 2, 3]]
|
||||
H, edges = histogramdd(x, bins=ed, density=True)
|
||||
assert_(np.all(H == answer / 12.))
|
||||
|
||||
# Check that H has the correct shape.
|
||||
H, edges = histogramdd(x, (2, 3, 4),
|
||||
range=[[-1, 1], [0, 3], [0, 4]],
|
||||
density=True)
|
||||
answer = np.array([[[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]],
|
||||
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0]]])
|
||||
assert_array_almost_equal(H, answer / 6., 4)
|
||||
# Check that a sequence of arrays is accepted and H has the correct
|
||||
# shape.
|
||||
z = [np.squeeze(y) for y in np.split(x, 3, axis=1)]
|
||||
H, edges = histogramdd(
|
||||
z, bins=(4, 3, 2), range=[[-2, 2], [0, 3], [0, 2]])
|
||||
answer = np.array([[[0, 0], [0, 0], [0, 0]],
|
||||
[[0, 1], [0, 0], [1, 0]],
|
||||
[[0, 1], [0, 0], [0, 0]],
|
||||
[[0, 0], [0, 0], [0, 0]]])
|
||||
assert_array_equal(H, answer)
|
||||
|
||||
Z = np.zeros((5, 5, 5))
|
||||
Z[list(range(5)), list(range(5)), list(range(5))] = 1.
|
||||
H, edges = histogramdd([np.arange(5), np.arange(5), np.arange(5)], 5)
|
||||
assert_array_equal(H, Z)
|
||||
|
||||
def test_shape_3d(self):
|
||||
# All possible permutations for bins of different lengths in 3D.
|
||||
bins = ((5, 4, 6), (6, 4, 5), (5, 6, 4), (4, 6, 5), (6, 5, 4),
|
||||
(4, 5, 6))
|
||||
r = np.random.rand(10, 3)
|
||||
for b in bins:
|
||||
H, edges = histogramdd(r, b)
|
||||
assert_(H.shape == b)
|
||||
|
||||
def test_shape_4d(self):
|
||||
# All possible permutations for bins of different lengths in 4D.
|
||||
bins = ((7, 4, 5, 6), (4, 5, 7, 6), (5, 6, 4, 7), (7, 6, 5, 4),
|
||||
(5, 7, 6, 4), (4, 6, 7, 5), (6, 5, 7, 4), (7, 5, 4, 6),
|
||||
(7, 4, 6, 5), (6, 4, 7, 5), (6, 7, 5, 4), (4, 6, 5, 7),
|
||||
(4, 7, 5, 6), (5, 4, 6, 7), (5, 7, 4, 6), (6, 7, 4, 5),
|
||||
(6, 5, 4, 7), (4, 7, 6, 5), (4, 5, 6, 7), (7, 6, 4, 5),
|
||||
(5, 4, 7, 6), (5, 6, 7, 4), (6, 4, 5, 7), (7, 5, 6, 4))
|
||||
|
||||
r = np.random.rand(10, 4)
|
||||
for b in bins:
|
||||
H, edges = histogramdd(r, b)
|
||||
assert_(H.shape == b)
|
||||
|
||||
def test_weights(self):
|
||||
v = np.random.rand(100, 2)
|
||||
hist, edges = histogramdd(v)
|
||||
n_hist, edges = histogramdd(v, density=True)
|
||||
w_hist, edges = histogramdd(v, weights=np.ones(100))
|
||||
assert_array_equal(w_hist, hist)
|
||||
w_hist, edges = histogramdd(v, weights=np.ones(100) * 2, density=True)
|
||||
assert_array_equal(w_hist, n_hist)
|
||||
w_hist, edges = histogramdd(v, weights=np.ones(100, int) * 2)
|
||||
assert_array_equal(w_hist, 2 * hist)
|
||||
|
||||
def test_identical_samples(self):
|
||||
x = np.zeros((10, 2), int)
|
||||
hist, edges = histogramdd(x, bins=2)
|
||||
assert_array_equal(edges[0], np.array([-0.5, 0., 0.5]))
|
||||
|
||||
def test_empty(self):
|
||||
a, b = histogramdd([[], []], bins=([0, 1], [0, 1]))
|
||||
assert_array_max_ulp(a, np.array([[0.]]))
|
||||
a, b = np.histogramdd([[], [], []], bins=2)
|
||||
assert_array_max_ulp(a, np.zeros((2, 2, 2)))
|
||||
|
||||
def test_bins_errors(self):
|
||||
# There are two ways to specify bins. Check for the right errors
|
||||
# when mixing those.
|
||||
x = np.arange(8).reshape(2, 4)
|
||||
assert_raises(ValueError, np.histogramdd, x, bins=[-1, 2, 4, 5])
|
||||
assert_raises(ValueError, np.histogramdd, x, bins=[1, 0.99, 1, 1])
|
||||
assert_raises(
|
||||
ValueError, np.histogramdd, x, bins=[1, 1, 1, [1, 2, 3, -3]])
|
||||
assert_(np.histogramdd(x, bins=[1, 1, 1, [1, 2, 3, 4]]))
|
||||
|
||||
def test_inf_edges(self):
|
||||
# Test using +/-inf bin edges works. See #1788.
|
||||
with np.errstate(invalid='ignore'):
|
||||
x = np.arange(6).reshape(3, 2)
|
||||
expected = np.array([[1, 0], [0, 1], [0, 1]])
|
||||
h, e = np.histogramdd(x, bins=[3, [-np.inf, 2, 10]])
|
||||
assert_allclose(h, expected)
|
||||
h, e = np.histogramdd(x, bins=[3, np.array([-1, 2, np.inf])])
|
||||
assert_allclose(h, expected)
|
||||
h, e = np.histogramdd(x, bins=[3, [-np.inf, 3, np.inf]])
|
||||
assert_allclose(h, expected)
|
||||
|
||||
def test_rightmost_binedge(self):
|
||||
# Test event very close to rightmost binedge. See Github issue #4266
|
||||
x = [0.9999999995]
|
||||
bins = [[0., 0.5, 1.0]]
|
||||
hist, _ = histogramdd(x, bins=bins)
|
||||
assert_(hist[0] == 0.0)
|
||||
assert_(hist[1] == 1.)
|
||||
x = [1.0]
|
||||
bins = [[0., 0.5, 1.0]]
|
||||
hist, _ = histogramdd(x, bins=bins)
|
||||
assert_(hist[0] == 0.0)
|
||||
assert_(hist[1] == 1.)
|
||||
x = [1.0000000001]
|
||||
bins = [[0., 0.5, 1.0]]
|
||||
hist, _ = histogramdd(x, bins=bins)
|
||||
assert_(hist[0] == 0.0)
|
||||
assert_(hist[1] == 0.0)
|
||||
x = [1.0001]
|
||||
bins = [[0., 0.5, 1.0]]
|
||||
hist, _ = histogramdd(x, bins=bins)
|
||||
assert_(hist[0] == 0.0)
|
||||
assert_(hist[1] == 0.0)
|
||||
|
||||
def test_finite_range(self):
|
||||
vals = np.random.random((100, 3))
|
||||
histogramdd(vals, range=[[0.0, 1.0], [0.25, 0.75], [0.25, 0.5]])
|
||||
assert_raises(ValueError, histogramdd, vals,
|
||||
range=[[0.0, 1.0], [0.25, 0.75], [0.25, np.inf]])
|
||||
assert_raises(ValueError, histogramdd, vals,
|
||||
range=[[0.0, 1.0], [np.nan, 0.75], [0.25, 0.5]])
|
||||
|
||||
def test_equal_edges(self):
|
||||
""" Test that adjacent entries in an edge array can be equal """
|
||||
x = np.array([0, 1, 2])
|
||||
y = np.array([0, 1, 2])
|
||||
x_edges = np.array([0, 2, 2])
|
||||
y_edges = 1
|
||||
hist, edges = histogramdd((x, y), bins=(x_edges, y_edges))
|
||||
|
||||
hist_expected = np.array([
|
||||
[2.],
|
||||
[1.], # x == 2 falls in the final bin
|
||||
])
|
||||
assert_equal(hist, hist_expected)
|
||||
|
||||
def test_edge_dtype(self):
|
||||
""" Test that if an edge array is input, its type is preserved """
|
||||
x = np.array([0, 10, 20])
|
||||
y = x / 10
|
||||
x_edges = np.array([0, 5, 15, 20])
|
||||
y_edges = x_edges / 10
|
||||
hist, edges = histogramdd((x, y), bins=(x_edges, y_edges))
|
||||
|
||||
assert_equal(edges[0].dtype, x_edges.dtype)
|
||||
assert_equal(edges[1].dtype, y_edges.dtype)
|
||||
|
||||
def test_large_integers(self):
|
||||
big = 2**60 # Too large to represent with a full precision float
|
||||
|
||||
x = np.array([0], np.int64)
|
||||
x_edges = np.array([-1, +1], np.int64)
|
||||
y = big + x
|
||||
y_edges = big + x_edges
|
||||
|
||||
hist, edges = histogramdd((x, y), bins=(x_edges, y_edges))
|
||||
|
||||
assert_equal(hist[0, 0], 1)
|
||||
|
||||
def test_density_non_uniform_2d(self):
|
||||
# Defines the following grid:
|
||||
#
|
||||
# 0 2 8
|
||||
# 0+-+-----+
|
||||
# + | +
|
||||
# + | +
|
||||
# 6+-+-----+
|
||||
# 8+-+-----+
|
||||
x_edges = np.array([0, 2, 8])
|
||||
y_edges = np.array([0, 6, 8])
|
||||
relative_areas = np.array([
|
||||
[3, 9],
|
||||
[1, 3]])
|
||||
|
||||
# ensure the number of points in each region is proportional to its area
|
||||
x = np.array([1] + [1]*3 + [7]*3 + [7]*9)
|
||||
y = np.array([7] + [1]*3 + [7]*3 + [1]*9)
|
||||
|
||||
# sanity check that the above worked as intended
|
||||
hist, edges = histogramdd((y, x), bins=(y_edges, x_edges))
|
||||
assert_equal(hist, relative_areas)
|
||||
|
||||
# resulting histogram should be uniform, since counts and areas are proportional
|
||||
hist, edges = histogramdd((y, x), bins=(y_edges, x_edges), density=True)
|
||||
assert_equal(hist, 1 / (8*8))
|
||||
|
||||
def test_density_non_uniform_1d(self):
|
||||
# compare to histogram to show the results are the same
|
||||
v = np.arange(10)
|
||||
bins = np.array([0, 1, 3, 6, 10])
|
||||
hist, edges = histogram(v, bins, density=True)
|
||||
hist_dd, edges_dd = histogramdd((v,), (bins,), density=True)
|
||||
assert_equal(hist, hist_dd)
|
||||
assert_equal(edges, edges_dd[0])
|
||||
553
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_index_tricks.py
vendored
Normal file
553
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_index_tricks.py
vendored
Normal file
@@ -0,0 +1,553 @@
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
from numpy.testing import (
|
||||
assert_, assert_equal, assert_array_equal, assert_almost_equal,
|
||||
assert_array_almost_equal, assert_raises, assert_raises_regex,
|
||||
)
|
||||
from numpy.lib._index_tricks_impl import (
|
||||
mgrid, ogrid, ndenumerate, fill_diagonal, diag_indices, diag_indices_from,
|
||||
index_exp, ndindex, c_, r_, s_, ix_
|
||||
)
|
||||
|
||||
|
||||
class TestRavelUnravelIndex:
|
||||
def test_basic(self):
|
||||
assert_equal(np.unravel_index(2, (2, 2)), (1, 0))
|
||||
|
||||
# test that new shape argument works properly
|
||||
assert_equal(np.unravel_index(indices=2,
|
||||
shape=(2, 2)),
|
||||
(1, 0))
|
||||
|
||||
# test that an invalid second keyword argument
|
||||
# is properly handled, including the old name `dims`.
|
||||
with assert_raises(TypeError):
|
||||
np.unravel_index(indices=2, hape=(2, 2))
|
||||
|
||||
with assert_raises(TypeError):
|
||||
np.unravel_index(2, hape=(2, 2))
|
||||
|
||||
with assert_raises(TypeError):
|
||||
np.unravel_index(254, ims=(17, 94))
|
||||
|
||||
with assert_raises(TypeError):
|
||||
np.unravel_index(254, dims=(17, 94))
|
||||
|
||||
assert_equal(np.ravel_multi_index((1, 0), (2, 2)), 2)
|
||||
assert_equal(np.unravel_index(254, (17, 94)), (2, 66))
|
||||
assert_equal(np.ravel_multi_index((2, 66), (17, 94)), 254)
|
||||
assert_raises(ValueError, np.unravel_index, -1, (2, 2))
|
||||
assert_raises(TypeError, np.unravel_index, 0.5, (2, 2))
|
||||
assert_raises(ValueError, np.unravel_index, 4, (2, 2))
|
||||
assert_raises(ValueError, np.ravel_multi_index, (-3, 1), (2, 2))
|
||||
assert_raises(ValueError, np.ravel_multi_index, (2, 1), (2, 2))
|
||||
assert_raises(ValueError, np.ravel_multi_index, (0, -3), (2, 2))
|
||||
assert_raises(ValueError, np.ravel_multi_index, (0, 2), (2, 2))
|
||||
assert_raises(TypeError, np.ravel_multi_index, (0.1, 0.), (2, 2))
|
||||
|
||||
assert_equal(np.unravel_index((2*3 + 1)*6 + 4, (4, 3, 6)), [2, 1, 4])
|
||||
assert_equal(
|
||||
np.ravel_multi_index([2, 1, 4], (4, 3, 6)), (2*3 + 1)*6 + 4)
|
||||
|
||||
arr = np.array([[3, 6, 6], [4, 5, 1]])
|
||||
assert_equal(np.ravel_multi_index(arr, (7, 6)), [22, 41, 37])
|
||||
assert_equal(
|
||||
np.ravel_multi_index(arr, (7, 6), order='F'), [31, 41, 13])
|
||||
assert_equal(
|
||||
np.ravel_multi_index(arr, (4, 6), mode='clip'), [22, 23, 19])
|
||||
assert_equal(np.ravel_multi_index(arr, (4, 4), mode=('clip', 'wrap')),
|
||||
[12, 13, 13])
|
||||
assert_equal(np.ravel_multi_index((3, 1, 4, 1), (6, 7, 8, 9)), 1621)
|
||||
|
||||
assert_equal(np.unravel_index(np.array([22, 41, 37]), (7, 6)),
|
||||
[[3, 6, 6], [4, 5, 1]])
|
||||
assert_equal(
|
||||
np.unravel_index(np.array([31, 41, 13]), (7, 6), order='F'),
|
||||
[[3, 6, 6], [4, 5, 1]])
|
||||
assert_equal(np.unravel_index(1621, (6, 7, 8, 9)), [3, 1, 4, 1])
|
||||
|
||||
def test_empty_indices(self):
|
||||
msg1 = 'indices must be integral: the provided empty sequence was'
|
||||
msg2 = 'only int indices permitted'
|
||||
assert_raises_regex(TypeError, msg1, np.unravel_index, [], (10, 3, 5))
|
||||
assert_raises_regex(TypeError, msg1, np.unravel_index, (), (10, 3, 5))
|
||||
assert_raises_regex(TypeError, msg2, np.unravel_index, np.array([]),
|
||||
(10, 3, 5))
|
||||
assert_equal(np.unravel_index(np.array([],dtype=int), (10, 3, 5)),
|
||||
[[], [], []])
|
||||
assert_raises_regex(TypeError, msg1, np.ravel_multi_index, ([], []),
|
||||
(10, 3))
|
||||
assert_raises_regex(TypeError, msg1, np.ravel_multi_index, ([], ['abc']),
|
||||
(10, 3))
|
||||
assert_raises_regex(TypeError, msg2, np.ravel_multi_index,
|
||||
(np.array([]), np.array([])), (5, 3))
|
||||
assert_equal(np.ravel_multi_index(
|
||||
(np.array([], dtype=int), np.array([], dtype=int)), (5, 3)), [])
|
||||
assert_equal(np.ravel_multi_index(np.array([[], []], dtype=int),
|
||||
(5, 3)), [])
|
||||
|
||||
def test_big_indices(self):
|
||||
# ravel_multi_index for big indices (issue #7546)
|
||||
if np.intp == np.int64:
|
||||
arr = ([1, 29], [3, 5], [3, 117], [19, 2],
|
||||
[2379, 1284], [2, 2], [0, 1])
|
||||
assert_equal(
|
||||
np.ravel_multi_index(arr, (41, 7, 120, 36, 2706, 8, 6)),
|
||||
[5627771580, 117259570957])
|
||||
|
||||
# test unravel_index for big indices (issue #9538)
|
||||
assert_raises(ValueError, np.unravel_index, 1, (2**32-1, 2**31+1))
|
||||
|
||||
# test overflow checking for too big array (issue #7546)
|
||||
dummy_arr = ([0],[0])
|
||||
half_max = np.iinfo(np.intp).max // 2
|
||||
assert_equal(
|
||||
np.ravel_multi_index(dummy_arr, (half_max, 2)), [0])
|
||||
assert_raises(ValueError,
|
||||
np.ravel_multi_index, dummy_arr, (half_max+1, 2))
|
||||
assert_equal(
|
||||
np.ravel_multi_index(dummy_arr, (half_max, 2), order='F'), [0])
|
||||
assert_raises(ValueError,
|
||||
np.ravel_multi_index, dummy_arr, (half_max+1, 2), order='F')
|
||||
|
||||
def test_dtypes(self):
|
||||
# Test with different data types
|
||||
for dtype in [np.int16, np.uint16, np.int32,
|
||||
np.uint32, np.int64, np.uint64]:
|
||||
coords = np.array(
|
||||
[[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0]], dtype=dtype)
|
||||
shape = (5, 8)
|
||||
uncoords = 8*coords[0]+coords[1]
|
||||
assert_equal(np.ravel_multi_index(coords, shape), uncoords)
|
||||
assert_equal(coords, np.unravel_index(uncoords, shape))
|
||||
uncoords = coords[0]+5*coords[1]
|
||||
assert_equal(
|
||||
np.ravel_multi_index(coords, shape, order='F'), uncoords)
|
||||
assert_equal(coords, np.unravel_index(uncoords, shape, order='F'))
|
||||
|
||||
coords = np.array(
|
||||
[[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0], [1, 3, 1, 0, 9, 5]],
|
||||
dtype=dtype)
|
||||
shape = (5, 8, 10)
|
||||
uncoords = 10*(8*coords[0]+coords[1])+coords[2]
|
||||
assert_equal(np.ravel_multi_index(coords, shape), uncoords)
|
||||
assert_equal(coords, np.unravel_index(uncoords, shape))
|
||||
uncoords = coords[0]+5*(coords[1]+8*coords[2])
|
||||
assert_equal(
|
||||
np.ravel_multi_index(coords, shape, order='F'), uncoords)
|
||||
assert_equal(coords, np.unravel_index(uncoords, shape, order='F'))
|
||||
|
||||
def test_clipmodes(self):
|
||||
# Test clipmodes
|
||||
assert_equal(
|
||||
np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12), mode='wrap'),
|
||||
np.ravel_multi_index([1, 1, 6, 2], (4, 3, 7, 12)))
|
||||
assert_equal(np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12),
|
||||
mode=(
|
||||
'wrap', 'raise', 'clip', 'raise')),
|
||||
np.ravel_multi_index([1, 1, 0, 2], (4, 3, 7, 12)))
|
||||
assert_raises(
|
||||
ValueError, np.ravel_multi_index, [5, 1, -1, 2], (4, 3, 7, 12))
|
||||
|
||||
def test_writeability(self):
|
||||
# See gh-7269
|
||||
x, y = np.unravel_index([1, 2, 3], (4, 5))
|
||||
assert_(x.flags.writeable)
|
||||
assert_(y.flags.writeable)
|
||||
|
||||
def test_0d(self):
|
||||
# gh-580
|
||||
x = np.unravel_index(0, ())
|
||||
assert_equal(x, ())
|
||||
|
||||
assert_raises_regex(ValueError, "0d array", np.unravel_index, [0], ())
|
||||
assert_raises_regex(
|
||||
ValueError, "out of bounds", np.unravel_index, [1], ())
|
||||
|
||||
@pytest.mark.parametrize("mode", ["clip", "wrap", "raise"])
|
||||
def test_empty_array_ravel(self, mode):
|
||||
res = np.ravel_multi_index(
|
||||
np.zeros((3, 0), dtype=np.intp), (2, 1, 0), mode=mode)
|
||||
assert(res.shape == (0,))
|
||||
|
||||
with assert_raises(ValueError):
|
||||
np.ravel_multi_index(
|
||||
np.zeros((3, 1), dtype=np.intp), (2, 1, 0), mode=mode)
|
||||
|
||||
def test_empty_array_unravel(self):
|
||||
res = np.unravel_index(np.zeros(0, dtype=np.intp), (2, 1, 0))
|
||||
# res is a tuple of three empty arrays
|
||||
assert(len(res) == 3)
|
||||
assert(all(a.shape == (0,) for a in res))
|
||||
|
||||
with assert_raises(ValueError):
|
||||
np.unravel_index([1], (2, 1, 0))
|
||||
|
||||
class TestGrid:
|
||||
def test_basic(self):
|
||||
a = mgrid[-1:1:10j]
|
||||
b = mgrid[-1:1:0.1]
|
||||
assert_(a.shape == (10,))
|
||||
assert_(b.shape == (20,))
|
||||
assert_(a[0] == -1)
|
||||
assert_almost_equal(a[-1], 1)
|
||||
assert_(b[0] == -1)
|
||||
assert_almost_equal(b[1]-b[0], 0.1, 11)
|
||||
assert_almost_equal(b[-1], b[0]+19*0.1, 11)
|
||||
assert_almost_equal(a[1]-a[0], 2.0/9.0, 11)
|
||||
|
||||
def test_linspace_equivalence(self):
|
||||
y, st = np.linspace(2, 10, retstep=True)
|
||||
assert_almost_equal(st, 8/49.0)
|
||||
assert_array_almost_equal(y, mgrid[2:10:50j], 13)
|
||||
|
||||
def test_nd(self):
|
||||
c = mgrid[-1:1:10j, -2:2:10j]
|
||||
d = mgrid[-1:1:0.1, -2:2:0.2]
|
||||
assert_(c.shape == (2, 10, 10))
|
||||
assert_(d.shape == (2, 20, 20))
|
||||
assert_array_equal(c[0][0, :], -np.ones(10, 'd'))
|
||||
assert_array_equal(c[1][:, 0], -2*np.ones(10, 'd'))
|
||||
assert_array_almost_equal(c[0][-1, :], np.ones(10, 'd'), 11)
|
||||
assert_array_almost_equal(c[1][:, -1], 2*np.ones(10, 'd'), 11)
|
||||
assert_array_almost_equal(d[0, 1, :] - d[0, 0, :],
|
||||
0.1*np.ones(20, 'd'), 11)
|
||||
assert_array_almost_equal(d[1, :, 1] - d[1, :, 0],
|
||||
0.2*np.ones(20, 'd'), 11)
|
||||
|
||||
def test_sparse(self):
|
||||
grid_full = mgrid[-1:1:10j, -2:2:10j]
|
||||
grid_sparse = ogrid[-1:1:10j, -2:2:10j]
|
||||
|
||||
# sparse grids can be made dense by broadcasting
|
||||
grid_broadcast = np.broadcast_arrays(*grid_sparse)
|
||||
for f, b in zip(grid_full, grid_broadcast):
|
||||
assert_equal(f, b)
|
||||
|
||||
@pytest.mark.parametrize("start, stop, step, expected", [
|
||||
(None, 10, 10j, (200, 10)),
|
||||
(-10, 20, None, (1800, 30)),
|
||||
])
|
||||
def test_mgrid_size_none_handling(self, start, stop, step, expected):
|
||||
# regression test None value handling for
|
||||
# start and step values used by mgrid;
|
||||
# internally, this aims to cover previously
|
||||
# unexplored code paths in nd_grid()
|
||||
grid = mgrid[start:stop:step, start:stop:step]
|
||||
# need a smaller grid to explore one of the
|
||||
# untested code paths
|
||||
grid_small = mgrid[start:stop:step]
|
||||
assert_equal(grid.size, expected[0])
|
||||
assert_equal(grid_small.size, expected[1])
|
||||
|
||||
def test_accepts_npfloating(self):
|
||||
# regression test for #16466
|
||||
grid64 = mgrid[0.1:0.33:0.1, ]
|
||||
grid32 = mgrid[np.float32(0.1):np.float32(0.33):np.float32(0.1), ]
|
||||
assert_array_almost_equal(grid64, grid32)
|
||||
# At some point this was float64, but NEP 50 changed it:
|
||||
assert grid32.dtype == np.float32
|
||||
assert grid64.dtype == np.float64
|
||||
|
||||
# different code path for single slice
|
||||
grid64 = mgrid[0.1:0.33:0.1]
|
||||
grid32 = mgrid[np.float32(0.1):np.float32(0.33):np.float32(0.1)]
|
||||
assert_(grid32.dtype == np.float64)
|
||||
assert_array_almost_equal(grid64, grid32)
|
||||
|
||||
def test_accepts_longdouble(self):
|
||||
# regression tests for #16945
|
||||
grid64 = mgrid[0.1:0.33:0.1, ]
|
||||
grid128 = mgrid[
|
||||
np.longdouble(0.1):np.longdouble(0.33):np.longdouble(0.1),
|
||||
]
|
||||
assert_(grid128.dtype == np.longdouble)
|
||||
assert_array_almost_equal(grid64, grid128)
|
||||
|
||||
grid128c_a = mgrid[0:np.longdouble(1):3.4j]
|
||||
grid128c_b = mgrid[0:np.longdouble(1):3.4j, ]
|
||||
assert_(grid128c_a.dtype == grid128c_b.dtype == np.longdouble)
|
||||
assert_array_equal(grid128c_a, grid128c_b[0])
|
||||
|
||||
# different code path for single slice
|
||||
grid64 = mgrid[0.1:0.33:0.1]
|
||||
grid128 = mgrid[
|
||||
np.longdouble(0.1):np.longdouble(0.33):np.longdouble(0.1)
|
||||
]
|
||||
assert_(grid128.dtype == np.longdouble)
|
||||
assert_array_almost_equal(grid64, grid128)
|
||||
|
||||
def test_accepts_npcomplexfloating(self):
|
||||
# Related to #16466
|
||||
assert_array_almost_equal(
|
||||
mgrid[0.1:0.3:3j, ], mgrid[0.1:0.3:np.complex64(3j), ]
|
||||
)
|
||||
|
||||
# different code path for single slice
|
||||
assert_array_almost_equal(
|
||||
mgrid[0.1:0.3:3j], mgrid[0.1:0.3:np.complex64(3j)]
|
||||
)
|
||||
|
||||
# Related to #16945
|
||||
grid64_a = mgrid[0.1:0.3:3.3j]
|
||||
grid64_b = mgrid[0.1:0.3:3.3j, ][0]
|
||||
assert_(grid64_a.dtype == grid64_b.dtype == np.float64)
|
||||
assert_array_equal(grid64_a, grid64_b)
|
||||
|
||||
grid128_a = mgrid[0.1:0.3:np.clongdouble(3.3j)]
|
||||
grid128_b = mgrid[0.1:0.3:np.clongdouble(3.3j), ][0]
|
||||
assert_(grid128_a.dtype == grid128_b.dtype == np.longdouble)
|
||||
assert_array_equal(grid64_a, grid64_b)
|
||||
|
||||
|
||||
class TestConcatenator:
|
||||
def test_1d(self):
|
||||
assert_array_equal(r_[1, 2, 3, 4, 5, 6], np.array([1, 2, 3, 4, 5, 6]))
|
||||
b = np.ones(5)
|
||||
c = r_[b, 0, 0, b]
|
||||
assert_array_equal(c, [1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1])
|
||||
|
||||
def test_mixed_type(self):
|
||||
g = r_[10.1, 1:10]
|
||||
assert_(g.dtype == 'f8')
|
||||
|
||||
def test_more_mixed_type(self):
|
||||
g = r_[-10.1, np.array([1]), np.array([2, 3, 4]), 10.0]
|
||||
assert_(g.dtype == 'f8')
|
||||
|
||||
def test_complex_step(self):
|
||||
# Regression test for #12262
|
||||
g = r_[0:36:100j]
|
||||
assert_(g.shape == (100,))
|
||||
|
||||
# Related to #16466
|
||||
g = r_[0:36:np.complex64(100j)]
|
||||
assert_(g.shape == (100,))
|
||||
|
||||
def test_2d(self):
|
||||
b = np.random.rand(5, 5)
|
||||
c = np.random.rand(5, 5)
|
||||
d = r_['1', b, c] # append columns
|
||||
assert_(d.shape == (5, 10))
|
||||
assert_array_equal(d[:, :5], b)
|
||||
assert_array_equal(d[:, 5:], c)
|
||||
d = r_[b, c]
|
||||
assert_(d.shape == (10, 5))
|
||||
assert_array_equal(d[:5, :], b)
|
||||
assert_array_equal(d[5:, :], c)
|
||||
|
||||
def test_0d(self):
|
||||
assert_equal(r_[0, np.array(1), 2], [0, 1, 2])
|
||||
assert_equal(r_[[0, 1, 2], np.array(3)], [0, 1, 2, 3])
|
||||
assert_equal(r_[np.array(0), [1, 2, 3]], [0, 1, 2, 3])
|
||||
|
||||
|
||||
class TestNdenumerate:
|
||||
def test_basic(self):
|
||||
a = np.array([[1, 2], [3, 4]])
|
||||
assert_equal(list(ndenumerate(a)),
|
||||
[((0, 0), 1), ((0, 1), 2), ((1, 0), 3), ((1, 1), 4)])
|
||||
|
||||
|
||||
class TestIndexExpression:
|
||||
def test_regression_1(self):
|
||||
# ticket #1196
|
||||
a = np.arange(2)
|
||||
assert_equal(a[:-1], a[s_[:-1]])
|
||||
assert_equal(a[:-1], a[index_exp[:-1]])
|
||||
|
||||
def test_simple_1(self):
|
||||
a = np.random.rand(4, 5, 6)
|
||||
|
||||
assert_equal(a[:, :3, [1, 2]], a[index_exp[:, :3, [1, 2]]])
|
||||
assert_equal(a[:, :3, [1, 2]], a[s_[:, :3, [1, 2]]])
|
||||
|
||||
|
||||
class TestIx_:
|
||||
def test_regression_1(self):
|
||||
# Test empty untyped inputs create outputs of indexing type, gh-5804
|
||||
a, = np.ix_(range(0))
|
||||
assert_equal(a.dtype, np.intp)
|
||||
|
||||
a, = np.ix_([])
|
||||
assert_equal(a.dtype, np.intp)
|
||||
|
||||
# but if the type is specified, don't change it
|
||||
a, = np.ix_(np.array([], dtype=np.float32))
|
||||
assert_equal(a.dtype, np.float32)
|
||||
|
||||
def test_shape_and_dtype(self):
|
||||
sizes = (4, 5, 3, 2)
|
||||
# Test both lists and arrays
|
||||
for func in (range, np.arange):
|
||||
arrays = np.ix_(*[func(sz) for sz in sizes])
|
||||
for k, (a, sz) in enumerate(zip(arrays, sizes)):
|
||||
assert_equal(a.shape[k], sz)
|
||||
assert_(all(sh == 1 for j, sh in enumerate(a.shape) if j != k))
|
||||
assert_(np.issubdtype(a.dtype, np.integer))
|
||||
|
||||
def test_bool(self):
|
||||
bool_a = [True, False, True, True]
|
||||
int_a, = np.nonzero(bool_a)
|
||||
assert_equal(np.ix_(bool_a)[0], int_a)
|
||||
|
||||
def test_1d_only(self):
|
||||
idx2d = [[1, 2, 3], [4, 5, 6]]
|
||||
assert_raises(ValueError, np.ix_, idx2d)
|
||||
|
||||
def test_repeated_input(self):
|
||||
length_of_vector = 5
|
||||
x = np.arange(length_of_vector)
|
||||
out = ix_(x, x)
|
||||
assert_equal(out[0].shape, (length_of_vector, 1))
|
||||
assert_equal(out[1].shape, (1, length_of_vector))
|
||||
# check that input shape is not modified
|
||||
assert_equal(x.shape, (length_of_vector,))
|
||||
|
||||
|
||||
def test_c_():
|
||||
a = c_[np.array([[1, 2, 3]]), 0, 0, np.array([[4, 5, 6]])]
|
||||
assert_equal(a, [[1, 2, 3, 0, 0, 4, 5, 6]])
|
||||
|
||||
|
||||
class TestFillDiagonal:
|
||||
def test_basic(self):
|
||||
a = np.zeros((3, 3), int)
|
||||
fill_diagonal(a, 5)
|
||||
assert_array_equal(
|
||||
a, np.array([[5, 0, 0],
|
||||
[0, 5, 0],
|
||||
[0, 0, 5]])
|
||||
)
|
||||
|
||||
def test_tall_matrix(self):
|
||||
a = np.zeros((10, 3), int)
|
||||
fill_diagonal(a, 5)
|
||||
assert_array_equal(
|
||||
a, np.array([[5, 0, 0],
|
||||
[0, 5, 0],
|
||||
[0, 0, 5],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0]])
|
||||
)
|
||||
|
||||
def test_tall_matrix_wrap(self):
|
||||
a = np.zeros((10, 3), int)
|
||||
fill_diagonal(a, 5, True)
|
||||
assert_array_equal(
|
||||
a, np.array([[5, 0, 0],
|
||||
[0, 5, 0],
|
||||
[0, 0, 5],
|
||||
[0, 0, 0],
|
||||
[5, 0, 0],
|
||||
[0, 5, 0],
|
||||
[0, 0, 5],
|
||||
[0, 0, 0],
|
||||
[5, 0, 0],
|
||||
[0, 5, 0]])
|
||||
)
|
||||
|
||||
def test_wide_matrix(self):
|
||||
a = np.zeros((3, 10), int)
|
||||
fill_diagonal(a, 5)
|
||||
assert_array_equal(
|
||||
a, np.array([[5, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 5, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 5, 0, 0, 0, 0, 0, 0, 0]])
|
||||
)
|
||||
|
||||
def test_operate_4d_array(self):
|
||||
a = np.zeros((3, 3, 3, 3), int)
|
||||
fill_diagonal(a, 4)
|
||||
i = np.array([0, 1, 2])
|
||||
assert_equal(np.where(a != 0), (i, i, i, i))
|
||||
|
||||
def test_low_dim_handling(self):
|
||||
# raise error with low dimensionality
|
||||
a = np.zeros(3, int)
|
||||
with assert_raises_regex(ValueError, "at least 2-d"):
|
||||
fill_diagonal(a, 5)
|
||||
|
||||
def test_hetero_shape_handling(self):
|
||||
# raise error with high dimensionality and
|
||||
# shape mismatch
|
||||
a = np.zeros((3,3,7,3), int)
|
||||
with assert_raises_regex(ValueError, "equal length"):
|
||||
fill_diagonal(a, 2)
|
||||
|
||||
|
||||
def test_diag_indices():
|
||||
di = diag_indices(4)
|
||||
a = np.array([[1, 2, 3, 4],
|
||||
[5, 6, 7, 8],
|
||||
[9, 10, 11, 12],
|
||||
[13, 14, 15, 16]])
|
||||
a[di] = 100
|
||||
assert_array_equal(
|
||||
a, np.array([[100, 2, 3, 4],
|
||||
[5, 100, 7, 8],
|
||||
[9, 10, 100, 12],
|
||||
[13, 14, 15, 100]])
|
||||
)
|
||||
|
||||
# Now, we create indices to manipulate a 3-d array:
|
||||
d3 = diag_indices(2, 3)
|
||||
|
||||
# And use it to set the diagonal of a zeros array to 1:
|
||||
a = np.zeros((2, 2, 2), int)
|
||||
a[d3] = 1
|
||||
assert_array_equal(
|
||||
a, np.array([[[1, 0],
|
||||
[0, 0]],
|
||||
[[0, 0],
|
||||
[0, 1]]])
|
||||
)
|
||||
|
||||
|
||||
class TestDiagIndicesFrom:
|
||||
|
||||
def test_diag_indices_from(self):
|
||||
x = np.random.random((4, 4))
|
||||
r, c = diag_indices_from(x)
|
||||
assert_array_equal(r, np.arange(4))
|
||||
assert_array_equal(c, np.arange(4))
|
||||
|
||||
def test_error_small_input(self):
|
||||
x = np.ones(7)
|
||||
with assert_raises_regex(ValueError, "at least 2-d"):
|
||||
diag_indices_from(x)
|
||||
|
||||
def test_error_shape_mismatch(self):
|
||||
x = np.zeros((3, 3, 2, 3), int)
|
||||
with assert_raises_regex(ValueError, "equal length"):
|
||||
diag_indices_from(x)
|
||||
|
||||
|
||||
def test_ndindex():
|
||||
x = list(ndindex(1, 2, 3))
|
||||
expected = [ix for ix, e in ndenumerate(np.zeros((1, 2, 3)))]
|
||||
assert_array_equal(x, expected)
|
||||
|
||||
x = list(ndindex((1, 2, 3)))
|
||||
assert_array_equal(x, expected)
|
||||
|
||||
# Test use of scalars and tuples
|
||||
x = list(ndindex((3,)))
|
||||
assert_array_equal(x, list(ndindex(3)))
|
||||
|
||||
# Make sure size argument is optional
|
||||
x = list(ndindex())
|
||||
assert_equal(x, [()])
|
||||
|
||||
x = list(ndindex(()))
|
||||
assert_equal(x, [()])
|
||||
|
||||
# Make sure 0-sized ndindex works correctly
|
||||
x = list(ndindex(*[0]))
|
||||
assert_equal(x, [])
|
||||
2801
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_io.py
vendored
Normal file
2801
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_io.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1075
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_loadtxt.py
vendored
Normal file
1075
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_loadtxt.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
216
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_mixins.py
vendored
Normal file
216
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_mixins.py
vendored
Normal file
@@ -0,0 +1,216 @@
|
||||
import numbers
|
||||
import operator
|
||||
|
||||
import numpy as np
|
||||
from numpy.testing import assert_, assert_equal, assert_raises
|
||||
|
||||
|
||||
# NOTE: This class should be kept as an exact copy of the example from the
|
||||
# docstring for NDArrayOperatorsMixin.
|
||||
|
||||
class ArrayLike(np.lib.mixins.NDArrayOperatorsMixin):
|
||||
def __init__(self, value):
|
||||
self.value = np.asarray(value)
|
||||
|
||||
# One might also consider adding the built-in list type to this
|
||||
# list, to support operations like np.add(array_like, list)
|
||||
_HANDLED_TYPES = (np.ndarray, numbers.Number)
|
||||
|
||||
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
|
||||
out = kwargs.get('out', ())
|
||||
for x in inputs + out:
|
||||
# Only support operations with instances of _HANDLED_TYPES.
|
||||
# Use ArrayLike instead of type(self) for isinstance to
|
||||
# allow subclasses that don't override __array_ufunc__ to
|
||||
# handle ArrayLike objects.
|
||||
if not isinstance(x, self._HANDLED_TYPES + (ArrayLike,)):
|
||||
return NotImplemented
|
||||
|
||||
# Defer to the implementation of the ufunc on unwrapped values.
|
||||
inputs = tuple(x.value if isinstance(x, ArrayLike) else x
|
||||
for x in inputs)
|
||||
if out:
|
||||
kwargs['out'] = tuple(
|
||||
x.value if isinstance(x, ArrayLike) else x
|
||||
for x in out)
|
||||
result = getattr(ufunc, method)(*inputs, **kwargs)
|
||||
|
||||
if type(result) is tuple:
|
||||
# multiple return values
|
||||
return tuple(type(self)(x) for x in result)
|
||||
elif method == 'at':
|
||||
# no return value
|
||||
return None
|
||||
else:
|
||||
# one return value
|
||||
return type(self)(result)
|
||||
|
||||
def __repr__(self):
|
||||
return '%s(%r)' % (type(self).__name__, self.value)
|
||||
|
||||
|
||||
def wrap_array_like(result):
|
||||
if type(result) is tuple:
|
||||
return tuple(ArrayLike(r) for r in result)
|
||||
else:
|
||||
return ArrayLike(result)
|
||||
|
||||
|
||||
def _assert_equal_type_and_value(result, expected, err_msg=None):
|
||||
assert_equal(type(result), type(expected), err_msg=err_msg)
|
||||
if isinstance(result, tuple):
|
||||
assert_equal(len(result), len(expected), err_msg=err_msg)
|
||||
for result_item, expected_item in zip(result, expected):
|
||||
_assert_equal_type_and_value(result_item, expected_item, err_msg)
|
||||
else:
|
||||
assert_equal(result.value, expected.value, err_msg=err_msg)
|
||||
assert_equal(getattr(result.value, 'dtype', None),
|
||||
getattr(expected.value, 'dtype', None), err_msg=err_msg)
|
||||
|
||||
|
||||
_ALL_BINARY_OPERATORS = [
|
||||
operator.lt,
|
||||
operator.le,
|
||||
operator.eq,
|
||||
operator.ne,
|
||||
operator.gt,
|
||||
operator.ge,
|
||||
operator.add,
|
||||
operator.sub,
|
||||
operator.mul,
|
||||
operator.truediv,
|
||||
operator.floordiv,
|
||||
operator.mod,
|
||||
divmod,
|
||||
pow,
|
||||
operator.lshift,
|
||||
operator.rshift,
|
||||
operator.and_,
|
||||
operator.xor,
|
||||
operator.or_,
|
||||
]
|
||||
|
||||
|
||||
class TestNDArrayOperatorsMixin:
|
||||
|
||||
def test_array_like_add(self):
|
||||
|
||||
def check(result):
|
||||
_assert_equal_type_and_value(result, ArrayLike(0))
|
||||
|
||||
check(ArrayLike(0) + 0)
|
||||
check(0 + ArrayLike(0))
|
||||
|
||||
check(ArrayLike(0) + np.array(0))
|
||||
check(np.array(0) + ArrayLike(0))
|
||||
|
||||
check(ArrayLike(np.array(0)) + 0)
|
||||
check(0 + ArrayLike(np.array(0)))
|
||||
|
||||
check(ArrayLike(np.array(0)) + np.array(0))
|
||||
check(np.array(0) + ArrayLike(np.array(0)))
|
||||
|
||||
def test_inplace(self):
|
||||
array_like = ArrayLike(np.array([0]))
|
||||
array_like += 1
|
||||
_assert_equal_type_and_value(array_like, ArrayLike(np.array([1])))
|
||||
|
||||
array = np.array([0])
|
||||
array += ArrayLike(1)
|
||||
_assert_equal_type_and_value(array, ArrayLike(np.array([1])))
|
||||
|
||||
def test_opt_out(self):
|
||||
|
||||
class OptOut:
|
||||
"""Object that opts out of __array_ufunc__."""
|
||||
__array_ufunc__ = None
|
||||
|
||||
def __add__(self, other):
|
||||
return self
|
||||
|
||||
def __radd__(self, other):
|
||||
return self
|
||||
|
||||
array_like = ArrayLike(1)
|
||||
opt_out = OptOut()
|
||||
|
||||
# supported operations
|
||||
assert_(array_like + opt_out is opt_out)
|
||||
assert_(opt_out + array_like is opt_out)
|
||||
|
||||
# not supported
|
||||
with assert_raises(TypeError):
|
||||
# don't use the Python default, array_like = array_like + opt_out
|
||||
array_like += opt_out
|
||||
with assert_raises(TypeError):
|
||||
array_like - opt_out
|
||||
with assert_raises(TypeError):
|
||||
opt_out - array_like
|
||||
|
||||
def test_subclass(self):
|
||||
|
||||
class SubArrayLike(ArrayLike):
|
||||
"""Should take precedence over ArrayLike."""
|
||||
|
||||
x = ArrayLike(0)
|
||||
y = SubArrayLike(1)
|
||||
_assert_equal_type_and_value(x + y, y)
|
||||
_assert_equal_type_and_value(y + x, y)
|
||||
|
||||
def test_object(self):
|
||||
x = ArrayLike(0)
|
||||
obj = object()
|
||||
with assert_raises(TypeError):
|
||||
x + obj
|
||||
with assert_raises(TypeError):
|
||||
obj + x
|
||||
with assert_raises(TypeError):
|
||||
x += obj
|
||||
|
||||
def test_unary_methods(self):
|
||||
array = np.array([-1, 0, 1, 2])
|
||||
array_like = ArrayLike(array)
|
||||
for op in [operator.neg,
|
||||
operator.pos,
|
||||
abs,
|
||||
operator.invert]:
|
||||
_assert_equal_type_and_value(op(array_like), ArrayLike(op(array)))
|
||||
|
||||
def test_forward_binary_methods(self):
|
||||
array = np.array([-1, 0, 1, 2])
|
||||
array_like = ArrayLike(array)
|
||||
for op in _ALL_BINARY_OPERATORS:
|
||||
expected = wrap_array_like(op(array, 1))
|
||||
actual = op(array_like, 1)
|
||||
err_msg = 'failed for operator {}'.format(op)
|
||||
_assert_equal_type_and_value(expected, actual, err_msg=err_msg)
|
||||
|
||||
def test_reflected_binary_methods(self):
|
||||
for op in _ALL_BINARY_OPERATORS:
|
||||
expected = wrap_array_like(op(2, 1))
|
||||
actual = op(2, ArrayLike(1))
|
||||
err_msg = 'failed for operator {}'.format(op)
|
||||
_assert_equal_type_and_value(expected, actual, err_msg=err_msg)
|
||||
|
||||
def test_matmul(self):
|
||||
array = np.array([1, 2], dtype=np.float64)
|
||||
array_like = ArrayLike(array)
|
||||
expected = ArrayLike(np.float64(5))
|
||||
_assert_equal_type_and_value(expected, np.matmul(array_like, array))
|
||||
_assert_equal_type_and_value(
|
||||
expected, operator.matmul(array_like, array))
|
||||
_assert_equal_type_and_value(
|
||||
expected, operator.matmul(array, array_like))
|
||||
|
||||
def test_ufunc_at(self):
|
||||
array = ArrayLike(np.array([1, 2, 3, 4]))
|
||||
assert_(np.negative.at(array, np.array([0, 1])) is None)
|
||||
_assert_equal_type_and_value(array, ArrayLike([-1, -2, 3, 4]))
|
||||
|
||||
def test_ufunc_two_outputs(self):
|
||||
mantissa, exponent = np.frexp(2 ** -3)
|
||||
expected = (ArrayLike(mantissa), ArrayLike(exponent))
|
||||
_assert_equal_type_and_value(
|
||||
np.frexp(ArrayLike(2 ** -3)), expected)
|
||||
_assert_equal_type_and_value(
|
||||
np.frexp(ArrayLike(np.array(2 ** -3))), expected)
|
||||
1418
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_nanfunctions.py
vendored
Normal file
1418
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_nanfunctions.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
376
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_packbits.py
vendored
Normal file
376
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_packbits.py
vendored
Normal file
@@ -0,0 +1,376 @@
|
||||
import numpy as np
|
||||
from numpy.testing import assert_array_equal, assert_equal, assert_raises
|
||||
import pytest
|
||||
from itertools import chain
|
||||
|
||||
def test_packbits():
|
||||
# Copied from the docstring.
|
||||
a = [[[1, 0, 1], [0, 1, 0]],
|
||||
[[1, 1, 0], [0, 0, 1]]]
|
||||
for dt in '?bBhHiIlLqQ':
|
||||
arr = np.array(a, dtype=dt)
|
||||
b = np.packbits(arr, axis=-1)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
assert_array_equal(b, np.array([[[160], [64]], [[192], [32]]]))
|
||||
|
||||
assert_raises(TypeError, np.packbits, np.array(a, dtype=float))
|
||||
|
||||
|
||||
def test_packbits_empty():
|
||||
shapes = [
|
||||
(0,), (10, 20, 0), (10, 0, 20), (0, 10, 20), (20, 0, 0), (0, 20, 0),
|
||||
(0, 0, 20), (0, 0, 0),
|
||||
]
|
||||
for dt in '?bBhHiIlLqQ':
|
||||
for shape in shapes:
|
||||
a = np.empty(shape, dtype=dt)
|
||||
b = np.packbits(a)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
assert_equal(b.shape, (0,))
|
||||
|
||||
|
||||
def test_packbits_empty_with_axis():
|
||||
# Original shapes and lists of packed shapes for different axes.
|
||||
shapes = [
|
||||
((0,), [(0,)]),
|
||||
((10, 20, 0), [(2, 20, 0), (10, 3, 0), (10, 20, 0)]),
|
||||
((10, 0, 20), [(2, 0, 20), (10, 0, 20), (10, 0, 3)]),
|
||||
((0, 10, 20), [(0, 10, 20), (0, 2, 20), (0, 10, 3)]),
|
||||
((20, 0, 0), [(3, 0, 0), (20, 0, 0), (20, 0, 0)]),
|
||||
((0, 20, 0), [(0, 20, 0), (0, 3, 0), (0, 20, 0)]),
|
||||
((0, 0, 20), [(0, 0, 20), (0, 0, 20), (0, 0, 3)]),
|
||||
((0, 0, 0), [(0, 0, 0), (0, 0, 0), (0, 0, 0)]),
|
||||
]
|
||||
for dt in '?bBhHiIlLqQ':
|
||||
for in_shape, out_shapes in shapes:
|
||||
for ax, out_shape in enumerate(out_shapes):
|
||||
a = np.empty(in_shape, dtype=dt)
|
||||
b = np.packbits(a, axis=ax)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
assert_equal(b.shape, out_shape)
|
||||
|
||||
@pytest.mark.parametrize('bitorder', ('little', 'big'))
|
||||
def test_packbits_large(bitorder):
|
||||
# test data large enough for 16 byte vectorization
|
||||
a = np.array([1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0,
|
||||
0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1,
|
||||
1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,
|
||||
1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1,
|
||||
1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1,
|
||||
1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1,
|
||||
1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1,
|
||||
0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0,
|
||||
1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1,
|
||||
1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0,
|
||||
0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1,
|
||||
1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0,
|
||||
1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0,
|
||||
1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0])
|
||||
a = a.repeat(3)
|
||||
for dtype in '?bBhHiIlLqQ':
|
||||
arr = np.array(a, dtype=dtype)
|
||||
b = np.packbits(arr, axis=None, bitorder=bitorder)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
r = [252, 127, 192, 3, 254, 7, 252, 0, 7, 31, 240, 0, 28, 1, 255, 252,
|
||||
113, 248, 3, 255, 192, 28, 15, 192, 28, 126, 0, 224, 127, 255,
|
||||
227, 142, 7, 31, 142, 63, 28, 126, 56, 227, 240, 0, 227, 128, 63,
|
||||
224, 14, 56, 252, 112, 56, 255, 241, 248, 3, 240, 56, 224, 112,
|
||||
63, 255, 255, 199, 224, 14, 0, 31, 143, 192, 3, 255, 199, 0, 1,
|
||||
255, 224, 1, 255, 252, 126, 63, 0, 1, 192, 252, 14, 63, 0, 15,
|
||||
199, 252, 113, 255, 3, 128, 56, 252, 14, 7, 0, 113, 255, 255, 142, 56, 227,
|
||||
129, 248, 227, 129, 199, 31, 128]
|
||||
if bitorder == 'big':
|
||||
assert_array_equal(b, r)
|
||||
# equal for size being multiple of 8
|
||||
assert_array_equal(np.unpackbits(b, bitorder=bitorder)[:-4], a)
|
||||
|
||||
# check last byte of different remainders (16 byte vectorization)
|
||||
b = [np.packbits(arr[:-i], axis=None)[-1] for i in range(1, 16)]
|
||||
assert_array_equal(b, [128, 128, 128, 31, 30, 28, 24, 16, 0, 0, 0, 199,
|
||||
198, 196, 192])
|
||||
|
||||
|
||||
arr = arr.reshape(36, 25)
|
||||
b = np.packbits(arr, axis=0)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
assert_array_equal(b, [[190, 186, 178, 178, 150, 215, 87, 83, 83, 195,
|
||||
199, 206, 204, 204, 140, 140, 136, 136, 8, 40, 105,
|
||||
107, 75, 74, 88],
|
||||
[72, 216, 248, 241, 227, 195, 202, 90, 90, 83,
|
||||
83, 119, 127, 109, 73, 64, 208, 244, 189, 45,
|
||||
41, 104, 122, 90, 18],
|
||||
[113, 120, 248, 216, 152, 24, 60, 52, 182, 150,
|
||||
150, 150, 146, 210, 210, 246, 255, 255, 223,
|
||||
151, 21, 17, 17, 131, 163],
|
||||
[214, 210, 210, 64, 68, 5, 5, 1, 72, 88, 92,
|
||||
92, 78, 110, 39, 181, 149, 220, 222, 218, 218,
|
||||
202, 234, 170, 168],
|
||||
[0, 128, 128, 192, 80, 112, 48, 160, 160, 224,
|
||||
240, 208, 144, 128, 160, 224, 240, 208, 144,
|
||||
144, 176, 240, 224, 192, 128]])
|
||||
|
||||
b = np.packbits(arr, axis=1)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
assert_array_equal(b, [[252, 127, 192, 0],
|
||||
[ 7, 252, 15, 128],
|
||||
[240, 0, 28, 0],
|
||||
[255, 128, 0, 128],
|
||||
[192, 31, 255, 128],
|
||||
[142, 63, 0, 0],
|
||||
[255, 240, 7, 0],
|
||||
[ 7, 224, 14, 0],
|
||||
[126, 0, 224, 0],
|
||||
[255, 255, 199, 0],
|
||||
[ 56, 28, 126, 0],
|
||||
[113, 248, 227, 128],
|
||||
[227, 142, 63, 0],
|
||||
[ 0, 28, 112, 0],
|
||||
[ 15, 248, 3, 128],
|
||||
[ 28, 126, 56, 0],
|
||||
[ 56, 255, 241, 128],
|
||||
[240, 7, 224, 0],
|
||||
[227, 129, 192, 128],
|
||||
[255, 255, 254, 0],
|
||||
[126, 0, 224, 0],
|
||||
[ 3, 241, 248, 0],
|
||||
[ 0, 255, 241, 128],
|
||||
[128, 0, 255, 128],
|
||||
[224, 1, 255, 128],
|
||||
[248, 252, 126, 0],
|
||||
[ 0, 7, 3, 128],
|
||||
[224, 113, 248, 0],
|
||||
[ 0, 252, 127, 128],
|
||||
[142, 63, 224, 0],
|
||||
[224, 14, 63, 0],
|
||||
[ 7, 3, 128, 0],
|
||||
[113, 255, 255, 128],
|
||||
[ 28, 113, 199, 0],
|
||||
[ 7, 227, 142, 0],
|
||||
[ 14, 56, 252, 0]])
|
||||
|
||||
arr = arr.T.copy()
|
||||
b = np.packbits(arr, axis=0)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
assert_array_equal(b, [[252, 7, 240, 255, 192, 142, 255, 7, 126, 255,
|
||||
56, 113, 227, 0, 15, 28, 56, 240, 227, 255,
|
||||
126, 3, 0, 128, 224, 248, 0, 224, 0, 142, 224,
|
||||
7, 113, 28, 7, 14],
|
||||
[127, 252, 0, 128, 31, 63, 240, 224, 0, 255,
|
||||
28, 248, 142, 28, 248, 126, 255, 7, 129, 255,
|
||||
0, 241, 255, 0, 1, 252, 7, 113, 252, 63, 14,
|
||||
3, 255, 113, 227, 56],
|
||||
[192, 15, 28, 0, 255, 0, 7, 14, 224, 199, 126,
|
||||
227, 63, 112, 3, 56, 241, 224, 192, 254, 224,
|
||||
248, 241, 255, 255, 126, 3, 248, 127, 224, 63,
|
||||
128, 255, 199, 142, 252],
|
||||
[0, 128, 0, 128, 128, 0, 0, 0, 0, 0, 0, 128, 0,
|
||||
0, 128, 0, 128, 0, 128, 0, 0, 0, 128, 128,
|
||||
128, 0, 128, 0, 128, 0, 0, 0, 128, 0, 0, 0]])
|
||||
|
||||
b = np.packbits(arr, axis=1)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
assert_array_equal(b, [[190, 72, 113, 214, 0],
|
||||
[186, 216, 120, 210, 128],
|
||||
[178, 248, 248, 210, 128],
|
||||
[178, 241, 216, 64, 192],
|
||||
[150, 227, 152, 68, 80],
|
||||
[215, 195, 24, 5, 112],
|
||||
[ 87, 202, 60, 5, 48],
|
||||
[ 83, 90, 52, 1, 160],
|
||||
[ 83, 90, 182, 72, 160],
|
||||
[195, 83, 150, 88, 224],
|
||||
[199, 83, 150, 92, 240],
|
||||
[206, 119, 150, 92, 208],
|
||||
[204, 127, 146, 78, 144],
|
||||
[204, 109, 210, 110, 128],
|
||||
[140, 73, 210, 39, 160],
|
||||
[140, 64, 246, 181, 224],
|
||||
[136, 208, 255, 149, 240],
|
||||
[136, 244, 255, 220, 208],
|
||||
[ 8, 189, 223, 222, 144],
|
||||
[ 40, 45, 151, 218, 144],
|
||||
[105, 41, 21, 218, 176],
|
||||
[107, 104, 17, 202, 240],
|
||||
[ 75, 122, 17, 234, 224],
|
||||
[ 74, 90, 131, 170, 192],
|
||||
[ 88, 18, 163, 168, 128]])
|
||||
|
||||
|
||||
# result is the same if input is multiplied with a nonzero value
|
||||
for dtype in 'bBhHiIlLqQ':
|
||||
arr = np.array(a, dtype=dtype)
|
||||
rnd = np.random.randint(low=np.iinfo(dtype).min,
|
||||
high=np.iinfo(dtype).max, size=arr.size,
|
||||
dtype=dtype)
|
||||
rnd[rnd == 0] = 1
|
||||
arr *= rnd.astype(dtype)
|
||||
b = np.packbits(arr, axis=-1)
|
||||
assert_array_equal(np.unpackbits(b)[:-4], a)
|
||||
|
||||
assert_raises(TypeError, np.packbits, np.array(a, dtype=float))
|
||||
|
||||
|
||||
def test_packbits_very_large():
|
||||
# test some with a larger arrays gh-8637
|
||||
# code is covered earlier but larger array makes crash on bug more likely
|
||||
for s in range(950, 1050):
|
||||
for dt in '?bBhHiIlLqQ':
|
||||
x = np.ones((200, s), dtype=bool)
|
||||
np.packbits(x, axis=1)
|
||||
|
||||
|
||||
def test_unpackbits():
|
||||
# Copied from the docstring.
|
||||
a = np.array([[2], [7], [23]], dtype=np.uint8)
|
||||
b = np.unpackbits(a, axis=1)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
assert_array_equal(b, np.array([[0, 0, 0, 0, 0, 0, 1, 0],
|
||||
[0, 0, 0, 0, 0, 1, 1, 1],
|
||||
[0, 0, 0, 1, 0, 1, 1, 1]]))
|
||||
|
||||
def test_pack_unpack_order():
|
||||
a = np.array([[2], [7], [23]], dtype=np.uint8)
|
||||
b = np.unpackbits(a, axis=1)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
b_little = np.unpackbits(a, axis=1, bitorder='little')
|
||||
b_big = np.unpackbits(a, axis=1, bitorder='big')
|
||||
assert_array_equal(b, b_big)
|
||||
assert_array_equal(a, np.packbits(b_little, axis=1, bitorder='little'))
|
||||
assert_array_equal(b[:,::-1], b_little)
|
||||
assert_array_equal(a, np.packbits(b_big, axis=1, bitorder='big'))
|
||||
assert_raises(ValueError, np.unpackbits, a, bitorder='r')
|
||||
assert_raises(TypeError, np.unpackbits, a, bitorder=10)
|
||||
|
||||
|
||||
|
||||
def test_unpackbits_empty():
|
||||
a = np.empty((0,), dtype=np.uint8)
|
||||
b = np.unpackbits(a)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
assert_array_equal(b, np.empty((0,)))
|
||||
|
||||
|
||||
def test_unpackbits_empty_with_axis():
|
||||
# Lists of packed shapes for different axes and unpacked shapes.
|
||||
shapes = [
|
||||
([(0,)], (0,)),
|
||||
([(2, 24, 0), (16, 3, 0), (16, 24, 0)], (16, 24, 0)),
|
||||
([(2, 0, 24), (16, 0, 24), (16, 0, 3)], (16, 0, 24)),
|
||||
([(0, 16, 24), (0, 2, 24), (0, 16, 3)], (0, 16, 24)),
|
||||
([(3, 0, 0), (24, 0, 0), (24, 0, 0)], (24, 0, 0)),
|
||||
([(0, 24, 0), (0, 3, 0), (0, 24, 0)], (0, 24, 0)),
|
||||
([(0, 0, 24), (0, 0, 24), (0, 0, 3)], (0, 0, 24)),
|
||||
([(0, 0, 0), (0, 0, 0), (0, 0, 0)], (0, 0, 0)),
|
||||
]
|
||||
for in_shapes, out_shape in shapes:
|
||||
for ax, in_shape in enumerate(in_shapes):
|
||||
a = np.empty(in_shape, dtype=np.uint8)
|
||||
b = np.unpackbits(a, axis=ax)
|
||||
assert_equal(b.dtype, np.uint8)
|
||||
assert_equal(b.shape, out_shape)
|
||||
|
||||
|
||||
def test_unpackbits_large():
|
||||
# test all possible numbers via comparison to already tested packbits
|
||||
d = np.arange(277, dtype=np.uint8)
|
||||
assert_array_equal(np.packbits(np.unpackbits(d)), d)
|
||||
assert_array_equal(np.packbits(np.unpackbits(d[::2])), d[::2])
|
||||
d = np.tile(d, (3, 1))
|
||||
assert_array_equal(np.packbits(np.unpackbits(d, axis=1), axis=1), d)
|
||||
d = d.T.copy()
|
||||
assert_array_equal(np.packbits(np.unpackbits(d, axis=0), axis=0), d)
|
||||
|
||||
|
||||
class TestCount:
|
||||
x = np.array([
|
||||
[1, 0, 1, 0, 0, 1, 0],
|
||||
[0, 1, 1, 1, 0, 0, 0],
|
||||
[0, 0, 1, 0, 0, 1, 1],
|
||||
[1, 1, 0, 0, 0, 1, 1],
|
||||
[1, 0, 1, 0, 1, 0, 1],
|
||||
[0, 0, 1, 1, 1, 0, 0],
|
||||
[0, 1, 0, 1, 0, 1, 0],
|
||||
], dtype=np.uint8)
|
||||
padded1 = np.zeros(57, dtype=np.uint8)
|
||||
padded1[:49] = x.ravel()
|
||||
padded1b = np.zeros(57, dtype=np.uint8)
|
||||
padded1b[:49] = x[::-1].copy().ravel()
|
||||
padded2 = np.zeros((9, 9), dtype=np.uint8)
|
||||
padded2[:7, :7] = x
|
||||
|
||||
@pytest.mark.parametrize('bitorder', ('little', 'big'))
|
||||
@pytest.mark.parametrize('count', chain(range(58), range(-1, -57, -1)))
|
||||
def test_roundtrip(self, bitorder, count):
|
||||
if count < 0:
|
||||
# one extra zero of padding
|
||||
cutoff = count - 1
|
||||
else:
|
||||
cutoff = count
|
||||
# test complete invertibility of packbits and unpackbits with count
|
||||
packed = np.packbits(self.x, bitorder=bitorder)
|
||||
unpacked = np.unpackbits(packed, count=count, bitorder=bitorder)
|
||||
assert_equal(unpacked.dtype, np.uint8)
|
||||
assert_array_equal(unpacked, self.padded1[:cutoff])
|
||||
|
||||
@pytest.mark.parametrize('kwargs', [
|
||||
{}, {'count': None},
|
||||
])
|
||||
def test_count(self, kwargs):
|
||||
packed = np.packbits(self.x)
|
||||
unpacked = np.unpackbits(packed, **kwargs)
|
||||
assert_equal(unpacked.dtype, np.uint8)
|
||||
assert_array_equal(unpacked, self.padded1[:-1])
|
||||
|
||||
@pytest.mark.parametrize('bitorder', ('little', 'big'))
|
||||
# delta==-1 when count<0 because one extra zero of padding
|
||||
@pytest.mark.parametrize('count', chain(range(8), range(-1, -9, -1)))
|
||||
def test_roundtrip_axis(self, bitorder, count):
|
||||
if count < 0:
|
||||
# one extra zero of padding
|
||||
cutoff = count - 1
|
||||
else:
|
||||
cutoff = count
|
||||
packed0 = np.packbits(self.x, axis=0, bitorder=bitorder)
|
||||
unpacked0 = np.unpackbits(packed0, axis=0, count=count,
|
||||
bitorder=bitorder)
|
||||
assert_equal(unpacked0.dtype, np.uint8)
|
||||
assert_array_equal(unpacked0, self.padded2[:cutoff, :self.x.shape[1]])
|
||||
|
||||
packed1 = np.packbits(self.x, axis=1, bitorder=bitorder)
|
||||
unpacked1 = np.unpackbits(packed1, axis=1, count=count,
|
||||
bitorder=bitorder)
|
||||
assert_equal(unpacked1.dtype, np.uint8)
|
||||
assert_array_equal(unpacked1, self.padded2[:self.x.shape[0], :cutoff])
|
||||
|
||||
@pytest.mark.parametrize('kwargs', [
|
||||
{}, {'count': None},
|
||||
{'bitorder' : 'little'},
|
||||
{'bitorder': 'little', 'count': None},
|
||||
{'bitorder' : 'big'},
|
||||
{'bitorder': 'big', 'count': None},
|
||||
])
|
||||
def test_axis_count(self, kwargs):
|
||||
packed0 = np.packbits(self.x, axis=0)
|
||||
unpacked0 = np.unpackbits(packed0, axis=0, **kwargs)
|
||||
assert_equal(unpacked0.dtype, np.uint8)
|
||||
if kwargs.get('bitorder', 'big') == 'big':
|
||||
assert_array_equal(unpacked0, self.padded2[:-1, :self.x.shape[1]])
|
||||
else:
|
||||
assert_array_equal(unpacked0[::-1, :], self.padded2[:-1, :self.x.shape[1]])
|
||||
|
||||
packed1 = np.packbits(self.x, axis=1)
|
||||
unpacked1 = np.unpackbits(packed1, axis=1, **kwargs)
|
||||
assert_equal(unpacked1.dtype, np.uint8)
|
||||
if kwargs.get('bitorder', 'big') == 'big':
|
||||
assert_array_equal(unpacked1, self.padded2[:self.x.shape[0], :-1])
|
||||
else:
|
||||
assert_array_equal(unpacked1[:, ::-1], self.padded2[:self.x.shape[0], :-1])
|
||||
|
||||
def test_bad_count(self):
|
||||
packed0 = np.packbits(self.x, axis=0)
|
||||
assert_raises(ValueError, np.unpackbits, packed0, axis=0, count=-9)
|
||||
packed1 = np.packbits(self.x, axis=1)
|
||||
assert_raises(ValueError, np.unpackbits, packed1, axis=1, count=-9)
|
||||
packed = np.packbits(self.x)
|
||||
assert_raises(ValueError, np.unpackbits, packed, count=-57)
|
||||
303
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_polynomial.py
vendored
Normal file
303
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_polynomial.py
vendored
Normal file
@@ -0,0 +1,303 @@
|
||||
import numpy as np
|
||||
from numpy.testing import (
|
||||
assert_, assert_equal, assert_array_equal, assert_almost_equal,
|
||||
assert_array_almost_equal, assert_raises, assert_allclose
|
||||
)
|
||||
|
||||
import pytest
|
||||
|
||||
# `poly1d` has some support for `np.bool` and `np.timedelta64`,
|
||||
# but it is limited and they are therefore excluded here
|
||||
TYPE_CODES = np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "O"
|
||||
|
||||
|
||||
class TestPolynomial:
|
||||
def test_poly1d_str_and_repr(self):
|
||||
p = np.poly1d([1., 2, 3])
|
||||
assert_equal(repr(p), 'poly1d([1., 2., 3.])')
|
||||
assert_equal(str(p),
|
||||
' 2\n'
|
||||
'1 x + 2 x + 3')
|
||||
|
||||
q = np.poly1d([3., 2, 1])
|
||||
assert_equal(repr(q), 'poly1d([3., 2., 1.])')
|
||||
assert_equal(str(q),
|
||||
' 2\n'
|
||||
'3 x + 2 x + 1')
|
||||
|
||||
r = np.poly1d([1.89999 + 2j, -3j, -5.12345678, 2 + 1j])
|
||||
assert_equal(str(r),
|
||||
' 3 2\n'
|
||||
'(1.9 + 2j) x - 3j x - 5.123 x + (2 + 1j)')
|
||||
|
||||
assert_equal(str(np.poly1d([-3, -2, -1])),
|
||||
' 2\n'
|
||||
'-3 x - 2 x - 1')
|
||||
|
||||
def test_poly1d_resolution(self):
|
||||
p = np.poly1d([1., 2, 3])
|
||||
q = np.poly1d([3., 2, 1])
|
||||
assert_equal(p(0), 3.0)
|
||||
assert_equal(p(5), 38.0)
|
||||
assert_equal(q(0), 1.0)
|
||||
assert_equal(q(5), 86.0)
|
||||
|
||||
def test_poly1d_math(self):
|
||||
# here we use some simple coeffs to make calculations easier
|
||||
p = np.poly1d([1., 2, 4])
|
||||
q = np.poly1d([4., 2, 1])
|
||||
assert_equal(p/q, (np.poly1d([0.25]), np.poly1d([1.5, 3.75])))
|
||||
assert_equal(p.integ(), np.poly1d([1/3, 1., 4., 0.]))
|
||||
assert_equal(p.integ(1), np.poly1d([1/3, 1., 4., 0.]))
|
||||
|
||||
p = np.poly1d([1., 2, 3])
|
||||
q = np.poly1d([3., 2, 1])
|
||||
assert_equal(p * q, np.poly1d([3., 8., 14., 8., 3.]))
|
||||
assert_equal(p + q, np.poly1d([4., 4., 4.]))
|
||||
assert_equal(p - q, np.poly1d([-2., 0., 2.]))
|
||||
assert_equal(p ** 4, np.poly1d([1., 8., 36., 104., 214., 312., 324., 216., 81.]))
|
||||
assert_equal(p(q), np.poly1d([9., 12., 16., 8., 6.]))
|
||||
assert_equal(q(p), np.poly1d([3., 12., 32., 40., 34.]))
|
||||
assert_equal(p.deriv(), np.poly1d([2., 2.]))
|
||||
assert_equal(p.deriv(2), np.poly1d([2.]))
|
||||
assert_equal(np.polydiv(np.poly1d([1, 0, -1]), np.poly1d([1, 1])),
|
||||
(np.poly1d([1., -1.]), np.poly1d([0.])))
|
||||
|
||||
@pytest.mark.parametrize("type_code", TYPE_CODES)
|
||||
def test_poly1d_misc(self, type_code: str) -> None:
|
||||
dtype = np.dtype(type_code)
|
||||
ar = np.array([1, 2, 3], dtype=dtype)
|
||||
p = np.poly1d(ar)
|
||||
|
||||
# `__eq__`
|
||||
assert_equal(np.asarray(p), ar)
|
||||
assert_equal(np.asarray(p).dtype, dtype)
|
||||
assert_equal(len(p), 2)
|
||||
|
||||
# `__getitem__`
|
||||
comparison_dct = {-1: 0, 0: 3, 1: 2, 2: 1, 3: 0}
|
||||
for index, ref in comparison_dct.items():
|
||||
scalar = p[index]
|
||||
assert_equal(scalar, ref)
|
||||
if dtype == np.object_:
|
||||
assert isinstance(scalar, int)
|
||||
else:
|
||||
assert_equal(scalar.dtype, dtype)
|
||||
|
||||
def test_poly1d_variable_arg(self):
|
||||
q = np.poly1d([1., 2, 3], variable='y')
|
||||
assert_equal(str(q),
|
||||
' 2\n'
|
||||
'1 y + 2 y + 3')
|
||||
q = np.poly1d([1., 2, 3], variable='lambda')
|
||||
assert_equal(str(q),
|
||||
' 2\n'
|
||||
'1 lambda + 2 lambda + 3')
|
||||
|
||||
def test_poly(self):
|
||||
assert_array_almost_equal(np.poly([3, -np.sqrt(2), np.sqrt(2)]),
|
||||
[1, -3, -2, 6])
|
||||
|
||||
# From matlab docs
|
||||
A = [[1, 2, 3], [4, 5, 6], [7, 8, 0]]
|
||||
assert_array_almost_equal(np.poly(A), [1, -6, -72, -27])
|
||||
|
||||
# Should produce real output for perfect conjugates
|
||||
assert_(np.isrealobj(np.poly([+1.082j, +2.613j, -2.613j, -1.082j])))
|
||||
assert_(np.isrealobj(np.poly([0+1j, -0+-1j, 1+2j,
|
||||
1-2j, 1.+3.5j, 1-3.5j])))
|
||||
assert_(np.isrealobj(np.poly([1j, -1j, 1+2j, 1-2j, 1+3j, 1-3.j])))
|
||||
assert_(np.isrealobj(np.poly([1j, -1j, 1+2j, 1-2j])))
|
||||
assert_(np.isrealobj(np.poly([1j, -1j, 2j, -2j])))
|
||||
assert_(np.isrealobj(np.poly([1j, -1j])))
|
||||
assert_(np.isrealobj(np.poly([1, -1])))
|
||||
|
||||
assert_(np.iscomplexobj(np.poly([1j, -1.0000001j])))
|
||||
|
||||
np.random.seed(42)
|
||||
a = np.random.randn(100) + 1j*np.random.randn(100)
|
||||
assert_(np.isrealobj(np.poly(np.concatenate((a, np.conjugate(a))))))
|
||||
|
||||
def test_roots(self):
|
||||
assert_array_equal(np.roots([1, 0, 0]), [0, 0])
|
||||
|
||||
def test_str_leading_zeros(self):
|
||||
p = np.poly1d([4, 3, 2, 1])
|
||||
p[3] = 0
|
||||
assert_equal(str(p),
|
||||
" 2\n"
|
||||
"3 x + 2 x + 1")
|
||||
|
||||
p = np.poly1d([1, 2])
|
||||
p[0] = 0
|
||||
p[1] = 0
|
||||
assert_equal(str(p), " \n0")
|
||||
|
||||
def test_polyfit(self):
|
||||
c = np.array([3., 2., 1.])
|
||||
x = np.linspace(0, 2, 7)
|
||||
y = np.polyval(c, x)
|
||||
err = [1, -1, 1, -1, 1, -1, 1]
|
||||
weights = np.arange(8, 1, -1)**2/7.0
|
||||
|
||||
# Check exception when too few points for variance estimate. Note that
|
||||
# the estimate requires the number of data points to exceed
|
||||
# degree + 1
|
||||
assert_raises(ValueError, np.polyfit,
|
||||
[1], [1], deg=0, cov=True)
|
||||
|
||||
# check 1D case
|
||||
m, cov = np.polyfit(x, y+err, 2, cov=True)
|
||||
est = [3.8571, 0.2857, 1.619]
|
||||
assert_almost_equal(est, m, decimal=4)
|
||||
val0 = [[ 1.4694, -2.9388, 0.8163],
|
||||
[-2.9388, 6.3673, -2.1224],
|
||||
[ 0.8163, -2.1224, 1.161 ]]
|
||||
assert_almost_equal(val0, cov, decimal=4)
|
||||
|
||||
m2, cov2 = np.polyfit(x, y+err, 2, w=weights, cov=True)
|
||||
assert_almost_equal([4.8927, -1.0177, 1.7768], m2, decimal=4)
|
||||
val = [[ 4.3964, -5.0052, 0.4878],
|
||||
[-5.0052, 6.8067, -0.9089],
|
||||
[ 0.4878, -0.9089, 0.3337]]
|
||||
assert_almost_equal(val, cov2, decimal=4)
|
||||
|
||||
m3, cov3 = np.polyfit(x, y+err, 2, w=weights, cov="unscaled")
|
||||
assert_almost_equal([4.8927, -1.0177, 1.7768], m3, decimal=4)
|
||||
val = [[ 0.1473, -0.1677, 0.0163],
|
||||
[-0.1677, 0.228 , -0.0304],
|
||||
[ 0.0163, -0.0304, 0.0112]]
|
||||
assert_almost_equal(val, cov3, decimal=4)
|
||||
|
||||
# check 2D (n,1) case
|
||||
y = y[:, np.newaxis]
|
||||
c = c[:, np.newaxis]
|
||||
assert_almost_equal(c, np.polyfit(x, y, 2))
|
||||
# check 2D (n,2) case
|
||||
yy = np.concatenate((y, y), axis=1)
|
||||
cc = np.concatenate((c, c), axis=1)
|
||||
assert_almost_equal(cc, np.polyfit(x, yy, 2))
|
||||
|
||||
m, cov = np.polyfit(x, yy + np.array(err)[:, np.newaxis], 2, cov=True)
|
||||
assert_almost_equal(est, m[:, 0], decimal=4)
|
||||
assert_almost_equal(est, m[:, 1], decimal=4)
|
||||
assert_almost_equal(val0, cov[:, :, 0], decimal=4)
|
||||
assert_almost_equal(val0, cov[:, :, 1], decimal=4)
|
||||
|
||||
# check order 1 (deg=0) case, were the analytic results are simple
|
||||
np.random.seed(123)
|
||||
y = np.random.normal(size=(4, 10000))
|
||||
mean, cov = np.polyfit(np.zeros(y.shape[0]), y, deg=0, cov=True)
|
||||
# Should get sigma_mean = sigma/sqrt(N) = 1./sqrt(4) = 0.5.
|
||||
assert_allclose(mean.std(), 0.5, atol=0.01)
|
||||
assert_allclose(np.sqrt(cov.mean()), 0.5, atol=0.01)
|
||||
# Without scaling, since reduced chi2 is 1, the result should be the same.
|
||||
mean, cov = np.polyfit(np.zeros(y.shape[0]), y, w=np.ones(y.shape[0]),
|
||||
deg=0, cov="unscaled")
|
||||
assert_allclose(mean.std(), 0.5, atol=0.01)
|
||||
assert_almost_equal(np.sqrt(cov.mean()), 0.5)
|
||||
# If we estimate our errors wrong, no change with scaling:
|
||||
w = np.full(y.shape[0], 1./0.5)
|
||||
mean, cov = np.polyfit(np.zeros(y.shape[0]), y, w=w, deg=0, cov=True)
|
||||
assert_allclose(mean.std(), 0.5, atol=0.01)
|
||||
assert_allclose(np.sqrt(cov.mean()), 0.5, atol=0.01)
|
||||
# But if we do not scale, our estimate for the error in the mean will
|
||||
# differ.
|
||||
mean, cov = np.polyfit(np.zeros(y.shape[0]), y, w=w, deg=0, cov="unscaled")
|
||||
assert_allclose(mean.std(), 0.5, atol=0.01)
|
||||
assert_almost_equal(np.sqrt(cov.mean()), 0.25)
|
||||
|
||||
def test_objects(self):
|
||||
from decimal import Decimal
|
||||
p = np.poly1d([Decimal('4.0'), Decimal('3.0'), Decimal('2.0')])
|
||||
p2 = p * Decimal('1.333333333333333')
|
||||
assert_(p2[1] == Decimal("3.9999999999999990"))
|
||||
p2 = p.deriv()
|
||||
assert_(p2[1] == Decimal('8.0'))
|
||||
p2 = p.integ()
|
||||
assert_(p2[3] == Decimal("1.333333333333333333333333333"))
|
||||
assert_(p2[2] == Decimal('1.5'))
|
||||
assert_(np.issubdtype(p2.coeffs.dtype, np.object_))
|
||||
p = np.poly([Decimal(1), Decimal(2)])
|
||||
assert_equal(np.poly([Decimal(1), Decimal(2)]),
|
||||
[1, Decimal(-3), Decimal(2)])
|
||||
|
||||
def test_complex(self):
|
||||
p = np.poly1d([3j, 2j, 1j])
|
||||
p2 = p.integ()
|
||||
assert_((p2.coeffs == [1j, 1j, 1j, 0]).all())
|
||||
p2 = p.deriv()
|
||||
assert_((p2.coeffs == [6j, 2j]).all())
|
||||
|
||||
def test_integ_coeffs(self):
|
||||
p = np.poly1d([3, 2, 1])
|
||||
p2 = p.integ(3, k=[9, 7, 6])
|
||||
assert_(
|
||||
(p2.coeffs == [1/4./5., 1/3./4., 1/2./3., 9/1./2., 7, 6]).all())
|
||||
|
||||
def test_zero_dims(self):
|
||||
try:
|
||||
np.poly(np.zeros((0, 0)))
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def test_poly_int_overflow(self):
|
||||
"""
|
||||
Regression test for gh-5096.
|
||||
"""
|
||||
v = np.arange(1, 21)
|
||||
assert_almost_equal(np.poly(v), np.poly(np.diag(v)))
|
||||
|
||||
def test_zero_poly_dtype(self):
|
||||
"""
|
||||
Regression test for gh-16354.
|
||||
"""
|
||||
z = np.array([0, 0, 0])
|
||||
p = np.poly1d(z.astype(np.int64))
|
||||
assert_equal(p.coeffs.dtype, np.int64)
|
||||
|
||||
p = np.poly1d(z.astype(np.float32))
|
||||
assert_equal(p.coeffs.dtype, np.float32)
|
||||
|
||||
p = np.poly1d(z.astype(np.complex64))
|
||||
assert_equal(p.coeffs.dtype, np.complex64)
|
||||
|
||||
def test_poly_eq(self):
|
||||
p = np.poly1d([1, 2, 3])
|
||||
p2 = np.poly1d([1, 2, 4])
|
||||
assert_equal(p == None, False)
|
||||
assert_equal(p != None, True)
|
||||
assert_equal(p == p, True)
|
||||
assert_equal(p == p2, False)
|
||||
assert_equal(p != p2, True)
|
||||
|
||||
def test_polydiv(self):
|
||||
b = np.poly1d([2, 6, 6, 1])
|
||||
a = np.poly1d([-1j, (1+2j), -(2+1j), 1])
|
||||
q, r = np.polydiv(b, a)
|
||||
assert_equal(q.coeffs.dtype, np.complex128)
|
||||
assert_equal(r.coeffs.dtype, np.complex128)
|
||||
assert_equal(q*a + r, b)
|
||||
|
||||
c = [1, 2, 3]
|
||||
d = np.poly1d([1, 2, 3])
|
||||
s, t = np.polydiv(c, d)
|
||||
assert isinstance(s, np.poly1d)
|
||||
assert isinstance(t, np.poly1d)
|
||||
u, v = np.polydiv(d, c)
|
||||
assert isinstance(u, np.poly1d)
|
||||
assert isinstance(v, np.poly1d)
|
||||
|
||||
def test_poly_coeffs_mutable(self):
|
||||
""" Coefficients should be modifiable """
|
||||
p = np.poly1d([1, 2, 3])
|
||||
|
||||
p.coeffs += 1
|
||||
assert_equal(p.coeffs, [2, 3, 4])
|
||||
|
||||
p.coeffs[2] += 10
|
||||
assert_equal(p.coeffs, [2, 3, 14])
|
||||
|
||||
# this never used to be allowed - let's not add features to deprecated
|
||||
# APIs
|
||||
assert_raises(AttributeError, setattr, p, 'coeffs', np.array(1))
|
||||
1043
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_recfunctions.py
vendored
Normal file
1043
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_recfunctions.py
vendored
Normal file
File diff suppressed because it is too large
Load Diff
228
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_regression.py
vendored
Normal file
228
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_regression.py
vendored
Normal file
@@ -0,0 +1,228 @@
|
||||
import os
|
||||
|
||||
import numpy as np
|
||||
from numpy.testing import (
|
||||
assert_, assert_equal, assert_array_equal, assert_array_almost_equal,
|
||||
assert_raises, _assert_valid_refcount,
|
||||
)
|
||||
import pytest
|
||||
|
||||
|
||||
class TestRegression:
|
||||
def test_poly1d(self):
|
||||
# Ticket #28
|
||||
assert_equal(np.poly1d([1]) - np.poly1d([1, 0]),
|
||||
np.poly1d([-1, 1]))
|
||||
|
||||
def test_cov_parameters(self):
|
||||
# Ticket #91
|
||||
x = np.random.random((3, 3))
|
||||
y = x.copy()
|
||||
np.cov(x, rowvar=True)
|
||||
np.cov(y, rowvar=False)
|
||||
assert_array_equal(x, y)
|
||||
|
||||
def test_mem_digitize(self):
|
||||
# Ticket #95
|
||||
for i in range(100):
|
||||
np.digitize([1, 2, 3, 4], [1, 3])
|
||||
np.digitize([0, 1, 2, 3, 4], [1, 3])
|
||||
|
||||
def test_unique_zero_sized(self):
|
||||
# Ticket #205
|
||||
assert_array_equal([], np.unique(np.array([])))
|
||||
|
||||
def test_mem_vectorise(self):
|
||||
# Ticket #325
|
||||
vt = np.vectorize(lambda *args: args)
|
||||
vt(np.zeros((1, 2, 1)), np.zeros((2, 1, 1)), np.zeros((1, 1, 2)))
|
||||
vt(np.zeros((1, 2, 1)), np.zeros((2, 1, 1)), np.zeros((1,
|
||||
1, 2)), np.zeros((2, 2)))
|
||||
|
||||
def test_mgrid_single_element(self):
|
||||
# Ticket #339
|
||||
assert_array_equal(np.mgrid[0:0:1j], [0])
|
||||
assert_array_equal(np.mgrid[0:0], [])
|
||||
|
||||
def test_refcount_vectorize(self):
|
||||
# Ticket #378
|
||||
def p(x, y):
|
||||
return 123
|
||||
v = np.vectorize(p)
|
||||
_assert_valid_refcount(v)
|
||||
|
||||
def test_poly1d_nan_roots(self):
|
||||
# Ticket #396
|
||||
p = np.poly1d([np.nan, np.nan, 1], r=False)
|
||||
assert_raises(np.linalg.LinAlgError, getattr, p, "r")
|
||||
|
||||
def test_mem_polymul(self):
|
||||
# Ticket #448
|
||||
np.polymul([], [1.])
|
||||
|
||||
def test_mem_string_concat(self):
|
||||
# Ticket #469
|
||||
x = np.array([])
|
||||
np.append(x, 'asdasd\tasdasd')
|
||||
|
||||
def test_poly_div(self):
|
||||
# Ticket #553
|
||||
u = np.poly1d([1, 2, 3])
|
||||
v = np.poly1d([1, 2, 3, 4, 5])
|
||||
q, r = np.polydiv(u, v)
|
||||
assert_equal(q*v + r, u)
|
||||
|
||||
def test_poly_eq(self):
|
||||
# Ticket #554
|
||||
x = np.poly1d([1, 2, 3])
|
||||
y = np.poly1d([3, 4])
|
||||
assert_(x != y)
|
||||
assert_(x == x)
|
||||
|
||||
def test_polyfit_build(self):
|
||||
# Ticket #628
|
||||
ref = [-1.06123820e-06, 5.70886914e-04, -1.13822012e-01,
|
||||
9.95368241e+00, -3.14526520e+02]
|
||||
x = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103,
|
||||
104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
|
||||
116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 129,
|
||||
130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
|
||||
146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
|
||||
158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
|
||||
170, 171, 172, 173, 174, 175, 176]
|
||||
y = [9.0, 3.0, 7.0, 4.0, 4.0, 8.0, 6.0, 11.0, 9.0, 8.0, 11.0, 5.0,
|
||||
6.0, 5.0, 9.0, 8.0, 6.0, 10.0, 6.0, 10.0, 7.0, 6.0, 6.0, 6.0,
|
||||
13.0, 4.0, 9.0, 11.0, 4.0, 5.0, 8.0, 5.0, 7.0, 7.0, 6.0, 12.0,
|
||||
7.0, 7.0, 9.0, 4.0, 12.0, 6.0, 6.0, 4.0, 3.0, 9.0, 8.0, 8.0,
|
||||
6.0, 7.0, 9.0, 10.0, 6.0, 8.0, 4.0, 7.0, 7.0, 10.0, 8.0, 8.0,
|
||||
6.0, 3.0, 8.0, 4.0, 5.0, 7.0, 8.0, 6.0, 6.0, 4.0, 12.0, 9.0,
|
||||
8.0, 8.0, 8.0, 6.0, 7.0, 4.0, 4.0, 5.0, 7.0]
|
||||
tested = np.polyfit(x, y, 4)
|
||||
assert_array_almost_equal(ref, tested)
|
||||
|
||||
def test_polydiv_type(self):
|
||||
# Make polydiv work for complex types
|
||||
msg = "Wrong type, should be complex"
|
||||
x = np.ones(3, dtype=complex)
|
||||
q, r = np.polydiv(x, x)
|
||||
assert_(q.dtype == complex, msg)
|
||||
msg = "Wrong type, should be float"
|
||||
x = np.ones(3, dtype=int)
|
||||
q, r = np.polydiv(x, x)
|
||||
assert_(q.dtype == float, msg)
|
||||
|
||||
def test_histogramdd_too_many_bins(self):
|
||||
# Ticket 928.
|
||||
assert_raises(ValueError, np.histogramdd, np.ones((1, 10)), bins=2**10)
|
||||
|
||||
def test_polyint_type(self):
|
||||
# Ticket #944
|
||||
msg = "Wrong type, should be complex"
|
||||
x = np.ones(3, dtype=complex)
|
||||
assert_(np.polyint(x).dtype == complex, msg)
|
||||
msg = "Wrong type, should be float"
|
||||
x = np.ones(3, dtype=int)
|
||||
assert_(np.polyint(x).dtype == float, msg)
|
||||
|
||||
def test_ndenumerate_crash(self):
|
||||
# Ticket 1140
|
||||
# Shouldn't crash:
|
||||
list(np.ndenumerate(np.array([[]])))
|
||||
|
||||
def test_large_fancy_indexing(self):
|
||||
# Large enough to fail on 64-bit.
|
||||
nbits = np.dtype(np.intp).itemsize * 8
|
||||
thesize = int((2**nbits)**(1.0/5.0)+1)
|
||||
|
||||
def dp():
|
||||
n = 3
|
||||
a = np.ones((n,)*5)
|
||||
i = np.random.randint(0, n, size=thesize)
|
||||
a[np.ix_(i, i, i, i, i)] = 0
|
||||
|
||||
def dp2():
|
||||
n = 3
|
||||
a = np.ones((n,)*5)
|
||||
i = np.random.randint(0, n, size=thesize)
|
||||
a[np.ix_(i, i, i, i, i)]
|
||||
|
||||
assert_raises(ValueError, dp)
|
||||
assert_raises(ValueError, dp2)
|
||||
|
||||
def test_void_coercion(self):
|
||||
dt = np.dtype([('a', 'f4'), ('b', 'i4')])
|
||||
x = np.zeros((1,), dt)
|
||||
assert_(np.r_[x, x].dtype == dt)
|
||||
|
||||
def test_include_dirs(self):
|
||||
# As a sanity check, just test that get_include
|
||||
# includes something reasonable. Somewhat
|
||||
# related to ticket #1405.
|
||||
include_dirs = [np.get_include()]
|
||||
for path in include_dirs:
|
||||
assert_(isinstance(path, str))
|
||||
assert_(path != '')
|
||||
|
||||
def test_polyder_return_type(self):
|
||||
# Ticket #1249
|
||||
assert_(isinstance(np.polyder(np.poly1d([1]), 0), np.poly1d))
|
||||
assert_(isinstance(np.polyder([1], 0), np.ndarray))
|
||||
assert_(isinstance(np.polyder(np.poly1d([1]), 1), np.poly1d))
|
||||
assert_(isinstance(np.polyder([1], 1), np.ndarray))
|
||||
|
||||
def test_append_fields_dtype_list(self):
|
||||
# Ticket #1676
|
||||
from numpy.lib.recfunctions import append_fields
|
||||
|
||||
base = np.array([1, 2, 3], dtype=np.int32)
|
||||
names = ['a', 'b', 'c']
|
||||
data = np.eye(3).astype(np.int32)
|
||||
dlist = [np.float64, np.int32, np.int32]
|
||||
try:
|
||||
append_fields(base, names, data, dlist)
|
||||
except Exception:
|
||||
raise AssertionError()
|
||||
|
||||
def test_loadtxt_fields_subarrays(self):
|
||||
# For ticket #1936
|
||||
from io import StringIO
|
||||
|
||||
dt = [("a", 'u1', 2), ("b", 'u1', 2)]
|
||||
x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt)
|
||||
assert_equal(x, np.array([((0, 1), (2, 3))], dtype=dt))
|
||||
|
||||
dt = [("a", [("a", 'u1', (1, 3)), ("b", 'u1')])]
|
||||
x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt)
|
||||
assert_equal(x, np.array([(((0, 1, 2), 3),)], dtype=dt))
|
||||
|
||||
dt = [("a", 'u1', (2, 2))]
|
||||
x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt)
|
||||
assert_equal(x, np.array([(((0, 1), (2, 3)),)], dtype=dt))
|
||||
|
||||
dt = [("a", 'u1', (2, 3, 2))]
|
||||
x = np.loadtxt(StringIO("0 1 2 3 4 5 6 7 8 9 10 11"), dtype=dt)
|
||||
data = [((((0, 1), (2, 3), (4, 5)), ((6, 7), (8, 9), (10, 11))),)]
|
||||
assert_equal(x, np.array(data, dtype=dt))
|
||||
|
||||
def test_nansum_with_boolean(self):
|
||||
# gh-2978
|
||||
a = np.zeros(2, dtype=bool)
|
||||
try:
|
||||
np.nansum(a)
|
||||
except Exception:
|
||||
raise AssertionError()
|
||||
|
||||
def test_py3_compat(self):
|
||||
# gh-2561
|
||||
# Test if the oldstyle class test is bypassed in python3
|
||||
class C:
|
||||
"""Old-style class in python2, normal class in python3"""
|
||||
pass
|
||||
|
||||
out = open(os.devnull, 'w')
|
||||
try:
|
||||
np.info(C(), output=out)
|
||||
except AttributeError:
|
||||
raise AssertionError()
|
||||
finally:
|
||||
out.close()
|
||||
808
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_shape_base.py
vendored
Normal file
808
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_shape_base.py
vendored
Normal file
@@ -0,0 +1,808 @@
|
||||
import numpy as np
|
||||
import functools
|
||||
import sys
|
||||
import pytest
|
||||
|
||||
from numpy import (
|
||||
apply_along_axis, apply_over_axes, array_split, split, hsplit, dsplit,
|
||||
vsplit, dstack, column_stack, kron, tile, expand_dims, take_along_axis,
|
||||
put_along_axis
|
||||
)
|
||||
from numpy.exceptions import AxisError
|
||||
from numpy.testing import (
|
||||
assert_, assert_equal, assert_array_equal, assert_raises, assert_warns
|
||||
)
|
||||
|
||||
|
||||
IS_64BIT = sys.maxsize > 2**32
|
||||
|
||||
|
||||
def _add_keepdims(func):
|
||||
""" hack in keepdims behavior into a function taking an axis """
|
||||
@functools.wraps(func)
|
||||
def wrapped(a, axis, **kwargs):
|
||||
res = func(a, axis=axis, **kwargs)
|
||||
if axis is None:
|
||||
axis = 0 # res is now a scalar, so we can insert this anywhere
|
||||
return np.expand_dims(res, axis=axis)
|
||||
return wrapped
|
||||
|
||||
|
||||
class TestTakeAlongAxis:
|
||||
def test_argequivalent(self):
|
||||
""" Test it translates from arg<func> to <func> """
|
||||
from numpy.random import rand
|
||||
a = rand(3, 4, 5)
|
||||
|
||||
funcs = [
|
||||
(np.sort, np.argsort, dict()),
|
||||
(_add_keepdims(np.min), _add_keepdims(np.argmin), dict()),
|
||||
(_add_keepdims(np.max), _add_keepdims(np.argmax), dict()),
|
||||
#(np.partition, np.argpartition, dict(kth=2)),
|
||||
]
|
||||
|
||||
for func, argfunc, kwargs in funcs:
|
||||
for axis in list(range(a.ndim)) + [None]:
|
||||
a_func = func(a, axis=axis, **kwargs)
|
||||
ai_func = argfunc(a, axis=axis, **kwargs)
|
||||
assert_equal(a_func, take_along_axis(a, ai_func, axis=axis))
|
||||
|
||||
def test_invalid(self):
|
||||
""" Test it errors when indices has too few dimensions """
|
||||
a = np.ones((10, 10))
|
||||
ai = np.ones((10, 2), dtype=np.intp)
|
||||
|
||||
# sanity check
|
||||
take_along_axis(a, ai, axis=1)
|
||||
|
||||
# not enough indices
|
||||
assert_raises(ValueError, take_along_axis, a, np.array(1), axis=1)
|
||||
# bool arrays not allowed
|
||||
assert_raises(IndexError, take_along_axis, a, ai.astype(bool), axis=1)
|
||||
# float arrays not allowed
|
||||
assert_raises(IndexError, take_along_axis, a, ai.astype(float), axis=1)
|
||||
# invalid axis
|
||||
assert_raises(AxisError, take_along_axis, a, ai, axis=10)
|
||||
# invalid indices
|
||||
assert_raises(ValueError, take_along_axis, a, ai, axis=None)
|
||||
|
||||
def test_empty(self):
|
||||
""" Test everything is ok with empty results, even with inserted dims """
|
||||
a = np.ones((3, 4, 5))
|
||||
ai = np.ones((3, 0, 5), dtype=np.intp)
|
||||
|
||||
actual = take_along_axis(a, ai, axis=1)
|
||||
assert_equal(actual.shape, ai.shape)
|
||||
|
||||
def test_broadcast(self):
|
||||
""" Test that non-indexing dimensions are broadcast in both directions """
|
||||
a = np.ones((3, 4, 1))
|
||||
ai = np.ones((1, 2, 5), dtype=np.intp)
|
||||
actual = take_along_axis(a, ai, axis=1)
|
||||
assert_equal(actual.shape, (3, 2, 5))
|
||||
|
||||
|
||||
class TestPutAlongAxis:
|
||||
def test_replace_max(self):
|
||||
a_base = np.array([[10, 30, 20], [60, 40, 50]])
|
||||
|
||||
for axis in list(range(a_base.ndim)) + [None]:
|
||||
# we mutate this in the loop
|
||||
a = a_base.copy()
|
||||
|
||||
# replace the max with a small value
|
||||
i_max = _add_keepdims(np.argmax)(a, axis=axis)
|
||||
put_along_axis(a, i_max, -99, axis=axis)
|
||||
|
||||
# find the new minimum, which should max
|
||||
i_min = _add_keepdims(np.argmin)(a, axis=axis)
|
||||
|
||||
assert_equal(i_min, i_max)
|
||||
|
||||
def test_broadcast(self):
|
||||
""" Test that non-indexing dimensions are broadcast in both directions """
|
||||
a = np.ones((3, 4, 1))
|
||||
ai = np.arange(10, dtype=np.intp).reshape((1, 2, 5)) % 4
|
||||
put_along_axis(a, ai, 20, axis=1)
|
||||
assert_equal(take_along_axis(a, ai, axis=1), 20)
|
||||
|
||||
def test_invalid(self):
|
||||
""" Test invalid inputs """
|
||||
a_base = np.array([[10, 30, 20], [60, 40, 50]])
|
||||
indices = np.array([[0], [1]])
|
||||
values = np.array([[2], [1]])
|
||||
|
||||
# sanity check
|
||||
a = a_base.copy()
|
||||
put_along_axis(a, indices, values, axis=0)
|
||||
assert np.all(a == [[2, 2, 2], [1, 1, 1]])
|
||||
|
||||
# invalid indices
|
||||
a = a_base.copy()
|
||||
with assert_raises(ValueError) as exc:
|
||||
put_along_axis(a, indices, values, axis=None)
|
||||
assert "single dimension" in str(exc.exception)
|
||||
|
||||
|
||||
|
||||
class TestApplyAlongAxis:
|
||||
def test_simple(self):
|
||||
a = np.ones((20, 10), 'd')
|
||||
assert_array_equal(
|
||||
apply_along_axis(len, 0, a), len(a)*np.ones(a.shape[1]))
|
||||
|
||||
def test_simple101(self):
|
||||
a = np.ones((10, 101), 'd')
|
||||
assert_array_equal(
|
||||
apply_along_axis(len, 0, a), len(a)*np.ones(a.shape[1]))
|
||||
|
||||
def test_3d(self):
|
||||
a = np.arange(27).reshape((3, 3, 3))
|
||||
assert_array_equal(apply_along_axis(np.sum, 0, a),
|
||||
[[27, 30, 33], [36, 39, 42], [45, 48, 51]])
|
||||
|
||||
def test_preserve_subclass(self):
|
||||
def double(row):
|
||||
return row * 2
|
||||
|
||||
class MyNDArray(np.ndarray):
|
||||
pass
|
||||
|
||||
m = np.array([[0, 1], [2, 3]]).view(MyNDArray)
|
||||
expected = np.array([[0, 2], [4, 6]]).view(MyNDArray)
|
||||
|
||||
result = apply_along_axis(double, 0, m)
|
||||
assert_(isinstance(result, MyNDArray))
|
||||
assert_array_equal(result, expected)
|
||||
|
||||
result = apply_along_axis(double, 1, m)
|
||||
assert_(isinstance(result, MyNDArray))
|
||||
assert_array_equal(result, expected)
|
||||
|
||||
def test_subclass(self):
|
||||
class MinimalSubclass(np.ndarray):
|
||||
data = 1
|
||||
|
||||
def minimal_function(array):
|
||||
return array.data
|
||||
|
||||
a = np.zeros((6, 3)).view(MinimalSubclass)
|
||||
|
||||
assert_array_equal(
|
||||
apply_along_axis(minimal_function, 0, a), np.array([1, 1, 1])
|
||||
)
|
||||
|
||||
def test_scalar_array(self, cls=np.ndarray):
|
||||
a = np.ones((6, 3)).view(cls)
|
||||
res = apply_along_axis(np.sum, 0, a)
|
||||
assert_(isinstance(res, cls))
|
||||
assert_array_equal(res, np.array([6, 6, 6]).view(cls))
|
||||
|
||||
def test_0d_array(self, cls=np.ndarray):
|
||||
def sum_to_0d(x):
|
||||
""" Sum x, returning a 0d array of the same class """
|
||||
assert_equal(x.ndim, 1)
|
||||
return np.squeeze(np.sum(x, keepdims=True))
|
||||
a = np.ones((6, 3)).view(cls)
|
||||
res = apply_along_axis(sum_to_0d, 0, a)
|
||||
assert_(isinstance(res, cls))
|
||||
assert_array_equal(res, np.array([6, 6, 6]).view(cls))
|
||||
|
||||
res = apply_along_axis(sum_to_0d, 1, a)
|
||||
assert_(isinstance(res, cls))
|
||||
assert_array_equal(res, np.array([3, 3, 3, 3, 3, 3]).view(cls))
|
||||
|
||||
def test_axis_insertion(self, cls=np.ndarray):
|
||||
def f1to2(x):
|
||||
"""produces an asymmetric non-square matrix from x"""
|
||||
assert_equal(x.ndim, 1)
|
||||
return (x[::-1] * x[1:,None]).view(cls)
|
||||
|
||||
a2d = np.arange(6*3).reshape((6, 3))
|
||||
|
||||
# 2d insertion along first axis
|
||||
actual = apply_along_axis(f1to2, 0, a2d)
|
||||
expected = np.stack([
|
||||
f1to2(a2d[:,i]) for i in range(a2d.shape[1])
|
||||
], axis=-1).view(cls)
|
||||
assert_equal(type(actual), type(expected))
|
||||
assert_equal(actual, expected)
|
||||
|
||||
# 2d insertion along last axis
|
||||
actual = apply_along_axis(f1to2, 1, a2d)
|
||||
expected = np.stack([
|
||||
f1to2(a2d[i,:]) for i in range(a2d.shape[0])
|
||||
], axis=0).view(cls)
|
||||
assert_equal(type(actual), type(expected))
|
||||
assert_equal(actual, expected)
|
||||
|
||||
# 3d insertion along middle axis
|
||||
a3d = np.arange(6*5*3).reshape((6, 5, 3))
|
||||
|
||||
actual = apply_along_axis(f1to2, 1, a3d)
|
||||
expected = np.stack([
|
||||
np.stack([
|
||||
f1to2(a3d[i,:,j]) for i in range(a3d.shape[0])
|
||||
], axis=0)
|
||||
for j in range(a3d.shape[2])
|
||||
], axis=-1).view(cls)
|
||||
assert_equal(type(actual), type(expected))
|
||||
assert_equal(actual, expected)
|
||||
|
||||
def test_subclass_preservation(self):
|
||||
class MinimalSubclass(np.ndarray):
|
||||
pass
|
||||
self.test_scalar_array(MinimalSubclass)
|
||||
self.test_0d_array(MinimalSubclass)
|
||||
self.test_axis_insertion(MinimalSubclass)
|
||||
|
||||
def test_axis_insertion_ma(self):
|
||||
def f1to2(x):
|
||||
"""produces an asymmetric non-square matrix from x"""
|
||||
assert_equal(x.ndim, 1)
|
||||
res = x[::-1] * x[1:,None]
|
||||
return np.ma.masked_where(res%5==0, res)
|
||||
a = np.arange(6*3).reshape((6, 3))
|
||||
res = apply_along_axis(f1to2, 0, a)
|
||||
assert_(isinstance(res, np.ma.masked_array))
|
||||
assert_equal(res.ndim, 3)
|
||||
assert_array_equal(res[:,:,0].mask, f1to2(a[:,0]).mask)
|
||||
assert_array_equal(res[:,:,1].mask, f1to2(a[:,1]).mask)
|
||||
assert_array_equal(res[:,:,2].mask, f1to2(a[:,2]).mask)
|
||||
|
||||
def test_tuple_func1d(self):
|
||||
def sample_1d(x):
|
||||
return x[1], x[0]
|
||||
res = np.apply_along_axis(sample_1d, 1, np.array([[1, 2], [3, 4]]))
|
||||
assert_array_equal(res, np.array([[2, 1], [4, 3]]))
|
||||
|
||||
def test_empty(self):
|
||||
# can't apply_along_axis when there's no chance to call the function
|
||||
def never_call(x):
|
||||
assert_(False) # should never be reached
|
||||
|
||||
a = np.empty((0, 0))
|
||||
assert_raises(ValueError, np.apply_along_axis, never_call, 0, a)
|
||||
assert_raises(ValueError, np.apply_along_axis, never_call, 1, a)
|
||||
|
||||
# but it's sometimes ok with some non-zero dimensions
|
||||
def empty_to_1(x):
|
||||
assert_(len(x) == 0)
|
||||
return 1
|
||||
|
||||
a = np.empty((10, 0))
|
||||
actual = np.apply_along_axis(empty_to_1, 1, a)
|
||||
assert_equal(actual, np.ones(10))
|
||||
assert_raises(ValueError, np.apply_along_axis, empty_to_1, 0, a)
|
||||
|
||||
def test_with_iterable_object(self):
|
||||
# from issue 5248
|
||||
d = np.array([
|
||||
[{1, 11}, {2, 22}, {3, 33}],
|
||||
[{4, 44}, {5, 55}, {6, 66}]
|
||||
])
|
||||
actual = np.apply_along_axis(lambda a: set.union(*a), 0, d)
|
||||
expected = np.array([{1, 11, 4, 44}, {2, 22, 5, 55}, {3, 33, 6, 66}])
|
||||
|
||||
assert_equal(actual, expected)
|
||||
|
||||
# issue 8642 - assert_equal doesn't detect this!
|
||||
for i in np.ndindex(actual.shape):
|
||||
assert_equal(type(actual[i]), type(expected[i]))
|
||||
|
||||
|
||||
class TestApplyOverAxes:
|
||||
def test_simple(self):
|
||||
a = np.arange(24).reshape(2, 3, 4)
|
||||
aoa_a = apply_over_axes(np.sum, a, [0, 2])
|
||||
assert_array_equal(aoa_a, np.array([[[60], [92], [124]]]))
|
||||
|
||||
|
||||
class TestExpandDims:
|
||||
def test_functionality(self):
|
||||
s = (2, 3, 4, 5)
|
||||
a = np.empty(s)
|
||||
for axis in range(-5, 4):
|
||||
b = expand_dims(a, axis)
|
||||
assert_(b.shape[axis] == 1)
|
||||
assert_(np.squeeze(b).shape == s)
|
||||
|
||||
def test_axis_tuple(self):
|
||||
a = np.empty((3, 3, 3))
|
||||
assert np.expand_dims(a, axis=(0, 1, 2)).shape == (1, 1, 1, 3, 3, 3)
|
||||
assert np.expand_dims(a, axis=(0, -1, -2)).shape == (1, 3, 3, 3, 1, 1)
|
||||
assert np.expand_dims(a, axis=(0, 3, 5)).shape == (1, 3, 3, 1, 3, 1)
|
||||
assert np.expand_dims(a, axis=(0, -3, -5)).shape == (1, 1, 3, 1, 3, 3)
|
||||
|
||||
def test_axis_out_of_range(self):
|
||||
s = (2, 3, 4, 5)
|
||||
a = np.empty(s)
|
||||
assert_raises(AxisError, expand_dims, a, -6)
|
||||
assert_raises(AxisError, expand_dims, a, 5)
|
||||
|
||||
a = np.empty((3, 3, 3))
|
||||
assert_raises(AxisError, expand_dims, a, (0, -6))
|
||||
assert_raises(AxisError, expand_dims, a, (0, 5))
|
||||
|
||||
def test_repeated_axis(self):
|
||||
a = np.empty((3, 3, 3))
|
||||
assert_raises(ValueError, expand_dims, a, axis=(1, 1))
|
||||
|
||||
def test_subclasses(self):
|
||||
a = np.arange(10).reshape((2, 5))
|
||||
a = np.ma.array(a, mask=a%3 == 0)
|
||||
|
||||
expanded = np.expand_dims(a, axis=1)
|
||||
assert_(isinstance(expanded, np.ma.MaskedArray))
|
||||
assert_equal(expanded.shape, (2, 1, 5))
|
||||
assert_equal(expanded.mask.shape, (2, 1, 5))
|
||||
|
||||
|
||||
class TestArraySplit:
|
||||
def test_integer_0_split(self):
|
||||
a = np.arange(10)
|
||||
assert_raises(ValueError, array_split, a, 0)
|
||||
|
||||
def test_integer_split(self):
|
||||
a = np.arange(10)
|
||||
res = array_split(a, 1)
|
||||
desired = [np.arange(10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
res = array_split(a, 2)
|
||||
desired = [np.arange(5), np.arange(5, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
res = array_split(a, 3)
|
||||
desired = [np.arange(4), np.arange(4, 7), np.arange(7, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
res = array_split(a, 4)
|
||||
desired = [np.arange(3), np.arange(3, 6), np.arange(6, 8),
|
||||
np.arange(8, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
res = array_split(a, 5)
|
||||
desired = [np.arange(2), np.arange(2, 4), np.arange(4, 6),
|
||||
np.arange(6, 8), np.arange(8, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
res = array_split(a, 6)
|
||||
desired = [np.arange(2), np.arange(2, 4), np.arange(4, 6),
|
||||
np.arange(6, 8), np.arange(8, 9), np.arange(9, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
res = array_split(a, 7)
|
||||
desired = [np.arange(2), np.arange(2, 4), np.arange(4, 6),
|
||||
np.arange(6, 7), np.arange(7, 8), np.arange(8, 9),
|
||||
np.arange(9, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
res = array_split(a, 8)
|
||||
desired = [np.arange(2), np.arange(2, 4), np.arange(4, 5),
|
||||
np.arange(5, 6), np.arange(6, 7), np.arange(7, 8),
|
||||
np.arange(8, 9), np.arange(9, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
res = array_split(a, 9)
|
||||
desired = [np.arange(2), np.arange(2, 3), np.arange(3, 4),
|
||||
np.arange(4, 5), np.arange(5, 6), np.arange(6, 7),
|
||||
np.arange(7, 8), np.arange(8, 9), np.arange(9, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
res = array_split(a, 10)
|
||||
desired = [np.arange(1), np.arange(1, 2), np.arange(2, 3),
|
||||
np.arange(3, 4), np.arange(4, 5), np.arange(5, 6),
|
||||
np.arange(6, 7), np.arange(7, 8), np.arange(8, 9),
|
||||
np.arange(9, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
res = array_split(a, 11)
|
||||
desired = [np.arange(1), np.arange(1, 2), np.arange(2, 3),
|
||||
np.arange(3, 4), np.arange(4, 5), np.arange(5, 6),
|
||||
np.arange(6, 7), np.arange(7, 8), np.arange(8, 9),
|
||||
np.arange(9, 10), np.array([])]
|
||||
compare_results(res, desired)
|
||||
|
||||
def test_integer_split_2D_rows(self):
|
||||
a = np.array([np.arange(10), np.arange(10)])
|
||||
res = array_split(a, 3, axis=0)
|
||||
tgt = [np.array([np.arange(10)]), np.array([np.arange(10)]),
|
||||
np.zeros((0, 10))]
|
||||
compare_results(res, tgt)
|
||||
assert_(a.dtype.type is res[-1].dtype.type)
|
||||
|
||||
# Same thing for manual splits:
|
||||
res = array_split(a, [0, 1], axis=0)
|
||||
tgt = [np.zeros((0, 10)), np.array([np.arange(10)]),
|
||||
np.array([np.arange(10)])]
|
||||
compare_results(res, tgt)
|
||||
assert_(a.dtype.type is res[-1].dtype.type)
|
||||
|
||||
def test_integer_split_2D_cols(self):
|
||||
a = np.array([np.arange(10), np.arange(10)])
|
||||
res = array_split(a, 3, axis=-1)
|
||||
desired = [np.array([np.arange(4), np.arange(4)]),
|
||||
np.array([np.arange(4, 7), np.arange(4, 7)]),
|
||||
np.array([np.arange(7, 10), np.arange(7, 10)])]
|
||||
compare_results(res, desired)
|
||||
|
||||
def test_integer_split_2D_default(self):
|
||||
""" This will fail if we change default axis
|
||||
"""
|
||||
a = np.array([np.arange(10), np.arange(10)])
|
||||
res = array_split(a, 3)
|
||||
tgt = [np.array([np.arange(10)]), np.array([np.arange(10)]),
|
||||
np.zeros((0, 10))]
|
||||
compare_results(res, tgt)
|
||||
assert_(a.dtype.type is res[-1].dtype.type)
|
||||
# perhaps should check higher dimensions
|
||||
|
||||
@pytest.mark.skipif(not IS_64BIT, reason="Needs 64bit platform")
|
||||
def test_integer_split_2D_rows_greater_max_int32(self):
|
||||
a = np.broadcast_to([0], (1 << 32, 2))
|
||||
res = array_split(a, 4)
|
||||
chunk = np.broadcast_to([0], (1 << 30, 2))
|
||||
tgt = [chunk] * 4
|
||||
for i in range(len(tgt)):
|
||||
assert_equal(res[i].shape, tgt[i].shape)
|
||||
|
||||
def test_index_split_simple(self):
|
||||
a = np.arange(10)
|
||||
indices = [1, 5, 7]
|
||||
res = array_split(a, indices, axis=-1)
|
||||
desired = [np.arange(0, 1), np.arange(1, 5), np.arange(5, 7),
|
||||
np.arange(7, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
def test_index_split_low_bound(self):
|
||||
a = np.arange(10)
|
||||
indices = [0, 5, 7]
|
||||
res = array_split(a, indices, axis=-1)
|
||||
desired = [np.array([]), np.arange(0, 5), np.arange(5, 7),
|
||||
np.arange(7, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
def test_index_split_high_bound(self):
|
||||
a = np.arange(10)
|
||||
indices = [0, 5, 7, 10, 12]
|
||||
res = array_split(a, indices, axis=-1)
|
||||
desired = [np.array([]), np.arange(0, 5), np.arange(5, 7),
|
||||
np.arange(7, 10), np.array([]), np.array([])]
|
||||
compare_results(res, desired)
|
||||
|
||||
|
||||
class TestSplit:
|
||||
# The split function is essentially the same as array_split,
|
||||
# except that it test if splitting will result in an
|
||||
# equal split. Only test for this case.
|
||||
|
||||
def test_equal_split(self):
|
||||
a = np.arange(10)
|
||||
res = split(a, 2)
|
||||
desired = [np.arange(5), np.arange(5, 10)]
|
||||
compare_results(res, desired)
|
||||
|
||||
def test_unequal_split(self):
|
||||
a = np.arange(10)
|
||||
assert_raises(ValueError, split, a, 3)
|
||||
|
||||
|
||||
class TestColumnStack:
|
||||
def test_non_iterable(self):
|
||||
assert_raises(TypeError, column_stack, 1)
|
||||
|
||||
def test_1D_arrays(self):
|
||||
# example from docstring
|
||||
a = np.array((1, 2, 3))
|
||||
b = np.array((2, 3, 4))
|
||||
expected = np.array([[1, 2],
|
||||
[2, 3],
|
||||
[3, 4]])
|
||||
actual = np.column_stack((a, b))
|
||||
assert_equal(actual, expected)
|
||||
|
||||
def test_2D_arrays(self):
|
||||
# same as hstack 2D docstring example
|
||||
a = np.array([[1], [2], [3]])
|
||||
b = np.array([[2], [3], [4]])
|
||||
expected = np.array([[1, 2],
|
||||
[2, 3],
|
||||
[3, 4]])
|
||||
actual = np.column_stack((a, b))
|
||||
assert_equal(actual, expected)
|
||||
|
||||
def test_generator(self):
|
||||
with pytest.raises(TypeError, match="arrays to stack must be"):
|
||||
column_stack(np.arange(3) for _ in range(2))
|
||||
|
||||
|
||||
class TestDstack:
|
||||
def test_non_iterable(self):
|
||||
assert_raises(TypeError, dstack, 1)
|
||||
|
||||
def test_0D_array(self):
|
||||
a = np.array(1)
|
||||
b = np.array(2)
|
||||
res = dstack([a, b])
|
||||
desired = np.array([[[1, 2]]])
|
||||
assert_array_equal(res, desired)
|
||||
|
||||
def test_1D_array(self):
|
||||
a = np.array([1])
|
||||
b = np.array([2])
|
||||
res = dstack([a, b])
|
||||
desired = np.array([[[1, 2]]])
|
||||
assert_array_equal(res, desired)
|
||||
|
||||
def test_2D_array(self):
|
||||
a = np.array([[1], [2]])
|
||||
b = np.array([[1], [2]])
|
||||
res = dstack([a, b])
|
||||
desired = np.array([[[1, 1]], [[2, 2, ]]])
|
||||
assert_array_equal(res, desired)
|
||||
|
||||
def test_2D_array2(self):
|
||||
a = np.array([1, 2])
|
||||
b = np.array([1, 2])
|
||||
res = dstack([a, b])
|
||||
desired = np.array([[[1, 1], [2, 2]]])
|
||||
assert_array_equal(res, desired)
|
||||
|
||||
def test_generator(self):
|
||||
with pytest.raises(TypeError, match="arrays to stack must be"):
|
||||
dstack(np.arange(3) for _ in range(2))
|
||||
|
||||
|
||||
# array_split has more comprehensive test of splitting.
|
||||
# only do simple test on hsplit, vsplit, and dsplit
|
||||
class TestHsplit:
|
||||
"""Only testing for integer splits.
|
||||
|
||||
"""
|
||||
def test_non_iterable(self):
|
||||
assert_raises(ValueError, hsplit, 1, 1)
|
||||
|
||||
def test_0D_array(self):
|
||||
a = np.array(1)
|
||||
try:
|
||||
hsplit(a, 2)
|
||||
assert_(0)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def test_1D_array(self):
|
||||
a = np.array([1, 2, 3, 4])
|
||||
res = hsplit(a, 2)
|
||||
desired = [np.array([1, 2]), np.array([3, 4])]
|
||||
compare_results(res, desired)
|
||||
|
||||
def test_2D_array(self):
|
||||
a = np.array([[1, 2, 3, 4],
|
||||
[1, 2, 3, 4]])
|
||||
res = hsplit(a, 2)
|
||||
desired = [np.array([[1, 2], [1, 2]]), np.array([[3, 4], [3, 4]])]
|
||||
compare_results(res, desired)
|
||||
|
||||
|
||||
class TestVsplit:
|
||||
"""Only testing for integer splits.
|
||||
|
||||
"""
|
||||
def test_non_iterable(self):
|
||||
assert_raises(ValueError, vsplit, 1, 1)
|
||||
|
||||
def test_0D_array(self):
|
||||
a = np.array(1)
|
||||
assert_raises(ValueError, vsplit, a, 2)
|
||||
|
||||
def test_1D_array(self):
|
||||
a = np.array([1, 2, 3, 4])
|
||||
try:
|
||||
vsplit(a, 2)
|
||||
assert_(0)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def test_2D_array(self):
|
||||
a = np.array([[1, 2, 3, 4],
|
||||
[1, 2, 3, 4]])
|
||||
res = vsplit(a, 2)
|
||||
desired = [np.array([[1, 2, 3, 4]]), np.array([[1, 2, 3, 4]])]
|
||||
compare_results(res, desired)
|
||||
|
||||
|
||||
class TestDsplit:
|
||||
# Only testing for integer splits.
|
||||
def test_non_iterable(self):
|
||||
assert_raises(ValueError, dsplit, 1, 1)
|
||||
|
||||
def test_0D_array(self):
|
||||
a = np.array(1)
|
||||
assert_raises(ValueError, dsplit, a, 2)
|
||||
|
||||
def test_1D_array(self):
|
||||
a = np.array([1, 2, 3, 4])
|
||||
assert_raises(ValueError, dsplit, a, 2)
|
||||
|
||||
def test_2D_array(self):
|
||||
a = np.array([[1, 2, 3, 4],
|
||||
[1, 2, 3, 4]])
|
||||
try:
|
||||
dsplit(a, 2)
|
||||
assert_(0)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def test_3D_array(self):
|
||||
a = np.array([[[1, 2, 3, 4],
|
||||
[1, 2, 3, 4]],
|
||||
[[1, 2, 3, 4],
|
||||
[1, 2, 3, 4]]])
|
||||
res = dsplit(a, 2)
|
||||
desired = [np.array([[[1, 2], [1, 2]], [[1, 2], [1, 2]]]),
|
||||
np.array([[[3, 4], [3, 4]], [[3, 4], [3, 4]]])]
|
||||
compare_results(res, desired)
|
||||
|
||||
|
||||
class TestSqueeze:
|
||||
def test_basic(self):
|
||||
from numpy.random import rand
|
||||
|
||||
a = rand(20, 10, 10, 1, 1)
|
||||
b = rand(20, 1, 10, 1, 20)
|
||||
c = rand(1, 1, 20, 10)
|
||||
assert_array_equal(np.squeeze(a), np.reshape(a, (20, 10, 10)))
|
||||
assert_array_equal(np.squeeze(b), np.reshape(b, (20, 10, 20)))
|
||||
assert_array_equal(np.squeeze(c), np.reshape(c, (20, 10)))
|
||||
|
||||
# Squeezing to 0-dim should still give an ndarray
|
||||
a = [[[1.5]]]
|
||||
res = np.squeeze(a)
|
||||
assert_equal(res, 1.5)
|
||||
assert_equal(res.ndim, 0)
|
||||
assert_equal(type(res), np.ndarray)
|
||||
|
||||
|
||||
class TestKron:
|
||||
def test_basic(self):
|
||||
# Using 0-dimensional ndarray
|
||||
a = np.array(1)
|
||||
b = np.array([[1, 2], [3, 4]])
|
||||
k = np.array([[1, 2], [3, 4]])
|
||||
assert_array_equal(np.kron(a, b), k)
|
||||
a = np.array([[1, 2], [3, 4]])
|
||||
b = np.array(1)
|
||||
assert_array_equal(np.kron(a, b), k)
|
||||
|
||||
# Using 1-dimensional ndarray
|
||||
a = np.array([3])
|
||||
b = np.array([[1, 2], [3, 4]])
|
||||
k = np.array([[3, 6], [9, 12]])
|
||||
assert_array_equal(np.kron(a, b), k)
|
||||
a = np.array([[1, 2], [3, 4]])
|
||||
b = np.array([3])
|
||||
assert_array_equal(np.kron(a, b), k)
|
||||
|
||||
# Using 3-dimensional ndarray
|
||||
a = np.array([[[1]], [[2]]])
|
||||
b = np.array([[1, 2], [3, 4]])
|
||||
k = np.array([[[1, 2], [3, 4]], [[2, 4], [6, 8]]])
|
||||
assert_array_equal(np.kron(a, b), k)
|
||||
a = np.array([[1, 2], [3, 4]])
|
||||
b = np.array([[[1]], [[2]]])
|
||||
k = np.array([[[1, 2], [3, 4]], [[2, 4], [6, 8]]])
|
||||
assert_array_equal(np.kron(a, b), k)
|
||||
|
||||
def test_return_type(self):
|
||||
class myarray(np.ndarray):
|
||||
__array_priority__ = 1.0
|
||||
|
||||
a = np.ones([2, 2])
|
||||
ma = myarray(a.shape, a.dtype, a.data)
|
||||
assert_equal(type(kron(a, a)), np.ndarray)
|
||||
assert_equal(type(kron(ma, ma)), myarray)
|
||||
assert_equal(type(kron(a, ma)), myarray)
|
||||
assert_equal(type(kron(ma, a)), myarray)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"array_class", [np.asarray, np.asmatrix]
|
||||
)
|
||||
def test_kron_smoke(self, array_class):
|
||||
a = array_class(np.ones([3, 3]))
|
||||
b = array_class(np.ones([3, 3]))
|
||||
k = array_class(np.ones([9, 9]))
|
||||
|
||||
assert_array_equal(np.kron(a, b), k)
|
||||
|
||||
def test_kron_ma(self):
|
||||
x = np.ma.array([[1, 2], [3, 4]], mask=[[0, 1], [1, 0]])
|
||||
k = np.ma.array(np.diag([1, 4, 4, 16]),
|
||||
mask=~np.array(np.identity(4), dtype=bool))
|
||||
|
||||
assert_array_equal(k, np.kron(x, x))
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"shape_a,shape_b", [
|
||||
((1, 1), (1, 1)),
|
||||
((1, 2, 3), (4, 5, 6)),
|
||||
((2, 2), (2, 2, 2)),
|
||||
((1, 0), (1, 1)),
|
||||
((2, 0, 2), (2, 2)),
|
||||
((2, 0, 0, 2), (2, 0, 2)),
|
||||
])
|
||||
def test_kron_shape(self, shape_a, shape_b):
|
||||
a = np.ones(shape_a)
|
||||
b = np.ones(shape_b)
|
||||
normalised_shape_a = (1,) * max(0, len(shape_b)-len(shape_a)) + shape_a
|
||||
normalised_shape_b = (1,) * max(0, len(shape_a)-len(shape_b)) + shape_b
|
||||
expected_shape = np.multiply(normalised_shape_a, normalised_shape_b)
|
||||
|
||||
k = np.kron(a, b)
|
||||
assert np.array_equal(
|
||||
k.shape, expected_shape), "Unexpected shape from kron"
|
||||
|
||||
|
||||
class TestTile:
|
||||
def test_basic(self):
|
||||
a = np.array([0, 1, 2])
|
||||
b = [[1, 2], [3, 4]]
|
||||
assert_equal(tile(a, 2), [0, 1, 2, 0, 1, 2])
|
||||
assert_equal(tile(a, (2, 2)), [[0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2]])
|
||||
assert_equal(tile(a, (1, 2)), [[0, 1, 2, 0, 1, 2]])
|
||||
assert_equal(tile(b, 2), [[1, 2, 1, 2], [3, 4, 3, 4]])
|
||||
assert_equal(tile(b, (2, 1)), [[1, 2], [3, 4], [1, 2], [3, 4]])
|
||||
assert_equal(tile(b, (2, 2)), [[1, 2, 1, 2], [3, 4, 3, 4],
|
||||
[1, 2, 1, 2], [3, 4, 3, 4]])
|
||||
|
||||
def test_tile_one_repetition_on_array_gh4679(self):
|
||||
a = np.arange(5)
|
||||
b = tile(a, 1)
|
||||
b += 2
|
||||
assert_equal(a, np.arange(5))
|
||||
|
||||
def test_empty(self):
|
||||
a = np.array([[[]]])
|
||||
b = np.array([[], []])
|
||||
c = tile(b, 2).shape
|
||||
d = tile(a, (3, 2, 5)).shape
|
||||
assert_equal(c, (2, 0))
|
||||
assert_equal(d, (3, 2, 0))
|
||||
|
||||
def test_kroncompare(self):
|
||||
from numpy.random import randint
|
||||
|
||||
reps = [(2,), (1, 2), (2, 1), (2, 2), (2, 3, 2), (3, 2)]
|
||||
shape = [(3,), (2, 3), (3, 4, 3), (3, 2, 3), (4, 3, 2, 4), (2, 2)]
|
||||
for s in shape:
|
||||
b = randint(0, 10, size=s)
|
||||
for r in reps:
|
||||
a = np.ones(r, b.dtype)
|
||||
large = tile(b, r)
|
||||
klarge = kron(a, b)
|
||||
assert_equal(large, klarge)
|
||||
|
||||
|
||||
class TestMayShareMemory:
|
||||
def test_basic(self):
|
||||
d = np.ones((50, 60))
|
||||
d2 = np.ones((30, 60, 6))
|
||||
assert_(np.may_share_memory(d, d))
|
||||
assert_(np.may_share_memory(d, d[::-1]))
|
||||
assert_(np.may_share_memory(d, d[::2]))
|
||||
assert_(np.may_share_memory(d, d[1:, ::-1]))
|
||||
|
||||
assert_(not np.may_share_memory(d[::-1], d2))
|
||||
assert_(not np.may_share_memory(d[::2], d2))
|
||||
assert_(not np.may_share_memory(d[1:, ::-1], d2))
|
||||
assert_(np.may_share_memory(d2[1:, ::-1], d2))
|
||||
|
||||
|
||||
# Utility
|
||||
def compare_results(res, desired):
|
||||
"""Compare lists of arrays."""
|
||||
if len(res) != len(desired):
|
||||
raise ValueError("Iterables have different lengths")
|
||||
# See also PEP 618 for Python 3.10
|
||||
for x, y in zip(res, desired):
|
||||
assert_array_equal(x, y)
|
||||
647
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_stride_tricks.py
vendored
Normal file
647
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_stride_tricks.py
vendored
Normal file
@@ -0,0 +1,647 @@
|
||||
import numpy as np
|
||||
from numpy._core._rational_tests import rational
|
||||
from numpy.testing import (
|
||||
assert_equal, assert_array_equal, assert_raises, assert_,
|
||||
assert_raises_regex, assert_warns,
|
||||
)
|
||||
from numpy.lib._stride_tricks_impl import (
|
||||
as_strided, broadcast_arrays, _broadcast_shape, broadcast_to,
|
||||
broadcast_shapes, sliding_window_view,
|
||||
)
|
||||
import pytest
|
||||
|
||||
|
||||
def assert_shapes_correct(input_shapes, expected_shape):
|
||||
# Broadcast a list of arrays with the given input shapes and check the
|
||||
# common output shape.
|
||||
|
||||
inarrays = [np.zeros(s) for s in input_shapes]
|
||||
outarrays = broadcast_arrays(*inarrays)
|
||||
outshapes = [a.shape for a in outarrays]
|
||||
expected = [expected_shape] * len(inarrays)
|
||||
assert_equal(outshapes, expected)
|
||||
|
||||
|
||||
def assert_incompatible_shapes_raise(input_shapes):
|
||||
# Broadcast a list of arrays with the given (incompatible) input shapes
|
||||
# and check that they raise a ValueError.
|
||||
|
||||
inarrays = [np.zeros(s) for s in input_shapes]
|
||||
assert_raises(ValueError, broadcast_arrays, *inarrays)
|
||||
|
||||
|
||||
def assert_same_as_ufunc(shape0, shape1, transposed=False, flipped=False):
|
||||
# Broadcast two shapes against each other and check that the data layout
|
||||
# is the same as if a ufunc did the broadcasting.
|
||||
|
||||
x0 = np.zeros(shape0, dtype=int)
|
||||
# Note that multiply.reduce's identity element is 1.0, so when shape1==(),
|
||||
# this gives the desired n==1.
|
||||
n = int(np.multiply.reduce(shape1))
|
||||
x1 = np.arange(n).reshape(shape1)
|
||||
if transposed:
|
||||
x0 = x0.T
|
||||
x1 = x1.T
|
||||
if flipped:
|
||||
x0 = x0[::-1]
|
||||
x1 = x1[::-1]
|
||||
# Use the add ufunc to do the broadcasting. Since we're adding 0s to x1, the
|
||||
# result should be exactly the same as the broadcasted view of x1.
|
||||
y = x0 + x1
|
||||
b0, b1 = broadcast_arrays(x0, x1)
|
||||
assert_array_equal(y, b1)
|
||||
|
||||
|
||||
def test_same():
|
||||
x = np.arange(10)
|
||||
y = np.arange(10)
|
||||
bx, by = broadcast_arrays(x, y)
|
||||
assert_array_equal(x, bx)
|
||||
assert_array_equal(y, by)
|
||||
|
||||
def test_broadcast_kwargs():
|
||||
# ensure that a TypeError is appropriately raised when
|
||||
# np.broadcast_arrays() is called with any keyword
|
||||
# argument other than 'subok'
|
||||
x = np.arange(10)
|
||||
y = np.arange(10)
|
||||
|
||||
with assert_raises_regex(TypeError, 'got an unexpected keyword'):
|
||||
broadcast_arrays(x, y, dtype='float64')
|
||||
|
||||
|
||||
def test_one_off():
|
||||
x = np.array([[1, 2, 3]])
|
||||
y = np.array([[1], [2], [3]])
|
||||
bx, by = broadcast_arrays(x, y)
|
||||
bx0 = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
|
||||
by0 = bx0.T
|
||||
assert_array_equal(bx0, bx)
|
||||
assert_array_equal(by0, by)
|
||||
|
||||
|
||||
def test_same_input_shapes():
|
||||
# Check that the final shape is just the input shape.
|
||||
|
||||
data = [
|
||||
(),
|
||||
(1,),
|
||||
(3,),
|
||||
(0, 1),
|
||||
(0, 3),
|
||||
(1, 0),
|
||||
(3, 0),
|
||||
(1, 3),
|
||||
(3, 1),
|
||||
(3, 3),
|
||||
]
|
||||
for shape in data:
|
||||
input_shapes = [shape]
|
||||
# Single input.
|
||||
assert_shapes_correct(input_shapes, shape)
|
||||
# Double input.
|
||||
input_shapes2 = [shape, shape]
|
||||
assert_shapes_correct(input_shapes2, shape)
|
||||
# Triple input.
|
||||
input_shapes3 = [shape, shape, shape]
|
||||
assert_shapes_correct(input_shapes3, shape)
|
||||
|
||||
|
||||
def test_two_compatible_by_ones_input_shapes():
|
||||
# Check that two different input shapes of the same length, but some have
|
||||
# ones, broadcast to the correct shape.
|
||||
|
||||
data = [
|
||||
[[(1,), (3,)], (3,)],
|
||||
[[(1, 3), (3, 3)], (3, 3)],
|
||||
[[(3, 1), (3, 3)], (3, 3)],
|
||||
[[(1, 3), (3, 1)], (3, 3)],
|
||||
[[(1, 1), (3, 3)], (3, 3)],
|
||||
[[(1, 1), (1, 3)], (1, 3)],
|
||||
[[(1, 1), (3, 1)], (3, 1)],
|
||||
[[(1, 0), (0, 0)], (0, 0)],
|
||||
[[(0, 1), (0, 0)], (0, 0)],
|
||||
[[(1, 0), (0, 1)], (0, 0)],
|
||||
[[(1, 1), (0, 0)], (0, 0)],
|
||||
[[(1, 1), (1, 0)], (1, 0)],
|
||||
[[(1, 1), (0, 1)], (0, 1)],
|
||||
]
|
||||
for input_shapes, expected_shape in data:
|
||||
assert_shapes_correct(input_shapes, expected_shape)
|
||||
# Reverse the input shapes since broadcasting should be symmetric.
|
||||
assert_shapes_correct(input_shapes[::-1], expected_shape)
|
||||
|
||||
|
||||
def test_two_compatible_by_prepending_ones_input_shapes():
|
||||
# Check that two different input shapes (of different lengths) broadcast
|
||||
# to the correct shape.
|
||||
|
||||
data = [
|
||||
[[(), (3,)], (3,)],
|
||||
[[(3,), (3, 3)], (3, 3)],
|
||||
[[(3,), (3, 1)], (3, 3)],
|
||||
[[(1,), (3, 3)], (3, 3)],
|
||||
[[(), (3, 3)], (3, 3)],
|
||||
[[(1, 1), (3,)], (1, 3)],
|
||||
[[(1,), (3, 1)], (3, 1)],
|
||||
[[(1,), (1, 3)], (1, 3)],
|
||||
[[(), (1, 3)], (1, 3)],
|
||||
[[(), (3, 1)], (3, 1)],
|
||||
[[(), (0,)], (0,)],
|
||||
[[(0,), (0, 0)], (0, 0)],
|
||||
[[(0,), (0, 1)], (0, 0)],
|
||||
[[(1,), (0, 0)], (0, 0)],
|
||||
[[(), (0, 0)], (0, 0)],
|
||||
[[(1, 1), (0,)], (1, 0)],
|
||||
[[(1,), (0, 1)], (0, 1)],
|
||||
[[(1,), (1, 0)], (1, 0)],
|
||||
[[(), (1, 0)], (1, 0)],
|
||||
[[(), (0, 1)], (0, 1)],
|
||||
]
|
||||
for input_shapes, expected_shape in data:
|
||||
assert_shapes_correct(input_shapes, expected_shape)
|
||||
# Reverse the input shapes since broadcasting should be symmetric.
|
||||
assert_shapes_correct(input_shapes[::-1], expected_shape)
|
||||
|
||||
|
||||
def test_incompatible_shapes_raise_valueerror():
|
||||
# Check that a ValueError is raised for incompatible shapes.
|
||||
|
||||
data = [
|
||||
[(3,), (4,)],
|
||||
[(2, 3), (2,)],
|
||||
[(3,), (3,), (4,)],
|
||||
[(1, 3, 4), (2, 3, 3)],
|
||||
]
|
||||
for input_shapes in data:
|
||||
assert_incompatible_shapes_raise(input_shapes)
|
||||
# Reverse the input shapes since broadcasting should be symmetric.
|
||||
assert_incompatible_shapes_raise(input_shapes[::-1])
|
||||
|
||||
|
||||
def test_same_as_ufunc():
|
||||
# Check that the data layout is the same as if a ufunc did the operation.
|
||||
|
||||
data = [
|
||||
[[(1,), (3,)], (3,)],
|
||||
[[(1, 3), (3, 3)], (3, 3)],
|
||||
[[(3, 1), (3, 3)], (3, 3)],
|
||||
[[(1, 3), (3, 1)], (3, 3)],
|
||||
[[(1, 1), (3, 3)], (3, 3)],
|
||||
[[(1, 1), (1, 3)], (1, 3)],
|
||||
[[(1, 1), (3, 1)], (3, 1)],
|
||||
[[(1, 0), (0, 0)], (0, 0)],
|
||||
[[(0, 1), (0, 0)], (0, 0)],
|
||||
[[(1, 0), (0, 1)], (0, 0)],
|
||||
[[(1, 1), (0, 0)], (0, 0)],
|
||||
[[(1, 1), (1, 0)], (1, 0)],
|
||||
[[(1, 1), (0, 1)], (0, 1)],
|
||||
[[(), (3,)], (3,)],
|
||||
[[(3,), (3, 3)], (3, 3)],
|
||||
[[(3,), (3, 1)], (3, 3)],
|
||||
[[(1,), (3, 3)], (3, 3)],
|
||||
[[(), (3, 3)], (3, 3)],
|
||||
[[(1, 1), (3,)], (1, 3)],
|
||||
[[(1,), (3, 1)], (3, 1)],
|
||||
[[(1,), (1, 3)], (1, 3)],
|
||||
[[(), (1, 3)], (1, 3)],
|
||||
[[(), (3, 1)], (3, 1)],
|
||||
[[(), (0,)], (0,)],
|
||||
[[(0,), (0, 0)], (0, 0)],
|
||||
[[(0,), (0, 1)], (0, 0)],
|
||||
[[(1,), (0, 0)], (0, 0)],
|
||||
[[(), (0, 0)], (0, 0)],
|
||||
[[(1, 1), (0,)], (1, 0)],
|
||||
[[(1,), (0, 1)], (0, 1)],
|
||||
[[(1,), (1, 0)], (1, 0)],
|
||||
[[(), (1, 0)], (1, 0)],
|
||||
[[(), (0, 1)], (0, 1)],
|
||||
]
|
||||
for input_shapes, expected_shape in data:
|
||||
assert_same_as_ufunc(input_shapes[0], input_shapes[1],
|
||||
"Shapes: %s %s" % (input_shapes[0], input_shapes[1]))
|
||||
# Reverse the input shapes since broadcasting should be symmetric.
|
||||
assert_same_as_ufunc(input_shapes[1], input_shapes[0])
|
||||
# Try them transposed, too.
|
||||
assert_same_as_ufunc(input_shapes[0], input_shapes[1], True)
|
||||
# ... and flipped for non-rank-0 inputs in order to test negative
|
||||
# strides.
|
||||
if () not in input_shapes:
|
||||
assert_same_as_ufunc(input_shapes[0], input_shapes[1], False, True)
|
||||
assert_same_as_ufunc(input_shapes[0], input_shapes[1], True, True)
|
||||
|
||||
|
||||
def test_broadcast_to_succeeds():
|
||||
data = [
|
||||
[np.array(0), (0,), np.array(0)],
|
||||
[np.array(0), (1,), np.zeros(1)],
|
||||
[np.array(0), (3,), np.zeros(3)],
|
||||
[np.ones(1), (1,), np.ones(1)],
|
||||
[np.ones(1), (2,), np.ones(2)],
|
||||
[np.ones(1), (1, 2, 3), np.ones((1, 2, 3))],
|
||||
[np.arange(3), (3,), np.arange(3)],
|
||||
[np.arange(3), (1, 3), np.arange(3).reshape(1, -1)],
|
||||
[np.arange(3), (2, 3), np.array([[0, 1, 2], [0, 1, 2]])],
|
||||
# test if shape is not a tuple
|
||||
[np.ones(0), 0, np.ones(0)],
|
||||
[np.ones(1), 1, np.ones(1)],
|
||||
[np.ones(1), 2, np.ones(2)],
|
||||
# these cases with size 0 are strange, but they reproduce the behavior
|
||||
# of broadcasting with ufuncs (see test_same_as_ufunc above)
|
||||
[np.ones(1), (0,), np.ones(0)],
|
||||
[np.ones((1, 2)), (0, 2), np.ones((0, 2))],
|
||||
[np.ones((2, 1)), (2, 0), np.ones((2, 0))],
|
||||
]
|
||||
for input_array, shape, expected in data:
|
||||
actual = broadcast_to(input_array, shape)
|
||||
assert_array_equal(expected, actual)
|
||||
|
||||
|
||||
def test_broadcast_to_raises():
|
||||
data = [
|
||||
[(0,), ()],
|
||||
[(1,), ()],
|
||||
[(3,), ()],
|
||||
[(3,), (1,)],
|
||||
[(3,), (2,)],
|
||||
[(3,), (4,)],
|
||||
[(1, 2), (2, 1)],
|
||||
[(1, 1), (1,)],
|
||||
[(1,), -1],
|
||||
[(1,), (-1,)],
|
||||
[(1, 2), (-1, 2)],
|
||||
]
|
||||
for orig_shape, target_shape in data:
|
||||
arr = np.zeros(orig_shape)
|
||||
assert_raises(ValueError, lambda: broadcast_to(arr, target_shape))
|
||||
|
||||
|
||||
def test_broadcast_shape():
|
||||
# tests internal _broadcast_shape
|
||||
# _broadcast_shape is already exercised indirectly by broadcast_arrays
|
||||
# _broadcast_shape is also exercised by the public broadcast_shapes function
|
||||
assert_equal(_broadcast_shape(), ())
|
||||
assert_equal(_broadcast_shape([1, 2]), (2,))
|
||||
assert_equal(_broadcast_shape(np.ones((1, 1))), (1, 1))
|
||||
assert_equal(_broadcast_shape(np.ones((1, 1)), np.ones((3, 4))), (3, 4))
|
||||
assert_equal(_broadcast_shape(*([np.ones((1, 2))] * 32)), (1, 2))
|
||||
assert_equal(_broadcast_shape(*([np.ones((1, 2))] * 100)), (1, 2))
|
||||
|
||||
# regression tests for gh-5862
|
||||
assert_equal(_broadcast_shape(*([np.ones(2)] * 32 + [1])), (2,))
|
||||
bad_args = [np.ones(2)] * 32 + [np.ones(3)] * 32
|
||||
assert_raises(ValueError, lambda: _broadcast_shape(*bad_args))
|
||||
|
||||
|
||||
def test_broadcast_shapes_succeeds():
|
||||
# tests public broadcast_shapes
|
||||
data = [
|
||||
[[], ()],
|
||||
[[()], ()],
|
||||
[[(7,)], (7,)],
|
||||
[[(1, 2), (2,)], (1, 2)],
|
||||
[[(1, 1)], (1, 1)],
|
||||
[[(1, 1), (3, 4)], (3, 4)],
|
||||
[[(6, 7), (5, 6, 1), (7,), (5, 1, 7)], (5, 6, 7)],
|
||||
[[(5, 6, 1)], (5, 6, 1)],
|
||||
[[(1, 3), (3, 1)], (3, 3)],
|
||||
[[(1, 0), (0, 0)], (0, 0)],
|
||||
[[(0, 1), (0, 0)], (0, 0)],
|
||||
[[(1, 0), (0, 1)], (0, 0)],
|
||||
[[(1, 1), (0, 0)], (0, 0)],
|
||||
[[(1, 1), (1, 0)], (1, 0)],
|
||||
[[(1, 1), (0, 1)], (0, 1)],
|
||||
[[(), (0,)], (0,)],
|
||||
[[(0,), (0, 0)], (0, 0)],
|
||||
[[(0,), (0, 1)], (0, 0)],
|
||||
[[(1,), (0, 0)], (0, 0)],
|
||||
[[(), (0, 0)], (0, 0)],
|
||||
[[(1, 1), (0,)], (1, 0)],
|
||||
[[(1,), (0, 1)], (0, 1)],
|
||||
[[(1,), (1, 0)], (1, 0)],
|
||||
[[(), (1, 0)], (1, 0)],
|
||||
[[(), (0, 1)], (0, 1)],
|
||||
[[(1,), (3,)], (3,)],
|
||||
[[2, (3, 2)], (3, 2)],
|
||||
]
|
||||
for input_shapes, target_shape in data:
|
||||
assert_equal(broadcast_shapes(*input_shapes), target_shape)
|
||||
|
||||
assert_equal(broadcast_shapes(*([(1, 2)] * 32)), (1, 2))
|
||||
assert_equal(broadcast_shapes(*([(1, 2)] * 100)), (1, 2))
|
||||
|
||||
# regression tests for gh-5862
|
||||
assert_equal(broadcast_shapes(*([(2,)] * 32)), (2,))
|
||||
|
||||
|
||||
def test_broadcast_shapes_raises():
|
||||
# tests public broadcast_shapes
|
||||
data = [
|
||||
[(3,), (4,)],
|
||||
[(2, 3), (2,)],
|
||||
[(3,), (3,), (4,)],
|
||||
[(1, 3, 4), (2, 3, 3)],
|
||||
[(1, 2), (3, 1), (3, 2), (10, 5)],
|
||||
[2, (2, 3)],
|
||||
]
|
||||
for input_shapes in data:
|
||||
assert_raises(ValueError, lambda: broadcast_shapes(*input_shapes))
|
||||
|
||||
bad_args = [(2,)] * 32 + [(3,)] * 32
|
||||
assert_raises(ValueError, lambda: broadcast_shapes(*bad_args))
|
||||
|
||||
|
||||
def test_as_strided():
|
||||
a = np.array([None])
|
||||
a_view = as_strided(a)
|
||||
expected = np.array([None])
|
||||
assert_array_equal(a_view, np.array([None]))
|
||||
|
||||
a = np.array([1, 2, 3, 4])
|
||||
a_view = as_strided(a, shape=(2,), strides=(2 * a.itemsize,))
|
||||
expected = np.array([1, 3])
|
||||
assert_array_equal(a_view, expected)
|
||||
|
||||
a = np.array([1, 2, 3, 4])
|
||||
a_view = as_strided(a, shape=(3, 4), strides=(0, 1 * a.itemsize))
|
||||
expected = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
|
||||
assert_array_equal(a_view, expected)
|
||||
|
||||
# Regression test for gh-5081
|
||||
dt = np.dtype([('num', 'i4'), ('obj', 'O')])
|
||||
a = np.empty((4,), dtype=dt)
|
||||
a['num'] = np.arange(1, 5)
|
||||
a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
|
||||
expected_num = [[1, 2, 3, 4]] * 3
|
||||
expected_obj = [[None]*4]*3
|
||||
assert_equal(a_view.dtype, dt)
|
||||
assert_array_equal(expected_num, a_view['num'])
|
||||
assert_array_equal(expected_obj, a_view['obj'])
|
||||
|
||||
# Make sure that void types without fields are kept unchanged
|
||||
a = np.empty((4,), dtype='V4')
|
||||
a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
|
||||
assert_equal(a.dtype, a_view.dtype)
|
||||
|
||||
# Make sure that the only type that could fail is properly handled
|
||||
dt = np.dtype({'names': [''], 'formats': ['V4']})
|
||||
a = np.empty((4,), dtype=dt)
|
||||
a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
|
||||
assert_equal(a.dtype, a_view.dtype)
|
||||
|
||||
# Custom dtypes should not be lost (gh-9161)
|
||||
r = [rational(i) for i in range(4)]
|
||||
a = np.array(r, dtype=rational)
|
||||
a_view = as_strided(a, shape=(3, 4), strides=(0, a.itemsize))
|
||||
assert_equal(a.dtype, a_view.dtype)
|
||||
assert_array_equal([r] * 3, a_view)
|
||||
|
||||
|
||||
class TestSlidingWindowView:
|
||||
def test_1d(self):
|
||||
arr = np.arange(5)
|
||||
arr_view = sliding_window_view(arr, 2)
|
||||
expected = np.array([[0, 1],
|
||||
[1, 2],
|
||||
[2, 3],
|
||||
[3, 4]])
|
||||
assert_array_equal(arr_view, expected)
|
||||
|
||||
def test_2d(self):
|
||||
i, j = np.ogrid[:3, :4]
|
||||
arr = 10*i + j
|
||||
shape = (2, 2)
|
||||
arr_view = sliding_window_view(arr, shape)
|
||||
expected = np.array([[[[0, 1], [10, 11]],
|
||||
[[1, 2], [11, 12]],
|
||||
[[2, 3], [12, 13]]],
|
||||
[[[10, 11], [20, 21]],
|
||||
[[11, 12], [21, 22]],
|
||||
[[12, 13], [22, 23]]]])
|
||||
assert_array_equal(arr_view, expected)
|
||||
|
||||
def test_2d_with_axis(self):
|
||||
i, j = np.ogrid[:3, :4]
|
||||
arr = 10*i + j
|
||||
arr_view = sliding_window_view(arr, 3, 0)
|
||||
expected = np.array([[[0, 10, 20],
|
||||
[1, 11, 21],
|
||||
[2, 12, 22],
|
||||
[3, 13, 23]]])
|
||||
assert_array_equal(arr_view, expected)
|
||||
|
||||
def test_2d_repeated_axis(self):
|
||||
i, j = np.ogrid[:3, :4]
|
||||
arr = 10*i + j
|
||||
arr_view = sliding_window_view(arr, (2, 3), (1, 1))
|
||||
expected = np.array([[[[0, 1, 2],
|
||||
[1, 2, 3]]],
|
||||
[[[10, 11, 12],
|
||||
[11, 12, 13]]],
|
||||
[[[20, 21, 22],
|
||||
[21, 22, 23]]]])
|
||||
assert_array_equal(arr_view, expected)
|
||||
|
||||
def test_2d_without_axis(self):
|
||||
i, j = np.ogrid[:4, :4]
|
||||
arr = 10*i + j
|
||||
shape = (2, 3)
|
||||
arr_view = sliding_window_view(arr, shape)
|
||||
expected = np.array([[[[0, 1, 2], [10, 11, 12]],
|
||||
[[1, 2, 3], [11, 12, 13]]],
|
||||
[[[10, 11, 12], [20, 21, 22]],
|
||||
[[11, 12, 13], [21, 22, 23]]],
|
||||
[[[20, 21, 22], [30, 31, 32]],
|
||||
[[21, 22, 23], [31, 32, 33]]]])
|
||||
assert_array_equal(arr_view, expected)
|
||||
|
||||
def test_errors(self):
|
||||
i, j = np.ogrid[:4, :4]
|
||||
arr = 10*i + j
|
||||
with pytest.raises(ValueError, match='cannot contain negative values'):
|
||||
sliding_window_view(arr, (-1, 3))
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match='must provide window_shape for all dimensions of `x`'):
|
||||
sliding_window_view(arr, (1,))
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match='Must provide matching length window_shape and axis'):
|
||||
sliding_window_view(arr, (1, 3, 4), axis=(0, 1))
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match='window shape cannot be larger than input array'):
|
||||
sliding_window_view(arr, (5, 5))
|
||||
|
||||
def test_writeable(self):
|
||||
arr = np.arange(5)
|
||||
view = sliding_window_view(arr, 2, writeable=False)
|
||||
assert_(not view.flags.writeable)
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match='assignment destination is read-only'):
|
||||
view[0, 0] = 3
|
||||
view = sliding_window_view(arr, 2, writeable=True)
|
||||
assert_(view.flags.writeable)
|
||||
view[0, 1] = 3
|
||||
assert_array_equal(arr, np.array([0, 3, 2, 3, 4]))
|
||||
|
||||
def test_subok(self):
|
||||
class MyArray(np.ndarray):
|
||||
pass
|
||||
|
||||
arr = np.arange(5).view(MyArray)
|
||||
assert_(not isinstance(sliding_window_view(arr, 2,
|
||||
subok=False),
|
||||
MyArray))
|
||||
assert_(isinstance(sliding_window_view(arr, 2, subok=True), MyArray))
|
||||
# Default behavior
|
||||
assert_(not isinstance(sliding_window_view(arr, 2), MyArray))
|
||||
|
||||
|
||||
def as_strided_writeable():
|
||||
arr = np.ones(10)
|
||||
view = as_strided(arr, writeable=False)
|
||||
assert_(not view.flags.writeable)
|
||||
|
||||
# Check that writeable also is fine:
|
||||
view = as_strided(arr, writeable=True)
|
||||
assert_(view.flags.writeable)
|
||||
view[...] = 3
|
||||
assert_array_equal(arr, np.full_like(arr, 3))
|
||||
|
||||
# Test that things do not break down for readonly:
|
||||
arr.flags.writeable = False
|
||||
view = as_strided(arr, writeable=False)
|
||||
view = as_strided(arr, writeable=True)
|
||||
assert_(not view.flags.writeable)
|
||||
|
||||
|
||||
class VerySimpleSubClass(np.ndarray):
|
||||
def __new__(cls, *args, **kwargs):
|
||||
return np.array(*args, subok=True, **kwargs).view(cls)
|
||||
|
||||
|
||||
class SimpleSubClass(VerySimpleSubClass):
|
||||
def __new__(cls, *args, **kwargs):
|
||||
self = np.array(*args, subok=True, **kwargs).view(cls)
|
||||
self.info = 'simple'
|
||||
return self
|
||||
|
||||
def __array_finalize__(self, obj):
|
||||
self.info = getattr(obj, 'info', '') + ' finalized'
|
||||
|
||||
|
||||
def test_subclasses():
|
||||
# test that subclass is preserved only if subok=True
|
||||
a = VerySimpleSubClass([1, 2, 3, 4])
|
||||
assert_(type(a) is VerySimpleSubClass)
|
||||
a_view = as_strided(a, shape=(2,), strides=(2 * a.itemsize,))
|
||||
assert_(type(a_view) is np.ndarray)
|
||||
a_view = as_strided(a, shape=(2,), strides=(2 * a.itemsize,), subok=True)
|
||||
assert_(type(a_view) is VerySimpleSubClass)
|
||||
# test that if a subclass has __array_finalize__, it is used
|
||||
a = SimpleSubClass([1, 2, 3, 4])
|
||||
a_view = as_strided(a, shape=(2,), strides=(2 * a.itemsize,), subok=True)
|
||||
assert_(type(a_view) is SimpleSubClass)
|
||||
assert_(a_view.info == 'simple finalized')
|
||||
|
||||
# similar tests for broadcast_arrays
|
||||
b = np.arange(len(a)).reshape(-1, 1)
|
||||
a_view, b_view = broadcast_arrays(a, b)
|
||||
assert_(type(a_view) is np.ndarray)
|
||||
assert_(type(b_view) is np.ndarray)
|
||||
assert_(a_view.shape == b_view.shape)
|
||||
a_view, b_view = broadcast_arrays(a, b, subok=True)
|
||||
assert_(type(a_view) is SimpleSubClass)
|
||||
assert_(a_view.info == 'simple finalized')
|
||||
assert_(type(b_view) is np.ndarray)
|
||||
assert_(a_view.shape == b_view.shape)
|
||||
|
||||
# and for broadcast_to
|
||||
shape = (2, 4)
|
||||
a_view = broadcast_to(a, shape)
|
||||
assert_(type(a_view) is np.ndarray)
|
||||
assert_(a_view.shape == shape)
|
||||
a_view = broadcast_to(a, shape, subok=True)
|
||||
assert_(type(a_view) is SimpleSubClass)
|
||||
assert_(a_view.info == 'simple finalized')
|
||||
assert_(a_view.shape == shape)
|
||||
|
||||
|
||||
def test_writeable():
|
||||
# broadcast_to should return a readonly array
|
||||
original = np.array([1, 2, 3])
|
||||
result = broadcast_to(original, (2, 3))
|
||||
assert_equal(result.flags.writeable, False)
|
||||
assert_raises(ValueError, result.__setitem__, slice(None), 0)
|
||||
|
||||
# but the result of broadcast_arrays needs to be writeable, to
|
||||
# preserve backwards compatibility
|
||||
test_cases = [((False,), broadcast_arrays(original,)),
|
||||
((True, False), broadcast_arrays(0, original))]
|
||||
for is_broadcast, results in test_cases:
|
||||
for array_is_broadcast, result in zip(is_broadcast, results):
|
||||
# This will change to False in a future version
|
||||
if array_is_broadcast:
|
||||
with assert_warns(FutureWarning):
|
||||
assert_equal(result.flags.writeable, True)
|
||||
with assert_warns(DeprecationWarning):
|
||||
result[:] = 0
|
||||
# Warning not emitted, writing to the array resets it
|
||||
assert_equal(result.flags.writeable, True)
|
||||
else:
|
||||
# No warning:
|
||||
assert_equal(result.flags.writeable, True)
|
||||
|
||||
for results in [broadcast_arrays(original),
|
||||
broadcast_arrays(0, original)]:
|
||||
for result in results:
|
||||
# resets the warn_on_write DeprecationWarning
|
||||
result.flags.writeable = True
|
||||
# check: no warning emitted
|
||||
assert_equal(result.flags.writeable, True)
|
||||
result[:] = 0
|
||||
|
||||
# keep readonly input readonly
|
||||
original.flags.writeable = False
|
||||
_, result = broadcast_arrays(0, original)
|
||||
assert_equal(result.flags.writeable, False)
|
||||
|
||||
# regression test for GH6491
|
||||
shape = (2,)
|
||||
strides = [0]
|
||||
tricky_array = as_strided(np.array(0), shape, strides)
|
||||
other = np.zeros((1,))
|
||||
first, second = broadcast_arrays(tricky_array, other)
|
||||
assert_(first.shape == second.shape)
|
||||
|
||||
|
||||
def test_writeable_memoryview():
|
||||
# The result of broadcast_arrays exports as a non-writeable memoryview
|
||||
# because otherwise there is no good way to opt in to the new behaviour
|
||||
# (i.e. you would need to set writeable to False explicitly).
|
||||
# See gh-13929.
|
||||
original = np.array([1, 2, 3])
|
||||
|
||||
test_cases = [((False, ), broadcast_arrays(original,)),
|
||||
((True, False), broadcast_arrays(0, original))]
|
||||
for is_broadcast, results in test_cases:
|
||||
for array_is_broadcast, result in zip(is_broadcast, results):
|
||||
# This will change to False in a future version
|
||||
if array_is_broadcast:
|
||||
# memoryview(result, writable=True) will give warning but cannot
|
||||
# be tested using the python API.
|
||||
assert memoryview(result).readonly
|
||||
else:
|
||||
assert not memoryview(result).readonly
|
||||
|
||||
|
||||
def test_reference_types():
|
||||
input_array = np.array('a', dtype=object)
|
||||
expected = np.array(['a'] * 3, dtype=object)
|
||||
actual = broadcast_to(input_array, (3,))
|
||||
assert_array_equal(expected, actual)
|
||||
|
||||
actual, _ = broadcast_arrays(input_array, np.ones(3))
|
||||
assert_array_equal(expected, actual)
|
||||
541
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_twodim_base.py
vendored
Normal file
541
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_twodim_base.py
vendored
Normal file
@@ -0,0 +1,541 @@
|
||||
"""Test functions for matrix module
|
||||
|
||||
"""
|
||||
from numpy.testing import (
|
||||
assert_equal, assert_array_equal, assert_array_max_ulp,
|
||||
assert_array_almost_equal, assert_raises, assert_
|
||||
)
|
||||
from numpy import (
|
||||
arange, add, fliplr, flipud, zeros, ones, eye, array, diag, histogram2d,
|
||||
tri, mask_indices, triu_indices, triu_indices_from, tril_indices,
|
||||
tril_indices_from, vander,
|
||||
)
|
||||
import numpy as np
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def get_mat(n):
|
||||
data = arange(n)
|
||||
data = add.outer(data, data)
|
||||
return data
|
||||
|
||||
|
||||
class TestEye:
|
||||
def test_basic(self):
|
||||
assert_equal(eye(4),
|
||||
array([[1, 0, 0, 0],
|
||||
[0, 1, 0, 0],
|
||||
[0, 0, 1, 0],
|
||||
[0, 0, 0, 1]]))
|
||||
|
||||
assert_equal(eye(4, dtype='f'),
|
||||
array([[1, 0, 0, 0],
|
||||
[0, 1, 0, 0],
|
||||
[0, 0, 1, 0],
|
||||
[0, 0, 0, 1]], 'f'))
|
||||
|
||||
assert_equal(eye(3) == 1,
|
||||
eye(3, dtype=bool))
|
||||
|
||||
def test_uint64(self):
|
||||
# Regression test for gh-9982
|
||||
assert_equal(eye(np.uint64(2), dtype=int), array([[1, 0], [0, 1]]))
|
||||
assert_equal(eye(np.uint64(2), M=np.uint64(4), k=np.uint64(1)),
|
||||
array([[0, 1, 0, 0], [0, 0, 1, 0]]))
|
||||
|
||||
def test_diag(self):
|
||||
assert_equal(eye(4, k=1),
|
||||
array([[0, 1, 0, 0],
|
||||
[0, 0, 1, 0],
|
||||
[0, 0, 0, 1],
|
||||
[0, 0, 0, 0]]))
|
||||
|
||||
assert_equal(eye(4, k=-1),
|
||||
array([[0, 0, 0, 0],
|
||||
[1, 0, 0, 0],
|
||||
[0, 1, 0, 0],
|
||||
[0, 0, 1, 0]]))
|
||||
|
||||
def test_2d(self):
|
||||
assert_equal(eye(4, 3),
|
||||
array([[1, 0, 0],
|
||||
[0, 1, 0],
|
||||
[0, 0, 1],
|
||||
[0, 0, 0]]))
|
||||
|
||||
assert_equal(eye(3, 4),
|
||||
array([[1, 0, 0, 0],
|
||||
[0, 1, 0, 0],
|
||||
[0, 0, 1, 0]]))
|
||||
|
||||
def test_diag2d(self):
|
||||
assert_equal(eye(3, 4, k=2),
|
||||
array([[0, 0, 1, 0],
|
||||
[0, 0, 0, 1],
|
||||
[0, 0, 0, 0]]))
|
||||
|
||||
assert_equal(eye(4, 3, k=-2),
|
||||
array([[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
[1, 0, 0],
|
||||
[0, 1, 0]]))
|
||||
|
||||
def test_eye_bounds(self):
|
||||
assert_equal(eye(2, 2, 1), [[0, 1], [0, 0]])
|
||||
assert_equal(eye(2, 2, -1), [[0, 0], [1, 0]])
|
||||
assert_equal(eye(2, 2, 2), [[0, 0], [0, 0]])
|
||||
assert_equal(eye(2, 2, -2), [[0, 0], [0, 0]])
|
||||
assert_equal(eye(3, 2, 2), [[0, 0], [0, 0], [0, 0]])
|
||||
assert_equal(eye(3, 2, 1), [[0, 1], [0, 0], [0, 0]])
|
||||
assert_equal(eye(3, 2, -1), [[0, 0], [1, 0], [0, 1]])
|
||||
assert_equal(eye(3, 2, -2), [[0, 0], [0, 0], [1, 0]])
|
||||
assert_equal(eye(3, 2, -3), [[0, 0], [0, 0], [0, 0]])
|
||||
|
||||
def test_strings(self):
|
||||
assert_equal(eye(2, 2, dtype='S3'),
|
||||
[[b'1', b''], [b'', b'1']])
|
||||
|
||||
def test_bool(self):
|
||||
assert_equal(eye(2, 2, dtype=bool), [[True, False], [False, True]])
|
||||
|
||||
def test_order(self):
|
||||
mat_c = eye(4, 3, k=-1)
|
||||
mat_f = eye(4, 3, k=-1, order='F')
|
||||
assert_equal(mat_c, mat_f)
|
||||
assert mat_c.flags.c_contiguous
|
||||
assert not mat_c.flags.f_contiguous
|
||||
assert not mat_f.flags.c_contiguous
|
||||
assert mat_f.flags.f_contiguous
|
||||
|
||||
|
||||
class TestDiag:
|
||||
def test_vector(self):
|
||||
vals = (100 * arange(5)).astype('l')
|
||||
b = zeros((5, 5))
|
||||
for k in range(5):
|
||||
b[k, k] = vals[k]
|
||||
assert_equal(diag(vals), b)
|
||||
b = zeros((7, 7))
|
||||
c = b.copy()
|
||||
for k in range(5):
|
||||
b[k, k + 2] = vals[k]
|
||||
c[k + 2, k] = vals[k]
|
||||
assert_equal(diag(vals, k=2), b)
|
||||
assert_equal(diag(vals, k=-2), c)
|
||||
|
||||
def test_matrix(self, vals=None):
|
||||
if vals is None:
|
||||
vals = (100 * get_mat(5) + 1).astype('l')
|
||||
b = zeros((5,))
|
||||
for k in range(5):
|
||||
b[k] = vals[k, k]
|
||||
assert_equal(diag(vals), b)
|
||||
b = b * 0
|
||||
for k in range(3):
|
||||
b[k] = vals[k, k + 2]
|
||||
assert_equal(diag(vals, 2), b[:3])
|
||||
for k in range(3):
|
||||
b[k] = vals[k + 2, k]
|
||||
assert_equal(diag(vals, -2), b[:3])
|
||||
|
||||
def test_fortran_order(self):
|
||||
vals = array((100 * get_mat(5) + 1), order='F', dtype='l')
|
||||
self.test_matrix(vals)
|
||||
|
||||
def test_diag_bounds(self):
|
||||
A = [[1, 2], [3, 4], [5, 6]]
|
||||
assert_equal(diag(A, k=2), [])
|
||||
assert_equal(diag(A, k=1), [2])
|
||||
assert_equal(diag(A, k=0), [1, 4])
|
||||
assert_equal(diag(A, k=-1), [3, 6])
|
||||
assert_equal(diag(A, k=-2), [5])
|
||||
assert_equal(diag(A, k=-3), [])
|
||||
|
||||
def test_failure(self):
|
||||
assert_raises(ValueError, diag, [[[1]]])
|
||||
|
||||
|
||||
class TestFliplr:
|
||||
def test_basic(self):
|
||||
assert_raises(ValueError, fliplr, ones(4))
|
||||
a = get_mat(4)
|
||||
b = a[:, ::-1]
|
||||
assert_equal(fliplr(a), b)
|
||||
a = [[0, 1, 2],
|
||||
[3, 4, 5]]
|
||||
b = [[2, 1, 0],
|
||||
[5, 4, 3]]
|
||||
assert_equal(fliplr(a), b)
|
||||
|
||||
|
||||
class TestFlipud:
|
||||
def test_basic(self):
|
||||
a = get_mat(4)
|
||||
b = a[::-1, :]
|
||||
assert_equal(flipud(a), b)
|
||||
a = [[0, 1, 2],
|
||||
[3, 4, 5]]
|
||||
b = [[3, 4, 5],
|
||||
[0, 1, 2]]
|
||||
assert_equal(flipud(a), b)
|
||||
|
||||
|
||||
class TestHistogram2d:
|
||||
def test_simple(self):
|
||||
x = array(
|
||||
[0.41702200, 0.72032449, 1.1437481e-4, 0.302332573, 0.146755891])
|
||||
y = array(
|
||||
[0.09233859, 0.18626021, 0.34556073, 0.39676747, 0.53881673])
|
||||
xedges = np.linspace(0, 1, 10)
|
||||
yedges = np.linspace(0, 1, 10)
|
||||
H = histogram2d(x, y, (xedges, yedges))[0]
|
||||
answer = array(
|
||||
[[0, 0, 0, 1, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 1, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[1, 0, 1, 0, 0, 0, 0, 0, 0],
|
||||
[0, 1, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0]])
|
||||
assert_array_equal(H.T, answer)
|
||||
H = histogram2d(x, y, xedges)[0]
|
||||
assert_array_equal(H.T, answer)
|
||||
H, xedges, yedges = histogram2d(list(range(10)), list(range(10)))
|
||||
assert_array_equal(H, eye(10, 10))
|
||||
assert_array_equal(xedges, np.linspace(0, 9, 11))
|
||||
assert_array_equal(yedges, np.linspace(0, 9, 11))
|
||||
|
||||
def test_asym(self):
|
||||
x = array([1, 1, 2, 3, 4, 4, 4, 5])
|
||||
y = array([1, 3, 2, 0, 1, 2, 3, 4])
|
||||
H, xed, yed = histogram2d(
|
||||
x, y, (6, 5), range=[[0, 6], [0, 5]], density=True)
|
||||
answer = array(
|
||||
[[0., 0, 0, 0, 0],
|
||||
[0, 1, 0, 1, 0],
|
||||
[0, 0, 1, 0, 0],
|
||||
[1, 0, 0, 0, 0],
|
||||
[0, 1, 1, 1, 0],
|
||||
[0, 0, 0, 0, 1]])
|
||||
assert_array_almost_equal(H, answer/8., 3)
|
||||
assert_array_equal(xed, np.linspace(0, 6, 7))
|
||||
assert_array_equal(yed, np.linspace(0, 5, 6))
|
||||
|
||||
def test_density(self):
|
||||
x = array([1, 2, 3, 1, 2, 3, 1, 2, 3])
|
||||
y = array([1, 1, 1, 2, 2, 2, 3, 3, 3])
|
||||
H, xed, yed = histogram2d(
|
||||
x, y, [[1, 2, 3, 5], [1, 2, 3, 5]], density=True)
|
||||
answer = array([[1, 1, .5],
|
||||
[1, 1, .5],
|
||||
[.5, .5, .25]])/9.
|
||||
assert_array_almost_equal(H, answer, 3)
|
||||
|
||||
def test_all_outliers(self):
|
||||
r = np.random.rand(100) + 1. + 1e6 # histogramdd rounds by decimal=6
|
||||
H, xed, yed = histogram2d(r, r, (4, 5), range=([0, 1], [0, 1]))
|
||||
assert_array_equal(H, 0)
|
||||
|
||||
def test_empty(self):
|
||||
a, edge1, edge2 = histogram2d([], [], bins=([0, 1], [0, 1]))
|
||||
assert_array_max_ulp(a, array([[0.]]))
|
||||
|
||||
a, edge1, edge2 = histogram2d([], [], bins=4)
|
||||
assert_array_max_ulp(a, np.zeros((4, 4)))
|
||||
|
||||
def test_binparameter_combination(self):
|
||||
x = array(
|
||||
[0, 0.09207008, 0.64575234, 0.12875982, 0.47390599,
|
||||
0.59944483, 1])
|
||||
y = array(
|
||||
[0, 0.14344267, 0.48988575, 0.30558665, 0.44700682,
|
||||
0.15886423, 1])
|
||||
edges = (0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)
|
||||
H, xe, ye = histogram2d(x, y, (edges, 4))
|
||||
answer = array(
|
||||
[[2., 0., 0., 0.],
|
||||
[0., 1., 0., 0.],
|
||||
[0., 0., 0., 0.],
|
||||
[0., 0., 0., 0.],
|
||||
[0., 1., 0., 0.],
|
||||
[1., 0., 0., 0.],
|
||||
[0., 1., 0., 0.],
|
||||
[0., 0., 0., 0.],
|
||||
[0., 0., 0., 0.],
|
||||
[0., 0., 0., 1.]])
|
||||
assert_array_equal(H, answer)
|
||||
assert_array_equal(ye, array([0., 0.25, 0.5, 0.75, 1]))
|
||||
H, xe, ye = histogram2d(x, y, (4, edges))
|
||||
answer = array(
|
||||
[[1., 1., 0., 1., 0., 0., 0., 0., 0., 0.],
|
||||
[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
|
||||
[0., 1., 0., 0., 1., 0., 0., 0., 0., 0.],
|
||||
[0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])
|
||||
assert_array_equal(H, answer)
|
||||
assert_array_equal(xe, array([0., 0.25, 0.5, 0.75, 1]))
|
||||
|
||||
def test_dispatch(self):
|
||||
class ShouldDispatch:
|
||||
def __array_function__(self, function, types, args, kwargs):
|
||||
return types, args, kwargs
|
||||
|
||||
xy = [1, 2]
|
||||
s_d = ShouldDispatch()
|
||||
r = histogram2d(s_d, xy)
|
||||
# Cannot use assert_equal since that dispatches...
|
||||
assert_(r == ((ShouldDispatch,), (s_d, xy), {}))
|
||||
r = histogram2d(xy, s_d)
|
||||
assert_(r == ((ShouldDispatch,), (xy, s_d), {}))
|
||||
r = histogram2d(xy, xy, bins=s_d)
|
||||
assert_(r, ((ShouldDispatch,), (xy, xy), dict(bins=s_d)))
|
||||
r = histogram2d(xy, xy, bins=[s_d, 5])
|
||||
assert_(r, ((ShouldDispatch,), (xy, xy), dict(bins=[s_d, 5])))
|
||||
assert_raises(Exception, histogram2d, xy, xy, bins=[s_d])
|
||||
r = histogram2d(xy, xy, weights=s_d)
|
||||
assert_(r, ((ShouldDispatch,), (xy, xy), dict(weights=s_d)))
|
||||
|
||||
@pytest.mark.parametrize(("x_len", "y_len"), [(10, 11), (20, 19)])
|
||||
def test_bad_length(self, x_len, y_len):
|
||||
x, y = np.ones(x_len), np.ones(y_len)
|
||||
with pytest.raises(ValueError,
|
||||
match='x and y must have the same length.'):
|
||||
histogram2d(x, y)
|
||||
|
||||
|
||||
class TestTri:
|
||||
def test_dtype(self):
|
||||
out = array([[1, 0, 0],
|
||||
[1, 1, 0],
|
||||
[1, 1, 1]])
|
||||
assert_array_equal(tri(3), out)
|
||||
assert_array_equal(tri(3, dtype=bool), out.astype(bool))
|
||||
|
||||
|
||||
def test_tril_triu_ndim2():
|
||||
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
|
||||
a = np.ones((2, 2), dtype=dtype)
|
||||
b = np.tril(a)
|
||||
c = np.triu(a)
|
||||
assert_array_equal(b, [[1, 0], [1, 1]])
|
||||
assert_array_equal(c, b.T)
|
||||
# should return the same dtype as the original array
|
||||
assert_equal(b.dtype, a.dtype)
|
||||
assert_equal(c.dtype, a.dtype)
|
||||
|
||||
|
||||
def test_tril_triu_ndim3():
|
||||
for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']:
|
||||
a = np.array([
|
||||
[[1, 1], [1, 1]],
|
||||
[[1, 1], [1, 0]],
|
||||
[[1, 1], [0, 0]],
|
||||
], dtype=dtype)
|
||||
a_tril_desired = np.array([
|
||||
[[1, 0], [1, 1]],
|
||||
[[1, 0], [1, 0]],
|
||||
[[1, 0], [0, 0]],
|
||||
], dtype=dtype)
|
||||
a_triu_desired = np.array([
|
||||
[[1, 1], [0, 1]],
|
||||
[[1, 1], [0, 0]],
|
||||
[[1, 1], [0, 0]],
|
||||
], dtype=dtype)
|
||||
a_triu_observed = np.triu(a)
|
||||
a_tril_observed = np.tril(a)
|
||||
assert_array_equal(a_triu_observed, a_triu_desired)
|
||||
assert_array_equal(a_tril_observed, a_tril_desired)
|
||||
assert_equal(a_triu_observed.dtype, a.dtype)
|
||||
assert_equal(a_tril_observed.dtype, a.dtype)
|
||||
|
||||
|
||||
def test_tril_triu_with_inf():
|
||||
# Issue 4859
|
||||
arr = np.array([[1, 1, np.inf],
|
||||
[1, 1, 1],
|
||||
[np.inf, 1, 1]])
|
||||
out_tril = np.array([[1, 0, 0],
|
||||
[1, 1, 0],
|
||||
[np.inf, 1, 1]])
|
||||
out_triu = out_tril.T
|
||||
assert_array_equal(np.triu(arr), out_triu)
|
||||
assert_array_equal(np.tril(arr), out_tril)
|
||||
|
||||
|
||||
def test_tril_triu_dtype():
|
||||
# Issue 4916
|
||||
# tril and triu should return the same dtype as input
|
||||
for c in np.typecodes['All']:
|
||||
if c == 'V':
|
||||
continue
|
||||
arr = np.zeros((3, 3), dtype=c)
|
||||
assert_equal(np.triu(arr).dtype, arr.dtype)
|
||||
assert_equal(np.tril(arr).dtype, arr.dtype)
|
||||
|
||||
# check special cases
|
||||
arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'],
|
||||
['2004-01-01T12:00', '2003-01-03T13:45']],
|
||||
dtype='datetime64')
|
||||
assert_equal(np.triu(arr).dtype, arr.dtype)
|
||||
assert_equal(np.tril(arr).dtype, arr.dtype)
|
||||
|
||||
arr = np.zeros((3, 3), dtype='f4,f4')
|
||||
assert_equal(np.triu(arr).dtype, arr.dtype)
|
||||
assert_equal(np.tril(arr).dtype, arr.dtype)
|
||||
|
||||
|
||||
def test_mask_indices():
|
||||
# simple test without offset
|
||||
iu = mask_indices(3, np.triu)
|
||||
a = np.arange(9).reshape(3, 3)
|
||||
assert_array_equal(a[iu], array([0, 1, 2, 4, 5, 8]))
|
||||
# Now with an offset
|
||||
iu1 = mask_indices(3, np.triu, 1)
|
||||
assert_array_equal(a[iu1], array([1, 2, 5]))
|
||||
|
||||
|
||||
def test_tril_indices():
|
||||
# indices without and with offset
|
||||
il1 = tril_indices(4)
|
||||
il2 = tril_indices(4, k=2)
|
||||
il3 = tril_indices(4, m=5)
|
||||
il4 = tril_indices(4, k=2, m=5)
|
||||
|
||||
a = np.array([[1, 2, 3, 4],
|
||||
[5, 6, 7, 8],
|
||||
[9, 10, 11, 12],
|
||||
[13, 14, 15, 16]])
|
||||
b = np.arange(1, 21).reshape(4, 5)
|
||||
|
||||
# indexing:
|
||||
assert_array_equal(a[il1],
|
||||
array([1, 5, 6, 9, 10, 11, 13, 14, 15, 16]))
|
||||
assert_array_equal(b[il3],
|
||||
array([1, 6, 7, 11, 12, 13, 16, 17, 18, 19]))
|
||||
|
||||
# And for assigning values:
|
||||
a[il1] = -1
|
||||
assert_array_equal(a,
|
||||
array([[-1, 2, 3, 4],
|
||||
[-1, -1, 7, 8],
|
||||
[-1, -1, -1, 12],
|
||||
[-1, -1, -1, -1]]))
|
||||
b[il3] = -1
|
||||
assert_array_equal(b,
|
||||
array([[-1, 2, 3, 4, 5],
|
||||
[-1, -1, 8, 9, 10],
|
||||
[-1, -1, -1, 14, 15],
|
||||
[-1, -1, -1, -1, 20]]))
|
||||
# These cover almost the whole array (two diagonals right of the main one):
|
||||
a[il2] = -10
|
||||
assert_array_equal(a,
|
||||
array([[-10, -10, -10, 4],
|
||||
[-10, -10, -10, -10],
|
||||
[-10, -10, -10, -10],
|
||||
[-10, -10, -10, -10]]))
|
||||
b[il4] = -10
|
||||
assert_array_equal(b,
|
||||
array([[-10, -10, -10, 4, 5],
|
||||
[-10, -10, -10, -10, 10],
|
||||
[-10, -10, -10, -10, -10],
|
||||
[-10, -10, -10, -10, -10]]))
|
||||
|
||||
|
||||
class TestTriuIndices:
|
||||
def test_triu_indices(self):
|
||||
iu1 = triu_indices(4)
|
||||
iu2 = triu_indices(4, k=2)
|
||||
iu3 = triu_indices(4, m=5)
|
||||
iu4 = triu_indices(4, k=2, m=5)
|
||||
|
||||
a = np.array([[1, 2, 3, 4],
|
||||
[5, 6, 7, 8],
|
||||
[9, 10, 11, 12],
|
||||
[13, 14, 15, 16]])
|
||||
b = np.arange(1, 21).reshape(4, 5)
|
||||
|
||||
# Both for indexing:
|
||||
assert_array_equal(a[iu1],
|
||||
array([1, 2, 3, 4, 6, 7, 8, 11, 12, 16]))
|
||||
assert_array_equal(b[iu3],
|
||||
array([1, 2, 3, 4, 5, 7, 8, 9,
|
||||
10, 13, 14, 15, 19, 20]))
|
||||
|
||||
# And for assigning values:
|
||||
a[iu1] = -1
|
||||
assert_array_equal(a,
|
||||
array([[-1, -1, -1, -1],
|
||||
[5, -1, -1, -1],
|
||||
[9, 10, -1, -1],
|
||||
[13, 14, 15, -1]]))
|
||||
b[iu3] = -1
|
||||
assert_array_equal(b,
|
||||
array([[-1, -1, -1, -1, -1],
|
||||
[6, -1, -1, -1, -1],
|
||||
[11, 12, -1, -1, -1],
|
||||
[16, 17, 18, -1, -1]]))
|
||||
|
||||
# These cover almost the whole array (two diagonals right of the
|
||||
# main one):
|
||||
a[iu2] = -10
|
||||
assert_array_equal(a,
|
||||
array([[-1, -1, -10, -10],
|
||||
[5, -1, -1, -10],
|
||||
[9, 10, -1, -1],
|
||||
[13, 14, 15, -1]]))
|
||||
b[iu4] = -10
|
||||
assert_array_equal(b,
|
||||
array([[-1, -1, -10, -10, -10],
|
||||
[6, -1, -1, -10, -10],
|
||||
[11, 12, -1, -1, -10],
|
||||
[16, 17, 18, -1, -1]]))
|
||||
|
||||
|
||||
class TestTrilIndicesFrom:
|
||||
def test_exceptions(self):
|
||||
assert_raises(ValueError, tril_indices_from, np.ones((2,)))
|
||||
assert_raises(ValueError, tril_indices_from, np.ones((2, 2, 2)))
|
||||
# assert_raises(ValueError, tril_indices_from, np.ones((2, 3)))
|
||||
|
||||
|
||||
class TestTriuIndicesFrom:
|
||||
def test_exceptions(self):
|
||||
assert_raises(ValueError, triu_indices_from, np.ones((2,)))
|
||||
assert_raises(ValueError, triu_indices_from, np.ones((2, 2, 2)))
|
||||
# assert_raises(ValueError, triu_indices_from, np.ones((2, 3)))
|
||||
|
||||
|
||||
class TestVander:
|
||||
def test_basic(self):
|
||||
c = np.array([0, 1, -2, 3])
|
||||
v = vander(c)
|
||||
powers = np.array([[0, 0, 0, 0, 1],
|
||||
[1, 1, 1, 1, 1],
|
||||
[16, -8, 4, -2, 1],
|
||||
[81, 27, 9, 3, 1]])
|
||||
# Check default value of N:
|
||||
assert_array_equal(v, powers[:, 1:])
|
||||
# Check a range of N values, including 0 and 5 (greater than default)
|
||||
m = powers.shape[1]
|
||||
for n in range(6):
|
||||
v = vander(c, N=n)
|
||||
assert_array_equal(v, powers[:, m-n:m])
|
||||
|
||||
def test_dtypes(self):
|
||||
c = array([11, -12, 13], dtype=np.int8)
|
||||
v = vander(c)
|
||||
expected = np.array([[121, 11, 1],
|
||||
[144, -12, 1],
|
||||
[169, 13, 1]])
|
||||
assert_array_equal(v, expected)
|
||||
|
||||
c = array([1.0+1j, 1.0-1j])
|
||||
v = vander(c, N=3)
|
||||
expected = np.array([[2j, 1+1j, 1],
|
||||
[-2j, 1-1j, 1]])
|
||||
# The data is floating point, but the values are small integers,
|
||||
# so assert_array_equal *should* be safe here (rather than, say,
|
||||
# assert_array_almost_equal).
|
||||
assert_array_equal(v, expected)
|
||||
465
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_type_check.py
vendored
Normal file
465
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_type_check.py
vendored
Normal file
@@ -0,0 +1,465 @@
|
||||
import numpy as np
|
||||
from numpy import (
|
||||
common_type, mintypecode, isreal, iscomplex, isposinf, isneginf,
|
||||
nan_to_num, isrealobj, iscomplexobj, real_if_close
|
||||
)
|
||||
from numpy.testing import (
|
||||
assert_, assert_equal, assert_array_equal, assert_raises
|
||||
)
|
||||
|
||||
|
||||
def assert_all(x):
|
||||
assert_(np.all(x), x)
|
||||
|
||||
|
||||
class TestCommonType:
|
||||
def test_basic(self):
|
||||
ai32 = np.array([[1, 2], [3, 4]], dtype=np.int32)
|
||||
af16 = np.array([[1, 2], [3, 4]], dtype=np.float16)
|
||||
af32 = np.array([[1, 2], [3, 4]], dtype=np.float32)
|
||||
af64 = np.array([[1, 2], [3, 4]], dtype=np.float64)
|
||||
acs = np.array([[1+5j, 2+6j], [3+7j, 4+8j]], dtype=np.complex64)
|
||||
acd = np.array([[1+5j, 2+6j], [3+7j, 4+8j]], dtype=np.complex128)
|
||||
assert_(common_type(ai32) == np.float64)
|
||||
assert_(common_type(af16) == np.float16)
|
||||
assert_(common_type(af32) == np.float32)
|
||||
assert_(common_type(af64) == np.float64)
|
||||
assert_(common_type(acs) == np.complex64)
|
||||
assert_(common_type(acd) == np.complex128)
|
||||
|
||||
|
||||
class TestMintypecode:
|
||||
|
||||
def test_default_1(self):
|
||||
for itype in '1bcsuwil':
|
||||
assert_equal(mintypecode(itype), 'd')
|
||||
assert_equal(mintypecode('f'), 'f')
|
||||
assert_equal(mintypecode('d'), 'd')
|
||||
assert_equal(mintypecode('F'), 'F')
|
||||
assert_equal(mintypecode('D'), 'D')
|
||||
|
||||
def test_default_2(self):
|
||||
for itype in '1bcsuwil':
|
||||
assert_equal(mintypecode(itype+'f'), 'f')
|
||||
assert_equal(mintypecode(itype+'d'), 'd')
|
||||
assert_equal(mintypecode(itype+'F'), 'F')
|
||||
assert_equal(mintypecode(itype+'D'), 'D')
|
||||
assert_equal(mintypecode('ff'), 'f')
|
||||
assert_equal(mintypecode('fd'), 'd')
|
||||
assert_equal(mintypecode('fF'), 'F')
|
||||
assert_equal(mintypecode('fD'), 'D')
|
||||
assert_equal(mintypecode('df'), 'd')
|
||||
assert_equal(mintypecode('dd'), 'd')
|
||||
#assert_equal(mintypecode('dF',savespace=1),'F')
|
||||
assert_equal(mintypecode('dF'), 'D')
|
||||
assert_equal(mintypecode('dD'), 'D')
|
||||
assert_equal(mintypecode('Ff'), 'F')
|
||||
#assert_equal(mintypecode('Fd',savespace=1),'F')
|
||||
assert_equal(mintypecode('Fd'), 'D')
|
||||
assert_equal(mintypecode('FF'), 'F')
|
||||
assert_equal(mintypecode('FD'), 'D')
|
||||
assert_equal(mintypecode('Df'), 'D')
|
||||
assert_equal(mintypecode('Dd'), 'D')
|
||||
assert_equal(mintypecode('DF'), 'D')
|
||||
assert_equal(mintypecode('DD'), 'D')
|
||||
|
||||
def test_default_3(self):
|
||||
assert_equal(mintypecode('fdF'), 'D')
|
||||
#assert_equal(mintypecode('fdF',savespace=1),'F')
|
||||
assert_equal(mintypecode('fdD'), 'D')
|
||||
assert_equal(mintypecode('fFD'), 'D')
|
||||
assert_equal(mintypecode('dFD'), 'D')
|
||||
|
||||
assert_equal(mintypecode('ifd'), 'd')
|
||||
assert_equal(mintypecode('ifF'), 'F')
|
||||
assert_equal(mintypecode('ifD'), 'D')
|
||||
assert_equal(mintypecode('idF'), 'D')
|
||||
#assert_equal(mintypecode('idF',savespace=1),'F')
|
||||
assert_equal(mintypecode('idD'), 'D')
|
||||
|
||||
|
||||
class TestIsscalar:
|
||||
|
||||
def test_basic(self):
|
||||
assert_(np.isscalar(3))
|
||||
assert_(not np.isscalar([3]))
|
||||
assert_(not np.isscalar((3,)))
|
||||
assert_(np.isscalar(3j))
|
||||
assert_(np.isscalar(4.0))
|
||||
|
||||
|
||||
class TestReal:
|
||||
|
||||
def test_real(self):
|
||||
y = np.random.rand(10,)
|
||||
assert_array_equal(y, np.real(y))
|
||||
|
||||
y = np.array(1)
|
||||
out = np.real(y)
|
||||
assert_array_equal(y, out)
|
||||
assert_(isinstance(out, np.ndarray))
|
||||
|
||||
y = 1
|
||||
out = np.real(y)
|
||||
assert_equal(y, out)
|
||||
assert_(not isinstance(out, np.ndarray))
|
||||
|
||||
def test_cmplx(self):
|
||||
y = np.random.rand(10,)+1j*np.random.rand(10,)
|
||||
assert_array_equal(y.real, np.real(y))
|
||||
|
||||
y = np.array(1 + 1j)
|
||||
out = np.real(y)
|
||||
assert_array_equal(y.real, out)
|
||||
assert_(isinstance(out, np.ndarray))
|
||||
|
||||
y = 1 + 1j
|
||||
out = np.real(y)
|
||||
assert_equal(1.0, out)
|
||||
assert_(not isinstance(out, np.ndarray))
|
||||
|
||||
|
||||
class TestImag:
|
||||
|
||||
def test_real(self):
|
||||
y = np.random.rand(10,)
|
||||
assert_array_equal(0, np.imag(y))
|
||||
|
||||
y = np.array(1)
|
||||
out = np.imag(y)
|
||||
assert_array_equal(0, out)
|
||||
assert_(isinstance(out, np.ndarray))
|
||||
|
||||
y = 1
|
||||
out = np.imag(y)
|
||||
assert_equal(0, out)
|
||||
assert_(not isinstance(out, np.ndarray))
|
||||
|
||||
def test_cmplx(self):
|
||||
y = np.random.rand(10,)+1j*np.random.rand(10,)
|
||||
assert_array_equal(y.imag, np.imag(y))
|
||||
|
||||
y = np.array(1 + 1j)
|
||||
out = np.imag(y)
|
||||
assert_array_equal(y.imag, out)
|
||||
assert_(isinstance(out, np.ndarray))
|
||||
|
||||
y = 1 + 1j
|
||||
out = np.imag(y)
|
||||
assert_equal(1.0, out)
|
||||
assert_(not isinstance(out, np.ndarray))
|
||||
|
||||
|
||||
class TestIscomplex:
|
||||
|
||||
def test_fail(self):
|
||||
z = np.array([-1, 0, 1])
|
||||
res = iscomplex(z)
|
||||
assert_(not np.any(res, axis=0))
|
||||
|
||||
def test_pass(self):
|
||||
z = np.array([-1j, 1, 0])
|
||||
res = iscomplex(z)
|
||||
assert_array_equal(res, [1, 0, 0])
|
||||
|
||||
|
||||
class TestIsreal:
|
||||
|
||||
def test_pass(self):
|
||||
z = np.array([-1, 0, 1j])
|
||||
res = isreal(z)
|
||||
assert_array_equal(res, [1, 1, 0])
|
||||
|
||||
def test_fail(self):
|
||||
z = np.array([-1j, 1, 0])
|
||||
res = isreal(z)
|
||||
assert_array_equal(res, [0, 1, 1])
|
||||
|
||||
|
||||
class TestIscomplexobj:
|
||||
|
||||
def test_basic(self):
|
||||
z = np.array([-1, 0, 1])
|
||||
assert_(not iscomplexobj(z))
|
||||
z = np.array([-1j, 0, -1])
|
||||
assert_(iscomplexobj(z))
|
||||
|
||||
def test_scalar(self):
|
||||
assert_(not iscomplexobj(1.0))
|
||||
assert_(iscomplexobj(1+0j))
|
||||
|
||||
def test_list(self):
|
||||
assert_(iscomplexobj([3, 1+0j, True]))
|
||||
assert_(not iscomplexobj([3, 1, True]))
|
||||
|
||||
def test_duck(self):
|
||||
class DummyComplexArray:
|
||||
@property
|
||||
def dtype(self):
|
||||
return np.dtype(complex)
|
||||
dummy = DummyComplexArray()
|
||||
assert_(iscomplexobj(dummy))
|
||||
|
||||
def test_pandas_duck(self):
|
||||
# This tests a custom np.dtype duck-typed class, such as used by pandas
|
||||
# (pandas.core.dtypes)
|
||||
class PdComplex(np.complex128):
|
||||
pass
|
||||
class PdDtype:
|
||||
name = 'category'
|
||||
names = None
|
||||
type = PdComplex
|
||||
kind = 'c'
|
||||
str = '<c16'
|
||||
base = np.dtype('complex128')
|
||||
class DummyPd:
|
||||
@property
|
||||
def dtype(self):
|
||||
return PdDtype
|
||||
dummy = DummyPd()
|
||||
assert_(iscomplexobj(dummy))
|
||||
|
||||
def test_custom_dtype_duck(self):
|
||||
class MyArray(list):
|
||||
@property
|
||||
def dtype(self):
|
||||
return complex
|
||||
|
||||
a = MyArray([1+0j, 2+0j, 3+0j])
|
||||
assert_(iscomplexobj(a))
|
||||
|
||||
|
||||
class TestIsrealobj:
|
||||
def test_basic(self):
|
||||
z = np.array([-1, 0, 1])
|
||||
assert_(isrealobj(z))
|
||||
z = np.array([-1j, 0, -1])
|
||||
assert_(not isrealobj(z))
|
||||
|
||||
|
||||
class TestIsnan:
|
||||
|
||||
def test_goodvalues(self):
|
||||
z = np.array((-1., 0., 1.))
|
||||
res = np.isnan(z) == 0
|
||||
assert_all(np.all(res, axis=0))
|
||||
|
||||
def test_posinf(self):
|
||||
with np.errstate(divide='ignore'):
|
||||
assert_all(np.isnan(np.array((1.,))/0.) == 0)
|
||||
|
||||
def test_neginf(self):
|
||||
with np.errstate(divide='ignore'):
|
||||
assert_all(np.isnan(np.array((-1.,))/0.) == 0)
|
||||
|
||||
def test_ind(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
assert_all(np.isnan(np.array((0.,))/0.) == 1)
|
||||
|
||||
def test_integer(self):
|
||||
assert_all(np.isnan(1) == 0)
|
||||
|
||||
def test_complex(self):
|
||||
assert_all(np.isnan(1+1j) == 0)
|
||||
|
||||
def test_complex1(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
assert_all(np.isnan(np.array(0+0j)/0.) == 1)
|
||||
|
||||
|
||||
class TestIsfinite:
|
||||
# Fixme, wrong place, isfinite now ufunc
|
||||
|
||||
def test_goodvalues(self):
|
||||
z = np.array((-1., 0., 1.))
|
||||
res = np.isfinite(z) == 1
|
||||
assert_all(np.all(res, axis=0))
|
||||
|
||||
def test_posinf(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
assert_all(np.isfinite(np.array((1.,))/0.) == 0)
|
||||
|
||||
def test_neginf(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
assert_all(np.isfinite(np.array((-1.,))/0.) == 0)
|
||||
|
||||
def test_ind(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
assert_all(np.isfinite(np.array((0.,))/0.) == 0)
|
||||
|
||||
def test_integer(self):
|
||||
assert_all(np.isfinite(1) == 1)
|
||||
|
||||
def test_complex(self):
|
||||
assert_all(np.isfinite(1+1j) == 1)
|
||||
|
||||
def test_complex1(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
assert_all(np.isfinite(np.array(1+1j)/0.) == 0)
|
||||
|
||||
|
||||
class TestIsinf:
|
||||
# Fixme, wrong place, isinf now ufunc
|
||||
|
||||
def test_goodvalues(self):
|
||||
z = np.array((-1., 0., 1.))
|
||||
res = np.isinf(z) == 0
|
||||
assert_all(np.all(res, axis=0))
|
||||
|
||||
def test_posinf(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
assert_all(np.isinf(np.array((1.,))/0.) == 1)
|
||||
|
||||
def test_posinf_scalar(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
assert_all(np.isinf(np.array(1.,)/0.) == 1)
|
||||
|
||||
def test_neginf(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
assert_all(np.isinf(np.array((-1.,))/0.) == 1)
|
||||
|
||||
def test_neginf_scalar(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
assert_all(np.isinf(np.array(-1.)/0.) == 1)
|
||||
|
||||
def test_ind(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
assert_all(np.isinf(np.array((0.,))/0.) == 0)
|
||||
|
||||
|
||||
class TestIsposinf:
|
||||
|
||||
def test_generic(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
vals = isposinf(np.array((-1., 0, 1))/0.)
|
||||
assert_(vals[0] == 0)
|
||||
assert_(vals[1] == 0)
|
||||
assert_(vals[2] == 1)
|
||||
|
||||
|
||||
class TestIsneginf:
|
||||
|
||||
def test_generic(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
vals = isneginf(np.array((-1., 0, 1))/0.)
|
||||
assert_(vals[0] == 1)
|
||||
assert_(vals[1] == 0)
|
||||
assert_(vals[2] == 0)
|
||||
|
||||
|
||||
class TestNanToNum:
|
||||
|
||||
def test_generic(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
vals = nan_to_num(np.array((-1., 0, 1))/0.)
|
||||
assert_all(vals[0] < -1e10) and assert_all(np.isfinite(vals[0]))
|
||||
assert_(vals[1] == 0)
|
||||
assert_all(vals[2] > 1e10) and assert_all(np.isfinite(vals[2]))
|
||||
assert_equal(type(vals), np.ndarray)
|
||||
|
||||
# perform the same tests but with nan, posinf and neginf keywords
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
vals = nan_to_num(np.array((-1., 0, 1))/0.,
|
||||
nan=10, posinf=20, neginf=30)
|
||||
assert_equal(vals, [30, 10, 20])
|
||||
assert_all(np.isfinite(vals[[0, 2]]))
|
||||
assert_equal(type(vals), np.ndarray)
|
||||
|
||||
# perform the same test but in-place
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
vals = np.array((-1., 0, 1))/0.
|
||||
result = nan_to_num(vals, copy=False)
|
||||
|
||||
assert_(result is vals)
|
||||
assert_all(vals[0] < -1e10) and assert_all(np.isfinite(vals[0]))
|
||||
assert_(vals[1] == 0)
|
||||
assert_all(vals[2] > 1e10) and assert_all(np.isfinite(vals[2]))
|
||||
assert_equal(type(vals), np.ndarray)
|
||||
|
||||
# perform the same test but in-place
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
vals = np.array((-1., 0, 1))/0.
|
||||
result = nan_to_num(vals, copy=False, nan=10, posinf=20, neginf=30)
|
||||
|
||||
assert_(result is vals)
|
||||
assert_equal(vals, [30, 10, 20])
|
||||
assert_all(np.isfinite(vals[[0, 2]]))
|
||||
assert_equal(type(vals), np.ndarray)
|
||||
|
||||
def test_array(self):
|
||||
vals = nan_to_num([1])
|
||||
assert_array_equal(vals, np.array([1], int))
|
||||
assert_equal(type(vals), np.ndarray)
|
||||
vals = nan_to_num([1], nan=10, posinf=20, neginf=30)
|
||||
assert_array_equal(vals, np.array([1], int))
|
||||
assert_equal(type(vals), np.ndarray)
|
||||
|
||||
def test_integer(self):
|
||||
vals = nan_to_num(1)
|
||||
assert_all(vals == 1)
|
||||
assert_equal(type(vals), np.int_)
|
||||
vals = nan_to_num(1, nan=10, posinf=20, neginf=30)
|
||||
assert_all(vals == 1)
|
||||
assert_equal(type(vals), np.int_)
|
||||
|
||||
def test_float(self):
|
||||
vals = nan_to_num(1.0)
|
||||
assert_all(vals == 1.0)
|
||||
assert_equal(type(vals), np.float64)
|
||||
vals = nan_to_num(1.1, nan=10, posinf=20, neginf=30)
|
||||
assert_all(vals == 1.1)
|
||||
assert_equal(type(vals), np.float64)
|
||||
|
||||
def test_complex_good(self):
|
||||
vals = nan_to_num(1+1j)
|
||||
assert_all(vals == 1+1j)
|
||||
assert_equal(type(vals), np.complex128)
|
||||
vals = nan_to_num(1+1j, nan=10, posinf=20, neginf=30)
|
||||
assert_all(vals == 1+1j)
|
||||
assert_equal(type(vals), np.complex128)
|
||||
|
||||
def test_complex_bad(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
v = 1 + 1j
|
||||
v += np.array(0+1.j)/0.
|
||||
vals = nan_to_num(v)
|
||||
# !! This is actually (unexpectedly) zero
|
||||
assert_all(np.isfinite(vals))
|
||||
assert_equal(type(vals), np.complex128)
|
||||
|
||||
def test_complex_bad2(self):
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
v = 1 + 1j
|
||||
v += np.array(-1+1.j)/0.
|
||||
vals = nan_to_num(v)
|
||||
assert_all(np.isfinite(vals))
|
||||
assert_equal(type(vals), np.complex128)
|
||||
# Fixme
|
||||
#assert_all(vals.imag > 1e10) and assert_all(np.isfinite(vals))
|
||||
# !! This is actually (unexpectedly) positive
|
||||
# !! inf. Comment out for now, and see if it
|
||||
# !! changes
|
||||
#assert_all(vals.real < -1e10) and assert_all(np.isfinite(vals))
|
||||
|
||||
def test_do_not_rewrite_previous_keyword(self):
|
||||
# This is done to test that when, for instance, nan=np.inf then these
|
||||
# values are not rewritten by posinf keyword to the posinf value.
|
||||
with np.errstate(divide='ignore', invalid='ignore'):
|
||||
vals = nan_to_num(np.array((-1., 0, 1))/0., nan=np.inf, posinf=999)
|
||||
assert_all(np.isfinite(vals[[0, 2]]))
|
||||
assert_all(vals[0] < -1e10)
|
||||
assert_equal(vals[[1, 2]], [np.inf, 999])
|
||||
assert_equal(type(vals), np.ndarray)
|
||||
|
||||
|
||||
class TestRealIfClose:
|
||||
|
||||
def test_basic(self):
|
||||
a = np.random.rand(10)
|
||||
b = real_if_close(a+1e-15j)
|
||||
assert_all(isrealobj(b))
|
||||
assert_array_equal(a, b)
|
||||
b = real_if_close(a+1e-7j)
|
||||
assert_all(iscomplexobj(b))
|
||||
b = real_if_close(a+1e-7j, tol=1e-6)
|
||||
assert_all(isrealobj(b))
|
||||
100
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_ufunclike.py
vendored
Normal file
100
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_ufunclike.py
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
import numpy as np
|
||||
|
||||
from numpy import fix, isposinf, isneginf
|
||||
from numpy.testing import (
|
||||
assert_, assert_equal, assert_array_equal, assert_raises
|
||||
)
|
||||
|
||||
|
||||
class TestUfunclike:
|
||||
|
||||
def test_isposinf(self):
|
||||
a = np.array([np.inf, -np.inf, np.nan, 0.0, 3.0, -3.0])
|
||||
out = np.zeros(a.shape, bool)
|
||||
tgt = np.array([True, False, False, False, False, False])
|
||||
|
||||
res = isposinf(a)
|
||||
assert_equal(res, tgt)
|
||||
res = isposinf(a, out)
|
||||
assert_equal(res, tgt)
|
||||
assert_equal(out, tgt)
|
||||
|
||||
a = a.astype(np.complex128)
|
||||
with assert_raises(TypeError):
|
||||
isposinf(a)
|
||||
|
||||
def test_isneginf(self):
|
||||
a = np.array([np.inf, -np.inf, np.nan, 0.0, 3.0, -3.0])
|
||||
out = np.zeros(a.shape, bool)
|
||||
tgt = np.array([False, True, False, False, False, False])
|
||||
|
||||
res = isneginf(a)
|
||||
assert_equal(res, tgt)
|
||||
res = isneginf(a, out)
|
||||
assert_equal(res, tgt)
|
||||
assert_equal(out, tgt)
|
||||
|
||||
a = a.astype(np.complex128)
|
||||
with assert_raises(TypeError):
|
||||
isneginf(a)
|
||||
|
||||
def test_fix(self):
|
||||
a = np.array([[1.0, 1.1, 1.5, 1.8], [-1.0, -1.1, -1.5, -1.8]])
|
||||
out = np.zeros(a.shape, float)
|
||||
tgt = np.array([[1., 1., 1., 1.], [-1., -1., -1., -1.]])
|
||||
|
||||
res = fix(a)
|
||||
assert_equal(res, tgt)
|
||||
res = fix(a, out)
|
||||
assert_equal(res, tgt)
|
||||
assert_equal(out, tgt)
|
||||
assert_equal(fix(3.14), 3)
|
||||
|
||||
def test_fix_with_subclass(self):
|
||||
class MyArray(np.ndarray):
|
||||
def __new__(cls, data, metadata=None):
|
||||
res = np.array(data, copy=True).view(cls)
|
||||
res.metadata = metadata
|
||||
return res
|
||||
|
||||
def __array_wrap__(self, obj, context=None, return_scalar=False):
|
||||
if not isinstance(obj, MyArray):
|
||||
obj = obj.view(MyArray)
|
||||
if obj.metadata is None:
|
||||
obj.metadata = self.metadata
|
||||
return obj
|
||||
|
||||
def __array_finalize__(self, obj):
|
||||
self.metadata = getattr(obj, 'metadata', None)
|
||||
return self
|
||||
|
||||
a = np.array([1.1, -1.1])
|
||||
m = MyArray(a, metadata='foo')
|
||||
f = fix(m)
|
||||
assert_array_equal(f, np.array([1, -1]))
|
||||
assert_(isinstance(f, MyArray))
|
||||
assert_equal(f.metadata, 'foo')
|
||||
|
||||
# check 0d arrays don't decay to scalars
|
||||
m0d = m[0,...]
|
||||
m0d.metadata = 'bar'
|
||||
f0d = fix(m0d)
|
||||
assert_(isinstance(f0d, MyArray))
|
||||
assert_equal(f0d.metadata, 'bar')
|
||||
|
||||
def test_scalar(self):
|
||||
x = np.inf
|
||||
actual = np.isposinf(x)
|
||||
expected = np.True_
|
||||
assert_equal(actual, expected)
|
||||
assert_equal(type(actual), type(expected))
|
||||
|
||||
x = -3.4
|
||||
actual = np.fix(x)
|
||||
expected = np.float64(-3.0)
|
||||
assert_equal(actual, expected)
|
||||
assert_equal(type(actual), type(expected))
|
||||
|
||||
out = np.array(0.0)
|
||||
actual = np.fix(x, out=out)
|
||||
assert_(actual is out)
|
||||
80
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_utils.py
vendored
Normal file
80
.CondaPkg/env/Lib/site-packages/numpy/lib/tests/test_utils.py
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
import pytest
|
||||
|
||||
import numpy as np
|
||||
from numpy.testing import assert_raises_regex
|
||||
import numpy.lib._utils_impl as _utils_impl
|
||||
|
||||
from io import StringIO
|
||||
|
||||
|
||||
def test_assert_raises_regex_context_manager():
|
||||
with assert_raises_regex(ValueError, 'no deprecation warning'):
|
||||
raise ValueError('no deprecation warning')
|
||||
|
||||
|
||||
def test_info_method_heading():
|
||||
# info(class) should only print "Methods:" heading if methods exist
|
||||
|
||||
class NoPublicMethods:
|
||||
pass
|
||||
|
||||
class WithPublicMethods:
|
||||
def first_method():
|
||||
pass
|
||||
|
||||
def _has_method_heading(cls):
|
||||
out = StringIO()
|
||||
np.info(cls, output=out)
|
||||
return 'Methods:' in out.getvalue()
|
||||
|
||||
assert _has_method_heading(WithPublicMethods)
|
||||
assert not _has_method_heading(NoPublicMethods)
|
||||
|
||||
|
||||
def test_drop_metadata():
|
||||
def _compare_dtypes(dt1, dt2):
|
||||
return np.can_cast(dt1, dt2, casting='no')
|
||||
|
||||
# structured dtype
|
||||
dt = np.dtype([('l1', [('l2', np.dtype('S8', metadata={'msg': 'toto'}))])],
|
||||
metadata={'msg': 'titi'})
|
||||
dt_m = _utils_impl.drop_metadata(dt)
|
||||
assert _compare_dtypes(dt, dt_m) is True
|
||||
assert dt_m.metadata is None
|
||||
assert dt_m['l1'].metadata is None
|
||||
assert dt_m['l1']['l2'].metadata is None
|
||||
|
||||
# alignment
|
||||
dt = np.dtype([('x', '<f8'), ('y', '<i4')],
|
||||
align=True,
|
||||
metadata={'msg': 'toto'})
|
||||
dt_m = _utils_impl.drop_metadata(dt)
|
||||
assert _compare_dtypes(dt, dt_m) is True
|
||||
assert dt_m.metadata is None
|
||||
|
||||
# subdtype
|
||||
dt = np.dtype('8f',
|
||||
metadata={'msg': 'toto'})
|
||||
dt_m = _utils_impl.drop_metadata(dt)
|
||||
assert _compare_dtypes(dt, dt_m) is True
|
||||
assert dt_m.metadata is None
|
||||
|
||||
# scalar
|
||||
dt = np.dtype('uint32',
|
||||
metadata={'msg': 'toto'})
|
||||
dt_m = _utils_impl.drop_metadata(dt)
|
||||
assert _compare_dtypes(dt, dt_m) is True
|
||||
assert dt_m.metadata is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("dtype",
|
||||
[np.dtype("i,i,i,i")[["f1", "f3"]],
|
||||
np.dtype("f8"),
|
||||
np.dtype("10i")])
|
||||
def test_drop_metadata_identity_and_copy(dtype):
|
||||
# If there is no metadata, the identity is preserved:
|
||||
assert _utils_impl.drop_metadata(dtype) is dtype
|
||||
|
||||
# If there is any, it is dropped (subforms are checked above)
|
||||
dtype = np.dtype(dtype, metadata={1: 2})
|
||||
assert _utils_impl.drop_metadata(dtype).metadata is None
|
||||
Reference in New Issue
Block a user