Failures saving a volume to path with % sign

I’m observing an issue with some file I/O when reading/writing metaimage mhd+raw data in a filepath location that contains a character such as “%”. I have seen users utilize this character for a directory name when describing a volume such as the concentration of a drug. This “%” character is also a valid character in the Windows file system.

Below is a python code snippet to observe the issue which can also be triggered when defining an output filepath for a metaimage using the Slicer Save dialog. You will see below that the output reports that the .mhd is written, but the corresponding raw file is not present. When writing the split file format for nrrd (nhdr+raw) is successful writing out both files.

The saving of the volume is successful with the single file format of .mha.

import os
import SampleData

volume_node = SampleData.SampleDataLogic().downloadMRHead()

output_filepath = os.path.join(os.getenv("USERPROFILE"), "Downloads", "2%concentration", "MyVolume.mhd")
slicer.util.saveNode(volume_node, output_filepath)
print(f"{output_filepath} exists?: {os.path.exists(output_filepath)}")
raw_filepath = os.path.splitext(output_filepath)[0] + '.raw'
print(f"{raw_filepath} exists?: {os.path.exists(raw_filepath)}")

output_filepath = os.path.join(os.getenv("USERPROFILE"), "Downloads", "2%concentration", "MyVolume.nhdr")
slicer.util.saveNode(volume_node, output_filepath)
print(f"{output_filepath} exists?: {os.path.exists(output_filepath)}")
raw_filepath = os.path.splitext(output_filepath)[0] + '.raw.gz'
print(f"{raw_filepath} exists?: {os.path.exists(raw_filepath)}")

Is there a workaround to support saving the split file format for metaimage to a filepath with a “%” character? Or does something need to be done to update to a newer version of the metaimage I/O library which might contain a bug fix?

It appears MetaIO has an issue written up for this and a PR trying to close it. @dzenanz Do you know who might be able to help move this along?

Brad King (no profile on this forum) and @jcfr. They both commented on MetaIO#68.

Since % is not an extended character, I don’t see how the referenced MRs in MetaIO are relevant here.

Are you using a non-US keyboard? It is not clear to me what your Python code is demonstrating.

@todoooo I am using a regular US keyboard.

It appears relevant to the MetaIO issues because the ElementDataFile points to a filename with a special character (%) where the target filename is like the following:

“C:\Users\butlej30383\Downloads\2%concentration\MyVolume.mhd”

What my python code snippet is showing is that when saving the volume to this path, MetaIO only writes MyVolume.mhd, but does not write the corresponding MyVolume.raw (or MyVolume.zraw). MetaImage: M_WriteElementsData: file stream is fail after write

Things save correctly if I save with the single file format
“C:\Users\butlej30383\Downloads\2%concentration\MyVolume.mha”

The issue is when the separate ElementDataFile (.raw/.zraw) is to be a filepath that contains a special character such as %.

However when using the NRRD format, writing a MyVolume.nhdr is successful as it also writes the corresponding MyVolume.raw.gz. So NRRD is doing a better job handling this situation.

MetaIO supports One-Slice-Per-File Data Formats and it uses the % character to specify the slice number in the filename format string.

Due to the special meaning of the % character, currently MetaIO does not allow using this chracter in the path. This limitation should either be clearly documented or the implementation should be made more sophisticated (to somehow differentiate the % that refers to per-slice filename generation from simple % occurrences in the path; or by allowing completely disabling this extremely rarely used one-slice-per-file feature).

1 Like

It seems then that % in the filename should be treated with special meaning whereas % in the path name should be ignored.

As far as utf-8 encoding is concerned % is not a special character.

1 Like

I’ve created the following issue to keep track of this problem:

1 Like