How can I import NIFTI files to DSCMRIAnalysis module?

Dear Dr. Fedorov,

I’ve decided to correct motion of 4D DSC dicom files.
However, after motion correction, the 4D DSC dicom files should be in NIFTI format.
So I saved the NIFTI output file into NRRD file using slicer.util.loadVolume, and slicer.util.saveNode using Python.
However, I can’t load 4D images using slicer.util.loadVolume.
Could you please make NIFTI files imported to DSCMRIAnalysis module as well as DICOM files?
I tried to do it on my own using Python script, which did not work well.
I referred to this post and link.
If it’s impossible, then I’m gonna trying to do it by myself, but I think I need your hint.

So sorry to bother you again.
Thank you for your help!

Best,
Kyu Sung

Recently we added a functionality that allows import of 4D NIfTI as MultiVolumes. It has not been thoroughly tested, so it would be great if you could try it out and see if it helps.

To use it, you will need to place your 4D NIfTI file into a separate directory, and use MultiVolumeImporter module. Since your NIfTI file will not contain the metadata needed for the analysis (such as timing information), you will need to use MultiVolumeImporter to initialize those.

If the import is working for you from the GUI, you could then look into this function to automate the load process.

@pieper added this functionality to MultiVolumeImporter, so I cc him here so that he can follow the conversation.

p.s. In the future, if you want to bring my attention to your post, you should mention me with @fedorov - this way I will receive notification about your message. I do not regularly check all posts on the forum, so just mentioning my name does not mean I will read your message!

1 Like

Dear Dr. @fedorov ,

Following your instruction, the import of a 4D NIFTI file did work for me from the GUI.
However, I cannot make python file to load 4D NIFTI file and save as NRRD file.
A simple python code I wrote to load a 3D NIFTI file to save as a NRRD file is attached below (actually, I am quite a beginner to python), which works good for me.
image

import os 
import slicer 

path1 = "/home/cndl/DSC/test/3d_nifti/"
path2 = "/home/cndl/DSC/test/nrrd/"
os.mkdir(path2)
list = os.listdir(path1)

for filename in sorted(list):
  [success, loadedVolumeNode] = slicer.util.loadVolume(path1+filename, returnNode=True)
  fn_ext = os.path.splitext(filename)
  true_fn_ext = os.path.splitext(fn_ext[0])
  slicer.util.saveNode(loadedVolumeNode, path2+true_fn_ext[0]+".nrrd")

exit()

So I tried to write similar codes with the function you mentioned.
In the function, “read4DNIfTI” was the method of class “MultiVolumeImporterWidget”, so I wrote like,

import os 
import slicer 

path1 = "/home/cndl/DSC/test/4d_nifti/"
path2 = "/home/cndl/DSC/test/nrrd/"
os.mkdir(path2)
list = os.listdir(path1)

for filename in sorted(list):
  [success, loadedVolumeNode] = slicer.util.loadVolume(path1+filename, returnNode=True)
  mvNode = MultiVolumeImporterWidget.read4DNIfTI(loadedVolumeNode, path1+filename)
  fn_ext = os.path.splitext(filename)
  true_fn_ext = os.path.splitext(fn_ext[0])
  slicer.util.saveNode(mvNode , path2+true_fn_ext[0]+".nrrd")

exit()

,which gave me an error that ‘MultiVolumeImporterWidget’ is not defined.

image

I know there are a lot of mistakes in the code, which I can’t fix.
Could you please help me fix this code?

All the best,
Kyu Sung

@pieper could you help @Kyu_Sung_Choi with programmatically invoking the 4d NIfTI read function you wrote?

The MultiVolumeImporter doesn’t have a Logic class, the code below creates the widget and then runs the converter. It’s a little ugly but it looks like it works (I don’t have any 4D nii data to try but it works on a 3D volume). If you find you are doing a lot of this and don’t like the widget popping up then you could easily refactor this method for use outside of the widget.

import os 
import slicer 

import MultiVolumeImporter

importer = MultiVolumeImporter.MultiVolumeImporterWidget()


mvNode = slicer.mrmlScene.CreateNodeByClass('vtkMRMLMultiVolumeNode')
slicer.mrmlScene.AddNode(mvNode)

niiFilePath = "/Users/pieper/data//prostate-MR-US/MRProstate.nii"

importer.read4DNIfTI(mvNode, niiFilePath)

slicer.util.saveNode(mvNode , "/tmp/converted.nrrd")

exit()
2 Likes

Dear Dr. @pieper and Dr. @fedorov,

Thank you so much!
It works perfectly, and moreover, I think reading multiple DSC files in the folder works fine with the code below. (There are a few warnings, though.)

import os
import slicer
import MultiVolumeImporter

path1 = “/home/cndl/DSC/test/4d_nifti/”
path2 = “/home/cndl/DSC/test/nrrd/”
os.mkdir(path2)
list = os.listdir(path1)

for filename in sorted(list):
#[success, loadedVolumeNode] = slicer.util.loadVolume(path1+filename, returnNode=True) ## for 3D NIFTI
importer = MultiVolumeImporter.MultiVolumeImporterWidget()

mvNode = slicer.mrmlScene.CreateNodeByClass(‘vtkMRMLMultiVolumeNode’)
slicer.mrmlScene.AddNode(mvNode)

importer.read4DNIfTI(mvNode, path1+filename)
fn_ext = os.path.splitext(filename)
true_fn_ext = os.path.splitext(fn_ext[0])
slicer.util.saveNode(mvNode, path2+true_fn_ext[0]+“.nrrd”)

exit()

image

However, after I load these 3 NRRD files (test_DSC, test2_DSC, and TCGA-08-0524_DSC.nrrd) and enter as input to DSCMRIAnalysis module in command line, it does not recognize frame identifying DICOM tag (error msg below). When I enter just 1 NRRD file saved with the code above as an input file, same error msg pops up. Maybe this NRRD file is not the same NRRD file that I obtained using mpReviewPreprocessor.py (the post you answer is linked here)

image

Could you please fix this problem?
I always appreciate it.

All the best,
Kyu Sung

I need to make a volume reconstruction evaluation form: Imagedata.nii, transformdata.npy and timespampdata.npy. These data are collected form tracked ultrasound images using PLUS libraray.

I am not being able on getting around this problem. I tried MultivolumeImporter volume giving the directory of imagedata.nii and transformdata.npy, but there is an error coming out: Select outpot node… after I created a new node! I know it is possible to merge all above mentioned data into one nrrd file, but I am not that experienced in programming!

Thank you!

PLUS toolkit does not use nifti or npy file formats. How did you create these images?

Anyway, if you can save your data in PLUS sequence file format (.mha or .nrrd) and install SlicerIGSIO extension then you can load the files into Slicer (by drag-and-drop or “Add data”) and reconstruct it using “Volume Reconstruction” module.

Thank you for your reply!

I have been working with “SlicerIGSIO” and “Volume Reconstruction” modules, but with .nrrd files (generated from PLUS toolkit, as you mentioned)!

The data output from the PLUS toolkit was .nrrd file format. After recording, the data was converted to .npy (transform data and timestamps) and .nii (image data) for storage and transmission feasibility. Is it possible to make such a merge of data in Slicer or through Python Interactor in Slicer?

It is probably easier to write the content of your nii/npy files back to a nrrd/nhdr file. It has a simple text file header, followed by images as a binary blob - complete specification is available here.

Nifti (.nii) file format is developed specifically for functional brain imaging and not well suited for any other application areas. There seems to be a confusion among machine learning people and they use it for all kinds of images, but don’t follow them.

Npz is not widely supported by anything else than numpy, but there seem to be some relatively lightweight npz file readers/writers in C++ (https://github.com/ceptontech/cnpy, https://github.com/pmontalb/NpyCpp), so we may consider supporting a numpy-based file format in the future. PLUS supports compressed streaming writing of tracked image data, which may not be feasible with npz though, so it may not be that easy to fully switch to that in PLUS/IGSIO.

For now, I think sequence meta/nrrd files are still your best bet for exchanging and archiving tracked image data (of course you can convert them into other formats during a processing workflow).

I also thought meta/nrrd is the best option. A module supporting Nifti format would be great tho!

Thanks for the support!

Nifti header cannot contain more than a couple of bytes of custom data, so it cannot be used for storing tracked image data. Neuroimaging people use json sidecar files as a workaround, but that’s quite inconvenient and error-prone. Nifti has other limitations, too, and lots of complexities that are only necessary if someone is storing neuro MRI images. So, I don’t think we’ll add support for it in PLUS/IGSIO.