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

Relative data file path printing #79

Merged
merged 8 commits into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
12 changes: 7 additions & 5 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,13 +199,14 @@ 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)
self.assertEqual(self.expected_data, data.tostring(order='F'))
self.assertEqual(header['encoding'], 'gz')
self.assertEqual(header['data file'], output_data_filename)
self.assertEqual(header['data file'], 'testfile_detached_raw.raw.gz')

def test_write_detached_bz2(self):
output_filename = os.path.join(self.temp_write_dir, 'testfile_detached_raw.nhdr')
Expand All @@ -216,7 +218,7 @@ 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'], os.path.basename(output_data_filename))

def test_write_detached_ascii(self):
output_filename = os.path.join(self.temp_write_dir, 'testfile_detached_raw.nhdr')
Expand All @@ -228,7 +230,7 @@ def test_write_detached_ascii(self):
data, header = nrrd.read(output_filename)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, one small thing. Since you directly placed the string to be asserted, the output_data_filename variable is not used. Remove that please

Same for above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will review your other PRs, but may take some time.

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 file name in detached header is saved with a relative path or absolute path.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Alright, rereading this now I agree with how it's set.

Just one minor thing, I usually prefer the use of filename rather than file name. To stay consistent let's change it. I think I have it without the space in the docs and such.

Suggested change
Whether the data file name in detached header is saved with a relative path or absolute path.
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