Example #1
0
def run():
    candlist = pd.read_csv(LABELS_PATH)
    filelist, filepathlist = getFilelist(DATA_PATH)
    fout = open(DATA_CANDIDATES_PATH[:-1] + '.status', 'w')
    for i in range(STARTFILE, len(filelist)):
        print "FILE NUMBER: " + str(i) + " FILENAME: " + str(filelist[i])
        time1 = time.time()
        currentNodules = candlist[candlist['seriesuid'] == filelist[i]]
        test = currentNodules.as_matrix()
        if test.shape[0] == 0:
            continue
        fpath = filepathlist[i]
        itk = sitk.ReadImage(fpath)
        origin = np.array(itk.GetOrigin())
        image, _ = loadItk(filepathlist[i], None)
        segmentation = cd.segmentLung(image)
        mask = cd.applyMask(image, segmentation)
        segmentedImage = copy.deepcopy(image)
        segmentedImage[mask == 0]
        zmin, zmax, ymin, ymax, xmin, xmax = cd.findROI(segmentedImage)
        roi = cd.crop(segmentedImage)
        t1 = time.time()
        cands = cd.filterHessian(cd.multiscaleHessian(roi))
        t2 = time.time()
        print "SEGMENTED NODULES IN: " + str(t2 - t1) + 's'
        #NEW CODE
        #Masking the ROI prior to volume thresholding
        maskROI = mask[zmin:zmax, ymin:ymax, xmin:xmax]
        cands[maskROI == 0] = 0
        #END NEW CODE
        t1 = time.time()
        cands = cd.optVolumeThresholding(cands, mn=8, mx=20000)
        t2 = time.time()
        print "THREHOLDING APPLIED TO CANDIDATES: " + str(t2 - t1) + 's'
        coords = getCurrentNodulesCoords(currentNodules, origin)
        labels = measure.label(cands, connectivity=1)
        com = scipy.ndimage.measurements.center_of_mass(
            labels, labels, list(range(1,
                                       np.max(labels) + 1)))
        generateCandidates(segmentedImage, labels, coords, (xmin, ymin, zmin),
                           com, DATA_CANDIDATES_PATH, filelist[i])
        time2 = time.time()
        print "TOTAL TIME FOR FILE NUMBER " + str(i) + " IS " + str(
            time2 - time1) + "s"
        fout.write('FINISHED FILE NUMBER ' + str(i) + ' IN ' +
                   str(time2 - time1) + 's\n')
Example #2
0
def run():
    global MODEL
    global TEST
    global VAL
    global DATAPATH
    global WEIGHTSPATH
    global OUTPATH
    global OUTFILE
    print "TEST: " + str(TEST) + "   VAL: " + str(VAL) + "   MODEL: " + str(
        MODEL)
    filelist, filepathlist = getFilelist(DATAPATH)
    model = seg3d.getModel()
    model.load_weights(WEIGHTSPATH)
    candlist = pd.read_csv(ANNOTATIONS)
    time1 = time.time()
    flag = False
    #for i in range(len(filelist)):
    #  print filelist[i]
    #return
    for n in range(START, len(filelist)):
        if scanids is not None:
            for scan in scanids:
                #print n
                flag = True
                if filelist[n] == scan:
                    flag = False
                    break
        if flag:
            flag = False
            continue
        t1 = time.time()
        print "FILE NO " + str(n) + "   UID: " + filelist[n]
        currentNodules = candlist[candlist['seriesuid'] == filelist[n]]
        itk = sitk.ReadImage(filepathlist[n])
        image, _ = cg.loadItk(filepathlist[n], None)
        origin = np.array(itk.GetOrigin())
        spacing = np.array(itk.GetSpacing())
        coords, d = getCoords(currentNodules, origin, spacing)
        print coords
        #TODO: ADD TO OTHER SEGMENT_x_.PY
        if len(coords) == 0:
            continue
        segmentation = cd.segmentLung(image, reseg=False)
        if cd.checkSeg(segmentation):
            segmentation = cd.segmentLung(image, reseg=True)
        mask = cd.applyMask(image, segmentation)
        segmentedImage = copy.deepcopy(image)
        segmentedImage[mask == 0] = 0
        zmin, zmax, ymin, ymax, xmin, xmax = cd.findROI(segmentedImage)
        roi = cd.crop(segmentedImage)
        if roi.shape[0] < 100 or roi.shape[1] < 100 or roi.shape[2] < 100:
            segmentation = cd.segmentLung(image, error=True)
            mask = cd.applyMask(image, segmentation)
            segmentedImage = copy.deepcopy(image)
            segmentedImage[mask == 0] = 0
            zmin, zmax, ymin, ymax, xmin, xmax = cd.findROI(segmentedImage)
            roi = cd.crop(segmentedImage)
        shift = (xmin, ymin, zmin)  #**XYZ NOT ZYX
        segShape = getSegmentationShape(
            roi.shape)  #, sixteen=SIXTEEN)#image.shape)
        lpad, hpad = getPadding(segShape, roi)  #image)
        segImage = np.zeros(segShape)
        segImage[lpad[0]:segShape[0] - hpad[0], lpad[1]:segShape[1] - hpad[1],
                 lpad[2]:segShape[2] - hpad[2]] = roi  #image
        segImage = np.clip(segImage, -1000, 1000)
        segImage += 1000
        segImage *= (255. / 2000.)
        print "SEGMENTING ..."
        stride = STRIDE
        strides = copy.deepcopy(segShape)
        strides /= 32
        print segShape
        print strides
        segmentation = segmentImage(segImage, model, strides, stride)
        #if stride == 32:
        lpad -= 16
        hpad -= 16
        #elif stride == 16:
        #  lpad -= 8
        #  hpad -= 8
        #print segmentation.shape
        #print mask.shape
        #segmentation[mask==0] = 0
        tmpseg = segmentation[lpad[0]:segmentation.shape[0] - hpad[0],
                              lpad[1]:segmentation.shape[1] - hpad[1],
                              lpad[2]:segmentation.shape[2] - hpad[2]]
        #tmpseg[mask==0] = 0
        pseg = np.zeros(image.shape)
        pseg[zmin:zmax, ymin:ymax, xmin:xmax] = tmpseg
        seg = np.zeros(pseg.shape)
        seg = np.where(pseg > THRESH, 1, 0)
        #pseg[mask==0] = 0
        #seg[mask==0] = 0
        print "ASSESSING ..."
        labels = measure.label(seg, connectivity=1)
        #if np.max(labels) >= 315:
        #  print "TOO MANY: " + str(np.max(labels))
        #  continue
        assess(OUTFILE, labels, coords, filelist[n], OUTPATH, pseg, image, d)
        t2 = time.time()
        print "LAST SCAN TIME: " + str((t2 - t1) / 60) + 'm ' + str(
            (t2 - t1) % 60) + 's'
    time2 = time.time()
    print "TOTAL TIME: " + str((time2 - time1) / 60) + 'm ' + str(
        (time2 - time1) % 60) + 's'