Example #1
0
def makeImageCorrections(inputImg):
    """Make various corrections to problematic images

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    masterFunc = '/Volumes/Data/IcewordNEW/sub005/Func/Run1/sub005-run1-RAW.nii.gz'
    masterFSE = '/Volumes/Data/IcewordNEW/sub005/Morph/sub005-fse-RAW.nii.gz'
    masterSPGR = '/Volumes/Data/IcewordNEW/sub005/Morph/sub005-spgr-RAW.nii.gz'
    subj, scan, outImg = getOutName(inputImg)

    if scan in 'fse' and inputImg not in masterFSE:
        return __MakeImageCorrections(masterFSE, inputImg)

    elif scan in 'spgr' and inputImg not in masterSPGR:
        return __MakeImageCorrections(masterSPGR, inputImg)

    elif inputImg not in masterFunc:
        return __MakeImageCorrections(masterFunc, inputImg)

    else:
        getImgData(inputImg, outImg)
        logImageStats(outImg)
        return outImg
Example #2
0
def logImageStats(inputImg, baseVol=''):
    """Log robust_min robust_max mean sd statistics to log file
       for each input dataset

    Params:
        inputImg -- Image file to log computed statistics for.
        baseVol -- The image volume determined to be the most stable

    Returns:
        If a log does not exist already, a new one with appended image
        stats will be created. Otherwise, append image statistics.
    """
    subj, scan = getOutName(inputImg)[0:2]
    image = os.path.split(inputImg)[-1]
    stats = [image]

    path = os.path.join(ICEWORD, subj, 'Func')
    logFile = os.path.join(path, 'log_preproc.txt')

    template = '{0}\t{1}\t{2}\t{3}\t{4}\n'
    header = ['image', 'robust_min', 'robust_max', 'mean', 'sd']

    fslstats = 'fslstats ' + inputImg + ' -r -m -s'
    imgStats = os.popen(fslstats).read().split()
    stats.extend(imgStats)

    if os.path.exists(logFile):
        fout = open(logFile, 'a')
        fout.write(template.format(*stats))
    else:
        fout = open(logFile, 'w')
        fout.write(template.format(*header))
        fout.write(template.format(*stats))
    fout.close()
Example #3
0
def makeImageCorrections(inputImg):
    """Make various corrections to problematic images

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    masterFunc = '/Volumes/Data/IcewordNEW/sub005/Func/Run1/sub005-run1-RAW.nii.gz'
    masterFSE = '/Volumes/Data/IcewordNEW/sub005/Morph/sub005-fse-RAW.nii.gz'
    masterSPGR = '/Volumes/Data/IcewordNEW/sub005/Morph/sub005-spgr-RAW.nii.gz'
    subj, scan, outImg = getOutName(inputImg)

    if scan in 'fse' and inputImg not in masterFSE:
        return __MakeImageCorrections(masterFSE, inputImg)

    elif scan in 'spgr' and inputImg not in masterSPGR:
        return __MakeImageCorrections(masterSPGR, inputImg)

    elif inputImg not in masterFunc:
        return __MakeImageCorrections(masterFunc, inputImg)

    else:
        getImgData(inputImg, outImg)
        logImageStats(outImg)
        return outImg
Example #4
0
def __MakeImageCorrections(masterImg, inputImg):
    """Modify inputImg to match masterImg

    This function uses 3dResample to make major structural changes
    either to the voxel sizes, the image orientation, or both. If the
    input image is of a Functional type, then perform 3drefit first to
    ensure that the TR time is correct, then proceed to resampling.
    When resampling has completed, remove the original inputFile,
    and rename the outputFile to that of the old inputFile.

    Params:
        masterImg -- The master image file which the inputImg should
                     be matched to.
        inputImg -- The image file to be adjusted.

    Returns:
         Description of returns
    """
    subj, scan, outImg = getOutName(inputImg)
    resample = ' '.join([
        '3dresample -master', masterImg, '-prefix', outImg, '-inset', inputImg
    ])
    refit = ' '.join(['3drefit -TR', masterImg, inputImg])

    os.system(refit)
    os.system(resample)
    logImageStats(outImg)
    plotImageStats(outImg)
    return outImg
Example #5
0
def __MakeImageCorrections(masterImg, inputImg):
    """Modify inputImg to match masterImg

    This function uses 3dResample to make major structural changes
    either to the voxel sizes, the image orientation, or both. If the
    input image is of a Functional type, then perform 3drefit first to
    ensure that the TR time is correct, then proceed to resampling.
    When resampling has completed, remove the original inputFile,
    and rename the outputFile to that of the old inputFile.

    Params:
        masterImg -- The master image file which the inputImg should
                     be matched to.
        inputImg -- The image file to be adjusted.

    Returns:
         Description of returns
    """
    subj, scan, outImg = getOutName(inputImg)
    resample = ' '.join(['3dresample -master', masterImg, '-prefix', outImg, '-inset', inputImg])
    refit = ' '.join(['3drefit -TR', masterImg, inputImg])

    os.system(refit)
    os.system(resample)
    logImageStats(outImg)
    plotImageStats(outImg)
    return outImg
Example #6
0
def logImageStats(inputImg, baseVol=''):
    """Log robust_min robust_max mean sd statistics to log file
       for each input dataset

    Params:
        inputImg -- Image file to log computed statistics for.
        baseVol -- The image volume determined to be the most stable

    Returns:
        If a log does not exist already, a new one with appended image
        stats will be created. Otherwise, append image statistics.
    """
    subj, scan = getOutName(inputImg)[0:2]
    image = os.path.split(inputImg)[-1]
    stats = [image]

    path = os.path.join(ICEWORD, subj, 'Func')
    logFile = os.path.join(path, 'log_preproc.txt')

    template = '{0}\t{1}\t{2}\t{3}\t{4}\n'
    header = ['image', 'robust_min', 'robust_max', 'mean', 'sd']

    fslstats = 'fslstats ' + inputImg + ' -r -m -s'
    imgStats = os.popen(fslstats).read().split()
    stats.extend(imgStats)

    if os.path.exists(logFile):
        fout = open(logFile, 'a')
        fout.write(template.format(*stats))
    else:
        fout = open(logFile, 'w')
        fout.write(template.format(*header))
        fout.write(template.format(*stats))
    fout.close()
Example #7
0
def despikeVolume(inputImg):
    """Remove spikes from the data by fitting a smoothish curve to the data.
        -nomask prevents automasking during despiking. We will use a different
        mask later on (generated from the skull stripped T1), so we don't
        want to automask now. Despiking may negatively affect volreg, and so
        should be done after volreg.

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    outImg = getOutName(inputImg, 'despike')
    spikes = getOutName(inputImg, 'spikes')
    despike = ' '.join(['3dDespike -nomask -prefix', outImg[-1],
                                '-ssave', spikes[-1], inputImg])
    os.system(despike)
    logImageStats(outImg[-1])
    plotImageStats(outImg[-1])  # Produce a plot of the image stats
    return outImg[-1]
Example #8
0
def despikeVolume(inputImg):
    """Remove spikes from the data by fitting a smoothish curve to the data.
        -nomask prevents automasking during despiking. We will use a different
        mask later on (generated from the skull stripped T1), so we don't
        want to automask now. Despiking may negatively affect volreg, and so
        should be done after volreg.

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    outImg = getOutName(inputImg, 'despike')
    spikes = getOutName(inputImg, 'spikes')
    despike = ' '.join([
        '3dDespike -nomask -prefix', outImg[-1], '-ssave', spikes[-1], inputImg
    ])
    os.system(despike)
    logImageStats(outImg[-1])
    plotImageStats(outImg[-1])  # Produce a plot of the image stats
    return outImg[-1]
Example #9
0
def smoothVolume(inputImg, kernel='7.0'):
    """ Smooth image at fwhm using 7.0mm kernel.

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz
        kernel -- The size of the gaussian kernel deisred. Default is 7mm

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    outImg = getOutName(inputImg, '7mm')
    smooth = ' '.join(['3dmerge -1blur_fwhm', kernel, '-doall -prefix',
                                                 outImg[-1], inputImg])
    os.system(smooth)
    logImageStats(outImg[-1])
    plotImageStats(outImg[-1])
    return outImg[-1]
Example #10
0
def sliceTiming(inputImg):
    """ Run 3dTshift to temporally align the slices in each volume to the
        middle slice using sepminus. Uses Fourier interpolation by default,
        which is slowest but the most accurate

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    outImg = getOutName(inputImg, 'tshift')
    tshift = ' '.join(['3dTshift -tpattern seqminus -prefix', outImg[-1], inputImg])
    os.system(tshift)
    logImageStats(outImg[-1])
    plotImageStats(outImg[-1])

    return outImg[-1]
Example #11
0
def smoothVolume(inputImg, kernel='7.0'):
    """ Smooth image at fwhm using 7.0mm kernel.

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz
        kernel -- The size of the gaussian kernel deisred. Default is 7mm

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    outImg = getOutName(inputImg, '7mm')
    smooth = ' '.join([
        '3dmerge -1blur_fwhm', kernel, '-doall -prefix', outImg[-1], inputImg
    ])
    os.system(smooth)
    logImageStats(outImg[-1])
    plotImageStats(outImg[-1])
    return outImg[-1]
Example #12
0
def volumeTrim(inputImg, trim):
    """Remove first n volumes from image

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    print '\n\tTrimming Volumes: ', trim

    outImg = getOutName(inputImg, '213tr', 'RealignDetails')
    trunc = '[' + trim + '..$]'
    tcat = ' '.join(['3dTcat -verb -prefix', outImg[-1], inputImg]) + trunc
    os.system(tcat)
    logImageStats(outImg[-1])
    plotImageStats(outImg[-1])

    return outImg[-1]
Example #13
0
def sliceTiming(inputImg):
    """ Run 3dTshift to temporally align the slices in each volume to the
        middle slice using sepminus. Uses Fourier interpolation by default,
        which is slowest but the most accurate

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    outImg = getOutName(inputImg, 'tshift')
    tshift = ' '.join(
        ['3dTshift -tpattern seqminus -prefix', outImg[-1], inputImg])
    os.system(tshift)
    logImageStats(outImg[-1])
    plotImageStats(outImg[-1])

    return outImg[-1]
Example #14
0
def volumeTrim(inputImg, trim):
    """Remove first n volumes from image

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    print '\n\tTrimming Volumes: ', trim

    outImg = getOutName(inputImg, '213tr', 'RealignDetails')
    trunc = '[' + trim + '..$]'
    tcat = ' '.join(['3dTcat -verb -prefix', outImg[-1], inputImg]) + trunc
    os.system(tcat)
    logImageStats(outImg[-1])
    plotImageStats(outImg[-1])

    return outImg[-1]
Example #15
0
def volumeReg(inputImg, baseVol='0'):
    """ One line description

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    outImg = getOutName(inputImg, 'volreg')
    baseImg = inputImg + '[' + baseVol + ']'
    dfile = '_'.join([outImg[-1].split('.')[0], 'dfile.1D'])
    maxdisp = '_'.join([outImg[-1].split('.')[0], 'mm.1D'])
    volreg = ' '.join(['3dvolreg -zpad 4 -base', baseImg,
                       '-1Dfile', dfile, '-maxdisp1D', maxdisp,
                       '-prefix', outImg[-1], '-Fourier', inputImg])
    os.system(volreg)
    logImageStats(outImg[-1], baseVol)
    plotImageStats(outImg[-1])  # Produce a plot of the image stats
    plotImageStats(outImg[-1], dfile)

    return outImg[-1]
Example #16
0
def baseVolume(inputImg):
    """Find best volume (smallest outlier value) for base realignment
       volume. By default, this uses the whole image (brain and nonbrain)
       when it looks for outliers.

    Params:
        inputImg -- 4d functional of the form runX_subXXX.nii.gz

    Returns:
        An integer value representing the the base realignment volume.
    """
    subj, scan = getOutName(inputImg)[:2]
    outcount = ' '.join(['3dToutcount', inputImg])
    result = [int(count.strip()) for count in os.popen(outcount).readlines()]
    base = str(result.index(min(result)))

    baseLog = os.path.join(ICEWORD, subj, 'Func', scan, 'base_Volume.txt')
    fout = open(baseLog, 'w')
    fout.write('{0}\t{1}\n'.format('base Volume', base))
    fout.close()

    return base
Example #17
0
def baseVolume(inputImg):
    """Find best volume (smallest outlier value) for base realignment
       volume. By default, this uses the whole image (brain and nonbrain)
       when it looks for outliers.

    Params:
        inputImg -- 4d functional of the form runX_subXXX.nii.gz

    Returns:
        An integer value representing the the base realignment volume.
    """
    subj, scan = getOutName(inputImg)[:2]
    outcount = ' '.join(['3dToutcount', inputImg])
    result = [int(count.strip()) for count in os.popen(outcount).readlines()]
    base = str(result.index(min(result)))

    baseLog = os.path.join(ICEWORD, subj, 'Func', scan, 'base_Volume.txt')
    fout = open(baseLog, 'w')
    fout.write('{0}\t{1}\n'.format('base Volume', base))
    fout.close()

    return base
Example #18
0
def volumeReg(inputImg, baseVol='0'):
    """ One line description

    Params:
        inputImg -- The image to be trimmed -> /PATH/TO/sub001-run1.nii.gz

    Returns:
         String of output image name -> /PATH/TO/sub001-run1-213tr.nii.gz
    """
    outImg = getOutName(inputImg, 'volreg')
    baseImg = inputImg + '[' + baseVol + ']'
    dfile = '_'.join([outImg[-1].split('.')[0], 'dfile.1D'])
    maxdisp = '_'.join([outImg[-1].split('.')[0], 'mm.1D'])
    volreg = ' '.join([
        '3dvolreg -zpad 4 -base', baseImg, '-1Dfile', dfile, '-maxdisp1D',
        maxdisp, '-prefix', outImg[-1], '-Fourier', inputImg
    ])
    os.system(volreg)
    logImageStats(outImg[-1], baseVol)
    plotImageStats(outImg[-1])  # Produce a plot of the image stats
    plotImageStats(outImg[-1], dfile)

    return outImg[-1]