- Notifications
You must be signed in to change notification settings - Fork269
WIP: Save SpatialImages into HDF5 files.#215
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
alexsavio wants to merge2 commits intonipy:masterChoose a base branch fromalexsavio:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+104 −0
Open
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletionsnibabel/hdfloadsave.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- | ||
| # vi: set ft=python sts=4 ts=4 sw=4 et: | ||
| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
| # | ||
| # See COPYING file distributed along with the NiBabel package for the | ||
| # copyright and license terms. | ||
| # | ||
| ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
| import numpy as np | ||
| import h5py | ||
| from .nifti1 import Nifti1Image, Nifti1Header | ||
| #from .spm2analyze import Spm2AnalyzeImage | ||
| #from .nifti1 import Nifti1Image, Nifti1Pair, Nifti1Header, header_dtype as ni1_hdr_dtype | ||
| #from .nifti2 import Nifti2Image, Nifti2Pair | ||
| #from .minc1 import Minc1Image | ||
| #from .minc2 import Minc2Image | ||
| #from .freesurfer import MGHImage | ||
| def nifti1img_to_hdf(fname, spatial_img, h5path='/img', append=True): | ||
| """ | ||
| Saves a Nifti1Image into an HDF5 file. | ||
| fname: string | ||
| Output HDF5 file path | ||
| spatial_img: nibabel SpatialImage | ||
| Image to be saved | ||
| h5path: string | ||
| HDF5 group path where the image data will be saved. | ||
| Datasets will be created inside the given group path: | ||
| 'data', 'extra', 'affine', the header information will | ||
| be set as attributes of the 'data' dataset. | ||
| append: bool | ||
| True if you don't want to erase the content of the file | ||
| if it already exists, False otherwise. | ||
| @note: | ||
| HDF5 open modes | ||
| >>> 'r' Readonly, file must exist | ||
| >>> 'r+' Read/write, file must exist | ||
| >>> 'w' Create file, truncate if exists | ||
| >>> 'w-' Create file, fail if exists | ||
| >>> 'a' Read/write if exists, create otherwise (default) | ||
| """ | ||
| mode = 'w' | ||
| if append: | ||
| mode = 'a' | ||
| with h5py.File(fname, mode) as f: | ||
| h5img = f.create_group(h5path) | ||
| h5img['data'] = spatial_img.get_data() | ||
| h5img['extra'] = spatial_img.get_extra() | ||
| h5img['affine'] = spatial_img.get_affine() | ||
| hdr = spatial_img.get_header() | ||
| for k in hdr.keys(): | ||
| h5img['data'].attrs[k] = hdr[k] | ||
| def hdfgroup_to_nifti1image(fname, h5path): | ||
| """ | ||
| Returns a nibabel Nifti1Image from an HDF5 group datasets | ||
| @param fname: string | ||
| HDF5 file path | ||
| @param h5path: | ||
| HDF5 group path in fname | ||
| @return: nibabel Nifti1Image | ||
| """ | ||
| with h5py.File(fname, 'r') as f: | ||
| h5img = f[h5path] | ||
| data = h5img['data'][()] | ||
| extra = h5img['extra'][()] | ||
| affine = h5img['affine'][()] | ||
| header = get_nifti1hdr_from_h5attrs(h5img['data'].attrs) | ||
| img = Nifti1Image(data, affine, header=header, extra=extra) | ||
| return img | ||
| def get_nifti1hdr_from_h5attrs(h5attrs): | ||
| """ | ||
| Transforms an H5py Attributes set to a dict. | ||
| Converts unicode string keys into standard strings | ||
| and each value into a numpy array. | ||
| @param h5attrs: H5py Attributes | ||
| @return: dict | ||
| """ | ||
| hdr = Nifti1Header() | ||
| for k in h5attrs.keys(): | ||
| hdr[str(k)] = np.array(h5attrs[k]) | ||
| return hdr |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.