Skip to content
\n

Then, I use the following code to get the header, and the fdata:

\n
        header_bytes = cp.asnumpy(image[:348])\n        header = nib.Nifti1Header.from_fileobj(io.BytesIO(header_bytes))\n        data_offset = header.get_data_offset()\n        data_shape = header.get_data_shape()\n        data_dtype = header.get_data_dtype()\n        affine = header.get_best_affine()\n        data = image[data_offset:].view(data_dtype).reshape(data_shape, order=\"F\")\n\n
\n

I have some questions on the above implementation:

\n
    \n
  1. Does Nibabel already have GDS related feature? If so, I can use it directly
  2. \n
  3. Does my way of loading header and using offset, dtype and shape have any issues in some cases?
  4. \n
\n

It would be very appreciate if you can help to answer them, thanks very much!

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"
    \n
  1. No.
  2. \n
  3. I don't know if kvikio.CuFile handles compression, but many NIfTI files are gzipped. You will need to account for that if it's not done automatically. The construction of the header, affine and data seem reasonable. However, if there are scale factors, you will not get the same data. Also, nibabel lazily loads data, so you can get the header and your parameters like:
  4. \n
\n
img = nb.load(filename)\nshape = img.shape\noffset = img.dataobj.offset\ndata_dtype = img.dataobj.dtype\nslope = img.dataobj.slope\ninter = img.dataobj.inter\n\nraw = ...  # Do what's needed to get your cupy array.\nif (slope, inter) != (1, 0):\n    data = slope * raw + inter\nelse:\n    data = raw
","upvoteCount":1,"url":"https://github.com/nipy/nibabel/discussions/1385#discussioncomment-11155176"}}}

Questions on nifti data loading with GPU #1385

Answered by effigies
yiheng-wang-nv asked this question in Q&A
Discussion options

You must be logged in to vote
  1. No.
  2. I don't know if kvikio.CuFile handles compression, but many NIfTI files are gzipped. You will need to account for that if it's not done automatically. The construction of the header, affine and data seem reasonable. However, if there are scale factors, you will not get the same data. Also, nibabel lazily loads data, so you can get the header and your parameters like:
img = nb.load(filename)
shape = img.shape
offset = img.dataobj.offset
data_dtype = img.dataobj.dtype
slope = img.dataobj.slope
inter = img.dataobj.inter

raw = ...  # Do what's needed to get your cupy array.
if (slope, inter) != (1, 0):
    data = slope * raw + inter
else:
    data = raw

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@yiheng-wang-nv
Comment options

@effigies
Comment options

Answer selected by yiheng-wang-nv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants