Skip to content
Snippets Groups Projects
Commit 10f4b70f authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

RF: Don't try and save over a memory-mapped image file

parent 7e2594c9
No related branches found
No related tags found
No related merge requests found
...@@ -35,8 +35,10 @@ and file names: ...@@ -35,8 +35,10 @@ and file names:
import os import os
import os.path as op import os.path as op
import shutil
import string import string
import logging import logging
import tempfile
import six import six
import deprecation import deprecation
...@@ -1165,11 +1167,41 @@ class Image(Nifti): ...@@ -1165,11 +1167,41 @@ class Image(Nifti):
log.debug('Saving {} to {}'.format(self.name, filename)) log.debug('Saving {} to {}'.format(self.name, filename))
# If this Image is not managing its # If this Image is not managing its
# own file object, nibabel does all # own file object, and the image is not
# of the hard work. # memory-mapped, nibabel does all of
if self.__fileobj is None: # the hard work.
newnibimage = False
ismmap = isinstance(self[0, 0, :10], np.memmap)
if self.__fileobj is None and (not ismmap):
nib.save(self.__nibImage, filename) nib.save(self.__nibImage, filename)
# If the image is memory-mapped, we need
# to close and re-open the image
elif self.__fileobj is None:
# We save the image out to a temp file,
# then close the old image, move the
# temp file to the real destination,
# then re-open the file.
newnibimage = True
tmphd, tmpfname = tempfile.mkstemp(suffix=op.splitext(filename)[1])
os.close(tmphd)
try:
nib.save(self.__nibImage, tmpfname)
self.__nibImage = None
self.header = None
shutil.copy(tmpfname, filename)
self.__nibImage = nib.load(filename)
self.header = self.__nibImage.header
except Exception:
os.remove(tmpfname)
raise
# Otherwise we've got our own file # Otherwise we've got our own file
# handle to an IndexedGzipFile # handle to an IndexedGzipFile
else: else:
...@@ -1190,15 +1222,19 @@ class Image(Nifti): ...@@ -1190,15 +1222,19 @@ class Image(Nifti):
# compressed data. And then be able to # compressed data. And then be able to
# transfer the index generated from the # transfer the index generated from the
# write to a new read-only file handle. # write to a new read-only file handle.
newnibimage = True
nib.save(self.__nibImage, filename) nib.save(self.__nibImage, filename)
self.__fileobj.close() self.__fileobj.close()
self.__nibImage, self.__fileobj = loadIndexedImageFile(filename) self.__nibImage, self.__fileobj = loadIndexedImageFile(filename)
self.header = self.__nibImage.header self.header = self.__nibImage.header
# We have to create a new ImageWrapper # If we've created a new nibabel image,
# instance too, as we have just destroyed # we have to create a new ImageWrapper
# the nibabel image we gave to the last # instance too, as we have just destroyed
# one. # the nibabel image we gave to the last
# one.
if newnibimage:
self.__imageWrapper.deregister(self.__lName) self.__imageWrapper.deregister(self.__lName)
self.__imageWrapper = imagewrapper.ImageWrapper( self.__imageWrapper = imagewrapper.ImageWrapper(
self.nibImage, self.nibImage,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment