Exemple #1
0
def import_gtfs_feed(dbhost, dbuser, dbpass, dbname, feeddir, action):
	print "\nUniBus-MySQL-Tool Suite"
	print "Import GTFS data from csv format to MySQL database."
	print "Copyright @ Zhenwang.Yao 2008. \n"
	
	if not os.path.exists(feeddir):
		print "Error: Path %s does not exist!!!" % feeddir
		sys.exit(1) 
	
	if action == "-append":
		agency.append(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "agency.txt"))
		calendar.append(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "calendar.txt"))
		calendar_dates.append(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "calendar_dates.txt"))
		routes.append(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "routes.txt"))
		shapes.append(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "shapes.txt"))
		stops.append(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "stops.txt"))
		stop_times.append(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "stop_times.txt"))
		trips.append(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "trips.txt"))

	else: #action == "-refresh"
		#citydb.drop(dbname)
		citydb.create(dbhost, dbuser, dbpass, dbname)
		agency.refresh(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "agency.txt"))
		calendar.refresh(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "calendar.txt"))
		calendar_dates.refresh(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "calendar_dates.txt"))
		routes.refresh(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "routes.txt"))
		shapes.refresh(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "shapes.txt"))
		stops.refresh(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "stops.txt"))
		stop_times.refresh(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "stop_times.txt"))
		trips.refresh(dbhost, dbuser, dbpass, dbname, os.path.join(feeddir, "trips.txt"))
Exemple #2
0
def pick_data(ns, digits):
    """Pick a bunch of digits from MNIST dataset.
    
    Input
    -----

    ns = [n1,n2,n3, ...]
    digits = [1,2,3, ....]

    both must be the same length. ns contais the number of items for
    each class determined by digits.

    Output
    ------
    
    original images (vector)
    extracted shapes
    true labels 
    
    """
    f = gzip.open('data/mnist.pkl.gz', 'rb')
    train_set, valid_set, test_set = cPickle.load(f)
    f.close()
    images, labels = train_set

    originals = []
    shapes = []
    true_labels = []
    i = 0
    for n, d in zip(ns, digits):
        # picking n elements with digit d
        x = np.where(labels == d)[0]
        idx = np.random.choice(x, n, replace=False)
        imgs = images[idx]
        originals.append(imgs)
        contours = [
            mnistshape.get_shape2(im.reshape((28, 28)), n=30, s=5, ir=2)
            for im in imgs
        ]
        shapes.append(contours)
        true_labels.append([i] * n)
        i += 1
    originals = np.concatenate(originals)
    true_labels = np.concatenate(true_labels)

    new_shapes = []
    for cluster in shapes:
        for shape in cluster:
            new_shapes.append(shape)
    new_shapes = np.array(new_shapes)

    # return shuffled data
    idx = range(len(originals))
    np.random.shuffle(idx)
    return originals[idx], new_shapes[idx], true_labels[idx]
def pick_data(ns, digits):
    """Pick a bunch of digits from MNIST dataset.
    
    Input
    -----

    ns = [n1,n2,n3, ...]
    digits = [1,2,3, ....]

    both must be the same length. ns contais the number of items for
    each class determined by digits.

    Output
    ------
    
    original images (vector)
    extracted shapes
    true labels 
    
    """
    f = gzip.open('data/mnist.pkl.gz', 'rb')
    train_set, valid_set, test_set = cPickle.load(f)
    f.close()
    images, labels = train_set

    originals = []; 
    shapes = []; 
    true_labels = [];
    i = 0
    for n, d in zip(ns, digits):
        # picking n elements with digit d
        x = np.where(labels==d)[0]
        idx = np.random.choice(x, n, replace=False)
        imgs = images[idx]
        originals.append(imgs)
        contours = [mnistshape.get_shape2(im.reshape((28,28)), n=30, s=5, ir=2)
                        for im in imgs]
        shapes.append(contours)
        true_labels.append([i]*n)
        i += 1
    originals = np.concatenate(originals)
    true_labels = np.concatenate(true_labels)
    
    new_shapes = []
    for cluster in shapes:
        for shape in cluster:
            new_shapes.append(shape)
    new_shapes = np.array(new_shapes)

    # return shuffled data
    idx = range(len(originals))
    np.random.shuffle(idx)
    return originals[idx], new_shapes[idx], true_labels[idx]
Exemple #4
0
def main():
    
    #---- Prepare
    
    # Game field size
    SCREEN_W, SCREEN_H = (640, 420)
    
    # Shape drag machine states
    (ST_MOVE, ST_ROTATE, ST_NONE) = range(3)
    state = ST_NONE
    
    # Setup game window
    display.set_caption(TITLE)
    display.set_icon(image.load('pytang.bmp'))
    
    # Create and setup game field
    screen = display.set_mode((SCREEN_W, SCREEN_H))
    screen.fill(BACKGROUND_COLOR)
    
    # Create a game field copy for internal purposes
    background = screen.copy()
    
    # Game objects - different shapes
    shapes = [
        Triangle((0, 0), (60, 60), (0, 120)),
        Triangle((0, 0), (60, 0), (30, 30)),
        Triangle((60, 0), (120, 0), (120, 60)),
        Triangle((0, 120), (60, 60), (120, 120)),
        Triangle((60, 60), (90, 30), (90, 90)),
        Parallelogram((60, 0), (90, 30), (60, 60)),
        Parallelogram((90, 30), (120, 60), (120, 120))
    ]
    
    # Game object bounding rectangles as a dictionary,
    # where key is the game object instance, and
    # the appropriate value is it's bounding rectangle
    frames = {}
    
    # Each shape is located in a separate cell of the game field
    # at first time. The game field is divided by 3 rows and
    # 3 columns, ergo, 3 x 3 = 9 cells.
    
    # One cell size
    CELL_SIZE = (SCREEN_W / 3, SCREEN_H / 3)
    
    # Shapes initial locations as (column, row) tuples.
    locations = (
        (0, 0), (1, 0), (2, 1),
        (2, 2), (0, 1), (0, 2),
        (2, 0)
    )
    
    if DRAFT_MODE:
        draw_shape = _draw_shape_draft
    else:
        draw_shape = _draw_shape_filled
    
    # Move shapes to the initial locations
    for i in range(len(shapes)):
        shapes[i].move_to(
            [
                (locations[i][coord] + 0.5) * CELL_SIZE[coord]
                for coord in (0, 1)
            ]
        )
        frames[shapes[i]] = draw_shape(screen, shapes[i])
    
    #---- Event loop
    
    do_exit = False
    
    # Shape object which is moving or rotating now
    active_shape = None
    # Mouse previous position, is valid when some shape
    # is rotating or moving
    prev_pos = None
    
    while not do_exit:
        
        display.flip()
        
        for ev in event.get():
            
            # Game is over - user close main window
            if ev.type == QUIT :
                do_exit = True
                break
            
            # Mouse button is push down:
            # user wants to move or rotate one of the shapes
            
            elif (ev.type == MOUSEBUTTONDOWN) and (state == ST_NONE):
                
                touchpoint = mouse.get_pos()
                
                # Search for the first shape including
                # the mouse button down point.
                
                for shape in shapes:
                    
                    if shape.include(touchpoint):
                        
                        active_shape = shape
                        prev_pos = touchpoint
                        
                        # If the touchpoint is laying inside the
                        # inner circle, the shape s.b. moved;
                        # the shape s.b. rotated otherwise
                        if (
                            distance(touchpoint, shape.get_ref_point())
                            <=
                            shape.get_r_inner()
                        ):
                            state = ST_MOVE
                        else:
                            state = ST_ROTATE
                        
                        
                        # Take the active shape from the shapes
                        # common list,
                        # draw other shapes on the background surface.
                        
                        shapes.remove(active_shape)
                        background.fill(BACKGROUND_COLOR)
                        for shape in shapes:
                            draw_shape(background, shape)
                        
                        break
            
            # Mouse button is up: shape moving/rotating is done
            
            elif (ev.type == MOUSEBUTTONUP) and (state != ST_NONE) :
                shapes.append(active_shape)
                active_shape = None
                state = ST_NONE
            
            # Mouse is moving. If the mouse button is down
            # (i.e. if we are in ST_MOVE or ST_ROTATE state),
            # the active shape s.b. moved or rotated.
            
            elif (ev.type == MOUSEMOTION) and (state != ST_NONE) :
                
                screen.blit(
                    background,
                    frames[active_shape].topleft, frames[active_shape]
                )
                
                curr_pos = mouse.get_pos()
                
                if state == ST_MOVE :
                    active_shape.move_by(vectorAB(prev_pos, curr_pos))
                else:
                    active_shape.rotate(
                        inclination(
                            active_shape.get_ref_point(), curr_pos
                        )
                        -
                        inclination(
                            active_shape.get_ref_point(), prev_pos
                        )
                    )
                
                prev_pos = curr_pos
                frames[active_shape] = draw_shape(screen, active_shape)
    
    # Game is over, print shapes location if needed
    if PRINT_OUT:
        for i in range(len(shapes)):
            print "Shape %u vertices:" % (i,)
            for vertex in shapes[i].get_vertices():
                print "\t (%.2f, %.2f)" % vertex
Exemple #5
0
def main():

    #---- Prepare

    # Game field size
    SCREEN_W, SCREEN_H = (640, 420)

    # Shape drag machine states
    (ST_MOVE, ST_ROTATE, ST_NONE) = range(3)
    state = ST_NONE

    # Setup game window
    display.set_caption(TITLE)
    display.set_icon(image.load('pytang.bmp'))

    # Create and setup game field
    screen = display.set_mode((SCREEN_W, SCREEN_H))
    screen.fill(BACKGROUND_COLOR)

    # Create a game field copy for internal purposes
    background = screen.copy()

    # Game objects - different shapes
    shapes = [
        Triangle((0, 0), (60, 60), (0, 120)),
        Triangle((0, 0), (60, 0), (30, 30)),
        Triangle((60, 0), (120, 0), (120, 60)),
        Triangle((0, 120), (60, 60), (120, 120)),
        Triangle((60, 60), (90, 30), (90, 90)),
        Parallelogram((60, 0), (90, 30), (60, 60)),
        Parallelogram((90, 30), (120, 60), (120, 120))
    ]

    # Game object bounding rectangles as a dictionary,
    # where key is the game object instance, and
    # the appropriate value is it's bounding rectangle
    frames = {}

    # Each shape is located in a separate cell of the game field
    # at first time. The game field is divided by 3 rows and
    # 3 columns, ergo, 3 x 3 = 9 cells.

    # One cell size
    CELL_SIZE = (SCREEN_W / 3, SCREEN_H / 3)

    # Shapes initial locations as (column, row) tuples.
    locations = ((0, 0), (1, 0), (2, 1), (2, 2), (0, 1), (0, 2), (2, 0))

    if DRAFT_MODE:
        draw_shape = _draw_shape_draft
    else:
        draw_shape = _draw_shape_filled

    # Move shapes to the initial locations
    for i in range(len(shapes)):
        shapes[i].move_to([(locations[i][coord] + 0.5) * CELL_SIZE[coord]
                           for coord in (0, 1)])
        frames[shapes[i]] = draw_shape(screen, shapes[i])

    #---- Event loop

    do_exit = False

    # Shape object which is moving or rotating now
    active_shape = None
    # Mouse previous position, is valid when some shape
    # is rotating or moving
    prev_pos = None

    while not do_exit:

        display.flip()

        for ev in event.get():

            # Game is over - user close main window
            if ev.type == QUIT:
                do_exit = True
                break

            # Mouse button is push down:
            # user wants to move or rotate one of the shapes

            elif (ev.type == MOUSEBUTTONDOWN) and (state == ST_NONE):

                touchpoint = mouse.get_pos()

                # Search for the first shape including
                # the mouse button down point.

                for shape in shapes:

                    if shape.include(touchpoint):

                        active_shape = shape
                        prev_pos = touchpoint

                        # If the touchpoint is laying inside the
                        # inner circle, the shape s.b. moved;
                        # the shape s.b. rotated otherwise
                        if (distance(touchpoint, shape.get_ref_point()) <=
                                shape.get_r_inner()):
                            state = ST_MOVE
                        else:
                            state = ST_ROTATE

                        # Take the active shape from the shapes
                        # common list,
                        # draw other shapes on the background surface.

                        shapes.remove(active_shape)
                        background.fill(BACKGROUND_COLOR)
                        for shape in shapes:
                            draw_shape(background, shape)

                        break

            # Mouse button is up: shape moving/rotating is done

            elif (ev.type == MOUSEBUTTONUP) and (state != ST_NONE):
                shapes.append(active_shape)
                active_shape = None
                state = ST_NONE

            # Mouse is moving. If the mouse button is down
            # (i.e. if we are in ST_MOVE or ST_ROTATE state),
            # the active shape s.b. moved or rotated.

            elif (ev.type == MOUSEMOTION) and (state != ST_NONE):

                screen.blit(background, frames[active_shape].topleft,
                            frames[active_shape])

                curr_pos = mouse.get_pos()

                if state == ST_MOVE:
                    active_shape.move_by(vectorAB(prev_pos, curr_pos))
                else:
                    active_shape.rotate(
                        inclination(active_shape.get_ref_point(), curr_pos) -
                        inclination(active_shape.get_ref_point(), prev_pos))

                prev_pos = curr_pos
                frames[active_shape] = draw_shape(screen, active_shape)

    # Game is over, print shapes location if needed
    if PRINT_OUT:
        for i in range(len(shapes)):
            print "Shape %u vertices:" % (i, )
            for vertex in shapes[i].get_vertices():
                print "\t (%.2f, %.2f)" % vertex