Skip to content

Commit ed30a6a

Browse files
committed
fix: Fix NRRD loading failure caused by LC_NUMERIC locale in ITK
1 parent 7372962 commit ed30a6a

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

medvol/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "0.0.16"
1+
__version__ = "0.0.17"
22

33
from medvol.medvol import MedVol

medvol/medvol.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from typing import Dict, Optional, Union, List, Tuple
33
import numpy as np
44
from pathlib import Path
5+
import locale
6+
from contextlib import contextmanager
57

68
# TODO:
79
# - Enable user to set affine
@@ -10,6 +12,20 @@
1012
# - Rename into MedImg
1113

1214

15+
@contextmanager
16+
def temporary_c_locale():
17+
# Save current LC_NUMERIC
18+
old_locale = locale.setlocale(locale.LC_NUMERIC, None)
19+
20+
try:
21+
# Switch to safe C locale
22+
locale.setlocale(locale.LC_NUMERIC, "C")
23+
yield
24+
finally:
25+
# Restore original locale
26+
locale.setlocale(locale.LC_NUMERIC, old_locale)
27+
28+
1329
class MedVol:
1430
def __init__(self,
1531
array: Union[np.ndarray, str, Path],
@@ -211,7 +227,8 @@ def _load(self, filepath):
211227
Raises:
212228
RuntimeError: If the dimensionality of the image and metadata do not match.
213229
"""
214-
image_sitk = sitk.ReadImage(str(filepath))
230+
with temporary_c_locale():
231+
image_sitk = sitk.ReadImage(str(filepath))
215232
array = sitk.GetArrayFromImage(image_sitk)
216233
ndims = len(array.shape)
217234
metadata_ndims = len(image_sitk.GetSpacing())

0 commit comments

Comments
 (0)