Ejemplo n.º 1
0
class TestAllNewContour(MacroBase):
    def getName(self):
        return 'Test All Contour Error Without Registration'
    def run(self, window = None):
        self.path = sys.argv[0]
        if os.path.isfile(self.path):
            self.path = os.path.dirname(self.path)
        
        self.ini = DictIni(self.path + '/Script/Macro/test.ini')
        self.cnt = len(self.ini.file.name_fix)
        
        self.contourerror = ContourErrorAnalysis(None)
        self.contourareaerror = ContourErrorWithAreaAnalysis(None)
        self.contourweigherror = WeightedContourErrorAnalysis(None)
        self.areaerror = AreaIndexAnalysis(None)
        self.centerlineerror = CenterlineErrorAnalysis(None)
        self.savepath = 'D:/ResultData/'
        self.dirs = ['icp-cen-clip-nolabel/', 'icp-cen-noclip-label/', 'icp-surface-clip-label/', 'icp-surface-noclip-nolabel/']
        self.type = ['_icp_cen.mat', '_icp_cen.mat', '_icp_con.mat', '_icp_con.mat']
        self.book = xlwt.Workbook()
        
        self.sheet = self.book.add_sheet('icp_cen')
        self.sheet.write(1, 0, 'MRE')
        self.sheet.write(2, 0, 'AMRE')
        self.sheet.write(3, 0, 'WMRE')
        self.sheet.write(4, 0, 'AWMRE')
        for k in range(4):
            for i in range(self.cnt):
                dataset = self.load(k, i)
                self.process(dataset, k, i)
                del dataset
        if self.gui:
            self.gui.showErrorMessage('Success', 'Test sucessfully!')
        else:
            print 'Test sucessfully!'
            
    def load(self, k, i):
        dataset = {'result': [], 'fix': []}
        data, info, point = db.loadMatData(self.savepath + self.dirs[k]
            + self.ini.file.name_result[i] + self.type[k], None)
        dataset['result'] = db.BasicData(data, info, point)
        
        data, info, point = db.loadMatData(self.path + self.ini.file.datadir + '/Contour/'
            + self.ini.file.name_fix[i] + '.mat', None)
        dataset['fix'] = db.BasicData(data, info, point)
        
        print 'Data %s loaded!' % self.ini.file.name_result[i]
            
        return dataset
    def process(self, dataset, k, i):
        self.sheet.write(0, k * 20 + i + 1, self.ini.file.name_result[i] + str(k))
        print 'Evaluation Data %s...' % self.ini.file.name_result[i]
        mean_dis, mean_whole, max_dis, max_whole = self.contourerror.analysis(dataset['result'], dataset['fix'].getPointSet('Contour').copy())
        self.sheet.write(1, k * 20 + i + 1, mean_whole)
        mean_dis, mean_whole, max_dis, max_whole = self.contourareaerror.analysis(dataset['result'], dataset['fix'].getPointSet('Contour').copy())
        self.sheet.write(2, k * 20 + i + 1, mean_whole)
        mean_dis, mean_whole, max_dis, max_whole = self.contourweigherror.analysis(dataset['result'], dataset['fix'].getPointSet('Contour').copy())
        self.sheet.write(3, k * 20 + i + 1, mean_whole)
        mean_dis, mean_whole, max_dis, max_whole = self.contourweigherror.analysis(dataset['result'], dataset['fix'].getPointSet('Contour').copy(), True)
        self.sheet.write(4, k * 20 + i + 1, mean_whole)
        print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole
        
        self.book.save(self.path + self.ini.file.savedir + 'Test_weighted.xls')
Ejemplo n.º 2
0
class TestSegmentationRate(MacroBase):
    def getName(self):
        return 'Test Segmentation Distance Error'

    def run(self, window=None):
        self.path = sys.argv[0]
        if os.path.isfile(self.path):
            self.path = os.path.dirname(self.path)

        self.ini = DictIni(self.path + '/Script/Macro/test.ini')
        self.cnt = len(self.ini.file.name_fix)
        self.savepath = self.path + self.ini.file.savedir

        self.contourerror = ContourErrorAnalysis(None)
        self.contourareaerror = ContourErrorWithAreaAnalysis(None)
        self.result = [{}, {}, {}]
        self.resultCnt = [{}, {}, {}]
        self.correct = npy.array([0.0, 0.0, 0.0])
        self.count = npy.array([0, 0, 0])

        for i in range(self.cnt):
            dataset = self.load(i)
            self.process(dataset, i)
            del dataset
        print self.correct / self.count, npy.sum(self.correct) / npy.sum(
            self.count)
        '''
        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.result[cnt][x] /= self.resultCnt[cnt][x]
        
        
        self.book = xlwt.Workbook()
        title = ['CCA', 'ECA', 'ICA']
        
        self.sheet = self.book.add_sheet('Distance_error')
        for i in range(3):
            self.sheet.write(i + 1, 0, title[i])
        zmin = [0, 0, 0]
        zmax = [0, 0, 0]
        for cnt in range(3):
            zmin[cnt] = min(self.result[cnt].keys())
            zmax[cnt] = max(self.result[cnt].keys())
        z1 = int(min(zmin))
        z2 = int(max(zmax))
        for z in range(z1, z2 + 1):
            self.sheet.write(0, z - z1 + 1, z * 0.34722220897674)
        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.sheet.write(cnt + 1, int(x) - z1 + 1, self.result[cnt][x])
        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.sheet.write(cnt + 4, int(x) - z1 + 1, self.resultCnt[cnt][x])
        self.book.save(self.path + self.ini.file.savedir + 'Distance_error_segmentation.xls')
        '''
        if self.gui:
            self.gui.showErrorMessage('Success', 'Test sucessfully!')
        else:
            print 'Test sucessfully!'

    def load(self, i):
        dataset = {'mov': [], 'fix': [], 'seg': []}

        data, info, point = db.loadMatData(
            self.path + self.ini.file.datadir + '/Contour/' +
            self.ini.file.name_fix[i] + '.mat', None)
        fileData = db.BasicData(data, info, point)
        dataset['fix'] = fileData

        data, info, point = db.loadMatData(
            self.savepath + self.ini.file.name_result[i] + '_mr.mat', None)
        fileData = db.BasicData(data, info, point)
        dataset['seg'] = fileData
        print 'Data %s loaded!' % self.ini.file.name_result[i]

        return dataset

    def process(self, dataset, i):
        print 'Evaluation Data %s...' % self.ini.file.name_result[i]
        mean_dis, mean_whole, max_dis, max_whole, result = self.contourerror.analysis(
            dataset['seg'], dataset['fix'].getPointSet('Contour').copy(), True)
        print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole
        for cnt in range(3):
            for x in result[cnt].keys():
                #self.result[cnt][x] = self.result[cnt].get(x, 0) + result[cnt][x]
                #self.resultCnt[cnt][x] = self.resultCnt[cnt].get(x, 0) + 1
                self.count[cnt] += 1
                if result[cnt][x] <= 1.50:
                    self.correct[cnt] += 1
Ejemplo n.º 3
0
class TestAllNewContourReg(MacroBase):
    def getName(self):
        return 'Test All Contour Error With Registration'
    def run(self, window = None):
        self.path = sys.argv[0]
        if os.path.isfile(self.path):
            self.path = os.path.dirname(self.path)
        
        self.ini = DictIni(self.path + '/Script/Macro/test.ini')
        self.cnt = len(self.ini.file.name_fix)
        
        self.icp = IcpPointsetRegistration(None)
        self.contourerror = ContourErrorAnalysis(None)
        self.contourareaerror = ContourErrorWithAreaAnalysis(None)
        self.contourweigherror = WeightedContourErrorAnalysis(None)
        self.areaerror = AreaIndexAnalysis(None)
        self.centerlineerror = CenterlineErrorAnalysis(None)
        self.savepath = 'D:/ResultData/'
        self.dirs = ['icp-cen-clip-nolabel/', 'icp-cen-noclip-label/', 'icp-surface-clip-label/', 'icp-surface-noclip-nolabel/']
        self.type = ['_icp_cen.mat', '_icp_cen.mat', '_icp_con.mat', '_icp_con.mat']
        self.book = xlwt.Workbook()
        
        self.sheet = self.book.add_sheet('icp_cen')
        self.sheet.write(1, 0, 'MRE')
        self.sheet.write(2, 0, 'AMRE')
        self.sheet.write(3, 0, 'WMRE')
        self.sheet.write(4, 0, 'AWMRE')
        for k in range(4):
            for i in range(self.cnt):
                dataset = self.load(k, i)
                self.process(dataset, k, i)
                del dataset
        if self.gui:
            self.gui.showErrorMessage('Success', 'Test sucessfully!')
        else:
            print 'Test sucessfully!'
            
    def load(self, k, i):
        dataset = {'mov': [], 'fix': []}
        
        data, info, point = db.loadMatData(self.path + self.ini.file.datadir + '/Contour/'
            + self.ini.file.name_fix[i] + '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        fileData = db.BasicData(data, info, point)
        dataset['fix'] = fileData
        
        data, info, point = db.loadMatData(self.path + self.ini.file.datadir + '/Contour/'
            + self.ini.file.name_mov[i] + '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        fileData = db.BasicData(data, info, point)
        dataset['mov'] = fileData
        print 'Data %s loaded!' % self.ini.file.name_result[i]
            
        return dataset
    def process(self, dataset, k, i):
        self.sheet.write(0, k * 20 + i + 1, self.ini.file.name_result[i] + str(k))
        print 'Register Data %s...' % self.ini.file.name_result[i]
        if k == 0:
            data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1, op = True)
        elif k == 1:
            data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1)
        elif k == 2:
            data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 0, op = True)
        else:
            data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 0)
        resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point)
        resultData.info.addData('fix', 1)
        resultData.info.addData('move', 2)
        resultData.info.addData('transform', para)
        mean_dis, mean_whole, max_dis, max_whole = self.contourerror.analysis(resultData, dataset['fix'].getPointSet('Contour').copy())
        self.sheet.write(1, k * 20 + i + 1, mean_whole)
        mean_dis, mean_whole, max_dis, max_whole = self.contourareaerror.analysis(resultData, dataset['fix'].getPointSet('Contour').copy())
        self.sheet.write(2, k * 20 + i + 1, mean_whole)
        mean_dis, mean_whole, max_dis, max_whole = self.contourweigherror.analysis(resultData, dataset['fix'].getPointSet('Contour').copy())
        self.sheet.write(3, k * 20 + i + 1, mean_whole)
        mean_dis, mean_whole, max_dis, max_whole = self.contourweigherror.analysis(resultData, dataset['fix'].getPointSet('Contour').copy(), True)
        self.sheet.write(4, k * 20 + i + 1, mean_whole)
        print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole
        
        self.book.save(self.path + self.ini.file.savedir + 'AllTest.xls')
        del data, point, resultData, para
Ejemplo n.º 4
0
class TestAllNewContour(MacroBase):
    def getName(self):
        return 'Test All Contour Error Without Registration'

    def run(self, window=None):
        self.path = sys.argv[0]
        if os.path.isfile(self.path):
            self.path = os.path.dirname(self.path)

        self.ini = DictIni(self.path + '/Script/Macro/test.ini')
        self.cnt = len(self.ini.file.name_fix)

        self.contourerror = ContourErrorAnalysis(None)
        self.contourareaerror = ContourErrorWithAreaAnalysis(None)
        self.contourweigherror = WeightedContourErrorAnalysis(None)
        self.areaerror = AreaIndexAnalysis(None)
        self.centerlineerror = CenterlineErrorAnalysis(None)
        self.savepath = 'D:/ResultData/'
        self.dirs = [
            'icp-cen-clip-nolabel/', 'icp-cen-noclip-label/',
            'icp-surface-clip-label/', 'icp-surface-noclip-nolabel/'
        ]
        self.type = [
            '_icp_cen.mat', '_icp_cen.mat', '_icp_con.mat', '_icp_con.mat'
        ]
        self.book = xlwt.Workbook()

        self.sheet = self.book.add_sheet('icp_cen')
        self.sheet.write(1, 0, 'MRE')
        self.sheet.write(2, 0, 'AMRE')
        self.sheet.write(3, 0, 'WMRE')
        self.sheet.write(4, 0, 'AWMRE')
        for k in range(4):
            for i in range(self.cnt):
                dataset = self.load(k, i)
                self.process(dataset, k, i)
                del dataset
        if self.gui:
            self.gui.showErrorMessage('Success', 'Test sucessfully!')
        else:
            print 'Test sucessfully!'

    def load(self, k, i):
        dataset = {'result': [], 'fix': []}
        data, info, point = db.loadMatData(
            self.savepath + self.dirs[k] + self.ini.file.name_result[i] +
            self.type[k], None)
        dataset['result'] = db.BasicData(data, info, point)

        data, info, point = db.loadMatData(
            self.path + self.ini.file.datadir + '/Contour/' +
            self.ini.file.name_fix[i] + '.mat', None)
        dataset['fix'] = db.BasicData(data, info, point)

        print 'Data %s loaded!' % self.ini.file.name_result[i]

        return dataset

    def process(self, dataset, k, i):
        self.sheet.write(0, k * 20 + i + 1,
                         self.ini.file.name_result[i] + str(k))
        print 'Evaluation Data %s...' % self.ini.file.name_result[i]
        mean_dis, mean_whole, max_dis, max_whole = self.contourerror.analysis(
            dataset['result'], dataset['fix'].getPointSet('Contour').copy())
        self.sheet.write(1, k * 20 + i + 1, mean_whole)
        mean_dis, mean_whole, max_dis, max_whole = self.contourareaerror.analysis(
            dataset['result'], dataset['fix'].getPointSet('Contour').copy())
        self.sheet.write(2, k * 20 + i + 1, mean_whole)
        mean_dis, mean_whole, max_dis, max_whole = self.contourweigherror.analysis(
            dataset['result'], dataset['fix'].getPointSet('Contour').copy())
        self.sheet.write(3, k * 20 + i + 1, mean_whole)
        mean_dis, mean_whole, max_dis, max_whole = self.contourweigherror.analysis(
            dataset['result'], dataset['fix'].getPointSet('Contour').copy(),
            True)
        self.sheet.write(4, k * 20 + i + 1, mean_whole)
        print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole

        self.book.save(self.path + self.ini.file.savedir + 'Test_weighted.xls')
Ejemplo n.º 5
0
class TestMissingRobust(MacroBase):
    def getName(self):
        return 'Test Missing Robustness'
    def run(self, window = None):
        self.path = sys.argv[0]
        if os.path.isfile(self.path):
            self.path = os.path.dirname(self.path)
        
        #self.ini = DictIni(self.path + '/Script/Macro/test1.ini') # Remove 56L for small FOV
        self.ini = DictIni(self.path + '/Script/Macro/test.ini')
        self.cnt = len(self.ini.file.name_fix)
        
        self.icp = IcpPointsetRegistration(None)
        self.contourerror = ContourErrorAnalysis(None)
        
        self.error = npy.zeros([10, 4], dtype = npy.float32)
        
        for i in range(self.cnt):
            dataset = self.load(i)
            self.process(dataset, i)
            del dataset
            
        self.error /= self.cnt
        
        self.book = xlwt.Workbook()
        title = ['CCA', 'ECA', 'ICA', 'Overall']
        self.sheet = self.book.add_sheet('Missing Slice')
        
        for i in range(4):
            self.sheet.write(i + 1, 0, title[i])
        for i in range(10):
            self.sheet.write(0, i + 1, i + 1)
        for i in range(4):
            for j in range(10):
                self.sheet.write(i + 1, j + 1, float(self.error[j, i]))
        self.book.save('./Result/Robust_Missing.xls')
        
        if self.gui:
            self.gui.showErrorMessage('Success', 'Test sucessfully!')
        else:
            print 'Test sucessfully!'
            
    def load(self, i):
        dataset = {'mov': [], 'fix': []}
        
        data, info, point = db.loadMatData(self.path + self.ini.file.datadir
            + self.ini.file.name_fix[i] + '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        fileData = db.BasicData(data, info, point)
        dataset['fix'] = fileData
        
        data, info, point = db.loadMatData(self.path + self.ini.file.datadir
            + self.ini.file.name_mov[i] + '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        fileData = db.BasicData(data, info, point)
        dataset['mov'] = fileData
        print 'Data %s loaded!' % self.ini.file.name_result[i]
            
            
        return dataset
    def process(self, dataset, i):
        # ICP with centerline
        print 'Register Data %s with ICP(centerline)...' % self.ini.file.name_result[i]
        for fov in range(10):
            data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1, False, 0, fov + 1) 
            resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point)
            resultData.info.addData('fix', 1)
            resultData.info.addData('move', 2)
            resultData.info.addData('transform', para)
            print 'FOV %dmm Done!' % (fov + 1)
            mean_dis, mean_whole, max_dis, max_whole = self.contourerror.analysis(resultData, dataset['fix'].getPointSet('Contour').copy())
            print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole
            self.error[fov, :3] += mean_dis
            self.error[fov, 3] += mean_whole
            del data, point, resultData
Ejemplo n.º 6
0
class TestDistanceError(MacroBase):
    def getName(self):
        return 'Test Distance Error'

    def run(self, window=None):
        self.path = sys.argv[0]
        if os.path.isfile(self.path):
            self.path = os.path.dirname(self.path)

        self.ini = DictIni(self.path + '/Script/Macro/test_modal.ini')
        self.cnt = len(self.ini.file.name_fix)

        self.icp = IcpPointsetRegistration(None)
        self.contourerror = ContourErrorAnalysis(None)
        self.contourareaerror = ContourErrorWithAreaAnalysis(None)
        self.result = [{}, {}, {}]
        self.resultCnt = [{}, {}, {}]

        for i in range(self.cnt):
            dataset = self.load(i)
            self.process(dataset, i)
            del dataset

        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.result[cnt][x] /= self.resultCnt[cnt][x]

        self.savepath = self.path + self.ini.file.savedir
        self.book = xlwt.Workbook()
        title = ['CCA', 'ECA', 'ICA']

        self.sheet = self.book.add_sheet('Distance_error')
        for i in range(3):
            self.sheet.write(i + 1, 0, title[i])
        zmin = [0, 0, 0]
        zmax = [0, 0, 0]
        for cnt in range(3):
            zmin[cnt] = min(self.result[cnt].keys())
            zmax[cnt] = max(self.result[cnt].keys())
        z1 = int(min(zmin))
        z2 = int(max(zmax))
        for z in range(z1, z2 + 1):
            self.sheet.write(0, z - z1 + 1, z * 0.4)
        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.sheet.write(cnt + 1,
                                 int(x) - z1 + 1, float(self.result[cnt][x]))
        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.sheet.write(cnt + 4,
                                 int(x) - z1 + 1,
                                 float(self.resultCnt[cnt][x]))
        self.book.save(self.path + self.ini.file.savedir +
                       'Distance_error_merge.xls')

        if self.gui:
            self.gui.showErrorMessage('Success', 'Test sucessfully!')
        else:
            print 'Test sucessfully!'

    def load(self, i):
        dataset = {'mov': [], 'fix': []}
        data, info, point = db.loadMatData(
            self.path + self.ini.file.datadir + '/Contour/' +
            self.ini.file.name_mov[i] + '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        dataset['mov'] = db.BasicData(data, info, point)

        data, info, point = db.loadMatData(
            self.path + self.ini.file.datadir + '/Contour/' +
            self.ini.file.name_fix[i] + '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        dataset['fix'] = db.BasicData(data, info, point)

        print 'Data %s loaded!' % self.ini.file.name_result[i]

        return dataset

    def process(self, dataset, i):
        print 'Register Data %s with ICP(centerline) with label...' % self.ini.file.name_result[
            i]
        data, point, para = self.icp.register(dataset['fix'], dataset['mov'],
                                              1)
        resultData = db.ResultData(data,
                                   db.ImageInfo(dataset['fix'].info.data),
                                   point)
        resultData.info.addData('fix', 1)
        resultData.info.addData('move', 2)
        resultData.info.addData('transform', para)
        print 'Done!'
        print 'Evaluation Data %s...' % self.ini.file.name_result[i]
        mean_dis, mean_whole, max_dis, max_whole, result = self.contourerror.analysis(
            resultData, dataset['fix'].getPointSet('Contour').copy(), True)
        print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole
        print result
        for cnt in range(3):
            for x in result[cnt].keys():
                self.result[cnt][x] = self.result[cnt].get(x,
                                                           0) + result[cnt][x]
                self.resultCnt[cnt][x] = self.resultCnt[cnt].get(x, 0) + 1
Ejemplo n.º 7
0
class TestDistanceError(MacroBase):
    def getName(self):
        return 'Test Distance Error'
    def run(self, window = None):
        self.path = sys.argv[0]
        if os.path.isfile(self.path):
            self.path = os.path.dirname(self.path)
        
        self.ini = DictIni(self.path + '/Script/Macro/test_modal.ini')
        self.cnt = len(self.ini.file.name_fix)
        
        self.icp = IcpPointsetRegistration(None)
        self.contourerror = ContourErrorAnalysis(None)
        self.contourareaerror = ContourErrorWithAreaAnalysis(None)
        self.result = [{}, {}, {}]
        self.resultCnt = [{}, {}, {}]
        
        for i in range(self.cnt):
            dataset = self.load(i)
            self.process(dataset, i)
            del dataset
        
        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.result[cnt][x] /= self.resultCnt[cnt][x]
        
        self.savepath = self.path + self.ini.file.savedir
        self.book = xlwt.Workbook()
        title = ['CCA', 'ECA', 'ICA']
        
        self.sheet = self.book.add_sheet('Distance_error')
        for i in range(3):
            self.sheet.write(i + 1, 0, title[i])
        zmin = [0, 0, 0]
        zmax = [0, 0, 0]
        for cnt in range(3):
            zmin[cnt] = min(self.result[cnt].keys())
            zmax[cnt] = max(self.result[cnt].keys())
        z1 = int(min(zmin))
        z2 = int(max(zmax))
        for z in range(z1, z2 + 1):
            self.sheet.write(0, z - z1 + 1, z * 0.4)
        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.sheet.write(cnt + 1, int(x) - z1 + 1, float(self.result[cnt][x]))
        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.sheet.write(cnt + 4, int(x) - z1 + 1, float(self.resultCnt[cnt][x]))
        self.book.save(self.path + self.ini.file.savedir + 'Distance_error_merge.xls')
        
        if self.gui:
            self.gui.showErrorMessage('Success', 'Test sucessfully!')
        else:
            print 'Test sucessfully!'
            
    def load(self, i):
        dataset = {'mov': [], 'fix': []}
        data, info, point = db.loadMatData(self.path + self.ini.file.datadir + '/Contour/'
            + self.ini.file.name_mov[i] + '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        dataset['mov'] = db.BasicData(data, info, point)
        
        data, info, point = db.loadMatData(self.path + self.ini.file.datadir + '/Contour/'
            + self.ini.file.name_fix[i] + '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        dataset['fix'] = db.BasicData(data, info, point)
        
        print 'Data %s loaded!' % self.ini.file.name_result[i]
            
        return dataset
    def process(self, dataset, i):
        print 'Register Data %s with ICP(centerline) with label...' % self.ini.file.name_result[i]
        data, point, para = self.icp.register(dataset['fix'], dataset['mov'], 1) 
        resultData = db.ResultData(data, db.ImageInfo(dataset['fix'].info.data), point)
        resultData.info.addData('fix', 1)
        resultData.info.addData('move', 2)
        resultData.info.addData('transform', para)
        print 'Done!'
        print 'Evaluation Data %s...' % self.ini.file.name_result[i]
        mean_dis, mean_whole, max_dis, max_whole, result = self.contourerror.analysis(resultData, dataset['fix'].getPointSet('Contour').copy(), True)
        print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole
        print result
        for cnt in range(3):
            for x in result[cnt].keys():
                self.result[cnt][x] = self.result[cnt].get(x, 0) + result[cnt][x]
                self.resultCnt[cnt][x] = self.resultCnt[cnt].get(x, 0) + 1
Ejemplo n.º 8
0
class TestSegmentationRate(MacroBase):
    def getName(self):
        return 'Test Segmentation Distance Error'
    def run(self, window = None):
        self.path = sys.argv[0]
        if os.path.isfile(self.path):
            self.path = os.path.dirname(self.path)
        
        self.ini = DictIni(self.path + '/Script/Macro/test.ini')
        self.cnt = len(self.ini.file.name_fix)
        self.savepath = self.path + self.ini.file.savedir
        
        self.contourerror = ContourErrorAnalysis(None)
        self.contourareaerror = ContourErrorWithAreaAnalysis(None)
        self.result = [{}, {}, {}]
        self.resultCnt = [{}, {}, {}]
        self.correct = npy.array([0.0, 0.0, 0.0])
        self.count = npy.array([0, 0, 0])
        
        for i in range(self.cnt):
            dataset = self.load(i)
            self.process(dataset, i)
            del dataset
        print self.correct / self.count, npy.sum(self.correct) / npy.sum(self.count)
        '''
        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.result[cnt][x] /= self.resultCnt[cnt][x]
        
        
        self.book = xlwt.Workbook()
        title = ['CCA', 'ECA', 'ICA']
        
        self.sheet = self.book.add_sheet('Distance_error')
        for i in range(3):
            self.sheet.write(i + 1, 0, title[i])
        zmin = [0, 0, 0]
        zmax = [0, 0, 0]
        for cnt in range(3):
            zmin[cnt] = min(self.result[cnt].keys())
            zmax[cnt] = max(self.result[cnt].keys())
        z1 = int(min(zmin))
        z2 = int(max(zmax))
        for z in range(z1, z2 + 1):
            self.sheet.write(0, z - z1 + 1, z * 0.34722220897674)
        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.sheet.write(cnt + 1, int(x) - z1 + 1, self.result[cnt][x])
        for cnt in range(3):
            for x in self.result[cnt].keys():
                self.sheet.write(cnt + 4, int(x) - z1 + 1, self.resultCnt[cnt][x])
        self.book.save(self.path + self.ini.file.savedir + 'Distance_error_segmentation.xls')
        '''
        if self.gui:
            self.gui.showErrorMessage('Success', 'Test sucessfully!')
        else:
            print 'Test sucessfully!'
            
    def load(self, i):
        dataset = {'mov': [], 'fix': [], 'seg': []}
        
        data, info, point = db.loadMatData(self.path + self.ini.file.datadir + '/Contour/'
            + self.ini.file.name_fix[i] + '.mat', None)
        fileData = db.BasicData(data, info, point)
        dataset['fix'] = fileData
        
        data, info, point = db.loadMatData(self.savepath + self.ini.file.name_result[i] + '_mr.mat', None)
        fileData = db.BasicData(data, info, point)
        dataset['seg'] = fileData
        print 'Data %s loaded!' % self.ini.file.name_result[i]
            
        return dataset
    def process(self, dataset, i):
        print 'Evaluation Data %s...' % self.ini.file.name_result[i]
        mean_dis, mean_whole, max_dis, max_whole, result = self.contourerror.analysis(dataset['seg'], dataset['fix'].getPointSet('Contour').copy(), True)
        print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole
        for cnt in range(3):
            for x in result[cnt].keys():
                #self.result[cnt][x] = self.result[cnt].get(x, 0) + result[cnt][x]
                #self.resultCnt[cnt][x] = self.resultCnt[cnt].get(x, 0) + 1
                self.count[cnt] += 1
                if result[cnt][x] <= 1.50:
                    self.correct[cnt] += 1
Ejemplo n.º 9
0
class TestOcclusionRobust(MacroBase):
    def getName(self):
        return 'Test Occlusion Robustness'

    def run(self, window=None):
        self.path = sys.argv[0]
        if os.path.isfile(self.path):
            self.path = os.path.dirname(self.path)

        self.ini = DictIni(self.path + '/Script/Macro/test.ini')
        self.cnt = len(self.ini.file.name_fix)

        self.icp = IcpPointsetRegistration(None)
        self.contourerror = ContourErrorAnalysis(None)

        self.error = npy.zeros([7, 4], dtype=npy.float32)

        for i in range(self.cnt):
            dataset = self.load(i)
            self.process(dataset, i)
            del dataset

        self.error /= self.cnt

        self.book = xlwt.Workbook()
        title = ['CCA', 'ECA', 'ICA', 'Overall']
        self.sheet = self.book.add_sheet('Occlusion Slice')

        for i in range(4):
            self.sheet.write(i + 1, 0, title[i])
        for i in range(7):
            self.sheet.write(0, i + 1, i + 1)
        for i in range(4):
            for j in range(7):
                self.sheet.write(i + 1, j + 1, float(self.error[j, i]))
        self.book.save('./Result/Robust_Occlusion.xls')

        if self.gui:
            self.gui.showErrorMessage('Success', 'Test sucessfully!')
        else:
            print 'Test sucessfully!'

    def load(self, i):
        dataset = {'mov': [], 'fix': []}

        data, info, point = db.loadMatData(
            self.path + self.ini.file.datadir + self.ini.file.name_fix[i] +
            '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        fileData = db.BasicData(data, info, point)
        dataset['fix'] = fileData

        data, info, point = db.loadMatData(
            self.path + self.ini.file.datadir + self.ini.file.name_mov[i] +
            '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        fileData = db.BasicData(data, info, point)
        dataset['mov'] = fileData
        print 'Data %s loaded!' % self.ini.file.name_result[i]

        return dataset

    def process(self, dataset, i):
        # ICP with centerline
        print 'Register Data %s with ICP(centerline)...' % self.ini.file.name_result[
            i]
        for fov in range(7):
            data, point, para = self.icp.register(dataset['fix'],
                                                  dataset['mov'], 1, False, 0,
                                                  9999.0, 1, fov + 1)
            resultData = db.ResultData(data,
                                       db.ImageInfo(dataset['fix'].info.data),
                                       point)
            resultData.info.addData('fix', 1)
            resultData.info.addData('move', 2)
            resultData.info.addData('transform', para)
            print 'FOV %dmm Done!' % (fov + 1)
            mean_dis, mean_whole, max_dis, max_whole = self.contourerror.analysis(
                resultData, dataset['fix'].getPointSet('Contour').copy())
            print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole
            self.error[fov, :3] += mean_dis
            self.error[fov, 3] += mean_whole
            del data, point, resultData
Ejemplo n.º 10
0
class TestAllSegmentation(MacroBase):
    def getName(self):
        return 'Test All Segmentation'

    def run(self, window=None):
        self.path = sys.argv[0]
        if os.path.isfile(self.path):
            self.path = os.path.dirname(self.path)

        self.ini = DictIni(self.path + '/Script/Macro/test.ini')
        self.cnt = len(self.ini.file.name_fix)

        self.icp = IcpPointsetRegistration(None)
        self.contourerror = ContourErrorAnalysis(None)
        self.areaerror = AreaIndexAnalysis(None)

        self.savepath = self.path + self.ini.file.savedir
        self.book = xlwt.Workbook()
        title = ['CCA', 'ECA', 'ICA', 'Overall']

        self.sheet1 = self.book.add_sheet('segmentation')
        self.sheet1.write(1, 0, 'MRE')

        for i in range(4):
            self.sheet1.write(i + 1, 1, title[i])
        self.sheet1.write(5, 0, 'Time')

        self.sheet2 = self.book.add_sheet('registration')
        self.sheet2.write(1, 0, 'MRE')
        self.sheet2.write(5, 0, 'MAXE')
        self.sheet2.write(9, 0, 'Dice Index')
        self.sheet2.write(13, 0, 'Time')

        for j in range(3):
            for i in range(4):
                self.sheet2.write(j * 4 + i + 1, 1, title[i])

        for i in range(self.cnt):
            dataset = self.load(i)
            self.process(dataset, i)
            del dataset
        if self.gui:
            self.gui.showErrorMessage('Success', 'Test sucessfully!')
        else:
            print 'Test sucessfully!'

    def load(self, i):
        dataset = {'mov': [], 'fix': []}

        data, info, point = db.loadMatData(
            self.path + self.ini.file.datadir + '/Contour/' +
            self.ini.file.name_fix[i] + '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        fileData = db.BasicData(data, info, point)
        dataset['fix'] = fileData

        data, info, point = db.loadMatData(
            self.path + self.ini.file.datadir + '/Contour/' +
            self.ini.file.name_mov[i] + '.mat', None)
        point['Centerline'] = calCenterlineFromContour(point)
        fileData = db.BasicData(data, info, point)
        dataset['mov'] = fileData

        print 'Data %s loaded!' % self.ini.file.name_result[i]

        return dataset

    def process(self, dataset, i):
        def autoDetectContour(point, cnt, start, end, delta):
            self.new_points = npy.append(self.new_points, point, 0)
            points = point[:, :-2]
            count = 0
            for j in range(start + delta, end + delta, delta):
                center = calCentroidFromContour(points).reshape(2)
                image = dataset['fix'].getData()[j, :, :].transpose().copy()
                image = (image - npy.min(image)) / (npy.max(image) -
                                                    npy.min(image)) * 255
                result = ac_segmentation(center, image)

                a1 = ac_area(points.transpose(), image.shape)
                a2 = ac_area(result, image.shape)
                rate = a2 * 1.0 / a1
                if rate >= min(1.5 + count * 0.2, 2.1) or rate <= 0.7:
                    temp_array = points.copy()
                    if cnt != 1 and rate > 0.7:
                        count += 1
                else:
                    temp_array = result.transpose().copy()
                    count = 0
                points = temp_array.copy()
                temp_array = npy.insert(temp_array, 2, [[j], [cnt]], 1)
                self.new_points = npy.append(self.new_points, temp_array, 0)

                sys.stdout.write(str(j) + ',')
                sys.stdout.flush()
            print ' '

        print 'Segment Data %s...' % self.ini.file.name_result[i]
        tmp = dataset['fix'].pointSet.data['Contour']
        tmp = tmp[tmp[:, 0] >= 0]

        self.new_points = npy.array([[-1, -1, -1, -1]], dtype=npy.float32)
        time1 = time.time()
        bottom = int(npy.round(npy.min(tmp[:, 2])))
        bif = int(db.getBifurcation(tmp) + 0.5)
        up = int(npy.round(npy.max(tmp[:, 2])))

        point_vital = [0] * 3
        point_vital[0] = tmp[(npy.round(tmp[:, -1]) == 0)
                             & (npy.round(tmp[:, 2]) == bottom)].copy()
        point_vital[1] = tmp[(npy.round(tmp[:, -1]) == 1)
                             & (npy.round(tmp[:, 2]) == up)].copy()
        point_vital[2] = tmp[(npy.round(tmp[:, -1]) == 2)
                             & (npy.round(tmp[:, 2]) == up)].copy()

        autoDetectContour(point_vital[0], 0, bottom, bif, 1)
        autoDetectContour(point_vital[1], 1, up, bif, -1)
        autoDetectContour(point_vital[2], 2, up, bif, -1)
        time2 = time.time()
        '''
        # Use centerline for contour
        j = 0
        for center in tmp:
            image = dataset['fix'].getData()[npy.round(center[2]), :, :].transpose().copy()
            image = (image - npy.min(image)) / (npy.max(image) - npy.min(image)) * 255
            result = ac_segmentation(center[:2], image)
            
            point_array = npy.insert(result.transpose(), 2, [[center[2]],[center[3]]], axis = 1)
            new_points = npy.append(new_points, point_array, 0)
            
            j += 1
            if j % 10 == 0:
                sys.stdout.write(str(j) + ',')
                sys.stdout.flush()
        '''
        print ' '
        print 'Done! Time for segmentation is %0.2fs' % (time2 - time1)
        pointset = {'Contour': self.new_points}
        pointset['Centerline'] = calCenterlineFromContour(pointset)
        print 'Saving Data %s...' % self.ini.file.name_result[i]
        new_data = db.BasicData(dataset['fix'].data,
                                db.ImageInfo(dataset['fix'].info.data),
                                pointset)
        db.saveMatData(
            self.savepath + self.ini.file.name_result[i] + '_mr.mat',
            [new_data], 0)
        print 'Done!'
        mean_dis, mean_whole, max_dis, max_whole = self.contourerror.analysis(
            new_data, dataset['fix'].getPointSet('Contour').copy())
        print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole
        self.sheet1.write(0, i + 2, self.ini.file.name_result[i])
        for j in range(3):
            self.sheet1.write(j + 1, i + 2, mean_dis[j])
        self.sheet1.write(4, i + 2, mean_whole)
        self.sheet1.write(5, i + 2, time2 - time1)

        self.book.save(self.path + self.ini.file.savedir +
                       'Test_segmentation_final_refined.xls')

        # ICP with centerline
        print 'Register Data %s with ICP(centerline)...' % self.ini.file.name_result[
            i]
        time1 = time.time()
        data, point, para = self.icp.register(new_data, dataset['mov'], 1)
        time2 = time.time()
        print 'Done! Time for registration is %0.2fs' % (time2 - time1)
        resultData = db.ResultData(data,
                                   db.ImageInfo(dataset['fix'].info.data),
                                   point)
        resultData.info.addData('fix', 1)
        resultData.info.addData('move', 2)
        resultData.info.addData('transform', para)
        mean_dis, mean_whole, max_dis, max_whole = self.contourerror.analysis(
            resultData, dataset['fix'].getPointSet('Contour').copy())
        print 'Contour Error Done! Whole mean is %0.2fmm.' % mean_whole
        dice_index, dice_index_all = self.areaerror.analysis(
            resultData, dataset['fix'].getPointSet('Contour').copy())
        print 'Area Error Done! Whole Dice index is %0.3f.' % dice_index_all

        self.sheet2.write(0, i + 2, self.ini.file.name_result[i])
        for j in range(3):
            self.sheet2.write(j + 1, i + 2, mean_dis[j])
            self.sheet2.write(j + 5, i + 2, max_dis[j])
            self.sheet2.write(j + 9, i + 2, dice_index[j])

        self.sheet2.write(4, i + 2, mean_whole)
        self.sheet2.write(8, i + 2, max_whole)
        self.sheet2.write(12, i + 2, dice_index_all)
        self.sheet2.write(13, i + 2, time2 - time1)

        self.book.save(self.path + self.ini.file.savedir +
                       'Test_segmentation_final_refined2.xls')
        del data, point, resultData