dfsp-spirit / freesurferformats

GNU R package for reading and writing neuroimaging file formats. Not limited to FreeSurfer anymore, despite its name.
Other
23 stars 3 forks source link

[Question] re-orient data in read.fs.volume #13

Closed thomshaw92 closed 4 years ago

thomshaw92 commented 4 years ago

Hi, is it possible to turn off the re-orient data feature of read.fs.volume? Is there a flag to do this?

> library(freesurferformats)
> read.fs.volume(
+   "brainWarp_jacobian.nii.gz",
+   format = "auto",
+   flatten = TRUE,
+   with_header = FALSE,
+   drop_empty_dims = FALSE
+ )

Error in performPermutation(trans, real.dimensions, data, verbose) : 
  Transformation is not simple, cannot reorient!
dfsp-spirit commented 4 years ago

That error message comes from the oro.nifti package. To work around it, try:

ni = oro.nifti::readNIfTI("brainWarp_jacobian.nii.gz", reorient = FALSE);
read.fs.volume.nii(ni);

In case you want to use the image in combination with other data, make sure they align. The error message suggests that the s-form/q-form in the file contains a spatial transformation matrix with a non-trivial transformation, and by using the commands above you are ignoring this transformation. This may or may not be irrelevant, depending on what you want to do.

Please also keep in mind that the NIFTI to MGH conversion code in freesurferformats is still pretty experimental, I am working on it as we speak. Let me know how it goes.

A side note: depending on what you want to do, you could use the data from the ni directly, without calling read.fs.volume.nii() at all.

Just to be sure: does your file contain voxel-wise data (a volume) or vertex-wise data (a surface overlay)?

thomshaw92 commented 4 years ago

Thanks for that info! I used the code, and yes it worked! I want to vectorize the information from multiple niftis (all in the same space). You mentioned i don't need to call read.fs.volume.nii()... How would this look if I was trying to vectorize around 10 niftis into 1D array? Yes they are volume files, not surfaces... Thanks for the great tool!

dfsp-spirit commented 4 years ago

If you want a list of vectorized data for your images, you could try:

niifiles = c("~/brain1.nii.gz", "~/brain2.nii.gz")
nii_data = lapply(niifiles, function(nii) {read.fs.volume.nii(oro.nifti::readNIfTI(nii, reorient = FALSE), flatten = T)})

You would get a list, at each entry is a vector with the data from one image. Here is the number of voxels in the first image:

length(nii_data[[1]])

...and here the histogram for the 2nd one:

hist(nii_data[[2]])

However, it is not clear to me why you would want a vector of the values, and you should take great care with any assumptions regarding the spatial orientation or equivalence of positions between the images. You are fine if you only compute stuff like summary statistics (but you would not need the vectorization for that).

But if all you want is a vector from the raw data from the NIFTI image, you do not need freesurferformats at all, the oro.nifti package alone will do, (and you should thank the authors of the oro.nifti package instead of me):

niifiles = c("~/brain1.nii.gz", "~/brain2.nii.gz")
nii_data = lapply(niifiles, function(niifile) { ni = oro.nifti::readNIfTI(niifile, reorient = FALSE); return(as.vector(ni@.Data))})
thomshaw92 commented 4 years ago

OK Great! I did get this error though:

 Error in read.fs.volume.nii(oro.nifti::readNIfTI(nii, reorient = FALSE),  : 
  Nifti images with @datatype=64 and @bitpix=64 not supported yet. 

But I will just convert them to short datatype first (I don't suppose you have a function for that? Otherwise, will do it with FSL or something.)

Thank you so much for that - These are satistical images in a common (template) space, so I am looking for a vector to actually compare where there are spatial differences by running some stats in R. So I'm kind of hoping for differences :) Thanks again for your help!

dfsp-spirit commented 4 years ago

Have a look at my updated comment, you do not need freesurferformats at all. The last part explains how to go directly with oro.nifti.

If they are in a common template space, that is fine. If you can't do the analysis in 3D, take care when you transform back to 3D in the end (because in the end, you will want to know where in the brain your differences are, at voxel 132451421 is not gonna help).

thomshaw92 commented 4 years ago

Well thank you again! That is very, very useful! I appreciate the help!

dfsp-spirit commented 4 years ago

You're welcome, good luck with your analysis.