Esempio n. 1
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    parser = OptionParser(add_help_option=False)
    parser.add_option("-N", dest="nNeighbor", type="int", default=3)
    parser.add_option("-f", dest="filesList")
    parser.add_option("-l", dest="labelList")
    parser.add_option("-o", dest="outDiffFile")
    parser.add_option("-h", dest="doHelp", action="store_true", default=False)
    options, _ = parser.parse_args()

    if options.doHelp:
        usage()
        sys.exit(-1)

    filesList = options.filesList
    labelList = options.labelList
    nNeighbor = options.nNeighbor
    outDiffFile = options.outDiffFile

    if filesList is None or labelList is None or outDiffFile is None:
        usage()
        sys.exit(-1)

    with open(filesList) as fid:
        files = fid.readlines()
    with open(labelList) as fid:
        label = fid.readlines()

    numLab = []
    [numLab.append(int(x)) for x in label]

    dataLst = []
    for img in files:
        sitkIm = sitk.ReadImage(img.rstrip())
        x = sitk.GetArrayFromImage(sitkIm)
        dataLst.append(sitk.GetArrayFromImage(sitkIm).ravel())

    # build difference image matrix (using Euclidean distance as similarity)
    diffImg = pbmutils.groupDiff(np.asmatrix(dataLst).T, numLab, nNeighbor)
    print "Difference image matrix (%d x %d)" % diffImg.shape

    outFid = open(outDiffFile, 'w')
    np.reshape(diffImg, -1).ravel().astype('float32').tofile(outFid)
    outFid.close()
Esempio n. 2
0
def main(argv=None):
    if argv is None:
        argv=sys.argv

    parser = OptionParser(add_help_option=False)
    parser.add_option("-N", dest="nNeighbor", type="int", default=3)
    parser.add_option("-f", dest="filesList")
    parser.add_option("-l", dest="labelList")
    parser.add_option("-o", dest="outDiffFile")
    parser.add_option("-h", dest="doHelp", action="store_true", default=False)
    options, _ = parser.parse_args()

    if options.doHelp:
        usage()
        sys.exit(-1)

    filesList = options.filesList
    labelList = options.labelList
    nNeighbor = options.nNeighbor
    outDiffFile = options.outDiffFile

    if filesList is None or labelList is None or outDiffFile is None:
        usage()
        sys.exit(-1)

    with open(filesList) as fid: files = fid.readlines()
    with open(labelList) as fid: label = fid.readlines()

    numLab = []
    [numLab.append(int(x)) for x in label]

    dataLst = []
    for img in files:
        sitkIm = sitk.ReadImage(img.rstrip())
        x = sitk.GetArrayFromImage(sitkIm)
        dataLst.append(sitk.GetArrayFromImage(sitkIm).ravel())

    # build difference image matrix (using Euclidean distance as similarity)
    diffImg = pbmutils.groupDiff(np.asmatrix(dataLst).T, numLab, nNeighbor)
    print "Difference image matrix (%d x %d)" % diffImg.shape

    outFid = open(outDiffFile, 'w')
    np.reshape(diffImg, -1).ravel().astype('float32').tofile(outFid)
    outFid.close()
Esempio n. 3
0
def main(argv=None):
    if argv is None:
        argv=sys.argv

    parser = OptionParser(add_help_option=False)
    parser.add_option("-i", dest="imgJSON")
    parser.add_option("-c", dest="cfgJSON")
    parser.add_option("-a", dest="outAtomFile")
    parser.add_option("-d", dest="outDiffFile")
    parser.add_option("-x", dest="outImagFile")
    parser.add_option("-s", dest="imSlice", type="int")
    parser.add_option("-r", dest="imScale", type="float")
    parser.add_option("-D", dest="dictSiz", type="int", default=5)
    parser.add_option("-k", dest="nearest", type="int", default=5)
    parser.add_option("-h", dest="doHelp", action="store_true", default=False)
    options, _ = parser.parse_args()

    if options.doHelp:
        usage()
        sys.exit(-1)

    imgJSON = options.imgJSON
    cfgJSON = options.cfgJSON

    outAtomFile = options.outAtomFile
    outImagFile = options.outImagFile
    outDiffFile = options.outDiffFile

    imSlice = options.imSlice
    dictSiz = options.dictSiz
    imScale = options.imScale
    nearest = options.nearest

    imData = json.load(open(imgJSON))
    helper = regtools.regtools(cfgJSON)

    groupSet = set()
    groupMap = dict()
    groupLab = []

    # generate numeric labels for each image
    for entry in imData["Data"]: groupSet.add(entry["Group"])
    for cnt, group in enumerate(groupSet): groupMap[group] = cnt
    for entry in imData["Data"]: groupLab.append(groupMap[entry["Group"]])

    imgFiles = []
    [imgFiles.append(str(e["Source"])) for e in imData["Data"]]

    dataList = []
    for i, imFile in enumerate(imgFiles):
        im0 = sitk.ReadImage(imFile)
        im1 = pbmutils.imResize(im0, imScale)
        imSz = sitk.GetArrayFromImage(im1).shape
        helper.infoMsg("Image size : (%d,%d,%d)" % imSz)
        if not imSlice is None:
            sl0 = pbmutils.imSlice(im1, [0, 0, imSlice])
            dataList.append(sl0.ravel())
        else:
            dataList.append(sitk.GetArrayFromImage(im1).ravel())
        helper.infoMsg("Done with image %d!" % i)

    # write raw image data
    if not outImagFile is None:
        tfid = open(outImgFile, 'w')
        np.reshape(np.asmatrix(dataList).T,-1).astype('float32').tofile(tfid)
        tfid.close()

    # build difference images
    diffIm = pbmutils.groupDiff(np.asmatrix(dataList).T, groupLab, nearest)
    helper.infoMsg("Difference image matrix (%d x %d)" % diffIm.shape)

    # write raw difference data
    if not outDiffFile is None:
        outFid = open(outDiffFile, 'w')
        np.reshape(diffIm, -1).ravel().astype('float32').tofile(outFid)
        outFid.close()

    # create the dictionary learner and run (alpha=1)
    lrnObj = MiniBatchDictionaryLearning(dictSiz, 1, verbose=True)
    lrnRes = lrnObj.fit(np.asmatrix(diffIm).T).components_

    # write dictionary atoms
    if not outAtomFile is None:
        outFid = open(outAtomFile, 'w')
        np.reshape(lrnRes.T, -1).ravel().astype('float32').tofile(outFid)
        outFid.close()
Esempio n. 4
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    parser = OptionParser(add_help_option=False)
    parser.add_option("-i", dest="imgJSON")
    parser.add_option("-c", dest="cfgJSON")
    parser.add_option("-a", dest="outAtomFile")
    parser.add_option("-d", dest="outDiffFile")
    parser.add_option("-x", dest="outImagFile")
    parser.add_option("-s", dest="imSlice", type="int")
    parser.add_option("-r", dest="imScale", type="float")
    parser.add_option("-D", dest="dictSiz", type="int", default=5)
    parser.add_option("-k", dest="nearest", type="int", default=5)
    parser.add_option("-h", dest="doHelp", action="store_true", default=False)
    options, _ = parser.parse_args()

    if options.doHelp:
        usage()
        sys.exit(-1)

    imgJSON = options.imgJSON
    cfgJSON = options.cfgJSON

    outAtomFile = options.outAtomFile
    outImagFile = options.outImagFile
    outDiffFile = options.outDiffFile

    imSlice = options.imSlice
    dictSiz = options.dictSiz
    imScale = options.imScale
    nearest = options.nearest

    imData = json.load(open(imgJSON))
    helper = regtools.regtools(cfgJSON)

    groupSet = set()
    groupMap = dict()
    groupLab = []

    # generate numeric labels for each image
    for entry in imData["Data"]:
        groupSet.add(entry["Group"])
    for cnt, group in enumerate(groupSet):
        groupMap[group] = cnt
    for entry in imData["Data"]:
        groupLab.append(groupMap[entry["Group"]])

    imgFiles = []
    [imgFiles.append(str(e["Source"])) for e in imData["Data"]]

    dataList = []
    for i, imFile in enumerate(imgFiles):
        im0 = sitk.ReadImage(imFile)
        im1 = pbmutils.imResize(im0, imScale)
        imSz = sitk.GetArrayFromImage(im1).shape
        helper.infoMsg("Image size : (%d,%d,%d)" % imSz)
        if not imSlice is None:
            sl0 = pbmutils.imSlice(im1, [0, 0, imSlice])
            dataList.append(sl0.ravel())
        else:
            dataList.append(sitk.GetArrayFromImage(im1).ravel())
        helper.infoMsg("Done with image %d!" % i)

    # write raw image data
    if not outImagFile is None:
        tfid = open(outImgFile, 'w')
        np.reshape(np.asmatrix(dataList).T, -1).astype('float32').tofile(tfid)
        tfid.close()

    # build difference images
    diffIm = pbmutils.groupDiff(np.asmatrix(dataList).T, groupLab, nearest)
    helper.infoMsg("Difference image matrix (%d x %d)" % diffIm.shape)

    # write raw difference data
    if not outDiffFile is None:
        outFid = open(outDiffFile, 'w')
        np.reshape(diffIm, -1).ravel().astype('float32').tofile(outFid)
        outFid.close()

    # create the dictionary learner and run (alpha=1)
    lrnObj = MiniBatchDictionaryLearning(dictSiz, 1, verbose=True)
    lrnRes = lrnObj.fit(np.asmatrix(diffIm).T).components_

    # write dictionary atoms
    if not outAtomFile is None:
        outFid = open(outAtomFile, 'w')
        np.reshape(lrnRes.T, -1).ravel().astype('float32').tofile(outFid)
        outFid.close()