# Buggy code
def get_figures(boxes, page_bbox, page_num, boxes_figures, page_width, page_height):
    plane = Plane(page_bbox)
    plane.extend(boxes)

    nodes_figures = []

    for fig_box in boxes_figures:
        node_fig = Node(fig_box)
        nodes_figures.append(node_fig)

    merge_indices = [i for i in range(len(nodes_figures))]
    page_stat = Node(boxes)
    nodes, merge_indices = merge_nodes(nodes_figures, plane, page_stat, merge_indices)

    ##Merging Nodes
    new_nodes = []
    for idx in range(len(merge_indices)):
        if merge_indices[idx] == idx:
            new_nodes.append(nodes[idx])

    figures = [
        (page_num, page_width, page_height) + (node.y0, node.x0, node.y1, node.x1)
        for node in new_nodes
    ]
    return figures


# Traceback (IndexError)
$ pdftotree -vv dtc114w.pdf
[INFO] pdftotree.core - Digitized PDF detected, building tree structure...
[ERROR] pdftotree.TreeExtract - list index out of range
Traceback (most recent call last):
File "/home/lwhsiao/repos/pdftotree/pdftotree/TreeExtract.py", line 148, in get_candidates_alignments
nodes, features = parse_layout(elems, font_stat)
File "/home/lwhsiao/repos/pdftotree/pdftotree/utils/pdf/pdf_parsers.py", line 36, in parse_layout
avg_font_pts = get_most_common_font_pts(elems.mentions, font_stat)
File "/home/lwhsiao/repos/pdftotree/pdftotree/utils/pdf/pdf_parsers.py", line 1159, in get_most_common_font_pts
most_common_font_size = font_stat.most_common(1)[0][0]
IndexError: list index out of range
Traceback (most recent call last):
File "/home/lwhsiao/repos/pdftotree/.venv/bin/pdftotree", line 6, in <module>
exec(compile(open(__file__).read(), __file__, 'exec'))
File "/home/lwhsiao/repos/pdftotree/bin/pdftotree", line 82, in <module>
args.favor_figures, args.visualize)
File "/home/lwhsiao/repos/pdftotree/pdftotree/core.py", line 58, in parse
pdf_tree = extractor.get_tree_structure(model, favor_figures)
File "/home/lwhsiao/repos/pdftotree/pdftotree/TreeExtract.py", line 188, in get_tree_structure
favor_figures)
File "/home/lwhsiao/repos/pdftotree/pdftotree/utils/pdf/pdf_parsers.py", line 666, in parse_tree_structure
avg_font_pts = get_most_common_font_pts(elems.mentions, font_stat)
File "/home/lwhsiao/repos/pdftotree/pdftotree/utils/pdf/pdf_parsers.py", line 1159, in get_most_common_font_pts
most_common_font_size = font_stat.most_common(1)[0][0]
IndexError: list index out of range

# Fixed code
def get_figures(boxes, page_bbox, page_num, boxes_figures, page_width, page_height):
    if len(boxes) == 0:
        log.warning("No boxes to get figures from on page {}.".format(page_num))
        return []

    plane = Plane(page_bbox)
    plane.extend(boxes)

    nodes_figures = []

    for fig_box in boxes_figures:
        node_fig = Node(fig_box)
        nodes_figures.append(node_fig)

    merge_indices = [i for i in range(len(nodes_figures))]
    page_stat = Node(boxes)
    nodes, merge_indices = merge_nodes(nodes_figures, plane, page_stat, merge_indices)

    ##Merging Nodes
    new_nodes = []
    for idx in range(len(merge_indices)):
        if merge_indices[idx] == idx:
            new_nodes.append(nodes[idx])

    figures = [
        (page_num, page_width, page_height) + (node.y0, node.x0, node.y1, node.x1)
        for node in new_nodes
    ]
    return figures