예제 #1
0
def interval_histogram(group, configuration, saccades):    #@UnusedVariable
    interval = saccades[:]['time_passed']

    edges = (2.0 ** numpy.array(range(1, 21))) / 1000
    # centers = (edges[1:]+edges[:-1])/2
    h, edges_ = numpy.histogram(interval, bins=edges, normed=True) #@UnusedVariable
    
    bin_width = numpy.diff(edges);
    hn = h / bin_width;
    
    print 'h', h
    print 'hn', hn
    print 'edges', edges
    print 'width', bin_width
                                
    r = Report()
    attach_description(r, description)
    
    node_id = 'inthist'
    with r.data_pylab(node_id) as pylab:
        pylab.loglog(bin_width, h, 'x-')
        pylab.title('not normalized')
        pylab.xlabel('interval bin width (s)')
        pylab.ylabel('density (s)')
        
    node_id = 'inthistn'
    with r.data_pylab(node_id) as pylab:
        pylab.loglog(bin_width, hn, 'x-')
        pylab.title('normalized by bin width')
        pylab.xlabel('interval bin width (s)')
        pylab.ylabel('density (s)')
        
        
    return r
def sample_var_time_correlation(
        sample, expdata, configuration, saccades, #@UnusedVariable
        variables, delays, type='pearson'):
    
    # all together
    R, P, labels = get_correlation_matrix(saccades, variables, delays,
                                          type)
    
    #  Significance 
    S = P < 0.01 
    
    nvars = len(variables)
    Rhalf = R[:nvars, :]
    Phalf = P[:nvars, :]
    Shalf = S[:nvars, :]
    
    ylabels = labels[:nvars]
    
    r = Report()
    attach_description(r, create_description(variables, delays, type))
    with r.data_pylab('correlation') as pylab:
        draw_correlation_figure(pylab, labels, ylabels, Rhalf)

    rshow = lambda x: "%+.2f" % x
    r.table('correlation_values', values_to_strings(Rhalf, rshow),
            cols=labels, rows=ylabels, caption="%s coefficient" % type)    
    r.table('pvalues', values_to_strings(Phalf, pvalue_format),
            cols=labels, rows=ylabels, caption="p-values")    

    with r.data_pylab('significance') as pylab:
        draw_significance_figure(pylab, labels, ylabels, Shalf)
    
    return r
예제 #3
0
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 create_report(data, image, outdir):
    r = Report('%s_stats' % image)
    
    xcorr = data['results']
    lags = data['lags']
    T = lags * (1.0 / 60) * 1000
    
    mean_xcorr = numpy.mean(xcorr, axis=0)
    min_xcorr = numpy.min(xcorr, axis=0)
    max_xcorr = numpy.max(xcorr, axis=0)
    
    with r.data_pylab('some') as pylab:
        
        for i in range(0, 1000, 50):
            pylab.plot(T, xcorr[i, :], 'x-', label='%d' % i)
    
        pylab.axis([T[0], T[-1], -0.5, 1])
        pylab.xlabel('delay (ms)')
        pylab.ylabel('autocorrelation')
        pylab.legend()
    
    with r.data_pylab('mean_xcorr') as pylab:
        pylab.plot(T, mean_xcorr, 'x-')
        
        pylab.plot([T[0], T[-1]], [0, 0], 'k-')
        pylab.plot([0, 0], [-0.5, 1], 'k-')
        pylab.axis([T[0], T[-1], -0.5, 1.1])
        
        pylab.xlabel('delay (ms)')
        pylab.ylabel('autocorrelation')
        
        
    with r.data_pylab('various') as pylab:
        pylab.plot(T, mean_xcorr, 'gx-', label='mean')
        pylab.plot(T, min_xcorr, 'bx-', label='min')
        pylab.plot(T, max_xcorr, 'rx-', label='max')
        
        pylab.plot([T[0], T[-1]], [0, 0], 'k-')
        pylab.plot([0, 0], [-0.5, 1], 'k-')
        pylab.axis([T[0], T[-1], -0.5, 1.1])
        
        pylab.xlabel('delay (ms)')
        pylab.ylabel('autocorrelation')
        pylab.legend()
        
    f = r.figure()
    f.sub('some', caption='Autocorrelation of some receptors')
    f.sub('mean_xcorr', caption='Mean autocorrelation')
    
    f.sub('various', caption='Mean,min,max')
    
    filename = os.path.join(outdir, r.id + '.html')
    resources = os.path.join(outdir, 'images')
    print 'Writing to %s' % filename
    r.to_html(filename, resources)
    
    return r
예제 #5
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)
예제 #6
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)
예제 #7
0
def group_var_joint(group, configuration, saccades, #@UnusedVariable
                    var1, delay1, var2, delay2):    
    
    var1delay = get_delay_desc_string(delay1)
    var2delay = get_delay_desc_string(delay2)
    
    r = Report()
    attach_description(r, description.format(var1=var1, var2=var2,
                            var1delay=var1delay, var2delay=var2delay))
    
    node_id = 'joint_%s%d_%s%d' % (var1.id, delay1, var2.id, delay2)
    with r.data_pylab(node_id) as pylab:
    
        colors = ['r', 'g', 'b', 'm', 'k'] * 50        
        for sample, saccades_for_sample in iterate_over_samples(saccades): #@UnusedVariable
            x = saccades_for_sample[var1.field]
            y = saccades_for_sample[var2.field]
            x, y = get_delayed(x, delay1, y, delay2)
            
            color = colors.pop()            
            pylab.plot(x, y, "%s." % color, markersize=MS)
            
        pylab.axis([var1.interesting[0], var1.interesting[1],
                    var2.interesting[0], var2.interesting[1]]
                    )
         
        pylab.xlabel('%s (%s)' % (var1.name, var1.unit))
        pylab.ylabel('%s (%s)' % (var2.name, var2.unit))
        
    node_id += "_log"
    with r.data_pylab(node_id) as pylab:
    
        colors = ['r', 'g', 'b', 'm', 'k'] * 50        
        for sample, saccades_for_sample in iterate_over_samples(saccades): #@UnusedVariable
            x = saccades_for_sample[var1.field]
            y = saccades_for_sample[var2.field]
            x, y = get_delayed(x, delay1, y, delay2)
            
            color = colors.pop()            
            pylab.loglog(x, y, "%s." % color, markersize=MS)
            
        pylab.axis([var1.interesting[0], var1.interesting[1],
                    var2.interesting[0], var2.interesting[1]]
                    )
         
        pylab.xlabel('%s (%s)' % (var1.name, var1.unit))
        pylab.ylabel('%s (%s)' % (var2.name, var2.unit))
        
    return r
예제 #8
0
def group_var_hist(group, configuration, saccades, variable): #@UnusedVariable
    lb = variable.interesting[0]
    ub = variable.interesting[1]
    
    x = saccades[variable.field]
    
    if variable.mod:
        M = variable.interesting[1]
        x = numpy.fmod(x + M, M)

    hist, bin_edges = numpy.histogram(x, bins=variable.density_bins,
                range=variable.interesting, normed=True)

    bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
    
    r = Report()
    attach_description(r, description.format(var=variable, ub=ub, lb=lb))

    with r.data_pylab('histogram') as pylab:
        pylab.plot(bin_centers, hist, 'b-')
        
        pylab.ylabel('density')
        pylab.xlabel('%s (%s)' % (variable.name, variable.unit))
        
        pylab.axis([lb, ub, 0, variable.density_max_y])
        
    return r
예제 #9
0
def sample_var_hist(sample, expdata, configuration, #@UnusedVariable
                    saccades, variable):
    lb = variable.interesting[0]
    ub = variable.interesting[1]
    
    x = saccades[variable.field]
    if variable.mod:
        M = variable.interesting[1]
        x = numpy.fmod(x + M, M)
    
    # TODO: we don't strictly enforce the bounds and we do not compute
    # how many are left out
    hist, bin_edges = numpy.histogram(x, bins=variable.density_bins,
                range=variable.interesting, normed=True)

    bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
     
    r = Report()
    attach_description(r, description.format(var=variable, ub=ub, lb=lb))
    with r.data_pylab('histogram') as pylab:
        pylab.plot(bin_centers, hist, 'b-')
        
        pylab.ylabel('density')
        pylab.xlabel('%s (%s)' % (variable.name, variable.unit))
        
        pylab.axis([lb, ub, 0, variable.density_max_y])
    return r
def plot_simulated_sample_trajectories(
                    sample, exp_data, configuration, saccades): #@UnusedVariable
    x = [0]
    y = [0]
    theta = 0 
    for saccade in saccades:
        dt = saccade['time_passed']
        xp = x[-1] + numpy.cos(theta) * dt * v
        yp = y[-1] + numpy.sin(theta) * dt * v
        
        x.append(xp)
        y.append(yp)
        
        theta += numpy.radians(saccade['amplitude']) * saccade['sign']
        
    r = Report()
    attach_description(r, description)
    
    with r.data_pylab('simulated_trajectory') as pylab:
        pylab.plot(x, y, 'b-')
        pylab.xlabel('x position (m)') 
        pylab.ylabel('y position (m)')
        pylab.axis('equal')
        
    return r
예제 #11
0
def group_sign_hist(group, configuration, saccades): #@UnusedVariable
    r = Report()
    attach_description(r, description)

    left_percentage = []
    for sample, saccades_for_sample in iterate_over_samples(saccades): #@UnusedVariable
        
        sign = saccades_for_sample['sign']
        left, = numpy.nonzero(sign == +1)
        
        perc = len(left) * 100.0 / len(sign)
        
        left_percentage.append(perc)

    left_percentage = numpy.array(left_percentage) 
    N = len(left_percentage)
    with r.data_pylab('sign_hist') as pylab:
        R = range(N)
         
        right_percentage = -left_percentage + 100
        
        pylab.bar(left=R, height=left_percentage, color='b')
        pylab.bar(left=R, height=right_percentage, bottom=left_percentage,
                  color='#faacb6')
        
        pylab.plot([0], [0])
        pylab.ylabel('percentage of left turns')
        pylab.xlabel('sample')
        pylab.axis([0, N, 0, 100])
        
        
    return r
def plot_raw_trajectories(sample, exp_data): #@UnusedVariable
    thetas = numpy.radians(exp_data[:]['orientation'])
    T = exp_data[:]['timestamp']
    
    x = [0]
    y = [0]
    
    dt = T[1] - T[0]  
    for i in range(len(thetas)):
        theta = thetas[i]
        xp = x[-1] + numpy.cos(theta) * dt * v
        yp = y[-1] + numpy.sin(theta) * dt * v
        
        x.append(xp)
        y.append(yp)        
        
    r = Report()
    attach_description(r, description)
    with r.data_pylab('simulated_trajectory') as pylab:
        pylab.plot(x, y, 'b-') 
        pylab.xlabel('x position (m)') 
        pylab.ylabel('y position (m)')
        pylab.axis('equal')

    return r
def compute_general_statistics(id, db, samples, interval_function,
                               signal, signal_component):
    r = Report(id)
    
    x = get_all_data_for_signal(db, samples, interval_function,
                                signal, signal_component)
    
    limit = 0.3
    
    perc = [0.001, limit, 1, 10, 25, 50, 75, 90, 99, 100 - limit, 100 - 0.001]
    xp = map(lambda p: "%.3f" % scipy.stats.scoreatpercentile(x, p), perc)
    
    lower = scipy.stats.scoreatpercentile(x, limit)
    upper = scipy.stats.scoreatpercentile(x, 100 - limit)
    
    f = r.figure()
    
    with r.data_pylab('histogram') as pylab:
        bins = numpy. linspace(lower, upper, 100)
        pylab.hist(x, bins=bins)
        
    f.sub('histogram')

    
    labels = map(lambda p: "%.3f%%" % p, perc)
    
    
    r.table("percentiles", data=[xp], cols=labels, caption="Percentiles")
    
    r.table("stats", data=[[x.mean(), x.std()]], cols=['mean', 'std.dev.'],
            caption="Other statistics")

    print "Computing correlation..."
    corr, lags = xcorr(x, maxlag=20)
    print "...done."

    with r.data_pylab('cross_correlation') as pylab:
        delta = (1.0 / 60) * lags * 1000;
        pylab.plot(delta, corr, 'o-')
        pylab.axis([min(delta), max(delta), -0.7, 1.1])
        pylab.xlabel('delay (ms)')
    
    f = r.figure()
    f.sub('cross_correlation')

     
    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
예제 #15
0
def sample_var_joint(sample, expdata, configuration, saccades, #@UnusedVariable
                     var1, delay1, var2, delay2):
    x = saccades[var1.field]
    y = saccades[var2.field]
    x, y = get_delayed(x, delay1, y, delay2)

    var1delay = get_delay_desc_string(delay1)
    var2delay = get_delay_desc_string(delay2)
    
    r = Report()
    attach_description(r, description.format(var1=var1, var2=var2,
                                             var1delay=var1delay,
                                             var2delay=var2delay))
    node_id = 'joint_%s%d_%s%d' % (var1.id, delay1, var2.id, delay2)
    with r.data_pylab(node_id) as pylab:
            
        pylab.plot(x, y, "b.", markersize=MS)
            
        pylab.axis([var1.interesting[0], var1.interesting[1],
                    var2.interesting[0], var2.interesting[1]]
                    )
         
        pylab.xlabel('%s (%s)' % (var1.name, var1.unit))
        pylab.ylabel('%s (%s)' % (var2.name, var2.unit))
        
    node_id += '_log'
    with r.data_pylab(node_id) as pylab:
            
        pylab.loglog(x, y, "b.", markersize=MS)
            
        pylab.axis([var1.interesting[0], var1.interesting[1],
                    var2.interesting[0], var2.interesting[1]]
                    )
         
        pylab.xlabel('%s (%s)' % (var1.name, var1.unit))
        pylab.ylabel('%s (%s)' % (var2.name, var2.unit))
    
    return r
def group_var_percentiles(group, configuration, saccades, variable):  # @UnusedVariable
    percentiles = [1, 5, 25, 50, 75, 95, 99]
    colors = ["k", "r", "b", "g", "b", "r", "k"]
    vcolors = ["k", "r", "b", "b", "r", "k"]
    scores = {}
    for p in percentiles:
        scores[p] = []

    scores_for_sample = []

    for sample, saccades_for_sample in iterate_over_samples(saccades):  # @UnusedVariable
        x = saccades_for_sample[variable.field]
        sc = numpy.percentile(x, percentiles)
        # print sample, sc
        scores_for_sample.append(sc)
        for i, p in enumerate(percentiles):
            scores[p].append(sc[i])

    for p in percentiles:
        scores[p] = numpy.array(scores[p])

    order = numpy.argsort(scores[50])

    r = Report()
    attach_description(r, description.format(var=variable, percentiles=",".join(map(str, percentiles))))
    with r.data_pylab("%s_percentiles" % variable.id) as pylab:
        # print percentiles
        # plot horizontal
        for i, p in enumerate(percentiles):
            # print p, scores[p][order]
            pylab.plot(scores[p][order], "%sx--" % colors[i])

        # plot vertical for each sample
        for x, index in enumerate(order):
            sample_scores = scores_for_sample[index]

            for i in range(0, len(sample_scores) - 1):
                y0 = sample_scores[i]
                y1 = sample_scores[i + 1]
                col = "%s-" % vcolors[i]
                # print x, y0, y1
                pylab.plot([x, x], [y0, y1], col)

        pylab.axis([-0.5, len(order) - 0.5, variable.interesting[0], variable.interesting[1]])

        pylab.ylabel("%s (%s)" % (variable.name, variable.unit))

        pylab.xlabel("sample")

    return r
예제 #17
0
def raw_theta_hist(sample, exp_data): #@UnusedVariable
    thetas = numpy.fmod(exp_data[:]['orientation'] + 360, 360)    
        
    r = Report()
    attach_description(r, description)
    with r.data_pylab('simulated_trajectory') as pylab:
        pylab.hist(thetas, bins=90, normed=True) 
        pylab.xlabel('orientation (degrees)') 
        pylab.ylabel('density')
        # TODO: choose ymax
        a = pylab.axis()
        pylab.axis([0, 360, 0, a[3]])

    return r
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
예제 #19
0
def group_sign_xcorr(group, configuration, saccades): #@UnusedVariable
    r = Report()
    attach_description(r, description)

    with r.data_pylab('sign_xcorr') as pylab:
        for sample, saccades_for_sample in iterate_over_samples(saccades): #@UnusedVariable
            sign = saccades_for_sample['sign']
            sign = sign.astype('float32')
            xc, lags = xcorr(sign, maxlag=10)
            pylab.plot(lags, xc, 'x-')
        pylab.axis([-10, 10, -0.5, 1.1])
        pylab.ylabel('cross-correlation')
        pylab.xlabel('interval in sequence')
    
    return r
예제 #20
0
def group_var_xcorr(group, configuration, saccades, variable): #@UnusedVariable
    r = Report()
    attach_description(r, description.format(var=variable))

    with r.data_pylab('%s_xcorr' % variable.id) as pylab:
        for sample, saccades_for_sample in iterate_over_samples(saccades): #@UnusedVariable
            x = saccades_for_sample[variable.field]
            x = x.astype('float32')
            xc, lags = xcorr(x, maxlag=10)
            pylab.plot(lags, xc, 'x-')
        pylab.axis([-10, 10, -0.5, 1.1])
        pylab.ylabel('cross-correlation')
        pylab.xlabel('interval in sequence')
    
    return r
예제 #21
0
def group_saccade_count(group, configuration, saccades): #@UnusedVariable
    r = Report()
    attach_description(r, description)
    
    stats = []
    for sample, saccades_for_sample in iterate_over_samples(saccades): #@UnusedVariable
        stats.append(len(saccades_for_sample))

    N = len(stats)
    with r.data_pylab('saccade_count') as pylab:
        R = range(N)
        pylab.bar(R, stats)
        pylab.ylabel('number of saccades')
        
        pylab.axis([0, N, 0, 4000])
    return r
예제 #22
0
def main():
    data = yaml.load(sys.stdin)
    
    r  = Report('plot_activities')
    
    with r.data_pylab('activities') as pylab:
        for activity, stats in data.items():
            ndays = max(stats.keys()) + 1
            accum = np.zeros(ndays)
            for day, amount in stats.items():
                accum[day] = amount
            
            x = range(ndays)
            pylab.plot(x, accum, label=activity)
        pylab.legend()
    r.to_html('out/plots.html')
예제 #23
0
def group_saccade_density(group, configuration, saccades): #@UnusedVariable
    r = Report()
    attach_description(r, description)
    stats = []
    for sample, saccades_for_sample in iterate_over_samples(saccades): #@UnusedVariable
        T = saccades_for_sample['time_start']
        length = T[-1] - T[0]
        stat = len(saccades_for_sample) / length
        stats.append(stat)

    N = len(stats)
    with r.data_pylab('saccade_density') as pylab:
        R = range(N)
        pylab.bar(R, stats)
        pylab.ylabel('saccade density (saccades/s)')
        pylab.axis([0, N, 0, 3.0])
    return r
예제 #24
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)
def create_report_subset(id, desc, saccades):
    report = Report('subset_' + id)
    report.text('description', '''

Subset: %s

It contains %d saccades.

''' % (desc, len(saccades)))
 
    saccade_angle = saccades['saccade_angle']
    approach_angle = saccades['approach_angle']
           
    with report.data_pylab('distance_from_center') as pylab:
        distance = saccades['distance_from_center']
        pylab.hist(distance, 100)
        pylab.xlabel('meters')
        pylab.ylabel('number of saccades')
        pylab.title('Distance from center (%s)' % id)
        a = pylab.axis()
        pylab.axis([0, 1, 0, a[3]])
        
    with report.data_pylab('saccade_angle') as pylab:
        pylab.hist(saccade_angle, range(-180, 185, 5))
        pylab.xlabel('degrees')
        pylab.ylabel('number of saccades')
        pylab.title('Saccade angle (%s)' % id)
        a = pylab.axis()
        pylab.axis([-180, 180, 0, a[3]])
    
    with report.data_pylab('approach_angle') as pylab:
        pylab.hist(approach_angle, range(-60, 65, 5))
        pylab.xlabel('degrees')
        pylab.ylabel('number of saccades')
        pylab.title('Approach angle (%s)' % id)
        a = pylab.axis()
        pylab.axis([-60, 60, 0, a[3]])
    
    with report.data_pylab('approach_vs_saccade') as pylab:
        pylab.plot(approach_angle, saccade_angle, '.')
        pylab.xlabel('approach angle (deg)')
        pylab.ylabel('saccade angle (deg)')
        pylab.title('Approach vs saccade angle (%s)' % id)
        a = pylab.axis()
        pylab.axis([-60, 60, -180, 180])
    
    
    
    
    # compute probabili
    approach, probability_left = \
        compute_turning_probability(approach_angle=approach_angle,
                                    saccade_angle=saccade_angle)
    
    probability_right = -probability_left + 1
    
    with report.data_pylab('turning_probability') as pylab:
        pylab.plot(approach, probability_left, 'gx-', label='left')
        pylab.plot(approach, probability_right, 'rx-', label='right')
        pylab.xlabel('approach angle (deg)')
        pylab.ylabel('probability of turning')
        pylab.title('Probability of turning (%s)' % id)
        a = pylab.axis()
        pylab.plot([0, 0], [0.2, 0.8], 'k--')
        pylab.axis([-60, 60, 0, 1])
        pylab.legend()
 
    bin_size = 10
    saccade_bin_centers = np.array(range(-180, 185, bin_size))
    n = len(saccade_bin_centers)
    saccade_bins = np.zeros(shape=(n + 1))
    saccade_bins[0:n] = saccade_bin_centers - bin_size
    saccade_bins[n] = saccade_bin_centers[-1]
    
    bin_centers = np.array(range(-50, 55, 10))
    bin_size = 15
    distributions = []
    for angle in bin_centers:
        indices, = np.nonzero(
                    np.logical_and(
                        approach_angle > angle - bin_size,
                        approach_angle < angle + bin_size
                    ))
        x = saccade_angle[indices]
        hist, edges = np.histogram(x, bins=saccade_bins, normed=True) #@UnusedVariable

        distributions.append(hist)
 
    with report.data_pylab('distribution_vs_approach2') as pylab:
        for k in range(len(bin_centers)):
            label = '%d' % bin_centers[k]
            pylab.plot(saccade_bin_centers, distributions[k], '-', label=label)
        #a = pylab.axis()
         
        pylab.legend()
        pylab.xlabel('saccade angle')
        pylab.ylabel('density')
 
    with report.data_pylab('distribution_vs_approach', figsize=(8, 20)) as pylab:
        # get the maximum density
        # max_density = max(map(max, distributions))
        num_plots = len(bin_centers)
        for k in range(num_plots):
            rect = [0.1, k * 1.0 / num_plots, 0.8, 1.0 / num_plots]
            pylab.axes(rect)
            label = '%d' % bin_centers[k]
            pylab.plot(saccade_bin_centers, distributions[k], '-', label=label)
            # pylab.axis([-180, 180, 0, max_density])
        #a = pylab.axis()
            pylab.legend()
        pylab.xlabel('saccade angle')
        pylab.ylabel('density') 
        
    
    f = report.figure(shape=(3, 3))
    f.sub('distance_from_center', caption='Distance from center')
    f.sub('saccade_angle', caption='Saccade angle')
    f.sub('approach_angle', caption='Approach angle')
    f.sub('approach_vs_saccade', caption='Approach vs saccade angle')
    f.sub('turning_probability', caption='Probability of turning')
    f.sub('distribution_vs_approach', caption='Saccade distribution vs approach angle')
    
    return report
예제 #26
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
예제 #27
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)



예제 #28
0
def create_report_subset(id, desc, saccades):
    report = Report('subset_' + id)
    report.text('description', '''%s\n%d saccades total.''' % (desc, len(saccades)))
    
    #f = report.figure(cols=3)
 
    saccade_angle = saccades['saccade_angle']
    approach_angle = saccades['approach_angle']
           
    with report.data_pylab('distance_from_center') as pylab:
        distance = saccades['distance_from_center']
        pylab.hist(distance, 100)
        pylab.xlabel('meters')
        pylab.ylabel('number of saccades')
        pylab.title('Distance from center (%s)' % id)
        a = pylab.axis()
        pylab.axis([0, 1, 0, a[3]])
        
    #report.last().add_to(f)
    
    with report.data_pylab('distance_from_wall') as pylab:
        distance = saccades['distance_from_wall']
        pylab.hist(distance, 100)
        pylab.xlabel('meters')
        pylab.ylabel('number of saccades')
        pylab.title('Distance from center (%s)' % id)
        a = pylab.axis()
        pylab.axis([0, 1, 0, a[3]])
        
    #report.last().add_to(f)
  
    with report.data_pylab('saccade_angle') as pylab:
        pylab.hist(saccade_angle, range(-180, 185, 5))
        pylab.xlabel('degrees')
        pylab.ylabel('number of saccades')
        pylab.title('Saccade angle (%s)' % id)
        a = pylab.axis()
        pylab.axis([-180, 180, 0, a[3]])
    
    #  report.last().add_to(f)
  
    with report.data_pylab('approach_angle') as pylab:
        pylab.hist(approach_angle, range(-60, 65, 5))
        pylab.xlabel('degrees')
        pylab.ylabel('number of saccades')
        pylab.title('Approach angle (%s)' % id)
        a = pylab.axis()
        pylab.axis([-60, 60, 0, a[3]])
    
    #  report.last().add_to(f)
  
    with report.data_pylab('approach_vs_saccade') as pylab:
        pylab.plot(approach_angle, saccade_angle, '.')
        pylab.xlabel('approach angle (deg)')
        pylab.ylabel('saccade angle (deg)')
        pylab.title('Approach vs saccade angle (%s)' % id)
        a = pylab.axis()
        pylab.axis([-60, 60, -180, 180])
    
    #  report.last().add_to(f)
  
    
    # compute probability
    approach, probability_left, probability_right, margin_left, margin_right = \
        compute_turning_probability(approach_angle=approach_angle,
                                    saccade_angle=saccade_angle)
    with report.data_pylab('turning_probability') as pylab:
        n = len(approach)
        el = np.zeros((2,n))
        el[0,:] = +(margin_left[0,:]-probability_left)
        el[1,:] = -(margin_left[1,:]-probability_left)
        pylab.errorbar(approach, probability_left, el,None, None,
                       ecolor='g', label='left',  capsize=8, elinewidth=1)
        er = np.zeros((2,n))
        er[0,:] = +(margin_right[0,:]-probability_right)
        er[1,:] = -(margin_right[1,:]-probability_right)
        pylab.errorbar(approach, probability_right, er, None, None,
                       ecolor='r', label='right',   capsize=8, elinewidth=1)

        pylab.plot(approach, probability_left, 'g-', label='left')
        pylab.plot(approach, probability_right, 'r-', label='right')
        pylab.xlabel('approach angle (deg)')
        pylab.ylabel('probability of turning')
        pylab.title('Probability of turning (%s)' % id)
        a = pylab.axis()
        pylab.plot([0, 0], [0.2, 0.8], 'k--')
        pylab.axis([-60, 60, 0, 1])
        pylab.legend()
 
    #   report.last().add_to(f)
 
    bin_size = 10
    saccade_bin_centers = np.array(range(-180, 185, bin_size))
    n = len(saccade_bin_centers)
    saccade_bins = np.zeros(shape=(n + 1))
    saccade_bins[0:n] = saccade_bin_centers - bin_size
    saccade_bins[n] = saccade_bin_centers[-1]
    
    bin_centers = np.array(range(-50, 55, 5))
    bin_size = 15
    distributions = []
    for angle in bin_centers:
        indices, = np.nonzero(
                    np.logical_and(
                        approach_angle > angle - bin_size,
                        approach_angle < angle + bin_size
                    ))
        x = saccade_angle[indices]
        if len(indices) > 0:
            # Otherwise histogram divides by 0
            hist, edges = np.histogram(x, bins=saccade_bins, normed=True) #@UnusedVariable
        else:
            hist, edges = np.histogram(x, bins=saccade_bins, normed=False) #@UnusedVariable
        
        distributions.append(hist)
 
    with report.data_pylab('distribution_vs_approach2') as pylab:
        for k in range(len(bin_centers)):
            label = '%d' % bin_centers[k]
            pylab.plot(saccade_bin_centers, distributions[k], '-', label=label)
        #a = pylab.axis()
         
        pylab.legend()
        pylab.xlabel('saccade angle')
        pylab.ylabel('density')
 
    with report.data_pylab('distribution_vs_approach', figsize=(8, 20)) as pylab:
        # get the maximum density
        # max_density = max(map(max, distributions))
        num_plots = len(bin_centers)
        for k in range(num_plots):
            rect = [0.1, k * 1.0 / num_plots, 0.8, 1.0 / num_plots]
            pylab.axes(rect)
            label = '%d' % bin_centers[k]
            pylab.plot(saccade_bin_centers, distributions[k], '-', label=label)
            # pylab.axis([-180, 180, 0, max_density])
        #a = pylab.axis()
            pylab.legend()
        pylab.xlabel('saccade angle')
        pylab.ylabel('density')         
    
    f = report.figure(cols=3)
    f.sub('distance_from_center', caption='Distance from center')
    f.sub('saccade_angle', caption='Saccade angle')
    f.sub('approach_angle', caption='Approach angle')
    f.sub('approach_vs_saccade', caption='Approach vs saccade angle')
    f.sub('turning_probability', caption='Probability of turning')
    f.sub('distribution_vs_approach2', caption='Saccade distribution vs approach angle')
    f.sub('distribution_vs_approach', caption='Saccade distribution vs approach angle')
    
    return report
예제 #29
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 plot_detected_saccades(sample, exp_data, configuration, saccades):
    thetas = exp_data[:]['orientation']
    T = exp_data[:]['timestamp']
    
    r = Report()
    attach_description(r, description)
    
    f = r.figure(cols=2, caption='Detected saccades')
    
    dt = T[1] - T[0]
    chunk_length = 15 # seconds
    
    
    chunk_size = numpy.ceil(chunk_length / dt)
    num_chunks = int(numpy.ceil(len(T) / chunk_size))

    # oopsi, we start from 0 in the saccades
    # FIXME: detect this
    if len(saccades) and saccades[0]['time_start'] < 100000:
        print "Fixing saccades timestamp for sample %r, configuration %r" % (sample, configuration)
        saccades = numpy.array(saccades, dtype=saccades.dtype)
        for i in range(len(saccades)):
            saccades[i]['time_start'] += T[0]
            saccades[i]['time_stop'] += T[0]

    for i in range(num_chunks):
        start = i * chunk_size
        stop = min(start + chunk_size, len(T))
        
        if stop - start < 10:
            continue 
        
        theta_i = thetas[start:stop]
        T_i = T[start:stop]
        
        T_norm = T_i - T[0]
        
        caption = 'Chunk %d (from time %.1f to %.1f)' % (i, T_norm[0], T_norm[-1])
        node_id = 'chunk%d' % i
        
        s = 2.5
        with r.data_pylab(node_id, figsize=(8 * s, 1.5 * s)) as pylab:
            pylab.plot(T_norm, theta_i, 'k-')
            
            pylab.xlabel('time (s)')
            pylab.ylabel('orientation (deg)')
            #a = pylab.axis()
                       
            interval = 90 # deg
            ub = int(numpy.ceil(max(theta_i) / interval))
            lb = int(numpy.floor(min(theta_i) / interval))
            if ub == lb:
                lb = ub - 1
            a = [T_norm[0], T_norm[-1], lb * interval - 15, ub * interval + 15]
            
            for k in range(lb, ub + 1):
                line_theta = k * interval
                pylab.plot([a[0], a[1]], [line_theta, line_theta], 'k--')
    
    
    
    
            in_range = numpy.logical_and(saccades[:]['time_start'] >= T_i[0],
                                         saccades[:]['time_start'] <= T_i[-1])
            
            for saccade in saccades[in_range]: 
                tstart = saccade['time_start'] - T[0]
                tstop = saccade['time_stop'] - T[0]
                
                pylab.plot([tstart, tstart], [a[2], a[3]], 'b-')
                pylab.plot([tstop, tstop], [a[2], a[3]], 'g-')
                 
    
            
            pylab.axis(a)
        
        f.sub(node_id, caption=caption)
    
    
    # r.table('saccades', saccades)
    
    return r
예제 #31
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)
예제 #32
0
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
예제 #33
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)