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
9d329801
Commit
9d329801
authored
1 year ago
by
Christian Kento Rasmussen
Browse files
Options
Downloads
Patches
Plain Diff
added support for compressed nifti
parent
bbc63538
No related branches found
No related tags found
2 merge requests
!102
Conv zarr tiff folders
,
!100
Conv zarr nifti
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
qim3d/io/convert.py
+19
-10
19 additions, 10 deletions
qim3d/io/convert.py
with
19 additions
and
10 deletions
qim3d/io/convert.py
+
19
−
10
View file @
9d329801
...
...
@@ -9,6 +9,7 @@ import zarr
from
tqdm
import
tqdm
from
qim3d.utils.internal_tools
import
stringify_path
from
qim3d.io.saving
import
save
class
Convert
:
...
...
@@ -21,17 +22,23 @@ class Convert:
self
.
chunk_shape
=
kwargs
.
get
(
"
chunk_shape
"
,
(
64
,
64
,
64
))
def
convert
(
self
,
input_path
,
output_path
):
def
get_file_extension
(
file_path
):
root
,
ext
=
os
.
path
.
splitext
(
file_path
)
if
ext
in
[
'
.gz
'
,
'
.bz2
'
,
'
.xz
'
]:
# handle common compressed extensions
root
,
ext2
=
os
.
path
.
splitext
(
root
)
ext
=
ext2
+
ext
return
ext
# Stringify path in case it is not already a string
input_path
=
stringify_path
(
input_path
)
input_ext
=
os
.
path
.
splitext
(
input_path
)
[
1
]
output_ext
=
os
.
path
.
splitext
(
output_path
)
[
1
]
input_ext
=
get_file_extension
(
input_path
)
output_ext
=
get_file_extension
(
output_path
)
output_path
=
stringify_path
(
output_path
)
if
os
.
path
.
isfile
(
input_path
):
match
input_ext
,
output_ext
:
case
(
"
.tif
"
,
"
.zarr
"
)
|
(
"
.tiff
"
,
"
.zarr
"
):
return
self
.
convert_tif_to_zarr
(
input_path
,
output_path
)
case
(
"
.nii
"
,
"
.zarr
"
):
case
(
"
.nii
"
,
"
.zarr
"
)
|
(
"
.nii.gz
"
,
"
.zarr
"
):
return
self
.
convert_nifti_to_zarr
(
input_path
,
output_path
)
case
_
:
raise
ValueError
(
"
Unsupported file format
"
)
...
...
@@ -41,7 +48,9 @@ class Convert:
case
(
"
.zarr
"
,
"
.tif
"
)
|
(
"
.zarr
"
,
"
.tiff
"
):
return
self
.
convert_zarr_to_tif
(
input_path
,
output_path
)
case
(
"
.zarr
"
,
"
.nii
"
):
return
self
.
convert_zarr_to_tif
(
input_path
,
output_path
)
return
self
.
convert_zarr_to_nifti
(
input_path
,
output_path
)
case
(
"
.zarr
"
,
"
.nii.gz
"
):
return
self
.
convert_zarr_to_nifti
(
input_path
,
output_path
,
compress
=
True
)
case
_
:
raise
ValueError
(
"
Unsupported file format
"
)
# Fail
...
...
@@ -71,7 +80,7 @@ class Convert:
"""
vol
=
tiff
.
memmap
(
tif_path
)
z
=
zarr
.
open
(
zarr_path
,
mode
=
"
w
"
,
shape
=
vol
.
shape
,
chunks
=
self
.
chunk
s
,
dtype
=
vol
.
dtype
zarr_path
,
mode
=
"
w
"
,
shape
=
vol
.
shape
,
chunks
=
self
.
chunk
_shape
,
dtype
=
vol
.
dtype
)
chunk_shape
=
tuple
((
s
+
c
-
1
)
//
c
for
s
,
c
in
zip
(
z
.
shape
,
z
.
chunks
))
# ! Fastest way is z[:] = vol[:], but does not have a progress bar
...
...
@@ -99,7 +108,7 @@ class Convert:
None
"""
z
=
zarr
.
open
(
zarr_path
)
tiff
.
imwrit
e
(
tif_path
,
z
)
sav
e
(
tif_path
,
z
)
def
convert_nifti_to_zarr
(
self
,
nifti_path
,
zarr_path
):
"""
Convert a nifti file to a zarr file
...
...
@@ -113,7 +122,7 @@ class Convert:
"""
vol
=
nib
.
load
(
nifti_path
).
dataobj
z
=
zarr
.
open
(
zarr_path
,
mode
=
"
w
"
,
shape
=
vol
.
shape
,
chunks
=
self
.
chunk
s
,
dtype
=
vol
.
dtype
zarr_path
,
mode
=
"
w
"
,
shape
=
vol
.
shape
,
chunks
=
self
.
chunk
_shape
,
dtype
=
vol
.
dtype
)
chunk_shape
=
tuple
((
s
+
c
-
1
)
//
c
for
s
,
c
in
zip
(
z
.
shape
,
z
.
chunks
))
# ! Fastest way is z[:] = vol[:], but does not have a progress bar
...
...
@@ -130,7 +139,7 @@ class Convert:
return
z
def
convert_zarr_to_nifti
(
self
,
zarr_path
,
nifti_path
):
def
convert_zarr_to_nifti
(
self
,
zarr_path
,
nifti_path
,
compress
=
False
):
"""
Convert a zarr file to a nifti file
Args:
...
...
@@ -141,7 +150,8 @@ class Convert:
None
"""
z
=
zarr
.
open
(
zarr_path
)
nib
.
save
(
nib
.
Nifti1Image
(
z
,
np
.
eye
(
4
)),
nifti_path
)
save
(
nifti_path
,
z
,
compress
=
compress
)
def
convert
(
input_path
:
str
,
output_path
:
str
,
chunk_shape
:
tuple
=
(
64
,
64
,
64
)):
"""
Convert a file to another format without loading the entire file into memory
...
...
@@ -151,6 +161,5 @@ def convert(input_path: str, output_path: str, chunk_shape: tuple = (64, 64, 64)
output_path (str): path to the output file
chunk_shape (tuple, optional): chunk size for the zarr file. Defaults to (64, 64, 64).
"""
converter
=
Convert
(
chunk_shape
=
chunk_shape
)
converter
.
convert
(
input_path
,
output_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