Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor syntax fixes #81

Merged
merged 7 commits into from
Jan 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions nrrd/tests/test_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def setUp(self):
self.expected_data = np.fromfile(RAW_DATA_FILE_PATH, np.int16).reshape((30, 30, 30), order='F')

def test_read_header_only(self):
with open(RAW_NRRD_FILE_PATH, 'rb') as f:
header = nrrd.read_header(f)
with open(RAW_NRRD_FILE_PATH, 'rb') as fh:
header = nrrd.read_header(fh)

# np.testing.assert_equal is used to compare the headers because it will appropriately handle each
# value in the structure. Since some of the values can be Numpy arrays inside the headers, this must be
Expand All @@ -42,8 +42,8 @@ def test_read_detached_header_only(self):
expected_header = self.expected_header
expected_header[u'data file'] = os.path.basename(RAW_DATA_FILE_PATH)

with open(RAW_NHDR_FILE_PATH, 'rb') as f:
header = nrrd.read_header(f)
with open(RAW_NHDR_FILE_PATH, 'rb') as fh:
header = nrrd.read_header(fh)

np.testing.assert_equal(self.expected_header, header)

Expand Down
28 changes: 9 additions & 19 deletions nrrd/tests/test_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ def setUp(self):
self.temp_write_dir = tempfile.mkdtemp('nrrdtest')
self.data_input, _ = nrrd.read(RAW_NRRD_FILE_PATH)

with open(RAW_DATA_FILE_PATH, 'rb') as f:
self.expected_data = f.read()
with open(RAW_DATA_FILE_PATH, 'rb') as fh:
self.expected_data = fh.read()

def write_and_read_back_with_encoding(self, encoding, level=9):
output_filename = os.path.join(self.temp_write_dir, 'testfile_{}_{}.nrrd'.format(encoding, str(level)))
nrrd.write(output_filename, self.data_input, {u'encoding': encoding},
compression_level=level)
nrrd.write(output_filename, self.data_input, {u'encoding': encoding}, compression_level=level)

# Read back the same file
data, header = nrrd.read(output_filename)
Expand All @@ -39,13 +38,12 @@ def test_write_bzip2(self):
self.write_and_read_back_with_encoding(u'bzip2')

def test_write_gz_level1(self):
import os
fn = self.write_and_read_back_with_encoding(u'gzip', level=1)
filename = self.write_and_read_back_with_encoding(u'gzip', level=1)

self.assertLess(os.path.getsize(GZ_NRRD_FILE_PATH), os.path.getsize(fn))
self.assertLess(os.path.getsize(GZ_NRRD_FILE_PATH), os.path.getsize(filename))

def test_write_bzip2_level1(self):
fn = self.write_and_read_back_with_encoding(u'bzip2', level=1)
_ = self.write_and_read_back_with_encoding(u'bzip2', level=1)

# note: we don't currently assert reduction here, because with the binary ball test data,
# the output size does not change at different bz2 levels.
Expand Down Expand Up @@ -178,17 +176,6 @@ def test_write_detached_raw_odd_extension(self):
self.assertEqual(header['encoding'], 'raw')
self.assertEqual('data file' in header, False)

def test_write_detached_raw_odd_extension(self):
output_data_filename = os.path.join(self.temp_write_dir, 'testfile_detached_raw.nrrd2')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this test repeated? Sorry, couldn't understand the commit message.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, exactly. I was confused at first too but I just copied/pasted the same test twice when writing and forgot to delete the duplicate.


nrrd.write(output_data_filename, self.data_input, {u'encoding': 'raw'}, detached_header=True)

# Read back the same file
data, header = nrrd.read(output_data_filename)
self.assertEqual(self.expected_data, data.tostring(order='F'))
self.assertEqual(header['encoding'], 'raw')
self.assertEqual('data file' in header, False)

def test_write_fake_encoding(self):
output_filename = os.path.join(self.temp_write_dir, 'testfile_detached_raw.nhdr')

Expand All @@ -199,6 +186,7 @@ def test_write_detached_gz(self):
output_filename = os.path.join(self.temp_write_dir, 'testfile_detached_raw.nhdr')
output_data_filename = os.path.join(self.temp_write_dir, 'testfile_detached_raw.raw.gz')

# Data & header are still detached even though detached_header is False because the filename is .nhdr
nrrd.write(output_filename, self.data_input, {u'encoding': 'gz'}, detached_header=False,
relative_data_path=False)

Expand All @@ -211,6 +199,7 @@ def test_write_detached_gz(self):
def test_write_detached_bz2(self):
output_filename = os.path.join(self.temp_write_dir, 'testfile_detached_raw.nhdr')

# Data & header are still detached even though detached_header is False because the filename is .nhdr
nrrd.write(output_filename, self.data_input, {u'encoding': 'bz2'}, detached_header=False)

# Read back the same file
Expand All @@ -222,6 +211,7 @@ def test_write_detached_bz2(self):
def test_write_detached_ascii(self):
output_filename = os.path.join(self.temp_write_dir, 'testfile_detached_raw.nhdr')

# Data & header are still detached even though detached_header is False because the filename is .nhdr
nrrd.write(output_filename, self.data_input, {u'encoding': 'txt'}, detached_header=False)

# Read back the same file
Expand Down
7 changes: 4 additions & 3 deletions nrrd/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _format_field_value(value, field_type):


def write(filename, data, header={}, detached_header=False, relative_data_path=True, custom_field_map=None,
compression_level = 9):
compression_level=9):
"""Write :class:`numpy.ndarray` to NRRD file

The :obj:`filename` parameter specifies the absolute or relative filename to write the NRRD file to. If the
Expand All @@ -106,7 +106,8 @@ def write(filename, data, header={}, detached_header=False, relative_data_path=T
:obj:`header` is an optional parameter containing the fields and values to be added to the NRRD header.

.. note::
The following fields are automatically generated based on the :obj:`data` parameter ignoring these values in the :obj:`header`: 'type', 'endian', 'dimension', 'sizes'.
The following fields are automatically generated based on the :obj:`data` parameter ignoring these values
in the :obj:`header`: 'type', 'endian', 'dimension', 'sizes'.

.. note::
The default encoding field used if not specified in :obj:`header` is 'gzip'.
Expand Down Expand Up @@ -253,7 +254,7 @@ def write(filename, data, header={}, detached_header=False, relative_data_path=T
_write_data(data, data_fh, header, compression_level=compression_level)


def _write_data(data, fh, header, compression_level = None):
def _write_data(data, fh, header, compression_level=None):
if header['encoding'] == 'raw':
# Convert the data into a string
raw_data = data.tostring(order='F')
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
currentPath = os.path.abspath(os.path.dirname(__file__))

# Get the long description from the README file
with open(os.path.join(currentPath, 'README.rst'), 'r') as f:
longDescription = f.read()
with open(os.path.join(currentPath, 'README.rst'), 'r') as fh:
longDescription = fh.read()

longDescription = '\n' + longDescription

Expand Down