Skip to content

Commit 7933fae

Browse files
committed
FIX: Clean up header values before raising exception in AnalyzeImage.to_file_map
1 parent 4fa74c7 commit 7933fae

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

nibabel/analyze.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,13 +1020,24 @@ def to_file_map(self, file_map=None, dtype=None):
10201020
inter = hdr['scl_inter'].item() if hdr.has_data_intercept else np.nan
10211021
# Check whether to calculate slope / inter
10221022
scale_me = np.all(np.isnan((slope, inter)))
1023-
if scale_me:
1024-
arr_writer = make_array_writer(data,
1025-
out_dtype,
1026-
hdr.has_data_slope,
1027-
hdr.has_data_intercept)
1028-
else:
1029-
arr_writer = ArrayWriter(data, out_dtype, check_scaling=False)
1023+
try:
1024+
if scale_me:
1025+
arr_writer = make_array_writer(data,
1026+
out_dtype,
1027+
hdr.has_data_slope,
1028+
hdr.has_data_intercept)
1029+
else:
1030+
arr_writer = ArrayWriter(data, out_dtype, check_scaling=False)
1031+
except WriterError:
1032+
# Restore any changed consumable values, in case caller catches
1033+
# Should match cleanup at the end of the method
1034+
hdr.set_data_offset(offset)
1035+
hdr.set_data_dtype(data_dtype)
1036+
if hdr.has_data_slope:
1037+
hdr['scl_slope'] = slope
1038+
if hdr.has_data_intercept:
1039+
hdr['scl_inter'] = inter
1040+
raise
10301041
hdr_fh, img_fh = self._get_fileholders(file_map)
10311042
# Check if hdr and img refer to same file; this can happen with odd
10321043
# analyze images but most often this is because it's a single nifti

0 commit comments

Comments
 (0)