Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible not optimal zipping for nii.gz? #566

Closed
SebastianoF opened this issue Oct 20, 2017 · 4 comments
Closed

Possible not optimal zipping for nii.gz? #566

SebastianoF opened this issue Oct 20, 2017 · 4 comments

Comments

@SebastianoF
Copy link
Member

Hi,
In loading and re-saving an image with nibabel I noticed an increase in memory.
Below some IO from an ipython session:

In [19]: im = nib.load('initial.nii.gz')

In [20]: ! du -h initial.nii.gz
388K	initial.nii.gz

In [21]: im.get_data_dtype()
Out[21]: dtype('<u2')

In [22]: im.set_data_dtype('<u2')

In [23]: nib.save(im, 'initial_re_saved_same_dtype.nii.gz')

In [24]: ! du -h initial_re_saved_same_dtype.nii.gz
872K	initial_re_saved_same_dtype.nii.gz

Here I open the image with ITK-snap, and re-save it under the name initial_re_saved_same_dtype_re_Saved_itksnap.nii.gz:

In [25]: ! du -h initial_re_saved_same_dtype_re_Saved_itksnap.nii.gz
388K	initial_re_saved_same_dtype_re_Saved_itksnap.nii.gz

Same issue happens even if not changing the data-type, but only in loading and re-saving.

In [26]: im = nib.load('initial.nii.gz')

In [27]: nib.save(im, 'initial_just_re_saved.nii.gz')

In [28]: ! du -h initial_just_re_saved.nii.gz
872K	initial_just_re_saved.nii.gz

Is it an issue or am I doing something wrong? Any possible workaround?
Thanks!

@effigies
Copy link
Member

Possibly related: #382

You can adjust the default compression level. I can look up where, if you can't quickly find it. (I think it's in openers.py.)

@matthew-brett
Copy link
Member

Nibabel uses a relatively low level of compression by default. This usually doesn't make much difference, but maybe that's not true for you image. Try doing:

nib.openers.Opener.default_compresslevel = 9

before you load and save, does that help?

@SebastianoF
Copy link
Member Author

In [1]: import nibabel as nib

In [2]: nib.openers.Opener.default_compresslevel = 9

In [3]: im = nib.load('initial.nii.gz')

In [4]: ! du -h initial.nii.gz
388K	initial.nii.gz

In [5]: nib.save(im, 'initial_just_re_saved.nii.gz')

In [6]: ! du -h initial_just_re_saved.nii.gz
352K	initial_just_re_saved.nii.gz

Bingo!
Thanks for the quick answer!

@satra
Copy link
Member

satra commented Oct 20, 2017

@matthew-brett - would it make sense to up the default to something like 5? may provide a reasonable balance between time to compress and compression ratio. looks like decompression isn't bad in terms of time.

https://catchchallenger.first-world.info/wiki/Quick_Benchmark:_Gzip_vs_Bzip2_vs_LZMA_vs_XZ_vs_LZ4_vs_LZO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants