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
68b48c48
Commit
68b48c48
authored
8 months ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
MNT: Work-around nibabel auto-rescaling logic so that data/slope/intercept in imcp'd images
is not modified
parent
d6eaaeee
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/utils/imcp.py
+23
-3
23 additions, 3 deletions
fsl/utils/imcp.py
with
23 additions
and
3 deletions
fsl/utils/imcp.py
+
23
−
3
View file @
68b48c48
...
...
@@ -50,7 +50,8 @@ def imcp(src,
:func:`~fsl.data.image.defaultOutputType`. If the
source file does not have the same type as the default
extension, it will be converted. If ``False``, the
source file type is not changed.
source file type is used, and the destination file type
(if specified) is ignored.
:arg move: If ``True``, the files are moved, instead of being
copied. See :func:`immv`.
...
...
@@ -143,7 +144,7 @@ def imcp(src,
img
=
nib
.
load
(
src
)
# Force conversion to specific
data type
if
# Force conversion to specific
file format
if
# necessary. The file format (pair, gzipped
# or not) is taken care of automatically by
# nibabel
...
...
@@ -151,7 +152,26 @@ def imcp(src,
elif
'
NIFTI2
'
in
destType
.
name
:
cls
=
nib
.
Nifti2Image
elif
'
NIFTI
'
in
destType
.
name
:
cls
=
nib
.
Nifti1Image
img
=
cls
(
np
.
asanyarray
(
img
.
dataobj
),
None
,
header
=
img
.
header
)
# The default behaviour of nibabel when saving
# is to rescale the image data to the full range
# of the data type, and then set the scl_slope/
# inter header fields accordingly. This is highly
# disruptive in many circumstances. Fortunately:
# - The nibabel ArrayProxy class provides a
# get_unscaled method, which allows us to
# bypass the rescaling at load time.
# - Explicitly setting the slope and intercept
# on the header allows us to bypass rescaling
# at save time.
#
# https://github.com/nipy/nibabel/issues/1001
# https://neurostars.org/t/preserve-datatype-and-precision-with-nibabel/27641/2
slope
=
img
.
dataobj
.
slope
inter
=
img
.
dataobj
.
inter
data
=
np
.
asanyarray
(
img
.
dataobj
.
get_unscaled
(),
dtype
=
img
.
get_data_dtype
())
img
=
cls
(
data
,
None
,
header
=
img
.
header
)
img
.
header
.
set_slope_inter
(
slope
,
inter
)
nib
.
save
(
img
,
dest
)
# Make sure the image reference is cleared, and
...
...
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