Permalink
Browse files

REF: rename io.hb.read/write to hb_read/hb_write, import them in io n…

…amespace.

Also update the hb_read/hb_write docstrings to note what is returnted and what
features are currently supported.  Finally, change some imports to fit on one
line and PEP8-ify the blank lines.
  • Loading branch information...
1 parent 9e88954 commit 831ab950364bb8abedad6a60a6b8f318ad326e5e @rgommers rgommers committed Sep 11, 2011
View
@@ -79,9 +79,6 @@
netcdf_variable - A data object for the netcdf module
"""
-from numpy import deprecate
-
-
# matfile read and write
from matlab import loadmat, savemat, byteordercodes
@@ -91,7 +88,9 @@
from data_store import save_as_module
from mmio import mminfo, mmread, mmwrite
from idl import readsav
+from harwell_boeing import hb_read, hb_write
__all__ = filter(lambda s:not s.startswith('_'),dir())
from numpy.testing import Tester
test = Tester().test
+
@@ -1,3 +1,3 @@
-from scipy.io.harwell_boeing.hb \
- import \
- MalformedHeader, HBInfo, HBFile, HBMatrixType, read, write
+from scipy.io.harwell_boeing.hb import MalformedHeader, HBInfo, HBFile, \
+ HBMatrixType, hb_read, hb_write
+
@@ -12,8 +12,10 @@
import numpy as np
+
__all__ = ["BadFortranFormat", "FortranFormatParser", "IntFormat", "ExpFormat"]
+
TOKENS = {
"LPAR": r"\(",
"RPAR": r"\)",
@@ -23,12 +25,15 @@
"DOT": r"\.",
}
+
class BadFortranFormat(SyntaxError):
pass
+
def number_digits(n):
return int(np.floor(np.log10(np.abs(n))) + 1)
+
class IntFormat(object):
@classmethod
def from_number(cls, n, min=None):
@@ -87,6 +92,7 @@ def fortran_format(self):
def python_format(self):
return "%" + str(self.width) + "d"
+
class ExpFormat(object):
@classmethod
def from_number(cls, n, min=None):
@@ -158,6 +164,7 @@ def fortran_format(self):
def python_format(self):
return "%" + str(self.width-1) + "." + str(self.significand) + "E"
+
class Token(object):
def __init__(self, type, value, pos):
self.type = type
@@ -170,6 +177,7 @@ def __str__(self):
def __repr__(self):
return self.__str__()
+
class Tokenizer(object):
def __init__(self):
self.tokens = TOKENS.keys()
@@ -196,6 +204,7 @@ def next_token(self):
raise SyntaxError("Unknown character at position %d (%s)" \
% (self.curpos, self.data[curpos]))
+
# Grammar for fortran format:
# format : LPAR format_string RPAR
# format_string : repeated | simple
@@ -300,3 +309,4 @@ def _next(self, tokens, tp):
next = tokens.pop(0)
self._expect(next, tp)
return next
+
@@ -1,10 +1,14 @@
"""
Implementation of Harwell-Boeing read/write.
+
+At the moment not the full Harwell-Boeing format is supported. Supported
+features are:
+
+ - assembled, non-symmetric, real matrices
+ - integer for pointer/indices
+ - exponential format for float values, and int format
+
"""
-# Really crude at the moment:
-# - only support for assembled, non-symmetric, real matrices
-# - only support integer for pointer/indices
-# - only support exponential format for float values, and int format
# TODO:
# - Add more support (symmetric/complex matrices, non-assembled matrices ?)
@@ -14,31 +18,32 @@
# write is not efficient. Although not a terribly exciting task,
# having reusable facilities to efficiently read/write fortran-formatted files
# would be useful outside this module.
+
import warnings
import numpy as np
-
-from scipy.sparse \
- import \
- csc_matrix
-from scipy.io.harwell_boeing._fortran_format_parser \
- import\
+from scipy.sparse import csc_matrix
+from scipy.io.harwell_boeing._fortran_format_parser import \
FortranFormatParser, IntFormat, ExpFormat
+
__all__ = ["MalformedHeader", "read_hb", "write", "HBInfo", "HBFile",
"HBMatrixType"]
+
class MalformedHeader(Exception):
pass
class LineOverflow(Warning):
pass
+
def _nbytes_full(fmt, nlines):
"""Return the number of bytes to read to get every full lines for the
given parsed fortran format."""
return (fmt.repeat * fmt.width + 1) * (nlines - 1)
+
class HBInfo(object):
@classmethod
def from_data(cls, m, title="Default title", key="0", mxtype=None, fmt=None):
@@ -291,6 +296,7 @@ def dump(self):
(pffmt.ljust(16), iffmt.ljust(16), vffmt.ljust(20)))
return "\n".join(header)
+
def _expect_int(value, msg=None):
try:
return int(value)
@@ -299,6 +305,7 @@ def _expect_int(value, msg=None):
msg = "Expected an int, got %s"
raise ValueError(msg % value)
+
def _read_hb_data(content, header):
# XXX: look at a way to reduce memory here (big string creation)
ptr_string = "".join([content.read(header.pointer_nbytes_full),
@@ -322,6 +329,7 @@ def _read_hb_data(content, header):
except ValueError, e:
raise e
+
def _write_data(m, fid, header):
def write_array(f, ar, nlines, fmt):
# ar_nlines is the number of full lines, n is the number of items per
@@ -348,6 +356,7 @@ def write_array(f, ar, nlines, fmt):
write_array(fid, m.data, header.values_nlines,
header.values_format)
+
class HBMatrixType(object):
"""Class to hold the matrix type."""
# q2f* translates qualified names to fortran character
@@ -408,6 +417,7 @@ def __repr__(self):
return "HBMatrixType(%s, %s, %s)" % \
(self.value_type, self.structure, self.storage)
+
class HBFile(object):
def __init__(self, file, hb_info=None):
"""Create a HBFile instance.
@@ -454,14 +464,30 @@ def read_matrix(self):
def write_matrix(self, m):
return _write_data(m, self._fid, self._hb_info)
-def read(file):
+
+def hb_read(file):
"""Read HB-format file.
Parameters
----------
file: str-like or file-like
- if a string-like object, file is the name of the file to read. If a
+ If a string-like object, file is the name of the file to read. If a
file-like object, the data are read from it.
+
+ Returns
+ -------
+ data : scipy.sparse.csc_matrix instance
+ The data read from the HB file as a sparse matrix.
+
+ Notes
+ -----
+ At the moment not the full Harwell-Boeing format is supported. Supported
+ features are:
+
+ - assembled, non-symmetric, real matrices
+ - integer for pointer/indices
+ - exponential format for float values, and int format
+
"""
def _get_matrix(fid):
hb = HBFile(fid)
@@ -476,7 +502,8 @@ def _get_matrix(fid):
else:
return _get_matrix(file)
-def write(file, m, hb_info=None):
+
+def hb_write(file, m, hb_info=None):
"""Write HB-format file.
Parameters
@@ -488,6 +515,20 @@ def write(file, m, hb_info=None):
the sparse matrix to write
hb_info: HBInfo
contains the meta-data for write
+
+ Returns
+ -------
+ None
+
+ Notes
+ -----
+ At the moment not the full Harwell-Boeing format is supported. Supported
+ features are:
+
+ - assembled, non-symmetric, real matrices
+ - integer for pointer/indices
+ - exponential format for float values, and int format
+
"""
if hb_info is None:
hb_info = HBInfo.from_data(m)
@@ -504,3 +545,4 @@ def _set_matrix(fid):
fid.close()
else:
return _set_matrix(file)
+
@@ -10,3 +10,4 @@ def configuration(parent_package='',top_path=None):
if __name__ == '__main__':
from numpy.distutils.core import setup
setup(**configuration(top_path='').todict())
+
@@ -1,12 +1,11 @@
import numpy as np
-from numpy.testing \
- import \
- TestCase, assert_equal, assert_raises
+from numpy.testing import TestCase, assert_equal, assert_raises
+
+from scipy.io.harwell_boeing._fortran_format_parser import \
+ FortranFormatParser, IntFormat, ExpFormat, BadFortranFormat, \
+ number_digits
-from scipy.io.harwell_boeing._fortran_format_parser \
- import \
- FortranFormatParser, IntFormat, ExpFormat, BadFortranFormat, number_digits
class TestFortranFormatParser(TestCase):
def setUp(self):
@@ -71,3 +70,4 @@ def test_from_number(self):
r_f = [ExpFormat(24, 16, repeat=3), ExpFormat(25, 16, repeat=3)]
for i, j in zip(f, r_f):
assert_equal(ExpFormat.from_number(i).__dict__, j.__dict__)
+
@@ -1,21 +1,17 @@
import os
-from cStringIO \
- import \
- StringIO
+from cStringIO import StringIO
import tempfile
import numpy as np
-from numpy.testing \
- import \
- TestCase, assert_equal, assert_array_almost_equal_nulp
-from scipy.sparse \
- import \
- coo_matrix, csc_matrix, rand
+from numpy.testing import TestCase, assert_equal, \
+ assert_array_almost_equal_nulp
+
+from scipy.sparse import coo_matrix, csc_matrix, rand
+
+from scipy.io import hb_read, hb_write
+from scipy.io.harwell_boeing import HBFile, HBInfo
-from scipy.io.harwell_boeing \
- import \
- HBFile, HBInfo, read, write
SIMPLE = """\
No Title |No Key
@@ -50,18 +46,19 @@ def assert_csc_almost_equal(r, l):
class TestHBReader(TestCase):
def test_simple(self):
- m = read(StringIO(SIMPLE))
+ m = hb_read(StringIO(SIMPLE))
assert_csc_almost_equal(m, SIMPLE_MATRIX)
class TestRBRoundtrip(TestCase):
def test_simple(self):
rm = rand(100, 1000, 0.05).tocsc()
fd, filename = tempfile.mkstemp(suffix="rb")
try:
- write(filename, rm, HBInfo.from_data(rm))
- m = read(filename)
+ hb_write(filename, rm, HBInfo.from_data(rm))
+ m = hb_read(filename)
finally:
os.close(fd)
os.remove(filename)
assert_csc_almost_equal(m, rm)
+

0 comments on commit 831ab95

Please sign in to comment.