Skip to content

Commit

Permalink
Allow Numpy data to be written for compressed data (#68)
Browse files Browse the repository at this point in the history
When reading compressed data in pynrrd, the resulting numpy array is not writeable. This PR fixes the issue.
  • Loading branch information
addisonElliott authored Sep 20, 2018
1 parent 175b5b1 commit 6b158c6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build/*
*.egg-info/*
MANIFEST
.idea/*
.cache

# Sphinx documentation
docs/build/
Expand Down
2 changes: 1 addition & 1 deletion nrrd/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def read_data(header, fh=None, filename=None):

# Byte skip is applied AFTER the decompression. Skip first x bytes of the decompressed data and parse it using
# NumPy
data = np.frombuffer(decompressed_data[byte_skip:], dtype)
data = np.fromstring(decompressed_data[byte_skip:], dtype)

# Close the file
# Even if opened using with keyword, closing it does not hurt
Expand Down
24 changes: 24 additions & 0 deletions nrrd/tests/test_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def test_read_header_and_data_filename(self):
np.testing.assert_equal(self.expected_header, header)
np.testing.assert_equal(data, self.expected_data)

# Test that the data read is able to be edited
self.assertTrue(data.flags['WRITEABLE'])

def test_read_detached_header_and_data(self):
expected_header = self.expected_header
expected_header[u'data file'] = os.path.basename(RAW_DATA_FILE_PATH)
Expand All @@ -63,6 +66,9 @@ def test_read_detached_header_and_data(self):
np.testing.assert_equal(self.expected_header, header)
np.testing.assert_equal(data, self.expected_data)

# Test that the data read is able to be edited
self.assertTrue(data.flags['WRITEABLE'])

def test_read_header_and_gz_compressed_data(self):
expected_header = self.expected_header
expected_header[u'encoding'] = 'gzip'
Expand All @@ -72,6 +78,9 @@ def test_read_header_and_gz_compressed_data(self):
np.testing.assert_equal(self.expected_header, header)
np.testing.assert_equal(data, self.expected_data)

# Test that the data read is able to be edited
self.assertTrue(data.flags['WRITEABLE'])

def test_read_header_and_bz2_compressed_data(self):
expected_header = self.expected_header
expected_header[u'encoding'] = 'bzip2'
Expand All @@ -81,6 +90,9 @@ def test_read_header_and_bz2_compressed_data(self):
np.testing.assert_equal(self.expected_header, header)
np.testing.assert_equal(data, self.expected_data)

# Test that the data read is able to be edited
self.assertTrue(data.flags['WRITEABLE'])

def test_read_header_and_gz_compressed_data_with_lineskip3(self):
expected_header = self.expected_header
expected_header[u'encoding'] = 'gzip'
Expand All @@ -91,6 +103,9 @@ def test_read_header_and_gz_compressed_data_with_lineskip3(self):
np.testing.assert_equal(self.expected_header, header)
np.testing.assert_equal(data, self.expected_data)

# Test that the data read is able to be edited
self.assertTrue(data.flags['WRITEABLE'])

def test_read_raw_header(self):
expected_header = {u'type': 'float', u'dimension': 3}
header = nrrd.read_header(('NRRD0005', 'type: float', 'dimension: 3'))
Expand Down Expand Up @@ -131,6 +146,9 @@ def test_read_header_and_ascii_1d_data(self):
np.testing.assert_equal(data.dtype, np.uint8)
np.testing.assert_equal(data, np.arange(1, 28))

# Test that the data read is able to be edited
self.assertTrue(data.flags['WRITEABLE'])

def test_read_header_and_ascii_2d_data(self):
expected_header = {u'dimension': 2,
u'encoding': 'ASCII',
Expand All @@ -145,6 +163,9 @@ def test_read_header_and_ascii_2d_data(self):
np.testing.assert_equal(data.dtype, np.uint16)
np.testing.assert_equal(data, np.arange(1, 28).reshape(3, 9, order='F'))

# Test that the data read is able to be edited
self.assertTrue(data.flags['WRITEABLE'])

def test_read_simple_4d_nrrd(self):
expected_header = {'type': 'double',
'dimension': 4,
Expand All @@ -166,6 +187,9 @@ def test_read_simple_4d_nrrd(self):
np.testing.assert_equal(data.dtype, np.float64)
np.testing.assert_equal(data, np.array([[[[0.76903426]]]]))

# Test that the data read is able to be edited
self.assertTrue(data.flags['WRITEABLE'])

def test_custom_fields_without_field_map(self):
expected_header = {u'dimension': 1,
u'encoding': 'ASCII',
Expand Down

0 comments on commit 6b158c6

Please sign in to comment.