def test_attribute_error_from_data_frame(): df = DataFrame( data=[ [39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1], [39.984198, 116.319322, Timestamp('2008-10-23 05:53:06'), 1], [39.984224, 116.319402, Timestamp('2008-10-23 05:53:11'), 2], [39.984224, 116.319402, Timestamp('2008-10-23 05:53:11'), 2], ], columns=['laterr', 'lon', 'datetime', 'id'], index=[0, 1, 2, 3], ) try: MoveDataFrame( data=df, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME ) raise AssertionError( 'AttributeError error not raised by MoveDataFrame' ) except KeyError: pass df = DataFrame( data=[ [39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1], [39.984198, 116.319322, Timestamp('2008-10-23 05:53:06'), 1], [39.984224, 116.319402, Timestamp('2008-10-23 05:53:11'), 2], [39.984224, 116.319402, Timestamp('2008-10-23 05:53:11'), 2], ], columns=['lat', 'lonerr', 'datetime', 'id'], index=[0, 1, 2, 3], ) try: MoveDataFrame( data=df, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME ) raise AssertionError( 'AttributeError error not raised by MoveDataFrame' ) except KeyError: pass df = DataFrame( data=[ [39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1], [39.984198, 116.319322, Timestamp('2008-10-23 05:53:06'), 1], [39.984224, 116.319402, Timestamp('2008-10-23 05:53:11'), 2], [39.984224, 116.319402, Timestamp('2008-10-23 05:53:11'), 2], ], columns=['lat', 'lon', 'datetimerr', 'id'], index=[0, 1, 2, 3], ) try: MoveDataFrame( data=df, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME ) raise AssertionError( 'AttributeError error not raised by MoveDataFrame' ) except KeyError: pass
def test_MEDT(): expected = 619.9417037397966 move_df1 = MoveDataFrame(data=traj_example1) move_df2 = MoveDataFrame(data=traj_example3) medt = distances.MEDT(move_df1, move_df2) assert_almost_equal(medt, expected)
def test_MEDP(): expected = 241.91923668814994 move_df1 = MoveDataFrame(data=traj_example1) move_df2 = MoveDataFrame(data=traj_example2) medp = distances.MEDP(move_df1, move_df2) assert_almost_equal(medp, expected)
def test_move_data_frame_from_data_frame(): df = DataFrame( data=[ [39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1], [39.984198, 116.319322, Timestamp('2008-10-23 05:53:06'), 1], [39.984224, 116.319402, Timestamp('2008-10-23 05:53:11'), 2], [39.984224, 116.319402, Timestamp('2008-10-23 05:53:11'), 2], ], columns=['lat', 'lon', 'datetime', 'id'], index=[0, 1, 2, 3], ) move_df = MoveDataFrame( data=df, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, type_=TYPE_DASK, ) assert _has_columns(move_df) assert _validate_move_data_frame_data(move_df) assert isinstance(move_df, DaskMoveDataFrame)
def test_append(): move_df = _default_move_df() df = DataFrame( data=[[39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1]], columns=['lat', 'lon', 'datetime', 'id'], index=[0], ) mdf = MoveDataFrame(df) expected = DataFrame( data=[ [39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1], [39.984198, 116.319322, Timestamp('2008-10-23 05:53:06'), 1], [39.984224, 116.319402, Timestamp('2008-10-23 05:53:11'), 2], [39.984224, 116.319402, Timestamp('2008-10-23 05:53:11'), 2], [39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1], ], columns=['lat', 'lon', 'datetime', 'id'], index=[0, 1, 2, 3, 0], ) assert_frame_equal(move_df.append(df), expected) assert_frame_equal(move_df.append(mdf), expected)
def _prepare_df_with_long_trajectory(): move_df = MoveDataFrame( data=list_data_3, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, ) move_df[DIST_TO_PREV] = [ nan, 13.884481484192932, 7.563335999941849, 1.3208180727070074, nan, 2039.4197149375298, ] move_df[TIME_TO_PREV] = [nan, 61.00000000000001, 70.0, 65.0, nan, 5.0] move_df[SPEED_TO_PREV] = [ nan, 0.22761445056053983, 0.10804765714202641, 0.020320278041646267, nan, 407.883942987506, ] cols = [ 'id', 'lat', 'lon', 'datetime', 'dist_to_prev', 'time_to_prev', 'speed_to_prev', ] return move_df[cols], cols
def _prepare_df_with_dist_time_speed(): move_df = MoveDataFrame( data=list_data_2, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, ) move_df[DIST_TO_PREV] = [ nan, 13.884481484192932, 140454.64510608764, 140460.97747220137, 1.3208180727070074, ] move_df[TIME_TO_PREV] = [nan, 1.0, 5.0, 5.0, 5.0] move_df[SPEED_TO_PREV] = [ nan, 13.884481484192932, 28090.92902121753, 28092.195494440275, 0.26416361454140147, ] cols = [ 'id', 'lat', 'lon', 'datetime', 'dist_to_prev', 'time_to_prev', 'speed_to_prev', ] return move_df[cols], cols
def test_join_with_pois(): move_df = MoveDataFrame(list_move) pois = DataFrame( data=list_pois, columns=[LATITUDE, LONGITUDE, TRAJ_ID, TYPE_POI, NAME_POI], index=[0, 1, 2, 3, 4, 5, 6]) expected = DataFrame(data=[[ 39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1, 1, 0.0, 'distrito_pol_1' ], [ 39.984559000000004, 116.326696, Timestamp('2008-10-23 10:37:26'), 1, 6, 128.24869775642176, 'adocao_de_animais' ], [ 40.002899, 116.32151999999999, Timestamp('2008-10-23 10:50:16'), 1, 5, 663.0104596559174, 'rinha_de_galo_world_cup' ], [ 40.016238, 116.30769099999999, Timestamp('2008-10-23 11:03:06'), 1, 4, 286.3387434682031, 'forro_tropykalia' ], [ 40.013814, 116.306525, Timestamp('2008-10-23 11:58:33'), 2, 4, 0.9311014399622559, 'forro_tropykalia' ], [ 40.009735, 116.315069, Timestamp('2008-10-23 23:50:45'), 2, 3, 211.06912863495492, 'supermercado_aroldo' ], [ 39.993527, 116.32648300000001, Timestamp('2008-10-24 00:02:14'), 2, 2, 279.6712398549538, 'policia_federal' ], [ 39.978575, 116.326975, Timestamp('2008-10-24 00:22:01'), 3, 6, 792.7526066105717, 'adocao_de_animais' ], [ 39.981668, 116.310769, Timestamp('2008-10-24 01:57:57'), 3, 7, 270.7018856738821, 'dia_do_municipio' ]], columns=[ LATITUDE, LONGITUDE, DATETIME, TRAJ_ID, ID_POI, DIST_POI, NAME_POI ], index=[0, 1, 2, 3, 4, 5, 6, 7, 8]) integration.join_with_pois(move_df, pois, inplace=True) assert_frame_equal(move_df, expected, check_dtype=False)
def _default_move_df(): return MoveDataFrame( data=list_data, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, traj_id=TRAJ_ID, )
def test_move_data_frame_from_data_frame(): df = _default_pandas_df() move_df = MoveDataFrame( data=df, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME ) assert _has_columns(move_df) assert _validate_move_data_frame_data(move_df) assert isinstance(move_df, PandasMoveDataFrame)
def read_postgres( query, in_memory=True, type_=TYPE_PANDAS, dbname='postgres', user='******', psswrd='', host='localhost', port=5432, ): """ Builds a dataframe from a query to a postgres database. Parameters ---------- query : string Sql query in_memory : bool, default True Whether te operation will be executed in memory type_ : 'pandas', 'dask' or None, defaults 'pandas' It will try to convert the dataframe into a MoveDataFrame dbname : string, default 'postgres' Name of the database user : string, default 'postgres' The user connecting to the database psswrd : string, default '' The password of the database host : string, default 'localhost' The address of the database port : int, default 5432 The port of the database in the host Returns ------- PandasMoveDataFrame, DaskMoveDataFrame or PandasDataFrame a dataframe object containing the result from the query """ conn = None try: conn = connect_postgres(dbname, user, psswrd, host, port) if in_memory: dataframe = read_sql_inmem_uncompressed(query, conn) else: dataframe = read_sql_tmpfile(query, conn) except Exception as e: if conn is not None: conn.close() raise e finally: if conn is not None: conn.close() try: return MoveDataFrame(dataframe, type_=type_) except Exception: return dataframe
def test_merge_home_with_poi(): list_move4merge = [ [39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1], [39.984559000000004, 116.326696, Timestamp('2008-10-23 10:37:26'), 1], [40.002899, 116.32151999999999, Timestamp('2008-10-23 10:50:16'), 1], [40.016238, 116.30769099999999, Timestamp('2008-10-23 11:03:06'), 1], [40.013814, 116.306525, Timestamp('2008-10-23 11:58:33'), 2], [40.009735, 116.315069, Timestamp('2008-10-23 23:50:45'), 2], [39.993527, 116.32648300000001, Timestamp('2008-10-24 00:02:14'), 2], ] move_df = MoveDataFrame(list_move4merge) pois = DataFrame( data=list_pois, columns=[LATITUDE, LONGITUDE, TRAJ_ID, TYPE_POI, NAME_POI], index=[0, 1, 2, 3, 4, 5, 6] ) integration.join_with_pois(move_df, pois) list_home = [ [39.984094, 116.319236, 1, 'rua da mae', 'quixiling'], [40.013821, 116.306531, 2, 'rua da familia', 'quixeramoling'] ] home_df = DataFrame( data=list_home, columns=[LATITUDE, LONGITUDE, TRAJ_ID, ADDRESS, CITY], index=[0, 1] ) integration.join_with_home_by_id(move_df, home_df) expected = DataFrame( data=[ [1, 39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 'rua da mae', 0.0, 'home', 'quixiling'], [1, 39.984559000000004, 116.326696, Timestamp('2008-10-23 10:37:26'), 6, 128.24869775642176, 'adocao_de_animais', 'quixiling'], [1, 40.002899, 116.32151999999999, Timestamp('2008-10-23 10:50:16'), 5, 663.0104596559174, 'rinha_de_galo_world_cup', 'quixiling'], [1, 40.016238, 116.30769099999999, Timestamp('2008-10-23 11:03:06'), 4, 286.3387434682031, 'forro_tropykalia', 'quixiling'], [2, 40.013814, 116.306525, Timestamp('2008-10-23 11:58:33'), 'rua da familia', 0.9311014399622559, 'home', 'quixeramoling'], [2, 40.009735, 116.315069, Timestamp('2008-10-23 23:50:45'), 3, 211.06912863495492, 'supermercado_aroldo', 'quixeramoling'], [2, 39.993527, 116.32648300000001, Timestamp('2008-10-24 00:02:14'), 2, 279.6712398549538, 'policia_federal', 'quixeramoling'] ], columns=[ TRAJ_ID, LATITUDE, LONGITUDE, DATETIME, ID_POI, DIST_POI, NAME_POI, CITY ], index=[0, 1, 2, 3, 4, 5, 6] ) integration.merge_home_with_poi(move_df) assert_frame_equal(move_df, expected, check_dtype=False)
def test_to_grid(): move_df = MoveDataFrame( data=list_data, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, traj_id=TRAJ_ID, ) assert isinstance(move_df.to_grid(8), pymove.core.grid.Grid)
def _prepare_df_default(list_data): move_df = MoveDataFrame( data=list_data, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, traj_id=TRAJ_ID, ) cols = ['lat', 'lon', 'datetime', 'id'] return move_df, cols
def _default_traj_df(data=None): if data is None: data = traj_example return MoveDataFrame( data=data, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, traj_id=TRAJ_ID, )
def test_to_data_frame(): move_df = MoveDataFrame( data=list_data, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, traj_id=TRAJ_ID, ) assert isinstance(move_df.to_data_frame(), DataFrame)
def test_move_data_frame_from_data_frame(): df = _default_pandas_df() move_df = MoveDataFrame( data=df, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME ) assert MoveDataFrame.has_columns(move_df) try: MoveDataFrame.validate_move_data_frame(move_df) except Exception: assert False assert isinstance(move_df, PandasMoveDataFrame)
def test__reset_set_window__and_creates_event_id_type(): list_events = [[ 39.984094, 116.319236, 1, Timestamp('2008-10-24 01:57:57'), 'show do tropykalia' ], [ 39.991013, 116.326384, 2, Timestamp('2008-10-24 00:22:01'), 'evento da prefeitura' ], [ 40.01, 116.312615, 3, Timestamp('2008-10-25 00:21:01'), 'show do seu joao' ], [ 40.013821, 116.306531, 4, Timestamp('2008-10-26 00:22:01'), 'missa' ]] move_df = MoveDataFrame(list_move) pois = DataFrame( data=list_events, columns=[LATITUDE, LONGITUDE, EVENT_ID, DATETIME, EVENT_TYPE], index=[0, 1, 2, 3]) list_win_start = [ '2008-10-22T17:23:05.000000000', '2008-10-22T22:07:26.000000000', '2008-10-22T22:20:16.000000000', '2008-10-22T22:33:06.000000000', '2008-10-22T23:28:33.000000000', '2008-10-23T11:20:45.000000000', '2008-10-23T11:32:14.000000000', '2008-10-23T11:52:01.000000000', '2008-10-23T13:27:57.000000000' ] win_start_expected = Series(pd.to_datetime(list_win_start), name=DATETIME) list_win_end = [ '2008-10-23T18:23:05.000000000', '2008-10-23T23:07:26.000000000', '2008-10-23T23:20:16.000000000', '2008-10-23T23:33:06.000000000', '2008-10-24T00:28:33.000000000', '2008-10-24T12:20:45.000000000', '2008-10-24T12:32:14.000000000', '2008-10-24T12:52:01.000000000', '2008-10-24T14:27:57.000000000' ] win_end_expected = Series(pd.to_datetime(list_win_end), name=DATETIME) dist_expected = np.full(9, np.Infinity, dtype=np.float64) type_expected = np.full(9, '', dtype='object_') id_expected = np.full(9, '', dtype='object_') window_starts, window_ends, current_distances, event_id, event_type = ( integration._reset_set_window__and_creates_event_id_type( move_df, pois, 45000, DATETIME)) assert_series_equal(window_starts, win_start_expected) assert_series_equal(window_ends, win_end_expected) assert_array_almost_equal(current_distances, dist_expected) assert_array_equal(event_id, id_expected) assert_array_equal(event_type, type_expected)
def test_number_users(): move_df = MoveDataFrame( data=list_data, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, traj_id=TRAJ_ID, ) assert move_df.get_users_number() == 1 move_df[UID] = [1, 1, 2, 3] assert move_df.get_users_number() == 3
def test__reset_and_creates_id_and_lat_lon(): move_df = MoveDataFrame(list_move) pois = DataFrame( data=list_pois, columns=[LATITUDE, LONGITUDE, TRAJ_ID, TYPE_POI, NAME_POI], index=[0, 1, 2, 3, 4, 5, 6]) dists, ids, tags, lats, lons = ( integration._reset_and_creates_id_and_lat_lon(move_df, pois, True, True)) id_expected = np.full(9, '', dtype='object_') tag_expected = np.full(9, '', dtype='object_') dist_expected = np.full(9, np.Infinity, dtype=np.float64) lat_expected = np.full(7, np.Infinity, dtype=np.float64) lon_expected = np.full(7, np.Infinity, dtype=np.float64) assert_array_almost_equal(dists, dist_expected) assert_array_equal(ids, id_expected) assert_array_equal(tags, tag_expected) assert_array_almost_equal(lats, lat_expected) assert_array_almost_equal(lons, lon_expected) dists, ids, tags, lats, lons = ( integration._reset_and_creates_id_and_lat_lon(move_df, pois, True, False)) assert_array_almost_equal(dists, dist_expected) assert_array_equal(ids, id_expected) assert_array_equal(tags, tag_expected) assert_array_almost_equal(lats, lat_expected) assert_array_almost_equal(lons, lon_expected) dists, ids, tags, lats, lons = ( integration._reset_and_creates_id_and_lat_lon(move_df, pois, False, True)) lat_expected = np.full(9, np.Infinity, dtype=np.float64) lon_expected = np.full(9, np.Infinity, dtype=np.float64) assert_array_almost_equal(dists, dist_expected) assert_array_equal(ids, id_expected) assert_array_equal(tags, tag_expected) assert_array_almost_equal(lats, lat_expected) assert_array_almost_equal(lons, lon_expected) dists, ids, tags, lats, lons = ( integration._reset_and_creates_id_and_lat_lon(move_df, pois, False, False)) assert_array_almost_equal(dists, dist_expected) assert_array_equal(ids, id_expected) assert_array_equal(tags, tag_expected) assert_array_almost_equal(lats, lat_expected) assert_array_almost_equal(lons, lon_expected)
def test_join_colletive_areas(): move_df = MoveDataFrame(data=list_move, ) move_df['geometry'] = move_df.apply(lambda x: Point(x['lon'], x['lat']), axis=1) expected = move_df.copy() indexes_ac = np.linspace(0, move_df.shape[0], 5, dtype=int) area_c = move_df[move_df.index.isin(indexes_ac)].copy() integration.join_collective_areas(move_df, area_c, inplace=True) expected[VIOLATING] = [ True, False, True, False, True, False, True, False, False ] assert_frame_equal(move_df, expected)
def test_join_with_pois_by_category(): move_df = MoveDataFrame(list_move) pois = DataFrame( data=list_pois, columns=[LATITUDE, LONGITUDE, TRAJ_ID, TYPE_POI, NAME_POI], index=[0, 1, 2, 3, 4, 5, 6] ) expected = DataFrame( data=[ [39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1, 1, 0.0, 3, 2935.3102772960456, 7, 814.8193850933852, 5, 2672.393533820207, 6, 675.1730686007362], [39.984559000000004, 116.326696, Timestamp('2008-10-23 10:37:26'), 1, 1, 637.6902157810676, 3, 3072.6963790707114, 7, 1385.3649632111096, 5, 2727.1360691122813, 6, 128.24869775642176], [40.002899, 116.32151999999999, Timestamp('2008-10-23 10:50:16'), 1, 2, 1385.0871812075436, 3, 1094.8606633486436, 4, 1762.0085654338782, 5, 663.0104596559174, 6, 1965.702358742657], [40.016238, 116.30769099999999, Timestamp('2008-10-23 11:03:06'), 1, 2, 3225.288830967221, 3, 810.5429984051405, 4, 286.3387434682031, 5, 1243.8915481769327, 6, 3768.0652637796675], [40.013814, 116.306525, Timestamp('2008-10-23 11:58:33'), 2, 2, 3047.8382223981853, 3, 669.9731550451877, 4, 0.9311014399622559, 5, 1145.172578151837, 6, 3574.252994707609], [40.009735, 116.315069, Timestamp('2008-10-23 23:50:45'), 2, 2, 2294.0758201547073, 3, 211.06912863495492, 4, 857.4175399672413, 5, 289.35378153627966, 6, 2855.1657930463994], [39.993527, 116.32648300000001, Timestamp('2008-10-24 00:02:14'), 2, 2, 279.6712398549538, 3, 2179.5701631051966, 7, 2003.4096341742952, 5, 1784.3132149978549, 6, 870.5252810680124], [39.978575, 116.326975, Timestamp('2008-10-24 00:22:01'), 3, 1, 900.7798955139455, 3, 3702.2394204188754, 7, 1287.7039084016499, 5, 3376.4438614084356, 6, 792.7526066105717], [39.981668, 116.310769, Timestamp('2008-10-24 01:57:57'), 3, 1, 770.188754517813, 3, 3154.296880053552, 7, 270.7018856738821, 5, 2997.898227057909, 6, 1443.9247752786023] ], columns=[ LATITUDE, LONGITUDE, DATETIME, TRAJ_ID, 'id_policia', 'dist_policia', 'id_comercio', 'dist_comercio', 'id_show', 'dist_show', 'id_risca-faca', 'dist_risca-faca', 'id_evento', 'dist_evento' ], index=[0, 1, 2, 3, 4, 5, 6, 7, 8] ) integration.join_with_pois_by_category(move_df, pois) assert_frame_equal(move_df, expected, check_dtype=False)
def test_join_with_poi_datetime(): list_events = [ [39.984094, 116.319236, 1, Timestamp('2008-10-24 01:57:57'), 'show do tropykalia'], [39.991013, 116.326384, 2, Timestamp('2008-10-24 00:22:01'), 'evento da prefeitura'], [40.01, 116.312615, 3, Timestamp('2008-10-25 00:21:01'), 'show do seu joao'], [40.013821, 116.306531, 4, Timestamp('2008-10-26 00:22:01'), 'missa'] ] move_df = MoveDataFrame(list_move) pois = DataFrame( data=list_events, columns=[LATITUDE, LONGITUDE, EVENT_ID, DATETIME, EVENT_TYPE], index=[0, 1, 2, 3] ) expected = DataFrame( data=[ [39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1, '', inf, ''], [39.984559000000004, 116.326696, Timestamp('2008-10-23 10:37:26'), 1, '', inf, ''], [40.002899, 116.32151999999999, Timestamp('2008-10-23 10:50:16'), 1, '', inf, ''], [40.016238, 116.30769099999999, Timestamp('2008-10-23 11:03:06'), 1, '', inf, ''], [40.013814, 116.306525, Timestamp('2008-10-23 11:58:33'), 2, 2, 3047.8382223981853, 'evento da prefeitura'], [40.009735, 116.315069, Timestamp('2008-10-23 23:50:45'), 2, 2, 2294.0758201547073, 'evento da prefeitura'], [39.993527, 116.32648300000001, Timestamp('2008-10-24 00:02:14'), 2, 2, 279.6712398549538, 'evento da prefeitura'], [39.978575, 116.326975, Timestamp('2008-10-24 00:22:01'), 3, 1, 900.7798955139455, 'show do tropykalia'], [39.981668, 116.310769, Timestamp('2008-10-24 01:57:57'), 3, 1, 770.188754517813, 'show do tropykalia'] ], columns=[ LATITUDE, LONGITUDE, DATETIME, TRAJ_ID, EVENT_ID, DIST_EVENT, EVENT_TYPE ], index=[0, 1, 2, 3, 4, 5, 6, 7, 8] ) integration.join_with_poi_datetime(move_df, pois, time_window=45000) assert_frame_equal(move_df, expected, check_dtype=False)
def test_join_with_poi_datetime_optimizer(): list_events = [ [39.984094, 116.319236, 1, Timestamp('2008-10-24 01:57:57'), 'show do tropykalia'], [39.991013, 116.326384, 2, Timestamp('2008-10-24 00:22:01'), 'evento da prefeitura'], [40.01, 116.312615, 3, Timestamp('2008-10-25 00:21:01'), 'show do seu joao'], [40.013821, 116.306531, 4, Timestamp('2008-10-26 00:22:01'), 'missa'] ] move_df = MoveDataFrame(list_move) pois = DataFrame( data=list_events, columns=[LATITUDE, LONGITUDE, EVENT_ID, DATETIME, EVENT_TYPE], index=[0, 1, 2, 3] ) expected = DataFrame( data=[ [39.984094, 116.319236, Timestamp('2008-10-23 05:53:05'), 1, 1, 0.0, 'show do tropykalia'], [39.984559000000004, 116.326696, Timestamp('2008-10-23 10:37:26'), 1, 1, 637.6902157810676, 'show do tropykalia'], [40.002899, 116.32151999999999, Timestamp('2008-10-23 10:50:16'), 1, 1, 1094.8606633486436, 'show do tropykalia'], [40.016238, 116.30769099999999, Timestamp('2008-10-23 11:03:06'), 1, 1, 286.3387434682031, 'show do tropykalia'], [40.013814, 116.306525, Timestamp('2008-10-23 11:58:33'), 2, 1, 0.9311014399622559, 'show do tropykalia'], [40.009735, 116.315069, Timestamp('2008-10-23 23:50:45'), 2, '', inf, ''], [39.993527, 116.32648300000001, Timestamp('2008-10-24 00:02:14'), 2, '', inf, ''], [39.978575, 116.326975, Timestamp('2008-10-24 00:22:01'), 3, '', inf, ''], [39.981668, 116.310769, Timestamp('2008-10-24 01:57:57'), 3, '', inf, ''] ], columns=[ LATITUDE, LONGITUDE, DATETIME, TRAJ_ID, EVENT_ID, DIST_EVENT, EVENT_TYPE ], index=[0, 1, 2, 3, 4, 5, 6, 7, 8] ) integration.join_with_poi_datetime_optimizer(move_df, pois, time_window=45000) assert_frame_equal(move_df, expected, check_dtype=False)
def test_get_bbox_by_radius(): list_data = [ [39.984092712402344, 116.31923675537101, '2008-10-23 05:53:05', 1], [39.984199952392578, 116.31932067871094, '2008-10-23 05:53:06', 1], [39.984222412109375, 116.31940460205078, '2008-10-23 05:53:11', 2], [39.984222412109375, 116.31940460205078, '2008-10-23 05:53:11', 2], ] move_df = MoveDataFrame(list_data) bbox = filters.get_bbox_by_radius((move_df[LATITUDE], move_df[LONGITUDE])) bbox_expected = [[39.9750995, 39.97520674, 39.9752292, 39.9752292], [116.30749968, 116.30758358, 116.3076675, 116.3076675], [39.99308593, 39.99319317, 39.99321563, 39.99321563], [116.33097383, 116.33105777, 116.3311417, 116.3311417]] assert_array_almost_equal(bbox, bbox_expected)
def _prepare_df_with_distances(): move_df = MoveDataFrame( data=[ [39.984093, 116.319237, '2008-10-23 05:53:05', 1], [39.984200, 116.319321, '2008-10-23 05:53:06', 1], [38.984211, 115.319389, '2008-10-23 05:53:11', 1], [39.984222, 116.319405, '2008-10-23 05:53:16', 1], [39.984219, 116.319420, '2008-10-23 05:53:21', 1], ], latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, ) move_df[DIST_TO_PREV] = [ nan, 13.884481484192932, 140454.64510608764, 140460.97747220137, 1.3208180727070074, ] move_df[DIST_TO_NEXT] = [ 13.884481484192932, 140454.64510608764, 140460.97747220137, 1.3208180727070074, nan, ] move_df[DIST_PREV_TO_NEXT] = [ nan, 140440.86267282354, 7.563335999941849, 140461.50100279532, nan, ] cols = [ 'id', 'lat', 'lon', 'datetime', 'dist_to_prev', 'dist_to_next', 'dist_prev_to_next', ] return move_df[cols], cols
def _prepare_df_tid(tid): move_df = MoveDataFrame( data=list_data, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, traj_id=TRAJ_ID, ) cols = [ 'id', 'lat', 'lon', 'datetime', 'dist_to_prev', 'time_to_prev', 'speed_to_prev', tid, ] return move_df, cols
def test_move_data_frame_from_dict(): dict_data = { LATITUDE: [39.984198, 39.984224, 39.984094], LONGITUDE: [116.319402, 116.319322, 116.319402], DATETIME: [ '2008-10-23 05:53:11', '2008-10-23 05:53:06', '2008-10-23 05:53:06', ], } move_df = MoveDataFrame( data=dict_data, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, traj_id=TRAJ_ID, ) assert _has_columns(move_df) assert _validate_move_data_frame_data(move_df) assert isinstance(move_df, PandasMoveDataFrame)
def _prepare_df_with_distances(): move_df = MoveDataFrame( data=list_data_2, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, ) move_df[DIST_TO_PREV] = [ nan, 13.884481484192932, 140454.64510608764, 140460.97747220137, 1.3208180727070074, ] move_df[DIST_TO_NEXT] = [ 13.884481484192932, 140454.64510608764, 140460.97747220137, 1.3208180727070074, nan, ] move_df[DIST_PREV_TO_NEXT] = [ nan, 140440.86267282354, 7.563335999941849, 140461.50100279532, nan, ] cols = [ 'id', 'lat', 'lon', 'datetime', 'dist_to_prev', 'dist_to_next', 'dist_prev_to_next', ] return move_df[cols], cols
def test_by_tid(): move_df = MoveDataFrame( data=list_data_1, latitude=LATITUDE, longitude=LONGITUDE, datetime=DATETIME, traj_id=TRAJ_ID, ) move_df['tid'] = ['123', '456', '789', '789'] cols = ['lat', 'lon', 'datetime', 'id', 'tid'] filter_values = filters.by_tid(move_df, tid_='456') expected = DataFrame( [ [ 39.984199952392578, 116.31932067871094, Timestamp('2008-10-23 05:53:06'), 1, '456', ], ], columns=cols, index=[1], ) assert_frame_equal(filter_values, expected) assert move_df.len() == 4 filter_out_values = filters.by_tid(move_df, tid_='456', filter_out=True) expected = DataFrame( [ [ 39.984092712402344, 116.31923675537101, Timestamp('2008-10-23 05:53:05'), 1, '123', ], [ 39.984222412109375, 116.31940460205078, Timestamp('2008-10-23 05:53:11'), 2, '789', ], [ 39.984222412109375, 116.31940460205078, Timestamp('2008-10-23 05:53:11'), 2, '789', ], ], columns=cols, index=[0, 2, 3], ) assert_frame_equal(filter_out_values, expected) assert move_df.len() == 4 filters.by_tid(move_df, tid_='789', filter_out=False, inplace=True) expected = DataFrame( [ [ 39.984222412109375, 116.31940460205078, Timestamp('2008-10-23 05:53:11'), 2, '789', ], [ 39.984222412109375, 116.31940460205078, Timestamp('2008-10-23 05:53:11'), 2, '789', ], ], columns=cols, index=[2, 3], ) assert_frame_equal(move_df, expected) assert move_df.len() == 2