test_paths.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """
  2. Ensure that we can use pathlib.Path objects in all relevant IO functions.
  3. """
  4. from pathlib import Path
  5. import numpy as np
  6. import scipy.io
  7. import scipy.io.wavfile
  8. from scipy._lib._tmpdirs import tempdir
  9. import scipy.sparse
  10. class TestPaths:
  11. data = np.arange(5).astype(np.int64)
  12. def test_savemat(self):
  13. with tempdir() as temp_dir:
  14. path = Path(temp_dir) / 'data.mat'
  15. scipy.io.savemat(path, {'data': self.data})
  16. assert path.is_file()
  17. def test_loadmat(self):
  18. # Save data with string path, load with pathlib.Path
  19. with tempdir() as temp_dir:
  20. path = Path(temp_dir) / 'data.mat'
  21. scipy.io.savemat(str(path), {'data': self.data})
  22. mat_contents = scipy.io.loadmat(path)
  23. assert (mat_contents['data'] == self.data).all()
  24. def test_whosmat(self):
  25. # Save data with string path, load with pathlib.Path
  26. with tempdir() as temp_dir:
  27. path = Path(temp_dir) / 'data.mat'
  28. scipy.io.savemat(str(path), {'data': self.data})
  29. contents = scipy.io.whosmat(path)
  30. assert contents[0] == ('data', (1, 5), 'int64')
  31. def test_readsav(self):
  32. path = Path(__file__).parent / 'data/scalar_string.sav'
  33. scipy.io.readsav(path)
  34. def test_hb_read(self):
  35. # Save data with string path, load with pathlib.Path
  36. with tempdir() as temp_dir:
  37. data = scipy.sparse.csr_matrix(scipy.sparse.eye(3))
  38. path = Path(temp_dir) / 'data.hb'
  39. scipy.io.hb_write(str(path), data)
  40. data_new = scipy.io.hb_read(path)
  41. assert (data_new != data).nnz == 0
  42. def test_hb_write(self):
  43. with tempdir() as temp_dir:
  44. data = scipy.sparse.csr_matrix(scipy.sparse.eye(3))
  45. path = Path(temp_dir) / 'data.hb'
  46. scipy.io.hb_write(path, data)
  47. assert path.is_file()
  48. def test_mmio_read(self):
  49. # Save data with string path, load with pathlib.Path
  50. with tempdir() as temp_dir:
  51. data = scipy.sparse.csr_matrix(scipy.sparse.eye(3))
  52. path = Path(temp_dir) / 'data.mtx'
  53. scipy.io.mmwrite(str(path), data)
  54. data_new = scipy.io.mmread(path)
  55. assert (data_new != data).nnz == 0
  56. def test_mmio_write(self):
  57. with tempdir() as temp_dir:
  58. data = scipy.sparse.csr_matrix(scipy.sparse.eye(3))
  59. path = Path(temp_dir) / 'data.mtx'
  60. scipy.io.mmwrite(path, data)
  61. def test_netcdf_file(self):
  62. path = Path(__file__).parent / 'data/example_1.nc'
  63. scipy.io.netcdf_file(path)
  64. def test_wavfile_read(self):
  65. path = Path(__file__).parent / 'data/test-8000Hz-le-2ch-1byteu.wav'
  66. scipy.io.wavfile.read(path)
  67. def test_wavfile_write(self):
  68. # Read from str path, write to Path
  69. input_path = Path(__file__).parent / 'data/test-8000Hz-le-2ch-1byteu.wav'
  70. rate, data = scipy.io.wavfile.read(str(input_path))
  71. with tempdir() as temp_dir:
  72. output_path = Path(temp_dir) / input_path.name
  73. scipy.io.wavfile.write(output_path, rate, data)