viewPatch = getViewPatch(image, line)

    #     avgline = [int(avg_line_x), 0, int(avg_line_x), image.shape[0], 0]
    #     print avgline
    #     PhyloParser.displayLines(image, [avgline])

    #     PhyloParser.displayImage(viewPatch)
    #     PhyloParser.displayImage(hpatch)
    #     PhyloParser.displayImage(vpatch)

    return feature, viewPatch


if __name__ == '__main__':

    phyloParser = PhyloParser()

    folderPath = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/far_species'
    ground_truth = {}
    fileNameList = []

    fileList = []
    for dirPath, dirNames, fileNames in os.walk(folderPath):
        for f in fileNames:
            extension = f.split('.')[-1].lower()
            if extension in ["jpg", "png"]:
                fileList.append(os.path.join(dirPath, f))

#     fileNameList = getFilesInFolder(folderPath)
    pathFolder = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/line_patch'
    #     datasetFlieName = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/line_feature.npy'
    with open(output, 'wb') as outcsv:
        writer = csv.writer(outcsv, dialect='excel')
        header = [
            'filename', 'distance', 'num_node', "score", "num_leaf",
            "count_contourBoxes", "count_shareBox"
        ]
        writer.writerow(header)

        for row in data:
            writer.writerow(row)


if __name__ == '__main__':

    clfPath = "/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/line_patch/models/RF.pkl"
    phyloParser = PhyloParser(clfPath=clfPath)
    ground_truth_path = "/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/hq_ground_truth0228.csv"
    folderPath = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/high_quality_tree'
    ground_truth = {}
    fileNameList = []
    with open(ground_truth_path, 'rb') as incsv:
        reader = csv.reader(incsv, dialect='excel')
        reader.next()

        for row in reader:
            ground_truth[row[0]] = row[1]
            #             print row[0], row[1]
            fileNameList.append(row[0])

#     fileNameList = getFilesInFolder(folderPath)
    outFileName = '/Users/sephon/Desktop/Research/VizioMetrics/Corpus/Phylogenetic/CNN_corpus/high_quality_tree_20170307_rline_new.csv'
def execute(filePath):

    ############ run experiment ######################

    # a = ExperimentExecutor()

    # a.autoRun('high_quality_tree/', 'groundTruth.csv', 'compareResult_0124.csv')

    ###################################################

    # PMC2323573_pone.0002033.g002.jpg

    try:
        phyloParser = PhyloParser(clfPath=clfPath)

        # folderPath = 'images/'
        # folderPath = 'high_quality_tree/'

        # fileNameList = getFilesInFolder(folderPath)

        # fileNameList.sort()
        # with open('image_fileName_list.txt', 'r')as f:
        #     fileNameList = f.readlines()

        # testingFilePath = [join(folderPath, x) for x in fileNameList]
        # result = {}
        # groundTruth = {}
        # a = ExperimentExecutor()
        # a.execute(result, groundTruth, testingFilePath)

        # for index, fileName in enumerate(fileNameList):
        #     print index, fileName
        #         # f.write(fileName + '\n')
        # fileNameList = [x.rstrip() for x in fileNameList]
        # fileNameList = [folderPath + x for x in fileNameList]

        # for index in range(15, len(fileNameList)):
        # try:
        # print index, fileNameList[index]
        # filePath = folderPath + fileNameList[index]
        # fileName = fileNameList[index]
        # # fileName = 'PMC2614190_ebo-04-181-g01.jpg'
        # # fileName = 'PMC2674049_1471-2148-9-74-4.jpg'
        # # fileName = 'PMC1326215_1471-2148-5-71-6.jpg'
        # # fileName = 'PMC2644698_1471-2229-8-133-4.jpg'
        # # fileName = 'PMC2644698_1471-2229-8-133-4.jpg'
        # # fileName = 'PMC2775678_IJMB2009-701735.001.jpg'
        # # fileName = 'PMC2697986_1471-2148-9-107-3.jpg'
        # # fileName = 'PMC1182362_1471-2148-5-38-3.jpg'
        # filePath = folderPath + fileName
        # filePath = 'treeset/images/multi/1471-2148-10-52-2-l.jpg'
        if isfile(filePath):
            image = cv.imread(filePath, 0)
        # PhyloParser.displayImage(image)

        # imageData = ImageData(image)
        # imageData = PhyloParser.preprocces(imageData, debug = False)
        # # imageData = PhyloParser.detectLines__v2(imageData, debug=True)
        # imageData = PhyloParser.testing(imageData)

        image_data = ImageData(image)
        image_data = phyloParser.preprocces(image_data, debug=False)
        # PhyloParser.displayImage(image)

        image_data = phyloParser.detectLines(image_data, debug=False)

        # image_data = phyloPaser.traceTree(image_data, debug = True)
        image_data = phyloParser.getCorners(image_data, debug=False)
        image_data = phyloParser.makeLinesFromCorner(image_data, debug=False)
        image_data = phyloParser.includeLinesFromCorners(image_data)

        image_data = phyloParser.postProcessLines(image_data)

        # image_data = phyloParser.removeRepeatedLines(image_data)
        image_data = phyloParser.groupLines(image_data, debug=False)
        image_data = phyloParser.matchLineGroups(image_data, debug=False)

        #         # image_data = phyloParser.detectCorners(image_data)
        #         # image_data = phyloParser.refineLinesByCorners(image_data)
        #         # image_data = phyloPaser.getSpecies_v2(image_data, debug = True)
        #         # image_data = phyloPaser.getSpecies(image_data, debug = True)
        # image_data = phyloParser.matchLines(image_data, debug = True, useNew = False)
        # image_data = phyloParser.getSpecies_v2(image_data, debug = True)
        #         # print image_data.orphanBox2Text
        image_data = phyloParser.getSpecies_v3(image_data, debug=False)
        # testString = phyloParser.makeTree(image_data, debug = True, tracing = False)
        # treeString = phyloParser.constructTreeByTracing(image_data, debug = False)
        treeString = phyloParser.constructTree_eval(image_data,
                                                    debug=False,
                                                    tracing=False)

        dbdata = treeString.treeHead.getParentChildrenStyle()
        # print dbdata['cluster']

        with open('dbdata.csv', 'ab') as f:
            csvwriter = csv.writer(f, delimiter='\t')
            for i in range(dbdata['relation_index'] + 1):
                if i >= dbdata['count_labels']:
                    string = ''
                    for node in dbdata[i]:
                        string += '#' + str(node)
                    csvwriter.writerow([fileName, str(i), string])
                else:
                    csvwriter.writerow([fileName, str(i), dbdata[i]])
        with open('dbdata_cluster.csv', 'ab') as f:
            csvwriter = csv.writer(f, delimiter='\t')
            if 'root' in dbdata['cluster']:
                csvwriter.writerow([
                    fileName,
                    str(dbdata['relation_index']), dbdata['cluster']['root']
                ])
            for i in range(dbdata['relation_index']):
                csvwriter.writerow([fileName, str(i), dbdata['cluster'][i]])
    except:
        with open('error.txt', 'ab') as f:
            f.write(filePath + '\n')