Skip to content

Commit

Permalink
Relative data file path printing (#79)
Browse files Browse the repository at this point in the history
Add argument `relative_data_path` to `nrrd.write` for specifying whether to save the 'data file' field in the header relative or absolute.

Fixes #78
  • Loading branch information
tashrifbillah authored and addisonElliott committed Jan 26, 2019
1 parent c1c42d9 commit 017a905
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
12 changes: 6 additions & 6 deletions nrrd/tests/test_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ def test_write_detached_raw_as_nrrd(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.nrrd')

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

# Read back the same file
data, header = nrrd.read(output_filename)
Expand Down Expand Up @@ -198,7 +199,8 @@ 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')

nrrd.write(output_filename, self.data_input, {u'encoding': 'gz'}, detached_header=False)
nrrd.write(output_filename, self.data_input, {u'encoding': 'gz'}, detached_header=False,
relative_data_path=False)

# Read back the same file
data, header = nrrd.read(output_filename)
Expand All @@ -208,27 +210,25 @@ 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')
output_data_filename = os.path.join(self.temp_write_dir, 'testfile_detached_raw.raw.bz2')

nrrd.write(output_filename, self.data_input, {u'encoding': 'bz2'}, detached_header=False)

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

def test_write_detached_ascii(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.txt')

nrrd.write(output_filename, self.data_input, {u'encoding': 'txt'}, detached_header=False)

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


if __name__ == '__main__':
Expand Down
11 changes: 8 additions & 3 deletions nrrd/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _format_field_value(value, field_type):
raise NRRDError('Invalid field type given: %s' % field_type)


def write(filename, data, header={}, detached_header=False, custom_field_map=None,
def write(filename, data, header={}, detached_header=False, relative_data_path=True, custom_field_map=None,
compression_level = 9):
"""Write :class:`numpy.ndarray` to NRRD file
Expand Down Expand Up @@ -121,6 +121,9 @@ def write(filename, data, header={}, detached_header=False, custom_field_map=Non
Data to save to the NRRD file
detached_header : :obj:`bool`, optional
Whether the header and data should be saved in separate files. Defaults to :obj:`False`
relative_data_path : :class:`bool`
Whether the data filename in detached header is saved with a relative path or absolute path.
This parameter is ignored if there is no detached header. Defaults to :obj:`True`
custom_field_map : :class:`dict` (:class:`str`, :class:`str`), optional
Dictionary used for parsing custom field types where the key is the custom field name and the value is a
string identifying datatype for the custom field.
Expand Down Expand Up @@ -183,12 +186,14 @@ def write(filename, data, header={}, detached_header=False, custom_field_map=Non
else:
raise NRRDError('Invalid encoding specification while writing NRRD file: %s' % header['encoding'])

header['data file'] = data_filename
header['data file'] = os.path.basename(data_filename) \
if relative_data_path else os.path.abspath(data_filename)
else:
data_filename = header['data file']
elif filename.endswith('.nrrd') and detached_header:
data_filename = filename
header['data file'] = data_filename
header['data file'] = os.path.basename(data_filename) \
if relative_data_path else os.path.abspath(data_filename)
filename = '%s.nhdr' % os.path.splitext(filename)[0]
else:
# Write header & data as one file
Expand Down

0 comments on commit 017a905

Please sign in to comment.