예제 #1
0
 def testFigures(self):
     rgb = numpy.zeros((4, 4, 3), "uint8")
     r = Report("test")
     f = r.figure()
     f.data_rgb("rgb", rgb, caption="ciao")
     assert len(f.get_subfigures()) == 1
     r.data_rgb("rgb", rgb, caption="ciao2")
     r.last().add_to(f)
     assert len(f.get_subfigures()) == 2
예제 #2
0
 def testFigures(self):
     rgb = numpy.zeros((4, 4, 3), 'uint8')
     r = Report('test')
     f = r.figure()
     f.data_rgb('rgb', rgb, caption='ciao')
     assert len(f.get_subfigures()) == 1
     r.data_rgb('rgb', rgb, caption='ciao2')
     r.last().add_to(f)
     assert len(f.get_subfigures()) == 2
예제 #3
0
파일: kernels.py 프로젝트: afcarl/cbc
def main():
    N = 100
    num_svds = 8

    radius_deg = 180

    kernels = [identity, linear01_sat, pow3_sat, pow7_sat]
    #    kernels = [linear01_sat, pow3_sat, pow7_sat]

    r = Report('eig analysis')
    #    warps_desc = ", ".join(['%.2f' % x for x in warps])
    caption = """ This figure shows that on S^1 things can be warped easily.
    The initial distribution of {N} points, with radius {radius_deg}.
    """.format(**locals())

    f = r.figure(caption=caption)
    mime = 'application/pdf'
    figsize = (4, 3)
    with r.data_pylab('kernels', mime=mime, figsize=figsize) as pylab:

        for kernel in kernels:
            x = np.linspace(-1, +1, 256)
            y = kernel(x)
            pylab.plot(x, y, label=kernel.__name__)
        pylab.axis([-1, 1, -1, 1])
        pylab.xlabel('Cosine between orientations')
        pylab.ylabel('Correlation')
        pylab.legend(loc='lower right')

    r.last().add_to(f, caption='Correlation kernels')

    for ndim in [2, 3]:
        S = get_distribution(ndim, N, radius_deg)
        C = cosines_from_directions(S)
        D = distances_from_directions(S)
        assert np.degrees(D.max()) <= 2 * radius_deg

        with r.data_pylab('svds%d' % ndim, mime=mime,
                          figsize=figsize) as pylab:
            for kernel in kernels:
                Cw = kernel(C)
                # TODO:
                # Cw = cos(kernel(D))
                s = svds(Cw, num_svds)
                pylab.semilogy(s, 'x-', label=kernel.__name__)
            pylab.legend(loc='center right')
        r.last().add_to(
            f, caption='Singular value for different kernels (ndim=%d)' % ndim)

    filename = 'cbc_demos/kernels.html'
    print("Writing to %r." % filename)
    r.to_html(filename)
예제 #4
0
파일: kernels.py 프로젝트: AndreaCensi/cbc
def main():
    N = 100
    num_svds = 8

    radius_deg = 180

    kernels = [identity, linear01_sat, pow3_sat, pow7_sat]
#    kernels = [linear01_sat, pow3_sat, pow7_sat]

    r = Report('eig analysis')
#    warps_desc = ", ".join(['%.2f' % x for x in warps])
    caption = """ This figure shows that on S^1 things can be warped easily.
    The initial distribution of {N} points, with radius {radius_deg}.
    """.format(**locals())

    f = r.figure(caption=caption)
    mime = 'application/pdf'
    figsize = (4, 3)
    with r.data_pylab('kernels', mime=mime, figsize=figsize) as pylab:

        for kernel in kernels:
            x = np.linspace(-1, +1, 256)
            y = kernel(x)
            pylab.plot(x, y, label=kernel.__name__)
        pylab.axis([-1, 1, -1, 1])
        pylab.xlabel('Cosine between orientations')
        pylab.ylabel('Correlation')
        pylab.legend(loc='lower right')

    r.last().add_to(f, caption='Correlation kernels')

    for ndim in [2, 3]:
        S = get_distribution(ndim, N, radius_deg)
        C = cosines_from_directions(S)
        D = distances_from_directions(S)
        assert np.degrees(D.max()) <= 2 * radius_deg

        with r.data_pylab('svds%d' % ndim,
                          mime=mime, figsize=figsize) as pylab:
            for kernel in kernels:
                Cw = kernel(C)
                # TODO: 
                # Cw = cos(kernel(D))
                s = svds(Cw, num_svds)
                pylab.semilogy(s, 'x-', label=kernel.__name__)
            pylab.legend(loc='center right')
        r.last().add_to(f,
            caption='Singular value for different kernels (ndim=%d)' % ndim)

    filename = 'cbc_demos/kernels.html'
    print("Writing to %r." % filename)
    r.to_html(filename)
def create_report_axis_angle(id, desc, saccades):
    r = Report('axis_angle')
        # 
        # axis_angle = saccades['axis_angle']
        # saccade_angle = saccades['saccade_angle']
                      
    stats = statistics_distance_axis_angle(saccades,
        num_distance_intervals=10,
        axis_angle_bin_interval=10,
        axis_angle_bin_size=10
    )
               
    f = r.figure(cols=1)
    
    for i, section in enumerate(stats['distance_sections']):
        distance_min = section['distance_min']
        distance_max = section['distance_max']
        prob_left = section['prob_left']
        prob_right = section['prob_right']
        margin_left = section['margin_left']
        margin_right = section['margin_right']
        bin_centers = section['bin_centers']
        num_saccades = section['num_saccades']
        n = len(bin_centers)
        
        with r.data_pylab('section%d' % i) as pylab:
            el = np.zeros((2, n))
            el[0, :] = +(margin_left[0, :] - prob_left)
            el[1, :] = -(margin_left[1, :] - prob_left)
            pylab.errorbar(bin_centers, prob_left, el, None, None,
                           ecolor='g', label='left', capsize=8, elinewidth=1)
            er = np.zeros((2, n))
            er[0, :] = +(margin_right[0, :] - prob_right)
            er[1, :] = -(margin_right[1, :] - prob_right)
            pylab.errorbar(bin_centers, prob_right, er, None, None,
                           ecolor='r', label='right', capsize=8, elinewidth=1)
    
            pylab.plot(bin_centers, prob_left, 'g-', label='left')
            pylab.plot(bin_centers, prob_right, 'r-', label='right')
            pylab.xlabel('axis angle (deg)')
            pylab.ylabel('probability of turning')
            pylab.title('Direction probability for distance in [%dcm,%dcm], %d saccades' % 
                        (distance_min * 100, distance_max * 100, num_saccades))
            pylab.plot([0, 0], [0, 1], 'k-')
            pylab.axis([-180, 180, 0, 1])
            pylab.legend()
        r.last().add_to(f)
            
    return r
예제 #6
0
파일: real.py 프로젝트: AndreaCensi/cbc
    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)



예제 #7
0
def main():

    def spearman(a, b):
        ao = scale_score(a)
        bo = scale_score(b)
        return correlation_coefficient(ao, bo)

    disable_all()

    def seq():
        N = 180
        iterations = 10
        nradii = 100
        radii = np.linspace(5, 180, nradii)

        K = 1
        for radius_deg, i in itertools.product(radii, range(K)):
            print radius_deg, i
            # Generate a random symmetric matrix
            # x = np.random.rand(N, N)
            S = random_directions_bounded(3, np.radians(radius_deg), N)
            C = np.dot(S.T, S)
            alpha = 1
            f = lambda x: np.exp(-alpha * (1 - x))
            # f = lambda x : x
            R = f(C)
            # Normalize in [0,1]
            R1 = (R - R.min()) / (R.max() - R.min())
            # Normalize in [-1,1]
            R2 = (R1 - 0.5) * 2

            S1 = simplified_algo(R1, iterations)
            S1w = simplified_algo(R1, iterations, warp=50)
            S2 = simplified_algo(R2, iterations)

            s1 = spearman(cosines_from_directions(S1), R1)
            s1w = spearman(cosines_from_directions(S1w), R1)
            s2 = spearman(cosines_from_directions(S2), R2)

            e1 = np.degrees(overlap_error_after_orthogonal_transform(S, S1))
            e1w = np.degrees(overlap_error_after_orthogonal_transform(S, S1w))
            e2 = np.degrees(overlap_error_after_orthogonal_transform(S, S2))
            r0 = np.degrees(distribution_radius(S))
            r1 = np.degrees(distribution_radius(S1))
            r1w = np.degrees(distribution_radius(S1w))
            r2 = np.degrees(distribution_radius(S2))
            yield dict(R0=r0, R1=r1, R1w=r1w, R2=r2, e1=e1, e2=e2,
                       s1=s1, s2=s2,
                       s1w=s1w, e1w=e1w)

    results = list(seq())
    data = dict((k, np.array([d[k] for d in results])) for k in results[0])

    r = Report('demo-convergence')

    api1 = 'pi1'
    api1w = 'pi1w'
    api2 = 'pi2'

    sets = [(data['R0'] < 90, 'r.'), (data['R0'] >= 90, 'g.')]

    f = r.figure('radius', cols=3, caption='radius of solution')
    with r.data_pylab('r0r1') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['R1'][sel]
            pylab.plot(x, x, 'k--')
            pylab.plot(x, y, col)

        pylab.xlabel('real radius')
        pylab.ylabel('radius (pi1)')
        pylab.axis('equal')
    r.last().add_to(f, caption=api1)

    with r.data_pylab('r0r1w') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['R1w'][sel]
            pylab.plot(x, x, 'k--')
            pylab.plot(x, y, col)

        pylab.xlabel('real radius')
        pylab.ylabel('radius (pi1 + warp)')
        pylab.axis('equal')
    r.last().add_to(f, caption=api1w)

    with r.data_pylab('r0r2') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['R2'][sel]
            pylab.plot(x, x, 'k--')
            pylab.plot(x, y, col)
        pylab.xlabel('real radius')
        pylab.ylabel('radius (pi2)')
        pylab.axis('equal')
    r.last().add_to(f, caption=api2)

    with r.data_pylab('r1r2') as pylab:
        for sel, col in sets:
            pylab.plot(data['R1'][sel], data['R2'][sel], col)

        pylab.xlabel('radius (pi1)')
        pylab.ylabel('radius (pi2)')
        pylab.axis('equal')
    r.last().add_to(f, 'Comparison %s - %s' % (api1, api2))

    with r.data_pylab('r1r1w') as pylab:
        for sel, col in sets:
            pylab.plot(data['R1'][sel], data['R1w'][sel], col)

        pylab.xlabel('radius (pi1)')
        pylab.ylabel('radius (pi1+warp)')
        pylab.axis('equal')
    r.last().add_to(f, 'Comparison %s - %s' % (api1, api1w))

    f = r.figure('spearman', cols=3, caption='Spearman score')
    with r.data_pylab('r0s1') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['s1'][sel]
            pylab.plot(x, y, col)
        pylab.xlabel('real radius')
        pylab.ylabel('spearman (pi1)')
    r.last().add_to(f, caption=api1)

    with r.data_pylab('r0s1w') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['s1w'][sel]
            pylab.plot(x, y, col)
        pylab.xlabel('real radius')
        pylab.ylabel('spearman (pi1+warp)')
    r.last().add_to(f, caption=api1w)

    with r.data_pylab('r0s2') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['s2'][sel]
            pylab.plot(x, y, col)
        pylab.xlabel('real radius')
        pylab.ylabel('spearman (pi2)')
    r.last().add_to(f, caption=api2)

    f = r.figure('final_error', cols=3, caption='Average absolute error')
    with r.data_pylab('r0e') as pylab:
        x = data['R0']
        y = data['e1']
        pylab.plot(x, y, 'm-', label=api1)
        x = data['R0']
        y = data['e1w']
        pylab.plot(x, y, 'g-', label=api1w)
        x = data['R0']
        y = data['e2']
        pylab.plot(x, y, 'b-', label=api2)
        pylab.xlabel('real radius')
        pylab.ylabel('average error (deg)')
        pylab.legend()
    r.last().add_to(f)

    filename = 'cbc_demos/convergence.html'
    print("Writing to %r." % filename)
    r.to_html(filename)
예제 #8
0
def create_report_randomness(id, desc, saccades): #@UnusedVariable
    report = Report(id)
   
    f = report.figure(cols=3)
     

    axis_angle = saccades['axis_angle']
    approach_angle = saccades['approach_angle']
    distance_from_wall = saccades['distance_from_wall']


    # additional analysis
    with report.data_pylab('axisangle_vs_distance') as pylab:
        pylab.plot(axis_angle, distance_from_wall, '.', markersize=1)
        pylab.xlabel('axis angle (deg)')
        pylab.ylabel('distance from wall  (m)')
        pylab.title('axis angle vs distance  (%s)' % id)
        pylab.axis([-180, 180, 0, 1])
        
    report.last().add_to(f)
 
    
    right = saccades['sign'] < 0
    left = saccades['sign'] > 0
    
    ms = 2
    
    with report.data_pylab('axisangle_vs_distance_lr') as pylab:
        pylab.plot(axis_angle[right], distance_from_wall[right], 'r.', markersize=ms)
        pylab.plot(axis_angle[left], distance_from_wall[left], 'b.', markersize=ms)
        pylab.xlabel('axis angle (deg)')
        pylab.ylabel('distance from wall  (m)')
        pylab.title('left and right saccades  (%s)' % id)
        pylab.axis([-180, 180, 0, 1])
  
    report.last().add_to(f)

 
    with report.data_pylab('axisangle_vs_distance_l') as pylab:
        # 
        pylab.plot(axis_angle[left], distance_from_wall[left], 'b.', markersize=ms)
        pylab.xlabel('axis angle (deg)')
        pylab.ylabel('distance from wall  (m)')
        pylab.title('only left saccades  (%s)' % id)
        pylab.axis([-180, 180, 0, 1])
        
    report.last().add_to(f)
 
 
    with report.data_pylab('axisangle_vs_distance_r') as pylab:
        pylab.plot(axis_angle[right], distance_from_wall[right], 'r.', markersize=ms)
        #
        pylab.xlabel('axis angle (deg)')
        pylab.ylabel('distance from wall  (m)')
        pylab.title('only right saccades  (%s)' % id)
        pylab.axis([-180, 180, 0, 1])
 
    report.last().add_to(f)
 
    with report.data_pylab('axisangle_vs_distance_rm') as pylab:
        pylab.plot(-axis_angle[right], distance_from_wall[right], 'r.', markersize=ms)
        #
        pylab.xlabel('axis angle (deg)')
        pylab.ylabel('distance from wall  (m)')
        pylab.title('only right saccades (mirror) (%s)' % id)
        pylab.axis([-180, 180, 0, 1])
        
    report.last().add_to(f)
 
    
    with report.data_pylab('approachangle_vs_distance_lr') as pylab:
        pylab.plot(approach_angle[right], distance_from_wall[right], 'r.', markersize=ms)
        pylab.plot(approach_angle[left], distance_from_wall[left], 'b.', markersize=ms)
        pylab.xlabel('approach angle (deg)')
        pylab.ylabel('distance from wall  (m)')
        pylab.title('left and right saccades  (%s)' % id)
        pylab.axis([-60, 60, 0, 1])
 
    report.last().add_to(f)
    
    smooth_displacement = saccades['smooth_displacement']
    with report.data_pylab('smooth_displacement_hist') as pylab:
        bins = range(-180, 180, 10)
        pylab.hist(smooth_displacement, bins, normed=True)
        pylab.xlabel('inter-saccade smooth displacement (deg)')
        pylab.ylabel('density')
        pylab.title('smooth displacement  (%s)' % id)
        # pylab.axis([-180, 180, 0, 700])
  
    f = report.figure('smooth')
    f.sub('smooth_displacement_hist')
  
    return report
def main():
    
    np.seterr(all='warn')
    
    n = 100
    z = np.linspace(-1, 1, n)
    z_order = np.array(range(n))
    alpha = 0.1
    base = 0.3
    noise_eff = 0.05
    noise_est = noise_eff
    f_L = lambda z: np.exp(-np.abs(+1 - np.maximum(z, 0)) / alpha) + base
    f_R = lambda z: np.exp(-np.abs(-1 - np.minimum(z, 0)) / alpha) + base
    
    rate_L0 = f_L(z) 
    rate_R0 = f_R(z) 
    
    simulate_L = lambda: f_L(z) + np.random.uniform(-1, 1, n) * noise_eff 
    simulate_R = lambda: f_R(z) + np.random.uniform(-1, 1, n) * noise_eff
    
    rate_L = simulate_L()
    rate_R = simulate_R()
    

    T = 100
    ord1 = np.zeros((n, T))
    for k in range(T):
        ord1[:, k] = scale_score(simulate_L())
    order_L_sim = np.ndarray(n, fit_dtype) 
    for i in range(n):
        order_L_sim[i]['mean'] = np.mean(ord1[i, :])
        l, u = np.percentile(ord1[i, :], [5, 95])
        order_L_sim[i]['upper'] = u
        order_L_sim[i]['lower'] = l
    
    
    rate_L_est = np.ndarray(n, fit_dtype) 
    rate_L_est['upper'] = rate_L + noise_est
    rate_L_est['lower'] = rate_L - noise_est
    rate_R_est = np.ndarray(n, fit_dtype) 
    rate_R_est['upper'] = rate_R + noise_est
    rate_R_est['lower'] = rate_R - noise_est

    # estimate according to naive procedure
    z_naive = estimate_stimulus_naive(rate_L, rate_R)
    
    res = estimate_stimulus(rate_L_est, rate_R_est)
    L_order = res.L_order
    R_order = res.R_order

        
    scale_rate = max(rate_L.max(), rate_R.max()) * 1.2
    cL = 'r'
    cR = 'b'
    
    r = Report()
    f = r.figure(cols=3)
    with r.plot('noiseless') as pylab:
        pylab.plot(z, rate_L0, '%s-' % cL)
        pylab.plot(z, rate_R0, '%s-' % cR)
        pylab.axis((-1, 1, 0.0, scale_rate))
    r.last().add_to(f, caption='noiseless rates')
    
    with r.plot('observed_rates') as pylab:
        pylab.plot(z, rate_R0, '%s-' % cR)
        pylab.plot(z, rate_L0, '%s-' % cL)
        plot_rate_bars(pylab, z, rate_L_est, '%s' % cL)
        plot_rate_bars(pylab, z, rate_R_est, '%s' % cR)
        pylab.axis((-1, 1, 0.0, scale_rate))
    r.last().add_to(f, caption='true_rates')
  
    with r.plot('M') as pylab:
        pylab.plot(z, rate_L0, '%s-' % cL)
        pylab.plot(z, rate_R0, '%s-' % cR)
        pylab.axis((-1, 1, 0.0, scale_rate))
        
    with r.plot('z_naive') as pylab:
        pylab.plot(z_naive, rate_L, '%s.' % cL)
        pylab.plot(z_naive, rate_R, '%s.' % cR)
        pylab.axis((-1, 1, 0.0, scale_rate))
    r.last().add_to(f, caption='Stimulus estimated in naive way.')
    
    
    with r.plot('simulated_order_stats') as pylab:
        pylab.plot([0, 0], [n, n], 'k-')
        pylab.plot([0, n], [n, 0], 'k-')
        pylab.plot(z_order, order_L_sim['mean'], '%s.' % cL)
        plot_rate_bars(pylab, z_order, order_L_sim, '%s' % cL)
        pylab.axis((0, n, -n / 10, n * 1.1))
        pylab.axis('equal')
    r.last().add_to(f, caption='Orders as found by simulation')
  
    
    with r.plot('estimated_order') as pylab:
        pylab.plot(z, L_order['mean'], '%s.' % cL)
        pylab.plot(z, R_order['mean'], '%s.' % cR)
        pylab.axis((-1, 1, -n / 2, n * 3 / 2))
    r.last().add_to(f, caption='estimated_order')
  
    with r.plot('estimated_order_order') as pylab:
        
        pylab.plot([0, 0], [n, n], 'k-')
        plot_rate_bars(pylab, z_order, L_order, '%s' % cL)
        plot_rate_bars(pylab, z_order, R_order, '%s' % cR)
        pylab.plot([0, 0], [n, n], 'k-')
        pylab.axis((0, n, -n / 10, n * 1.1))
        pylab.axis('equal')
    r.last().add_to(f, caption='estimated_order_order')
  
    with r.plot('estimated_order_bar') as pylab:
        pylab.plot(z, L_order['mean'], '%s.' % cL)
        pylab.plot(z, R_order['mean'], '%s.' % cR)
        plot_rate_bars(pylab, z, L_order, '%s' % cL)
        plot_rate_bars(pylab, z, R_order, '%s' % cR)     
        pylab.axis((-1, 1, -n / 2, n * 3 / 2))
    r.last().add_to(f, caption='estimated_order_bar') 

    with r.plot('estimated_order_both') as pylab:
        pylab.plot([0, 0], [n, n], 'g-')
        pylab.plot(z_order, res.order['mean'], 'k.')
        plot_rate_bars(pylab, z_order, res.order, 'k')
        pylab.axis((0, n, -n / 10, n * 1.1))
        pylab.axis('equal')
    r.last().add_to(f, caption='estimated_order_order') 

    with r.plot('z_better') as pylab:
        pylab.plot(res.z['mean'], rate_L, '%s.' % cL)
        pylab.plot(res.z['mean'], rate_R, '%s.' % cR)
        pylab.axis((-1, 1, 0.0, scale_rate))
    r.last().add_to(f, caption='Stimulus estimated in a better way.')
    
  
    filename = 'order_estimation.html' 
    print('Writing to %r.' % filename)
    r.to_html(filename)
예제 #10
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)
예제 #11
0
def main():
    def spearman(a, b):
        ao = scale_score(a)
        bo = scale_score(b)
        return correlation_coefficient(ao, bo)

    disable_all()

    def seq():
        N = 180
        iterations = 10
        nradii = 100
        radii = np.linspace(5, 180, nradii)

        K = 1
        for radius_deg, i in itertools.product(radii, range(K)):
            print radius_deg, i
            # Generate a random symmetric matrix
            # x = np.random.rand(N, N)
            S = random_directions_bounded(3, np.radians(radius_deg), N)
            C = np.dot(S.T, S)
            alpha = 1
            f = lambda x: np.exp(-alpha * (1 - x))
            # f = lambda x : x
            R = f(C)
            # Normalize in [0,1]
            R1 = (R - R.min()) / (R.max() - R.min())
            # Normalize in [-1,1]
            R2 = (R1 - 0.5) * 2

            S1 = simplified_algo(R1, iterations)
            S1w = simplified_algo(R1, iterations, warp=50)
            S2 = simplified_algo(R2, iterations)

            s1 = spearman(cosines_from_directions(S1), R1)
            s1w = spearman(cosines_from_directions(S1w), R1)
            s2 = spearman(cosines_from_directions(S2), R2)

            e1 = np.degrees(overlap_error_after_orthogonal_transform(S, S1))
            e1w = np.degrees(overlap_error_after_orthogonal_transform(S, S1w))
            e2 = np.degrees(overlap_error_after_orthogonal_transform(S, S2))
            r0 = np.degrees(distribution_radius(S))
            r1 = np.degrees(distribution_radius(S1))
            r1w = np.degrees(distribution_radius(S1w))
            r2 = np.degrees(distribution_radius(S2))
            yield dict(R0=r0,
                       R1=r1,
                       R1w=r1w,
                       R2=r2,
                       e1=e1,
                       e2=e2,
                       s1=s1,
                       s2=s2,
                       s1w=s1w,
                       e1w=e1w)

    results = list(seq())
    data = dict((k, np.array([d[k] for d in results])) for k in results[0])

    r = Report('demo-convergence')

    api1 = 'pi1'
    api1w = 'pi1w'
    api2 = 'pi2'

    sets = [(data['R0'] < 90, 'r.'), (data['R0'] >= 90, 'g.')]

    f = r.figure('radius', cols=3, caption='radius of solution')
    with r.data_pylab('r0r1') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['R1'][sel]
            pylab.plot(x, x, 'k--')
            pylab.plot(x, y, col)

        pylab.xlabel('real radius')
        pylab.ylabel('radius (pi1)')
        pylab.axis('equal')
    r.last().add_to(f, caption=api1)

    with r.data_pylab('r0r1w') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['R1w'][sel]
            pylab.plot(x, x, 'k--')
            pylab.plot(x, y, col)

        pylab.xlabel('real radius')
        pylab.ylabel('radius (pi1 + warp)')
        pylab.axis('equal')
    r.last().add_to(f, caption=api1w)

    with r.data_pylab('r0r2') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['R2'][sel]
            pylab.plot(x, x, 'k--')
            pylab.plot(x, y, col)
        pylab.xlabel('real radius')
        pylab.ylabel('radius (pi2)')
        pylab.axis('equal')
    r.last().add_to(f, caption=api2)

    with r.data_pylab('r1r2') as pylab:
        for sel, col in sets:
            pylab.plot(data['R1'][sel], data['R2'][sel], col)

        pylab.xlabel('radius (pi1)')
        pylab.ylabel('radius (pi2)')
        pylab.axis('equal')
    r.last().add_to(f, 'Comparison %s - %s' % (api1, api2))

    with r.data_pylab('r1r1w') as pylab:
        for sel, col in sets:
            pylab.plot(data['R1'][sel], data['R1w'][sel], col)

        pylab.xlabel('radius (pi1)')
        pylab.ylabel('radius (pi1+warp)')
        pylab.axis('equal')
    r.last().add_to(f, 'Comparison %s - %s' % (api1, api1w))

    f = r.figure('spearman', cols=3, caption='Spearman score')
    with r.data_pylab('r0s1') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['s1'][sel]
            pylab.plot(x, y, col)
        pylab.xlabel('real radius')
        pylab.ylabel('spearman (pi1)')
    r.last().add_to(f, caption=api1)

    with r.data_pylab('r0s1w') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['s1w'][sel]
            pylab.plot(x, y, col)
        pylab.xlabel('real radius')
        pylab.ylabel('spearman (pi1+warp)')
    r.last().add_to(f, caption=api1w)

    with r.data_pylab('r0s2') as pylab:
        for sel, col in sets:
            x = data['R0'][sel]
            y = data['s2'][sel]
            pylab.plot(x, y, col)
        pylab.xlabel('real radius')
        pylab.ylabel('spearman (pi2)')
    r.last().add_to(f, caption=api2)

    f = r.figure('final_error', cols=3, caption='Average absolute error')
    with r.data_pylab('r0e') as pylab:
        x = data['R0']
        y = data['e1']
        pylab.plot(x, y, 'm-', label=api1)
        x = data['R0']
        y = data['e1w']
        pylab.plot(x, y, 'g-', label=api1w)
        x = data['R0']
        y = data['e2']
        pylab.plot(x, y, 'b-', label=api2)
        pylab.xlabel('real radius')
        pylab.ylabel('average error (deg)')
        pylab.legend()
    r.last().add_to(f)

    filename = 'cbc_demos/convergence.html'
    print("Writing to %r." % filename)
    r.to_html(filename)