Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
fslpy
Commits
10f4b70f
Commit
10f4b70f
authored
6 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
RF: Don't try and save over a memory-mapped image file
parent
7e2594c9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/data/image.py
+43
-7
43 additions, 7 deletions
fsl/data/image.py
with
43 additions
and
7 deletions
fsl/data/image.py
+
43
−
7
View file @
10f4b70f
...
...
@@ -35,8 +35,10 @@ and file names:
import
os
import
os.path
as
op
import
shutil
import
string
import
logging
import
tempfile
import
six
import
deprecation
...
...
@@ -1165,11 +1167,41 @@ class Image(Nifti):
log
.
debug
(
'
Saving {} to {}
'
.
format
(
self
.
name
,
filename
))
# If this Image is not managing its
# own file object, nibabel does all
# of the hard work.
if
self
.
__fileobj
is
None
:
# own file object, and the image is not
# memory-mapped, nibabel does all of
# 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
)
# 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
# handle to an IndexedGzipFile
else
:
...
...
@@ -1190,15 +1222,19 @@ class Image(Nifti):
# compressed data. And then be able to
# transfer the index generated from the
# write to a new read-only file handle.
newnibimage
=
True
nib
.
save
(
self
.
__nibImage
,
filename
)
self
.
__fileobj
.
close
()
self
.
__nibImage
,
self
.
__fileobj
=
loadIndexedImageFile
(
filename
)
self
.
header
=
self
.
__nibImage
.
header
# We have to create a new ImageWrapper
# instance too, as we have just destroyed
# the nibabel image we gave to the last
# one.
# If we've created a new nibabel image,
# we have to create a new ImageWrapper
# instance too, as we have just destroyed
# the nibabel image we gave to the last
# one.
if
newnibimage
:
self
.
__imageWrapper
.
deregister
(
self
.
__lName
)
self
.
__imageWrapper
=
imagewrapper
.
ImageWrapper
(
self
.
nibImage
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment