Beispiel #1
0
def display_tensor_with_zoom(
    fig, V, gid, label, width, xlabel, ylabel, zoom=16, x=0.15, w=0.03, normalize_small=False, skim=0.4
):
    n = V.shape[0]

    def render(x):
        return posneg(x, skim=skim)

    rgb = render(V)

    if n > 50:
        fig.save_graphics_data(jpg_data(rgb), MIME_JPG, gid)
    else:
        rgb = rgb_zoom(rgb, 16)
        fig.save_graphics_data(png_data(rgb), MIME_PNG, gid)

    if n > 100:
        xp = int(x * n)
        wp = int(w * n)

        if normalize_small:
            # let's normalize the values in the rectangle
            cut = V[xp : (xp + wp), xp : (xp + wp)]
            cut_rgb = render(cut)
        else:
            cut_rgb = rgb[xp : (xp + wp), xp : (xp + wp), :]

        cut_rgb = rgb_zoom(cut_rgb, zoom)

        fig.save_graphics_data(png_data(cut_rgb), MIME_PNG, gid + "-zoom")
        fig.tex("\\tensorFigure{%s}{%s}{%s}{%s}{%s}{%s}{%s}%%\n" % (gid, width, xlabel, ylabel, label, x, w))
    else:
        fig.tex("\\tensorFigure{%s}{%s}{%s}{%s}{%s}{%s}{%s}%%\n" % (gid, width, xlabel, ylabel, label, "", ""))
 def display(self, report):
     if not self.initialized():
         report.text('notice', 'Cannot display() because not initialized.')
         return
     
     report.text('estimator', type(self).__name__)
     report.data('num_samples', self.num_samples)
     
     score = self._get_score()
     flattening = self.flat_structure.flattening
     min_score = flattening.flat2rect(np.min(score, axis=1))
     max_score = flattening.flat2rect(np.max(score, axis=1))
     caption = 'minimum and maximum score per pixel across neighbors'
     f = report.figure('scores', caption=caption)
     f.data('min_score', min_score).display('scale')
     f.data('max_score', max_score).display('scale')
     
     f = report.figure(cols=4)
     
     def make_best(x):
         return x == np.min(x, axis=1)
 
     max_d = int(np.ceil(np.hypot(np.floor(self.area[0] / 2.0),
                                  np.floor(self.area[1] / 2.0))))
     safe_d = int(np.floor(np.min(self.area) / 2.0))
     
     
     def plot_safe(pylab):
         plot_vertical_line(pylab, safe_d, 'g--')
         plot_vertical_line(pylab, max_d, 'r--')
         
     if self.shape[0] <= 64: 
         as_grid = self.make_grid(score)
         f.data_rgb('grid', rgb_zoom(scale(as_grid), 4))
     else:
         report.text('warn', 'grid visualization not done because too big')
         
     distance_to_border = distance_to_border_for_best(self.flat_structure, score) 
     distance_to_center = distance_from_center_for_best(self.flat_structure, score)
     
     bdist_scale = dict(min_value=0, max_value=max_d, max_color=[0, 1, 0])
     d = report.data('distance_to_border', distance_to_border).display('scale', **bdist_scale)
     d.add_to(f, caption='Distance to border of best guess, white=0, green=%d' % max_d)
     
     cdist_scale = dict(min_value=0, max_value=max_d, max_color=[1, 0, 0])
     d = report.data('distance_to_center', distance_to_center).display('scale', **cdist_scale)
     d.add_to(f, caption='Distance to center of best guess, white=0, red=%d' % max_d)
     
     bins = np.array(range(max_d + 2))
     # values will be integers (0, 1, 2, ...), center bins appropriately
     bins = bins - 0.5
     with f.plot('distance_to_border_hist') as pylab:
         pylab.hist(distance_to_border.flat, bins)
     
     caption = 'Red line: on border; Green line (red-1): safe to be here.'
     with f.plot('distance_to_center_hist', caption=caption) as pylab:
         pylab.hist(distance_to_center.flat, bins)
         plot_safe(pylab)
Beispiel #3
0
 def write_image(name, values):    
     time_us = timestamp * 1000 * 1000
     dirname2 = os.path.join(dirname, name) 
     if not os.path.exists(dirname2):
         os.makedirs(dirname2)
     filename = os.path.join(dirname2, 'ms%08d.png' % time_us)
     if np.nanmin(values) < 0:
         rgb = posneg(values)
     else:
         rgb = scale(values)
     rgb = rgb_zoom(rgb, K=4)
     imwrite(rgb, filename)
     logger.debug('print to %r' % filename)
Beispiel #4
0
 def write_image(name, values):
     time_us = timestamp * 1000 * 1000
     dirname2 = os.path.join(dirname, name)
     if not os.path.exists(dirname2):
         os.makedirs(dirname2)
     filename = os.path.join(dirname2, 'ms%08d.png' % time_us)
     if np.nanmin(values) < 0:
         rgb = posneg(values)
     else:
         rgb = scale(values)
     rgb = rgb_zoom(rgb, K=4)
     imwrite(rgb, filename)
     logger.debug('print to %r' % filename)
Beispiel #5
0
def template_bds_P(frag, id_set, id_robot, id_agent, width="3cm"):
    report = load_report_phase(id_set=id_set, agent=id_agent, robot=id_robot, phase="learn")
    gid = "%s-%s-%s-P" % (id_set, id_robot, id_agent)
    V = report["estimator/tensors/P/value"].raw_data

    n = V.shape[0]
    rgb = posneg(V)

    if n > 50:
        frag.save_graphics_data(jpg_data(rgb), MIME_JPG, gid)
    else:
        rgb = rgb_zoom(rgb, 16)
        frag.save_graphics_data(png_data(rgb), MIME_PNG, gid)

    #    frag.save_graphics_data(node.raw_data, node.mime, gid)
    tensor_figure(frag, gid=gid, xlabel="s", ylabel="v", width=width, label="\TPe^{sv}")
Beispiel #6
0
def visualize_result(id_tc, id_algo, stats):
    """ Returns a report """

    result = stats['result']
    r = Report('%s-%s' % (id_tc, id_algo))
    #    tc = config.testcases.instance(id_tc)
    #    discdds = config.discdds.instance(tc.id_discdds)
    algo = stats['algo']
    tc = stats['tc']
    discdds = algo.get_dds()

    tc.display(r.section('testcase'), discdds=discdds)

    if not result.success:
        r.text('warning', 'Plannning unsuccesful')
    else:
        rsol = r.section('solution')
        rsol.text('plan', 'Plan: %s' % str(result.plan))

        y0 = tc.y0
        y1 = tc.y1
        y1plan = discdds.predict(y0, result.plan)
        mismatch = np.abs(y1.get_values() - y1plan.get_values()).sum(axis=2)

        f = rsol.figure(cols=4)
        zoom = lambda x: rgb_zoom(x, 8)

        f.data_rgb('y1plan',
                   zoom(y1plan.get_rgb()),
                   caption='plan prediction (certain)')
        f.data_rgb('y1plan_certain',
                   zoom(y1plan.get_rgb_uncertain()),
                   caption='certainty of prediction')

        f.data_rgb('mismatch',
                   zoom(scale(mismatch)),
                   caption='Mismatch value pixel by pixel '
                   '(zero for synthetic testcases...)')

    algo.plan_report(r.section('planning'), result, tc)

    extra = result.extra

    write_log_lines(r, extra)

    return r
def visualize_result(id_tc, id_algo, stats):
    """ Returns a report """
    
    result = stats['result']
    r = Report('%s-%s' % (id_tc, id_algo))
#    tc = config.testcases.instance(id_tc)
#    discdds = config.discdds.instance(tc.id_discdds)
    algo = stats['algo']
    tc = stats['tc']
    discdds = algo.get_dds()
    
    tc.display(r.section('testcase'), discdds=discdds)
    
    if not result.success:
        r.text('warning', 'Plannning unsuccesful')
    else:
        rsol = r.section('solution')
        rsol.text('plan', 'Plan: %s' % str(result.plan))
    
        y0 = tc.y0
        y1 = tc.y1
        y1plan = discdds.predict(y0, result.plan)
        mismatch = np.abs(y1.get_values() - y1plan.get_values()).sum(axis=2)
        
        f = rsol.figure(cols=4)
        zoom = lambda x: rgb_zoom(x, 8)
        
        f.data_rgb('y1plan', zoom(y1plan.get_rgb()),
                   caption='plan prediction (certain)')
        f.data_rgb('y1plan_certain', zoom(y1plan.get_rgb_uncertain()),
                   caption='certainty of prediction')
        
        f.data_rgb('mismatch', zoom(scale(mismatch)),
                   caption='Mismatch value pixel by pixel '
                            '(zero for synthetic testcases...)')
    
    
    algo.plan_report(r.section('planning'), result, tc)
    
    extra = result.extra
    
    write_log_lines(r, extra)
    
    return r
def visualize_result(config, id_tc, id_algo, stats):
    """ Returns a report """
    set_current_config(config)

    result = stats["result"]
    r = Report("%s-%s" % (id_tc, id_algo))
    #    tc = config.testcases.instance(id_tc)
    #    discdds = config.discdds.instance(tc.id_discdds)
    algo = stats["algo"]
    tc = stats["tc"]
    discdds = algo.get_dds()

    tc.display(r.section("testcase"), discdds=discdds)

    if not result.success:
        r.text("warning", "Plannning unsuccesful")
    else:
        rsol = r.section("solution")
        rsol.text("plan", "Plan: %s" % str(result.plan))

        y0 = tc.y0
        y1 = tc.y1
        y1plan = discdds.predict(y0, result.plan)
        mismatch = np.abs(y1.get_values() - y1plan.get_values()).sum(axis=2)

        f = rsol.figure(cols=4)
        zoom = lambda x: rgb_zoom(x, 8)

        f.data_rgb("y1plan", zoom(y1plan.get_rgb()), caption="plan prediction (certain)")
        f.data_rgb("y1plan_certain", zoom(y1plan.get_rgb_uncertain()), caption="certainty of prediction")

        f.data_rgb(
            "mismatch",
            zoom(scale(mismatch)),
            caption="Mismatch value pixel by pixel " "(zero for synthetic testcases...)",
        )

    algo.plan_report(r.section("planning"), result, tc)

    extra = result.extra

    write_log_lines(r, extra)

    return r
 def add_pieces(sub, V, n, zoom=16):
     pieces = get_pieces(V, n)
     for k, piece in enumerate(pieces):
         with sub.subsection("%d" % k) as subk:
             value = piece["value"]
             rgb = posneg(value)
             rgb = rgb_zoom(rgb, zoom)
             # XXX
             subk.r.data_rgb("png", rgb, mime=MIME_PNG)
             rel = subk.section("rel")
             rel.r.data("x", piece["xfrac"])
             rel.r.data("y", piece["yfrac"])
             rel.r.data("width", piece["wfrac"])
             rel.r.data("height", piece["wfrac"])
             pixels = subk.section("pixels")
             pixels.r.data("x", piece["x"])
             pixels.r.data("y", piece["y"])
             pixels.r.data("width", piece["w"])
             pixels.r.data("height", piece["h"])
def pub_save_versions(pub, rgb, png_zoom=4):
    if False:
        jpg_zoom = 1
        pub.data_rgb("jpg", rgb_zoom(rgb, jpg_zoom), mime=MIME_JPG, caption="Converted to JPG")
    d = pub.data_rgb("png", rgb_zoom(rgb, png_zoom), mime=MIME_PNG)
    return d
Beispiel #11
0
 def zoom(rgb):
     """ Enlarge image so that pixels are distinguishable even though
         the brows makes them smooth. """
     return rgb_zoom(rgb, K=8)
Beispiel #12
0
 def zoom(rgb):
     """ Enlarge image so that pixels are distinguishable even though
         the brows makes them smooth. """
     return rgb_zoom(rgb, K=8)
Beispiel #13
0
    def display(self, report):
        if not self.initialized():
            report.text('notice', 'Cannot display() because not initialized.')
            return

        report.text('estimator', type(self).__name__)
        report.data('num_samples', self.num_samples)

        score = self._get_score()
        flattening = self.flat_structure.flattening
        min_score = flattening.flat2rect(np.min(score, axis=1))
        max_score = flattening.flat2rect(np.max(score, axis=1))
        caption = 'minimum and maximum score per pixel across neighbors'
        f = report.figure('scores', caption=caption)
        f.data('min_score', min_score).display('scale')
        f.data('max_score', max_score).display('scale')

        f = report.figure(cols=4)

        def make_best(x):
            return x == np.min(x, axis=1)

        max_d = int(
            np.ceil(
                np.hypot(np.floor(self.area[0] / 2.0),
                         np.floor(self.area[1] / 2.0))))
        safe_d = int(np.floor(np.min(self.area) / 2.0))

        def plot_safe(pylab):
            plot_vertical_line(pylab, safe_d, 'g--')
            plot_vertical_line(pylab, max_d, 'r--')

        if self.shape[0] <= 64:
            as_grid = self.make_grid(score)
            f.data_rgb('grid', rgb_zoom(scale(as_grid), 4))
        else:
            report.text('warn', 'grid visualization not done because too big')

        distance_to_border = distance_to_border_for_best(
            self.flat_structure, score)
        distance_to_center = distance_from_center_for_best(
            self.flat_structure, score)

        bdist_scale = dict(min_value=0, max_value=max_d, max_color=[0, 1, 0])
        d = report.data('distance_to_border',
                        distance_to_border).display('scale', **bdist_scale)
        d.add_to(
            f,
            caption='Distance to border of best guess, white=0, green=%d' %
            max_d)

        cdist_scale = dict(min_value=0, max_value=max_d, max_color=[1, 0, 0])
        d = report.data('distance_to_center',
                        distance_to_center).display('scale', **cdist_scale)
        d.add_to(f,
                 caption='Distance to center of best guess, white=0, red=%d' %
                 max_d)

        bins = np.array(range(max_d + 2))
        # values will be integers (0, 1, 2, ...), center bins appropriately
        bins = bins - 0.5
        with f.plot('distance_to_border_hist') as pylab:
            pylab.hist(distance_to_border.flat, bins)

        caption = 'Red line: on border; Green line (red-1): safe to be here.'
        with f.plot('distance_to_center_hist', caption=caption) as pylab:
            pylab.hist(distance_to_center.flat, bins)
            plot_safe(pylab)
Beispiel #14
0
def _nmapobs_to_rgb(m):
    print m.shape
    m = m.T
    rgb = scale(m, min_value=0, max_value=1, nan_color=[0.6, 1, 0.6])
    return rgb_zoom(rgb, 4)