run_pipeline.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python3
  2. """Run behavior features, social features, and final aggregation in sequence."""
  3. from __future__ import annotations
  4. import argparse
  5. import subprocess
  6. import sys
  7. from pathlib import Path
  8. PROJECT_DIR = Path(__file__).resolve().parents[1]
  9. SCRIPTS_DIR = PROJECT_DIR / "scripts"
  10. def run(command: list[str]) -> None:
  11. print("+ " + " ".join(command), flush=True)
  12. subprocess.run(command, check=True)
  13. def main() -> None:
  14. parser = argparse.ArgumentParser(description=__doc__)
  15. parser.add_argument("--input", type=Path, required=True)
  16. parser.add_argument("--run-name", required=True)
  17. parser.add_argument("--lookback-days", type=int, default=180)
  18. parser.add_argument("--skip-behavior", action="store_true")
  19. parser.add_argument("--skip-social", action="store_true")
  20. parser.add_argument("--sql-only", action="store_true")
  21. args = parser.parse_args()
  22. behavior_detail = PROJECT_DIR / "output" / args.run_name / "behavior" / "user_detail.csv"
  23. social_dir = PROJECT_DIR / "output" / args.run_name / "social"
  24. if not args.skip_behavior:
  25. command = [
  26. sys.executable,
  27. str(SCRIPTS_DIR / "run_behavior_features.py"),
  28. "--input", str(args.input),
  29. "--run-name", args.run_name,
  30. "--lookback-days", str(args.lookback_days),
  31. ]
  32. if args.sql_only:
  33. command.append("--sql-only")
  34. run(command)
  35. if args.sql_only:
  36. if behavior_detail.exists() and not args.skip_social:
  37. run(
  38. [
  39. sys.executable,
  40. str(SCRIPTS_DIR / "run_social_features.py"),
  41. "--input", str(args.input),
  42. "--behavior-detail", str(behavior_detail),
  43. "--run-name", args.run_name,
  44. "--sql-only",
  45. ]
  46. )
  47. return
  48. if not args.skip_social:
  49. run(
  50. [
  51. sys.executable,
  52. str(SCRIPTS_DIR / "run_social_features.py"),
  53. "--input", str(args.input),
  54. "--behavior-detail", str(behavior_detail),
  55. "--run-name", args.run_name,
  56. "--lookback-days", str(args.lookback_days),
  57. ]
  58. )
  59. report = PROJECT_DIR / "output" / args.run_name / "risk_user_feature_report.xlsx"
  60. command = [
  61. sys.executable,
  62. str(SCRIPTS_DIR / "build_report.py"),
  63. "--behavior-detail", str(behavior_detail),
  64. "--output", str(report),
  65. ]
  66. if social_dir.exists():
  67. command.extend(["--social-dir", str(social_dir)])
  68. run(command)
  69. if __name__ == "__main__":
  70. main()