Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Michiel Cottaar
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
No related tags found
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:
...
@@ -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
,
...
...
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