Esempio n. 1
0
def main():

    usage = "usage: %prog [options] arg1 arg2"

    parser = argparse.ArgumentParser(prog='label_connected_components')

    parser.add_argument("in_nii", help="Filename of NIFTI input label ")
    parser.add_argument(
        "--out_nii",
        help="Filename of NIFTI output label. (default = label.<in> ) ",
        default=None)

    parser.add_argument('-l',
                        '--lower',
                        help="Lower threshold. (-infinity)",
                        type=float,
                        default=-numpy.inf)
    parser.add_argument('-u',
                        '--upper',
                        help="Upper threshold. (+infinity) ",
                        type=float,
                        default=numpy.inf)

    inArgs = parser.parse_args()

    if inArgs.out_nii == None:
        out_filename = _utilities.add_prefix_to_filename(
            inArgs.in_nii, 'label.')
    else:
        out_filename = inArgs.out_nii

    label_connected_components(inArgs.in_nii, out_filename, inArgs.lower,
                               inArgs.upper)

    return 0
Esempio n. 2
0
def main():

    ## Parsing Arguments

    usage = "usage: %prog [options] arg1 arg2"

    parser = argparse.ArgumentParser(prog='remove')

    parser.add_argument("in_nii", help="Filename of NIFTI input label ")
    parser.add_argument(
        "--out_nii",
        help=
        "Filename of NIFTI output label. If <out_nii> is set to in then overwrite "
        "input file (default = remove.<in_nii> )")

    parser.add_argument('-r',
                        "--remove",
                        help="Labels to remove",
                        type=float,
                        nargs="*",
                        default=[])
    parser.add_argument("--csv",
                        help="CSV filename containing labels to remove",
                        default=[])

    parser.add_argument("-v",
                        "--verbose",
                        help="Verbose flag",
                        action="store_true",
                        default=False)

    inArgs = parser.parse_args()

    # Set output filename
    if inArgs.out_nii == None:
        out_filename = util.add_prefix_to_filename(inArgs.in_nii, 'remove.')

    elif inArgs.out_nii == 'in':
        out_filename = inArgs.in_nii

    else:
        out_filename = inArgs.out_nii

    remove(inArgs.in_nii, inArgs.remove, inArgs.csv, out_filename)
Esempio n. 3
0
def main():

    ## Parsing Arguments
    #
    #

    usage = "usage: %prog [options] arg1 arg2"

    parser = argparse.ArgumentParser(prog='keep')

    parser.add_argument("in_nii", help="Filename of NIFTI input label ")
    parser.add_argument(
        "--out_nii",
        help="Filename of NIFTI output label. (default = keep.<in_nii> )")

    parser.add_argument('-k',
                        "--keep",
                        help="Labels to remove",
                        type=float,
                        nargs="*",
                        default=[])
    parser.add_argument("--csv",
                        help="CSV filename containing labels to remove",
                        default=[])

    parser.add_argument("-v",
                        "--verbose",
                        help="Verbose flag",
                        action="store_true",
                        default=False)

    inArgs = parser.parse_args()

    if inArgs.out_nii == None:
        out_filename = util.add_prefix_to_filename(inArgs.in_nii, 'keep.')
    elif inArgs.out_nii == 'same':
        out_filename = inArgs.in_nii
    else:
        out_filename = inArgs.out_nii

    keep(inArgs.in_nii, inArgs.keep, inArgs.csv, out_filename)
Esempio n. 4
0
        "--out_nii",
        help="Filename of NIFTI output label. (default = extract.<in> ) ",
        default=None)

    parser.add_argument('-v',
                        '--verbose',
                        help='Verbose flag',
                        action='store_true',
                        default=False)

    inArgs = parser.parse_args()

    # Read NIFTI File
    in_nii = nibabel.load(inArgs.in_nii)

    df_bvals = pandas.read_csv(inArgs.bvals,
                               delim_whitespace=True,
                               header=None,
                               index_col=False)
    bvals = df_bvals.transpose()[0].values
    bvals_index = numpy.where(bvals == 0)

    out_nii = tools.extract_volumes(in_nii, bvals_index, inArgs.verbose)

    if inArgs.out_nii is None:
        out_nii_filename = util.add_prefix_to_filename(inArgs.in_nii, 'b0.')
    else:
        out_nii_filename = inArgs.out_nii

    nibabel.save(out_nii, out_nii_filename)
Esempio n. 5
0
                        help="CSV Filename (default=imageFile.csv).",
                        default=None)

    parser.add_argument('-s',
                        '--sort',
                        help='Labels to sort',
                        type=str,
                        nargs=1,
                        default='phase/freq')
    parser.add_argument('-v',
                        '--verbose',
                        help='Verbose flag',
                        action='store_true',
                        default=False)

    inArgs = parser.parse_args()

    df_phase_ghosts = calculate_phase_ghosts(inArgs.in_csv, inArgs.verbose)

    # Save results to file

    if inArgs.out is not None:

        if inArgs.out == 'prefix':
            out_filename = util.add_prefix_to_filename(inArgs.in_csv,
                                                       'phase_ghosts.')
        else:
            out_filename = inArgs.out

        df_phase_ghosts.to_csv(out_filename, index=False)
Esempio n. 6
0
    parser.add_argument(
        "--out_nii",
        help="Filename of NIFTI output label. (default = cumsum_nii.<in> ) ",
        default=None)

    parser.add_argument('-s',
                        '--scale',
                        help='Multiply NIFTI output array by scale factor ',
                        type=float,
                        default=1.0)
    parser.add_argument('-v',
                        '--verbose',
                        help='Verbose flag',
                        action='store_true',
                        default=False)

    inArgs = parser.parse_args()

    # Read NIFTI File
    in_nii = nibabel.load(inArgs.in_nii)

    out_nii = cumsum_nii(in_nii, inArgs.scale)

    if inArgs.out_nii is None:
        out_nii_filename = util.add_prefix_to_filename(inArgs.in_nii,
                                                       'cumsum_nii.')
    else:
        out_nii_filename = inArgs.out_nii

    nibabel.save(out_nii, out_nii_filename)
Esempio n. 7
0
        'Filename of NIFTI QA background label. (qa_background_label.<in_nii> )',
        default=None)

    parser.add_argument(
        '--labels',
        help="QA labels for phase, frequency and other. (100,200,300)",
        nargs=3,
        type=int,
        default=[100, 200, 300])

    parser.add_argument('--swap',
                        help="Swap phase and frequency labels. (false)",
                        action="store_true",
                        default=False)

    parser.add_argument("-v",
                        "--verbose",
                        help="Verbose flag",
                        action="store_true",
                        default=False)
    inArgs = parser.parse_args()

    if inArgs.out_nii is None:
        out_nii_filename = util.add_prefix_to_filename(inArgs.in_nii,
                                                       'qa_background_label.')
    else:
        out_nii_filename = inArgs.out_nii

    create_qa_labels(inArgs.in_nii, inArgs.labels, out_nii_filename,
                     inArgs.swap, inArgs.verbose)
Esempio n. 8
0
def create_mask(in_image, n4_flag=True, imopen_flag=True, thr=0, thrp=25, scale=1, imopen_iterations=0, imclose_iterations=0, 
                verbose=False, debug=False, noglc_flag=True):

    inMask = in_image

    # N4 Bias Correction
    
    if n4_flag:
        
        outMask = util.add_prefix_to_filename( in_image, 'n4')
        
        if not os.path.isfile( outMask ):
            util.iw_subprocess( ["N4BiasFieldCorrection","-d","3", "-i", inMask, "-r", "-s", "-o", outMask], verbose)
            
        inMask = outMask

    # Threshold 

    iiMask  = 1
    outMask = util.add_prefix_to_filename( in_image, str(iiMask) + ".create_mask.")
        
    util.iw_subprocess( ["fslmaths", inMask, "-mul", str(scale), "-thrp", 
                                str(thrp), "-thr", str(thr), "-bin",outMask ],           verbose, debug)

    # Fill Holes

    inMask  = outMask
    iiMask  = iiMask + 1
    outMask = util.add_prefix_to_filename( in_image, str(iiMask) + ".create_mask.")

    util.iw_subprocess( ["ImageMath","3", outMask, "FillHoles",   inMask ], verbose, debug)

    # Opening image operation

    inMask  = outMask

    if imopen_iterations > 0: 
        iiMask  = iiMask + 1
        outMask = util.add_prefix_to_filename( in_image, str(iiMask) + ".create_mask.") 
        util.iw_subprocess( ["fslmaths", inMask,  "-kernel", "2D"] + 
                                   ["-ero"]*imopen_iterations +["-dilM"]*imopen_iterations  + [outMask], verbose, debug)

    # Close image operation
    inMask  = outMask

    if imclose_iterations > 0: 
        iiMask  = iiMask + 1
        outMask = util.add_prefix_to_filename( in_image, str(iiMask) + ".create_mask.")
        util.iw_subprocess( ["fslmaths", inMask,  "-kernel", "2D"] + 
                                   ["-dilM"]*imclose_iterations +["-ero"]*imclose_iterations  + [outMask], verbose, debug)

    # Don't grab greatest larges component

    inMask = outMask
         
    if not noglc_flag:
        iiMask  = iiMask + 1
        outMask = util.add_prefix_to_filename( in_image, str(iiMask) + ".create_mask.")
        util.iw_subprocess(["ImageMath","3", outMask, "GetLargestComponent",  inMask],verbose, debug)

    # Active Contour

    inMask  = outMask
    out_mask_filename = util.add_prefix_to_filename( in_image, "mask.")

#    if inArgs.active_contour and False:  # Active contour no longer works. Talk to Craig 
#
#        util.iw_subprocess( ["matlab", "-nodisplay", "-noFigureWindows", "-nosplash", "-r", 
#                                    "iw_calculate_boundary('"+inMask+"',[ " +  str(inArgs.smoothing) + "," + 
#                                    str(inArgs.iterations) + " ],'ch."+outMask+"', 'ac." + outMask + "'); exit"], verbose)
#        shutil.copyfile("ac."+outMask, out_mask_filename)
#
#    else:
 
    shutil.copyfile(outMask, out_mask_filename)    
    util.iw_subprocess( ["fslcpgeom", in_image, out_mask_filename], verbose)


    return out_mask_filename