Skip to content

Commit 98ea9b5

Browse files
committed
BF - quaternion fill threshold was not adjusted for float32 data
1 parent 6823b96 commit 98ea9b5

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

nibabel/nifti1.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
from . import analyze # module import
1919
from .spm99analyze import SpmAnalyzeHeader
2020

21+
# Needed for quaternion calculation
22+
FLOAT32_EPS_3 = -np.finfo(np.float32).eps * 3
23+
2124
# nifti1 flat header definition for Analyze-like first 348 bytes
2225
# first number in comments indicates offset in file header in bytes
2326
header_dtd = [
@@ -578,7 +581,8 @@ def get_qform_quaternion(self):
578581
'''
579582
hdr = self._header_data
580583
bcd = [hdr['quatern_b'], hdr['quatern_c'], hdr['quatern_d']]
581-
return fillpositive(bcd)
584+
# Adjust threshold to fact that source data was float32
585+
return fillpositive(bcd, FLOAT32_EPS_3)
582586

583587
def get_qform(self):
584588
''' Return 4x4 affine matrix from qform parameters in header '''

nibabel/quaternions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ def fillpositive(xyz, w2_thresh=None):
8787
# If necessary, guess precision of input
8888
if w2_thresh is None:
8989
try: # trap errors for non-array, integer array
90-
w2_thresh = -np.finfo(xyz.dtype).eps
90+
w2_thresh = -np.finfo(xyz.dtype).eps * 3
9191
except (AttributeError, ValueError):
92-
w2_thresh = -FLOAT_EPS
92+
w2_thresh = -FLOAT_EPS * 3
9393
# Use maximum precision
9494
xyz = np.asarray(xyz, dtype=MAX_FLOAT)
9595
# Calculate w

nibabel/tests/test_nifti1.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,14 @@ def test_quaternion():
205205
hdr['quatern_b'] = 0
206206
hdr['quatern_c'] = 0
207207
hdr['quatern_d'] = 0
208-
yield assert_true, np.allclose(hdr.get_qform_quaternion(),
209-
[1.0, 0, 0, 0])
208+
assert_true(np.allclose(hdr.get_qform_quaternion(), [1.0, 0, 0, 0]))
210209
hdr['quatern_b'] = 1
211210
hdr['quatern_c'] = 0
212211
hdr['quatern_d'] = 0
213-
yield assert_true, np.allclose(hdr.get_qform_quaternion(),
214-
[0, 1, 0, 0])
212+
assert_true(np.allclose(hdr.get_qform_quaternion(), [0, 1, 0, 0]))
213+
# Check threshold set correctly for float32
214+
hdr['quatern_b'] = 1+np.finfo(np.float32).eps
215+
assert_array_almost_equal(hdr.get_qform_quaternion(), [0, 1, 0, 0])
215216

216217

217218
def test_qform():

0 commit comments

Comments
 (0)