From 017a9059eef8146ac4d97b16de1fc79d1301aa19 Mon Sep 17 00:00:00 2001 From: Tashrif Billah Date: Sat, 26 Jan 2019 11:51:19 -0500 Subject: [PATCH] Relative data file path printing (#79) 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 --- nrrd/tests/test_writing.py | 12 ++++++------ nrrd/writer.py | 11 ++++++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/nrrd/tests/test_writing.py b/nrrd/tests/test_writing.py index 2b8274e..db1b507 100644 --- a/nrrd/tests/test_writing.py +++ b/nrrd/tests/test_writing.py @@ -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) @@ -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) @@ -208,7 +210,6 @@ 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) @@ -216,11 +217,10 @@ def test_write_detached_bz2(self): 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) @@ -228,7 +228,7 @@ def test_write_detached_ascii(self): 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__': diff --git a/nrrd/writer.py b/nrrd/writer.py index d8c0b1c..0a63f59 100644 --- a/nrrd/writer.py +++ b/nrrd/writer.py @@ -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 @@ -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. @@ -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