Skip to content

Commit c507949

Browse files
committed
WIP: Save SpatialImages into HDF5 files.
First commit: functions to save a Nifti1Image into a HDF5 file group and retrieve it back.
1 parent 4a541ca commit c507949

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

nibabel/hdfloadsave.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
4+
#
5+
# See COPYING file distributed along with the NiBabel package for the
6+
# copyright and license terms.
7+
#
8+
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9+
10+
import numpy as np
11+
import h5py
12+
13+
from .nifti1 import Nifti1Image, Nifti1Header
14+
15+
#from .spm2analyze import Spm2AnalyzeImage
16+
#from .nifti1 import Nifti1Image, Nifti1Pair, Nifti1Header, header_dtype as ni1_hdr_dtype
17+
#from .nifti2 import Nifti2Image, Nifti2Pair
18+
#from .minc1 import Minc1Image
19+
#from .minc2 import Minc2Image
20+
#from .freesurfer import MGHImage
21+
22+
def nifti1img_to_hdf(fname, spatial_img, h5path='/img', append=True):
23+
"""
24+
Saves a Nifti1Image into an HDF5 file.
25+
26+
fname: string
27+
Output HDF5 file path
28+
29+
spatial_img: nibabel SpatialImage
30+
Image to be saved
31+
32+
h5path: string
33+
HDF5 group path where the image data will be saved.
34+
Datasets will be created inside the given group path:
35+
'data', 'extra', 'affine', the header information will
36+
be set as attributes of the 'data' dataset.
37+
38+
append: bool
39+
True if you don't want to erase the content of the file
40+
if it already exists, False otherwise.
41+
"""
42+
mode = 'w'
43+
if append:
44+
mode = 'r+'
45+
46+
f = h5py.File(fname, mode)
47+
48+
h5img = f.create_group(h5path)
49+
h5img['data'] = spatial_img.get_data()
50+
h5img['extra'] = spatial_img.get_extra()
51+
h5img['affine'] = spatial_img.get_affine()
52+
53+
hdr = spatial_img.get_header()
54+
for k in hdr.keys():
55+
h5img['data'].attrs[k] = hdr[k]
56+
57+
f.close()
58+
59+
60+
def hdfgroup_to_nifti1image(fname, h5path):
61+
"""
62+
Returns a nibabel Nifti1Image from an HDF5 group datasets
63+
64+
@param fname: string
65+
HDF5 file path
66+
67+
@param h5path:
68+
HDF5 group path in fname
69+
70+
@return: nibabel Nifti1Image
71+
"""
72+
f = h5py.File(fname, 'r')
73+
74+
h5img = f[h5path]
75+
data = h5img['data'].value
76+
extra = h5img['extra'].value
77+
affine = h5img['affine'].value
78+
79+
header = get_nifti1hdr_from_h5attrs(h5img['data'].attrs)
80+
81+
f.close()
82+
83+
img = Nifti1Image(data, affine, header=header, extra=extra)
84+
85+
return img
86+
87+
88+
def get_nifti1hdr_from_h5attrs(h5attrs):
89+
"""
90+
Transforms an H5py Attributes set to a dict.
91+
Converts unicode string keys into standard strings
92+
and each value into a numpy array.
93+
94+
@param h5attrs: H5py Attributes
95+
96+
@return: dict
97+
"""
98+
hdr = Nifti1Header()
99+
for k in h5attrs.keys():
100+
hdr[str(k)] = np.array(h5attrs[k])
101+
102+
return hdr

0 commit comments

Comments
 (0)