Easiest way to save parameter estimates to nifti

64 views
Skip to first unread message

Paul Glad Mihai

unread,
Nov 18, 2016, 5:51:39 AM11/18/16
to GLMdenoise
Hi Kendrick,

After calculating the parameter estimates I wanted to save them to nifti files. I ran into trouble, as I tried it with make_nii and then save_nii. Make_nii creates a structure with hdr and img, where img is mostly made up of zeros, except for one slice which has integers. What did I do wrong? Here's the command I used:

nii = make_nii(results.modelmd{2}(:,:,:,iCond), [1.1 1.1 1.1], [], ...
            r.run1.hdr.dime.datatype);

The datatype is 512, taken from the header of the loaded nifti from run 1.

Is this the right way to do it, or do you have a better way?

Regards,
Glad


Kendrick Kay

unread,
Nov 18, 2016, 9:19:18 AM11/18/16
to Paul Glad Mihai, GLMdenoise
Maybe something like this?


% load an existing volume NIFTI
a1 = load_untouch_nii(niftifile);

% get the data to be saved
vol = results.modelmd{2}(:,:,:,iCond);  % one volume, X x Y x Z

% hack it up
a1.img = single(vol);    % shove in the data as single format
a1.hdr.dime.dim(5) = 1;  % we have only one volume
a1.hdr.dime.datatype = 16;  % indicate single (float) format ["help save_nii" will tell you about datatypes]
a1.hdr.dime.bitpix = 16;

% save and gzip
save_untouch_nii(a1,newfile);
gzip(newfile);
delete(newfile);



Paul Glad Mihai

unread,
Nov 18, 2016, 10:32:54 AM11/18/16
to GLMdenoise, pgm...@googlemail.com, k...@umn.edu
Thanks for the quick reply.

If I use save_untouch_nii it tells me to use save_nii.m and aborts.

It looks a bit better but it's not perfect. It looks like only a part of the image is saved or it is shifted and cut. I've attached a figure, so you can see what I mean.

Does it work for you?

Kendrick Kay

unread,
Nov 18, 2016, 10:36:06 AM11/18/16
to Paul Glad Mihai, GLMdenoise
Maybe there is some voxel dimension info in the header that is mis-set.  Let's take this offline to investigate your code.


--
You received this message because you are subscribed to the Google Groups "GLMdenoise" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glmdenoise+...@googlegroups.com.
To post to this group, send email to glmde...@googlegroups.com.
Visit this group at https://groups.google.com/group/glmdenoise.
For more options, visit https://groups.google.com/d/optout.

Paul Glad Mihai

unread,
Nov 18, 2016, 11:18:10 AM11/18/16
to GLMdenoise, pgm...@googlemail.com, k...@umn.edu
Kendrick was kind enough to help me with my troubles.

The solution is to "piggyback on a file you trust", as he put it. That means you either open a nifti untouched:

a1=load_untouch_nii('DATA.nii');

and then add your image data and other relevant information to the a1 structure:
vol = results.modelmd{2}(:,:,:,iCond);  % parameter estimates from GLMDenoise for a specific condition.
a1.img = single(vol);  % data in single format
a1.hdr.dime.dim(5) = 1;  % only one
a1.hdr.dime.datatype = 16;  % indicate single (float) format
a1.hdr.dime.bitpix = 16;

Or, if you already loaded a run previously you can just piggyback on that one, without needing to load a new nifti.

r.run1 = load_untouch_nii(filePath);
...
a1 = r.run1;  % piggyback on previously load_untouched nifti
a1.img = single(vol);  % data in single format
a1.hdr.dime.dim(5) = 1;  % only one
a1.hdr.dime.datatype = 16;  % indicate single (float) format
a1.hdr.dime.bitpix = 16;
Reply all
Reply to author
Forward
0 new messages