Skip to content

Support to load single volumes from 4D images #9

Closed
@mih

Description

@mih

Dave Flitney requested a leaner way to get access to parts of a 4D image (NIfTI in his case), i.e. the ability to get data of a single volume or timeseries. He sent the following sketch::

class FSLNiftiImage(nb.Nifti1Image):
#    def __init__(self):
#       super(FSLNiftiImage, self).__init__()

    @memoized
        def get_fileobj(self):
        fileobj = allopen(self.get_filename())
        return fileobj

    def get_volume(self, v):
        dtype = self.get_data_dtype()
        s = self.get_header().get_data_shape()
        shape = (s[0], s[1], s[2])
        offset = self.get_header().get_data_offset() + (v * np.prod(shape))
        data = array_from_file(shape, dtype, self.get_fileobj(), offset)
        return data

Usage:

image = nb.load(fname)
image.__class__ = FSLNiftiImage
v27 = image.get_volume(27)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions