ITK icon indicating copy to clipboard operation
ITK copied to clipboard

NrrdImageIO writes incorrect file when system locale uses comma instead of dot for decimal numbers (space directions)

Open rAum opened this issue 2 years ago • 1 comments

Description

We discovered funny issue, when system locale is set to one where decimal numbers are wrote with comma "," instead of "." then the written NRRD file has broken space direction matrix, corrupting header.

Steps to Reproduce

  1. Change system locale to for example Polish (maybe env flag LC_NUMERIC can do the trick)
  2. Create any volume where spacing is fractional (e.g. 0.3 mm ) and write it as NRRD file
  3. Volume does not open in Slicer and header is obviously malformed when inspected. The loading fails with error

Expected behavior

Conform to NRRD file format specification (it uses comma for separating values therefore you cannot use the same character for decimals). Always write correctly parsable space directions (and other numeric fields if applicable).

Actual behavior

When opening a written nrrd file:

>>> reader.SetFileName('foobar.nrrd')
>>> sitk_image = reader.Execute()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/miniconda3/envs/fooy/lib/python3.8/site-packages/SimpleITK/SimpleITK.py", line 5779, in Execute
    return _SimpleITK.ImageFileReader_Execute(self)
RuntimeError: Exception thrown in SimpleITK ImageFileReader_Execute: /tmp/SimpleITK-build/ITK/Modules/IO/NRRD/src/itkNrrdImageIO.cxx:290:
itk::ERROR: NrrdImageIO(0x55b8a4e482f0): ReadImageInformation: Error reading foobar.nrrd:
[nrrd] nrrdLoad: trouble reading "foobar.nrrd"
[nrrd] nrrdRead: trouble
[nrrd] _nrrdRead: trouble reading NRRD file
[nrrd] _nrrdFormatNRRD_read: trouble parsing space directions info |(0,29999999999999999,0,0) (0,0,29999999999999999,0) (0,0,0,29999999999999999)|
[nrrd] _nrrdReadNrrdParse_space_directions: trouble getting space vector 1 of 3
[nrrd] _nrrdSpaceVectorParse: space dimension is 3, but seem to have 4 coefficients

Reproducibility

100% on systems with locale set to one using comma as decimal delimiter

Versions

5.2.1

Environment

Python 3.8, ITK==5.2.1pre1, SimpleITK==2.0.2

Additional Information

N/A

rAum avatar Apr 13 '22 12:04 rAum

This is similar to issue #2067, and I expect the solution (#2297) to be similar too. @rAum can you propose a PR with the fix?

dzenanz avatar Apr 13 '22 13:04 dzenanz