test_paths.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. Ensure that we can use pathlib.Path objects in all relevant IO functions.
  3. """
  4. import sys
  5. try:
  6. from pathlib import Path
  7. except ImportError:
  8. # Not available. No fallback import, since we'll skip the entire
  9. # test suite for Python < 3.6.
  10. pass
  11. import numpy as np
  12. from numpy.testing import assert_
  13. import pytest
  14. import scipy.io
  15. import scipy.io.wavfile
  16. from scipy._lib._tmpdirs import tempdir
  17. import scipy.sparse
  18. @pytest.mark.skipif(sys.version_info < (3, 6),
  19. reason='Passing path-like objects to IO functions requires Python >= 3.6')
  20. class TestPaths(object):
  21. data = np.arange(5).astype(np.int64)
  22. def test_savemat(self):
  23. with tempdir() as temp_dir:
  24. path = Path(temp_dir) / 'data.mat'
  25. scipy.io.savemat(path, {'data': self.data})
  26. assert_(path.is_file())
  27. def test_loadmat(self):
  28. # Save data with string path, load with pathlib.Path
  29. with tempdir() as temp_dir:
  30. path = Path(temp_dir) / 'data.mat'
  31. scipy.io.savemat(str(path), {'data': self.data})
  32. mat_contents = scipy.io.loadmat(path)
  33. assert_((mat_contents['data'] == self.data).all())
  34. def test_whosmat(self):
  35. # Save data with string path, load with pathlib.Path
  36. with tempdir() as temp_dir:
  37. path = Path(temp_dir) / 'data.mat'
  38. scipy.io.savemat(str(path), {'data': self.data})
  39. contents = scipy.io.whosmat(path)
  40. assert_(contents[0] == ('data', (1, 5), 'int64'))
  41. def test_readsav(self):
  42. path = Path(__file__).parent / 'data/scalar_string.sav'
  43. scipy.io.readsav(path)
  44. def test_hb_read(self):
  45. # Save data with string path, load with pathlib.Path
  46. with tempdir() as temp_dir:
  47. data = scipy.sparse.csr_matrix(scipy.sparse.eye(3))
  48. path = Path(temp_dir) / 'data.hb'
  49. scipy.io.harwell_boeing.hb_write(str(path), data)
  50. data_new = scipy.io.harwell_boeing.hb_read(path)
  51. assert_((data_new != data).nnz == 0)
  52. def test_hb_write(self):
  53. with tempdir() as temp_dir:
  54. data = scipy.sparse.csr_matrix(scipy.sparse.eye(3))
  55. path = Path(temp_dir) / 'data.hb'
  56. scipy.io.harwell_boeing.hb_write(path, data)
  57. assert_(path.is_file())
  58. def test_netcdf_file(self):
  59. path = Path(__file__).parent / 'data/example_1.nc'
  60. scipy.io.netcdf.netcdf_file(path)
  61. def test_wavfile_read(self):
  62. path = Path(__file__).parent / 'data/test-8000Hz-le-2ch-1byteu.wav'
  63. scipy.io.wavfile.read(path)
  64. def test_wavfile_write(self):
  65. # Read from str path, write to Path
  66. input_path = Path(__file__).parent / 'data/test-8000Hz-le-2ch-1byteu.wav'
  67. rate, data = scipy.io.wavfile.read(str(input_path))
  68. with tempdir() as temp_dir:
  69. output_path = Path(temp_dir) / input_path.name
  70. scipy.io.wavfile.write(output_path, rate, data)