def print_odd_ones(self, pub, P, perc=50, ratio=0.2):
     std = np.sqrt(P.diagonal())
     odd, okay = find_odd(std, perc, ratio)
     
     f = pub.figure()
     pub.text('okay', '%s' % list(okay))
     pub.text('odd', '%s' % list(odd))
     
     std = std.copy()
     with f.plot('odd') as pylab:
         style_ieee_fullcol_xy(pylab)
         pylab.plot(odd, std[odd], 'rs')
         pylab.plot(okay, std[okay], 'gs')
         y_axis_extra_space(pylab)
Exemple #2
0
    def publish(self, pub):
        if self.y_stats.get_num_samples() == 0:
            pub.text('warning', 'Too early to publish anything.')
            return
        Py = self.y_stats.get_covariance()
        Ry = self.y_stats.get_correlation()
        Py_inv = self.y_stats.get_information()
        Ey = self.y_stats.get_mean()
        y_max = self.y_stats.get_maximum()
        y_min = self.y_stats.get_minimum()

        Ry0 = Ry.copy()
        np.fill_diagonal(Ry0, np.NaN)
        Py0 = Py.copy()
        np.fill_diagonal(Py0, np.NaN)

        pub.text('stats', 'Num samples: %s' % self.y_stats.get_num_samples())

        with pub.plot('y_bounds') as pylab:
            style_ieee_fullcol_xy(pylab)
            pylab.plot(Ey, label='E(y)')
            pylab.plot(y_max, label='y_max')
            pylab.plot(y_min, label='y_min')
            pylab.legend()

        all_positive = (np.min(Ey) > 0
                        and np.min(y_max) > 0
                        and np.min(y_min) > 0)
        if all_positive:
            with pub.plot('y_stats_log') as pylab:
                style_ieee_fullcol_xy(pylab)
                pylab.semilogy(Ey, label='E(y)')
                pylab.semilogy(y_max, label='y_max')
                pylab.semilogy(y_min, label='y_min')
                pylab.legend()

        pub.array_as_image('Py', Py, caption='cov(y)')
        pub.array_as_image('Py0', Py0, caption='cov(y) - no diagonal')

        pub.array_as_image('Ry', Ry, caption='corr(y)')
        pub.array_as_image('Ry0', Ry0, caption='corr(y) - no diagonal')

        pub.array_as_image('Py_inv', Py_inv)
        pub.array_as_image('Py_inv_n', cov2corr(Py_inv))

        with pub.plot('Py_svd') as pylab:  # XXX: use spectrum
            style_ieee_fullcol_xy(pylab)
            _, s, _ = np.linalg.svd(Py)
            s /= s[0]
            pylab.semilogy(s, 'bx-')

        with pub.subsection('y_stats') as sub:
            self.y_stats.publish(sub)
    def publish(self, pub, publish_information=False):
        if self.num_samples == 0:
            pub.text('warning',
                     'Cannot publish anything as I was never updated.')
            return

        P = self.get_covariance()
        R = self.get_correlation()
        Ey = self.get_mean()
        y_max = self.get_maximum()
        y_min = self.get_minimum()

        pub.text('data', 'Num samples: %s' % self.mean_accum.get_mass())

        if Ey.size > 1:
            with pub.plot('expectation') as pylab:
                style_ieee_fullcol_xy(pylab)
                pylab.plot(Ey, 's', label='expectation')
                pylab.plot(y_max, 's', label='max')
                pylab.plot(y_min, 's', label='min')
                y_axis_extra_space(pylab)
                pylab.legend()
            
            stddev = np.sqrt(P.diagonal())
            with pub.plot('stddev') as pylab:
                style_ieee_fullcol_xy(pylab)
                pylab.plot(stddev, 's', label='stddev')
                y_axis_extra_space(pylab)
                pylab.legend()
                
            self.print_odd_ones(pub, P, perc=50, ratio=0.2)

            from boot_agents.misc_utils.tensors_display import pub_tensor2_cov
            pub_tensor2_cov(pub, 'covariance', P)
            # pub.array_as_image('covariance', P)
            

            # TODO: get rid of this?
            R = R.copy()
            np.fill_diagonal(R, np.nan)
            pub.array_as_image('correlation', R)
            if publish_information:
                P_inv = self.get_information()
                pub.array_as_image('information', P_inv)

            with pub.plot('P_diagonal') as pylab:
                style_ieee_fullcol_xy(pylab)
                pylab.plot(P.diagonal(), 's')
                y_axis_positive(pylab)

            with pub.plot('P_diagonal_sqrt') as pylab:
                style_ieee_fullcol_xy(pylab)
                pylab.plot(np.sqrt(P.diagonal()), 's')
                y_axis_positive(pylab)
        else:
            stats = ""
            stats += 'min: %g\n' % y_min
            stats += 'mean: %g\n' % Ey
            stats += 'max: %g\n' % y_min
            stats += 'std: %g\n' % np.sqrt(P)
            pub.text('stats', stats)
Exemple #4
0
def servo_stats_report(data_central, id_agent, id_robot, summaries,
                       phase='servo_stats'):
    from reprep import Report
    from reprep.plot_utils import x_axis_balanced
    from reprep.plot_utils import (style_ieee_fullcol_xy,
        style_ieee_halfcol_xy)
    from geometry import translation_from_SE2
    
    if not summaries:
        raise Exception('Empty summaries')

    def extract(key):
        return np.array([s[key] for s in summaries])

    initial_distance = extract('initial_distance')
    initial_rotation = extract('initial_rotation')

    dist_xy_converged = 0.25
    dist_th_converged = np.deg2rad(5)

    for s in summaries:
        dist_xy = s['dist_xy']
        dist_th = s['dist_th']
        # converged = (dist_xy[0] > dist_xy[-1]) and (dist_th[0] > dist_th[-1])

        converged = ((dist_xy[-1] < dist_xy_converged) and
                     (dist_th[-1] < dist_th_converged))
        s['converged'] = converged
        s['dist_th_deg'] = np.rad2deg(s['dist_th'])
        s['color'] = 'b' if converged else 'r'

        trans = [translation_from_SE2(x) for x in s['poses']]
        s['x'] = np.abs([t[0] for t in trans])
        s['y'] = np.abs([t[1] for t in trans])
        s['dist_x'] = np.abs(s['x'])
        s['dist_y'] = np.abs(s['y'])

    basename = 'servo_analysis-%s-%s-%s' % (id_agent, id_robot, phase)
    r = Report(basename)
    r.data('summaries', s, caption='All raw statistics')

    f = r.figure(cols=3)

    with f.plot('image_L2_error') as pylab:
        style_ieee_fullcol_xy(pylab)
        for s in summaries:
            errors = s['errors']
            pylab.plot(errors, s['color'])

    with f.plot('dist_xy') as pylab:
        style_ieee_fullcol_xy(pylab)
        for s in summaries:
            pylab.plot(s['dist_xy'], s['color'] + '-')

    with f.plot('xy') as pylab:
        style_ieee_fullcol_xy(pylab)
        for s in summaries:
            pylab.plot(s['x'], s['y'], s['color'] + '-')
        pylab.xlabel('x')
        pylab.ylabel('y')

    with f.plot('dist_th') as pylab:
        style_ieee_fullcol_xy(pylab)
        for s in summaries:
            pylab.plot(s['dist_th'], s['color'] + '-')

    with f.plot('dist_th_deg') as pylab:
        style_ieee_fullcol_xy(pylab)
        for s in summaries:
            pylab.plot(np.rad2deg(s['dist_th']), s['color'] + '-')

    with f.plot('dist_xy_th') as pl:
        style_ieee_fullcol_xy(pylab)
        for s in summaries:
            pl.plot(s['dist_xy'], s['dist_th_deg'], s['color'] + '-')
        pl.xlabel('dist x-y')
        pl.ylabel('dist th (deg)')

    with f.plot('dist_xy_th_log') as pl:
        style_ieee_fullcol_xy(pylab)
        for s in summaries:
            pl.semilogx(s['dist_xy'],
                        s['dist_th_deg'], s['color'] + '.')
        pl.xlabel('dist x-y')
        pl.ylabel('dist th (deg)')
    
    with f.plot('dist_y') as pylab:
        style_ieee_fullcol_xy(pylab)
        for s in summaries:
            pylab.plot(s['dist_y'], s['color'] + '-')
    with f.plot('dist_x') as pylab:
        style_ieee_fullcol_xy(pylab)
        for s in summaries:
            pylab.plot(s['dist_x'], s['color'] + '-')

    mark_start = 's'
    mark_end = 'o'

    with f.plot('dist_xy_th_start',
                caption="Initial error (blue: converged)"
                ) as pl:
        style_ieee_fullcol_xy(pylab)
        for s in summaries:
            pl.plot([s['dist_xy'][0], s['dist_xy'][-1]],
                     [s['dist_th_deg'][0],
                       s['dist_th_deg'][-1]], s['color'] + '-')
            pl.plot(s['dist_xy'][0], s['dist_th_deg'][0],
                    s['color'] + mark_start)
            pl.plot(s['dist_xy'][-1], s['dist_th_deg'][-1],
                    s['color'] + mark_end)

        pl.xlabel('dist x-y')
        pl.ylabel('dist th (deg)')

    with f.plot('dist_xy_th_start2',
                caption="Trajectories. If converged, plot square at beginning"
                " and cross at end. If not converged, plot trajectory (red)."
                ) as pl:
        style_ieee_fullcol_xy(pylab)
        for s in summaries:
            if s['converged']: 
                continue

            pl.plot([s['dist_xy'][0], s['dist_xy'][-1]],
                     [s['dist_th_deg'][0], s['dist_th_deg'][-1]], 'r-')

        for s in summaries:
            pl.plot(s['dist_xy'][0], s['dist_th_deg'][0], s['color'] + mark_start)
            pl.plot(s['dist_xy'][-1], s['dist_th_deg'][-1], s['color'] + mark_end)

        pl.xlabel('dist x-y')
        pl.ylabel('dist th (deg)')

    with f.plot('initial_rotation') as pylab:
        style_ieee_halfcol_xy(pylab)
        pylab.hist(np.rad2deg(initial_rotation))
        x_axis_balanced(pylab)
        pylab.xlabel('Initial rotation (deg)')

    with f.plot('initial_distance') as pylab:
        style_ieee_halfcol_xy(pylab)
        pylab.hist(initial_distance)
        pylab.xlabel('Initial distance (m)')

    ds = data_central.get_dir_structure()
    filename = ds.get_report_filename(id_agent=id_agent,
                                       id_robot=id_robot,
                                       id_state='servo_stats',
                                       phase=phase)
    resources_dir = ds.get_report_res_dir(id_agent=id_agent,
                                       id_robot=id_robot,
                                       id_state='servo_stats',
                                       phase=phase)
    save_report(data_central, r, filename, resources_dir, save_pickle=True)