test_fortran.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. ''' Tests for fortran sequential files '''
  2. import tempfile
  3. import shutil
  4. from os import path
  5. from glob import iglob
  6. import re
  7. from numpy.testing import assert_equal, assert_allclose
  8. import numpy as np
  9. import pytest
  10. from scipy.io import (FortranFile,
  11. _test_fortran,
  12. FortranEOFError,
  13. FortranFormattingError)
  14. DATA_PATH = path.join(path.dirname(__file__), 'data')
  15. def test_fortranfiles_read():
  16. for filename in iglob(path.join(DATA_PATH, "fortran-*-*x*x*.dat")):
  17. m = re.search(r'fortran-([^-]+)-(\d+)x(\d+)x(\d+).dat', filename, re.I)
  18. if not m:
  19. raise RuntimeError("Couldn't match %s filename to regex" % filename)
  20. dims = (int(m.group(2)), int(m.group(3)), int(m.group(4)))
  21. dtype = m.group(1).replace('s', '<')
  22. f = FortranFile(filename, 'r', '<u4')
  23. data = f.read_record(dtype=dtype).reshape(dims, order='F')
  24. f.close()
  25. expected = np.arange(np.prod(dims)).reshape(dims).astype(dtype)
  26. assert_equal(data, expected)
  27. def test_fortranfiles_mixed_record():
  28. filename = path.join(DATA_PATH, "fortran-mixed.dat")
  29. with FortranFile(filename, 'r', '<u4') as f:
  30. record = f.read_record('<i4,<f4,<i8,(2)<f8')
  31. assert_equal(record['f0'][0], 1)
  32. assert_allclose(record['f1'][0], 2.3)
  33. assert_equal(record['f2'][0], 4)
  34. assert_allclose(record['f3'][0], [5.6, 7.8])
  35. def test_fortranfiles_write():
  36. for filename in iglob(path.join(DATA_PATH, "fortran-*-*x*x*.dat")):
  37. m = re.search(r'fortran-([^-]+)-(\d+)x(\d+)x(\d+).dat', filename, re.I)
  38. if not m:
  39. raise RuntimeError("Couldn't match %s filename to regex" % filename)
  40. dims = (int(m.group(2)), int(m.group(3)), int(m.group(4)))
  41. dtype = m.group(1).replace('s', '<')
  42. data = np.arange(np.prod(dims)).reshape(dims).astype(dtype)
  43. tmpdir = tempfile.mkdtemp()
  44. try:
  45. testFile = path.join(tmpdir,path.basename(filename))
  46. f = FortranFile(testFile, 'w','<u4')
  47. f.write_record(data.T)
  48. f.close()
  49. originalfile = open(filename, 'rb')
  50. newfile = open(testFile, 'rb')
  51. assert_equal(originalfile.read(), newfile.read(),
  52. err_msg=filename)
  53. originalfile.close()
  54. newfile.close()
  55. finally:
  56. shutil.rmtree(tmpdir)
  57. def test_fortranfile_read_mixed_record():
  58. # The data file fortran-3x3d-2i.dat contains the program that
  59. # produced it at the end.
  60. #
  61. # double precision :: a(3,3)
  62. # integer :: b(2)
  63. # ...
  64. # open(1, file='fortran-3x3d-2i.dat', form='unformatted')
  65. # write(1) a, b
  66. # close(1)
  67. #
  68. filename = path.join(DATA_PATH, "fortran-3x3d-2i.dat")
  69. with FortranFile(filename, 'r', '<u4') as f:
  70. record = f.read_record('(3,3)<f8', '2<i4')
  71. ax = np.arange(3*3).reshape(3, 3).astype(np.double)
  72. bx = np.array([-1, -2], dtype=np.int32)
  73. assert_equal(record[0], ax.T)
  74. assert_equal(record[1], bx.T)
  75. def test_fortranfile_write_mixed_record(tmpdir):
  76. tf = path.join(str(tmpdir), 'test.dat')
  77. records = [
  78. (('f4', 'f4', 'i4'), (np.float32(2), np.float32(3), np.int32(100))),
  79. (('4f4', '(3,3)f4', '8i4'), (np.random.randint(255, size=[4]).astype(np.float32),
  80. np.random.randint(255, size=[3, 3]).astype(np.float32),
  81. np.random.randint(255, size=[8]).astype(np.int32)))
  82. ]
  83. for dtype, a in records:
  84. with FortranFile(tf, 'w') as f:
  85. f.write_record(*a)
  86. with FortranFile(tf, 'r') as f:
  87. b = f.read_record(*dtype)
  88. assert_equal(len(a), len(b))
  89. for aa, bb in zip(a, b):
  90. assert_equal(bb, aa)
  91. def test_fortran_roundtrip(tmpdir):
  92. filename = path.join(str(tmpdir), 'test.dat')
  93. np.random.seed(1)
  94. # double precision
  95. m, n, k = 5, 3, 2
  96. a = np.random.randn(m, n, k)
  97. with FortranFile(filename, 'w') as f:
  98. f.write_record(a.T)
  99. a2 = _test_fortran.read_unformatted_double(m, n, k, filename)
  100. with FortranFile(filename, 'r') as f:
  101. a3 = f.read_record('(2,3,5)f8').T
  102. assert_equal(a2, a)
  103. assert_equal(a3, a)
  104. # integer
  105. m, n, k = 5, 3, 2
  106. a = np.random.randn(m, n, k).astype(np.int32)
  107. with FortranFile(filename, 'w') as f:
  108. f.write_record(a.T)
  109. a2 = _test_fortran.read_unformatted_int(m, n, k, filename)
  110. with FortranFile(filename, 'r') as f:
  111. a3 = f.read_record('(2,3,5)i4').T
  112. assert_equal(a2, a)
  113. assert_equal(a3, a)
  114. # mixed
  115. m, n, k = 5, 3, 2
  116. a = np.random.randn(m, n)
  117. b = np.random.randn(k).astype(np.intc)
  118. with FortranFile(filename, 'w') as f:
  119. f.write_record(a.T, b.T)
  120. a2, b2 = _test_fortran.read_unformatted_mixed(m, n, k, filename)
  121. with FortranFile(filename, 'r') as f:
  122. a3, b3 = f.read_record('(3,5)f8', '2i4')
  123. a3 = a3.T
  124. assert_equal(a2, a)
  125. assert_equal(a3, a)
  126. assert_equal(b2, b)
  127. assert_equal(b3, b)
  128. def test_fortran_eof_ok(tmpdir):
  129. filename = path.join(str(tmpdir), "scratch")
  130. np.random.seed(1)
  131. with FortranFile(filename, 'w') as f:
  132. f.write_record(np.random.randn(5))
  133. f.write_record(np.random.randn(3))
  134. with FortranFile(filename, 'r') as f:
  135. assert len(f.read_reals()) == 5
  136. assert len(f.read_reals()) == 3
  137. with pytest.raises(FortranEOFError):
  138. f.read_reals()
  139. def test_fortran_eof_broken_size(tmpdir):
  140. filename = path.join(str(tmpdir), "scratch")
  141. np.random.seed(1)
  142. with FortranFile(filename, 'w') as f:
  143. f.write_record(np.random.randn(5))
  144. f.write_record(np.random.randn(3))
  145. with open(filename, "ab") as f:
  146. f.write(b"\xff")
  147. with FortranFile(filename, 'r') as f:
  148. assert len(f.read_reals()) == 5
  149. assert len(f.read_reals()) == 3
  150. with pytest.raises(FortranFormattingError):
  151. f.read_reals()
  152. def test_fortran_bogus_size(tmpdir):
  153. filename = path.join(str(tmpdir), "scratch")
  154. np.random.seed(1)
  155. with FortranFile(filename, 'w') as f:
  156. f.write_record(np.random.randn(5))
  157. f.write_record(np.random.randn(3))
  158. with open(filename, "w+b") as f:
  159. f.write(b"\xff\xff")
  160. with FortranFile(filename, 'r') as f:
  161. with pytest.raises(FortranFormattingError):
  162. f.read_reals()
  163. def test_fortran_eof_broken_record(tmpdir):
  164. filename = path.join(str(tmpdir), "scratch")
  165. np.random.seed(1)
  166. with FortranFile(filename, 'w') as f:
  167. f.write_record(np.random.randn(5))
  168. f.write_record(np.random.randn(3))
  169. with open(filename, "ab") as f:
  170. f.truncate(path.getsize(filename)-20)
  171. with FortranFile(filename, 'r') as f:
  172. assert len(f.read_reals()) == 5
  173. with pytest.raises(FortranFormattingError):
  174. f.read_reals()
  175. def test_fortran_eof_multidimensional(tmpdir):
  176. filename = path.join(str(tmpdir), "scratch")
  177. n, m, q = 3, 5, 7
  178. dt = np.dtype([("field", np.float64, (n, m))])
  179. a = np.zeros(q, dtype=dt)
  180. with FortranFile(filename, 'w') as f:
  181. f.write_record(a[0])
  182. f.write_record(a)
  183. f.write_record(a)
  184. with open(filename, "ab") as f:
  185. f.truncate(path.getsize(filename)-20)
  186. with FortranFile(filename, 'r') as f:
  187. assert len(f.read_record(dtype=dt)) == 1
  188. assert len(f.read_record(dtype=dt)) == q
  189. with pytest.raises(FortranFormattingError):
  190. f.read_record(dtype=dt)