File size: 3,835 Bytes
3befc7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Stage 9: validate Trip World and write README.md.

Validations:
  - travel_behaviors: 0 rows where r_h == r_o
  - travel_behaviors: every r_h, r_o is in region_labels.parquet
  - pois: every fsq_place_id in checkins_consolidated also in pois.parquet
  - region_labels: every region_id is reached by some travel record
"""
import duckdb, json, time, os
from pathlib import Path

ROOT = Path(os.environ.get("TRIP_WORLD_ROOT", Path(__file__).resolve().parent.parent))
TB = ROOT / "travel_behaviors.parquet"
POIS = ROOT / "pois.parquet"
RL = ROOT / "region_labels.parquet"
META = ROOT / "metadata" / "metadata_all.parquet"
REVIEWS = ROOT / "reviews" / "reviews_all.parquet"
INC = ROOT / "_intermediate" / "checkins_consolidated.parquet"
MAP = ROOT / "metro_mapping_clean.json"

t0 = time.time()
def step(msg): print(f"[{time.time()-t0:6.1f}s] {msg}", flush=True)

con = duckdb.connect()
con.execute("PRAGMA threads=32")
con.execute("SET memory_limit='32GB'")

# ---- validations ----
step("validating ...")
v = {}
v["tb_rows"]    = con.execute(f"SELECT COUNT(*) FROM '{TB}'").fetchone()[0]
v["tb_phantom"] = con.execute(f"SELECT COUNT(*) FROM '{TB}' WHERE r_h = r_o").fetchone()[0]
v["tb_users"]   = con.execute(f"SELECT COUNT(DISTINCT user_id) FROM '{TB}'").fetchone()[0]
v["tb_homes"]   = con.execute(f"SELECT COUNT(DISTINCT r_h) FROM '{TB}'").fetchone()[0]
v["tb_dests"]   = con.execute(f"SELECT COUNT(DISTINCT r_o) FROM '{TB}'").fetchone()[0]
v["tb_pairs"]   = con.execute(f"SELECT COUNT(*) FROM (SELECT DISTINCT r_h, r_o FROM '{TB}')").fetchone()[0]
v["tb_avg_ch"]  = con.execute(f"SELECT AVG(n_home_ci) FROM '{TB}'").fetchone()[0]
v["tb_avg_co"]  = con.execute(f"SELECT AVG(n_travel_ci) FROM '{TB}'").fetchone()[0]
v["tb_unique_ci_h"] = con.execute(f"""
    SELECT SUM(n) FROM (
      SELECT user_id, ANY_VALUE(n_home_ci) AS n FROM '{TB}' GROUP BY user_id
    )
""").fetchone()[0]
v["tb_total_ci_o"] = con.execute(f"SELECT SUM(n_travel_ci) FROM '{TB}'").fetchone()[0]
v["tb_total_ci"] = v["tb_unique_ci_h"] + v["tb_total_ci_o"]

v["pois_rows"]      = con.execute(f"SELECT COUNT(*) FROM '{POIS}'").fetchone()[0]
v["pois_with_meta"] = con.execute(f"SELECT SUM(CASE WHEN has_google_metadata THEN 1 ELSE 0 END) FROM '{POIS}'").fetchone()[0]
v["pois_with_rev"]  = con.execute(f"SELECT SUM(CASE WHEN has_reviews THEN 1 ELSE 0 END) FROM '{POIS}'").fetchone()[0]
v["pois_total_reviews"] = con.execute(f"SELECT SUM(n_reviews) FROM '{POIS}'").fetchone()[0]
v["pois_reviews_text"]  = con.execute(f"SELECT SUM(n_reviews_with_text) FROM '{POIS}'").fetchone()[0]

v["rl_rows"]    = con.execute(f"SELECT COUNT(*) FROM '{RL}'").fetchone()[0]

# referential integrity
v["rh_missing_in_rl"] = con.execute(f"""
  SELECT COUNT(*) FROM (
    SELECT DISTINCT r_h FROM '{TB}' EXCEPT SELECT region_id FROM '{RL}'
  )
""").fetchone()[0]
v["ro_missing_in_rl"] = con.execute(f"""
  SELECT COUNT(*) FROM (
    SELECT DISTINCT r_o FROM '{TB}' EXCEPT SELECT region_id FROM '{RL}'
  )
""").fetchone()[0]

# distinct trails in c_h ∪ c_o
v["distinct_trails"] = con.execute(f"""
  SELECT COUNT(DISTINCT trail_id) FROM (
    SELECT UNNEST(c_h).trail_id AS trail_id FROM '{TB}'
    UNION ALL
    SELECT UNNEST(c_o).trail_id AS trail_id FROM '{TB}'
  )
""").fetchone()[0]

# time span
ts_range = con.execute(f"""
  SELECT MIN(ts), MAX(ts) FROM (
    SELECT UNNEST(c_h).ts AS ts FROM '{TB}'
    UNION ALL
    SELECT UNNEST(c_o).ts AS ts FROM '{TB}'
  )
""").fetchone()
v["ts_min"], v["ts_max"] = ts_range

step("done validating")
for k, val in v.items():
    print(f"  {k:24s}: {val}")

# The public-facing README.md is maintained separately and ships at the
# top level of the dataset release; this script does not regenerate it.
# The validation results above are sufficient to confirm a successful build.
print(f"\n  total elapsed:       {time.time()-t0:.1f}s")