Skip to content

Commit 72f4c3d

Browse files
committed
BF: disable float128 datadtype if no binary128
We should not be using float128 data format in nifti if we don't have binary128 format on this machine. Closes gh-131.
1 parent 77ea78c commit 72f4c3d

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

nibabel/nifti1.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .quaternions import fillpositive, quat2mat, mat2quat
2121
from . import analyze # module import
2222
from .spm99analyze import SpmAnalyzeHeader
23+
from .casting import have_binary128
2324

2425
# Needed for quaternion calculation
2526
FLOAT32_EPS_3 = -np.finfo(np.float32).eps * 3
@@ -76,13 +77,12 @@
7677
header_dtype = np.dtype(header_dtd)
7778

7879
# datatypes not in analyze format, with codes
79-
try:
80-
_float128t = np.float128
81-
except AttributeError:
80+
if have_binary128():
81+
# Only enable 128 bit floats if we really have IEEE binary 128 longdoubles
82+
_float128t = np.longdouble
83+
_complex256t = np.longcomplex
84+
else:
8285
_float128t = np.void
83-
try:
84-
_complex256t = np.complex256
85-
except AttributeError:
8686
_complex256t = np.void
8787

8888
_dtdefs = ( # code, label, dtype definition, niistring

nibabel/tests/test_nifti1.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import numpy as np
1616

17-
from ..casting import type_info
17+
from ..casting import type_info, have_binary128
1818
from ..tmpdirs import InTemporaryDirectory
1919
from ..spatialimages import HeaderDataError
2020
from ..affines import from_matvec
@@ -309,6 +309,14 @@ def test_binblock_is_file(self):
309309
hdr.write_to(str_io)
310310
assert_equal(str_io.getvalue(), hdr.binaryblock + ZEROB * 4)
311311

312+
def test_float128(self):
313+
hdr = self.header_class()
314+
if have_binary128():
315+
hdr.set_data_dtype(np.longdouble)
316+
assert_equal(hdr.get_data_dtype().type, np.longdouble)
317+
else:
318+
assert_raises(HeaderDataError, hdr.set_data_dtype, np.longdouble)
319+
312320

313321
class TestNifti1Image(tana.TestAnalyzeImage):
314322
# Run analyze-flavor spatialimage tests

0 commit comments

Comments
 (0)