NIfTI.jl icon indicating copy to clipboard operation
NIfTI.jl copied to clipboard

bug with Integer valued niftis

Open vsaase opened this issue 4 years ago • 9 comments

When creating a Integer valued nifti: ni = NIVolume(Int16.(round.(rand(2,2,2)))) its type is NIVolume{Float32,3,Array{Int16,3}} I am not sure why, but it may have to do with the return type of getindex being Float32

Then when writing the nifti it becomes corrupted, as the header always says Float32 datatype

vsaase avatar Sep 11 '19 11:09 vsaase

ok, the reason are the lines like NIVolume{typeof(one(T)*1f0+1f0),N,typeof(raw)}(header, extensions, raw)

which then leads to niupdate corrupting the header

vsaase avatar Sep 11 '19 12:09 vsaase

@vsaase I found essentially the same bug. My solution is different though. I wonder why you solved it the way you did? What would be the advantage relative to my proposal? (Just wondering if I should chuck mine, in case yours is superior.)

PetrKryslUCSD avatar Dec 08 '20 23:12 PetrKryslUCSD

I'm tempted to drop the whole distinction between "raw" and actual data type. That would make fixing this straightforward. Thoughts?

Tokazama avatar Dec 09 '20 12:12 Tokazama

I am not clear on whether this distinction reflects something in the description of the format or not. If it doesn't, it would certainly make things more straightforward if the implementation did not distinguish.

PetrKryslUCSD avatar Dec 09 '20 15:12 PetrKryslUCSD

Technically there are two values for adding and multiplying the raw data. These serve to minimize the file size if you don't have enough unique values to need a Float64. I'm pretty sure this was developed with Matlab in mind where it can be painful to manage that manually.

Tokazama avatar Dec 09 '20 16:12 Tokazama

Hi and thank you for a nice package.

I stumbled into this type of problem and found that @PetrKryslUCSD 's solution in #46 solved it for me. If it does not break anything, it would be fantastic if his suggestion could be added to the package.

abrandberg avatar Aug 31 '21 20:08 abrandberg

I'm going to be making a fairly breaking change here soon that will fix this. I'm mostly getting dependencies registered and working.

Tokazama avatar Sep 13 '21 15:09 Tokazama

I am having the same problem with integer valued niftis. #57 #46 seems to be related to the same issue. Is there any chance that this will be fixed soon? This issue might be a critical deal-breaker.

By the way, for a workaround, I have used FSL fslmaths utility (here) to cast my int16 nifti to float32:

fslmaths /path/to/my_nifti.nii.gz -mul 1.0 /path/to/my_nifti_float32.nii.gz 

After doing this, niread niwrite works. Might be helpful if someone is looking for a quick workaround where it is not important to preserve the integer nature of the input niftis.

ofgulban avatar Mar 19 '22 11:03 ofgulban

Sorry I haven't had time to fix this. I'm not sure if I'll have time to figure it out for a couple weeks but if someone makes a PR I could take a look

Tokazama avatar Mar 20 '22 00:03 Tokazama