def render_page(view2result, outdir, page_id):

    def iterate_views():
        for view in views: 
            yield view, view2result[view.id]
            
    # first compute max value
    mean_max = max(map(lambda x: numpy.max(x[1].mean), iterate_views()))
    var_max = max(map(lambda x: numpy.max(x[1].var), iterate_views()))
             
    n = Report(page_id)
    f = n.figure(cols=3)
    for view, stats in iterate_views():
        nv = n.node(view.id)
        add_scaled(nv, 'mean', stats.mean, max_value=mean_max)
        add_scaled(nv, 'var', stats.var, max_value=var_max)
        #add_scaled(nv, 'min', stats.min)
        #add_scaled(nv, 'max', stats.max)
    
    for view in views:
        what = 'mean'
    #for what, view in prod(['mean', 'var'], views):
        f.sub('%s/%s' % (view.id, what),
              caption='%s (%s)' % (view.desc, what))
    
    output_file = os.path.join(outdir, '%s.html' % n.id)
    resources_dir = os.path.join(outdir, 'images')
    print "Writing to %s" % output_file
    n.to_html(output_file, resources_dir=resources_dir)
Ejemplo n.º 2
0
class ReprepPublisher(Publisher):

    default_max_cols = 5

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

        self.cols = cols
        self._f = None

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

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

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

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

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

    def section(self, section_name, cols=default_max_cols, caption=None):
        child = self.r.node(section_name, caption=caption)
        return ReprepPublisher(report=child, cols=cols)
def create_and_write_report(flydra_db, sample, image_name):
    
    view_start = 'saccades_view_start_%s' % image_name
    view_stop = 'saccades_view_stop_%s' % image_name
    view_rstop = 'saccades_view_rstop_%s' % image_name    


    db = FlydraDB(flydra_db, False) 
    
    saccades = db.get_saccades(sample)
    values_start = db.get_table(sample, view_start)
    values_stop = db.get_table(sample, view_stop)
    values_rstop = db.get_table(sample, view_rstop)
    
    
    r = Report(sample)
    
    for i in range(len(saccades)):
        
        ri = r.node("saccade-%04d" % i)
        
        ri.data_rgb('start', plot_luminance(values_start[i]['value']))
        ri.data_rgb('stop', plot_luminance(values_stop[i]['value']))
        ri.data_rgb('rstop', plot_luminance(values_rstop[i]['value']))
        
        f = ri.figure(shape=(1, 3))
        f.sub('start', 'At saccade start')
        f.sub('stop', 'At saccade stop')
        #f.sub('rstop', '(random stop)')


    db.release_table(saccades)
    db.release_table(values_start)
    db.release_table(values_stop)
    db.release_table(values_rstop)
    

    filename = "%s/out/saccade_view_show/%s_%s.html" % (flydra_db, image_name, sample)
    print "Writing to %s" % filename
    r.to_html(filename)
Ejemplo n.º 4
0
def main():
    if not vehicles_has_cairo:
        logger.error('This program cannot be run if Cairo is not installed.')
        return
    from vehicles_cairo import (vehicles_cairo_display_pdf,
                                vehicles_cairo_display_png,
                                vehicles_cairo_display_svg)

    parser = OptionParser(usage=usage)
    parser.disable_interspersed_args()

    parser.add_option("--vehicle",
                      default='d_SE2_rb_v-rf360',
                      help="ID vehicle [%default].")
    parser.add_option("--world",
                      default='stochastic_box_10',
                      help="ID world [%default].")
    parser.add_option("-n",
                      default=1,
                      type='int',
                      help="number of simulations [%default].")
    parser.add_option("--outdir",
                      "-o",
                      default='display_demo',
                      help="output directory [%default]")
    parser.add_option("--figsize",
                      default=10,
                      type='float',
                      help="figsize (inches) [%default]")
    parser.add_option("-z",
                      "--zoom",
                      default=0,
                      type='float',
                      help="zoom in meters; 0 for full view [%default]")
    parser.add_option("-g",
                      "--grid",
                      default=1,
                      type='float',
                      help="grid size in meters; 0 for no grid [%default]")

    parser.add_option("--cairo", default=False, action='store_true')

    parser.add_option("--seed", default=None, type='int')

    (options, args) = parser.parse_args()
    if args:
        raise Exception()

    id_vehicle = options.vehicle
    id_world = options.world

    logger.info('id_vehicle: %s' % id_vehicle)
    logger.info('  id_world: %s' % id_world)

    if options.seed is None:
        options.seed = np.random.randint(1000000)

    np.random.seed(seed=options.seed)
    logger.info('Using seed %s (your lucky number is %s)' %
                (options.seed, np.random.randint(1000)))

    vehicle = VehiclesConfig.vehicles.instance(
        id_vehicle)  # @UndefinedVariable
    world = VehiclesConfig.worlds.instance(id_world)  # @UndefinedVariable

    simulation = VehicleSimulation(vehicle, world)

    from reprep import Report, MIME_PDF
    basename = 'display-%s-%s' % (id_vehicle, id_world)
    r = Report(basename)
    r.text('seed', 'Seed = %s' % options.seed)
    for i in range(options.n):
        sec = r.node('simulation%d' % i)
        f = sec.figure()

        simulation.new_episode()
        simulation.compute_observations()

        sim_state = simulation.to_yaml()

        plot_params = dict(grid=options.grid,
                           zoom=options.zoom,
                           show_sensor_data=True)
        #            with f.plot('start', figsize=(options.figsize,
        #                                                options.figsize)) as pylab:
        #                    display_all(pylab, sim_state, **plot_params)

        with f.data_file('start_cairo_png', MIME_PNG) as filename:
            vehicles_cairo_display_png(filename,
                                       width=800,
                                       height=800,
                                       sim_state=sim_state,
                                       **plot_params)

        with f.data_file('start_cairo_pdf', MIME_PDF) as filename:
            vehicles_cairo_display_pdf(filename,
                                       width=800,
                                       height=800,
                                       sim_state=sim_state,
                                       **plot_params)

        with f.data_file('start_cairo_svg', MIME_SVG) as filename:
            vehicles_cairo_display_svg(filename,
                                       width=800,
                                       height=800,
                                       sim_state=sim_state,
                                       **plot_params)

    filename = os.path.join(options.outdir, 'index.html')
    logger.info('Writing to %r.' % filename)
    r.to_html(filename)
Ejemplo n.º 5
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
def las_vegas_report(outdir, page_id, results):
    # threshold for considering 0 response
    #eps = 0.0001
    # eps = 0.001
    # eps = 0
    
    r = Report('lasvegas_' + page_id)    
    f = r.figure('summary', cols=4, caption='Response to various filters')
    f_overlap = r.figure('summary-overlap', cols=4,
                         caption='Response area (overlap) of various filters')
    
    kernels = sorted(results.keys())
    for kernel in kernels:
        sign = results[kernel]['signs']
        response = results[kernel]['response']
        # overlap = results[kernel]['overlap']
        overlap = numpy.abs(response)
        
        eps = percentile(overlap, 75)
        
        matched_filter = results[kernel]['kernel']
        
        left = numpy.nonzero(sign == +1)
        right = numpy.nonzero(sign == -1)
        
        response_right = response[right]
        response_left = response[left]
         
        
        n = r.node(kernel)
        
        with n.data_pylab('response') as pylab:

            try:
                b = numpy.percentile(response_left, 95) #@UndefinedVariable
            except:
                b = scipy.stats.scoreatpercentile(response_left, 95)
                        
            def plothist(x, nbins, eps, **kwargs):
                nz, = numpy.nonzero(numpy.abs(x) > eps)
                # x with nonzero response
                print "using %d/%d" % (len(nz), len(x))
                xnz = x[nz]
                hist, bin_edges = numpy.histogram(xnz, range=(-b, b), bins=nbins)
                bins = (bin_edges[:-1] + bin_edges[1:]) * 0.5
                pylab.plot(bins, hist, **kwargs)
                
            nbins = 500
            plothist(response_left, nbins, eps, label='left')
            plothist(response_right, nbins, eps, label='right')

            a = pylab.axis()
            pylab.axis([-b, b, 0, a[-1]])
            pylab.legend()
            
        f.sub('%s/response' % kernel)
        
        with n.data_pylab('overlap') as pylab:

            def plothist2(x, nbins, **kwargs):
                hist, bin_edges = numpy.histogram(x, bins=nbins)
                bins = (bin_edges[:-1] + bin_edges[1:]) * 0.5
                pylab.plot(bins, hist, **kwargs)
                
            nbins = 200
            
            # plothist2(overlap, nbins, label='overlap')
            pylab.hist(overlap, nbins, log=True, label='hist of abs.response')

            a = pylab.axis()
            pylab.plot([ eps, eps], [a[2], a[3]], 'r-', label='threshold')
            #pylab.axis([-b, b, 0, a[-1]])
            pylab.legend()
            
        f_overlap.sub('%s/overlap' % kernel)
        
        def ratio2perc(i, n):
            p = 100.0 * i / n
            return "%.1f" % p
        
        def perc(x):
            pos, = numpy.nonzero(x)
            return ratio2perc(len(pos), len(x))
        
        cols = ['probability', 'no response', 'guessed L', 'guessed R']
        rows = ['left saccade', 'right saccade']
        table = [
            [ perc(sign == +1), perc(numpy.abs(response_left) < eps),
                              perc(response_left > eps),
                              perc(response_left < -eps) ],
            [ perc(sign == -1), perc(numpy.abs(response_right) < eps),
                              perc(response_right > eps),
                              perc(response_right < -eps) ],
        ]
        
        n.table('performance', data=table, rows=rows, cols=cols)
        
        use_eps = eps
        total = len(sign)
        given = numpy.abs(response) > use_eps
        num_given = len(numpy.nonzero(given)[0])
        correct = numpy.logical_or(
                numpy.logical_and(response > use_eps, sign == +1),
                numpy.logical_and(response < -use_eps, sign == -1)
        )
        
        num_correct = len(numpy.nonzero(correct)[0])
        
        perc_given = ratio2perc(num_given, total)
        perc_not_given = ratio2perc(len(sign) - num_given, len(sign))
        
    #    perc_correct_abs =  ratio2perc(num_correct, total) 
        
        perc_correct_given = ratio2perc(num_correct, num_given)
        
        signif = 0.01
        expected = \
            scipy.stats.binom.ppf([signif / 2, 1 - signif / 2], num_given, 0.5) / num_given
        #cdf = scipy.stats.binom.cdf(perc_correct_given, num_given, 0.5)
        pvalue = binom_test(num_correct, num_given, 0.5)
        
        cols = ['no response', 'with response',
                'correct (%given)', 'p-value', 'bounds under H0']
        table = [
            [ perc_not_given, perc_given,
               perc_correct_given,
              "%.4f" % pvalue,
              "[%.1f, %.1f]" % (100 * expected[0], 100 * expected[1]) ],
              
        ]
        
        n.table('performance2', data=table, cols=cols)
       
        
        add_posneg(n, 'kernel', matched_filter)
        
       
    output_file = os.path.join(outdir, '%s.html' % r.id)
    resources_dir = os.path.join(outdir, 'images')
    print("Writing to %s" % output_file)
    r.to_html(output_file, resources_dir=resources_dir) 
Ejemplo n.º 7
0
def main():
    if not vehicles_has_cairo:
        logger.error('This program cannot be run if Cairo is not installed.')
        return
    from vehicles_cairo import (vehicles_cairo_display_pdf,
                                vehicles_cairo_display_png,
                                vehicles_cairo_display_svg)

    parser = OptionParser(usage=usage)
    parser.disable_interspersed_args()

    parser.add_option("--vehicle", default='d_SE2_rb_v-rf360',
                      help="ID vehicle [%default].")
    parser.add_option("--world", default='stochastic_box_10',
                       help="ID world [%default].")
    parser.add_option("-n", default=1, type='int',
                    help="number of simulations [%default].")
    parser.add_option("--outdir", "-o", default='display_demo',
                    help="output directory [%default]")
    parser.add_option("--figsize", default=10, type='float',
                    help="figsize (inches) [%default]")
    parser.add_option("-z", "--zoom", default=0, type='float',
                    help="zoom in meters; 0 for full view [%default]")
    parser.add_option("-g", "--grid", default=1, type='float',
                    help="grid size in meters; 0 for no grid [%default]")

    parser.add_option("--cairo", default=False, action='store_true')

    parser.add_option("--seed", default=None, type='int')

    (options, args) = parser.parse_args()
    if args:
        raise Exception()

    id_vehicle = options.vehicle
    id_world = options.world

    logger.info('id_vehicle: %s' % id_vehicle)
    logger.info('  id_world: %s' % id_world)

    if options.seed is None:
        options.seed = np.random.randint(1000000)

    np.random.seed(seed=options.seed)
    logger.info('Using seed %s (your lucky number is %s)' % 
                (options.seed, np.random.randint(1000)))

    vehicle = VehiclesConfig.vehicles.instance(id_vehicle)  # @UndefinedVariable
    world = VehiclesConfig.worlds.instance(id_world)  # @UndefinedVariable

    simulation = VehicleSimulation(vehicle, world)

    from reprep import Report, MIME_PDF
    basename = 'display-%s-%s' % (id_vehicle, id_world)
    r = Report(basename)
    r.text('seed', 'Seed = %s' % options.seed)
    for i in range(options.n):
        sec = r.node('simulation%d' % i)
        f = sec.figure()

        simulation.new_episode()
        simulation.compute_observations()

        sim_state = simulation.to_yaml()

        plot_params = dict(grid=options.grid,
                           zoom=options.zoom, show_sensor_data=True)
#            with f.plot('start', figsize=(options.figsize,
#                                                options.figsize)) as pylab:
#                    display_all(pylab, sim_state, **plot_params)

        with f.data_file('start_cairo_png', MIME_PNG) as filename:
            vehicles_cairo_display_png(filename, width=800, height=800,
                        sim_state=sim_state, **plot_params)

        with f.data_file('start_cairo_pdf', MIME_PDF) as filename:
            vehicles_cairo_display_pdf(filename, width=800, height=800,
                        sim_state=sim_state, **plot_params)

        with f.data_file('start_cairo_svg', MIME_SVG) as filename:
            vehicles_cairo_display_svg(filename, width=800, height=800,
                        sim_state=sim_state, **plot_params)

    filename = os.path.join(options.outdir, 'index.html')
    logger.info('Writing to %r.' % filename)
    r.to_html(filename)
Ejemplo n.º 8
0
def inspect_textures():
    parser = OptionParser(usage=usage)
    parser.disable_interspersed_args()

    parser.add_option("-w", "--world", default='stochastic_box_10',
                       help="ID world [%default].")

    parser.add_option("-n", default=1, type='int',
                    help="number of simulations [%default].")
    parser.add_option("--outdir", "-o", default='out-inspect_textures',
                    help="output directory [%default]")
    parser.add_option("--figsize", default=1, type='float',
                    help="figsize (inches) [%default]")
    parser.add_option("-z", "--zoom", default=0, type='float',
                    help="zoom in meters; 0 for full view [%default]")

    (options, args) = parser.parse_args()
    if args:
        raise Exception()  # XXX

    id_world = options.world

    logger.info('  id_world: %s' % id_world)

    world = VehiclesConfig.worlds.instance(id_world)  # @UndefinedVariable

    from reprep import Report
    basename = 'inspect_textures-%s' % (id_world)
    r = Report(basename)

    for i in range(options.n):
        sec = r.node('simulation%d' % i)
        world.new_episode()
        primitives = world.get_primitives()
        for p in primitives:
            psec = sec.node('%s_%d' % (p.__class__.__name__, p.id_object))
            psec.text('object', pformat(p.to_yaml()))

            f = psec.figure(cols=2)
            if not isinstance(p, GeometricShape):
                continue

            perimeter = p.get_perimeter()
            texture = instantiate_spec(p.texture)

            chunk_size = 10
            nchunks = np.ceil(perimeter * 1.0 / chunk_size)

            for c in range(int(nchunks)):
                xfrom = c * chunk_size
                xto = np.minimum(perimeter, xfrom + chunk_size)
                N = 1000
                x = np.linspace(xfrom, xto, N)

                lum = texture(x)

                with f.plot('chunk%s' % c,
                              figsize=(options.figsize * 10,
                                      options.figsize)) as pylab:
                    pylab.plot(x, lum)
                    pylab.axis((xfrom, xfrom + chunk_size, -0.1, 1.1))

#
#        if False:
#            with f.data_pylab('pdf', mime=MIME_PDF,
#                              figsize=(options.figsize,
#                                      options.figsize)) as pylab:
#                    display_all(pylab, sim_state, grid=1, zoom=0, 
# show_sensor_data = True)
#            f.last().add_to(f)

    filename = os.path.join(options.outdir, 'index.html')
    logger.info('Writing to %r.' % filename)
    r.to_html(filename)
Ejemplo n.º 9
0
def main():
    from vehicles_cairo import vehicles_has_cairo
    if not vehicles_has_cairo:
        logger.error('This program cannot be run if Cairo is not installed.')
        return

    from vehicles_cairo import (vehicles_cairo_display_pdf,
                                vehicles_cairo_display_png,
                                vehicles_cairo_display_svg,
                                cairo_plot_circle2)

    parser = OptionParser(usage=usage)
    parser.disable_interspersed_args()

    parser.add_option("--vehicles", default='*',
                      help="ID vehicle [%default].")
    parser.add_option("--world", default='SBox2_10a',
                      # default='empty_fixed',
                       help="ID world [%default].")
    parser.add_option("--outdir", "-o",
                      default='vehicles_demo_display_vehicles',
                    help="output directory [%default]")
    parser.add_option("--figsize", default=10, type='float',
                    help="figsize (inches) [%default]")
    parser.add_option("-g", "--grid", default=1, type='float',
                    help="grid size in meters; 0 for no grid [%default]")
    parser.add_option("-d", dest="config", default=".",
                      help="Config directory")
    parser.add_option("--scale", default=False, action='store_true',
                    help="If given, displays the scale with a red circle")

    (options, args) = parser.parse_args()
    if args:
        raise Exception()  # XXX

    id_world = options.world

    logger.info('  id_world: %s' % id_world)

    from reprep import Report, MIME_PDF
    basename = 'vehicles_demo'
    r = Report(basename)

    logger.info('Loading configuration from %s' % options.config)
    VehiclesConfig.load(options.config)

    # TODO: selection
    all_vehicles = VehiclesConfig.vehicles.keys()
    if options.vehicles is None:
        vehicles = all_vehicles
    else:
        vehicles = expand_string(options.vehicles, all_vehicles)

    print('Plotting vehicles: %s' % vehicles)

    f0 = r.figure(cols=6)
    f0_data = r.figure(cols=6)

    for id_vehicle in sorted(vehicles):
        sec = r.node(id_vehicle)
        f = sec.figure(cols=6)

        world = VehiclesConfig.specs['worlds'].instance(id_world)
        vehicle = VehiclesConfig.specs['vehicles'].instance(id_vehicle)
        simulation = VehicleSimulation(vehicle, world)
        simulation.new_episode()
        simulation.compute_observations()
        sim_state = simulation.to_yaml()

        def draw_scale(cr):
            if options.scale:
                cairo_plot_circle2(cr, 0, 0,
                               vehicle.radius, fill_color=(1, .7, .7))

        plot_params = dict(grid=options.grid,
                           zoom=1.5,
                           zoom_scale_radius=True,
                           width=500, height=500,
                           show_sensor_data=True,
                           show_sensor_data_compact=True,
                           extra_draw_world=draw_scale,
                           bgcolor=None,
                           show_world=False)

        with f.data_file('png_with', MIME_PNG) as filename:
            vehicles_cairo_display_png(filename,
                        sim_state=sim_state, **plot_params)

        f0_data.sub(f.last(), caption=id_vehicle)

        plot_params['show_sensor_data'] = False
        with f.data_file('png_without', MIME_PNG) as filename:
            vehicles_cairo_display_png(filename,
                        sim_state=sim_state, **plot_params)

        f0.sub(f.last(), caption=id_vehicle)

        with f.data_file('svg', MIME_SVG) as filename:
            vehicles_cairo_display_svg(filename,
                        sim_state=sim_state, **plot_params)

        plot_params['grid'] = 0
        plot_params['extra_draw_world'] = None
        with sec.data_file('pdf', MIME_PDF) as filename:
            vehicles_cairo_display_pdf(filename,
                        sim_state=sim_state, **plot_params)

        plot_params['show_robot_body'] = True
        plot_params['show_robot_sensors'] = False
        with f.data_file('png_only_body', MIME_PNG) as filename:
            vehicles_cairo_display_png(filename,
                        sim_state=sim_state, **plot_params)

    filename = os.path.join(options.outdir, 'index.html')
    logger.info('Writing to %r.' % filename)
    r.to_html(filename)
Ejemplo n.º 10
0
def inspect_textures():
    parser = OptionParser(usage=usage)
    parser.disable_interspersed_args()

    parser.add_option("-w",
                      "--world",
                      default='stochastic_box_10',
                      help="ID world [%default].")

    parser.add_option("-n",
                      default=1,
                      type='int',
                      help="number of simulations [%default].")
    parser.add_option("--outdir",
                      "-o",
                      default='out-inspect_textures',
                      help="output directory [%default]")
    parser.add_option("--figsize",
                      default=1,
                      type='float',
                      help="figsize (inches) [%default]")
    parser.add_option("-z",
                      "--zoom",
                      default=0,
                      type='float',
                      help="zoom in meters; 0 for full view [%default]")

    (options, args) = parser.parse_args()
    if args:
        raise Exception()  # XXX

    id_world = options.world

    logger.info('  id_world: %s' % id_world)

    world = VehiclesConfig.worlds.instance(id_world)  # @UndefinedVariable

    from reprep import Report
    basename = 'inspect_textures-%s' % (id_world)
    r = Report(basename)

    for i in range(options.n):
        sec = r.node('simulation%d' % i)
        world.new_episode()
        primitives = world.get_primitives()
        for p in primitives:
            psec = sec.node('%s_%d' % (p.__class__.__name__, p.id_object))
            psec.text('object', pformat(p.to_yaml()))

            f = psec.figure(cols=2)
            if not isinstance(p, GeometricShape):
                continue

            perimeter = p.get_perimeter()
            texture = instantiate_spec(p.texture)

            chunk_size = 10
            nchunks = np.ceil(perimeter * 1.0 / chunk_size)

            for c in range(int(nchunks)):
                xfrom = c * chunk_size
                xto = np.minimum(perimeter, xfrom + chunk_size)
                N = 1000
                x = np.linspace(xfrom, xto, N)

                lum = texture(x)

                with f.plot('chunk%s' % c,
                            figsize=(options.figsize * 10,
                                     options.figsize)) as pylab:
                    pylab.plot(x, lum)
                    pylab.axis((xfrom, xfrom + chunk_size, -0.1, 1.1))


#
#        if False:
#            with f.data_pylab('pdf', mime=MIME_PDF,
#                              figsize=(options.figsize,
#                                      options.figsize)) as pylab:
#                    display_all(pylab, sim_state, grid=1, zoom=0,
# show_sensor_data = True)
#            f.last().add_to(f)

    filename = os.path.join(options.outdir, 'index.html')
    logger.info('Writing to %r.' % filename)
    r.to_html(filename)
def add_comparisons(all_experiments, outdir): 
        
    r = Report('comparisons')

    fmain = r.figure('summary', cols=3,
                     caption="Summary of projection analysis")

    n = r.node('odds analysis')
    fodds_real = n.figure('odds_real', cols=3,
                     caption="Summary of odds analysis (real)")
    fodds_hall = n.figure('odds_hall', cols=3,
                     caption="Summary of odds analysis (hall)")
    fodds_rel = n.figure('rel', cols=3, caption="Relative odds")

    generic = r.node('generic')
    if 1:
        key = ('posts', 'contrast_w')
        real_traj = all_experiments[key]
        key = ('noposts', 'hcontrast_w')
        hall_traj = all_experiments[key]
        diff = real_traj.mean - hall_traj.mean
        
        max_value = numpy.max(numpy.max(real_traj.mean),
                              numpy.max(hall_traj.mean))
        add_scaled(generic, 'real_traj', real_traj.mean, max_value=max_value)
        add_scaled(generic, 'hall_traj', hall_traj.mean, max_value=max_value)
        add_posneg(generic, 'diff_traj', diff)
        with generic.data_pylab('diff_traj_plot') as pylab:
            pylab.plot(real_traj.mean, 'b.', label='real')
            pylab.plot(hall_traj.mean, 'r.', label='hallucination')
        
        
        f = generic.figure(cols=2)
        f.sub('real_traj', caption='signal over all trajectory (real)')
        f.sub('hall_traj', caption='signal over all trajectory (hallucinated)')
        f.sub('diff_traj_plot', caption='real - hallucination')
        f.sub('diff_traj', caption='real - hallucination')
        
    for view, dir, saccades_set in \
        prod(['start' ],
             ['left', 'right', 'alldir'],
             ['allsac', 'center', 'border']):
        # statistics over the whole trajectory
        
        
        key = Exp(image='contrast_w', group='posts',
                  view=view, dir=dir, saccades_set=saccades_set)
        real = all_experiments[key]
        
        key = Exp(image='hcontrast_w', group='noposts',
                  view=view, dir=dir, saccades_set=saccades_set)
        hall = all_experiments[key]
         
        
        case = r.node('analysis_%s_%s_%s' % (view, dir, saccades_set))
        
        cased = " (%s, %s, %s)" % (dir, view, saccades_set)
        
        diff = real.mean - hall.mean

        max_value = numpy.max(numpy.max(real.mean),
                              numpy.max(hall.mean))
        add_scaled(case, 'real', real.mean, max_value=max_value)
        add_scaled(case, 'hall', hall.mean, max_value=max_value)
        add_posneg(case, 'diff', diff)
    
    
        max_value = numpy.max(numpy.max(real.var),
                              numpy.max(hall.var))
        add_scaled(case, 'real_var', real.var, max_value=max_value)
        add_scaled(case, 'hall_var', hall.var, max_value=max_value)
    
        
        real_minus_traj = real.mean - real_traj.mean
        hall_minus_traj = hall.mean - hall_traj.mean
        rmt_minus_hmt = numpy.maximum(0, real_minus_traj) - \
                        numpy.maximum(0, hall_minus_traj)
        M = numpy.max(numpy.abs(real_minus_traj).max(),
                      numpy.abs(hall_minus_traj).max())
        add_posneg(case, 'real_minus_traj', real_minus_traj, max_value=M)
        add_posneg(case, 'hall_minus_traj', hall_minus_traj, max_value=M)
        add_posneg(case, 'rmt_minus_hmt', rmt_minus_hmt)
        
        from scipy import polyfit
        (ar, br) = polyfit(real.mean, hall.mean, 1)
        case.text('case', 'Best linear fit: a = %f, b = %f.' % (ar, br))
        
        diffr = (ar * real.mean + br) - hall.mean
        add_posneg(case, 'diffr', diffr)
        
        diffn = diff / numpy.sqrt(hall.var)
        add_posneg(case, 'diffn', diffn)
        
        with case.data_pylab('diffn_plot') as pylab:
            pylab.plot(diffn, 'k.')
        
        with case.data_pylab('linear-fit') as pylab:
            pylab.plot(real.mean, hall.mean, '.')
            pylab.plot(real.mean, real.mean, 'k.')
            
            pylab.plot(real.mean, real.mean * ar + br, 'r.')
            pylab.axis('equal')

        with case.data_pylab('mean') as pylab:
            x = range(len(real.mean))
            pylab.plot(x, real.mean, 'b.', label='real')
            pylab.plot(x, hall.mean, 'r.', label='hallucinated')
            #yerr = numpy.sqrt(var_cwnd) * 2
            #pylab.errorbar(x, cwnd, yerr=yerr, color='k', label='hallucinated')
            pylab.legend()
            pylab.title('mean')

        with case.data_pylab('var') as pylab:
            x = range(len(real.var))
            pylab.plot(x, numpy.sqrt(real.var) * 2, 'b.', label='real')
            pylab.plot(x, numpy.sqrt(hall.var) * 2, 'r.', label='hallucinated')
            pylab.legend()
            pylab.title('variance')

    
        f = case.figure(cols=2)
        f.sub('real', caption='real response (mean)' + cased)
        f.sub('hall', caption='hallucinated response (mean)' + cased)
        f.sub('real_var', caption='real response (var)' + cased)
        f.sub('hall_var', caption='hallucinated response (var)' + cased)
        f.sub('mean', caption='mean comparison' + cased)
        f.sub('var', caption='variance comparison' + cased)
        f.sub('linear-fit', caption='Linear fit between the two (a=%f, b=%f)' 
              % (ar, br))
        f.sub('diff', caption='difference (real - hallucinated)' + cased)
        f.sub('diffr', caption='difference (real*a- hallucinated)' + cased)
        f.sub('diffn', caption='Normalized difference' + cased)
        f.sub('diffn_plot', caption='Normalized difference' + cased)
        f.sub('diffn_plot', caption='Normalized difference' + cased)
        
        f.sub('real_minus_traj',
              'Difference between saccading and trajectory (real)' + cased)
        f.sub('hall_minus_traj',
              'Difference between saccading and trajectory (hall)' + cased)
        f.sub('rmt_minus_hmt', 'diff-diff')
        
        
        proj, err, alpha = compute_projection(real.mean, hall.mean)
        
        add_scaled(case, 'proj', proj)
        money = add_posneg(case, 'err', err)
        case.text('stats', 'alpha = %f' % alpha)
        
        f = case.figure(cols=2)
        
        f.sub('proj', caption='projection' + cased)
        f.sub('err', caption='error' + cased)
        
        fmain.sub(money, caption='mismatch' + cased)
       
       
        f = case.figure(cols=2, caption="relative odds")
        
        real_ratio = numpy.log(real.mean / real_traj.mean)
        hall_ratio = numpy.log(hall.mean / hall_traj.mean)
        rel = real_ratio - hall_ratio
        
        a = add_posneg(case, 'real_ratio', (real_ratio))
        b = add_posneg(case, 'hall_ratio', (hall_ratio))
        c = add_posneg(case, 'rel', rel)
        
        f.sub('real_ratio', caption='real relative' + cased)
        f.sub('hall_ratio', caption='hall relative' + cased)
        f.sub('rel')
        
        fodds_real.sub(a, cased)
        fodds_hall.sub(b, cased)
        fodds_rel.sub(c, cased)
        
    output_file = os.path.join(outdir, '%s.html' % r.id)
    resources_dir = os.path.join(outdir, 'images')
    r.to_html(output_file, resources_dir=resources_dir)