Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST/CLN: Test structure #197

Merged
merged 3 commits into from
Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@ def setup(app):
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
# 'searchbox.html']``.
#
html_sidebars = {"**": ["about.html", "navigation.html", "relations.html", "searchbox.html", "donate.html",]}
html_sidebars = {
"**": [
"about.html",
"navigation.html",
"relations.html",
"searchbox.html",
"donate.html",
]
}

# only defined in 'Alabaster' html_theme
# html_theme_options = {
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ This will additionally plot the original positionfixes, as well as the underlyin
street network from OSM. We can for example continue by extracting and plotting locations
(locations that "contain" multiple staypoints, i.e., are visited often by a user)::

locs = spts.as_staypoints.extract_locations(method='dbscan',
locs = stps.as_staypoints.extract_locations(method='dbscan',
epsilon=meters_to_decimal_degrees(120, 47.5), num_samples=3)
locs.as_locations.plot(out_filename='locations.png',
radius=meters_to_decimal_degrees(120, 47.5), positionfixes=pfs, staypoints=spts,
radius=meters_to_decimal_degrees(120, 47.5), positionfixes=pfs, staypoints=stps,
staypoints_radius=meters_to_decimal_degrees(100, 47.5), plot_osm=True)

This will extract locations and plot them to a file called ``locations.png``, additionally
Expand Down
24 changes: 12 additions & 12 deletions examples/preprocess_trajectories.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,47 @@
pfs = ti.read_positionfixes_csv("examples/data/geolife_trajectory.csv", sep=";", crs="EPSG:4326", index_col=None)
pfs.as_positionfixes.plot(out_filename="examples/out/gps_trajectory_positionfixes.png", plot_osm=True)

_, spts = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=100, time_threshold=5)
spts.as_staypoints.plot(
_, stps = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=100, time_threshold=5)
stps.as_staypoints.plot(
out_filename="examples/out/gps_trajectory_staypoints.png", radius=100, positionfixes=pfs, plot_osm=True
)

_, locs = spts.as_staypoints.generate_locations(method="dbscan", epsilon=0.01, num_samples=3)
_, locs = stps.as_staypoints.generate_locations(method="dbscan", epsilon=0.01, num_samples=3)
locs.as_locations.plot(
out_filename="examples/out/gps_trajectory_locations.png",
radius=120,
positionfixes=pfs,
staypoints=spts,
staypoints=stps,
staypoints_radius=100,
plot_osm=True,
)

_, tpls = pfs.as_positionfixes.generate_triplegs(stps_input=spts)
_, tpls = pfs.as_positionfixes.generate_triplegs(stps_input=stps)
tpls.as_triplegs.plot(
out_filename="examples/out/gpsies_trajectory_triplegs.png", staypoints=spts, staypoints_radius=100, plot_osm=True
out_filename="examples/out/gpsies_trajectory_triplegs.png", staypoints=stps, staypoints_radius=100, plot_osm=True
)

# Geolife trajectory.
pfs = ti.read_positionfixes_csv("examples/data/geolife_trajectory.csv", sep=";", crs="EPSG:4326", index_col=None)
pfs.as_positionfixes.plot(out_filename="examples/out/geolife_trajectory_positionfixes.png", plot_osm=False)

_, spts = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=100, time_threshold=10)
spts.as_staypoints.plot(
_, stps = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=100, time_threshold=10)
stps.as_staypoints.plot(
out_filename="examples/out/geolife_trajectory_staypoints.png", radius=100, positionfixes=pfs, plot_osm=True
)

# Google trajectory.
pfs = ti.read_positionfixes_csv("examples/data/google_trajectory.csv", sep=";", crs="EPSG:4326", index_col=None)
_, spts = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=75, time_threshold=10)
spts.as_staypoints.plot(
_, stps = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=75, time_threshold=10)
stps.as_staypoints.plot(
out_filename="examples/out/google_trajectory_staypoints.png", radius=75, positionfixes=pfs, plot_osm=True
)

# Posmo trajectory.
pfs = ti.read_positionfixes_csv(
"examples/data/posmo_trajectory.csv", sep=";", crs="EPSG:4326", index_col=None, tz="UTC"
)
_, spts = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=50, time_threshold=1)
spts.as_staypoints.plot(
_, stps = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=50, time_threshold=1)
stps.as_staypoints.plot(
out_filename="examples/out/posmo_trajectory_staypoints.png", radius=50, positionfixes=pfs, plot_osm=False
)
6 changes: 3 additions & 3 deletions tests/analysis/test_modal_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
def read_geolife_with_modes():
pfs, labels = read_geolife(os.path.join("tests", "data", "geolife_modes"))

pfs, spts = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=25, time_threshold=5)
_, tpls = pfs.as_positionfixes.generate_triplegs(spts, method="between_staypoints")
pfs, stps = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=25, time_threshold=5)
_, tpls = pfs.as_positionfixes.generate_triplegs(stps, method="between_staypoints")

tpls_with_modes = geolife_add_modes_to_triplegs(tpls, labels)
return tpls_with_modes
Expand All @@ -36,7 +36,7 @@ def ls_long():

@pytest.fixture
def test_triplegs_modal_split(ls_short, ls_long):
""" Triplegs with transport modes that can be aggregated over days and weeks.
"""Triplegs with transport modes that can be aggregated over days and weeks.

user 0: day 1: 2 triplegs (car + bike)
day 2: 1 tripleg (walk)
Expand Down
4 changes: 2 additions & 2 deletions tests/analysis/test_transport_mode_identification.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class TestTransportModeIdentification:
def test_check_empty_dataframe(self):
"""Assert that the method does not work for empty DataFrames
"""Assert that the method does not work for empty DataFrames
(but that the rest works fine, e.g., method signature).
"""
tpls_file = os.path.join("tests", "data", "triplegs_transport_mode_identification.csv")
Expand All @@ -20,7 +20,7 @@ def test_check_empty_dataframe(self):
empty_frame.as_triplegs.predict_transport_mode(method="simple-coarse")

def test_simple_coarse_identification_no_crs(self):
"""Assert that the simple-coarse transport mode identification throws the correct
"""Assert that the simple-coarse transport mode identification throws the correct
warning and and yields the correct results for WGS84.
"""
tpls_file = os.path.join("tests", "data", "triplegs_transport_mode_identification.csv")
Expand Down
26 changes: 13 additions & 13 deletions tests/geogr/test_distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class TestCalculate_distance_matrix:
"""Tests for the calculate_distance_matrix() function."""

def test_shape_for_different_array_length(self):
spts_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
spts = ti.read_staypoints_csv(spts_file, tz="utc", index_col="id")
stps_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
stps = ti.read_staypoints_csv(stps_file, tz="utc", index_col="id")

x = spts.iloc[0:5]
y = spts.iloc[5:15]
x = stps.iloc[0:5]
y = stps.iloc[5:15]

d_euc1 = calculate_distance_matrix(X=x, Y=y, dist_metric="euclidean")
d_euc2 = calculate_distance_matrix(X=y, Y=x, dist_metric="euclidean")
Expand All @@ -76,11 +76,11 @@ def test_shape_for_different_array_length(self):
assert np.isclose(0, np.sum(np.abs(d_hav1 - d_hav2.T)))

def test_keyword_combinations(self):
spts_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
spts = ti.read_staypoints_csv(spts_file, tz="utc", index_col="id")
stps_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
stps = ti.read_staypoints_csv(stps_file, tz="utc", index_col="id")

x = spts.iloc[0:5]
y = spts.iloc[5:15]
x = stps.iloc[0:5]
y = stps.iloc[5:15]

_ = calculate_distance_matrix(X=x, Y=y, dist_metric="euclidean", n_jobs=-1)
_ = calculate_distance_matrix(X=y, Y=x, dist_metric="haversine", n_jobs=-1)
Expand All @@ -92,12 +92,12 @@ def test_keyword_combinations(self):
assert np.array_equal(d_euc, d_mink2)

def test_compare_haversine_to_scikit_xy(self):
spts_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
spts = ti.read_staypoints_csv(spts_file, tz="utc", index_col="id")
our_d_matrix = calculate_distance_matrix(X=spts, Y=spts, dist_metric="haversine")
stps_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
stps = ti.read_staypoints_csv(stps_file, tz="utc", index_col="id")
our_d_matrix = calculate_distance_matrix(X=stps, Y=stps, dist_metric="haversine")

x = spts.geometry.x.values
y = spts.geometry.y.values
x = stps.geometry.x.values
y = stps.geometry.y.values

x_rad = np.asarray([radians(_) for _ in x])
y_rad = np.asarray([radians(_) for _ in y])
Expand Down
4 changes: 2 additions & 2 deletions tests/io/test_dataset_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def read_geolife_modes():
@pytest.fixture
def read_geolife_triplegs_with_modes(read_geolife_modes):
pfs, labels = read_geolife_modes
pfs, spts = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=25, time_threshold=5)
_, tpls = pfs.as_positionfixes.generate_triplegs(spts, method="between_staypoints")
pfs, stps = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=25, time_threshold=5)
_, tpls = pfs.as_positionfixes.generate_triplegs(stps, method="between_staypoints")

return tpls, labels

Expand Down
24 changes: 12 additions & 12 deletions tests/preprocessing/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
def locs_from_geolife():
"""Create locations from geolife staypoints."""
# read staypoints
spts_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
spts = ti.read_staypoints_csv(spts_file, tz="utc", index_col="id")
stps_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
stps = ti.read_staypoints_csv(stps_file, tz="utc", index_col="id")

# cluster staypoints to locations
_, locs = spts.as_staypoints.generate_locations(
_, locs = stps.as_staypoints.generate_locations(
method="dbscan", epsilon=10, num_samples=0, distance_matrix_metric="haversine", agg_level="dataset"
)

Expand All @@ -29,26 +29,26 @@ class TestSpatial_filter:
def test_filter_staypoints(self):
"""Test if spatial_filter works for staypoints."""
# read staypoints and area file
spts_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
spts = ti.read_staypoints_csv(spts_file, tz="utc", index_col="id")
stps_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
stps = ti.read_staypoints_csv(stps_file, tz="utc", index_col="id")
extent = gpd.read_file(os.path.join("tests", "data", "area", "tsinghua.geojson"))

# the projection needs to be defined: WGS84
spts.crs = "epsg:4326"
within_spts = spts.as_staypoints.spatial_filter(areas=extent, method="within", re_project=True)
intersects_spts = spts.as_staypoints.spatial_filter(areas=extent, method="intersects", re_project=True)
crosses_spts = spts.as_staypoints.spatial_filter(areas=extent, method="crosses", re_project=True)
stps.crs = "epsg:4326"
within_stps = stps.as_staypoints.spatial_filter(areas=extent, method="within", re_project=True)
intersects_stps = stps.as_staypoints.spatial_filter(areas=extent, method="intersects", re_project=True)
crosses_stps = stps.as_staypoints.spatial_filter(areas=extent, method="crosses", re_project=True)

# the result obtained from ArcGIS
gis_within_num = 13

assert len(within_spts) == gis_within_num, (
assert len(within_stps) == gis_within_num, (
"The spatial filtered sp number should be the same as" + "the one from the result with ArcGIS"
)
assert len(crosses_spts) == 0, "There will be no point crossing area"
assert len(crosses_stps) == 0, "There will be no point crossing area"

# For staypoints the result of within and intersects should be the same
assert_geodataframe_equal(within_spts, intersects_spts, check_less_precise=True)
assert_geodataframe_equal(within_stps, intersects_stps, check_less_precise=True)

def test_filter_triplegs(self):
"""Test if spatial_filter works for triplegs."""
Expand Down
2 changes: 1 addition & 1 deletion tests/preprocessing/test_positionfixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def test_temporal(self, geolife_pfs_stps_long):
assert (pfs["diff"] > gap_threshold).all()

def test_stps_tpls_overlap(self, geolife_pfs_stps_long):
"""Tpls and spts should not overlap when generated using the default extract triplegs method."""
"""Tpls and stps should not overlap when generated using the default extract triplegs method."""
pfs, stps = geolife_pfs_stps_long
pfs, tpls = pfs.as_positionfixes.generate_triplegs(stps)

Expand Down
15 changes: 14 additions & 1 deletion tests/preprocessing/test_staypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@


class TestGenerate_locations:
"""Tests for generate_locations() method."""

def test_generate_locations_dbscan_hav_euc(self):
stps_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
stps = ti.read_staypoints_csv(stps_file, tz="utc", index_col="id")
Expand Down Expand Up @@ -172,4 +174,15 @@ def test_generate_locations_index_start(self):


class TestCreate_activity_flag:
pass
"""Tests for create_activity_flag() method."""

def test_create_activity_flag(self):
stps_file = os.path.join("tests", "data", "geolife", "geolife_staypoints.csv")
stps_test = ti.read_staypoints_csv(stps_file, tz="utc", index_col="id")

activity_true = stps_test["activity"].copy()
stps_test["activity"] = False

stps_test = stps_test.as_staypoints.create_activity_flag()

pd.testing.assert_series_equal(stps_test["activity"], activity_true)
12 changes: 6 additions & 6 deletions tests/preprocessing/test_triplegs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_generate_trips(self):
pd.testing.assert_frame_equal(trips_loaded, trips)

def test_generate_trips_missing_link(self):
"""Test nan is assigned for missing link between spts and trips, and tpls and trips."""
"""Test nan is assigned for missing link between stps and trips, and tpls and trips."""
# create trips from geolife (based on positionfixes)
pfs, _ = ti.io.dataset_reader.read_geolife(os.path.join("tests", "data", "geolife_long"))
pfs, stps = pfs.as_positionfixes.generate_staypoints(method="sliding", dist_threshold=25, time_threshold=5)
Expand Down Expand Up @@ -146,13 +146,13 @@ def test_generate_trips_gap_detection(self):

# generate trips and a joint staypoint/triplegs dataframe
stps_proc, tpls_proc, trips = ti.preprocessing.triplegs.generate_trips(stps_in, tpls_in, gap_threshold=15)
spts_tpls = _create_debug_spts_tpls_data(stps_proc, tpls_proc, gap_threshold=gap_threshold)
stps_tpls = _create_debug_stps_tpls_data(stps_proc, tpls_proc, gap_threshold=gap_threshold)

# test if generated trips are equal
pd.testing.assert_frame_equal(trips_loaded, trips)

# test if generated staypoints/triplegs are equal (especially important for trip ids)
pd.testing.assert_frame_equal(stps_tpls_loaded, spts_tpls, check_dtype=False)
pd.testing.assert_frame_equal(stps_tpls_loaded, stps_tpls, check_dtype=False)

def test_generate_trips_id_management(self):
"""Test if we can generate the example trips based on example data."""
Expand All @@ -170,13 +170,13 @@ def test_generate_trips_id_management(self):
# generate trips and a joint staypoint/triplegs dataframe
gap_threshold = 15
stps, tpls, _ = ti.preprocessing.triplegs.generate_trips(stps, tpls, gap_threshold=gap_threshold)
spts_tpls = _create_debug_spts_tpls_data(stps, tpls, gap_threshold=gap_threshold)
stps_tpls = _create_debug_stps_tpls_data(stps, tpls, gap_threshold=gap_threshold)

# test if generated staypoints/triplegs are equal (especially important for trip ids)
pd.testing.assert_frame_equal(stps_tpls_loaded, spts_tpls, check_dtype=False)
pd.testing.assert_frame_equal(stps_tpls_loaded, stps_tpls, check_dtype=False)


def _create_debug_spts_tpls_data(stps, tpls, gap_threshold):
def _create_debug_stps_tpls_data(stps, tpls, gap_threshold):
"""Preprocess stps and tpls for "test_generate_trips_*."""
# create table with relevant information from triplegs and staypoints.
tpls["type"] = "tripleg"
Expand Down
11 changes: 11 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import trackintel as ti


class TestPrint_version:
"""Tests for print_version() method."""

def test_print_version(self, capsys):
"""Check if the correct message is printed."""
ti.print_version()
captured = capsys.readouterr()
assert "This is trackintel v" in captured.out
21 changes: 0 additions & 21 deletions tests/test_staypoints.py

This file was deleted.

Loading