How to create a Niftimage object?

Hi, I am trying to learn nipy / python tools for analyzing fMRI data. In the past I’ve always used MATLAB. I have >15yrs experience with Matlab, including with MRI data, and not a lot of experience with Python, to set the scene…

Anyways, once I got a dataset of my own pre-processed using some of the tutorial code from nipype, I started computing some basic statistical stuff to get more familiar just with the environment. My first mistake, it seems was this:

``````for data in dataset:
``````

dat is now a list of EPI series in the form of numpy arrays, i.e. dat = [[64x64x24x188], [64x64x24x188], …]

Now I can treat numerically it to my heart’s delight. I am basically using Python as a MATLAB.

But then, I think, I’d like to visualize some of this, or start to make use of some tools. Nilearn.plotting has some things I’d like to try, e.g. plotting.plot_stat_map().

But now I am stuck, because the input to plot_stat_map(img), or just about anything else interesting, is a nifti object, not a numpy array.

I thought that perhaps I could take a nifti image of the dataset, e.g.:

``````D = nb.load(fpath + fname)
``````

and switch out (with adjustments) the raw data for my processed numbers, and then view this. But I don’t understand how to even start. So, perhaps, I could have been processing it from the start while keeping the object format? Or… how should I be going about this?

Is it possible to ‘synthesize’ a nifti object, e.g. using an existing one (and all that it knows about the alignment, scale, etc, of the raw data) and processed numerical data? Or is this stupid or illegal to consider?

Basically, I want to do whatever I like with the numbers in the volume, but still interact with the environment. That’s my level. If my situation is clear to anyone I would appreciate any pointers or suggestions, relevant tutorials etc.

Thanks!
-andrew

Ok, only after giving up and posting my question did I find an answer:

https://nipy.org/nibabel/nibabel_images.html

where it says:

Array images

We can also create images from numpy arrays. For example:

``````array_data = np.arange(24, dtype=np.int16).reshape((2, 3, 4))
affine = np.diag([1, 2, 3, 1])
array_img = nib.Nifti1Image(array_data, affine)
``````

This might help.

But still, if anyone wants to suggest some direction for me I’d appreciate it.

Hi @amhaun01, I think indeed your answer works. I can also recommend to look into the function `nilearn.image.new_img_like`. With this you can provide a reference image `ref_niimg` as your original nifti image and then define the parameter `data` with your processed data in the form of a numpy array. This will return a new `Niimg-like object` with the processed data that you can then use as input for plotting. Let me know if this helps answer your question!

great! thanks, this does seem more complete.