Ejemplo n.º 1
0
 def test_check_valid(self):
     start_year = 2011
     start_month = 3
     n = 4
     # Shows a 5 minute trip spanning midnight boundary, over 1.1 miles within Manhattan
     valid_lines    = ['2010000001,2010000001,"VTS",1,,"2011-03-14 23:59:10","2011-03-15 00:04:11",4,301,1.1,-73.970610,40.793724,-73.974672,40.783098\n', # Short trip; 5min 1.1 mile, l2dist 1.23km
                       '2010000002,2010000002,"VTS",1,,"2011-03-14 22:10:10","2011-03-14 22:42:16",4,1926,9.7,-73.954559,40.799313,-74.005135,40.721522\n'] # Long trip; 32min 9.7 mile, l2dist 9.64km
                      # start_year, start_month don't match
     invalid_lines = ['2010000001,2010000001,"VTS",1,,"2011-02-14 23:59:10","2011-03-15 00:04:11",4,301,1.1,-73.970610,40.793724,-73.974672,40.783098\n',
                      '2010000001,2010000001,"VTS",1,,"2011-04-14 23:59:10","2011-03-15 00:04:11",4,301,1.1,-73.970610,40.793724,-73.974672,40.783098\n',
                      '2010000001,2010000001,"VTS",1,,"2010-03-14 23:59:10","2011-03-15 00:04:11",4,301,1.1,-73.970610,40.793724,-73.974672,40.783098\n',
                      '2010000001,2010000001,"VTS",1,,"2012-03-14 23:59:10","2011-03-15 00:04:11",4,301,1.1,-73.970610,40.793724,-73.974672,40.783098\n',
                      '2010000001,2010000001,"VTS",1,,"2010-02-14 23:59:10","2011-03-15 00:04:11",4,301,1.1,-73.970610,40.793724,-73.974672,40.783098\n',
                      '2010000001,2010000001,"VTS",1,,"2012-02-14 23:59:10","2011-03-15 00:04:11",4,301,1.1,-73.970610,40.793724,-73.974672,40.783098\n',
                      '2010000001,2010000001,"VTS",1,,"2010-04-14 23:59:10","2011-03-15 00:04:11",4,301,1.1,-73.970610,40.793724,-73.974672,40.783098\n',
                      '2010000001,2010000001,"VTS",1,,"2012-04-14 23:59:10","2011-03-15 00:04:11",4,301,1.1,-73.970610,40.793724,-73.974672,40.783098\n',
                      # Invalid due to zero trip distance, or very short trip distance
                      '2010000001,2010000001,"VTS",1,,"2011-03-14 23:59:10","2011-03-15 01:04:11",4,301,1.1,-73.970610,40.793724,-73.970610,40.793724\n',
                      '2010000001,2010000001,"VTS",1,,"2011-03-14 23:59:10","2011-03-15 01:04:11",4,301,1.1,-73.970610,40.793724,-73.970620,40.793734\n',
                      # Invalid due to high trip speed; long trip l2distance is 9640m; 9.64m in 3 minutes is too fast!
                      '2010000002,2010000002,"VTS",1,,"2011-03-14 22:10:10","2011-03-14 22:13:10",4,180,9.7,-73.954559,40.799313,-74.005135,40.721522\n',
                      # Invalid due to very short trip time; 55s. (~23 m/s)
                      '2010000001,2010000001,"VTS",1,,"2011-03-14 23:59:00","2011-03-14 23:59:55",4,55,1.1,-73.970610,40.793724,-73.974672,40.783098\n',
                      # Invalid due to negative trip time (-115 s!)
                      '2010000001,2010000001,"VTS",1,,"2011-03-14 23:59:55","2011-03-14 23:58:00",4,-55,1.1,-73.970610,40.793724,-73.974672,40.783098\n']
     for line in valid_lines:
         entry = utils.process_entry(line=line, n=4)
         self.assertTrue(utils.check_valid(entry=entry, year=start_year, month=start_month))
     for line in invalid_lines:
         entry = utils.process_entry(line=line, n=4)
         self.assertFalse(utils.check_valid(entry=entry, year=start_year, month=start_month))
Ejemplo n.º 2
0
def move(key):
    global brick, field, window
    window.clear()
    window.refresh()
    brick.control_brick(key, field)

    # check valid move or not
    valid = utils.check_valid(field, brick)
    if not valid:
        brick.revert()

    # check_get floor
    brick.control_brick(ord("s"), field)
    can_down = utils.check_valid(field, brick)
    brick.revert()
    if not can_down:
        field.add_brick(brick)
        brick.create_new_brick()

    # clear one row when full
    field.check_and_clear_rows()

    # render screen
    rendered = utils.create_screen(field, brick)
    window.addstr(0, 0, rendered)
Ejemplo n.º 3
0
def auto_down():
    while True:
        time.sleep(cfg.TIME_SLEEP)
        global bricks, field, count
        bricks.control_brick(ord("s"))
        can_down = utils.check_valid(field, bricks)
        if not can_down:
            bricks.revert()
            field.add_bricks(bricks)
            bricks = go.Bricks()
            field.check_and_clear_rows()
        # render screen
        rendered = utils.create_screen(field, bricks)
        window.refresh()
        window.addstr(0, 0, rendered)
Ejemplo n.º 4
0
        rendered = utils.create_screen(field, bricks)
        window.refresh()
        window.addstr(0, 0, rendered)


Thread(target=auto_down).start()

while True:
    # get key
    key = window.getch()
    window.clear()
    window.refresh()
    bricks.control_brick(key)

    # check valid move or not
    valid = utils.check_valid(field, bricks)
    if not valid:
        bricks.revert()

    # check_get floor
    bricks.control_brick(ord("s"))
    can_down = utils.check_valid(field, bricks)
    bricks.revert()
    if not can_down:
        field.add_bricks(bricks)
        bricks = go.Bricks()

    # clear one row when full
    field.check_and_clear_rows()

    # render screen
Ejemplo n.º 5
0
 def move_to_floor(self, field):
     while utils.check_valid(field, self):
         self.brick["coord"][0] -= 1
     self.brick["coord"][0] += 1
Ejemplo n.º 6
0
def process(startyear=2010,
            startmonth=1,
            endyear=2013,
            endmonth=12,
            width=10,
            height=20,
            n=4,
            V=False,
            restart=False):
    ''' Processes data from FOIL201*/trip_data_*.csv into compressed .npz files.
    
    Returns nothing. Processes month-by-month.
    
    # Arguments:
        startyear, startmonth, endyear, endmonth: Integers representing
            the files to start and end processing on.
        width, height: Integers representing the resolution of the grid
            that trips are mapped to.
        n: Integer, the number of time slots per hour.
        V: Boolean; if True, print extra information to console.
        restart: Boolean; if True, don't save the processed data for
            the first year, month.
    '''
    # List of year-month dates to iterate over.
    dates = utils.generate_dates(startyear, startmonth, endyear, endmonth)

    # Generate empty arrays for the 'next month' of data.
    vdata_next_mo = utils.gen_empty_vdata(year=startyear,
                                          month=startmonth,
                                          w=width,
                                          h=height,
                                          n=n)
    fdata_next_mo = utils.gen_empty_fdata(year=startyear,
                                          month=startmonth,
                                          w=width,
                                          h=height,
                                          n=n)

    for (year, month) in dates:
        trips = np.zeros(
            (2, 2,
             2))  # Statistical info about the trips this month. (See README)
        invalid_count = 0  # Entries that are parsable, but are not a valid trip
        unparsable_count = 0  # Entries that raise an error on parsing
        line_number = 0

        # Shift the vdata, fdata that are in-focus to this month
        vdata = vdata_next_mo
        fdata = fdata_next_mo

        # Generate new, empty 'next-month' arrays
        #   (For trips that cross the boundary, e.g. 2-28 at 11:59 to 3:01 at 0:02
        next_year, next_month = get_next(year=year, month=month)
        vdata_next_mo = utils.gen_empty_vdata(year=next_year,
                                              month=next_month,
                                              w=width,
                                              h=height,
                                              n=n)
        fdata_next_mo = utils.gen_empty_fdata(year=next_year,
                                              month=next_month,
                                              w=width,
                                              h=height,
                                              n=n)

        load_filename = "../decompressed/FOIL" + str(
            year) + "/trip_data_" + str(month) + ".csv"
        #load_filename = "example.csv"

        if V:
            print("Starting on", year, month)
            print_time()

        with open(load_filename, "r") as read_f:
            read_f.readline()  # Skip header
            for line in read_f:
                line_number += 1
                if V and ((line_number % 1000000) == 0):
                    print("    Line", line_number)
                try:
                    # This is where the processing happens.
                    entry = utils.process_entry(line=line, n=n)
                    if utils.check_valid(entry=entry, year=year, month=month):
                        utils.update_data(entry=entry,
                                          vdata=vdata,
                                          fdata=fdata,
                                          vdata_next_mo=vdata_next_mo,
                                          fdata_next_mo=fdata_next_mo,
                                          trips=trips,
                                          w=width,
                                          h=height,
                                          n=n)
                    else:
                        invalid_count += 1
                except:
                    unparsable_count += 1
                    print("  ERROR - could not parse line", line_number)

        print("    Line", line_number)

        if restart and year == startyear and month == startmonth:
            if V:
                print("Not saving for", year, month, "due to restart flag.")
        else:
            # Save the file
            save_filename_date = str(year) + "-" + str(month).zfill(2)

            if V:
                print("Saving", save_filename_date)
                print_time()
            np.savez_compressed(save_filename_date + "-data.npz",
                                vdata=vdata,
                                fdata=fdata,
                                trips=trips,
                                errors=np.array(
                                    [invalid_count, unparsable_count]))

    if V:
        print("All finished!")
        print_time()