Skip to content

Commit

Permalink
create od truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
YueeeeeLi committed Dec 18, 2024
1 parent d06f586 commit 2b049c2
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions scripts/od_truncation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# %%
from pathlib import Path
import os
import pandas as pd
from tqdm import tqdm
from nird.utils import load_config


base_path = Path(load_config()["paths"]["base_path"])
tqdm.pandas()

od_df = pd.read_csv(
os.path.join(base_path, "census_datasets", "od_matrix", "od_gb_oa_2021_node.csv")
)
od_df = od_df[od_df["Car21"] > 0]

od_df = od_df.sort_values(by=["Car21"], ascending=False)
od_df["car_total"] = od_df.groupby(["origin_node"])["Car21"].transform("sum")
od_df["car_cumsum"] = od_df.groupby(["origin_node"])["Car21"].transform("cumsum")
od_df["fraction"] = od_df["car_cumsum"] / od_df["car_total"]
od_df["count"] = od_df.groupby(["origin_node"])["origin_node"].transform("count")

before = od_df["Car21"].sum()
print("Before trunction...")
print(f"The total number of trips: {before} ")
print(f"The number of origins: {od_df.origin_node.unique().shape[0]}")
print(f"The number of destinations: {od_df.shape[0]}")

# od_df = od_df[(od_df["fraction"] < 0.7) | (od_df["count"] == 1)]

od_df = od_df[
(od_df["Car21"] > 1) # 32.6% (~1.79 millon)
| (od_df["count"] == 1) # 32.7% (~1.81 million)
# | ((od_df["Car21"] == 1) & (od_df["fraction"] < 0.7)) # 69.2% (~7.74 million)
]

after = od_df["Car21"].sum()
print("After trunction...")
print(f"The total number of trips: {after} ")
print(f"The number of origins: {od_df.origin_node.unique().shape[0]}")
print(f"The number of destinations: {od_df.shape[0]}")

print(f"The number of trip reduction: {before - after}")
print(f"The percentage of trips retained: {1.0 * after / before}")

0 comments on commit 2b049c2

Please sign in to comment.