def build_feature_importance_legend()

in apps/cloudwatch-dashboard/lambdas/plot-feature-importance-legend/handler.py [0:0]


def build_feature_importance_legend(model_name, width, height):
    model_response = l4e_client.describe_model(ModelName=model_name)
    predictions = json.loads(model_response['ModelMetrics'])['predicted_ranges']

    diagnostics = predictions[0]['diagnostics']
    tags_list = [d['name'].split('\\')[-1] for d in diagnostics]
    colors = set_aws_stylesheet()
    matplotlib.rcParams['figure.facecolor'] = 'FFFFFF'
    palette = {s: colors[index % len(colors)] for index, s in enumerate(tags_list)}

    # Create legend handles manually:
    handles = [matplotlib.patches.Patch(color=palette[x], label=x) for x in palette.keys()]
    
    # Create legend:
    fig = plt.figure(figsize=(width/dpi, height/dpi), dpi=dpi)
    plt.legend(handles=handles, loc='upper left')
    plt.gca().set_axis_off()

    # Build the SVG from this figure:
    svg_io = StringIO()
    fig.savefig(svg_io, format="svg", bbox_inches='tight')
    
    return svg_io.getvalue().replace('DejaVu Sans', 'Amazon Ember')