示例#1
0
    def test_from_to_csv(self):
        """Test basic reading and writing functions."""
        orig_file = os.path.join("tests", "data", "positionfixes.csv")
        mod_file = os.path.join("tests", "data",
                                "positionfixes_mod_columns.csv")
        tmp_file = os.path.join("tests", "data", "positionfixes_test_1.csv")

        pfs = ti.read_positionfixes_csv(orig_file, sep=";", index_col="id")

        column_mapping = {
            "lat": "latitude",
            "lon": "longitude",
            "time": "tracked_at"
        }
        mod_pfs = ti.read_positionfixes_csv(mod_file,
                                            sep=";",
                                            index_col="id",
                                            columns=column_mapping)
        assert mod_pfs.equals(pfs)
        pfs["tracked_at"] = pfs["tracked_at"].apply(
            lambda d: d.isoformat().replace("+00:00", "Z"))

        columns = [
            "user_id", "tracked_at", "latitude", "longitude", "elevation",
            "accuracy"
        ]
        pfs.as_positionfixes.to_csv(tmp_file, sep=";", columns=columns)
        assert filecmp.cmp(orig_file, tmp_file, shallow=False)
        os.remove(tmp_file)
示例#2
0
 def test_read_positionfixes_csv_crs_parameter(self):
     file = os.path.join('tests', 'data', 'positionfixes.csv')
     pfs = ti.read_positionfixes_csv(file, sep=';', index_col="id")
     crs = "EPSG:2056"
     assert pfs.crs is None
     pfs = ti.read_positionfixes_csv(file, sep=';', index_col="id", crs=crs)
     assert pfs.crs == crs
示例#3
0
 def test_set_index(self):
     """Test if `index_col` can be set."""
     file = os.path.join("tests", "data", "positionfixes.csv")
     ind_name = "id"
     pfs = ti.read_positionfixes_csv(file, sep=";", index_col=ind_name)
     assert pfs.index.name == ind_name
     pfs = ti.read_positionfixes_csv(file, sep=";", index_col=None)
     assert pfs.index.name is None
示例#4
0
 def test_positionfixes_csv_index_col(self):
     """Test if `index_col` can be set."""
     file = os.path.join('tests', 'data', 'positionfixes.csv')
     ind_name = 'id'
     pfs = ti.read_positionfixes_csv(file, sep=";", index_col=ind_name)
     assert pfs.index.name == ind_name
     pfs = ti.read_positionfixes_csv(file, sep=";", index_col=None)
     assert pfs.index.name is None
示例#5
0
 def test_set_crs(self):
     """Test setting the crs when reading."""
     file = os.path.join('tests', 'data', 'positionfixes.csv')
     pfs = ti.read_positionfixes_csv(file, sep=';', index_col="id")
     assert pfs.crs is None
     
     crs = "EPSG:2056"
     pfs = ti.read_positionfixes_csv(file, sep=';', index_col="id", crs=crs)
     assert pfs.crs == crs
示例#6
0
 def test_dbscan_max(self):
     """Test with large epsilon parameter."""
     pfs_file = os.path.join("tests", "data", "positionfixes.csv")
     pfs = ti.read_positionfixes_csv(pfs_file,
                                     sep=";",
                                     tz="utc",
                                     index_col="id")
     _, sp = pfs.as_positionfixes.generate_staypoints(method="sliding",
                                                      gap_threshold=1e6,
                                                      dist_threshold=0,
                                                      time_threshold=0)
     warn_string = "No locations can be generated, returning empty locs."
     with pytest.warns(UserWarning, match=warn_string):
         _, locs_user = sp.as_staypoints.generate_locations(
             method="dbscan",
             epsilon=1e18,
             num_samples=1000,
             agg_level="user")
         _, locs_data = sp.as_staypoints.generate_locations(
             method="dbscan",
             epsilon=1e18,
             num_samples=1000,
             agg_level="dataset")
     # "With large epsilon, every user location is an outlier"
     assert len(locs_user) == 0
     assert len(locs_data) == 0
示例#7
0
    def test_positionfixes_from_to_csv(self):
        orig_file = os.path.join('tests', 'data', 'positionfixes.csv')
        mod_file = os.path.join('tests', 'data', 'positionfixes_mod_columns.csv')
        tmp_file = os.path.join('tests', 'data', 'positionfixes_test.csv')

        pfs = ti.read_positionfixes_csv(orig_file, sep=';', index_col="id")
        
        column_mapping = {'lat': 'latitude', 'lon': 'longitude', 'time': 'tracked_at'}
        mod_pfs = ti.read_positionfixes_csv(mod_file, sep=';', index_col="id", columns=column_mapping)
        assert mod_pfs.equals(pfs)
        pfs['tracked_at'] = pfs['tracked_at'].apply(lambda d: d.isoformat().replace('+00:00', 'Z'))
        
        columns = ['user_id', 'tracked_at', 'latitude', 'longitude', 'elevation', 'accuracy']
        pfs.as_positionfixes.to_csv(tmp_file, sep=';', columns=columns)
        assert filecmp.cmp(orig_file, tmp_file, shallow=False)
        os.remove(tmp_file)
示例#8
0
 def test_loop_read(self):
     pfs = read_geolife(os.path.join('tests', 'data', 'geolife'))
     tmp_file = os.path.join('tests', 'data', 'positionfixes_test.csv')
     pfs.as_positionfixes.to_csv(tmp_file)
     pfs2 = ti.read_positionfixes_csv(tmp_file, index_col='id')[pfs.columns]
     os.remove(tmp_file)
     assert np.isclose(0, (pfs.lat - pfs2.lat).abs().sum())
示例#9
0
 def test_positionfixes_plot(self):
     tmp_file = 'tests/data/positionfixes_plot.png'
     pfs = ti.read_positionfixes_csv('tests/data/positionfixes.csv',
                                     sep=';')
     pfs.as_positionfixes.plot(out_filename=tmp_file, plot_osm=False)
     assert os.path.exists(tmp_file)
     os.remove(tmp_file)
 def test_loop_read(self):
     """Use read_geolife reader, store posfix as .csv, load them again."""
     pfs, _ = read_geolife(os.path.join("tests", "data", "geolife"))
     tmp_file = os.path.join("tests", "data", "positionfixes_test.csv")
     pfs.as_positionfixes.to_csv(tmp_file)
     pfs2 = ti.read_positionfixes_csv(tmp_file, index_col="id")[pfs.columns]
     os.remove(tmp_file)
     assert np.isclose(0, (pfs.lat - pfs2.lat).abs().sum())
示例#11
0
    def test_as_positionfixes_accessor(self):
        orig_file = os.path.join('tests', 'data', 'positionfixes.csv')
        pfs = ti.read_positionfixes_csv(orig_file, sep=';')
        assert pfs.as_positionfixes

        pfs = pfs.drop(['geom'], axis=1)
        with pytest.raises(AttributeError):
            pfs.as_positionfixes
示例#12
0
def testdata_pfs_ids_geolife():
    pfs_file = os.path.join('tests', 'data', 'geolife',
                            'geolife_positionfixes_with_ids.csv')
    pfs = ti.read_positionfixes_csv(pfs_file, index_col='id')
    pfs['staypoint_id'] = pfs['staypoint_id'].astype('Int64')
    pfs['tripleg_id'] = pfs['tripleg_id'].astype('Int64')
    pfs.geometry = pfs.geometry.set_crs(epsg=4326)
    return pfs
示例#13
0
    def test_positionfixes_plot(self):
        """Use trackintel visualization function to plot positionfixes and check if file exists."""

        tmp_file = os.path.join('tests', 'data', 'positionfixes_plot.png')
        pfs_file = os.path.join('tests', 'data', 'positionfixes.csv')
        pfs = ti.read_positionfixes_csv(pfs_file, sep=';', index_col='id', crs='EPSG:4326')
        pfs.as_positionfixes.plot(out_filename=tmp_file, plot_osm=False)
        assert os.path.exists(tmp_file)
        os.remove(tmp_file)
示例#14
0
 def test_crs_warning(self):
     """Check if warning is raised for data without crs."""
     file = os.path.join("tests", "data", "positionfixes.csv")
     pfs = ti.read_positionfixes_csv(file,
                                     sep=";",
                                     crs=None,
                                     index_col=None)
     with pytest.warns(UserWarning):
         check_gdf_crs(pfs)
 def test_extract_staypoints_sliding_min(self):
     pfs = ti.read_positionfixes_csv('tests/data/positionfixes.csv',
                                     sep=';')
     spts = pfs.as_positionfixes.extract_staypoints(method='sliding',
                                                    dist_threshold=0,
                                                    time_threshold=0)
     assert len(spts) == len(
         pfs
     ), "With small thresholds, staypoint extraction should yield each positionfix"
示例#16
0
 def test_extract_triplegs_staypoint(self):
     pfs = ti.read_positionfixes_csv('tests/data/positionfixes.csv', sep=';')
     spts = pfs.as_positionfixes.extract_staypoints(method='sliding', dist_threshold=0, time_threshold=0)
     tpls1 = pfs.as_positionfixes.extract_triplegs()
     tpls2 = pfs.as_positionfixes.extract_triplegs(spts)
     assert len(tpls1) > 0, "There should be more than zero triplegs"
     assert len(tpls2) > 0, "There should be more than zero triplegs"
     assert len(tpls1) == len(tpls2), "If we extract the staypoints in the same way, it should lead to " + \
         "the same number of triplegs"
示例#17
0
    def test_read_positionfixes_gpd(self):
        gdf = gpd.read_file(os.path.join('tests', 'data', 'positionfixes.geojson'))
        gdf.set_index('id', inplace=True)
        pfs_from_gpd = ti.io.from_geopandas.read_positionfixes_gpd(gdf, user_id='User', geom='geometry', tz='utc')
        
        pfs_file = os.path.join('tests', 'data', 'positionfixes.csv')
        pfs_from_csv = ti.read_positionfixes_csv(pfs_file, sep=';', tz='utc', index_col='id')

        pd.testing.assert_frame_equal(pfs_from_gpd, pfs_from_csv, check_exact=False)
示例#18
0
 def test_transformation(self):
     """Check if data gets transformed."""
     file = os.path.join("tests", "data", "positionfixes.csv")
     pfs = ti.read_positionfixes_csv(file,
                                     sep=";",
                                     crs="EPSG:4326",
                                     index_col=None)
     pfs_2056 = pfs.to_crs("EPSG:2056")
     _, pfs_4326 = check_gdf_crs(pfs_2056, transform=True)
     assert_geodataframe_equal(pfs, pfs_4326, check_less_precise=True)
示例#19
0
    def test_loop_read(self):
        """Use read_geolife reader, store posfix as .csv, load them again."""
        pfs, _ = read_geolife(os.path.join("tests", "data", "geolife"), print_progress=True)

        saved_file = os.path.join("tests", "data", "positionfixes_test.csv")
        pfs.as_positionfixes.to_csv(saved_file)
        pfs_reRead = ti.read_positionfixes_csv(saved_file, index_col="id", crs="epsg:4326")
        os.remove(saved_file)

        assert_geoseries_equal(pfs.geometry, pfs_reRead.geometry)
示例#20
0
def test_data():
    """Read tests data from files."""
    pfs_file = os.path.join("examples", "data", "geolife_trajectory.csv")
    pfs = ti.read_positionfixes_csv(pfs_file,
                                    sep=";",
                                    index_col=None,
                                    crs="EPSG:4326")

    pfs, stps = pfs.as_positionfixes.generate_staypoints(method="sliding")
    return pfs, stps
示例#21
0
    def test_crs_warning(self):
        """Check if warning is raised for data without crs."""

        file = os.path.join('tests', 'data', 'positionfixes.csv')
        pfs = ti.read_positionfixes_csv(file,
                                        sep=';',
                                        crs=None,
                                        index_col=None)
        with pytest.warns(UserWarning):
            ti.visualization.util.transform_gdf_to_wgs84(pfs)
 def test_extract_staypoints_sliding_max(self):
     pfs = ti.read_positionfixes_csv('tests/data/positionfixes.csv',
                                     sep=';')
     spts = pfs.as_positionfixes.extract_staypoints(
         method='sliding',
         dist_threshold=sys.maxsize,
         time_threshold=sys.maxsize)
     assert len(
         spts
     ) == 0, "With large thresholds, staypoint extraction should not yield positionfixes"
示例#23
0
    def test_missing_link(self):
        """Test nan is assigned for missing link between stps and locs."""
        pfs_file = os.path.join("tests", "data", "positionfixes.csv")
        pfs = ti.read_positionfixes_csv(pfs_file, sep=";", tz="utc", index_col="id")
        _, stps = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=0, time_threshold=0)
        stps, _ = stps.as_staypoints.generate_locations(
            method="dbscan", epsilon=1e18, num_samples=1000, agg_level="user"
        )

        assert pd.isna(stps["location_id"]).any()
示例#24
0
    def test_transformation(self):
        """Check if data gets transformed."""

        file = os.path.join('tests', 'data', 'positionfixes.csv')
        pfs = ti.read_positionfixes_csv(file,
                                        sep=';',
                                        crs='EPSG:4326',
                                        index_col=None)
        pfs_2056 = pfs.to_crs("EPSG:2056")
        pfs_4326 = ti.visualization.util.transform_gdf_to_wgs84(pfs_2056)
        assert_geodataframe_equal(pfs, pfs_4326, check_less_precise=True)
示例#25
0
 def test_staypoints_plot(self):
     tmp_file = 'tests/data/staypoints_plot.png'
     pfs = ti.read_positionfixes_csv('tests/data/positionfixes.csv',
                                     sep=';')
     stps = ti.read_staypoints_csv('tests/data/staypoints.csv', sep=';')
     stps.as_staypoints.plot(out_filename=tmp_file,
                             radius=0.01,
                             positionfixes=pfs,
                             plot_osm=False)
     assert os.path.exists(tmp_file)
     os.remove(tmp_file)
示例#26
0
 def test_positionfixes_plot(self):
     """Use trackintel visualization function to plot positionfixes and check if the file exists."""
     tmp_file = os.path.join("tests", "data", "positionfixes_plot.png")
     pfs_file = os.path.join("tests", "data", "positionfixes.csv")
     pfs = ti.read_positionfixes_csv(pfs_file,
                                     sep=";",
                                     index_col="id",
                                     crs="EPSG:4326")
     pfs.as_positionfixes.plot(out_filename=tmp_file, plot_osm=False)
     assert os.path.exists(tmp_file)
     os.remove(tmp_file)
 def test_cluster_staypoints_dbscan_max(self):
     pfs = ti.read_positionfixes_csv('tests/data/positionfixes.csv',
                                     sep=';')
     spts = pfs.as_positionfixes.extract_staypoints(method='sliding',
                                                    dist_threshold=0,
                                                    time_threshold=0)
     _, clusters = spts.as_staypoints.extract_locations(method='dbscan',
                                                        epsilon=1e18,
                                                        num_samples=1000)
     assert len(
         clusters
     ) == 0, "With large hyperparameters, everything is an outlier"
示例#28
0
 def test_generate_staypoints_sliding_min(self):
     pfs_file = os.path.join('tests', 'data', 'positionfixes.csv')
     pfs = ti.read_positionfixes_csv(pfs_file,
                                     sep=';',
                                     tz='utc',
                                     index_col='id')
     pfs, stps = pfs.as_positionfixes.generate_staypoints(method='sliding',
                                                          dist_threshold=0,
                                                          time_threshold=0)
     assert len(stps) == len(
         pfs
     ), "With small thresholds, staypoint extraction should yield each positionfix"
 def test_cluster_staypoints_dbscan_min(self):
     pfs = ti.read_positionfixes_csv('tests/data/positionfixes.csv',
                                     sep=';')
     spts = pfs.as_positionfixes.extract_staypoints(method='sliding',
                                                    dist_threshold=0,
                                                    time_threshold=0)
     _, clusters = spts.as_staypoints.extract_locations(method='dbscan',
                                                        epsilon=1e-18,
                                                        num_samples=0)
     assert len(clusters) == len(
         spts
     ), "With small hyperparameters, clustering should not reduce the number"
示例#30
0
 def test_set_datatime_tz(self):
     """Test setting the timezone infomation when reading."""
     # check if tz is added to the datatime column
     file = os.path.join('tests', 'data', 'positionfixes.csv')
     pfs = ti.read_positionfixes_csv(file, sep=';', index_col="id")
     assert pd.api.types.is_datetime64tz_dtype(pfs["tracked_at"])
     
     # check if a timezone will be set after manually deleting the timezone
     pfs['tracked_at'] = pfs['tracked_at'].dt.tz_localize(None)
     assert not pd.api.types.is_datetime64tz_dtype(pfs["tracked_at"])
     tmp_file = os.path.join('tests', 'data', 'positionfixes_test.csv')
     pfs.as_positionfixes.to_csv(tmp_file, sep=';')
     pfs = ti.read_positionfixes_csv(tmp_file, sep=';', index_col="id", tz = 'utc')
     
     assert pd.api.types.is_datetime64tz_dtype(pfs["tracked_at"])
     
     # check if a warning is raised if 'tz' is not provided
     with pytest.warns(UserWarning):
         ti.read_positionfixes_csv(tmp_file, sep=';', index_col="id")
     
     os.remove(tmp_file)