def plot_contrast(values):
    #print 'contrast', min(values), max(values)
    # im = values2retina(values, background=numpy.NaN)
    #print values[0:100].tolist()
    im = values2retina(values, background=numpy.NaN)
    return scale(im, min_color=[0, 0, 0], max_color=[0, 1, 1], 
                 nan_color=[0.5, 0.5, 0.5])
def create_sidebar(cr, width, height, sim_state, id_vehicle, id_episode,  # @UnusedVariable
                   timestamp, observations_values,
                   commands_values, commands_source,
                   bg_color=None,
                   show_observations=True,
                   show_commands=True,
                   show_annotations=True):

    if len(commands_values.shape) == 1:
        commands_values = np.array([commands_values.tolist()])

    commands_rgb = posneg(commands_values,
                          max_value=(+1),  # not sure +1 
                          nan_color=[1, 1, 1])

    observations_rgb = scale(reshape2d(observations_values), min_value=0,
                         nan_color=[1, 1, 1])

    import cairo
    if bg_color is not None:
        cr.rectangle(0, 0, width, height)
        cr.set_source_rgb(bg_color[0], bg_color[1], bg_color[2])
        cr.fill()

    fo = cairo.FontOptions()  # @UndefinedVariable
    fo.set_hint_style(cairo.HINT_STYLE_FULL)  # @UndefinedVariable
    fo.set_antialias(cairo.ANTIALIAS_GRAY)  # @UndefinedVariable
    cr.set_font_options(fo)

    # M = width / 20.0
    M = width / 15.0

    legend_font_size = M * 0.75
    details_font_size = M

    label_font = 'Mono'
    legend_font = 'Serif'

    cr.set_source_rgb(0, 0, 0)

    padding_fraction = 0.1
    padding = width * padding_fraction
    nvalues = 128
    bar_width = 0.4 * width
    bar_ratio = 0.15
    bar_height = bar_width * bar_ratio
    spacer = 0.05 * width

    values = np.linspace(-1, +1, nvalues)
    values = np.vstack([values] * 1)
    colorbar_posneg = posneg(values)
    values = np.linspace(-1, +1, nvalues)
    values = np.vstack([values] * 1)
    colorbar_scale = scale(values)

    cr.translate(0, 2 * M)

    if show_observations:
        with cairo_transform(cr, t=[width / 2, 0]):
            cr.select_font_face(label_font)
            cr.set_font_size(M)
            cairo_text_align(cr, 'observations', halign='center')

        cr.translate(0, M * 0.8)

        with cairo_transform(cr, t=[padding, 0]):
            data_width = width - 2 * padding
            # Don't draw grid if there are many pixels
            if max(observations_rgb.shape[0], observations_rgb.shape[1]) > 15:
                grid_color = None
            else:
                grid_color = [1, .9, .9]

            last_height = cairo_pixels(cr, observations_rgb, width=data_width,
                                       # Force square
                                       height=data_width,
                                       grid_color=grid_color)

        cr.translate(0, last_height)

        cr.translate(0, spacer)

        with cairo_transform(cr, t=[width / 2, 0]):
            with cairo_transform(cr, t=[-bar_width / 2, 0]):
                last_height = cairo_pixels(cr, colorbar_scale,
                             bar_width, height=bar_height,
                              grid_color=None)

            cr.set_font_size(legend_font_size)
            cr.select_font_face(legend_font)

            with cairo_transform(cr, t=[0, bar_height / 2]):
                with cairo_transform(cr, t=[-bar_width / 2 - M / 2, 0]):
                    cairo_text_align(cr, '0', 'right', 'middle')
                with cairo_transform(cr, t=[+bar_width / 2 + M / 2, 0]):
                    cairo_text_align(cr, '1', 'left', 'middle')

        cr.translate(0, last_height + spacer * 3)

    if show_commands:
        with cairo_transform(cr, t=[width / 2, 0]):
            cr.select_font_face(label_font)
            cr.set_font_size(M)
            cairo_text_align(cr, 'commands', halign='center')
        cr.translate(0, M * 0.8)

        padding = padding * 2
        with cairo_transform(cr, t=[padding, 0]):
            data_width = width - 2 * padding
            last_height = cairo_pixels(cr, commands_rgb, data_width)
        cr.translate(0, last_height)

        cr.translate(0, spacer)

        with cairo_transform(cr, t=[width / 2, 0]):
            with cairo_transform(cr, t=[-bar_width / 2, 0]):
                last_height = cairo_pixels(cr, colorbar_posneg,
                             bar_width, height=bar_width * bar_ratio,
                              grid_color=None)

            cr.set_font_size(legend_font_size)
            cr.select_font_face(legend_font)

            with cairo_transform(cr, t=[0, bar_height / 2]):
                with cairo_transform(cr, t=[-bar_width / 2 - M / 2, 0]):
                    cairo_text_align(cr, '-1', 'right', 'middle')
                with cairo_transform(cr, t=[+bar_width / 2 + M / 2, 0]):
                    cairo_text_align(cr, '+1', 'left', 'middle')

        cr.translate(0, last_height + spacer * 2)

    if show_annotations:
        cr.translate(width / 10, 0)
        strings = ['vehicle: %s' % id_vehicle,
                   '  agent: %s' % commands_source,
                   'episode: %s' % id_episode,
                   '   time: %6.2f' % timestamp,
                   ]
        cr.select_font_face('Mono')

        max_len = max(len(x) for x in strings)
        padding = 5
        font_size = 1.6 * width / (max_len + padding)
        cr.set_font_size(font_size)
        line = details_font_size * 1.2
        for s in strings:
            with cairo_save(cr):
                cr.show_text(s)
                cr.stroke()
            cr.translate(0, line)
def plot_nearness(values):
    distance = 1.0 / values
    im = values2retina(distance, background=numpy.NaN)
    return scale(im, min_color=[0, 0, 0], max_color=[0, 1, 0], 
                 nan_color=[0.5, 0.5, 0.5])