Initial commit: Set up Territory Analysis Tool
This commit is contained in:
117
run_all.py
Normal file
117
run_all.py
Normal file
@@ -0,0 +1,117 @@
|
||||
|
||||
import subprocess
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
def run_script(command):
|
||||
"""Runs a script and checks for errors, printing output in real-time."""
|
||||
print(f"Executing: {' '.join(command)}", flush=True)
|
||||
try:
|
||||
process = subprocess.run(
|
||||
command,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True
|
||||
)
|
||||
print("✓ Success!")
|
||||
if process.stdout:
|
||||
print(process.stdout)
|
||||
return process
|
||||
except FileNotFoundError as e:
|
||||
print(f"\nError: Command not found. Ensure Python is in your PATH. Details: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"\nError running command: {' '.join(command)}", file=sys.stderr)
|
||||
print("\n--- STDERR ---", file=sys.stderr)
|
||||
print(e.stderr, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
def process_files(addresses_file, boundaries_file):
|
||||
"""
|
||||
Runs the processing script to generate the 'Final' CSV.
|
||||
Returns the path to the generated file or exits on error.
|
||||
"""
|
||||
print("\n--- Step 1: Processing territory files ---")
|
||||
|
||||
# Check if input files exist
|
||||
if not os.path.exists(addresses_file):
|
||||
print(f"Error: Address file not found at '{addresses_file}'", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
if not os.path.exists(boundaries_file):
|
||||
print(f"Error: Boundaries file not found at '{boundaries_file}'", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
process_command = ["python", "process_territories.py", addresses_file, boundaries_file]
|
||||
run_script(process_command)
|
||||
|
||||
# Find the most recently modified "Final.csv"
|
||||
try:
|
||||
final_files = [f for f in os.listdir('.') if "Final.csv" in f and os.path.isfile(f)]
|
||||
if not final_files:
|
||||
print("Error: No 'Final.csv' file found after processing.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
latest_file = max(final_files, key=os.path.getmtime)
|
||||
print(f"Generated file: {latest_file}")
|
||||
return latest_file
|
||||
except Exception as e:
|
||||
print(f"Error locating processed file: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
def run_analysis(processed_file_path):
|
||||
"""
|
||||
Runs the analysis scripts on the processed file.
|
||||
"""
|
||||
if not processed_file_path or not os.path.exists(processed_file_path):
|
||||
print(f"\nError: Processed file not found at '{processed_file_path}'. Please run the 'process' step first.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
print("\n--- Step 2: Running analysis scripts ---")
|
||||
|
||||
analysis_command = ["python", "analysis.py", processed_file_path]
|
||||
run_script(analysis_command)
|
||||
|
||||
category_analysis_command = ["python", "category_analysis.py", processed_file_path]
|
||||
run_script(category_analysis_command)
|
||||
|
||||
print("\nAnalysis complete!")
|
||||
print("Generated files: analysis.md, map.html, category_map.html")
|
||||
|
||||
def main():
|
||||
"""Parses command-line arguments and orchestrates the workflow."""
|
||||
parser = argparse.ArgumentParser(description="Territory Analysis Tool")
|
||||
subparsers = parser.add_subparsers(dest="command", required=True, help="Available commands")
|
||||
|
||||
# Sub-command for 'process'
|
||||
parser_process = subparsers.add_parser("process", help="Step 1: Process raw address and boundary files into a final CSV.")
|
||||
parser_process.add_argument("--addresses", required=True, help="Path to the addresses CSV file.")
|
||||
parser_process.add_argument("--boundaries", required=True, help="Path to the boundaries CSV file.")
|
||||
|
||||
# Sub-command for 'analyze'
|
||||
parser_analyze = subparsers.add_parser("analyze", help="Step 2: Run analysis on a processed 'Final' CSV file.")
|
||||
parser_analyze.add_argument("--input", required=True, help="Path to the processed 'Final' CSV file.")
|
||||
|
||||
# Sub-command for 'full-run'
|
||||
parser_full_run = subparsers.add_parser("full-run", help="Run the full pipeline: process and then analyze.")
|
||||
parser_full_run.add_argument("--addresses", required=True, help="Path to the addresses CSV file.")
|
||||
parser_full_run.add_argument("--boundaries", required=True, help="Path to the boundaries CSV file.")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.command == "process":
|
||||
process_files(args.addresses, args.boundaries)
|
||||
|
||||
elif args.command == "analyze":
|
||||
run_analysis(args.input)
|
||||
|
||||
elif args.command == "full-run":
|
||||
# Run step 1
|
||||
processed_file = process_files(args.addresses, args.boundaries)
|
||||
# Run step 2
|
||||
run_analysis(processed_file)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Change working directory to the script's directory
|
||||
# This makes file paths relative to the script's location
|
||||
os.chdir(os.path.dirname(os.path.abspath(__file__)))
|
||||
main()
|
||||
Reference in New Issue
Block a user