1111import numpy as np
1212import h5py
1313import warnings
14+ from collections import namedtuple
1415from nibabel .loadsave import load as _nbload
1516from nibabel import funcs as _nbfuncs
1617from nibabel .nifti1 import intent_codes as INTENT_CODES
@@ -92,7 +93,7 @@ def shape(self):
9293class ImageGrid (SampledSpatialData ):
9394 """Class to represent spaces of gridded data (images)."""
9495
95- __slots__ = ["_affine" , "_inverse" , "_ndindex" ]
96+ __slots__ = ["_affine" , "_inverse" , "_ndindex" , "_header" ]
9697
9798 def __init__ (self , image ):
9899 """Create a gridded sampling reference."""
@@ -101,6 +102,7 @@ def __init__(self, image):
101102
102103 self ._affine = image .affine
103104 self ._shape = image .shape
105+ self ._header = getattr (image , "header" , None )
104106
105107 self ._ndim = getattr (image , "ndim" , len (image .shape ))
106108 if self ._ndim >= 4 :
@@ -117,6 +119,11 @@ def affine(self):
117119 """Access the indexes-to-RAS affine."""
118120 return self ._affine
119121
122+ @property
123+ def header (self ):
124+ """Access the original reference's header."""
125+ return self ._header
126+
120127 @property
121128 def inverse (self ):
122129 """Access the RAS-to-indexes affine."""
@@ -293,12 +300,15 @@ def apply(
293300 )
294301
295302 if isinstance (_ref , ImageGrid ): # If reference is grid, reshape
303+ hdr = None
304+ if _ref .header is not None :
305+ hdr = _ref .header .copy ()
306+ hdr .set_data_dtype (output_dtype )
296307 moved = spatialimage .__class__ (
297308 resampled .reshape (_ref .shape ).astype (output_dtype ),
298309 _ref .affine ,
299- spatialimage . header
310+ hdr ,
300311 )
301- moved .set_data_dtype (output_dtype )
302312 return moved
303313
304314 return resampled
0 commit comments