def report_quality_metrics(designs, metrics, path, clustering=False):
    """
    Create a nicely formatted spreadsheet showing all the designs and metrics.
    """
    import xlsxwriter
    print "Reporting quality metrics..."

    # Open a XLSX worksheet.

    workbook = xlsxwriter.Workbook(path)
    worksheet = workbook.add_worksheet()

    workbook.formats[0].border = 1
    workbook.formats[0].border_color = 'gray'

    # Setup the cell background color highlights.

    from matplotlib.cm import ScalarMappable
    from matplotlib.colors import LinearSegmentedColormap

    best_color = np.array([93, 175, 219]) / 255    # UCSF Blue (70%)
    worst_color = np.array([255, 255, 255]) / 255
    color_table = {
            'red':   [(0.0, worst_color[0], worst_color[0]),
                      (1.0,  best_color[0],  best_color[0])],
            'green': [(0.0, worst_color[1], worst_color[1]),
                      (1.0,  best_color[1],  best_color[1])],
            'blue':  [(0.0, worst_color[2], worst_color[2]),
                      (1.0,  best_color[2],  best_color[2])],
    }
    color_map = LinearSegmentedColormap('highlight', color_table)

    # Write the header row.
    for col, metric in enumerate(metrics):
        cell = xlsxwriter.utility.xl_col_to_name(col) + '1'
        format = workbook.add_format(metric.header_format())
        worksheet.write(cell, metric.title, format)
        worksheet.set_column(col, col, metric.width)

    # Write the data rows.

    designs = designs[:]
    designs.sort(key=lambda x: x.restraint_dist)
    designs.sort(key=lambda x: x.sequence_cluster)

    for col, metric in enumerate(metrics):
        face_values = [metric.face_value(x) for x in designs]
        score_values = np.array([metric.score_value(x) for x in designs])
        cell_format = metric.cell_format()

        if metric.color:
            color_spectrum = ScalarMappable(cmap=color_map)
            rgba = (255 * color_spectrum.to_rgba(score_values)).astype(int)
            colors = ['#{:02x}{:02x}{:02x}'.format(*x) for x in rgba]

        for index, face_value in enumerate(face_values):
            if face_value is None:
                continue
            if metric.color:
                cell_format.update({
                    'bg_color': colors[index],
                    'border_color': '#c7c7c7',
                    'border': 1 })

            format_handle = workbook.add_format(cell_format)
            worksheet.write(index + 1, col, face_value, format_handle)

    # Write the XLSX file.

    workbook.close()
Create a nicely formatted spreadsheet showing all the designs and metrics.