Analyze a simple trajectory
In this tutorial, we will create a small trajectory dataset, compute two mobility measures, and inspect the results.
We use pandas so the example is easy to read. Fastmob also accepts other eager dataframe backends; see Columns and backends when adapting this to your data.
Before we start
Make sure fastmob and pandas are installed in your Python environment:
Start a Python session or create a file named simple_trajectory.py.
Step 1: Create a trajectory dataframe
First, create a small dataframe with two users, timestamps, and coordinates:
import pandas as pd
df = pd.DataFrame({
"uid": ["alice", "alice", "alice", "bob", "bob", "bob"],
"datetime": pd.to_datetime([
"2020-01-01 08:00:00",
"2020-01-01 09:00:00",
"2020-01-01 10:00:00",
"2020-01-01 08:00:00",
"2020-01-01 09:00:00",
"2020-01-01 10:00:00",
]),
"lat": [41.8902, 41.9028, 41.9109, 40.7128, 40.7306, 40.7580],
"lng": [12.4922, 12.4964, 12.4818, -74.0060, -73.9352, -73.9855],
})
print(df)
The output should look something like:
uid datetime lat lng
0 alice 2020-01-01 08:00:00 41.8902 12.4922
1 alice 2020-01-01 09:00:00 41.9028 12.4964
2 alice 2020-01-01 10:00:00 41.9109 12.4818
3 bob 2020-01-01 08:00:00 40.7128 -74.0060
4 bob 2020-01-01 09:00:00 40.7306 -73.9352
5 bob 2020-01-01 10:00:00 40.7580 -73.9855
Notice that each row is one point in a user's trajectory.
Step 2: Compute jump lengths
Now compute the distance between each user's consecutive points:
The output should look something like:
Notice that fastmob returns one row per user. The jump_lengths value is a list because each user has more than one movement between points.
Step 3: Compute radius of gyration
Next, compute how widely each user moves around their center of mass:
The output should look something like:
Notice that this result has one number per user. A larger radius of gyration means the user's points are more spread out.
Step 4: Run the whole example
Let's put the pieces together:
import pandas as pd
from fastmob import jump_lengths, radius_of_gyration
df = pd.DataFrame({
"uid": ["alice", "alice", "alice", "bob", "bob", "bob"],
"datetime": pd.to_datetime([
"2020-01-01 08:00:00",
"2020-01-01 09:00:00",
"2020-01-01 10:00:00",
"2020-01-01 08:00:00",
"2020-01-01 09:00:00",
"2020-01-01 10:00:00",
]),
"lat": [41.8902, 41.9028, 41.9109, 40.7128, 40.7306, 40.7580],
"lng": [12.4922, 12.4964, 12.4818, -74.0060, -73.9352, -73.9855],
})
print(jump_lengths(df))
print(radius_of_gyration(df))
You should see two small result dataframes, both grouped by user.
What we have made
You have created a minimal trajectory dataset and used fastmob to compute two user-level mobility measures. The same pattern works for larger trajectory dataframes: build a dataframe with time, latitude, longitude, and user columns, then pass it to the measure you want to compute.