test_mmio.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. from __future__ import division, print_function, absolute_import
  2. from tempfile import mkdtemp, mktemp
  3. import os
  4. import shutil
  5. import numpy as np
  6. from numpy import array, transpose, pi
  7. from numpy.testing import (assert_equal,
  8. assert_array_equal, assert_array_almost_equal)
  9. import pytest
  10. from pytest import raises as assert_raises
  11. import scipy.sparse
  12. from scipy.io.mmio import mminfo, mmread, mmwrite
  13. parametrize_args = [('integer', 'int'),
  14. ('unsigned-integer', 'uint')]
  15. class TestMMIOArray(object):
  16. def setup_method(self):
  17. self.tmpdir = mkdtemp()
  18. self.fn = os.path.join(self.tmpdir, 'testfile.mtx')
  19. def teardown_method(self):
  20. shutil.rmtree(self.tmpdir)
  21. def check(self, a, info):
  22. mmwrite(self.fn, a)
  23. assert_equal(mminfo(self.fn), info)
  24. b = mmread(self.fn)
  25. assert_array_almost_equal(a, b)
  26. def check_exact(self, a, info):
  27. mmwrite(self.fn, a)
  28. assert_equal(mminfo(self.fn), info)
  29. b = mmread(self.fn)
  30. assert_equal(a, b)
  31. @pytest.mark.parametrize('typeval, dtype', parametrize_args)
  32. def test_simple_integer(self, typeval, dtype):
  33. self.check_exact(array([[1, 2], [3, 4]], dtype=dtype),
  34. (2, 2, 4, 'array', typeval, 'general'))
  35. @pytest.mark.parametrize('typeval, dtype', parametrize_args)
  36. def test_32bit_integer(self, typeval, dtype):
  37. a = array([[2**31-1, 2**31-2], [2**31-3, 2**31-4]], dtype=dtype)
  38. self.check_exact(a, (2, 2, 4, 'array', typeval, 'general'))
  39. def test_64bit_integer(self):
  40. a = array([[2**31, 2**32], [2**63-2, 2**63-1]], dtype=np.int64)
  41. if (np.intp(0).itemsize < 8):
  42. assert_raises(OverflowError, mmwrite, self.fn, a)
  43. else:
  44. self.check_exact(a, (2, 2, 4, 'array', 'integer', 'general'))
  45. def test_64bit_unsigned_integer(self):
  46. a = array([[2**31, 2**32], [2**64-2, 2**64-1]], dtype=np.uint64)
  47. self.check_exact(a, (2, 2, 4, 'array', 'unsigned-integer', 'general'))
  48. @pytest.mark.parametrize('typeval, dtype', parametrize_args)
  49. def test_simple_upper_triangle_integer(self, typeval, dtype):
  50. self.check_exact(array([[0, 1], [0, 0]], dtype=dtype),
  51. (2, 2, 4, 'array', typeval, 'general'))
  52. @pytest.mark.parametrize('typeval, dtype', parametrize_args)
  53. def test_simple_lower_triangle_integer(self, typeval, dtype):
  54. self.check_exact(array([[0, 0], [1, 0]], dtype=dtype),
  55. (2, 2, 4, 'array', typeval, 'general'))
  56. @pytest.mark.parametrize('typeval, dtype', parametrize_args)
  57. def test_simple_rectangular_integer(self, typeval, dtype):
  58. self.check_exact(array([[1, 2, 3], [4, 5, 6]], dtype=dtype),
  59. (2, 3, 6, 'array', typeval, 'general'))
  60. def test_simple_rectangular_float(self):
  61. self.check([[1, 2], [3.5, 4], [5, 6]],
  62. (3, 2, 6, 'array', 'real', 'general'))
  63. def test_simple_float(self):
  64. self.check([[1, 2], [3, 4.0]],
  65. (2, 2, 4, 'array', 'real', 'general'))
  66. def test_simple_complex(self):
  67. self.check([[1, 2], [3, 4j]],
  68. (2, 2, 4, 'array', 'complex', 'general'))
  69. @pytest.mark.parametrize('typeval, dtype', parametrize_args)
  70. def test_simple_symmetric_integer(self, typeval, dtype):
  71. self.check_exact(array([[1, 2], [2, 4]], dtype=dtype),
  72. (2, 2, 4, 'array', typeval, 'symmetric'))
  73. def test_simple_skew_symmetric_integer(self):
  74. self.check_exact([[0, 2], [-2, 0]],
  75. (2, 2, 4, 'array', 'integer', 'skew-symmetric'))
  76. def test_simple_skew_symmetric_float(self):
  77. self.check(array([[0, 2], [-2.0, 0.0]], 'f'),
  78. (2, 2, 4, 'array', 'real', 'skew-symmetric'))
  79. def test_simple_hermitian_complex(self):
  80. self.check([[1, 2+3j], [2-3j, 4]],
  81. (2, 2, 4, 'array', 'complex', 'hermitian'))
  82. def test_random_symmetric_float(self):
  83. sz = (20, 20)
  84. a = np.random.random(sz)
  85. a = a + transpose(a)
  86. self.check(a, (20, 20, 400, 'array', 'real', 'symmetric'))
  87. def test_random_rectangular_float(self):
  88. sz = (20, 15)
  89. a = np.random.random(sz)
  90. self.check(a, (20, 15, 300, 'array', 'real', 'general'))
  91. class TestMMIOSparseCSR(TestMMIOArray):
  92. def setup_method(self):
  93. self.tmpdir = mkdtemp()
  94. self.fn = os.path.join(self.tmpdir, 'testfile.mtx')
  95. def teardown_method(self):
  96. shutil.rmtree(self.tmpdir)
  97. def check(self, a, info):
  98. mmwrite(self.fn, a)
  99. assert_equal(mminfo(self.fn), info)
  100. b = mmread(self.fn)
  101. assert_array_almost_equal(a.todense(), b.todense())
  102. def check_exact(self, a, info):
  103. mmwrite(self.fn, a)
  104. assert_equal(mminfo(self.fn), info)
  105. b = mmread(self.fn)
  106. assert_equal(a.todense(), b.todense())
  107. @pytest.mark.parametrize('typeval, dtype', parametrize_args)
  108. def test_simple_integer(self, typeval, dtype):
  109. self.check_exact(scipy.sparse.csr_matrix([[1, 2], [3, 4]], dtype=dtype),
  110. (2, 2, 4, 'coordinate', typeval, 'general'))
  111. def test_32bit_integer(self):
  112. a = scipy.sparse.csr_matrix(array([[2**31-1, -2**31+2],
  113. [2**31-3, 2**31-4]],
  114. dtype=np.int32))
  115. self.check_exact(a, (2, 2, 4, 'coordinate', 'integer', 'general'))
  116. def test_64bit_integer(self):
  117. a = scipy.sparse.csr_matrix(array([[2**32+1, 2**32+1],
  118. [-2**63+2, 2**63-2]],
  119. dtype=np.int64))
  120. if (np.intp(0).itemsize < 8):
  121. assert_raises(OverflowError, mmwrite, self.fn, a)
  122. else:
  123. self.check_exact(a, (2, 2, 4, 'coordinate', 'integer', 'general'))
  124. def test_32bit_unsigned_integer(self):
  125. a = scipy.sparse.csr_matrix(array([[2**31-1, 2**31-2],
  126. [2**31-3, 2**31-4]],
  127. dtype=np.uint32))
  128. self.check_exact(a, (2, 2, 4, 'coordinate', 'unsigned-integer', 'general'))
  129. def test_64bit_unsigned_integer(self):
  130. a = scipy.sparse.csr_matrix(array([[2**32+1, 2**32+1],
  131. [2**64-2, 2**64-1]],
  132. dtype=np.uint64))
  133. self.check_exact(a, (2, 2, 4, 'coordinate', 'unsigned-integer', 'general'))
  134. @pytest.mark.parametrize('typeval, dtype', parametrize_args)
  135. def test_simple_upper_triangle_integer(self, typeval, dtype):
  136. self.check_exact(scipy.sparse.csr_matrix([[0, 1], [0, 0]], dtype=dtype),
  137. (2, 2, 1, 'coordinate', typeval, 'general'))
  138. @pytest.mark.parametrize('typeval, dtype', parametrize_args)
  139. def test_simple_lower_triangle_integer(self, typeval, dtype):
  140. self.check_exact(scipy.sparse.csr_matrix([[0, 0], [1, 0]], dtype=dtype),
  141. (2, 2, 1, 'coordinate', typeval, 'general'))
  142. @pytest.mark.parametrize('typeval, dtype', parametrize_args)
  143. def test_simple_rectangular_integer(self, typeval, dtype):
  144. self.check_exact(scipy.sparse.csr_matrix([[1, 2, 3], [4, 5, 6]], dtype=dtype),
  145. (2, 3, 6, 'coordinate', typeval, 'general'))
  146. def test_simple_rectangular_float(self):
  147. self.check(scipy.sparse.csr_matrix([[1, 2], [3.5, 4], [5, 6]]),
  148. (3, 2, 6, 'coordinate', 'real', 'general'))
  149. def test_simple_float(self):
  150. self.check(scipy.sparse.csr_matrix([[1, 2], [3, 4.0]]),
  151. (2, 2, 4, 'coordinate', 'real', 'general'))
  152. def test_simple_complex(self):
  153. self.check(scipy.sparse.csr_matrix([[1, 2], [3, 4j]]),
  154. (2, 2, 4, 'coordinate', 'complex', 'general'))
  155. @pytest.mark.parametrize('typeval, dtype', parametrize_args)
  156. def test_simple_symmetric_integer(self, typeval, dtype):
  157. self.check_exact(scipy.sparse.csr_matrix([[1, 2], [2, 4]], dtype=dtype),
  158. (2, 2, 3, 'coordinate', typeval, 'symmetric'))
  159. def test_simple_skew_symmetric_integer(self):
  160. self.check_exact(scipy.sparse.csr_matrix([[1, 2], [-2, 4]]),
  161. (2, 2, 3, 'coordinate', 'integer', 'skew-symmetric'))
  162. def test_simple_skew_symmetric_float(self):
  163. self.check(scipy.sparse.csr_matrix(array([[1, 2], [-2.0, 4]], 'f')),
  164. (2, 2, 3, 'coordinate', 'real', 'skew-symmetric'))
  165. def test_simple_hermitian_complex(self):
  166. self.check(scipy.sparse.csr_matrix([[1, 2+3j], [2-3j, 4]]),
  167. (2, 2, 3, 'coordinate', 'complex', 'hermitian'))
  168. def test_random_symmetric_float(self):
  169. sz = (20, 20)
  170. a = np.random.random(sz)
  171. a = a + transpose(a)
  172. a = scipy.sparse.csr_matrix(a)
  173. self.check(a, (20, 20, 210, 'coordinate', 'real', 'symmetric'))
  174. def test_random_rectangular_float(self):
  175. sz = (20, 15)
  176. a = np.random.random(sz)
  177. a = scipy.sparse.csr_matrix(a)
  178. self.check(a, (20, 15, 300, 'coordinate', 'real', 'general'))
  179. def test_simple_pattern(self):
  180. a = scipy.sparse.csr_matrix([[0, 1.5], [3.0, 2.5]])
  181. p = np.zeros_like(a.todense())
  182. p[a.todense() > 0] = 1
  183. info = (2, 2, 3, 'coordinate', 'pattern', 'general')
  184. mmwrite(self.fn, a, field='pattern')
  185. assert_equal(mminfo(self.fn), info)
  186. b = mmread(self.fn)
  187. assert_array_almost_equal(p, b.todense())
  188. _32bit_integer_dense_example = '''\
  189. %%MatrixMarket matrix array integer general
  190. 2 2
  191. 2147483647
  192. 2147483646
  193. 2147483647
  194. 2147483646
  195. '''
  196. _32bit_integer_sparse_example = '''\
  197. %%MatrixMarket matrix coordinate integer symmetric
  198. 2 2 2
  199. 1 1 2147483647
  200. 2 2 2147483646
  201. '''
  202. _64bit_integer_dense_example = '''\
  203. %%MatrixMarket matrix array integer general
  204. 2 2
  205. 2147483648
  206. -9223372036854775806
  207. -2147483648
  208. 9223372036854775807
  209. '''
  210. _64bit_integer_sparse_general_example = '''\
  211. %%MatrixMarket matrix coordinate integer general
  212. 2 2 3
  213. 1 1 2147483648
  214. 1 2 9223372036854775807
  215. 2 2 9223372036854775807
  216. '''
  217. _64bit_integer_sparse_symmetric_example = '''\
  218. %%MatrixMarket matrix coordinate integer symmetric
  219. 2 2 3
  220. 1 1 2147483648
  221. 1 2 -9223372036854775807
  222. 2 2 9223372036854775807
  223. '''
  224. _64bit_integer_sparse_skew_example = '''\
  225. %%MatrixMarket matrix coordinate integer skew-symmetric
  226. 2 2 3
  227. 1 1 2147483648
  228. 1 2 -9223372036854775807
  229. 2 2 9223372036854775807
  230. '''
  231. _over64bit_integer_dense_example = '''\
  232. %%MatrixMarket matrix array integer general
  233. 2 2
  234. 2147483648
  235. 9223372036854775807
  236. 2147483648
  237. 9223372036854775808
  238. '''
  239. _over64bit_integer_sparse_example = '''\
  240. %%MatrixMarket matrix coordinate integer symmetric
  241. 2 2 2
  242. 1 1 2147483648
  243. 2 2 19223372036854775808
  244. '''
  245. class TestMMIOReadLargeIntegers(object):
  246. def setup_method(self):
  247. self.tmpdir = mkdtemp()
  248. self.fn = os.path.join(self.tmpdir, 'testfile.mtx')
  249. def teardown_method(self):
  250. shutil.rmtree(self.tmpdir)
  251. def check_read(self, example, a, info, dense, over32, over64):
  252. with open(self.fn, 'w') as f:
  253. f.write(example)
  254. assert_equal(mminfo(self.fn), info)
  255. if (over32 and (np.intp(0).itemsize < 8)) or over64:
  256. assert_raises(OverflowError, mmread, self.fn)
  257. else:
  258. b = mmread(self.fn)
  259. if not dense:
  260. b = b.todense()
  261. assert_equal(a, b)
  262. def test_read_32bit_integer_dense(self):
  263. a = array([[2**31-1, 2**31-1],
  264. [2**31-2, 2**31-2]], dtype=np.int64)
  265. self.check_read(_32bit_integer_dense_example,
  266. a,
  267. (2, 2, 4, 'array', 'integer', 'general'),
  268. dense=True,
  269. over32=False,
  270. over64=False)
  271. def test_read_32bit_integer_sparse(self):
  272. a = array([[2**31-1, 0],
  273. [0, 2**31-2]], dtype=np.int64)
  274. self.check_read(_32bit_integer_sparse_example,
  275. a,
  276. (2, 2, 2, 'coordinate', 'integer', 'symmetric'),
  277. dense=False,
  278. over32=False,
  279. over64=False)
  280. def test_read_64bit_integer_dense(self):
  281. a = array([[2**31, -2**31],
  282. [-2**63+2, 2**63-1]], dtype=np.int64)
  283. self.check_read(_64bit_integer_dense_example,
  284. a,
  285. (2, 2, 4, 'array', 'integer', 'general'),
  286. dense=True,
  287. over32=True,
  288. over64=False)
  289. def test_read_64bit_integer_sparse_general(self):
  290. a = array([[2**31, 2**63-1],
  291. [0, 2**63-1]], dtype=np.int64)
  292. self.check_read(_64bit_integer_sparse_general_example,
  293. a,
  294. (2, 2, 3, 'coordinate', 'integer', 'general'),
  295. dense=False,
  296. over32=True,
  297. over64=False)
  298. def test_read_64bit_integer_sparse_symmetric(self):
  299. a = array([[2**31, -2**63+1],
  300. [-2**63+1, 2**63-1]], dtype=np.int64)
  301. self.check_read(_64bit_integer_sparse_symmetric_example,
  302. a,
  303. (2, 2, 3, 'coordinate', 'integer', 'symmetric'),
  304. dense=False,
  305. over32=True,
  306. over64=False)
  307. def test_read_64bit_integer_sparse_skew(self):
  308. a = array([[2**31, -2**63+1],
  309. [2**63-1, 2**63-1]], dtype=np.int64)
  310. self.check_read(_64bit_integer_sparse_skew_example,
  311. a,
  312. (2, 2, 3, 'coordinate', 'integer', 'skew-symmetric'),
  313. dense=False,
  314. over32=True,
  315. over64=False)
  316. def test_read_over64bit_integer_dense(self):
  317. self.check_read(_over64bit_integer_dense_example,
  318. None,
  319. (2, 2, 4, 'array', 'integer', 'general'),
  320. dense=True,
  321. over32=True,
  322. over64=True)
  323. def test_read_over64bit_integer_sparse(self):
  324. self.check_read(_over64bit_integer_sparse_example,
  325. None,
  326. (2, 2, 2, 'coordinate', 'integer', 'symmetric'),
  327. dense=False,
  328. over32=True,
  329. over64=True)
  330. _general_example = '''\
  331. %%MatrixMarket matrix coordinate real general
  332. %=================================================================================
  333. %
  334. % This ASCII file represents a sparse MxN matrix with L
  335. % nonzeros in the following Matrix Market format:
  336. %
  337. % +----------------------------------------------+
  338. % |%%MatrixMarket matrix coordinate real general | <--- header line
  339. % |% | <--+
  340. % |% comments | |-- 0 or more comment lines
  341. % |% | <--+
  342. % | M N L | <--- rows, columns, entries
  343. % | I1 J1 A(I1, J1) | <--+
  344. % | I2 J2 A(I2, J2) | |
  345. % | I3 J3 A(I3, J3) | |-- L lines
  346. % | . . . | |
  347. % | IL JL A(IL, JL) | <--+
  348. % +----------------------------------------------+
  349. %
  350. % Indices are 1-based, i.e. A(1,1) is the first element.
  351. %
  352. %=================================================================================
  353. 5 5 8
  354. 1 1 1.000e+00
  355. 2 2 1.050e+01
  356. 3 3 1.500e-02
  357. 1 4 6.000e+00
  358. 4 2 2.505e+02
  359. 4 4 -2.800e+02
  360. 4 5 3.332e+01
  361. 5 5 1.200e+01
  362. '''
  363. _hermitian_example = '''\
  364. %%MatrixMarket matrix coordinate complex hermitian
  365. 5 5 7
  366. 1 1 1.0 0
  367. 2 2 10.5 0
  368. 4 2 250.5 22.22
  369. 3 3 1.5e-2 0
  370. 4 4 -2.8e2 0
  371. 5 5 12. 0
  372. 5 4 0 33.32
  373. '''
  374. _skew_example = '''\
  375. %%MatrixMarket matrix coordinate real skew-symmetric
  376. 5 5 7
  377. 1 1 1.0
  378. 2 2 10.5
  379. 4 2 250.5
  380. 3 3 1.5e-2
  381. 4 4 -2.8e2
  382. 5 5 12.
  383. 5 4 0
  384. '''
  385. _symmetric_example = '''\
  386. %%MatrixMarket matrix coordinate real symmetric
  387. 5 5 7
  388. 1 1 1.0
  389. 2 2 10.5
  390. 4 2 250.5
  391. 3 3 1.5e-2
  392. 4 4 -2.8e2
  393. 5 5 12.
  394. 5 4 8
  395. '''
  396. _symmetric_pattern_example = '''\
  397. %%MatrixMarket matrix coordinate pattern symmetric
  398. 5 5 7
  399. 1 1
  400. 2 2
  401. 4 2
  402. 3 3
  403. 4 4
  404. 5 5
  405. 5 4
  406. '''
  407. class TestMMIOCoordinate(object):
  408. def setup_method(self):
  409. self.tmpdir = mkdtemp()
  410. self.fn = os.path.join(self.tmpdir, 'testfile.mtx')
  411. def teardown_method(self):
  412. shutil.rmtree(self.tmpdir)
  413. def check_read(self, example, a, info):
  414. f = open(self.fn, 'w')
  415. f.write(example)
  416. f.close()
  417. assert_equal(mminfo(self.fn), info)
  418. b = mmread(self.fn).todense()
  419. assert_array_almost_equal(a, b)
  420. def test_read_general(self):
  421. a = [[1, 0, 0, 6, 0],
  422. [0, 10.5, 0, 0, 0],
  423. [0, 0, .015, 0, 0],
  424. [0, 250.5, 0, -280, 33.32],
  425. [0, 0, 0, 0, 12]]
  426. self.check_read(_general_example, a,
  427. (5, 5, 8, 'coordinate', 'real', 'general'))
  428. def test_read_hermitian(self):
  429. a = [[1, 0, 0, 0, 0],
  430. [0, 10.5, 0, 250.5 - 22.22j, 0],
  431. [0, 0, .015, 0, 0],
  432. [0, 250.5 + 22.22j, 0, -280, -33.32j],
  433. [0, 0, 0, 33.32j, 12]]
  434. self.check_read(_hermitian_example, a,
  435. (5, 5, 7, 'coordinate', 'complex', 'hermitian'))
  436. def test_read_skew(self):
  437. a = [[1, 0, 0, 0, 0],
  438. [0, 10.5, 0, -250.5, 0],
  439. [0, 0, .015, 0, 0],
  440. [0, 250.5, 0, -280, 0],
  441. [0, 0, 0, 0, 12]]
  442. self.check_read(_skew_example, a,
  443. (5, 5, 7, 'coordinate', 'real', 'skew-symmetric'))
  444. def test_read_symmetric(self):
  445. a = [[1, 0, 0, 0, 0],
  446. [0, 10.5, 0, 250.5, 0],
  447. [0, 0, .015, 0, 0],
  448. [0, 250.5, 0, -280, 8],
  449. [0, 0, 0, 8, 12]]
  450. self.check_read(_symmetric_example, a,
  451. (5, 5, 7, 'coordinate', 'real', 'symmetric'))
  452. def test_read_symmetric_pattern(self):
  453. a = [[1, 0, 0, 0, 0],
  454. [0, 1, 0, 1, 0],
  455. [0, 0, 1, 0, 0],
  456. [0, 1, 0, 1, 1],
  457. [0, 0, 0, 1, 1]]
  458. self.check_read(_symmetric_pattern_example, a,
  459. (5, 5, 7, 'coordinate', 'pattern', 'symmetric'))
  460. def test_empty_write_read(self):
  461. # https://github.com/scipy/scipy/issues/1410 (Trac #883)
  462. b = scipy.sparse.coo_matrix((10, 10))
  463. mmwrite(self.fn, b)
  464. assert_equal(mminfo(self.fn),
  465. (10, 10, 0, 'coordinate', 'real', 'symmetric'))
  466. a = b.todense()
  467. b = mmread(self.fn).todense()
  468. assert_array_almost_equal(a, b)
  469. def test_bzip2_py3(self):
  470. # test if fix for #2152 works
  471. try:
  472. # bz2 module isn't always built when building Python.
  473. import bz2
  474. except ImportError:
  475. return
  476. I = array([0, 0, 1, 2, 3, 3, 3, 4])
  477. J = array([0, 3, 1, 2, 1, 3, 4, 4])
  478. V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0])
  479. b = scipy.sparse.coo_matrix((V, (I, J)), shape=(5, 5))
  480. mmwrite(self.fn, b)
  481. fn_bzip2 = "%s.bz2" % self.fn
  482. with open(self.fn, 'rb') as f_in:
  483. f_out = bz2.BZ2File(fn_bzip2, 'wb')
  484. f_out.write(f_in.read())
  485. f_out.close()
  486. a = mmread(fn_bzip2).todense()
  487. assert_array_almost_equal(a, b.todense())
  488. def test_gzip_py3(self):
  489. # test if fix for #2152 works
  490. try:
  491. # gzip module can be missing from Python installation
  492. import gzip
  493. except ImportError:
  494. return
  495. I = array([0, 0, 1, 2, 3, 3, 3, 4])
  496. J = array([0, 3, 1, 2, 1, 3, 4, 4])
  497. V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0])
  498. b = scipy.sparse.coo_matrix((V, (I, J)), shape=(5, 5))
  499. mmwrite(self.fn, b)
  500. fn_gzip = "%s.gz" % self.fn
  501. with open(self.fn, 'rb') as f_in:
  502. f_out = gzip.open(fn_gzip, 'wb')
  503. f_out.write(f_in.read())
  504. f_out.close()
  505. a = mmread(fn_gzip).todense()
  506. assert_array_almost_equal(a, b.todense())
  507. def test_real_write_read(self):
  508. I = array([0, 0, 1, 2, 3, 3, 3, 4])
  509. J = array([0, 3, 1, 2, 1, 3, 4, 4])
  510. V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0])
  511. b = scipy.sparse.coo_matrix((V, (I, J)), shape=(5, 5))
  512. mmwrite(self.fn, b)
  513. assert_equal(mminfo(self.fn),
  514. (5, 5, 8, 'coordinate', 'real', 'general'))
  515. a = b.todense()
  516. b = mmread(self.fn).todense()
  517. assert_array_almost_equal(a, b)
  518. def test_complex_write_read(self):
  519. I = array([0, 0, 1, 2, 3, 3, 3, 4])
  520. J = array([0, 3, 1, 2, 1, 3, 4, 4])
  521. V = array([1.0 + 3j, 6.0 + 2j, 10.50 + 0.9j, 0.015 + -4.4j,
  522. 250.5 + 0j, -280.0 + 5j, 33.32 + 6.4j, 12.00 + 0.8j])
  523. b = scipy.sparse.coo_matrix((V, (I, J)), shape=(5, 5))
  524. mmwrite(self.fn, b)
  525. assert_equal(mminfo(self.fn),
  526. (5, 5, 8, 'coordinate', 'complex', 'general'))
  527. a = b.todense()
  528. b = mmread(self.fn).todense()
  529. assert_array_almost_equal(a, b)
  530. def test_sparse_formats(self):
  531. mats = []
  532. I = array([0, 0, 1, 2, 3, 3, 3, 4])
  533. J = array([0, 3, 1, 2, 1, 3, 4, 4])
  534. V = array([1.0, 6.0, 10.5, 0.015, 250.5, -280.0, 33.32, 12.0])
  535. mats.append(scipy.sparse.coo_matrix((V, (I, J)), shape=(5, 5)))
  536. V = array([1.0 + 3j, 6.0 + 2j, 10.50 + 0.9j, 0.015 + -4.4j,
  537. 250.5 + 0j, -280.0 + 5j, 33.32 + 6.4j, 12.00 + 0.8j])
  538. mats.append(scipy.sparse.coo_matrix((V, (I, J)), shape=(5, 5)))
  539. for mat in mats:
  540. expected = mat.todense()
  541. for fmt in ['csr', 'csc', 'coo']:
  542. fn = mktemp(dir=self.tmpdir) # safe, we own tmpdir
  543. mmwrite(fn, mat.asformat(fmt))
  544. result = mmread(fn).todense()
  545. assert_array_almost_equal(result, expected)
  546. def test_precision(self):
  547. test_values = [pi] + [10**(i) for i in range(0, -10, -1)]
  548. test_precisions = range(1, 10)
  549. for value in test_values:
  550. for precision in test_precisions:
  551. # construct sparse matrix with test value at last main diagonal
  552. n = 10**precision + 1
  553. A = scipy.sparse.dok_matrix((n, n))
  554. A[n-1, n-1] = value
  555. # write matrix with test precision and read again
  556. mmwrite(self.fn, A, precision=precision)
  557. A = scipy.io.mmread(self.fn)
  558. # check for right entries in matrix
  559. assert_array_equal(A.row, [n-1])
  560. assert_array_equal(A.col, [n-1])
  561. assert_array_almost_equal(A.data,
  562. [float('%%.%dg' % precision % value)])