From 6abd4abf93c01b06716ab1a53f46f386179af3d8 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Fri, 28 Feb 2020 15:02:59 +0000
Subject: [PATCH] RF,BF: Nifti.header is now protected via property setter -
 Failure in Image.save was resulting in a Nifti without a header.

---
 fsl/data/image.py | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/fsl/data/image.py b/fsl/data/image.py
index 2060aad04..d39259a9e 100644
--- a/fsl/data/image.py
+++ b/fsl/data/image.py
@@ -254,7 +254,7 @@ class Nifti(notifier.Notifier, meta.Meta):
                                                          shape,
                                                          pixdim)
 
-        self.header           = header
+        self.__header         = header
         self.__shape          = shape
         self.__origShape      = origShape
         self.__pixdim         = pixdim
@@ -492,6 +492,24 @@ class Nifti(notifier.Notifier, meta.Meta):
         return ''.join([c for c in val if c in string.printable]).strip()
 
 
+    @property
+    def header(self):
+        """Return a reference to the ``nibabel`` header object. """
+        return self.__header
+
+
+    @header.setter
+    def header(self, header):
+        """Replace the ``nibabel`` header object managed by this ``Nifti``
+        with a new header. The new header must have the same dimensions,
+        voxel size, and orientation as the old one.
+        """
+        new = Nifti(header)
+        if not (self.sameSpace(new) and self.ndim == new.ndim):
+            raise ValueError('Incompatible header')
+        self.__header = new
+
+
     @property
     def niftiVersion(self):
         """Returns the NIFTI file version:
@@ -1411,12 +1429,6 @@ class Image(Nifti):
 
             nib.save(self.__nibImage, tmpfname)
 
-            # nibabel should close any old
-            # file handles when the image/
-            # header refs are deleted
-            self.__nibImage = None
-            self.header     = None
-
             # Copy to final destination,
             # and reload from there
             imcp.imcp(tmpfname, filename, overwrite=True)
-- 
GitLab