Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
qim3d
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
QIM
Tools
qim3d
Commits
13958fda
Commit
13958fda
authored
1 year ago
by
Christian Kento Rasmussen
Browse files
Options
Downloads
Patches
Plain Diff
added support to save nifti file
parent
96de1a8c
Branches
Branches containing commit
No related tags found
1 merge request
!45
Save files function
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
qim3d/io/save.py
+32
-1
32 additions, 1 deletion
qim3d/io/save.py
with
32 additions
and
1 deletion
qim3d/io/save.py
+
32
−
1
View file @
13958fda
"""
Provides functionality for saving data to various file formats.
"""
"""
Provides functionality for saving data to various file formats.
"""
import
os
import
os
import
tifffile
import
nibabel
as
nib
import
numpy
as
np
import
numpy
as
np
import
tifffile
from
qim3d.io.logger
import
log
from
qim3d.io.logger
import
log
from
qim3d.utils.internal_tools
import
sizeof
,
stringify_path
from
qim3d.utils.internal_tools
import
sizeof
,
stringify_path
...
@@ -90,6 +93,32 @@ class DataSaver:
...
@@ -90,6 +93,32 @@ class DataSaver:
log
.
info
(
f
"
Total of
{
no_slices
}
files saved following the pattern
'
{
pattern_string
}
'"
)
log
.
info
(
f
"
Total of
{
no_slices
}
files saved following the pattern
'
{
pattern_string
}
'"
)
def
save_nifti
(
self
,
path
,
data
):
"""
Save data to a NIfTI file to the given path.
Args:
path (str): The path to save file to
data (numpy.ndarray): The data to be saved
"""
# Create header
header
=
nib
.
Nifti1Header
()
header
.
set_data_dtype
(
data
.
dtype
)
# Create NIfTI image object
img
=
nib
.
Nifti1Image
(
data
,
np
.
eye
(
4
),
header
)
# nib does automatically compress if filetype ends with .gz
if
self
.
compression
and
not
path
.
endswith
(
"
.gz
"
):
path
+=
"
.gz
"
log
.
warning
(
"
File extension
'
.gz
'
is added since compression is enabled.
"
)
if
not
self
.
compression
and
path
.
endswith
(
"
.gz
"
):
path
=
path
[:
-
3
]
log
.
warning
(
"
File extension
'
.gz
'
is ignored since compression is disabled.
"
)
# Save image
nib
.
save
(
img
,
path
)
def
save
(
self
,
path
,
data
):
def
save
(
self
,
path
,
data
):
"""
Save data to the given path.
"""
Save data to the given path.
...
@@ -154,6 +183,8 @@ class DataSaver:
...
@@ -154,6 +183,8 @@ class DataSaver:
if
path
.
endswith
((
"
.tif
"
,
"
.tiff
"
)):
if
path
.
endswith
((
"
.tif
"
,
"
.tiff
"
)):
return
self
.
save_tiff
(
path
,
data
)
return
self
.
save_tiff
(
path
,
data
)
elif
path
.
endswith
((
"
.nii
"
,
"
nii.gz
"
)):
return
self
.
save_nifti
(
path
,
data
)
else
:
else
:
raise
ValueError
(
"
Unsupported file format
"
)
raise
ValueError
(
"
Unsupported file format
"
)
# If there is no file extension in the path
# If there is no file extension in the path
...
...
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