"""Validate African Mobile Subscriber Data dataset.""" import csv import os import sys from collections import Counter DATA_DIR = os.path.join(os.path.dirname(__file__), "data") CSV_PATH = os.path.join(DATA_DIR, "african_mobile_subscribers.csv") JSONL_PATH = os.path.join(DATA_DIR, "african_mobile_subscribers.jsonl") EXPECTED_COLUMNS = [ "record_id", "country", "year", "quarter", "operator", "technology", "active_subscribers_millions", "arpu_usd", "monthly_churn_pct", "prepaid_share_pct", "data_revenue_share_pct", "voice_revenue_share_pct", "mobile_money_subscribers_millions", "data_usage_gb_per_user", "network_coverage_pct", "market_share_pct", "spectrum_efficiency_index", "customer_satisfaction_score", "scenario", ] EXPECTED_SCENARIOS = {"baseline", "5g_rollout", "market_saturation"} EXPECTED_COUNTRIES = 15 EXPECTED_TECHNOLOGIES = {"2g", "3g", "4g", "5g"} EXPECTED_QUARTERS = {"Q1", "Q2", "Q3", "Q4"} EXPECTED_RECORDS = 30000 errors = [] warnings = [] def log_error(msg: str): errors.append(msg) print(f" [ERROR] {msg}") def log_warn(msg: str): warnings.append(msg) print(f" [WARN] {msg}") def validate_csv(): print(f"Reading CSV: {CSV_PATH}") if not os.path.exists(CSV_PATH): log_error(f"CSV file not found: {CSV_PATH}") return [] with open(CSV_PATH, "r", encoding="utf-8") as f: reader = csv.DictReader(f) rows = list(reader) # Column check if reader.fieldnames != EXPECTED_COLUMNS: missing = set(EXPECTED_COLUMNS) - set(reader.fieldnames or []) extra = set(reader.fieldnames or []) - set(EXPECTED_COLUMNS) if missing: log_error(f"Missing columns: {missing}") if extra: log_warn(f"Extra columns: {extra}") print(f" Rows: {len(rows)}") if len(rows) != EXPECTED_RECORDS: log_error(f"Expected {EXPECTED_RECORDS} rows, got {len(rows)}") return rows def validate_records(rows: list): print("\nValidating records...") ids = [r["record_id"] for r in rows] if len(ids) != len(set(ids)): log_error("Duplicate record_id values found") countries = set(r["country"] for r in rows) if len(countries) != EXPECTED_COUNTRIES: log_error(f"Expected {EXPECTED_COUNTRIES} countries, found {len(countries)}: {sorted(countries)}") scenarios = set(r["scenario"] for r in rows) if scenarios != EXPECTED_SCENARIOS: log_error(f"Expected scenarios {EXPECTED_SCENARIOS}, found {scenarios}") # Per-scenario counts scenario_counts = Counter(r["scenario"] for r in rows) for sc, count in scenario_counts.items(): print(f" {sc}: {count} records") if count != 10000: log_error(f"Scenario '{sc}' has {count} records, expected 10000") # Validate technology values bad_tech = [r["record_id"] for r in rows if r["technology"] not in EXPECTED_TECHNOLOGIES] if bad_tech: log_error(f"Invalid technology values in records: {bad_tech[:5]}...") # Validate quarter values bad_q = [r["record_id"] for r in rows if r["quarter"] not in EXPECTED_QUARTERS] if bad_q: log_error(f"Invalid quarter values in records: {bad_q[:5]}...") # Numeric range checks for r in rows: rid = r["record_id"] subs = float(r["active_subscribers_millions"]) if subs < 0 or subs > 250: log_error(f"Record {rid}: active_subscribers_millions={subs} out of range") arpu = float(r["arpu_usd"]) if arpu < 0 or arpu > 20: log_error(f"Record {rid}: arpu_usd={arpu} out of range") churn = float(r["monthly_churn_pct"]) if churn < 0 or churn > 20: log_error(f"Record {rid}: monthly_churn_pct={churn} out of range") prepaid = float(r["prepaid_share_pct"]) if prepaid < 0 or prepaid > 100: log_error(f"Record {rid}: prepaid_share_pct={prepaid} out of range") data_rev = float(r["data_revenue_share_pct"]) voice_rev = float(r["voice_revenue_share_pct"]) total_rev = round(data_rev + voice_rev, 2) if abs(total_rev - 100.0) > 0.5: log_warn(f"Record {rid}: data+voice revenue={total_rev} (not ~100%)") mm_subs = float(r["mobile_money_subscribers_millions"]) if mm_subs < 0: log_error(f"Record {rid}: mobile_money_subscribers_millions={mm_subs} negative") data_usage = float(r["data_usage_gb_per_user"]) if data_usage < 0 or data_usage > 50: log_warn(f"Record {rid}: data_usage_gb_per_user={data_usage} unusual") coverage = float(r["network_coverage_pct"]) if coverage < 0 or coverage > 100: log_error(f"Record {rid}: network_coverage_pct={coverage} out of range") mkt_share = float(r["market_share_pct"]) if mkt_share < 0 or mkt_share > 100: log_error(f"Record {rid}: market_share_pct={mkt_share} out of range") spectrum = float(r["spectrum_efficiency_index"]) if spectrum < 0 or spectrum > 10: log_warn(f"Record {rid}: spectrum_efficiency_index={spectrum} unusual") satisfaction = float(r["customer_satisfaction_score"]) if satisfaction < 1 or satisfaction > 5: log_error(f"Record {rid}: customer_satisfaction_score={satisfaction} out of range") # Country-specific checks ng_rows = [r for r in rows if r["country"] == "Nigeria"] ng_total = sum(float(r["active_subscribers_millions"]) for r in ng_rows) / len(ng_rows) * len(set(r["operator"] for r in ng_rows)) print(f"\n Nigeria avg total subscribers estimate: {ng_total:.0f}M") sa_rows = [r for r in rows if r["country"] == "South Africa"] sa_total = sum(float(r["active_subscribers_millions"]) for r in sa_rows) / len(sa_rows) * len(set(r["operator"] for r in sa_rows)) print(f" South Africa avg total subscribers estimate: {sa_total:.0f}M") # Kenya Safaricom mobile money dominance ke_saf = [r for r in rows if r["country"] == "Kenya" and "Safaricom" in r["operator"]] if ke_saf: avg_mm = sum(float(r["mobile_money_subscribers_millions"]) for r in ke_saf) / len(ke_saf) avg_subs = sum(float(r["active_subscribers_millions"]) for r in ke_saf) / len(ke_saf) print(f" Kenya Safaricom avg M-Pesa subs: {avg_mm:.1f}M (avg active: {avg_subs:.1f}M)") def validate_jsonl(): print(f"\nValidating JSONL: {JSONL_PATH}") if not os.path.exists(JSONL_PATH): log_error(f"JSONL file not found: {JSONL_PATH}") return import json with open(JSONL_PATH, "r", encoding="utf-8") as f: lines = f.readlines() print(f" JSONL lines: {len(lines)}") if len(lines) != EXPECTED_RECORDS: log_error(f"Expected {EXPECTED_RECORDS} JSONL lines, got {len(lines)}") for i, line in enumerate(lines[:5]): try: json.loads(line.strip()) except json.JSONDecodeError: log_error(f"Invalid JSON on line {i+1}") break def main(): print("=" * 60) print("African Mobile Subscriber Data — Validation Report") print("=" * 60) rows = validate_csv() if rows: validate_records(rows) validate_jsonl() print("\n" + "=" * 60) if errors: print(f"FAILED: {len(errors)} error(s), {len(warnings)} warning(s)") for e in errors: print(f" ERROR: {e}") sys.exit(1) else: print(f"PASSED: 0 errors, {len(warnings)} warning(s)") if warnings: for w in warnings: print(f" WARN: {w}") sys.exit(0) if __name__ == "__main__": main()