Beispiel #1
0
def add_order_comparison_figure(report, nid, figure, caption,
                                x_order, y_order, xlabel, ylabel):
    print('add_order_comparison_figure(%s)' % nid)

    x_order = np.array(x_order.flat)
    y_order = np.array(y_order.flat)
    n = x_order.size
    assert x_order.max() == n - 1
    assert y_order.max() == n - 1
    with report.plot(nid, figsize=(fsize, fsize)) as pylab:
        set_spines_look_A(pylab)

        pylab.plot(x_order, y_order, 'ms', **get_plot_params(x_order.size))

        pylab.xlabel(xlabel)
        pylab.ylabel(ylabel)
        pylab.xticks([0, n - 1], ['0', '$n-1$'])
        pylab.yticks([0, n - 1], ['0', '$n-1$'])
        r = correlation_coefficient(x_order, y_order)

        pylab.annotate('corr. = %.4f' % r,
                       va='top',
                       ha='right',
                       xy=(0.95, 0.95), # 0.25 for other
                       xycoords='figure fraction')

        pylab.axis([0, n - 1, 0, n - 1])

        pylab.gca().xaxis.set_label_coords(0.5, -0.25)
        pylab.gca().yaxis.set_label_coords(-0.25, 0.5)

    report.last().add_to(figure, caption)
Beispiel #2
0
def add_order_comparison_figure(report, nid, figure, caption, x_order, y_order,
                                xlabel, ylabel):
    print('add_order_comparison_figure(%s)' % nid)

    x_order = np.array(x_order.flat)
    y_order = np.array(y_order.flat)
    n = x_order.size
    assert x_order.max() == n - 1
    assert y_order.max() == n - 1
    with report.plot(nid, figsize=(fsize, fsize)) as pylab:
        set_spines_look_A(pylab)

        pylab.plot(x_order, y_order, 'ms', **get_plot_params(x_order.size))

        pylab.xlabel(xlabel)
        pylab.ylabel(ylabel)
        pylab.xticks([0, n - 1], ['0', '$n-1$'])
        pylab.yticks([0, n - 1], ['0', '$n-1$'])
        r = correlation_coefficient(x_order, y_order)

        pylab.annotate(
            'corr. = %.4f' % r,
            va='top',
            ha='right',
            xy=(0.95, 0.95),  # 0.25 for other
            xycoords='figure fraction')

        pylab.axis([0, n - 1, 0, n - 1])

        pylab.gca().xaxis.set_label_coords(0.5, -0.25)
        pylab.gca().yaxis.set_label_coords(-0.25, 0.5)

    report.last().add_to(figure, caption)
Beispiel #3
0
def set_spines_look_A_demo(r):
    """ Displaying only left and bottom spines. """
    f = r.figure()
    x = np.linspace(0, 2 * np.pi, 100)
    y = 2 * np.sin(x)

    for off in [0, 10, 50]:
        with f.plot(figsize=(3, 2),
                    caption="set_spines_look_A(pylab, outward_offset=%s)" %
                    off) as pylab:
            pylab.plot(x, y)
            set_spines_look_A(pylab, outward_offset=off)
Beispiel #4
0
def set_spines_look_A_demo(r):
    ''' Displaying only left and bottom spines. '''
    f = r.figure()
    x = np.linspace(0, 2 * np.pi, 100)
    y = 2 * np.sin(x)         

    for off in [0, 10, 50]:
        with f.plot(
            figsize=(3, 2),
            caption='set_spines_look_A(pylab, outward_offset=%s)' % off 
        ) as pylab:
            pylab.plot(x, y)
            set_spines_look_A(pylab, outward_offset=off)
Beispiel #5
0
def util_plot_euclidean_coords2d(report, f, nid, S, caption=None):
    print('util_plot_euclidean_coords2d(%s)' % nid)

    with report.plot(nid, figsize=(fsize, fsize), caption=caption) as pylab:
        set_spines_look_A(pylab)
        # do not rasterize, they are small
        pylab.plot(S[0, :], S[1, :], 'ks', markersize=0.5)
        #M = np.abs(S).max()
        M = 1.1
        #pylab.axis('equal')
        pylab.axis([-M, +M, -M, +M])

    report.last().add_to(f)
Beispiel #6
0
def util_plot_euclidean_coords2d(report, f, nid, S, caption=None):
    print('util_plot_euclidean_coords2d(%s)' % nid)

    with report.plot(nid, figsize=(fsize, fsize), caption=caption) as pylab:
        set_spines_look_A(pylab)
        # do not rasterize, they are small
        pylab.plot(S[0, :], S[1, :], 'ks', markersize=0.5)
        #M = np.abs(S).max()
        M = 1.1
        #pylab.axis('equal')
        pylab.axis([-M, +M, -M, +M])

    report.last().add_to(f)
Beispiel #7
0
def add_distance_vs_sim_figure(report, nid, figure, caption,
                                D, R, xlabel, ylabel, degrees=False):
    print('add_distance_vs_sim_figure(%s)' % nid)

    D = np.array(D.flat)
    R = np.array(R.flat)

    with report.plot(nid, figsize=(fsize, fsize)) as pylab:
        set_spines_look_A(pylab)

        plot_params = get_plot_params(nsamples=D.size)
        print(' using plot params: %s' % plot_params)
        pylab.plot(D, R, 'bs', **plot_params)
        pylab.xlabel(xlabel)
        pylab.ylabel(ylabel)
        pylab.axis([D.min(), D.max(), R.min(), R.max()])
        m = D.max()

        def set_ticks(t, M):
            pos = [a for a, _ in t]
            val = [b for _, b in t]
            if degrees:
                val = add_textdegree(val)
            pylab.xticks(np.deg2rad(pos), val)
            pylab.axis([0, np.deg2rad(M), -0.3, +1])

        # TODO: euclidean geometry
        if m < np.pi / 4:
            set_ticks([(0, '0'),
                     (10, '10'),
                     (20, '20'),
                     (30, '30'),
                     (40, '40'),
                      (50, '50')], 50)
        elif m < np.pi / 2:
            set_ticks([(0, '0'),
                     (15, ''),
                     (30, '30'),
                     (45, ''),
                     (60, '60'),
                      (75, ''),
                     (90, '90')], 90)
        else:
            set_ticks([(0, '0'),
                     (45, '45'),
                     (90, '90'),
                     (135, '135'),
                     (180, '180')], 180)

    report.last().add_to(figure, caption)
def plot_feature_comparison(r, f, Z, Zpred):
    def normalize(x):
        return x / np.abs(x).max()
    Z = normalize(Z)
    Zpred = normalize(Zpred)

    Z_order = scale_score_norm(Z) 
    Zpred_order = scale_score_norm(Zpred)

    #print Zpred_order[Z_order == 0]
    if Zpred_order[Z_order == 0] > 0.5:
        Zpred = -Zpred  
        Zpred_order = scale_score_norm(Zpred)
    #print Zpred_order[Z_order == 0]
    
    M = 1

    def lab(pylab):    
        pass
        if False:
            pylab.ylabel('predicted $\\hat{z}$')
            pylab.xlabel('observed $z$')

    def ticks(cord):
        l = ['-1', '', '0', '', '+1']
        pylab.xticks(cord, l)
        pylab.yticks(cord, l)

    with r.plot('Z_Z2', figsize=PlotParams.figsize_order,
                caption='Feature vs predicted feature') as pylab:
        set_spines_look_A(pylab, PlotParams.spines_outward)
        pylab.plot(Z, Zpred, 'ks', markersize=0.6) 
        pylab.axis([-M, M, -M, M])
        ticks([-1, -0.5, 0, 0.5, 1])
        
        lab(pylab)
        
    r.last().add_to(f)
         
    with r.plot('Z_Z2_order', figsize=PlotParams.figsize_order,
        caption="order(feature) vs order(predicted feat.)") as pylab:
        pylab.plot(Z_order, Zpred_order, 'ks', markersize=0.6)
        lab(pylab)
        set_spines_look_A(pylab, PlotParams.spines_outward)
        pylab.axis([0, 1, 0, 1])
        ticks([0, 0.25, 0.5, 0.75, 1])        
        lab(pylab)
        
    r.last().add_to(f)
Beispiel #9
0
def add_distance_vs_sim_figure(report,
                               nid,
                               figure,
                               caption,
                               D,
                               R,
                               xlabel,
                               ylabel,
                               degrees=False):
    print('add_distance_vs_sim_figure(%s)' % nid)

    D = np.array(D.flat)
    R = np.array(R.flat)

    with report.plot(nid, figsize=(fsize, fsize)) as pylab:
        set_spines_look_A(pylab)

        plot_params = get_plot_params(nsamples=D.size)
        print(' using plot params: %s' % plot_params)
        pylab.plot(D, R, 'bs', **plot_params)
        pylab.xlabel(xlabel)
        pylab.ylabel(ylabel)
        pylab.axis([D.min(), D.max(), R.min(), R.max()])
        m = D.max()

        def set_ticks(t, M):
            pos = [a for a, _ in t]
            val = [b for _, b in t]
            if degrees:
                val = add_textdegree(val)
            pylab.xticks(np.deg2rad(pos), val)
            pylab.axis([0, np.deg2rad(M), -0.3, +1])

        # TODO: euclidean geometry
        if m < np.pi / 4:
            set_ticks([(0, '0'), (10, '10'), (20, '20'), (30, '30'),
                       (40, '40'), (50, '50')], 50)
        elif m < np.pi / 2:
            set_ticks([(0, '0'), (15, ''), (30, '30'), (45, ''), (60, '60'),
                       (75, ''), (90, '90')], 90)
        else:
            set_ticks([(0, '0'), (45, '45'), (90, '90'), (135, '135'),
                       (180, '180')], 180)

    report.last().add_to(figure, caption)
def plot_kernel(r, f, name, directions, kernel, caption=None):
    
    middle = kernel[kernel.shape[0] / 2]
    if middle < 0:
        kernel *= -1
        
    kernel = kernel / np.abs(kernel).max()
    
    with r.plot(name, caption=caption,
                figsize=PlotParams.figsize_kernel) as pylab:
        set_spines_look_A(pylab, PlotParams.spines_outward)
        theta = np.rad2deg(directions)
        
        for i in [-1, 0, 1]:
            plot_horizontal_line(pylab, i, 'k--', linewidth=0.6)

        l = pylab.plot(theta, kernel, 'g-', linewidth=1)
        l[0].set_clip_on(False)
        
        pylab.axis([-180, 180, -1, +1])

        #pylab.xlabel('directions')
        
        #pylab.ylabel('kernel')
        xt = [-180, -90, 0, +90, 180]
        pylab.xticks(xt,
                     add_deg(['-180', '', '0', '', '+180']))
#                             add_deg(['-180', '-90', '0', '+90', '+180']))
                     
        xt = [-1, -0.5, 0, 0.5, +1]
        xtt = ['-1.0', "-0.5", "0", "0.5", '+1.0']
        pylab.yticks(xt, xtt)

        #pylab.gca().yaxis.set_label_coords(-0.05, 0.5)
        #pylab.gca().xaxis.set_label_coords(0.5, -0.05)
        
    r.last().add_to(f)
def plot_image(r, fig, nid, cells, field, caption=None, scale_params={},
               colors='scale',
               scale_format=None):
    print('plot_image(%s)' % nid)

    assert colors in ['scale', 'posneg', 'cmap']
    
    cells.check_compatible_shape(field)
    
    d_edges = cells.d_edges
    a_edges = cells.a_edges
    
    nd = len(d_edges) - 1 
    
    properties = get_rgb(field, colors, **scale_params)
    colorbar = properties['color_bar']
    rgb = properties['rgb']    
    
    figparams = dict(figsize=PlotParams.figsize)
    
    with r.plot(nid, caption=caption, **figparams) as pl:
        # left bottom width height
#        L = 0.6
#        axes2 = pl.axes([L, 0.0, 1 - L, 0.1])
#        for loc, spine in axes2.spines.iteritems():
#            spine.set_color('none') # don't draw spi
##        pl.xticks([], [])
##        pl.yticks([], [])                
#        #pl.sca(ax)
#
#        pl.axes([0, 0, L - 0.2, 1])

        #ax = pl.gca()  
        
        set_spines_look_A(pl, PlotParams.spines_outward)

        for a, d in itertools.product(range(len(a_edges) - 1),
                                      range(len(d_edges) - 1)):
            a_min = a_edges[a]
            a_max = a_edges[a + 1] 
            d_min = d
            d_max = d + 1
            quatx = [a_min, a_min, a_max, a_max]
            quaty = [d_min, d_max, d_max, d_min] 
            pl.fill(quatx, quaty, color=rgb[d, a, :])
            
        labels_at = [0.16, 0.20, 0.30, 0.40, 0.5, 0.6, 0.70, 1]
        tick_pos = []
        tick_label = []
        for l in labels_at:
            closest = np.argmin(np.abs(d_edges - l))
            tick_pos.append(closest)
            tick_label.append('%.2f' % l)
        pl.yticks(tick_pos, tick_label)
       
        bar_w = 15
        bar_x = 180 + 3 * bar_w
        pl.axis((-180, bar_x + bar_w * 4, 0, nd))

        def putcolorbar():   
            if colorbar is not None:
                f = 3
                plot_vertical_colorbar(pl, colorbar,
                                       bar_x=bar_x, bar_w=bar_w,
                                       bar_y_min=f, bar_y_max=nd - f,
                                       vdist=0.40,
                                       min_value=properties['min_value'],
                                       max_value=properties['max_value'],
                                       scale_format=scale_format)
        
        if True:
            putcolorbar()
            pl.axis((-180, bar_x + bar_w * 4, 0, nd))
           
#            ex = [180, bar_x + bar_w * 4, 0.4, 0.5]
#            pl.fill([ex[0], ex[0], ex[1], ex[1]],
#                        [ex[2], ex[3], ex[3], ex[2]], facecolor='w', 
#                        edgecolor='none')
        else:
            pl.axis((-180, 180, 0, nd))
            putcolorbar()
#            pl.gca().set_position([0, 0, L - 0.2, 1])

#        
#        else:
#            pl.axis((-180, 180, 0, nd))
#            L = 0.8
#            pl.gca().set_position([0.125, 0.1, L, 0.9])
#
#            ax = pl.gca()
#            
#            # left bottom width height
#            axes2 = pl.axes([L, 0.0, 1 - L, 1])
#            for loc, spine in axes2.spines.iteritems():
#                spine.set_color('none') # don't draw spi
#            pl.xticks([], [])
#            pl.yticks([], [])                
# 
#            putcolorbar()
#
#            
#            pl.sca(ax)
        
        pl.xticks([-180, -135, -90, -45, 0, 45, +90, 135, 180],
                  add_deg(["-180", "", "-90", "", "0",
                           "", "+90", "", "180"])) 
                
        # pl.xlabel('axis angle \\ $\\varphi$ (deg)')
        pl.xlabel('axis angle $\\varphi$')
        pl.ylabel('distance from wall $d$ (m)')
        #pl.gca().xaxis.set_label_coords(0.38, -0.02)
        #pl.gca().yaxis.set_label_coords(-0.17, 0.5)
        
    if r != fig:
        r.last().add_to(fig)
Beispiel #12
0
def util_plot_3D_points(report, f, nid, S, caption=None):
    print('util_plot_3D_points(%s)' % nid)
    # let (1,0,0) be (0,0,1)
    S1 = np.empty_like(S)
    S1[0, :] = S[2, :]
    S1[1, :] = S[1, :]
    S1[2, :] = S[0, :]

    if np.mean(S1[0, :]) < 0:
        S1[0, :] *= -1

    # switch Y and Z if necessary
    if np.mean(np.abs(S1[1, :])) < np.mean(np.abs(S1[2, :])):
        S2 = np.empty_like(S1)
        S2[0, :] = S1[0, :]
        S2[1, :] = S1[2, :]
        S2[2, :] = S1[1, :]
        S1 = S2

    AE = azi_elev_from_directions(S1)

    A = AE[0, :]
    E = AE[1, :]
    A_deg = np.rad2deg(A)
    E_deg = np.rad2deg(E)
    with report.plot(nid, figsize=figsize_points) as pylab:
        set_spines_look_A(pylab)

        pylab.plot(A_deg, E_deg, 'bs', markersize=0.2)

        max_azi_deg = np.max(A_deg)
        max_ele_deg = np.max(E_deg)
        #print('max_azi_deg', max_azi_deg)
        #print('max_ele', np.max(E_deg))
        if max_azi_deg < 60:  # mino
            #print('config mino')
            set_xticks_from_seq(
                pylab,
                [  #(-45, '-45'), 
                    (-30, '-30'),
                    (-15, ''),
                    (0, '0'),
                    (+15, ''),
                    (30, '30'),
                    #(45, '+45')
                ])
            MA = 30
        elif max_azi_deg < 90:  # GOPRO
            #print('config gopro')
            set_xticks_from_seq(pylab, [(-90, '-90'), (-75, ''), (-60, ''),
                                        (-45, '-45'), (-30, ''), (-15, ''),
                                        (0, '0'), (+15, ''), (30, ''),
                                        (45, '45'), (60, ''), (75, ''),
                                        (90, '+90')])
            MA = 95

        else:  # omni
            #print('config omni')
            set_xticks_from_seq(pylab, [(-180, '-180'), (-135, ''),
                                        (-90, '-90'), (-45, ''), (0, '0'),
                                        (+45, ''), (90, '+90'), (135, ''),
                                        (180, '+180')])
            MA = 180

        if max_ele_deg < 15:
            ME = 15
            set_yticks_from_seq(pylab, [(-15, '-15'), (-10, ''), (-5, ''),
                                        (0, '0'), (5, ''), (10, ''),
                                        (+15, '15')])
        elif max_ele_deg < 55:
            ME = 55
            set_yticks_from_seq(pylab, [(-45, '-45'), (-30, ''), (-15, ''),
                                        (0, '0'), (15, ''), (30, ''),
                                        (+45, '45')])

        else:
            ME = 90

            set_yticks_from_seq(pylab, [(-90, '-90'), (-45, '-45'), (0, '0'),
                                        (+45, '+45'), (+90, '+90')])

        pylab.axis([-MA, MA, -ME, ME])
        pylab.ylabel('elevation')
        pylab.xlabel('azimuth')

        if False:
            pylab.gca().xaxis.set_label_coords(0.5, -0.02)
            pylab.gca().yaxis.set_label_coords(-0.02, 0.5)

    report.last().add_to(f, caption)
Beispiel #13
0
def util_plot_3D_points(report, f, nid, S, caption=None):
    print('util_plot_3D_points(%s)' % nid)
    # let (1,0,0) be (0,0,1)
    S1 = np.empty_like(S)
    S1[0, :] = S[2, :]
    S1[1, :] = S[1, :]
    S1[2, :] = S[0, :]

    if np.mean(S1[0, :]) < 0:
        S1[0, :] *= -1

    # switch Y and Z if necessary
    if np.mean(np.abs(S1[1, :])) < np.mean(np.abs(S1[2, :])):
        S2 = np.empty_like(S1)
        S2[0, :] = S1[0, :]
        S2[1, :] = S1[2, :]
        S2[2, :] = S1[1, :]
        S1 = S2

    AE = azi_elev_from_directions(S1)

    A = AE[0, :]
    E = AE[1, :]
    A_deg = np.rad2deg(A)
    E_deg = np.rad2deg(E)
    with report.plot(nid, figsize=figsize_points) as pylab:
        set_spines_look_A(pylab)

        pylab.plot(A_deg, E_deg, 'bs', markersize=0.2)

        max_azi_deg = np.max(A_deg)
        max_ele_deg = np.max(E_deg)
        #print('max_azi_deg', max_azi_deg)
        #print('max_ele', np.max(E_deg))
        if max_azi_deg < 60: # mino
            #print('config mino')
            set_xticks_from_seq(pylab,
                                [#(-45, '-45'), 
                                 (-30, '-30'), (-15, ''), (0, '0'),
                                 (+15, ''), (30, '30'),
                                 #(45, '+45')
                                 ]
                                )
            MA = 30
        elif max_azi_deg < 90: # GOPRO
            #print('config gopro')
            set_xticks_from_seq(pylab,
                                [(-90, '-90'), (-75, ''), (-60, ''),
                                 (-45, '-45'),
                                 (-30, ''),
                                 (-15, ''), (0, '0'),
                                 (+15, ''), (30, ''), (45, '45'), (60, ''),
                                 (75, ''), (90, '+90')])
            MA = 95

        else: # omni
            #print('config omni')
            set_xticks_from_seq(pylab,
                                [(-180, '-180'), (-135, ''), (-90, '-90'),
                                 (-45, ''), (0, '0'),
                                 (+45, ''), (90, '+90'), (135, ''),
                                 (180, '+180')])
            MA = 180

        if max_ele_deg < 15:
            ME = 15
            set_yticks_from_seq(pylab,
            [(-15, '-15'), (-10, ''), (-5, ''), (0, '0'),
             (5, ''), (10, ''), (+15, '15')])
        elif max_ele_deg < 55:
            ME = 55
            set_yticks_from_seq(pylab,
            [(-45, '-45'), (-30, ''), (-15, ''), (0, '0'), (15, ''), (30, ''),
             (+45, '45')])

        else:
            ME = 90

            set_yticks_from_seq(pylab,
                [(-90, '-90'), (-45, '-45'), (0, '0'),
                 (+45, '+45'), (+90, '+90')])

        pylab.axis([-MA, MA, -ME, ME])
        pylab.ylabel('elevation')
        pylab.xlabel('azimuth')

        if False:
            pylab.gca().xaxis.set_label_coords(0.5, -0.02)
            pylab.gca().yaxis.set_label_coords(-0.02, 0.5)

    report.last().add_to(f, caption)