Skip to content

N4BiasFieldCorrection output! #2806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mrahimpour opened this issue Nov 27, 2018 · 13 comments
Closed

N4BiasFieldCorrection output! #2806

mrahimpour opened this issue Nov 27, 2018 · 13 comments

Comments

@mrahimpour
Copy link

mrahimpour commented Nov 27, 2018

Summary

Hi,

I am trying to use N4BiasFieldCorrection function imported from nipype.interfaces.ants to preprocess my MRI data. I am running the example code you have provided and I have this line :

" N4BiasFieldCorrection --bspline-fitting [ 300 ] -d 3 --input-image /home/masoomeh/MR_Preprocess/data/Brats17_CBICA_AQA_1/Normalized_Brats17_CBICA_AQA_1_flair.nii --convergence [ 50x50x30x20 ] --output /home/masoomeh/MR_Preprocess/data/Brats17_CBICA_AQA_1/corrected.nii --shrink-factor 3'
"

But the problem is that there is no output on my disk!
I'll be grateful for any debugging advice or other insight​.

Platform details:

{'commit_hash': '%h',
 'commit_source': 'archive substitution',
 'networkx_version': '2.1',
 'nibabel_version': '2.3.0',
 'nipype_version': '1.1.2',
 'numpy_version': '1.15.0',
 'pkg_path': '/home/masoomeh/opt/anaconda3/lib/python3.5/site-packages/nipype',
 'scipy_version': '1.1.0',
 'sys_executable': '/home/masoomeh/opt/anaconda3/bin/python',
 'sys_platform': 'linux',
 'sys_version': '3.5.5 |Anaconda, Inc.| (default, May 13 2018, 21:12:35) \n'
                '[GCC 7.2.0]',
 'traits_version': '4.6.0'}

Execution environment

Python 3.5

@effigies
Copy link
Member

Hi, could you provide the code you're running?

@mrahimpour
Copy link
Author

This is the code I am running:

import copy
from nipype.interfaces.ants import N4BiasFieldCorrection
n4 = N4BiasFieldCorrection()
n4.inputs.dimension = 3
n4.inputs.bspline_fitting_distance = 300
n4.inputs.shrink_factor = 3
n4.inputs.n_iterations = [50, 50, 30, 20]
n4.inputs.input_image = "/home/masoomeh/MR_Preprocess/data/Brats17_CBICA_AQA_1/Normalized_Brats17_CBICA_AQA_1_flair.nii"
n4.inputs.output_image ="/home/masoomeh/MR_Preprocess/data/Brats17_CBICA_AQA_1/corrected.nii "
n4.cmdline

@effigies
Copy link
Member

Can you run:

res = n4.run()
print(res.outputs)

@mrahimpour
Copy link
Author

Great! It worked.
If I want to define an individual function of "biascorrection" using "N4BiasFieldCorrection" block, should I return "res.outputs" ?
Doing so, I got the following error:

AttributeError: 'NoneType' object has no attribute 'outputs'.

I also tried "res.output_image" as an output of N4BiasFieldCorrection.

Thanks for your help!

@effigies
Copy link
Member

Sorry, I don't understand the question. What are you trying to do?

@mrahimpour
Copy link
Author

mrahimpour commented Nov 27, 2018

I want to create a node doing bias field correction like this:

bias = pe.Node(Function(input_names=["in_file"],
                           output_names=["bias_corrected"],
                           function=biasfield_correct), 
                           name="bias_correction")

While function biasfield_correct has been defined as follows:

def biasfield_correct(in_file):
   
    from nipype.interfaces.ants import N4BiasFieldCorrection
    n4 = N4BiasFieldCorrection()
    n4.inputs.dimension = 3
    n4.inputs.input_image = in_file
    n4.inputs.bspline_fitting_distance = 300
    n4.inputs.shrink_factor = 3
    n4.inputs.n_iterations = [50, 50, 30, 20]
    n4.inputs.convergence_threshold = 1e-6
    #n4.inputs.bspline_order = 5
    #n4.inputs.save_bias = True
    res = n4.run()

    return (res.outputs) 

putting this node in my workflow, I have the error: "res.output_image" as an output of N4BiasFieldCorrection!

@effigies
Copy link
Member

You can wrap the interface in a Node without going through a Function interface:

from nipype.pipeline import engine as pe
from nipype.interfaces import ants

bias = pe.Node(
    N4BiasFieldCorrection(
        dimension=3,
        bspline_fitting_distance=300,
        shrink_factor=3,
        n_iterations=[50, 50, 30, 20],
        convergence_threshold=1e-6),
    name='bias_correction')

Then you would connect to input_image, rather than in_file, but otherwise it should be the same. And

res = bias.run()
res.outputs

should produce essentially the same results as running the interface directly, but all of the outputs should be contained in the Node's working directory.

I have the error: "res.output_image" as an output of N4BiasFieldCorrection!

I'm not sure how this would arise from what you've posted.

@mrahimpour
Copy link
Author

mrahimpour commented Nov 28, 2018

Hi,
Thank you for your help. Now it is working.
Sorry, I have posted the wrong error(I forgot to paste the error here) and I could not reproduce it!!

I have another problem with input stream, Selectfile node when I want to run the whole pipeline.

Following is my pipeline:

#1: import appropriate modules

import nipype.pipeline.engine as pe                        # the workflow and node wrappers
from nipype.interfaces.utility import IdentityInterface
from nipype.interfaces.ants import N4BiasFieldCorrection
from nipype.interfaces.io import SelectFiles, DataSink

import os

#2: Specify variables
base_dir = "/home/masoomeh/MR_Preprocess/"
output_dir = os.path.join(os.path.dirname(base_dir), "out")
in_file = "/home"   
subject_id = ["Brats17_CBICA_AQA_1"]

#3: Specify nodes
anat_input = pe.Node(IdentityInterface(fields=in_file, mandatory_inputs=True),name="anat_input")
biascor = pe.Node(N4BiasFieldCorrection(input_image = in_file,
                                        dimension=3,
        				bspline_fitting_distance=300,
       					shrink_factor=3,
        				n_iterations=[50, 50, 30, 20],
        				convergence_threshold=1e-6),
					name = "bias_correction")

#4: Create the workflow object, specify workflow 
wf = pe.Workflow(name='preproc')
wf.base_dir = '.'


#5: Input and output stream

templates = {'anat': 'data/{subject_id}/struct.nii'}
selectfiles = pe.Node(SelectFiles(templates,
                                  base_directory=base_dir), name="selectfiles")


datasink = pe.Node(DataSink(parameterization=False), name="datasink")
datasink.inputs.base_directory = "/home/masoomeh/MR_Preprocess/"
datasink.inputs.container = output_dir
substitutions = [('_subject_id_', '')]
datasink.inputs.substitutions = substitutions


infosource = pe.Node(IdentityInterface(fields=["subject_id"]), name="infosource")

wf.connect([
           # Connect SelectFiles and DataSink to the workflow
           (infosource, selectfiles, [("subject_id", "subject_id")]),
           (selectfiles, anat_input, [("anat", "in_file")]),
           (anat_input, biascor, [("in_file", "input_image")]),
           (anat_input, datasink, [("in_file", "output_anat")]),
            # input to datasink
           (biascor,   datasink    , [("output_image", "output_bias")]),
           ])
#6. run worflow
wf.write_graph()

wf.run()

Running the code, I have this error:

raise IOError(msg)
OSError: No files were found matching anat template: /home/masoomeh/MR_Preprocess/data/<undefined>/struct.nii

This is for only one data, while I want to extend it for more data.
I have not understood how to specify input stream!
I also need to initialize in_file (in_file = "/home" ), otherwise, I'll have this error:
NameError: name 'in_file' is not defined

Thanks in advance for your help!

@effigies
Copy link
Member

anat_input = pe.Node(IdentityInterface(fields=in_file, mandatory_inputs=True),name="anat_input")

in_file == '/home', so I'm not really sure what this will do. I suspect what you want is fields=['in_file'].

biascor = pe.Node(N4BiasFieldCorrection(input_image = in_file,

Again, same issue, but I think it gets overridden by your connect statement below. You can just remove input_image=in_file here.

infosource = pe.Node(IdentityInterface(fields=["subject_id"]), name="infosource")

It doesn't appear you ever supply a subject ID to infosource. I'm guessing you actually want this to be an iterable.

I assume your datasink is okay, but it's really impossible to say without running.

@mrahimpour
Copy link
Author

mrahimpour commented Nov 30, 2018

Now I get the iterable idea and my pipeline is working quite well for whole subjects each includes different modalities.

infosource = pe.Node(IdentityInterface(fields=["subject_id","session_id"]), name="infosource")

infosource.iterables = [("subject_id", subject_id),
("session_id", session_id)]

templates = {'anat': 'data/{subject_id}/{session_id}.nii'}
selectfiles = pe.Node(SelectFiles(templates, base_directory=base_dir), name="selectfiles")

wf.connect([
(infosource, selectfiles, [("subject_id", "subject_id"),
("session_id","session_id")]),
...
])

Thank you very much for all your help!

@effigies
Copy link
Member

No problem!

@biyoner
Copy link

biyoner commented Dec 20, 2018

Hello , @effigies
In my application, I try "n4.run()" mentioned before, while getting an IOError: No command "N4BiasFieldCorrection" found. Is there any configuration problem or something wrong with my code?

def n4_corrction(im_input):
      n4 = N4BiasFieldCorrection()
       n4.inputs.dimension=3
       n4.inputs.input_image = im_input
       n4.inputs.bspline_fitting_distance = 300
       n4.inputs.shrink_factor = 3
       n4.inputs.n_iterations = [50, 50, 30, 20]
       n4.inputs.output_image = im_input.replace('.nii.gz', '_correcred.nii.gz')
       n4.run()

@satra
Copy link
Member

satra commented Dec 20, 2018

this usually happens if the command is not available in your path. try opening a terminal and see if N4BiasFieldCorrection is available in the shell.

also if you are using tools like spyder or pycharm, you will need to ensure that shell variables are exposed.

or if you are familiar with docker, you can create a docker image with ants and nipype (using neurodocker) and run the script you are trying to run within the docker instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants