Add loan-advisor-agent (part 4)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +31 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/Transparent Busy.ani +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-3ch-5S-24bit.wav +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-3ch-5S-36bit.wav +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-3ch-5S-45bit.wav +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-3ch-5S-53bit.wav +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-3ch-5S-64bit.wav +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-4ch-9S-12bit.wav +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-5ch-9S-5bit.wav +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/various_compressed.sav +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_fortran.py +344 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_idl.py +482 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_mmio.py +829 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_netcdf.py +553 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_paths.py +93 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_wavfile.py +520 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/io/wavfile.py +944 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__init__.pxd +1 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__init__.py +234 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/__init__.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_basic.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_cholesky.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_cossin.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_ldl.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_lu.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_polar.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_qr.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_qz.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_schur.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_svd.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_expm_frechet.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_matfuncs.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_misc.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_procrustes.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_sketches.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_solvers.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_special_matrices.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/basic.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/blas.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp_cholesky.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp_lu.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp_qr.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp_schur.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp_svd.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/interpolative.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/lapack.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/matfuncs.cpython-312.pyc +0 -0
- loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/misc.cpython-312.pyc +0 -0
.gitattributes
CHANGED
|
@@ -580,3 +580,34 @@ loan-advisor-agent/.venv/Lib/site-packages/scipy/interpolate/_rgi_cython.cp312-w
|
|
| 580 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/_fast_matrix_market/_fmm_core.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 581 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/matlab/_mio5_utils.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 582 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/matlab/_streams.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/_fast_matrix_market/_fmm_core.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 581 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/matlab/_mio5_utils.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 582 |
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/matlab/_streams.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 583 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_batched_linalg.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 584 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_cythonized_array_utils.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 585 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_decomp_interpolative.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 586 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_decomp_lu_cython.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 587 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_decomp_update.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 588 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_fblas.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 589 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_flapack.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 590 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_linalg_pythran.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 591 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_matfuncs_expm.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 592 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_matfuncs_schur_sqrtm.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 593 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_matfuncs_sqrtm_triu.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 594 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/_solve_toeplitz.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 595 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/cython_blas.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 596 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/cython_lapack.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 597 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/ndimage/__pycache__/_morphology.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 598 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/ndimage/_nd_image.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 599 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/ndimage/_ni_label.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 600 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/ndimage/_rank_filter_1d.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 601 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/odr/__odrpack.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 602 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/__pycache__/_optimize.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
|
| 603 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_bglu_dense.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 604 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_group_columns.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 605 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_highspy/_core.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 606 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_highspy/_highs_options.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 607 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_lbfgsb.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 608 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_lsap.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 609 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_minpack.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 610 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_moduleTNC.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 611 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_pava_pybind.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 612 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_slsqplib.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
| 613 |
+
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_trlib/_trlib.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/Transparent Busy.ani
ADDED
|
Binary file (4.36 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-3ch-5S-24bit.wav
ADDED
|
Binary file (90 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-3ch-5S-36bit.wav
ADDED
|
Binary file (120 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-3ch-5S-45bit.wav
ADDED
|
Binary file (134 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-3ch-5S-53bit.wav
ADDED
|
Binary file (150 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-3ch-5S-64bit.wav
ADDED
|
Binary file (164 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-4ch-9S-12bit.wav
ADDED
|
Binary file (116 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/test-8000Hz-le-5ch-9S-5bit.wav
ADDED
|
Binary file (89 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/data/various_compressed.sav
ADDED
|
Binary file (1.02 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_fortran.py
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
| 9 |
+
import re
|
| 10 |
+
|
| 11 |
+
from numpy.testing import assert_equal, assert_allclose
|
| 12 |
+
import numpy as np
|
| 13 |
+
import pytest
|
| 14 |
+
|
| 15 |
+
from scipy.io import (FortranFile,
|
| 16 |
+
FortranEOFError,
|
| 17 |
+
FortranFormattingError)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
DATA_PATH = path.join(path.dirname(__file__), 'data')
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@pytest.fixture
|
| 24 |
+
def io_lock():
|
| 25 |
+
return threading.Lock()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test_fortranfiles_read(io_lock):
|
| 29 |
+
for filename in iglob(path.join(DATA_PATH, "fortran-*-*x*x*.dat")):
|
| 30 |
+
m = re.search(r'fortran-([^-]+)-(\d+)x(\d+)x(\d+).dat', filename, re.I)
|
| 31 |
+
if not m:
|
| 32 |
+
raise RuntimeError(f"Couldn't match {filename} filename to regex")
|
| 33 |
+
|
| 34 |
+
dims = (int(m.group(2)), int(m.group(3)), int(m.group(4)))
|
| 35 |
+
|
| 36 |
+
dtype = m.group(1).replace('s', '<')
|
| 37 |
+
|
| 38 |
+
with io_lock:
|
| 39 |
+
f = FortranFile(filename, 'r', '<u4')
|
| 40 |
+
data = f.read_record(dtype=dtype).reshape(dims, order='F')
|
| 41 |
+
f.close()
|
| 42 |
+
|
| 43 |
+
expected = np.arange(np.prod(dims)).reshape(dims).astype(dtype)
|
| 44 |
+
assert_equal(data, expected)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def test_fortranfiles_mixed_record(io_lock):
|
| 48 |
+
filename = path.join(DATA_PATH, "fortran-mixed.dat")
|
| 49 |
+
with io_lock:
|
| 50 |
+
with FortranFile(filename, 'r', '<u4') as f:
|
| 51 |
+
record = f.read_record('<i4,<f4,<i8,2<f8')
|
| 52 |
+
|
| 53 |
+
assert_equal(record['f0'][0], 1)
|
| 54 |
+
assert_allclose(record['f1'][0], 2.3)
|
| 55 |
+
assert_equal(record['f2'][0], 4)
|
| 56 |
+
assert_allclose(record['f3'][0], [5.6, 7.8])
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_fortranfiles_write():
|
| 60 |
+
for filename in iglob(path.join(DATA_PATH, "fortran-*-*x*x*.dat")):
|
| 61 |
+
m = re.search(r'fortran-([^-]+)-(\d+)x(\d+)x(\d+).dat', filename, re.I)
|
| 62 |
+
if not m:
|
| 63 |
+
raise RuntimeError(f"Couldn't match {filename} filename to regex")
|
| 64 |
+
dims = (int(m.group(2)), int(m.group(3)), int(m.group(4)))
|
| 65 |
+
|
| 66 |
+
dtype = m.group(1).replace('s', '<')
|
| 67 |
+
data = np.arange(np.prod(dims)).reshape(dims).astype(dtype)
|
| 68 |
+
|
| 69 |
+
tmpdir = tempfile.mkdtemp()
|
| 70 |
+
try:
|
| 71 |
+
testFile = path.join(str(threading.get_native_id()),
|
| 72 |
+
tmpdir,path.basename(filename))
|
| 73 |
+
f = FortranFile(testFile, 'w','<u4')
|
| 74 |
+
f.write_record(data.T)
|
| 75 |
+
f.close()
|
| 76 |
+
originalfile = open(filename, 'rb')
|
| 77 |
+
newfile = open(testFile, 'rb')
|
| 78 |
+
assert_equal(originalfile.read(), newfile.read(),
|
| 79 |
+
err_msg=filename)
|
| 80 |
+
originalfile.close()
|
| 81 |
+
newfile.close()
|
| 82 |
+
finally:
|
| 83 |
+
shutil.rmtree(tmpdir)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def test_fortranfile_read_mixed_record(io_lock):
|
| 87 |
+
# The data file fortran-3x3d-2i.dat contains the program that
|
| 88 |
+
# produced it at the end.
|
| 89 |
+
#
|
| 90 |
+
# double precision :: a(3,3)
|
| 91 |
+
# integer :: b(2)
|
| 92 |
+
# ...
|
| 93 |
+
# open(1, file='fortran-3x3d-2i.dat', form='unformatted')
|
| 94 |
+
# write(1) a, b
|
| 95 |
+
# close(1)
|
| 96 |
+
#
|
| 97 |
+
|
| 98 |
+
filename = path.join(DATA_PATH, "fortran-3x3d-2i.dat")
|
| 99 |
+
with io_lock:
|
| 100 |
+
with FortranFile(filename, 'r', '<u4') as f:
|
| 101 |
+
record = f.read_record('(3,3)<f8', '2<i4')
|
| 102 |
+
|
| 103 |
+
ax = np.arange(3*3).reshape(3, 3).astype(np.float64)
|
| 104 |
+
bx = np.array([-1, -2], dtype=np.int32)
|
| 105 |
+
|
| 106 |
+
assert_equal(record[0], ax.T)
|
| 107 |
+
assert_equal(record[1], bx.T)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def test_fortranfile_write_mixed_record(tmpdir):
|
| 111 |
+
tf = path.join(str(tmpdir), str(threading.get_native_id()), 'test.dat')
|
| 112 |
+
os.makedirs(path.dirname(tf), exist_ok=True)
|
| 113 |
+
|
| 114 |
+
r1 = (('f4', 'f4', 'i4'), (np.float32(2), np.float32(3), np.int32(100)))
|
| 115 |
+
r2 = (('4f4', '(3,3)f4', '8i4'),
|
| 116 |
+
(np.random.randint(255, size=[4]).astype(np.float32),
|
| 117 |
+
np.random.randint(255, size=[3, 3]).astype(np.float32),
|
| 118 |
+
np.random.randint(255, size=[8]).astype(np.int32)))
|
| 119 |
+
records = [r1, r2]
|
| 120 |
+
|
| 121 |
+
for dtype, a in records:
|
| 122 |
+
with FortranFile(tf, 'w') as f:
|
| 123 |
+
f.write_record(*a)
|
| 124 |
+
|
| 125 |
+
with FortranFile(tf, 'r') as f:
|
| 126 |
+
b = f.read_record(*dtype)
|
| 127 |
+
|
| 128 |
+
assert_equal(len(a), len(b))
|
| 129 |
+
|
| 130 |
+
for aa, bb in zip(a, b):
|
| 131 |
+
assert_equal(bb, aa)
|
| 132 |
+
|
| 133 |
+
def read_unformatted_double(m, n, k, filename):
|
| 134 |
+
"""
|
| 135 |
+
Read a Fortran-style unformatted binary file written with a single write() call,
|
| 136 |
+
assuming it wraps the data with 4-byte record markers.
|
| 137 |
+
|
| 138 |
+
Returns:
|
| 139 |
+
np.ndarray of shape (m, n, k) with dtype float64
|
| 140 |
+
|
| 141 |
+
Reference:
|
| 142 |
+
Fortran implementation:
|
| 143 |
+
https://github.com/scipy/scipy/blob/maintenance/1.15.x/scipy/io/_test_fortran.f#L1-L9
|
| 144 |
+
"""
|
| 145 |
+
with open(filename.strip(), 'rb') as f:
|
| 146 |
+
f.read(4) # Skip initial 4-byte record marker
|
| 147 |
+
data = np.fromfile(f, dtype=np.float64, count=m * n * k)
|
| 148 |
+
f.read(4) # Skip trailing 4-byte record marker
|
| 149 |
+
|
| 150 |
+
if data.size != m * n * k:
|
| 151 |
+
raise ValueError(f"Expected {m*n*k} elements, got {data.size}")
|
| 152 |
+
|
| 153 |
+
return data.reshape((m, n, k), order='F') # Fortran column-major order
|
| 154 |
+
|
| 155 |
+
def read_unformatted_mixed(m, n, k, filename):
|
| 156 |
+
"""
|
| 157 |
+
Read a Fortran unformatted binary file that contains a mix of:
|
| 158 |
+
- a double precision array a(m, n)
|
| 159 |
+
- an integer array b(k)
|
| 160 |
+
|
| 161 |
+
Assumes a single write(10) a, b was used and file is wrapped
|
| 162 |
+
with Fortran record markers.
|
| 163 |
+
|
| 164 |
+
Returns:
|
| 165 |
+
a: np.ndarray of shape (m, n) with dtype float64
|
| 166 |
+
b: np.ndarray of shape (k,) with dtype int32
|
| 167 |
+
|
| 168 |
+
Reference:
|
| 169 |
+
Fortran implementation:
|
| 170 |
+
https://github.com/scipy/scipy/blob/maintenance/1.15.x/scipy/io/_test_fortran.f#L21-L30
|
| 171 |
+
"""
|
| 172 |
+
with open(filename.strip(), 'rb') as f:
|
| 173 |
+
f.read(4) # Skip initial 4-byte record marker
|
| 174 |
+
|
| 175 |
+
# Read a(m,n): total m*n float64 values
|
| 176 |
+
a_flat = np.fromfile(f, dtype=np.float64, count=m * n)
|
| 177 |
+
|
| 178 |
+
# Read b(k): total k int32 values (assuming Fortran default integer*4)
|
| 179 |
+
b = np.fromfile(f, dtype=np.int32, count=k)
|
| 180 |
+
|
| 181 |
+
f.read(4) # Skip trailing 4-byte record marker
|
| 182 |
+
|
| 183 |
+
# Reshape a to (m,n) Fortran-style
|
| 184 |
+
a = a_flat.reshape((m, n), order='F')
|
| 185 |
+
|
| 186 |
+
return a, b
|
| 187 |
+
|
| 188 |
+
def read_unformatted_int(m, n, k, filename):
|
| 189 |
+
"""
|
| 190 |
+
Read a Fortran unformatted binary file
|
| 191 |
+
containing a 3D integer array (m, n, k).
|
| 192 |
+
Assumes the array is written with a single
|
| 193 |
+
write(10) a and wrapped with record markers.
|
| 194 |
+
|
| 195 |
+
Returns:
|
| 196 |
+
np.ndarray: 3D array of shape (m, n, k) with dtype int32
|
| 197 |
+
|
| 198 |
+
Reference:
|
| 199 |
+
Fortran implementation:
|
| 200 |
+
https://github.com/scipy/scipy/blob/maintenance/1.15.x/scipy/io/_test_fortran.f#L11-L19
|
| 201 |
+
"""
|
| 202 |
+
with open(filename.strip(), 'rb') as f:
|
| 203 |
+
f.read(4) # Skip Fortran record marker at start
|
| 204 |
+
|
| 205 |
+
# Read m*n*k integers (Fortran default = 4 bytes per integer)
|
| 206 |
+
data = np.fromfile(f, dtype=np.int32, count=m * n * k)
|
| 207 |
+
|
| 208 |
+
f.read(4) # Skip Fortran record marker at end
|
| 209 |
+
|
| 210 |
+
if data.size != m * n * k:
|
| 211 |
+
raise ValueError(f"Expected {m*n*k} elements, got {data.size}")
|
| 212 |
+
|
| 213 |
+
return data.reshape((m, n, k), order='F') # Fortran-style column-major order
|
| 214 |
+
|
| 215 |
+
def test_fortran_roundtrip(tmpdir, io_lock):
|
| 216 |
+
filename = path.join(str(tmpdir), str(threading.get_native_id()),
|
| 217 |
+
'test.dat')
|
| 218 |
+
os.makedirs(path.dirname(filename), exist_ok=True)
|
| 219 |
+
|
| 220 |
+
rng = np.random.RandomState(1)
|
| 221 |
+
|
| 222 |
+
# double precision
|
| 223 |
+
m, n, k = 5, 3, 2
|
| 224 |
+
a = rng.randn(m, n, k)
|
| 225 |
+
with FortranFile(filename, 'w') as f:
|
| 226 |
+
f.write_record(a.T)
|
| 227 |
+
with io_lock:
|
| 228 |
+
a2 = read_unformatted_double(m, n, k, filename)
|
| 229 |
+
|
| 230 |
+
with FortranFile(filename, 'r') as f:
|
| 231 |
+
a3 = f.read_record('(2,3,5)f8').T
|
| 232 |
+
assert_equal(a2, a)
|
| 233 |
+
assert_equal(a3, a)
|
| 234 |
+
|
| 235 |
+
# integer
|
| 236 |
+
m, n, k = 5, 3, 2
|
| 237 |
+
a = rng.randn(m, n, k).astype(np.int32)
|
| 238 |
+
with FortranFile(filename, 'w') as f:
|
| 239 |
+
f.write_record(a.T)
|
| 240 |
+
with io_lock:
|
| 241 |
+
a2 = read_unformatted_int(m, n, k, filename)
|
| 242 |
+
with FortranFile(filename, 'r') as f:
|
| 243 |
+
a3 = f.read_record('(2,3,5)i4').T
|
| 244 |
+
assert_equal(a2, a)
|
| 245 |
+
assert_equal(a3, a)
|
| 246 |
+
|
| 247 |
+
# mixed
|
| 248 |
+
m, n, k = 5, 3, 2
|
| 249 |
+
a = rng.randn(m, n)
|
| 250 |
+
b = rng.randn(k).astype(np.intc)
|
| 251 |
+
with FortranFile(filename, 'w') as f:
|
| 252 |
+
f.write_record(a.T, b.T)
|
| 253 |
+
with io_lock:
|
| 254 |
+
a2, b2 = read_unformatted_mixed(m, n, k, filename)
|
| 255 |
+
with FortranFile(filename, 'r') as f:
|
| 256 |
+
a3, b3 = f.read_record('(3,5)f8', '2i4')
|
| 257 |
+
a3 = a3.T
|
| 258 |
+
assert_equal(a2, a)
|
| 259 |
+
assert_equal(a3, a)
|
| 260 |
+
assert_equal(b2, b)
|
| 261 |
+
assert_equal(b3, b)
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
def test_fortran_eof_ok(tmpdir):
|
| 265 |
+
filename = path.join(str(tmpdir), str(threading.get_native_id()),
|
| 266 |
+
"scratch")
|
| 267 |
+
os.makedirs(path.dirname(filename), exist_ok=True)
|
| 268 |
+
rng = np.random.RandomState(1)
|
| 269 |
+
with FortranFile(filename, 'w') as f:
|
| 270 |
+
f.write_record(rng.randn(5))
|
| 271 |
+
f.write_record(rng.randn(3))
|
| 272 |
+
with FortranFile(filename, 'r') as f:
|
| 273 |
+
assert len(f.read_reals()) == 5
|
| 274 |
+
assert len(f.read_reals()) == 3
|
| 275 |
+
with pytest.raises(FortranEOFError):
|
| 276 |
+
f.read_reals()
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def test_fortran_eof_broken_size(tmpdir):
|
| 280 |
+
filename = path.join(str(tmpdir), str(threading.get_native_id()),
|
| 281 |
+
"scratch")
|
| 282 |
+
os.makedirs(path.dirname(filename), exist_ok=True)
|
| 283 |
+
rng = np.random.RandomState(1)
|
| 284 |
+
with FortranFile(filename, 'w') as f:
|
| 285 |
+
f.write_record(rng.randn(5))
|
| 286 |
+
f.write_record(rng.randn(3))
|
| 287 |
+
with open(filename, "ab") as f:
|
| 288 |
+
f.write(b"\xff")
|
| 289 |
+
with FortranFile(filename, 'r') as f:
|
| 290 |
+
assert len(f.read_reals()) == 5
|
| 291 |
+
assert len(f.read_reals()) == 3
|
| 292 |
+
with pytest.raises(FortranFormattingError):
|
| 293 |
+
f.read_reals()
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def test_fortran_bogus_size(tmpdir):
|
| 297 |
+
filename = path.join(str(tmpdir), str(threading.get_native_id()),
|
| 298 |
+
"scratch")
|
| 299 |
+
os.makedirs(path.dirname(filename), exist_ok=True)
|
| 300 |
+
rng = np.random.RandomState(1)
|
| 301 |
+
with FortranFile(filename, 'w') as f:
|
| 302 |
+
f.write_record(rng.randn(5))
|
| 303 |
+
f.write_record(rng.randn(3))
|
| 304 |
+
with open(filename, "w+b") as f:
|
| 305 |
+
f.write(b"\xff\xff")
|
| 306 |
+
with FortranFile(filename, 'r') as f:
|
| 307 |
+
with pytest.raises(FortranFormattingError):
|
| 308 |
+
f.read_reals()
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
def test_fortran_eof_broken_record(tmpdir):
|
| 312 |
+
filename = path.join(str(tmpdir), str(threading.get_native_id()),
|
| 313 |
+
"scratch")
|
| 314 |
+
os.makedirs(path.dirname(filename), exist_ok=True)
|
| 315 |
+
rng = np.random.RandomState(1)
|
| 316 |
+
with FortranFile(filename, 'w') as f:
|
| 317 |
+
f.write_record(rng.randn(5))
|
| 318 |
+
f.write_record(rng.randn(3))
|
| 319 |
+
with open(filename, "ab") as f:
|
| 320 |
+
f.truncate(path.getsize(filename)-20)
|
| 321 |
+
with FortranFile(filename, 'r') as f:
|
| 322 |
+
assert len(f.read_reals()) == 5
|
| 323 |
+
with pytest.raises(FortranFormattingError):
|
| 324 |
+
f.read_reals()
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def test_fortran_eof_multidimensional(tmpdir):
|
| 328 |
+
filename = path.join(str(tmpdir), str(threading.get_native_id()),
|
| 329 |
+
"scratch")
|
| 330 |
+
os.makedirs(path.dirname(filename), exist_ok=True)
|
| 331 |
+
n, m, q = 3, 5, 7
|
| 332 |
+
dt = np.dtype([("field", np.float64, (n, m))])
|
| 333 |
+
a = np.zeros(q, dtype=dt)
|
| 334 |
+
with FortranFile(filename, 'w') as f:
|
| 335 |
+
f.write_record(a[0])
|
| 336 |
+
f.write_record(a)
|
| 337 |
+
f.write_record(a)
|
| 338 |
+
with open(filename, "ab") as f:
|
| 339 |
+
f.truncate(path.getsize(filename)-20)
|
| 340 |
+
with FortranFile(filename, 'r') as f:
|
| 341 |
+
assert len(f.read_record(dtype=dt)) == 1
|
| 342 |
+
assert len(f.read_record(dtype=dt)) == q
|
| 343 |
+
with pytest.raises(FortranFormattingError):
|
| 344 |
+
f.read_record(dtype=dt)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_idl.py
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from os import path
|
| 2 |
+
import warnings
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
from numpy.testing import (assert_equal, assert_array_equal,
|
| 6 |
+
assert_)
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
from scipy.io import readsav
|
| 10 |
+
from scipy.io import _idl
|
| 11 |
+
|
| 12 |
+
DATA_PATH = path.join(path.dirname(__file__), 'data')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def assert_identical(a, b):
|
| 16 |
+
"""Assert whether value AND type are the same"""
|
| 17 |
+
assert_equal(a, b)
|
| 18 |
+
if isinstance(b, str):
|
| 19 |
+
assert_equal(type(a), type(b))
|
| 20 |
+
else:
|
| 21 |
+
assert_equal(np.asarray(a).dtype.type, np.asarray(b).dtype.type)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def assert_array_identical(a, b):
|
| 25 |
+
"""Assert whether values AND type are the same"""
|
| 26 |
+
assert_array_equal(a, b)
|
| 27 |
+
assert_equal(a.dtype.type, b.dtype.type)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Define vectorized ID function for pointer arrays
|
| 31 |
+
vect_id = np.vectorize(id)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class TestIdict:
|
| 35 |
+
|
| 36 |
+
def test_idict(self):
|
| 37 |
+
custom_dict = {'a': np.int16(999)}
|
| 38 |
+
original_id = id(custom_dict)
|
| 39 |
+
s = readsav(path.join(DATA_PATH, 'scalar_byte.sav'),
|
| 40 |
+
idict=custom_dict, verbose=False)
|
| 41 |
+
assert_equal(original_id, id(s))
|
| 42 |
+
assert_('a' in s)
|
| 43 |
+
assert_identical(s['a'], np.int16(999))
|
| 44 |
+
assert_identical(s['i8u'], np.uint8(234))
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class TestScalars:
|
| 48 |
+
# Test that scalar values are read in with the correct value and type
|
| 49 |
+
|
| 50 |
+
def test_byte(self):
|
| 51 |
+
s = readsav(path.join(DATA_PATH, 'scalar_byte.sav'), verbose=False)
|
| 52 |
+
assert_identical(s.i8u, np.uint8(234))
|
| 53 |
+
|
| 54 |
+
def test_int16(self):
|
| 55 |
+
s = readsav(path.join(DATA_PATH, 'scalar_int16.sav'), verbose=False)
|
| 56 |
+
assert_identical(s.i16s, np.int16(-23456))
|
| 57 |
+
|
| 58 |
+
def test_int32(self):
|
| 59 |
+
s = readsav(path.join(DATA_PATH, 'scalar_int32.sav'), verbose=False)
|
| 60 |
+
assert_identical(s.i32s, np.int32(-1234567890))
|
| 61 |
+
|
| 62 |
+
def test_float32(self):
|
| 63 |
+
s = readsav(path.join(DATA_PATH, 'scalar_float32.sav'), verbose=False)
|
| 64 |
+
assert_identical(s.f32, np.float32(-3.1234567e+37))
|
| 65 |
+
|
| 66 |
+
def test_float64(self):
|
| 67 |
+
s = readsav(path.join(DATA_PATH, 'scalar_float64.sav'), verbose=False)
|
| 68 |
+
assert_identical(s.f64, np.float64(-1.1976931348623157e+307))
|
| 69 |
+
|
| 70 |
+
def test_complex32(self):
|
| 71 |
+
s = readsav(path.join(DATA_PATH, 'scalar_complex32.sav'), verbose=False)
|
| 72 |
+
assert_identical(s.c32, np.complex64(3.124442e13-2.312442e31j))
|
| 73 |
+
|
| 74 |
+
def test_bytes(self):
|
| 75 |
+
s = readsav(path.join(DATA_PATH, 'scalar_string.sav'), verbose=False)
|
| 76 |
+
msg = "The quick brown fox jumps over the lazy python"
|
| 77 |
+
assert_identical(s.s, np.bytes_(msg))
|
| 78 |
+
|
| 79 |
+
def test_structure(self):
|
| 80 |
+
pass
|
| 81 |
+
|
| 82 |
+
def test_complex64(self):
|
| 83 |
+
s = readsav(path.join(DATA_PATH, 'scalar_complex64.sav'), verbose=False)
|
| 84 |
+
assert_identical(
|
| 85 |
+
s.c64,
|
| 86 |
+
np.complex128(1.1987253647623157e+112-5.1987258887729157e+307j)
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
def test_heap_pointer(self):
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
+
def test_object_reference(self):
|
| 93 |
+
pass
|
| 94 |
+
|
| 95 |
+
def test_uint16(self):
|
| 96 |
+
s = readsav(path.join(DATA_PATH, 'scalar_uint16.sav'), verbose=False)
|
| 97 |
+
assert_identical(s.i16u, np.uint16(65511))
|
| 98 |
+
|
| 99 |
+
def test_uint32(self):
|
| 100 |
+
s = readsav(path.join(DATA_PATH, 'scalar_uint32.sav'), verbose=False)
|
| 101 |
+
assert_identical(s.i32u, np.uint32(4294967233))
|
| 102 |
+
|
| 103 |
+
def test_int64(self):
|
| 104 |
+
s = readsav(path.join(DATA_PATH, 'scalar_int64.sav'), verbose=False)
|
| 105 |
+
assert_identical(s.i64s, np.int64(-9223372036854774567))
|
| 106 |
+
|
| 107 |
+
def test_uint64(self):
|
| 108 |
+
s = readsav(path.join(DATA_PATH, 'scalar_uint64.sav'), verbose=False)
|
| 109 |
+
assert_identical(s.i64u, np.uint64(18446744073709529285))
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
class TestCompressed(TestScalars):
|
| 113 |
+
# Test that compressed .sav files can be read in
|
| 114 |
+
|
| 115 |
+
def test_compressed(self):
|
| 116 |
+
s = readsav(path.join(DATA_PATH, 'various_compressed.sav'), verbose=False)
|
| 117 |
+
|
| 118 |
+
assert_identical(s.i8u, np.uint8(234))
|
| 119 |
+
assert_identical(s.f32, np.float32(-3.1234567e+37))
|
| 120 |
+
assert_identical(
|
| 121 |
+
s.c64,
|
| 122 |
+
np.complex128(1.1987253647623157e+112-5.1987258887729157e+307j)
|
| 123 |
+
)
|
| 124 |
+
assert_equal(s.array5d.shape, (4, 3, 4, 6, 5))
|
| 125 |
+
assert_identical(s.arrays.a[0], np.array([1, 2, 3], dtype=np.int16))
|
| 126 |
+
assert_identical(s.arrays.b[0], np.array([4., 5., 6., 7.], dtype=np.float32))
|
| 127 |
+
assert_identical(s.arrays.c[0],
|
| 128 |
+
np.array([np.complex64(1+2j), np.complex64(7+8j)]))
|
| 129 |
+
assert_identical(s.arrays.d[0],
|
| 130 |
+
np.array([b"cheese", b"bacon", b"spam"], dtype=object))
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
class TestArrayDimensions:
|
| 134 |
+
# Test that multi-dimensional arrays are read in with the correct dimensions
|
| 135 |
+
|
| 136 |
+
def test_1d(self):
|
| 137 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_1d.sav'), verbose=False)
|
| 138 |
+
assert_equal(s.array1d.shape, (123, ))
|
| 139 |
+
|
| 140 |
+
def test_2d(self):
|
| 141 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_2d.sav'), verbose=False)
|
| 142 |
+
assert_equal(s.array2d.shape, (22, 12))
|
| 143 |
+
|
| 144 |
+
def test_3d(self):
|
| 145 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_3d.sav'), verbose=False)
|
| 146 |
+
assert_equal(s.array3d.shape, (11, 22, 12))
|
| 147 |
+
|
| 148 |
+
def test_4d(self):
|
| 149 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_4d.sav'), verbose=False)
|
| 150 |
+
assert_equal(s.array4d.shape, (4, 5, 8, 7))
|
| 151 |
+
|
| 152 |
+
def test_5d(self):
|
| 153 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_5d.sav'), verbose=False)
|
| 154 |
+
assert_equal(s.array5d.shape, (4, 3, 4, 6, 5))
|
| 155 |
+
|
| 156 |
+
def test_6d(self):
|
| 157 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_6d.sav'), verbose=False)
|
| 158 |
+
assert_equal(s.array6d.shape, (3, 6, 4, 5, 3, 4))
|
| 159 |
+
|
| 160 |
+
def test_7d(self):
|
| 161 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_7d.sav'), verbose=False)
|
| 162 |
+
assert_equal(s.array7d.shape, (2, 1, 2, 3, 4, 3, 2))
|
| 163 |
+
|
| 164 |
+
def test_8d(self):
|
| 165 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_8d.sav'), verbose=False)
|
| 166 |
+
assert_equal(s.array8d.shape, (4, 3, 2, 1, 2, 3, 5, 4))
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
class TestStructures:
|
| 170 |
+
|
| 171 |
+
def test_scalars(self):
|
| 172 |
+
s = readsav(path.join(DATA_PATH, 'struct_scalars.sav'), verbose=False)
|
| 173 |
+
assert_identical(s.scalars.a, np.array(np.int16(1)))
|
| 174 |
+
assert_identical(s.scalars.b, np.array(np.int32(2)))
|
| 175 |
+
assert_identical(s.scalars.c, np.array(np.float32(3.)))
|
| 176 |
+
assert_identical(s.scalars.d, np.array(np.float64(4.)))
|
| 177 |
+
assert_identical(s.scalars.e, np.array([b"spam"], dtype=object))
|
| 178 |
+
assert_identical(s.scalars.f, np.array(np.complex64(-1.+3j)))
|
| 179 |
+
|
| 180 |
+
def test_scalars_replicated(self):
|
| 181 |
+
s = readsav(path.join(DATA_PATH, 'struct_scalars_replicated.sav'),
|
| 182 |
+
verbose=False)
|
| 183 |
+
assert_identical(s.scalars_rep.a, np.repeat(np.int16(1), 5))
|
| 184 |
+
assert_identical(s.scalars_rep.b, np.repeat(np.int32(2), 5))
|
| 185 |
+
assert_identical(s.scalars_rep.c, np.repeat(np.float32(3.), 5))
|
| 186 |
+
assert_identical(s.scalars_rep.d, np.repeat(np.float64(4.), 5))
|
| 187 |
+
assert_identical(s.scalars_rep.e, np.repeat(b"spam", 5).astype(object))
|
| 188 |
+
assert_identical(s.scalars_rep.f, np.repeat(np.complex64(-1.+3j), 5))
|
| 189 |
+
|
| 190 |
+
def test_scalars_replicated_3d(self):
|
| 191 |
+
s = readsav(path.join(DATA_PATH, 'struct_scalars_replicated_3d.sav'),
|
| 192 |
+
verbose=False)
|
| 193 |
+
assert_identical(s.scalars_rep.a, np.repeat(np.int16(1), 24).reshape(4, 3, 2))
|
| 194 |
+
assert_identical(s.scalars_rep.b, np.repeat(np.int32(2), 24).reshape(4, 3, 2))
|
| 195 |
+
assert_identical(s.scalars_rep.c,
|
| 196 |
+
np.repeat(np.float32(3.), 24).reshape(4, 3, 2))
|
| 197 |
+
assert_identical(s.scalars_rep.d,
|
| 198 |
+
np.repeat(np.float64(4.), 24).reshape(4, 3, 2))
|
| 199 |
+
assert_identical(s.scalars_rep.e,
|
| 200 |
+
np.repeat(b"spam", 24).reshape(4, 3, 2).astype(object))
|
| 201 |
+
assert_identical(s.scalars_rep.f,
|
| 202 |
+
np.repeat(np.complex64(-1.+3j), 24).reshape(4, 3, 2))
|
| 203 |
+
|
| 204 |
+
def test_arrays(self):
|
| 205 |
+
s = readsav(path.join(DATA_PATH, 'struct_arrays.sav'), verbose=False)
|
| 206 |
+
assert_array_identical(s.arrays.a[0], np.array([1, 2, 3], dtype=np.int16))
|
| 207 |
+
assert_array_identical(s.arrays.b[0],
|
| 208 |
+
np.array([4., 5., 6., 7.], dtype=np.float32))
|
| 209 |
+
assert_array_identical(s.arrays.c[0],
|
| 210 |
+
np.array([np.complex64(1+2j), np.complex64(7+8j)]))
|
| 211 |
+
assert_array_identical(s.arrays.d[0],
|
| 212 |
+
np.array([b"cheese", b"bacon", b"spam"], dtype=object))
|
| 213 |
+
|
| 214 |
+
def test_arrays_replicated(self):
|
| 215 |
+
s = readsav(path.join(DATA_PATH, 'struct_arrays_replicated.sav'), verbose=False)
|
| 216 |
+
|
| 217 |
+
# Check column types
|
| 218 |
+
assert_(s.arrays_rep.a.dtype.type is np.object_)
|
| 219 |
+
assert_(s.arrays_rep.b.dtype.type is np.object_)
|
| 220 |
+
assert_(s.arrays_rep.c.dtype.type is np.object_)
|
| 221 |
+
assert_(s.arrays_rep.d.dtype.type is np.object_)
|
| 222 |
+
|
| 223 |
+
# Check column shapes
|
| 224 |
+
assert_equal(s.arrays_rep.a.shape, (5, ))
|
| 225 |
+
assert_equal(s.arrays_rep.b.shape, (5, ))
|
| 226 |
+
assert_equal(s.arrays_rep.c.shape, (5, ))
|
| 227 |
+
assert_equal(s.arrays_rep.d.shape, (5, ))
|
| 228 |
+
|
| 229 |
+
# Check values
|
| 230 |
+
for i in range(5):
|
| 231 |
+
assert_array_identical(s.arrays_rep.a[i],
|
| 232 |
+
np.array([1, 2, 3], dtype=np.int16))
|
| 233 |
+
assert_array_identical(s.arrays_rep.b[i],
|
| 234 |
+
np.array([4., 5., 6., 7.], dtype=np.float32))
|
| 235 |
+
assert_array_identical(s.arrays_rep.c[i],
|
| 236 |
+
np.array([np.complex64(1+2j),
|
| 237 |
+
np.complex64(7+8j)]))
|
| 238 |
+
assert_array_identical(s.arrays_rep.d[i],
|
| 239 |
+
np.array([b"cheese", b"bacon", b"spam"],
|
| 240 |
+
dtype=object))
|
| 241 |
+
|
| 242 |
+
def test_arrays_replicated_3d(self):
|
| 243 |
+
s = readsav(path.join(DATA_PATH, 'struct_arrays_replicated_3d.sav'),
|
| 244 |
+
verbose=False)
|
| 245 |
+
|
| 246 |
+
# Check column types
|
| 247 |
+
assert_(s.arrays_rep.a.dtype.type is np.object_)
|
| 248 |
+
assert_(s.arrays_rep.b.dtype.type is np.object_)
|
| 249 |
+
assert_(s.arrays_rep.c.dtype.type is np.object_)
|
| 250 |
+
assert_(s.arrays_rep.d.dtype.type is np.object_)
|
| 251 |
+
|
| 252 |
+
# Check column shapes
|
| 253 |
+
assert_equal(s.arrays_rep.a.shape, (4, 3, 2))
|
| 254 |
+
assert_equal(s.arrays_rep.b.shape, (4, 3, 2))
|
| 255 |
+
assert_equal(s.arrays_rep.c.shape, (4, 3, 2))
|
| 256 |
+
assert_equal(s.arrays_rep.d.shape, (4, 3, 2))
|
| 257 |
+
|
| 258 |
+
# Check values
|
| 259 |
+
for i in range(4):
|
| 260 |
+
for j in range(3):
|
| 261 |
+
for k in range(2):
|
| 262 |
+
assert_array_identical(s.arrays_rep.a[i, j, k],
|
| 263 |
+
np.array([1, 2, 3], dtype=np.int16))
|
| 264 |
+
assert_array_identical(s.arrays_rep.b[i, j, k],
|
| 265 |
+
np.array([4., 5., 6., 7.],
|
| 266 |
+
dtype=np.float32))
|
| 267 |
+
assert_array_identical(s.arrays_rep.c[i, j, k],
|
| 268 |
+
np.array([np.complex64(1+2j),
|
| 269 |
+
np.complex64(7+8j)]))
|
| 270 |
+
assert_array_identical(s.arrays_rep.d[i, j, k],
|
| 271 |
+
np.array([b"cheese", b"bacon", b"spam"],
|
| 272 |
+
dtype=object))
|
| 273 |
+
|
| 274 |
+
def test_inheritance(self):
|
| 275 |
+
s = readsav(path.join(DATA_PATH, 'struct_inherit.sav'), verbose=False)
|
| 276 |
+
assert_identical(s.fc.x, np.array([0], dtype=np.int16))
|
| 277 |
+
assert_identical(s.fc.y, np.array([0], dtype=np.int16))
|
| 278 |
+
assert_identical(s.fc.r, np.array([0], dtype=np.int16))
|
| 279 |
+
assert_identical(s.fc.c, np.array([4], dtype=np.int16))
|
| 280 |
+
|
| 281 |
+
def test_arrays_corrupt_idl80(self):
|
| 282 |
+
# test byte arrays with missing nbyte information from IDL 8.0 .sav file
|
| 283 |
+
with warnings.catch_warnings():
|
| 284 |
+
warnings.filterwarnings(
|
| 285 |
+
"ignore", "Not able to verify number of bytes from header", UserWarning)
|
| 286 |
+
s = readsav(path.join(DATA_PATH,'struct_arrays_byte_idl80.sav'),
|
| 287 |
+
verbose=False)
|
| 288 |
+
|
| 289 |
+
assert_identical(s.y.x[0], np.array([55,66], dtype=np.uint8))
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
class TestPointers:
|
| 293 |
+
# Check that pointers in .sav files produce references to the same object in Python
|
| 294 |
+
|
| 295 |
+
def test_pointers(self):
|
| 296 |
+
s = readsav(path.join(DATA_PATH, 'scalar_heap_pointer.sav'), verbose=False)
|
| 297 |
+
assert_identical(
|
| 298 |
+
s.c64_pointer1,
|
| 299 |
+
np.complex128(1.1987253647623157e+112-5.1987258887729157e+307j)
|
| 300 |
+
)
|
| 301 |
+
assert_identical(
|
| 302 |
+
s.c64_pointer2,
|
| 303 |
+
np.complex128(1.1987253647623157e+112-5.1987258887729157e+307j)
|
| 304 |
+
)
|
| 305 |
+
assert_(s.c64_pointer1 is s.c64_pointer2)
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
class TestPointerArray:
|
| 309 |
+
# Test that pointers in arrays are correctly read in
|
| 310 |
+
|
| 311 |
+
def test_1d(self):
|
| 312 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_pointer_1d.sav'), verbose=False)
|
| 313 |
+
assert_equal(s.array1d.shape, (123, ))
|
| 314 |
+
assert_(np.all(s.array1d == np.float32(4.)))
|
| 315 |
+
assert_(np.all(vect_id(s.array1d) == id(s.array1d[0])))
|
| 316 |
+
|
| 317 |
+
def test_2d(self):
|
| 318 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_pointer_2d.sav'), verbose=False)
|
| 319 |
+
assert_equal(s.array2d.shape, (22, 12))
|
| 320 |
+
assert_(np.all(s.array2d == np.float32(4.)))
|
| 321 |
+
assert_(np.all(vect_id(s.array2d) == id(s.array2d[0,0])))
|
| 322 |
+
|
| 323 |
+
def test_3d(self):
|
| 324 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_pointer_3d.sav'), verbose=False)
|
| 325 |
+
assert_equal(s.array3d.shape, (11, 22, 12))
|
| 326 |
+
assert_(np.all(s.array3d == np.float32(4.)))
|
| 327 |
+
assert_(np.all(vect_id(s.array3d) == id(s.array3d[0,0,0])))
|
| 328 |
+
|
| 329 |
+
def test_4d(self):
|
| 330 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_pointer_4d.sav'), verbose=False)
|
| 331 |
+
assert_equal(s.array4d.shape, (4, 5, 8, 7))
|
| 332 |
+
assert_(np.all(s.array4d == np.float32(4.)))
|
| 333 |
+
assert_(np.all(vect_id(s.array4d) == id(s.array4d[0,0,0,0])))
|
| 334 |
+
|
| 335 |
+
def test_5d(self):
|
| 336 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_pointer_5d.sav'), verbose=False)
|
| 337 |
+
assert_equal(s.array5d.shape, (4, 3, 4, 6, 5))
|
| 338 |
+
assert_(np.all(s.array5d == np.float32(4.)))
|
| 339 |
+
assert_(np.all(vect_id(s.array5d) == id(s.array5d[0,0,0,0,0])))
|
| 340 |
+
|
| 341 |
+
def test_6d(self):
|
| 342 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_pointer_6d.sav'), verbose=False)
|
| 343 |
+
assert_equal(s.array6d.shape, (3, 6, 4, 5, 3, 4))
|
| 344 |
+
assert_(np.all(s.array6d == np.float32(4.)))
|
| 345 |
+
assert_(np.all(vect_id(s.array6d) == id(s.array6d[0,0,0,0,0,0])))
|
| 346 |
+
|
| 347 |
+
def test_7d(self):
|
| 348 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_pointer_7d.sav'), verbose=False)
|
| 349 |
+
assert_equal(s.array7d.shape, (2, 1, 2, 3, 4, 3, 2))
|
| 350 |
+
assert_(np.all(s.array7d == np.float32(4.)))
|
| 351 |
+
assert_(np.all(vect_id(s.array7d) == id(s.array7d[0,0,0,0,0,0,0])))
|
| 352 |
+
|
| 353 |
+
def test_8d(self):
|
| 354 |
+
s = readsav(path.join(DATA_PATH, 'array_float32_pointer_8d.sav'), verbose=False)
|
| 355 |
+
assert_equal(s.array8d.shape, (4, 3, 2, 1, 2, 3, 5, 4))
|
| 356 |
+
assert_(np.all(s.array8d == np.float32(4.)))
|
| 357 |
+
assert_(np.all(vect_id(s.array8d) == id(s.array8d[0,0,0,0,0,0,0,0])))
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
class TestPointerStructures:
|
| 361 |
+
# Test that structures are correctly read in
|
| 362 |
+
|
| 363 |
+
def test_scalars(self):
|
| 364 |
+
s = readsav(path.join(DATA_PATH, 'struct_pointers.sav'), verbose=False)
|
| 365 |
+
assert_identical(s.pointers.g, np.array(np.float32(4.), dtype=np.object_))
|
| 366 |
+
assert_identical(s.pointers.h, np.array(np.float32(4.), dtype=np.object_))
|
| 367 |
+
assert_(id(s.pointers.g[0]) == id(s.pointers.h[0]))
|
| 368 |
+
|
| 369 |
+
def test_pointers_replicated(self):
|
| 370 |
+
s = readsav(path.join(DATA_PATH, 'struct_pointers_replicated.sav'),
|
| 371 |
+
verbose=False)
|
| 372 |
+
assert_identical(s.pointers_rep.g,
|
| 373 |
+
np.repeat(np.float32(4.), 5).astype(np.object_))
|
| 374 |
+
assert_identical(s.pointers_rep.h,
|
| 375 |
+
np.repeat(np.float32(4.), 5).astype(np.object_))
|
| 376 |
+
assert_(np.all(vect_id(s.pointers_rep.g) == vect_id(s.pointers_rep.h)))
|
| 377 |
+
|
| 378 |
+
def test_pointers_replicated_3d(self):
|
| 379 |
+
s = readsav(path.join(DATA_PATH, 'struct_pointers_replicated_3d.sav'),
|
| 380 |
+
verbose=False)
|
| 381 |
+
s_expect = np.repeat(np.float32(4.), 24).reshape(4, 3, 2).astype(np.object_)
|
| 382 |
+
assert_identical(s.pointers_rep.g, s_expect)
|
| 383 |
+
assert_identical(s.pointers_rep.h, s_expect)
|
| 384 |
+
assert_(np.all(vect_id(s.pointers_rep.g) == vect_id(s.pointers_rep.h)))
|
| 385 |
+
|
| 386 |
+
def test_arrays(self):
|
| 387 |
+
s = readsav(path.join(DATA_PATH, 'struct_pointer_arrays.sav'), verbose=False)
|
| 388 |
+
assert_array_identical(s.arrays.g[0],
|
| 389 |
+
np.repeat(np.float32(4.), 2).astype(np.object_))
|
| 390 |
+
assert_array_identical(s.arrays.h[0],
|
| 391 |
+
np.repeat(np.float32(4.), 3).astype(np.object_))
|
| 392 |
+
assert_(np.all(vect_id(s.arrays.g[0]) == id(s.arrays.g[0][0])))
|
| 393 |
+
assert_(np.all(vect_id(s.arrays.h[0]) == id(s.arrays.h[0][0])))
|
| 394 |
+
assert_(id(s.arrays.g[0][0]) == id(s.arrays.h[0][0]))
|
| 395 |
+
|
| 396 |
+
def test_arrays_replicated(self):
|
| 397 |
+
s = readsav(path.join(DATA_PATH, 'struct_pointer_arrays_replicated.sav'),
|
| 398 |
+
verbose=False)
|
| 399 |
+
|
| 400 |
+
# Check column types
|
| 401 |
+
assert_(s.arrays_rep.g.dtype.type is np.object_)
|
| 402 |
+
assert_(s.arrays_rep.h.dtype.type is np.object_)
|
| 403 |
+
|
| 404 |
+
# Check column shapes
|
| 405 |
+
assert_equal(s.arrays_rep.g.shape, (5, ))
|
| 406 |
+
assert_equal(s.arrays_rep.h.shape, (5, ))
|
| 407 |
+
|
| 408 |
+
# Check values
|
| 409 |
+
for i in range(5):
|
| 410 |
+
assert_array_identical(s.arrays_rep.g[i],
|
| 411 |
+
np.repeat(np.float32(4.), 2).astype(np.object_))
|
| 412 |
+
assert_array_identical(s.arrays_rep.h[i],
|
| 413 |
+
np.repeat(np.float32(4.), 3).astype(np.object_))
|
| 414 |
+
assert_(np.all(vect_id(s.arrays_rep.g[i]) == id(s.arrays_rep.g[0][0])))
|
| 415 |
+
assert_(np.all(vect_id(s.arrays_rep.h[i]) == id(s.arrays_rep.h[0][0])))
|
| 416 |
+
|
| 417 |
+
def test_arrays_replicated_3d(self):
|
| 418 |
+
pth = path.join(DATA_PATH, 'struct_pointer_arrays_replicated_3d.sav')
|
| 419 |
+
s = readsav(pth, verbose=False)
|
| 420 |
+
|
| 421 |
+
# Check column types
|
| 422 |
+
assert_(s.arrays_rep.g.dtype.type is np.object_)
|
| 423 |
+
assert_(s.arrays_rep.h.dtype.type is np.object_)
|
| 424 |
+
|
| 425 |
+
# Check column shapes
|
| 426 |
+
assert_equal(s.arrays_rep.g.shape, (4, 3, 2))
|
| 427 |
+
assert_equal(s.arrays_rep.h.shape, (4, 3, 2))
|
| 428 |
+
|
| 429 |
+
# Check values
|
| 430 |
+
for i in range(4):
|
| 431 |
+
for j in range(3):
|
| 432 |
+
for k in range(2):
|
| 433 |
+
assert_array_identical(s.arrays_rep.g[i, j, k],
|
| 434 |
+
np.repeat(np.float32(4.), 2).astype(np.object_))
|
| 435 |
+
assert_array_identical(s.arrays_rep.h[i, j, k],
|
| 436 |
+
np.repeat(np.float32(4.), 3).astype(np.object_))
|
| 437 |
+
g0 = vect_id(s.arrays_rep.g[i, j, k])
|
| 438 |
+
g1 = id(s.arrays_rep.g[0, 0, 0][0])
|
| 439 |
+
assert np.all(g0 == g1)
|
| 440 |
+
h0 = vect_id(s.arrays_rep.h[i, j, k])
|
| 441 |
+
h1 = id(s.arrays_rep.h[0, 0, 0][0])
|
| 442 |
+
assert np.all(h0 == h1)
|
| 443 |
+
class TestTags:
|
| 444 |
+
'''Test that sav files with description tag read at all'''
|
| 445 |
+
|
| 446 |
+
def test_description(self):
|
| 447 |
+
s = readsav(path.join(DATA_PATH, 'scalar_byte_descr.sav'), verbose=False)
|
| 448 |
+
assert_identical(s.i8u, np.uint8(234))
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
def test_null_pointer():
|
| 452 |
+
# Regression test for null pointers.
|
| 453 |
+
s = readsav(path.join(DATA_PATH, 'null_pointer.sav'), verbose=False)
|
| 454 |
+
assert_identical(s.point, None)
|
| 455 |
+
assert_identical(s.check, np.int16(5))
|
| 456 |
+
|
| 457 |
+
|
| 458 |
+
def test_invalid_pointer():
|
| 459 |
+
# Regression test for invalid pointers (gh-4613).
|
| 460 |
+
|
| 461 |
+
# In some files in the wild, pointers can sometimes refer to a heap
|
| 462 |
+
# variable that does not exist. In that case, we now gracefully fail for
|
| 463 |
+
# that variable and replace the variable with None and emit a warning.
|
| 464 |
+
# Since it's difficult to artificially produce such files, the file used
|
| 465 |
+
# here has been edited to force the pointer reference to be invalid.
|
| 466 |
+
with warnings.catch_warnings(record=True) as w:
|
| 467 |
+
warnings.simplefilter("always")
|
| 468 |
+
s = readsav(path.join(DATA_PATH, 'invalid_pointer.sav'), verbose=False)
|
| 469 |
+
assert_(len(w) == 1)
|
| 470 |
+
assert_(str(w[0].message) == ("Variable referenced by pointer not found in "
|
| 471 |
+
"heap: variable will be set to None"))
|
| 472 |
+
assert_identical(s['a'], np.array([None, None]))
|
| 473 |
+
|
| 474 |
+
|
| 475 |
+
def test_attrdict():
|
| 476 |
+
d = _idl.AttrDict({'one': 1})
|
| 477 |
+
assert d['one'] == 1
|
| 478 |
+
assert d.one == 1
|
| 479 |
+
with pytest.raises(KeyError):
|
| 480 |
+
d['two']
|
| 481 |
+
with pytest.raises(AttributeError, match='has no attribute'):
|
| 482 |
+
d.two
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_mmio.py
ADDED
|
@@ -0,0 +1,829 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from tempfile import mkdtemp
|
| 2 |
+
import os
|
| 3 |
+
import io
|
| 4 |
+
import shutil
|
| 5 |
+
import threading
|
| 6 |
+
import textwrap
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
from numpy import array, transpose, pi
|
| 10 |
+
from numpy.testing import (assert_equal, assert_allclose,
|
| 11 |
+
assert_array_equal, assert_array_almost_equal)
|
| 12 |
+
import pytest
|
| 13 |
+
from pytest import raises as assert_raises
|
| 14 |
+
|
| 15 |
+
import scipy.sparse
|
| 16 |
+
import scipy.io._mmio
|
| 17 |
+
import scipy.io._fast_matrix_market as fmm
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
parametrize_args = [('integer', 'int'),
|
| 21 |
+
('unsigned-integer', 'uint')]
|
| 22 |
+
|
| 23 |
+
pytestmark = pytest.mark.thread_unsafe
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# Run the entire test suite on both _mmio and _fast_matrix_market implementations
|
| 27 |
+
@pytest.fixture(scope='module', params=(scipy.io._mmio, fmm), autouse=True)
|
| 28 |
+
def implementations(request):
|
| 29 |
+
global mminfo
|
| 30 |
+
global mmread
|
| 31 |
+
global mmwrite
|
| 32 |
+
mminfo = request.param.mminfo
|
| 33 |
+
mmread = request.param.mmread
|
| 34 |
+
mmwrite = request.param.mmwrite
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class TestMMIOArray:
|
| 38 |
+
def setup_method(self):
|
| 39 |
+
self.tmpdir = mkdtemp(suffix=str(threading.get_native_id()))
|
| 40 |
+
self.fn = os.path.join(self.tmpdir, 'testfile.mtx')
|
| 41 |
+
|
| 42 |
+
def teardown_method(self):
|
| 43 |
+
shutil.rmtree(self.tmpdir)
|
| 44 |
+
|
| 45 |
+
def check(self, a, info):
|
| 46 |
+
mmwrite(self.fn, a)
|
| 47 |
+
assert_equal(mminfo(self.fn), info)
|
| 48 |
+
b = mmread(self.fn, spmatrix=False)
|
| 49 |
+
assert_array_almost_equal(a, b)
|
| 50 |
+
|
| 51 |
+
def check_exact(self, a, info):
|
| 52 |
+
mmwrite(self.fn, a)
|
| 53 |
+
assert_equal(mminfo(self.fn), info)
|
| 54 |
+
b = mmread(self.fn, spmatrix=False)
|
| 55 |
+
assert_equal(a, b)
|
| 56 |
+
|
| 57 |
+
@pytest.mark.parametrize('typeval, dtype', parametrize_args)
|
| 58 |
+
def test_simple_integer(self, typeval, dtype):
|
| 59 |
+
self.check_exact(array([[1, 2], [3, 4]], dtype=dtype),
|
| 60 |
+
(2, 2, 4, 'array', typeval, 'general'))
|
| 61 |
+
|
| 62 |
+
@pytest.mark.parametrize('typeval, dtype', parametrize_args)
|
| 63 |
+
def test_32bit_integer(self, typeval, dtype):
|
| 64 |
+
a = array([[2**31-1, 2**31-2], [2**31-3, 2**31-4]], dtype=dtype)
|
| 65 |
+
self.check_exact(a, (2, 2, 4, 'array', typeval, 'general'))
|
| 66 |
+
|
| 67 |
+
def test_64bit_integer(self):
|
| 68 |
+
a = array([[2**31, 2**32], [2**63-2, 2**63-1]], dtype=np.int64)
|
| 69 |
+
if (np.intp(0).itemsize < 8) and mmwrite == scipy.io._mmio.mmwrite:
|
| 70 |
+
assert_raises(OverflowError, mmwrite, self.fn, a)
|
| 71 |
+
else:
|
| 72 |
+
self.check_exact(a, (2, 2, 4, 'array', 'integer', 'general'))
|
| 73 |
+
|
| 74 |
+
def test_64bit_unsigned_integer(self):
|
| 75 |
+
a = array([[2**31, 2**32], [2**64-2, 2**64-1]], dtype=np.uint64)
|
| 76 |
+
self.check_exact(a, (2, 2, 4, 'array', 'unsigned-integer', 'general'))
|
| 77 |
+
|
| 78 |
+
@pytest.mark.parametrize('typeval, dtype', parametrize_args)
|
| 79 |
+
def test_simple_upper_triangle_integer(self, typeval, dtype):
|
| 80 |
+
self.check_exact(array([[0, 1], [0, 0]], dtype=dtype),
|
| 81 |
+
(2, 2, 4, 'array', typeval, 'general'))
|
| 82 |
+
|
| 83 |
+
@pytest.mark.parametrize('typeval, dtype', parametrize_args)
|
| 84 |
+
def test_simple_lower_triangle_integer(self, typeval, dtype):
|
| 85 |
+
self.check_exact(array([[0, 0], [1, 0]], dtype=dtype),
|
| 86 |
+
(2, 2, 4, 'array', typeval, 'general'))
|
| 87 |
+
|
| 88 |
+
@pytest.mark.parametrize('typeval, dtype', parametrize_args)
|
| 89 |
+
def test_simple_rectangular_integer(self, typeval, dtype):
|
| 90 |
+
self.check_exact(array([[1, 2, 3], [4, 5, 6]], dtype=dtype),
|
| 91 |
+
(2, 3, 6, 'array', typeval, 'general'))
|
| 92 |
+
|
| 93 |
+
def test_simple_rectangular_float(self):
|
| 94 |
+
self.check([[1, 2], [3.5, 4], [5, 6]],
|
| 95 |
+
(3, 2, 6, 'array', 'real', 'general'))
|
| 96 |
+
|
| 97 |
+
def test_simple_float(self):
|
| 98 |
+
self.check([[1, 2], [3, 4.0]],
|
| 99 |
+
(2, 2, 4, 'array', 'real', 'general'))
|
| 100 |
+
|
| 101 |
+
def test_simple_complex(self):
|
| 102 |
+
self.check([[1, 2], [3, 4j]],
|
| 103 |
+
(2, 2, 4, 'array', 'complex', 'general'))
|
| 104 |
+
|
| 105 |
+
@pytest.mark.parametrize('typeval, dtype', parametrize_args)
|
| 106 |
+
def test_simple_symmetric_integer(self, typeval, dtype):
|
| 107 |
+
self.check_exact(array([[1, 2], [2, 4]], dtype=dtype),
|
| 108 |
+
(2, 2, 4, 'array', typeval, 'symmetric'))
|
| 109 |
+
|
| 110 |
+
def test_simple_skew_symmetric_integer(self):
|
| 111 |
+
self.check_exact([[0, 2], [-2, 0]],
|
| 112 |
+
(2, 2, 4, 'array', 'integer', 'skew-symmetric'))
|
| 113 |
+
|
| 114 |
+
def test_simple_skew_symmetric_float(self):
|
| 115 |
+
self.check(array([[0, 2], [-2.0, 0.0]], 'f'),
|
| 116 |
+
(2, 2, 4, 'array', 'real', 'skew-symmetric'))
|
| 117 |
+
|
| 118 |
+
def test_simple_hermitian_complex(self):
|
| 119 |
+
self.check([[1, 2+3j], [2-3j, 4]],
|
| 120 |
+
(2, 2, 4, 'array', 'complex', 'hermitian'))
|
| 121 |
+
|
| 122 |
+
def test_random_symmetric_float(self):
|
| 123 |
+
sz = (20, 20)
|
| 124 |
+
a = np.random.random(sz)
|
| 125 |
+
a = a + transpose(a)
|
| 126 |
+
self.check(a, (20, 20, 400, 'array', 'real', 'symmetric'))
|
| 127 |
+
|
| 128 |
+
def test_random_rectangular_float(self):
|
| 129 |
+
sz = (20, 15)
|
| 130 |
+
a = np.random.random(sz)
|
| 131 |
+
self.check(a, (20, 15, 300, 'array', 'real', 'general'))
|
| 132 |
+
|
| 133 |
+
@pytest.mark.fail_slow(10)
|
| 134 |
+
def test_bad_number_of_array_header_fields(self):
|
| 135 |
+
s = """\
|
| 136 |
+
%%MatrixMarket matrix array real general
|
| 137 |
+
3 3 999
|
| 138 |
+
1.0
|
| 139 |
+
2.0
|
| 140 |
+
3.0
|
| 141 |
+
4.0
|
| 142 |
+
5.0
|
| 143 |
+
6.0
|
| 144 |
+
7.0
|
| 145 |
+
8.0
|
| 146 |
+
9.0
|
| 147 |
+
"""
|
| 148 |
+
text = textwrap.dedent(s).encode('ascii')
|
| 149 |
+
with pytest.raises(ValueError, match='not of length 2'):
|
| 150 |
+
scipy.io.mmread(io.BytesIO(text))
|
| 151 |
+
|
| 152 |
+
def test_gh13634_non_skew_symmetric_int(self):
|
| 153 |
+
self.check_exact(array([[1, 2], [-2, 99]], dtype=np.int32),
|
| 154 |
+
(2, 2, 4, 'array', 'integer', 'general'))
|
| 155 |
+
|
| 156 |
+
def test_gh13634_non_skew_symmetric_float(self):
|
| 157 |
+
self.check(array([[1, 2], [-2, 99.]], dtype=np.float32),
|
| 158 |
+
(2, 2, 4, 'array', 'real', 'general'))
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
class TestMMIOSparseCSR(TestMMIOArray):
|
| 162 |
+
def setup_method(self):
|
| 163 |
+
self.tmpdir = mkdtemp(suffix=str(threading.get_native_id()))
|
| 164 |
+
self.fn = os.path.join(self.tmpdir, 'testfile.mtx')
|
| 165 |
+
|
| 166 |
+
def teardown_method(self):
|
| 167 |
+
shutil.rmtree(self.tmpdir)
|
| 168 |
+
|
| 169 |
+
def check(self, a, info):
|
| 170 |
+
mmwrite(self.fn, a)
|
| 171 |
+
assert_equal(mminfo(self.fn), info)
|
| 172 |
+
b = mmread(self.fn, spmatrix=False)
|
| 173 |
+
assert_array_almost_equal(a.toarray(), b.toarray())
|
| 174 |
+
|
| 175 |
+
def check_exact(self, a, info):
|
| 176 |
+
mmwrite(self.fn, a)
|
| 177 |
+
assert_equal(mminfo(self.fn), info)
|
| 178 |
+
b = mmread(self.fn, spmatrix=False)
|
| 179 |
+
assert_equal(a.toarray(), b.toarray())
|
| 180 |
+
|
| 181 |
+
@pytest.mark.parametrize('typeval, dtype', parametrize_args)
|
| 182 |
+
def test_simple_integer(self, typeval, dtype):
|
| 183 |
+
self.check_exact(scipy.sparse.csr_array([[1, 2], [3, 4]], dtype=dtype),
|
| 184 |
+
(2, 2, 4, 'coordinate', typeval, 'general'))
|
| 185 |
+
|
| 186 |
+
def test_32bit_integer(self):
|
| 187 |
+
a = scipy.sparse.csr_array(array([[2**31-1, -2**31+2],
|
| 188 |
+
[2**31-3, 2**31-4]],
|
| 189 |
+
dtype=np.int32))
|
| 190 |
+
self.check_exact(a, (2, 2, 4, 'coordinate', 'integer', 'general'))
|
| 191 |
+
|
| 192 |
+
def test_64bit_integer(self):
|
| 193 |
+
a = scipy.sparse.csr_array(array([[2**32+1, 2**32+1],
|
| 194 |
+
[-2**63+2, 2**63-2]],
|
| 195 |
+
dtype=np.int64))
|
| 196 |
+
if (np.intp(0).itemsize < 8) and mmwrite == scipy.io._mmio.mmwrite:
|
| 197 |
+
assert_raises(OverflowError, mmwrite, self.fn, a)
|
| 198 |
+
else:
|
| 199 |
+
self.check_exact(a, (2, 2, 4, 'coordinate', 'integer', 'general'))
|
| 200 |
+
|
| 201 |
+
def test_32bit_unsigned_integer(self):
|
| 202 |
+
a = scipy.sparse.csr_array(array([[2**31-1, 2**31-2],
|
| 203 |
+
[2**31-3, 2**31-4]],
|
| 204 |
+
dtype=np.uint32))
|
| 205 |
+
self.check_exact(a, (2, 2, 4, 'coordinate', 'unsigned-integer', 'general'))
|
| 206 |
+
|
| 207 |
+
def test_64bit_unsigned_integer(self):
|
| 208 |
+
a = scipy.sparse.csr_array(array([[2**32+1, 2**32+1],
|
| 209 |
+
[2**64-2, 2**64-1]],
|
| 210 |
+
dtype=np.uint64))
|
| 211 |
+
self.check_exact(a, (2, 2, 4, 'coordinate', 'unsigned-integer', 'general'))
|
| 212 |
+
|
| 213 |
+
@pytest.mark.parametrize('typeval, dtype', parametrize_args)
|
| 214 |
+
def test_simple_upper_triangle_integer(self, typeval, dtype):
|
| 215 |
+
self.check_exact(scipy.sparse.csr_array([[0, 1], [0, 0]], dtype=dtype),
|
| 216 |
+
(2, 2, 1, 'coordinate', typeval, 'general'))
|
| 217 |
+
|
| 218 |
+
@pytest.mark.parametrize('typeval, dtype', parametrize_args)
|
| 219 |
+
def test_simple_lower_triangle_integer(self, typeval, dtype):
|
| 220 |
+
self.check_exact(scipy.sparse.csr_array([[0, 0], [1, 0]], dtype=dtype),
|
| 221 |
+
(2, 2, 1, 'coordinate', typeval, 'general'))
|
| 222 |
+
|
| 223 |
+
@pytest.mark.parametrize('typeval, dtype', parametrize_args)
|
| 224 |
+
def test_simple_rectangular_integer(self, typeval, dtype):
|
| 225 |
+
self.check_exact(scipy.sparse.csr_array([[1, 2, 3], [4, 5, 6]], dtype=dtype),
|
| 226 |
+
(2, 3, 6, 'coordinate', typeval, 'general'))
|
| 227 |
+
|
| 228 |
+
def test_simple_rectangular_float(self):
|
| 229 |
+
self.check(scipy.sparse.csr_array([[1, 2], [3.5, 4], [5, 6]]),
|
| 230 |
+
(3, 2, 6, 'coordinate', 'real', 'general'))
|
| 231 |
+
|
| 232 |
+
def test_simple_float(self):
|
| 233 |
+
self.check(scipy.sparse.csr_array([[1, 2], [3, 4.0]]),
|
| 234 |
+
(2, 2, 4, 'coordinate', 'real', 'general'))
|
| 235 |
+
|
| 236 |
+
def test_simple_complex(self):
|
| 237 |
+
self.check(scipy.sparse.csr_array([[1, 2], [3, 4j]]),
|
| 238 |
+
(2, 2, 4, 'coordinate', 'complex', 'general'))
|
| 239 |
+
|
| 240 |
+
@pytest.mark.parametrize('typeval, dtype', parametrize_args)
|
| 241 |
+
def test_simple_symmetric_integer(self, typeval, dtype):
|
| 242 |
+
self.check_exact(scipy.sparse.csr_array([[1, 2], [2, 4]], dtype=dtype),
|
| 243 |
+
(2, 2, 3, 'coordinate', typeval, 'symmetric'))
|
| 244 |
+
|
| 245 |
+
def test_simple_skew_symmetric_integer(self):
|
| 246 |
+
self.check_exact(scipy.sparse.csr_array([[0, 2], [-2, 0]]),
|
| 247 |
+
(2, 2, 1, 'coordinate', 'integer', 'skew-symmetric'))
|
| 248 |
+
|
| 249 |
+
def test_simple_skew_symmetric_float(self):
|
| 250 |
+
self.check(scipy.sparse.csr_array(array([[0, 2], [-2.0, 0]], 'f')),
|
| 251 |
+
(2, 2, 1, 'coordinate', 'real', 'skew-symmetric'))
|
| 252 |
+
|
| 253 |
+
def test_simple_hermitian_complex(self):
|
| 254 |
+
self.check(scipy.sparse.csr_array([[1, 2+3j], [2-3j, 4]]),
|
| 255 |
+
(2, 2, 3, 'coordinate', 'complex', 'hermitian'))
|
| 256 |
+
|
| 257 |
+
def test_random_symmetric_float(self):
|
| 258 |
+
sz = (20, 20)
|
| 259 |
+
a = np.random.random(sz)
|
| 260 |
+
a = a + transpose(a)
|
| 261 |
+
a = scipy.sparse.csr_array(a)
|
| 262 |
+
self.check(a, (20, 20, 210, 'coordinate', 'real', 'symmetric'))
|
| 263 |
+
|
| 264 |
+
def test_random_rectangular_float(self):
|
| 265 |
+
sz = (20, 15)
|
| 266 |
+
a = np.random.random(sz)
|
| 267 |
+
a = scipy.sparse.csr_array(a)
|
| 268 |
+
self.check(a, (20, 15, 300, 'coordinate', 'real', 'general'))
|
| 269 |
+
|
| 270 |
+
def test_simple_pattern(self):
|
| 271 |
+
a = scipy.sparse.csr_array([[0, 1.5], [3.0, 2.5]])
|
| 272 |
+
p = np.zeros_like(a.toarray())
|
| 273 |
+
p[a.toarray() > 0] = 1
|
| 274 |
+
info = (2, 2, 3, 'coordinate', 'pattern', 'general')
|
| 275 |
+
mmwrite(self.fn, a, field='pattern')
|
| 276 |
+
assert_equal(mminfo(self.fn), info)
|
| 277 |
+
b = mmread(self.fn, spmatrix=False)
|
| 278 |
+
assert_array_almost_equal(p, b.toarray())
|
| 279 |
+
assert not scipy.sparse.isspmatrix(b)
|
| 280 |
+
|
| 281 |
+
b = mmread(self.fn, spmatrix=True)
|
| 282 |
+
assert scipy.sparse.isspmatrix(b)
|
| 283 |
+
b = mmread(self.fn) # chk default
|
| 284 |
+
assert scipy.sparse.isspmatrix(b)
|
| 285 |
+
|
| 286 |
+
def test_gh13634_non_skew_symmetric_int(self):
|
| 287 |
+
a = scipy.sparse.csr_array([[1, 2], [-2, 99]], dtype=np.int32)
|
| 288 |
+
self.check_exact(a, (2, 2, 4, 'coordinate', 'integer', 'general'))
|
| 289 |
+
|
| 290 |
+
def test_gh13634_non_skew_symmetric_float(self):
|
| 291 |
+
a = scipy.sparse.csr_array([[1, 2], [-2, 99.]], dtype=np.float32)
|
| 292 |
+
self.check(a, (2, 2, 4, 'coordinate', 'real', 'general'))
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
_32bit_integer_dense_example = '''\
|
| 296 |
+
%%MatrixMarket matrix array integer general
|
| 297 |
+
2 2
|
| 298 |
+
2147483647
|
| 299 |
+
2147483646
|
| 300 |
+
2147483647
|
| 301 |
+
2147483646
|
| 302 |
+
'''
|
| 303 |
+
|
| 304 |
+
_32bit_integer_sparse_example = '''\
|
| 305 |
+
%%MatrixMarket matrix coordinate integer symmetric
|
| 306 |
+
2 2 2
|
| 307 |
+
1 1 2147483647
|
| 308 |
+
2 2 2147483646
|
| 309 |
+
'''
|
| 310 |
+
|
| 311 |
+
_64bit_integer_dense_example = '''\
|
| 312 |
+
%%MatrixMarket matrix array integer general
|
| 313 |
+
2 2
|
| 314 |
+
2147483648
|
| 315 |
+
-9223372036854775806
|
| 316 |
+
-2147483648
|
| 317 |
+
9223372036854775807
|
| 318 |
+
'''
|
| 319 |
+
|
| 320 |
+
_64bit_integer_sparse_general_example = '''\
|
| 321 |
+
%%MatrixMarket matrix coordinate integer general
|
| 322 |
+
2 2 3
|
| 323 |
+
1 1 2147483648
|
| 324 |
+
1 2 9223372036854775807
|
| 325 |
+
2 2 9223372036854775807
|
| 326 |
+
'''
|
| 327 |
+
|
| 328 |
+
_64bit_integer_sparse_symmetric_example = '''\
|
| 329 |
+
%%MatrixMarket matrix coordinate integer symmetric
|
| 330 |
+
2 2 3
|
| 331 |
+
1 1 2147483648
|
| 332 |
+
1 2 -9223372036854775807
|
| 333 |
+
2 2 9223372036854775807
|
| 334 |
+
'''
|
| 335 |
+
|
| 336 |
+
_64bit_integer_sparse_skew_example = '''\
|
| 337 |
+
%%MatrixMarket matrix coordinate integer skew-symmetric
|
| 338 |
+
2 2 3
|
| 339 |
+
1 1 2147483648
|
| 340 |
+
1 2 -9223372036854775807
|
| 341 |
+
2 2 9223372036854775807
|
| 342 |
+
'''
|
| 343 |
+
|
| 344 |
+
_over64bit_integer_dense_example = '''\
|
| 345 |
+
%%MatrixMarket matrix array integer general
|
| 346 |
+
2 2
|
| 347 |
+
2147483648
|
| 348 |
+
9223372036854775807
|
| 349 |
+
2147483648
|
| 350 |
+
9223372036854775808
|
| 351 |
+
'''
|
| 352 |
+
|
| 353 |
+
_over64bit_integer_sparse_example = '''\
|
| 354 |
+
%%MatrixMarket matrix coordinate integer symmetric
|
| 355 |
+
2 2 2
|
| 356 |
+
1 1 2147483648
|
| 357 |
+
2 2 19223372036854775808
|
| 358 |
+
'''
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
class TestMMIOReadLargeIntegers:
|
| 362 |
+
def setup_method(self):
|
| 363 |
+
self.tmpdir = mkdtemp(suffix=str(threading.get_native_id()))
|
| 364 |
+
self.fn = os.path.join(self.tmpdir, 'testfile.mtx')
|
| 365 |
+
|
| 366 |
+
def teardown_method(self):
|
| 367 |
+
shutil.rmtree(self.tmpdir)
|
| 368 |
+
|
| 369 |
+
def check_read(self, example, a, info, dense, over32, over64):
|
| 370 |
+
with open(self.fn, 'w') as f:
|
| 371 |
+
f.write(example)
|
| 372 |
+
assert_equal(mminfo(self.fn), info)
|
| 373 |
+
if ((over32 and (np.intp(0).itemsize < 8) and mmwrite == scipy.io._mmio.mmwrite)
|
| 374 |
+
or over64):
|
| 375 |
+
assert_raises(OverflowError, mmread, self.fn)
|
| 376 |
+
else:
|
| 377 |
+
b = mmread(self.fn, spmatrix=False)
|
| 378 |
+
if not dense:
|
| 379 |
+
b = b.toarray()
|
| 380 |
+
assert_equal(a, b)
|
| 381 |
+
|
| 382 |
+
def test_read_32bit_integer_dense(self):
|
| 383 |
+
a = array([[2**31-1, 2**31-1],
|
| 384 |
+
[2**31-2, 2**31-2]], dtype=np.int64)
|
| 385 |
+
self.check_read(_32bit_integer_dense_example,
|
| 386 |
+
a,
|
| 387 |
+
(2, 2, 4, 'array', 'integer', 'general'),
|
| 388 |
+
dense=True,
|
| 389 |
+
over32=False,
|
| 390 |
+
over64=False)
|
| 391 |
+
|
| 392 |
+
def test_read_32bit_integer_sparse(self):
|
| 393 |
+
a = array([[2**31-1, 0],
|
| 394 |
+
[0, 2**31-2]], dtype=np.int64)
|
| 395 |
+
self.check_read(_32bit_integer_sparse_example,
|
| 396 |
+
a,
|
| 397 |
+
(2, 2, 2, 'coordinate', 'integer', 'symmetric'),
|
| 398 |
+
dense=False,
|
| 399 |
+
over32=False,
|
| 400 |
+
over64=False)
|
| 401 |
+
|
| 402 |
+
def test_read_64bit_integer_dense(self):
|
| 403 |
+
a = array([[2**31, -2**31],
|
| 404 |
+
[-2**63+2, 2**63-1]], dtype=np.int64)
|
| 405 |
+
self.check_read(_64bit_integer_dense_example,
|
| 406 |
+
a,
|
| 407 |
+
(2, 2, 4, 'array', 'integer', 'general'),
|
| 408 |
+
dense=True,
|
| 409 |
+
over32=True,
|
| 410 |
+
over64=False)
|
| 411 |
+
|
| 412 |
+
def test_read_64bit_integer_sparse_general(self):
|
| 413 |
+
a = array([[2**31, 2**63-1],
|
| 414 |
+
[0, 2**63-1]], dtype=np.int64)
|
| 415 |
+
self.check_read(_64bit_integer_sparse_general_example,
|
| 416 |
+
a,
|
| 417 |
+
(2, 2, 3, 'coordinate', 'integer', 'general'),
|
| 418 |
+
dense=False,
|
| 419 |
+
over32=True,
|
| 420 |
+
over64=False)
|
| 421 |
+
|
| 422 |
+
def test_read_64bit_integer_sparse_symmetric(self):
|
| 423 |
+
a = array([[2**31, -2**63+1],
|
| 424 |
+
[-2**63+1, 2**63-1]], dtype=np.int64)
|
| 425 |
+
self.check_read(_64bit_integer_sparse_symmetric_example,
|
| 426 |
+
a,
|
| 427 |
+
(2, 2, 3, 'coordinate', 'integer', 'symmetric'),
|
| 428 |
+
dense=False,
|
| 429 |
+
over32=True,
|
| 430 |
+
over64=False)
|
| 431 |
+
|
| 432 |
+
def test_read_64bit_integer_sparse_skew(self):
|
| 433 |
+
a = array([[2**31, -2**63+1],
|
| 434 |
+
[2**63-1, 2**63-1]], dtype=np.int64)
|
| 435 |
+
self.check_read(_64bit_integer_sparse_skew_example,
|
| 436 |
+
a,
|
| 437 |
+
(2, 2, 3, 'coordinate', 'integer', 'skew-symmetric'),
|
| 438 |
+
dense=False,
|
| 439 |
+
over32=True,
|
| 440 |
+
over64=False)
|
| 441 |
+
|
| 442 |
+
def test_read_over64bit_integer_dense(self):
|
| 443 |
+
self.check_read(_over64bit_integer_dense_example,
|
| 444 |
+
None,
|
| 445 |
+
(2, 2, 4, 'array', 'integer', 'general'),
|
| 446 |
+
dense=True,
|
| 447 |
+
over32=True,
|
| 448 |
+
over64=True)
|
| 449 |
+
|
| 450 |
+
def test_read_over64bit_integer_sparse(self):
|
| 451 |
+
self.check_read(_over64bit_integer_sparse_example,
|
| 452 |
+
None,
|
| 453 |
+
(2, 2, 2, 'coordinate', 'integer', 'symmetric'),
|
| 454 |
+
dense=False,
|
| 455 |
+
over32=True,
|
| 456 |
+
over64=True)
|
| 457 |
+
|
| 458 |
+
|
| 459 |
+
_general_example = '''\
|
| 460 |
+
%%MatrixMarket matrix coordinate real general
|
| 461 |
+
%=================================================================================
|
| 462 |
+
%
|
| 463 |
+
% This ASCII file represents a sparse MxN matrix with L
|
| 464 |
+
% nonzeros in the following Matrix Market format:
|
| 465 |
+
%
|
| 466 |
+
% +----------------------------------------------+
|
| 467 |
+
% |%%MatrixMarket matrix coordinate real general | <--- header line
|
| 468 |
+
% |% | <--+
|
| 469 |
+
% |% comments | |-- 0 or more comment lines
|
| 470 |
+
% |% | <--+
|
| 471 |
+
% | M N L | <--- rows, columns, entries
|
| 472 |
+
% | I1 J1 A(I1, J1) | <--+
|
| 473 |
+
% | I2 J2 A(I2, J2) | |
|
| 474 |
+
% | I3 J3 A(I3, J3) | |-- L lines
|
| 475 |
+
% | . . . | |
|
| 476 |
+
% | IL JL A(IL, JL) | <--+
|
| 477 |
+
% +----------------------------------------------+
|
| 478 |
+
%
|
| 479 |
+
% Indices are 1-based, i.e. A(1,1) is the first element.
|
| 480 |
+
%
|
| 481 |
+
%=================================================================================
|
| 482 |
+
5 5 8
|
| 483 |
+
1 1 1.000e+00
|
| 484 |
+
2 2 1.050e+01
|
| 485 |
+
3 3 1.500e-02
|
| 486 |
+
1 4 6.000e+00
|
| 487 |
+
4 2 2.505e+02
|
| 488 |
+
4 4 -2.800e+02
|
| 489 |
+
4 5 3.332e+01
|
| 490 |
+
5 5 1.200e+01
|
| 491 |
+
'''
|
| 492 |
+
|
| 493 |
+
_hermitian_example = '''\
|
| 494 |
+
%%MatrixMarket matrix coordinate complex hermitian
|
| 495 |
+
5 5 7
|
| 496 |
+
1 1 1.0 0
|
| 497 |
+
2 2 10.5 0
|
| 498 |
+
4 2 250.5 22.22
|
| 499 |
+
3 3 1.5e-2 0
|
| 500 |
+
4 4 -2.8e2 0
|
| 501 |
+
5 5 12. 0
|
| 502 |
+
5 4 0 33.32
|
| 503 |
+
'''
|
| 504 |
+
|
| 505 |
+
_skew_example = '''\
|
| 506 |
+
%%MatrixMarket matrix coordinate real skew-symmetric
|
| 507 |
+
5 5 7
|
| 508 |
+
1 1 1.0
|
| 509 |
+
2 2 10.5
|
| 510 |
+
4 2 250.5
|
| 511 |
+
3 3 1.5e-2
|
| 512 |
+
4 4 -2.8e2
|
| 513 |
+
5 5 12.
|
| 514 |
+
5 4 0
|
| 515 |
+
'''
|
| 516 |
+
|
| 517 |
+
_symmetric_example = '''\
|
| 518 |
+
%%MatrixMarket matrix coordinate real symmetric
|
| 519 |
+
5 5 7
|
| 520 |
+
1 1 1.0
|
| 521 |
+
2 2 10.5
|
| 522 |
+
4 2 250.5
|
| 523 |
+
3 3 1.5e-2
|
| 524 |
+
4 4 -2.8e2
|
| 525 |
+
5 5 12.
|
| 526 |
+
5 4 8
|
| 527 |
+
'''
|
| 528 |
+
|
| 529 |
+
_symmetric_pattern_example = '''\
|
| 530 |
+
%%MatrixMarket matrix coordinate pattern symmetric
|
| 531 |
+
5 5 7
|
| 532 |
+
1 1
|
| 533 |
+
2 2
|
| 534 |
+
4 2
|
| 535 |
+
3 3
|
| 536 |
+
4 4
|
| 537 |
+
5 5
|
| 538 |
+
5 4
|
| 539 |
+
'''
|
| 540 |
+
|
| 541 |
+
# example (without comment lines) from Figure 1 in
|
| 542 |
+
# https://math.nist.gov/MatrixMarket/reports/MMformat.ps
|
| 543 |
+
_empty_lines_example = '''\
|
| 544 |
+
%%MatrixMarket MATRIX Coordinate Real General
|
| 545 |
+
|
| 546 |
+
5 5 8
|
| 547 |
+
|
| 548 |
+
1 1 1.0
|
| 549 |
+
2 2 10.5
|
| 550 |
+
3 3 1.5e-2
|
| 551 |
+
4 4 -2.8E2
|
| 552 |
+
5 5 12.
|
| 553 |
+
1 4 6
|
| 554 |
+
4 2 250.5
|
| 555 |
+
4 5 33.32
|
| 556 |
+
|
| 557 |
+
'''
|
| 558 |
+
|
| 559 |
+
|
| 560 |
+
class TestMMIOCoordinate:
|
| 561 |
+
def setup_method(self):
|
| 562 |
+
self.tmpdir = mkdtemp(suffix=str(threading.get_native_id()))
|
| 563 |
+
self.fn = os.path.join(self.tmpdir, 'testfile.mtx')
|
| 564 |
+
|
| 565 |
+
def teardown_method(self):
|
| 566 |
+
shutil.rmtree(self.tmpdir)
|
| 567 |
+
|
| 568 |
+
def check_read(self, example, a, info):
|
| 569 |
+
f = open(self.fn, 'w')
|
| 570 |
+
f.write(example)
|
| 571 |
+
f.close()
|
| 572 |
+
assert_equal(mminfo(self.fn), info)
|
| 573 |
+
b = mmread(self.fn, spmatrix=False).toarray()
|
| 574 |
+
assert_array_almost_equal(a, b)
|
| 575 |
+
|
| 576 |
+
def test_read_general(self):
|
| 577 |
+
a = [[1, 0, 0, 6, 0],
|
| 578 |
+
[0, 10.5, 0, 0, 0],
|
| 579 |
+
[0, 0, .015, 0, 0],
|
| 580 |
+
[0, 250.5, 0, -280, 33.32],
|
| 581 |
+
[0, 0, 0, 0, 12]]
|
| 582 |
+
self.check_read(_general_example, a,
|
| 583 |
+
(5, 5, 8, 'coordinate', 'real', 'general'))
|
| 584 |
+
|
| 585 |
+
def test_read_hermitian(self):
|
| 586 |
+
a = [[1, 0, 0, 0, 0],
|
| 587 |
+
[0, 10.5, 0, 250.5 - 22.22j, 0],
|
| 588 |
+
[0, 0, .015, 0, 0],
|
| 589 |
+
[0, 250.5 + 22.22j, 0, -280, -33.32j],
|
| 590 |
+
[0, 0, 0, 33.32j, 12]]
|
| 591 |
+
self.check_read(_hermitian_example, a,
|
| 592 |
+
(5, 5, 7, 'coordinate', 'complex', 'hermitian'))
|
| 593 |
+
|
| 594 |
+
def test_read_skew(self):
|
| 595 |
+
a = [[1, 0, 0, 0, 0],
|
| 596 |
+
[0, 10.5, 0, -250.5, 0],
|
| 597 |
+
[0, 0, .015, 0, 0],
|
| 598 |
+
[0, 250.5, 0, -280, 0],
|
| 599 |
+
[0, 0, 0, 0, 12]]
|
| 600 |
+
self.check_read(_skew_example, a,
|
| 601 |
+
(5, 5, 7, 'coordinate', 'real', 'skew-symmetric'))
|
| 602 |
+
|
| 603 |
+
def test_read_symmetric(self):
|
| 604 |
+
a = [[1, 0, 0, 0, 0],
|
| 605 |
+
[0, 10.5, 0, 250.5, 0],
|
| 606 |
+
[0, 0, .015, 0, 0],
|
| 607 |
+
[0, 250.5, 0, -280, 8],
|
| 608 |
+
[0, 0, 0, 8, 12]]
|
| 609 |
+
self.check_read(_symmetric_example, a,
|
| 610 |
+
(5, 5, 7, 'coordinate', 'real', 'symmetric'))
|
| 611 |
+
|
| 612 |
+
def test_read_symmetric_pattern(self):
|
| 613 |
+
a = [[1, 0, 0, 0, 0],
|
| 614 |
+
[0, 1, 0, 1, 0],
|
| 615 |
+
[0, 0, 1, 0, 0],
|
| 616 |
+
[0, 1, 0, 1, 1],
|
| 617 |
+
[0, 0, 0, 1, 1]]
|
| 618 |
+
self.check_read(_symmetric_pattern_example, a,
|
| 619 |
+
(5, 5, 7, 'coordinate', 'pattern', 'symmetric'))
|
| 620 |
+
|
| 621 |
+
def test_read_empty_lines(self):
|
| 622 |
+
a = [[1, 0, 0, 6, 0],
|
| 623 |
+
[0, 10.5, 0, 0, 0],
|
| 624 |
+
[0, 0, .015, 0, 0],
|
| 625 |
+
[0, 250.5, 0, -280, 33.32],
|
| 626 |
+
[0, 0, 0, 0, 12]]
|
| 627 |
+
self.check_read(_empty_lines_example, a,
|
| 628 |
+
(5, 5, 8, 'coordinate', 'real', 'general'))
|
| 629 |
+
|
| 630 |
+
def test_empty_write_read(self):
|
| 631 |
+
# https://github.com/scipy/scipy/issues/1410 (Trac #883)
|
| 632 |
+
|
| 633 |
+
b = scipy.sparse.coo_array((10, 10))
|
| 634 |
+
mmwrite(self.fn, b)
|
| 635 |
+
|
| 636 |
+
assert_equal(mminfo(self.fn),
|
| 637 |
+
(10, 10, 0, 'coordinate', 'real', 'symmetric'))
|
| 638 |
+
a = b.toarray()
|
| 639 |
+
b = mmread(self.fn, spmatrix=False).toarray()
|
| 640 |
+
assert_array_almost_equal(a, b)
|
| 641 |
+
|
| 642 |
+
def test_bzip2_py3(self):
|
| 643 |
+
# test if fix for #2152 works
|
| 644 |
+
try:
|
| 645 |
+
# bz2 module isn't always built when building Python.
|
| 646 |
+
import bz2
|
| 647 |
+
except ImportError:
|
| 648 |
+
return
|
| 649 |
+
I = array([0, 0, 1, 2, 3, 3, 3, 4])
|
| 650 |
+
J = array([0, 3, 1, 2, 1, 3, 4, 4])
|
| 651 |
+
V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0])
|
| 652 |
+
|
| 653 |
+
b = scipy.sparse.coo_array((V, (I, J)), shape=(5, 5))
|
| 654 |
+
|
| 655 |
+
mmwrite(self.fn, b)
|
| 656 |
+
|
| 657 |
+
fn_bzip2 = f"{self.fn}.bz2"
|
| 658 |
+
with open(self.fn, 'rb') as f_in:
|
| 659 |
+
f_out = bz2.BZ2File(fn_bzip2, 'wb')
|
| 660 |
+
f_out.write(f_in.read())
|
| 661 |
+
f_out.close()
|
| 662 |
+
|
| 663 |
+
a = mmread(fn_bzip2, spmatrix=False).toarray()
|
| 664 |
+
assert_array_almost_equal(a, b.toarray())
|
| 665 |
+
|
| 666 |
+
def test_gzip_py3(self):
|
| 667 |
+
# test if fix for #2152 works
|
| 668 |
+
try:
|
| 669 |
+
# gzip module can be missing from Python installation
|
| 670 |
+
import gzip
|
| 671 |
+
except ImportError:
|
| 672 |
+
return
|
| 673 |
+
I = array([0, 0, 1, 2, 3, 3, 3, 4])
|
| 674 |
+
J = array([0, 3, 1, 2, 1, 3, 4, 4])
|
| 675 |
+
V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0])
|
| 676 |
+
|
| 677 |
+
b = scipy.sparse.coo_array((V, (I, J)), shape=(5, 5))
|
| 678 |
+
|
| 679 |
+
mmwrite(self.fn, b)
|
| 680 |
+
|
| 681 |
+
fn_gzip = f"{self.fn}.gz"
|
| 682 |
+
with open(self.fn, 'rb') as f_in:
|
| 683 |
+
f_out = gzip.open(fn_gzip, 'wb')
|
| 684 |
+
f_out.write(f_in.read())
|
| 685 |
+
f_out.close()
|
| 686 |
+
|
| 687 |
+
a = mmread(fn_gzip, spmatrix=False).toarray()
|
| 688 |
+
assert_array_almost_equal(a, b.toarray())
|
| 689 |
+
|
| 690 |
+
def test_real_write_read(self):
|
| 691 |
+
I = array([0, 0, 1, 2, 3, 3, 3, 4])
|
| 692 |
+
J = array([0, 3, 1, 2, 1, 3, 4, 4])
|
| 693 |
+
V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0])
|
| 694 |
+
|
| 695 |
+
b = scipy.sparse.coo_array((V, (I, J)), shape=(5, 5))
|
| 696 |
+
|
| 697 |
+
mmwrite(self.fn, b)
|
| 698 |
+
|
| 699 |
+
assert_equal(mminfo(self.fn),
|
| 700 |
+
(5, 5, 8, 'coordinate', 'real', 'general'))
|
| 701 |
+
a = b.toarray()
|
| 702 |
+
b = mmread(self.fn, spmatrix=False).toarray()
|
| 703 |
+
assert_array_almost_equal(a, b)
|
| 704 |
+
|
| 705 |
+
def test_complex_write_read(self):
|
| 706 |
+
I = array([0, 0, 1, 2, 3, 3, 3, 4])
|
| 707 |
+
J = array([0, 3, 1, 2, 1, 3, 4, 4])
|
| 708 |
+
V = array([1.0 + 3j, 6.0 + 2j, 10.50 + 0.9j, 0.015 + -4.4j,
|
| 709 |
+
250.5 + 0j, -280.0 + 5j, 33.32 + 6.4j, 12.00 + 0.8j])
|
| 710 |
+
|
| 711 |
+
b = scipy.sparse.coo_array((V, (I, J)), shape=(5, 5))
|
| 712 |
+
|
| 713 |
+
mmwrite(self.fn, b)
|
| 714 |
+
|
| 715 |
+
assert_equal(mminfo(self.fn),
|
| 716 |
+
(5, 5, 8, 'coordinate', 'complex', 'general'))
|
| 717 |
+
a = b.toarray()
|
| 718 |
+
b = mmread(self.fn, spmatrix=False).toarray()
|
| 719 |
+
assert_array_almost_equal(a, b)
|
| 720 |
+
|
| 721 |
+
def test_sparse_formats(self, tmp_path):
|
| 722 |
+
# Note: `tmp_path` is a pytest fixture, it handles cleanup
|
| 723 |
+
tmpdir = tmp_path / 'sparse_formats'
|
| 724 |
+
tmpdir.mkdir()
|
| 725 |
+
|
| 726 |
+
mats = []
|
| 727 |
+
I = array([0, 0, 1, 2, 3, 3, 3, 4])
|
| 728 |
+
J = array([0, 3, 1, 2, 1, 3, 4, 4])
|
| 729 |
+
|
| 730 |
+
V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0])
|
| 731 |
+
mats.append(scipy.sparse.coo_array((V, (I, J)), shape=(5, 5)))
|
| 732 |
+
|
| 733 |
+
V = array([1.0 + 3j, 6.0 + 2j, 10.50 + 0.9j, 0.015 + -4.4j,
|
| 734 |
+
250.5 + 0j, -280.0 + 5j, 33.32 + 6.4j, 12.00 + 0.8j])
|
| 735 |
+
mats.append(scipy.sparse.coo_array((V, (I, J)), shape=(5, 5)))
|
| 736 |
+
|
| 737 |
+
for mat in mats:
|
| 738 |
+
expected = mat.toarray()
|
| 739 |
+
for fmt in ['csr', 'csc', 'coo']:
|
| 740 |
+
fname = tmpdir / (fmt + '.mtx')
|
| 741 |
+
mmwrite(fname, mat.asformat(fmt))
|
| 742 |
+
result = mmread(fname, spmatrix=False).toarray()
|
| 743 |
+
assert_array_almost_equal(result, expected)
|
| 744 |
+
|
| 745 |
+
def test_precision(self):
|
| 746 |
+
test_values = [pi] + [10**(i) for i in range(0, -10, -1)]
|
| 747 |
+
test_precisions = range(1, 10)
|
| 748 |
+
for value in test_values:
|
| 749 |
+
for precision in test_precisions:
|
| 750 |
+
# construct sparse matrix with test value at last main diagonal
|
| 751 |
+
n = 10**precision + 1
|
| 752 |
+
A = scipy.sparse.dok_array((n, n))
|
| 753 |
+
A[n-1, n-1] = value
|
| 754 |
+
# write matrix with test precision and read again
|
| 755 |
+
mmwrite(self.fn, A, precision=precision)
|
| 756 |
+
A = scipy.io.mmread(self.fn, spmatrix=False)
|
| 757 |
+
# check for right entries in matrix
|
| 758 |
+
assert_array_equal(A.row, [n-1])
|
| 759 |
+
assert_array_equal(A.col, [n-1])
|
| 760 |
+
assert_allclose(A.data, [float(f'{value:.{precision}g}')])
|
| 761 |
+
|
| 762 |
+
def test_bad_number_of_coordinate_header_fields(self):
|
| 763 |
+
s = """\
|
| 764 |
+
%%MatrixMarket matrix coordinate real general
|
| 765 |
+
5 5 8 999
|
| 766 |
+
1 1 1.000e+00
|
| 767 |
+
2 2 1.050e+01
|
| 768 |
+
3 3 1.500e-02
|
| 769 |
+
1 4 6.000e+00
|
| 770 |
+
4 2 2.505e+02
|
| 771 |
+
4 4 -2.800e+02
|
| 772 |
+
4 5 3.332e+01
|
| 773 |
+
5 5 1.200e+01
|
| 774 |
+
"""
|
| 775 |
+
text = textwrap.dedent(s).encode('ascii')
|
| 776 |
+
with pytest.raises(ValueError, match='not of length 3'):
|
| 777 |
+
scipy.io.mmread(io.BytesIO(text))
|
| 778 |
+
|
| 779 |
+
|
| 780 |
+
def test_gh11389():
|
| 781 |
+
mmread(io.StringIO("%%MatrixMarket matrix coordinate complex symmetric\n"
|
| 782 |
+
" 1 1 1\n"
|
| 783 |
+
"1 1 -2.1846000000000e+02 0.0000000000000e+00"),
|
| 784 |
+
spmatrix=False)
|
| 785 |
+
|
| 786 |
+
|
| 787 |
+
def test_gh18123(tmp_path):
|
| 788 |
+
lines = [" %%MatrixMarket matrix coordinate real general\n",
|
| 789 |
+
"5 5 3\n",
|
| 790 |
+
"2 3 1.0\n",
|
| 791 |
+
"3 4 2.0\n",
|
| 792 |
+
"3 5 3.0\n"]
|
| 793 |
+
test_file = tmp_path / "test.mtx"
|
| 794 |
+
with open(test_file, "w") as f:
|
| 795 |
+
f.writelines(lines)
|
| 796 |
+
mmread(test_file, spmatrix=False)
|
| 797 |
+
|
| 798 |
+
def test_mtx_append(tmp_path):
|
| 799 |
+
a = mmread(io.StringIO("%%MatrixMarket matrix coordinate complex symmetric\n"
|
| 800 |
+
" 1 1 1\n"
|
| 801 |
+
"1 1 -2.1846000000000e+02 0.0000000000000e+00"),
|
| 802 |
+
spmatrix=False)
|
| 803 |
+
test_writefile = tmp_path / "test_mtx"
|
| 804 |
+
test_readfile = tmp_path / "test_mtx.mtx"
|
| 805 |
+
mmwrite(test_writefile, a)
|
| 806 |
+
mmread(test_readfile, spmatrix=False)
|
| 807 |
+
|
| 808 |
+
|
| 809 |
+
def test_threadpoolctl():
|
| 810 |
+
try:
|
| 811 |
+
import threadpoolctl
|
| 812 |
+
if not hasattr(threadpoolctl, "register"):
|
| 813 |
+
pytest.skip("threadpoolctl too old")
|
| 814 |
+
return
|
| 815 |
+
except ImportError:
|
| 816 |
+
pytest.skip("no threadpoolctl")
|
| 817 |
+
return
|
| 818 |
+
|
| 819 |
+
with threadpoolctl.threadpool_limits(limits=4):
|
| 820 |
+
assert_equal(fmm.PARALLELISM, 4)
|
| 821 |
+
|
| 822 |
+
with threadpoolctl.threadpool_limits(limits=2, user_api='scipy'):
|
| 823 |
+
assert_equal(fmm.PARALLELISM, 2)
|
| 824 |
+
|
| 825 |
+
|
| 826 |
+
def test_gh21999_file_not_exist():
|
| 827 |
+
tmpdir = mkdtemp(suffix=str(threading.get_native_id()))
|
| 828 |
+
wrong_fn = os.path.join(tmpdir, 'not_exist_test_file.mtx')
|
| 829 |
+
assert_raises(FileNotFoundError, mmread, wrong_fn)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_netcdf.py
ADDED
|
@@ -0,0 +1,553 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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| 5 |
+
import tempfile
|
| 6 |
+
import warnings
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
from glob import glob
|
| 9 |
+
from contextlib import contextmanager
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
from numpy.testing import (assert_, assert_allclose, assert_equal,
|
| 13 |
+
break_cycles, IS_PYPY)
|
| 14 |
+
import pytest
|
| 15 |
+
from pytest import raises as assert_raises
|
| 16 |
+
|
| 17 |
+
from scipy.io import netcdf_file
|
| 18 |
+
from scipy._lib._tmpdirs import in_tempdir
|
| 19 |
+
|
| 20 |
+
TEST_DATA_PATH = pjoin(dirname(__file__), 'data')
|
| 21 |
+
|
| 22 |
+
N_EG_ELS = 11 # number of elements for example variable
|
| 23 |
+
VARTYPE_EG = 'b' # var type for example variable
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
pytestmark = pytest.mark.thread_unsafe
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@contextmanager
|
| 30 |
+
def make_simple(*args, **kwargs):
|
| 31 |
+
f = netcdf_file(*args, **kwargs)
|
| 32 |
+
f.history = 'Created for a test'
|
| 33 |
+
f.createDimension('time', N_EG_ELS)
|
| 34 |
+
time = f.createVariable('time', VARTYPE_EG, ('time',))
|
| 35 |
+
time[:] = np.arange(N_EG_ELS)
|
| 36 |
+
time.units = 'days since 2008-01-01'
|
| 37 |
+
f.flush()
|
| 38 |
+
yield f
|
| 39 |
+
f.close()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def check_simple(ncfileobj):
|
| 43 |
+
'''Example fileobj tests '''
|
| 44 |
+
assert_equal(ncfileobj.history, b'Created for a test')
|
| 45 |
+
time = ncfileobj.variables['time']
|
| 46 |
+
assert_equal(time.units, b'days since 2008-01-01')
|
| 47 |
+
assert_equal(time.shape, (N_EG_ELS,))
|
| 48 |
+
assert_equal(time[-1], N_EG_ELS-1)
|
| 49 |
+
|
| 50 |
+
def assert_mask_matches(arr, expected_mask):
|
| 51 |
+
'''
|
| 52 |
+
Asserts that the mask of arr is effectively the same as expected_mask.
|
| 53 |
+
|
| 54 |
+
In contrast to numpy.ma.testutils.assert_mask_equal, this function allows
|
| 55 |
+
testing the 'mask' of a standard numpy array (the mask in this case is treated
|
| 56 |
+
as all False).
|
| 57 |
+
|
| 58 |
+
Parameters
|
| 59 |
+
----------
|
| 60 |
+
arr : ndarray or MaskedArray
|
| 61 |
+
Array to test.
|
| 62 |
+
expected_mask : array_like of booleans
|
| 63 |
+
A list giving the expected mask.
|
| 64 |
+
'''
|
| 65 |
+
|
| 66 |
+
mask = np.ma.getmaskarray(arr)
|
| 67 |
+
assert_equal(mask, expected_mask)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def test_read_write_files():
|
| 71 |
+
# test round trip for example file
|
| 72 |
+
cwd = os.getcwd()
|
| 73 |
+
try:
|
| 74 |
+
tmpdir = tempfile.mkdtemp()
|
| 75 |
+
os.chdir(tmpdir)
|
| 76 |
+
with make_simple('simple.nc', 'w') as f:
|
| 77 |
+
pass
|
| 78 |
+
# read the file we just created in 'a' mode
|
| 79 |
+
with netcdf_file('simple.nc', 'a') as f:
|
| 80 |
+
check_simple(f)
|
| 81 |
+
# add something
|
| 82 |
+
f._attributes['appendRan'] = 1
|
| 83 |
+
|
| 84 |
+
# To read the NetCDF file we just created::
|
| 85 |
+
with netcdf_file('simple.nc') as f:
|
| 86 |
+
# Using mmap is the default (but not on pypy)
|
| 87 |
+
assert_equal(f.use_mmap, not IS_PYPY)
|
| 88 |
+
check_simple(f)
|
| 89 |
+
assert_equal(f._attributes['appendRan'], 1)
|
| 90 |
+
|
| 91 |
+
# Read it in append (and check mmap is off)
|
| 92 |
+
with netcdf_file('simple.nc', 'a') as f:
|
| 93 |
+
assert_(not f.use_mmap)
|
| 94 |
+
check_simple(f)
|
| 95 |
+
assert_equal(f._attributes['appendRan'], 1)
|
| 96 |
+
|
| 97 |
+
# Now without mmap
|
| 98 |
+
with netcdf_file('simple.nc', mmap=False) as f:
|
| 99 |
+
# Using mmap is the default
|
| 100 |
+
assert_(not f.use_mmap)
|
| 101 |
+
check_simple(f)
|
| 102 |
+
|
| 103 |
+
# To read the NetCDF file we just created, as file object, no
|
| 104 |
+
# mmap. When n * n_bytes(var_type) is not divisible by 4, this
|
| 105 |
+
# raised an error in pupynere 1.0.12 and scipy rev 5893, because
|
| 106 |
+
# calculated vsize was rounding up in units of 4 - see
|
| 107 |
+
# https://www.unidata.ucar.edu/software/netcdf/guide_toc.html
|
| 108 |
+
with open('simple.nc', 'rb') as fobj:
|
| 109 |
+
with netcdf_file(fobj) as f:
|
| 110 |
+
# by default, don't use mmap for file-like
|
| 111 |
+
assert_(not f.use_mmap)
|
| 112 |
+
check_simple(f)
|
| 113 |
+
|
| 114 |
+
# Read file from fileobj, with mmap
|
| 115 |
+
with warnings.catch_warnings():
|
| 116 |
+
if IS_PYPY:
|
| 117 |
+
warnings.filterwarnings(
|
| 118 |
+
"ignore",
|
| 119 |
+
"Cannot close a netcdf_file opened with mmap=True.*",
|
| 120 |
+
RuntimeWarning
|
| 121 |
+
)
|
| 122 |
+
with open('simple.nc', 'rb') as fobj:
|
| 123 |
+
with netcdf_file(fobj, mmap=True) as f:
|
| 124 |
+
assert_(f.use_mmap)
|
| 125 |
+
check_simple(f)
|
| 126 |
+
|
| 127 |
+
# Again read it in append mode (adding another att)
|
| 128 |
+
with open('simple.nc', 'r+b') as fobj:
|
| 129 |
+
with netcdf_file(fobj, 'a') as f:
|
| 130 |
+
assert_(not f.use_mmap)
|
| 131 |
+
check_simple(f)
|
| 132 |
+
f.createDimension('app_dim', 1)
|
| 133 |
+
var = f.createVariable('app_var', 'i', ('app_dim',))
|
| 134 |
+
var[:] = 42
|
| 135 |
+
|
| 136 |
+
# And... check that app_var made it in...
|
| 137 |
+
with netcdf_file('simple.nc') as f:
|
| 138 |
+
check_simple(f)
|
| 139 |
+
assert_equal(f.variables['app_var'][:], 42)
|
| 140 |
+
|
| 141 |
+
finally:
|
| 142 |
+
if IS_PYPY:
|
| 143 |
+
# windows cannot remove a dead file held by a mmap
|
| 144 |
+
# that has not been collected in PyPy
|
| 145 |
+
break_cycles()
|
| 146 |
+
break_cycles()
|
| 147 |
+
os.chdir(cwd)
|
| 148 |
+
shutil.rmtree(tmpdir)
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def test_read_write_sio():
|
| 152 |
+
eg_sio1 = BytesIO()
|
| 153 |
+
with make_simple(eg_sio1, 'w'):
|
| 154 |
+
str_val = eg_sio1.getvalue()
|
| 155 |
+
|
| 156 |
+
eg_sio2 = BytesIO(str_val)
|
| 157 |
+
with netcdf_file(eg_sio2) as f2:
|
| 158 |
+
check_simple(f2)
|
| 159 |
+
|
| 160 |
+
# Test that error is raised if attempting mmap for sio
|
| 161 |
+
eg_sio3 = BytesIO(str_val)
|
| 162 |
+
assert_raises(ValueError, netcdf_file, eg_sio3, 'r', True)
|
| 163 |
+
# Test 64-bit offset write / read
|
| 164 |
+
eg_sio_64 = BytesIO()
|
| 165 |
+
with make_simple(eg_sio_64, 'w', version=2) as f_64:
|
| 166 |
+
str_val = eg_sio_64.getvalue()
|
| 167 |
+
|
| 168 |
+
eg_sio_64 = BytesIO(str_val)
|
| 169 |
+
with netcdf_file(eg_sio_64) as f_64:
|
| 170 |
+
check_simple(f_64)
|
| 171 |
+
assert_equal(f_64.version_byte, 2)
|
| 172 |
+
# also when version 2 explicitly specified
|
| 173 |
+
eg_sio_64 = BytesIO(str_val)
|
| 174 |
+
with netcdf_file(eg_sio_64, version=2) as f_64:
|
| 175 |
+
check_simple(f_64)
|
| 176 |
+
assert_equal(f_64.version_byte, 2)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def test_bytes():
|
| 180 |
+
raw_file = BytesIO()
|
| 181 |
+
f = netcdf_file(raw_file, mode='w')
|
| 182 |
+
# Dataset only has a single variable, dimension and attribute to avoid
|
| 183 |
+
# any ambiguity related to order.
|
| 184 |
+
f.a = 'b'
|
| 185 |
+
f.createDimension('dim', 1)
|
| 186 |
+
var = f.createVariable('var', np.int16, ('dim',))
|
| 187 |
+
var[0] = -9999
|
| 188 |
+
var.c = 'd'
|
| 189 |
+
f.sync()
|
| 190 |
+
|
| 191 |
+
actual = raw_file.getvalue()
|
| 192 |
+
|
| 193 |
+
expected = (b'CDF\x01'
|
| 194 |
+
b'\x00\x00\x00\x00'
|
| 195 |
+
b'\x00\x00\x00\x0a'
|
| 196 |
+
b'\x00\x00\x00\x01'
|
| 197 |
+
b'\x00\x00\x00\x03'
|
| 198 |
+
b'dim\x00'
|
| 199 |
+
b'\x00\x00\x00\x01'
|
| 200 |
+
b'\x00\x00\x00\x0c'
|
| 201 |
+
b'\x00\x00\x00\x01'
|
| 202 |
+
b'\x00\x00\x00\x01'
|
| 203 |
+
b'a\x00\x00\x00'
|
| 204 |
+
b'\x00\x00\x00\x02'
|
| 205 |
+
b'\x00\x00\x00\x01'
|
| 206 |
+
b'b\x00\x00\x00'
|
| 207 |
+
b'\x00\x00\x00\x0b'
|
| 208 |
+
b'\x00\x00\x00\x01'
|
| 209 |
+
b'\x00\x00\x00\x03'
|
| 210 |
+
b'var\x00'
|
| 211 |
+
b'\x00\x00\x00\x01'
|
| 212 |
+
b'\x00\x00\x00\x00'
|
| 213 |
+
b'\x00\x00\x00\x0c'
|
| 214 |
+
b'\x00\x00\x00\x01'
|
| 215 |
+
b'\x00\x00\x00\x01'
|
| 216 |
+
b'c\x00\x00\x00'
|
| 217 |
+
b'\x00\x00\x00\x02'
|
| 218 |
+
b'\x00\x00\x00\x01'
|
| 219 |
+
b'd\x00\x00\x00'
|
| 220 |
+
b'\x00\x00\x00\x03'
|
| 221 |
+
b'\x00\x00\x00\x04'
|
| 222 |
+
b'\x00\x00\x00\x78'
|
| 223 |
+
b'\xd8\xf1\x80\x01')
|
| 224 |
+
|
| 225 |
+
assert_equal(actual, expected)
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
def test_encoded_fill_value():
|
| 229 |
+
with netcdf_file(BytesIO(), mode='w') as f:
|
| 230 |
+
f.createDimension('x', 1)
|
| 231 |
+
var = f.createVariable('var', 'S1', ('x',))
|
| 232 |
+
assert_equal(var._get_encoded_fill_value(), b'\x00')
|
| 233 |
+
var._FillValue = b'\x01'
|
| 234 |
+
assert_equal(var._get_encoded_fill_value(), b'\x01')
|
| 235 |
+
var._FillValue = b'\x00\x00' # invalid, wrong size
|
| 236 |
+
assert_equal(var._get_encoded_fill_value(), b'\x00')
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def test_read_example_data():
|
| 240 |
+
# read any example data files
|
| 241 |
+
for fname in glob(pjoin(TEST_DATA_PATH, '*.nc')):
|
| 242 |
+
with netcdf_file(fname, 'r'):
|
| 243 |
+
pass
|
| 244 |
+
with netcdf_file(fname, 'r', mmap=False):
|
| 245 |
+
pass
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def test_itemset_no_segfault_on_readonly():
|
| 249 |
+
# Regression test for ticket #1202.
|
| 250 |
+
# Open the test file in read-only mode.
|
| 251 |
+
|
| 252 |
+
filename = pjoin(TEST_DATA_PATH, 'example_1.nc')
|
| 253 |
+
with warnings.catch_warnings():
|
| 254 |
+
message = ("Cannot close a netcdf_file opened with mmap=True, when "
|
| 255 |
+
"netcdf_variables or arrays referring to its data still exist")
|
| 256 |
+
warnings.filterwarnings("ignore", message, RuntimeWarning)
|
| 257 |
+
with netcdf_file(filename, 'r', mmap=True) as f:
|
| 258 |
+
time_var = f.variables['time']
|
| 259 |
+
|
| 260 |
+
# time_var.assignValue(42) should raise a RuntimeError--not seg. fault!
|
| 261 |
+
assert_raises(RuntimeError, time_var.assignValue, 42)
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
def test_appending_issue_gh_8625():
|
| 265 |
+
stream = BytesIO()
|
| 266 |
+
|
| 267 |
+
with make_simple(stream, mode='w') as f:
|
| 268 |
+
f.createDimension('x', 2)
|
| 269 |
+
f.createVariable('x', float, ('x',))
|
| 270 |
+
f.variables['x'][...] = 1
|
| 271 |
+
f.flush()
|
| 272 |
+
contents = stream.getvalue()
|
| 273 |
+
|
| 274 |
+
stream = BytesIO(contents)
|
| 275 |
+
with netcdf_file(stream, mode='a') as f:
|
| 276 |
+
f.variables['x'][...] = 2
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def test_write_invalid_dtype():
|
| 280 |
+
dtypes = ['int64', 'uint64']
|
| 281 |
+
if np.dtype('int').itemsize == 8: # 64-bit machines
|
| 282 |
+
dtypes.append('int')
|
| 283 |
+
if np.dtype('uint').itemsize == 8: # 64-bit machines
|
| 284 |
+
dtypes.append('uint')
|
| 285 |
+
|
| 286 |
+
with netcdf_file(BytesIO(), 'w') as f:
|
| 287 |
+
f.createDimension('time', N_EG_ELS)
|
| 288 |
+
for dt in dtypes:
|
| 289 |
+
assert_raises(ValueError, f.createVariable, 'time', dt, ('time',))
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def test_flush_rewind():
|
| 293 |
+
stream = BytesIO()
|
| 294 |
+
with make_simple(stream, mode='w') as f:
|
| 295 |
+
f.createDimension('x',4) # x is used in createVariable
|
| 296 |
+
v = f.createVariable('v', 'i2', ['x'])
|
| 297 |
+
v[:] = 1
|
| 298 |
+
f.flush()
|
| 299 |
+
len_single = len(stream.getvalue())
|
| 300 |
+
f.flush()
|
| 301 |
+
len_double = len(stream.getvalue())
|
| 302 |
+
|
| 303 |
+
assert_(len_single == len_double)
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
def test_dtype_specifiers():
|
| 307 |
+
# Numpy 1.7.0-dev had a bug where 'i2' wouldn't work.
|
| 308 |
+
# Specifying np.int16 or similar only works from the same commit as this
|
| 309 |
+
# comment was made.
|
| 310 |
+
with make_simple(BytesIO(), mode='w') as f:
|
| 311 |
+
f.createDimension('x',4)
|
| 312 |
+
f.createVariable('v1', 'i2', ['x'])
|
| 313 |
+
f.createVariable('v2', np.int16, ['x'])
|
| 314 |
+
f.createVariable('v3', np.dtype(np.int16), ['x'])
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
def test_ticket_1720():
|
| 318 |
+
io = BytesIO()
|
| 319 |
+
|
| 320 |
+
items = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
|
| 321 |
+
|
| 322 |
+
with netcdf_file(io, 'w') as f:
|
| 323 |
+
f.history = 'Created for a test'
|
| 324 |
+
f.createDimension('float_var', 10)
|
| 325 |
+
float_var = f.createVariable('float_var', 'f', ('float_var',))
|
| 326 |
+
float_var[:] = items
|
| 327 |
+
float_var.units = 'metres'
|
| 328 |
+
f.flush()
|
| 329 |
+
contents = io.getvalue()
|
| 330 |
+
|
| 331 |
+
io = BytesIO(contents)
|
| 332 |
+
with netcdf_file(io, 'r') as f:
|
| 333 |
+
assert_equal(f.history, b'Created for a test')
|
| 334 |
+
float_var = f.variables['float_var']
|
| 335 |
+
assert_equal(float_var.units, b'metres')
|
| 336 |
+
assert_equal(float_var.shape, (10,))
|
| 337 |
+
assert_allclose(float_var[:], items)
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
def test_mmaps_segfault():
|
| 341 |
+
filename = pjoin(TEST_DATA_PATH, 'example_1.nc')
|
| 342 |
+
|
| 343 |
+
if not IS_PYPY:
|
| 344 |
+
with warnings.catch_warnings():
|
| 345 |
+
warnings.simplefilter("error")
|
| 346 |
+
with netcdf_file(filename, mmap=True) as f:
|
| 347 |
+
x = f.variables['lat'][:]
|
| 348 |
+
# should not raise warnings
|
| 349 |
+
del x
|
| 350 |
+
|
| 351 |
+
def doit():
|
| 352 |
+
with netcdf_file(filename, mmap=True) as f:
|
| 353 |
+
return f.variables['lat'][:]
|
| 354 |
+
|
| 355 |
+
# should not crash
|
| 356 |
+
with warnings.catch_warnings():
|
| 357 |
+
message = ("Cannot close a netcdf_file opened with mmap=True, when "
|
| 358 |
+
"netcdf_variables or arrays referring to its data still exist")
|
| 359 |
+
warnings.filterwarnings("ignore", message, RuntimeWarning)
|
| 360 |
+
x = doit()
|
| 361 |
+
x.sum()
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def test_zero_dimensional_var():
|
| 365 |
+
io = BytesIO()
|
| 366 |
+
with make_simple(io, 'w') as f:
|
| 367 |
+
v = f.createVariable('zerodim', 'i2', [])
|
| 368 |
+
# This is checking that .isrec returns a boolean - don't simplify it
|
| 369 |
+
# to 'assert not ...'
|
| 370 |
+
assert v.isrec is False, v.isrec
|
| 371 |
+
f.flush()
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
def test_byte_gatts():
|
| 375 |
+
# Check that global "string" atts work like they did before py3k
|
| 376 |
+
# unicode and general bytes confusion
|
| 377 |
+
with in_tempdir():
|
| 378 |
+
filename = 'g_byte_atts.nc'
|
| 379 |
+
f = netcdf_file(filename, 'w')
|
| 380 |
+
f._attributes['holy'] = b'grail'
|
| 381 |
+
f._attributes['witch'] = 'floats'
|
| 382 |
+
f.close()
|
| 383 |
+
f = netcdf_file(filename, 'r')
|
| 384 |
+
assert_equal(f._attributes['holy'], b'grail')
|
| 385 |
+
assert_equal(f._attributes['witch'], b'floats')
|
| 386 |
+
f.close()
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
def test_open_append():
|
| 390 |
+
# open 'w' put one attr
|
| 391 |
+
with in_tempdir():
|
| 392 |
+
filename = 'append_dat.nc'
|
| 393 |
+
f = netcdf_file(filename, 'w')
|
| 394 |
+
f._attributes['Kilroy'] = 'was here'
|
| 395 |
+
f.close()
|
| 396 |
+
|
| 397 |
+
# open again in 'a', read the att and a new one
|
| 398 |
+
f = netcdf_file(filename, 'a')
|
| 399 |
+
assert_equal(f._attributes['Kilroy'], b'was here')
|
| 400 |
+
f._attributes['naughty'] = b'Zoot'
|
| 401 |
+
f.close()
|
| 402 |
+
|
| 403 |
+
# open yet again in 'r' and check both atts
|
| 404 |
+
f = netcdf_file(filename, 'r')
|
| 405 |
+
assert_equal(f._attributes['Kilroy'], b'was here')
|
| 406 |
+
assert_equal(f._attributes['naughty'], b'Zoot')
|
| 407 |
+
f.close()
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
def test_append_recordDimension():
|
| 411 |
+
dataSize = 100
|
| 412 |
+
|
| 413 |
+
with in_tempdir():
|
| 414 |
+
# Create file with record time dimension
|
| 415 |
+
with netcdf_file('withRecordDimension.nc', 'w') as f:
|
| 416 |
+
f.createDimension('time', None)
|
| 417 |
+
f.createVariable('time', 'd', ('time',))
|
| 418 |
+
f.createDimension('x', dataSize)
|
| 419 |
+
x = f.createVariable('x', 'd', ('x',))
|
| 420 |
+
x[:] = np.array(range(dataSize))
|
| 421 |
+
f.createDimension('y', dataSize)
|
| 422 |
+
y = f.createVariable('y', 'd', ('y',))
|
| 423 |
+
y[:] = np.array(range(dataSize))
|
| 424 |
+
f.createVariable('testData', 'i', ('time', 'x', 'y'))
|
| 425 |
+
f.flush()
|
| 426 |
+
f.close()
|
| 427 |
+
|
| 428 |
+
for i in range(2):
|
| 429 |
+
# Open the file in append mode and add data
|
| 430 |
+
with netcdf_file('withRecordDimension.nc', 'a') as f:
|
| 431 |
+
f.variables['time'].data = np.append(f.variables["time"].data, i)
|
| 432 |
+
f.variables['testData'][i, :, :] = np.full((dataSize, dataSize), i)
|
| 433 |
+
f.flush()
|
| 434 |
+
|
| 435 |
+
# Read the file and check that append worked
|
| 436 |
+
with netcdf_file('withRecordDimension.nc') as f:
|
| 437 |
+
assert_equal(f.variables['time'][-1], i)
|
| 438 |
+
assert_equal(f.variables['testData'][-1, :, :].copy(),
|
| 439 |
+
np.full((dataSize, dataSize), i))
|
| 440 |
+
assert_equal(f.variables['time'].data.shape[0], i+1)
|
| 441 |
+
assert_equal(f.variables['testData'].data.shape[0], i+1)
|
| 442 |
+
|
| 443 |
+
# Read the file and check that 'data' was not saved as user defined
|
| 444 |
+
# attribute of testData variable during append operation
|
| 445 |
+
with netcdf_file('withRecordDimension.nc') as f:
|
| 446 |
+
with assert_raises(KeyError) as ar:
|
| 447 |
+
f.variables['testData']._attributes['data']
|
| 448 |
+
ex = ar.value
|
| 449 |
+
assert_equal(ex.args[0], 'data')
|
| 450 |
+
|
| 451 |
+
def test_maskandscale():
|
| 452 |
+
t = np.linspace(20, 30, 15)
|
| 453 |
+
t[3] = 100
|
| 454 |
+
tm = np.ma.masked_greater(t, 99)
|
| 455 |
+
fname = pjoin(TEST_DATA_PATH, 'example_2.nc')
|
| 456 |
+
with netcdf_file(fname, maskandscale=True) as f:
|
| 457 |
+
Temp = f.variables['Temperature']
|
| 458 |
+
assert_equal(Temp.missing_value, 9999)
|
| 459 |
+
assert_equal(Temp.add_offset, 20)
|
| 460 |
+
assert_equal(Temp.scale_factor, np.float32(0.01))
|
| 461 |
+
found = Temp[:].compressed()
|
| 462 |
+
del Temp # Remove ref to mmap, so file can be closed.
|
| 463 |
+
expected = np.round(tm.compressed(), 2)
|
| 464 |
+
assert_allclose(found, expected)
|
| 465 |
+
|
| 466 |
+
with in_tempdir():
|
| 467 |
+
newfname = 'ms.nc'
|
| 468 |
+
f = netcdf_file(newfname, 'w', maskandscale=True)
|
| 469 |
+
f.createDimension('Temperature', len(tm))
|
| 470 |
+
temp = f.createVariable('Temperature', 'i', ('Temperature',))
|
| 471 |
+
temp.missing_value = 9999
|
| 472 |
+
temp.scale_factor = 0.01
|
| 473 |
+
temp.add_offset = 20
|
| 474 |
+
temp[:] = tm
|
| 475 |
+
f.close()
|
| 476 |
+
|
| 477 |
+
with netcdf_file(newfname, maskandscale=True) as f:
|
| 478 |
+
Temp = f.variables['Temperature']
|
| 479 |
+
assert_equal(Temp.missing_value, 9999)
|
| 480 |
+
assert_equal(Temp.add_offset, 20)
|
| 481 |
+
assert_equal(Temp.scale_factor, np.float32(0.01))
|
| 482 |
+
expected = np.round(tm.compressed(), 2)
|
| 483 |
+
found = Temp[:].compressed()
|
| 484 |
+
del Temp
|
| 485 |
+
assert_allclose(found, expected)
|
| 486 |
+
|
| 487 |
+
|
| 488 |
+
# ------------------------------------------------------------------------
|
| 489 |
+
# Test reading with masked values (_FillValue / missing_value)
|
| 490 |
+
# ------------------------------------------------------------------------
|
| 491 |
+
|
| 492 |
+
def test_read_withValuesNearFillValue():
|
| 493 |
+
# Regression test for ticket #5626
|
| 494 |
+
fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc')
|
| 495 |
+
with netcdf_file(fname, maskandscale=True) as f:
|
| 496 |
+
vardata = f.variables['var1_fillval0'][:]
|
| 497 |
+
assert_mask_matches(vardata, [False, True, False])
|
| 498 |
+
|
| 499 |
+
def test_read_withNoFillValue():
|
| 500 |
+
# For a variable with no fill value, reading data with maskandscale=True
|
| 501 |
+
# should return unmasked data
|
| 502 |
+
fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc')
|
| 503 |
+
with netcdf_file(fname, maskandscale=True) as f:
|
| 504 |
+
vardata = f.variables['var2_noFillval'][:]
|
| 505 |
+
assert_mask_matches(vardata, [False, False, False])
|
| 506 |
+
assert_equal(vardata, [1,2,3])
|
| 507 |
+
|
| 508 |
+
def test_read_withFillValueAndMissingValue():
|
| 509 |
+
# For a variable with both _FillValue and missing_value, the _FillValue
|
| 510 |
+
# should be used
|
| 511 |
+
IRRELEVANT_VALUE = 9999
|
| 512 |
+
fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc')
|
| 513 |
+
with netcdf_file(fname, maskandscale=True) as f:
|
| 514 |
+
vardata = f.variables['var3_fillvalAndMissingValue'][:]
|
| 515 |
+
assert_mask_matches(vardata, [True, False, False])
|
| 516 |
+
assert_equal(vardata, [IRRELEVANT_VALUE, 2, 3])
|
| 517 |
+
|
| 518 |
+
def test_read_withMissingValue():
|
| 519 |
+
# For a variable with missing_value but not _FillValue, the missing_value
|
| 520 |
+
# should be used
|
| 521 |
+
fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc')
|
| 522 |
+
with netcdf_file(fname, maskandscale=True) as f:
|
| 523 |
+
vardata = f.variables['var4_missingValue'][:]
|
| 524 |
+
assert_mask_matches(vardata, [False, True, False])
|
| 525 |
+
|
| 526 |
+
def test_read_withFillValNaN():
|
| 527 |
+
fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc')
|
| 528 |
+
with netcdf_file(fname, maskandscale=True) as f:
|
| 529 |
+
vardata = f.variables['var5_fillvalNaN'][:]
|
| 530 |
+
assert_mask_matches(vardata, [False, True, False])
|
| 531 |
+
|
| 532 |
+
def test_read_withChar():
|
| 533 |
+
fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc')
|
| 534 |
+
with netcdf_file(fname, maskandscale=True) as f:
|
| 535 |
+
vardata = f.variables['var6_char'][:]
|
| 536 |
+
assert_mask_matches(vardata, [False, True, False])
|
| 537 |
+
|
| 538 |
+
def test_read_with2dVar():
|
| 539 |
+
fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc')
|
| 540 |
+
with netcdf_file(fname, maskandscale=True) as f:
|
| 541 |
+
vardata = f.variables['var7_2d'][:]
|
| 542 |
+
assert_mask_matches(vardata, [[True, False], [False, False], [False, True]])
|
| 543 |
+
|
| 544 |
+
def test_read_withMaskAndScaleFalse():
|
| 545 |
+
# If a variable has a _FillValue (or missing_value) attribute, but is read
|
| 546 |
+
# with maskandscale set to False, the result should be unmasked
|
| 547 |
+
fname = pjoin(TEST_DATA_PATH, 'example_3_maskedvals.nc')
|
| 548 |
+
# Open file with mmap=False to avoid problems with closing a mmap'ed file
|
| 549 |
+
# when arrays referring to its data still exist:
|
| 550 |
+
with netcdf_file(fname, maskandscale=False, mmap=False) as f:
|
| 551 |
+
vardata = f.variables['var3_fillvalAndMissingValue'][:]
|
| 552 |
+
assert_mask_matches(vardata, [False, False, False])
|
| 553 |
+
assert_equal(vardata, [1, 2, 3])
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_paths.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Ensure that we can use pathlib.Path objects in all relevant IO functions.
|
| 3 |
+
"""
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
import scipy.io
|
| 9 |
+
import scipy.io.wavfile
|
| 10 |
+
from scipy._lib._tmpdirs import tempdir
|
| 11 |
+
import scipy.sparse
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestPaths:
|
| 15 |
+
data = np.arange(5).astype(np.int64)
|
| 16 |
+
|
| 17 |
+
def test_savemat(self):
|
| 18 |
+
with tempdir() as temp_dir:
|
| 19 |
+
path = Path(temp_dir) / 'data.mat'
|
| 20 |
+
scipy.io.savemat(path, {'data': self.data})
|
| 21 |
+
assert path.is_file()
|
| 22 |
+
|
| 23 |
+
def test_loadmat(self):
|
| 24 |
+
# Save data with string path, load with pathlib.Path
|
| 25 |
+
with tempdir() as temp_dir:
|
| 26 |
+
path = Path(temp_dir) / 'data.mat'
|
| 27 |
+
scipy.io.savemat(str(path), {'data': self.data})
|
| 28 |
+
|
| 29 |
+
mat_contents = scipy.io.loadmat(path)
|
| 30 |
+
assert (mat_contents['data'] == self.data).all()
|
| 31 |
+
|
| 32 |
+
def test_whosmat(self):
|
| 33 |
+
# Save data with string path, load with pathlib.Path
|
| 34 |
+
with tempdir() as temp_dir:
|
| 35 |
+
path = Path(temp_dir) / 'data.mat'
|
| 36 |
+
scipy.io.savemat(str(path), {'data': self.data})
|
| 37 |
+
|
| 38 |
+
contents = scipy.io.whosmat(path)
|
| 39 |
+
assert contents[0] == ('data', (1, 5), 'int64')
|
| 40 |
+
|
| 41 |
+
def test_readsav(self):
|
| 42 |
+
path = Path(__file__).parent / 'data/scalar_string.sav'
|
| 43 |
+
scipy.io.readsav(path)
|
| 44 |
+
|
| 45 |
+
def test_hb_read(self):
|
| 46 |
+
# Save data with string path, load with pathlib.Path
|
| 47 |
+
with tempdir() as temp_dir:
|
| 48 |
+
data = scipy.sparse.eye_array(3, format='csr')
|
| 49 |
+
path = Path(temp_dir) / 'data.hb'
|
| 50 |
+
scipy.io.hb_write(str(path), data)
|
| 51 |
+
|
| 52 |
+
data_new = scipy.io.hb_read(path, spmatrix=False)
|
| 53 |
+
assert (data_new != data).nnz == 0
|
| 54 |
+
|
| 55 |
+
def test_hb_write(self):
|
| 56 |
+
with tempdir() as temp_dir:
|
| 57 |
+
data = scipy.sparse.eye_array(3, format='csr')
|
| 58 |
+
path = Path(temp_dir) / 'data.hb'
|
| 59 |
+
scipy.io.hb_write(path, data)
|
| 60 |
+
assert path.is_file()
|
| 61 |
+
|
| 62 |
+
def test_mmio_read(self):
|
| 63 |
+
# Save data with string path, load with pathlib.Path
|
| 64 |
+
with tempdir() as temp_dir:
|
| 65 |
+
data = scipy.sparse.eye_array(3, format='csr')
|
| 66 |
+
path = Path(temp_dir) / 'data.mtx'
|
| 67 |
+
scipy.io.mmwrite(str(path), data)
|
| 68 |
+
|
| 69 |
+
data_new = scipy.io.mmread(path, spmatrix=False)
|
| 70 |
+
assert (data_new != data).nnz == 0
|
| 71 |
+
|
| 72 |
+
def test_mmio_write(self):
|
| 73 |
+
with tempdir() as temp_dir:
|
| 74 |
+
data = scipy.sparse.eye_array(3, format='csr')
|
| 75 |
+
path = Path(temp_dir) / 'data.mtx'
|
| 76 |
+
scipy.io.mmwrite(path, data)
|
| 77 |
+
|
| 78 |
+
def test_netcdf_file(self):
|
| 79 |
+
path = Path(__file__).parent / 'data/example_1.nc'
|
| 80 |
+
scipy.io.netcdf_file(path)
|
| 81 |
+
|
| 82 |
+
def test_wavfile_read(self):
|
| 83 |
+
path = Path(__file__).parent / 'data/test-8000Hz-le-2ch-1byteu.wav'
|
| 84 |
+
scipy.io.wavfile.read(path)
|
| 85 |
+
|
| 86 |
+
def test_wavfile_write(self):
|
| 87 |
+
# Read from str path, write to Path
|
| 88 |
+
input_path = Path(__file__).parent / 'data/test-8000Hz-le-2ch-1byteu.wav'
|
| 89 |
+
rate, data = scipy.io.wavfile.read(str(input_path))
|
| 90 |
+
|
| 91 |
+
with tempdir() as temp_dir:
|
| 92 |
+
output_path = Path(temp_dir) / input_path.name
|
| 93 |
+
scipy.io.wavfile.write(output_path, rate, data)
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/tests/test_wavfile.py
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
from io import (BytesIO, UnsupportedOperation)
|
| 4 |
+
import threading
|
| 5 |
+
import warnings
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
from numpy.testing import (assert_equal, assert_, assert_array_equal,
|
| 9 |
+
break_cycles, IS_PYPY)
|
| 10 |
+
import pytest
|
| 11 |
+
from pytest import raises, warns
|
| 12 |
+
|
| 13 |
+
from scipy.io import wavfile
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def datafile(fn):
|
| 17 |
+
return os.path.join(os.path.dirname(__file__), 'data', fn)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_read_1():
|
| 21 |
+
# 32-bit PCM (which uses extensible format)
|
| 22 |
+
for mmap in [False, True]:
|
| 23 |
+
filename = 'test-44100Hz-le-1ch-4bytes.wav'
|
| 24 |
+
rate, data = wavfile.read(datafile(filename), mmap=mmap)
|
| 25 |
+
|
| 26 |
+
assert_equal(rate, 44100)
|
| 27 |
+
assert_(np.issubdtype(data.dtype, np.int32))
|
| 28 |
+
assert_equal(data.shape, (4410,))
|
| 29 |
+
|
| 30 |
+
del data
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def test_read_2():
|
| 34 |
+
# 8-bit unsigned PCM
|
| 35 |
+
for mmap in [False, True]:
|
| 36 |
+
filename = 'test-8000Hz-le-2ch-1byteu.wav'
|
| 37 |
+
rate, data = wavfile.read(datafile(filename), mmap=mmap)
|
| 38 |
+
|
| 39 |
+
assert_equal(rate, 8000)
|
| 40 |
+
assert_(np.issubdtype(data.dtype, np.uint8))
|
| 41 |
+
assert_equal(data.shape, (800, 2))
|
| 42 |
+
|
| 43 |
+
del data
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_read_3():
|
| 47 |
+
# Little-endian float
|
| 48 |
+
for mmap in [False, True]:
|
| 49 |
+
filename = 'test-44100Hz-2ch-32bit-float-le.wav'
|
| 50 |
+
rate, data = wavfile.read(datafile(filename), mmap=mmap)
|
| 51 |
+
|
| 52 |
+
assert_equal(rate, 44100)
|
| 53 |
+
assert_(np.issubdtype(data.dtype, np.float32))
|
| 54 |
+
assert_equal(data.shape, (441, 2))
|
| 55 |
+
|
| 56 |
+
del data
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_read_4():
|
| 60 |
+
# Contains unsupported 'PEAK' chunk
|
| 61 |
+
for mmap in [False, True]:
|
| 62 |
+
with warnings.catch_warnings():
|
| 63 |
+
warnings.filterwarnings(
|
| 64 |
+
"ignore",
|
| 65 |
+
"Chunk .non-data. not understood, skipping it",
|
| 66 |
+
wavfile.WavFileWarning
|
| 67 |
+
)
|
| 68 |
+
filename = 'test-48000Hz-2ch-64bit-float-le-wavex.wav'
|
| 69 |
+
rate, data = wavfile.read(datafile(filename), mmap=mmap)
|
| 70 |
+
|
| 71 |
+
assert_equal(rate, 48000)
|
| 72 |
+
assert_(np.issubdtype(data.dtype, np.float64))
|
| 73 |
+
assert_equal(data.shape, (480, 2))
|
| 74 |
+
|
| 75 |
+
del data
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def test_read_5():
|
| 79 |
+
# Big-endian float
|
| 80 |
+
for mmap in [False, True]:
|
| 81 |
+
filename = 'test-44100Hz-2ch-32bit-float-be.wav'
|
| 82 |
+
rate, data = wavfile.read(datafile(filename), mmap=mmap)
|
| 83 |
+
|
| 84 |
+
assert_equal(rate, 44100)
|
| 85 |
+
assert_(np.issubdtype(data.dtype, np.float32))
|
| 86 |
+
assert_(data.dtype.byteorder == '>' or (sys.byteorder == 'big' and
|
| 87 |
+
data.dtype.byteorder == '='))
|
| 88 |
+
assert_equal(data.shape, (441, 2))
|
| 89 |
+
|
| 90 |
+
del data
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def test_5_bit_odd_size_no_pad():
|
| 94 |
+
# 5-bit, 1 B container, 5 channels, 9 samples, 45 B data chunk
|
| 95 |
+
# Generated by LTspice, which incorrectly omits pad byte, but should be
|
| 96 |
+
# readable anyway
|
| 97 |
+
for mmap in [False, True]:
|
| 98 |
+
filename = 'test-8000Hz-le-5ch-9S-5bit.wav'
|
| 99 |
+
rate, data = wavfile.read(datafile(filename), mmap=mmap)
|
| 100 |
+
|
| 101 |
+
assert_equal(rate, 8000)
|
| 102 |
+
assert_(np.issubdtype(data.dtype, np.uint8))
|
| 103 |
+
assert_equal(data.shape, (9, 5))
|
| 104 |
+
|
| 105 |
+
# 8-5 = 3 LSBits should be 0
|
| 106 |
+
assert_equal(data & 0b00000111, 0)
|
| 107 |
+
|
| 108 |
+
# Unsigned
|
| 109 |
+
assert_equal(data.max(), 0b11111000) # Highest possible
|
| 110 |
+
assert_equal(data[0, 0], 128) # Midpoint is 128 for <= 8-bit
|
| 111 |
+
assert_equal(data.min(), 0) # Lowest possible
|
| 112 |
+
|
| 113 |
+
del data
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def test_12_bit_even_size():
|
| 117 |
+
# 12-bit, 2 B container, 4 channels, 9 samples, 72 B data chunk
|
| 118 |
+
# Generated by LTspice from 1 Vpk sine waves
|
| 119 |
+
for mmap in [False, True]:
|
| 120 |
+
filename = 'test-8000Hz-le-4ch-9S-12bit.wav'
|
| 121 |
+
rate, data = wavfile.read(datafile(filename), mmap=mmap)
|
| 122 |
+
|
| 123 |
+
assert_equal(rate, 8000)
|
| 124 |
+
assert_(np.issubdtype(data.dtype, np.int16))
|
| 125 |
+
assert_equal(data.shape, (9, 4))
|
| 126 |
+
|
| 127 |
+
# 16-12 = 4 LSBits should be 0
|
| 128 |
+
assert_equal(data & 0b00000000_00001111, 0)
|
| 129 |
+
|
| 130 |
+
# Signed
|
| 131 |
+
assert_equal(data.max(), 0b01111111_11110000) # Highest possible
|
| 132 |
+
assert_equal(data[0, 0], 0) # Midpoint is 0 for >= 9-bit
|
| 133 |
+
assert_equal(data.min(), -0b10000000_00000000) # Lowest possible
|
| 134 |
+
|
| 135 |
+
del data
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def test_24_bit_odd_size_with_pad():
|
| 139 |
+
# 24-bit, 3 B container, 3 channels, 5 samples, 45 B data chunk
|
| 140 |
+
# Should not raise any warnings about the data chunk pad byte
|
| 141 |
+
filename = 'test-8000Hz-le-3ch-5S-24bit.wav'
|
| 142 |
+
rate, data = wavfile.read(datafile(filename), mmap=False)
|
| 143 |
+
|
| 144 |
+
assert_equal(rate, 8000)
|
| 145 |
+
assert_(np.issubdtype(data.dtype, np.int32))
|
| 146 |
+
assert_equal(data.shape, (5, 3))
|
| 147 |
+
|
| 148 |
+
# All LSBytes should be 0
|
| 149 |
+
assert_equal(data & 0xff, 0)
|
| 150 |
+
|
| 151 |
+
# Hand-made max/min samples under different conventions:
|
| 152 |
+
# 2**(N-1) 2**(N-1)-1 LSB
|
| 153 |
+
assert_equal(data, [[-0x8000_0000, -0x7fff_ff00, -0x200],
|
| 154 |
+
[-0x4000_0000, -0x3fff_ff00, -0x100],
|
| 155 |
+
[+0x0000_0000, +0x0000_0000, +0x000],
|
| 156 |
+
[+0x4000_0000, +0x3fff_ff00, +0x100],
|
| 157 |
+
[+0x7fff_ff00, +0x7fff_ff00, +0x200]])
|
| 158 |
+
# ^ clipped
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def test_20_bit_extra_data():
|
| 162 |
+
# 20-bit, 3 B container, 1 channel, 10 samples, 30 B data chunk
|
| 163 |
+
# with extra data filling container beyond the bit depth
|
| 164 |
+
filename = 'test-1234Hz-le-1ch-10S-20bit-extra.wav'
|
| 165 |
+
rate, data = wavfile.read(datafile(filename), mmap=False)
|
| 166 |
+
|
| 167 |
+
assert_equal(rate, 1234)
|
| 168 |
+
assert_(np.issubdtype(data.dtype, np.int32))
|
| 169 |
+
assert_equal(data.shape, (10,))
|
| 170 |
+
|
| 171 |
+
# All LSBytes should still be 0, because 3 B container in 4 B dtype
|
| 172 |
+
assert_equal(data & 0xff, 0)
|
| 173 |
+
|
| 174 |
+
# But it should load the data beyond 20 bits
|
| 175 |
+
assert_((data & 0xf00).any())
|
| 176 |
+
|
| 177 |
+
# Full-scale positive/negative samples, then being halved each time
|
| 178 |
+
assert_equal(data, [+0x7ffff000, # +full-scale 20-bit
|
| 179 |
+
-0x7ffff000, # -full-scale 20-bit
|
| 180 |
+
+0x7ffff000 >> 1, # +1/2
|
| 181 |
+
-0x7ffff000 >> 1, # -1/2
|
| 182 |
+
+0x7ffff000 >> 2, # +1/4
|
| 183 |
+
-0x7ffff000 >> 2, # -1/4
|
| 184 |
+
+0x7ffff000 >> 3, # +1/8
|
| 185 |
+
-0x7ffff000 >> 3, # -1/8
|
| 186 |
+
+0x7ffff000 >> 4, # +1/16
|
| 187 |
+
-0x7ffff000 >> 4, # -1/16
|
| 188 |
+
])
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
def test_36_bit_odd_size():
|
| 192 |
+
# 36-bit, 5 B container, 3 channels, 5 samples, 75 B data chunk + pad
|
| 193 |
+
filename = 'test-8000Hz-le-3ch-5S-36bit.wav'
|
| 194 |
+
rate, data = wavfile.read(datafile(filename), mmap=False)
|
| 195 |
+
|
| 196 |
+
assert_equal(rate, 8000)
|
| 197 |
+
assert_(np.issubdtype(data.dtype, np.int64))
|
| 198 |
+
assert_equal(data.shape, (5, 3))
|
| 199 |
+
|
| 200 |
+
# 28 LSBits should be 0
|
| 201 |
+
assert_equal(data & 0xfffffff, 0)
|
| 202 |
+
|
| 203 |
+
# Hand-made max/min samples under different conventions:
|
| 204 |
+
# Fixed-point 2**(N-1) Full-scale 2**(N-1)-1 LSB
|
| 205 |
+
correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_f000_0000, -0x2000_0000],
|
| 206 |
+
[-0x4000_0000_0000_0000, -0x3fff_ffff_f000_0000, -0x1000_0000],
|
| 207 |
+
[+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x0000_0000],
|
| 208 |
+
[+0x4000_0000_0000_0000, +0x3fff_ffff_f000_0000, +0x1000_0000],
|
| 209 |
+
[+0x7fff_ffff_f000_0000, +0x7fff_ffff_f000_0000, +0x2000_0000]]
|
| 210 |
+
# ^ clipped
|
| 211 |
+
|
| 212 |
+
assert_equal(data, correct)
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def test_45_bit_even_size():
|
| 216 |
+
# 45-bit, 6 B container, 3 channels, 5 samples, 90 B data chunk
|
| 217 |
+
filename = 'test-8000Hz-le-3ch-5S-45bit.wav'
|
| 218 |
+
rate, data = wavfile.read(datafile(filename), mmap=False)
|
| 219 |
+
|
| 220 |
+
assert_equal(rate, 8000)
|
| 221 |
+
assert_(np.issubdtype(data.dtype, np.int64))
|
| 222 |
+
assert_equal(data.shape, (5, 3))
|
| 223 |
+
|
| 224 |
+
# 19 LSBits should be 0
|
| 225 |
+
assert_equal(data & 0x7ffff, 0)
|
| 226 |
+
|
| 227 |
+
# Hand-made max/min samples under different conventions:
|
| 228 |
+
# Fixed-point 2**(N-1) Full-scale 2**(N-1)-1 LSB
|
| 229 |
+
correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_fff8_0000, -0x10_0000],
|
| 230 |
+
[-0x4000_0000_0000_0000, -0x3fff_ffff_fff8_0000, -0x08_0000],
|
| 231 |
+
[+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x00_0000],
|
| 232 |
+
[+0x4000_0000_0000_0000, +0x3fff_ffff_fff8_0000, +0x08_0000],
|
| 233 |
+
[+0x7fff_ffff_fff8_0000, +0x7fff_ffff_fff8_0000, +0x10_0000]]
|
| 234 |
+
# ^ clipped
|
| 235 |
+
|
| 236 |
+
assert_equal(data, correct)
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def test_53_bit_odd_size():
|
| 240 |
+
# 53-bit, 7 B container, 3 channels, 5 samples, 105 B data chunk + pad
|
| 241 |
+
filename = 'test-8000Hz-le-3ch-5S-53bit.wav'
|
| 242 |
+
rate, data = wavfile.read(datafile(filename), mmap=False)
|
| 243 |
+
|
| 244 |
+
assert_equal(rate, 8000)
|
| 245 |
+
assert_(np.issubdtype(data.dtype, np.int64))
|
| 246 |
+
assert_equal(data.shape, (5, 3))
|
| 247 |
+
|
| 248 |
+
# 11 LSBits should be 0
|
| 249 |
+
assert_equal(data & 0x7ff, 0)
|
| 250 |
+
|
| 251 |
+
# Hand-made max/min samples under different conventions:
|
| 252 |
+
# Fixed-point 2**(N-1) Full-scale 2**(N-1)-1 LSB
|
| 253 |
+
correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_ffff_f800, -0x1000],
|
| 254 |
+
[-0x4000_0000_0000_0000, -0x3fff_ffff_ffff_f800, -0x0800],
|
| 255 |
+
[+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x0000],
|
| 256 |
+
[+0x4000_0000_0000_0000, +0x3fff_ffff_ffff_f800, +0x0800],
|
| 257 |
+
[+0x7fff_ffff_ffff_f800, +0x7fff_ffff_ffff_f800, +0x1000]]
|
| 258 |
+
# ^ clipped
|
| 259 |
+
|
| 260 |
+
assert_equal(data, correct)
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
def test_64_bit_even_size():
|
| 264 |
+
# 64-bit, 8 B container, 3 channels, 5 samples, 120 B data chunk
|
| 265 |
+
for mmap in [False, True]:
|
| 266 |
+
filename = 'test-8000Hz-le-3ch-5S-64bit.wav'
|
| 267 |
+
rate, data = wavfile.read(datafile(filename), mmap=mmap)
|
| 268 |
+
|
| 269 |
+
assert_equal(rate, 8000)
|
| 270 |
+
assert_(np.issubdtype(data.dtype, np.int64))
|
| 271 |
+
assert_equal(data.shape, (5, 3))
|
| 272 |
+
|
| 273 |
+
# Hand-made max/min samples under different conventions:
|
| 274 |
+
# Fixed-point 2**(N-1) Full-scale 2**(N-1)-1 LSB
|
| 275 |
+
correct = [[-0x8000_0000_0000_0000, -0x7fff_ffff_ffff_ffff, -0x2],
|
| 276 |
+
[-0x4000_0000_0000_0000, -0x3fff_ffff_ffff_ffff, -0x1],
|
| 277 |
+
[+0x0000_0000_0000_0000, +0x0000_0000_0000_0000, +0x0],
|
| 278 |
+
[+0x4000_0000_0000_0000, +0x3fff_ffff_ffff_ffff, +0x1],
|
| 279 |
+
[+0x7fff_ffff_ffff_ffff, +0x7fff_ffff_ffff_ffff, +0x2]]
|
| 280 |
+
# ^ clipped
|
| 281 |
+
|
| 282 |
+
assert_equal(data, correct)
|
| 283 |
+
|
| 284 |
+
del data
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def test_unsupported_mmap():
|
| 288 |
+
# Test containers that cannot be mapped to numpy types
|
| 289 |
+
for filename in {'test-8000Hz-le-3ch-5S-24bit.wav',
|
| 290 |
+
'test-8000Hz-le-3ch-5S-36bit.wav',
|
| 291 |
+
'test-8000Hz-le-3ch-5S-45bit.wav',
|
| 292 |
+
'test-8000Hz-le-3ch-5S-53bit.wav',
|
| 293 |
+
'test-1234Hz-le-1ch-10S-20bit-extra.wav'}:
|
| 294 |
+
with raises(ValueError, match="mmap.*not compatible"):
|
| 295 |
+
rate, data = wavfile.read(datafile(filename), mmap=True)
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
def test_rifx():
|
| 299 |
+
# Compare equivalent RIFX and RIFF files
|
| 300 |
+
for rifx, riff in {('test-44100Hz-be-1ch-4bytes.wav',
|
| 301 |
+
'test-44100Hz-le-1ch-4bytes.wav'),
|
| 302 |
+
('test-8000Hz-be-3ch-5S-24bit.wav',
|
| 303 |
+
'test-8000Hz-le-3ch-5S-24bit.wav')}:
|
| 304 |
+
rate1, data1 = wavfile.read(datafile(rifx), mmap=False)
|
| 305 |
+
rate2, data2 = wavfile.read(datafile(riff), mmap=False)
|
| 306 |
+
assert_equal(rate1, rate2)
|
| 307 |
+
assert_equal(data1, data2)
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
def test_rf64():
|
| 311 |
+
# Compare equivalent RF64 and RIFF files
|
| 312 |
+
for rf64, riff in {('test-44100Hz-le-1ch-4bytes-rf64.wav',
|
| 313 |
+
'test-44100Hz-le-1ch-4bytes.wav'),
|
| 314 |
+
('test-8000Hz-le-3ch-5S-24bit-rf64.wav',
|
| 315 |
+
'test-8000Hz-le-3ch-5S-24bit.wav')}:
|
| 316 |
+
rate1, data1 = wavfile.read(datafile(rf64), mmap=False)
|
| 317 |
+
rate2, data2 = wavfile.read(datafile(riff), mmap=False)
|
| 318 |
+
assert_array_equal(rate1, rate2)
|
| 319 |
+
assert_array_equal(data1, data2)
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
@pytest.mark.xslow
|
| 323 |
+
def test_write_roundtrip_rf64(tmpdir):
|
| 324 |
+
dtype = np.dtype("<i8")
|
| 325 |
+
tmpfile = str(tmpdir.join('temp.wav'))
|
| 326 |
+
rate = 44100
|
| 327 |
+
data = np.random.randint(0, 127, (2**29,)).astype(dtype)
|
| 328 |
+
|
| 329 |
+
wavfile.write(tmpfile, rate, data)
|
| 330 |
+
|
| 331 |
+
rate2, data2 = wavfile.read(tmpfile, mmap=True)
|
| 332 |
+
|
| 333 |
+
assert_equal(rate, rate2)
|
| 334 |
+
msg = f"{data2.dtype} byteorder not in ('<', '=', '|')"
|
| 335 |
+
assert data2.dtype.byteorder in ('<', '=', '|'), msg
|
| 336 |
+
assert_array_equal(data, data2)
|
| 337 |
+
# also test writing (gh-12176)
|
| 338 |
+
data2[0] = 0
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
# Fake a non-seekable file-like object without resorting to subprocesses.
|
| 342 |
+
class Nonseekable:
|
| 343 |
+
def __init__(self, fp):
|
| 344 |
+
self.fp = fp
|
| 345 |
+
|
| 346 |
+
def seekable(self):
|
| 347 |
+
return False
|
| 348 |
+
|
| 349 |
+
def read(self, size=-1, /):
|
| 350 |
+
return self.fp.read(size)
|
| 351 |
+
|
| 352 |
+
def close(self):
|
| 353 |
+
self.fp.close()
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
def test_streams():
|
| 357 |
+
for filename in ['test-44100Hz-le-1ch-4bytes.wav',
|
| 358 |
+
'test-8000Hz-le-2ch-1byteu.wav',
|
| 359 |
+
'test-44100Hz-2ch-32bit-float-le.wav',
|
| 360 |
+
'test-44100Hz-2ch-32bit-float-be.wav',
|
| 361 |
+
'test-8000Hz-le-5ch-9S-5bit.wav',
|
| 362 |
+
'test-8000Hz-le-4ch-9S-12bit.wav',
|
| 363 |
+
'test-8000Hz-le-3ch-5S-24bit.wav',
|
| 364 |
+
'test-1234Hz-le-1ch-10S-20bit-extra.wav',
|
| 365 |
+
'test-8000Hz-le-3ch-5S-36bit.wav',
|
| 366 |
+
'test-8000Hz-le-3ch-5S-45bit.wav',
|
| 367 |
+
'test-8000Hz-le-3ch-5S-53bit.wav',
|
| 368 |
+
'test-8000Hz-le-3ch-5S-64bit.wav',
|
| 369 |
+
'test-44100Hz-be-1ch-4bytes.wav', # RIFX
|
| 370 |
+
'test-44100Hz-le-1ch-4bytes-rf64.wav']:
|
| 371 |
+
dfname = datafile(filename)
|
| 372 |
+
with open(dfname, 'rb') as fp1, open(dfname, 'rb') as fp2:
|
| 373 |
+
rate1, data1 = wavfile.read(fp1)
|
| 374 |
+
rate2, data2 = wavfile.read(Nonseekable(fp2))
|
| 375 |
+
rate3, data3 = wavfile.read(dfname, mmap=False)
|
| 376 |
+
assert_equal(rate1, rate3)
|
| 377 |
+
assert_equal(rate2, rate3)
|
| 378 |
+
assert_equal(data1, data3)
|
| 379 |
+
assert_equal(data2, data3)
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
def test_read_unknown_filetype_fail():
|
| 383 |
+
# Not an RIFF
|
| 384 |
+
for mmap in [False, True]:
|
| 385 |
+
filename = 'example_1.nc'
|
| 386 |
+
with open(datafile(filename), 'rb') as fp:
|
| 387 |
+
with raises(ValueError, match="CDF.*'RIFF', 'RIFX', and 'RF64' supported"):
|
| 388 |
+
wavfile.read(fp, mmap=mmap)
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
def test_read_unknown_riff_form_type():
|
| 392 |
+
# RIFF, but not WAVE form
|
| 393 |
+
for mmap in [False, True]:
|
| 394 |
+
filename = 'Transparent Busy.ani'
|
| 395 |
+
with open(datafile(filename), 'rb') as fp:
|
| 396 |
+
with raises(ValueError, match='Not a WAV file.*ACON'):
|
| 397 |
+
wavfile.read(fp, mmap=mmap)
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
def test_read_unknown_wave_format():
|
| 401 |
+
# RIFF and WAVE, but not supported format
|
| 402 |
+
for mmap in [False, True]:
|
| 403 |
+
filename = 'test-8000Hz-le-1ch-1byte-ulaw.wav'
|
| 404 |
+
with open(datafile(filename), 'rb') as fp:
|
| 405 |
+
with raises(ValueError, match='Unknown wave file format.*MULAW.*'
|
| 406 |
+
'Supported formats'):
|
| 407 |
+
wavfile.read(fp, mmap=mmap)
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
def test_read_early_eof_with_data():
|
| 411 |
+
# File ends inside 'data' chunk, but we keep incomplete data
|
| 412 |
+
for mmap in [False, True]:
|
| 413 |
+
filename = 'test-44100Hz-le-1ch-4bytes-early-eof.wav'
|
| 414 |
+
with open(datafile(filename), 'rb') as fp:
|
| 415 |
+
with warns(wavfile.WavFileWarning, match='Reached EOF'):
|
| 416 |
+
rate, data = wavfile.read(fp, mmap=mmap)
|
| 417 |
+
assert data.size > 0
|
| 418 |
+
assert rate == 44100
|
| 419 |
+
# also test writing (gh-12176)
|
| 420 |
+
data[0] = 0
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
def test_read_early_eof():
|
| 424 |
+
# File ends after 'fact' chunk at boundary, no data read
|
| 425 |
+
for mmap in [False, True]:
|
| 426 |
+
filename = 'test-44100Hz-le-1ch-4bytes-early-eof-no-data.wav'
|
| 427 |
+
with open(datafile(filename), 'rb') as fp:
|
| 428 |
+
with raises(ValueError, match="Unexpected end of file."):
|
| 429 |
+
wavfile.read(fp, mmap=mmap)
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
def test_read_incomplete_chunk():
|
| 433 |
+
# File ends inside 'fmt ' chunk ID, no data read
|
| 434 |
+
for mmap in [False, True]:
|
| 435 |
+
filename = 'test-44100Hz-le-1ch-4bytes-incomplete-chunk.wav'
|
| 436 |
+
with open(datafile(filename), 'rb') as fp:
|
| 437 |
+
with raises(ValueError, match="Incomplete chunk ID.*b'f'"):
|
| 438 |
+
wavfile.read(fp, mmap=mmap)
|
| 439 |
+
|
| 440 |
+
|
| 441 |
+
def test_read_inconsistent_header():
|
| 442 |
+
# File header's size fields contradict each other
|
| 443 |
+
for mmap in [False, True]:
|
| 444 |
+
filename = 'test-8000Hz-le-3ch-5S-24bit-inconsistent.wav'
|
| 445 |
+
with open(datafile(filename), 'rb') as fp:
|
| 446 |
+
with raises(ValueError, match="header is invalid"):
|
| 447 |
+
wavfile.read(fp, mmap=mmap)
|
| 448 |
+
|
| 449 |
+
|
| 450 |
+
# signed 8-bit integer PCM is not allowed
|
| 451 |
+
# unsigned > 8-bit integer PCM is not allowed
|
| 452 |
+
# 8- or 16-bit float PCM is not expected
|
| 453 |
+
# g and q are platform-dependent, so not included
|
| 454 |
+
@pytest.mark.parametrize("dt_str", ["<i2", "<i4", "<i8", "<f4", "<f8",
|
| 455 |
+
">i2", ">i4", ">i8", ">f4", ">f8", '|u1'])
|
| 456 |
+
@pytest.mark.parametrize("channels", [1, 2, 5])
|
| 457 |
+
@pytest.mark.parametrize("rate", [8000, 32000])
|
| 458 |
+
@pytest.mark.parametrize("mmap", [False, True])
|
| 459 |
+
@pytest.mark.parametrize("realfile", [False, True])
|
| 460 |
+
def test_write_roundtrip(realfile, mmap, rate, channels, dt_str, tmpdir):
|
| 461 |
+
dtype = np.dtype(dt_str)
|
| 462 |
+
if realfile:
|
| 463 |
+
tmpfile = str(tmpdir.join(str(threading.get_native_id()), 'temp.wav'))
|
| 464 |
+
os.makedirs(os.path.dirname(tmpfile), exist_ok=True)
|
| 465 |
+
else:
|
| 466 |
+
tmpfile = BytesIO()
|
| 467 |
+
data = np.random.rand(100, channels)
|
| 468 |
+
if channels == 1:
|
| 469 |
+
data = data[:, 0]
|
| 470 |
+
if dtype.kind == 'f':
|
| 471 |
+
# The range of the float type should be in [-1, 1]
|
| 472 |
+
data = data.astype(dtype)
|
| 473 |
+
else:
|
| 474 |
+
data = (data*128).astype(dtype)
|
| 475 |
+
|
| 476 |
+
wavfile.write(tmpfile, rate, data)
|
| 477 |
+
|
| 478 |
+
rate2, data2 = wavfile.read(tmpfile, mmap=mmap)
|
| 479 |
+
|
| 480 |
+
assert_equal(rate, rate2)
|
| 481 |
+
assert_(data2.dtype.byteorder in ('<', '=', '|'), msg=data2.dtype)
|
| 482 |
+
assert_array_equal(data, data2)
|
| 483 |
+
# also test writing (gh-12176)
|
| 484 |
+
if realfile:
|
| 485 |
+
data2[0] = 0
|
| 486 |
+
else:
|
| 487 |
+
with pytest.raises(ValueError, match='read-only'):
|
| 488 |
+
data2[0] = 0
|
| 489 |
+
|
| 490 |
+
if realfile and mmap and IS_PYPY and sys.platform == 'win32':
|
| 491 |
+
# windows cannot remove a dead file held by a mmap but not collected
|
| 492 |
+
# in PyPy; since the filename gets reused in this test, clean this up
|
| 493 |
+
break_cycles()
|
| 494 |
+
break_cycles()
|
| 495 |
+
|
| 496 |
+
|
| 497 |
+
@pytest.mark.parametrize("dtype", [np.float16])
|
| 498 |
+
def test_wavfile_dtype_unsupported(tmpdir, dtype):
|
| 499 |
+
tmpfile = str(tmpdir.join('temp.wav'))
|
| 500 |
+
rng = np.random.default_rng(1234)
|
| 501 |
+
data = rng.random((100, 5)).astype(dtype)
|
| 502 |
+
rate = 8000
|
| 503 |
+
with pytest.raises(ValueError, match="Unsupported"):
|
| 504 |
+
wavfile.write(tmpfile, rate, data)
|
| 505 |
+
|
| 506 |
+
def test_seek_emulating_reader_invalid_seek():
|
| 507 |
+
# Dummy data for the reader
|
| 508 |
+
reader = wavfile.SeekEmulatingReader(BytesIO(b'\x00\x00'))
|
| 509 |
+
|
| 510 |
+
# Test SEEK_END with an invalid whence value
|
| 511 |
+
with pytest.raises(UnsupportedOperation):
|
| 512 |
+
reader.seek(0, 5) # Invalid whence value
|
| 513 |
+
|
| 514 |
+
# Test with negative seek value
|
| 515 |
+
with pytest.raises(UnsupportedOperation):
|
| 516 |
+
reader.seek(-1, 0) # Negative position with SEEK_SET
|
| 517 |
+
|
| 518 |
+
# Test SEEK_END with valid parameters (should not raise)
|
| 519 |
+
pos = reader.seek(0, os.SEEK_END) # Valid usage
|
| 520 |
+
assert pos == 2, f"Failed to seek to end, got position {pos}"
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/io/wavfile.py
ADDED
|
@@ -0,0 +1,944 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Module to read / write wav files using NumPy arrays
|
| 3 |
+
|
| 4 |
+
Functions
|
| 5 |
+
---------
|
| 6 |
+
`read`: Return the sample rate (in samples/sec) and data from a WAV file.
|
| 7 |
+
|
| 8 |
+
`write`: Write a NumPy array as a WAV file.
|
| 9 |
+
|
| 10 |
+
"""
|
| 11 |
+
import io
|
| 12 |
+
import os
|
| 13 |
+
import sys
|
| 14 |
+
import numpy as np
|
| 15 |
+
import struct
|
| 16 |
+
import warnings
|
| 17 |
+
from enum import IntEnum
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
__all__ = [
|
| 21 |
+
'WavFileWarning',
|
| 22 |
+
'read',
|
| 23 |
+
'write'
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class WavFileWarning(UserWarning):
|
| 28 |
+
"""
|
| 29 |
+
Warning for WAV files with format issues that can still be read.
|
| 30 |
+
|
| 31 |
+
Raised when a WAV file has problems like missing metadata or
|
| 32 |
+
non-standard formatting, but can still be processed successfully.
|
| 33 |
+
"""
|
| 34 |
+
pass
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class SeekEmulatingReader:
|
| 38 |
+
"""
|
| 39 |
+
Tracks stream position, provides tell(), and emulates only those
|
| 40 |
+
seeks that can be supported by reading forward. Other seeks raise
|
| 41 |
+
io.UnsupportedOperation. Note that this class implements only the
|
| 42 |
+
minimum necessary to keep wavfile.read() happy.
|
| 43 |
+
"""
|
| 44 |
+
def __init__(self, reader):
|
| 45 |
+
self.reader = reader
|
| 46 |
+
self.pos = 0
|
| 47 |
+
|
| 48 |
+
def read(self, size=-1, /):
|
| 49 |
+
data = self.reader.read(size)
|
| 50 |
+
self.pos += len(data)
|
| 51 |
+
return data
|
| 52 |
+
|
| 53 |
+
def seek(self, offset, whence=os.SEEK_SET, /):
|
| 54 |
+
match whence:
|
| 55 |
+
case os.SEEK_SET if offset >= self.pos:
|
| 56 |
+
self.read(offset - self.pos) # convert to relative
|
| 57 |
+
case os.SEEK_CUR if offset >= 0:
|
| 58 |
+
self.read(offset) # advance by offset
|
| 59 |
+
case os.SEEK_END if offset == 0:
|
| 60 |
+
self.read() # advance to end of stream
|
| 61 |
+
case _:
|
| 62 |
+
raise io.UnsupportedOperation("SeekEmulatingReader was asked to emulate"
|
| 63 |
+
" a seek operation it does not support.")
|
| 64 |
+
return self.pos
|
| 65 |
+
|
| 66 |
+
def tell(self):
|
| 67 |
+
return self.pos
|
| 68 |
+
|
| 69 |
+
def close(self):
|
| 70 |
+
self.reader.close()
|
| 71 |
+
|
| 72 |
+
# np.fromfile expects to be able to call flush(), and _read_data_chunk
|
| 73 |
+
# expects to catch io.UnsupportedOperation if np.fromfile fails.
|
| 74 |
+
def flush(self):
|
| 75 |
+
raise io.UnsupportedOperation("SeekEmulatingReader can't flush.")
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class WAVE_FORMAT(IntEnum):
|
| 79 |
+
"""
|
| 80 |
+
WAVE form wFormatTag IDs
|
| 81 |
+
|
| 82 |
+
Complete list is in mmreg.h in Windows 10 SDK. ALAC and OPUS are the
|
| 83 |
+
newest additions, in v10.0.14393 2016-07
|
| 84 |
+
"""
|
| 85 |
+
UNKNOWN = 0x0000
|
| 86 |
+
PCM = 0x0001
|
| 87 |
+
ADPCM = 0x0002
|
| 88 |
+
IEEE_FLOAT = 0x0003
|
| 89 |
+
VSELP = 0x0004
|
| 90 |
+
IBM_CVSD = 0x0005
|
| 91 |
+
ALAW = 0x0006
|
| 92 |
+
MULAW = 0x0007
|
| 93 |
+
DTS = 0x0008
|
| 94 |
+
DRM = 0x0009
|
| 95 |
+
WMAVOICE9 = 0x000A
|
| 96 |
+
WMAVOICE10 = 0x000B
|
| 97 |
+
OKI_ADPCM = 0x0010
|
| 98 |
+
DVI_ADPCM = 0x0011
|
| 99 |
+
IMA_ADPCM = 0x0011 # Duplicate
|
| 100 |
+
MEDIASPACE_ADPCM = 0x0012
|
| 101 |
+
SIERRA_ADPCM = 0x0013
|
| 102 |
+
G723_ADPCM = 0x0014
|
| 103 |
+
DIGISTD = 0x0015
|
| 104 |
+
DIGIFIX = 0x0016
|
| 105 |
+
DIALOGIC_OKI_ADPCM = 0x0017
|
| 106 |
+
MEDIAVISION_ADPCM = 0x0018
|
| 107 |
+
CU_CODEC = 0x0019
|
| 108 |
+
HP_DYN_VOICE = 0x001A
|
| 109 |
+
YAMAHA_ADPCM = 0x0020
|
| 110 |
+
SONARC = 0x0021
|
| 111 |
+
DSPGROUP_TRUESPEECH = 0x0022
|
| 112 |
+
ECHOSC1 = 0x0023
|
| 113 |
+
AUDIOFILE_AF36 = 0x0024
|
| 114 |
+
APTX = 0x0025
|
| 115 |
+
AUDIOFILE_AF10 = 0x0026
|
| 116 |
+
PROSODY_1612 = 0x0027
|
| 117 |
+
LRC = 0x0028
|
| 118 |
+
DOLBY_AC2 = 0x0030
|
| 119 |
+
GSM610 = 0x0031
|
| 120 |
+
MSNAUDIO = 0x0032
|
| 121 |
+
ANTEX_ADPCME = 0x0033
|
| 122 |
+
CONTROL_RES_VQLPC = 0x0034
|
| 123 |
+
DIGIREAL = 0x0035
|
| 124 |
+
DIGIADPCM = 0x0036
|
| 125 |
+
CONTROL_RES_CR10 = 0x0037
|
| 126 |
+
NMS_VBXADPCM = 0x0038
|
| 127 |
+
CS_IMAADPCM = 0x0039
|
| 128 |
+
ECHOSC3 = 0x003A
|
| 129 |
+
ROCKWELL_ADPCM = 0x003B
|
| 130 |
+
ROCKWELL_DIGITALK = 0x003C
|
| 131 |
+
XEBEC = 0x003D
|
| 132 |
+
G721_ADPCM = 0x0040
|
| 133 |
+
G728_CELP = 0x0041
|
| 134 |
+
MSG723 = 0x0042
|
| 135 |
+
INTEL_G723_1 = 0x0043
|
| 136 |
+
INTEL_G729 = 0x0044
|
| 137 |
+
SHARP_G726 = 0x0045
|
| 138 |
+
MPEG = 0x0050
|
| 139 |
+
RT24 = 0x0052
|
| 140 |
+
PAC = 0x0053
|
| 141 |
+
MPEGLAYER3 = 0x0055
|
| 142 |
+
LUCENT_G723 = 0x0059
|
| 143 |
+
CIRRUS = 0x0060
|
| 144 |
+
ESPCM = 0x0061
|
| 145 |
+
VOXWARE = 0x0062
|
| 146 |
+
CANOPUS_ATRAC = 0x0063
|
| 147 |
+
G726_ADPCM = 0x0064
|
| 148 |
+
G722_ADPCM = 0x0065
|
| 149 |
+
DSAT = 0x0066
|
| 150 |
+
DSAT_DISPLAY = 0x0067
|
| 151 |
+
VOXWARE_BYTE_ALIGNED = 0x0069
|
| 152 |
+
VOXWARE_AC8 = 0x0070
|
| 153 |
+
VOXWARE_AC10 = 0x0071
|
| 154 |
+
VOXWARE_AC16 = 0x0072
|
| 155 |
+
VOXWARE_AC20 = 0x0073
|
| 156 |
+
VOXWARE_RT24 = 0x0074
|
| 157 |
+
VOXWARE_RT29 = 0x0075
|
| 158 |
+
VOXWARE_RT29HW = 0x0076
|
| 159 |
+
VOXWARE_VR12 = 0x0077
|
| 160 |
+
VOXWARE_VR18 = 0x0078
|
| 161 |
+
VOXWARE_TQ40 = 0x0079
|
| 162 |
+
VOXWARE_SC3 = 0x007A
|
| 163 |
+
VOXWARE_SC3_1 = 0x007B
|
| 164 |
+
SOFTSOUND = 0x0080
|
| 165 |
+
VOXWARE_TQ60 = 0x0081
|
| 166 |
+
MSRT24 = 0x0082
|
| 167 |
+
G729A = 0x0083
|
| 168 |
+
MVI_MVI2 = 0x0084
|
| 169 |
+
DF_G726 = 0x0085
|
| 170 |
+
DF_GSM610 = 0x0086
|
| 171 |
+
ISIAUDIO = 0x0088
|
| 172 |
+
ONLIVE = 0x0089
|
| 173 |
+
MULTITUDE_FT_SX20 = 0x008A
|
| 174 |
+
INFOCOM_ITS_G721_ADPCM = 0x008B
|
| 175 |
+
CONVEDIA_G729 = 0x008C
|
| 176 |
+
CONGRUENCY = 0x008D
|
| 177 |
+
SBC24 = 0x0091
|
| 178 |
+
DOLBY_AC3_SPDIF = 0x0092
|
| 179 |
+
MEDIASONIC_G723 = 0x0093
|
| 180 |
+
PROSODY_8KBPS = 0x0094
|
| 181 |
+
ZYXEL_ADPCM = 0x0097
|
| 182 |
+
PHILIPS_LPCBB = 0x0098
|
| 183 |
+
PACKED = 0x0099
|
| 184 |
+
MALDEN_PHONYTALK = 0x00A0
|
| 185 |
+
RACAL_RECORDER_GSM = 0x00A1
|
| 186 |
+
RACAL_RECORDER_G720_A = 0x00A2
|
| 187 |
+
RACAL_RECORDER_G723_1 = 0x00A3
|
| 188 |
+
RACAL_RECORDER_TETRA_ACELP = 0x00A4
|
| 189 |
+
NEC_AAC = 0x00B0
|
| 190 |
+
RAW_AAC1 = 0x00FF
|
| 191 |
+
RHETOREX_ADPCM = 0x0100
|
| 192 |
+
IRAT = 0x0101
|
| 193 |
+
VIVO_G723 = 0x0111
|
| 194 |
+
VIVO_SIREN = 0x0112
|
| 195 |
+
PHILIPS_CELP = 0x0120
|
| 196 |
+
PHILIPS_GRUNDIG = 0x0121
|
| 197 |
+
DIGITAL_G723 = 0x0123
|
| 198 |
+
SANYO_LD_ADPCM = 0x0125
|
| 199 |
+
SIPROLAB_ACEPLNET = 0x0130
|
| 200 |
+
SIPROLAB_ACELP4800 = 0x0131
|
| 201 |
+
SIPROLAB_ACELP8V3 = 0x0132
|
| 202 |
+
SIPROLAB_G729 = 0x0133
|
| 203 |
+
SIPROLAB_G729A = 0x0134
|
| 204 |
+
SIPROLAB_KELVIN = 0x0135
|
| 205 |
+
VOICEAGE_AMR = 0x0136
|
| 206 |
+
G726ADPCM = 0x0140
|
| 207 |
+
DICTAPHONE_CELP68 = 0x0141
|
| 208 |
+
DICTAPHONE_CELP54 = 0x0142
|
| 209 |
+
QUALCOMM_PUREVOICE = 0x0150
|
| 210 |
+
QUALCOMM_HALFRATE = 0x0151
|
| 211 |
+
TUBGSM = 0x0155
|
| 212 |
+
MSAUDIO1 = 0x0160
|
| 213 |
+
WMAUDIO2 = 0x0161
|
| 214 |
+
WMAUDIO3 = 0x0162
|
| 215 |
+
WMAUDIO_LOSSLESS = 0x0163
|
| 216 |
+
WMASPDIF = 0x0164
|
| 217 |
+
UNISYS_NAP_ADPCM = 0x0170
|
| 218 |
+
UNISYS_NAP_ULAW = 0x0171
|
| 219 |
+
UNISYS_NAP_ALAW = 0x0172
|
| 220 |
+
UNISYS_NAP_16K = 0x0173
|
| 221 |
+
SYCOM_ACM_SYC008 = 0x0174
|
| 222 |
+
SYCOM_ACM_SYC701_G726L = 0x0175
|
| 223 |
+
SYCOM_ACM_SYC701_CELP54 = 0x0176
|
| 224 |
+
SYCOM_ACM_SYC701_CELP68 = 0x0177
|
| 225 |
+
KNOWLEDGE_ADVENTURE_ADPCM = 0x0178
|
| 226 |
+
FRAUNHOFER_IIS_MPEG2_AAC = 0x0180
|
| 227 |
+
DTS_DS = 0x0190
|
| 228 |
+
CREATIVE_ADPCM = 0x0200
|
| 229 |
+
CREATIVE_FASTSPEECH8 = 0x0202
|
| 230 |
+
CREATIVE_FASTSPEECH10 = 0x0203
|
| 231 |
+
UHER_ADPCM = 0x0210
|
| 232 |
+
ULEAD_DV_AUDIO = 0x0215
|
| 233 |
+
ULEAD_DV_AUDIO_1 = 0x0216
|
| 234 |
+
QUARTERDECK = 0x0220
|
| 235 |
+
ILINK_VC = 0x0230
|
| 236 |
+
RAW_SPORT = 0x0240
|
| 237 |
+
ESST_AC3 = 0x0241
|
| 238 |
+
GENERIC_PASSTHRU = 0x0249
|
| 239 |
+
IPI_HSX = 0x0250
|
| 240 |
+
IPI_RPELP = 0x0251
|
| 241 |
+
CS2 = 0x0260
|
| 242 |
+
SONY_SCX = 0x0270
|
| 243 |
+
SONY_SCY = 0x0271
|
| 244 |
+
SONY_ATRAC3 = 0x0272
|
| 245 |
+
SONY_SPC = 0x0273
|
| 246 |
+
TELUM_AUDIO = 0x0280
|
| 247 |
+
TELUM_IA_AUDIO = 0x0281
|
| 248 |
+
NORCOM_VOICE_SYSTEMS_ADPCM = 0x0285
|
| 249 |
+
FM_TOWNS_SND = 0x0300
|
| 250 |
+
MICRONAS = 0x0350
|
| 251 |
+
MICRONAS_CELP833 = 0x0351
|
| 252 |
+
BTV_DIGITAL = 0x0400
|
| 253 |
+
INTEL_MUSIC_CODER = 0x0401
|
| 254 |
+
INDEO_AUDIO = 0x0402
|
| 255 |
+
QDESIGN_MUSIC = 0x0450
|
| 256 |
+
ON2_VP7_AUDIO = 0x0500
|
| 257 |
+
ON2_VP6_AUDIO = 0x0501
|
| 258 |
+
VME_VMPCM = 0x0680
|
| 259 |
+
TPC = 0x0681
|
| 260 |
+
LIGHTWAVE_LOSSLESS = 0x08AE
|
| 261 |
+
OLIGSM = 0x1000
|
| 262 |
+
OLIADPCM = 0x1001
|
| 263 |
+
OLICELP = 0x1002
|
| 264 |
+
OLISBC = 0x1003
|
| 265 |
+
OLIOPR = 0x1004
|
| 266 |
+
LH_CODEC = 0x1100
|
| 267 |
+
LH_CODEC_CELP = 0x1101
|
| 268 |
+
LH_CODEC_SBC8 = 0x1102
|
| 269 |
+
LH_CODEC_SBC12 = 0x1103
|
| 270 |
+
LH_CODEC_SBC16 = 0x1104
|
| 271 |
+
NORRIS = 0x1400
|
| 272 |
+
ISIAUDIO_2 = 0x1401
|
| 273 |
+
SOUNDSPACE_MUSICOMPRESS = 0x1500
|
| 274 |
+
MPEG_ADTS_AAC = 0x1600
|
| 275 |
+
MPEG_RAW_AAC = 0x1601
|
| 276 |
+
MPEG_LOAS = 0x1602
|
| 277 |
+
NOKIA_MPEG_ADTS_AAC = 0x1608
|
| 278 |
+
NOKIA_MPEG_RAW_AAC = 0x1609
|
| 279 |
+
VODAFONE_MPEG_ADTS_AAC = 0x160A
|
| 280 |
+
VODAFONE_MPEG_RAW_AAC = 0x160B
|
| 281 |
+
MPEG_HEAAC = 0x1610
|
| 282 |
+
VOXWARE_RT24_SPEECH = 0x181C
|
| 283 |
+
SONICFOUNDRY_LOSSLESS = 0x1971
|
| 284 |
+
INNINGS_TELECOM_ADPCM = 0x1979
|
| 285 |
+
LUCENT_SX8300P = 0x1C07
|
| 286 |
+
LUCENT_SX5363S = 0x1C0C
|
| 287 |
+
CUSEEME = 0x1F03
|
| 288 |
+
NTCSOFT_ALF2CM_ACM = 0x1FC4
|
| 289 |
+
DVM = 0x2000
|
| 290 |
+
DTS2 = 0x2001
|
| 291 |
+
MAKEAVIS = 0x3313
|
| 292 |
+
DIVIO_MPEG4_AAC = 0x4143
|
| 293 |
+
NOKIA_ADAPTIVE_MULTIRATE = 0x4201
|
| 294 |
+
DIVIO_G726 = 0x4243
|
| 295 |
+
LEAD_SPEECH = 0x434C
|
| 296 |
+
LEAD_VORBIS = 0x564C
|
| 297 |
+
WAVPACK_AUDIO = 0x5756
|
| 298 |
+
OGG_VORBIS_MODE_1 = 0x674F
|
| 299 |
+
OGG_VORBIS_MODE_2 = 0x6750
|
| 300 |
+
OGG_VORBIS_MODE_3 = 0x6751
|
| 301 |
+
OGG_VORBIS_MODE_1_PLUS = 0x676F
|
| 302 |
+
OGG_VORBIS_MODE_2_PLUS = 0x6770
|
| 303 |
+
OGG_VORBIS_MODE_3_PLUS = 0x6771
|
| 304 |
+
ALAC = 0x6C61
|
| 305 |
+
_3COM_NBX = 0x7000 # Can't have leading digit
|
| 306 |
+
OPUS = 0x704F
|
| 307 |
+
FAAD_AAC = 0x706D
|
| 308 |
+
AMR_NB = 0x7361
|
| 309 |
+
AMR_WB = 0x7362
|
| 310 |
+
AMR_WP = 0x7363
|
| 311 |
+
GSM_AMR_CBR = 0x7A21
|
| 312 |
+
GSM_AMR_VBR_SID = 0x7A22
|
| 313 |
+
COMVERSE_INFOSYS_G723_1 = 0xA100
|
| 314 |
+
COMVERSE_INFOSYS_AVQSBC = 0xA101
|
| 315 |
+
COMVERSE_INFOSYS_SBC = 0xA102
|
| 316 |
+
SYMBOL_G729_A = 0xA103
|
| 317 |
+
VOICEAGE_AMR_WB = 0xA104
|
| 318 |
+
INGENIENT_G726 = 0xA105
|
| 319 |
+
MPEG4_AAC = 0xA106
|
| 320 |
+
ENCORE_G726 = 0xA107
|
| 321 |
+
ZOLL_ASAO = 0xA108
|
| 322 |
+
SPEEX_VOICE = 0xA109
|
| 323 |
+
VIANIX_MASC = 0xA10A
|
| 324 |
+
WM9_SPECTRUM_ANALYZER = 0xA10B
|
| 325 |
+
WMF_SPECTRUM_ANAYZER = 0xA10C
|
| 326 |
+
GSM_610 = 0xA10D
|
| 327 |
+
GSM_620 = 0xA10E
|
| 328 |
+
GSM_660 = 0xA10F
|
| 329 |
+
GSM_690 = 0xA110
|
| 330 |
+
GSM_ADAPTIVE_MULTIRATE_WB = 0xA111
|
| 331 |
+
POLYCOM_G722 = 0xA112
|
| 332 |
+
POLYCOM_G728 = 0xA113
|
| 333 |
+
POLYCOM_G729_A = 0xA114
|
| 334 |
+
POLYCOM_SIREN = 0xA115
|
| 335 |
+
GLOBAL_IP_ILBC = 0xA116
|
| 336 |
+
RADIOTIME_TIME_SHIFT_RADIO = 0xA117
|
| 337 |
+
NICE_ACA = 0xA118
|
| 338 |
+
NICE_ADPCM = 0xA119
|
| 339 |
+
VOCORD_G721 = 0xA11A
|
| 340 |
+
VOCORD_G726 = 0xA11B
|
| 341 |
+
VOCORD_G722_1 = 0xA11C
|
| 342 |
+
VOCORD_G728 = 0xA11D
|
| 343 |
+
VOCORD_G729 = 0xA11E
|
| 344 |
+
VOCORD_G729_A = 0xA11F
|
| 345 |
+
VOCORD_G723_1 = 0xA120
|
| 346 |
+
VOCORD_LBC = 0xA121
|
| 347 |
+
NICE_G728 = 0xA122
|
| 348 |
+
FRACE_TELECOM_G729 = 0xA123
|
| 349 |
+
CODIAN = 0xA124
|
| 350 |
+
FLAC = 0xF1AC
|
| 351 |
+
EXTENSIBLE = 0xFFFE
|
| 352 |
+
DEVELOPMENT = 0xFFFF
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
KNOWN_WAVE_FORMATS = {WAVE_FORMAT.PCM, WAVE_FORMAT.IEEE_FLOAT}
|
| 356 |
+
|
| 357 |
+
|
| 358 |
+
def _raise_bad_format(format_tag):
|
| 359 |
+
try:
|
| 360 |
+
format_name = WAVE_FORMAT(format_tag).name
|
| 361 |
+
except ValueError:
|
| 362 |
+
format_name = f'{format_tag:#06x}'
|
| 363 |
+
raise ValueError(f"Unknown wave file format: {format_name}. Supported "
|
| 364 |
+
"formats: " +
|
| 365 |
+
', '.join(x.name for x in KNOWN_WAVE_FORMATS))
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
def _read_fmt_chunk(fid, is_big_endian):
|
| 369 |
+
"""
|
| 370 |
+
Returns
|
| 371 |
+
-------
|
| 372 |
+
size : int
|
| 373 |
+
size of format subchunk in bytes (minus 8 for "fmt " and itself)
|
| 374 |
+
format_tag : int
|
| 375 |
+
PCM, float, or compressed format
|
| 376 |
+
channels : int
|
| 377 |
+
number of channels
|
| 378 |
+
fs : int
|
| 379 |
+
sampling frequency in samples per second
|
| 380 |
+
bytes_per_second : int
|
| 381 |
+
overall byte rate for the file
|
| 382 |
+
block_align : int
|
| 383 |
+
bytes per sample, including all channels
|
| 384 |
+
bit_depth : int
|
| 385 |
+
bits per sample
|
| 386 |
+
|
| 387 |
+
Notes
|
| 388 |
+
-----
|
| 389 |
+
Assumes file pointer is immediately after the 'fmt ' id
|
| 390 |
+
"""
|
| 391 |
+
if is_big_endian:
|
| 392 |
+
fmt = '>'
|
| 393 |
+
else:
|
| 394 |
+
fmt = '<'
|
| 395 |
+
|
| 396 |
+
size = struct.unpack(fmt+'I', fid.read(4))[0]
|
| 397 |
+
|
| 398 |
+
if size < 16:
|
| 399 |
+
raise ValueError("Binary structure of wave file is not compliant")
|
| 400 |
+
|
| 401 |
+
res = struct.unpack(fmt+'HHIIHH', fid.read(16))
|
| 402 |
+
bytes_read = 16
|
| 403 |
+
|
| 404 |
+
format_tag, channels, fs, bytes_per_second, block_align, bit_depth = res
|
| 405 |
+
|
| 406 |
+
if format_tag == WAVE_FORMAT.EXTENSIBLE and size >= (16+2):
|
| 407 |
+
ext_chunk_size = struct.unpack(fmt+'H', fid.read(2))[0]
|
| 408 |
+
bytes_read += 2
|
| 409 |
+
if ext_chunk_size >= 22:
|
| 410 |
+
extensible_chunk_data = fid.read(22)
|
| 411 |
+
bytes_read += 22
|
| 412 |
+
raw_guid = extensible_chunk_data[2+4:2+4+16]
|
| 413 |
+
# GUID template {XXXXXXXX-0000-0010-8000-00AA00389B71} (RFC-2361)
|
| 414 |
+
# MS GUID byte order: first three groups are native byte order,
|
| 415 |
+
# rest is Big Endian
|
| 416 |
+
if is_big_endian:
|
| 417 |
+
tail = b'\x00\x00\x00\x10\x80\x00\x00\xAA\x00\x38\x9B\x71'
|
| 418 |
+
else:
|
| 419 |
+
tail = b'\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71'
|
| 420 |
+
if raw_guid.endswith(tail):
|
| 421 |
+
format_tag = struct.unpack(fmt+'I', raw_guid[:4])[0]
|
| 422 |
+
else:
|
| 423 |
+
raise ValueError("Binary structure of wave file is not compliant")
|
| 424 |
+
|
| 425 |
+
if format_tag not in KNOWN_WAVE_FORMATS:
|
| 426 |
+
_raise_bad_format(format_tag)
|
| 427 |
+
|
| 428 |
+
# move file pointer to next chunk
|
| 429 |
+
if size > bytes_read:
|
| 430 |
+
fid.read(size - bytes_read)
|
| 431 |
+
|
| 432 |
+
# fmt should always be 16, 18 or 40, but handle it just in case
|
| 433 |
+
_handle_pad_byte(fid, size)
|
| 434 |
+
|
| 435 |
+
if format_tag == WAVE_FORMAT.PCM:
|
| 436 |
+
if bytes_per_second != fs * block_align:
|
| 437 |
+
raise ValueError("WAV header is invalid: nAvgBytesPerSec must"
|
| 438 |
+
" equal product of nSamplesPerSec and"
|
| 439 |
+
" nBlockAlign, but file has nSamplesPerSec ="
|
| 440 |
+
f" {fs}, nBlockAlign = {block_align}, and"
|
| 441 |
+
f" nAvgBytesPerSec = {bytes_per_second}")
|
| 442 |
+
|
| 443 |
+
return (size, format_tag, channels, fs, bytes_per_second, block_align,
|
| 444 |
+
bit_depth)
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
def _read_data_chunk(fid, format_tag, channels, bit_depth, is_big_endian, is_rf64,
|
| 448 |
+
block_align, mmap=False, rf64_chunk_size=None):
|
| 449 |
+
"""
|
| 450 |
+
Notes
|
| 451 |
+
-----
|
| 452 |
+
Assumes file pointer is immediately after the 'data' id
|
| 453 |
+
|
| 454 |
+
It's possible to not use all available bits in a container, or to store
|
| 455 |
+
samples in a container bigger than necessary, so bytes_per_sample uses
|
| 456 |
+
the actual reported container size (nBlockAlign / nChannels). Real-world
|
| 457 |
+
examples:
|
| 458 |
+
|
| 459 |
+
Adobe Audition's "24-bit packed int (type 1, 20-bit)"
|
| 460 |
+
|
| 461 |
+
nChannels = 2, nBlockAlign = 6, wBitsPerSample = 20
|
| 462 |
+
|
| 463 |
+
http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Samples/AFsp/M1F1-int12-AFsp.wav
|
| 464 |
+
is:
|
| 465 |
+
|
| 466 |
+
nChannels = 2, nBlockAlign = 4, wBitsPerSample = 12
|
| 467 |
+
|
| 468 |
+
http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/multichaudP.pdf
|
| 469 |
+
gives an example of:
|
| 470 |
+
|
| 471 |
+
nChannels = 2, nBlockAlign = 8, wBitsPerSample = 20
|
| 472 |
+
"""
|
| 473 |
+
if is_big_endian:
|
| 474 |
+
fmt = '>'
|
| 475 |
+
else:
|
| 476 |
+
fmt = '<'
|
| 477 |
+
|
| 478 |
+
# Size of the data subchunk in bytes
|
| 479 |
+
if not is_rf64:
|
| 480 |
+
size = struct.unpack(fmt+'I', fid.read(4))[0]
|
| 481 |
+
else:
|
| 482 |
+
# chunk size is stored in global file header for RF64
|
| 483 |
+
size = rf64_chunk_size
|
| 484 |
+
# skip data chunk size as it is 0xFFFFFFF
|
| 485 |
+
fid.read(4)
|
| 486 |
+
|
| 487 |
+
# Number of bytes per sample (sample container size)
|
| 488 |
+
bytes_per_sample = block_align // channels
|
| 489 |
+
n_samples = size // bytes_per_sample
|
| 490 |
+
|
| 491 |
+
if format_tag == WAVE_FORMAT.PCM:
|
| 492 |
+
if 1 <= bit_depth <= 8:
|
| 493 |
+
dtype = 'u1' # WAV of 8-bit integer or less are unsigned
|
| 494 |
+
elif bytes_per_sample in {3, 5, 6, 7}:
|
| 495 |
+
# No compatible dtype. Load as raw bytes for reshaping later.
|
| 496 |
+
dtype = 'V1'
|
| 497 |
+
elif bit_depth <= 64:
|
| 498 |
+
# Remaining bit depths can map directly to signed numpy dtypes
|
| 499 |
+
dtype = f'{fmt}i{bytes_per_sample}'
|
| 500 |
+
else:
|
| 501 |
+
raise ValueError("Unsupported bit depth: the WAV file "
|
| 502 |
+
f"has {bit_depth}-bit integer data.")
|
| 503 |
+
elif format_tag == WAVE_FORMAT.IEEE_FLOAT:
|
| 504 |
+
if bit_depth in {32, 64}:
|
| 505 |
+
dtype = f'{fmt}f{bytes_per_sample}'
|
| 506 |
+
else:
|
| 507 |
+
raise ValueError("Unsupported bit depth: the WAV file "
|
| 508 |
+
f"has {bit_depth}-bit floating-point data.")
|
| 509 |
+
else:
|
| 510 |
+
_raise_bad_format(format_tag)
|
| 511 |
+
|
| 512 |
+
start = fid.tell()
|
| 513 |
+
if not mmap:
|
| 514 |
+
try:
|
| 515 |
+
count = size if dtype == 'V1' else n_samples
|
| 516 |
+
data = np.fromfile(fid, dtype=dtype, count=count)
|
| 517 |
+
except io.UnsupportedOperation: # not a C-like file
|
| 518 |
+
fid.seek(start, 0) # just in case it seeked, though it shouldn't
|
| 519 |
+
data = np.frombuffer(fid.read(size), dtype=dtype)
|
| 520 |
+
|
| 521 |
+
if dtype == 'V1':
|
| 522 |
+
# Rearrange raw bytes into smallest compatible numpy dtype
|
| 523 |
+
dt = f'{fmt}i4' if bytes_per_sample == 3 else f'{fmt}i8'
|
| 524 |
+
a = np.zeros((len(data) // bytes_per_sample, np.dtype(dt).itemsize),
|
| 525 |
+
dtype='V1')
|
| 526 |
+
if is_big_endian:
|
| 527 |
+
a[:, :bytes_per_sample] = data.reshape((-1, bytes_per_sample))
|
| 528 |
+
else:
|
| 529 |
+
a[:, -bytes_per_sample:] = data.reshape((-1, bytes_per_sample))
|
| 530 |
+
data = a.view(dt).reshape(a.shape[:-1])
|
| 531 |
+
else:
|
| 532 |
+
if bytes_per_sample in {1, 2, 4, 8}:
|
| 533 |
+
start = fid.tell()
|
| 534 |
+
data = np.memmap(fid, dtype=dtype, mode='c', offset=start,
|
| 535 |
+
shape=(n_samples,))
|
| 536 |
+
fid.seek(start + size)
|
| 537 |
+
else:
|
| 538 |
+
raise ValueError("mmap=True not compatible with "
|
| 539 |
+
f"{bytes_per_sample}-byte container size.")
|
| 540 |
+
|
| 541 |
+
_handle_pad_byte(fid, size)
|
| 542 |
+
|
| 543 |
+
if channels > 1:
|
| 544 |
+
data = data.reshape(-1, channels)
|
| 545 |
+
return data
|
| 546 |
+
|
| 547 |
+
|
| 548 |
+
def _skip_unknown_chunk(fid, is_big_endian):
|
| 549 |
+
if is_big_endian:
|
| 550 |
+
fmt = '>I'
|
| 551 |
+
else:
|
| 552 |
+
fmt = '<I'
|
| 553 |
+
|
| 554 |
+
data = fid.read(4)
|
| 555 |
+
# call unpack() and seek() only if we have really read data from file
|
| 556 |
+
# otherwise empty read at the end of the file would trigger
|
| 557 |
+
# unnecessary exception at unpack() call
|
| 558 |
+
# in case data equals somehow to 0, there is no need for seek() anyway
|
| 559 |
+
if data:
|
| 560 |
+
size = struct.unpack(fmt, data)[0]
|
| 561 |
+
fid.seek(size, 1)
|
| 562 |
+
_handle_pad_byte(fid, size)
|
| 563 |
+
|
| 564 |
+
|
| 565 |
+
def _read_riff_chunk(fid):
|
| 566 |
+
str1 = fid.read(4) # File signature
|
| 567 |
+
if str1 == b'RIFF':
|
| 568 |
+
is_rf64 = False
|
| 569 |
+
is_big_endian = False
|
| 570 |
+
fmt = '<I'
|
| 571 |
+
elif str1 == b'RIFX':
|
| 572 |
+
is_rf64 = False
|
| 573 |
+
is_big_endian = True
|
| 574 |
+
fmt = '>I'
|
| 575 |
+
elif str1 == b'RF64':
|
| 576 |
+
is_rf64 = True
|
| 577 |
+
is_big_endian = False
|
| 578 |
+
fmt = '<Q'
|
| 579 |
+
else:
|
| 580 |
+
# There are also .wav files with "FFIR" or "XFIR" signatures?
|
| 581 |
+
raise ValueError(f"File format {repr(str1)} not understood. Only "
|
| 582 |
+
"'RIFF', 'RIFX', and 'RF64' supported.")
|
| 583 |
+
# Size of entire file
|
| 584 |
+
if not is_rf64:
|
| 585 |
+
file_size = struct.unpack(fmt, fid.read(4))[0] + 8
|
| 586 |
+
rf64_chunk_size = None
|
| 587 |
+
str2 = fid.read(4)
|
| 588 |
+
else:
|
| 589 |
+
# Skip 0xFFFFFFFF (-1) bytes
|
| 590 |
+
fid.read(4)
|
| 591 |
+
str2 = fid.read(4)
|
| 592 |
+
str3 = fid.read(4)
|
| 593 |
+
if str3 != b'ds64':
|
| 594 |
+
raise ValueError("Invalid RF64 file: ds64 chunk not found.")
|
| 595 |
+
ds64_size = struct.unpack("<I", fid.read(4))[0]
|
| 596 |
+
file_size = struct.unpack(fmt, fid.read(8))[0] + 8
|
| 597 |
+
rf64_chunk_size = struct.unpack('<Q', fid.read(8))[0]
|
| 598 |
+
# Ignore additional attributes of ds64 chunk like sample count, tables, etc.
|
| 599 |
+
# and just skip to the next chunk
|
| 600 |
+
fid.seek(ds64_size - 16, 1)
|
| 601 |
+
|
| 602 |
+
if str2 != b'WAVE':
|
| 603 |
+
raise ValueError(f"Not a WAV file. RIFF form type is {repr(str2)}.")
|
| 604 |
+
|
| 605 |
+
return file_size, is_big_endian, is_rf64, rf64_chunk_size
|
| 606 |
+
|
| 607 |
+
|
| 608 |
+
def _handle_pad_byte(fid, size):
|
| 609 |
+
# "If the chunk size is an odd number of bytes, a pad byte with value zero
|
| 610 |
+
# is written after ckData." So we need to seek past this after each chunk.
|
| 611 |
+
if size % 2:
|
| 612 |
+
fid.seek(1, 1)
|
| 613 |
+
|
| 614 |
+
|
| 615 |
+
def read(filename, mmap=False):
|
| 616 |
+
"""
|
| 617 |
+
Open a WAV file.
|
| 618 |
+
|
| 619 |
+
Return the sample rate (in samples/sec) and data from an LPCM WAV file.
|
| 620 |
+
|
| 621 |
+
Parameters
|
| 622 |
+
----------
|
| 623 |
+
filename : string or open file handle
|
| 624 |
+
Input WAV file.
|
| 625 |
+
mmap : bool, optional
|
| 626 |
+
Whether to read data as memory-mapped (default: False). Not compatible
|
| 627 |
+
with some bit depths; see Notes. Only to be used on real files.
|
| 628 |
+
|
| 629 |
+
.. versionadded:: 0.12.0
|
| 630 |
+
|
| 631 |
+
Returns
|
| 632 |
+
-------
|
| 633 |
+
rate : int
|
| 634 |
+
Sample rate of WAV file.
|
| 635 |
+
data : numpy array
|
| 636 |
+
Data read from WAV file. Data-type is determined from the file;
|
| 637 |
+
see Notes. Data is 1-D for 1-channel WAV, or 2-D of shape
|
| 638 |
+
(Nsamples, Nchannels) otherwise. If a file-like input without a
|
| 639 |
+
C-like file descriptor (e.g., :class:`python:io.BytesIO`) is
|
| 640 |
+
passed, this will not be writeable.
|
| 641 |
+
|
| 642 |
+
Notes
|
| 643 |
+
-----
|
| 644 |
+
Common data types: [1]_
|
| 645 |
+
|
| 646 |
+
===================== =========== =========== =============
|
| 647 |
+
WAV format Min Max NumPy dtype
|
| 648 |
+
===================== =========== =========== =============
|
| 649 |
+
32-bit floating-point -1.0 +1.0 float32
|
| 650 |
+
32-bit integer PCM -2147483648 +2147483647 int32
|
| 651 |
+
24-bit integer PCM -2147483648 +2147483392 int32
|
| 652 |
+
16-bit integer PCM -32768 +32767 int16
|
| 653 |
+
8-bit integer PCM 0 255 uint8
|
| 654 |
+
===================== =========== =========== =============
|
| 655 |
+
|
| 656 |
+
WAV files can specify arbitrary bit depth, and this function supports
|
| 657 |
+
reading any integer PCM depth from 1 to 64 bits. Data is returned in the
|
| 658 |
+
smallest compatible numpy int type, in left-justified format. 8-bit and
|
| 659 |
+
lower is unsigned, while 9-bit and higher is signed.
|
| 660 |
+
|
| 661 |
+
For example, 24-bit data will be stored as int32, with the MSB of the
|
| 662 |
+
24-bit data stored at the MSB of the int32, and typically the least
|
| 663 |
+
significant byte is 0x00. (However, if a file actually contains data past
|
| 664 |
+
its specified bit depth, those bits will be read and output, too. [2]_)
|
| 665 |
+
|
| 666 |
+
This bit justification and sign matches WAV's native internal format, which
|
| 667 |
+
allows memory mapping of WAV files that use 1, 2, 4, or 8 bytes per sample
|
| 668 |
+
(so 24-bit files cannot be memory-mapped, but 32-bit can).
|
| 669 |
+
|
| 670 |
+
IEEE float PCM in 32- or 64-bit format is supported, with or without mmap.
|
| 671 |
+
Values exceeding [-1, +1] are not clipped.
|
| 672 |
+
|
| 673 |
+
Non-linear PCM (mu-law, A-law) is not supported.
|
| 674 |
+
|
| 675 |
+
References
|
| 676 |
+
----------
|
| 677 |
+
.. [1] IBM Corporation and Microsoft Corporation, "Multimedia Programming
|
| 678 |
+
Interface and Data Specifications 1.0", section "Data Format of the
|
| 679 |
+
Samples", August 1991
|
| 680 |
+
http://www.tactilemedia.com/info/MCI_Control_Info.html
|
| 681 |
+
.. [2] Adobe Systems Incorporated, "Adobe Audition 3 User Guide", section
|
| 682 |
+
"Audio file formats: 24-bit Packed Int (type 1, 20-bit)", 2007
|
| 683 |
+
|
| 684 |
+
Examples
|
| 685 |
+
--------
|
| 686 |
+
>>> from os.path import dirname, join as pjoin
|
| 687 |
+
>>> from scipy.io import wavfile
|
| 688 |
+
>>> import scipy.io
|
| 689 |
+
|
| 690 |
+
Get the filename for an example .wav file from the tests/data directory.
|
| 691 |
+
|
| 692 |
+
>>> data_dir = pjoin(dirname(scipy.io.__file__), 'tests', 'data')
|
| 693 |
+
>>> wav_fname = pjoin(data_dir, 'test-44100Hz-2ch-32bit-float-be.wav')
|
| 694 |
+
|
| 695 |
+
Load the .wav file contents.
|
| 696 |
+
|
| 697 |
+
>>> samplerate, data = wavfile.read(wav_fname)
|
| 698 |
+
>>> print(f"number of channels = {data.shape[1]}")
|
| 699 |
+
number of channels = 2
|
| 700 |
+
>>> length = data.shape[0] / samplerate
|
| 701 |
+
>>> print(f"length = {length}s")
|
| 702 |
+
length = 0.01s
|
| 703 |
+
|
| 704 |
+
Plot the waveform.
|
| 705 |
+
|
| 706 |
+
>>> import matplotlib.pyplot as plt
|
| 707 |
+
>>> import numpy as np
|
| 708 |
+
>>> time = np.linspace(0., length, data.shape[0])
|
| 709 |
+
>>> plt.plot(time, data[:, 0], label="Left channel")
|
| 710 |
+
>>> plt.plot(time, data[:, 1], label="Right channel")
|
| 711 |
+
>>> plt.legend()
|
| 712 |
+
>>> plt.xlabel("Time [s]")
|
| 713 |
+
>>> plt.ylabel("Amplitude")
|
| 714 |
+
>>> plt.show()
|
| 715 |
+
|
| 716 |
+
"""
|
| 717 |
+
if hasattr(filename, 'read'):
|
| 718 |
+
fid = filename
|
| 719 |
+
mmap = False
|
| 720 |
+
else:
|
| 721 |
+
fid = open(filename, 'rb')
|
| 722 |
+
|
| 723 |
+
if not (was_seekable := fid.seekable()):
|
| 724 |
+
fid = SeekEmulatingReader(fid)
|
| 725 |
+
|
| 726 |
+
try:
|
| 727 |
+
file_size, is_big_endian, is_rf64, rf64_chunk_size = _read_riff_chunk(fid)
|
| 728 |
+
fmt_chunk_received = False
|
| 729 |
+
data_chunk_received = False
|
| 730 |
+
while fid.tell() < file_size:
|
| 731 |
+
# read the next chunk
|
| 732 |
+
chunk_id = fid.read(4)
|
| 733 |
+
|
| 734 |
+
if not chunk_id:
|
| 735 |
+
if data_chunk_received:
|
| 736 |
+
# End of file but data successfully read
|
| 737 |
+
warnings.warn(
|
| 738 |
+
f"Reached EOF prematurely; finished at {fid.tell():d} bytes, "
|
| 739 |
+
f"expected {file_size:d} bytes from header.",
|
| 740 |
+
WavFileWarning, stacklevel=2)
|
| 741 |
+
break
|
| 742 |
+
else:
|
| 743 |
+
raise ValueError("Unexpected end of file.")
|
| 744 |
+
elif len(chunk_id) < 4:
|
| 745 |
+
msg = f"Incomplete chunk ID: {repr(chunk_id)}"
|
| 746 |
+
# If we have the data, ignore the broken chunk
|
| 747 |
+
if fmt_chunk_received and data_chunk_received:
|
| 748 |
+
warnings.warn(msg + ", ignoring it.", WavFileWarning,
|
| 749 |
+
stacklevel=2)
|
| 750 |
+
else:
|
| 751 |
+
raise ValueError(msg)
|
| 752 |
+
|
| 753 |
+
if chunk_id == b'fmt ':
|
| 754 |
+
fmt_chunk_received = True
|
| 755 |
+
fmt_chunk = _read_fmt_chunk(fid, is_big_endian)
|
| 756 |
+
format_tag, channels, fs = fmt_chunk[1:4]
|
| 757 |
+
bit_depth = fmt_chunk[6]
|
| 758 |
+
block_align = fmt_chunk[5]
|
| 759 |
+
elif chunk_id == b'fact':
|
| 760 |
+
_skip_unknown_chunk(fid, is_big_endian)
|
| 761 |
+
elif chunk_id == b'data':
|
| 762 |
+
data_chunk_received = True
|
| 763 |
+
if not fmt_chunk_received:
|
| 764 |
+
raise ValueError("No fmt chunk before data")
|
| 765 |
+
data = _read_data_chunk(fid, format_tag, channels, bit_depth,
|
| 766 |
+
is_big_endian, is_rf64, block_align,
|
| 767 |
+
mmap, rf64_chunk_size)
|
| 768 |
+
elif chunk_id == b'LIST':
|
| 769 |
+
# Someday this could be handled properly but for now skip it
|
| 770 |
+
_skip_unknown_chunk(fid, is_big_endian)
|
| 771 |
+
elif chunk_id in {b'JUNK', b'Fake'}:
|
| 772 |
+
# Skip alignment chunks without warning
|
| 773 |
+
_skip_unknown_chunk(fid, is_big_endian)
|
| 774 |
+
else:
|
| 775 |
+
warnings.warn("Chunk (non-data) not understood, skipping it.",
|
| 776 |
+
WavFileWarning, stacklevel=2)
|
| 777 |
+
_skip_unknown_chunk(fid, is_big_endian)
|
| 778 |
+
finally:
|
| 779 |
+
if not hasattr(filename, 'read'):
|
| 780 |
+
fid.close()
|
| 781 |
+
elif was_seekable:
|
| 782 |
+
# Rewind, if we are able, so that caller can do something
|
| 783 |
+
# else with the raw WAV stream.
|
| 784 |
+
fid.seek(0)
|
| 785 |
+
|
| 786 |
+
return fs, data
|
| 787 |
+
|
| 788 |
+
|
| 789 |
+
def write(filename, rate, data):
|
| 790 |
+
"""
|
| 791 |
+
Write a NumPy array as a WAV file.
|
| 792 |
+
|
| 793 |
+
Parameters
|
| 794 |
+
----------
|
| 795 |
+
filename : string or open file handle
|
| 796 |
+
Output wav file.
|
| 797 |
+
rate : int
|
| 798 |
+
The sample rate (in samples/sec).
|
| 799 |
+
data : ndarray
|
| 800 |
+
A 1-D or 2-D NumPy array of either integer or float data-type.
|
| 801 |
+
|
| 802 |
+
Notes
|
| 803 |
+
-----
|
| 804 |
+
* Writes a simple uncompressed WAV file.
|
| 805 |
+
* To write multiple-channels, use a 2-D array of shape
|
| 806 |
+
(Nsamples, Nchannels).
|
| 807 |
+
* The bits-per-sample and PCM/float will be determined by the data-type.
|
| 808 |
+
|
| 809 |
+
Common data types: [1]_
|
| 810 |
+
|
| 811 |
+
===================== =========== =========== =============
|
| 812 |
+
WAV format Min Max NumPy dtype
|
| 813 |
+
===================== =========== =========== =============
|
| 814 |
+
32-bit floating-point -1.0 +1.0 float32
|
| 815 |
+
32-bit PCM -2147483648 +2147483647 int32
|
| 816 |
+
16-bit PCM -32768 +32767 int16
|
| 817 |
+
8-bit PCM 0 255 uint8
|
| 818 |
+
===================== =========== =========== =============
|
| 819 |
+
|
| 820 |
+
Note that 8-bit PCM is unsigned.
|
| 821 |
+
|
| 822 |
+
References
|
| 823 |
+
----------
|
| 824 |
+
.. [1] IBM Corporation and Microsoft Corporation, "Multimedia Programming
|
| 825 |
+
Interface and Data Specifications 1.0", section "Data Format of the
|
| 826 |
+
Samples", August 1991
|
| 827 |
+
http://www.tactilemedia.com/info/MCI_Control_Info.html
|
| 828 |
+
|
| 829 |
+
Examples
|
| 830 |
+
--------
|
| 831 |
+
Create a 100Hz sine wave, sampled at 44100Hz.
|
| 832 |
+
Write to 16-bit PCM, Mono.
|
| 833 |
+
|
| 834 |
+
>>> from scipy.io.wavfile import write
|
| 835 |
+
>>> import numpy as np
|
| 836 |
+
>>> samplerate = 44100; fs = 100
|
| 837 |
+
>>> t = np.linspace(0., 1., samplerate)
|
| 838 |
+
>>> amplitude = np.iinfo(np.int16).max
|
| 839 |
+
>>> data = amplitude * np.sin(2. * np.pi * fs * t)
|
| 840 |
+
>>> write("example.wav", samplerate, data.astype(np.int16))
|
| 841 |
+
|
| 842 |
+
"""
|
| 843 |
+
if hasattr(filename, 'write'):
|
| 844 |
+
fid = filename
|
| 845 |
+
else:
|
| 846 |
+
fid = open(filename, 'wb')
|
| 847 |
+
|
| 848 |
+
fs = rate
|
| 849 |
+
|
| 850 |
+
try:
|
| 851 |
+
dkind = data.dtype.kind
|
| 852 |
+
allowed_dtypes = ['float32', 'float64',
|
| 853 |
+
'uint8', 'int16', 'int32', 'int64']
|
| 854 |
+
if data.dtype.name not in allowed_dtypes:
|
| 855 |
+
raise ValueError(f"Unsupported data type '{data.dtype}'")
|
| 856 |
+
|
| 857 |
+
header_data = b''
|
| 858 |
+
|
| 859 |
+
header_data += b'RIFF'
|
| 860 |
+
header_data += b'\x00\x00\x00\x00'
|
| 861 |
+
header_data += b'WAVE'
|
| 862 |
+
|
| 863 |
+
# fmt chunk
|
| 864 |
+
header_data += b'fmt '
|
| 865 |
+
if dkind == 'f':
|
| 866 |
+
format_tag = WAVE_FORMAT.IEEE_FLOAT
|
| 867 |
+
else:
|
| 868 |
+
format_tag = WAVE_FORMAT.PCM
|
| 869 |
+
if data.ndim == 1:
|
| 870 |
+
channels = 1
|
| 871 |
+
else:
|
| 872 |
+
channels = data.shape[1]
|
| 873 |
+
bit_depth = data.dtype.itemsize * 8
|
| 874 |
+
bytes_per_second = fs*(bit_depth // 8)*channels
|
| 875 |
+
block_align = channels * (bit_depth // 8)
|
| 876 |
+
|
| 877 |
+
fmt_chunk_data = struct.pack('<HHIIHH', format_tag, channels, fs,
|
| 878 |
+
bytes_per_second, block_align, bit_depth)
|
| 879 |
+
if not (dkind == 'i' or dkind == 'u'):
|
| 880 |
+
# add cbSize field for non-PCM files
|
| 881 |
+
fmt_chunk_data += b'\x00\x00'
|
| 882 |
+
|
| 883 |
+
header_data += struct.pack('<I', len(fmt_chunk_data))
|
| 884 |
+
header_data += fmt_chunk_data
|
| 885 |
+
|
| 886 |
+
# check data size (needs to be immediately before the data chunk)
|
| 887 |
+
# if too large for standard RIFF, use RF64 instead
|
| 888 |
+
resulting_file_size = len(header_data) + 4 + 4 + data.nbytes
|
| 889 |
+
is_rf64 = (resulting_file_size - 8) > 0xFFFFFFFF
|
| 890 |
+
if is_rf64:
|
| 891 |
+
header_data = b''
|
| 892 |
+
header_data += b'RF64'
|
| 893 |
+
header_data += b'\xFF\xFF\xFF\xFF'
|
| 894 |
+
header_data += b'WAVE'
|
| 895 |
+
header_data += b'ds64'
|
| 896 |
+
# size of ds64 chunk
|
| 897 |
+
header_data += struct.pack('<I', 28)
|
| 898 |
+
# will be filled later with real file size
|
| 899 |
+
header_data += struct.pack('<Q', 0)
|
| 900 |
+
header_data += struct.pack('<Q', data.nbytes)
|
| 901 |
+
header_data += struct.pack('<Q', data.shape[0])
|
| 902 |
+
# ignore 'table' field for now
|
| 903 |
+
header_data += struct.pack('<I', 0)
|
| 904 |
+
header_data += b'fmt '
|
| 905 |
+
header_data += struct.pack('<I', len(fmt_chunk_data))
|
| 906 |
+
header_data += fmt_chunk_data
|
| 907 |
+
|
| 908 |
+
# fact chunk (non-PCM files)
|
| 909 |
+
if not (dkind == 'i' or dkind == 'u'):
|
| 910 |
+
header_data += b'fact'
|
| 911 |
+
header_data += struct.pack('<II', 4, data.shape[0])
|
| 912 |
+
|
| 913 |
+
fid.write(header_data)
|
| 914 |
+
|
| 915 |
+
# data chunk
|
| 916 |
+
fid.write(b'data')
|
| 917 |
+
# write data chunk size, unless its too big in which case 0xFFFFFFFF is written
|
| 918 |
+
fid.write(struct.pack('<I', min(data.nbytes, 4294967295)))
|
| 919 |
+
|
| 920 |
+
if data.dtype.byteorder == '>' or (data.dtype.byteorder == '=' and
|
| 921 |
+
sys.byteorder == 'big'):
|
| 922 |
+
data = data.byteswap()
|
| 923 |
+
_array_tofile(fid, data)
|
| 924 |
+
|
| 925 |
+
# Determine file size and place it in correct
|
| 926 |
+
# position at start of the file or the data chunk.
|
| 927 |
+
size = fid.tell()
|
| 928 |
+
if not is_rf64:
|
| 929 |
+
fid.seek(4)
|
| 930 |
+
fid.write(struct.pack('<I', size-8))
|
| 931 |
+
else:
|
| 932 |
+
fid.seek(20)
|
| 933 |
+
fid.write(struct.pack('<Q', size-8))
|
| 934 |
+
|
| 935 |
+
finally:
|
| 936 |
+
if not hasattr(filename, 'write'):
|
| 937 |
+
fid.close()
|
| 938 |
+
else:
|
| 939 |
+
fid.seek(0)
|
| 940 |
+
|
| 941 |
+
|
| 942 |
+
def _array_tofile(fid, data):
|
| 943 |
+
# ravel gives a c-contiguous buffer
|
| 944 |
+
fid.write(data.ravel().view('b').data)
|
loan-advisor-agent/.venv/Lib/site-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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
====================================
|
| 3 |
+
Linear algebra (:mod:`scipy.linalg`)
|
| 4 |
+
====================================
|
| 5 |
+
|
| 6 |
+
.. currentmodule:: scipy.linalg
|
| 7 |
+
|
| 8 |
+
.. toctree::
|
| 9 |
+
:hidden:
|
| 10 |
+
|
| 11 |
+
linalg.blas
|
| 12 |
+
linalg.cython_blas
|
| 13 |
+
linalg.cython_lapack
|
| 14 |
+
linalg.interpolative
|
| 15 |
+
linalg.lapack
|
| 16 |
+
|
| 17 |
+
Linear algebra functions.
|
| 18 |
+
|
| 19 |
+
.. eventually, we should replace the numpy.linalg HTML link with just `numpy.linalg`
|
| 20 |
+
|
| 21 |
+
.. seealso::
|
| 22 |
+
|
| 23 |
+
`numpy.linalg <https://www.numpy.org/devdocs/reference/routines.linalg.html>`__
|
| 24 |
+
for more linear algebra functions. Note that identically named
|
| 25 |
+
functions from `scipy.linalg` may offer more or slightly differing
|
| 26 |
+
functionality.
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
Basics
|
| 30 |
+
======
|
| 31 |
+
|
| 32 |
+
.. autosummary::
|
| 33 |
+
:toctree: generated/
|
| 34 |
+
|
| 35 |
+
inv - Find the inverse of a square matrix
|
| 36 |
+
solve - Solve a linear system of equations
|
| 37 |
+
solve_banded - Solve a banded linear system
|
| 38 |
+
solveh_banded - Solve a Hermitian or symmetric banded system
|
| 39 |
+
solve_circulant - Solve a circulant system
|
| 40 |
+
solve_triangular - Solve a triangular matrix
|
| 41 |
+
solve_toeplitz - Solve a toeplitz matrix
|
| 42 |
+
matmul_toeplitz - Multiply a Toeplitz matrix with an array.
|
| 43 |
+
det - Find the determinant of a square matrix
|
| 44 |
+
norm - Matrix and vector norm
|
| 45 |
+
lstsq - Solve a linear least-squares problem
|
| 46 |
+
pinv - Pseudo-inverse (Moore-Penrose) using lstsq
|
| 47 |
+
pinvh - Pseudo-inverse of hermitian matrix
|
| 48 |
+
khatri_rao - Khatri-Rao product of two arrays
|
| 49 |
+
orthogonal_procrustes - Solve an orthogonal Procrustes problem
|
| 50 |
+
matrix_balance - Balance matrix entries with a similarity transformation
|
| 51 |
+
subspace_angles - Compute the subspace angles between two matrices
|
| 52 |
+
bandwidth - Return the lower and upper bandwidth of an array
|
| 53 |
+
issymmetric - Check if a square 2D array is symmetric
|
| 54 |
+
ishermitian - Check if a square 2D array is Hermitian
|
| 55 |
+
LinAlgError
|
| 56 |
+
LinAlgWarning
|
| 57 |
+
|
| 58 |
+
Eigenvalue Problems
|
| 59 |
+
===================
|
| 60 |
+
|
| 61 |
+
.. autosummary::
|
| 62 |
+
:toctree: generated/
|
| 63 |
+
|
| 64 |
+
eig - Find the eigenvalues and eigenvectors of a square matrix
|
| 65 |
+
eigvals - Find just the eigenvalues of a square matrix
|
| 66 |
+
eigh - Find the e-vals and e-vectors of a Hermitian or symmetric matrix
|
| 67 |
+
eigvalsh - Find just the eigenvalues of a Hermitian or symmetric matrix
|
| 68 |
+
eig_banded - Find the eigenvalues and eigenvectors of a banded matrix
|
| 69 |
+
eigvals_banded - Find just the eigenvalues of a banded matrix
|
| 70 |
+
eigh_tridiagonal - Find the eigenvalues and eigenvectors of a tridiagonal matrix
|
| 71 |
+
eigvalsh_tridiagonal - Find just the eigenvalues of a tridiagonal matrix
|
| 72 |
+
|
| 73 |
+
Decompositions
|
| 74 |
+
==============
|
| 75 |
+
|
| 76 |
+
.. autosummary::
|
| 77 |
+
:toctree: generated/
|
| 78 |
+
|
| 79 |
+
lu - LU decomposition of a matrix
|
| 80 |
+
lu_factor - LU decomposition returning unordered matrix and pivots
|
| 81 |
+
lu_solve - Solve Ax=b using back substitution with output of lu_factor
|
| 82 |
+
svd - Singular value decomposition of a matrix
|
| 83 |
+
svdvals - Singular values of a matrix
|
| 84 |
+
diagsvd - Construct matrix of singular values from output of svd
|
| 85 |
+
orth - Construct orthonormal basis for the range of A using svd
|
| 86 |
+
null_space - Construct orthonormal basis for the null space of A using svd
|
| 87 |
+
ldl - LDL.T decomposition of a Hermitian or a symmetric matrix.
|
| 88 |
+
cholesky - Cholesky decomposition of a matrix
|
| 89 |
+
cholesky_banded - Cholesky decomp. of a sym. or Hermitian banded matrix
|
| 90 |
+
cho_factor - Cholesky decomposition for use in solving a linear system
|
| 91 |
+
cho_solve - Solve previously factored linear system
|
| 92 |
+
cho_solve_banded - Solve previously factored banded linear system
|
| 93 |
+
polar - Compute the polar decomposition.
|
| 94 |
+
qr - QR decomposition of a matrix
|
| 95 |
+
qr_multiply - QR decomposition and multiplication by Q
|
| 96 |
+
qr_update - Rank k QR update
|
| 97 |
+
qr_delete - QR downdate on row or column deletion
|
| 98 |
+
qr_insert - QR update on row or column insertion
|
| 99 |
+
rq - RQ decomposition of a matrix
|
| 100 |
+
qz - QZ decomposition of a pair of matrices
|
| 101 |
+
ordqz - QZ decomposition of a pair of matrices with reordering
|
| 102 |
+
schur - Schur decomposition of a matrix
|
| 103 |
+
rsf2csf - Real to complex Schur form
|
| 104 |
+
hessenberg - Hessenberg form of a matrix
|
| 105 |
+
cdf2rdf - Complex diagonal form to real diagonal block form
|
| 106 |
+
cossin - Cosine sine decomposition of a unitary or orthogonal matrix
|
| 107 |
+
|
| 108 |
+
.. seealso::
|
| 109 |
+
|
| 110 |
+
`scipy.linalg.interpolative` -- Interpolative matrix decompositions
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
Matrix Functions
|
| 114 |
+
================
|
| 115 |
+
|
| 116 |
+
.. autosummary::
|
| 117 |
+
:toctree: generated/
|
| 118 |
+
|
| 119 |
+
expm - Matrix exponential
|
| 120 |
+
logm - Matrix logarithm
|
| 121 |
+
cosm - Matrix cosine
|
| 122 |
+
sinm - Matrix sine
|
| 123 |
+
tanm - Matrix tangent
|
| 124 |
+
coshm - Matrix hyperbolic cosine
|
| 125 |
+
sinhm - Matrix hyperbolic sine
|
| 126 |
+
tanhm - Matrix hyperbolic tangent
|
| 127 |
+
signm - Matrix sign
|
| 128 |
+
sqrtm - Matrix square root
|
| 129 |
+
funm - Evaluating an arbitrary matrix function
|
| 130 |
+
expm_frechet - Frechet derivative of the matrix exponential
|
| 131 |
+
expm_cond - Relative condition number of expm in the Frobenius norm
|
| 132 |
+
fractional_matrix_power - Fractional matrix power
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
Matrix Equation Solvers
|
| 136 |
+
=======================
|
| 137 |
+
|
| 138 |
+
.. autosummary::
|
| 139 |
+
:toctree: generated/
|
| 140 |
+
|
| 141 |
+
solve_sylvester - Solve the Sylvester matrix equation
|
| 142 |
+
solve_continuous_are - Solve the continuous-time algebraic Riccati equation
|
| 143 |
+
solve_discrete_are - Solve the discrete-time algebraic Riccati equation
|
| 144 |
+
solve_continuous_lyapunov - Solve the continuous-time Lyapunov equation
|
| 145 |
+
solve_discrete_lyapunov - Solve the discrete-time Lyapunov equation
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
Sketches and Random Projections
|
| 149 |
+
===============================
|
| 150 |
+
|
| 151 |
+
.. autosummary::
|
| 152 |
+
:toctree: generated/
|
| 153 |
+
|
| 154 |
+
clarkson_woodruff_transform - Applies the Clarkson Woodruff Sketch (a.k.a CountMin Sketch)
|
| 155 |
+
|
| 156 |
+
Special Matrices
|
| 157 |
+
================
|
| 158 |
+
|
| 159 |
+
.. autosummary::
|
| 160 |
+
:toctree: generated/
|
| 161 |
+
|
| 162 |
+
block_diag - Construct a block diagonal matrix from submatrices
|
| 163 |
+
circulant - Circulant matrix
|
| 164 |
+
companion - Companion matrix
|
| 165 |
+
convolution_matrix - Convolution matrix
|
| 166 |
+
dft - Discrete Fourier transform matrix
|
| 167 |
+
fiedler - Fiedler matrix
|
| 168 |
+
fiedler_companion - Fiedler companion matrix
|
| 169 |
+
hadamard - Hadamard matrix of order 2**n
|
| 170 |
+
hankel - Hankel matrix
|
| 171 |
+
helmert - Helmert matrix
|
| 172 |
+
hilbert - Hilbert matrix
|
| 173 |
+
invhilbert - Inverse Hilbert matrix
|
| 174 |
+
leslie - Leslie matrix
|
| 175 |
+
pascal - Pascal matrix
|
| 176 |
+
invpascal - Inverse Pascal matrix
|
| 177 |
+
toeplitz - Toeplitz matrix
|
| 178 |
+
|
| 179 |
+
Low-level routines
|
| 180 |
+
==================
|
| 181 |
+
|
| 182 |
+
.. autosummary::
|
| 183 |
+
:toctree: generated/
|
| 184 |
+
|
| 185 |
+
get_blas_funcs
|
| 186 |
+
get_lapack_funcs
|
| 187 |
+
find_best_blas_type
|
| 188 |
+
|
| 189 |
+
.. seealso::
|
| 190 |
+
|
| 191 |
+
`scipy.linalg.blas` -- Low-level BLAS functions
|
| 192 |
+
|
| 193 |
+
`scipy.linalg.lapack` -- Low-level LAPACK functions
|
| 194 |
+
|
| 195 |
+
`scipy.linalg.cython_blas` -- Low-level BLAS functions for Cython
|
| 196 |
+
|
| 197 |
+
`scipy.linalg.cython_lapack` -- Low-level LAPACK functions for Cython
|
| 198 |
+
|
| 199 |
+
""" # noqa: E501
|
| 200 |
+
|
| 201 |
+
from ._misc import *
|
| 202 |
+
from ._cythonized_array_utils import *
|
| 203 |
+
from ._basic import *
|
| 204 |
+
from ._decomp import *
|
| 205 |
+
from ._decomp_lu import *
|
| 206 |
+
from ._decomp_ldl import *
|
| 207 |
+
from ._decomp_cholesky import *
|
| 208 |
+
from ._decomp_qr import *
|
| 209 |
+
from ._decomp_qz import *
|
| 210 |
+
from ._decomp_svd import *
|
| 211 |
+
from ._decomp_schur import *
|
| 212 |
+
from ._decomp_polar import *
|
| 213 |
+
from ._matfuncs import *
|
| 214 |
+
from .blas import *
|
| 215 |
+
from .lapack import *
|
| 216 |
+
from ._special_matrices import *
|
| 217 |
+
from ._solvers import *
|
| 218 |
+
from ._procrustes import *
|
| 219 |
+
from ._decomp_update import *
|
| 220 |
+
from ._sketches import *
|
| 221 |
+
from ._decomp_cossin import *
|
| 222 |
+
|
| 223 |
+
# Deprecated namespaces, to be removed in v2.0.0
|
| 224 |
+
from . import (
|
| 225 |
+
decomp, decomp_cholesky, decomp_lu, decomp_qr, decomp_svd, decomp_schur,
|
| 226 |
+
basic, misc, special_matrices, matfuncs,
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
__all__ = [s for s in dir() if not s.startswith('_')]
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
from scipy._lib._testutils import PytestTester
|
| 233 |
+
test = PytestTester(__name__)
|
| 234 |
+
del PytestTester
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (7.78 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_basic.cpython-312.pyc
ADDED
|
Binary file (95.1 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp.cpython-312.pyc
ADDED
|
Binary file (63.5 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_cholesky.cpython-312.pyc
ADDED
|
Binary file (16.6 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_cossin.cpython-312.pyc
ADDED
|
Binary file (11.7 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_ldl.cpython-312.pyc
ADDED
|
Binary file (13.1 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_lu.cpython-312.pyc
ADDED
|
Binary file (15.3 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_polar.cpython-312.pyc
ADDED
|
Binary file (4.32 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_qr.cpython-312.pyc
ADDED
|
Binary file (17.3 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_qz.cpython-312.pyc
ADDED
|
Binary file (17.6 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_schur.cpython-312.pyc
ADDED
|
Binary file (15.6 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_decomp_svd.cpython-312.pyc
ADDED
|
Binary file (19.4 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_expm_frechet.cpython-312.pyc
ADDED
|
Binary file (19.2 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_matfuncs.cpython-312.pyc
ADDED
|
Binary file (37.6 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_misc.cpython-312.pyc
ADDED
|
Binary file (6.77 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_procrustes.cpython-312.pyc
ADDED
|
Binary file (4.58 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_sketches.cpython-312.pyc
ADDED
|
Binary file (7.47 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_solvers.cpython-312.pyc
ADDED
|
Binary file (35.7 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/_special_matrices.cpython-312.pyc
ADDED
|
Binary file (48 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/basic.cpython-312.pyc
ADDED
|
Binary file (859 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/blas.cpython-312.pyc
ADDED
|
Binary file (11.9 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp.cpython-312.pyc
ADDED
|
Binary file (822 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp_cholesky.cpython-312.pyc
ADDED
|
Binary file (794 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp_lu.cpython-312.pyc
ADDED
|
Binary file (733 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp_qr.cpython-312.pyc
ADDED
|
Binary file (714 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp_schur.cpython-312.pyc
ADDED
|
Binary file (747 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/decomp_svd.cpython-312.pyc
ADDED
|
Binary file (767 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/interpolative.cpython-312.pyc
ADDED
|
Binary file (32.2 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/lapack.cpython-312.pyc
ADDED
|
Binary file (18.4 kB). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/matfuncs.cpython-312.pyc
ADDED
|
Binary file (844 Bytes). View file
|
|
|
loan-advisor-agent/.venv/Lib/site-packages/scipy/linalg/__pycache__/misc.cpython-312.pyc
ADDED
|
Binary file (728 Bytes). View file
|
|
|