Skip to content

Instantly share code, notes, and snippets.

@kastman
Created September 7, 2012 16:19
Show Gist options
  • Save kastman/3667508 to your computer and use it in GitHub Desktop.
Save kastman/3667508 to your computer and use it in GitHub Desktop.
HVD NRG Nipype Interface to QA
#!/usr/bin/env python
from nipype.interfaces.base import BaseInterface, BaseInterfaceInputSpec, TraitedSpec, traits, InputMultiPath, OutputMultiPath, File
from nipype.utils.filemanip import split_filename
import niftiqa
import os, sys
####### Begin Class niftiqa ########
class niftiqaInputSpec(BaseInterfaceInputSpec):
niftis = InputMultiPath(traits.Either(traits.List(File(exists=True)),File(exists=True)),
desc='List of NIfTI scans to QA', mandatory=True, copyfile=False)
class niftiqaOutputSpec(TraitedSpec):
reports = traits.List(OutputMultiPath(File(exists=True)),
desc="Lift of Image Reports, including mean, snr, and slice graphs.")
class niftiqaInterface(BaseInterface):
input_spec = niftiqaInputSpec
output_spec = niftiqaOutputSpec
def _run_interface(self, runtime):
args = niftiqa.parse_arguments(['--all'])
for image in [os.path.basename(nifti) for nifti in self.inputs.niftis]:
print image
niftiqa.run(image, args)
return runtime
def _list_outputs(self):
outputs = self._outputs().get()
reports = []
# Python has no native flatten(); here it's just a simple lambda.
# This will only handle lists where everything is nested one level deep;
# see flatten_list in the main fmriNipype script for a more robust version using itertools
flatten = lambda nested_list: [item for sublist in nested_list for item in sublist]
for image in self.inputs.niftis:
path, base, ext = split_filename(image)
outfiles = [[
os.path.join(path, base + '_history.html'),
os.path.join(path, base + '_thumbnail_histogram.svg'),
os.path.join(path, base + '_thumbnail_history.html'),
os.path.join(path, base + '_thumbnail.png'),
os.path.join(path, base + '_mean_slice.csv'),
os.path.join(path, base + '_mean_slice.txt'),
os.path.join(path, base + '_mean_slice.svg'),
os.path.join(path, base + '_slice_report.txt'),
os.path.join(path, base + '_slice_report.html')
]]
for calculation in ['slope', 'snr', 'stdev', 'mask', 'mean']:
outfiles.append([
os.path.join(path, base + '_%s_history.html' % calculation ),
os.path.join(path, base + '_%s_thumbnail_histogram.svg' % calculation ),
os.path.join(path, base + '_%s_thumbnail_history.html' % calculation ),
os.path.join(path, base + '_%s_thumbnail.png' % calculation ),
os.path.join(path, base + '_%s.nii' % calculation )
])
reports.append(flatten(outfiles))
outputs['reports'] = reports
return outputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment