def goodness(self, arg):#particle, image):
        particle, image = arg
        mask = wimage(GWhiskerLayer(particle, self.renderer_dl, self.renderer_length, self.renderer_width, translate=self.current_translation), width=IMAGE_WIDTH, height=IMAGE_HEIGHT)
        mask_sum = mask.sum()
        if mask_sum == 0:
            return 0

        result = ((mask*image).sum()/(255.0*mask_sum))**self.goodness_power
        
        if DEBUG:
            from common import make_run_path
            import os
            wim = wimage((mask*image).data/255.0)
            
            imsurf=cairo.ImageSurface(cairo.FORMAT_ARGB32, IMAGE_WIDTH*3,IMAGE_HEIGHT)
            ctx = cairo.Context(imsurf)
            for im in (image, mask, wim):
                ctx.save()
                im.render(ctx)
                ctx.restore()
                ctx.translate(IMAGE_WIDTH,0)
            imsurf.write_to_png(make_run_path(os.path.join("debug","%s%i.png"%("frame", self.debug_i))))
            self.debug_i += 1

            print "sum, result:", mask_sum, result
        
        return result
def run_cli():
    """Usage: python draw_database.py DATASET_NAME OUTPUT_name

    Draws the named database.
    """

    import sys

    if len(sys.argv) < 2:
        print run_cli.__doc__
        sys.exit()

    from common import cliutils
    import wgenerator

    args, kwargs = cliutils.extract_variables(sys.argv[1:], "DATASET_NAME OUTPUT_NAME")
    
    if len(args) < 1:
        print "ERROR: Dataset name was not specified."
        sys.exit(1)

    db_name = args[0]
    out_name = args[1]

    db = wdb.StateTransitionDatabase(db_name)
    from_states, to_states = numpy.split(db.get_all_transitions(0), 2)
    """
    from_states[:,0] = 0
    from_states[:,1] = 0
    to_states[:,0] = 0
    to_states[:,1] = 0
    """

    renderer = GWhiskerDatabaseRenderer(5, 150, 5, translate=(256-75, 256), particle_alpha=0.0001)

    imsurf=cairo.ImageSurface(cairo.FORMAT_ARGB32, 512,512)
    ctx = cairo.Context(imsurf)
    #for btm, top in zip(numpy.linspace(0, 1-1.0/8, 8), numpy.linspace(1.0/8, 1, 8)):
    for states, name in ((from_states, "_from"), (to_states, "_to")):
        ctx.identity_matrix()
        ctx.rectangle(0, 0, 512, 512)
        ctx.set_source_rgb(0, 0, 0)
        ctx.fill()
        for s in states:
            #if 0.00002*btm < abs(s[0]) and abs(s[0]) < 0.00002*top:
            renderer.render(ctx, s, alpha=0.05)
	    imsurf.write_to_png(make_run_path(os.path.join("debug","db","%s.png"%(out_name + name))))
    blurbounce=bounce.transform(blur5)
 #   edge_filter=filters.prewitt
 #   abs_edge_filter=lambda img:numpy.sqrt(edge_filter(img,axis=0)**2+edge_filter(img,axis=1)**2)
 #   edgebounce=blurbounce.transform(abs_edge_filter)
    
    #badedgebounce=bounce.transform(abs_edge_filter)
    #bluredbadedgebounce=badedgebounce.transform(blur5)
    #layermanager.add_layer(bluredbadedgebounce.transform(normalize))
    
    #layermanager.add_layer(wvideo(testscreen(alpha=0.1)))



    #layermanager.add_layer(wvideo(testscreen(alpha=0.5)))
    #layermanager.add_layer(testscreen(alpha=0.1))
   
    layermanager.add_layer(blurbounce)
    layermanager.add_layer(testscreen(alpha=0.5))

    #layermanager.add_layer(multiply.transform(normalize))
  

    layermanager.exportPNGVIN(make_run_path("layermanager.pngvin"))


    win=wwindow(layermanager)

    gtk.gdk.threads_enter()
    gtk.main()
    gtk.gdk.threads_leave()