Example #1
0
def report_statistics(id_sub, stats):
    records = stats['records']
    distance = records['distance']
    delta = records['delta']
    order = scale_score(distance)
    order = order / float(order.size)

    r = Report('stats-%s' % id_sub)
    r.data('records', records)
    f = r.figure()

    with f.plot('scatter') as pylab:
        pylab.scatter(delta, distance)
        pylab.xlabel('delta')
        pylab.ylabel('distance')
        pylab.axis((-1, np.max(delta) + 1, -0.05, np.max(distance)))

    with f.plot('with_stats', **dp_predstats_fig) as pylab:
        fancy_error_display(pylab, delta, distance, 'g')

    with f.plot('distance_order', **dp_predstats_fig) as pylab:
        fancy_error_display(pylab, delta, order, color='k')

    f = r.figure(cols=1)
    bins = np.linspace(0, np.max(distance), 100)
    for i, d in enumerate(set(delta)):
        with f.plot('conditional%d' % i) as pylab:
            which = delta == d
            pylab.hist(distance[which], bins)

    return r
def hist_plots(d):
    # TO
    vars = [ ('C', d.C, {}),
             ('y', cov2corr(d.y_cov, False), {}),
             ('y_dot', cov2corr(d.y_dot_cov, False), {}),
             ('y_dot_sign', cov2corr(d.y_dot_sign_cov, False), {}) ] 

    r = Report()
    f = r.figure(cols=5)
    
    for var in vars:
        label = var[0]
        x = var[1]

        nid = "hist_%s" % label
        with r.data_pylab(nid) as pylab:
            pylab.hist(x.flat, bins=128)
        f.sub(nid, 'histogram of correlation of %s' % label)
            
        order = scale_score(x)
        r.data('order%s' % label, order).display('posneg').add_to(f, 'ordered')

        nid = "hist2_%s" % label
        with r.data_pylab(nid) as pylab:
            pylab.plot(x.flat, order.flat, '.', markersize=0.2)
            pylab.xlabel(label)
            pylab.ylabel('order')
        f.sub(nid, 'histogram of correlation of %s' % label)
        
        h = create_histogram_2d(d.C, x, resolution=128)
        r.data('h2d_%s' % label, numpy.flipud(h.T)).display('scale').add_to(f)
        
    return r
def simple_plots(d):
    # TO
    y_cov = d.y_cov
    y_dot_cov = d.y_dot_cov
    y_dot_sign_cov = d.y_dot_sign_cov
    
    vars = [ ('y', y_cov, {}),
             ('y_dot', y_dot_cov, {}),
             ('y_dot_sign', y_dot_sign_cov, {}) ] 
#
#    I = numpy.eye(y_cov.shape[0])
#    
    r = Report()
    f = r.figure(cols=3)
    for var in vars:
        label = var[0]
        cov = var[1]
        corr = cov2corr(cov, zero_diagonal=False)
        corr_z = cov2corr(cov, zero_diagonal=True)
        
        n1 = r.data("cov_%s" % label, cov).display('posneg')
        n2 = r.data("corr_%s" % label, corr).display('posneg')
        n3 = r.data("corrz_%s" % label, corr_z).display('posneg')
        
        f.sub(n1, 'Covariance of %s' % label)
        f.sub(n2, 'Correlation of %s ' % label)
        f.sub(n3, 'Correlation of %s (zeroing diagonal)' % label)
        
    return r
Example #4
0
def table_by_rows(id_report, samples, rows_field, cols_fields, source_descs):
    samples2 = StoreResultsDict(samples)
    
    class Missing(dict):
        def __missing__(self, key):
            logger.warning('Description for %r missing.' % key)
            d = WithDescription(name=key, symbol='\\text{%s}' % key,
                                desc=None)
            self[key] = d
            return d
        
    source_descs = Missing(source_descs)
        
    r = Report(id_report)
    data_views = [DataView.from_string(x, source_descs) for x in cols_fields]
    # data: list of list of list
    rows_field, data, reduced, display = summarize_data(samples2, rows_field, data_views)
    rows = ['$%s$' % source_descs[x].get_symbol() for x in rows_field]
    cols = ['$%s$' % x.get_symbol() for x in data_views]
    r.table('table', data=display, cols=cols, rows=rows)
    r.data('table_data', data=reduced,
           caption="Data without presentation applied.")
    r.data('table_data_source', data=data,
           caption="Source data, before reduction.")
    
    row_desc = "\n".join(['- $%s$: %s' % (x.get_symbol(), x.get_desc()) 
                          for x in map(source_descs.__getitem__, rows_field)])
    col_desc = "\n".join(['- $%s$: %s' % (x.get_symbol(), x.get_desc()) 
                          for x in data_views])
    r.text('row_desc', rst_escape_slash(row_desc), mime=MIME_RST)
    r.text('col_desc', rst_escape_slash(col_desc), mime=MIME_RST)    
    return  r
Example #5
0
def report_statistics(id_sub, stats):
    records = stats['records']
    distance = records['distance']
    delta = records['delta']
    order = scale_score(distance)
    order = order / float(order.size)

    r = Report('stats-%s' % id_sub)
    r.data('records', records)
    f = r.figure()
    
    with f.plot('scatter') as pylab:
        pylab.scatter(delta, distance)
        pylab.xlabel('delta')
        pylab.ylabel('distance')
        pylab.axis((-1, np.max(delta) + 1, -0.05, np.max(distance)))
        
    with f.plot('with_stats', **dp_predstats_fig) as pylab:
        fancy_error_display(pylab, delta, distance, 'g')

    with f.plot('distance_order', **dp_predstats_fig) as pylab:
        fancy_error_display(pylab, delta, order, color='k')
        
    f = r.figure(cols=1)        
    bins = np.linspace(0, np.max(distance), 100)
    for i, d in enumerate(set(delta)):
        with f.plot('conditional%d' % i) as pylab:
            which = delta == d
            pylab.hist(distance[which], bins)

    return r
Example #6
0
def filter_phase_report(stats):
    P = stats['P']

    r = Report('unknown')
    f = r.figure()
    r.data('P', P.T).display('scale').add_to(f)

    return r
Example #7
0
def filter_phase_report(stats):
    P = stats['P']
    
    r = Report('unknown')
    f = r.figure()
    r.data('P', P.T).display('scale').add_to(f)
    
    return r    
def create_report_delayed(exp_id, delayed, description):

    delays = numpy.array(sorted(delayed.keys()))

    r = Report(exp_id)
    r.text("description", description)

    f = r.figure(cols=3)

    # max and sum of correlation for each delay
    # corr_max = []
    corr_mean = []

    for delay in delays:
        data = delayed[delay]

        a = data["action_image_correlation"]

        id = "delay%d" % delay

        # rr = r.node('delay%d' % delay)
        r.data(id, a).data_rgb("retina", add_reflines(posneg(values2retina(a))))

        corr_mean.append(numpy.abs(a).mean())

        caption = "delay: %d (max: %.3f, sum: %f)" % (delay, numpy.abs(a).max(), numpy.abs(a).sum())
        f.sub(id, caption=caption)

    timestamp2ms = lambda x: x * (1.0 / 60) * 1000

    peak = numpy.argmax(corr_mean)
    peak_ms = timestamp2ms(delays[peak])
    with r.data_pylab("mean") as pylab:
        T = timestamp2ms(delays)
        pylab.plot(T, corr_mean, "o-")
        pylab.ylabel("mean correlation field")
        pylab.xlabel("delay (ms) ")

        a = pylab.axis()

        pylab.plot([0, 0], [a[2], a[3]], "k-")

        y = a[2] + (a[3] - a[2]) * 0.1
        pylab.text(+5, y, "causal", horizontalalignment="left")
        pylab.text(-5, y, "non causal", horizontalalignment="right")

        pylab.plot([peak_ms, peak_ms], [a[2], max(corr_mean)], "b--")

        y = a[2] + (a[3] - a[2]) * 0.2
        pylab.text(peak_ms + 10, y, "%d ms" % peak_ms, horizontalalignment="left")

    f = r.figure("stats")
    f.sub("mean")

    a = delayed[int(delays[peak])]["action_image_correlation"]
    r.data_rgb("best_delay", add_reflines(posneg(values2retina(a))))

    return r
class ReprepPublisher(Publisher):

    default_max_cols = 5

    def __init__(self, rid=None, report=None, cols=default_max_cols):
        # TODO: clear up this interface
        if report is None:
            self.r = Report(rid)
        else:
            self.r = report

        self.cols = cols
        self._f = None

    def fig(self):
        ''' Returns reference to current RepRep figure. '''
        if self._f is None:
            self._f = self.r.figure(cols=self.cols)
        return self._f

    @contract(name='str', value='array', caption='None|str')
    def array(self, name, value, caption=None):  # XXX to change
        self.r.data(name, value, mime=MIME_PYTHON, caption=caption)

    @contract(name='str', value='array', filter='str', caption='None|str')
    def array_as_image(self, name, value,
                       filter='posneg',  # @ReservedAssignment # XXX: config
                       filter_params={},
                       caption=None):  # @ReservedAssignment
        # try image XXX check uint8
        # If this is RGB
        if len(value.shape) == 3 and value.shape[2] == 3:
            # zoom images smaller than 50
            #            if value.shape[0] < 50:
            #                value = zoom(value, 10)
            self.fig().data_rgb(name, value, caption=caption)
        else:
            node = self.r.data(name, value, mime=MIME_PYTHON, caption=caption)
            m = node.display(filter, **filter_params)
            if caption is None:
                caption = name
            self.fig().sub(m, caption=caption)

    @contract(name='str', value='str')
    def text(self, name, value):
        self.r.text(name, value)

    @contextmanager
    @contract(name='str', caption='None|str')
    def plot(self, name, caption=None, **args):
        f = self.fig()
        # TODO: make a child of myself
        with f.plot(name, caption=caption, **args) as pylab:
            yield pylab

    def section(self, section_name, cols=default_max_cols, caption=None):
        child = self.r.node(section_name, caption=caption)
        return ReprepPublisher(report=child, cols=cols)
def report_predstats(id_discdds, id_subset, id_distances, records):
    r = Report('predistats-%s-%s' % (id_discdds, id_subset))
    print records.dtype
    r.data('records', records)
    f = r.figure()
    
    colors = list(islice(cycle(['r', 'g', 'b', 'k', 'y', 'm']), 50))
    delta = records['delta']
    W = 0.2
#    pdb.set_trace()
    # Save the raw values
    for i, id_d in enumerate(id_distances):
        r.data(id_d, records[id_d])
    
    with f.plot('values_order', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)

        for i, id_d in enumerate(id_distances):
            distance = records[id_d]
            distance_order = scale_score(distance) / (float(distance.size) - 1)
            
            step = float(i) / max(len(id_distances) - 1, 1)
            xstep = W * 2 * (step - 0.5) 
            fancy_error_display(ax, delta + xstep, distance_order,
                                colors[i], perc=10, label=id_d)
            
        ieee_spines(pylab)    
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('interval length')
        pylab.ylabel('normalized distance')
        pylab.xticks(ticks, ticks)
        pylab.yticks((0, 1), (0, 1))
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, 1.2))
        legend_put_below(ax)

    with f.plot('values', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)

        for i, id_d in enumerate(id_distances):
            distance = records[id_d]
            
            step = float(i) / max(len(id_distances) - 1, 1)
            xstep = W * 2 * (step - 0.5) 
            fancy_error_display(ax, delta + xstep, distance,
                                colors[i], perc=10, label=id_d)
            
        ieee_spines(pylab)    
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('interval length')
        pylab.ylabel('distance')
        pylab.xticks(ticks, ticks)
#        pylab.yticks((0, 1), (0, 1))
        a = pylab.axis()
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, a[3]))
        legend_put_below(ax)

    return r
Example #11
0
File: meat.py Project: kpykc/rcl
def aer_simple_stats_report(stats):
    r = Report('simplestatsreport')
    
    f = r.figure()
    for n in ['h_all', 'h_plus', 'h_minus']:
        h = stats[n]
        cap = '%d events' % (h.sum())
        r.data(n, h).display('scale').add_to(f, caption=cap)
    
    return r
Example #12
0
def aer_simple_stats_report(stats):
    r = Report("simplestatsreport")

    f = r.figure()
    for n in ["h_all", "h_plus", "h_minus"]:
        h = stats[n]
        cap = "%d events" % (h.sum())
        r.data(n, h).display("scale").add_to(f, caption=cap)

    return r
def basic_plots(d):
    G = d.G
    #G = skim_top_and_bottom(G, 1)
    #max_value = numpy.abs(G).max()
    r = Report('plots')
    f = r.figure('The learned G', cols=2)
    cmd = {0: 'vx', 1: 'vy', 2: 'omega'}
    grad = {0: 'hor', 1: 'vert'}
    for (k, j) in itertools.product([0, 1, 2], [0, 1]):
        x = G[k, j, :, :].squeeze()
        #max_value = numpy.abs(G[k, ...]).max()
        n = r.data('G%d%d' % (k, j), x).display('posneg')
        f.sub(n, 'G %s %s' % (cmd[k], grad[j]))

    P = d.P
    f = r.figure('The covariance of gradient', cols=2)
    for (i, j) in itertools.product([0, 1], [0, 1]):
        x = P[i, j, :, :].squeeze()
        if i == j: x = scale_score(x)
        display = "scale" if i == j else "posneg"
        n = r.data('cov%d%d' % (i, j), x).display(display)
        f.sub(n, 'cov %s %s' % (grad[i], grad[j]))
    
    f = r.figure('The inverse of the covariance', cols=2)
    P_inv = d.P_inv
    #P_inv = skim_top_and_bottom(P_inv, 5)
    for (i, j) in itertools.product([0, 1], [0, 1]):
        x = P_inv[i, j, :, :].squeeze()
        if i == j: x = scale_score(x)
        display = "scale" if i == j else "posneg"
        n = r.data('P_inv%d%d' % (i, j), x).display(display)
        f.sub(n, 'P_inv %s %s' % (grad[i], grad[j]))
        
    Gn = d.Gn
    f = r.figure('Normalized G', cols=2)
    for (k, j) in itertools.product([0, 1, 2], [0, 1]):
        x = Gn[k, j, :, :].squeeze()
        n = r.data('Gn%d%d' % (k, j), x).display('posneg')
        f.sub(n, 'Gn %s %s' % (cmd[k], grad[j]))

    Gnn = d.Gnn
    #max_value = numpy.abs(Gnn).max()
    f = r.figure('Normalized G (also inputs)', cols=2)
    for (k, j) in itertools.product([0, 1, 2], [0, 1]):
        x = Gnn[k, j, :, :].squeeze()
        max_value = numpy.abs(Gnn[k, ...]).max()
        #max_value = numpy.abs(x).max()
        n = r.data('Gnn%d%d' % (k, j), x).display('posneg', max_value=max_value)
        f.sub(n, 'Gnn %s %s' % (cmd[k], grad[j]))


    plot_hist_for_4d_tensor(r, G, 'G', 'Histograms for G')
    plot_hist_for_4d_tensor(r, P, 'P', 'Histograms for P (covariance)')
    
    return r
def ground_truth_plots(d):
    r = Report()
    f = r.figure(cols=3)

    n = r.data('cosine', d.C).display('posneg')  
    f.sub(n, 'Cosine matrix')
    
    n = r.data('dist', d.D).display('scale')  
    f.sub(n, 'Distance matrix')
    
    return r
Example #15
0
def show_some_correlations(d, num=30, cols=6):
    r = Report('sensels correlations')
    f = r.figure('Correlations of some sensels.', cols=cols)
    
    s = d.R.sum(axis=0)
    r.data('sum', d.toimg(s)).display('posneg')
    f.sub('sum', caption="Sum of correlations")
    
    for i in range(num):
        id = 'sensel%d' % i
        Ri = d.toimg(d.R[i, :])
        r.data(id, Ri).display('posneg')
        f.sub(id)
    return r
Example #16
0
def report_uncert_stats(records, id_ddss):

    #    records = stats['records']

    r = Report('uncert-statsall')
    r.data('records', records)
    f = r.figure()

    id_distances = ['L2', 'L2w']

    colors = list(islice(cycle(['r', 'g', 'b', 'k', 'y', 'm']), 50))
    perc = 10
    W = 0.2

    with f.plot('distance', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)
        for i, id_dds in enumerate(id_ddss):
            which = records['id_discdds'] == id_dds
            delta = records[which]['delta']

            distance = records[which]['L2w']

            if i == 0:
                distance0 = records[which]['L2']
                step = float(0) / max(len(id_distances) - 1, 1)
                xstep = W * 2 * (step - 0.5)
                fancy_error_display(ax,
                                    delta + xstep,
                                    distance0,
                                    colors[0],
                                    perc=perc,
                                    label='L2')

            step = float(i + 1) / max(len(id_distances) - 1, 1)
            xstep = W * 2 * (step - 0.5)
            fancy_error_display(ax,
                                delta + xstep,
                                distance,
                                colors[i + 1],
                                perc=perc,
                                label='L2w' + id_dds)

        legend_put_below(ax)
    return r
Example #17
0
def estimation(fid, f):  # @UnusedVariable
    shape = [50, 50]
    diffeo = diffeomorphism_from_function(shape, f)

    K = 50
    epsilon = 1
    de = DiffeomorphismEstimator([0.2, 0.2], MATCH_CONTINUOUS)
    for y0, y1 in generate_input(shape, K, diffeo, epsilon=epsilon):
        de.update(y0, y1)

    diff2d = de.summarize()
    diffeo_learned = diff2d.d

    from reprep import Report

    name = f.__name__
    r = Report(name)
    fig = r.figure(cols=4)

    diffeo_learned_rgb = diffeomorphism_to_rgb_cont(diffeo_learned)
    diffeo_rgb = diffeomorphism_to_rgb_cont(diffeo)
    r.data_rgb('diffeo_rgb', diffeo_rgb).add_to(fig)
    r.data_rgb('diffeo_learned_rgb', diffeo_learned_rgb).add_to(fig)
    L = r.data('diffeo_learned_uncertainty', diff2d.variance)
    L.display('scale').add_to(fig, caption='uncertainty')
    r.data('last_y0', y0).display('scale').add_to(fig, caption='last y0')
    r.data('last_y1', y1).display('scale').add_to(fig, caption='last y1')

    cs = [(0, 25), (10, 25), (25, 25), (25, 5)]
    for c in cs:
        M25 = de.get_similarity(c)
        r.data('cell-%s-%s' % c,
               M25).display('scale').add_to(fig,
                                            caption='Example similarity field')

    filename = 'out/diffeo_estimation_suite/%s.html' % name
    print('Writing to %r.' % filename)
    r.to_html(filename)
Example #18
0
def report_uncert_stats(records, id_ddss):

#    records = stats['records']

    r = Report('uncert-statsall')
    r.data('records', records)
    f = r.figure()
    
    id_distances = ['L2', 'L2w']
    
    colors = list(islice(cycle(['r', 'g', 'b', 'k', 'y', 'm']), 50))
    perc = 10
    W = 0.2

    with f.plot('distance', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)
        for i, id_dds in enumerate(id_ddss):
            which = records['id_discdds'] == id_dds
            delta = records[which]['delta']
            
            distance = records[which]['L2w']
            
            if i == 0:
                distance0 = records[which]['L2']
                step = float(0) / max(len(id_distances) - 1, 1)
                xstep = W * 2 * (step - 0.5)
                fancy_error_display(ax, delta + xstep, distance0,
                                    colors[0], perc=perc,
                                    label='L2')

            step = float(i + 1) / max(len(id_distances) - 1, 1)
            xstep = W * 2 * (step - 0.5) 
            fancy_error_display(ax, delta + xstep, distance,
                                colors[i + 1], perc=perc, label='L2w' + id_dds)
    
        legend_put_below(ax)
    return r
def estimation(fid, f):  # @UnusedVariable
    shape = [50, 50]
    diffeo = diffeomorphism_from_function(shape, f)

    K = 50
    epsilon = 1
    de = DiffeomorphismEstimator([0.2, 0.2], MATCH_CONTINUOUS)
    for y0, y1 in generate_input(shape, K, diffeo, epsilon=epsilon):
        de.update(y0, y1)

    diff2d = de.summarize()
    diffeo_learned = diff2d.d

    from reprep import Report

    name = f.__name__
    r = Report(name)
    fig = r.figure(cols=4)

    diffeo_learned_rgb = diffeomorphism_to_rgb_cont(diffeo_learned)
    diffeo_rgb = diffeomorphism_to_rgb_cont(diffeo)
    r.data_rgb('diffeo_rgb', diffeo_rgb).add_to(fig)
    r.data_rgb('diffeo_learned_rgb', diffeo_learned_rgb).add_to(fig)
    L = r.data('diffeo_learned_uncertainty', diff2d.variance)
    L.display('scale').add_to(fig, caption='uncertainty')
    r.data('last_y0', y0).display('scale').add_to(fig, caption='last y0')
    r.data('last_y1', y1).display('scale').add_to(fig, caption='last y1')

    cs = [(0, 25), (10, 25), (25, 25), (25, 5)]
    for c in cs:
        M25 = de.get_similarity(c)
        r.data('cell-%s-%s' % c, M25).display('scale').add_to(fig,
                                         caption='Example similarity field')

    filename = 'out/diffeo_estimation_suite/%s.html' % name
    print('Writing to %r.' % filename)
    r.to_html(filename)
Example #20
0
def table_by_rows(id_report, samples, rows_field, cols_fields, source_descs):
    samples2 = StoreResultsDict(samples)

    class Missing(dict):
        def __missing__(self, key):
            logger.warning("Description for %r missing." % key)
            d = WithDescription(name=key, symbol="\\text{%s}" % key, desc=None)
            self[key] = d
            return d

    source_descs = Missing(source_descs)

    r = Report(id_report)
    data_views = [DataView.from_string(x, source_descs) for x in cols_fields]
    # data: list of list of list
    rows_field, data, reduced, display = summarize_data(
        samples2, rows_field, data_views)
    rows = ["$%s$" % source_descs[x].get_symbol() for x in rows_field]
    cols = ["$%s$" % x.get_symbol() for x in data_views]
    r.table("table", data=display, cols=cols, rows=rows)
    r.data("table_data",
           data=reduced,
           caption="Data without presentation applied.")
    r.data("table_data_source",
           data=data,
           caption="Source data, before reduction.")

    row_desc = "\n".join([
        "- $%s$: %s" % (x.get_symbol(), x.get_desc())
        for x in list(map(source_descs.__getitem__, rows_field))
    ])
    col_desc = "\n".join(
        ["- $%s$: %s" % (x.get_symbol(), x.get_desc()) for x in data_views])
    r.text("row_desc", rst_escape_slash(row_desc), mime=MIME_RST)
    r.text("col_desc", rst_escape_slash(col_desc), mime=MIME_RST)
    return r
Example #21
0
def correlation_embedding_report(R, num_eig=6):
    imshape = (100, 100)
    toimg = lambda x : x.reshape(imshape)
    
    U, S, V = numpy.linalg.svd(R, full_matrices=0) #@UnusedVariable
    
    r = Report('correlation_embedding')
    fv = r.figure('V', caption='Coordinates of the embedding')
    
    for k in range(num_eig):
        v = V[k, :] * numpy.sqrt(S[k])
        print v.size
        id = 'eig_v_%d' % k
        n = r.data(id, toimg(v)).display('posneg')
        fv.sub(n, caption='Eigenvector #%d' % k)
    
    return r
Example #22
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)
Example #23
0
def report_predstats(id_discdds, id_subset, id_distances, records):
    r = Report('predistats-%s-%s' % (id_discdds, id_subset))

    r.data('records', records)
    f = r.figure()

    colors = list(islice(cycle(['r', 'g', 'b', 'k', 'y', 'm']), 50))
    delta = records['delta']
    W = 0.2

    # Save the raw values
    for i, id_d in enumerate(id_distances):
        r.data(id_d, records[id_d])

    with f.plot('values_order', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)

        for i, id_d in enumerate(id_distances):
            distance = records[id_d]
            distance_order = scale_score(distance) / (float(distance.size) - 1)

            step = float(i) / max(len(id_distances) - 1, 1)
            xstep = W * 2 * (step - 0.5)
            fancy_error_display(ax,
                                delta + xstep,
                                distance_order,
                                colors[i],
                                perc=10,
                                label=id_d)

        ieee_spines(pylab)
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('interval length')
        pylab.ylabel('normalized distance')
        pylab.xticks(ticks, ticks)
        pylab.yticks((0, 1), (0, 1))
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, 1.2))
        legend_put_below(ax)

    with f.plot('values', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)

        for i, id_d in enumerate(id_distances):
            distance = records[id_d]

            step = float(i) / max(len(id_distances) - 1, 1)
            xstep = W * 2 * (step - 0.5)
            fancy_error_display(ax,
                                delta + xstep,
                                distance,
                                colors[i],
                                perc=10,
                                label=id_d)

        ieee_spines(pylab)
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('interval length')
        pylab.ylabel('distance')
        pylab.xticks(ticks, ticks)
        #        pylab.yticks((0, 1), (0, 1))
        a = pylab.axis()
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, a[3]))
        legend_put_below(ax)

    return r
Example #24
0
def report_stats(records, id_ddss, id_streams, id_distances):
    r = Report('precision-stats')
    r.data('records', records)

    colors = list(islice(cycle(['r', 'g', 'b', 'k', 'y', 'm']), 50))
    perc = 10
    W = 0.2

    for i, id_dds in enumerate(id_ddss):
        r.text('dds%s' % i, id_dds)

    streams_sets = generate_stream_sets(id_streams)
    for stream_set, id_distance in itertools.product(streams_sets,
                                                     id_distances):
        f = r.figure(cols=2)
        with f.plot('distance_legend',
                    caption='Streams: %s, Distance: %s' %
                    (stream_set['label'], id_distance),
                    **dp_predstats_fig) as pylab:
            ax = pylab.subplot(111)
            for i, id_dds in enumerate(id_ddss):
                which = (records['id_discdds'] == id_dds).astype('int')
                for id_stream in stream_set['id_streams']:
                    which += (records['id_stream'] == id_stream).astype('int')
                which = (which /
                         (len(stream_set['id_streams']) + 1)).astype('bool')

                delta = records[which]['delta']

                distance = records[which][id_distance]

                step = float(i) / max(len(id_ddss) - 1, 1)
                xstep = W * 2 * (step - 0.5)
                fancy_error_display(ax,
                                    delta + xstep,
                                    distance,
                                    colors[i],
                                    perc=perc,
                                    label='%s' % i)

        with f.plot('distance',
                    caption='treams: %s, Distance: %s' %
                    (stream_set['label'], id_distance),
                    **dp_predstats_fig) as pylab:
            ax = pylab.subplot(111)
            for i, id_dds in enumerate(id_ddss):
                which = (records['id_discdds'] == id_dds).astype('int')
                for id_stream in stream_set['id_streams']:
                    which += (records['id_stream'] == id_stream).astype('int')
                which = (which /
                         (len(stream_set['id_streams']) + 1)).astype('bool')

                delta = records[which]['delta']

                distance = records[which][id_distance]

                step = float(i) / max(len(id_ddss) - 1, 1)
                xstep = W * 2 * (step - 0.5)
                fancy_error_display(ax,
                                    delta + xstep,
                                    distance,
                                    colors[i],
                                    perc=perc,
                                    label='%s' % i)

            legend_put_below(ax)

        with f.plot(
                'difference',
                caption='Difference from learner_0, SStreams: %s, Distance: %s'
                % (stream_set['label'], id_distance),
                **dp_predstats_fig) as pylab:
            ax = pylab.subplot(111)

            which0 = (records['id_discdds'] == id_ddss[0])
            _ = records[which0]['delta']  # delta0
            distance0 = records[which0][id_distance]
            for i, id_dds in enumerate(id_ddss[1:]):
                which = (records['id_discdds'] == id_dds).astype('int')
                for id_stream in stream_set['id_streams']:
                    which += (records['id_stream'] == id_stream).astype('int')
                which = (which /
                         (len(stream_set['id_streams']) + 1)).astype('bool')

                delta = records[which]['delta']

                distance = records[which][id_distance]
                difference = distance0 - distance

                step = float(i) / max(len(id_ddss) - 1, 1)
                xstep = W * 2 * (step - 0.5)
                fancy_error_display(ax,
                                    delta + xstep,
                                    difference,
                                    colors[i + 1],
                                    perc=perc,
                                    label='%s' % i)
    return r
Example #25
0
def old_analysis(data):
    R = data['correlation']
    variance = data['variance']
    num_sensels = max(R.shape)
    # XXX
    imshape = (100, 100)
    num_coords_keep = 10
    
    num_sensels_display = 100
    
    r = Report('calibrator_plots')
    f0 = r.figure(cols=5, caption='Main quantities')
    f1 = r.figure(cols=6)
    f2 = r.figure(cols=6)
    f3 = r.figure(cols=6, caption='distances in sensing space')
    f4 = r.figure(cols=6, caption='dependency between eigenvectors')
    
    f0.sub(r.data('variance', variance.reshape(imshape)).display('scale', min_value=0),
           caption='Variance (darker=stronger)')

    
    with r.data_pylab('variance_scalar') as pylab:
        pylab.hist(variance)
    f0.sub('variance_scalar')
    
    for i in range(num_sensels_display):
        id = 'sensel%d' % i
        Ri = R[i, :].reshape(imshape)
        r.data(id, Ri)
        f1.sub(id, display='posneg')
    
    U, S, V = numpy.linalg.svd(R, full_matrices=0) #@UnusedVariable
     
    
    
    coords = numpy.zeros(shape=(num_sensels, num_coords_keep))
    # set coordinates
    for k in range(num_coords_keep):
        v = V[k, :] * numpy.sqrt(S[k])
        coords[:, k] = v
        
    # normalize coords
    if False:
        for i in range(num_sensels):
            coords[i, :] = coords[i, :] / numpy.linalg.norm(coords)
            
    for k in range(num_coords_keep):
        id = 'coord%d' % k
        M = coords[:, k].reshape(imshape)
        r.data(id, M)
        f2.sub(id, display='posneg')
    
    # compute the distance on the sphere for some sensel

    for w in  RandomExtract.choose_selection(30, num_sensels):
        D = numpy.zeros(num_sensels)
        s = 14 # number of coordinates
        p1 = coords[w, 0:s] / numpy.linalg.norm(coords[w, 0:s])
        for i in range(num_sensels):
            p2 = coords[i, 0:s] / numpy.linalg.norm(coords[i, 0:s])
            D[i] = numpy.linalg.norm(p1 - p2)
            
        D_sorted = numpy.argsort(D)
        neighbors = 50
        D[D_sorted[:neighbors]] = 0
        D[D_sorted[neighbors:]] = 1
        # D= D_sorted
        id = 'dist%s' % w
        r.data(id, D.reshape(imshape))
        f3.sub(id, display='scale')
    
    # Divide the sensels in classes
    if False:
        ncoords_classes = 5
        classes = numpy.zeros((num_sensels))
        for k in range(ncoords_classes):
            c = coords[:, k]
            cs = divide_in_classes(c, 3) 
            classes += cs * (3 ** k)
        
        f0.sub(r.data('classes', classes.reshape(imshape)).display('posneg'))
    if True:
        nclasses = 20
        classes = group_by_correlation(R[:nclasses, :])
        f0.sub(r.data('classes_by_R', classes.reshape(imshape)).display('scale'))
        
    if False:
        ncoords = 10
        print("computing similarity matrix")
        coord_similarity = numpy.zeros((ncoords, ncoords))
        for k1, k2 in itertools.product(range(ncoords), range(ncoords)):
            if k1 == k2:
                coord_similarity[k1, k2] = 0 # numpy.nan
                continue
            if k1 < k2:
                continue
            
            c1 = coords[:, k1]
            c2 = coords[:, k2]
            step = 4
            c1 = c1[::step]
            c2 = c2[::step]
            tau, prob = fast_kendall_tau(c1, c2)
            
            coord_similarity[k1, k2] = numpy.abs(tau)
            coord_similarity[k2, k1] = coord_similarity[k1, k2]
        
            print('%r %r : tau = %.4f  prob = %.4f' % (k1, k2, tau, prob))
        print coord_similarity.__repr__()
        n = r.data('coord_similarity', coord_similarity).display('posneg')
        f4.sub(n)
            
        with r.data_pylab('sim_score') as pylab:
            pylab.plot(coord_similarity.sum(axis=0), 'x-')
            pylab.xlabel('coordinate')
            pylab.ylabel('total similarity')
        f4.sub('sim_score')
        
        with r.data_pylab('comp0') as pylab:
            pylab.plot(coord_similarity[0, :], 'x-')
            pylab.xlabel('coordinate')
            pylab.ylabel('similarity with #0')
        f4.sub('comp0')
        
        ref = 0
        for k in range(10):
            print "comparison", k
            id = 'cmp_%d_%d' % (ref, k)
            c0 = coords[:, ref]
            ck = coords[:, k]
            with r.data_pylab(id) as pylab:
                pylab.plot(c0, ck, '.', markersize=0.3)
    
            f4.sub(id)
    return r
Example #26
0
    logpd = np.log(pd)
    pd[zeros] = 0

    return -(pd * logpd).sum()


if __name__ == '__main__':
    filename = sys.argv[1]
    data = pickle.load(open(filename, 'rb'))
    h, hdist = compute_hdist(data['single'], data['joint'])

    pickle.dump(hdist, open('hdist.pickle', 'wb'))

    hdist = np.cos(hdist * np.pi)
    e = -np.eye(hdist.shape[0]) + 1
    hdist = hdist * e

    from reprep import Report
    r = Report()
    f = r.figure()
    r.data('hdist', hdist).display('scale').add_to(f)
    with r.data_pylab('h')as pylab:
        pylab.plot(h)
    r.last().add_to(f)
    filename = 'real_test_cases.html'
    print('Writing to %r.' % filename)
    r.to_html(filename)



Example #27
0
def report_statistics_all(id_sub, stats, perc=10, W=0.2):
    records = stats['records']

    r = Report('statsall-%s' % id_sub)
    r.data('records', records)
    f = r.figure()
    
    id_distances = sorted(set(records['id_distance']))
        
    logger.info('%s: %s %s reo %s' % (id_sub, len(stats), id_distances,
                                      len(records)))

    colors = list(islice(cycle(['r', 'g', 'b', 'k', 'y', 'm']), 50))
    

    with f.plot('distance_order', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)
        for i, id_d in enumerate(id_distances):
            which = records['id_distance'] == id_d
            delta = records[which]['delta']
            distance = records[which]['distance']
            order = scale_score(distance)
            order = order / float(order.size)

            
            step = float(i) / (max(len(id_distances) - 1, 1))
            xstep = W * 2 * (step - 0.5) 
            fancy_error_display(ax, delta + xstep, order,
                                colors[i], perc=perc, label=id_d)
            
        ieee_spines(pylab)    
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('plan length')
        pylab.ylabel('normalized distance')
        pylab.xticks(ticks, ticks)
        pylab.yticks((0, 1), (0, 1))
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, 1.2))
        legend_put_below(ax)

    with f.plot('distance', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)
        for i, id_d in enumerate(id_distances):
            which = records['id_distance'] == id_d
            delta = records[which]['delta']
            distance = records[which]['distance']

            step = float(i) / max(len(id_distances) - 1, 1)
            xstep = W * 2 * (step - 0.5) 
            fancy_error_display(ax, delta + xstep, distance,
                                colors[i], perc=perc, label=id_d)
            
        ieee_spines(pylab)    
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('plan length')
        pylab.ylabel('distance')
        pylab.xticks(ticks, ticks)
#        pylab.yticks((0, 1), (0, 1))
        a = pylab.axis()
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, a[3]))
        legend_put_below(ax)

    return r
Example #28
0
for [S, _, name] in ovars:
    # Calculate mean value from sum
    Sn = S[2:-2, 2:-2] / n
    
    # Estimate parameters and plot analytic prob function
#    gkde = stats.gaussian_kde(Sn.flatten())

#    S_clip = np.zeros(Sn.shape)
#    S_clip[Sn < vmax] = Sn[Sn < vmax]
#    S_clip[Sn > vmin] = Sn[Sn > vmin]
#    pdb.set_trace()
    mu = np.mean(Sn)
    sigma = np.sqrt(np.std(Sn))
    f_mu = np.mean(np.abs(Sn))
    f_sigma = np.sqrt(np.std(np.abs(Sn)))
    report.data(name + 'mu', mu)
    report.data(name + 'sigma', sigma)
    report.data(name + 'folded_mu', f_mu)
    report.data(name + 'folded_sigma', f_sigma)
    def pdf(T):
        if len(T) is None:
            return 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-.5 * ((t - mu) / sigma) ** 2)
        else:
            return np.array([1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-.5 * ((t - mu) / sigma) ** 2) for t in T]) 
    
    x = np.linspace(0, vmax, 200)
#    pdb.set_trace()
    
#    Sn = np.abs(Sn)
    
    f = report.figure(cols=3)
def plot_results(label, results):
    iterations = results['iterations']
    r = Report(label)
    
    R = results['R']
    gt_C = results['gt_C']
    f = r.figure(cols=3, caption='Ground truth')
    with r.data_pylab('r_vs_c') as pylab:
        pylab.plot(gt_C.flat, R.flat, '.', markersize=0.2)
        pylab.xlabel('real cosine')
        pylab.ylabel('correlation measure')
        pylab.axis((-1, 1, -1, 1))
    f.sub('r_vs_c', 'Unknown function correlation -> cosine')

    r.data('gt_C', gt_C).display('posneg', max_value=1).add_to(f, 'ground truth cosine matrix')
    
    dist = numpy.real(numpy.arccos(gt_C))
    r.data('gt_dist', dist).display('scale').add_to(f, 'ground truth distance matrix')
    
    f = r.figure(cols=12)
    
    R = results['R']
    R_order = results['R_order']
    
    for i, it in enumerate(iterations):
        singular_values = it['singular_values']
        coords = it['coords']
        coords_proj = it['coords_proj']
        estimated_C = it['estimated_C']
        estimated_C_order = it['estimated_C_order']
        
        check('array[MxN],(M=2|M=3)', coords)
        
        rit = r.node('iteration%2d' % i)
        
        rit.data('Cest', it['Cest']).display('posneg', max_value=1).add_to(f, 'Cest')
        rit.data('dont_trust', it['dont_trust'] * 1.0).display('scale').add_to(f, 'trust')
        rit.data('Cestn', it['Cestn']).display('posneg', max_value=1).add_to(f, 'Cestn')
        dist = numpy.real(numpy.arccos(it['Cestn']))
        rit.data('dist', dist).display('scale', max_value=numpy.pi).add_to(f, 'corresponding distance')
        distp = propagate(dist)
        rit.data('distp', distp).display('scale', max_value=numpy.pi).add_to(f, 'propagated distance')
        
        n = rit.data('singular_values', singular_values)
        with n.data_pylab('plot') as pylab:
            s = singular_values 
            s = s / s[0]
            pylab.plot(s[:15], 'x-')
        f.sub(n, 'Singular values')
        
        n = rit.data('coords', coords)
        with n.data_pylab('plot') as pylab:
            pylab.plot(coords[0, :], coords[1, :], '.')
            pylab.axis('equal')
        f.sub(n, 'Coordinates')

        n = rit.data('coords_proj', coords)
        with n.data_pylab('plot') as pylab:
            pylab.plot(coords_proj[0, :], coords_proj[1, :], '.')
            pylab.axis((-1, 1, -1, 1))
        f.sub(n, 'Coordinates (projected)')
        
        with n.data_pylab('r_vs_est_c') as pylab:
            pylab.plot(estimated_C.flat, R.flat, '.', markersize=0.2)
            pylab.ylabel('estimated cosine')
            pylab.xlabel('correlation measure')
            pylab.axis((-1, 1, -1, 1))
        f.sub('r_vs_est_c', 'R vs estimated C')
            
        with n.data_pylab('order_order') as pylab:
            pylab.plot(estimated_C_order.flat, R_order.flat, '.', markersize=0.2)
            pylab.ylabel('est C order')
            pylab.xlabel('R order')
        f.sub('order_order')
        
        
        # XXX: if mistake: add_child, nothing happens
        rit.data('estimated_C', estimated_C).display('posneg').add_to(f, 'estimated_C') 
        
        rit.data('Cest_new', it['Cest_new']).display('posneg', max_value=1).add_to(f, 'Cest_new')
        
    return r
Example #30
0
for [S, _, name] in ovars:
    # Calculate mean value from sum
    Sn = S[2:-2, 2:-2] / n

    # Estimate parameters and plot analytic prob function
    #    gkde = stats.gaussian_kde(Sn.flatten())

    #    S_clip = np.zeros(Sn.shape)
    #    S_clip[Sn < vmax] = Sn[Sn < vmax]
    #    S_clip[Sn > vmin] = Sn[Sn > vmin]
    #    pdb.set_trace()
    mu = np.mean(Sn)
    sigma = np.sqrt(np.std(Sn))
    f_mu = np.mean(np.abs(Sn))
    f_sigma = np.sqrt(np.std(np.abs(Sn)))
    report.data(name + 'mu', mu)
    report.data(name + 'sigma', sigma)
    report.data(name + 'folded_mu', f_mu)
    report.data(name + 'folded_sigma', f_sigma)

    def pdf(T):
        if len(T) is None:
            return 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-.5 * (
                (t - mu) / sigma)**2)
        else:
            return np.array([
                1 / (sigma * np.sqrt(2 * np.pi)) *
                np.exp(-.5 * ((t - mu) / sigma)**2) for t in T
            ])

    x = np.linspace(0, vmax, 200)
Example #31
0
    pd = f.flatten().astype('float64') / s
    zeros, = np.nonzero(pd == 0)
    pd[zeros] = 1
    logpd = np.log(pd)
    pd[zeros] = 0

    return -(pd * logpd).sum()


if __name__ == '__main__':
    filename = sys.argv[1]
    data = pickle.load(open(filename, 'rb'))
    h, hdist = compute_hdist(data['single'], data['joint'])

    pickle.dump(hdist, open('hdist.pickle', 'wb'))

    hdist = np.cos(hdist * np.pi)
    e = -np.eye(hdist.shape[0]) + 1
    hdist = hdist * e

    from reprep import Report
    r = Report()
    f = r.figure()
    r.data('hdist', hdist).display('scale').add_to(f)
    with r.data_pylab('h') as pylab:
        pylab.plot(h)
    r.last().add_to(f)
    filename = 'real_test_cases.html'
    print('Writing to %r.' % filename)
    r.to_html(filename)
Example #32
0
def report_stats(records, id_ddss, id_streams, id_distances):
    r = Report('precision-stats')
    r.data('records', records)
    
    colors = list(islice(cycle(['r', 'g', 'b', 'k', 'y', 'm']), 50))
    perc = 10
    W = 0.2
    
    for i, id_dds in enumerate(id_ddss):
        r.text('dds%s' % i, id_dds)
    
    
    streams_sets = generate_stream_sets(id_streams)
    for stream_set, id_distance in itertools.product(streams_sets, id_distances):
        f = r.figure(cols=2)
        with f.plot('distance_legend', caption='Streams: %s, Distance: %s' 
                    % (stream_set['label'], id_distance),
                    **dp_predstats_fig) as pylab:
            ax = pylab.subplot(111)
            for i, id_dds in enumerate(id_ddss):
                which = (records['id_discdds'] == id_dds).astype('int')
                for id_stream in stream_set['id_streams']:
                    which += (records['id_stream'] == id_stream).astype('int')
                which = (which / (len(stream_set['id_streams']) + 1)).astype('bool')
                
                delta = records[which]['delta']
                
                distance = records[which][id_distance]
                
                step = float(i) / max(len(id_ddss) - 1, 1)
                xstep = W * 2 * (step - 0.5) 
                fancy_error_display(ax, delta + xstep, distance,
                                    colors[i], perc=perc, label='%s' % i)
            
        with f.plot('distance', caption='treams: %s, Distance: %s' 
                    % (stream_set['label'], id_distance),
                    **dp_predstats_fig) as pylab:
            ax = pylab.subplot(111)
            for i, id_dds in enumerate(id_ddss):
                which = (records['id_discdds'] == id_dds).astype('int')
                for id_stream in stream_set['id_streams']:
                    which += (records['id_stream'] == id_stream).astype('int')
                which = (which / (len(stream_set['id_streams']) + 1)).astype('bool')
                
                delta = records[which]['delta']
                
                distance = records[which][id_distance]
                
                step = float(i) / max(len(id_ddss) - 1, 1)
                xstep = W * 2 * (step - 0.5) 
                fancy_error_display(ax, delta + xstep, distance,
                                    colors[i], perc=perc, label='%s' % i)
            
            legend_put_below(ax)
            
        
        with f.plot('difference', caption='Difference from learner_0, SStreams: %s, Distance: %s' 
                    % (stream_set['label'], id_distance),
                    **dp_predstats_fig) as pylab:
            ax = pylab.subplot(111)
            
            which0 = (records['id_discdds'] == id_ddss[0])
            _ = records[which0]['delta']  # delta0
            distance0 = records[which0][id_distance]
            for i, id_dds in enumerate(id_ddss[1:]):
                which = (records['id_discdds'] == id_dds).astype('int')
                for id_stream in stream_set['id_streams']:
                    which += (records['id_stream'] == id_stream).astype('int')
                which = (which / (len(stream_set['id_streams']) + 1)).astype('bool')
                
                delta = records[which]['delta']
                
                distance = records[which][id_distance]
                difference = distance0 - distance
                
                step = float(i) / max(len(id_ddss) - 1, 1)
                xstep = W * 2 * (step - 0.5) 
                fancy_error_display(ax, delta + xstep, difference,
                                    colors[i + 1], perc=perc, label='%s' % i)
    return r
Example #33
0
def report_statistics_all(id_sub, stats, perc=10, W=0.2):
    records = stats['records']

    r = Report('statsall-%s' % id_sub)
    r.data('records', records)
    f = r.figure()

    id_distances = sorted(set(records['id_distance']))

    logger.info('%s: %s %s reo %s' %
                (id_sub, len(stats), id_distances, len(records)))

    colors = list(islice(cycle(['r', 'g', 'b', 'k', 'y', 'm']), 50))

    with f.plot('distance_order', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)
        for i, id_d in enumerate(id_distances):
            which = records['id_distance'] == id_d
            delta = records[which]['delta']
            distance = records[which]['distance']
            order = scale_score(distance)
            order = order / float(order.size)

            step = float(i) / (max(len(id_distances) - 1, 1))
            xstep = W * 2 * (step - 0.5)
            fancy_error_display(ax,
                                delta + xstep,
                                order,
                                colors[i],
                                perc=perc,
                                label=id_d)

        ieee_spines(pylab)
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('plan length')
        pylab.ylabel('normalized distance')
        pylab.xticks(ticks, ticks)
        pylab.yticks((0, 1), (0, 1))
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, 1.2))
        legend_put_below(ax)

    with f.plot('distance', **dp_predstats_fig) as pylab:
        ax = pylab.subplot(111)
        for i, id_d in enumerate(id_distances):
            which = records['id_distance'] == id_d
            delta = records[which]['delta']
            distance = records[which]['distance']

            step = float(i) / max(len(id_distances) - 1, 1)
            xstep = W * 2 * (step - 0.5)
            fancy_error_display(ax,
                                delta + xstep,
                                distance,
                                colors[i],
                                perc=perc,
                                label=id_d)

        ieee_spines(pylab)
        ticks = sorted(list(set(list(delta))))
        pylab.xlabel('plan length')
        pylab.ylabel('distance')
        pylab.xticks(ticks, ticks)
        #        pylab.yticks((0, 1), (0, 1))
        a = pylab.axis()
        pylab.axis((0.5, 0.5 + np.max(delta), -0.024, a[3]))
        legend_put_below(ax)

    return r
Example #34
0
def new_analysis(data, nclasses=100):
    R = data['correlation']
    num_ref, num_sensels = R.shape
    assert num_ref <= num_sensels
    
    # variance = data['variance']
    
     
    classes = group_by_correlation(R[:nclasses, :])
    
    nclasses = classes.max() + 1
    
    assert (classes >= 0).all()
    assert (classes < nclasses).all()
     
    print("Computing class correlation...")
    classes_correlation = numpy.zeros((nclasses, nclasses))
    for c1, c2 in itertools.product(range(nclasses), range(nclasses)):
        if c1 < c2:
            continue
        
        # If I had the full matrix
        #sensels1, = numpy.nonzero(classes == c1)
        #sensels2, = numpy.nonzero(classes == c2)
        #average_correlation = R[sensels1,sensels2].mean()
        
        corr1 = R[c1, :]
        corr2 = R[c2, :]
        assert len(corr1) == len(corr2) == num_sensels 
        
        # similarity = (corr1 * corr2).mean() 
        # similarity = (corr1 * corr2).mean() / (corr1.mean() * corr2.mean())
        
        corr1 = corr1 / numpy.linalg.norm(corr1)
        corr2 = corr2 / numpy.linalg.norm(corr2)
        similarity = numpy.linalg.norm(corr1 - corr2)
        
        classes_correlation[c1, c2] = similarity
        classes_correlation[c2, c1] = similarity
    
    print("Computing class correlation...")
    U, S, V = numpy.linalg.svd(classes_correlation, full_matrices=0)
    
    # FIXME: U or V?
    
    # XXX: should I use this?
    classes_coords = U / numpy.sqrt(S)
    
    
    print("Creating report...")
    r = Report('new_analysis')
    f0 = r.figure(caption='Summary')
    imshape = (100, 100) # XXX
    toimg = lambda x : x.reshape(imshape)
    
    f0.sub(r.data('supers', toimg(classes)).display('scale'))

    f0.sub(r.data('classes_correlation', classes_correlation).display('scale'))

    f = r.figure('eigen_v', cols=3)
    for k in range(6):
        coord = V[k, :] / numpy.sqrt(S)
        assert coord.size == nclasses
        coordi = coord[classes]
        assert coordi.size == num_sensels
        n = r.data('coord%d' % k, toimg(coordi)).display('posneg')
        f.sub(n)
    
    f = r.figure('eigen_u', cols=3)
    for k in range(6):
        coord = U[:, k] / numpy.sqrt(S)
        assert coord.size == nclasses
        coordi = coord[classes]
        assert coordi.size == num_sensels
        n = r.data('coordu%d' % k, toimg(coordi)).display('posneg')
        f.sub(n)
    
    return r