assassinlike commited on
Commit
2f87a3e
·
verified ·
1 Parent(s): 3ad37ec

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/__init__.cpython-310.pyc +0 -0
  2. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/_add_docstring.cpython-310.pyc +0 -0
  3. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/_char_codes.cpython-310.pyc +0 -0
  4. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/_dtype_like.cpython-310.pyc +0 -0
  5. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/_nbit.cpython-310.pyc +0 -0
  6. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/_shape.cpython-310.pyc +0 -0
  7. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/setup.cpython-310.pyc +0 -0
  8. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/__init__.cpython-310.pyc +0 -0
  9. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/_datasource.cpython-310.pyc +0 -0
  10. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/_iotools.cpython-310.pyc +0 -0
  11. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/_version.cpython-310.pyc +0 -0
  12. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/arraypad.cpython-310.pyc +0 -0
  13. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/arraysetops.cpython-310.pyc +0 -0
  14. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/arrayterator.cpython-310.pyc +0 -0
  15. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/format.cpython-310.pyc +0 -0
  16. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/histograms.cpython-310.pyc +0 -0
  17. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/index_tricks.cpython-310.pyc +0 -0
  18. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/mixins.cpython-310.pyc +0 -0
  19. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/nanfunctions.cpython-310.pyc +0 -0
  20. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/npyio.cpython-310.pyc +0 -0
  21. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/polynomial.cpython-310.pyc +0 -0
  22. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/recfunctions.cpython-310.pyc +0 -0
  23. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/scimath.cpython-310.pyc +0 -0
  24. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/setup.cpython-310.pyc +0 -0
  25. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/shape_base.cpython-310.pyc +0 -0
  26. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/stride_tricks.cpython-310.pyc +0 -0
  27. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/twodim_base.cpython-310.pyc +0 -0
  28. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/type_check.cpython-310.pyc +0 -0
  29. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/ufunclike.cpython-310.pyc +0 -0
  30. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/user_array.cpython-310.pyc +0 -0
  31. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/utils.cpython-310.pyc +0 -0
  32. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__init__.py +0 -0
  33. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/__init__.cpython-310.pyc +0 -0
  34. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test__datasource.cpython-310.pyc +0 -0
  35. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test__iotools.cpython-310.pyc +0 -0
  36. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test__version.cpython-310.pyc +0 -0
  37. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_arraysetops.cpython-310.pyc +0 -0
  38. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_io.cpython-310.pyc +0 -0
  39. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_loadtxt.cpython-310.pyc +0 -0
  40. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_mixins.cpython-310.pyc +0 -0
  41. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_packbits.cpython-310.pyc +0 -0
  42. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_shape_base.cpython-310.pyc +0 -0
  43. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_stride_tricks.cpython-310.pyc +0 -0
  44. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_ufunclike.cpython-310.pyc +0 -0
  45. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test__datasource.py +350 -0
  46. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test__iotools.py +353 -0
  47. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test__version.py +64 -0
  48. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test_arraypad.py +1380 -0
  49. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test_arraysetops.py +944 -0
  50. data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test_arrayterator.py +46 -0
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (6.13 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/_add_docstring.cpython-310.pyc ADDED
Binary file (3.82 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/_char_codes.cpython-310.pyc ADDED
Binary file (5.11 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/_dtype_like.cpython-310.pyc ADDED
Binary file (3.7 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/_nbit.cpython-310.pyc ADDED
Binary file (486 Bytes). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/_shape.cpython-310.pyc ADDED
Binary file (358 Bytes). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/_typing/__pycache__/setup.cpython-310.pyc ADDED
Binary file (568 Bytes). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (2.31 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/_datasource.cpython-310.pyc ADDED
Binary file (20.5 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/_iotools.cpython-310.pyc ADDED
Binary file (25.9 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/_version.cpython-310.pyc ADDED
Binary file (4.84 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/arraypad.cpython-310.pyc ADDED
Binary file (22.4 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/arraysetops.cpython-310.pyc ADDED
Binary file (28 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/arrayterator.cpython-310.pyc ADDED
Binary file (7.04 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/format.cpython-310.pyc ADDED
Binary file (27 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/histograms.cpython-310.pyc ADDED
Binary file (30.7 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/index_tricks.cpython-310.pyc ADDED
Binary file (29.2 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/mixins.cpython-310.pyc ADDED
Binary file (7.05 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/nanfunctions.cpython-310.pyc ADDED
Binary file (59 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/npyio.cpython-310.pyc ADDED
Binary file (74.7 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/polynomial.cpython-310.pyc ADDED
Binary file (41.5 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/recfunctions.cpython-310.pyc ADDED
Binary file (48.5 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/scimath.cpython-310.pyc ADDED
Binary file (15.7 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/setup.cpython-310.pyc ADDED
Binary file (633 Bytes). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/shape_base.cpython-310.pyc ADDED
Binary file (35.7 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/stride_tricks.cpython-310.pyc ADDED
Binary file (16.9 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/twodim_base.cpython-310.pyc ADDED
Binary file (33.1 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/type_check.cpython-310.pyc ADDED
Binary file (19.6 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/ufunclike.cpython-310.pyc ADDED
Binary file (6.28 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/user_array.cpython-310.pyc ADDED
Binary file (10.5 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/__pycache__/utils.cpython-310.pyc ADDED
Binary file (29.3 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__init__.py ADDED
File without changes
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (189 Bytes). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test__datasource.cpython-310.pyc ADDED
Binary file (12.4 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test__iotools.cpython-310.pyc ADDED
Binary file (11.3 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test__version.cpython-310.pyc ADDED
Binary file (2.22 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_arraysetops.cpython-310.pyc ADDED
Binary file (25.5 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_io.cpython-310.pyc ADDED
Binary file (96.5 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_loadtxt.cpython-310.pyc ADDED
Binary file (37.6 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_mixins.cpython-310.pyc ADDED
Binary file (7.47 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_packbits.cpython-310.pyc ADDED
Binary file (14.3 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_shape_base.cpython-310.pyc ADDED
Binary file (30.2 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_stride_tricks.cpython-310.pyc ADDED
Binary file (16.6 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/__pycache__/test_ufunclike.cpython-310.pyc ADDED
Binary file (3.47 kB). View file
 
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test__datasource.py ADDED
@@ -0,0 +1,350 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pytest
3
+ from tempfile import mkdtemp, mkstemp, NamedTemporaryFile
4
+ from shutil import rmtree
5
+
6
+ import numpy.lib._datasource as datasource
7
+ from numpy.testing import assert_, assert_equal, assert_raises
8
+
9
+ import urllib.request as urllib_request
10
+ from urllib.parse import urlparse
11
+ from urllib.error import URLError
12
+
13
+
14
+ def urlopen_stub(url, data=None):
15
+ '''Stub to replace urlopen for testing.'''
16
+ if url == valid_httpurl():
17
+ tmpfile = NamedTemporaryFile(prefix='urltmp_')
18
+ return tmpfile
19
+ else:
20
+ raise URLError('Name or service not known')
21
+
22
+ # setup and teardown
23
+ old_urlopen = None
24
+
25
+
26
+ def setup_module():
27
+ global old_urlopen
28
+
29
+ old_urlopen = urllib_request.urlopen
30
+ urllib_request.urlopen = urlopen_stub
31
+
32
+
33
+ def teardown_module():
34
+ urllib_request.urlopen = old_urlopen
35
+
36
+ # A valid website for more robust testing
37
+ http_path = 'http://www.google.com/'
38
+ http_file = 'index.html'
39
+
40
+ http_fakepath = 'http://fake.abc.web/site/'
41
+ http_fakefile = 'fake.txt'
42
+
43
+ malicious_files = ['/etc/shadow', '../../shadow',
44
+ '..\\system.dat', 'c:\\windows\\system.dat']
45
+
46
+ magic_line = b'three is the magic number'
47
+
48
+
49
+ # Utility functions used by many tests
50
+ def valid_textfile(filedir):
51
+ # Generate and return a valid temporary file.
52
+ fd, path = mkstemp(suffix='.txt', prefix='dstmp_', dir=filedir, text=True)
53
+ os.close(fd)
54
+ return path
55
+
56
+
57
+ def invalid_textfile(filedir):
58
+ # Generate and return an invalid filename.
59
+ fd, path = mkstemp(suffix='.txt', prefix='dstmp_', dir=filedir)
60
+ os.close(fd)
61
+ os.remove(path)
62
+ return path
63
+
64
+
65
+ def valid_httpurl():
66
+ return http_path+http_file
67
+
68
+
69
+ def invalid_httpurl():
70
+ return http_fakepath+http_fakefile
71
+
72
+
73
+ def valid_baseurl():
74
+ return http_path
75
+
76
+
77
+ def invalid_baseurl():
78
+ return http_fakepath
79
+
80
+
81
+ def valid_httpfile():
82
+ return http_file
83
+
84
+
85
+ def invalid_httpfile():
86
+ return http_fakefile
87
+
88
+
89
+ class TestDataSourceOpen:
90
+ def setup_method(self):
91
+ self.tmpdir = mkdtemp()
92
+ self.ds = datasource.DataSource(self.tmpdir)
93
+
94
+ def teardown_method(self):
95
+ rmtree(self.tmpdir)
96
+ del self.ds
97
+
98
+ def test_ValidHTTP(self):
99
+ fh = self.ds.open(valid_httpurl())
100
+ assert_(fh)
101
+ fh.close()
102
+
103
+ def test_InvalidHTTP(self):
104
+ url = invalid_httpurl()
105
+ assert_raises(OSError, self.ds.open, url)
106
+ try:
107
+ self.ds.open(url)
108
+ except OSError as e:
109
+ # Regression test for bug fixed in r4342.
110
+ assert_(e.errno is None)
111
+
112
+ def test_InvalidHTTPCacheURLError(self):
113
+ assert_raises(URLError, self.ds._cache, invalid_httpurl())
114
+
115
+ def test_ValidFile(self):
116
+ local_file = valid_textfile(self.tmpdir)
117
+ fh = self.ds.open(local_file)
118
+ assert_(fh)
119
+ fh.close()
120
+
121
+ def test_InvalidFile(self):
122
+ invalid_file = invalid_textfile(self.tmpdir)
123
+ assert_raises(OSError, self.ds.open, invalid_file)
124
+
125
+ def test_ValidGzipFile(self):
126
+ try:
127
+ import gzip
128
+ except ImportError:
129
+ # We don't have the gzip capabilities to test.
130
+ pytest.skip()
131
+ # Test datasource's internal file_opener for Gzip files.
132
+ filepath = os.path.join(self.tmpdir, 'foobar.txt.gz')
133
+ fp = gzip.open(filepath, 'w')
134
+ fp.write(magic_line)
135
+ fp.close()
136
+ fp = self.ds.open(filepath)
137
+ result = fp.readline()
138
+ fp.close()
139
+ assert_equal(magic_line, result)
140
+
141
+ def test_ValidBz2File(self):
142
+ try:
143
+ import bz2
144
+ except ImportError:
145
+ # We don't have the bz2 capabilities to test.
146
+ pytest.skip()
147
+ # Test datasource's internal file_opener for BZip2 files.
148
+ filepath = os.path.join(self.tmpdir, 'foobar.txt.bz2')
149
+ fp = bz2.BZ2File(filepath, 'w')
150
+ fp.write(magic_line)
151
+ fp.close()
152
+ fp = self.ds.open(filepath)
153
+ result = fp.readline()
154
+ fp.close()
155
+ assert_equal(magic_line, result)
156
+
157
+
158
+ class TestDataSourceExists:
159
+ def setup_method(self):
160
+ self.tmpdir = mkdtemp()
161
+ self.ds = datasource.DataSource(self.tmpdir)
162
+
163
+ def teardown_method(self):
164
+ rmtree(self.tmpdir)
165
+ del self.ds
166
+
167
+ def test_ValidHTTP(self):
168
+ assert_(self.ds.exists(valid_httpurl()))
169
+
170
+ def test_InvalidHTTP(self):
171
+ assert_equal(self.ds.exists(invalid_httpurl()), False)
172
+
173
+ def test_ValidFile(self):
174
+ # Test valid file in destpath
175
+ tmpfile = valid_textfile(self.tmpdir)
176
+ assert_(self.ds.exists(tmpfile))
177
+ # Test valid local file not in destpath
178
+ localdir = mkdtemp()
179
+ tmpfile = valid_textfile(localdir)
180
+ assert_(self.ds.exists(tmpfile))
181
+ rmtree(localdir)
182
+
183
+ def test_InvalidFile(self):
184
+ tmpfile = invalid_textfile(self.tmpdir)
185
+ assert_equal(self.ds.exists(tmpfile), False)
186
+
187
+
188
+ class TestDataSourceAbspath:
189
+ def setup_method(self):
190
+ self.tmpdir = os.path.abspath(mkdtemp())
191
+ self.ds = datasource.DataSource(self.tmpdir)
192
+
193
+ def teardown_method(self):
194
+ rmtree(self.tmpdir)
195
+ del self.ds
196
+
197
+ def test_ValidHTTP(self):
198
+ scheme, netloc, upath, pms, qry, frg = urlparse(valid_httpurl())
199
+ local_path = os.path.join(self.tmpdir, netloc,
200
+ upath.strip(os.sep).strip('/'))
201
+ assert_equal(local_path, self.ds.abspath(valid_httpurl()))
202
+
203
+ def test_ValidFile(self):
204
+ tmpfile = valid_textfile(self.tmpdir)
205
+ tmpfilename = os.path.split(tmpfile)[-1]
206
+ # Test with filename only
207
+ assert_equal(tmpfile, self.ds.abspath(tmpfilename))
208
+ # Test filename with complete path
209
+ assert_equal(tmpfile, self.ds.abspath(tmpfile))
210
+
211
+ def test_InvalidHTTP(self):
212
+ scheme, netloc, upath, pms, qry, frg = urlparse(invalid_httpurl())
213
+ invalidhttp = os.path.join(self.tmpdir, netloc,
214
+ upath.strip(os.sep).strip('/'))
215
+ assert_(invalidhttp != self.ds.abspath(valid_httpurl()))
216
+
217
+ def test_InvalidFile(self):
218
+ invalidfile = valid_textfile(self.tmpdir)
219
+ tmpfile = valid_textfile(self.tmpdir)
220
+ tmpfilename = os.path.split(tmpfile)[-1]
221
+ # Test with filename only
222
+ assert_(invalidfile != self.ds.abspath(tmpfilename))
223
+ # Test filename with complete path
224
+ assert_(invalidfile != self.ds.abspath(tmpfile))
225
+
226
+ def test_sandboxing(self):
227
+ tmpfile = valid_textfile(self.tmpdir)
228
+ tmpfilename = os.path.split(tmpfile)[-1]
229
+
230
+ tmp_path = lambda x: os.path.abspath(self.ds.abspath(x))
231
+
232
+ assert_(tmp_path(valid_httpurl()).startswith(self.tmpdir))
233
+ assert_(tmp_path(invalid_httpurl()).startswith(self.tmpdir))
234
+ assert_(tmp_path(tmpfile).startswith(self.tmpdir))
235
+ assert_(tmp_path(tmpfilename).startswith(self.tmpdir))
236
+ for fn in malicious_files:
237
+ assert_(tmp_path(http_path+fn).startswith(self.tmpdir))
238
+ assert_(tmp_path(fn).startswith(self.tmpdir))
239
+
240
+ def test_windows_os_sep(self):
241
+ orig_os_sep = os.sep
242
+ try:
243
+ os.sep = '\\'
244
+ self.test_ValidHTTP()
245
+ self.test_ValidFile()
246
+ self.test_InvalidHTTP()
247
+ self.test_InvalidFile()
248
+ self.test_sandboxing()
249
+ finally:
250
+ os.sep = orig_os_sep
251
+
252
+
253
+ class TestRepositoryAbspath:
254
+ def setup_method(self):
255
+ self.tmpdir = os.path.abspath(mkdtemp())
256
+ self.repos = datasource.Repository(valid_baseurl(), self.tmpdir)
257
+
258
+ def teardown_method(self):
259
+ rmtree(self.tmpdir)
260
+ del self.repos
261
+
262
+ def test_ValidHTTP(self):
263
+ scheme, netloc, upath, pms, qry, frg = urlparse(valid_httpurl())
264
+ local_path = os.path.join(self.repos._destpath, netloc,
265
+ upath.strip(os.sep).strip('/'))
266
+ filepath = self.repos.abspath(valid_httpfile())
267
+ assert_equal(local_path, filepath)
268
+
269
+ def test_sandboxing(self):
270
+ tmp_path = lambda x: os.path.abspath(self.repos.abspath(x))
271
+ assert_(tmp_path(valid_httpfile()).startswith(self.tmpdir))
272
+ for fn in malicious_files:
273
+ assert_(tmp_path(http_path+fn).startswith(self.tmpdir))
274
+ assert_(tmp_path(fn).startswith(self.tmpdir))
275
+
276
+ def test_windows_os_sep(self):
277
+ orig_os_sep = os.sep
278
+ try:
279
+ os.sep = '\\'
280
+ self.test_ValidHTTP()
281
+ self.test_sandboxing()
282
+ finally:
283
+ os.sep = orig_os_sep
284
+
285
+
286
+ class TestRepositoryExists:
287
+ def setup_method(self):
288
+ self.tmpdir = mkdtemp()
289
+ self.repos = datasource.Repository(valid_baseurl(), self.tmpdir)
290
+
291
+ def teardown_method(self):
292
+ rmtree(self.tmpdir)
293
+ del self.repos
294
+
295
+ def test_ValidFile(self):
296
+ # Create local temp file
297
+ tmpfile = valid_textfile(self.tmpdir)
298
+ assert_(self.repos.exists(tmpfile))
299
+
300
+ def test_InvalidFile(self):
301
+ tmpfile = invalid_textfile(self.tmpdir)
302
+ assert_equal(self.repos.exists(tmpfile), False)
303
+
304
+ def test_RemoveHTTPFile(self):
305
+ assert_(self.repos.exists(valid_httpurl()))
306
+
307
+ def test_CachedHTTPFile(self):
308
+ localfile = valid_httpurl()
309
+ # Create a locally cached temp file with an URL based
310
+ # directory structure. This is similar to what Repository.open
311
+ # would do.
312
+ scheme, netloc, upath, pms, qry, frg = urlparse(localfile)
313
+ local_path = os.path.join(self.repos._destpath, netloc)
314
+ os.mkdir(local_path, 0o0700)
315
+ tmpfile = valid_textfile(local_path)
316
+ assert_(self.repos.exists(tmpfile))
317
+
318
+
319
+ class TestOpenFunc:
320
+ def setup_method(self):
321
+ self.tmpdir = mkdtemp()
322
+
323
+ def teardown_method(self):
324
+ rmtree(self.tmpdir)
325
+
326
+ def test_DataSourceOpen(self):
327
+ local_file = valid_textfile(self.tmpdir)
328
+ # Test case where destpath is passed in
329
+ fp = datasource.open(local_file, destpath=self.tmpdir)
330
+ assert_(fp)
331
+ fp.close()
332
+ # Test case where default destpath is used
333
+ fp = datasource.open(local_file)
334
+ assert_(fp)
335
+ fp.close()
336
+
337
+ def test_del_attr_handling():
338
+ # DataSource __del__ can be called
339
+ # even if __init__ fails when the
340
+ # Exception object is caught by the
341
+ # caller as happens in refguide_check
342
+ # is_deprecated() function
343
+
344
+ ds = datasource.DataSource()
345
+ # simulate failed __init__ by removing key attribute
346
+ # produced within __init__ and expected by __del__
347
+ del ds._istmpdest
348
+ # should not raise an AttributeError if __del__
349
+ # gracefully handles failed __init__:
350
+ ds.__del__()
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test__iotools.py ADDED
@@ -0,0 +1,353 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from datetime import date
3
+
4
+ import numpy as np
5
+ from numpy.testing import (
6
+ assert_, assert_equal, assert_allclose, assert_raises,
7
+ )
8
+ from numpy.lib._iotools import (
9
+ LineSplitter, NameValidator, StringConverter,
10
+ has_nested_fields, easy_dtype, flatten_dtype
11
+ )
12
+
13
+
14
+ class TestLineSplitter:
15
+ "Tests the LineSplitter class."
16
+
17
+ def test_no_delimiter(self):
18
+ "Test LineSplitter w/o delimiter"
19
+ strg = " 1 2 3 4 5 # test"
20
+ test = LineSplitter()(strg)
21
+ assert_equal(test, ['1', '2', '3', '4', '5'])
22
+ test = LineSplitter('')(strg)
23
+ assert_equal(test, ['1', '2', '3', '4', '5'])
24
+
25
+ def test_space_delimiter(self):
26
+ "Test space delimiter"
27
+ strg = " 1 2 3 4 5 # test"
28
+ test = LineSplitter(' ')(strg)
29
+ assert_equal(test, ['1', '2', '3', '4', '', '5'])
30
+ test = LineSplitter(' ')(strg)
31
+ assert_equal(test, ['1 2 3 4', '5'])
32
+
33
+ def test_tab_delimiter(self):
34
+ "Test tab delimiter"
35
+ strg = " 1\t 2\t 3\t 4\t 5 6"
36
+ test = LineSplitter('\t')(strg)
37
+ assert_equal(test, ['1', '2', '3', '4', '5 6'])
38
+ strg = " 1 2\t 3 4\t 5 6"
39
+ test = LineSplitter('\t')(strg)
40
+ assert_equal(test, ['1 2', '3 4', '5 6'])
41
+
42
+ def test_other_delimiter(self):
43
+ "Test LineSplitter on delimiter"
44
+ strg = "1,2,3,4,,5"
45
+ test = LineSplitter(',')(strg)
46
+ assert_equal(test, ['1', '2', '3', '4', '', '5'])
47
+ #
48
+ strg = " 1,2,3,4,,5 # test"
49
+ test = LineSplitter(',')(strg)
50
+ assert_equal(test, ['1', '2', '3', '4', '', '5'])
51
+
52
+ # gh-11028 bytes comment/delimiters should get encoded
53
+ strg = b" 1,2,3,4,,5 % test"
54
+ test = LineSplitter(delimiter=b',', comments=b'%')(strg)
55
+ assert_equal(test, ['1', '2', '3', '4', '', '5'])
56
+
57
+ def test_constant_fixed_width(self):
58
+ "Test LineSplitter w/ fixed-width fields"
59
+ strg = " 1 2 3 4 5 # test"
60
+ test = LineSplitter(3)(strg)
61
+ assert_equal(test, ['1', '2', '3', '4', '', '5', ''])
62
+ #
63
+ strg = " 1 3 4 5 6# test"
64
+ test = LineSplitter(20)(strg)
65
+ assert_equal(test, ['1 3 4 5 6'])
66
+ #
67
+ strg = " 1 3 4 5 6# test"
68
+ test = LineSplitter(30)(strg)
69
+ assert_equal(test, ['1 3 4 5 6'])
70
+
71
+ def test_variable_fixed_width(self):
72
+ strg = " 1 3 4 5 6# test"
73
+ test = LineSplitter((3, 6, 6, 3))(strg)
74
+ assert_equal(test, ['1', '3', '4 5', '6'])
75
+ #
76
+ strg = " 1 3 4 5 6# test"
77
+ test = LineSplitter((6, 6, 9))(strg)
78
+ assert_equal(test, ['1', '3 4', '5 6'])
79
+
80
+ # -----------------------------------------------------------------------------
81
+
82
+
83
+ class TestNameValidator:
84
+
85
+ def test_case_sensitivity(self):
86
+ "Test case sensitivity"
87
+ names = ['A', 'a', 'b', 'c']
88
+ test = NameValidator().validate(names)
89
+ assert_equal(test, ['A', 'a', 'b', 'c'])
90
+ test = NameValidator(case_sensitive=False).validate(names)
91
+ assert_equal(test, ['A', 'A_1', 'B', 'C'])
92
+ test = NameValidator(case_sensitive='upper').validate(names)
93
+ assert_equal(test, ['A', 'A_1', 'B', 'C'])
94
+ test = NameValidator(case_sensitive='lower').validate(names)
95
+ assert_equal(test, ['a', 'a_1', 'b', 'c'])
96
+
97
+ # check exceptions
98
+ assert_raises(ValueError, NameValidator, case_sensitive='foobar')
99
+
100
+ def test_excludelist(self):
101
+ "Test excludelist"
102
+ names = ['dates', 'data', 'Other Data', 'mask']
103
+ validator = NameValidator(excludelist=['dates', 'data', 'mask'])
104
+ test = validator.validate(names)
105
+ assert_equal(test, ['dates_', 'data_', 'Other_Data', 'mask_'])
106
+
107
+ def test_missing_names(self):
108
+ "Test validate missing names"
109
+ namelist = ('a', 'b', 'c')
110
+ validator = NameValidator()
111
+ assert_equal(validator(namelist), ['a', 'b', 'c'])
112
+ namelist = ('', 'b', 'c')
113
+ assert_equal(validator(namelist), ['f0', 'b', 'c'])
114
+ namelist = ('a', 'b', '')
115
+ assert_equal(validator(namelist), ['a', 'b', 'f0'])
116
+ namelist = ('', 'f0', '')
117
+ assert_equal(validator(namelist), ['f1', 'f0', 'f2'])
118
+
119
+ def test_validate_nb_names(self):
120
+ "Test validate nb names"
121
+ namelist = ('a', 'b', 'c')
122
+ validator = NameValidator()
123
+ assert_equal(validator(namelist, nbfields=1), ('a',))
124
+ assert_equal(validator(namelist, nbfields=5, defaultfmt="g%i"),
125
+ ['a', 'b', 'c', 'g0', 'g1'])
126
+
127
+ def test_validate_wo_names(self):
128
+ "Test validate no names"
129
+ namelist = None
130
+ validator = NameValidator()
131
+ assert_(validator(namelist) is None)
132
+ assert_equal(validator(namelist, nbfields=3), ['f0', 'f1', 'f2'])
133
+
134
+ # -----------------------------------------------------------------------------
135
+
136
+
137
+ def _bytes_to_date(s):
138
+ return date(*time.strptime(s, "%Y-%m-%d")[:3])
139
+
140
+
141
+ class TestStringConverter:
142
+ "Test StringConverter"
143
+
144
+ def test_creation(self):
145
+ "Test creation of a StringConverter"
146
+ converter = StringConverter(int, -99999)
147
+ assert_equal(converter._status, 1)
148
+ assert_equal(converter.default, -99999)
149
+
150
+ def test_upgrade(self):
151
+ "Tests the upgrade method."
152
+
153
+ converter = StringConverter()
154
+ assert_equal(converter._status, 0)
155
+
156
+ # test int
157
+ assert_equal(converter.upgrade('0'), 0)
158
+ assert_equal(converter._status, 1)
159
+
160
+ # On systems where long defaults to 32-bit, the statuses will be
161
+ # offset by one, so we check for this here.
162
+ import numpy.core.numeric as nx
163
+ status_offset = int(nx.dtype(nx.int_).itemsize < nx.dtype(nx.int64).itemsize)
164
+
165
+ # test int > 2**32
166
+ assert_equal(converter.upgrade('17179869184'), 17179869184)
167
+ assert_equal(converter._status, 1 + status_offset)
168
+
169
+ # test float
170
+ assert_allclose(converter.upgrade('0.'), 0.0)
171
+ assert_equal(converter._status, 2 + status_offset)
172
+
173
+ # test complex
174
+ assert_equal(converter.upgrade('0j'), complex('0j'))
175
+ assert_equal(converter._status, 3 + status_offset)
176
+
177
+ # test str
178
+ # note that the longdouble type has been skipped, so the
179
+ # _status increases by 2. Everything should succeed with
180
+ # unicode conversion (8).
181
+ for s in ['a', b'a']:
182
+ res = converter.upgrade(s)
183
+ assert_(type(res) is str)
184
+ assert_equal(res, 'a')
185
+ assert_equal(converter._status, 8 + status_offset)
186
+
187
+ def test_missing(self):
188
+ "Tests the use of missing values."
189
+ converter = StringConverter(missing_values=('missing',
190
+ 'missed'))
191
+ converter.upgrade('0')
192
+ assert_equal(converter('0'), 0)
193
+ assert_equal(converter(''), converter.default)
194
+ assert_equal(converter('missing'), converter.default)
195
+ assert_equal(converter('missed'), converter.default)
196
+ try:
197
+ converter('miss')
198
+ except ValueError:
199
+ pass
200
+
201
+ def test_upgrademapper(self):
202
+ "Tests updatemapper"
203
+ dateparser = _bytes_to_date
204
+ _original_mapper = StringConverter._mapper[:]
205
+ try:
206
+ StringConverter.upgrade_mapper(dateparser, date(2000, 1, 1))
207
+ convert = StringConverter(dateparser, date(2000, 1, 1))
208
+ test = convert('2001-01-01')
209
+ assert_equal(test, date(2001, 1, 1))
210
+ test = convert('2009-01-01')
211
+ assert_equal(test, date(2009, 1, 1))
212
+ test = convert('')
213
+ assert_equal(test, date(2000, 1, 1))
214
+ finally:
215
+ StringConverter._mapper = _original_mapper
216
+
217
+ def test_string_to_object(self):
218
+ "Make sure that string-to-object functions are properly recognized"
219
+ old_mapper = StringConverter._mapper[:] # copy of list
220
+ conv = StringConverter(_bytes_to_date)
221
+ assert_equal(conv._mapper, old_mapper)
222
+ assert_(hasattr(conv, 'default'))
223
+
224
+ def test_keep_default(self):
225
+ "Make sure we don't lose an explicit default"
226
+ converter = StringConverter(None, missing_values='',
227
+ default=-999)
228
+ converter.upgrade('3.14159265')
229
+ assert_equal(converter.default, -999)
230
+ assert_equal(converter.type, np.dtype(float))
231
+ #
232
+ converter = StringConverter(
233
+ None, missing_values='', default=0)
234
+ converter.upgrade('3.14159265')
235
+ assert_equal(converter.default, 0)
236
+ assert_equal(converter.type, np.dtype(float))
237
+
238
+ def test_keep_default_zero(self):
239
+ "Check that we don't lose a default of 0"
240
+ converter = StringConverter(int, default=0,
241
+ missing_values="N/A")
242
+ assert_equal(converter.default, 0)
243
+
244
+ def test_keep_missing_values(self):
245
+ "Check that we're not losing missing values"
246
+ converter = StringConverter(int, default=0,
247
+ missing_values="N/A")
248
+ assert_equal(
249
+ converter.missing_values, {'', 'N/A'})
250
+
251
+ def test_int64_dtype(self):
252
+ "Check that int64 integer types can be specified"
253
+ converter = StringConverter(np.int64, default=0)
254
+ val = "-9223372036854775807"
255
+ assert_(converter(val) == -9223372036854775807)
256
+ val = "9223372036854775807"
257
+ assert_(converter(val) == 9223372036854775807)
258
+
259
+ def test_uint64_dtype(self):
260
+ "Check that uint64 integer types can be specified"
261
+ converter = StringConverter(np.uint64, default=0)
262
+ val = "9223372043271415339"
263
+ assert_(converter(val) == 9223372043271415339)
264
+
265
+
266
+ class TestMiscFunctions:
267
+
268
+ def test_has_nested_dtype(self):
269
+ "Test has_nested_dtype"
270
+ ndtype = np.dtype(float)
271
+ assert_equal(has_nested_fields(ndtype), False)
272
+ ndtype = np.dtype([('A', '|S3'), ('B', float)])
273
+ assert_equal(has_nested_fields(ndtype), False)
274
+ ndtype = np.dtype([('A', int), ('B', [('BA', float), ('BB', '|S1')])])
275
+ assert_equal(has_nested_fields(ndtype), True)
276
+
277
+ def test_easy_dtype(self):
278
+ "Test ndtype on dtypes"
279
+ # Simple case
280
+ ndtype = float
281
+ assert_equal(easy_dtype(ndtype), np.dtype(float))
282
+ # As string w/o names
283
+ ndtype = "i4, f8"
284
+ assert_equal(easy_dtype(ndtype),
285
+ np.dtype([('f0', "i4"), ('f1', "f8")]))
286
+ # As string w/o names but different default format
287
+ assert_equal(easy_dtype(ndtype, defaultfmt="field_%03i"),
288
+ np.dtype([('field_000', "i4"), ('field_001', "f8")]))
289
+ # As string w/ names
290
+ ndtype = "i4, f8"
291
+ assert_equal(easy_dtype(ndtype, names="a, b"),
292
+ np.dtype([('a', "i4"), ('b', "f8")]))
293
+ # As string w/ names (too many)
294
+ ndtype = "i4, f8"
295
+ assert_equal(easy_dtype(ndtype, names="a, b, c"),
296
+ np.dtype([('a', "i4"), ('b', "f8")]))
297
+ # As string w/ names (not enough)
298
+ ndtype = "i4, f8"
299
+ assert_equal(easy_dtype(ndtype, names=", b"),
300
+ np.dtype([('f0', "i4"), ('b', "f8")]))
301
+ # ... (with different default format)
302
+ assert_equal(easy_dtype(ndtype, names="a", defaultfmt="f%02i"),
303
+ np.dtype([('a', "i4"), ('f00', "f8")]))
304
+ # As list of tuples w/o names
305
+ ndtype = [('A', int), ('B', float)]
306
+ assert_equal(easy_dtype(ndtype), np.dtype([('A', int), ('B', float)]))
307
+ # As list of tuples w/ names
308
+ assert_equal(easy_dtype(ndtype, names="a,b"),
309
+ np.dtype([('a', int), ('b', float)]))
310
+ # As list of tuples w/ not enough names
311
+ assert_equal(easy_dtype(ndtype, names="a"),
312
+ np.dtype([('a', int), ('f0', float)]))
313
+ # As list of tuples w/ too many names
314
+ assert_equal(easy_dtype(ndtype, names="a,b,c"),
315
+ np.dtype([('a', int), ('b', float)]))
316
+ # As list of types w/o names
317
+ ndtype = (int, float, float)
318
+ assert_equal(easy_dtype(ndtype),
319
+ np.dtype([('f0', int), ('f1', float), ('f2', float)]))
320
+ # As list of types w names
321
+ ndtype = (int, float, float)
322
+ assert_equal(easy_dtype(ndtype, names="a, b, c"),
323
+ np.dtype([('a', int), ('b', float), ('c', float)]))
324
+ # As simple dtype w/ names
325
+ ndtype = np.dtype(float)
326
+ assert_equal(easy_dtype(ndtype, names="a, b, c"),
327
+ np.dtype([(_, float) for _ in ('a', 'b', 'c')]))
328
+ # As simple dtype w/o names (but multiple fields)
329
+ ndtype = np.dtype(float)
330
+ assert_equal(
331
+ easy_dtype(ndtype, names=['', '', ''], defaultfmt="f%02i"),
332
+ np.dtype([(_, float) for _ in ('f00', 'f01', 'f02')]))
333
+
334
+ def test_flatten_dtype(self):
335
+ "Testing flatten_dtype"
336
+ # Standard dtype
337
+ dt = np.dtype([("a", "f8"), ("b", "f8")])
338
+ dt_flat = flatten_dtype(dt)
339
+ assert_equal(dt_flat, [float, float])
340
+ # Recursive dtype
341
+ dt = np.dtype([("a", [("aa", '|S1'), ("ab", '|S2')]), ("b", int)])
342
+ dt_flat = flatten_dtype(dt)
343
+ assert_equal(dt_flat, [np.dtype('|S1'), np.dtype('|S2'), int])
344
+ # dtype with shaped fields
345
+ dt = np.dtype([("a", (float, 2)), ("b", (int, 3))])
346
+ dt_flat = flatten_dtype(dt)
347
+ assert_equal(dt_flat, [float, int])
348
+ dt_flat = flatten_dtype(dt, True)
349
+ assert_equal(dt_flat, [float] * 2 + [int] * 3)
350
+ # dtype w/ titles
351
+ dt = np.dtype([(("a", "A"), "f8"), (("b", "B"), "f8")])
352
+ dt_flat = flatten_dtype(dt)
353
+ assert_equal(dt_flat, [float, float])
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test__version.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for the NumpyVersion class.
2
+
3
+ """
4
+ from numpy.testing import assert_, assert_raises
5
+ from numpy.lib import NumpyVersion
6
+
7
+
8
+ def test_main_versions():
9
+ assert_(NumpyVersion('1.8.0') == '1.8.0')
10
+ for ver in ['1.9.0', '2.0.0', '1.8.1', '10.0.1']:
11
+ assert_(NumpyVersion('1.8.0') < ver)
12
+
13
+ for ver in ['1.7.0', '1.7.1', '0.9.9']:
14
+ assert_(NumpyVersion('1.8.0') > ver)
15
+
16
+
17
+ def test_version_1_point_10():
18
+ # regression test for gh-2998.
19
+ assert_(NumpyVersion('1.9.0') < '1.10.0')
20
+ assert_(NumpyVersion('1.11.0') < '1.11.1')
21
+ assert_(NumpyVersion('1.11.0') == '1.11.0')
22
+ assert_(NumpyVersion('1.99.11') < '1.99.12')
23
+
24
+
25
+ def test_alpha_beta_rc():
26
+ assert_(NumpyVersion('1.8.0rc1') == '1.8.0rc1')
27
+ for ver in ['1.8.0', '1.8.0rc2']:
28
+ assert_(NumpyVersion('1.8.0rc1') < ver)
29
+
30
+ for ver in ['1.8.0a2', '1.8.0b3', '1.7.2rc4']:
31
+ assert_(NumpyVersion('1.8.0rc1') > ver)
32
+
33
+ assert_(NumpyVersion('1.8.0b1') > '1.8.0a2')
34
+
35
+
36
+ def test_dev_version():
37
+ assert_(NumpyVersion('1.9.0.dev-Unknown') < '1.9.0')
38
+ for ver in ['1.9.0', '1.9.0a1', '1.9.0b2', '1.9.0b2.dev-ffffffff']:
39
+ assert_(NumpyVersion('1.9.0.dev-f16acvda') < ver)
40
+
41
+ assert_(NumpyVersion('1.9.0.dev-f16acvda') == '1.9.0.dev-11111111')
42
+
43
+
44
+ def test_dev_a_b_rc_mixed():
45
+ assert_(NumpyVersion('1.9.0a2.dev-f16acvda') == '1.9.0a2.dev-11111111')
46
+ assert_(NumpyVersion('1.9.0a2.dev-6acvda54') < '1.9.0a2')
47
+
48
+
49
+ def test_dev0_version():
50
+ assert_(NumpyVersion('1.9.0.dev0+Unknown') < '1.9.0')
51
+ for ver in ['1.9.0', '1.9.0a1', '1.9.0b2', '1.9.0b2.dev0+ffffffff']:
52
+ assert_(NumpyVersion('1.9.0.dev0+f16acvda') < ver)
53
+
54
+ assert_(NumpyVersion('1.9.0.dev0+f16acvda') == '1.9.0.dev0+11111111')
55
+
56
+
57
+ def test_dev0_a_b_rc_mixed():
58
+ assert_(NumpyVersion('1.9.0a2.dev0+f16acvda') == '1.9.0a2.dev0+11111111')
59
+ assert_(NumpyVersion('1.9.0a2.dev0+6acvda54') < '1.9.0a2')
60
+
61
+
62
+ def test_raises():
63
+ for ver in ['1.9', '1,9.0', '1.7.x']:
64
+ assert_raises(ValueError, NumpyVersion, ver)
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test_arraypad.py ADDED
@@ -0,0 +1,1380 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for the array padding functions.
2
+
3
+ """
4
+ import pytest
5
+
6
+ import numpy as np
7
+ from numpy.testing import assert_array_equal, assert_allclose, assert_equal
8
+ from numpy.lib.arraypad import _as_pairs
9
+
10
+
11
+ _numeric_dtypes = (
12
+ np.sctypes["uint"]
13
+ + np.sctypes["int"]
14
+ + np.sctypes["float"]
15
+ + np.sctypes["complex"]
16
+ )
17
+ _all_modes = {
18
+ 'constant': {'constant_values': 0},
19
+ 'edge': {},
20
+ 'linear_ramp': {'end_values': 0},
21
+ 'maximum': {'stat_length': None},
22
+ 'mean': {'stat_length': None},
23
+ 'median': {'stat_length': None},
24
+ 'minimum': {'stat_length': None},
25
+ 'reflect': {'reflect_type': 'even'},
26
+ 'symmetric': {'reflect_type': 'even'},
27
+ 'wrap': {},
28
+ 'empty': {}
29
+ }
30
+
31
+
32
+ class TestAsPairs:
33
+ def test_single_value(self):
34
+ """Test casting for a single value."""
35
+ expected = np.array([[3, 3]] * 10)
36
+ for x in (3, [3], [[3]]):
37
+ result = _as_pairs(x, 10)
38
+ assert_equal(result, expected)
39
+ # Test with dtype=object
40
+ obj = object()
41
+ assert_equal(
42
+ _as_pairs(obj, 10),
43
+ np.array([[obj, obj]] * 10)
44
+ )
45
+
46
+ def test_two_values(self):
47
+ """Test proper casting for two different values."""
48
+ # Broadcasting in the first dimension with numbers
49
+ expected = np.array([[3, 4]] * 10)
50
+ for x in ([3, 4], [[3, 4]]):
51
+ result = _as_pairs(x, 10)
52
+ assert_equal(result, expected)
53
+ # and with dtype=object
54
+ obj = object()
55
+ assert_equal(
56
+ _as_pairs(["a", obj], 10),
57
+ np.array([["a", obj]] * 10)
58
+ )
59
+
60
+ # Broadcasting in the second / last dimension with numbers
61
+ assert_equal(
62
+ _as_pairs([[3], [4]], 2),
63
+ np.array([[3, 3], [4, 4]])
64
+ )
65
+ # and with dtype=object
66
+ assert_equal(
67
+ _as_pairs([["a"], [obj]], 2),
68
+ np.array([["a", "a"], [obj, obj]])
69
+ )
70
+
71
+ def test_with_none(self):
72
+ expected = ((None, None), (None, None), (None, None))
73
+ assert_equal(
74
+ _as_pairs(None, 3, as_index=False),
75
+ expected
76
+ )
77
+ assert_equal(
78
+ _as_pairs(None, 3, as_index=True),
79
+ expected
80
+ )
81
+
82
+ def test_pass_through(self):
83
+ """Test if `x` already matching desired output are passed through."""
84
+ expected = np.arange(12).reshape((6, 2))
85
+ assert_equal(
86
+ _as_pairs(expected, 6),
87
+ expected
88
+ )
89
+
90
+ def test_as_index(self):
91
+ """Test results if `as_index=True`."""
92
+ assert_equal(
93
+ _as_pairs([2.6, 3.3], 10, as_index=True),
94
+ np.array([[3, 3]] * 10, dtype=np.intp)
95
+ )
96
+ assert_equal(
97
+ _as_pairs([2.6, 4.49], 10, as_index=True),
98
+ np.array([[3, 4]] * 10, dtype=np.intp)
99
+ )
100
+ for x in (-3, [-3], [[-3]], [-3, 4], [3, -4], [[-3, 4]], [[4, -3]],
101
+ [[1, 2]] * 9 + [[1, -2]]):
102
+ with pytest.raises(ValueError, match="negative values"):
103
+ _as_pairs(x, 10, as_index=True)
104
+
105
+ def test_exceptions(self):
106
+ """Ensure faulty usage is discovered."""
107
+ with pytest.raises(ValueError, match="more dimensions than allowed"):
108
+ _as_pairs([[[3]]], 10)
109
+ with pytest.raises(ValueError, match="could not be broadcast"):
110
+ _as_pairs([[1, 2], [3, 4]], 3)
111
+ with pytest.raises(ValueError, match="could not be broadcast"):
112
+ _as_pairs(np.ones((2, 3)), 3)
113
+
114
+
115
+ class TestConditionalShortcuts:
116
+ @pytest.mark.parametrize("mode", _all_modes.keys())
117
+ def test_zero_padding_shortcuts(self, mode):
118
+ test = np.arange(120).reshape(4, 5, 6)
119
+ pad_amt = [(0, 0) for _ in test.shape]
120
+ assert_array_equal(test, np.pad(test, pad_amt, mode=mode))
121
+
122
+ @pytest.mark.parametrize("mode", ['maximum', 'mean', 'median', 'minimum',])
123
+ def test_shallow_statistic_range(self, mode):
124
+ test = np.arange(120).reshape(4, 5, 6)
125
+ pad_amt = [(1, 1) for _ in test.shape]
126
+ assert_array_equal(np.pad(test, pad_amt, mode='edge'),
127
+ np.pad(test, pad_amt, mode=mode, stat_length=1))
128
+
129
+ @pytest.mark.parametrize("mode", ['maximum', 'mean', 'median', 'minimum',])
130
+ def test_clip_statistic_range(self, mode):
131
+ test = np.arange(30).reshape(5, 6)
132
+ pad_amt = [(3, 3) for _ in test.shape]
133
+ assert_array_equal(np.pad(test, pad_amt, mode=mode),
134
+ np.pad(test, pad_amt, mode=mode, stat_length=30))
135
+
136
+
137
+ class TestStatistic:
138
+ def test_check_mean_stat_length(self):
139
+ a = np.arange(100).astype('f')
140
+ a = np.pad(a, ((25, 20), ), 'mean', stat_length=((2, 3), ))
141
+ b = np.array(
142
+ [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
143
+ 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
144
+ 0.5, 0.5, 0.5, 0.5, 0.5,
145
+
146
+ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
147
+ 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
148
+ 20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
149
+ 30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
150
+ 40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
151
+ 50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
152
+ 60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
153
+ 70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
154
+ 80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
155
+ 90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
156
+
157
+ 98., 98., 98., 98., 98., 98., 98., 98., 98., 98.,
158
+ 98., 98., 98., 98., 98., 98., 98., 98., 98., 98.
159
+ ])
160
+ assert_array_equal(a, b)
161
+
162
+ def test_check_maximum_1(self):
163
+ a = np.arange(100)
164
+ a = np.pad(a, (25, 20), 'maximum')
165
+ b = np.array(
166
+ [99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
167
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
168
+ 99, 99, 99, 99, 99,
169
+
170
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
171
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
172
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
173
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
174
+ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
175
+ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
176
+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
177
+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
178
+ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
179
+ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
180
+
181
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
182
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
183
+ )
184
+ assert_array_equal(a, b)
185
+
186
+ def test_check_maximum_2(self):
187
+ a = np.arange(100) + 1
188
+ a = np.pad(a, (25, 20), 'maximum')
189
+ b = np.array(
190
+ [100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
191
+ 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
192
+ 100, 100, 100, 100, 100,
193
+
194
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
195
+ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
196
+ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
197
+ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
198
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
199
+ 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
200
+ 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
201
+ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
202
+ 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
203
+ 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
204
+
205
+ 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
206
+ 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
207
+ )
208
+ assert_array_equal(a, b)
209
+
210
+ def test_check_maximum_stat_length(self):
211
+ a = np.arange(100) + 1
212
+ a = np.pad(a, (25, 20), 'maximum', stat_length=10)
213
+ b = np.array(
214
+ [10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
215
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
216
+ 10, 10, 10, 10, 10,
217
+
218
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
219
+ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
220
+ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
221
+ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
222
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
223
+ 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
224
+ 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
225
+ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
226
+ 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
227
+ 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
228
+
229
+ 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
230
+ 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
231
+ )
232
+ assert_array_equal(a, b)
233
+
234
+ def test_check_minimum_1(self):
235
+ a = np.arange(100)
236
+ a = np.pad(a, (25, 20), 'minimum')
237
+ b = np.array(
238
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
239
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
240
+ 0, 0, 0, 0, 0,
241
+
242
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
243
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
244
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
245
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
246
+ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
247
+ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
248
+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
249
+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
250
+ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
251
+ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
252
+
253
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
254
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
255
+ )
256
+ assert_array_equal(a, b)
257
+
258
+ def test_check_minimum_2(self):
259
+ a = np.arange(100) + 2
260
+ a = np.pad(a, (25, 20), 'minimum')
261
+ b = np.array(
262
+ [2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
263
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
264
+ 2, 2, 2, 2, 2,
265
+
266
+ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
267
+ 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
268
+ 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
269
+ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
270
+ 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
271
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
272
+ 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
273
+ 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
274
+ 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
275
+ 92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
276
+
277
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
278
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
279
+ )
280
+ assert_array_equal(a, b)
281
+
282
+ def test_check_minimum_stat_length(self):
283
+ a = np.arange(100) + 1
284
+ a = np.pad(a, (25, 20), 'minimum', stat_length=10)
285
+ b = np.array(
286
+ [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
287
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
288
+ 1, 1, 1, 1, 1,
289
+
290
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
291
+ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
292
+ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
293
+ 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
294
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
295
+ 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
296
+ 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
297
+ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
298
+ 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
299
+ 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
300
+
301
+ 91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
302
+ 91, 91, 91, 91, 91, 91, 91, 91, 91, 91]
303
+ )
304
+ assert_array_equal(a, b)
305
+
306
+ def test_check_median(self):
307
+ a = np.arange(100).astype('f')
308
+ a = np.pad(a, (25, 20), 'median')
309
+ b = np.array(
310
+ [49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
311
+ 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
312
+ 49.5, 49.5, 49.5, 49.5, 49.5,
313
+
314
+ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
315
+ 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
316
+ 20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
317
+ 30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
318
+ 40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
319
+ 50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
320
+ 60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
321
+ 70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
322
+ 80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
323
+ 90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
324
+
325
+ 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
326
+ 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5]
327
+ )
328
+ assert_array_equal(a, b)
329
+
330
+ def test_check_median_01(self):
331
+ a = np.array([[3, 1, 4], [4, 5, 9], [9, 8, 2]])
332
+ a = np.pad(a, 1, 'median')
333
+ b = np.array(
334
+ [[4, 4, 5, 4, 4],
335
+
336
+ [3, 3, 1, 4, 3],
337
+ [5, 4, 5, 9, 5],
338
+ [8, 9, 8, 2, 8],
339
+
340
+ [4, 4, 5, 4, 4]]
341
+ )
342
+ assert_array_equal(a, b)
343
+
344
+ def test_check_median_02(self):
345
+ a = np.array([[3, 1, 4], [4, 5, 9], [9, 8, 2]])
346
+ a = np.pad(a.T, 1, 'median').T
347
+ b = np.array(
348
+ [[5, 4, 5, 4, 5],
349
+
350
+ [3, 3, 1, 4, 3],
351
+ [5, 4, 5, 9, 5],
352
+ [8, 9, 8, 2, 8],
353
+
354
+ [5, 4, 5, 4, 5]]
355
+ )
356
+ assert_array_equal(a, b)
357
+
358
+ def test_check_median_stat_length(self):
359
+ a = np.arange(100).astype('f')
360
+ a[1] = 2.
361
+ a[97] = 96.
362
+ a = np.pad(a, (25, 20), 'median', stat_length=(3, 5))
363
+ b = np.array(
364
+ [ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.,
365
+ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.,
366
+ 2., 2., 2., 2., 2.,
367
+
368
+ 0., 2., 2., 3., 4., 5., 6., 7., 8., 9.,
369
+ 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
370
+ 20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
371
+ 30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
372
+ 40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
373
+ 50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
374
+ 60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
375
+ 70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
376
+ 80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
377
+ 90., 91., 92., 93., 94., 95., 96., 96., 98., 99.,
378
+
379
+ 96., 96., 96., 96., 96., 96., 96., 96., 96., 96.,
380
+ 96., 96., 96., 96., 96., 96., 96., 96., 96., 96.]
381
+ )
382
+ assert_array_equal(a, b)
383
+
384
+ def test_check_mean_shape_one(self):
385
+ a = [[4, 5, 6]]
386
+ a = np.pad(a, (5, 7), 'mean', stat_length=2)
387
+ b = np.array(
388
+ [[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
389
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
390
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
391
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
392
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
393
+
394
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
395
+
396
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
397
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
398
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
399
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
400
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
401
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
402
+ [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6]]
403
+ )
404
+ assert_array_equal(a, b)
405
+
406
+ def test_check_mean_2(self):
407
+ a = np.arange(100).astype('f')
408
+ a = np.pad(a, (25, 20), 'mean')
409
+ b = np.array(
410
+ [49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
411
+ 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
412
+ 49.5, 49.5, 49.5, 49.5, 49.5,
413
+
414
+ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
415
+ 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
416
+ 20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
417
+ 30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
418
+ 40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
419
+ 50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
420
+ 60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
421
+ 70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
422
+ 80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
423
+ 90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
424
+
425
+ 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
426
+ 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5]
427
+ )
428
+ assert_array_equal(a, b)
429
+
430
+ @pytest.mark.parametrize("mode", [
431
+ "mean",
432
+ "median",
433
+ "minimum",
434
+ "maximum"
435
+ ])
436
+ def test_same_prepend_append(self, mode):
437
+ """ Test that appended and prepended values are equal """
438
+ # This test is constructed to trigger floating point rounding errors in
439
+ # a way that caused gh-11216 for mode=='mean'
440
+ a = np.array([-1, 2, -1]) + np.array([0, 1e-12, 0], dtype=np.float64)
441
+ a = np.pad(a, (1, 1), mode)
442
+ assert_equal(a[0], a[-1])
443
+
444
+ @pytest.mark.parametrize("mode", ["mean", "median", "minimum", "maximum"])
445
+ @pytest.mark.parametrize(
446
+ "stat_length", [-2, (-2,), (3, -1), ((5, 2), (-2, 3)), ((-4,), (2,))]
447
+ )
448
+ def test_check_negative_stat_length(self, mode, stat_length):
449
+ arr = np.arange(30).reshape((6, 5))
450
+ match = "index can't contain negative values"
451
+ with pytest.raises(ValueError, match=match):
452
+ np.pad(arr, 2, mode, stat_length=stat_length)
453
+
454
+ def test_simple_stat_length(self):
455
+ a = np.arange(30)
456
+ a = np.reshape(a, (6, 5))
457
+ a = np.pad(a, ((2, 3), (3, 2)), mode='mean', stat_length=(3,))
458
+ b = np.array(
459
+ [[6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
460
+ [6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
461
+
462
+ [1, 1, 1, 0, 1, 2, 3, 4, 3, 3],
463
+ [6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
464
+ [11, 11, 11, 10, 11, 12, 13, 14, 13, 13],
465
+ [16, 16, 16, 15, 16, 17, 18, 19, 18, 18],
466
+ [21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
467
+ [26, 26, 26, 25, 26, 27, 28, 29, 28, 28],
468
+
469
+ [21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
470
+ [21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
471
+ [21, 21, 21, 20, 21, 22, 23, 24, 23, 23]]
472
+ )
473
+ assert_array_equal(a, b)
474
+
475
+ @pytest.mark.filterwarnings("ignore:Mean of empty slice:RuntimeWarning")
476
+ @pytest.mark.filterwarnings(
477
+ "ignore:invalid value encountered in( scalar)? divide:RuntimeWarning"
478
+ )
479
+ @pytest.mark.parametrize("mode", ["mean", "median"])
480
+ def test_zero_stat_length_valid(self, mode):
481
+ arr = np.pad([1., 2.], (1, 2), mode, stat_length=0)
482
+ expected = np.array([np.nan, 1., 2., np.nan, np.nan])
483
+ assert_equal(arr, expected)
484
+
485
+ @pytest.mark.parametrize("mode", ["minimum", "maximum"])
486
+ def test_zero_stat_length_invalid(self, mode):
487
+ match = "stat_length of 0 yields no value for padding"
488
+ with pytest.raises(ValueError, match=match):
489
+ np.pad([1., 2.], 0, mode, stat_length=0)
490
+ with pytest.raises(ValueError, match=match):
491
+ np.pad([1., 2.], 0, mode, stat_length=(1, 0))
492
+ with pytest.raises(ValueError, match=match):
493
+ np.pad([1., 2.], 1, mode, stat_length=0)
494
+ with pytest.raises(ValueError, match=match):
495
+ np.pad([1., 2.], 1, mode, stat_length=(1, 0))
496
+
497
+
498
+ class TestConstant:
499
+ def test_check_constant(self):
500
+ a = np.arange(100)
501
+ a = np.pad(a, (25, 20), 'constant', constant_values=(10, 20))
502
+ b = np.array(
503
+ [10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
504
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
505
+ 10, 10, 10, 10, 10,
506
+
507
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
508
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
509
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
510
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
511
+ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
512
+ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
513
+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
514
+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
515
+ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
516
+ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
517
+
518
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
519
+ 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
520
+ )
521
+ assert_array_equal(a, b)
522
+
523
+ def test_check_constant_zeros(self):
524
+ a = np.arange(100)
525
+ a = np.pad(a, (25, 20), 'constant')
526
+ b = np.array(
527
+ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
528
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
529
+ 0, 0, 0, 0, 0,
530
+
531
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
532
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
533
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
534
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
535
+ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
536
+ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
537
+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
538
+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
539
+ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
540
+ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
541
+
542
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
543
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
544
+ )
545
+ assert_array_equal(a, b)
546
+
547
+ def test_check_constant_float(self):
548
+ # If input array is int, but constant_values are float, the dtype of
549
+ # the array to be padded is kept
550
+ arr = np.arange(30).reshape(5, 6)
551
+ test = np.pad(arr, (1, 2), mode='constant',
552
+ constant_values=1.1)
553
+ expected = np.array(
554
+ [[ 1, 1, 1, 1, 1, 1, 1, 1, 1],
555
+
556
+ [ 1, 0, 1, 2, 3, 4, 5, 1, 1],
557
+ [ 1, 6, 7, 8, 9, 10, 11, 1, 1],
558
+ [ 1, 12, 13, 14, 15, 16, 17, 1, 1],
559
+ [ 1, 18, 19, 20, 21, 22, 23, 1, 1],
560
+ [ 1, 24, 25, 26, 27, 28, 29, 1, 1],
561
+
562
+ [ 1, 1, 1, 1, 1, 1, 1, 1, 1],
563
+ [ 1, 1, 1, 1, 1, 1, 1, 1, 1]]
564
+ )
565
+ assert_allclose(test, expected)
566
+
567
+ def test_check_constant_float2(self):
568
+ # If input array is float, and constant_values are float, the dtype of
569
+ # the array to be padded is kept - here retaining the float constants
570
+ arr = np.arange(30).reshape(5, 6)
571
+ arr_float = arr.astype(np.float64)
572
+ test = np.pad(arr_float, ((1, 2), (1, 2)), mode='constant',
573
+ constant_values=1.1)
574
+ expected = np.array(
575
+ [[ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1],
576
+
577
+ [ 1.1, 0. , 1. , 2. , 3. , 4. , 5. , 1.1, 1.1],
578
+ [ 1.1, 6. , 7. , 8. , 9. , 10. , 11. , 1.1, 1.1],
579
+ [ 1.1, 12. , 13. , 14. , 15. , 16. , 17. , 1.1, 1.1],
580
+ [ 1.1, 18. , 19. , 20. , 21. , 22. , 23. , 1.1, 1.1],
581
+ [ 1.1, 24. , 25. , 26. , 27. , 28. , 29. , 1.1, 1.1],
582
+
583
+ [ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1],
584
+ [ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1]]
585
+ )
586
+ assert_allclose(test, expected)
587
+
588
+ def test_check_constant_float3(self):
589
+ a = np.arange(100, dtype=float)
590
+ a = np.pad(a, (25, 20), 'constant', constant_values=(-1.1, -1.2))
591
+ b = np.array(
592
+ [-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1,
593
+ -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1,
594
+ -1.1, -1.1, -1.1, -1.1, -1.1,
595
+
596
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
597
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
598
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
599
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
600
+ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
601
+ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
602
+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
603
+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
604
+ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
605
+ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
606
+
607
+ -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2,
608
+ -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2]
609
+ )
610
+ assert_allclose(a, b)
611
+
612
+ def test_check_constant_odd_pad_amount(self):
613
+ arr = np.arange(30).reshape(5, 6)
614
+ test = np.pad(arr, ((1,), (2,)), mode='constant',
615
+ constant_values=3)
616
+ expected = np.array(
617
+ [[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
618
+
619
+ [ 3, 3, 0, 1, 2, 3, 4, 5, 3, 3],
620
+ [ 3, 3, 6, 7, 8, 9, 10, 11, 3, 3],
621
+ [ 3, 3, 12, 13, 14, 15, 16, 17, 3, 3],
622
+ [ 3, 3, 18, 19, 20, 21, 22, 23, 3, 3],
623
+ [ 3, 3, 24, 25, 26, 27, 28, 29, 3, 3],
624
+
625
+ [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]]
626
+ )
627
+ assert_allclose(test, expected)
628
+
629
+ def test_check_constant_pad_2d(self):
630
+ arr = np.arange(4).reshape(2, 2)
631
+ test = np.lib.pad(arr, ((1, 2), (1, 3)), mode='constant',
632
+ constant_values=((1, 2), (3, 4)))
633
+ expected = np.array(
634
+ [[3, 1, 1, 4, 4, 4],
635
+ [3, 0, 1, 4, 4, 4],
636
+ [3, 2, 3, 4, 4, 4],
637
+ [3, 2, 2, 4, 4, 4],
638
+ [3, 2, 2, 4, 4, 4]]
639
+ )
640
+ assert_allclose(test, expected)
641
+
642
+ def test_check_large_integers(self):
643
+ uint64_max = 2 ** 64 - 1
644
+ arr = np.full(5, uint64_max, dtype=np.uint64)
645
+ test = np.pad(arr, 1, mode="constant", constant_values=arr.min())
646
+ expected = np.full(7, uint64_max, dtype=np.uint64)
647
+ assert_array_equal(test, expected)
648
+
649
+ int64_max = 2 ** 63 - 1
650
+ arr = np.full(5, int64_max, dtype=np.int64)
651
+ test = np.pad(arr, 1, mode="constant", constant_values=arr.min())
652
+ expected = np.full(7, int64_max, dtype=np.int64)
653
+ assert_array_equal(test, expected)
654
+
655
+ def test_check_object_array(self):
656
+ arr = np.empty(1, dtype=object)
657
+ obj_a = object()
658
+ arr[0] = obj_a
659
+ obj_b = object()
660
+ obj_c = object()
661
+ arr = np.pad(arr, pad_width=1, mode='constant',
662
+ constant_values=(obj_b, obj_c))
663
+
664
+ expected = np.empty((3,), dtype=object)
665
+ expected[0] = obj_b
666
+ expected[1] = obj_a
667
+ expected[2] = obj_c
668
+
669
+ assert_array_equal(arr, expected)
670
+
671
+ def test_pad_empty_dimension(self):
672
+ arr = np.zeros((3, 0, 2))
673
+ result = np.pad(arr, [(0,), (2,), (1,)], mode="constant")
674
+ assert result.shape == (3, 4, 4)
675
+
676
+
677
+ class TestLinearRamp:
678
+ def test_check_simple(self):
679
+ a = np.arange(100).astype('f')
680
+ a = np.pad(a, (25, 20), 'linear_ramp', end_values=(4, 5))
681
+ b = np.array(
682
+ [4.00, 3.84, 3.68, 3.52, 3.36, 3.20, 3.04, 2.88, 2.72, 2.56,
683
+ 2.40, 2.24, 2.08, 1.92, 1.76, 1.60, 1.44, 1.28, 1.12, 0.96,
684
+ 0.80, 0.64, 0.48, 0.32, 0.16,
685
+
686
+ 0.00, 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00,
687
+ 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0,
688
+ 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0,
689
+ 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0,
690
+ 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0,
691
+ 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0,
692
+ 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0,
693
+ 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0,
694
+ 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0,
695
+ 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0,
696
+
697
+ 94.3, 89.6, 84.9, 80.2, 75.5, 70.8, 66.1, 61.4, 56.7, 52.0,
698
+ 47.3, 42.6, 37.9, 33.2, 28.5, 23.8, 19.1, 14.4, 9.7, 5.]
699
+ )
700
+ assert_allclose(a, b, rtol=1e-5, atol=1e-5)
701
+
702
+ def test_check_2d(self):
703
+ arr = np.arange(20).reshape(4, 5).astype(np.float64)
704
+ test = np.pad(arr, (2, 2), mode='linear_ramp', end_values=(0, 0))
705
+ expected = np.array(
706
+ [[0., 0., 0., 0., 0., 0., 0., 0., 0.],
707
+ [0., 0., 0., 0.5, 1., 1.5, 2., 1., 0.],
708
+ [0., 0., 0., 1., 2., 3., 4., 2., 0.],
709
+ [0., 2.5, 5., 6., 7., 8., 9., 4.5, 0.],
710
+ [0., 5., 10., 11., 12., 13., 14., 7., 0.],
711
+ [0., 7.5, 15., 16., 17., 18., 19., 9.5, 0.],
712
+ [0., 3.75, 7.5, 8., 8.5, 9., 9.5, 4.75, 0.],
713
+ [0., 0., 0., 0., 0., 0., 0., 0., 0.]])
714
+ assert_allclose(test, expected)
715
+
716
+ @pytest.mark.xfail(exceptions=(AssertionError,))
717
+ def test_object_array(self):
718
+ from fractions import Fraction
719
+ arr = np.array([Fraction(1, 2), Fraction(-1, 2)])
720
+ actual = np.pad(arr, (2, 3), mode='linear_ramp', end_values=0)
721
+
722
+ # deliberately chosen to have a non-power-of-2 denominator such that
723
+ # rounding to floats causes a failure.
724
+ expected = np.array([
725
+ Fraction( 0, 12),
726
+ Fraction( 3, 12),
727
+ Fraction( 6, 12),
728
+ Fraction(-6, 12),
729
+ Fraction(-4, 12),
730
+ Fraction(-2, 12),
731
+ Fraction(-0, 12),
732
+ ])
733
+ assert_equal(actual, expected)
734
+
735
+ def test_end_values(self):
736
+ """Ensure that end values are exact."""
737
+ a = np.pad(np.ones(10).reshape(2, 5), (223, 123), mode="linear_ramp")
738
+ assert_equal(a[:, 0], 0.)
739
+ assert_equal(a[:, -1], 0.)
740
+ assert_equal(a[0, :], 0.)
741
+ assert_equal(a[-1, :], 0.)
742
+
743
+ @pytest.mark.parametrize("dtype", _numeric_dtypes)
744
+ def test_negative_difference(self, dtype):
745
+ """
746
+ Check correct behavior of unsigned dtypes if there is a negative
747
+ difference between the edge to pad and `end_values`. Check both cases
748
+ to be independent of implementation. Test behavior for all other dtypes
749
+ in case dtype casting interferes with complex dtypes. See gh-14191.
750
+ """
751
+ x = np.array([3], dtype=dtype)
752
+ result = np.pad(x, 3, mode="linear_ramp", end_values=0)
753
+ expected = np.array([0, 1, 2, 3, 2, 1, 0], dtype=dtype)
754
+ assert_equal(result, expected)
755
+
756
+ x = np.array([0], dtype=dtype)
757
+ result = np.pad(x, 3, mode="linear_ramp", end_values=3)
758
+ expected = np.array([3, 2, 1, 0, 1, 2, 3], dtype=dtype)
759
+ assert_equal(result, expected)
760
+
761
+
762
+ class TestReflect:
763
+ def test_check_simple(self):
764
+ a = np.arange(100)
765
+ a = np.pad(a, (25, 20), 'reflect')
766
+ b = np.array(
767
+ [25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
768
+ 15, 14, 13, 12, 11, 10, 9, 8, 7, 6,
769
+ 5, 4, 3, 2, 1,
770
+
771
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
772
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
773
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
774
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
775
+ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
776
+ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
777
+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
778
+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
779
+ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
780
+ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
781
+
782
+ 98, 97, 96, 95, 94, 93, 92, 91, 90, 89,
783
+ 88, 87, 86, 85, 84, 83, 82, 81, 80, 79]
784
+ )
785
+ assert_array_equal(a, b)
786
+
787
+ def test_check_odd_method(self):
788
+ a = np.arange(100)
789
+ a = np.pad(a, (25, 20), 'reflect', reflect_type='odd')
790
+ b = np.array(
791
+ [-25, -24, -23, -22, -21, -20, -19, -18, -17, -16,
792
+ -15, -14, -13, -12, -11, -10, -9, -8, -7, -6,
793
+ -5, -4, -3, -2, -1,
794
+
795
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
796
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
797
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
798
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
799
+ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
800
+ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
801
+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
802
+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
803
+ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
804
+ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
805
+
806
+ 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
807
+ 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]
808
+ )
809
+ assert_array_equal(a, b)
810
+
811
+ def test_check_large_pad(self):
812
+ a = [[4, 5, 6], [6, 7, 8]]
813
+ a = np.pad(a, (5, 7), 'reflect')
814
+ b = np.array(
815
+ [[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
816
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
817
+ [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
818
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
819
+ [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
820
+
821
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
822
+ [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
823
+
824
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
825
+ [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
826
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
827
+ [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
828
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
829
+ [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
830
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]]
831
+ )
832
+ assert_array_equal(a, b)
833
+
834
+ def test_check_shape(self):
835
+ a = [[4, 5, 6]]
836
+ a = np.pad(a, (5, 7), 'reflect')
837
+ b = np.array(
838
+ [[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
839
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
840
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
841
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
842
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
843
+
844
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
845
+
846
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
847
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
848
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
849
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
850
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
851
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
852
+ [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]]
853
+ )
854
+ assert_array_equal(a, b)
855
+
856
+ def test_check_01(self):
857
+ a = np.pad([1, 2, 3], 2, 'reflect')
858
+ b = np.array([3, 2, 1, 2, 3, 2, 1])
859
+ assert_array_equal(a, b)
860
+
861
+ def test_check_02(self):
862
+ a = np.pad([1, 2, 3], 3, 'reflect')
863
+ b = np.array([2, 3, 2, 1, 2, 3, 2, 1, 2])
864
+ assert_array_equal(a, b)
865
+
866
+ def test_check_03(self):
867
+ a = np.pad([1, 2, 3], 4, 'reflect')
868
+ b = np.array([1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3])
869
+ assert_array_equal(a, b)
870
+
871
+
872
+ class TestEmptyArray:
873
+ """Check how padding behaves on arrays with an empty dimension."""
874
+
875
+ @pytest.mark.parametrize(
876
+ # Keep parametrization ordered, otherwise pytest-xdist might believe
877
+ # that different tests were collected during parallelization
878
+ "mode", sorted(_all_modes.keys() - {"constant", "empty"})
879
+ )
880
+ def test_pad_empty_dimension(self, mode):
881
+ match = ("can't extend empty axis 0 using modes other than 'constant' "
882
+ "or 'empty'")
883
+ with pytest.raises(ValueError, match=match):
884
+ np.pad([], 4, mode=mode)
885
+ with pytest.raises(ValueError, match=match):
886
+ np.pad(np.ndarray(0), 4, mode=mode)
887
+ with pytest.raises(ValueError, match=match):
888
+ np.pad(np.zeros((0, 3)), ((1,), (0,)), mode=mode)
889
+
890
+ @pytest.mark.parametrize("mode", _all_modes.keys())
891
+ def test_pad_non_empty_dimension(self, mode):
892
+ result = np.pad(np.ones((2, 0, 2)), ((3,), (0,), (1,)), mode=mode)
893
+ assert result.shape == (8, 0, 4)
894
+
895
+
896
+ class TestSymmetric:
897
+ def test_check_simple(self):
898
+ a = np.arange(100)
899
+ a = np.pad(a, (25, 20), 'symmetric')
900
+ b = np.array(
901
+ [24, 23, 22, 21, 20, 19, 18, 17, 16, 15,
902
+ 14, 13, 12, 11, 10, 9, 8, 7, 6, 5,
903
+ 4, 3, 2, 1, 0,
904
+
905
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
906
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
907
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
908
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
909
+ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
910
+ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
911
+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
912
+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
913
+ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
914
+ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
915
+
916
+ 99, 98, 97, 96, 95, 94, 93, 92, 91, 90,
917
+ 89, 88, 87, 86, 85, 84, 83, 82, 81, 80]
918
+ )
919
+ assert_array_equal(a, b)
920
+
921
+ def test_check_odd_method(self):
922
+ a = np.arange(100)
923
+ a = np.pad(a, (25, 20), 'symmetric', reflect_type='odd')
924
+ b = np.array(
925
+ [-24, -23, -22, -21, -20, -19, -18, -17, -16, -15,
926
+ -14, -13, -12, -11, -10, -9, -8, -7, -6, -5,
927
+ -4, -3, -2, -1, 0,
928
+
929
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
930
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
931
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
932
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
933
+ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
934
+ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
935
+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
936
+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
937
+ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
938
+ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
939
+
940
+ 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
941
+ 109, 110, 111, 112, 113, 114, 115, 116, 117, 118]
942
+ )
943
+ assert_array_equal(a, b)
944
+
945
+ def test_check_large_pad(self):
946
+ a = [[4, 5, 6], [6, 7, 8]]
947
+ a = np.pad(a, (5, 7), 'symmetric')
948
+ b = np.array(
949
+ [[5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
950
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
951
+ [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
952
+ [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
953
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
954
+
955
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
956
+ [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
957
+
958
+ [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
959
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
960
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
961
+ [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
962
+ [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
963
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
964
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6]]
965
+ )
966
+
967
+ assert_array_equal(a, b)
968
+
969
+ def test_check_large_pad_odd(self):
970
+ a = [[4, 5, 6], [6, 7, 8]]
971
+ a = np.pad(a, (5, 7), 'symmetric', reflect_type='odd')
972
+ b = np.array(
973
+ [[-3, -2, -2, -1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6],
974
+ [-3, -2, -2, -1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6],
975
+ [-1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8],
976
+ [-1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8],
977
+ [ 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10],
978
+
979
+ [ 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10],
980
+ [ 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12],
981
+
982
+ [ 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12],
983
+ [ 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14],
984
+ [ 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14],
985
+ [ 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16],
986
+ [ 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16],
987
+ [ 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18],
988
+ [ 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18]]
989
+ )
990
+ assert_array_equal(a, b)
991
+
992
+ def test_check_shape(self):
993
+ a = [[4, 5, 6]]
994
+ a = np.pad(a, (5, 7), 'symmetric')
995
+ b = np.array(
996
+ [[5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
997
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
998
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
999
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
1000
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
1001
+
1002
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
1003
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
1004
+
1005
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
1006
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
1007
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
1008
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
1009
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
1010
+ [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6]]
1011
+ )
1012
+ assert_array_equal(a, b)
1013
+
1014
+ def test_check_01(self):
1015
+ a = np.pad([1, 2, 3], 2, 'symmetric')
1016
+ b = np.array([2, 1, 1, 2, 3, 3, 2])
1017
+ assert_array_equal(a, b)
1018
+
1019
+ def test_check_02(self):
1020
+ a = np.pad([1, 2, 3], 3, 'symmetric')
1021
+ b = np.array([3, 2, 1, 1, 2, 3, 3, 2, 1])
1022
+ assert_array_equal(a, b)
1023
+
1024
+ def test_check_03(self):
1025
+ a = np.pad([1, 2, 3], 6, 'symmetric')
1026
+ b = np.array([1, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 1, 1, 2, 3])
1027
+ assert_array_equal(a, b)
1028
+
1029
+
1030
+ class TestWrap:
1031
+ def test_check_simple(self):
1032
+ a = np.arange(100)
1033
+ a = np.pad(a, (25, 20), 'wrap')
1034
+ b = np.array(
1035
+ [75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
1036
+ 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
1037
+ 95, 96, 97, 98, 99,
1038
+
1039
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1040
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
1041
+ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
1042
+ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
1043
+ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
1044
+ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
1045
+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
1046
+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
1047
+ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
1048
+ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
1049
+
1050
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1051
+ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
1052
+ )
1053
+ assert_array_equal(a, b)
1054
+
1055
+ def test_check_large_pad(self):
1056
+ a = np.arange(12)
1057
+ a = np.reshape(a, (3, 4))
1058
+ a = np.pad(a, (10, 12), 'wrap')
1059
+ b = np.array(
1060
+ [[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
1061
+ 11, 8, 9, 10, 11, 8, 9, 10, 11],
1062
+ [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
1063
+ 3, 0, 1, 2, 3, 0, 1, 2, 3],
1064
+ [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
1065
+ 7, 4, 5, 6, 7, 4, 5, 6, 7],
1066
+ [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
1067
+ 11, 8, 9, 10, 11, 8, 9, 10, 11],
1068
+ [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
1069
+ 3, 0, 1, 2, 3, 0, 1, 2, 3],
1070
+ [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
1071
+ 7, 4, 5, 6, 7, 4, 5, 6, 7],
1072
+ [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
1073
+ 11, 8, 9, 10, 11, 8, 9, 10, 11],
1074
+ [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
1075
+ 3, 0, 1, 2, 3, 0, 1, 2, 3],
1076
+ [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
1077
+ 7, 4, 5, 6, 7, 4, 5, 6, 7],
1078
+ [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
1079
+ 11, 8, 9, 10, 11, 8, 9, 10, 11],
1080
+
1081
+ [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
1082
+ 3, 0, 1, 2, 3, 0, 1, 2, 3],
1083
+ [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
1084
+ 7, 4, 5, 6, 7, 4, 5, 6, 7],
1085
+ [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
1086
+ 11, 8, 9, 10, 11, 8, 9, 10, 11],
1087
+
1088
+ [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
1089
+ 3, 0, 1, 2, 3, 0, 1, 2, 3],
1090
+ [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
1091
+ 7, 4, 5, 6, 7, 4, 5, 6, 7],
1092
+ [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
1093
+ 11, 8, 9, 10, 11, 8, 9, 10, 11],
1094
+ [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
1095
+ 3, 0, 1, 2, 3, 0, 1, 2, 3],
1096
+ [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
1097
+ 7, 4, 5, 6, 7, 4, 5, 6, 7],
1098
+ [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
1099
+ 11, 8, 9, 10, 11, 8, 9, 10, 11],
1100
+ [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
1101
+ 3, 0, 1, 2, 3, 0, 1, 2, 3],
1102
+ [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
1103
+ 7, 4, 5, 6, 7, 4, 5, 6, 7],
1104
+ [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
1105
+ 11, 8, 9, 10, 11, 8, 9, 10, 11],
1106
+ [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
1107
+ 3, 0, 1, 2, 3, 0, 1, 2, 3],
1108
+ [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
1109
+ 7, 4, 5, 6, 7, 4, 5, 6, 7],
1110
+ [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
1111
+ 11, 8, 9, 10, 11, 8, 9, 10, 11]]
1112
+ )
1113
+ assert_array_equal(a, b)
1114
+
1115
+ def test_check_01(self):
1116
+ a = np.pad([1, 2, 3], 3, 'wrap')
1117
+ b = np.array([1, 2, 3, 1, 2, 3, 1, 2, 3])
1118
+ assert_array_equal(a, b)
1119
+
1120
+ def test_check_02(self):
1121
+ a = np.pad([1, 2, 3], 4, 'wrap')
1122
+ b = np.array([3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1])
1123
+ assert_array_equal(a, b)
1124
+
1125
+ def test_pad_with_zero(self):
1126
+ a = np.ones((3, 5))
1127
+ b = np.pad(a, (0, 5), mode="wrap")
1128
+ assert_array_equal(a, b[:-5, :-5])
1129
+
1130
+ def test_repeated_wrapping(self):
1131
+ """
1132
+ Check wrapping on each side individually if the wrapped area is longer
1133
+ than the original array.
1134
+ """
1135
+ a = np.arange(5)
1136
+ b = np.pad(a, (12, 0), mode="wrap")
1137
+ assert_array_equal(np.r_[a, a, a, a][3:], b)
1138
+
1139
+ a = np.arange(5)
1140
+ b = np.pad(a, (0, 12), mode="wrap")
1141
+ assert_array_equal(np.r_[a, a, a, a][:-3], b)
1142
+
1143
+ def test_repeated_wrapping_multiple_origin(self):
1144
+ """
1145
+ Assert that 'wrap' pads only with multiples of the original area if
1146
+ the pad width is larger than the original array.
1147
+ """
1148
+ a = np.arange(4).reshape(2, 2)
1149
+ a = np.pad(a, [(1, 3), (3, 1)], mode='wrap')
1150
+ b = np.array(
1151
+ [[3, 2, 3, 2, 3, 2],
1152
+ [1, 0, 1, 0, 1, 0],
1153
+ [3, 2, 3, 2, 3, 2],
1154
+ [1, 0, 1, 0, 1, 0],
1155
+ [3, 2, 3, 2, 3, 2],
1156
+ [1, 0, 1, 0, 1, 0]]
1157
+ )
1158
+ assert_array_equal(a, b)
1159
+
1160
+
1161
+ class TestEdge:
1162
+ def test_check_simple(self):
1163
+ a = np.arange(12)
1164
+ a = np.reshape(a, (4, 3))
1165
+ a = np.pad(a, ((2, 3), (3, 2)), 'edge')
1166
+ b = np.array(
1167
+ [[0, 0, 0, 0, 1, 2, 2, 2],
1168
+ [0, 0, 0, 0, 1, 2, 2, 2],
1169
+
1170
+ [0, 0, 0, 0, 1, 2, 2, 2],
1171
+ [3, 3, 3, 3, 4, 5, 5, 5],
1172
+ [6, 6, 6, 6, 7, 8, 8, 8],
1173
+ [9, 9, 9, 9, 10, 11, 11, 11],
1174
+
1175
+ [9, 9, 9, 9, 10, 11, 11, 11],
1176
+ [9, 9, 9, 9, 10, 11, 11, 11],
1177
+ [9, 9, 9, 9, 10, 11, 11, 11]]
1178
+ )
1179
+ assert_array_equal(a, b)
1180
+
1181
+ def test_check_width_shape_1_2(self):
1182
+ # Check a pad_width of the form ((1, 2),).
1183
+ # Regression test for issue gh-7808.
1184
+ a = np.array([1, 2, 3])
1185
+ padded = np.pad(a, ((1, 2),), 'edge')
1186
+ expected = np.array([1, 1, 2, 3, 3, 3])
1187
+ assert_array_equal(padded, expected)
1188
+
1189
+ a = np.array([[1, 2, 3], [4, 5, 6]])
1190
+ padded = np.pad(a, ((1, 2),), 'edge')
1191
+ expected = np.pad(a, ((1, 2), (1, 2)), 'edge')
1192
+ assert_array_equal(padded, expected)
1193
+
1194
+ a = np.arange(24).reshape(2, 3, 4)
1195
+ padded = np.pad(a, ((1, 2),), 'edge')
1196
+ expected = np.pad(a, ((1, 2), (1, 2), (1, 2)), 'edge')
1197
+ assert_array_equal(padded, expected)
1198
+
1199
+
1200
+ class TestEmpty:
1201
+ def test_simple(self):
1202
+ arr = np.arange(24).reshape(4, 6)
1203
+ result = np.pad(arr, [(2, 3), (3, 1)], mode="empty")
1204
+ assert result.shape == (9, 10)
1205
+ assert_equal(arr, result[2:-3, 3:-1])
1206
+
1207
+ def test_pad_empty_dimension(self):
1208
+ arr = np.zeros((3, 0, 2))
1209
+ result = np.pad(arr, [(0,), (2,), (1,)], mode="empty")
1210
+ assert result.shape == (3, 4, 4)
1211
+
1212
+
1213
+ def test_legacy_vector_functionality():
1214
+ def _padwithtens(vector, pad_width, iaxis, kwargs):
1215
+ vector[:pad_width[0]] = 10
1216
+ vector[-pad_width[1]:] = 10
1217
+
1218
+ a = np.arange(6).reshape(2, 3)
1219
+ a = np.pad(a, 2, _padwithtens)
1220
+ b = np.array(
1221
+ [[10, 10, 10, 10, 10, 10, 10],
1222
+ [10, 10, 10, 10, 10, 10, 10],
1223
+
1224
+ [10, 10, 0, 1, 2, 10, 10],
1225
+ [10, 10, 3, 4, 5, 10, 10],
1226
+
1227
+ [10, 10, 10, 10, 10, 10, 10],
1228
+ [10, 10, 10, 10, 10, 10, 10]]
1229
+ )
1230
+ assert_array_equal(a, b)
1231
+
1232
+
1233
+ def test_unicode_mode():
1234
+ a = np.pad([1], 2, mode='constant')
1235
+ b = np.array([0, 0, 1, 0, 0])
1236
+ assert_array_equal(a, b)
1237
+
1238
+
1239
+ @pytest.mark.parametrize("mode", ["edge", "symmetric", "reflect", "wrap"])
1240
+ def test_object_input(mode):
1241
+ # Regression test for issue gh-11395.
1242
+ a = np.full((4, 3), fill_value=None)
1243
+ pad_amt = ((2, 3), (3, 2))
1244
+ b = np.full((9, 8), fill_value=None)
1245
+ assert_array_equal(np.pad(a, pad_amt, mode=mode), b)
1246
+
1247
+
1248
+ class TestPadWidth:
1249
+ @pytest.mark.parametrize("pad_width", [
1250
+ (4, 5, 6, 7),
1251
+ ((1,), (2,), (3,)),
1252
+ ((1, 2), (3, 4), (5, 6)),
1253
+ ((3, 4, 5), (0, 1, 2)),
1254
+ ])
1255
+ @pytest.mark.parametrize("mode", _all_modes.keys())
1256
+ def test_misshaped_pad_width(self, pad_width, mode):
1257
+ arr = np.arange(30).reshape((6, 5))
1258
+ match = "operands could not be broadcast together"
1259
+ with pytest.raises(ValueError, match=match):
1260
+ np.pad(arr, pad_width, mode)
1261
+
1262
+ @pytest.mark.parametrize("mode", _all_modes.keys())
1263
+ def test_misshaped_pad_width_2(self, mode):
1264
+ arr = np.arange(30).reshape((6, 5))
1265
+ match = ("input operand has more dimensions than allowed by the axis "
1266
+ "remapping")
1267
+ with pytest.raises(ValueError, match=match):
1268
+ np.pad(arr, (((3,), (4,), (5,)), ((0,), (1,), (2,))), mode)
1269
+
1270
+ @pytest.mark.parametrize(
1271
+ "pad_width", [-2, (-2,), (3, -1), ((5, 2), (-2, 3)), ((-4,), (2,))])
1272
+ @pytest.mark.parametrize("mode", _all_modes.keys())
1273
+ def test_negative_pad_width(self, pad_width, mode):
1274
+ arr = np.arange(30).reshape((6, 5))
1275
+ match = "index can't contain negative values"
1276
+ with pytest.raises(ValueError, match=match):
1277
+ np.pad(arr, pad_width, mode)
1278
+
1279
+ @pytest.mark.parametrize("pad_width, dtype", [
1280
+ ("3", None),
1281
+ ("word", None),
1282
+ (None, None),
1283
+ (object(), None),
1284
+ (3.4, None),
1285
+ (((2, 3, 4), (3, 2)), object),
1286
+ (complex(1, -1), None),
1287
+ (((-2.1, 3), (3, 2)), None),
1288
+ ])
1289
+ @pytest.mark.parametrize("mode", _all_modes.keys())
1290
+ def test_bad_type(self, pad_width, dtype, mode):
1291
+ arr = np.arange(30).reshape((6, 5))
1292
+ match = "`pad_width` must be of integral type."
1293
+ if dtype is not None:
1294
+ # avoid DeprecationWarning when not specifying dtype
1295
+ with pytest.raises(TypeError, match=match):
1296
+ np.pad(arr, np.array(pad_width, dtype=dtype), mode)
1297
+ else:
1298
+ with pytest.raises(TypeError, match=match):
1299
+ np.pad(arr, pad_width, mode)
1300
+ with pytest.raises(TypeError, match=match):
1301
+ np.pad(arr, np.array(pad_width), mode)
1302
+
1303
+ def test_pad_width_as_ndarray(self):
1304
+ a = np.arange(12)
1305
+ a = np.reshape(a, (4, 3))
1306
+ a = np.pad(a, np.array(((2, 3), (3, 2))), 'edge')
1307
+ b = np.array(
1308
+ [[0, 0, 0, 0, 1, 2, 2, 2],
1309
+ [0, 0, 0, 0, 1, 2, 2, 2],
1310
+
1311
+ [0, 0, 0, 0, 1, 2, 2, 2],
1312
+ [3, 3, 3, 3, 4, 5, 5, 5],
1313
+ [6, 6, 6, 6, 7, 8, 8, 8],
1314
+ [9, 9, 9, 9, 10, 11, 11, 11],
1315
+
1316
+ [9, 9, 9, 9, 10, 11, 11, 11],
1317
+ [9, 9, 9, 9, 10, 11, 11, 11],
1318
+ [9, 9, 9, 9, 10, 11, 11, 11]]
1319
+ )
1320
+ assert_array_equal(a, b)
1321
+
1322
+ @pytest.mark.parametrize("pad_width", [0, (0, 0), ((0, 0), (0, 0))])
1323
+ @pytest.mark.parametrize("mode", _all_modes.keys())
1324
+ def test_zero_pad_width(self, pad_width, mode):
1325
+ arr = np.arange(30).reshape(6, 5)
1326
+ assert_array_equal(arr, np.pad(arr, pad_width, mode=mode))
1327
+
1328
+
1329
+ @pytest.mark.parametrize("mode", _all_modes.keys())
1330
+ def test_kwargs(mode):
1331
+ """Test behavior of pad's kwargs for the given mode."""
1332
+ allowed = _all_modes[mode]
1333
+ not_allowed = {}
1334
+ for kwargs in _all_modes.values():
1335
+ if kwargs != allowed:
1336
+ not_allowed.update(kwargs)
1337
+ # Test if allowed keyword arguments pass
1338
+ np.pad([1, 2, 3], 1, mode, **allowed)
1339
+ # Test if prohibited keyword arguments of other modes raise an error
1340
+ for key, value in not_allowed.items():
1341
+ match = "unsupported keyword arguments for mode '{}'".format(mode)
1342
+ with pytest.raises(ValueError, match=match):
1343
+ np.pad([1, 2, 3], 1, mode, **{key: value})
1344
+
1345
+
1346
+ def test_constant_zero_default():
1347
+ arr = np.array([1, 1])
1348
+ assert_array_equal(np.pad(arr, 2), [0, 0, 1, 1, 0, 0])
1349
+
1350
+
1351
+ @pytest.mark.parametrize("mode", [1, "const", object(), None, True, False])
1352
+ def test_unsupported_mode(mode):
1353
+ match= "mode '{}' is not supported".format(mode)
1354
+ with pytest.raises(ValueError, match=match):
1355
+ np.pad([1, 2, 3], 4, mode=mode)
1356
+
1357
+
1358
+ @pytest.mark.parametrize("mode", _all_modes.keys())
1359
+ def test_non_contiguous_array(mode):
1360
+ arr = np.arange(24).reshape(4, 6)[::2, ::2]
1361
+ result = np.pad(arr, (2, 3), mode)
1362
+ assert result.shape == (7, 8)
1363
+ assert_equal(result[2:-3, 2:-3], arr)
1364
+
1365
+
1366
+ @pytest.mark.parametrize("mode", _all_modes.keys())
1367
+ def test_memory_layout_persistence(mode):
1368
+ """Test if C and F order is preserved for all pad modes."""
1369
+ x = np.ones((5, 10), order='C')
1370
+ assert np.pad(x, 5, mode).flags["C_CONTIGUOUS"]
1371
+ x = np.ones((5, 10), order='F')
1372
+ assert np.pad(x, 5, mode).flags["F_CONTIGUOUS"]
1373
+
1374
+
1375
+ @pytest.mark.parametrize("dtype", _numeric_dtypes)
1376
+ @pytest.mark.parametrize("mode", _all_modes.keys())
1377
+ def test_dtype_persistence(dtype, mode):
1378
+ arr = np.zeros((3, 2, 1), dtype=dtype)
1379
+ result = np.pad(arr, 1, mode=mode)
1380
+ assert result.dtype == dtype
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test_arraysetops.py ADDED
@@ -0,0 +1,944 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Test functions for 1D array set operations.
2
+
3
+ """
4
+ import numpy as np
5
+
6
+ from numpy.testing import (assert_array_equal, assert_equal,
7
+ assert_raises, assert_raises_regex)
8
+ from numpy.lib.arraysetops import (
9
+ ediff1d, intersect1d, setxor1d, union1d, setdiff1d, unique, in1d, isin
10
+ )
11
+ import pytest
12
+
13
+
14
+ class TestSetOps:
15
+
16
+ def test_intersect1d(self):
17
+ # unique inputs
18
+ a = np.array([5, 7, 1, 2])
19
+ b = np.array([2, 4, 3, 1, 5])
20
+
21
+ ec = np.array([1, 2, 5])
22
+ c = intersect1d(a, b, assume_unique=True)
23
+ assert_array_equal(c, ec)
24
+
25
+ # non-unique inputs
26
+ a = np.array([5, 5, 7, 1, 2])
27
+ b = np.array([2, 1, 4, 3, 3, 1, 5])
28
+
29
+ ed = np.array([1, 2, 5])
30
+ c = intersect1d(a, b)
31
+ assert_array_equal(c, ed)
32
+ assert_array_equal([], intersect1d([], []))
33
+
34
+ def test_intersect1d_array_like(self):
35
+ # See gh-11772
36
+ class Test:
37
+ def __array__(self):
38
+ return np.arange(3)
39
+
40
+ a = Test()
41
+ res = intersect1d(a, a)
42
+ assert_array_equal(res, a)
43
+ res = intersect1d([1, 2, 3], [1, 2, 3])
44
+ assert_array_equal(res, [1, 2, 3])
45
+
46
+ def test_intersect1d_indices(self):
47
+ # unique inputs
48
+ a = np.array([1, 2, 3, 4])
49
+ b = np.array([2, 1, 4, 6])
50
+ c, i1, i2 = intersect1d(a, b, assume_unique=True, return_indices=True)
51
+ ee = np.array([1, 2, 4])
52
+ assert_array_equal(c, ee)
53
+ assert_array_equal(a[i1], ee)
54
+ assert_array_equal(b[i2], ee)
55
+
56
+ # non-unique inputs
57
+ a = np.array([1, 2, 2, 3, 4, 3, 2])
58
+ b = np.array([1, 8, 4, 2, 2, 3, 2, 3])
59
+ c, i1, i2 = intersect1d(a, b, return_indices=True)
60
+ ef = np.array([1, 2, 3, 4])
61
+ assert_array_equal(c, ef)
62
+ assert_array_equal(a[i1], ef)
63
+ assert_array_equal(b[i2], ef)
64
+
65
+ # non1d, unique inputs
66
+ a = np.array([[2, 4, 5, 6], [7, 8, 1, 15]])
67
+ b = np.array([[3, 2, 7, 6], [10, 12, 8, 9]])
68
+ c, i1, i2 = intersect1d(a, b, assume_unique=True, return_indices=True)
69
+ ui1 = np.unravel_index(i1, a.shape)
70
+ ui2 = np.unravel_index(i2, b.shape)
71
+ ea = np.array([2, 6, 7, 8])
72
+ assert_array_equal(ea, a[ui1])
73
+ assert_array_equal(ea, b[ui2])
74
+
75
+ # non1d, not assumed to be uniqueinputs
76
+ a = np.array([[2, 4, 5, 6, 6], [4, 7, 8, 7, 2]])
77
+ b = np.array([[3, 2, 7, 7], [10, 12, 8, 7]])
78
+ c, i1, i2 = intersect1d(a, b, return_indices=True)
79
+ ui1 = np.unravel_index(i1, a.shape)
80
+ ui2 = np.unravel_index(i2, b.shape)
81
+ ea = np.array([2, 7, 8])
82
+ assert_array_equal(ea, a[ui1])
83
+ assert_array_equal(ea, b[ui2])
84
+
85
+ def test_setxor1d(self):
86
+ a = np.array([5, 7, 1, 2])
87
+ b = np.array([2, 4, 3, 1, 5])
88
+
89
+ ec = np.array([3, 4, 7])
90
+ c = setxor1d(a, b)
91
+ assert_array_equal(c, ec)
92
+
93
+ a = np.array([1, 2, 3])
94
+ b = np.array([6, 5, 4])
95
+
96
+ ec = np.array([1, 2, 3, 4, 5, 6])
97
+ c = setxor1d(a, b)
98
+ assert_array_equal(c, ec)
99
+
100
+ a = np.array([1, 8, 2, 3])
101
+ b = np.array([6, 5, 4, 8])
102
+
103
+ ec = np.array([1, 2, 3, 4, 5, 6])
104
+ c = setxor1d(a, b)
105
+ assert_array_equal(c, ec)
106
+
107
+ assert_array_equal([], setxor1d([], []))
108
+
109
+ def test_ediff1d(self):
110
+ zero_elem = np.array([])
111
+ one_elem = np.array([1])
112
+ two_elem = np.array([1, 2])
113
+
114
+ assert_array_equal([], ediff1d(zero_elem))
115
+ assert_array_equal([0], ediff1d(zero_elem, to_begin=0))
116
+ assert_array_equal([0], ediff1d(zero_elem, to_end=0))
117
+ assert_array_equal([-1, 0], ediff1d(zero_elem, to_begin=-1, to_end=0))
118
+ assert_array_equal([], ediff1d(one_elem))
119
+ assert_array_equal([1], ediff1d(two_elem))
120
+ assert_array_equal([7, 1, 9], ediff1d(two_elem, to_begin=7, to_end=9))
121
+ assert_array_equal([5, 6, 1, 7, 8],
122
+ ediff1d(two_elem, to_begin=[5, 6], to_end=[7, 8]))
123
+ assert_array_equal([1, 9], ediff1d(two_elem, to_end=9))
124
+ assert_array_equal([1, 7, 8], ediff1d(two_elem, to_end=[7, 8]))
125
+ assert_array_equal([7, 1], ediff1d(two_elem, to_begin=7))
126
+ assert_array_equal([5, 6, 1], ediff1d(two_elem, to_begin=[5, 6]))
127
+
128
+ @pytest.mark.parametrize("ary, prepend, append, expected", [
129
+ # should fail because trying to cast
130
+ # np.nan standard floating point value
131
+ # into an integer array:
132
+ (np.array([1, 2, 3], dtype=np.int64),
133
+ None,
134
+ np.nan,
135
+ 'to_end'),
136
+ # should fail because attempting
137
+ # to downcast to int type:
138
+ (np.array([1, 2, 3], dtype=np.int64),
139
+ np.array([5, 7, 2], dtype=np.float32),
140
+ None,
141
+ 'to_begin'),
142
+ # should fail because attempting to cast
143
+ # two special floating point values
144
+ # to integers (on both sides of ary),
145
+ # `to_begin` is in the error message as the impl checks this first:
146
+ (np.array([1., 3., 9.], dtype=np.int8),
147
+ np.nan,
148
+ np.nan,
149
+ 'to_begin'),
150
+ ])
151
+ def test_ediff1d_forbidden_type_casts(self, ary, prepend, append, expected):
152
+ # verify resolution of gh-11490
153
+
154
+ # specifically, raise an appropriate
155
+ # Exception when attempting to append or
156
+ # prepend with an incompatible type
157
+ msg = 'dtype of `{}` must be compatible'.format(expected)
158
+ with assert_raises_regex(TypeError, msg):
159
+ ediff1d(ary=ary,
160
+ to_end=append,
161
+ to_begin=prepend)
162
+
163
+ @pytest.mark.parametrize(
164
+ "ary,prepend,append,expected",
165
+ [
166
+ (np.array([1, 2, 3], dtype=np.int16),
167
+ 2**16, # will be cast to int16 under same kind rule.
168
+ 2**16 + 4,
169
+ np.array([0, 1, 1, 4], dtype=np.int16)),
170
+ (np.array([1, 2, 3], dtype=np.float32),
171
+ np.array([5], dtype=np.float64),
172
+ None,
173
+ np.array([5, 1, 1], dtype=np.float32)),
174
+ (np.array([1, 2, 3], dtype=np.int32),
175
+ 0,
176
+ 0,
177
+ np.array([0, 1, 1, 0], dtype=np.int32)),
178
+ (np.array([1, 2, 3], dtype=np.int64),
179
+ 3,
180
+ -9,
181
+ np.array([3, 1, 1, -9], dtype=np.int64)),
182
+ ]
183
+ )
184
+ def test_ediff1d_scalar_handling(self,
185
+ ary,
186
+ prepend,
187
+ append,
188
+ expected):
189
+ # maintain backwards-compatibility
190
+ # of scalar prepend / append behavior
191
+ # in ediff1d following fix for gh-11490
192
+ actual = np.ediff1d(ary=ary,
193
+ to_end=append,
194
+ to_begin=prepend)
195
+ assert_equal(actual, expected)
196
+ assert actual.dtype == expected.dtype
197
+
198
+ @pytest.mark.parametrize("kind", [None, "sort", "table"])
199
+ def test_isin(self, kind):
200
+ # the tests for in1d cover most of isin's behavior
201
+ # if in1d is removed, would need to change those tests to test
202
+ # isin instead.
203
+ def _isin_slow(a, b):
204
+ b = np.asarray(b).flatten().tolist()
205
+ return a in b
206
+ isin_slow = np.vectorize(_isin_slow, otypes=[bool], excluded={1})
207
+
208
+ def assert_isin_equal(a, b):
209
+ x = isin(a, b, kind=kind)
210
+ y = isin_slow(a, b)
211
+ assert_array_equal(x, y)
212
+
213
+ # multidimensional arrays in both arguments
214
+ a = np.arange(24).reshape([2, 3, 4])
215
+ b = np.array([[10, 20, 30], [0, 1, 3], [11, 22, 33]])
216
+ assert_isin_equal(a, b)
217
+
218
+ # array-likes as both arguments
219
+ c = [(9, 8), (7, 6)]
220
+ d = (9, 7)
221
+ assert_isin_equal(c, d)
222
+
223
+ # zero-d array:
224
+ f = np.array(3)
225
+ assert_isin_equal(f, b)
226
+ assert_isin_equal(a, f)
227
+ assert_isin_equal(f, f)
228
+
229
+ # scalar:
230
+ assert_isin_equal(5, b)
231
+ assert_isin_equal(a, 6)
232
+ assert_isin_equal(5, 6)
233
+
234
+ # empty array-like:
235
+ if kind != "table":
236
+ # An empty list will become float64,
237
+ # which is invalid for kind="table"
238
+ x = []
239
+ assert_isin_equal(x, b)
240
+ assert_isin_equal(a, x)
241
+ assert_isin_equal(x, x)
242
+
243
+ # empty array with various types:
244
+ for dtype in [bool, np.int64, np.float64]:
245
+ if kind == "table" and dtype == np.float64:
246
+ continue
247
+
248
+ if dtype in {np.int64, np.float64}:
249
+ ar = np.array([10, 20, 30], dtype=dtype)
250
+ elif dtype in {bool}:
251
+ ar = np.array([True, False, False])
252
+
253
+ empty_array = np.array([], dtype=dtype)
254
+
255
+ assert_isin_equal(empty_array, ar)
256
+ assert_isin_equal(ar, empty_array)
257
+ assert_isin_equal(empty_array, empty_array)
258
+
259
+ @pytest.mark.parametrize("kind", [None, "sort", "table"])
260
+ def test_in1d(self, kind):
261
+ # we use two different sizes for the b array here to test the
262
+ # two different paths in in1d().
263
+ for mult in (1, 10):
264
+ # One check without np.array to make sure lists are handled correct
265
+ a = [5, 7, 1, 2]
266
+ b = [2, 4, 3, 1, 5] * mult
267
+ ec = np.array([True, False, True, True])
268
+ c = in1d(a, b, assume_unique=True, kind=kind)
269
+ assert_array_equal(c, ec)
270
+
271
+ a[0] = 8
272
+ ec = np.array([False, False, True, True])
273
+ c = in1d(a, b, assume_unique=True, kind=kind)
274
+ assert_array_equal(c, ec)
275
+
276
+ a[0], a[3] = 4, 8
277
+ ec = np.array([True, False, True, False])
278
+ c = in1d(a, b, assume_unique=True, kind=kind)
279
+ assert_array_equal(c, ec)
280
+
281
+ a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5])
282
+ b = [2, 3, 4] * mult
283
+ ec = [False, True, False, True, True, True, True, True, True,
284
+ False, True, False, False, False]
285
+ c = in1d(a, b, kind=kind)
286
+ assert_array_equal(c, ec)
287
+
288
+ b = b + [5, 5, 4] * mult
289
+ ec = [True, True, True, True, True, True, True, True, True, True,
290
+ True, False, True, True]
291
+ c = in1d(a, b, kind=kind)
292
+ assert_array_equal(c, ec)
293
+
294
+ a = np.array([5, 7, 1, 2])
295
+ b = np.array([2, 4, 3, 1, 5] * mult)
296
+ ec = np.array([True, False, True, True])
297
+ c = in1d(a, b, kind=kind)
298
+ assert_array_equal(c, ec)
299
+
300
+ a = np.array([5, 7, 1, 1, 2])
301
+ b = np.array([2, 4, 3, 3, 1, 5] * mult)
302
+ ec = np.array([True, False, True, True, True])
303
+ c = in1d(a, b, kind=kind)
304
+ assert_array_equal(c, ec)
305
+
306
+ a = np.array([5, 5])
307
+ b = np.array([2, 2] * mult)
308
+ ec = np.array([False, False])
309
+ c = in1d(a, b, kind=kind)
310
+ assert_array_equal(c, ec)
311
+
312
+ a = np.array([5])
313
+ b = np.array([2])
314
+ ec = np.array([False])
315
+ c = in1d(a, b, kind=kind)
316
+ assert_array_equal(c, ec)
317
+
318
+ if kind in {None, "sort"}:
319
+ assert_array_equal(in1d([], [], kind=kind), [])
320
+
321
+ def test_in1d_char_array(self):
322
+ a = np.array(['a', 'b', 'c', 'd', 'e', 'c', 'e', 'b'])
323
+ b = np.array(['a', 'c'])
324
+
325
+ ec = np.array([True, False, True, False, False, True, False, False])
326
+ c = in1d(a, b)
327
+
328
+ assert_array_equal(c, ec)
329
+
330
+ @pytest.mark.parametrize("kind", [None, "sort", "table"])
331
+ def test_in1d_invert(self, kind):
332
+ "Test in1d's invert parameter"
333
+ # We use two different sizes for the b array here to test the
334
+ # two different paths in in1d().
335
+ for mult in (1, 10):
336
+ a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5])
337
+ b = [2, 3, 4] * mult
338
+ assert_array_equal(np.invert(in1d(a, b, kind=kind)),
339
+ in1d(a, b, invert=True, kind=kind))
340
+
341
+ # float:
342
+ if kind in {None, "sort"}:
343
+ for mult in (1, 10):
344
+ a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5],
345
+ dtype=np.float32)
346
+ b = [2, 3, 4] * mult
347
+ b = np.array(b, dtype=np.float32)
348
+ assert_array_equal(np.invert(in1d(a, b, kind=kind)),
349
+ in1d(a, b, invert=True, kind=kind))
350
+
351
+ @pytest.mark.parametrize("kind", [None, "sort", "table"])
352
+ def test_in1d_ravel(self, kind):
353
+ # Test that in1d ravels its input arrays. This is not documented
354
+ # behavior however. The test is to ensure consistentency.
355
+ a = np.arange(6).reshape(2, 3)
356
+ b = np.arange(3, 9).reshape(3, 2)
357
+ long_b = np.arange(3, 63).reshape(30, 2)
358
+ ec = np.array([False, False, False, True, True, True])
359
+
360
+ assert_array_equal(in1d(a, b, assume_unique=True, kind=kind),
361
+ ec)
362
+ assert_array_equal(in1d(a, b, assume_unique=False,
363
+ kind=kind),
364
+ ec)
365
+ assert_array_equal(in1d(a, long_b, assume_unique=True,
366
+ kind=kind),
367
+ ec)
368
+ assert_array_equal(in1d(a, long_b, assume_unique=False,
369
+ kind=kind),
370
+ ec)
371
+
372
+ def test_in1d_hit_alternate_algorithm(self):
373
+ """Hit the standard isin code with integers"""
374
+ # Need extreme range to hit standard code
375
+ # This hits it without the use of kind='table'
376
+ a = np.array([5, 4, 5, 3, 4, 4, 1e9], dtype=np.int64)
377
+ b = np.array([2, 3, 4, 1e9], dtype=np.int64)
378
+ expected = np.array([0, 1, 0, 1, 1, 1, 1], dtype=bool)
379
+ assert_array_equal(expected, in1d(a, b))
380
+ assert_array_equal(np.invert(expected), in1d(a, b, invert=True))
381
+
382
+ a = np.array([5, 7, 1, 2], dtype=np.int64)
383
+ b = np.array([2, 4, 3, 1, 5, 1e9], dtype=np.int64)
384
+ ec = np.array([True, False, True, True])
385
+ c = in1d(a, b, assume_unique=True)
386
+ assert_array_equal(c, ec)
387
+
388
+ @pytest.mark.parametrize("kind", [None, "sort", "table"])
389
+ def test_in1d_boolean(self, kind):
390
+ """Test that in1d works for boolean input"""
391
+ a = np.array([True, False])
392
+ b = np.array([False, False, False])
393
+ expected = np.array([False, True])
394
+ assert_array_equal(expected,
395
+ in1d(a, b, kind=kind))
396
+ assert_array_equal(np.invert(expected),
397
+ in1d(a, b, invert=True, kind=kind))
398
+
399
+ @pytest.mark.parametrize("kind", [None, "sort"])
400
+ def test_in1d_timedelta(self, kind):
401
+ """Test that in1d works for timedelta input"""
402
+ rstate = np.random.RandomState(0)
403
+ a = rstate.randint(0, 100, size=10)
404
+ b = rstate.randint(0, 100, size=10)
405
+ truth = in1d(a, b)
406
+ a_timedelta = a.astype("timedelta64[s]")
407
+ b_timedelta = b.astype("timedelta64[s]")
408
+ assert_array_equal(truth, in1d(a_timedelta, b_timedelta, kind=kind))
409
+
410
+ def test_in1d_table_timedelta_fails(self):
411
+ a = np.array([0, 1, 2], dtype="timedelta64[s]")
412
+ b = a
413
+ # Make sure it raises a value error:
414
+ with pytest.raises(ValueError):
415
+ in1d(a, b, kind="table")
416
+
417
+ @pytest.mark.parametrize(
418
+ "dtype1,dtype2",
419
+ [
420
+ (np.int8, np.int16),
421
+ (np.int16, np.int8),
422
+ (np.uint8, np.uint16),
423
+ (np.uint16, np.uint8),
424
+ (np.uint8, np.int16),
425
+ (np.int16, np.uint8),
426
+ ]
427
+ )
428
+ @pytest.mark.parametrize("kind", [None, "sort", "table"])
429
+ def test_in1d_mixed_dtype(self, dtype1, dtype2, kind):
430
+ """Test that in1d works as expected for mixed dtype input."""
431
+ is_dtype2_signed = np.issubdtype(dtype2, np.signedinteger)
432
+ ar1 = np.array([0, 0, 1, 1], dtype=dtype1)
433
+
434
+ if is_dtype2_signed:
435
+ ar2 = np.array([-128, 0, 127], dtype=dtype2)
436
+ else:
437
+ ar2 = np.array([127, 0, 255], dtype=dtype2)
438
+
439
+ expected = np.array([True, True, False, False])
440
+
441
+ expect_failure = kind == "table" and any((
442
+ dtype1 == np.int8 and dtype2 == np.int16,
443
+ dtype1 == np.int16 and dtype2 == np.int8
444
+ ))
445
+
446
+ if expect_failure:
447
+ with pytest.raises(RuntimeError, match="exceed the maximum"):
448
+ in1d(ar1, ar2, kind=kind)
449
+ else:
450
+ assert_array_equal(in1d(ar1, ar2, kind=kind), expected)
451
+
452
+ @pytest.mark.parametrize("kind", [None, "sort", "table"])
453
+ def test_in1d_mixed_boolean(self, kind):
454
+ """Test that in1d works as expected for bool/int input."""
455
+ for dtype in np.typecodes["AllInteger"]:
456
+ a = np.array([True, False, False], dtype=bool)
457
+ b = np.array([0, 0, 0, 0], dtype=dtype)
458
+ expected = np.array([False, True, True], dtype=bool)
459
+ assert_array_equal(in1d(a, b, kind=kind), expected)
460
+
461
+ a, b = b, a
462
+ expected = np.array([True, True, True, True], dtype=bool)
463
+ assert_array_equal(in1d(a, b, kind=kind), expected)
464
+
465
+ def test_in1d_first_array_is_object(self):
466
+ ar1 = [None]
467
+ ar2 = np.array([1]*10)
468
+ expected = np.array([False])
469
+ result = np.in1d(ar1, ar2)
470
+ assert_array_equal(result, expected)
471
+
472
+ def test_in1d_second_array_is_object(self):
473
+ ar1 = 1
474
+ ar2 = np.array([None]*10)
475
+ expected = np.array([False])
476
+ result = np.in1d(ar1, ar2)
477
+ assert_array_equal(result, expected)
478
+
479
+ def test_in1d_both_arrays_are_object(self):
480
+ ar1 = [None]
481
+ ar2 = np.array([None]*10)
482
+ expected = np.array([True])
483
+ result = np.in1d(ar1, ar2)
484
+ assert_array_equal(result, expected)
485
+
486
+ def test_in1d_both_arrays_have_structured_dtype(self):
487
+ # Test arrays of a structured data type containing an integer field
488
+ # and a field of dtype `object` allowing for arbitrary Python objects
489
+ dt = np.dtype([('field1', int), ('field2', object)])
490
+ ar1 = np.array([(1, None)], dtype=dt)
491
+ ar2 = np.array([(1, None)]*10, dtype=dt)
492
+ expected = np.array([True])
493
+ result = np.in1d(ar1, ar2)
494
+ assert_array_equal(result, expected)
495
+
496
+ def test_in1d_with_arrays_containing_tuples(self):
497
+ ar1 = np.array([(1,), 2], dtype=object)
498
+ ar2 = np.array([(1,), 2], dtype=object)
499
+ expected = np.array([True, True])
500
+ result = np.in1d(ar1, ar2)
501
+ assert_array_equal(result, expected)
502
+ result = np.in1d(ar1, ar2, invert=True)
503
+ assert_array_equal(result, np.invert(expected))
504
+
505
+ # An integer is added at the end of the array to make sure
506
+ # that the array builder will create the array with tuples
507
+ # and after it's created the integer is removed.
508
+ # There's a bug in the array constructor that doesn't handle
509
+ # tuples properly and adding the integer fixes that.
510
+ ar1 = np.array([(1,), (2, 1), 1], dtype=object)
511
+ ar1 = ar1[:-1]
512
+ ar2 = np.array([(1,), (2, 1), 1], dtype=object)
513
+ ar2 = ar2[:-1]
514
+ expected = np.array([True, True])
515
+ result = np.in1d(ar1, ar2)
516
+ assert_array_equal(result, expected)
517
+ result = np.in1d(ar1, ar2, invert=True)
518
+ assert_array_equal(result, np.invert(expected))
519
+
520
+ ar1 = np.array([(1,), (2, 3), 1], dtype=object)
521
+ ar1 = ar1[:-1]
522
+ ar2 = np.array([(1,), 2], dtype=object)
523
+ expected = np.array([True, False])
524
+ result = np.in1d(ar1, ar2)
525
+ assert_array_equal(result, expected)
526
+ result = np.in1d(ar1, ar2, invert=True)
527
+ assert_array_equal(result, np.invert(expected))
528
+
529
+ def test_in1d_errors(self):
530
+ """Test that in1d raises expected errors."""
531
+
532
+ # Error 1: `kind` is not one of 'sort' 'table' or None.
533
+ ar1 = np.array([1, 2, 3, 4, 5])
534
+ ar2 = np.array([2, 4, 6, 8, 10])
535
+ assert_raises(ValueError, in1d, ar1, ar2, kind='quicksort')
536
+
537
+ # Error 2: `kind="table"` does not work for non-integral arrays.
538
+ obj_ar1 = np.array([1, 'a', 3, 'b', 5], dtype=object)
539
+ obj_ar2 = np.array([1, 'a', 3, 'b', 5], dtype=object)
540
+ assert_raises(ValueError, in1d, obj_ar1, obj_ar2, kind='table')
541
+
542
+ for dtype in [np.int32, np.int64]:
543
+ ar1 = np.array([-1, 2, 3, 4, 5], dtype=dtype)
544
+ # The range of this array will overflow:
545
+ overflow_ar2 = np.array([-1, np.iinfo(dtype).max], dtype=dtype)
546
+
547
+ # Error 3: `kind="table"` will trigger a runtime error
548
+ # if there is an integer overflow expected when computing the
549
+ # range of ar2
550
+ assert_raises(
551
+ RuntimeError,
552
+ in1d, ar1, overflow_ar2, kind='table'
553
+ )
554
+
555
+ # Non-error: `kind=None` will *not* trigger a runtime error
556
+ # if there is an integer overflow, it will switch to
557
+ # the `sort` algorithm.
558
+ result = np.in1d(ar1, overflow_ar2, kind=None)
559
+ assert_array_equal(result, [True] + [False] * 4)
560
+ result = np.in1d(ar1, overflow_ar2, kind='sort')
561
+ assert_array_equal(result, [True] + [False] * 4)
562
+
563
+ def test_union1d(self):
564
+ a = np.array([5, 4, 7, 1, 2])
565
+ b = np.array([2, 4, 3, 3, 2, 1, 5])
566
+
567
+ ec = np.array([1, 2, 3, 4, 5, 7])
568
+ c = union1d(a, b)
569
+ assert_array_equal(c, ec)
570
+
571
+ # Tests gh-10340, arguments to union1d should be
572
+ # flattened if they are not already 1D
573
+ x = np.array([[0, 1, 2], [3, 4, 5]])
574
+ y = np.array([0, 1, 2, 3, 4])
575
+ ez = np.array([0, 1, 2, 3, 4, 5])
576
+ z = union1d(x, y)
577
+ assert_array_equal(z, ez)
578
+
579
+ assert_array_equal([], union1d([], []))
580
+
581
+ def test_setdiff1d(self):
582
+ a = np.array([6, 5, 4, 7, 1, 2, 7, 4])
583
+ b = np.array([2, 4, 3, 3, 2, 1, 5])
584
+
585
+ ec = np.array([6, 7])
586
+ c = setdiff1d(a, b)
587
+ assert_array_equal(c, ec)
588
+
589
+ a = np.arange(21)
590
+ b = np.arange(19)
591
+ ec = np.array([19, 20])
592
+ c = setdiff1d(a, b)
593
+ assert_array_equal(c, ec)
594
+
595
+ assert_array_equal([], setdiff1d([], []))
596
+ a = np.array((), np.uint32)
597
+ assert_equal(setdiff1d(a, []).dtype, np.uint32)
598
+
599
+ def test_setdiff1d_unique(self):
600
+ a = np.array([3, 2, 1])
601
+ b = np.array([7, 5, 2])
602
+ expected = np.array([3, 1])
603
+ actual = setdiff1d(a, b, assume_unique=True)
604
+ assert_equal(actual, expected)
605
+
606
+ def test_setdiff1d_char_array(self):
607
+ a = np.array(['a', 'b', 'c'])
608
+ b = np.array(['a', 'b', 's'])
609
+ assert_array_equal(setdiff1d(a, b), np.array(['c']))
610
+
611
+ def test_manyways(self):
612
+ a = np.array([5, 7, 1, 2, 8])
613
+ b = np.array([9, 8, 2, 4, 3, 1, 5])
614
+
615
+ c1 = setxor1d(a, b)
616
+ aux1 = intersect1d(a, b)
617
+ aux2 = union1d(a, b)
618
+ c2 = setdiff1d(aux2, aux1)
619
+ assert_array_equal(c1, c2)
620
+
621
+
622
+ class TestUnique:
623
+
624
+ def test_unique_1d(self):
625
+
626
+ def check_all(a, b, i1, i2, c, dt):
627
+ base_msg = 'check {0} failed for type {1}'
628
+
629
+ msg = base_msg.format('values', dt)
630
+ v = unique(a)
631
+ assert_array_equal(v, b, msg)
632
+
633
+ msg = base_msg.format('return_index', dt)
634
+ v, j = unique(a, True, False, False)
635
+ assert_array_equal(v, b, msg)
636
+ assert_array_equal(j, i1, msg)
637
+
638
+ msg = base_msg.format('return_inverse', dt)
639
+ v, j = unique(a, False, True, False)
640
+ assert_array_equal(v, b, msg)
641
+ assert_array_equal(j, i2, msg)
642
+
643
+ msg = base_msg.format('return_counts', dt)
644
+ v, j = unique(a, False, False, True)
645
+ assert_array_equal(v, b, msg)
646
+ assert_array_equal(j, c, msg)
647
+
648
+ msg = base_msg.format('return_index and return_inverse', dt)
649
+ v, j1, j2 = unique(a, True, True, False)
650
+ assert_array_equal(v, b, msg)
651
+ assert_array_equal(j1, i1, msg)
652
+ assert_array_equal(j2, i2, msg)
653
+
654
+ msg = base_msg.format('return_index and return_counts', dt)
655
+ v, j1, j2 = unique(a, True, False, True)
656
+ assert_array_equal(v, b, msg)
657
+ assert_array_equal(j1, i1, msg)
658
+ assert_array_equal(j2, c, msg)
659
+
660
+ msg = base_msg.format('return_inverse and return_counts', dt)
661
+ v, j1, j2 = unique(a, False, True, True)
662
+ assert_array_equal(v, b, msg)
663
+ assert_array_equal(j1, i2, msg)
664
+ assert_array_equal(j2, c, msg)
665
+
666
+ msg = base_msg.format(('return_index, return_inverse '
667
+ 'and return_counts'), dt)
668
+ v, j1, j2, j3 = unique(a, True, True, True)
669
+ assert_array_equal(v, b, msg)
670
+ assert_array_equal(j1, i1, msg)
671
+ assert_array_equal(j2, i2, msg)
672
+ assert_array_equal(j3, c, msg)
673
+
674
+ a = [5, 7, 1, 2, 1, 5, 7]*10
675
+ b = [1, 2, 5, 7]
676
+ i1 = [2, 3, 0, 1]
677
+ i2 = [2, 3, 0, 1, 0, 2, 3]*10
678
+ c = np.multiply([2, 1, 2, 2], 10)
679
+
680
+ # test for numeric arrays
681
+ types = []
682
+ types.extend(np.typecodes['AllInteger'])
683
+ types.extend(np.typecodes['AllFloat'])
684
+ types.append('datetime64[D]')
685
+ types.append('timedelta64[D]')
686
+ for dt in types:
687
+ aa = np.array(a, dt)
688
+ bb = np.array(b, dt)
689
+ check_all(aa, bb, i1, i2, c, dt)
690
+
691
+ # test for object arrays
692
+ dt = 'O'
693
+ aa = np.empty(len(a), dt)
694
+ aa[:] = a
695
+ bb = np.empty(len(b), dt)
696
+ bb[:] = b
697
+ check_all(aa, bb, i1, i2, c, dt)
698
+
699
+ # test for structured arrays
700
+ dt = [('', 'i'), ('', 'i')]
701
+ aa = np.array(list(zip(a, a)), dt)
702
+ bb = np.array(list(zip(b, b)), dt)
703
+ check_all(aa, bb, i1, i2, c, dt)
704
+
705
+ # test for ticket #2799
706
+ aa = [1. + 0.j, 1 - 1.j, 1]
707
+ assert_array_equal(np.unique(aa), [1. - 1.j, 1. + 0.j])
708
+
709
+ # test for ticket #4785
710
+ a = [(1, 2), (1, 2), (2, 3)]
711
+ unq = [1, 2, 3]
712
+ inv = [0, 1, 0, 1, 1, 2]
713
+ a1 = unique(a)
714
+ assert_array_equal(a1, unq)
715
+ a2, a2_inv = unique(a, return_inverse=True)
716
+ assert_array_equal(a2, unq)
717
+ assert_array_equal(a2_inv, inv)
718
+
719
+ # test for chararrays with return_inverse (gh-5099)
720
+ a = np.chararray(5)
721
+ a[...] = ''
722
+ a2, a2_inv = np.unique(a, return_inverse=True)
723
+ assert_array_equal(a2_inv, np.zeros(5))
724
+
725
+ # test for ticket #9137
726
+ a = []
727
+ a1_idx = np.unique(a, return_index=True)[1]
728
+ a2_inv = np.unique(a, return_inverse=True)[1]
729
+ a3_idx, a3_inv = np.unique(a, return_index=True,
730
+ return_inverse=True)[1:]
731
+ assert_equal(a1_idx.dtype, np.intp)
732
+ assert_equal(a2_inv.dtype, np.intp)
733
+ assert_equal(a3_idx.dtype, np.intp)
734
+ assert_equal(a3_inv.dtype, np.intp)
735
+
736
+ # test for ticket 2111 - float
737
+ a = [2.0, np.nan, 1.0, np.nan]
738
+ ua = [1.0, 2.0, np.nan]
739
+ ua_idx = [2, 0, 1]
740
+ ua_inv = [1, 2, 0, 2]
741
+ ua_cnt = [1, 1, 2]
742
+ assert_equal(np.unique(a), ua)
743
+ assert_equal(np.unique(a, return_index=True), (ua, ua_idx))
744
+ assert_equal(np.unique(a, return_inverse=True), (ua, ua_inv))
745
+ assert_equal(np.unique(a, return_counts=True), (ua, ua_cnt))
746
+
747
+ # test for ticket 2111 - complex
748
+ a = [2.0-1j, np.nan, 1.0+1j, complex(0.0, np.nan), complex(1.0, np.nan)]
749
+ ua = [1.0+1j, 2.0-1j, complex(0.0, np.nan)]
750
+ ua_idx = [2, 0, 3]
751
+ ua_inv = [1, 2, 0, 2, 2]
752
+ ua_cnt = [1, 1, 3]
753
+ assert_equal(np.unique(a), ua)
754
+ assert_equal(np.unique(a, return_index=True), (ua, ua_idx))
755
+ assert_equal(np.unique(a, return_inverse=True), (ua, ua_inv))
756
+ assert_equal(np.unique(a, return_counts=True), (ua, ua_cnt))
757
+
758
+ # test for ticket 2111 - datetime64
759
+ nat = np.datetime64('nat')
760
+ a = [np.datetime64('2020-12-26'), nat, np.datetime64('2020-12-24'), nat]
761
+ ua = [np.datetime64('2020-12-24'), np.datetime64('2020-12-26'), nat]
762
+ ua_idx = [2, 0, 1]
763
+ ua_inv = [1, 2, 0, 2]
764
+ ua_cnt = [1, 1, 2]
765
+ assert_equal(np.unique(a), ua)
766
+ assert_equal(np.unique(a, return_index=True), (ua, ua_idx))
767
+ assert_equal(np.unique(a, return_inverse=True), (ua, ua_inv))
768
+ assert_equal(np.unique(a, return_counts=True), (ua, ua_cnt))
769
+
770
+ # test for ticket 2111 - timedelta
771
+ nat = np.timedelta64('nat')
772
+ a = [np.timedelta64(1, 'D'), nat, np.timedelta64(1, 'h'), nat]
773
+ ua = [np.timedelta64(1, 'h'), np.timedelta64(1, 'D'), nat]
774
+ ua_idx = [2, 0, 1]
775
+ ua_inv = [1, 2, 0, 2]
776
+ ua_cnt = [1, 1, 2]
777
+ assert_equal(np.unique(a), ua)
778
+ assert_equal(np.unique(a, return_index=True), (ua, ua_idx))
779
+ assert_equal(np.unique(a, return_inverse=True), (ua, ua_inv))
780
+ assert_equal(np.unique(a, return_counts=True), (ua, ua_cnt))
781
+
782
+ # test for gh-19300
783
+ all_nans = [np.nan] * 4
784
+ ua = [np.nan]
785
+ ua_idx = [0]
786
+ ua_inv = [0, 0, 0, 0]
787
+ ua_cnt = [4]
788
+ assert_equal(np.unique(all_nans), ua)
789
+ assert_equal(np.unique(all_nans, return_index=True), (ua, ua_idx))
790
+ assert_equal(np.unique(all_nans, return_inverse=True), (ua, ua_inv))
791
+ assert_equal(np.unique(all_nans, return_counts=True), (ua, ua_cnt))
792
+
793
+ def test_unique_axis_errors(self):
794
+ assert_raises(TypeError, self._run_axis_tests, object)
795
+ assert_raises(TypeError, self._run_axis_tests,
796
+ [('a', int), ('b', object)])
797
+
798
+ assert_raises(np.AxisError, unique, np.arange(10), axis=2)
799
+ assert_raises(np.AxisError, unique, np.arange(10), axis=-2)
800
+
801
+ def test_unique_axis_list(self):
802
+ msg = "Unique failed on list of lists"
803
+ inp = [[0, 1, 0], [0, 1, 0]]
804
+ inp_arr = np.asarray(inp)
805
+ assert_array_equal(unique(inp, axis=0), unique(inp_arr, axis=0), msg)
806
+ assert_array_equal(unique(inp, axis=1), unique(inp_arr, axis=1), msg)
807
+
808
+ def test_unique_axis(self):
809
+ types = []
810
+ types.extend(np.typecodes['AllInteger'])
811
+ types.extend(np.typecodes['AllFloat'])
812
+ types.append('datetime64[D]')
813
+ types.append('timedelta64[D]')
814
+ types.append([('a', int), ('b', int)])
815
+ types.append([('a', int), ('b', float)])
816
+
817
+ for dtype in types:
818
+ self._run_axis_tests(dtype)
819
+
820
+ msg = 'Non-bitwise-equal booleans test failed'
821
+ data = np.arange(10, dtype=np.uint8).reshape(-1, 2).view(bool)
822
+ result = np.array([[False, True], [True, True]], dtype=bool)
823
+ assert_array_equal(unique(data, axis=0), result, msg)
824
+
825
+ msg = 'Negative zero equality test failed'
826
+ data = np.array([[-0.0, 0.0], [0.0, -0.0], [-0.0, 0.0], [0.0, -0.0]])
827
+ result = np.array([[-0.0, 0.0]])
828
+ assert_array_equal(unique(data, axis=0), result, msg)
829
+
830
+ @pytest.mark.parametrize("axis", [0, -1])
831
+ def test_unique_1d_with_axis(self, axis):
832
+ x = np.array([4, 3, 2, 3, 2, 1, 2, 2])
833
+ uniq = unique(x, axis=axis)
834
+ assert_array_equal(uniq, [1, 2, 3, 4])
835
+
836
+ def test_unique_axis_zeros(self):
837
+ # issue 15559
838
+ single_zero = np.empty(shape=(2, 0), dtype=np.int8)
839
+ uniq, idx, inv, cnt = unique(single_zero, axis=0, return_index=True,
840
+ return_inverse=True, return_counts=True)
841
+
842
+ # there's 1 element of shape (0,) along axis 0
843
+ assert_equal(uniq.dtype, single_zero.dtype)
844
+ assert_array_equal(uniq, np.empty(shape=(1, 0)))
845
+ assert_array_equal(idx, np.array([0]))
846
+ assert_array_equal(inv, np.array([0, 0]))
847
+ assert_array_equal(cnt, np.array([2]))
848
+
849
+ # there's 0 elements of shape (2,) along axis 1
850
+ uniq, idx, inv, cnt = unique(single_zero, axis=1, return_index=True,
851
+ return_inverse=True, return_counts=True)
852
+
853
+ assert_equal(uniq.dtype, single_zero.dtype)
854
+ assert_array_equal(uniq, np.empty(shape=(2, 0)))
855
+ assert_array_equal(idx, np.array([]))
856
+ assert_array_equal(inv, np.array([]))
857
+ assert_array_equal(cnt, np.array([]))
858
+
859
+ # test a "complicated" shape
860
+ shape = (0, 2, 0, 3, 0, 4, 0)
861
+ multiple_zeros = np.empty(shape=shape)
862
+ for axis in range(len(shape)):
863
+ expected_shape = list(shape)
864
+ if shape[axis] == 0:
865
+ expected_shape[axis] = 0
866
+ else:
867
+ expected_shape[axis] = 1
868
+
869
+ assert_array_equal(unique(multiple_zeros, axis=axis),
870
+ np.empty(shape=expected_shape))
871
+
872
+ def test_unique_masked(self):
873
+ # issue 8664
874
+ x = np.array([64, 0, 1, 2, 3, 63, 63, 0, 0, 0, 1, 2, 0, 63, 0],
875
+ dtype='uint8')
876
+ y = np.ma.masked_equal(x, 0)
877
+
878
+ v = np.unique(y)
879
+ v2, i, c = np.unique(y, return_index=True, return_counts=True)
880
+
881
+ msg = 'Unique returned different results when asked for index'
882
+ assert_array_equal(v.data, v2.data, msg)
883
+ assert_array_equal(v.mask, v2.mask, msg)
884
+
885
+ def test_unique_sort_order_with_axis(self):
886
+ # These tests fail if sorting along axis is done by treating subarrays
887
+ # as unsigned byte strings. See gh-10495.
888
+ fmt = "sort order incorrect for integer type '%s'"
889
+ for dt in 'bhilq':
890
+ a = np.array([[-1], [0]], dt)
891
+ b = np.unique(a, axis=0)
892
+ assert_array_equal(a, b, fmt % dt)
893
+
894
+ def _run_axis_tests(self, dtype):
895
+ data = np.array([[0, 1, 0, 0],
896
+ [1, 0, 0, 0],
897
+ [0, 1, 0, 0],
898
+ [1, 0, 0, 0]]).astype(dtype)
899
+
900
+ msg = 'Unique with 1d array and axis=0 failed'
901
+ result = np.array([0, 1])
902
+ assert_array_equal(unique(data), result.astype(dtype), msg)
903
+
904
+ msg = 'Unique with 2d array and axis=0 failed'
905
+ result = np.array([[0, 1, 0, 0], [1, 0, 0, 0]])
906
+ assert_array_equal(unique(data, axis=0), result.astype(dtype), msg)
907
+
908
+ msg = 'Unique with 2d array and axis=1 failed'
909
+ result = np.array([[0, 0, 1], [0, 1, 0], [0, 0, 1], [0, 1, 0]])
910
+ assert_array_equal(unique(data, axis=1), result.astype(dtype), msg)
911
+
912
+ msg = 'Unique with 3d array and axis=2 failed'
913
+ data3d = np.array([[[1, 1],
914
+ [1, 0]],
915
+ [[0, 1],
916
+ [0, 0]]]).astype(dtype)
917
+ result = np.take(data3d, [1, 0], axis=2)
918
+ assert_array_equal(unique(data3d, axis=2), result, msg)
919
+
920
+ uniq, idx, inv, cnt = unique(data, axis=0, return_index=True,
921
+ return_inverse=True, return_counts=True)
922
+ msg = "Unique's return_index=True failed with axis=0"
923
+ assert_array_equal(data[idx], uniq, msg)
924
+ msg = "Unique's return_inverse=True failed with axis=0"
925
+ assert_array_equal(uniq[inv], data)
926
+ msg = "Unique's return_counts=True failed with axis=0"
927
+ assert_array_equal(cnt, np.array([2, 2]), msg)
928
+
929
+ uniq, idx, inv, cnt = unique(data, axis=1, return_index=True,
930
+ return_inverse=True, return_counts=True)
931
+ msg = "Unique's return_index=True failed with axis=1"
932
+ assert_array_equal(data[:, idx], uniq)
933
+ msg = "Unique's return_inverse=True failed with axis=1"
934
+ assert_array_equal(uniq[:, inv], data)
935
+ msg = "Unique's return_counts=True failed with axis=1"
936
+ assert_array_equal(cnt, np.array([2, 1, 1]), msg)
937
+
938
+ def test_unique_nanequals(self):
939
+ # issue 20326
940
+ a = np.array([1, 1, np.nan, np.nan, np.nan])
941
+ unq = np.unique(a)
942
+ not_unq = np.unique(a, equal_nan=False)
943
+ assert_array_equal(unq, np.array([1, np.nan]))
944
+ assert_array_equal(not_unq, np.array([1, np.nan, np.nan, np.nan]))
data/tefig/envs/olmes-eval/lib/python3.10/site-packages/numpy/lib/tests/test_arrayterator.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from operator import mul
2
+ from functools import reduce
3
+
4
+ import numpy as np
5
+ from numpy.random import randint
6
+ from numpy.lib import Arrayterator
7
+ from numpy.testing import assert_
8
+
9
+
10
+ def test():
11
+ np.random.seed(np.arange(10))
12
+
13
+ # Create a random array
14
+ ndims = randint(5)+1
15
+ shape = tuple(randint(10)+1 for dim in range(ndims))
16
+ els = reduce(mul, shape)
17
+ a = np.arange(els)
18
+ a.shape = shape
19
+
20
+ buf_size = randint(2*els)
21
+ b = Arrayterator(a, buf_size)
22
+
23
+ # Check that each block has at most ``buf_size`` elements
24
+ for block in b:
25
+ assert_(len(block.flat) <= (buf_size or els))
26
+
27
+ # Check that all elements are iterated correctly
28
+ assert_(list(b.flat) == list(a.flat))
29
+
30
+ # Slice arrayterator
31
+ start = [randint(dim) for dim in shape]
32
+ stop = [randint(dim)+1 for dim in shape]
33
+ step = [randint(dim)+1 for dim in shape]
34
+ slice_ = tuple(slice(*t) for t in zip(start, stop, step))
35
+ c = b[slice_]
36
+ d = a[slice_]
37
+
38
+ # Check that each block has at most ``buf_size`` elements
39
+ for block in c:
40
+ assert_(len(block.flat) <= (buf_size or els))
41
+
42
+ # Check that the arrayterator is sliced correctly
43
+ assert_(np.all(c.__array__() == d))
44
+
45
+ # Check that all elements are iterated correctly
46
+ assert_(list(c.flat) == list(d.flat))